fix: account failed attempts and stop unchanged task loops in 1.20.1
This commit is contained in:
+28
-2
@@ -363,7 +363,7 @@ export class Store extends EventEmitter {
|
||||
session.revision = (session.revision ?? 0) + 1;
|
||||
session.updatedAt = now();
|
||||
const task = this.core.task.get(session.taskId), run = this.core.run.get(session.runId);
|
||||
if (task) { if (run?.purpose !== 'diagnostic') task.status = session.status; task.updatedAt = session.updatedAt; }
|
||||
if (task) { if (run?.purpose !== 'diagnostic' || run.finishedAt) task.status = session.status; task.updatedAt = session.updatedAt; }
|
||||
if (run && !run.finishedAt) run.status = session.status;
|
||||
let committed;
|
||||
try { committed = this.storage.save(session, this.recordsFor(session), type, { status: session.status, ...detail }); }
|
||||
@@ -476,10 +476,11 @@ export class Store extends EventEmitter {
|
||||
} else if (content && purpose !== 'diagnostic' && (!resume || instructionSource)) {
|
||||
task.instructions.push({ id: randomUUID(), content, createdAt: now(), source: instructionSource ?? 'user' });
|
||||
updateTaskContract(task, content);
|
||||
if (!continuing && !instructionSource) task.stagnation = null;
|
||||
}
|
||||
task.contract ??= taskContract(task, id => this.core.task.get(id));
|
||||
if (task.readProgress) {
|
||||
task.readProgress.repeated = 0; task.readProgress.changingIndexes = []; task.readProgress.warning = null; task.readProgress.pause = false;
|
||||
task.readProgress.repeated = 0; task.readProgress.changingIndexes = []; task.readProgress.warning = null; task.readProgress.pause = false; task.readProgress.checkPause = false;
|
||||
task.readProgress.sessionInspection = null;
|
||||
}
|
||||
const workspaceIdentity = projectWorkspaceIdentity(this.project(session.projectId));
|
||||
@@ -526,8 +527,32 @@ export class Store extends EventEmitter {
|
||||
session.usage = usage;
|
||||
}
|
||||
|
||||
recordConsumption(session, run, receipt, estimate = 0) {
|
||||
if (!receipt?.attemptId || typeof receipt.attemptId !== 'string') throw new Error('模型消费记录缺少实际尝试身份。');
|
||||
const receipts = run.consumption ??= [];
|
||||
if (receipts.some(value => value.attemptId === receipt.attemptId)) return false;
|
||||
receipts.push({ attemptId: receipt.attemptId, requestId: receipt.requestId ?? null,
|
||||
connectionId: receipt.connectionId ?? null, connectionName: receipt.connectionName ?? null,
|
||||
model: receipt.model, endpoint: receipt.endpoint, protocol: receipt.protocol,
|
||||
startedAt: receipt.startedAt, finishedAt: receipt.finishedAt, outcome: receipt.outcome,
|
||||
usageStatus: receipt.usage ? 'reported' : 'unknown', usage: receipt.usage ?? null,
|
||||
estimatedUnknownTokens: receipt.usage ? 0 : Math.max(0, Math.ceil(estimate)) });
|
||||
this.recordResponse(session, run, receipt.usage, estimate);
|
||||
this.save(session, { type: 'model.consumption', detail: { attemptId: receipt.attemptId, usageStatus: receipt.usage ? 'reported' : 'unknown' } });
|
||||
return true;
|
||||
}
|
||||
|
||||
finishRun(session, run, stopReason = null, { status = session.status } = {}) {
|
||||
run.status = status; run.finishedAt = now(); run.stopReason = stopReason;
|
||||
const task = this.core.task.get(run.taskId);
|
||||
if (run.purpose !== 'diagnostic' && !task.workerId && ['no_progress', 'step_limit'].includes(stopReason) && run.finalSourceFingerprint) {
|
||||
const checks = [...this.core.check_job.values()].filter(job => job.taskId === task.id).map(job => ({ input: job.inputIdentity ?? job.sourceFingerprint, status: job.status }));
|
||||
const signature = createHash('sha256').update(JSON.stringify({ source: run.finalSourceFingerprint,
|
||||
changes: session.changes.filter(change => change.taskId === task.id).map(change => ({ path: change.path, sha256: change.sha256, status: change.status })),
|
||||
checks: [...new Map(checks.map(check => [JSON.stringify(check), check])).values()],
|
||||
reads: (task.readProgress?.reads ?? []).filter(read => !read.kind?.startsWith('context-')) })).digest('hex');
|
||||
task.stagnation = { signature, sourceFingerprint: run.finalSourceFingerprint, count: task.stagnation?.signature === signature ? task.stagnation.count + 1 : 1, runId: run.id };
|
||||
} else if (run.purpose !== 'diagnostic') task.stagnation = null;
|
||||
const step = this.core.step.get(run.checkpoint.stepId);
|
||||
if (step && ['running', 'waiting'].includes(step.status)) { step.status = session.status === 'completed' || session.status === 'needs_review' ? 'completed' : 'interrupted'; step.finishedAt = now(); }
|
||||
this.save(session, { type: 'run.finished', detail: { stopReason } });
|
||||
@@ -642,6 +667,7 @@ export class Store extends EventEmitter {
|
||||
status: value.status, startedAt: value.startedAt, finishedAt: value.finishedAt, steps: value.steps, toolCalls: value.toolCalls,
|
||||
usage: value.usage, stopReason: value.stopReason, model: value.model, budget: value.budget, resumeOf: value.resumeOf,
|
||||
...(value.purpose ? { purpose: value.purpose, source: value.source, taskStatusBefore: value.taskStatusBefore } : {}),
|
||||
...(value.consumption ? { consumption: value.consumption } : {}),
|
||||
...(value.workspaceIdentity ? { workspaceIdentity: value.workspaceIdentity } : {}), ...(value.review ? { review: value.review } : {}) })),
|
||||
progress: { totalTools: tools.length, completedTools: tools.filter(value => value.state === 'completed').length,
|
||||
requiresVerification: tools.filter(value => value.requiresVerification).map(value => ({ id: value.id, tool: value.tool, callId: value.callId, state: value.state })) },
|
||||
|
||||
Reference in New Issue
Block a user