feat(runtime): observe turn evidence without changing execution controls

This commit is contained in:
Agent Workbench Dev
2026-11-04 11:26:47 +08:00
parent 785dd0c54d
commit 6fe04042c5
291 changed files with 66740 additions and 3 deletions
+19
View File
@@ -41,6 +41,7 @@ import { BrowserManager, publicBrowserPlan } from '../core/browser/browser-manag
import { browserPlanInput } from '../shared/contracts/browser.mjs';
import { TaskPlanManager } from '../core/tasks/task-plan-manager.mjs';
import { RequirementReview } from '../core/tasks/requirement-review.mjs';
import { recordTurnProgress } from '../core/context/turn-progress-recorder.mjs';
import { FileResults } from '../core/tasks/file-results.mjs';
import { fileResultInput, fileResultToolDefinitions } from '../shared/contracts/file-results.mjs';
import { verifiedCheck } from '../core/delivery-report.mjs';
@@ -314,6 +315,7 @@ export class Runtime {
async run(session, config, run) {
const signal = run.controller.signal;
let projectionLogged = false;
let currentStep = null;
try {
// Immediate providers and local tools still need to deliver UI events and
// process stop/deadline input between completed, persisted operations.
@@ -325,10 +327,12 @@ export class Runtime {
if (current.sourceFingerprint === originalTask.stagnation.sourceFingerprint) throw new BudgetExceeded('no_progress', '连续两次运行未增加源码改动、检查结果或新的读取依据。本次未请求模型;请根据原日志修改源码或发送具体调整,避免重复空续接。');
originalTask.stagnation = null;
}
recordTurnProgress(this.store, session, run.record, null, 'baseline');
for (let step = 0; step < config.maxSteps; step++) {
signal.throwIfAborted();
checkBudget(run.record, this.store.core.task.get(run.taskId));
const stepRecord = this.store.beginStep(session, run.record);
currentStep = stepRecord;
const task = this.store.core.task.get(run.taskId);
config.inputImages=this.inputAttachments().images(session,task);
if(config.inputImages.length&&config.mode==='demo')throw new Error('固定演示执行器不会读取图片;请连接并声明支持视觉的模型。');
@@ -546,6 +550,8 @@ export class Runtime {
}
else if (signal.aborted) result = { status: 'interrupted', message: '用户停止了任务,这次调用没有执行。' };
else result = await this.executeTool(session, call, run);
const invocation = [...this.store.core.tool.values()].findLast(row => row.runId === run.id && row.callId === call.id);
if (invocation?.resultArtifactId) (stepRecord.progressToolIds ??= []).push(invocation.id);
spendReadBudget(run.readBatchBudget, call, result);
session.providerMessages.push({ role: 'tool', tool_call_id: call.id, content: JSON.stringify(result) });
if (call.function.name === 'prepare_change_set' && result.status === 'awaiting_review') preparedSets.add(result.change_set_id);
@@ -587,6 +593,8 @@ export class Runtime {
this.store.save(session, { type: 'change_set.awaiting_review', detail: { changeSetId: id } });
return;
}
stepRecord.progressSettled = true;
recordTurnProgress(this.store, session, run.record, stepRecord);
if (this.store.core.task.get(run.taskId).readProgress?.pause) throw new BudgetExceeded('no_progress',
'模型连续重复读取工件或核对相同的工程状态,未推进当前任务。实际结果和原任务已保存,请核对后继续。');
}
@@ -626,6 +634,10 @@ export class Runtime {
}
this.store.save(session);
} finally {
if (currentStep && !currentStep.progressSettled) {
currentStep.progressSettled = true;
recordTurnProgress(this.store, session, run.record, currentStep, 'settled', session.lastFailure?.runId === run.id ? session.lastFailure : null);
}
if (run.worker?.engine) await this.engineRunner().finish(session, run);
}
}
@@ -635,14 +647,17 @@ export class Runtime {
?? this.store.event(session, { kind: 'tool', toolCallId: call.id, tool, label: labels[tool] ?? tool, phase: 'queued', startedAt: now() });
this.store.beginInvocation(session, call, run.record, event);
let result;
let progressFailureCategory = 'invalid_input';
try {
const args = JSON.parse(call.function.arguments);
validateToolArgs(tool, args);
progressFailureCategory = 'permission';
if (!taskToolAllowed(this.store.core.task.get(run.taskId), run.record, tool, args)) throw new Error(run.record.purpose === 'diagnostic'
? '本次是只读运行诊断,不执行文件修改、命令或其他副作用操作。原任务目标保持。' : '原任务明确无需测试;这项自动检查没有执行。请直接完成原目标,追加测试需要用户明确要求。');
assertWorkerTool(this.store, session, tool, args);
if (run.worker?.engine) this.engineRunner().assertCurrent(session, run);
event.input = JSON.parse(this.provider.redact(JSON.stringify(args)));
progressFailureCategory = 'tool_or_environment';
const proofHint = !!this.store.core.task.get(run.taskId)?.currentTaskPlanId && PLAN_READ_TOOLS.includes(tool);
const readMetadata = { ...(proofHint ? { tool_invocation_id: event.invocationId } : {}),
...(tool === 'read_browser_result' ? { repeated_read_count: Number.MAX_SAFE_INTEGER, reading_hint: BROWSER_READ_HINT } : {}) };
@@ -780,6 +795,7 @@ export class Runtime {
...(check.python ? { python: { runtime: check.python.runtime, mode: check.python.mode } } : {}) })),
reason: '执行工程检查计划 ' + plan.id.slice(0, 8) + ';保存测试数量、退出码和日志。' }, run, async () => {
const { promise } = await this.checkRunner().start(session, plan.id, { signal: run.controller.signal, source: 'agent' });
recordTurnProgress(this.store, session, run.record, [...this.store.core.step.values()].findLast(row => row.runId === run.id), 'waiting');
const job = await promise;
return { status: job.status === 'cancelled' ? 'interrupted' : job.status === 'passed' ? 'completed' : job.status,
check_status: job.status, job_id: job.id, plan_id: job.planId, results: job.results, summary: job.summary,
@@ -814,6 +830,8 @@ export class Runtime {
event.output = this.provider.redact(typeof result.stdout === 'string' ? result.stdout + (result.stderr ? '\n' + result.stderr : '') : JSON.stringify(result).slice(0, 16000));
if (result.exit_code !== undefined) event.exitCode = result.exit_code;
} catch (error) {
const record = this.store.core.tool.get(event.invocationId);
if (record) record.progressFailureCategory = progressFailureCategory;
result = { status: run.controller.signal.aborted ? 'interrupted' : 'error', message: this.provider.redact(error.message ?? String(error)) };
event.phase = result.status === 'interrupted' ? 'interrupted' : 'failed';
event.output = result.message;
@@ -837,6 +855,7 @@ export class Runtime {
session.approvals.push(approval);
event.phase = 'waiting'; session.status = 'awaiting_approval';
this.store.invocationState(session, event, 'waiting');
recordTurnProgress(this.store, session, run.record, [...this.store.core.step.values()].findLast(row => row.runId === run.id), 'waiting');
const decision = await new Promise(resolve => {
const signal = run.controller.signal;
const settle = value => { this.waiters.delete(approval.id); signal.removeEventListener('abort', cancel); resolve(value); };