fix: account failed attempts and stop unchanged task loops in 1.20.1
This commit is contained in:
+42
-11
@@ -17,7 +17,7 @@ import { ProjectIndex } from '../core/context/project-index.mjs';
|
||||
import { RequestPlanner, verifyPlanEvidence } from '../core/context/request-plan.mjs';
|
||||
import { InputAttachments } from '../core/context/input-attachments.mjs';
|
||||
import { BudgetResume } from '../core/context/budget-resume.mjs';
|
||||
import { artifactPage, filePage, readMessageBudget, recordArtifactRead, recordBrowserRead, clearReadStreak, recordSessionInspection, readMetadataReserve, BROWSER_READ_HINT } from '../core/context/read-budget.mjs';
|
||||
import { artifactPage, filePage, readMessageBudget, recordArtifactRead, recordBrowserRead, recordFileRead, recordUnchangedCheck, clearReadStreak, recordSessionInspection, readMetadataReserve, BROWSER_READ_HINT } from '../core/context/read-budget.mjs';
|
||||
import { ChangeSetManager } from '../core/workspaces/change-sets.mjs';
|
||||
import { assertChangeSetsResolved } from '../shared/contracts/change-sets.mjs';
|
||||
import { pendingDeliveryReview } from '../shared/contracts/delivery-review.mjs';
|
||||
@@ -207,11 +207,16 @@ export class Runtime {
|
||||
session.failure = null;
|
||||
this.store.save(session);
|
||||
const deadline = record.budget.maxDurationMs > 0 ? setTimeout(() => controller.abort(new BudgetExceeded('time_limit', '已到达本次运行的时间预算,执行记录已保存。')), record.budget.maxDurationMs) : null;
|
||||
run.promise = this.run(session, config, run).finally(() => {
|
||||
clearTimeout(deadline); this.active.delete(id);
|
||||
const status = session.status;
|
||||
if (record.purpose === 'diagnostic') session.status = record.taskStatusBefore;
|
||||
this.store.finishRun(session, record, run.stopReason ?? null, { status });
|
||||
run.promise = this.run(session, config, run).finally(async () => {
|
||||
clearTimeout(deadline);
|
||||
try {
|
||||
if (!run.worker && record.purpose !== 'diagnostic' && ['no_progress', 'step_limit'].includes(run.stopReason)) {
|
||||
try { record.finalSourceFingerprint = (await inspectProject(project)).sourceFingerprint; } catch { /* No fingerprint means no claim of unchanged inputs. */ }
|
||||
}
|
||||
const status = session.status;
|
||||
if (record.purpose === 'diagnostic') session.status = record.taskStatusBefore;
|
||||
this.store.finishRun(session, record, run.stopReason ?? null, { status });
|
||||
} finally { this.active.delete(id); }
|
||||
});
|
||||
run.promise.catch(() => {});
|
||||
return session;
|
||||
@@ -244,6 +249,12 @@ export class Runtime {
|
||||
// process stop/deadline input between completed, persisted operations.
|
||||
await yieldExecution(undefined, { signal });
|
||||
await verifyWorkspaceProject(this.store, run.project);
|
||||
const originalTask = this.store.core.task.get(run.taskId);
|
||||
if (!run.worker && run.record.purpose !== 'diagnostic' && originalTask.stagnation?.count >= 2) {
|
||||
const current = await inspectProject(run.project);
|
||||
if (current.sourceFingerprint === originalTask.stagnation.sourceFingerprint) throw new BudgetExceeded('no_progress', '连续两次运行未增加源码改动、检查结果或新的读取依据。本次未请求模型;请根据原日志修改源码或发送具体调整,避免重复空续接。');
|
||||
originalTask.stagnation = null;
|
||||
}
|
||||
for (let step = 0; step < config.maxSteps; step++) {
|
||||
signal.throwIfAborted();
|
||||
checkBudget(run.record, this.store.core.task.get(run.taskId));
|
||||
@@ -262,7 +273,7 @@ export class Runtime {
|
||||
}) : '') + '\n当前项目/会话/任务身份:' + JSON.stringify({
|
||||
project_id: run.project.id, session_id: session.id, task_id: task.id, workspace: projectWorkspaceIdentity(run.project) }) + '\n当前Task文件提案状态:' + JSON.stringify(state)
|
||||
+ '\n当前任务目标与全部追加要求(用户任务数据,完整保留):\n' + JSON.stringify({
|
||||
goal: taskContract(task).goal, instructions: task.instructions, constraints: task.constraints ?? [], acceptance: task.acceptance ?? [], contract: taskContract(task),
|
||||
goal: taskContract(task).goal, instructions: task.instructions.filter(instruction => instruction.source !== 'review-action'), constraints: task.constraints ?? [], acceptance: task.acceptance ?? [], contract: taskContract(task),
|
||||
}) + (run.record.purpose === 'diagnostic' ? '\n本次仅回答运行诊断问题,允许读取所属结果;不得修改文件、执行命令或将解释视为原任务交付。诊断问题:' + run.record.question : '')
|
||||
+ '\n结果未知的调用,不得假定成功或重放:' + JSON.stringify(unknown)
|
||||
+ '\n' + sessionContext(session, run.project, config, value => this.provider.redact(value))
|
||||
@@ -298,6 +309,12 @@ export class Runtime {
|
||||
if (run.worker && !externalEngine) { planned.record.workerDispatchedAt = now(); this.store.save(session, { type: 'worker.request_intent' }); }
|
||||
requestStarted = true;
|
||||
const progressHandler = progress => {
|
||||
if (progress.consumption) {
|
||||
this.store.recordConsumption(session, run.record, { ...progress.consumption, requestId: planned.record.id }, planned.record.estimatedInputTokens + config.maxOutputTokens);
|
||||
responseAccounted = true;
|
||||
if (run.worker) { planned.record.workerUsageAccounted = true; this.store.save(session); }
|
||||
return;
|
||||
}
|
||||
if (!requestEvent) requestEvent = this.store.event(session, { kind: externalEngine ? 'engine' : 'model', runId: run.id,
|
||||
source: externalEngine ? 'controlled_engine' : 'model_connection', label: externalEngine ? '受控引擎执行' : '请求模型服务', phase: 'running', startedAt: now(), contextPlanId: planned.record.id });
|
||||
if (progress.engineRunId) requestEvent.engineRunId = progress.engineRunId;
|
||||
@@ -324,8 +341,9 @@ export class Runtime {
|
||||
response = externalEngine
|
||||
? await this.engineRunner().complete(session, run, planned.messages, progressHandler, planned.record, tools)
|
||||
: await this.provider.complete(config, planned.messages, run.project, signal, progressHandler, planned.record, tools);
|
||||
if (response.compatibility?.length && requestEvent) { requestEvent.compatibility = response.compatibility; this.store.save(session); }
|
||||
this.requestPlanner().finish(session, planned.record, { response, attempts: planned.record.requestAttempts });
|
||||
if (run.worker && !externalEngine) {
|
||||
if (run.worker && !externalEngine && !responseAccounted) {
|
||||
this.store.recordResponse(session, run.record, response.usage, planned.record.estimatedInputTokens + Math.ceil(Buffer.byteLength(JSON.stringify(response.message), 'utf8') / 3) + 8);
|
||||
planned.record.workerUsageAccounted = true; this.store.save(session); responseAccounted = true;
|
||||
}
|
||||
@@ -361,6 +379,10 @@ export class Runtime {
|
||||
this.store.finishStep(session, stepRecord);
|
||||
session.status = session.changes.some(change => ['pending', 'conflict'].includes(change.status)) ? 'needs_review' : 'completed';
|
||||
if (run.record.purpose === 'diagnostic') { session.status = 'completed'; run.stopReason = 'diagnostic_answered'; }
|
||||
else if (session.status === 'completed' && taskContract(task).kind === 'file' && !session.changes.some(change => change.taskId === task.id && change.status === 'applied')) {
|
||||
session.status = 'paused'; run.stopReason = 'delivery_incomplete';
|
||||
this.store.message(session, 'system', '答复已收到,但原目标要求的文件尚无已应用记录,不能视为文件交付完成。请生成可审查的文件改动。');
|
||||
}
|
||||
else if (session.status === 'completed' && taskContract(task).checks !== 'skip' && (['failed', 'stale', 'unknown', 'unverified', 'running'].includes(task.verification?.status)
|
||||
|| taskContract(task).checks === 'required' && task.verification?.status !== 'passed')) {
|
||||
session.status = 'paused'; run.stopReason = 'verification_incomplete';
|
||||
@@ -571,7 +593,13 @@ export class Runtime {
|
||||
}
|
||||
else if (tool === 'run_checks') {
|
||||
const plan = this.checkRunner().planFor(session, args.plan_id);
|
||||
result = await this.requestCommand(session, event, { command: plan.checks.map(check => check.command).join('\n'), checkPlanId: plan.id,
|
||||
const previous = await this.checkRunner().unchangedResult(session, plan.id);
|
||||
if (previous) {
|
||||
recordUnchangedCheck(this.store.core.task.get(run.taskId), previous, run.id);
|
||||
result = { status: previous.status === 'passed' ? 'completed' : previous.status, check_status: previous.status, job_id: previous.id,
|
||||
plan_id: previous.planId, results: previous.results, summary: previous.summary, reused: true, verification_refreshed: false,
|
||||
message: '当前已记录工程、命令和运行环境输入与原检查一致;返回原结果,未再次执行或刷新核验。请修复失败;用户可在工程检查中显式重跑。' };
|
||||
} else result = await this.requestCommand(session, event, { command: plan.checks.map(check => check.command).join('\n'), checkPlanId: plan.id,
|
||||
checkScripts: plan.checks.map(check => ({ name: check.name, script: check.script, lifecycle: check.lifecycle,
|
||||
...(check.python ? { python: { runtime: check.python.runtime, mode: check.python.mode } } : {}) })),
|
||||
reason: '执行工程检查计划 ' + plan.id.slice(0, 8) + ';保存测试数量、退出码和日志。' }, run, async () => {
|
||||
@@ -583,7 +611,10 @@ export class Runtime {
|
||||
});
|
||||
}
|
||||
else if (tool === 'list_files') result = listFiles(run.project, args.path, args.depth ?? 3, 500);
|
||||
else if (tool === 'read_file') result = filePage(readLines(run.project, args), args, { callId: call.id, maximumMessageBytes });
|
||||
else if (tool === 'read_file') {
|
||||
result = filePage(readLines(run.project, args), args, { callId: call.id, maximumMessageBytes });
|
||||
recordFileRead(this.store.core.task.get(run.taskId), result, event.invocationId, run.id);
|
||||
}
|
||||
else if (tool === 'search_files') result = searchFiles(run.project, args);
|
||||
else if (tool === 'propose_file') result = proposeFile(this.store, session, args);
|
||||
else if (tool === 'propose_patch') result = proposePatch(this.store, session, args);
|
||||
@@ -592,7 +623,7 @@ export class Runtime {
|
||||
if (proofHint) result.tool_invocation_id = event.invocationId;
|
||||
event.phase = ['denied', 'interrupted', 'unknown', 'unverified', 'stale'].includes(result.status) ? result.status : ['failed', 'error'].includes(result.status) ? 'failed' : 'completed';
|
||||
if (tool === 'inspect_session') recordSessionInspection(this.store.core.task.get(run.taskId), result);
|
||||
else if (!['read_artifact', 'read_browser_result', 'read_registered_skill'].includes(tool) && !['error', 'interrupted', 'unknown', 'denied'].includes(result.status)) clearReadStreak(this.store.core.task.get(run.taskId));
|
||||
else if (['propose_file', 'propose_patch', 'verify_task_step'].includes(tool) && !['error', 'failed', 'interrupted', 'unknown', 'denied'].includes(result.status)) clearReadStreak(this.store.core.task.get(run.taskId));
|
||||
if (typeof result.stdout === 'string') { result.stdout = this.provider.redact(result.stdout); result.stderr = this.provider.redact(result.stderr ?? ''); }
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user