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, compatibleFixture, call, completion, assertToolPairs } from './helpers.mjs'; import { batchFixture, assertBatchBytes } from './fixtures/change-set-project.mjs'; import { Runtime } from '../src/server/runtime.mjs'; import { Provider } from '../src/server/provider.mjs'; test('batch APIs enforce ownership and review fingerprints, apply once, and persist the completed batch across restart', { timeout: 30000 }, async t => { const f = fixture(t), b = batchFixture(f); f.store.close(); const host = await launchWorkbench(t, f.data), route = '/sessions/' + f.session.id; const first = await host.api(route + '/change-sets', { changeIds: b.ids }), set = first.execution.changeSets.at(-1); assert.equal((await host.api(route + '/change-sets', { changeIds: b.ids })).execution.changeSets.at(-1).id, set.id); assertBatchBytes(assert, f, b.before); await assert.rejects(host.api(route + '/change-sets/' + set.id + '/apply', {}), /指纹/); await assert.rejects(host.api(route + '/changes/' + b.ids[0] + '/apply', {}), /封存/); await assert.rejects(host.api(route + '/messages', { content: 'different task' }), /变更集/); const other = await host.api('/sessions', { projectId: f.project.id }); await assert.rejects(host.api('/sessions/' + other.id + '/change-sets/' + set.id), /不属于/); await host.api(route + '/change-sets/' + set.id + '/apply', { fingerprint: set.fingerprint }); let applied; await until(async () => { applied = await host.api(route); return applied.execution.changeSets.at(-1).status === 'completed'; }); assertBatchBytes(assert, f, b.after); await assert.rejects(host.api(route + '/change-sets/' + set.id + '/apply', { fingerprint: set.fingerprint }), /重复应用/); await host.close(); const restarted = await launchWorkbench(t, f.data), restored = await restarted.api(route); assert.deepEqual(restored.execution.changeSets, applied.execution.changeSets); assertBatchBytes(assert, f, b.after); }); test('delivery reports bind actual batch identity and reject corrupted snapshot evidence without changing project files', { timeout: 30000 }, async t => { const f = fixture(t), b = batchFixture(f), set = await b.manager.prepare(f.session, b.ids); await (await b.manager.start(f.session, set.id, set.fingerprint)).promise; const snapshot = f.store.owned('artifact', set.files[1].snapshotArtifactId, f.session.id), snapshotPath = path.join(f.store.artifactStore.root, snapshot.relative); f.store.close(); const host = await launchWorkbench(t, f.data), route = '/sessions/' + f.session.id; const valid = await host.api(route + '/delivery-reports', {}), jsonArtifact = valid.execution.artifacts.slice().reverse().find(item => item.kind === 'delivery-report'); const preview = await host.api('/artifacts/' + jsonArtifact.id + '?session=' + f.session.id + '&limit=16000'), report = JSON.parse(preview.content); assert.equal(report.changeSets[0].id, set.id); assert.equal(report.changeSets[0].snapshotVerified, true); assert.ok(report.changes.every(change => change.changeSetId === set.id)); assert.equal(report.status, 'needs_checks'); fs.writeFileSync(snapshotPath, 'Corrupt owned QA snapshot'); const invalid = await host.api(route + '/delivery-reports', {}), artifact = invalid.execution.artifacts.slice().reverse().find(item => item.kind === 'delivery-report'); const invalidReport = JSON.parse((await host.api('/artifacts/' + artifact.id + '?session=' + f.session.id + '&limit=16000')).content); assert.equal(invalidReport.status, 'needs_verification'); assert.equal(invalidReport.changeSets[0].snapshotVerified, false); assertBatchBytes(assert, f, b.after); }); test('a compatible model can seal proposals through the real HTTP loop while application stays behind user review', { timeout: 60000 }, async t => { const f = fixture(t); f.store.close(); let count = 0; const provider = await compatibleFixture(t, (url, input) => { if (!input?.messages) return { data: [{ id: 'fixture-model' }] }; count++; if (count === 1) return completion([ call('first-file', 'propose_file', { path: 'src/new.mjs', content: 'export const newValue = 1;\n', reason: '新增模块' }), call('second-file', 'propose_file', { path: 'docs/new.md', content: '# New\n', reason: '补充说明' }), ], '准备两个文件提案。'); if (count === 2) { const ids = input.messages.filter(message => message.role === 'tool').map(message => JSON.parse(message.content).change_id).filter(Boolean); return completion([call('seal-batch', 'prepare_change_set', { change_ids: ids })], '封存整批审查范围。'); } return completion([], '文件批次已封存,等待用户审查应用。'); }); const host = await launchWorkbench(t, f.data); await host.api('/config', { mode: 'compatible', baseUrl: provider.baseUrl, model: 'fixture-model', apiKey: 'SYNTHETIC-QA-ONLY' }); await host.api('/sessions/' + f.session.id + '/messages', { content: '创建两个文件并组合审查' }); let session; await until(async () => { session = await host.api('/sessions/' + f.session.id); return session.status === 'needs_review'; }, 30000); assert.equal(session.execution.changeSets.length, 1); assert.equal(session.execution.changeSets[0].status, 'prepared'); assert.equal(session.execution.changeSets[0].files.length, 2); assert.equal(fs.existsSync(path.join(f.projectRoot, 'src')), false); assert.equal(fs.existsSync(path.join(f.projectRoot, 'docs')), false); assert.equal(session.events.find(event => event.tool === 'prepare_change_set').phase, 'completed'); assert.equal(count, 2); assert.equal(session.execution.run.stopReason, 'change_set_review'); const tools = provider.requests.find(request => request.input?.tools)?.input.tools.map(tool => tool.function.name); assert.ok(tools.includes('prepare_change_set')); assert.ok(!tools.includes('apply_change_set')); assert.ok(!tools.includes('rollback_change_set')); }); test('a sealed batch at the step limit hands off and continues the same Task into actual checks without replaying saved tools', { timeout: 60000 }, async t => { const f = fixture(t); fs.mkdirSync(path.join(f.projectRoot, 'test')); const acceptance = "import test from 'node:test'; import assert from 'node:assert/strict'; import { left } from '../src/left.mjs'; import { right } from '../src/right.mjs';\ntest('left value', () => assert.equal(left, 1)); test('right value', () => assert.equal(right, 2));\n"; fs.writeFileSync(path.join(f.projectRoot, 'test/values.test.mjs'), acceptance); fs.writeFileSync(path.join(f.projectRoot, 'package.json'), JSON.stringify({ type: 'module', scripts: { test: 'node --test test/*.test.mjs' } })); f.store.close(); let count = 0; const model = await compatibleFixture(t, (url, input) => { if (!input?.messages) return { data: [{ id: 'batch-continuation-fixture' }] }; count++; if (count === 1) return completion([ call('paused-left', 'propose_file', { path: 'src/left.mjs', content: 'export const left = 1;\n', reason: '第一份受控模块' }), call('paused-right', 'propose_file', { path: 'src/right.mjs', content: 'export const right = 2;\n', reason: '第二份受控模块' }), ]); const results = input.messages.filter(message => message.role === 'tool').map(message => JSON.parse(message.content)); if (count === 2) return completion([call('paused-seal', 'prepare_change_set', { change_ids: results.map(result => result.change_id).filter(Boolean) })]); if (count === 3) return completion([call('continued-check-plan', 'plan_checks', { names: ['test'] })]); if (count === 4) return completion([call('continued-actual-check', 'run_checks', { plan_id: results.findLast(result => result.checks && result.fingerprint).id })]); return completion([], '原任务的两份文件已通过真实工程检查。'); }); const host = await launchWorkbench(t, f.data), route = '/sessions/' + f.session.id; await host.api('/config', { mode: 'compatible', baseUrl: model.baseUrl, model: 'batch-continuation-fixture', maxSteps: 2 }); await host.api(route + '/messages', { content: '完成原始双模块工程目标并运行测试' }); let paused; await until(async () => { paused = await host.api(route); return paused.status === 'needs_review'; }, 30000); assert.equal(paused.execution.run.stopReason, 'change_set_review'); assert.equal(count, 2); const set = paused.execution.changeSets.at(-1), firstRun = structuredClone(paused.execution.run); assert.equal(set.status, 'prepared'); assert.equal(fs.existsSync(path.join(f.projectRoot, 'src')), false); const savedMessages = (await host.api(route + '/export')).session.providerMessages; const savedResults = savedMessages.filter(message => message.role === 'tool'); assert.equal(savedResults.length, 3); await assert.rejects(host.api(route + '/resume', {}), { status: 409, message: /请先审查待处理改动/ }); await assert.rejects(host.api(route + '/continue-reviewed', {}), { status: 409 }); await host.api(route + '/change-sets/' + set.id + '/apply', { fingerprint: set.fingerprint }); await until(async () => (await host.api(route)).execution.changeSets.at(-1).status === 'completed', 30000); await host.api('/config', { ...(await host.api('/bootstrap')).config, maxSteps: 5 }); await host.api(route + '/continue-reviewed', {}); let waiting; await until(async () => { waiting = await host.api(route); return waiting.approvals.some(approval => approval.status === 'pending'); }, 30000); await host.api(route + '/approvals/' + waiting.approvals.find(approval => approval.status === 'pending').id, { allow: true }); let completed; await until(async () => { completed = await host.api(route); return completed.status === 'completed'; }, 30000); assert.equal(completed.execution.task.id, paused.execution.task.id); assert.equal(completed.execution.task.goal, '完成原始双模块工程目标并运行测试'); assert.equal(completed.execution.run.resumeOf, firstRun.id); assert.equal(completed.execution.task.verification.status, 'passed'); assert.equal(completed.execution.checks.length, 1); assert.equal(completed.execution.checks[0].results[0].tests.passed, 2); assert.equal(completed.execution.runs.find(run => run.id === firstRun.id).status, 'needs_review'); assert.deepEqual(completed.execution.runs.find(run => run.id === firstRun.id).usage, firstRun.usage); assert.equal(completed.execution.changeSets[0].runId, firstRun.id); assert.equal(count, 5); const exported = (await host.api(route + '/export')).session.providerMessages; assert.deepEqual(exported.filter(message => message.role === 'tool').slice(0, 3), savedResults); assertToolPairs(exported); assert.equal(completed.events.filter(event => event.kind === 'tool' && ['propose_file', 'prepare_change_set'].includes(event.tool)).length, 3); assert.equal(fs.readFileSync(path.join(f.projectRoot, 'test/values.test.mjs'), 'utf8'), acceptance); await assert.rejects(host.api(route + '/continue-reviewed', {}), { status: 409 }); }); test('applied batches cannot turn cancellation, other budgets, restart or unknown commands into a review continuation', async t => { const f = fixture(t), b = batchFixture(f), set = await b.manager.prepare(f.session, b.ids); await (await b.manager.start(f.session, set.id, set.fingerprint)).promise; const runtime = new Runtime(f.store, new Provider()), run = f.store.core.run.get(f.session.runId); for (const reason of ['cancelled', 'time_limit', 'token_limit', 'task_token_limit', 'restart']) { run.status = 'paused'; run.stopReason = reason; const before = JSON.stringify(f.store.publicSession(f.session)); assert.throws(() => runtime.continueReviewed(f.session.id), { status: 409 }); assert.equal(JSON.stringify(f.store.publicSession(f.session)), before); assertBatchBytes(assert, f, b.after); } const invocation = call('unknown-command', 'run_command', { command: 'echo owned-fixture', reason: 'Unknown result must be reviewed' }); const event = f.store.event(f.session, { kind: 'tool', tool: 'run_command', toolCallId: invocation.id, phase: 'running' }); f.store.beginInvocation(f.session, invocation, run, event); f.store.invocationState(f.session, event, 'running'); f.store.interruptPending(f.session, run); f.session.status = 'paused'; f.store.finishRun(f.session, run, 'step_limit'); const before = JSON.stringify(f.store.publicSession(f.session)); assert.equal(f.store.publicSession(f.session).execution.progress.requiresVerification.length, 1); assert.throws(() => runtime.continueReviewed(f.session.id), { status: 409 }); assert.equal(JSON.stringify(f.store.publicSession(f.session)), before); assertBatchBytes(assert, f, b.after); });