import test from 'node:test'; import assert from 'node:assert/strict'; import { fixture, launchWorkbench, assertToolPairs } from './helpers.mjs'; import { beginPlanTask, completeRead, readingStep } from './fixtures/task-plan-fixture.mjs'; test('task-plan HTTP mutations bind current Task/Run and retain original evidence through real host restart', async t => { const f = fixture(t); beginPlanTask(f); const record = completeRead(f), taskId = f.session.taskId, runId = f.session.runId; f.store.close(); let host = await launchWorkbench(t, f.data), route = '/sessions/' + f.session.id; const input = { taskId, runId, expected_revision: 0, steps: [readingStep()] }; assert.equal((await host.api(route + '/task-plan')).configured, false); await assert.rejects(host.api(route + '/task-plans', { ...input, taskId: '00000000-0000-0000-0000-000000000000' }), /任务或运行已变化/); const saved = await host.api(route + '/task-plans', input), planId = saved.plan.plan_id; assert.equal(saved.plan.revision, 1); assert.equal(saved.session.execution.taskPlans[0].id, planId); const other = await host.api('/sessions', { projectId: f.project.id }); await assert.rejects(host.api('/sessions/' + other.id + '/task-plans', input), /任务或运行已变化/); await assert.rejects(host.api(route + '/task-plan?taskId=wrong'), /所选任务已变化/); const verified = await host.api(route + '/task-proofs', { taskId, runId, plan_id: planId, expected_revision: 1, step_key: 'read', evidence_id: record.id }); assert.equal(verified.verification.status, 'completed'); assert.equal(verified.plan.all_verified, true); assert.equal(verified.session.execution.taskProofs.length, 1); const before = await host.api(route + '/export'); await host.close(); host = await launchWorkbench(t, f.data); const restored = await host.api(route + '/task-plan?taskId=' + taskId); assert.equal(restored.all_verified, true); assert.equal(restored.plan_id, planId); const after = await host.api(route + '/export'); assert.deepEqual(after.session.providerMessages, before.session.providerMessages); assert.equal(after.execution.progress.totalTools, 1); assertToolPairs(after.session.providerMessages); }); test('concurrent stale HTTP plan revisions cannot replace conditions or attach proofs to a different plan', async t => { const f = fixture(t); beginPlanTask(f); const read = completeRead(f), taskId = f.session.taskId, runId = f.session.runId; f.store.close(); const host = await launchWorkbench(t, f.data), route = '/sessions/' + f.session.id; const saved = await host.api(route + '/task-plans', { taskId, runId, expected_revision: 0, steps: [readingStep()] }); const requests = await Promise.allSettled(['later_a', 'later_b'].map(key => host.api(route + '/task-plans', { taskId, runId, expected_revision: 1, steps: [readingStep(), readingStep(key)] }))); assert.equal(requests.filter(value => value.status === 'fulfilled').length, 1); assert.equal(requests.filter(value => value.status === 'rejected').length, 1); const current = await host.api(route + '/task-plan'); assert.equal(current.revision, 2); await assert.rejects(host.api(route + '/task-plans', { taskId, runId, expected_revision: 2, steps: [{ ...readingStep(), criterion: '弱化条件' }] }), /不能删改/); await assert.rejects(host.api(route + '/task-proofs', { taskId, runId, plan_id: saved.plan.plan_id, expected_revision: 1, step_key: 'read', evidence_id: read.id }), /原计划视图/); const exported = await host.api(route + '/export'); assert.equal(exported.execution.taskPlans.length, 2); assert.equal(exported.execution.taskProofs.length, 0); });