fix: account failed attempts and stop unchanged task loops in 1.20.1

This commit is contained in:
Agent Workbench Dev
2026-11-01 13:18:03 +08:00
parent 0736eca4cd
commit 441785a8b7
35 changed files with 579 additions and 106 deletions
+30
View File
@@ -107,6 +107,35 @@ export class CheckRunner {
return plan;
}
inputIdentity(session, plan, inspection) {
const describe = file => {
try { const info = fs.statSync(file); return { path: fs.realpathSync(file), bytes: info.size, modified: info.mtimeMs, changed: info.ctimeMs }; }
catch { return { path: file, missing: true }; }
};
const project = this.store.project(session.projectId);
const checks = plan.checks.map(({ id, ...check }) => check);
const npm = checks.some(check => check.engine !== 'python') ? npmInvocation('', this.runtimeDirectory) : null;
// This detects repeated declared inputs. Reusing an old result never
// creates a new verification or claims that arbitrary external state was checked.
return createHash('sha256').update(JSON.stringify({ fingerprint: inspection.fingerprint, checks,
node: { version: process.version, file: describe(process.execPath) }, npm: npm ? describe(npm.args[0]) : null,
dependencies: ['node_modules', 'node_modules/.package-lock.json', '.pnp.cjs', '.venv/pyvenv.cfg'].map(relative => describe(path.join(project.root, relative))),
environment: projectEnvironment() })).digest('hex');
}
async unchangedResult(session, planId) {
const plan = this.planFor(session, planId), inspection = await inspectProject(this.store.project(session.projectId));
if (inspection.fingerprint !== plan.fingerprint) throw conflict('检查计划创建后工程或Git基线已变化,请重新识别再执行。');
if (!inspection.scope.complete) return null;
const identity = this.inputIdentity(session, plan, inspection);
const previous = [...this.store.core.check_job.values()].findLast(job => job.taskId === session.taskId && job.inputIdentity === identity
&& ['passed', 'failed', 'unverified'].includes(job.status) && !job.stale && !job.requiresVerification && job.finishedAt);
if (!previous || !previous.results.length) return null;
// Never hand back missing/corrupt logs as usable historical evidence.
for (const result of previous.results) this.store.artifactStore.read(this.store.owned('artifact', result.logArtifactId, session.id));
return previous;
}
async start(session, planId, { signal, source = 'user' } = {}) {
assertWorkersResolved(this.store, session.projectId);
if (workerForSession(this.store, session)?.status === 'completed') throw conflict('子成果已经封存,不重新运行原检查。');
@@ -144,6 +173,7 @@ export class CheckRunner {
signal.throwIfAborted();
if (before.fingerprint !== plan.fingerprint) throw conflict('检查计划创建后工程或Git基线已变化,请重新识别再执行。');
job.sourceFingerprint = before.sourceFingerprint;
job.inputIdentity = this.inputIdentity(session, plan, before);
const current = createCheckList(before, plan.checks.map(check => check.name));
if (current.some((check, index) => check.script !== plan.checks[index].script || check.command !== plan.checks[index].command)) throw conflict('检查脚本与计划不一致,未执行任何命令。');
job.status = 'running'; session.status = 'running'; task.verification = { status: 'running', checkJobId: job.id };
+25 -7
View File
@@ -65,10 +65,9 @@ export function filePage(file, args, { maximumMessageBytes = 16000, callId = ''
export function recordArtifactRead(task, artifact, result, invocationId, runId = task.activeRunId ?? null) {
const progress = task.readProgress ??= { schemaVersion: 1, reads: [], repeated: 0, changingIndexes: [], warning: null, pause: false };
progress.sessionInspection = null;
const identity = artifact.id + ':' + (artifact.section ? artifact.section + ':' : '') + artifact.sha256, start = result.offset, end = result.next_offset;
let read = progress.reads.find(value => value.identity === identity);
if (!read) { read = { identity, ranges: [] }; progress.reads.push(read); if (progress.reads.length > 128) progress.reads.shift(); }
if (!read) { read = { identity, kind: artifact.kind, ranges: [] }; progress.reads.push(read); if (progress.reads.length > 128) progress.reads.shift(); }
// Historical coverage is an audit of returned pages, not proof that a new
// Run received them in its bounded request. Re-reading different old pages
// in a continuation must not accumulate as three semantic repeats.
@@ -90,7 +89,7 @@ export function recordArtifactRead(task, artifact, result, invocationId, runId =
} else progress.changingIndexes = [];
progress.warning = progress.repeated >= 1 ? '本次运行已经返回该工件区间,请使用已返回内容,或按next_offset读取新的区间。'
: progress.changingIndexes.length >= 3 ? '连续读取更新的历史索引没有推进工程;请直接读取实际结果或项目文件,依据已有信息继续任务。' : null;
progress.pause = progress.repeated >= 3 || progress.changingIndexes.length >= 6;
progress.pause = progress.checkPause === true || progress.repeated >= 3 || progress.changingIndexes.length >= 6;
progress.last = { artifactId: artifact.id, sha256: artifact.sha256, offset: start, nextOffset: end, invocationId,
contentSha256: createHash('sha256').update(result.content, 'utf8').digest('hex') };
return progress;
@@ -100,6 +99,25 @@ export function clearReadStreak(task) {
if (!task.readProgress) return;
task.readProgress.repeated = 0; task.readProgress.changingIndexes = []; task.readProgress.warning = null; task.readProgress.pause = false;
task.readProgress.sessionInspection = null;
task.readProgress.observations = [];
task.readProgress.checkPause = false;
}
export function recordFileRead(task, result, invocationId, runId) {
return recordArtifactRead(task, { id: 'file:' + result.path + ':' + result.range_start_line + ':' + result.range_end_line,
sha256: result.sha256, kind: 'project-file' }, result, invocationId, runId);
}
export function recordUnchangedCheck(task, job, runId) {
const progress = task.readProgress ??= { schemaVersion: 1, reads: [], repeated: 0, changingIndexes: [], warning: null, pause: false };
const observations = progress.observations ??= [];
let value = observations.find(value => value.runId === runId && value.key === 'check:' + job.inputIdentity);
if (!value) { value = { runId, key: 'check:' + job.inputIdentity, count: 0 }; observations.push(value); if (observations.length > 64) observations.shift(); }
value.count++;
progress.warning = '所选检查的已记录输入未变化,已返回原检查日志,没有再次执行。请依据失败修复源码;确需重跑可由用户在工程检查中确认。';
progress.checkPause ||= value.count >= 2;
progress.pause ||= progress.checkPause;
return progress;
}
export function recordBrowserRead(task, result, invocationId, runId = task.activeRunId ?? null) {
@@ -115,14 +133,14 @@ export function recordSessionInspection(task, result) {
const execution = result.execution ?? {};
// Revision, request usage and tool counters change merely by inspecting.
// They must not make identical engineering state appear to be progress.
const state = { sessionId: result.session?.id, status: result.session?.status,
const state = { sessionId: result.session?.id,
lastFailure: result.lastFailure, changes: result.changes, approvals: result.pendingApprovals,
verification: execution.task?.verification,
verification: execution.task?.verification && { status: execution.task.verification.status, sourceFingerprint: execution.task.verification.sourceFingerprint },
taskPlanId: execution.task?.currentTaskPlanId,
taskProofs: execution.taskProofs?.map(proof => ({ id: proof.id, planId: proof.planId, stepKey: proof.stepKey, status: proof.status })),
browserJobs: execution.browserJobs?.map(job => ({ id: job.id, planId: job.planId, status: job.status, resultSha256: job.resultSha256, assertions: job.assertions })),
needsVerification: execution.progress?.requiresVerification,
checks: execution.checks?.map(job => ({ id: job.id, status: job.status, stale: job.stale,
checks: execution.checks?.map(job => ({ inputIdentity: job.inputIdentity, fingerprint: job.fingerprint, status: job.status, stale: job.stale,
results: job.results.map(check => ({ name: check.name, status: check.status, exitCode: check.exitCode, tests: check.tests })) })),
changeSets: execution.changeSets?.map(set => ({ id: set.id, status: set.status, requiresVerification: set.requiresVerification })),
deliveries: execution.deliveries?.map(job => ({ id: job.id, status: job.status, commit: job.commit })) };
@@ -131,6 +149,6 @@ export function recordSessionInspection(task, result) {
const consecutive = previous?.signature === signature ? previous.consecutive + 1 : 1;
progress.sessionInspection = { signature, consecutive };
progress.warning = consecutive >= 2 ? '连续核对的工程状态没有变化,请使用已经返回的状态与源码推进当前任务。' : null;
progress.pause = consecutive >= 3;
progress.pause = progress.checkPause === true || consecutive >= 3 || progress.repeated >= 3;
return progress;
}
+10 -2
View File
@@ -208,6 +208,11 @@ class EngineRunDriver {
request.eventId = modelEvent.id; request.status = 'dispatched'; request.dispatchedAt = at(); this.job.status = 'running'; this.save('model_intent');
dispatched = true;
const response = await this.provider.complete(selected, messages, this.run.project, signal, progress => {
if (progress.consumption) {
const recorded = this.store.recordConsumption(this.session, this.run.record, { ...progress.consumption, requestId: request.id }, estimate);
if (recorded) request.accountedAttempts++;
this.save('model_consumption'); return;
}
if (progress.phase === 'running') {
const attempt = progress.attempt ?? 1;
if (!Number.isSafeInteger(attempt) || attempt < 1 || attempt > request.maxAttempts) throw engineError('模型尝试超出原引擎封存预算。');
@@ -225,8 +230,11 @@ class EngineRunDriver {
while (request.accountedAttempts < count - 1) {
request.accountedAttempts++; this.store.recordResponse(this.session, this.run.record, null, estimate);
}
request.accountedAttempts++; request.usage = response.usage ?? null;
this.store.recordResponse(this.session, this.run.record, response.usage, estimate);
request.usage = response.usage ?? null;
if (request.accountedAttempts < count) {
request.accountedAttempts++;
this.store.recordResponse(this.session, this.run.record, response.usage, estimate);
}
this.save('model_usage');
const output = JSON.parse(this.provider.redact(JSON.stringify(response))), saved = this.store.artifactStore.write(this.session, this.run.record, output, { kind: 'engine-model-output' });
this.store.core.artifact.set(saved.id, saved); request.responseArtifactId = saved.id; request.responseSha256 = saved.sha256;
+9 -1
View File
@@ -309,6 +309,13 @@ export class ModelHub {
}
this.attempts.push(attempt); this.saveAttempts();
const progress = event => {
if (event.consumption) {
attempt.usage = event.consumption.usage; attempt.usageStatus = event.consumption.usageStatus;
onProgress?.({ consumption: { ...event.consumption, wireAttemptId: event.consumption.attemptId, attemptId: attempt.id,
connectionId: member.id, connectionName: member.name }, connectionId: member.id, model: member.model, modelAttemptId: attempt.id });
this.saveAttempts();
return;
}
if (event.streamCapture !== undefined) {
attempt.stream = event.stream;
if (plan?.sessionId) {
@@ -326,7 +333,8 @@ export class ModelHub {
onProgress?.({ ...event, attempt: count, maxAttempts: routing.maxAttempts, connectionId: member.id, connectionName: member.name, protocol: modelProtocol(member.protocol), model: member.model, modelAttemptId: attempt.id });
};
const result = await execute({ ...config, baseUrl: member.baseUrl, model: member.model, protocol: modelProtocol(member.protocol), streaming: member.streaming === true }, credential?.key ?? '', actualBody, progress);
attempt.status = 'completed'; attempt.usage = result.usage ?? null; attempt.usageStatus = result.usage ? 'reported' : 'unknown';
attempt.status = 'completed'; attempt.usage = result.usage ?? attempt.usage; attempt.usageStatus = attempt.usage ? 'reported' : 'unknown';
if (result.compatibility?.length) attempt.compatibility = result.compatibility;
this.health.set(this.identity(member), { state: 'responded', lastSuccessAt: stamp(), cooldownUntil: 0, failures: 0 });
return result;
} catch (cause) {
+8 -5
View File
@@ -1,9 +1,9 @@
import { createHash } from 'node:crypto';
import { protocolFailure, normalizeProtocolResponse, validateModelResponse } from './protocols.mjs';
import { protocolFailure, normalizeProtocolResponse, validateModelResponse, reportedUsage } from './protocols.mjs';
// Only a fully terminated and validated stream produces a dispatchable message.
// Progress is cumulative, bounded and explicitly not a tool invocation.
export async function readModelStream(response, config, signal, onProgress) {
export async function readModelStream(response, config, signal, onProgress, tools) {
if (!response.headers.get('content-type')?.toLowerCase().includes('text/event-stream') || !response.body) throw protocolFailure('服务未返回所选协议的事件流,请核对流式配置。');
const reader = response.body.getReader(), decoder = new TextDecoder('utf-8', { fatal: true }), hash = createHash('sha256');
const chunks = [];
@@ -53,7 +53,10 @@ export async function readModelStream(response, config, signal, onProgress) {
} else if (event.type === 'response.completed') {
if (!event.response || responseId && event.response.id !== responseId) throw protocolFailure('Responses终结身份与创建身份不一致。');
terminal = event.response;
} else if (['response.failed', 'response.incomplete', 'response.cancelled'].includes(event.type)) throw protocolFailure('Responses流未完整结束,工具未执行。', 'stream_interrupted');
} else if (['response.failed', 'response.incomplete', 'response.cancelled'].includes(event.type)) {
usage = event.response?.usage ?? usage;
throw protocolFailure('Responses流未完整结束,工具未执行。', 'stream_interrupted');
}
} else {
if (!Array.isArray(event.choices)) throw protocolFailure('Chat流缺少choices数组。');
if (event.usage !== undefined && event.usage !== null) usage = event.usage;
@@ -125,13 +128,13 @@ export async function readModelStream(response, config, signal, onProgress) {
if (!done || !['stop', 'tool_calls'].includes(finish)) throw protocolFailure('Chat流未完整终结或达到输出上限,未执行工具。', 'stream_interrupted');
value = { choices: [{ message: { role: 'assistant', content: text, ...(calls.size ? { tool_calls: [...calls.entries()].sort((a, b) => a[0] - b[0]).map(([, item]) => item) } : {}) }, finish_reason: finish }], usage };
}
validateModelResponse(config.protocol === 'responses' ? normalizeProtocolResponse(config, value) : value);
validateModelResponse(config.protocol === 'responses' ? normalizeProtocolResponse(config, value) : value, tools);
validated = true; progress(true);
return { value, evidence: { bytes, frames, sha256: hash.copy().digest('hex'), terminal: true, protocol: config.protocol ?? 'chat-completions' } };
} finally {
if (progressTimer) clearTimeout(progressTimer);
try { await reader.cancel(); } catch {} reader.releaseLock();
onProgress?.({ streamCapture: Buffer.concat(chunks), stream: { bytes, capturedBytes: chunks.reduce((sum, item) => sum + item.length, 0), frames,
onProgress?.({ streamCapture: Buffer.concat(chunks), reportedUsage: reportedUsage(config, { usage: terminal?.usage ?? usage }), stream: { bytes, capturedBytes: chunks.reduce((sum, item) => sum + item.length, 0), frames,
sha256: hash.digest('hex'), terminal: validated, terminalSeen: !!terminal || done, protocol: config.protocol ?? 'chat-completions' } });
}
}
+26 -3
View File
@@ -3,7 +3,26 @@ export const protocolFailure = (message, kind = 'protocol') => new ModelRequestE
title: '模型响应未通过完整性核对', message, retryable: false, occurredAt: new Date().toISOString() });
export const protocolRoute = config => config.protocol === 'responses' ? '/responses' : '/chat/completions';
export function validateModelResponse(value) {
export function reportedUsage(config, value) {
const raw = value?.usage;
if (!raw || typeof raw !== 'object') return null;
const input = config.protocol === 'responses' ? raw.input_tokens : raw.prompt_tokens;
const output = config.protocol === 'responses' ? raw.output_tokens : raw.completion_tokens;
const valid = number => Number.isSafeInteger(number) && number >= 0;
if (!valid(raw.total_tokens) && !(valid(input) && valid(output))) return null;
return { ...(valid(input) ? { prompt_tokens: input } : {}), ...(valid(output) ? { completion_tokens: output } : {}),
total_tokens: valid(raw.total_tokens) ? raw.total_tokens : input + output };
}
function acceptsEmptyObject(tools, name) {
const tool = tools?.find(tool => (tool.function?.name ?? tool.name) === name);
const schema = tool?.function?.parameters ?? tool?.parameters;
// Restrict adaptation to schemas whose empty-object validity is evident.
return schema?.type === 'object' && !schema.required?.length && Object.keys(schema).every(key =>
['type', 'properties', 'required', 'additionalProperties', 'description', 'title', '$schema'].includes(key));
}
export function validateModelResponse(value, tools) {
const choice = value.choices?.[0], message = choice?.message;
if (!message || choice.finish_reason === 'length' || choice.finish_reason === 'content_filter') throw protocolFailure('模型响应缺少完整message或没有完整结束,未执行工具。');
const calls = message.tool_calls;
@@ -12,8 +31,12 @@ export function validateModelResponse(value) {
for (const call of calls ?? []) {
if (!call.id || typeof call.id !== 'string' || ids.has(call.id) || call.type !== 'function' || typeof call.function?.name !== 'string' || !call.function.name || typeof call.function.arguments !== 'string') throw protocolFailure('模型工具身份或参数无效。');
ids.add(call.id);
let args; try { args = JSON.parse(call.function.arguments); } catch { throw protocolFailure('模型工具参数不是完整JSON,未执行工具。'); }
if (!args || typeof args !== 'object' || Array.isArray(args)) throw protocolFailure('模型工具参数必须为完整对象。');
if (call.function.arguments.trim() === '' && acceptsEmptyObject(tools, call.function.name)) {
(value.compatibility ??= []).push({ kind: 'empty_object_arguments', callId: call.id, tool: call.function.name, originalArguments: call.function.arguments, arguments: '{}' });
call.function.arguments = '{}';
}
let args; try { args = JSON.parse(call.function.arguments); } catch { throw protocolFailure('工具 ' + call.function.name + '(' + call.id + ')参数不是完整JSON,未执行这组工具。'); }
if (!args || typeof args !== 'object' || Array.isArray(args)) throw protocolFailure('工具 ' + call.function.name + '(' + call.id + ')参数必须为完整对象,未执行这组工具。');
}
return value;
}