Files
dxdesigner-part-replacement/tools/verify_dsh_files.mjs

107 lines
7.2 KiB
JavaScript

/** Exercise actual Harness file tools against owned folders and ZIP archives. */
import assert from 'node:assert/strict';
import {mkdir,mkdtemp,writeFile,readFile,readdir} from 'node:fs/promises';
import path from 'node:path';
import {createRequire} from 'node:module';
import {pathToFileURL} from 'node:url';
import {bootRuntime} from '../integration/dsh/start.mjs';
const require=createRequire(new URL('../integration/dsh/start.mjs',import.meta.url));
const {zipSync,strToU8}=require('fflate');
const output=path.resolve('build/dsh-files-verification');await mkdir(output,{recursive:true});
const root=await mkdtemp(path.join(output,'run-'));
for(const key of ['DSH_API_KEY','DSH_API_BASE','DSH_MODEL','DX_DSH_MODELS'])delete process.env[key];
process.env.DX_DSH_HOME=path.join(root,'home');process.env.DX_DSH_CONTEXT_FILE=path.join(root,'context.json');
await writeFile(process.env.DX_DSH_CONTEXT_FILE,'{"task_id":"files-fixture","revision":"files","status":"observe"}');
const source=path.join(root,'输入 文件夹');await mkdir(source);
await writeFile(path.join(source,'说明.txt'),'DSH_FILE_CONTENT_中文\n');
const archive=path.join(source,'资料.zip');
await writeFile(archive,zipSync({'子目录/说明.txt':strToU8('DSH_ZIP_CONTENT_中文'),'binary.bin':new Uint8Array([0,1,2,255])}));
const {ctx,shutdown}=await bootRuntime({announce:false});
try{
const workspace=await ctx.workspaceRegistry.create(process.env.DX_DSH_WORKSPACE,'File fixture');
const {sessionId}=await ctx.sessionController.create({workspaceId:workspace.id,agentPreset:'dx-task'});
const agent=ctx.agents.get(sessionId);
const names=ctx.tools.schemas(agent).map(x=>x.name);
for(const name of ['read','read_image','write','edit','glob','grep','working_directory','extract_zip'])
assert.ok(names.includes(name),'Missing file capability: '+name);
const call=async(name,args)=>ctx.tools.execute({name,arguments:args,callId:crypto.randomUUID(),agent,signal:AbortSignal.timeout(20000)});
const ok=async(name,args)=>{
const result=await call(name,args);assert.equal(result.isError,false,name+': '+JSON.stringify(result));return result;
};
assert.match(JSON.stringify(await ok('read',{file_path:path.join(source,'说明.txt')})),/DSH_FILE_CONTENT_中文/);
assert.match(JSON.stringify(await ok('glob',{pattern:'*',path:source})),/说明.txt/);
assert.match(JSON.stringify(await ok('grep',{pattern:'DSH_FILE_CONTENT',path:source})),/DSH_FILE_CONTENT/);
await ok('working_directory',{cd:source});
assert.match(JSON.stringify(await ok('read',{file_path:'说明.txt'})),/DSH_FILE_CONTENT/);
const extracted=await ok('extract_zip',{file_path:'资料.zip'});
const destination=extracted.value.directory;
assert.equal(await readFile(path.join(destination,'子目录/说明.txt'),'utf8'),'DSH_ZIP_CONTENT_中文');
assert.deepEqual(new Uint8Array(await readFile(path.join(destination,'binary.bin'))),new Uint8Array([0,1,2,255]));
assert.match(JSON.stringify(await ok('read',{file_path:path.join(destination,'子目录/说明.txt')})),/DSH_ZIP_CONTENT/);
assert.notEqual((await ok('extract_zip',{file_path:archive})).value.directory,destination,'repeated extraction must not overwrite');
await writeFile(path.join(source,'unsafe.zip'),zipSync({'../escaped.txt':strToU8('must not escape')}));
const before=await readdir(source);
assert.equal((await call('extract_zip',{file_path:'unsafe.zip'})).isError,true);
assert.deepEqual(await readdir(source),before,'unsafe archive must not publish a partial directory');
await writeFile(path.join(source,'broken.zip'),'not a zip');
assert.equal((await call('extract_zip',{file_path:'broken.zip'})).isError,true);
const corrupt=Buffer.from(zipSync({'evidence.txt':strToU8('ORIGINAL_CONTENT')},{level:0}));
const payload=corrupt.indexOf(Buffer.from('ORIGINAL_CONTENT'));assert.ok(payload>0);corrupt[payload]=88;
await writeFile(path.join(source,'corrupt.zip'),corrupt);
assert.equal((await call('extract_zip',{file_path:'corrupt.zip'})).isError,true,'CRC mismatch must not report successful extraction');
assert.equal(await readFile(path.join(source,'说明.txt'),'utf8'),'DSH_FILE_CONTENT_中文\n');
const {LlmAdapter}=await import(pathToFileURL(require.resolve('@deepseek-ai/dsh-llm')).href);
let resolveChecked,rejectChecked;
const checked=new Promise((resolve,reject)=>{resolveChecked=resolve;rejectChecked=reject;});
const callsById=new Map(),diagnosticResults=[];
let incidentSession;
ctx.on('session/event',(session,event)=>{
if(event.type==='tool/call')callsById.set(event.data.callId,event.data.name);
if(event.type!=='tool/result'||!callsById.has(event.data.message.toolCallId))return;
incidentSession=session.id;
diagnosticResults.push({name:callsById.get(event.data.message.toolCallId),...event.data.message});
if(diagnosticResults.length===3){
try{
assert.equal(diagnosticResults[0].isError,false);
assert.match(JSON.stringify(diagnosticResults[0].content),new RegExp('a'.repeat(24)));
for(const denied of diagnosticResults.slice(1)){
assert.equal(denied.isError,true);
assert.match(JSON.stringify(denied.content),/Automatic incident sessions/);
}
resolveChecked();
}catch(error){rejectChecked(error);}
}
});
let response=0;
class DiagnosticFixture extends LlmAdapter{
providerInfo(provider){return {id:provider,name:'Owned diagnostic fixture'};}
async listModels(provider){return [{provider,id:'fixture',name:'Fixture'}];}
async *stream(options){
const checks=[['dx_task_snapshot',{}],['read',{file_path:path.join(source,'说明.txt')}],['extract_zip',{file_path:archive}]];
const tool=options.purpose==='session-title'?null:checks[response++];
if(tool){
const block={type:'tool-call',id:crypto.randomUUID(),name:tool[0],arguments:JSON.stringify(tool[1])};
yield {type:'block-start',index:0,blockType:'tool-call'};
yield {type:'block-end',index:0,block};
yield {type:'finish',reason:{kind:'tool-calls'}};
return;
}
yield {type:'block-start',index:0,blockType:'text'};
yield {type:'block-end',index:0,block:{type:'text',text:'Controlled diagnosis. No engineering changes. {"action":"none"}'}};
yield {type:'finish',reason:{kind:'stop'}};
}
}
ctx.llm.registerAdapter(['files-fixture'],new DiagnosticFixture());
await ctx.agentDefaultModel.saveSelection({provider:'files-fixture',model:'fixture'});
ctx.emit('dx/diagnose-request',{request_id:'files-incident-probe',incident_id:'a'.repeat(24),phase:'diagnose',
snapshot:{task_id:'files-fixture'},evidence:'Owned fixture with a controlled adapter.'});
let timer;
try{await Promise.race([checked,new Promise((_,reject)=>{timer=setTimeout(()=>reject(Error('Diagnostic admission check timed out')),120000);})]);}
finally{clearTimeout(timer);}
await ctx.agents.get(incidentSession).whenIdle();
const report={status:'passed',realHarness:true,read:true,folderSearch:true,workingDirectory:true,
zipTextAndBinary:true,repeatedExtractionNoOverwrite:true,zipTraversalRejected:true,invalidZipRejected:true,corruptZipRejected:true,
automaticSessionIsolation:true};
await writeFile(path.join(root,'report.json'),JSON.stringify(report,null,2));console.log(JSON.stringify(report));
}finally{await shutdown.shutdown(0);}