Files
Agent-Workbench/src/core/engines/original-purge-source.mjs
T
2026-11-02 22:32:31 +08:00

65 lines
5.0 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import {createHash} from 'node:crypto';
import lock from './original-purge-lock.json' with {type:'json'};
import {engineCanonicalDigest as digest,engineError} from '../../shared/contracts/engines.mjs';
import {engineFileIdentity} from './deepseek-installation.mjs';
export const ORIGINAL_PURGE_KIND='purge-original-restricted-v1';
export const ORIGINAL_PURGE_LOCK_SHA256=digest(lock);
const hash=bytes=>createHash('sha256').update(bytes).digest('hex');
const changed=message=>engineError(message,409,'ENGINE_INPUT_CHANGED');
function directory(root){
if(typeof root!=='string'||!path.isAbsolute(root)||!fs.lstatSync(root).isDirectory()||fs.lstatSync(root).isSymbolicLink())throw changed('原版来源需要普通绝对目录。');
return fs.realpathSync.native(root);
}
function pinnedInputs(root,expected,{original=false}={}){
const inputs=expected.map(input=>{
const file=path.join(root,input.relative),identity=engineFileIdentity(file,8*1024*1024);
if(!identity.path.startsWith(root+path.sep)||identity.bytes!==input.bytes||identity.sha256!==input.sha256)throw changed('固定原版输入已变化:'+input.relative);
if(original){
for(let current=file;current!==root;current=path.dirname(current))if(fs.lstatSync(current).isSymbolicLink())throw changed('原版输入不能经过链接。');
}
return {...identity,relative:input.relative};
});
if(original){
const found=[];function walk(relative){const file=path.join(root,relative),stat=fs.lstatSync(file);
if(stat.isSymbolicLink())throw changed('原版 lib 不能包含链接。');
if(stat.isDirectory()){for(const name of fs.readdirSync(file))walk(path.join(relative,name));}
else if(stat.isFile())found.push(relative.replaceAll('\\','/'));else throw changed('原版 lib 包含非普通文件。');
if(found.length>128)throw changed('原版 lib 文件数量超出固定版本。');
}walk('lib');
if(digest(found.sort())!==digest(expected.filter(item=>item.relative.startsWith('lib/')).map(item=>item.relative).sort()))throw changed('原版 lib 文件集合已变化。');
}
return inputs;
}
export function originalPurgeSource(root){
root=directory(root);const inputs=pinnedInputs(root,lock.original,{original:true});
const source={name:'dsh-purge',version:lock.purgeVersion,dshTarget:lock.harnessVersion,license:'Apache-2.0',attribution:'YuJunZhiXue / dsh-purge',root,inputs,lockSha256:ORIGINAL_PURGE_LOCK_SHA256};
return {...source,sourceSha256:digest(source)};
}
export function originalPurgeRuntime(installation){
if(process.platform!=='win32'||process.arch!=='x64')throw changed('当前原版受限配置仅核验 Windows x64。');
if(installation?.controlledStatus!=='available'||installation.version!==lock.harnessVersion)throw changed('原版受限模式需要已检测的 Harness 0.2.1-rc.2。');
const root=directory(installation.root),inputs=pinnedInputs(root,lock.harness);
const platformLibraries=['kernel32.dll','advapi32.dll'].map(name=>({...engineFileIdentity(path.join(process.env.SystemRoot,'System32',name)),name}));
const runtime={root,inputs,node:installation.node,lockSha256:ORIGINAL_PURGE_LOCK_SHA256,platformLibraries,
nativeAddons:lock.nativeAddons.map(input=>({...input,path:path.join(root,input.relative)}))};
return {...runtime,sha256:digest(runtime)};
}
export function originalPurgeStrategy(source,runtime,prompt){
const postPrompt='[Agent Workbench original Purge '+source.sourceSha256+' / '+hash(Buffer.from(prompt))+']\n'+prompt+'\n[/Agent Workbench original Purge]';
const config={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};
const strategy={schemaVersion:1,kind:ORIGINAL_PURGE_KIND,enabled:true,source,runtime,prompt,promptSha256:hash(Buffer.from(prompt)),originalConfig:config,
implementation:'workbench-original-entry-restricted-v1',upstreamPluginLoaded:true,automaticRetry:false,hostModification:false,
isolation:{profile:'fixed-trusted-source-node-permissions-v1',osSandbox:false,pinnedNativeAddons:true,arbitraryNativeAddons:false,originalTools:false,credentials:false}};
return {...strategy,sha256:digest(strategy)};
}
export function assertOriginalPurgeStrategy(strategy){
const source=originalPurgeSource(strategy.source?.root),runtime=originalPurgeRuntime({controlledStatus:'available',version:lock.harnessVersion,...strategy.runtime});
const node=engineFileIdentity(strategy.runtime.node.path,256*1024*1024);
if(node.sha256!==strategy.runtime.node.sha256||node.bytes!==strategy.runtime.node.bytes||strategy.runtime.node.version!==process.versions.node
||digest(originalPurgeStrategy(source,runtime,strategy.prompt))!==digest(strategy))throw changed('原版策略、运行器或固定加载输入已变化,请保留原记录并封存新计划。');
}