import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import path from 'node:path'; import { createHash, randomUUID } from 'node:crypto'; import { RequestPlanner, historyGroups, verifyPlanEvidence } from '../src/core/context/request-plan.mjs'; import { workingCandidates, workingMessage } from '../src/core/context/working-set.mjs'; import { filePage } from '../src/core/context/read-budget.mjs'; import { requestMetrics } from '../src/shared/contracts/model-request.mjs'; import { Runtime } from '../src/server/runtime.mjs'; import { Provider, toolDefinitions } from '../src/server/provider.mjs'; import { Store } from '../src/server/store.mjs'; import { CheckRunner } from '../src/core/check-runner.mjs'; import { createDeliveryProject } from './fixtures/delivery-project.mjs'; import { fixture, call, completion, compatibleFixture, until, assertToolPairs } from './helpers.mjs'; const sha = text => createHash('sha256').update(text, 'utf8').digest('hex'); const setFrom = messages => messages.filter(message => message.role === 'system').map(message => { try { return JSON.parse(message.content.split('\n').at(-1)); } catch { return {}; } }).find(value => value.working_set)?.data; function setup(t, config = {}) { const f = fixture(t); f.config = { ...f.store.config, contextLimit: 10000, maxOutputTokens: 1024, ...config }; f.run = f.store.beginRun(f.session, 'Fix this project, preserve the public API and report actual checks.', false, f.config); f.session.providerMessages.push({ role: 'user', content: f.store.core.task.get(f.run.taskId).goal }); f.planner = new RequestPlanner(f.store); f.prepare = () => f.planner.prepare(f.session, f.run, f.config, { system: 'Current project and complete Task requirements.', tools: toolDefinitions }); return f; } function group(f, items) { const calls = items.map(item => call(randomUUID(), item.tool, item.args)); const results = []; for (const [index, invocation] of calls.entries()) { const event = f.store.event(f.session, { kind: 'tool', tool: invocation.function.name, toolCallId: invocation.id, runId: f.run.id, phase: 'queued', startedAt: new Date().toISOString(), label: 'Recorded fixture observation' }); f.store.registerInvocation(f.session, invocation, f.run, event); f.store.beginInvocation(f.session, invocation, f.run, event); f.store.finishInvocation(f.session, event, items[index].result); results.push({ role: 'tool', tool_call_id: invocation.id, content: JSON.stringify(items[index].result) }); } f.session.providerMessages.push({ role: 'assistant', content: '', tool_calls: calls }, ...results); f.store.save(f.session); return calls; } const read = (file, content) => ({ tool: 'read_file', args: { path: file }, result: { path: file, content, sha256: sha(content), start_line: 1, end_line: 2, total_lines: 2, truncated: false } }); const huge = () => ({ tool: 'query_code', args: { query: 'source' }, result: { results: 'Large mixed metadata '.repeat(16000) } }); test('small code and actual failed command stay visible when their original multi-tool group cannot fit', t => { const f = setup(t), code = 'export const add = (a, b) => a + b;\n'; const calls = group(f, [read('src/add.mjs', code), huge(), { tool: 'run_command', args: { command: 'node check.mjs', reason: 'Fixture evidence.' }, result: { status: 'completed', exit_code: 1, stdout: '1 test failed', stderr: 'Expected add(2, 3) to equal 5' } }]); const original = structuredClone(f.session.providerMessages), plan = f.prepare(), set = setFrom(plan.messages); assert.ok(set); assert.equal(set.taskId, f.run.taskId); assert.equal(set.entries.find(entry => entry.observation.path === 'src/add.mjs').content, code); assert.ok(set.entries.some(entry => entry.observation.exit_code === 1 && entry.content.includes('Expected add'))); assert.ok(!plan.messages.some(message => message.tool_calls?.some(call => call.id === calls[0].id))); assert.deepEqual(f.session.providerMessages, original); assertToolPairs(plan.messages); assert.ok(requestMetrics(plan.body).estimatedInputTokens <= plan.record.budget.inputBudget); verifyPlanEvidence(f.store, f.session, plan.record); const sent = f.store.artifactStore.read(f.store.owned('artifact', plan.record.requestArtifactId, f.session.id)); assert.equal(sha(sent), plan.record.requestSha256); assert.ok(plan.record.workingSetEntries >= 2); }); test('mixed group retains six distinct small file observations with exact multilingual text and bounded request bytes', t => { const f = setup(t, { contextLimit: 14000 }), files = Array.from({ length: 6 }, (_, index) => read('src/模块-' + index + '.mjs', 'export const value = "第' + index + '个🙂\\雪";\r\n')); group(f, [...files, huge()]); const plan = f.prepare(), set = setFrom(plan.messages); for (const file of files) { const entry = set.entries.find(entry => entry.observation.path === file.args.path); assert.ok(entry); assert.equal(entry.content, file.result.content); assert.equal(entry.content_sha256, sha(file.result.content)); assert.ok(entry.content.isWellFormed()); assert.equal(entry.truncated, false); } assert.ok(requestMetrics(plan.body).estimatedInputTokens <= plan.record.budget.inputBudget); assertToolPairs(plan.messages); verifyPlanEvidence(f.store, f.session, plan.record); }); test('working observations are bound to the current Task and stop treating a pre-application file revision as current evidence', t => { const f = setup(t); group(f, [read('src/old.mjs', 'Previous task bytes\n'), huge()]); f.session.status = 'completed'; f.store.finishRun(f.session, f.run); f.run = f.store.beginRun(f.session, 'A different Task must use its own observations.', false, f.config); f.session.providerMessages.push({ role: 'user', content: 'A different Task must use its own observations.' }); group(f, [read('src/current.mjs', 'Current task bytes\n'), huge()]); const plan = f.prepare(), set = setFrom(plan.messages); assert.ok(set.entries.some(entry => entry.observation.path === 'src/current.mjs')); assert.ok(!set.entries.some(entry => entry.observation.path === 'src/old.mjs')); f.session.changes.push({ id: randomUUID(), taskId: f.run.taskId, path: 'src/current.mjs', status: 'applied', sha256: sha('Newly applied revision'), appliedAt: new Date(Date.now() + 1000).toISOString() }); assert.ok(!workingCandidates(f.store, f.session, f.run, historyGroups(f.session.providerMessages)).some(entry => entry.observation.path === 'src/current.mjs')); // A historical sent observation remains valid evidence of what was sent. verifyPlanEvidence(f.store, f.session, plan.record); }); test('a sealed observation rejects changed or missing original result artifacts instead of recreating evidence', t => { const f = setup(t); group(f, [read('src/evidence.mjs', 'Exact recorded source\n'), huge()]); const plan = f.prepare(), set = setFrom(plan.messages), entry = set.entries[0]; const artifact = f.store.owned('artifact', entry.source.result_artifact_id, f.session.id), target = path.join(f.data, 'artifacts', artifact.relative); fs.writeFileSync(target, 'External corrupted observation'); assert.throws(() => verifyPlanEvidence(f.store, f.session, plan.record), /工件校验失败/); assert.throws(() => f.prepare(), /工件校验失败/); assert.equal(fs.readFileSync(target, 'utf8'), 'External corrupted observation'); fs.unlinkSync(target); assert.throws(() => f.prepare(), /ENOENT/); assert.equal(fs.existsSync(target), false); }); test('a real passed-check observation is excluded after a source edit invalidates the check, while its historical sent evidence remains valid', async t => { const f = setup(t); createDeliveryProject(f); fs.writeFileSync(path.join(f.projectRoot, 'src/add.mjs'), 'export const add = (a, b) => a + b;\n'); const runner = new CheckRunner(f.store), plan = await runner.plan(f.session), { promise } = await runner.start(f.session, plan.id); const job = await promise; assert.equal(job.status, 'passed'); group(f, [{ tool: 'run_checks', args: { plan_id: plan.id }, result: { status: 'completed', check_status: job.status, job_id: job.id, plan_id: plan.id, results: job.results, summary: job.summary, fingerprint: job.fingerprint } }, huge()]); const sent = f.prepare(); assert.ok(setFrom(sent.messages).entries.some(entry => entry.observation.job_id === job.id)); fs.appendFileSync(path.join(f.projectRoot, 'src/add.mjs'), '// Later real source edit.\n'); await runner.plan(f.session); assert.equal(job.stale, true); assert.ok(!workingCandidates(f.store, f.session, f.run, historyGroups(f.session.providerMessages)).some(entry => entry.observation.job_id === job.id)); verifyPlanEvidence(f.store, f.session, sent.record); }); test('budgeted file pages reconstruct UTF-8 text including long single lines and surrogate boundaries', () => { const text = '首行🙂"\\\r\n' + '超长单行🙂\\"'.repeat(2000) + '\n最后一行🙂'; const file = { path: '长文件.mjs', content: text, sha256: sha(text), start_line: 21, end_line: 23, total_lines: 23, bom: true, truncated: true }; let offset = 0, joined = '', count = 0; while (offset < text.length) { const page = filePage(file, { offset }, { maximumMessageBytes: 2800, callId: 'file-page' }); assert.ok(Buffer.byteLength(JSON.stringify({ role: 'tool', tool_call_id: 'file-page', content: JSON.stringify(page) })) <= 2800); assert.ok(page.next_offset > offset); assert.equal(page.offset, offset); assert.equal(page.content, text.slice(offset, page.next_offset)); assert.ok(page.content.isWellFormed()); assert.equal(page.sha256, file.sha256); assert.equal(page.range_start_line, 21); assert.equal(page.range_end_line, 23); joined += page.content; offset = page.next_offset; count++; } assert.equal(joined, text); assert.ok(count > 10); const unicode = filePage({ ...file, content: '🙂雪' }, { offset: 1, limit: 1 }, { maximumMessageBytes: 1500 }); assert.equal(unicode.offset, 0); assert.equal(unicode.content, '🙂'); assert.equal(unicode.next_offset, 2); }); test('actual HTTP multi-tool reads survive an oversized runtime snapshot and the sealed evidence survives reopening', { timeout: 30000 }, async t => { const f = fixture(t); fs.mkdirSync(path.join(f.projectRoot, 'src')); const code = 'export const actual = "真实模块🙂";\n'; fs.writeFileSync(path.join(f.projectRoot, 'src/actual.mjs'), code); let requests = 0; const model = await compatibleFixture(t, (_route, body) => { requests++; assertToolPairs(body.messages); if (requests === 1) { const run = f.store.core.run.get(f.session.runId); for (let index = 0; index < 160; index++) { const artifact = f.store.artifactStore.write(f.session, run, 'Runtime snapshot fixture ' + index, { kind: 'fixture-evidence' }); f.store.core.artifact.set(artifact.id, artifact); } f.store.save(f.session); return { body: completion([call('mixed-read', 'read_file', { path: 'src/actual.mjs' }), call('mixed-snapshot', 'inspect_session', {})]) }; } const set = setFrom(body.messages); assert.ok(set, 'A small result in a large mixed group must be visible in a sealed observation'); assert.equal(set.entries.find(entry => entry.observation.path === 'src/actual.mjs').content, code); return { body: completion([], 'Used the actual source observation to complete this read-only task.') }; }); f.store.saveConfig({ mode: 'compatible', baseUrl: model.baseUrl, model: 'working-set-fixture', maxSteps: 6 }); const runtime = new Runtime(f.store, new Provider()); f.onCleanup(() => runtime.stopAll()); runtime.start(f.session.id, 'Read src/actual.mjs and the runtime state. Preserve all files.'); await until(() => !runtime.isActive(f.session.id), 20000); assert.equal(f.session.status, 'completed', JSON.stringify(f.session.failure)); assert.equal(requests, 2); assert.equal([...f.store.core.tool.values()].length, 2); assert.equal(fs.readFileSync(path.join(f.projectRoot, 'src/actual.mjs'), 'utf8'), code); const plan = [...f.store.core.context_plan.values()].at(-1); assert.ok(plan.workingSetEntries); verifyPlanEvidence(f.store, f.session, plan); f.store.close(); const reopened = new Store(f.data); f.onCleanup(() => reopened.close()); verifyPlanEvidence(reopened, reopened.session(f.session.id), reopened.core.context_plan.get(plan.id)); assert.equal(requests, 2); assertToolPairs(reopened.session(f.session.id).providerMessages); }); test('actual HTTP file pagination advances over a long file with each returned page visible in the next request', { timeout: 30000 }, async t => { const f = fixture(t), text = ('真实长行🙂"\\' + 'content'.repeat(25) + '\n').repeat(150); fs.writeFileSync(path.join(f.projectRoot, 'README.md'), text); let joined = '', requests = 0, priorOffset = 0; const model = await compatibleFixture(t, (_route, body) => { requests++; assertToolPairs(body.messages); if (requests === 1) return { body: completion([call('file-first', 'read_file', { path: 'README.md', start_line: 1, end_line: 200 })]) }; const result = body.messages.filter(message => message.role === 'tool').map(message => JSON.parse(message.content)).findLast(result => result.path === 'README.md'); assert.ok(result, 'The newest actual budgeted page must be sent'); assert.equal(result.offset, priorOffset); assert.equal(result.content, text.slice(result.offset, result.next_offset)); joined += result.content; priorOffset = result.next_offset; if (result.next_offset === result.total_characters) return { body: completion([], 'Completed the actual paginated read.') }; return { body: completion([call('file-next-' + requests, 'read_file', { path: 'README.md', start_line: result.range_start_line, end_line: result.range_end_line, offset: result.next_offset })]) }; }); f.store.saveConfig({ mode: 'compatible', baseUrl: model.baseUrl, model: 'file-page-fixture', maxSteps: 12 }); const runtime = new Runtime(f.store, new Provider()); f.onCleanup(() => runtime.stopAll()); runtime.start(f.session.id, 'Read this long file using actual pages. Preserve its bytes.'); await until(() => !runtime.isActive(f.session.id), 20000); assert.equal(f.session.status, 'completed', JSON.stringify(f.session.failure)); assert.ok(requests > 2); assert.equal(joined, text); assert.equal(fs.readFileSync(path.join(f.projectRoot, 'README.md'), 'utf8'), text); }); test('later mixed history pages cannot displace the actual module sources and complete requirements', t => { const f = setup(t, { contextLimit: 16000, maxOutputTokens: 2048 }); const files = [read('src/normalize.mjs', 'export const normalizeTasks = input => input;\n'), read('src/summary.mjs', 'export const summarizeTasks = tasks => ({ total: tasks.length });\n'), read('src/render.mjs', 'export const renderSummary = summary => JSON.stringify(summary);\n'), read('src/cli.mjs', 'export const run = args => args;\n'), read('README.md', 'The complete seven engineering requirements.\n'.repeat(18)), read('test/delivery.test.mjs', 'A sealed actual test assertion with Unicode 雪🙂.\n'.repeat(65))]; group(f, [...files, huge()]); for (let index = 0; index < 12; index++) { const text = 'Historical locator metadata '.repeat(400), artifact = f.store.artifactStore.write(f.session, f.run, text, { kind: 'context-history-index' }); f.store.core.artifact.set(artifact.id, artifact); group(f, [{ tool: 'read_artifact', args: { artifact_id: artifact.id, offset: 0 }, result: { artifact_id: artifact.id, kind: artifact.kind, sha256: artifact.sha256, content: text, offset: 0, next_offset: text.length, total_characters: text.length, truncated: false, } }]); } const plan = f.planner.prepare(f.session, f.run, f.config, { system: 'Complete mandatory project and Task state. '.repeat(160), tools: toolDefinitions }); const set = setFrom(plan.messages); assert.ok(set); for (const file of files) { const entry = set.entries.find(entry => entry.observation.path === file.args.path); assert.ok(entry, 'Missing actual observed file: ' + file.args.path); assert.equal(entry.content, file.result.content); assert.equal(entry.truncated, false); } assertToolPairs(plan.messages); verifyPlanEvidence(f.store, f.session, plan.record); assert.ok(plan.record.estimatedInputTokens <= plan.record.budget.inputBudget); }); test('a later narrow page of the same file revision cannot evict the wider actual observation', t => { const f = setup(t), text = 'An actual previously read complete source line.\n'.repeat(42); group(f, [read('src/complete.mjs', text), huge()]); for (const offset of [400, 800, 1200]) group(f, [{ tool: 'read_file', args: { path: 'src/complete.mjs', offset }, result: { path: 'src/complete.mjs', sha256: sha(text), content: text.slice(offset, offset + 180), start_line: 1, end_line: 2, range_start_line: 1, range_end_line: 2, offset, next_offset: offset + 180, total_characters: text.length, } }, huge()]); const plan = f.prepare(), set = setFrom(plan.messages); const candidates = workingCandidates(f.store, f.session, f.run, historyGroups(f.session.providerMessages)); assert.equal(candidates.find(candidate => candidate.observation.path === 'src/complete.mjs').observation.content, text); assert.equal(set.entries.find(entry => entry.observation.path === 'src/complete.mjs').content, text); verifyPlanEvidence(f.store, f.session, plan.record); }); test('six small source files stay complete across metadata turns even when mandatory evidence exceeds the soft target', t => { const f = setup(t, { contextLimit: 32000, maxOutputTokens: 2048 }); const files = [['src/App.tsx', 1991], ['test/domain.test.mjs', 1715], ['src/transition.ts', 167], ['src/query.ts', 142], ['src/types.ts', 82], ['src/summary.ts', 155]].map(([name, size]) => read(name, ('// ' + name + '\n').padEnd(size, 'x'))); group(f, [...files, huge()]); const system = 'Complete original Task constraints and required permission boundaries. '.repeat(620); const original = structuredClone(f.session.providerMessages); let exceededSoftTarget = false; for (let index = 0; index < 4; index++) { group(f, [{ tool: 'inspect_session', args: {}, result: { session: { status: 'running' }, execution: { progress: { completedTools: index + 7 } }, metadata: 'Historical metadata '.repeat(1800) } }]); const prepared = f.planner.prepare(f.session, f.run, f.config, { system, tools: toolDefinitions }); const set = setFrom(prepared.messages); for (const file of files) { const entry = set.entries.find(entry => entry.observation.path === file.args.path); assert.equal(entry?.content, file.result.content); assert.equal(entry.truncated, false); } assert.equal(prepared.record.budget.targetInputTokens, 16000); assert.ok(prepared.record.estimatedInputTokens <= prepared.record.budget.inputBudget); assert.ok(prepared.record.workingRetentionTokens > 2048); exceededSoftTarget ||= prepared.record.estimatedInputTokens > 16000; assertToolPairs(prepared.messages); verifyPlanEvidence(f.store, f.session, prepared.record); } assert.ok(exceededSoftTarget, 'The regression must exercise the soft-target boundary.'); assert.deepEqual(f.session.providerMessages.slice(0, original.length), original); }); test('historical read-artifact navigation points to the original artifact and next page, never its result wrapper', t => { const f = setup(t), text = 'An actual saved failure log.\n'; const artifact = f.store.artifactStore.write(f.session, f.run, text, { kind: 'check-log' }); f.store.core.artifact.set(artifact.id, artifact); const calls = group(f, [{ tool: 'read_artifact', args: { artifact_id: artifact.id, offset: 0 }, result: { artifact_id: artifact.id, kind: artifact.kind, sha256: artifact.sha256, content: text, offset: 0, next_offset: text.length, total_characters: text.length, truncated: false, } }, huge()]); const wrapper = [...f.store.core.tool.values()].find(tool => tool.callId === calls[0].id).resultArtifactId; const plan = f.prepare(), navigation = plan.messages.filter(message => message.role === 'system').map(message => { try { return JSON.parse(message.content.split('\n').at(-1)).working_evidence; } catch { return null; } }).find(Boolean); const link = navigation.find(item => item.name === 'read_artifact'); assert.ok(link); assert.equal(link.artifact_id, artifact.id); assert.equal(link.next_offset, text.length); assert.notEqual(link.result_artifact_id, wrapper); verifyPlanEvidence(f.store, f.session, plan.record); }); test('a 1.6.1 full-provenance sent view remains verifiable after introducing the compact view', t => { const f = setup(t); group(f, [read('src/legacy.mjs', 'export const legacy = true;\n'), huge()]); const prepared = f.prepare(), record = structuredClone(prepared.record); const workingArtifact = f.store.owned('artifact', record.workingSetArtifactId, f.session.id); const document = JSON.parse(f.store.artifactStore.read(workingArtifact)); const oldMessage = workingMessage(workingArtifact, document, 1), body = structuredClone(prepared.body); const currentMessage = workingMessage(workingArtifact, document, 2); body.messages = body.messages.map(message => message.content === currentMessage.content ? oldMessage : message); const metrics = requestMetrics(body), request = f.planner.artifact(f.session, f.run, body, 'model-request'); Object.assign(record, { requestSha256: metrics.sha256, requestBytes: metrics.bytes, estimatedInputTokens: metrics.estimatedInputTokens, requestArtifactId: request.id }); // A modern reservation is bound to the actual request body. Rewriting only // the sent view must fail; genuine 1.6.1 records predate these fields. assert.throws(() => verifyPlanEvidence(f.store, f.session, record), /模型预算预留与原估算\/用量\/实际线路不一致/); delete record.tokenReservation; delete record.maxBudgetAttempts; delete record.workingSetViewVersion; delete record.workingRetentionTokens; const frozen = f.planner.artifact(f.session, f.run, record, 'context-plan'); record.artifactId = frozen.id; assert.notEqual(oldMessage.content, currentMessage.content); assert.ok(oldMessage.content.includes('invocation_id')); assert.ok(!currentMessage.content.includes('invocation_id')); verifyPlanEvidence(f.store, f.session, record); record.workingSetViewVersion = 2; assert.throws(() => verifyPlanEvidence(f.store, f.session, record), /工作依据视图或保留预算/); }); test('actual HTTP requests keep four observed modules after a later large history read and preserve all source bytes', { timeout: 30000 }, async t => { const f = fixture(t), files = ['normalize', 'summary', 'render', 'cli'].map(name => ({ path: 'src/' + name + '.mjs', content: 'export const ' + name + ' = value => value; // observed 雪🙂\n', })); fs.mkdirSync(path.join(f.projectRoot, 'src'), { recursive: true }); for (const file of files) fs.writeFileSync(path.join(f.projectRoot, file.path), file.content); let requests = 0; const model = await compatibleFixture(t, (_route, body) => { requests++; assertToolPairs(body.messages); if (requests === 1) { const run = f.store.core.run.get(f.session.runId); for (let index = 0; index < 160; index++) { const artifact = f.store.artifactStore.write(f.session, run, 'Recorded fixture snapshot ' + index, { kind: 'fixture-evidence' }); f.store.core.artifact.set(artifact.id, artifact); } f.store.save(f.session); return { body: completion([...files.map((file, index) => call('four-file-' + index, 'read_file', { path: file.path })), call('four-snapshot', 'inspect_session', {})]) }; } const set = setFrom(body.messages); for (const file of files) { const raw = body.messages.filter(message => message.role === 'tool').map(message => JSON.parse(message.content)).find(result => result.path === file.path); const view = set?.entries.find(entry => entry.observation.path === file.path); assert.equal(raw?.content ?? view?.content, file.content, 'Lost actual module after historical read: ' + file.path); } if (requests === 2) { const index = body.messages.filter(message => message.role === 'system').map(message => { try { return JSON.parse(message.content.split('\n').at(-1)).history_index; } catch { return null; } }).find(Boolean); assert.ok(index); return { body: completion([call('later-metadata-page', 'read_artifact', { artifact_id: index.artifact_id, limit: 16000 })]) }; } return { body: completion([], 'Used the previously observed modules after the large metadata result.') }; }); f.store.saveConfig({ mode: 'compatible', baseUrl: model.baseUrl, model: 'working-retention-http-fixture', contextLimit: 16000, maxOutputTokens: 2048, maxSteps: 6 }); const runtime = new Runtime(f.store, new Provider()); f.onCleanup(() => runtime.stopAll()); runtime.start(f.session.id, 'Read the four modules and preserve their bytes while inspecting historical metadata.'); await until(() => !runtime.isActive(f.session.id), 25000); assert.equal(f.session.status, 'completed', JSON.stringify(f.session.failure)); assert.equal(requests, 3); for (const file of files) assert.equal(fs.readFileSync(path.join(f.projectRoot, file.path), 'utf8'), file.content); const plans = [...f.store.core.context_plan.values()]; assert.equal(plans.length, 3); for (const plan of plans) verifyPlanEvidence(f.store, f.session, plan); });