import fs from 'node:fs'; import path from 'node:path'; import assert from 'node:assert/strict'; import {createHash} from 'node:crypto'; import {execFileSync} from 'node:child_process'; import {verifyPlanEvidence} from '../src/core/context/request-plan.mjs'; import {assertToolPairs} from '../tests/helpers.mjs'; const root=path.resolve('.qa/working-memory-u');const sha=b=>createHash('sha256').update(b).digest('hex');const read=r=>JSON.parse(fs.readFileSync(r==='original-t-fixture.json'?'tests/fixtures/working-memory-t.json':path.join(root,r.replace(/^receipts\//,'')),'utf8')); const reference={path:'docs/live-delivery-t-evidence/manifest.json',sha256:sha(fs.readFileSync('docs/live-delivery-t-evidence/manifest.json'))},oldRoot=path.resolve('docs/live-delivery-t-evidence'); assert.equal(sha(fs.readFileSync(reference.path)),reference.sha256);const oldManifest=JSON.parse(fs.readFileSync(reference.path)); for(const e of oldManifest.files){const p=path.resolve(oldRoot,e.path);assert.ok(p.startsWith(oldRoot+path.sep));const b=fs.readFileSync(p);assert.equal(sha(b),e.sha256);assert.equal(b.length,e.bytes);} const oldRead=r=>JSON.parse(fs.readFileSync(path.join(oldRoot,'run',r),'utf8')); const original=oldRead('node/export.json'),state=oldRead('node/session.json').execution,oldArtifacts=oldRead('analysis/artifact-index.json').artifacts; function makeStore(core,load){return{core,owned(kind,id,sid){const v=core[kind]?.get(id);assert.ok(v,kind+':'+id);assert.equal(v.sessionId,sid);return v;},artifactStore:{read(a){const b=load(a);assert.equal(sha(b),a.sha256);return b.toString('utf8');}}};} const oldCore={artifact:new Map(oldArtifacts.map(a=>[a.id,a])),tool:new Map(oldRead('analysis/owned-tool-rows.json').map(r=>{const v=JSON.parse(r.payload);return[v.id,v];})),run:new Map([[state.run.id,state.run]]),task:new Map([[state.task.id,state.task]])}; const oldLoad=a=>fs.readFileSync(path.join(oldRoot,'run',a.capturedPath)); for(const p of state.contextPlans)verifyPlanEvidence(makeStore(oldCore,oldLoad),original.session,p); assert.equal(state.contextPlans.length,8);assert.equal(read('receipts/history-result.json').originalRequestsRewritten,0); const fixture=read('original-t-fixture.json'),files=fixture.files.filter(f=>['test/expense.test.mjs','src/amount.mjs','src/summary.mjs','src/report.mjs'].includes(f.path)); function coverage(body){ assertToolPairs(body.messages);assert.ok(body.messages[0].content.includes(JSON.stringify(fixture.goal))); for(const file of files){assert.equal(sha(file.content),file.sha256);const positions=new Set(); for(const m of body.messages){const spans=[]; if(m.role==='tool'){const v=JSON.parse(m.content);if(v.path===file.path&&typeof v.content==='string')spans.push({offset:v.offset??0,content:v.content});} if(m.role==='system'){let v;try{v=JSON.parse(m.content.split('\n').at(-1));}catch{}for(const e of v?.data?.entries??[])if(e.observation?.path===file.path)spans.push({offset:e.observation.offset??0,content:e.content});} for(const s of spans){assert.equal(s.content,file.content.slice(s.offset,s.offset+s.content.length));for(let i=s.offset;ij.id===facts.latest_check.id),plan=execution.plans.find(p=>p.id===facts.check_plan.id); assert.ok(job&&plan);assert.equal(job.planId,plan.id);assert.equal(job.status,facts.latest_check.status);assert.equal(plan.sourceFingerprint,facts.source_fingerprint); for(const f of facts.latest_check.results){const r=job.results.find(v=>v.name===f.name);assert.ok(r);assert.equal(r.status,f.status);assert.deepEqual(r.tests??null,f.tests);assert.equal(r.logArtifactId,f.log_artifact_id);const a=store.owned('artifact',f.log_artifact_id,job.sessionId);assert.equal(a.sha256,f.log_sha256);store.artifactStore.read(a);} } for(const number of [7,8]){ const prefix='wire-'+number+'-final/',plan=read(prefix+'record.json'),core={...oldCore,artifact:new Map(oldCore.artifact)}; const mapping=[['requestArtifactId','model-request'],['artifactId','context-plan'],['workingSetArtifactId','context-working-set'],['latestToolsArtifactId','context-latest-tools'],['historyIndexArtifactId','context-history-index']]; for(const [field,kind]of mapping){const id=plan[field];if(!id)continue;if(!fs.existsSync(path.join(root,prefix,id+'.json'))){assert.equal(core.artifact.get(id)?.kind,kind);continue;}const b=fs.readFileSync(path.join(root,prefix,id+'.json'));core.artifact.set(id,{id,kind,sha256:sha(b),sessionId:plan.sessionId,projectId:plan.projectId,taskId:plan.taskId,runId:plan.runId,sealedPath:prefix+id+'.json'});} const wireStore=makeStore(core,a=>a.sealedPath?fs.readFileSync(path.join(root,a.sealedPath)):oldLoad(a));verifyPlanEvidence(wireStore,original.session,plan); assert.deepEqual(read(prefix+'request.json'),read(prefix+plan.requestArtifactId+'.json'));verifyFacts(coverage(read(prefix+'request.json')),state,wireStore); assert.ok(plan.estimatedInputTokens<=12352);assert.equal(plan.budget.contextLimit,16000);assert.equal(plan.budget.outputReservation,2048);assert.equal(plan.budget.safetyTokens,1600); } console.log(JSON.stringify({success:true,historicalPlans:8,reconstructedPlans:2,currentFactsLogs:true,realModelRequests:0}));