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 { readOwnedProcessTree } from '../owned-process-tree.mjs'; import { CONTROLLED_BRIDGE_PLUGIN } from './controlled-plugin.mjs'; import { assertControlledInstallation, controlledDigest, controlledPatch, controlledToolCatalogue, CONTROLLED_ENGINE_SCOPE } from './controlled-installation.mjs'; import { assertPurgeStrategy, PURGE_PROMPT_KIND, ORIGINAL_PURGE_KIND } from './purge-strategy.mjs'; import { OriginalPurgeRuntime } from './original-purge-runtime.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; this.processTreeReader = options.processTreeReader ?? readOwnedProcessTree; } async open({ directory, cwd, owner, sdkSessionId, tools, config, strategy, 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); assertPurgeStrategy(strategy); this.strategy = structuredClone(strategy); 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, ...(strategy?.kind===PURGE_PROMPT_KIND ? { promptExperiment: { sha256: strategy.sha256, section: strategy.section } } : {}) }; 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); let launch={executable:this.installation.node.path,args:[this.installation.entry,'--profile',this.installation.profile,'--patch',patch],cwd,env}; if(strategy?.kind===ORIGINAL_PURGE_KIND){ this.originalPurge=new OriginalPurgeRuntime(strategy,directory);this.report.originalPurge=this.originalPurge.report; launch=await this.originalPurge.prepare(launch,this.installation);this.report.launchInputs.patch=engineFileIdentity(patch); } this.client = this.clientFactory(launch, { 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.publishOwnership = () => onState('ownership', this.report.process); try { await this.captureTree(); } catch (error) { this.report.process.treeObservationError = error.message; throw error; } this.treeTimer = setInterval(() => { if (this.closed || this.treePending) return; this.treePending = this.captureTree().catch(error => { this.report.process.treeObservationError = error.message; this.client.fail(error); this.publishOwnership(); }).finally(() => { this.treePending = null; }); }, 1200); this.treeTimer.unref(); 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']) || (ready.promptExperimentSha256 ?? null) !== (strategy?.kind===PURGE_PROMPT_KIND?strategy.sha256:null)) throw engineError('启动后工具、模型、实验提示或Run目录不符合封存契约。'); this.originalPurge?.verify({ready:true,pid:this.client.pid}); 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); assertPurgeStrategy(this.strategy); for (const input of [this.report.launchInputs.plugin, this.report.launchInputs.patch]) { if (engineFileIdentity(input.path).sha256 !== input.sha256) throw engineError('所属叠加插件或启动输入发生变化。'); } this.originalPurge?.verify(); } async captureTree(publish = true) { const info = this.report.process, tree = await this.processTreeReader(info); if (tree.supported) { if (tree.root) info.rootCreatedAt ??= tree.root.createdAt; const known = new Map((info.descendants ?? []).map(value => [value.pid + ':' + value.createdAt, value])); for (const value of tree.processes) if (value.pid !== info.pid) known.set(value.pid + ':' + value.createdAt, value); info.descendants = [...known.values()].sort((left, right) => left.pid - right.pid); info.treeRemainingPids = tree.processes.map(value => value.pid); info.treeObservedAt = at(); } if (publish) this.publishOwnership?.(); return tree; } async handleBridge(request, response, { authentication, onModel, onTool, signal }) { if (this.closed || this.client?.exited === true || 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 }; if (ends.length !== 1 || ends[0].data?.reason?.kind !== 'completed') throw engineError('所属活动没有唯一的正常结束事件,原输出不能视为完成。'); 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; clearInterval(this.treeTimer); this.bridgeController?.abort(new Error('所属引擎正在关闭。')); return this.closePromise = (async () => { let failure = null; if (this.client) { await this.treePending; 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; } try { const tree = await this.captureTree(false); this.report.cleanup ??= { rootExited: false }; this.report.cleanup.treeConfirmed = tree.supported ? tree.processes.length === 0 && !!this.report.process.rootCreatedAt && !this.report.process.treeObservationError : this.report.cleanup.rootExited; this.report.cleanup.remainingProcesses = tree.processes; } catch (error) { failure ??= error; this.report.cleanup ??= { rootExited: false }; this.report.cleanup.treeConfirmed = false; } 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(this.originalPurge){try{this.originalPurge.verify();}catch(error){failure??=error;}finally{await this.originalPurge.close();}} 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 = this.client && (!this.report.cleanup?.rootExited || !this.report.cleanup?.treeConfirmed) ? 'unknown' : failure ? 'failed' : interrupted ? 'cancelled' : 'closed'; if (failure) this.report.error = failure.message; return this.report; })(); } }