feat: seal isolated worker recovery, parent integration and review console
This commit is contained in:
@@ -14,6 +14,7 @@ const conflict = message => Object.assign(new Error(message), { status: 409 });
|
||||
const key = value => process.platform === 'win32' ? value.toLowerCase() : value;
|
||||
const gitBaseline = inspection => ({ available: inspection.git.available, head: inspection.git.head ?? null, branch: inspection.git.branch ?? null });
|
||||
const sealed = set => ({ id: set.id, sessionId: set.sessionId, projectId: set.projectId, taskId: set.taskId, runId: set.runId,
|
||||
...(set.collaborationBinding ? { collaborationBinding: set.collaborationBinding } : {}),
|
||||
root: set.root, baseline: set.baseline, files: set.files.map(file => ({ changeId: file.changeId, path: file.path,
|
||||
baseExists: file.baseExists, baseSha256: file.baseSha256, sha256: file.sha256, baseBytes: file.baseBytes, bytes: file.bytes,
|
||||
mode: file.mode, reason: file.reason, sourceRunId: file.sourceRunId, snapshotArtifactId: file.snapshotArtifactId })) });
|
||||
@@ -70,8 +71,8 @@ function base64(value) {
|
||||
}
|
||||
|
||||
export class ChangeSetManager {
|
||||
constructor(store, { projectBusy = () => false, faultInjector = null } = {}) {
|
||||
this.store = store; this.projectBusy = projectBusy; this.faultInjector = faultInjector;
|
||||
constructor(store, { projectBusy = () => false, faultInjector = null, beforeApply = null } = {}) {
|
||||
this.store = store; this.projectBusy = projectBusy; this.faultInjector = faultInjector; this.beforeApply = beforeApply;
|
||||
this.active = new Map(); this.projects = new Map();
|
||||
}
|
||||
projectActive(id) { return this.projects.has(id); }
|
||||
@@ -81,8 +82,8 @@ export class ChangeSetManager {
|
||||
await new Promise(resolve => setImmediate(resolve));
|
||||
}
|
||||
save(session, set, phase) { this.store.save(session, { type: 'change_set.' + phase, detail: { changeSetId: set.id } }); }
|
||||
requireIdle(session, { source = 'user', exceptId = null, includePrepared = false } = {}) {
|
||||
assertWorkersResolved(this.store, session.projectId);
|
||||
requireIdle(session, { source = 'user', exceptId = null, includePrepared = false, recoveryTaskId = null } = {}) {
|
||||
assertWorkersResolved(this.store, session.projectId, { recoveryTaskId });
|
||||
assertMcpResolved(this.store, session.projectId);
|
||||
if (this.projectActive(session.projectId) || this.projectBusy(session.projectId, source === 'agent' ? session.id : null)) {
|
||||
throw conflict('同一项目正在执行任务、检查、提交或文件批次,请等待结束后再操作。');
|
||||
@@ -128,7 +129,7 @@ export class ChangeSetManager {
|
||||
for (let index = 0; index < set.files.length; index++) set.files[index].state = observations[index].state;
|
||||
return observations;
|
||||
}
|
||||
async prepare(session, changeIds, { source = 'user' } = {}) {
|
||||
async prepare(session, changeIds, { source = 'user', collaborationBinding = null } = {}) {
|
||||
if (!Array.isArray(changeIds) || !changeIds.length || changeIds.length > 32 || new Set(changeIds).size !== changeIds.length) throw conflict('变更集需要1到32个不同的提案ID。');
|
||||
this.requireIdle(session, { source });
|
||||
const task = this.store.owned('task', session.taskId, session.id), run = this.store.owned('run', session.runId, session.id);
|
||||
@@ -152,6 +153,7 @@ export class ChangeSetManager {
|
||||
const inspection = await inspectProject(project);
|
||||
if (session.taskId !== task.id || session.runId !== run.id) throw conflict('准备变更集期间任务已切换。');
|
||||
const set = { id: randomUUID(), sessionId: session.id, projectId: project.id, taskId: task.id, runId: run.id, root: project.root,
|
||||
...(collaborationBinding ? { collaborationBinding: structuredClone(collaborationBinding) } : {}),
|
||||
schemaVersion: 1, status: 'prepared', requiresVerification: false, source, createdAt: at(), baseline: gitBaseline(inspection), files: [],
|
||||
policy: 'reviewed-byte-snapshots', summary: '整批范围已封存,项目文件尚未改变。' };
|
||||
let budget = 0;
|
||||
@@ -191,6 +193,7 @@ export class ChangeSetManager {
|
||||
const operation = this.claim(session, set);
|
||||
try {
|
||||
await verifyWorkspaceProject(this.store, this.store.project(session.projectId));
|
||||
await this.beforeApply?.(session, set);
|
||||
const snapshots = this.snapshots(session, set, { pending: true });
|
||||
const project = this.store.project(session.projectId), inspection = await inspectProject(project);
|
||||
if (set.taskId !== session.taskId || JSON.stringify(gitBaseline(inspection)) !== JSON.stringify(set.baseline)) throw conflict('任务或Git HEAD/分支已变化,未写入任何文件。');
|
||||
@@ -308,7 +311,7 @@ export class ChangeSetManager {
|
||||
async reconcile(session, id) {
|
||||
const set = this.own(session, id);
|
||||
if (!set.attemptedAt || !['unknown', 'partial', 'completed', 'rolled_back', 'interrupted'].includes(set.status)) throw conflict('这个批次尚未执行或已结束外部改动确认,无需恢复核对。');
|
||||
this.requireIdle(session, { exceptId: set.id });
|
||||
this.requireIdle(session, { exceptId: set.id, recoveryTaskId: set.taskId });
|
||||
await verifyWorkspaceProject(this.store, this.store.project(session.projectId));
|
||||
this.snapshots(session, set);
|
||||
return this.settle(session, set, this.inspect(session, set), set.direction === 'rollback' ? 'rollback' : 'apply');
|
||||
@@ -316,7 +319,7 @@ export class ChangeSetManager {
|
||||
async rollback(session, id, fingerprint) {
|
||||
const set = this.own(session, id, fingerprint, { current: true });
|
||||
if (typeof fingerprint !== 'string' || !set.attemptedAt || !['completed', 'partial', 'unknown'].includes(set.status)) throw conflict('只有已执行且已审查指纹的批次可以回退。');
|
||||
this.requireIdle(session, { exceptId: set.id });
|
||||
this.requireIdle(session, { exceptId: set.id, recoveryTaskId: set.taskId });
|
||||
const snapshots = this.snapshots(session, set), operation = this.claim(session, set);
|
||||
try {
|
||||
await verifyWorkspaceProject(this.store, this.store.project(session.projectId));
|
||||
@@ -334,7 +337,7 @@ export class ChangeSetManager {
|
||||
acknowledge(session, id, fingerprint, observationFingerprint) {
|
||||
const set = this.own(session, id, fingerprint, { current: true });
|
||||
if (typeof fingerprint !== 'string' || set.status !== 'partial' || !set.requiresVerification || typeof observationFingerprint !== 'string') throw conflict('请先核对这个批次的当前文件,再确认保留外部改动。');
|
||||
this.requireIdle(session, { exceptId: set.id }); this.snapshots(session, set);
|
||||
this.requireIdle(session, { exceptId: set.id, recoveryTaskId: set.taskId }); this.snapshots(session, set);
|
||||
const observations = this.inspect(session, set);
|
||||
if (set.observationFingerprint !== observationFingerprint || observations.some(value => !['before', 'external'].includes(value.state))) throw conflict('当前文件变化或仍有本批次建议内容未回退,请先重新核对/回退。');
|
||||
set.status = 'resolved_external'; set.requiresVerification = false; set.finishedAt = at();
|
||||
@@ -348,7 +351,7 @@ export class ChangeSetManager {
|
||||
discard(session, id, fingerprint) {
|
||||
const set = this.own(session, id, fingerprint, { current: true });
|
||||
if (typeof fingerprint !== 'string' || !['prepared', 'stale'].includes(set.status) || set.attemptedAt) throw conflict('只有尚未执行的封存批次可以撤销。');
|
||||
this.requireIdle(session, { exceptId: set.id });
|
||||
this.requireIdle(session, { exceptId: set.id, recoveryTaskId: set.taskId });
|
||||
set.status = 'cancelled'; set.finishedAt = at(); set.summary = '已撤销未执行批次,文件提案仍可重新审查。';
|
||||
for (const file of set.files) { const change = session.changes.find(value => value.id === file.changeId); if (change?.changeSetId === set.id) delete change.changeSetId; }
|
||||
this.save(session, set, 'cancelled'); return set;
|
||||
|
||||
Reference in New Issue
Block a user