import fs from 'node:fs'; import { randomUUID } from 'node:crypto'; import { createTask, createRun, modelSnapshot, accountedTokens, BudgetExceeded } from '../../shared/contracts/execution.mjs'; import { COLLABORATION_ACTIVE, COLLABORATION_CLOSED, collaborationInput, collaborationSeal, workerSeal, workerHash, workerConflict, workerForProject, workerForSession, workerPath, pathInWorkerScope, workerPathKey, workerUsage, assertWorkersResolved } from '../../shared/contracts/workers.mjs'; import { WorkspaceManager, verifyWorkspaceProject } from '../workspaces/git-workspaces.mjs'; import { projectWorkspaceIdentity } from '../../shared/contracts/workspaces.mjs'; import { assertChangeSetsResolved } from '../../shared/contracts/change-sets.mjs'; import { assertMcpResolved } from '../tools/mcp-contract.mjs'; import { inspectProject } from '../project-inspector.mjs'; import { safePath, readText, proposeFile } from '../../server/files.mjs'; import { verifiedCheck } from '../delivery-report.mjs'; import { ChangeSetManager } from '../workspaces/change-sets.mjs'; import { parseTestEvidence } from '../check-runner.mjs'; import { verifyPlanEvidence } from '../context/request-plan.mjs'; import { repairToolPairs } from '../../server/store.mjs'; import { readOwnedProcessTree } from '../owned-process-tree.mjs'; import { assertControlledInstallation } from '../engines/controlled-installation.mjs'; import { verifyEngineInput, originalEngineRun, assertEngineWorkerResult } from '../../shared/contracts/engine-execution.mjs'; const at = () => new Date().toISOString(); const sourceIdentity = source => Object.fromEntries(['root', 'head', 'reference', 'branch', 'gitDir', 'commonDir', 'indexSha256', 'statusSha256', 'sourceFingerprint', 'treeSha256', 'configuration', 'filterDrivers'].map(key => [key, source[key]])); const configView = config => Object.fromEntries(['mode', 'baseUrl', 'model', 'contextLimit', 'maxOutputTokens', 'configRevision', 'maxSteps', 'maxRunDurationMs', 'maxRunTokens', 'maxTaskTokens', ...(config.protocol ? ['protocol', 'streaming'] : []), ...(config.modelRouting ? ['modelRouting'] : [])].map(key => [key, structuredClone(config[key])])); const alive = pid => { if (!pid) return false; try { process.kill(pid, 0); return true; } catch (error) { return error.code === 'EPERM'; } }; export class WorkerManager { constructor(store, runtime, { faultInjector = null } = {}) { this.store = store; this.runtime = runtime; this.faultInjector = faultInjector; this.active = new Map(); this.preparingGroupId = null; this.workspaces = new WorkspaceManager(store, { projectBusy: projectId => runtime.projectHasActivity(projectId, null, { excludeGroupId: this.preparingGroupId }) }); } groups(session) { return [...this.store.core.collaboration.values()].filter(group => group.sessionId === session.id); } view(session) { return { groups: this.groups(session).map(group => ({ ...group, workers: group.children.map(child => { const record = this.store.core.worker.get(child.id); return record ? { ...record, usage: workerUsage(this.store, record), sessionStatus: this.store.session(record.sessionId).status } : { ...child, status: 'unprepared', usage: null }; }) })), worker: workerForProject(this.store, session.projectId) }; } childActive(id) { return this.runtime.active.has(id) || !!this.runtime.controlledEngines?.isActive(id) || !!this.runtime.checks?.isActive(id) || !!this.runtime.changeSets?.isActive(id) || !!this.workspaces?.isActive(id); } isActive(sessionId) { return [...this.active.values()].some(value => value.sessionId === sessionId) || [...this.store.core.collaboration.values()].some(group => group.sessionId === sessionId && !COLLABORATION_CLOSED.has(group.status) && group.children.some(child => this.childActive(child.sessionId))); } projectActive(projectId, excludeGroupId = null) { return [...this.active.entries()].some(([id, value]) => id !== excludeGroupId && (value.projectId === projectId || ['preparing', 'collecting', 'reconciling', 'merge_preparing'].includes(value.group.status) && value.group.children.some(child => child.projectId === projectId))) || [...this.store.core.collaboration.values()].some(group => group.id !== excludeGroupId && group.projectId === projectId && !COLLABORATION_CLOSED.has(group.status) && group.children.some(child => this.childActive(child.sessionId))); } assertIdle(session) { if (this.runtime.projectHasActivity(session.projectId)) throw workerConflict('项目已有执行或协作操作,请等待结束。'); assertWorkersResolved(this.store, session.projectId); assertMcpResolved(this.store, session.projectId); assertChangeSetsResolved(this.store, session.projectId, { includePrepared: true }); if (session.changes.some(change => ['pending', 'conflict'].includes(change.status))) throw workerConflict('请先审查当前待处理改动,再创建协作目标。'); } own(session, id, fingerprint = null, { current = true } = {}) { const group = this.store.owned('collaboration', id, session.id); if (current && (group.taskId !== session.taskId || group.runId !== session.runId) || group.projectId !== session.projectId || workerHash(collaborationSeal(group)) !== group.fingerprint || fingerprint !== null && fingerprint !== group.fingerprint) throw workerConflict('父任务或原协作分工指纹已变化,请核对原记录。'); const artifact = this.store.owned('artifact', group.planArtifactId, session.id); if (artifact.kind !== 'collaboration-plan' || artifact.taskId !== group.taskId || artifact.runId !== group.runId || artifact.sha256 !== group.planSha256 || this.store.artifactStore.read(artifact) !== JSON.stringify(collaborationSeal(group))) throw workerConflict('协作计划与原封存工件不一致。'); return group; } save(session, group, phase) { group.updatedAt = at(); group.phase = phase; group.usage = group.children.reduce((total, child) => { const worker = this.store.core.worker.get(child.id), usage = worker ? workerUsage(this.store, worker) : {}; for (const key of ['accountedTokens', 'steps', 'toolCalls', 'requests', 'unknownRequests', 'estimatedUnknownTokens', 'totalTokens']) total[key] = (total[key] ?? 0) + (usage[key] ?? 0); return total; }, {}); return this.store.putRecord('collaboration', session, group, 'collaboration.' + phase); } async sourceFor(group) { const source = await this.workspaces.snapshot(this.store.project(group.projectId)); if (JSON.stringify(sourceIdentity(source)) !== JSON.stringify(group.source.identity)) throw workerConflict('来源HEAD、索引、配置或已有修改已变化;原工作区保留,请重新审查协作来源。'); return source; } async plan(session, input) { this.assertIdle(session); if (workerForProject(this.store, session.projectId)) throw workerConflict('受限子任务不能创建嵌套协作。'); if (this.groups(session).some(group => !COLLABORATION_CLOSED.has(group.status)) || [...this.store.core.collaboration.values()].some(group => group.projectId === session.projectId && !COLLABORATION_CLOSED.has(group.status))) throw workerConflict('项目已有未收尾的父协作,请先完成或明确保留原成果。'); const initial = structuredClone(this.store.config), config = this.runtime.provider.prepareConfig ? this.runtime.provider.prepareConfig(initial, this.store) : initial; const definition = collaborationInput(input, config), project = this.store.project(session.projectId); const engine = definition.engine ? this.runtime.engineRunner().selected(definition.engine) : null; const temporaryId = randomUUID(), controller = new AbortController(), active = { sessionId: session.id, projectId: project.id, group: { children: [], status: 'planning' }, controller }; active.promise = new Promise(resolve => { active.settle = resolve; }); this.active.set(temporaryId, active); try { this.preparingGroupId = temporaryId; const source = await this.workspaces.snapshot(project); controller.signal.throwIfAborted(); if (source.changedPathCount > 32) throw workerConflict('来源已有修改超过32个路径,请先缩小范围;不会遗漏原修改后合并。'); for (const child of definition.children) for (const relative of child.paths) safePath(project, relative, true); const run = this.store.beginRun(session, definition.goal, false, config, { forceNewTask: true }); session.status = 'paused'; this.store.finishRun(session, run, 'worker_control'); const group = { schemaVersion: 1, id: randomUUID(), projectId: project.id, sessionId: session.id, taskId: session.taskId, runId: run.id, goal: definition.goal, budget: definition.budget, model: modelSnapshot(config), config: configView(config), source: { identity: sourceIdentity(source), changedPaths: source.changedPaths, dirty: source.dirty, scope: source.scope }, children: definition.children.map(child => ({ ...child, ...(engine ? { engine: structuredClone(engine) } : {}), id: randomUUID(), workspaceId: randomUUID(), projectId: randomUUID(), sessionId: randomUUID(), taskId: randomUUID() })), status: 'planned', requiresVerification: false, createdAt: at(), updatedAt: at(), startedAt: null, deadline: null, finishedAt: null, summary: '父子目标、范围、模型和预算已封存,尚未创建工作区或请求模型。', collection: null, merge: null }; group.fingerprint = workerHash(collaborationSeal(group)); const artifact = this.store.artifactStore.write(session, run, collaborationSeal(group), { kind: 'collaboration-plan' }); this.store.core.artifact.set(artifact.id, artifact); group.planArtifactId = artifact.id; group.planSha256 = artifact.sha256; const task = this.store.core.task.get(group.taskId); task.origin = 'worker-coordination'; task.budget.maxTokens = group.budget.maxTokens; task.collaborationId = group.id; this.store.message(session, 'user', group.goal); this.save(session, group, 'planned'); return group; } finally { this.preparingGroupId = null; this.active.delete(temporaryId); active.settle(); } } launch(session, group, status, execute) { if (this.active.has(group.id) || this.runtime.projectHasActivity(group.projectId)) throw workerConflict('协作操作正在执行,不能重复启动。'); const controller = new AbortController(), active = { sessionId: session.id, projectId: group.projectId, group, controller, promise: null }; group.status = status; this.active.set(group.id, active); try { this.save(session, group, status); active.promise = execute(controller.signal).finally(() => { this.active.delete(group.id); if (this.preparingGroupId === group.id) this.preparingGroupId = null; }); active.promise.catch(() => {}); return { group, promise: active.promise }; } catch (error) { this.active.delete(group.id); throw error; } } prepare(session, id, fingerprint) { const group = this.own(session, id, fingerprint); if (!['planned', 'partially_prepared'].includes(group.status) || group.requiresVerification) throw workerConflict('工作区已创建、正在执行或待核对,不能重放创建。'); return this.launch(session, group, 'preparing', async signal => { this.preparingGroupId = group.id; try { await this.sourceFor(group); for (const child of group.children) { signal.throwIfAborted(); const existing = this.store.core.workspace.get(child.workspaceId); if (existing && !(existing.status === 'ready' || existing.status === 'prepared' && !existing.creationAttempted && !existing.requiresVerification)) throw workerConflict('原工作区记录需要核对,不能再次执行创建。'); if (!existing) { this.save(session, group, 'workspace_intent'); const workspace = await this.workspaces.prepare(session, child.label, { id: child.workspaceId, childProjectId: child.projectId, childSessionId: child.sessionId }); signal.throwIfAborted(); const result = await (await this.workspaces.start(session, workspace.id, workspace.fingerprint)).promise; if (result.status !== 'ready') throw workerConflict(result.summary); } else if (existing.status === 'prepared') { const result = await (await this.workspaces.start(session, existing.id, existing.fingerprint)).promise; if (result.status !== 'ready') throw workerConflict(result.summary); } await this.initializeChild(group, child); this.save(session, group, 'workspace_registered'); await this.faultInjector?.('workspace_registered', group, child); signal.throwIfAborted(); } await this.sourceFor(group); group.status = 'ready'; group.summary = group.children.length + ' 个独立工作区已核对,来源HEAD、索引和已有修改保持。可以开始受限子任务。'; } catch (error) { const uncertain = group.children.some(child => { const workspace = this.store.core.workspace.get(child.workspaceId); return workspace && (workspace.status !== 'ready' || !this.store.core.worker.has(child.id) && this.store.sessions.get(child.sessionId)?.taskId); }); group.status = uncertain ? 'unknown' : signal.aborted ? 'partially_prepared' : 'stale'; group.requiresVerification = uncertain; group.summary = this.runtime.provider.redact(error.message ?? String(error)); } this.save(session, group, group.status); return group; }); } async inventory(project) { const inspection = await inspectProject(project, { includeFiles: true }); if (!inspection.scope.complete) throw workerConflict('子工程超出完整快照范围,不能封存或合并成果。'); const files = inspection.files.map(file => { const bytes = fs.readFileSync(safePath(project, file.path)); return { path: file.path, sha256: workerHash(bytes), bytes: bytes.length }; }).sort((a, b) => a.path.localeCompare(b.path)); return { inspection, files }; } async initializeChild(group, child) { if (this.store.core.worker.has(child.id)) { workerForSession(this.store, this.store.session(child.sessionId)); return; } const project = this.store.project(child.projectId), session = this.store.session(child.sessionId); await verifyWorkspaceProject(this.store, project); if (session.messages.length || session.changes.length) throw workerConflict('新工作区已有其他任务记录,不能覆盖其身份。'); const config = { ...group.config, maxTaskTokens: child.budget.maxTokens }; let task = this.store.core.task.get(session.taskId), run = this.store.core.run.get(session.runId); if (session.taskId) { if (task?.id !== child.taskId || task.goal !== child.goal || task.workerId !== child.id || task.origin !== 'isolated-worker' || run?.taskId !== task.id || run.stopReason !== 'worker_prepared' || !run.finishedAt) throw workerConflict('子任务初始化有其他或不完整记录,请保留现场核对。'); } else { task = createTask(session, child.goal, config); task.id = child.taskId; task.origin = 'isolated-worker'; task.workspaceIdentity = projectWorkspaceIdentity(project); task.budget.maxTokens = child.budget.maxTokens; task.workerId = child.id; task.constraints = ['允许修改范围:' + child.paths.join('、'), '只接受原提案应用和当前工程检查;禁止独立Git提交、任意终端及外部工具。']; run = createRun(session, task, config); run.workspaceIdentity = projectWorkspaceIdentity(project); this.store.core.task.set(task.id, task); this.store.core.run.set(run.id, run); session.taskId = task.id; session.runId = run.id; session.status = 'paused'; this.store.finishRun(session, run, 'worker_prepared'); } const { files, inspection } = await this.inventory(project), git = await this.workspaces.snapshot(project); if (git.head !== group.source.identity.head || git.dirty) throw workerConflict('子工作区初始HEAD或源码不符合封存来源。'); const worker = { schemaVersion: 1, ...child, collaborationId: group.id, parentProjectId: group.projectId, parentSessionId: group.sessionId, parentTaskId: group.taskId, parentRunId: group.runId, baseCommit: group.source.identity.head, model: group.model, runId: run.id, currentRunId: null, status: 'ready', requiresVerification: false, runIds: [], startedAt: null, finishedAt: null, ownerPid: null, summary: '独立工作区与原始源码已封存,尚未请求模型。', result: null }; const baseline = this.store.artifactStore.write(session, run, { schemaVersion: 1, workerId: worker.id, collaborationId: group.id, projectId: project.id, sessionId: session.id, taskId: task.id, runId: run.id, groupFingerprint: group.fingerprint, files, sourceFingerprint: inspection.sourceFingerprint, git: sourceIdentity(git) }, { kind: 'worker-baseline' }); this.store.core.artifact.set(baseline.id, baseline); worker.baselineArtifactId = baseline.id; worker.baselineSha256 = baseline.sha256; worker.baselineRunId = run.id; worker.fingerprint = workerHash(workerSeal(worker)); const contract = this.store.artifactStore.write(session, run, workerSeal(worker), { kind: 'worker-contract' }); this.store.core.artifact.set(contract.id, contract); worker.contractArtifactId = contract.id; worker.contractSha256 = contract.sha256; this.store.putRecord('worker', session, worker, 'worker.ready'); return worker; } configFor(group, worker) { if (worker.engine) { assertControlledInstallation(worker.engine); for (const job of this.store.core.engine_run.values()) if (job.workerId === worker.id) verifyEngineInput(this.store, this.store.session(worker.sessionId), job); } const initial = structuredClone(this.store.config), current = this.runtime.provider.prepareConfig ? this.runtime.provider.prepareConfig(initial, this.store) : initial; if (JSON.stringify(modelSnapshot(current)) !== JSON.stringify(group.model)) throw workerConflict('模型连接或池快照已变化;不能在原协作中悄悄替换模型。'); const usage = workerUsage(this.store, worker), remaining = group.deadline - Date.now(); if (remaining <= 0) throw new BudgetExceeded('time_limit', '父协作总时限已用完,原任务与成果保留。'); if (usage.accountedTokens >= worker.budget.maxTokens || usage.steps >= worker.budget.maxSteps || usage.toolCalls >= worker.budget.maxToolCalls || worker.engine && usage.requests >= worker.budget.maxSteps) throw new BudgetExceeded('worker_budget', '子任务累计预算已用完,续接不能重置预算。'); return { ...group.config, maxSteps: worker.budget.maxSteps - usage.steps, maxTaskTokens: worker.budget.maxTokens, maxRunTokens: worker.budget.maxTokens - usage.accountedTokens, maxRunDurationMs: remaining }; } start(session, id, fingerprint, workerId = null) { const group = this.own(session, id, fingerprint); if (!['ready', 'paused', 'awaiting_review'].includes(group.status) || group.requiresVerification) throw workerConflict('协作尚未就绪、已封存或待核对,不能重新执行。'); const chosen = group.children.filter(child => workerId === null || child.id === workerId).map(child => this.store.core.worker.get(child.id)); if (!chosen.length || chosen.some(worker => !worker)) throw workerConflict('子任务不属于当前父目标。'); const candidates = chosen.filter(worker => worker.status !== 'completed'); if (!candidates.length) throw workerConflict('所选子任务已经完成,不会重放。'); for (const worker of candidates) { const childSession = this.store.session(worker.sessionId); workerForSession(this.store, childSession); assertWorkersResolved(this.store, childSession.projectId); if (childSession.changes.some(change => ['pending', 'conflict'].includes(change.status))) throw workerConflict('请在子任务中先审查改动,再继续原目标。'); if (worker.requiresVerification) throw workerConflict('子任务结果待核对,不能续接。'); } return this.launch(session, group, 'starting', async signal => { try { await this.sourceFor(group); signal.throwIfAborted(); group.startedAt ??= at(); group.deadline ??= Date.parse(group.startedAt) + group.budget.maxDurationMs; const configs = candidates.map(worker => this.configFor(group, worker)); group.status = 'running'; session.status = 'running'; this.save(session, group, 'running'); const promises = candidates.map((worker, index) => this.runChild(group, worker, configs[index], signal)); const outcomes = await Promise.allSettled(promises); const workers = group.children.map(child => this.store.core.worker.get(child.id)); group.requiresVerification = outcomes.some(outcome => outcome.status === 'rejected') || workers.some(worker => worker.requiresVerification || ['running', 'stopping'].includes(worker.status)); group.status = group.requiresVerification ? 'unknown' : workers.every(worker => worker.status === 'completed') ? 'ready_to_collect' : workers.some(worker => this.store.session(worker.sessionId).status === 'needs_review') ? 'awaiting_review' : 'paused'; group.summary = group.status === 'ready_to_collect' ? group.children.length + ' 个子任务均有实际改动和当前通过的检查,等待父任务核对原成果。' : group.status === 'awaiting_review' ? '子任务提案已保存,请分别审查应用后继续;完成状态以实际检查为准。' : '子任务已停止或需要处理,已完成结果保留。'; } catch (error) { group.status = 'paused'; group.summary = this.runtime.provider.redact(error.message ?? String(error)); } session.status = group.status === 'awaiting_review' ? 'needs_review' : 'paused'; this.save(session, group, group.status); return group; }); } async runChild(group, worker, config, signal) { const session = this.store.session(worker.sessionId); try { signal.throwIfAborted(); worker.status = 'running'; worker.startedAt ??= at(); worker.ownerPid = process.pid; this.store.putRecord('worker', session, worker, 'worker.starting'); this.runtime.startWorker(session, worker, config); const active = this.runtime.active.get(session.id); worker.currentRunId = active.id; worker.runIds.push(active.id); this.store.putRecord('worker', session, worker, 'worker.running'); const stop = () => active.controller.abort(signal.reason); signal.addEventListener('abort', stop, { once: true }); if (signal.aborted) stop(); try { await active.promise; } finally { signal.removeEventListener('abort', stop); } const unknown = this.uncertain(worker); worker.requiresVerification = unknown.length > 0; worker.status = unknown.length ? 'unknown' : session.status === 'needs_review' ? 'awaiting_review' : session.status === 'failed' ? 'failed' : 'paused'; if (session.status === 'completed' && !unknown.length) { const output = await this.output(session, worker); const artifact = this.store.artifactStore.write(session, this.store.owned('run', worker.currentRunId, session.id), output, { kind: 'worker-result' }); this.store.core.artifact.set(artifact.id, artifact); worker.result = { artifactId: artifact.id, sha256: artifact.sha256, fingerprint: workerHash(output), checkJobId: output.check.id, paths: output.files.map(file => file.path) }; worker.status = 'completed'; worker.summary = '实际改动与原检查证据已封存,等待父任务归集。'; } else worker.summary = unknown.length ? '执行结果或原进程待核对,不会重放。' : session.failure?.message ?? '原子任务已保存,等待审查或继续。'; } catch (error) { const active = this.runtime.active.get(session.id); if (active?.taskId === worker.taskId) { active.controller.abort(error); await Promise.allSettled([active.promise]); } worker.requiresVerification = this.uncertain(worker).length > 0; worker.status = worker.requiresVerification ? 'unknown' : 'failed'; worker.summary = this.runtime.provider.redact(error.message ?? String(error)); } finally { worker.ownerPid = null; worker.finishedAt = at(); this.store.putRecord('worker', session, worker, 'worker.' + worker.status); } return worker; } uncertain(worker) { return ['tool', 'check_job', 'change_set', 'delivery_job', 'engine_run'].flatMap(kind => [...this.store.core[kind].values()] .filter(value => value.taskId === worker.taskId && (value.requiresVerification || value.status === 'unknown' || value.state === 'unknown')) .map(value => ({ kind, id: value.id }))); } baseline(session, worker) { workerForSession(this.store, session); const artifact = this.store.owned('artifact', worker.baselineArtifactId, session.id); if (artifact.kind !== 'worker-baseline' || artifact.taskId !== worker.taskId || artifact.runId !== worker.baselineRunId || artifact.sha256 !== worker.baselineSha256) throw workerConflict('子任务初始源码工件归属无效。'); const baseline = JSON.parse(this.store.artifactStore.read(artifact)), group = this.store.core.collaboration.get(worker.collaborationId); if (baseline.workerId !== worker.id || baseline.collaborationId !== group.id || baseline.groupFingerprint !== group.fingerprint || baseline.projectId !== worker.projectId || baseline.sessionId !== worker.sessionId || baseline.taskId !== worker.taskId || baseline.runId !== worker.baselineRunId || baseline.git.head !== worker.baseCommit) throw workerConflict('子任务初始来源与封存契约不一致。'); return baseline; } async output(session, worker) { workerForSession(this.store, session); const baseline = this.baseline(session, worker), project = this.store.project(worker.projectId); await verifyWorkspaceProject(this.store, project); if (this.uncertain(worker).length) throw workerConflict('子任务仍有结果未知的工具、检查或文件批次'); assertEngineWorkerResult(this.store, session, worker); if (session.changes.some(change => ['pending', 'conflict'].includes(change.status))) throw workerConflict('子任务仍有未审查改动'); const before = new Map(baseline.files.map(file => [workerPathKey(file.path), file])); const { inspection, files: current } = await this.inventory(project), after = new Map(current.map(file => [workerPathKey(file.path), file])); const git = await this.workspaces.snapshot(project); if (git.head !== worker.baseCommit || git.indexSha256 !== baseline.git.indexSha256 || git.reference !== baseline.git.reference || git.gitDir !== baseline.git.gitDir || git.commonDir !== baseline.git.commonDir || JSON.stringify(git.configuration) !== JSON.stringify(baseline.git.configuration)) throw workerConflict('子任务HEAD、索引或Git配置已变化,独立提交或暂存不能作为原成果'); if (git.changedPathCount > 32 || git.changedPaths.some(file => !after.has(workerPathKey(file.path)))) throw workerConflict('实际Git修改包含删除、链接、排除文件或超出32文件范围,不能遗漏后合并'); const paths = [...new Set([...before.keys(), ...after.keys()])].filter(key => before.get(key)?.sha256 !== after.get(key)?.sha256); if (!paths.length || paths.length > 32) throw workerConflict('可交付成果需要1到32个实际文本改动'); let bytes = 0; const files = paths.map(key => { const original = before.get(key), actual = after.get(key), relative = actual?.path ?? original.path; workerPath(relative); if (!pathInWorkerScope(relative, worker.paths)) throw workerConflict('实际改动超出子任务范围:' + relative); if (!actual) throw workerConflict('本阶段不支持合并删除:' + relative); const chain = session.changes.filter(change => change.taskId === worker.taskId && workerPathKey(change.path) === key && change.status === 'applied'); if (!chain.length) throw workerConflict('实际修改缺少原Task已应用提案:' + relative); let expected = original?.sha256 ?? null; for (const change of chain) { if (change.baseExists !== (expected !== null) || change.baseSha256 !== expected || workerHash(Buffer.from(change.content, 'utf8')) !== change.sha256 || change.baseExists && workerHash(Buffer.from(change.baseContent, 'utf8')) !== change.baseSha256) throw workerConflict('应用提案的原字节链不连续:' + relative); if (change.changeSetId) { const set = this.store.owned('change_set', change.changeSetId, session.id); if (set.status !== 'completed' || set.requiresVerification) throw workerConflict('子成果中的批次没有完整应用结果:' + relative); this.runtime.changeSetManager().snapshots(session, set); } expected = change.sha256; } if (expected !== actual.sha256) throw workerConflict('当前文件与最后已应用提案不一致:' + relative); const text = readText(project, relative), last = chain.at(-1), first = chain[0]; if (text.sha256 !== last.sha256) throw workerConflict('成果读取期间原文件发生变化:' + relative); const raw = fs.readFileSync(safePath(project, relative)); bytes += raw.length + Buffer.byteLength(first.baseContent, 'utf8'); if (raw.length > 512000 || bytes > 4 * 1024 * 1024) throw workerConflict('成果超过单文件512KB或原文/建议合计4MB范围'); return { path: relative, beforeSha256: original?.sha256 ?? null, sha256: actual.sha256, bytes: raw.length, before: original ? Buffer.from(first.baseContent, 'utf8').toString('base64') : null, after: raw.toString('base64'), changeIds: chain.map(change => change.id) }; }).sort((a, b) => a.path.localeCompare(b.path)); const check = verifiedCheck(this.store, session, inspection); if (!check) throw workerConflict('缺少与当前源码一致、日志完整且测试数量明确的通过记录'); const plan = this.runtime.checkRunner().planFor(session, check.planId); for (const result of check.results) if (result.kind === 'test' && result.engine !== 'python') { const spec = plan.checks.find(value => value.name === result.name); const evidence = parseTestEvidence(this.store.artifactStore.read(this.store.owned('artifact', result.logArtifactId, session.id)), spec?.framework); if (JSON.stringify(evidence) !== JSON.stringify(result.tests)) throw workerConflict('检查数量与原日志不一致'); } const cache = new Map(); for (const request of this.store.core.context_plan.values()) if (request.taskId === worker.taskId) verifyPlanEvidence(this.store, session, request, cache); const latest = await this.inventory(project); if (latest.inspection.sourceFingerprint !== inspection.sourceFingerprint || JSON.stringify(latest.files) !== JSON.stringify(current)) throw workerConflict('核验期间子工程发生变化'); return { schemaVersion: 1, workerId: worker.id, collaborationId: worker.collaborationId, workerFingerprint: worker.fingerprint, projectId: worker.projectId, sessionId: worker.sessionId, taskId: worker.taskId, baseCommit: worker.baseCommit, sourceFingerprint: inspection.sourceFingerprint, files, inventoryFingerprint: workerHash(current), check: { id: check.id, planId: check.planId, runId: check.runId, sourceFingerprint: check.sourceFingerprint, results: check.results.map(result => ({ id: result.id, name: result.name, tests: result.tests, exitCode: result.exitCode, logArtifactId: result.logArtifactId, logSha256: this.store.owned('artifact', result.logArtifactId, session.id).sha256, ...(result.metricsArtifactId ? { metricsArtifactId: result.metricsArtifactId, metricsSha256: result.metricsSha256 } : {}), })) }, usage: workerUsage(this.store, worker), runIds: worker.runIds }; } async verifiedResult(group, child) { const worker = this.store.core.worker.get(child.id), session = this.store.session(child.sessionId); if (!worker || worker.status !== 'completed' || worker.requiresVerification || !worker.result) throw workerConflict('子任务尚未完成原成果封存:' + child.label); const artifact = this.store.owned('artifact', worker.result.artifactId, session.id); if (artifact.kind !== 'worker-result' || artifact.taskId !== worker.taskId || artifact.runId !== worker.currentRunId || artifact.sha256 !== worker.result.sha256) throw workerConflict('子成果工件归属或原散列不一致:' + child.label); const original = JSON.parse(this.store.artifactStore.read(artifact)); if (workerHash(original) !== worker.result.fingerprint || original.workerId !== worker.id || original.collaborationId !== group.id || original.workerFingerprint !== worker.fingerprint || original.taskId !== worker.taskId) throw workerConflict('子成果与原分工不一致:' + child.label); const current = await this.output(session, worker); if (JSON.stringify(original) !== JSON.stringify(current)) throw workerConflict('子工程、检查、账本或原成果已变化:' + child.label); return { worker, artifact, output: original }; } async collectionFor(session, group) { await this.sourceFor(group); const proofs = []; for (const child of group.children) proofs.push(await this.verifiedResult(group, child)); const conflicts = [], targets = [], project = this.store.project(group.projectId); const all = proofs.flatMap(proof => proof.output.files.map(file => ({ ...file, workerId: proof.worker.id, label: proof.worker.label }))); for (let index = 0; index < all.length; index++) { const file = all[index], key = workerPathKey(file.path); for (const other of all.slice(index + 1)) { const otherKey = workerPathKey(other.path); if (key === otherKey || key.startsWith(otherKey + '/') || otherKey.startsWith(key + '/')) conflicts.push({ path: file.path, workerIds: [file.workerId, other.workerId], reason: '两个子成果修改同一路径或文件/目录相互包含;需要明确分工后重新审查。' }); } const dirty = group.source.changedPaths.find(value => [value.path, value.originalPath].filter(Boolean).some(relative => { const dirtyKey = workerPathKey(relative); return key === dirtyKey || key.startsWith(dirtyKey + '/') || dirtyKey.startsWith(key + '/'); })); if (dirty) { conflicts.push({ path: file.path, workerIds: [file.workerId], reason: '父工程已有暂存、未提交或未跟踪修改:' + dirty.path }); continue; } try { const target = safePath(project, file.path, true), exists = fs.existsSync(target); if (exists !== (file.before !== null)) throw workerConflict('父文件存在状态与子HEAD不一致,不能覆盖已有内容。'); const original = exists ? readText(project, file.path) : null, before = original ? fs.readFileSync(target) : null; const childBefore = file.before === null ? null : Buffer.from(file.before, 'base64'), after = Buffer.from(file.after, 'base64'); if (workerHash(after) !== file.sha256 || childBefore && workerHash(childBefore) !== file.beforeSha256) throw workerConflict('子成果原字节不一致。'); const normalized = value => value.replace(/^\uFEFF/, '').replaceAll('\r\n', '\n'); if (before && normalized(before.toString('utf8')) !== normalized(childBefore.toString('utf8'))) throw workerConflict('父工程原内容与子HEAD不一致。'); let content = new TextDecoder('utf8', { fatal: true }).decode(after); if (original) { const source = original.content; if (/\r\n/.test(source) && !/(? 32 || targets.reduce((sum, file) => sum + Buffer.byteLength(file.content, 'utf8'), 0) > 2 * 1024 * 1024) conflicts.push({ path: '(整批)', workerIds: group.children.map(child => child.id), reason: '合并超过32文件或2MB建议内容,请缩小交付范围。' }); await this.sourceFor(group); return { schemaVersion: 1, collaborationId: group.id, groupFingerprint: group.fingerprint, projectId: group.projectId, sessionId: session.id, taskId: group.taskId, runId: group.runId, source: group.source.identity, workers: proofs.map(proof => ({ workerId: proof.worker.id, artifactId: proof.artifact.id, sha256: proof.artifact.sha256, fingerprint: proof.worker.fingerprint, checkJobId: proof.output.check.id, paths: proof.output.files.map(file => file.path) })), paths: all.map(file => file.path).sort(), conflicts, targets: targets.sort((a, b) => a.path.localeCompare(b.path)) }; } collect(session, id, fingerprint) { const group = this.own(session, id, fingerprint); if (!['ready_to_collect', 'collected', 'conflict'].includes(group.status) || group.requiresVerification || group.merge) throw workerConflict('先完成两个子任务,已有合并审查不能重新归集。'); return this.launch(session, group, 'collecting', async signal => { try { const collection = await this.collectionFor(session, group); signal.throwIfAborted(); const artifact = this.store.artifactStore.write(session, this.store.owned('run', group.runId, session.id), collection, { kind: 'worker-collection' }); this.store.core.artifact.set(artifact.id, artifact); group.collection = { artifactId: artifact.id, sha256: artifact.sha256, fingerprint: workerHash(collection), conflicts: collection.conflicts, paths: collection.paths }; group.status = collection.conflicts.length ? 'conflict' : 'collected'; group.summary = collection.conflicts.length ? '原成果已封存,存在具体路径冲突;父工程未改变。' : '原子成果、当前检查和来源均已核对,可以准备父Task的独立变更集审查。'; } catch (error) { group.status = signal.aborted ? 'ready_to_collect' : 'stale'; group.summary = this.runtime.provider.redact(error.message ?? String(error)); } this.save(session, group, group.status); return group; }); } async readCollection(session, group) { this.own(session, group.id, null, { current: false }); if (!group.collection) throw workerConflict('当前协作尚未封存成果归集。'); const artifact = this.store.owned('artifact', group.collection.artifactId, session.id); if (artifact.kind !== 'worker-collection' || artifact.taskId !== group.taskId || artifact.runId !== group.runId || artifact.sha256 !== group.collection.sha256) throw workerConflict('成果归集工件归属或散列不一致。'); const record = JSON.parse(this.store.artifactStore.read(artifact)); if (record.collaborationId !== group.id || record.groupFingerprint !== group.fingerprint || workerHash(record) !== group.collection.fingerprint) throw workerConflict('成果归集与原协作不一致。'); return record; } async verifyCollection(session, group) { const original = await this.readCollection(session, group), current = await this.collectionFor(session, group); if (JSON.stringify(original) !== JSON.stringify(current) || current.conflicts.length) throw workerConflict('来源、原成果或检查证据已变化,不能使用旧合并审查。'); return original; } prepareMerge(session, id, fingerprint) { const group = this.own(session, id, fingerprint); if (group.status !== 'collected' || group.merge && group.merge.status !== 'proposing' || group.requiresVerification) throw workerConflict('只有没有冲突的原成果可以准备一次父合并审查。'); return this.launch(session, group, 'merge_preparing', async signal => { try { const collection = await this.verifyCollection(session, group); signal.throwIfAborted(); if (session.changes.some(change => ['pending', 'conflict'].includes(change.status) && change.sourceBinding?.collaborationId !== group.id)) throw workerConflict('父Task已有未处理提案,不能混入子成果。'); group.merge ??= { collectionSha256: group.collection.sha256, changeIds: [], status: 'proposing' }; const recovered = this.mergeProposals(session, group, collection); group.merge.changeIds = recovered.map(change => change.id); this.save(session, group, 'merge_intent'); for (const file of collection.targets) { signal.throwIfAborted(); if (recovered.some(change => change.path === file.path)) continue; const proof = collection.workers.find(value => value.workerId === file.workerId); const result = proposeFile(this.store, session, { path: file.path, base_sha256: file.beforeSha256, content: file.content, reason: '合并已核验子任务「' + file.label + '」的原成果' }, { sourceBinding: { collaborationId: group.id, workerId: file.workerId, resultArtifactId: proof.artifactId, resultSha256: proof.sha256, collectionSha256: group.collection.sha256 } }); if (!result.change_id) throw workerConflict('原成果没有形成新的父提案,不能补造合并成功。'); group.merge.changeIds.push(result.change_id); this.save(session, group, 'merge_proposal'); await this.faultInjector?.('merge_proposal', group, file); } await this.verifyCollection(session, group); signal.throwIfAborted(); const manager = new ChangeSetManager(this.store, { projectBusy: (projectId, exceptId) => this.runtime.projectHasActivity(projectId, exceptId, { excludeGroupId: group.id }) }); const set = await manager.prepare(session, group.merge.changeIds, { collaborationBinding: { id: group.id, fingerprint: group.fingerprint, collectionArtifactId: group.collection.artifactId, collectionSha256: group.collection.sha256 } }); group.merge.changeSetId = set.id; group.merge.fingerprint = set.fingerprint; group.merge.status = 'prepared'; group.status = 'merge_prepared'; group.summary = '父Task独立提案与原字节变更集已封存,等待明确审查应用;父工程尚未修改。'; session.status = 'needs_review'; } catch (error) { group.status = group.merge ? 'unknown' : 'stale'; group.requiresVerification = !!group.merge; group.summary = this.runtime.provider.redact(error.message ?? String(error)); } this.save(session, group, group.status); return group; }); } mergeProposals(session, group, collection) { const changes = session.changes.filter(change => change.sourceBinding?.collaborationId === group.id); if (new Set(changes.map(change => workerPathKey(change.path))).size !== changes.length || group.merge.collectionSha256 !== group.collection.sha256 || group.merge.changeIds.some(id => !changes.some(change => change.id === id))) throw workerConflict('原合并提案账本不连续,不能补造或重放。'); for (const change of changes) { const target = collection.targets.find(file => file.path === change.path), proof = collection.workers.find(worker => worker.workerId === target?.workerId); if (!target || !proof || change.taskId !== group.taskId || change.runId !== group.runId || change.status !== 'pending' || change.changeSetId || change.baseSha256 !== target.beforeSha256 || change.sha256 !== target.sha256 || workerHash(change.content) !== target.sha256 || change.sourceBinding.workerId !== target.workerId || change.sourceBinding.resultArtifactId !== proof.artifactId || change.sourceBinding.resultSha256 !== proof.sha256 || change.sourceBinding.collectionSha256 !== group.collection.sha256) throw workerConflict('原父提案与封存子成果不一致,不能续封合并。'); } return collection.targets.flatMap(file => changes.filter(change => change.path === file.path)); } async recoverMerge(session, group) { if (!group.merge) return; const sets = [...this.store.core.change_set.values()].filter(set => set.collaborationBinding?.id === group.id || set.id === group.merge.changeSetId); if (sets.length > 1) throw workerConflict('原协作存在多个父批次,不能猜测或重放。'); if (sets.length) { const set = sets[0], binding = set.collaborationBinding; if (set.sessionId !== session.id || set.taskId !== group.taskId || binding?.fingerprint !== group.fingerprint || binding.collectionArtifactId !== group.collection?.artifactId || binding.collectionSha256 !== group.collection?.sha256 || !['prepared', 'completed'].includes(set.status) || set.requiresVerification || JSON.stringify(set.files.map(file => file.changeId)) !== JSON.stringify(group.merge.changeIds)) throw workerConflict('父批次仍需原恢复流程核对或只能保留现场结束。'); this.runtime.changeSetManager().snapshots(session, set); group.merge.changeSetId = set.id; group.merge.fingerprint = set.fingerprint; group.merge.status = 'prepared'; } else { const collection = await this.verifyCollection(session, group); group.merge.changeIds = this.mergeProposals(session, group, collection).map(change => change.id); group.merge.status = 'proposing'; } } async verifyMerge(session, set) { const linked = this.groups(session).find(group => group.merge?.changeSetId === set.id); if (!set.collaborationBinding && !linked) return; const group = this.own(session, set.collaborationBinding?.id ?? linked.id, set.collaborationBinding?.fingerprint ?? linked.fingerprint); if (group.status !== 'merge_prepared' || group.requiresVerification || group.merge?.changeSetId !== set.id || group.merge.fingerprint !== set.fingerprint || set.collaborationBinding?.collectionArtifactId !== group.collection?.artifactId || set.collaborationBinding?.collectionSha256 !== group.collection?.sha256 || JSON.stringify(set.files.map(file => file.changeId)) !== JSON.stringify(group.merge.changeIds)) throw workerConflict('父变更集与原协作/归集记录不一致,不能应用。'); const collection = await this.verifyCollection(session, group); for (const file of set.files) { const target = collection.targets.find(value => value.path === file.path), change = session.changes.find(value => value.id === file.changeId); if (!target || target.sha256 !== file.sha256 || target.beforeSha256 !== file.baseSha256 || change.sourceBinding?.collaborationId !== group.id || change.sourceBinding?.workerId !== target.workerId || change.sourceBinding?.collectionSha256 !== group.collection.sha256) throw workerConflict('父提案的原子成果绑定失效。'); } } async finalize(session, id, fingerprint) { const group = this.own(session, id, fingerprint); this.assertIdle(session); if (group.status !== 'merge_prepared' || !group.merge?.changeSetId) throw workerConflict('先审查应用原父变更集,再核对交付。'); const set = this.store.owned('change_set', group.merge.changeSetId, session.id); if (set.status !== 'completed' || set.requiresVerification || set.fingerprint !== group.merge.fingerprint) throw workerConflict('原父变更集没有完整审查应用结果。'); this.runtime.changeSetManager().snapshots(session, set); const inspection = await inspectProject(this.store.project(group.projectId)), check = verifiedCheck(this.store, session, inspection); if (!check) throw workerConflict('合并后父工程需要当前源码的真实通过检查。'); for (const file of set.files) if (readText(this.store.project(group.projectId), file.path).sha256 !== file.sha256) throw workerConflict('父合并文件已变化,请先审查实际修复与重新归集。'); const delivery = [...this.store.core.delivery_job.values()].find(job => job.taskId === group.taskId && job.sessionId === session.id && job.status === 'completed' && !job.requiresVerification && job.commit === inspection.git.head && job.sourceFingerprint === inspection.sourceFingerprint); if (!delivery || !delivery.reportArtifactId) throw workerConflict('父工程尚未完成当前成果的本地Git交付。'); const report = this.store.owned('artifact', delivery.reportArtifactId, session.id); this.store.artifactStore.read(report); for (const child of group.children) await this.verifiedResult(group, child); group.delivery = { jobId: delivery.id, commit: delivery.commit, checkJobId: check.id, reportArtifactId: report.id, reportSha256: report.sha256 }; group.status = 'completed'; group.finishedAt = at(); group.summary = group.children.length + ' 个隔离子成果已审查合并,父工程重新检查通过并完成本地Git交付。'; session.status = 'completed'; this.save(session, group, 'completed'); return group; } async observe(group) { const source = await this.workspaces.snapshot(this.store.project(group.projectId)), children = []; for (const child of group.children) { const workspace = this.store.core.workspace.get(child.workspaceId), worker = this.store.core.worker.get(child.id); if (alive(workspace?.ownerPid) || alive(workspace?.gitPid) || alive(worker?.ownerPid)) throw workerConflict('历史所属PID仍存活,请先人工核对进程归属;工作台不会按历史PID停止进程。'); let inventory = null, inventoryError = null; if (worker) { this.baseline(this.store.session(child.sessionId), worker); try { inventory = await this.inventory(this.store.project(child.projectId)); } catch (error) { inventoryError = error.message; } } const checks = []; for (const job of [...this.store.core.check_job.values()].filter(value => value.taskId === child.taskId)) { const processes = [job.currentCheck?.process, ...(job.unreportedProcesses ?? []), ...job.results.map(result => result.process)].filter(Boolean); if (processes.some(processInfo => !processInfo.rootExited && alive(processInfo.pid))) throw workerConflict('原检查PID仍存活,结束状态需要人工核对;不能自动重试或按历史PID终止。'); if (job.requiresVerification || job.status === 'unknown') for (const processInfo of processes) { const tree = await readOwnedProcessTree(processInfo); if (tree.processes.length) throw workerConflict('原检查子进程PID仍存活:' + tree.processes.map(value => value.pid).join('、') + ';不能按历史PID自动终止或解除核对。'); } checks.push({ id: job.id, status: job.status, requiresVerification: !!job.requiresVerification, processes, results: job.results.map(result => ({ id: result.id, status: result.status, logArtifactId: result.logArtifactId, logSha256: this.store.core.artifact.get(result.logArtifactId)?.sha256 ?? null })) }); } const session = this.store.sessions.get(child.sessionId); const engines = []; for (const job of this.store.core.engine_run.values()) if (job.taskId === child.taskId) { verifyEngineInput(this.store, session, job); if (job.reportArtifactId) originalEngineRun(this.store, session, job); const processInfo = job.process ?? job.native?.process; let tree = null; if (job.requiresVerification || job.status === 'unknown') { if (processInfo) { tree = await readOwnedProcessTree(processInfo); if (!tree.supported || tree.processes.length) throw workerConflict('原引擎进程树仍存活或无法核对,请先人工核对原创建身份;不会按历史PID停止。'); } } engines.push({ id: job.id, status: job.status, requiresVerification: !!job.requiresVerification, inputArtifactId: job.inputArtifactId, inputSha256: job.inputSha256, reportArtifactId: job.reportArtifactId ?? null, reportSha256: job.reportSha256 ?? null, process: processInfo ?? null, remainingProcesses: tree?.processes ?? null, missingProcessIdentity: !processInfo && job.status === 'unknown', requests: job.requests, calls: job.calls, originalOutcome: job.originalOutcome ?? null, error: job.error ?? null }); } const tools = [...this.store.core.tool.values()].filter(value => value.taskId === child.taskId && value.requiresVerification).map(tool => { const call = session?.providerMessages.flatMap(message => message.tool_calls ?? []).find(call => call.id === tool.callId); const input = call?.function.arguments; if (typeof input !== 'string' || workerHash(input) !== tool.argsSha256) throw workerConflict('原未知工具的输入哈希不一致,请保留现场。'); return { id: tool.id, callId: tool.callId, tool: tool.tool, state: tool.state, argsSha256: tool.argsSha256, input: this.runtime.provider.redact(input).slice(0, 24000), inputTruncated: input.length > 24000, resultArtifactId: tool.resultArtifactId }; }); const changes = (session?.changes ?? []).filter(change => change.taskId === child.taskId).map(change => { let sha256 = null, error = null; try { const target = safePath(this.store.project(child.projectId), change.path, true); sha256 = fs.existsSync(target) ? readText(this.store.project(child.projectId), change.path).sha256 : null; } catch (failure) { error = failure.message; } return { id: change.id, path: change.path, status: change.status, baseSha256: change.baseSha256, proposedSha256: change.sha256, actualSha256: sha256, error }; }); children.push({ id: child.id, workspaceId: child.workspaceId, workspaceStatus: workspace?.status ?? 'absent', workerStatus: worker?.status ?? 'absent', taskId: child.taskId, sessionId: child.sessionId, projectId: child.projectId, inventoryFingerprint: inventory ? workerHash(inventory.files) : null, sourceFingerprint: inventory?.inspection.sourceFingerprint ?? null, inventoryError, tools, checks, changes, ...(engines.length ? { engines } : {}), changeSets: [...this.store.core.change_set.values()].filter(set => set.taskId === child.taskId).map(set => ({ id: set.id, status: set.status, requiresVerification: !!set.requiresVerification, observationFingerprint: set.observationFingerprint ?? null })) }); } const parent = this.store.session(group.sessionId); return { schemaVersion: 1, kind: 'worker-recovery-observation', collaborationId: group.id, groupFingerprint: group.fingerprint, projectId: group.projectId, sessionId: group.sessionId, taskId: group.taskId, runId: group.runId, source: sourceIdentity(source), children, parentChanges: parent.changes.filter(change => change.sourceBinding?.collaborationId === group.id).map(change => ({ id: change.id, path: change.path, status: change.status, sha256: change.sha256 })), parentChangeSets: [...this.store.core.change_set.values()].filter(set => set.taskId === group.taskId).map(set => ({ id: set.id, status: set.status, requiresVerification: !!set.requiresVerification })), note: '原输入及当前现场供用户核对;未报告的检查或工具结果保持未知,不补造完成、不执行模型或旧工具。' }; } reconcile(session, id, fingerprint) { const group = this.own(session, id, fingerprint); if (!group.requiresVerification || group.status !== 'unknown') throw workerConflict('只有原结果未知的协作需要现场核对。'); return this.launch(session, group, 'reconciling', async signal => { this.preparingGroupId = group.id; try { for (const child of group.children) { const workspace = this.store.core.workspace.get(child.workspaceId); if (workspace?.status === 'unknown') await this.workspaces.reconcile(session, workspace.id, workspace.fingerprint); if (workspace?.status === 'ready' && !this.store.core.worker.has(child.id)) await this.initializeChild(group, child); } const observed = await this.observe(group); signal.throwIfAborted(); const artifact = this.store.artifactStore.write(session, this.store.owned('run', group.runId, session.id), observed, { kind: 'worker-recovery' }); this.store.core.artifact.set(artifact.id, artifact); group.evidence = { artifactId: artifact.id, sha256: artifact.sha256 }; group.summary = '原输入与现场核对已封存,请阅读后明确确认;不会自动发送模型、重放工具或补造检查通过。'; } catch (error) { group.summary = this.runtime.provider.redact(error.message ?? String(error)); } group.status = 'unknown'; group.requiresVerification = true; this.save(session, group, 'observed'); return group; }); } readEvidence(session, group) { this.own(session, group.id, null, { current: false }); const artifact = this.store.owned('artifact', group.evidence?.artifactId, session.id); if (artifact.kind !== 'worker-recovery' || artifact.taskId !== group.taskId || artifact.runId !== group.runId || artifact.sha256 !== group.evidence.sha256) throw workerConflict('原核对工件不属于当前父协作。'); const record = JSON.parse(this.store.artifactStore.read(artifact)); if (record.groupFingerprint !== group.fingerprint || record.collaborationId !== group.id) throw workerConflict('核对记录与原协作不一致。'); return record; } async acknowledge(session, id, fingerprint, evidenceSha256, retain = false) { const group = this.own(session, id, fingerprint); if (group.status !== 'unknown' || !group.requiresVerification || typeof evidenceSha256 !== 'string' || evidenceSha256 !== group.evidence?.sha256) throw workerConflict('请先阅读原输入与现场核对工件,再提交原SHA256。'); const started = this.launch(session, group, 'reconciling', async signal => { this.preparingGroupId = group.id; try { const original = this.readEvidence(session, group), current = await this.observe(group); signal.throwIfAborted(); if (JSON.stringify(original) !== JSON.stringify(current)) throw workerConflict('原输入或现场在核对后发生变化,请重新核对。'); const unresolvedSets = current.children.some(child => child.changeSets.some(set => set.requiresVerification)) || current.parentChangeSets.some(set => set.requiresVerification); if (unresolvedSets) throw workerConflict('原文件批次仍待核对或回退,请先在所属任务记录处理实际原字节。'); const merged = group.merge?.changeSetId && this.store.core.change_set.get(group.merge.changeSetId)?.status === 'completed'; if (!retain && !merged && JSON.stringify(current.source) !== JSON.stringify(group.source.identity)) throw workerConflict('父来源已经变化,只能明确保留现场后重新规划。'); if (current.children.some(child => !['ready', 'absent', 'prepared', 'discarded', 'resolved_external'].includes(child.workspaceStatus))) throw workerConflict('原工作区仍需专门核对,不能开始子执行或结束恢复。'); if (!retain && current.children.some(child => ['discarded', 'resolved_external'].includes(child.workspaceStatus))) throw workerConflict('原工作区没有核对为可执行,只能明确保留现场结束。'); if (!retain && current.children.some(child => child.engines?.some(job => job.missingProcessIdentity))) throw workerConflict('引擎原进程创建身份没有封存,只能人工处理后明确保留现场结束。'); if (!retain) await this.recoverMerge(session, group); for (const child of group.children) { const worker = this.store.core.worker.get(child.id); if (!worker) continue; const childSession = this.store.session(child.sessionId); for (const job of this.store.core.engine_run.values()) if (job.taskId === child.taskId && job.requiresVerification) { job.review = { source: 'user', evidenceSha256, reviewedAt: at(), result: 'unknown-preserved' }; job.status = 'resolved_external'; job.requiresVerification = false; } for (const tool of this.store.core.tool.values()) if (tool.taskId === child.taskId && tool.requiresVerification) { tool.originalState = tool.state; tool.state = 'resolved_external'; tool.requiresVerification = false; tool.review = { source: 'user', evidenceSha256, reviewedAt: at(), result: 'unknown-preserved' }; const run = this.store.core.run.get(tool.runId); if (run) { run.checkpoint.requiresVerification = run.checkpoint.requiresVerification.filter(id => id !== tool.id); run.checkpoint.pendingToolIds = run.checkpoint.pendingToolIds.filter(id => id !== tool.id); run.checkpoint.reviewedUnknownToolIds ??= []; if (!run.checkpoint.reviewedUnknownToolIds.includes(tool.id)) run.checkpoint.reviewedUnknownToolIds.push(tool.id); } } for (const job of this.store.core.check_job.values()) if (job.taskId === child.taskId && (job.requiresVerification || job.status === 'unknown')) { job.originalStatus = job.status; job.status = 'resolved_external'; job.requiresVerification = false; job.review = { evidenceSha256, reviewedAt: at() }; this.store.core.task.get(child.taskId).verification = { status: 'needs_checks', checkJobId: job.id }; } if (worker.requiresVerification || ['unknown', 'running', 'stopping'].includes(worker.status)) { worker.requiresVerification = false; worker.status = retain ? 'resolved_external' : 'paused'; worker.ownerPid = null; worker.summary = '用户已核对原未知输入与现场;原结果保持未知,重新检查需要新计划与审批。'; childSession.providerMessages = repairToolPairs(childSession.providerMessages, callId => this.store.savedToolResult(childSession.id, callId)).messages; childSession.providerMessages.push({ role: 'user', content: worker.summary + ' 核对记录SHA256:' + evidenceSha256 }); childSession.status = childSession.changes.some(change => ['pending', 'conflict'].includes(change.status)) ? 'needs_review' : 'paused'; this.store.putRecord('worker', childSession, worker, 'worker.reviewed_unknown'); } } group.requiresVerification = false; if (retain) { this.retain(session, group); group.status = 'resolved_external'; group.finishedAt = at(); } else group.status = merged || group.merge?.changeSetId ? 'merge_prepared' : group.merge ? 'collected' : group.children.some(child => !this.store.core.worker.has(child.id)) ? 'partially_prepared' : group.children.every(child => this.store.core.worker.get(child.id).status === 'completed') ? 'ready_to_collect' : group.children.some(child => this.store.session(child.sessionId).status === 'needs_review') ? 'awaiting_review' : group.startedAt ? 'paused' : 'ready'; group.summary = retain ? '用户已明确保留原工作区、任务与未知结果,结束协作;没有声明交付成功。' : '原未知结果已按用户核对保留,可以显式继续;累计预算和原时限保持。'; session.status = 'paused'; this.save(session, group, 'reviewed_unknown'); return group; } catch (error) { group.status = 'unknown'; group.requiresVerification = true; this.save(session, group, 'review_failed'); throw error; } }); return started.promise; } async stopParent(sessionId) { const running = [...this.active.values()].filter(value => value.sessionId === sessionId && value.controller); for (const value of running) { if (this.store.core.collaboration.has(value.group.id)) { value.group.status = 'stopping'; this.save(this.store.session(sessionId), value.group, 'stopping'); } value.controller.abort(new Error('父任务已停止。')); await this.workspaces.stop(sessionId); await Promise.allSettled(value.group.children.map(child => this.runtime.stop(child.sessionId))); await value.promise; } for (const group of this.store.core.collaboration.values()) if (group.sessionId === sessionId && !COLLABORATION_CLOSED.has(group.status) && group.children.some(child => this.childActive(child.sessionId))) { await Promise.allSettled(group.children.filter(child => this.store.sessions.has(child.sessionId)).map(child => this.runtime.stop(child.sessionId))); group.summary = '父任务已等待所属子执行与检查结束,已完成记录保留。'; this.save(this.store.session(sessionId), group, 'stopped'); } } async stopAll() { await Promise.all([...new Set([...this.active.values()].map(value => value.sessionId).concat([...this.store.core.collaboration.values()] .filter(group => !COLLABORATION_CLOSED.has(group.status) && group.children.some(child => this.childActive(child.sessionId))).map(group => group.sessionId)))].map(id => this.stopParent(id))); } retain(session, group) { const sessions = [session, ...group.children.flatMap(child => this.store.sessions.has(child.sessionId) ? [this.store.session(child.sessionId)] : [])]; for (const child of group.children) { const workspace = this.store.core.workspace.get(child.workspaceId); if (workspace?.status === 'prepared' && !workspace.creationAttempted) this.workspaces.discard(session, workspace.id, workspace.fingerprint); } const manager = new ChangeSetManager(this.store, { projectBusy: (projectId, exceptId) => this.runtime.projectHasActivity(projectId, exceptId, { excludeGroupId: group.id }) }); for (const owned of sessions) { for (const set of this.store.core.change_set.values()) if (set.sessionId === owned.id && set.taskId === owned.taskId && !set.attemptedAt && ['prepared', 'stale'].includes(set.status)) { manager.snapshots(owned, set); manager.discard(owned, set.id, set.fingerprint); } for (const change of owned.changes) if ((owned !== session || change.sourceBinding?.collaborationId === group.id) && ['pending', 'conflict'].includes(change.status) && !change.changeSetId) change.status = 'rejected'; const worker = workerForProject(this.store, owned.projectId); if (worker && worker.status !== 'completed') { worker.status = 'resolved_external'; worker.requiresVerification = false; worker.ownerPid = null; } owned.status = 'paused'; this.store.save(owned, { type: 'worker.retained' }); } } discard(session, id, fingerprint) { const group = this.own(session, id, fingerprint); if (this.runtime.projectHasActivity(group.projectId) || group.requiresVerification || group.children.some(child => this.store.core.worker.get(child.id)?.requiresVerification) || [...this.store.core.change_set.values()].some(set => (set.taskId === group.taskId || group.children.some(child => child.taskId === set.taskId)) && set.requiresVerification)) throw workerConflict('协作仍在执行或原批次待核对,请先处理原记录。'); this.retain(session, group); group.status = group.children.some(child => this.store.core.workspace.has(child.workspaceId)) ? 'resolved_external' : 'discarded'; group.finishedAt = at(); group.summary = '已明确保留原工作区、任务和成果,结束本次协作;未删除、重置或声明交付完成。'; session.status = 'paused'; this.save(session, group, 'discarded'); return group; } }