fix(engines): preserve verified review pauses and always close native sessions
This commit is contained in:
@@ -13,6 +13,7 @@ import { workerForSession } from '../../shared/contracts/workers.mjs';
|
||||
import { selectPurgeStrategy, assertPurgeModelInput } from './purge-strategy.mjs';
|
||||
import { engineHistoryGroups,engineContextEvidence,engineContextIndex,engineContextView,engineSeedPrompt } from '../../shared/contracts/engine-context.mjs';
|
||||
import { sanitizeModelText } from '../models/upstream-error.mjs';
|
||||
import { ChangeSetManager } from '../workspaces/change-sets.mjs';
|
||||
|
||||
const at = () => new Date().toISOString();
|
||||
const deferred = () => {
|
||||
@@ -51,6 +52,15 @@ export class EngineRunner {
|
||||
}
|
||||
async finish(session, run) {
|
||||
const driver = this.drivers.get(run.id); if (!driver) return;
|
||||
const review = run.record.review;
|
||||
try {
|
||||
if (!driver.failure && !driver.modelActive && !run.controller.signal.aborted && session.status === 'needs_review'
|
||||
&& run.stopReason === 'change_set_review' && review?.kind === 'change_set'
|
||||
&& await new ChangeSetManager(this.store).reviewReady(session, review.changeSetId, run.record)) {
|
||||
if (this.store.core.change_set.get(review.changeSetId)?.fingerprint !== review.fingerprint) throw engineError('整批审查指纹已变化,不能记录为已核对交接。');
|
||||
driver.prepareReviewHandoff(review);
|
||||
}
|
||||
} catch (error) { driver.fail(error); }
|
||||
try { await driver.close(); } finally { this.drivers.delete(run.id); }
|
||||
if (driver.job.requiresVerification) {
|
||||
session.status = 'paused'; run.stopReason = 'engine_verification';
|
||||
@@ -317,6 +327,18 @@ class EngineRunDriver {
|
||||
if (this.closePromise) return this.closePromise;
|
||||
return this.closePromise = this.finishClose();
|
||||
}
|
||||
prepareReviewHandoff(review) {
|
||||
if (this.queue.length || !this.pendingCalls.size) return;
|
||||
const tools = [];
|
||||
for (const [callId, pending] of this.pendingCalls) {
|
||||
const tool = [...this.store.core.tool.values()].find(value => value.runId === this.run.id && value.callId === callId);
|
||||
const result = this.store.savedToolResult(this.session.id, callId);
|
||||
if (!pending.delivered || !tool || tool.state !== 'completed' || tool.requiresVerification || !result || !tool.resultArtifactId) return;
|
||||
tools.push({ callId, invocationId: tool.id, resultArtifactId: tool.resultArtifactId, resultSha256: controlledDigest(result) });
|
||||
}
|
||||
this.job.reviewHandoff = { schemaVersion: 1, kind: 'change_set', changeSetId: review.changeSetId, fingerprint: review.fingerprint,
|
||||
scope: 'host_results_saved_native_session_closed_for_review_not_completed', tools };
|
||||
}
|
||||
async finishClose() {
|
||||
this.job.status = 'closing'; this.save('closing');
|
||||
for (const pending of this.pendingCalls.values()) pending.result.reject(new Error('所属引擎已结束,未返回结果保留原状态。'));
|
||||
@@ -328,10 +350,13 @@ class EngineRunDriver {
|
||||
const launched = !!this.native?.client || !!native?.process?.pid || !!this.job.pid;
|
||||
const unknown = launched && (!native?.cleanup?.rootExited || native?.cleanup?.treeConfirmed === false || native?.status === 'unknown');
|
||||
this.job.requiresVerification = unknown;
|
||||
this.job.status = unknown ? 'unknown' : this.finished && !this.failure && native?.status === 'closed' ? 'completed'
|
||||
const reviewPaused = !unknown && this.job.reviewHandoff && !this.run.controller.signal.aborted
|
||||
&& ['closed', 'cancelled'].includes(native?.status);
|
||||
this.job.status = unknown ? 'unknown' : reviewPaused ? 'paused' : this.finished && !this.failure && native?.status === 'closed' ? 'completed'
|
||||
: this.run.controller.signal.aborted ? 'cancelled' : 'failed';
|
||||
this.job.verified = this.job.status === 'completed'; this.job.finishedAt = at();
|
||||
this.job.error = this.failure ? this.provider.redact(this.failure.message) : native?.error ?? null;
|
||||
if (reviewPaused && this.failure) this.job.reviewHandoff.closeDiagnostic = this.provider.redact(this.failure.message);
|
||||
this.job.error = reviewPaused ? null : this.failure ? this.provider.redact(this.failure.message) : native?.error ?? null;
|
||||
this.job.originalOutcome = { status: this.job.status, requiresVerification: this.job.requiresVerification, verified: this.job.verified, error: this.job.error };
|
||||
try {
|
||||
verifyEngineInput(this.store, this.session, this.job);
|
||||
|
||||
@@ -556,6 +556,7 @@ export class Runtime {
|
||||
}
|
||||
signal.throwIfAborted();
|
||||
if (!ready) continue;
|
||||
run.record.review = { kind: 'change_set', changeSetId: id, fingerprint: this.store.core.change_set.get(id).fingerprint, status: 'pending' };
|
||||
session.status = 'needs_review'; run.stopReason = 'change_set_review';
|
||||
this.store.message(session, 'system', '当前任务的完整变更批次已封存,同轮工具结果已保存。请审查并应用或撤销批次;等待审查期间不继续请求模型。应用后可显式续接原任务进行检查。');
|
||||
this.store.save(session, { type: 'change_set.awaiting_review', detail: { changeSetId: id } });
|
||||
|
||||
@@ -23,6 +23,7 @@ export function engineRunReport(job) {
|
||||
return { schemaVersion: 1, owner: { sessionId: job.sessionId, projectId: job.projectId, taskId: job.taskId, runId: job.runId, jobId: job.id },
|
||||
inputSha256: job.inputSha256, installationSha256: job.installation.controlledSha256, sdkSessionId: job.sdkSessionId,
|
||||
outcome: job.originalOutcome, native: job.native, requests: job.requests, calls: job.calls,
|
||||
...(job.reviewHandoff ? { reviewHandoff: job.reviewHandoff } : {}),
|
||||
...(job.inputRejections?{inputRejections:job.inputRejections}:{}) };
|
||||
}
|
||||
export function publicEngineRun(job) {
|
||||
|
||||
Reference in New Issue
Block a user