Files
Agent-Workbench/tests/file-results.test.mjs

200 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 { Runtime } from '../src/server/runtime.mjs';
import { Provider } from '../src/server/provider.mjs';
import { FileResults } from '../src/core/tasks/file-results.mjs';
import { TaskPlanManager } from '../src/core/tasks/task-plan-manager.mjs';
import { DeliveryReporter, renderDeliveryMarkdown } from '../src/core/delivery-report.mjs';
import { mobileSession } from '../src/core/companion/projection.mjs';
import { readText, readLines, proposeFile, applyChange } from '../src/server/files.mjs';
import { filePage } from '../src/core/context/read-budget.mjs';
import { requestedFilePaths } from '../src/shared/contracts/file-intent.mjs';
import { fixture, call, assertToolPairs } from './helpers.mjs';
const answer = content => ({ message: { role: 'assistant', content }, usage: { prompt_tokens: 11, completion_tokens: 3 } });
const calls = (...tools) => ({ ...answer(''), message: { role: 'assistant', content: '', tool_calls: tools } });
function harness(t, respond) {
const f = fixture(t), provider = new Provider(); let round = 0;
provider.complete = async (...args) => respond(round++, f, args);
const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
f.runtime = runtime; f.manager = runtime.fileResults(); f.wait = () => runtime.active.get(f.session.id).promise; return f;
}
function begin(f, goal = '创建 README.md 文件,已有成果满足时采用,无需测试。', forceNewTask = false) {
const run = f.store.beginRun(f.session, goal, false, f.store.config, { forceNewTask });
f.session.status = 'paused'; f.store.finishRun(f.session, run, 'fixture_ready'); return run;
}
function read(f, args) {
const run = f.store.core.run.get(f.session.runId), invocation = call('read-' + crypto.randomUUID(), 'read_file', args);
f.session.providerMessages.push({ role: 'assistant', content: '', tool_calls: [invocation] });
const event = f.store.event(f.session, { kind: 'tool', phase: 'queued', tool: 'read_file', toolCallId: invocation.id, label: 'Fixture actual read' });
const record = f.store.beginInvocation(f.session, invocation, run, event); f.store.invocationState(f.session, event, 'running');
const result = filePage(readLines(f.project, args), args, { callId: invocation.id });
event.phase = 'completed'; f.store.finishInvocation(f.session, event, result);
f.session.providerMessages.push({ role: 'tool', tool_call_id: invocation.id, content: JSON.stringify(result) });
return invocation.id;
}
function input(f, extra = {}) { return { files: [{ path: 'README.md', sha256: readText(f.project, 'README.md').sha256, ...extra }], reason: '已按当前目标核对现有文件内容。' }; }
test('file targets exclude referenced inputs and prohibited subsequent modifications', () => {
for (const goal of ['create result.html using template.html', '创建 result.html 参考 README.md', '创建 result.html 不要修改 README.md']) {
assert.deepEqual(requestedFilePaths(goal), ['result.html']);
}
assert.deepEqual(requestedFilePaths('创建 result.html 和 style.css,参考 README.md'), ['result.html', 'style.css']);
assert.deepEqual(requestedFilePaths('创建基于参考 README.md 的说明'), []);
});
test('full actual read and existing receipt complete a no-test file task without fabricating changes, checks or commits', async t => {
const f = harness(t, (round, f) => round === 0 ? calls(call('existing-read', 'read_file', { path: 'README.md' }))
: round === 1 ? calls(call('existing-result', 'verify_existing_files', input(f, { read_call_ids: ['existing-read'] }))) : answer('已核对已有成果,无新增修改。'));
const bytes = fs.readFileSync(path.join(f.projectRoot, 'README.md'));
f.runtime.start(f.session.id, '创建 README.md 文件,已有成果满足时采用,无需测试。'); await f.wait();
assert.equal(f.session.status, 'completed');
const task = f.store.core.task.get(f.session.taskId);
assert.equal(task.delivery.status, 'existing_verified'); assert.equal(task.delivery.newChanges, 0); assert.equal(task.delivery.checks, 'not_requested');
assert.equal(f.session.changes.length, 0); assert.equal(f.store.core.check_job.size, 0); assert.equal(f.store.core.delivery_job.size, 0);
assert.deepEqual(fs.readFileSync(path.join(f.projectRoot, 'README.md')), bytes);
const row = f.manager.records(f.session)[0]; assert.equal(f.manager.verify(f.session, row.id).id, row.id);
assert.equal(row.files[0].reads[0].callId, 'existing-read'); assertToolPairs(f.session.providerMessages);
f.runtime.start(f.session.id, '继续完成'); await f.wait();
assert.equal(f.session.taskId, task.id); assert.equal(f.manager.inspect(f.session).results[0].verification, 'current'); assert.equal(f.session.status, 'completed');
});
test('partial pages, partial lines, wrong hashes and failed reads cannot be adopted; contiguous full paging can', t => {
const f = fixture(t); begin(f); const manager = new FileResults(f.store);
const a = read(f, { path: 'README.md', start_line: 1, end_line: 1 });
assert.throws(() => manager.record(f.session, input(f, { read_call_ids: [a] })), /完整读取/);
const b = read(f, { path: 'README.md', start_line: 2, end_line: 4, limit: 8 });
assert.throws(() => manager.record(f.session, input(f, { read_call_ids: [a, b] })), /完整读取/);
const c = read(f, { path: 'README.md', start_line: 2, end_line: 4, offset: 8 });
const result = manager.record(f.session, input(f, { read_call_ids: [a, b, c] }));
assert.equal(manager.verify(f.session, result.result_id).files[0].reads.length, 3);
const invocation = [...f.store.core.tool.values()].find(row => row.callId === c); invocation.state = 'unknown'; invocation.requiresVerification = true;
assert.throws(() => manager.verify(f.session, result.result_id), /读取调用/);
});
test('user edits and new requirements invalidate receipts, and a second task must explicitly adopt a source in its own project/session', t => {
const f = fixture(t); begin(f); const manager = new FileResults(f.store);
const old = manager.record(f.session, input(f), { source: 'user' }), oldTask = f.session.taskId;
assert.throws(() => manager.record(f.session, input(f, { source_result_id: old.result_id }), { source: 'user' }), /不重复采用自身成果/);
assert.equal(manager.records(f.session).length, 1);
fs.appendFileSync(path.join(f.projectRoot, 'README.md'), '\nUser contribution\n');
assert.equal(manager.inspect(f.session).results[0].verification, 'stale');
assert.throws(() => manager.record(f.session, { ...input(f), files: [{ path: 'README.md', sha256: manager.records(f.session)[0].files[0].sha256 }] }, { source: 'user' }), /变化/);
begin(f, '创建 README.md 文件,沿用已有内容,无需测试。', true);
assert.notEqual(f.session.taskId, oldTask);
assert.throws(() => manager.record(f.session, input(f), { source: 'user' }), /显式选择/);
const adopted = manager.record(f.session, input(f, { source_result_id: old.result_id }), { source: 'user' });
const origin = manager.verify(f.session, adopted.result_id).files[0].origin;
assert.equal(origin.taskId, oldTask); assert.notEqual(origin.sha256, manager.records(f.session)[0].files[0].sha256);
const otherRoot = path.join(f.root, 'other'); fs.mkdirSync(otherRoot); fs.writeFileSync(path.join(otherRoot, 'README.md'), 'other');
const otherProject = f.store.addProject(otherRoot), other = f.store.createSession(otherProject.id);
f.store.beginRun(other, '创建 README.md 文件', false, f.store.config);
assert.throws(() => manager.verify(other, adopted.result_id, { current: false }), /不属于/);
begin(f, '补充需求:标题必须更清晰');
assert.equal(manager.inspect(f.session).results[0].verification, 'stale');
});
test('adopting an unchanged file never satisfies an explicit modification or a new modification instruction', async t => {
const f = fixture(t); const runtime = new Runtime(f.store, new Provider()); f.onCleanup(() => runtime.stopAll());
begin(f, '修改 README.md 文件标题,无需测试'); const manager = runtime.fileResults();
manager.record(f.session, input(f), { source: 'user' });
assert.equal(manager.completion(f.session).requiresChange, true);
await assert.rejects(runtime.completeFileResults(f.session), /修改/);
const proposed = proposeFile(f.store, f.session, { path: 'README.md', content: '# Correct title\n', reason: '修改标题' });
applyChange(f.store, f.session, f.session.changes.at(-1).id);
assert.equal(manager.completion(f.session).satisfied, true);
await new Promise(resolve => setTimeout(resolve, 10));
begin(f, '再修改 README.md 文件,增加结果说明', true);
manager.record(f.session, input(f, { source_result_id: manager.records({ ...f.session, taskId: f.store.core.task.get(f.session.taskId).previousTaskId })[0].id }), { source: 'user' });
assert.equal(manager.completion(f.session).requiresChange, true);
});
test('required checks and stale passing flags cannot be bypassed by current file receipts', async t => {
const f = fixture(t), runtime = new Runtime(f.store, new Provider()); f.onCleanup(() => runtime.stopAll());
begin(f, '创建 README.md 文件并运行测试');
runtime.fileResults().record(f.session, input(f), { source: 'user' });
await assert.rejects(runtime.completeFileResults(f.session), /工程检查/);
f.store.core.task.get(f.session.taskId).verification = { status: 'passed', checkJobId: crypto.randomUUID() };
await assert.rejects(runtime.completeFileResults(f.session), /工程检查/);
assert.equal(f.session.status, 'paused');
});
test('results plan proofs bind current file receipts while changes proofs still demand real applied change sets', async t => {
const f = fixture(t); begin(f); const manager = new FileResults(f.store), planner = new TaskPlanManager(f.store);
const result = manager.record(f.session, input(f), { source: 'user' });
const plan = await planner.plan(f.session, { expected_revision: 0, steps: [{ key: 'existing', title: '核对已有说明', criterion: '当前说明原文件SHA与完整成果凭据一致', kind: 'results', paths: ['README.md'] }] });
const proof = await planner.verify(f.session, { plan_id: plan.id, expected_revision: 1, step_key: 'existing', evidence_id: result.result_id });
assert.equal(proof.status, 'completed'); assert.equal((await planner.inspect(f.session)).all_verified, true);
await assert.rejects(planner.evidence(f.session, { kind: 'changes', paths: ['README.md'] }, result.result_id), /不属于/);
fs.appendFileSync(path.join(f.projectRoot, 'README.md'), 'new owner text');
assert.equal((await planner.inspect(f.session)).all_verified, false);
});
test('duplicate receipts cannot create progress indefinitely or reset no-progress reads', async t => {
const f = harness(t, (round, f) => round === 0 ? calls(call('loop-read', 'read_file', { path: 'README.md' })) : calls(call('loop-' + round, 'verify_existing_files', input(f, { read_call_ids: ['loop-read'] }))));
f.runtime.start(f.session.id, '创建 README.md 文件,无需测试'); await f.wait();
assert.equal(f.session.status, 'paused'); assert.equal(f.store.core.run.get(f.session.runId).stopReason, 'no_progress');
assert.equal(f.manager.records(f.session).length, 1); assert.ok(f.store.core.run.get(f.session.runId).steps < 10);
});
test('manual completion preserves the failed original run and refuses unknown operations', async t => {
const f = fixture(t), runtime = new Runtime(f.store, new Provider()); f.onCleanup(() => runtime.stopAll());
const run = begin(f); runtime.fileResults().record(f.session, input(f), { source: 'user' });
const original = structuredClone(run);
await runtime.completeFileResults(f.session);
assert.equal(f.session.status, 'completed'); assert.equal(f.store.core.task.get(f.session.taskId).status, 'completed');
assert.deepEqual(run, original); assert.equal(f.session.changes.length, 0);
f.store.core.tool.set('unknown', { id: 'unknown', taskId: f.session.taskId, requiresVerification: true });
await assert.rejects(runtime.completeFileResults(f.session), /未知结果/);
});
test('completion refuses active work, files changed during inspection and a newly selected task', async t => {
const f = fixture(t), runtime = new Runtime(f.store, new Provider()); f.onCleanup(() => runtime.stopAll());
begin(f); runtime.fileResults().record(f.session, input(f), { source: 'user' });
runtime.active.set(f.session.id, { project: f.project });
try { await assert.rejects(runtime.completeFileResults(f.session), /仍有活动/); } finally { runtime.active.delete(f.session.id); }
const planner = runtime.taskPlanner();
planner.inspect = async () => { fs.appendFileSync(path.join(f.projectRoot, 'README.md'), 'Edit while inspecting\n'); return { configured: false }; };
await assert.rejects(runtime.completeFileResults(f.session), /核对期间/);
assert.equal(f.session.status, 'paused');
runtime.fileResults().record(f.session, input(f), { source: 'user' });
planner.inspect = async () => { begin(f, '创建 README.md 文件,无需测试', true); return { configured: false }; };
await assert.rejects(runtime.completeFileResults(f.session), /核对期间/);
assert.equal(f.session.status, 'paused');
assert.equal(f.store.core.task.get(f.session.taskId).delivery, undefined);
});
test('unrelated files cannot stand in for named output paths, nor can a late apply satisfy newer instructions with an old proposal', async t => {
const f = fixture(t), runtime = new Runtime(f.store, new Provider()); f.onCleanup(() => runtime.stopAll());
begin(f, '创建 result.html 文件,无需测试'); const manager = runtime.fileResults();
manager.record(f.session, input(f), { source: 'user' });
assert.deepEqual(manager.completion(f.session).missingPaths, ['result.html']); assert.equal(manager.completion(f.session).satisfied, false);
begin(f, '修改 README.md 文件标题,无需测试', true);
proposeFile(f.store, f.session, { path: 'README.md', content: '# Early proposal\n', reason: '早于追加要求' });
await new Promise(resolve => setTimeout(resolve, 10));
begin(f, '再修改 README.md 文件,加入新要求');
applyChange(f.store, f.session, f.session.changes.at(-1).id);
assert.equal(manager.completion(f.session).requiresChange, true);
assert.equal(f.session.status, 'paused');
await assert.rejects(runtime.completeFileResults(f.session), /修改/);
});
test('delivery and mobile projections distinguish adopted bytes, new changes, no tests and no commit; mutations cannot rewrite receipts', async t => {
const f = fixture(t), runtime = new Runtime(f.store, new Provider()); f.onCleanup(() => runtime.stopAll());
begin(f); const manager = runtime.fileResults(), saved = manager.record(f.session, input(f), { source: 'user' });
await runtime.completeFileResults(f.session);
const reporter = new DeliveryReporter(f.store), { report } = await reporter.create(f.session);
assert.equal(report.status, 'existing_verified'); assert.equal(report.checkPolicy, 'not_requested'); assert.equal(report.gitOutcome.status, 'no_new_changes');
assert.equal(report.verifiedCheckJobId, null); assert.equal(report.deliveries.length, 0); assert.match(renderDeliveryMarkdown(report), /未创建提交/);
const projection = mobileSession(f.store, runtime, f.session);
assert.equal(projection.fileResults[0].verification, 'current'); assert.equal(projection.fileResults[0].files[0].sha256, report.fileResults[0].files[0].sha256);
assert.equal(JSON.stringify(projection).includes(f.projectRoot.replaceAll('\\', '\\\\')), false);
const row = manager.records(f.session)[0]; row.reason = 'tampered';
assert.throws(() => manager.verify(f.session, saved.result_id), /封存原凭据/); row.reason = '已按当前目标核对现有文件内容。';
fs.appendFileSync(path.join(f.projectRoot, 'README.md'), 'changed');
assert.equal((await reporter.create(f.session)).report.status, 'needs_results');
assert.equal(mobileSession(f.store, runtime, f.session).fileResults[0].verification, 'stale');
});