fix(engines): preserve verified review pauses and always close native sessions

This commit is contained in:
Agent Workbench Dev
2026-11-03 12:20:20 +08:00
parent ecdc939c29
commit 983af1416e
8 changed files with 107 additions and 9 deletions
+27 -2
View File
@@ -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);