fix: preserve working sources ahead of historical metadata
This commit is contained in:
+121
-1
@@ -4,7 +4,7 @@ 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 } from '../src/core/context/working-set.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';
|
||||
@@ -185,3 +185,123 @@ test('actual HTTP file pagination advances over a long file with each returned p
|
||||
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('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 });
|
||||
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);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user