import fs from 'node:fs'; import path from 'node:path'; import assert from 'node:assert/strict'; import {createHash,randomUUID} from 'node:crypto'; import {pathToFileURL} from 'node:url'; import http from 'node:http'; import ts from 'typescript'; import {ControlledDeepSeekSession} from '../src/core/engines/controlled-session.mjs'; import {resolveControlledInstallation,assertControlledInstallation} from '../src/core/engines/controlled-installation.mjs'; import {engineFileIdentity} from '../src/core/engines/deepseek-installation.mjs'; import {McpStdio} from '../src/core/tools/mcp-stdio.mjs'; // A research driver, not a production strategy or a real-model benchmark. It // loads original, byte-identical modules in one owned Harness process with no // credentials. It never installs the upstream patch or runs postinstall. const directory=path.resolve('.qa','original-purge-load-'+randomUUID()),source=process.env.AGENT_WORKBENCH_PURGE_178_ROOT; assert.ok(source&&path.isAbsolute(source),'Set the existing original 1.1.78 source directory.'); const installation=resolveControlledInstallation(process.cwd(),process.env.AGENT_WORKBENCH_DEEPSEEK_ROOT); assert.equal(installation.controlledStatus,'available',installation.message); const sha=value=>createHash('sha256').update(value).digest('hex'),inputs=[]; fs.mkdirSync(directory);const ownSource=path.join(directory,'original-plugin');fs.mkdirSync(ownSource); function copy(relative){const file=path.join(source,relative),stat=fs.lstatSync(file);assert.equal(stat.isSymbolicLink(),false); if(stat.isDirectory()){fs.mkdirSync(path.join(ownSource,relative),{recursive:true});for(const entry of fs.readdirSync(file))copy(path.join(relative,entry));} else{assert.ok(stat.isFile());assert.ok(stat.size<4*1024*1024);const raw=fs.readFileSync(file);fs.mkdirSync(path.dirname(path.join(ownSource,relative)),{recursive:true}); fs.writeFileSync(path.join(ownSource,relative),raw,{flag:'wx'});inputs.push({relative:relative.replaceAll('\\','/'),bytes:raw.length,sha256:sha(raw)});}} for(const file of ['package.json','LICENSE','NOTICE','lib'])copy(file); const metadata=JSON.parse(fs.readFileSync(path.join(ownSource,'package.json')));assert.equal(metadata.version,'1.1.78');assert.equal(metadata.dshTarget,'0.2.1-rc.2'); const pending=['lib/index.js'],closure=new Set(),externals=new Set(); while(pending.length){const relative=pending.pop();if(closure.has(relative))continue;closure.add(relative); assert.ok(!relative.startsWith('../'));const file=path.join(ownSource,relative),tree=ts.createSourceFile(file,fs.readFileSync(file,'utf8'),ts.ScriptTarget.Latest,true,ts.ScriptKind.JS); function spec(value){if(value.startsWith('.'))pending.push(path.posix.normalize(path.posix.join(path.posix.dirname(relative),value)));else if(!value.startsWith('node:'))externals.add(value);} function visit(node){if((ts.isImportDeclaration(node)||ts.isExportDeclaration(node))&&node.moduleSpecifier)spec(node.moduleSpecifier.text); if(ts.isCallExpression(node)&&node.expression.kind===ts.SyntaxKind.ImportKeyword){assert.ok(ts.isStringLiteral(node.arguments[0]),'Unbounded dynamic import');spec(node.arguments[0].text);}ts.forEachChild(node,visit);}visit(tree);} assert.deepEqual([...externals],['@deepseek-ai/dsh-llm']); const config=Object.freeze({enabled:true,autoApplyOnStart:false,autoUpdateOnStart:false,autoRevertOnMissing:false,injectOnce:false, stripMnemon:true,verbose:false,autoRetry:false,retryMax:0,autoContinue:false,continueMax:0,capabilityProtocol:true,hooksDenyBypass:false, postPromptOrder:5100,postPrompt:'[Agent Workbench original Purge loading probe] Keep the owned task and external host permissions. [/Agent Workbench original Purge loading probe]'}); const wrapper=String.raw`import fs from 'node:fs'; import {registerHooks} from 'node:module'; import {fileURLToPath,pathToFileURL} from 'node:url'; import {createHash} from 'node:crypto'; import {execFileSync} from 'node:child_process'; export const name='workbench-original-purge-load-probe'; export const inject=['systemPrompt','tools','appReady']; export async function apply(ctx,config){ const loaded=[],blockedTools=[],rawInputs=new Map(config.inputs.map(input=>[input.url,input])); const hooks=registerHooks({resolve(specifier,context,next){ if(context.parentURL?.startsWith(config.sourceUrl)){ if(specifier==='@deepseek-ai/dsh-llm')return {url:config.llmModule,shortCircuit:true}; if(!specifier.startsWith('.')&&!specifier.startsWith('node:'))throw new Error('Unexpected original dependency: '+specifier); }return next(specifier,context);},load(url,context,next){ if(url.startsWith(config.sourceUrl)&&url.endsWith('.js')){ const input=rawInputs.get(url);if(!input)throw new Error('Original module is not sealed.'); const raw=fs.readFileSync(fileURLToPath(url));if(createHash('sha256').update(raw).digest('hex')!==input.sha256)throw new Error('Original module changed.'); loaded.push(url); }return next(url,context);}}); const toolProxy=new Proxy(ctx.tools,{get(target,key){if(key==='register')return definition=>{ if(!['purge_status','purge_apply','purge_revert'].includes(definition.name))throw new Error('Unexpected original tool registration.'); blockedTools.push(definition.name);return ()=>{};};const value=Reflect.get(target,key,target);return typeof value==='function'?value.bind(target):value;}}); const facade=new Proxy(ctx,{get(target,key){if(key==='tools')return toolProxy;if(key==='get')return name=>name==='tools'?toolProxy:target.get(name); const value=Reflect.get(target,key,target);return typeof value==='function'?value.bind(target):value;}}); const original=await import(config.entry);original.apply(facade,config.originalConfig); const denied={}; for(const action of ['read','write','child']){try{ if(action==='read')fs.readFileSync(config.protectedPath); else if(action==='write')fs.writeFileSync(config.protectedPath,'Unexpected write'); else execFileSync(process.execPath,['-e','process.stdout.write("owned probe")']); denied[action]=false; }catch(error){denied[action]=error.code==='ERR_ACCESS_DENIED';}} const marker={schemaVersion:1,pid:process.pid,loaded:[...new Set(loaded)],blockedTools,denied,originalConfig:config.originalConfig, originalEntryLoaded:true,originalApplyCalled:true,profile:'original-entry-with-external-tool-policy',upstreamPatchInstalled:false}; fs.writeFileSync(config.marker,JSON.stringify(marker)); ctx.effect(()=>()=>hooks.deregister()); } `; const wrapperFile=path.join(directory,'original-entry-wrapper.mjs');fs.writeFileSync(wrapperFile,wrapper); const accessLogFile=path.join(directory,'access-log.mjs'); fs.writeFileSync(accessLogFile,`import fs from 'node:fs';import path from 'node:path';import {syncBuiltinESMExports,registerHooks} from 'node:module';import {fileURLToPath} from 'node:url';import {createHash} from 'node:crypto'; const directoryMetadata=JSON.parse(fs.readFileSync(process.env.AWB_OWNED_DIRECTORY_METADATA,'utf8')); const moduleInputs=new Map(),digest=value=>createHash('sha256').update(value).digest('hex'); registerHooks({load(url,context,next){const result=next(url,context);if(url.startsWith('file:')){ const file=fileURLToPath(url),raw=fs.readFileSync(file),source=result.source==null?null:typeof result.source==='string'?Buffer.from(result.source):Buffer.from(result.source); const identity={url,path:file,format:result.format,bytes:raw.length,sha256:digest(raw),sourceSha256:source?digest(source):null}; const previous=moduleInputs.get(url);if(previous&&JSON.stringify(previous)!==JSON.stringify(identity))throw new Error('Owned loaded module changed.'); if(!previous){moduleInputs.set(url,identity);fs.appendFileSync(process.env.AWB_OWNED_MODULE_INPUTS,JSON.stringify(identity)+'\\n');} }return result;}}); const log=(name,args,error)=>{if(error.code==='ERR_ACCESS_DENIED')process.stderr.write('OWNED_ACCESS_DENIED '+name+' '+String(args[0])+'\\n'+(name.startsWith('promises.')?error.stack+'\\n':''));}; for(const name of ['existsSync','statSync','lstatSync','readFileSync','realpathSync','readdirSync','readFile','stat','lstat','realpath','readdir','access']){const original=fs[name];const wrapped=function(...args){try{return original.apply(this,args);}catch(error){log(name,args,error);throw error;}};Object.assign(wrapped,original);fs[name]=wrapped;} for(const name of ['readFile','stat','lstat','realpath','readdir','access']){const original=fs.promises[name];fs.promises[name]=async function(...args){try{return await original.apply(this,args);}catch(error){ if(error.code==='ERR_ACCESS_DENIED'&&name==='stat'&&!args[1]?.bigint&&directoryMetadata[path.resolve(String(args[0]))]){ const value=Object.assign(Object.create(fs.Stats.prototype),directoryMetadata[path.resolve(String(args[0]))]);for(const key of ['atime','mtime','ctime','birthtime'])value[key]=new Date(value[key]);return value; }log('promises.'+name,args,error);throw error;}};} const open=fs.promises.open;fs.promises.open=async function(file,...args){const handle=await open.call(this,file,...args),sync=handle.sync.bind(handle); handle.sync=async()=>{try{return await sync();}catch(error){if(error.code!=='ERR_ACCESS_DENIED')throw error; const stat=await handle.stat({bigint:true}),response=await fetch(process.env.AWB_OWNED_DURABILITY_ENDPOINT,{method:'POST',signal:AbortSignal.timeout(10000), headers:{'Content-Type':'application/json',Authorization:'Bearer '+process.env.AWB_OWNED_DURABILITY_NONCE}, body:JSON.stringify({path:String(file),dev:String(stat.dev),ino:String(stat.ino),size:String(stat.size)})}); if(!response.ok)throw new Error('Owned durability broker rejected: '+await response.text());await response.json(); }};return handle;};syncBuiltinESMExports();`); const protectedPath=path.join(directory,'outside-runtime-owned-sentinel.txt');fs.writeFileSync(protectedPath,'Owned sentinel; this is not user data or a key.\n'); const beforeSentinel=fs.readFileSync(protectedPath),modelInputs=[],results=[];let active,activeBroker; const inputUrls=inputs.map(input=>({...input,url:pathToFileURL(path.join(ownSource,input.relative)).href})); try{ for(const scenario of ['completed','failed-no-retry','blocked-purge-apply','host-denied-tool']){ const base=path.join(directory,scenario);fs.mkdirSync(base);const cwd=path.join(base,'workspace');fs.mkdirSync(cwd); const runDirectory=path.join(base,'native'),marker=path.join(runDirectory,'original-loaded.json'),owner=Object.fromEntries(['sessionId','projectId','taskId','runId','jobId'].map(key=>[key,randomUUID()])); const durabilityReceipts=[],brokerRejections=[],durabilityNonce=randomUUID(),broker=http.createServer(async(request,response)=>{ try{ assert.equal(request.method,'POST');assert.equal(request.url,'/fsync');assert.equal(request.headers.authorization,'Bearer '+durabilityNonce); const chunks=[];let size=0;for await(const chunk of request){size+=chunk.length;assert.ok(size<4096);chunks.push(chunk);} const value=JSON.parse(Buffer.concat(chunks)),file=path.resolve(value.path),real=fs.realpathSync.native(file); assert.ok(real.startsWith(fs.realpathSync.native(runDirectory)+path.sep));assert.equal(real,file);assert.equal(fs.lstatSync(file).isSymbolicLink(),false); const fd=fs.openSync(file,'r+');try{const stat=fs.fstatSync(fd,{bigint:true});assert.ok(stat.isFile()); for(const key of ['dev','ino','size'])assert.equal(String(stat[key]),value[key]);fs.fsyncSync(fd); durabilityReceipts.push({path:path.relative(runDirectory,file),dev:value.dev,ino:value.ino,size:value.size,operation:'actual-parent-fsync',sha256:sha(fs.readFileSync(fd))}); }finally{fs.closeSync(fd);}response.writeHead(200,{'Content-Type':'application/json'});response.end('{"flushed":true}'); }catch(error){response.writeHead(409);response.end(error.message);} });await new Promise(resolve=>broker.listen(0,'127.0.0.1',resolve));activeBroker=broker; let requests=0,toolCalls=0;const driver=new ControlledDeepSeekSession(installation,{timeoutMs:20000,clientFactory(launch,options){ const isolatedProfile=path.join(runDirectory,'home'); Object.assign(launch.env,{USERPROFILE:isolatedProfile,APPDATA:isolatedProfile,LOCALAPPDATA:isolatedProfile, AWB_OWNED_DURABILITY_ENDPOINT:'http://127.0.0.1:'+broker.address().port+'/fsync',AWB_OWNED_DURABILITY_NONCE:durabilityNonce}); // Windows JSONL durability checks stat every existing ancestor including // the drive root. Delegate only these real directory metadata snapshots; // granting root read would also grant unrelated file contents. const directoryMetadata={}; for(let ancestor=path.dirname(runDirectory);;ancestor=path.dirname(ancestor)){ assert.equal(fs.lstatSync(ancestor).isSymbolicLink(),false);const stats=fs.statSync(ancestor);assert.ok(stats.isDirectory()); directoryMetadata[ancestor]=stats;directoryMetadata[path.toNamespacedPath(ancestor)]=stats;if(path.dirname(ancestor)===ancestor)break; } const metadataFile=path.join(runDirectory,'ancestor-metadata.json');fs.writeFileSync(metadataFile,JSON.stringify(directoryMetadata)); launch.env.AWB_OWNED_DIRECTORY_METADATA=metadataFile; launch.env.AWB_OWNED_MODULE_INPUTS=path.join(runDirectory,'loaded-module-inputs.jsonl'); const patchFile=launch.args.at(-1),patch=JSON.parse(fs.readFileSync(patchFile)); patch.push({insert:[{id:'workbench-original-purge-probe',name:wrapperFile,config:{entry:pathToFileURL(path.join(ownSource,'lib/index.js')).href, sourceUrl:pathToFileURL(ownSource+path.sep).href,llmModule:installation.llmModule,inputs:inputUrls, originalConfig:config,protectedPath,marker}}]});fs.writeFileSync(patchFile,JSON.stringify(patch)); driver.report.launchInputs.patch=engineFileIdentity(patchFile);driver.report.launchInputs.originalSource=inputs; // Harness requires its native builtin loader. Node fs/child probes are // checked below; this profile is NOT an OS sandbox for arbitrary addons. // It is restricted to a byte-locked, keyless loading experiment. const readRoots=[installation.root,path.dirname(process.execPath),path.resolve(path.dirname(process.execPath),'..','lib','node','*'),runDirectory,cwd,ownSource,wrapperFile,accessLogFile]; for(const anchor of [installation.root,cwd,ownSource])for(let ancestor=path.dirname(anchor);;ancestor=path.dirname(ancestor)){ readRoots.push(path.join(ancestor,'node_modules','*'),path.join(ancestor,'package.json'));if(path.dirname(ancestor)===ancestor)break; } const args=['--permission','--allow-addons',...readRoots.flatMap(root=>['--allow-fs-read='+root,'--allow-fs-read='+path.toNamespacedPath(root)]), '--allow-fs-write='+runDirectory,'--allow-fs-write='+path.toNamespacedPath(runDirectory),'--import',pathToFileURL(accessLogFile).href,...launch.args]; return new McpStdio({...launch,args},options); }});active=driver; const start=Date.now();let error=null,text=null,ready; try{ await driver.open({directory:runDirectory,cwd,owner,sdkSessionId:randomUUID(),tools:[{type:'function',function:{name:'owned_probe',description:'An owned probe that must not be called.',parameters:{type:'object',properties:{},additionalProperties:false}}}],config:{model:'owned-original-purge-probe',maxOutputTokens:512,contextLimit:16000,maxRunDurationMs:30000}, onTool:async()=>{toolCalls++;throw new Error('No tool execution is permitted in this loading probe.');}, onModel:async input=>{requests++;modelInputs.push({scenario,input}); if(requests>1)throw new Error('Owned host stops after the first rejected tool result; no further model response is supplied.'); if(scenario==='failed-no-retry')throw new Error('Owned failure; no upstream model contacted.'); if(scenario==='blocked-purge-apply'||scenario==='host-denied-tool')return {message:{role:'assistant',content:'',tool_calls:[{id:'owned-rejected-call',type:'function', function:{name:scenario==='blocked-purge-apply'?'purge_apply':'owned_probe',arguments:'{}'}}]},usage:{prompt_tokens:123,completion_tokens:7,total_tokens:130},finishReason:'tool_calls'}; return {message:{role:'assistant',content:'Owned loading probe complete.'},usage:{prompt_tokens:123,completion_tokens:7,total_tokens:130},finishReason:'stop'};}}); ready=JSON.parse(fs.readFileSync(marker));assert.equal(ready.originalApplyCalled,true);assert.equal(ready.pid,driver.client.pid); assert.deepEqual(ready.blockedTools.sort(),['purge_apply','purge_revert','purge_status']);assert.ok(Object.values(ready.denied).every(Boolean)); assert.equal(ready.loaded.length,closure.size); if(scenario==='completed'){ const before=durabilityReceipts.length,stat=fs.statSync(marker,{bigint:true}); for(const [name,authorization,value] of [ ['unowned-auth','Bearer owned-invalid-auth',{path:marker}], ['outside-runtime','Bearer '+durabilityNonce,{path:protectedPath}], ['wrong-file-identity','Bearer '+durabilityNonce,{path:marker,dev:String(stat.dev),ino:'0',size:String(stat.size)}], ]){ const response=await fetch('http://127.0.0.1:'+broker.address().port+'/fsync',{method:'POST',headers:{Authorization:authorization,'Content-Type':'application/json'},body:JSON.stringify(value)}); await response.text();assert.equal(response.status,409);brokerRejections.push({name,status:response.status}); } assert.equal(durabilityReceipts.length,before);assert.deepEqual(fs.readFileSync(protectedPath),beforeSentinel); } await driver.prompt('Return the owned loading probe result. Do not call tools.');text=await driver.wait(); }catch(failure){error=failure.stack;} const processReport=await driver.close({force:!!error}).catch(failure=>({cleanupError:failure.stack,cleanup:null}));active=null;assertControlledInstallation(installation); broker.closeAllConnections();await new Promise(resolve=>broker.close(resolve));activeBroker=null; const moduleInputsFile=path.join(runDirectory,'loaded-module-inputs.jsonl'),moduleInputs=fs.existsSync(moduleInputsFile)?fs.readFileSync(moduleInputsFile,'utf8').trim().split(/\r?\n/).filter(Boolean).map(line=>JSON.parse(line)):[]; for(const input of moduleInputs)assert.equal(sha(fs.readFileSync(input.path)),input.sha256); const item={scenario,requests,toolCalls,text,error,ready,processReport,durabilityReceipts,brokerRejections,moduleInputs,elapsedMs:Date.now()-start};results.push(item); fs.writeFileSync(path.join(base,'result.json'),JSON.stringify(item,null,2)); assert.equal(requests,['blocked-purge-apply','host-denied-tool'].includes(scenario)?2:1,JSON.stringify({scenario,error,ready})); if(scenario==='completed'){assert.equal(error,null);assert.equal(text,'Owned loading probe complete.');}else assert.ok(error); assert.equal(toolCalls,scenario==='host-denied-tool'?1:0); assert.equal(processReport.cleanup.rootExited,true);assert.equal(processReport.cleanup.treeConfirmed,true); if(scenario==='completed'){assert.equal(processReport.status,'closed',processReport.error);assert.equal(processReport.cleanup.exit.code,0);} } assert.deepEqual(fs.readFileSync(protectedPath),beforeSentinel); for(const input of inputs){assert.equal(sha(fs.readFileSync(path.join(source,input.relative))),input.sha256);assert.equal(sha(fs.readFileSync(path.join(ownSource,input.relative))),input.sha256);} for(const {input} of modelInputs){const text=[input.system??'',...input.messages.filter(message=>message.role==='system').flatMap(message=>message.content.filter(block=>block.type==='text').map(block=>block.text))].join('\n'); assert.equal(text.split(config.postPrompt).length-1,1);} fs.writeFileSync(path.join(directory,'result.json'),JSON.stringify({passed:true,results,inputs,closure:[...closure].sort(),externals:[...externals],installation,config, realModelRequests:0,upstreamPatchInstalled:false,productionStrategyImplemented:false,nativeAddonIsolation:'not_an_os_sandbox', scope:'Original entry/load/config/permission/retry feasibility only; external tool registration restricted, original host-modifying patch omitted. No real model, autonomy or Purge benefit measured.'},null,2)); console.log(JSON.stringify({passed:true,directory,scenarios:results.length,originalModules:closure.size,realModelRequests:0})); }catch(error){fs.writeFileSync(path.join(directory,'result.json'),JSON.stringify({passed:false,error:error.stack,results,inputs,installation,config,realModelRequests:0},null,2)); console.log(JSON.stringify({passed:false,directory,error:error.stack,realModelRequests:0}));process.exitCode=1; }finally{await active?.close({force:true}).catch(()=>{});if(activeBroker){activeBroker.closeAllConnections();await new Promise(resolve=>activeBroker.close(resolve));}fs.writeFileSync(path.join(directory,'model-inputs.json'),JSON.stringify(modelInputs,null,2));}