fix: preserve task intent across diagnostics and review continuation
This commit is contained in:
+18
-6
@@ -7,6 +7,7 @@ import { StateDatabase, runtimeOwnerAlive } from '../core/storage/state-database
|
||||
import { ArtifactStore } from '../core/storage/artifact-store.mjs';
|
||||
import { collectLegacy, cleanConfig } from '../core/storage/legacy-import.mjs';
|
||||
import { ACTIVE_STATES, CORE_KINDS, createTask, createRun, ownedRecord, emptyUsage, accumulateUsage, accountedTokens, isId } from '../shared/contracts/execution.mjs';
|
||||
import { isContinuation, isDiagnosticQuestion, taskContract, updateTaskContract } from '../shared/contracts/task-control.mjs';
|
||||
import { WORKSPACE_ACTIVE, projectWorkspaceIdentity, assertWorkspacePath } from '../shared/contracts/workspaces.mjs';
|
||||
import { RESEARCH_ACTIVE } from '../shared/contracts/research.mjs';
|
||||
import { ENGINE_ACTIVE, publicEngineJob } from '../shared/contracts/engines.mjs';
|
||||
@@ -362,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) { task.status = session.status; task.updatedAt = session.updatedAt; }
|
||||
if (task) { if (run?.purpose !== 'diagnostic') 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 }); }
|
||||
@@ -465,11 +466,18 @@ export class Store extends EventEmitter {
|
||||
});
|
||||
if (inputs.length>1 || inputs.length && !forceNewTask && task?.attachmentIds?.length && (resume || !['completed','idle'].includes(task.status))) throw new Error('当前任务已有截图,请新建任务再添加。');
|
||||
const previousRunId = session.runId ?? null;
|
||||
if (forceNewTask || !task || !resume && ['completed', 'idle'].includes(task.status)) {
|
||||
if (task) task.contract ??= taskContract(task, id => this.core.task.get(id));
|
||||
const purpose = !forceNewTask && task && !resume && isDiagnosticQuestion(content) ? 'diagnostic' : 'execution';
|
||||
const continuing = task && isContinuation(content);
|
||||
if (forceNewTask || !task || !resume && !continuing && purpose !== 'diagnostic' && ['completed', 'idle'].includes(task.status)) {
|
||||
const previous = task?.id ?? null;
|
||||
const goal = content || session.messages.find(message => message.role === 'user')?.content || session.title;
|
||||
task = createTask(session, goal, config, previous); this.core.task.set(task.id, task);
|
||||
} else if (content && (!resume || instructionSource)) task.instructions.push({ id: randomUUID(), content, createdAt: now(), ...(instructionSource ? { source: instructionSource } : {}) });
|
||||
} else if (content && purpose !== 'diagnostic' && (!resume || instructionSource)) {
|
||||
task.instructions.push({ id: randomUUID(), content, createdAt: now(), source: instructionSource ?? 'user' });
|
||||
updateTaskContract(task, content);
|
||||
}
|
||||
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.sessionInspection = null;
|
||||
@@ -477,7 +485,10 @@ export class Store extends EventEmitter {
|
||||
const workspaceIdentity = projectWorkspaceIdentity(this.project(session.projectId));
|
||||
task.workspaceIdentity ??= structuredClone(workspaceIdentity);
|
||||
if (JSON.stringify(task.workspaceIdentity) !== JSON.stringify(workspaceIdentity)) throw Object.assign(new Error('任务的工作区身份已变化,不能将原任务移动到另一个工作区。'), { status: 409 });
|
||||
const run = createRun(session, task, config, resume ? previousRunId : null);
|
||||
const run = createRun(session, task, config, resume || continuing ? previousRunId : null);
|
||||
run.purpose = purpose;
|
||||
if (purpose === 'diagnostic') { run.question = String(content); run.taskStatusBefore = task.status; }
|
||||
if (instructionSource) run.source = instructionSource;
|
||||
if(resumePlan){task.budget.maxTokens=config.maxTaskTokens;run.resumePlanId=resumePlan.id;resumePlan.status='consumed';resumePlan.newRunId=run.id;}
|
||||
run.workspaceIdentity = structuredClone(workspaceIdentity);
|
||||
this.core.run.set(run.id, run); task.activeRunId = run.id;
|
||||
@@ -515,8 +526,8 @@ export class Store extends EventEmitter {
|
||||
session.usage = usage;
|
||||
}
|
||||
|
||||
finishRun(session, run, stopReason = null) {
|
||||
run.status = session.status; run.finishedAt = now(); run.stopReason = stopReason;
|
||||
finishRun(session, run, stopReason = null, { status = session.status } = {}) {
|
||||
run.status = status; run.finishedAt = now(); run.stopReason = stopReason;
|
||||
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 } });
|
||||
@@ -630,6 +641,7 @@ export class Store extends EventEmitter {
|
||||
runs: [...this.core.run.values()].filter(value => value.sessionId === session.id).slice(-20).map(value => ({ id: value.id, taskId: value.taskId,
|
||||
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.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