Files
Agent-Workbench/tests/task-checkpoints.test.mjs
2026-10-30 18:41:56 +08:00

181 lines
16 KiB
JavaScript

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, compatibleFixture, completion, call, until, launchWorkbench, assertToolPairs } from './helpers.mjs';
import { Provider } from '../src/server/provider.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { Store } from '../src/server/store.mjs';
import { proposeFile } from '../src/server/files.mjs';
import { gitEnvironment, runGit } from '../src/core/git-process.mjs';
async function failedTask(t, { goal = 'Keep the original engineering goal, read README once, and continue after the fixture outage.' } = {}) {
const f = fixture(t); let failing = true;
const server = await compatibleFixture(t, (_url, _body, requests) => requests.length === 1
? completion([call('checkpoint-owned-read', 'read_file', { path: 'README.md' })])
: failing ? { status: 503, body: { error: { message: 'Owned local fixture outage.' } } } : completion([], 'Original task resumed from verified results.'));
const provider = new Provider({ retryPolicy: { attempts: 1 } }); provider.setKey('checkpoint-owned-secret');
f.store.saveConfig({ mode: 'compatible', model: 'owned-checkpoint-fixture', baseUrl: server.baseUrl, contextLimit: 48000 });
const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
runtime.start(f.session.id, goal); await runtime.active.get(f.session.id).promise;
assert.equal(f.session.status, 'failed'); assert.equal(server.requests.length, 2);
return { ...f, server, provider, runtime, manager: runtime.checkpointManager(), recover: () => { failing = false; } };
}
test('a checkpoint seals all task requirements and original results without a model request and explicit continuation keeps the same Task without replay', async t => {
const goal = 'Keep every requirement🙂\n'.repeat(300) + 'Final exact requirement must remain.';
const f = await failedTask(t, { goal });
const task = f.store.core.task.get(f.session.taskId); task.instructions.push({ content: 'Keep the public API unchanged.', createdAt: new Date().toISOString() });
task.constraints = ['No outside files.']; task.acceptance = ['Actual tests must pass.']; f.store.save(f.session);
const checkpoint = await f.manager.create(f.session), original = f.manager.original(f.session, checkpoint.id).bundle;
assert.equal(f.server.requests.length, 2); assert.equal(original.requirements.goal, goal);
assert.equal(original.requirements.instructions[0].content, 'Keep the public API unchanged.');
assert.deepEqual(original.requirements.constraints, ['No outside files.']); assert.deepEqual(original.requirements.acceptance, ['Actual tests must pass.']);
assert.equal(original.state.tools.length, 1); assert.equal(original.state.tools[0].tool, 'read_file');
assert.match(original.state.tools[0].resultArtifact.sha256, /^[a-f0-9]{64}$/);
assert.equal((await f.manager.inspect(f.session, checkpoint.id)).resumable, true);
const taskId = f.session.taskId, runId = f.session.runId;
f.recover(); await f.manager.resume(f.session, checkpoint.id, checkpoint.sha256, f.runtime); await f.runtime.active.get(f.session.id).promise;
assert.equal(f.session.status, 'completed'); assert.equal(f.session.taskId, taskId);
assert.equal(f.store.core.run.get(f.session.runId).resumeOf, runId); assert.equal(f.server.requests.length, 3);
assert.equal([...f.store.core.tool.values()].length, 1); assertToolPairs(f.session.providerMessages);
assert.ok(f.server.requests.at(-1).input.messages.some(value => value.role === 'tool' && value.tool_call_id === 'checkpoint-owned-read'));
const old = await f.manager.inspect(f.session, checkpoint.id); assert.equal(old.resumable, false); assert.match(old.reasons.join(' '), /新运行/);
});
test('checkpoint sources, requirements, plan and original tool changes block continuation and leave original checkpoint bytes intact', async t => {
const f = await failedTask(t), checkpoint = await f.manager.create(f.session);
const artifact = f.store.core.artifact.get(checkpoint.artifactId), bytes = f.store.artifactStore.readBytes(artifact);
const readme = path.join(f.projectRoot, 'README.md'), before = fs.readFileSync(readme);
fs.appendFileSync(readme, '\nExternal edit.\n');
let view = await f.manager.inspect(f.session, checkpoint.id); assert.equal(view.resumable, false); assert.match(view.reasons.join(' '), /源码/);
await assert.rejects(f.manager.resume(f.session, checkpoint.id, checkpoint.sha256, f.runtime), /不能从此检查点/);
fs.writeFileSync(readme, before);
const task = f.store.core.task.get(f.session.taskId); task.constraints.push('New outside requirement.');
view = await f.manager.inspect(f.session, checkpoint.id); assert.equal(view.resumable, false); assert.match(view.reasons.join(' '), /要求/);
task.constraints.pop();
await f.runtime.taskPlanner().plan(f.session, { expected_revision: 0, steps: [{ key: 'read', title: 'Read source', criterion: 'Use an actual read.', kind: 'analysis', tools: ['read_file'], depends_on: [] }] });
view = await f.manager.inspect(f.session, checkpoint.id); assert.equal(view.resumable, false); assert.match(view.reasons.join(' '), /计划/);
assert.deepEqual(f.store.artifactStore.readBytes(artifact), bytes); assert.equal(f.server.requests.length, 2);
});
test('unknown tool effects and pending proposals stay blocked in recovery packages rather than being changed into completed work', async t => {
const f = await failedTask(t);
const invocation = [...f.store.core.tool.values()][0]; invocation.requiresVerification = true; invocation.state = 'unknown'; f.store.save(f.session);
const unknown = await f.manager.create(f.session), view = await f.manager.inspect(f.session, unknown.id);
assert.equal(unknown.counts.unknown, 1); assert.equal(view.resumable, false); assert.match(view.reasons.join(' '), /结果未知/);
await assert.rejects(f.manager.resume(f.session, unknown.id, unknown.sha256, f.runtime), /未知/);
invocation.requiresVerification = false; invocation.state = 'completed';
proposeFile(f.store, f.session, { path: 'new-owned.mjs', content: 'export const value = 1;\n', explanation: 'Pending review.' });
const pending = await f.manager.create(f.session), reviewed = await f.manager.inspect(f.session, pending.id);
assert.equal(pending.counts.pendingChanges, 1); assert.equal(reviewed.resumable, false); assert.match(reviewed.reasons.join(' '), /待审核/);
assert.equal(fs.existsSync(path.join(f.projectRoot, 'new-owned.mjs')), false); assert.equal(f.server.requests.length, 2);
});
test('checkpoint and referenced result corruption are rejected before dispatch and historical blobs are never rebuilt', async t => {
const f = await failedTask(t), checkpoint = await f.manager.create(f.session);
const source = f.store.core.artifact.get([...f.store.core.tool.values()][0].resultArtifactId), filename = path.join(f.store.artifactStore.root, source.relative), bytes = fs.readFileSync(filename);
fs.writeFileSync(filename, '{}'); await assert.rejects(f.manager.inspect(f.session, checkpoint.id), /工件校验失败/);
assert.equal(fs.readFileSync(filename, 'utf8'), '{}'); assert.equal(f.server.requests.length, 2);
fs.writeFileSync(filename, bytes);
const blob = path.join(f.store.artifactStore.root, f.store.core.artifact.get(checkpoint.artifactId).relative);
fs.writeFileSync(blob, 'corrupt checkpoint');
await assert.rejects(f.manager.resume(f.session, checkpoint.id, checkpoint.sha256, f.runtime), /工件校验失败/);
assert.equal(fs.readFileSync(blob, 'utf8'), 'corrupt checkpoint'); assert.equal(f.server.requests.length, 2);
});
test('checkpoint ownership, exact confirmation and concurrent operations reject foreign or stale resumes', async t => {
const f = await failedTask(t), checkpoint = await f.manager.create(f.session), otherSession = f.store.createSession(f.project.id);
await assert.rejects(f.manager.inspect(otherSession, checkpoint.id), /不属于/);
await assert.rejects(f.manager.resume(f.session, checkpoint.id, 'wrong hash', f.runtime), /SHA256/);
const first = f.manager.inspect(f.session, checkpoint.id);
await assert.rejects(f.manager.create(f.session), /正在执行/); await first;
const source = [...f.store.core.tool.values()][0]; source.argsSha256 = '0'.repeat(64);
await assert.rejects(f.manager.create(f.session), /原工具参数/); assert.equal(f.server.requests.length, 2);
});
test('Git identity changes and altered plan proofs cannot be accepted as the sealed recovery state', async t => {
const f = await failedTask(t), plans = f.runtime.taskPlanner();
const plan = await plans.plan(f.session, { expected_revision: 0, steps: [{ key: 'read', title: 'Read source', criterion: 'Use the actual owned read.', kind: 'analysis', tools: ['read_file'], depends_on: [] }] });
const proof = await plans.verify(f.session, { plan_id: plan.id, expected_revision: 1, step_key: 'read', evidence_id: [...f.store.core.tool.values()][0].id });
assert.equal(proof.status, 'completed');
const checkpoint = await f.manager.create(f.session);
const initialized = await runGit(f.projectRoot, ['init', '-q']); assert.equal(initialized.ok, true);
const view = await f.manager.inspect(f.session, checkpoint.id); assert.equal(view.resumable, false); assert.match(view.reasons.join(' '), /Git或工作区身份/);
const record = f.store.core.task_proof.get(proof.proof_id); record.outcome = { fabricated: true };
await assert.rejects(f.manager.inspect(f.session, checkpoint.id), /步骤证明与原工件不一致/);
assert.equal(f.server.requests.length, 2);
});
test('checkpoint records survive SQLite reopen and do not trigger automatic model or tool replay', async t => {
const f = await failedTask(t), checkpoint = await f.manager.create(f.session), taskId = f.session.taskId;
f.store.close(); const restored = new Store(f.data); f.onCleanup(() => restored.close());
const provider = new Provider(); provider.setKey(''); const runtime = new Runtime(restored, provider); f.onCleanup(() => runtime.stopAll());
const session = restored.session(f.session.id), manager = runtime.checkpointManager();
assert.equal(session.taskId, taskId); assert.equal(manager.list(session)[0].id, checkpoint.id);
const view = await manager.inspect(session, checkpoint.id); assert.equal(view.resumable, true);
assert.equal(f.server.requests.length, 2); assert.equal(runtime.hasActivity(), false);
assertToolPairs(session.providerMessages);
});
test('incomplete scans and in-progress project activity refuse checkpoint creation while private credentials are redacted from saved requirements', async t => {
const f = await failedTask(t);
const task = f.store.core.task.get(f.session.taskId); task.instructions.push({ content: 'Secret value checkpoint-owned-secret must not be exported.', createdAt: new Date().toISOString() }); f.store.save(f.session);
const checkpoint = await f.manager.create(f.session), original = f.manager.original(f.session, checkpoint.id).bundle;
assert.equal(JSON.stringify(original).includes('checkpoint-owned-secret'), false); assert.match(original.requirements.instructions[0].content, /已隐藏/);
const large = path.join(f.projectRoot, 'large-source.mjs'); fs.writeFileSync(large, Buffer.alloc(1024 * 1024 + 1));
await assert.rejects(f.manager.create(f.session), /扫描不完整/); fs.rmSync(large);
f.runtime.active.set('owned-project-busy', { project: f.project });
await assert.rejects(f.manager.create(f.session), /正在执行/); f.runtime.active.delete('owned-project-busy');
assert.equal(f.server.requests.length, 2);
});
test('oversized recovery evidence is refused before reading or publishing the checkpoint', async t => {
const f = await failedTask(t), artifact = f.store.core.artifact.get([...f.store.core.tool.values()][0].resultArtifactId), bytes = artifact.bytes;
artifact.bytes = 64 * 1024 * 1024 + 1;
await assert.rejects(f.manager.create(f.session), /64MB上限/);
artifact.bytes = bytes;
assert.equal(f.manager.list(f.session).length, 0); assert.equal(f.server.requests.length, 2);
});
test('compiled checkpoint HTTP APIs bind current Task/Run, isolate sessions and only resume on an explicit matching confirmation', async t => {
const f = fixture(t); f.store.close(); let failing = true;
const model = await compatibleFixture(t, (_url, _body, requests) => requests.length === 1 ? completion([call('http-checkpoint-read', 'read_file', { path: 'README.md' })])
: failing ? { status: 401, body: { error: { message: 'Owned HTTP fixture failure.' } } } : completion([], 'Recovered original HTTP Task.'));
const host = await launchWorkbench(t, f.data);
await host.api('/config', { mode: 'compatible', model: 'owned-http-checkpoint', baseUrl: model.baseUrl, apiKey: 'owned-http-key', contextLimit: 24000 });
await host.api('/sessions/' + f.session.id + '/messages', { content: 'Read README once and keep the original Task.' });
await until(async () => (await host.api('/sessions/' + f.session.id)).status === 'failed');
const before = await host.api('/sessions/' + f.session.id), route = '/sessions/' + f.session.id + '/task-checkpoints';
await assert.rejects(host.api(route, { taskId: before.taskId, runId: '00000000-0000-4000-8000-000000000000' }), /任务或运行已变化/);
const saved = await host.api(route, { taskId: before.taskId, runId: before.runId });
assert.equal((await host.api(route)).length, 1); assert.equal((await host.api(route + '/' + saved.checkpoint.id)).resumable, true);
const other = await host.api('/sessions', { projectId: f.project.id });
await assert.rejects(host.api('/sessions/' + other.id + '/task-checkpoints/' + saved.checkpoint.id), /不属于/);
await assert.rejects(host.api(route + '/' + saved.checkpoint.id + '/resume', { sha256: 'stale' }), /SHA256/);
assert.equal(model.requests.length, 2); failing = false;
await host.api(route + '/' + saved.checkpoint.id + '/resume', { sha256: saved.checkpoint.sha256 });
await until(async () => (await host.api('/sessions/' + f.session.id)).status === 'completed');
const after = await host.api('/sessions/' + f.session.id);
assert.equal(after.taskId, before.taskId); assert.equal(after.execution.run.resumeOf, before.runId); assert.equal(after.execution.progress.totalTools, 1);
assert.equal(model.requests.length, 3); await host.close();
});
for (const point of ['before', 'after']) test('actual process exit ' + point + ' checkpoint commit retains only durable records without replay', async t => {
const f = await failedTask(t), taskId = f.session.taskId, input = path.join(f.root, 'checkpoint-crash.json');
fs.writeFileSync(input, JSON.stringify({ data: f.data, sessionId: f.session.id, point })); f.store.close();
const child = spawn(process.execPath, ['tests/fixtures/task-checkpoint-crash-worker.mjs', input], { cwd: process.cwd(), env: gitEnvironment(), windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });
let output = ''; child.stdout.on('data', bytes => { output += bytes.toString('utf8'); }); child.stderr.on('data', bytes => { output += bytes.toString('utf8'); });
const timer = setTimeout(() => child.kill(), 15000);
const exit = await new Promise((resolve, reject) => { child.once('error', reject); child.once('close', resolve); }); clearTimeout(timer);
assert.equal(exit, 71, output);
const restored = new Store(f.data); f.onCleanup(() => restored.close());
const runtime = new Runtime(restored, new Provider()); f.onCleanup(() => runtime.stopAll());
const current = restored.session(f.session.id), records = runtime.checkpointManager().list(current);
assert.equal(current.taskId, taskId); assert.equal(records.length, point === 'after' ? 1 : 0);
if (records.length) assert.equal((await runtime.checkpointManager().inspect(current, records[0].id)).resumable, true);
assert.equal(restored.core.tool.size, 1); assert.equal([...restored.core.tool.values()][0].state, 'completed');
assert.equal(f.server.requests.length, 2); assert.equal(runtime.hasActivity(), false);
});