import fs from 'node:fs'; import path from 'node:path'; import assert from 'node:assert/strict'; import {randomUUID} from 'node:crypto'; import {ControlledDeepSeekSession} from '../src/core/engines/controlled-session.mjs'; import {resolveControlledInstallation} from '../src/core/engines/controlled-installation.mjs'; import {resolveOriginalPurgeSource,selectPurgeStrategy,assertPurgeModelInput,ORIGINAL_PURGE_KIND} from '../src/core/engines/purge-strategy.mjs'; // Real pinned Harness, production adapter, synthetic model responses. No Key, // configured provider, user project, clientFactory patch or upstream mutation. const directory=path.resolve('.qa','original-purge-runtime-'+randomUUID());fs.mkdirSync(directory,{recursive:true}); const installation=resolveControlledInstallation(process.cwd(),process.env.AGENT_WORKBENCH_DEEPSEEK_ROOT); assert.equal(installation.controlledStatus,'available',installation.message); const source=resolveOriginalPurgeSource(process.cwd(),process.env.AGENT_WORKBENCH_PURGE_178_ROOT,installation); assert.equal(source.status,'available',source.message); const strategy=selectPurgeStrategy({kind:ORIGINAL_PURGE_KIND,root:source.root,sourceSha256:source.sourceSha256,runtimeSha256:source.runtimeSha256, prompt:'Preserve the owned engineering task, host approvals, saved evidence and cumulative budget.'},process.cwd(),installation); const results=[];let active; try{ for(const scenario of ['completed','failed-no-retry','host-denied-tool']){ const cwd=path.join(directory,scenario);fs.mkdirSync(cwd);const driver=new ControlledDeepSeekSession(installation,{timeoutMs:30000});active=driver; const controller=new AbortController(),modelInputs=[];let tools=0,text=null,error=null; try{ await driver.open({directory:path.join(cwd,'native'),cwd,owner:Object.fromEntries(['sessionId','projectId','taskId','runId','jobId'].map(key=>[key,randomUUID()])), sdkSessionId:randomUUID(),strategy,signal:controller.signal,config:{model:'owned-original-purge-runtime',contextLimit:16000,maxOutputTokens:512,maxRunDurationMs:30000}, tools:[{name:'owned_probe',description:'A host-gated synthetic test tool.',parameters:{type:'object',properties:{},additionalProperties:false}}], onModel:async input=>{ modelInputs.push(input);assertPurgeModelInput(input,strategy); if(scenario==='failed-no-retry')throw new Error('Declared upstream fixture failure; no automatic retry.'); if(modelInputs.length>1)throw new Error('Host stops the next model round after the rejected tool result.'); return {message:{role:'assistant',content:scenario==='completed'?'Owned original entry completed.':'',...(scenario==='host-denied-tool'?{ tool_calls:[{id:'declared-owned-tool',type:'function',function:{name:'owned_probe',arguments:'{}'}}]}:{})},usage:{prompt_tokens:300,completion_tokens:50,total_tokens:350}}; },onTool:async()=>{tools++;throw new Error('External host denied the declared operation.');}}); await driver.prompt('Complete only this owned loading/protocol acceptance.');text=await driver.wait(); }catch(failure){error=failure.stack;} const report=await driver.close({force:!!error});active=null; const result={scenario,text,error,modelInputs,tools,report};results.push(result);fs.writeFileSync(path.join(cwd,'result.json'),JSON.stringify(result,null,2)); assert.equal(report.cleanup?.rootExited,true,JSON.stringify(report));assert.equal(report.cleanup?.treeConfirmed,true); assert.equal(report.originalPurge.readiness?.originalApplyCalled,true);assert.ok(report.originalPurge.loadedModules.length>330); if(scenario==='completed'){assert.equal(error,null);assert.equal(text,'Owned original entry completed.');assert.equal(report.status,'closed');assert.equal(report.cleanup.exit.code,0);assert.equal(modelInputs.length,1);assert.equal(tools,0);} assert.ok(report.originalPurge.durabilityReceipts.length>0); if(scenario==='failed-no-retry'){assert.notEqual(error,null);assert.equal(modelInputs.length,1);assert.equal(tools,0);} if(scenario==='host-denied-tool'){assert.notEqual(error,null);assert.equal(modelInputs.length,2);assert.equal(tools,1);} console.log('PASS '+scenario+'; model bridge '+modelInputs.length+'; host tools '+tools+'; process tree closed'); } const summary={passed:true,directory,productionAdapter:true,realModelRequests:0,sourceSha256:source.sourceSha256,runtimeSha256:source.runtimeSha256, cases:results.map(({scenario,modelInputs,tools,report})=>({scenario,bridgeCalls:modelInputs.length,hostTools:tools,status:report.status,exit:report.cleanup.exit, loadedModules:report.originalPurge.loadedModules.length,fsyncs:report.originalPurge.durabilityReceipts.length})), scope:'Pinned original entry through production ControlledDeepSeekSession. Synthetic model responses; no real autonomy or Purge benefit measured.'}; fs.writeFileSync(path.join(directory,'validation.json'),JSON.stringify(summary,null,2));console.log(JSON.stringify(summary)); }catch(error){console.error(JSON.stringify({passed:false,directory,error:error.stack,realModelRequests:0}));process.exitCode=1;} finally{if(active)await active.close({force:true});}