Files
Agent-Workbench/scripts/engine-fixtures.mjs
T

71 lines
5.1 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
// A declared wire fixture. It does not load the third-party SDK or call a model.
export function createEngineFixture(directory, options = {}) {
fs.mkdirSync(directory, { recursive: true });
const version = '0.2.1-rc.2';
const manifests = [
['apps/cli/package.json', '@deepseek-ai/dsh'],
['packages/bundle/sdk-minimal/package.json', '@deepseek-ai/dsh-sdk-minimal'],
['packages/sdk/server/package.json', '@deepseek-ai/dsh-sdk-jsonrpc-server'],
['packages/sdk/protocol/package.json', '@deepseek-ai/dsh-sdk-protocol'],
['packages/sdk/client/package.json', '@deepseek-ai/dsh-sdk-client'],
];
const write = (relative, content) => { const filename = path.join(directory, relative); fs.mkdirSync(path.dirname(filename), { recursive: true }); fs.writeFileSync(filename, content); };
for (const [relative, name] of manifests) write(relative, JSON.stringify({ name, version, type: 'module', fixture: true }));
for (const relative of ['packages/boot/cmdline/lib/index.js', 'packages/bundle/sdk-app/lib/index.js', 'packages/sdk/server/lib/index.js', 'packages/sdk/protocol/lib/index.js']) write(relative, '// Owned protocol fixture; not the actual SDK.\n');
write('packages/bundle/sdk-minimal/cordis.patch.yml', '# Owned wire-fixture profile\n');
write('pnpm-lock.yaml', '# Owned deterministic fixture identity\n');
write('apps/cli/node_modules/@deepseek-ai/cordis/package.json', JSON.stringify({ name: '@deepseek-ai/cordis', type: 'module', exports: './index.mjs', fixture: true }));
write('apps/cli/node_modules/@deepseek-ai/cordis/index.mjs', '// Owned Cordis identity fixture; never imported.\n');
write('fixture-options.json', JSON.stringify(options));
write('apps/cli/lib/bin.js', `import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { createInterface } from 'node:readline';
import { randomUUID } from 'node:crypto';
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../..');
const options = JSON.parse(fs.readFileSync(path.join(root, 'fixture-options.json'), 'utf8'));
const patch = JSON.parse(fs.readFileSync(process.argv[process.argv.indexOf('--patch') + 1], 'utf8'));
const config = patch[0].insert[0].config;
const audit = value => fs.appendFileSync(path.join(root, 'wire.jsonl'), JSON.stringify(value) + '\\n');
const send = value => process.stdout.write(JSON.stringify(value) + '\\n');
audit({ type: 'launch', cwd: process.cwd(), nodeOptionsInherited: !!process.env.NODE_OPTIONS,
secretInherited: !!process.env.OWNED_ENGINE_TEST_SECRET, profile: process.argv[process.argv.indexOf('--profile') + 1] });
if (options.mode !== 'no-ready') setTimeout(() => {
fs.writeFileSync(config.marker, JSON.stringify({ schemaVersion: 1, nonce: options.mode === 'bad-ready' ? 'wrong' : config.nonce,
pid: process.pid, signal: 'appReady', readyAt: new Date().toISOString() }), { flag: 'wx' });
}, options.readyDelayMs ?? 0);
const reader = createInterface({ input: process.stdin });
reader.on('line', async line => {
const value = JSON.parse(line); audit({ type: 'wire', value });
if (!value.method) return;
const params = value.params ?? {};
let result = {};
if (value.method === 'initialize') {
if (!fs.existsSync(config.marker)) { send({ jsonrpc: '2.0', id: value.id, error: { code: -32603, message: 'initialize before appReady' } }); return; }
if (options.mode === 'hang-init') return;
if (options.mode === 'malformed') { process.stdout.write('{broken-json}\\n'); return; }
if (options.mode === 'reverse-request') send({ jsonrpc: '2.0', id: 'reverse-control', method: 'approve-command', params: { command: 'fixture-never-executed' } });
if (options.mode === 'unowned-event') send({ jsonrpc: '2.0', method: 'session.status', params: { sessionId: randomUUID(), status: 'idle' } });
if (options.mode === 'model-request') await fetch(process.env.DEEPSEEK_BASE_URL + '/chat/completions', { method: 'POST', body: '{}' });
result = { serverInfo: { name: 'deepseek-harness-sdk-runtime', version: options.mode === 'wrong-version' ? '9.9' : '0.0.1' } };
} else if (value.method === 'session/working-directory/get') {
if (options.mode === 'change-input') fs.appendFileSync(path.join(root, 'packages/boot/cmdline/lib/index.js'), '// changed\\n');
if (options.mode === 'change-workspace') fs.writeFileSync(path.join(process.cwd(), 'README.md'), 'fixture mutated');
result = { cwd: options.mode === 'wrong-cwd' ? root : process.cwd() };
} else if (['session/wait', 'session/resume', 'session/cancel'].includes(value.method)) {
send({ jsonrpc: '2.0', id: value.id, error: { code: -32603, message: value.method === 'session/cancel'
? 'unknown DeepSeek Harness SDK runtime method: session/cancel' : 'unknown SDK session: ' + params.sessionId } }); return;
} else if (value.method !== 'shutdown') {
send({ jsonrpc: '2.0', id: value.id, error: { code: -32601, message: 'unknown fixture method' } }); return;
}
send({ jsonrpc: '2.0', id: value.id, result });
if (value.method === 'shutdown') setImmediate(() => process.exit(0));
});
process.stdin.on('end', () => process.exit(0));
`);
return directory;
}