feat: pin controlled Harness profile and verify owned tool and model bridge

This commit is contained in:
Agent Workbench Dev
2026-10-31 04:25:44 +08:00
parent 60b63bc70f
commit a01c41811b
14 changed files with 1928 additions and 0 deletions
+182
View File
@@ -0,0 +1,182 @@
import fs from 'node:fs';
import path from 'node:path';
import http from 'node:http';
import { randomUUID, timingSafeEqual } from 'node:crypto';
import { DeepSeekAdapter } from './deepseek-adapter.mjs';
import { engineFileIdentity } from './deepseek-installation.mjs';
import { engineError } from '../../shared/contracts/engines.mjs';
import { isId } from '../../shared/contracts/execution.mjs';
import { CONTROLLED_BRIDGE_PLUGIN } from './controlled-plugin.mjs';
import { assertControlledInstallation, controlledDigest, controlledPatch, controlledToolCatalogue, CONTROLLED_ENGINE_SCOPE } from './controlled-installation.mjs';
const allowedEnvironment = /^(path|pathext|systemroot|systemdrive|windir|comspec|temp|tmp|userprofile|appdata|localappdata|programfiles(?:\(x86\))?|homedrive|homepath)$/i;
const at = () => new Date().toISOString();
const equalSecret = (left, right) => typeof left === 'string' && Buffer.byteLength(left) === Buffer.byteLength(right)
&& timingSafeEqual(Buffer.from(left), Buffer.from(right));
export class ControlledDeepSeekSession extends DeepSeekAdapter {
constructor(installation, options = {}) {
super(installation, options); this.maximumFrameBytes = options.maximumFrameBytes ?? 4 * 1024 * 1024;
this.pending = new Set(); this.closed = false;
}
async open({ directory, cwd, owner, sdkSessionId, tools, config, signal, onModel, onTool, onState = () => {}, onNotification = () => {} }) {
for (const field of ['sessionId', 'projectId', 'taskId', 'runId', 'jobId']) if (!isId(owner?.[field])) throw engineError('受控引擎所属身份无效。', 400);
if (!isId(sdkSessionId) || typeof onModel !== 'function' || typeof onTool !== 'function') throw engineError('受控引擎回调或会话无效。', 400);
signal?.throwIfAborted(); assertControlledInstallation(this.installation);
const catalogue = controlledToolCatalogue(tools), catalogueSha256 = controlledDigest(catalogue);
this.bridgeController = new AbortController();
const bridgeSignal = signal ? AbortSignal.any([signal, this.bridgeController.signal]) : this.bridgeController.signal;
this.owner = structuredClone(owner); this.sdkSessionId = sdkSessionId; this.signal = bridgeSignal; this.parentSignal = signal;
this.catalogueNames = new Set(catalogue.map(tool => tool.name)); this.callIds = new Set();
this.catalogueSha256 = catalogueSha256; this.config = structuredClone(config);
this.report = { schemaVersion: 1, scope: CONTROLLED_ENGINE_SCOPE, owner: this.owner, sdkSessionId,
installationSha256: this.installation.controlledSha256, startedAt: at(), responses: [], notifications: [], unownedNotifications: [],
rejectedServerRequests: [], bridgeRequests: [], cleanup: null, modelRequests: 0, toolRequests: 0 };
fs.mkdirSync(directory, { recursive: false });
const home = path.join(directory, 'home'), agents = path.join(directory, 'agents');
fs.mkdirSync(home); fs.mkdirSync(agents);
const nonce = randomUUID(), marker = path.join(directory, 'ready.json'), plugin = path.join(directory, 'bridge.mjs'), patch = path.join(directory, 'bridge.patch.json');
const authentication = 'Bearer ' + nonce;
this.guard = http.createServer((request, response) => {
const operation = this.handleBridge(request, response, { authentication, onModel, onTool, signal: bridgeSignal }).catch(error => {
if (!response.destroyed && !response.writableEnded) { response.writeHead(error.status ?? 409, { 'Content-Type': 'application/json', Connection: 'close' }); response.end(JSON.stringify({ error: error.message })); }
});
this.pending.add(operation); operation.finally(() => this.pending.delete(operation));
});
await new Promise((resolve, reject) => { this.guard.once('error', reject); this.guard.listen(0, '127.0.0.1', resolve); });
const pluginConfig = { owner: this.owner, sdkSessionId, tools: catalogue, catalogueSha256, nonce, marker,
llmModule: this.installation.llmModule, endpoint: 'http://127.0.0.1:' + this.guard.address().port,
maximumFrameBytes: this.maximumFrameBytes, model: config.model, maxOutputTokens: config.maxOutputTokens, contextLimit: config.contextLimit };
fs.writeFileSync(plugin, CONTROLLED_BRIDGE_PLUGIN, { flag: 'wx' }); fs.writeFileSync(patch, JSON.stringify(controlledPatch(plugin, pluginConfig)), { flag: 'wx' });
this.report.launchInputs = { plugin: engineFileIdentity(plugin), patch: engineFileIdentity(patch), cwd: fs.realpathSync.native(cwd),
disabledPlugins: this.installation.disabledPlugins, catalogue, catalogueSha256 };
const env = {};
for (const name of Object.keys(process.env)) if (allowedEnvironment.test(name)) env[name] = process.env[name];
Object.assign(env, { DSH_HOME: home, DSH_AGENTS_HOME: agents, DSH_TELEMETRY_DISABLED: 'true' });
signal?.throwIfAborted(); assertControlledInstallation(this.installation);
this.client = this.clientFactory({ executable: this.installation.node.path,
args: [this.installation.entry, '--profile', this.installation.profile, '--patch', patch], cwd, env },
{ timeoutMs: this.timeoutMs, maximumFrameBytes: this.maximumFrameBytes, cancellationMethod: null });
this.report.process = { pid: this.client.pid, connectionId: this.client.id, startedAt: this.client.startedAt, ownership: 'one-process-per-engine-run' };
onState('starting', this.report.process);
this.client.on('server-request', request => {
this.report.rejectedServerRequests.push({ id: request.id, method: request.method, parametersSha256: controlledDigest(request.params ?? {}) });
this.client.fail(engineError('原SDK反向请求未在受控契约中开放。'));
});
let notificationBytes = 0;
this.client.on('notification', value => {
notificationBytes += Buffer.byteLength(JSON.stringify(value));
if (notificationBytes > 16 * 1024 * 1024 || this.report.notifications.length + this.report.unownedNotifications.length >= 8192) {
this.client.fail(engineError('受控引擎事件超过接收范围。')); return;
}
if (!['session.event', 'session.status', 'subagent.started', 'subagent.finished'].includes(value.method)) {
this.client.fail(engineError('受控引擎返回未支持的通知。')); return;
}
if (value.params?.sessionId !== sdkSessionId) { this.report.unownedNotifications.push(value); return; }
this.report.notifications.push(value);
try { onNotification(value); } catch (error) { this.client.fail(error); }
});
this.report.readiness = await this.waitReady(marker, nonce, signal);
const ready = this.report.readiness;
if (controlledDigest(ready.owner) !== controlledDigest(this.owner) || ready.sdkSessionId !== sdkSessionId
|| ready.catalogueSha256 !== catalogueSha256 || controlledDigest(ready.catalogue) !== catalogueSha256
|| JSON.stringify(ready.providers) !== JSON.stringify(['agent-workbench'])) throw engineError('启动后工具、模型或Run目录不符合封存契约。');
this.verifyLaunch(); onState('initializing', { readiness: ready });
const initialized = await this.request('initialize', { cwd, provider: 'agent-workbench', model: config.model, maxTokens: config.maxOutputTokens });
if (controlledDigest(initialized.serverInfo) !== controlledDigest(this.installation.serverInfo)) throw engineError('受控SDK返回身份不符合固定发行。');
const working = await this.request('session/working-directory/get', { sessionId: sdkSessionId });
if (fs.realpathSync.native(working.cwd) !== fs.realpathSync.native(cwd)) throw engineError('原SDK工作目录不属于所封存的工作区。');
onState('ready', { serverInfo: initialized.serverInfo }); return this.report;
}
verifyLaunch() {
assertControlledInstallation(this.installation);
for (const input of [this.report.launchInputs.plugin, this.report.launchInputs.patch]) {
if (engineFileIdentity(input.path).sha256 !== input.sha256) throw engineError('所属叠加插件或启动输入发生变化。');
}
}
async handleBridge(request, response, { authentication, onModel, onTool, signal }) {
if (this.closed || signal?.aborted || request.method !== 'POST' || !['/model', '/tool'].includes(request.url)
|| request.headers.origin !== undefined || request.headers['content-type'] !== 'application/json'
|| !equalSecret(request.headers.authorization, authentication)) throw engineError('桥请求不属于当前受控Run。', 403);
const chunks = []; let size = 0;
for await (const chunk of request) { size += chunk.length; if (size > this.maximumFrameBytes) throw engineError('桥请求超过输入范围。', 413); chunks.push(chunk); }
const body = JSON.parse(Buffer.concat(chunks).toString('utf8'));
if (Object.keys(body).some(key => !['owner', 'sessionId', 'pid', 'data'].includes(key)) || body.pid !== this.client?.pid
|| body.sessionId !== this.sdkSessionId || controlledDigest(body.owner) !== controlledDigest(this.owner)) throw engineError('桥请求的进程或任务归属不一致。', 403);
signal?.throwIfAborted(); this.verifyLaunch();
const data = body.data;
if (!data || typeof data !== 'object' || Array.isArray(data)) throw engineError('桥调用正文必须是对象。', 400);
if (request.url === '/tool') {
if (Object.keys(data).some(key => !['callId', 'name', 'arguments'].includes(key))
|| typeof data.callId !== 'string' || !data.callId || data.callId.length > 160 || !this.catalogueNames.has(data.name)
|| !data.arguments || typeof data.arguments !== 'object' || Array.isArray(data.arguments)) throw engineError('工具调用不符合原目录和参数契约。', 400);
if (this.callIds.has(data.callId)) throw engineError('原引擎工具调用ID已接收,不会重复执行。');
this.callIds.add(data.callId);
} else if (Object.keys(data).some(key => !['messages', 'system', 'tools', 'toolHistory', 'model', 'maxTokens'].includes(key))
|| data.model !== this.config.model || !Array.isArray(data.messages) || !Array.isArray(data.tools)
|| controlledDigest(controlledToolCatalogue(data.tools)) !== this.catalogueSha256
|| !Number.isSafeInteger(data.maxTokens) || data.maxTokens < 1 || data.maxTokens > this.config.maxOutputTokens) {
throw engineError('原引擎模型请求与封存的目录、模型或输出预算不一致。', 400);
}
const trace = { id: randomUUID(), route: request.url, requestSha256: controlledDigest(body), requestBytes: size, data: body.data, status: 'received', startedAt: at() };
if (this.report.bridgeRequests.length >= 1024) throw engineError('桥请求次数超过接收范围。');
this.report.bridgeRequests.push(trace);
const callback = request.url === '/model' ? onModel : onTool;
if (request.url === '/model') this.report.modelRequests++; else this.report.toolRequests++;
try {
const result = await callback(body.data, trace, signal);
trace.resultSha256 = controlledDigest(result); trace.status = 'completed'; trace.finishedAt = at();
if (this.closed || signal?.aborted) throw engineError('所属引擎已停止,迟到原结果只保留记录。');
response.writeHead(200, { 'Content-Type': 'application/json', Connection: 'close' }); response.end(JSON.stringify(result));
} catch (error) { trace.status = 'failed'; trace.error = error.message; trace.finishedAt = at(); throw error; }
}
async request(method, params = {}) {
this.signal?.throwIfAborted(); this.verifyLaunch();
const result = await this.client.request(method, params, { signal: this.signal,
...(method === 'session/wait' ? { timeoutMs: this.config.maxRunDurationMs ?? 600000 } : {}) });
this.report.responses.push({ method, params, result, trace: structuredClone(this.client.trace.at(-1)) }); return result;
}
async prompt(content) {
if (typeof content !== 'string' || !content || Buffer.byteLength(content) > this.maximumFrameBytes / 2) throw engineError('受控引擎任务输入超出范围。', 400);
const receipt = await this.request('session/prompt', { sessionId: this.sdkSessionId, contentBlocks: [{ type: 'text', text: content }] });
if (typeof receipt.messageId !== 'string' || !receipt.messageId) throw engineError('SDK没有返回原入队收据。');
this.report.receipt = receipt; return receipt;
}
async wait() {
const result = await this.request('session/wait', { sessionId: this.sdkSessionId });
if (!result || Array.isArray(result) || Object.keys(result).length) throw engineError('SDK等待结果不符合原协议。');
const events = this.report.notifications.filter(value => value.method === 'session.event').map(value => value.params.event);
const receipt = events.findIndex(event => event?.type === 'agent/inbox/spliced'
&& event.data?.inserted?.some(value => value.id === this.report.receipt?.messageId));
if (receipt < 0) throw engineError('未观察到所属prompt收据,不能采纳引擎输出。');
const activity = events.slice(receipt), assistant = activity.findLast(event => event?.type === 'assistant/message');
if (!assistant || !Array.isArray(assistant.data?.message?.content)) throw engineError('所属活动没有完整assistant原消息。');
const ends = activity.filter(event => event?.type === 'turn/end');
this.report.activity = { receiptIndex: receipt, endEvents: ends };
this.report.finalText = assistant.data.message.content.filter(block => block.type === 'text').map(block => block.text).join('');
return this.report.finalText;
}
async close({ force = false } = {}) {
if (this.closePromise) return this.closePromise;
const interrupted = force || this.parentSignal?.aborted === true;
this.closed = true;
this.bridgeController?.abort(new Error('所属引擎正在关闭。'));
return this.closePromise = (async () => {
let failure = null;
if (this.client) {
if (!interrupted && !this.client.exited) {
try { await this.client.request('shutdown', {}, { timeoutMs: 1000 }); } catch (error) { failure = error; }
}
try { this.report.cleanup = await this.client.close({ force: interrupted || !!failure }); } catch (error) { failure ??= error; }
this.report.trace = this.client.trace; this.report.stderr = this.client.stderr;
}
if (this.guard) { this.guard.closeAllConnections(); await new Promise(resolve => this.guard.close(resolve)); }
await Promise.allSettled([...this.pending]);
if (!interrupted && this.report.launchInputs) { try { this.verifyLaunch(); } catch (error) { failure ??= error; } }
if (!interrupted && this.report.cleanup?.exit?.code !== 0) failure ??= engineError('所属引擎未正常退出。');
this.report.finishedAt = at(); this.report.status = failure || !this.report.cleanup?.rootExited ? 'unknown' : interrupted ? 'cancelled' : 'closed';
if (failure) this.report.error = failure.message;
return this.report;
})();
}
}