feat: add reviewed Task Git delivery and verified evidence reports

This commit is contained in:
Agent Workbench Dev
2026-10-29 16:41:03 +08:00
parent 3781591f97
commit 73758985ee
50 changed files with 1964 additions and 69 deletions
+127
View File
@@ -0,0 +1,127 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { spawn } from 'node:child_process';
import { fixture, until, workspace } from './helpers.mjs';
import { checkedDelivery, deliveryGit as git, writeDeliveryFile as write } from './fixtures/delivery-project.mjs';
import { Store } from '../src/server/store.mjs';
import { GitDelivery } from '../src/core/git-delivery.mjs';
import { inspectProject } from '../src/core/project-inspector.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { Provider } from '../src/server/provider.mjs';
import { proposeFile, applyChange } from '../src/server/files.mjs';
const alive = pid => { if (!pid) return false; try { process.kill(pid, 0); return true; } catch { return false; } };
for (const phase of ['queued', 'preparing', 'index_locked', 'prepared', 'ref_prepared', 'publishing', 'committed', 'index_published', 'finished']) {
test('actual process exit at Git ' + phase + ' never replays a commit and can reconcile the durable result', { timeout: 60000 }, async t => {
const f = fixture(t), { delivery } = await checkedDelivery(f), plan = await delivery.plan(f.session);
const parent = git(f, ['rev-parse', 'HEAD']), stage = git(f, ['show', ':owner-staged.txt']), source = (await inspectProject(f.project)).sourceFingerprint;
const owner = fs.readFileSync(path.join(f.projectRoot, 'owner-staged.txt'));
f.store.close();
const child = spawn(process.execPath, [path.join(workspace, 'tests/fixtures/delivery-crash-worker.mjs'), f.data, f.session.id, plan.id, phase],
{ windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });
let output = '', errors = ''; child.stdout.on('data', bytes => { output += bytes.toString('utf8'); }); child.stderr.on('data', bytes => { errors += bytes.toString('utf8'); });
t.after(() => { if (child.exitCode === null && child.signalCode === null) child.kill(); });
await until(() => child.exitCode !== null, 25000); assert.equal(child.exitCode, 73, errors);
const cut = JSON.parse(output.trim()), recovered = new Store(f.data); f.onCleanup(() => recovered.close());
const session = recovered.session(f.session.id), job = recovered.owned('delivery_job', cut.jobId, session.id), adapter = new GitDelivery(recovered);
f.onCleanup(() => adapter.stopAll());
const published = ['committed', 'index_published', 'finished'].includes(phase);
assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), published ? '2' : '1');
if (!published) assert.equal(git(f, ['rev-parse', 'HEAD']), parent);
if (['queued', 'preparing'].includes(phase)) assert.ok(['interrupted', 'unknown'].includes(job.status));
else if (phase !== 'finished') assert.equal(job.status, 'unknown');
else assert.equal(job.status, 'completed');
await new Promise(resolve => setTimeout(resolve, 80)); assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), published ? '2' : '1');
await assert.rejects(adapter.start(session, plan.id), { status: 409 });
if (job.refProcessPid) await until(() => !alive(job.refProcessPid), 10000);
const reconciled = await adapter.reconcile(session, job.id);
assert.equal(reconciled.status, published ? 'completed' : 'interrupted', reconciled.summary); assert.equal(reconciled.requiresVerification, false);
if (published) assert.equal(reconciled.commit, cut.expectedCommit);
assert.equal(fs.existsSync(path.join(f.projectRoot, '.git/index.lock')), false);
assert.equal(git(f, ['show', ':owner-staged.txt']), stage); assert.deepEqual(fs.readFileSync(path.join(f.projectRoot, 'owner-staged.txt')), owner);
assert.equal((await inspectProject(f.project)).sourceFingerprint, source);
const report = await adapter.report(session); assert.equal(report.report.status, published ? 'verified_committed' : 'verified_uncommitted');
if (!published) {
const fresh = await adapter.plan(session), final = await (await adapter.start(session, fresh.id)).promise;
assert.equal(final.status, 'completed', final.summary); assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), '2');
}
});
}
test('an unknown Git result cannot overwrite an externally changed index or delete an unrelated lock', async t => {
const f = fixture(t); await checkedDelivery(f);
const adapter = new GitDelivery(f.store, { faultInjector: phase => { if (phase === 'committed') throw new Error('Synthetic lost result after actual publication'); } });
const plan = await adapter.plan(f.session), job = await (await adapter.start(f.session, plan.id)).promise;
assert.equal(job.status, 'unknown'); assert.equal(job.requiresVerification, true); assert.ok(job.expectedCommit);
const index = path.join(f.projectRoot, '.git/index'), lock = index + '.lock';
const original = fs.readFileSync(index); fs.appendFileSync(index, 'Owner changed index');
await assert.rejects(adapter.reconcile(f.session, job.id), /索引或遗留锁/);
assert.equal(fs.existsSync(lock), true); assert.ok(fs.readFileSync(index).includes(Buffer.from('Owner changed index')));
fs.writeFileSync(index, original); const ours = fs.readFileSync(lock); fs.writeFileSync(lock, 'Other operation owns this lock');
await assert.rejects(adapter.reconcile(f.session, job.id), /索引或遗留锁/); assert.equal(fs.readFileSync(lock, 'utf8'), 'Other operation owns this lock');
fs.writeFileSync(lock, ours);
const result = await adapter.reconcile(f.session, job.id); assert.equal(result.status, 'completed');
assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), '2');
});
test('known publication with a later source edit retains the commit and marks the delivery for inspection', async t => {
const f = fixture(t); await checkedDelivery(f);
let changed = false;
const adapter = new GitDelivery(f.store, { faultInjector: phase => { if (phase === 'index_published' && !changed) { changed = true; write(f, 'src/math.mjs', 'export const sum = (a, b) => a + b + 0;\n'); } } });
const job = await (await adapter.start(f.session, (await adapter.plan(f.session)).id)).promise;
assert.equal(job.status, 'unknown'); assert.equal(job.indexPreserved, true); assert.equal(job.workspacePreserved, false);
assert.equal(git(f, ['rev-parse', 'HEAD']).trim(), job.expectedCommit); assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), '2');
assert.equal(fs.readFileSync(path.join(f.projectRoot, 'src/math.mjs'), 'utf8'), 'export const sum = (a, b) => a + b + 0;\n');
const runtime = new Runtime(f.store, new Provider());
assert.throws(() => runtime.start(f.session.id, 'Start another task'), /结果未知/);
assert.equal((await adapter.report(f.session)).report.status, 'needs_verification');
write(f, 'src/math.mjs', 'export const sum = (a, b) => a + b;\n');
assert.equal((await adapter.reconcile(f.session, job.id)).status, 'completed');
});
test('CRLF normalization, multiple applied revisions and unrelated special index flags preserve exact bytes and ownership', async t => {
const f = fixture(t); await checkedDelivery(f);
git(f, ['config', 'core.autocrlf', 'true']);
git(f, ['update-index', '--assume-unchanged', '--', 'owner-unstaged.txt']);
const beforeFlags = git(f, ['ls-files', '-v', '--', 'owner-unstaged.txt']);
const latest = proposeFile(f.store, f.session, { path: 'src/math.mjs', content: 'export const sum = (a, b) => a + b + 0;\r\n', reason: 'Reviewed CRLF revision' });
applyChange(f.store, f.session, latest.change_id);
// This later applied revision intentionally invalidates old checks.
const { CheckRunner } = await import('../src/core/check-runner.mjs');
const checks = new CheckRunner(f.store); assert.equal((await (await checks.start(f.session, (await checks.plan(f.session)).id)).promise).status, 'passed');
const adapter = new GitDelivery(f.store), plan = await adapter.plan(f.session), job = await (await adapter.start(f.session, plan.id)).promise;
assert.equal(job.status, 'completed', job.summary); assert.equal(git(f, ['ls-files', '-v', '--', 'owner-unstaged.txt']), beforeFlags);
assert.equal(fs.readFileSync(path.join(f.projectRoot, 'src/math.mjs'), 'utf8'), 'export const sum = (a, b) => a + b + 0;\r\n');
assert.deepEqual(plan.files.find(file => file.path === 'src/math.mjs').changeIds, f.session.changes.filter(change => change.path === 'src/math.mjs').map(change => change.id));
});
test('adding new durable record kinds to an existing hot Store preserves all existing Maps and records', async t => {
const f = fixture(t); await checkedDelivery(f);
const oldCore = { ...f.store.core }; delete oldCore.delivery_plan; delete oldCore.delivery_job;
f.store.core = oldCore; const tasks = f.store.core.task, sessions = f.store.sessions;
f.store.ensureStorage();
assert.notEqual(f.store.core, oldCore); assert.equal(f.store.core.task, tasks); assert.equal(f.store.sessions, sessions);
assert.ok(f.store.core.delivery_plan instanceof Map); assert.ok(f.store.core.delivery_job instanceof Map);
const adapter = new GitDelivery(f.store), plan = await adapter.plan(f.session);
assert.equal((await (await adapter.start(f.session, plan.id)).promise).status, 'completed');
});
test('an unrelated owner deletion is fingerprinted and preserved while linked task paths are rejected', async t => {
const f = fixture(t); await checkedDelivery(f);
fs.unlinkSync(path.join(f.projectRoot, 'owner-unstaged.txt'));
const { CheckRunner } = await import('../src/core/check-runner.mjs'), checks = new CheckRunner(f.store);
const inspection = await inspectProject(f.project); assert.equal(inspection.scope.complete, true);
assert.equal((await (await checks.start(f.session, (await checks.plan(f.session)).id)).promise).status, 'passed');
const adapter = new GitDelivery(f.store), job = await (await adapter.start(f.session, (await adapter.plan(f.session)).id)).promise;
assert.equal(job.status, 'completed', job.summary); assert.equal(fs.existsSync(path.join(f.projectRoot, 'owner-unstaged.txt')), false);
assert.match(git(f, ['status', '--porcelain=v1']), / D owner-unstaged.txt/);
fs.symlinkSync(path.join(f.projectRoot, 'src'), path.join(f.projectRoot, 'linked-src'), process.platform === 'win32' ? 'junction' : 'dir');
const proposal = proposeFile(f.store, f.session, { path: 'linked-src/math.mjs', content: 'export const sum = (a, b) => a + b + 0;\n', reason: 'Linked path requires an explicit canonical review' });
applyChange(f.store, f.session, proposal.change_id);
await (await checks.start(f.session, (await checks.plan(f.session)).id)).promise;
await assert.rejects(adapter.plan(f.session), /经过链接/);
assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), '2');
});
+210
View File
@@ -0,0 +1,210 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { fixture, launchWorkbench, until, call, assertToolPairs } from './helpers.mjs';
import { createDeliveryProject, applyDeliveryFix, checkedDelivery, deliveryGit as git, writeDeliveryFile as write } from './fixtures/delivery-project.mjs';
import { CheckRunner } from '../src/core/check-runner.mjs';
import { GitDelivery } from '../src/core/git-delivery.mjs';
import { gitEnvironment } from '../src/core/git-process.mjs';
import { DeliveryReporter } from '../src/core/delivery-report.mjs';
import { inspectProject } from '../src/core/project-inspector.mjs';
import { proposeFile, applyChange } from '../src/server/files.mjs';
import { Store } from '../src/server/store.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { Provider } from '../src/server/provider.mjs';
test('actual failed tests become a reviewed local commit and evidence report while preserving unrelated index and files', async t => {
const f = fixture(t); createDeliveryProject(f);
const parent = git(f, ['rev-parse', 'HEAD']).trim(), protectedStage = git(f, ['show', ':owner-staged.txt']);
const ownerFiles = ['owner-staged.txt', 'owner-unstaged.txt', 'owner-untracked.txt'].map(name => [name, fs.readFileSync(path.join(f.projectRoot, name))]);
const checks = new CheckRunner(f.store), delivery = new GitDelivery(f.store); f.onCleanup(() => delivery.stopAll()); f.onCleanup(() => checks.stopAll());
let started = await checks.start(f.session, (await checks.plan(f.session)).id), failed = await started.promise;
assert.equal(failed.status, 'failed'); assert.equal(failed.results[0].tests.failed, 3);
await assert.rejects(delivery.plan(f.session), /通过记录/);
applyDeliveryFix(f);
started = await checks.start(f.session, (await checks.plan(f.session)).id); const passed = await started.promise;
assert.equal(passed.status, 'passed'); assert.equal(passed.results[0].tests.passed, 3);
const before = await inspectProject(f.project), plan = await delivery.plan(f.session, { message: 'feat: deliver reviewed addition repair' });
assert.deepEqual(plan.paths, ['src/add.mjs', 'src/math.mjs']); assert.equal(plan.author.name, 'Workbench QA'); assert.equal(plan.policy.hooks, 'disabled');
assert.equal(git(f, ['rev-parse', 'HEAD']).trim(), parent); assert.match(plan.files[1].diff, /sum/);
const accepted = await delivery.start(f.session, plan.id), job = await accepted.promise;
assert.equal(job.status, 'completed', job.summary); assert.equal(job.requiresVerification, false); assert.equal(job.indexPreserved, true); assert.equal(job.workspacePreserved, true);
assert.equal(git(f, ['rev-parse', 'HEAD']).trim(), job.commit); assert.equal(git(f, ['rev-parse', 'HEAD^']).trim(), parent);
assert.deepEqual(git(f, ['diff-tree', '--no-commit-id', '--name-only', '-r', 'HEAD']).trim().split('\n'), ['src/add.mjs', 'src/math.mjs']);
assert.equal(git(f, ['show', ':owner-staged.txt']), protectedStage);
for (const [name, bytes] of ownerFiles) assert.deepEqual(fs.readFileSync(path.join(f.projectRoot, name)), bytes);
assert.match(git(f, ['status', '--porcelain=v1']), /MM owner-staged.txt/); assert.match(git(f, ['status', '--porcelain=v1']), /\?\? owner-untracked.txt/);
const after = await inspectProject(f.project);
assert.notEqual(before.fingerprint, after.fingerprint); assert.equal(before.sourceFingerprint, after.sourceFingerprint);
assert.equal(f.store.core.task.get(f.session.taskId).verification.status, 'passed');
const report = JSON.parse(f.store.artifactStore.read(f.store.core.artifact.get(job.reportArtifactId)));
assert.equal(report.status, 'verified_committed'); assert.equal(report.verifiedCheckJobId, passed.id); assert.equal(report.task.requirementsAcceptance, 'not_recorded');
assert.equal(report.checks[0].results[0].tests.failed, 3); assert.equal(report.checks[1].results[0].tests.passed, 3); assert.equal(report.checks[1].stale, false);
assert.equal(report.deliveries[0].commit, job.commit); assert.equal(report.usage.amount, null);
assert.match(f.store.artifactStore.read(f.store.core.artifact.get(job.reportMarkdownArtifactId)), /检查通过并已提交/);
await assert.rejects(delivery.start(f.session, plan.id), { status: 409 });
await assert.rejects(delivery.plan(f.session), /没有新的/);
f.store.close(); const recovered = new Store(f.data); f.onCleanup(() => recovered.close());
assert.equal(recovered.owned('delivery_job', job.id, f.session.id).commit, job.commit);
assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), '2');
});
test('selected preexisting unstaged, staged and untracked owner edits cannot enter a task commit', async t => {
for (const kind of ['unstaged', 'staged', 'untracked']) {
const f = fixture(t); createDeliveryProject(f, { broken: false });
const relative = kind === 'untracked' ? 'owner-new.mjs' : 'src/add.mjs';
write(f, relative, kind === 'untracked' ? 'export const owner = true;\n' : 'export const add = (a, b) => a + b + 0;\n');
if (kind === 'staged') git(f, ['add', '--', relative]);
const proposal = proposeFile(f.store, f.session, { path: relative, content: kind === 'untracked' ? 'export const owner = true; export const reviewed = true;\n' : 'export const add = (a, b) => a + b + 0 + 0;\n', reason: 'Task starts after an owner edit' });
applyChange(f.store, f.session, proposal.change_id);
const checks = new CheckRunner(f.store), plan = await checks.plan(f.session);
assert.equal((await (await checks.start(f.session, plan.id)).promise).status, 'passed');
const before = git(f, ['status', '--porcelain=v1']), head = git(f, ['rev-parse', 'HEAD']);
await assert.rejects(new GitDelivery(f.store).plan(f.session), /其他工作区修改|暂存修改|任务之前已经存在/);
assert.equal(git(f, ['status', '--porcelain=v1']), before); assert.equal(git(f, ['rev-parse', 'HEAD']), head);
}
});
test('source, unrelated stage and branch changes after review invalidate a commit plan without changing HEAD', async t => {
for (const kind of ['source', 'stage', 'branch']) {
const f = fixture(t), { delivery } = await checkedDelivery(f), plan = await delivery.plan(f.session);
if (kind === 'source') write(f, 'src/add.mjs', "import { sum } from './math.mjs'; export const add = sum;\n");
if (kind === 'stage') { write(f, 'owner-unstaged.txt', 'Owner staged after review\n'); git(f, ['add', '--', 'owner-unstaged.txt']); }
if (kind === 'branch') git(f, ['switch', '-c', 'owner-branch']);
const head = git(f, ['rev-parse', 'HEAD']), stage = git(f, ['ls-files', '--stage', '-z']);
const job = await (await delivery.start(f.session, plan.id)).promise;
assert.equal(job.status, 'stale', job.summary); assert.equal(job.commit, null); assert.equal(git(f, ['rev-parse', 'HEAD']), head);
assert.equal(git(f, ['ls-files', '--stage', '-z']), stage); assert.equal(fs.existsSync(path.join(f.projectRoot, '.git/index.lock')), false);
await assert.rejects(delivery.start(f.session, plan.id), { status: 409 });
}
});
test('a later Git metadata change keeps matching source evidence valid but a real source edit requires checks', async t => {
const f = fixture(t), { delivery, check } = await checkedDelivery(f), source = (await inspectProject(f.project)).sourceFingerprint;
git(f, ['switch', '-c', 'delivery-branch']);
const plan = await delivery.plan(f.session), job = await (await delivery.start(f.session, plan.id)).promise;
assert.equal(job.status, 'completed', job.summary); assert.equal(plan.snapshot.branch, 'delivery-branch');
assert.equal((await inspectProject(f.project)).sourceFingerprint, source);
const reporter = new DeliveryReporter(f.store); assert.equal((await reporter.create(f.session)).report.verifiedCheckJobId, check.id);
write(f, 'src/math.mjs', 'export const sum = (a, b) => a + b + 1;\n');
const stale = await reporter.create(f.session);
assert.equal(stale.report.status, 'needs_checks'); assert.equal(stale.report.verifiedCheckJobId, null); assert.equal(stale.report.checks[0].stale, true);
});
test('a corrupt passed-check log and a pending proposal block Git delivery instead of inventing evidence', async t => {
const f = fixture(t), { delivery, check } = await checkedDelivery(f);
const proposal = proposeFile(f.store, f.session, { path: 'review.md', content: 'Still pending\n', reason: 'Pending item' });
await assert.rejects(delivery.plan(f.session), /待审查/);
f.session.changes.find(change => change.id === proposal.change_id).status = 'rejected'; f.session.status = 'completed'; f.store.save(f.session);
const log = f.store.core.artifact.get(check.results[0].logArtifactId);
fs.writeFileSync(path.join(f.store.artifactStore.root, log.relative), 'Corrupted result');
await assert.rejects(delivery.plan(f.session), /日志完整/);
assert.equal((await new DeliveryReporter(f.store).create(f.session)).report.status, 'needs_checks');
assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), '1');
});
test('Git filters, merge state, detached HEAD and old task ownership are explicitly rejected', async t => {
for (const kind of ['filter', 'merge', 'detached', 'old-task']) {
const f = fixture(t), { delivery } = await checkedDelivery(f);
if (kind === 'filter') {
write(f, '.gitattributes', '*.mjs filter=owner-filter\n');
git(f, ['config', 'filter.owner-filter.clean', 'node scripts/filter-marker.mjs']);
write(f, 'scripts/filter-marker.mjs', "import fs from 'node:fs'; fs.writeFileSync('filter-was-run.txt', 'unexpected');\n");
}
if (kind === 'merge') fs.writeFileSync(path.join(f.projectRoot, '.git/MERGE_HEAD'), git(f, ['rev-parse', 'HEAD']));
if (kind === 'detached') git(f, ['switch', '--detach']);
if (kind === 'old-task') {
const run = f.store.beginRun(f.session, 'A different task with no reviewed changes', false, f.store.config);
f.session.status = 'completed'; f.store.finishRun(f.session, run);
const checks = new CheckRunner(f.store); await (await checks.start(f.session, (await checks.plan(f.session)).id)).promise;
}
await assert.rejects(delivery.plan(f.session), /过滤|合并|命名分支|归属明确/);
assert.equal(fs.existsSync(path.join(f.projectRoot, 'filter-was-run.txt')), false);
assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), '1');
}
});
test('local snapshot commits disable repository hooks and signing and isolate the Git child environment', async t => {
const f = fixture(t), { delivery } = await checkedDelivery(f);
const hook = path.join(f.projectRoot, '.git/hooks/reference-transaction');
fs.writeFileSync(hook, '#!/bin/sh\nprintf unexpected > hook-was-run.txt\n', { mode: 0o755 });
git(f, ['config', 'commit.gpgSign', 'true']); git(f, ['config', 'user.signingKey', 'a-key-that-is-not-present']);
const previous = process.env.EXTRA_MODEL_CONNECTION_SECRET;
process.env.EXTRA_MODEL_CONNECTION_SECRET = 'synthetic-runtime-only-secret';
try {
assert.equal(gitEnvironment().EXTRA_MODEL_CONNECTION_SECRET, undefined); assert.equal(gitEnvironment().NODE_TEST_CONTEXT, undefined);
const job = await (await delivery.start(f.session, (await delivery.plan(f.session, { authorName: 'Local Reviewer', authorEmail: 'reviewer@local.invalid', message: 'fix: reviewed delivery' })).id)).promise;
assert.equal(job.status, 'completed', job.summary); assert.equal(fs.existsSync(path.join(f.projectRoot, 'hook-was-run.txt')), false);
assert.match(git(f, ['cat-file', 'commit', 'HEAD']), /author Local Reviewer <reviewer@local.invalid>/);
assert.ok(!git(f, ['cat-file', 'commit', 'HEAD']).includes('gpgsig'));
assert.ok(!fs.readFileSync(path.join(f.data, 'workbench.sqlite')).includes(Buffer.from('synthetic-runtime-only-secret')));
} finally { if (previous === undefined) delete process.env.EXTRA_MODEL_CONNECTION_SECRET; else process.env.EXTRA_MODEL_CONNECTION_SECRET = previous; }
});
test('stopping before publication cancels safely; stopping after publication preserves the actual completed commit', async t => {
for (const phase of ['prepared', 'committed']) {
const f = fixture(t); await checkedDelivery(f);
let reached = false, release;
const gate = new Promise(resolve => { release = resolve; });
const delivery = new GitDelivery(f.store, { faultInjector: async current => { if (current === phase) { reached = true; await gate; } } });
f.onCleanup(() => { release(); return delivery.stopAll(); });
const runtime = new Runtime(f.store, new Provider()); runtime.deliveries = delivery;
delivery.projectBusy = projectId => [...runtime.active.values()].some(run => run.project.id === projectId);
const plan = await delivery.plan(f.session), started = await delivery.start(f.session, plan.id);
await until(() => reached, 12000); assert.equal(runtime.hasActivity(), true); assert.equal(runtime.projectHasActivity(f.project.id), true);
await assert.rejects(delivery.start(f.session, plan.id), { status: 409 });
assert.throws(() => runtime.start(f.store.createSession(f.project.id).id, 'Concurrent work'), { status: 409 });
const stop = runtime.stop(f.session.id); release(); await stop; const job = await started.promise;
assert.equal(job.status, phase === 'prepared' ? 'cancelled' : 'completed', job.summary);
assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), phase === 'prepared' ? '1' : '2');
assert.equal(job.stopRequested, true); assert.equal(fs.existsSync(path.join(f.projectRoot, '.git/index.lock')), false);
assert.equal(runtime.hasActivity(), false);
}
});
test('delivery API confirms exactly one owned plan, serves verified reports and preserves results across restart', async t => {
const f = fixture(t); await checkedDelivery(f); f.store.close(); const app = await launchWorkbench(t, f.data);
const planned = await app.api('/sessions/' + f.session.id + '/delivery-plans', { message: 'API reviewed delivery' }), plan = planned.execution.deliveryPlans.at(-1);
assert.deepEqual(plan.paths, ['src/add.mjs', 'src/math.mjs']); assert.equal(plan.snapshot, undefined); assert.match(plan.baseline.head, /^[a-f0-9]{40}$/);
const other = await app.api('/sessions', { projectId: f.project.id });
await assert.rejects(app.api('/sessions/' + other.id + '/delivery-jobs', { planId: plan.id }), { status: 409 });
const responses = await Promise.allSettled([app.api('/sessions/' + f.session.id + '/delivery-jobs', { planId: plan.id }), app.api('/sessions/' + f.session.id + '/delivery-jobs', { planId: plan.id })]);
assert.equal(responses.filter(value => value.status === 'fulfilled').length, 1);
const accepted = responses.find(value => value.status === 'fulfilled').value, jobId = accepted.execution.deliveries.at(-1).id;
let job; await until(async () => { job = await app.api('/sessions/' + f.session.id + '/delivery-jobs/' + jobId); return !['queued', 'preparing', 'prepared', 'publishing', 'committed', 'reporting'].includes(job.status); }, 20000);
assert.equal(job.status, 'completed', job.summary);
await assert.rejects(app.api('/sessions/' + other.id + '/delivery-jobs/' + jobId), { status: 404 });
const preview = await app.api('/artifacts/' + job.reportArtifactId + '?session=' + f.session.id + '&limit=64000');
assert.equal(JSON.parse(preview.content).status, 'verified_committed');
await assert.rejects(app.api('/artifacts/' + job.reportArtifactId + '?session=' + other.id), { status: 404 });
const download = await fetch(app.url + '/api/artifacts/' + job.reportMarkdownArtifactId + '?session=' + f.session.id + '&download=1', { headers: { 'X-Workbench-Token': app.bootstrap.token } });
assert.equal(download.status, 200); assert.match(download.headers.get('content-disposition'), /\.md/); assert.match(await download.text(), /检查通过并已提交/);
const unknown = await fetch(app.url + '/api/artifacts/' + job.reportArtifactId + '?session=' + f.session.id + '&download=1');
assert.equal(unknown.status, 403);
await app.close(); const resumed = await launchWorkbench(t, f.data);
assert.equal((await resumed.api('/sessions/' + f.session.id + '/delivery-jobs/' + jobId)).commit, job.commit);
assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), '2');
});
test('the model can prepare an owned Git review and evidence report while publishing remains a separate user action', async t => {
const f = fixture(t); await checkedDelivery(f);
f.session.status = 'paused'; f.store.save(f.session);
const provider = new Provider(), runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
let round = 0;
provider.complete = async () => ({ message: { role: 'assistant', content: round > 1 ? 'Git预览已准备,等待用户确认。' : '',
tool_calls: round++ === 0 ? [call('model-git-plan', 'plan_delivery', { message: 'fix: model-prepared reviewed changes' })]
: round === 2 ? [call('model-evidence-report', 'delivery_report', {})] : [] }, usage: { prompt_tokens: 10, completion_tokens: 5 } });
const head = git(f, ['rev-parse', 'HEAD']);
runtime.resume(f.session.id); await runtime.active.get(f.session.id).promise;
const result = f.store.publicSession(f.session);
assert.equal(result.execution.deliveryPlans.length, 1); assert.equal(result.execution.deliveryPlans[0].preparedBy, 'agent');
assert.equal(result.execution.deliveries.length, 0); assert.equal(git(f, ['rev-parse', 'HEAD']), head);
assert.equal(result.events.find(event => event.tool === 'plan_delivery').phase, 'completed');
assert.equal(result.events.find(event => event.tool === 'delivery_report').phase, 'completed');
assertToolPairs(f.session.providerMessages);
const report = result.execution.artifacts.find(artifact => artifact.kind === 'delivery-report');
assert.equal(JSON.parse(f.store.artifactStore.read(f.store.core.artifact.get(report.id))).status, 'in_progress');
const accepted = await runtime.gitDelivery().start(f.session, result.execution.deliveryPlans[0].id), job = await accepted.promise;
assert.equal(job.status, 'completed', job.summary); assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), '2');
});
+15
View File
@@ -0,0 +1,15 @@
import { Store } from '../../src/server/store.mjs';
import { GitDelivery } from '../../src/core/git-delivery.mjs';
const [data, sessionId, planId, cut] = process.argv.slice(2);
const store = new Store(data);
const delivery = new GitDelivery(store, {
faultInjector: (phase, job) => {
if (phase !== cut) return;
process.stdout.write(JSON.stringify({ phase, jobId: job.id, sessionId, expectedCommit: job.expectedCommit }) + '\n');
process.exit(73);
},
});
const started = await delivery.start(store.session(sessionId), planId);
const job = await started.promise;
process.stderr.write('Did not reach crash phase: ' + cut + ' / ' + job.status + ' / ' + job.summary + '\n');
await delivery.stopAll(); store.close(); process.exit(74);
+55
View File
@@ -0,0 +1,55 @@
import fs from 'node:fs';
import path from 'node:path';
import { execFileSync } from 'node:child_process';
import { CheckRunner } from '../../src/core/check-runner.mjs';
import { GitDelivery } from '../../src/core/git-delivery.mjs';
import { proposeFile, applyChange } from '../../src/server/files.mjs';
import { gitEnvironment } from '../../src/core/git-process.mjs';
export function writeDeliveryFile(f, relative, contents) {
const target = path.join(f.projectRoot, relative); fs.mkdirSync(path.dirname(target), { recursive: true }); fs.writeFileSync(target, contents);
}
export function deliveryGit(f, args, input) {
return execFileSync('git', ['--no-pager', '--no-optional-locks', '-C', f.projectRoot, ...args],
{ windowsHide: true, encoding: 'utf8', env: gitEnvironment(), input });
}
export function createDeliveryProject(f, { broken = true, ownerChanges = true } = {}) {
writeDeliveryFile(f, '.gitignore', 'dist/\n.env\n.awb-*\n');
writeDeliveryFile(f, 'package.json', JSON.stringify({ name: 'git-delivery-fixture', type: 'module', private: true, scripts: { test: 'node --test test/*.test.mjs', build: 'node scripts/build.mjs' } }, null, 2));
writeDeliveryFile(f, 'src/add.mjs', 'export const add = (a, b) => a ' + (broken ? '-' : '+') + ' b;\n');
writeDeliveryFile(f, 'test/add.test.mjs', "import test from 'node:test'; import assert from 'node:assert/strict'; import { add } from '../src/add.mjs';\ntest('adds positive values', () => assert.equal(add(2, 3), 5));\ntest('adds negative values', () => assert.equal(add(-2, 3), 1));\ntest('preserves integer addition', () => assert.equal(add(10, 1), 11));\n");
writeDeliveryFile(f, 'scripts/build.mjs', "import fs from 'node:fs'; fs.cpSync('src', 'dist', { recursive: true }); console.log('Delivery fixture built.');\n");
writeDeliveryFile(f, 'owner-staged.txt', 'Owner baseline staged file\n');
writeDeliveryFile(f, 'owner-unstaged.txt', 'Owner baseline unstaged file\n');
deliveryGit(f, ['init', '--initial-branch=main']);
deliveryGit(f, ['config', 'core.autocrlf', 'false']);
deliveryGit(f, ['config', 'user.name', 'Workbench QA']);
deliveryGit(f, ['config', 'user.email', 'qa@workbench.invalid']);
const files = ['README.md', '.gitignore', 'package.json', 'src/add.mjs', 'test/add.test.mjs', 'scripts/build.mjs', 'owner-staged.txt', 'owner-unstaged.txt'];
deliveryGit(f, ['add', '--', ...files]);
deliveryGit(f, ['commit', '--no-gpg-sign', '-m', 'Sealed delivery fixture baseline']);
if (ownerChanges) {
writeDeliveryFile(f, 'owner-staged.txt', 'Owner staged change: keep this exact staged content\n');
deliveryGit(f, ['add', '--', 'owner-staged.txt']);
writeDeliveryFile(f, 'owner-staged.txt', 'Owner working copy after staging: keep both versions\n');
writeDeliveryFile(f, 'owner-unstaged.txt', 'Owner unstaged change: keep this content\n');
writeDeliveryFile(f, 'owner-untracked.txt', 'Owner untracked content: keep this file untracked\n');
}
const run = f.store.beginRun(f.session, 'Repair addition, verify the project and deliver only reviewed files', false, f.store.config);
f.session.status = 'completed'; f.store.finishRun(f.session, run);
return run;
}
export function applyDeliveryFix(f) {
for (const [relative, content] of [['src/add.mjs', "import { sum } from './math.mjs';\nexport const add = sum;\n"], ['src/math.mjs', 'export const sum = (a, b) => a + b;\n']]) {
const proposed = proposeFile(f.store, f.session, { path: relative, content, reason: 'Reviewed addition repair' });
applyChange(f.store, f.session, proposed.change_id);
}
}
export async function checkedDelivery(f, { broken = true, ownerChanges = true } = {}) {
createDeliveryProject(f, { broken, ownerChanges }); if (broken) applyDeliveryFix(f);
const checks = new CheckRunner(f.store), delivery = new GitDelivery(f.store);
f.onCleanup(() => checks.stopAll()); f.onCleanup(() => delivery.stopAll());
const plan = await checks.plan(f.session), started = await checks.start(f.session, plan.id);
const check = await started.promise;
return { checks, check, delivery };
}