Files
Agent-Workbench/docs/working-memory-u-evidence/drivers/working-memory-u-prevalidate.mjs.txt
T
2026-11-04 08:39:17 +08:00

44 lines
4.9 KiB
Plaintext

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;i<s.offset+s.content.length;i++)positions.add(i);}
}assert.equal(positions.size,file.content.length,file.path);
}
const marker='当前工程执行依据(每次请求重新核对,非操作授权):\n';const facts=JSON.parse(body.messages[0].content.split(marker)[1].split('\n')[0]);
assert.equal(facts.stage,'checks_need_repair');assert.equal(facts.has_applied_changes,false);assert.equal(facts.latest_check.current_source,true);
assert.equal(facts.latest_check.results[0].tests.failed,9);assert.equal(facts.latest_check.results[1].status,'passed');
return facts;
}
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'});}
verifyPlanEvidence(makeStore(core,a=>a.sealedPath?fs.readFileSync(path.join(root,a.sealedPath)):oldLoad(a)),original.session,plan);
assert.deepEqual(read(prefix+'request.json'),read(prefix+plan.requestArtifactId+'.json'));coverage(read(prefix+'request.json'));
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,realModelRequests:0}));