Files
Agent-Workbench/tests/original-purge.test.mjs
2026-11-07 22:50:39 +08:00

64 lines
5.9 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import {spawnSync} from 'node:child_process';
import {fixture} from './helpers.mjs';
import {originalPurgeSource,ORIGINAL_PURGE_KIND} from '../src/core/engines/original-purge-source.mjs';
import {resolveOriginalPurgeSource,selectPurgeStrategy,assertPurgeModelInput} from '../src/core/engines/purge-strategy.mjs';
import {originalPurgeReadRoots,OriginalPurgeRuntime} from '../src/core/engines/original-purge-runtime.mjs';
import lock from '../src/core/engines/original-purge-lock.json' with {type:'json'};
import { replayFixtureBytes } from '../scripts/verify-replay-fixtures.mjs';
function source(f){const root=path.join(f.root,'original');fs.mkdirSync(root);
for(const input of lock.original){const target=path.join(root,input.relative);fs.mkdirSync(path.dirname(target),{recursive:true});
fs.writeFileSync(target,replayFixtureBytes('original-purge/upstream/'+input.relative));}
return root;
}
test('original source locks all 60 byte-identical files including modules outside the nine-file prompt experiment',t=>{
const f=fixture(t),root=source(f),before=originalPurgeSource(root);assert.equal(before.inputs.length,60);assert.equal(before.version,'1.1.78');
const file=before.inputs.find(input=>input.relative==='lib/host.js');assert.ok(file);
fs.appendFileSync(file.path,'\n// changed outside old prompt lock\n');assert.throws(()=>originalPurgeSource(root),/固定原版输入已变化/);
});
test('a new original executable or missing license cannot silently join the fixed trusted-source profile',t=>{
const f=fixture(t),root=source(f);fs.writeFileSync(path.join(root,'lib/unsealed.js'),'export const added=true;');
assert.throws(()=>originalPurgeSource(root),/文件集合已变化/);fs.unlinkSync(path.join(root,'lib/unsealed.js'));fs.unlinkSync(path.join(root,'NOTICE'));
assert.throws(()=>originalPurgeSource(root));
});
test('original mode has a separate source identity and does not accept retry or privilege fields',t=>{
const f=fixture(t),root=source(f),available=resolveOriginalPurgeSource(f.root,root,{controlledStatus:'unavailable'});
assert.equal(available.kind,ORIGINAL_PURGE_KIND);assert.equal(available.status,'unavailable');assert.equal(available.sourceSha256,undefined);
assert.match(available.message,/Harness/);
for(const field of ['automaticRetry','hooksDenyBypass','hostModification'])assert.throws(()=>selectPurgeStrategy({kind:ORIGINAL_PURGE_KIND,root,prompt:'owned task',[field]:true}),/不接受/);
});
test('actual original model input requires the complete postPrompt once in system context and cannot borrow a user or tool copy',()=>{
const strategy={kind:ORIGINAL_PURGE_KIND,originalConfig:{postPrompt:'[sealed original engineering prompt]'}};
const system=text=>({system:text,messages:[]});assertPurgeModelInput(system(strategy.originalConfig.postPrompt),strategy);
assert.throws(()=>assertPurgeModelInput(system(strategy.originalConfig.postPrompt.repeat(2)),strategy),/唯一完整/);
assert.throws(()=>assertPurgeModelInput({messages:[{role:'user',content:[{type:'text',text:strategy.originalConfig.postPrompt}]}]},strategy),/唯一完整/);
});
test('Windows directory permissions normalize overlapping grants without allowing an unrelated sibling',t=>{
const f=fixture(t),workspace=path.join(f.root,'workspace'),native=path.join(workspace,'native'),sibling=path.join(f.root,'unrelated');
fs.mkdirSync(native,{recursive:true});fs.mkdirSync(sibling);fs.writeFileSync(path.join(sibling,'sentinel.txt'),'protected');
const roots=originalPurgeReadRoots([native,workspace],[]);assert.deepEqual(roots,[fs.realpathSync.native(workspace)]);
const script='const fs=require("node:fs");fs.realpath.native(process.argv[1],(error,result)=>{if(error)throw error;try{fs.readFileSync(process.argv[2]);process.exitCode=2;}catch(error){if(error.code!=="ERR_ACCESS_DENIED")throw error;process.stdout.write("allowed-directory-and-denied-sibling");}});';
const result=spawnSync(process.execPath,['--permission',...roots.flatMap(root=>['--allow-fs-read='+root,'--allow-fs-read='+path.toNamespacedPath(root)]),'-e',script,workspace,path.join(sibling,'sentinel.txt')],{encoding:'utf8',windowsHide:true});
assert.equal(result.status,0,result.stderr);assert.equal(result.stdout,'allowed-directory-and-denied-sibling');
});
test('durability broker rejects foreign authentication, outside paths and wrong file identity before real fsync',async t=>{
const f=fixture(t),directory=path.join(f.root,'run');fs.mkdirSync(directory);const file=path.join(directory,'owned.jsonl');fs.writeFileSync(file,'owned\n');
// Exercise the public broker handler with an explicitly declared object;
// original-source loading is verified separately by the actual Harness driver.
const broker=Object.assign(Object.create(OriginalPurgeRuntime.prototype),{directory,report:{durabilityReceipts:[],durabilityRejections:[]}});
const {Readable}=await import('node:stream');
const run=async(value,authorization='Bearer owned')=>{
const request=Readable.from([Buffer.from(JSON.stringify(value))]);Object.assign(request,{method:'POST',url:'/fsync',headers:{'content-type':'application/json',authorization}});
const response={writeHead(status){this.status=status;},end(body){this.body=body;}};
await broker.handleDurability(request,response,'owned');return response;
};
const stat=fs.statSync(file,{bigint:true}),value={path:file,dev:String(stat.dev),ino:String(stat.ino),size:String(stat.size)};
assert.equal((await run(value,'Bearer foreign')).status,409);assert.equal((await run({...value,path:path.join(f.root,'outside')})).status,409);
assert.equal((await run({...value,ino:'0'})).status,409);assert.equal(broker.report.durabilityReceipts.length,0);
assert.equal((await run(value)).status,200);assert.equal(broker.report.durabilityReceipts.length,1);assert.equal(broker.report.durabilityReceipts[0].operation,'actual-parent-fsync');
});