55 lines
6.5 KiB
Plaintext
55 lines
6.5 KiB
Plaintext
// Rebuild bounded requests from frozen T inputs/owned records. Never dispatch.
|
|
import fs from 'node:fs';import path from 'node:path';import assert from 'node:assert/strict';import {createHash,randomUUID} from 'node:crypto';
|
|
import {RequestPlanner,historyGroups,verifyPlanEvidence} from '../src/core/context/request-plan.mjs';
|
|
import {taskDeliveryProgress} from '../src/core/context/delivery-progress.mjs';
|
|
import {CheckRunner} from '../src/core/check-runner.mjs';
|
|
import {compactRoutineSnapshot,sessionContext} from '../src/server/diagnostics.mjs';
|
|
const root=path.resolve('.qa/live-delivery-t'),out=path.resolve('.qa/working-memory-u');
|
|
const read=n=>JSON.parse(fs.readFileSync(path.join(root,n),'utf8')),sha=b=>createHash('sha256').update(b).digest('hex');
|
|
const original=read('node/export.json'),state=read('node/session.json').execution,rows=read('analysis/owned-tool-rows.json').map(r=>JSON.parse(r.payload));
|
|
const index=read('analysis/artifact-index.json').artifacts,results=[];
|
|
for(const number of [7,8]){
|
|
const folder=path.join(out,'wire-'+number+'-'+(process.argv[2]??'final'));assert.ok(!fs.existsSync(folder));fs.mkdirSync(folder);
|
|
const allGroups=historyGroups(original.session.providerMessages),session=structuredClone(original.session);
|
|
session.providerMessages=allGroups.slice(0,number).flatMap(g=>g.messages);
|
|
const calls=new Set(session.providerMessages.flatMap(m=>(m.tool_calls??[]).map(c=>c.id))),run=structuredClone(state.run),task=structuredClone(state.task);
|
|
run.consumption=run.consumption.slice(0,number-1);run.usage={requests:run.consumption.length,totalTokens:run.consumption.reduce((sum,c)=>sum+c.usage.total_tokens,0),estimatedUnknownTokens:0};task.usage=run.usage;
|
|
const originalPlan=state.contextPlans.find(p=>p.requestIndex===number),wire=JSON.parse(fs.readFileSync(path.join(root,index.find(a=>a.id===originalPlan.requestArtifactId).capturedPath),'utf8'));
|
|
const time=originalPlan.createdAt;session.events=session.events.filter(e=>(e.createdAt??e.startedAt??'')<=time);
|
|
const core={tool:new Map(rows.filter(r=>calls.has(r.callId)).map(r=>[r.id,r])),run:new Map([[run.id,run]]),task:new Map([[task.id,task]]),
|
|
artifact:new Map(index.map(a=>[a.id,a])),check_plan:new Map(state.plans.filter(p=>p.createdAt<=time).map(p=>[p.id,p])),
|
|
check_job:new Map(state.checks.filter(j=>j.finishedAt<=time).map(j=>[j.id,j])),context_plan:new Map(),step:new Map(),delivery_plan:new Map(),change_set:new Map()};
|
|
const project={id:session.projectId,name:'T frozen Node project',root:run.workspaceIdentity.root};
|
|
const store={core,project(id){assert.equal(id,project.id);return project;},owned(kind,id,sid){assert.equal(sid,session.id);const v=core[kind].get(id);assert.ok(v,kind+':'+id);assert.equal(v.sessionId,sid);return v;},
|
|
putRecord(kind,s,row){core[kind].set(row.id,row);},artifactStore:{
|
|
read(a){const b=fs.readFileSync(a.uPath??path.join(root,a.capturedPath));assert.equal(sha(b),a.sha256);return b.toString('utf8');},
|
|
write(s,r,text,options){const id=randomUUID(),uPath=path.join(folder,id+'.json');fs.writeFileSync(uPath,text);return{id,sha256:sha(text),bytes:Buffer.byteLength(text),kind:options.kind,taskId:r.taskId,runId:r.id,sessionId:s.id,projectId:s.projectId,uPath};}
|
|
}};
|
|
const facts=await taskDeliveryProgress(store,session,run,{checks:new CheckRunner(store)});
|
|
assert.equal(facts.stage,'checks_need_repair');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');
|
|
let system=wire.messages[0].content;const lines=system.split('\n'),snapshotIndex=lines.findIndex(l=>l.startsWith('{"source":"agent_workbench_runtime"'));
|
|
assert.ok(snapshotIndex>=0);const snapshot=compactRoutineSnapshot(JSON.parse(lines[snapshotIndex]));
|
|
const routine=sessionContext(session,project,{...run.model},String,{executionFactsIncluded:true});
|
|
const routineIntro=routine.split('\n').slice(0,-1);
|
|
const introStart=lines.findIndex(line=>line.startsWith('以下是工作台保存的当前执行状态。'));
|
|
assert.ok(introStart>=0);lines.splice(introStart,snapshotIndex-introStart+1,...routineIntro,JSON.stringify(snapshot));
|
|
system=lines.join('\n')+'\n当前工程执行依据(每次请求重新核对,非操作授权):\n'+JSON.stringify(facts);
|
|
const config={...run.model,modelRouting:run.model.routing};const planned=new RequestPlanner(store).prepare(session,run,config,{system,tools:wire.tools});
|
|
verifyPlanEvidence(store,session,planned.record);fs.writeFileSync(path.join(folder,'request.json'),JSON.stringify(planned.body,null,2));
|
|
fs.writeFileSync(path.join(folder,'record.json'),JSON.stringify(planned.record,null,2));fs.writeFileSync(path.join(folder,'facts.json'),JSON.stringify(facts,null,2));
|
|
const files=['test/expense.test.mjs','src/amount.mjs','src/summary.mjs','src/report.mjs'];const covered=[];
|
|
for(const name of files){const source=fs.readFileSync(path.join(root,'node/source-before',name),'utf8');const spans=[];
|
|
for(const m of planned.messages){if(m.role==='tool'){const v=JSON.parse(m.content);if(v.path===name&&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===name)spans.push({offset:e.observation.offset??0,content:e.content});}}
|
|
const positions=new Set();for(const span of spans){assert.equal(span.content,source.slice(span.offset,span.offset+span.content.length));for(let n=span.offset;n<span.offset+span.content.length;n++)positions.add(n);}
|
|
covered.push({path:name,characters:source.length,covered:positions.size,sha256:sha(source)});
|
|
}
|
|
const result={requestNumber:number,budget:planned.record.budget,estimatedInputTokens:planned.record.estimatedInputTokens,requestSha256:planned.record.requestSha256,
|
|
covered,facts,originalGoalPreserved:system.includes(JSON.stringify(task.goal)),provenanceVerified:true,externalModelRequests:0};
|
|
fs.writeFileSync(path.join(folder,'result.json'),JSON.stringify(result,null,2));results.push(result);
|
|
console.log(JSON.stringify({requestNumber:number,input:result.estimatedInputTokens,budget:result.budget.inputBudget,covered,checkStage:facts.stage,requests:0}));
|
|
assert.ok(result.originalGoalPreserved);assert.ok(covered.every(f=>f.characters===f.covered),'All original source must be in the actual bounded request');
|
|
}
|
|
fs.writeFileSync(path.join(out,'wire-result-'+(process.argv[2]??'final')+'.json'),JSON.stringify({passed:true,results,externalModelRequests:0},null,2),{flag:'wx'});
|