import fs from 'node:fs'; import path from 'node:path'; import assert from 'node:assert/strict'; import {createHash,randomUUID} from 'node:crypto'; import {fixture,compatibleFixture,launchWorkbench,until,call,completion} from '../tests/helpers.mjs'; import {createDeliveryProject,deliveryGit} from '../tests/fixtures/delivery-project.mjs'; if(process.argv.includes('--help')){console.log('node scripts/verify-delivery-progress.mjs [--responses] [--app-root ABSOLUTE_ROOT] — compiled host, owned HTTP fixture, real tests/review/continuation/Git; no configured-model requests.');process.exit(0);} const args=process.argv.slice(2),responses=args.includes('--responses'),rootIndex=args.indexOf('--app-root'),appRoot=rootIndex<0?process.cwd():path.resolve(args[rootIndex+1]); const directory=path.resolve('.qa','delivery-progress-'+randomUUID());fs.mkdirSync(directory,{recursive:true}); const cleanups=[],t={after:fn=>cleanups.push(fn)},f=fixture(t),snapshots=[],checks=[]; createDeliveryProject(f);const parent=deliveryGit(f,['rev-parse','HEAD']).trim(),protectedIndex=deliveryGit(f,['show',':owner-staged.txt']); const protectedFiles=['owner-staged.txt','owner-unstaged.txt','owner-untracked.txt'].map(file=>[file,fs.readFileSync(path.join(f.projectRoot,file))]); f.store.close();let requests=0,firstPhase=0,host,result; const marker='已应用修改后的当前交付依据(每次请求重新核对,非操作授权):\n'; try{ const upstream=await compatibleFixture(t,(route,body)=>{ const messages=body.messages??body.input,allText=messages.map(value=>typeof value.content==='string'?value.content:JSON.stringify(value.content??'')).join('\n'); const position=allText.indexOf(marker),progress=position<0?null:JSON.parse(allText.slice(position+marker.length).split('\n')[0]);requests++; let calls; if(progress){snapshots.push(progress);assert.ok(progress.files.every(file=>file.current));assert.ok(progress.next_action,'known facts must permit the next fixture action'); calls=[call('next-'+requests,progress.next_action.tool,progress.next_action.arguments)]; }else{ firstPhase++; if(firstPhase===1)calls=[call('old-plan','plan_checks',{names:['test','build']})]; else if(firstPhase===2){ const original=messages.filter(value=>value.role==='tool'||value.type==='function_call_output').map(value=>{try{return JSON.parse(value.content??value.output);}catch{return null;}}).findLast(value=>value?.id&&Array.isArray(value.checks)); assert.ok(original);calls=[call('old-check','run_checks',{plan_id:original.id})]; }else if(firstPhase===3)calls=[call('fix-add','propose_file',{path:'src/add.mjs',content:"import {sum} from './math.mjs';\nexport const add=sum;\n",reason:'Owned repair after original failing tests'}), call('fix-math','propose_file',{path:'src/math.mjs',content:'export const sum=(a,b)=>a+b;\n',reason:'Owned repair'})]; else if(firstPhase===4){ const ids=messages.filter(value=>value.role==='tool'||value.type==='function_call_output').map(value=>{try{return JSON.parse(value.content??value.output)?.change_id;}catch{return null;}}).filter(Boolean); assert.equal(ids.length,2);calls=[call('seal','prepare_change_set',{change_ids:ids})]; }else throw new Error('Fixture must not poll for state after the recorded application.'); } const response=completion(calls);response.model='owned-delivery-progress'; if(!responses)return {body:response}; return {body:{id:'owned-response-'+requests,model:response.model,status:'completed',output:calls.map(tool=>({type:'function_call',id:'item-'+tool.id,call_id:tool.id,name:tool.function.name,arguments:tool.function.arguments})), usage:{input_tokens:13,output_tokens:7,total_tokens:20}}}; }); host=await launchWorkbench(t,f.data,{}, {appRoot});assert.notEqual(new URL(host.url).port,'4318'); const connection=await host.api('/model-center/connections',{name:'Owned progress fixture',model:'owned-delivery-progress',baseUrl:upstream.baseUrl,requiresKey:false, protocol:responses?'responses':'chat-completions',contextLimit:32000,maxOutputTokens:512,modelIdentityPolicy:'require-match'}); await host.api('/model-center/select',{type:'connection',id:connection.id});const route='/sessions/'+f.session.id; const settle=async()=>{let state;await until(async()=>{state=await host.api(route);for(const approval of state.approvals.filter(value=>value.status==='pending'))await host.api(route+'/approvals/'+approval.id,{allow:true}); return !['running','awaiting_approval','stopping'].includes(state.status);},60000);return state;}; await host.api(route+'/messages',{content:'修复加法模块,通过原测试与构建,并准备审查后的本地Git交付。'}); let state=await settle();assert.equal(state.status,'needs_review',JSON.stringify(state.failure));assert.equal(requests,4); assert.equal(state.execution.checks[0].status,'failed');assert.equal(state.execution.checks[0].results[0].tests.failed,3); const taskId=state.taskId,batch=state.execution.changeSets.at(-1);assert.equal(batch.status,'prepared'); await host.api(route+'/change-sets/'+batch.id+'/apply',{fingerprint:batch.fingerprint}); await until(async()=>(await host.api(route)).execution.changeSets.at(-1).status==='completed',20000);assert.equal(requests,4); checks.push('original three failed tests, complete batch review and application with no automatic model continuation'); await host.api(route+'/continue-reviewed',{});state=await settle();assert.equal(state.status,'needs_review',JSON.stringify(state.failure)); assert.equal(state.taskId,taskId);assert.equal(state.execution.run.stopReason,'git_review');assert.equal(requests,7); assert.deepEqual(snapshots.map(value=>value.stage),['needs_current_check_plan','ready_for_checks','ready_for_git_preview']); const passed=state.execution.checks.at(-1);assert.equal(passed.status,'passed');assert.equal(passed.results[0].tests.passed,3); assert.equal(deliveryGit(f,['rev-parse','HEAD']).trim(),parent);checks.push('current applied SHA and stale checks drive new plan, approval, original tests/build and Git preview without state polling'); await host.api(route+'/delivery-jobs',{planId:state.execution.deliveryPlans.at(-1).id}); await until(async()=>{state=await host.api(route);return !!state.execution.deliveries.at(-1)?.reportArtifactId;},30000); const job=state.execution.deliveries.at(-1);assert.equal(job.status,'completed');assert.deepEqual(job.paths.slice().sort(),['src/add.mjs','src/math.mjs']); assert.equal(deliveryGit(f,['show',':owner-staged.txt']),protectedIndex);for(const [file,bytes]of protectedFiles)assert.deepEqual(fs.readFileSync(path.join(f.projectRoot,file)),bytes); const report=await host.api('/artifacts/'+job.reportArtifactId+'?session='+state.id+'&limit=64000');assert.equal(report.truncated,false); const raw=JSON.parse(report.content);assert.equal(raw.status,'verified_committed');assert.equal(raw.task.id,taskId); assert.ok(raw.runs.flatMap(run=>run.consumption).every(receipt=>receipt.modelIdentity.status==='matched')); checks.push('explicit Git confirmation publishes only reviewed files and preserves owner index/worktree with identity-bearing report'); result={success:true,version:host.bootstrap.version,protocol:responses?'responses':'chat-completions',requests,checks,snapshots,commit:job.commit, reportSha256:createHash('sha256').update(report.content).digest('hex'),realModelRequests:0}; }catch(error){result={success:false,error:error.stack,requests,checks,snapshots,realModelRequests:0};throw error;} finally{fs.writeFileSync(path.join(directory,'result.json'),JSON.stringify(result,null,2));console.log(JSON.stringify({success:result.success,directory,checks:checks.length,requests}));for(const action of cleanups.reverse())await action();}