Files
Agent-Workbench/docs/task-agreement-1.25-evidence/source-bytes/tests/fixtures/controlled-worker.mjs
T

56 lines
3.9 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import { randomUUID } from 'node:crypto';
import { createEngineFixture } from '../../scripts/engine-fixtures.mjs';
import { CONTROLLED_CRITICAL_FILES, resolveControlledInstallation, controlledDigest } from '../../src/core/engines/controlled-installation.mjs';
import { EngineRunner } from '../../src/core/engines/engine-runner.mjs';
import { prepareWorkerFixture } from './workers-project.mjs';
// A protocol fixture, not the native Harness. Native acceptance is independent.
export class ControlledWorkerFixture {
constructor() { this.report = { notifications: [], bridgeRequests: [] }; this.received = []; }
async open(options) {
this.options = options; this.report.owner = options.owner; this.report.sdkSessionId = options.sdkSessionId;
options.onState('ready', {}); return this.report;
}
async prompt(content) { this.content = content; this.report.receipt = { messageId: randomUUID() }; return this.report.receipt; }
async wait() {
const { onModel, onTool, tools, config, signal } = this.options;
const messages = [{ role: 'user', content: [{ type: 'text', text: this.content }] }];
for (let step = 0; step < 100; step++) {
signal.throwIfAborted();
const input = { messages: [{ role: 'system', content: [{ type: 'text', text: 'Protocol fixture for the owned task.' + (this.options.strategy ? '\n\n' + this.options.strategy.section.text : '') }], source: { kind: 'system-prompt' } }, ...structuredClone(messages)], system: null,
tools: tools.map(value => value.function ?? value), toolHistory: null, model: config.model, maxTokens: config.maxOutputTokens };
const result = await onModel(input, { id: randomUUID() }, signal);
signal.throwIfAborted();
const message = result.message, content = [{ type: 'text', text: message.content }];
for (const call of message.tool_calls ?? []) content.push({ type: 'tool-call', id: call.id, name: call.function.name, arguments: call.function.arguments });
messages.push({ role: 'assistant', content });
if (!message.tool_calls?.length) return message.content;
for (const call of message.tool_calls) {
const input = { callId: call.id, name: call.function.name, arguments: JSON.parse(call.function.arguments) };
const result = await onTool(input, { id: randomUUID() }, signal);
this.received.push({ input, result, resultSha256: controlledDigest(result) });
messages.push({ role: 'tool', toolCallId: call.id, content: [{ type: 'text', text: JSON.stringify(result) }] });
}
}
throw new Error('Fixture exceeded its step bound.');
}
async close({ force } = {}) {
return Object.assign(this.report, { status: force ? 'cancelled' : 'closed', cleanup: { rootExited: true, treeConfirmed: true, remainingProcesses: [] } });
}
}
export function controlledWorker(f, { respond = null, sessionFactory = () => new ControlledWorkerFixture() } = {}) {
const context = prepareWorkerFixture(f, { respond });
const root = createEngineFixture(path.join(f.root, 'controlled-harness'));
for (const file of CONTROLLED_CRITICAL_FILES) {
fs.mkdirSync(path.dirname(path.join(root, file)), { recursive: true }); fs.writeFileSync(path.join(root, file), 'export const fixture = true;\n');
}
const installation = resolveControlledInstallation(f.root, root), native = [];
context.runtime.controlledEngines = new EngineRunner(f.store, context.provider, { directoryRoot: path.join(f.root, 'owned-engines'),
sessionFactory: () => { const value = sessionFactory(); native.push(value); return value; } });
const definition = { goal: '交付受控引擎的独立工程成果', engine: { id: 'deepseek-harness', root, controlledSha256: installation.controlledSha256 },
budget: { maxSteps: 30 }, children: [{ label: 'engine-1', goal: '实现独立文件并通过当前检查', paths: ['src/a.mjs'] }] };
return { ...context, installation, root, native, definition };
}