60 lines
3.4 KiB
JavaScript
60 lines
3.4 KiB
JavaScript
/** Test-only observer of actual session cwd and resolved sandbox policy. */
|
|
import {writeFile,rename,mkdtemp,rm} from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
import assert from 'node:assert/strict';
|
|
const normalized=value=>path.resolve(value).toLowerCase();
|
|
export function workspaceCandidates(state,previousIds,root){
|
|
const fresh=state.sessions.filter(session=>!previousIds.has(session.id));
|
|
if(fresh.length)return fresh;
|
|
return state.sessions.length===1&&normalized(state.sessions[0].cwd)===normalized(root)?state.sessions:[];
|
|
}
|
|
export function verifyWorkspaces(sessions,root){
|
|
assert.ok(sessions.length,'No actual active-session workspace proof');
|
|
for(const session of sessions){
|
|
assert.equal(normalized(session.cwd),normalized(root),'Active session cwd mismatch');
|
|
assert.equal(normalized(session.sandbox.workspaceRoot),normalized(root),'Resolved sandbox root mismatch');
|
|
assert.equal(session.sandbox.mode,'workspace-write');
|
|
}
|
|
}
|
|
export function observeRuntime(ctx){
|
|
const filename=process.env.DX_ACCEPTANCE_STATE_FILE;
|
|
if(!filename)return;
|
|
let pending=false;
|
|
const proofs=new Map();
|
|
const tick=async()=>{
|
|
if(pending)return;pending=true;
|
|
try{
|
|
const sessions=[];
|
|
for(const session of ctx.sessions.list()){
|
|
const cwd=ctx.workingDirectory.get(session),sandbox=ctx.sandboxPolicy.resolve({session});
|
|
if(!proofs.has(session.id)){
|
|
proofs.set(session.id,await verifyShell(ctx,cwd,sandbox).catch(error=>({passed:false,error:error.message})));
|
|
}
|
|
sessions.push({id:session.id,cwd,sandbox,shell_preflight:proofs.get(session.id)});
|
|
}
|
|
const value={at:new Date().toISOString(),sessions};
|
|
await writeFile(filename+'.tmp',JSON.stringify(value));await rename(filename+'.tmp',filename);
|
|
}finally{pending=false;}
|
|
};
|
|
const timer=setInterval(()=>void tick().catch(()=>{}),250);timer.unref();void tick();
|
|
}
|
|
|
|
/** No LLM call: exercise the same official executor with the actual session policy. */
|
|
export async function verifyShell(ctx,cwd,sandbox){
|
|
const python=process.env.DX_ACCEPTANCE_PYTHON;
|
|
assert.ok(python,'DX_ACCEPTANCE_PYTHON required for actual shell preflight');
|
|
assert.equal(sandbox.mode,'workspace-write');
|
|
const directory=await mkdtemp(path.join(cwd,'.acceptance-shell-'));
|
|
const quote=value=>"'"+value.replaceAll("'","''")+"'";
|
|
try{
|
|
const script=path.join(directory,'probe.py');
|
|
await writeFile(script,'import json\nprint(json.dumps({"shell_preflight":42}))\n');
|
|
const command=`$ErrorActionPreference='Stop'; & ${quote(python)} -c 'print(314159)'; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; & ${quote(python)} ${quote(script)}; if($LASTEXITCODE -ne 0){exit $LASTEXITCODE}; $items=@(Get-ChildItem -LiteralPath ${quote(directory)}); if($items.Name -notcontains 'probe.py'){throw 'Missing probe.py'}; Write-Output 'LIST_OK'`;
|
|
const process=await ctx.shell.execute(ctx.shell.resolve({command,workdir:cwd,sandboxPolicy:sandbox,timeoutMs:30000}));
|
|
const result=await process.result();
|
|
const proof={exit_code:result.exitCode,stdout:result.stdout.text,stderr:result.stderr.text,sandbox:result.sandbox,
|
|
command,passed:result.exitCode===0&&!result.timedOut&&!result.aborted&&result.stdout.text.includes('314159')&&result.stdout.text.includes('"shell_preflight": 42')&&result.stdout.text.includes('LIST_OK')};
|
|
return proof;
|
|
}finally{await rm(directory,{recursive:true,force:true});}
|
|
}
|