import { unrestricted, ceiling } from '../../shared/contracts/execution-policy.mjs'; import fs from 'node:fs'; import path from 'node:path'; import os from 'node:os'; import { randomUUID } from 'node:crypto'; import { ControlledDeepSeekSession } from './controlled-session.mjs'; import { resolveControlledInstallation, assertControlledInstallation, controlledDigest } from './controlled-installation.mjs'; import { engineRunInput, engineRunReport, verifyEngineInput, nativeMessagesToChat } from '../../shared/contracts/engine-execution.mjs'; import { engineError } from '../../shared/contracts/engines.mjs'; import { modelSnapshot, accountedTokens, checkBudget, BudgetExceeded } from '../../shared/contracts/execution.mjs'; import { modelReservation,taskConsumption,checkModelDispatchBudget } from '../../shared/contracts/model-reservation.mjs'; import { completionBody, requestMetrics } from '../../shared/contracts/model-request.mjs'; 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 = () => { let resolve, reject; const promise = new Promise((yes, no) => { resolve = yes; reject = no; }); promise.catch(() => {}); return { promise, resolve, reject }; }; export class EngineRunner { constructor(store, provider, { appDirectory = process.cwd(), sessionFactory = installation => new ControlledDeepSeekSession(installation), directoryRoot = null } = {}) { this.store = store; this.provider = provider; this.appDirectory = appDirectory; this.sessionFactory = sessionFactory; this.directoryRoot = directoryRoot; this.drivers = new Map(); } availability(root) { return resolveControlledInstallation(this.appDirectory, root); } isActive(sessionId) { return [...this.drivers.values()].some(driver => driver.session.id === sessionId); } projectActive(projectId, exceptSessionId = null) { return [...this.drivers.values()].some(driver => driver.session.projectId === projectId && driver.session.id !== exceptSessionId); } selected(input) { const installation = this.availability(input.root); if (installation.controlledStatus !== 'available' || installation.controlledSha256 !== input.controlledSha256) { throw engineError('受控引擎与刚才检测的程序/插件身份不一致,请重新检测后制定新计划。'); } return { ...installation, ...(input.strategy === undefined ? {} : { strategy: selectPurgeStrategy(input.strategy, this.appDirectory, installation) }) }; } assertCurrent(session, run) { const driver = this.drivers.get(run.id); if (!driver || driver.session !== session || driver.run !== run) throw engineError('原引擎执行不属于当前Run。'); driver.assertInput(); driver.native?.verifyLaunch?.(); } complete(session, run, messages, progress, planned, tools) { let driver = this.drivers.get(run.id); if (!driver) { if (workerForSession(this.store, session) !== run.worker || !run.worker?.engine) throw engineError('外部引擎只能在所属受限子工程中执行。'); assertControlledInstallation(run.worker.engine); driver = new EngineRunDriver(this, session, run, planned, tools, messages); this.drivers.set(run.id, driver); } return driver.complete(messages, progress).catch(error => { driver.fail(error); throw error; }); } 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'; this.store.message(session, 'system', '所属外部引擎的原进程或结果待核对。请在父工程核对原输入与现场后继续。'); } else if (driver.job.status !== 'completed' && session.status === 'completed') { session.status = 'paused'; run.stopReason = 'engine_incomplete'; this.store.message(session, 'system', '原引擎没有完成全部协议和结果核验,不能交付。'); } return driver.job; } async stopAll() { for (const driver of this.drivers.values()) driver.run.controller.abort(new Error('用户停止执行宿主。')); for (const [id, driver] of [...this.drivers]) { await driver.close(); if (driver.native && (!driver.job.native?.cleanup?.rootExited || driver.job.native?.cleanup?.treeConfirmed === false)) { throw engineError('所属原生引擎进程结束仍待核对,宿主保持运行。'); } this.drivers.delete(id); } } } class EngineRunDriver { constructor(manager, session, run, seedPlan, tools, messages) { this.manager = manager; this.store = manager.store; this.provider = manager.provider; this.session = session; this.run = run; this.queue = []; this.waiter = null; this.pendingCalls = new Map(); this.finished = false; const base = path.resolve(manager.directoryRoot ?? path.join(os.tmpdir(), 'AgentWorkbench-Engine-Runs')); fs.mkdirSync(base, { recursive: true }); if (fs.lstatSync(base).isSymbolicLink()) throw engineError('引擎运行目录不能是链接。'); const id = randomUUID(); this.job = { schemaVersion: 1, id, sessionId: session.id, projectId: session.projectId, taskId: run.taskId, runId: run.id, workerId: run.worker.id, collaborationId: run.worker.collaborationId, installation: structuredClone(run.worker.engine), sdkSessionId: randomUUID(), directory: path.join(base, id), model: modelSnapshot(run.config), budget: { ...run.record.budget, maxToolCalls: run.worker.budget.maxToolCalls }, tools: structuredClone(tools), sourceContextPlanId: seedPlan.id, sourceRequestSha256: seedPlan.requestSha256, status: 'queued', startedAt: at(), finishedAt: null, requiresVerification: false, verified: false, native: null, requests: [], calls: [] }; const seed=this.store.artifactStore.write(session,run.record, JSON.parse(this.provider.redact(JSON.stringify({schemaVersion:1,sourceContextPlanId:seedPlan.id,sourceRequestSha256:seedPlan.requestSha256,messages,prompt:engineSeedPrompt(messages)}))),{kind:'engine-seed-context'}); this.store.core.artifact.set(seed.id,seed);this.job.seedContext={schemaVersion:1,artifactId:seed.id,sha256:seed.sha256}; const artifact = this.store.artifactStore.write(session, run.record, engineRunInput(this.job), { kind: 'engine-run-input' }); this.store.core.artifact.set(artifact.id, artifact); this.job.inputArtifactId = artifact.id; this.job.inputSha256 = artifact.sha256; run.record.engine = 'deepseek-workbench'; run.record.engineRunId = id; this.save('queued'); } save(phase) { this.store.putRecord('engine_run', this.session, this.job, 'engine_run.' + phase); } assertInput() { verifyEngineInput(this.store, this.session, this.job); assertControlledInstallation(this.job.installation); if (this.session.runId !== this.run.id || workerForSession(this.store, this.session) !== this.run.worker) throw engineError('引擎原Run已变化。'); } publish(value) { if (this.waiter) { const waiter = this.waiter; this.waiter = null; waiter.resolve(value); } else this.queue.push(value); } async next() { this.run.controller.signal.throwIfAborted(); if (this.queue.length) return this.queue.shift(); const waiter = deferred(); this.waiter = waiter; const abort = () => waiter.reject(this.run.controller.signal.reason ?? new Error('所属Run已停止。')); this.run.controller.signal.addEventListener('abort', abort, { once: true }); try { return await waiter.promise; } finally { this.run.controller.signal.removeEventListener('abort', abort); if (this.waiter === waiter) this.waiter = null; } } fail(error) { this.failure ??= error; this.publish({ error: this.failure }); } async start(messages) { this.job.status = 'starting'; this.save('starting'); this.native = this.manager.sessionFactory(this.job.installation); const owner = { sessionId: this.session.id, projectId: this.session.projectId, taskId: this.run.taskId, runId: this.run.id, jobId: this.job.id }; await this.native.open({ directory: this.job.directory, cwd: this.run.project.root, owner, sdkSessionId: this.job.sdkSessionId, tools: this.job.tools, config: this.run.config, strategy: this.job.installation.strategy, signal: this.run.controller.signal, onState: (state, detail) => { if (state !== 'ownership') this.job.status = state === 'ready' ? 'running' : state; this.job.pid = detail.pid ?? this.job.pid; if (this.native.report?.process) this.job.process = structuredClone(this.native.report.process); if (detail.readiness) this.job.readiness = structuredClone(detail.readiness); this.save(state); }, onNotification: () => { this.job.notificationCount = this.native.report.notifications.length; }, onModel: (input, trace, signal) => this.model(input, trace, signal), onTool: (input, trace, signal) => this.tool(input, trace, signal), }); this.nativeWait = (async () => { try { await this.native.prompt(engineSeedPrompt(messages)); const text = await this.native.wait(); this.finished = true; this.publish({ finalText: text }); } catch (error) { this.fail(error); } })(); this.nativeWait.catch(() => {}); } async complete(messages, progress) { this.progress = progress; this.assertInput(); if (!this.started) { this.started = true; await this.start(messages); } for (const [callId, pending] of this.pendingCalls) { if (!pending.delivered) continue; 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 (!tool || !result || tool.requiresVerification || !['completed', 'failed', 'denied', 'interrupted'].includes(tool.state)) { throw engineError('原引擎工具尚无可核对的完成结果,不能继续或重放。'); } pending.record.invocationId = tool.id; pending.record.resultSha256 = controlledDigest(result); pending.record.resultArtifactId = tool.resultArtifactId ?? null; pending.record.status = 'returned'; pending.record.finishedAt = at(); const returned = this.store.artifactStore.write(this.session, this.run.record, result, { kind: 'engine-tool-output' }); this.store.core.artifact.set(returned.id, returned); pending.record.outputArtifactId = returned.id; pending.record.outputSha256 = returned.sha256; this.save('tool_returned'); pending.result.resolve(result); this.pendingCalls.delete(callId); } const next = await this.next(); if (next.error) throw next.error; this.progress?.({ phase: 'completed', attempt: 0, maxAttempts: 0, engineRunId: this.job.id, origin: 'external-engine' }); if (next.call) { next.pending.delivered = true; return { message: { role: 'assistant', content: '', tool_calls: [next.call] }, usage: null, engineAccounted: true }; } return { message: { role: 'assistant', content: next.finalText }, usage: null, engineAccounted: true }; } async tool(input, trace, signal) { try { this.assertInput(); signal.throwIfAborted(); checkBudget(this.run.record, this.store.core.task.get(this.run.taskId)); const allCalls = [...this.store.core.engine_run.values()].filter(job => job.workerId === this.run.worker.id).reduce((total, job) => total + job.calls.length, 0); if (this.run.worker.budget.maxToolCalls > 0 && allCalls >= this.run.worker.budget.maxToolCalls) throw new BudgetExceeded('worker_tool_limit', '原引擎工具次数已到所属子任务累计上限。'); const callId = this.job.id + ':' + input.callId; if (this.pendingCalls.has(callId) || this.job.calls.some(call => call.callId === callId)) throw engineError('原引擎调用已经登记,不会再次执行。'); const record = { sourceCallId: input.callId, callId, name: input.name, argumentsSha256: controlledDigest(input.arguments), bridgeTraceId: trace.id, status: 'received', receivedAt: at() }; const artifact = this.store.artifactStore.write(this.session, this.run.record, { owner: trace.id, ...input }, { kind: 'engine-tool-input' }); this.store.core.artifact.set(artifact.id, artifact); record.inputArtifactId = artifact.id; record.inputSha256 = artifact.sha256; const pending = { record, result: deferred(), delivered: false }; this.job.calls.push(record); this.pendingCalls.set(callId, pending); this.job.status = 'waiting_tool'; this.save('tool_received'); const abort = () => pending.result.reject(signal.reason ?? new Error('所属引擎工具已停止。')); signal.addEventListener('abort', abort, { once: true }); this.publish({ call: { id: callId, type: 'function', function: { name: input.name, arguments: JSON.stringify(input.arguments) } }, pending }); try { return await pending.result.promise; } finally { signal.removeEventListener('abort', abort); } } catch (error) { this.fail(error); throw error; } } async model(input, trace, signal) { let request, dispatched = false; if (this.modelActive) { const error = engineError('所属引擎已有模型请求,不能并发扩展封存预算。'); this.fail(error); throw error; } this.modelActive = true; try { this.assertInput(); signal.throwIfAborted(); assertPurgeModelInput(input, this.job.installation.strategy); const task = this.store.core.task.get(this.run.taskId); checkBudget(this.run.record, task); const originalMessages = nativeMessagesToChat(input), tools = input.tools.map(tool => ({ type: 'function', function: tool })); engineHistoryGroups(originalMessages); const config = { ...this.run.config, maxOutputTokens: input.maxTokens ?? this.run.config.maxOutputTokens }; const owner = { sessionId: this.session.id, projectId: this.session.projectId, taskId: this.run.taskId, runId: this.run.id }; const source=this.store.artifactStore.write(this.session,this.run.record, JSON.parse(this.provider.redact(JSON.stringify({schemaVersion:1,owner,native:input}))),{kind:'engine-native-input'}); this.store.core.artifact.set(source.id,source); const safety=Math.max(512,Math.ceil(Math.min(config.contextLimit,24000)*0.1)); let index=null,reference={id:null,sha256:null}; if(config.capacityMode !== 'passthrough' && requestMetrics(completionBody(config,originalMessages,tools)).estimatedInputTokens>config.contextLimit-config.maxOutputTokens-safety){ index=engineContextIndex(originalMessages,engineContextEvidence(this.store,this.session,this.job,originalMessages), {artifact_id:source.id,sha256:source.sha256,owner}); reference=this.store.artifactStore.write(this.session,this.run.record,JSON.parse(this.provider.redact(JSON.stringify(index))),{kind:'engine-history-index'}); this.store.core.artifact.set(reference.id,reference); } const projected=engineContextView(originalMessages,tools,config,index,reference),{messages,body,metrics}=projected; const view={schemaVersion:1,mode:projected.mode,sourceArtifactId:source.id,sourceSha256:source.sha256, indexArtifactId:projected.indexArtifactId,indexSha256:projected.indexSha256,beforeInputTokens:projected.before.estimatedInputTokens, inputBudget:projected.inputBudget,safetyTokens:projected.safetyTokens,retainedGroups:projected.retainedGroups,archivedGroups:projected.archivedGroups}; const maximumAttempts = config.mode === 'demo' || config.capacityMode === 'passthrough' ? 1 : config.modelRouting?.maxAttempts ?? 1; const credit = Math.min(this.run.record.budget.maxTokens > 0 ? this.run.record.budget.maxTokens - accountedTokens(this.run.record.usage) : Infinity, task.budget.maxTokens > 0 ? task.budget.maxTokens - accountedTokens(task.usage) : Infinity); const tokenReservation=modelReservation(config,body,taskConsumption(this.store,task.id),128); const estimate = tokenReservation.reservedTokens; const attempts = Math.min(maximumAttempts, Math.floor(credit / estimate), this.run.worker.budget.maxSteps > 0 ? this.run.worker.budget.maxSteps - task.usage.requests : Infinity); if (projected.inputBudget !== null && metrics.estimatedInputTokens>projected.inputBudget || attempts < 1) { const overhead=requestMetrics(completionBody(config,[],[])).estimatedInputTokens; const toolDefinitionTokens=requestMetrics(completionBody(config,[],tools)).estimatedInputTokens-overhead; const fixedInstructionTokens=requestMetrics(completionBody(config,originalMessages.filter(x=>['system','developer','user'].includes(x.role)),[])).estimatedInputTokens-overhead; const contextBudget={contextLimit:config.contextLimit,outputReservation:config.maxOutputTokens,safetyTokens:projected.safetyTokens, inputBudget:projected.inputBudget,requiredInputTokens:metrics.estimatedInputTokens,originalInputTokens:projected.before.estimatedInputTokens, remainingTokens:credit,tokenReservation,runRemainingTokens:Math.max(0,this.run.record.budget.maxTokens-accountedTokens(this.run.record.usage)), taskRemainingTokens:Math.max(0,task.budget.maxTokens-accountedTokens(task.usage)),maxAttempts:Math.max(0,attempts),sourceArtifactId:source.id,sourceSha256:source.sha256, breakdown:{requestOverheadTokens:overhead,toolDefinitionTokens,fixedInstructionTokens, retainedHistoryAndViewTokens:metrics.estimatedInputTokens-overhead-toolDefinitionTokens-fixedInstructionTokens}, limitingReason:projected.inputBudget !== null && metrics.estimatedInputTokens>projected.inputBudget?'context_limit':'remaining_token_budget'}; const rejected=this.store.artifactStore.write(this.session,this.run.record, JSON.parse(this.provider.redact(JSON.stringify({schemaVersion:1,owner,view,body,budget:contextBudget,dispatched:false}))),{kind:'engine-model-rejection'}); this.store.core.artifact.set(rejected.id,rejected);(this.job.inputRejections??=[]).push({artifactId:rejected.id,sha256:rejected.sha256,...contextBudget});this.save('input_rejected'); const error=new BudgetExceeded('engine_input_budget',contextBudget.limitingReason==='context_limit' ?'原引擎必需输入超过上下文窗口可用容量,未发送模型请求。原输入和容量分项已保存;请核对模型容量后显式继续。' :'原引擎输入可容纳于上下文窗口,但下一次请求的用量预留超过剩余预算,未发送模型请求。原输入和容量分项已保存;请核对运行和任务预算后显式继续。'); error.contextBudget={...contextBudget,rejectionArtifactId:rejected.id,rejectionSha256:rejected.sha256};throw error; } const selected = config.modelRouting ? { ...config, modelRouting: { ...config.modelRouting, maxAttempts: attempts } } : config; request = { id: randomUUID(), bridgeTraceId: trace.id, sourceSha256: controlledDigest(input), requestSha256: metrics.sha256, contextViewVersion: 1, requestBytes: metrics.bytes, estimatedInputTokens: metrics.estimatedInputTokens, reservedPerAttempt: estimate, reservationVersion:1,tokenReservation, maxAttempts: attempts, accountedAttempts: 0, observedAttempts: 0, status: 'prepared', startedAt: at() }; const plan = { ...request, ...owner }; const artifact = this.store.artifactStore.write(this.session, this.run.record, JSON.parse(this.provider.redact(JSON.stringify({ schemaVersion: 1, owner, native: input, body, plan,view }))), { kind: 'engine-model-input' }); this.store.core.artifact.set(artifact.id, artifact); request.inputArtifactId = artifact.id; request.inputSha256 = artifact.sha256; this.job.requests.push(request); const modelEvent = this.store.event(this.session, { kind: 'model', source: 'engine_model_bridge', engineRunId: this.job.id, engineModelRequestId: request.id, taskId: this.job.taskId, runId: this.job.runId, label: '引擎请求模型服务', phase: 'running', startedAt: at(), output: '原引擎完整输入已封存,模型服务由工作台代理。' }); request.eventId = modelEvent.id; request.status = 'dispatched'; request.dispatchedAt = at(); this.job.status = 'running'; this.save('model_intent'); dispatched = true; const response = await this.provider.complete(selected, messages, this.run.project, signal, progress => { if (progress.consumption) { if (progress.consumption.modelIdentity) request.modelIdentity = modelEvent.modelIdentity = progress.consumption.modelIdentity; const recorded = this.store.recordConsumption(this.session, this.run.record, { ...progress.consumption, requestId: request.id }, estimate); if (recorded) request.accountedAttempts++; this.save('model_consumption'); return; } if (progress.streamCapture !== undefined || progress.responseCapture !== undefined) { const raw = progress.streamCapture ?? progress.responseCapture, decoded = raw.toString('utf8'); const redacted = sanitizeModelText(decoded, value => this.provider.redact(value)); const artifact = this.store.artifactStore.write(this.session, this.run.record, redacted === decoded ? raw : Buffer.from(redacted, 'utf8'), { kind: progress.streamCapture !== undefined ? 'model-stream-response' : 'model-http-response', mediaType: 'text/plain' }); this.store.core.artifact.set(artifact.id, artifact); if (progress.modelIdentity) progress.modelIdentity.responseSource = { artifactId: artifact.id, sha256: artifact.sha256, sessionId: this.session.id, scope: progress.streamCapture !== undefined ? 'sse-stream' : 'http-body' }; if (progress.upstream) Object.assign(progress.upstream, { artifactId: artifact.id, artifactSha256: artifact.sha256, sessionId: this.session.id }); this.save('model_response_source'); return; } if (progress.modelIdentity) request.modelIdentity = modelEvent.modelIdentity = progress.modelIdentity; if (progress.phase === 'running') { const attempt = progress.attempt ?? 1; if (!Number.isSafeInteger(attempt) || attempt < 1 || attempt > request.maxAttempts) throw engineError('模型尝试超出原引擎封存预算。'); request.observedAttempts = Math.max(request.observedAttempts, attempt); } if (progress.connectionId) { request.connectionId = progress.connectionId; request.actualModel = progress.model; } modelEvent.phase = progress.phase; modelEvent.attempts = progress.attempt; modelEvent.connectionId = progress.connectionId ?? null; modelEvent.actualModel = progress.model ?? config.model; modelEvent.label = progress.switching ? '引擎切换模型池连接' : progress.phase === 'failed' ? '引擎模型请求失败' : progress.phase === 'completed' ? '引擎模型响应已收到' : '引擎请求模型服务'; if (['completed', 'failed'].includes(progress.phase)) modelEvent.finishedAt = at(); this.save('model_progress'); }, plan, tools, { attempts: 1,beforeDispatch:({previousFailure}={})=>checkModelDispatchBudget(this.store,this.session,this.run.record,config,body,128,previousFailure) }); const count = Math.max(1, request.observedAttempts); request.actualAttemptsFinal = count; request.responseReceivedAt = at(); request.status = 'responded'; while (request.accountedAttempts < count - 1) { request.accountedAttempts++; this.store.recordResponse(this.session, this.run.record, null, estimate); } request.usage = response.usage ?? null; if (request.accountedAttempts < count) { request.accountedAttempts++; this.store.recordResponse(this.session, this.run.record, response.usage, estimate); } this.save('model_usage'); const output = JSON.parse(this.provider.redact(JSON.stringify(response))), saved = this.store.artifactStore.write(this.session, this.run.record, output, { kind: 'engine-model-output' }); this.store.core.artifact.set(saved.id, saved); request.responseArtifactId = saved.id; request.responseSha256 = saved.sha256; request.status = 'completed'; request.finishedAt = at(); this.save('model_completed'); modelEvent.phase = 'completed'; modelEvent.finishedAt = at(); modelEvent.output = '原模型响应与尝试用量已保存。'; this.save('model_event_completed'); checkBudget(this.run.record, task); signal.throwIfAborted(); return output; } catch (error) { this.fail(error); if (request) { request.actualAttemptsFinal ??= request.observedAttempts || request.maxAttempts; if (dispatched) while (request.accountedAttempts < request.actualAttemptsFinal) { request.accountedAttempts++; this.store.recordResponse(this.session, this.run.record, null, request.reservedPerAttempt); } request.status = signal.aborted ? 'interrupted' : 'failed'; request.error = this.provider.redact(error.message); if (error.diagnostic) request.diagnostic = JSON.parse(this.provider.redact(JSON.stringify(error.diagnostic))); request.finishedAt = at(); this.save('model_failed'); const event = this.session.events.find(value => value.id === request.eventId); if (event) { event.phase = request.status; event.finishedAt = at(); event.output = request.error; this.save('model_event_failed'); } } throw error; } finally { this.modelActive = false; } } async close() { 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('所属引擎已结束,未返回结果保留原状态。')); let native; try { native = this.native ? await this.native.close({ force: !this.finished || !!this.failure || this.run.controller.signal.aborted }) : null; } catch (error) { this.failure ??= error; } if (this.nativeWait) await this.nativeWait; this.job.native = native ? JSON.parse(this.provider.redact(JSON.stringify(native))) : null; 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; 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(); 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); const artifact = this.store.artifactStore.write(this.session, this.run.record, engineRunReport(this.job), { kind: 'engine-run-report' }); this.store.core.artifact.set(artifact.id, artifact); this.job.reportArtifactId = artifact.id; this.job.reportSha256 = artifact.sha256; this.save('finished'); } catch (error) { this.job.status = 'unknown'; this.job.requiresVerification = true; this.job.verified = false; this.job.error = '原引擎结果尚未完整封存:' + this.provider.redact(error.message); this.save('seal_failed'); } } }