58 lines
4.7 KiB
JavaScript
58 lines
4.7 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { RequestPlanner } from '../src/core/context/request-plan.mjs';
|
|
import { DeliveryReporter } from '../src/core/delivery-report.mjs';
|
|
import { requestMetrics } from '../src/shared/contracts/model-request.mjs';
|
|
import { fixture, launchWorkbench, call } from './helpers.mjs';
|
|
|
|
function sample(t) {
|
|
const f = fixture(t), planner = new RequestPlanner(f.store);
|
|
const run = f.store.beginRun(f.session, 'Complete a long engineering task.', false, f.store.config);
|
|
f.session.providerMessages.push({ role: 'user', content: 'Complete a long engineering task.' });
|
|
for (let index = 0; index < 18; index++) {
|
|
f.session.providerMessages.push({ role: 'assistant', content: '', tool_calls: [call('api-history-' + index, 'read_file', { path: 'README.md', note: 'Exact arguments.' })] },
|
|
{ role: 'tool', tool_call_id: 'api-history-' + index, content: JSON.stringify({ status: index === 2 ? 'unknown' : 'completed', requires_verification: index === 2, content: '实际原记录🙂'.repeat(800) }) });
|
|
}
|
|
const plan = planner.prepare(f.session, run, f.store.config, { system: 'Pinned project identity and original requirement.', tools: [] });
|
|
planner.finish(f.session, plan.record, { response: { message: { role: 'assistant', content: 'Fixture complete.' }, usage: { prompt_tokens: 900, completion_tokens: 30 } }, attempts: 1 });
|
|
f.session.status = 'completed'; f.store.finishRun(f.session, run);
|
|
return { ...f, plan };
|
|
}
|
|
|
|
test('context APIs retain exact request identity, paginate archived groups and reject foreign sessions/runs', { timeout: 45000 }, async t => {
|
|
const f = sample(t), other = f.store.createSession(f.project.id); f.store.close();
|
|
const host = await launchWorkbench(t, f.data);
|
|
const session = await host.api('/sessions/' + f.session.id); assert.equal(session.execution.contextPlans.length, 1);
|
|
const plan = await host.api('/sessions/' + f.session.id + '/context-plans/' + f.plan.record.id);
|
|
assert.equal(plan.requestSha256, f.plan.record.requestSha256); assert.equal(plan.actualUsage.prompt_tokens, 900);
|
|
const body = await host.api('/artifacts/' + plan.requestArtifactId + '?session=' + f.session.id + '&limit=64000');
|
|
assert.equal(body.truncated, false); assert.equal(requestMetrics(JSON.parse(body.content)).sha256, plan.requestSha256);
|
|
const first = await host.api('/sessions/' + f.session.id + '/context-plans/' + plan.id + '/history?limit=3');
|
|
assert.equal(first.groups.length, 3); assert.ok(first.truncated); assert.ok(first.totalGroups > 12);
|
|
const next = await host.api('/sessions/' + f.session.id + '/context-plans/' + plan.id + '/history?offset=3&limit=3');
|
|
assert.notEqual(first.groups[0].index, next.groups[0].index);
|
|
const original = await host.api('/artifacts/' + first.groups[0].archive.artifact_id + '?session=' + f.session.id + '&limit=64000');
|
|
assert.equal(original.sha256, first.groups[0].sha256); assert.equal(JSON.parse(original.content)[0].tool_calls[0].function.arguments, JSON.stringify({ path: 'README.md', note: 'Exact arguments.' }));
|
|
await assert.rejects(host.api('/sessions/' + other.id + '/context-plans/' + plan.id), { status: 404 });
|
|
await assert.rejects(host.api('/sessions/' + other.id + '/context-plans?run=' + plan.runId), { status: 404 });
|
|
await assert.rejects(host.api('/artifacts/' + plan.requestArtifactId + '?session=' + other.id), { status: 404 });
|
|
});
|
|
|
|
test('delivery reports verify request body, sealed plan and original archived bytes, and show corruption as needing verification', async t => {
|
|
const f = sample(t), reporter = new DeliveryReporter(f.store);
|
|
const before = await reporter.create(f.session);
|
|
assert.equal(before.report.contextPlans.length, 1); assert.equal(before.report.contextPlans[0].evidenceVerified, true);
|
|
assert.equal(before.report.contextPlans[0].actualUsage.prompt_tokens, 900);
|
|
const markdown = f.store.artifactStore.read(f.store.owned('artifact', before.markdownArtifactId, f.session.id));
|
|
assert.match(markdown, /请求上下文/); assert.match(markdown, /900/);
|
|
const index = JSON.parse(f.store.artifactStore.read(f.store.owned('artifact', f.plan.record.historyIndexArtifactId, f.session.id)));
|
|
const artifact = f.store.owned('artifact', index.archivedGroups[0].archive.artifact_id, f.session.id);
|
|
fs.writeFileSync(path.join(f.data, 'artifacts', artifact.relative), 'changed original archive');
|
|
const after = await reporter.create(f.session);
|
|
assert.equal(after.report.status, 'needs_verification'); assert.equal(after.report.contextPlans[0].evidenceVerified, false);
|
|
assert.match(after.report.contextPlans[0].evidenceError, /工件校验失败/);
|
|
assert.ok(after.report.requiresVerification.some(value => value.tool === 'model_context'));
|
|
});
|