Files
Agent-Workbench/tests/engine-worker-http.test.mjs
T

45 lines
4.3 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { fixture, until } from './helpers.mjs';
import { workerHttp } from './fixtures/workers-http.mjs';
import { ControlledWorkerFixture } from './fixtures/controlled-worker.mjs';
import { createEngineFixture } from '../scripts/engine-fixtures.mjs';
import { EngineRunner } from '../src/core/engines/engine-runner.mjs';
import { resolveControlledInstallation, CONTROLLED_CRITICAL_FILES } from '../src/core/engines/controlled-installation.mjs';
test('controlled engine HTTP freezes one child, pages original owned input/report, rejects foreign reads and prevents changed-source reuse', async t => {
const f = fixture(t), host = await workerHttp(f), root = createEngineFixture(path.join(f.root, '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'); }
host.runtime.controlledEngines = new EngineRunner(f.store, host.provider, { directoryRoot: path.join(f.root, 'engines'), sessionFactory: () => new ControlledWorkerFixture() });
const installation = await host.api('/engines/deepseek/controlled-availability?root=' + encodeURIComponent(root));
assert.equal(installation.controlledStatus, 'available');
const input = { goal: 'HTTP controlled engine', engine: { id: 'deepseek-harness', root, controlledSha256: installation.controlledSha256 },
children: [{ label: 'engine-1', goal: 'Repair and check one child', paths: ['src/a.mjs'] }] };
const route = '/sessions/' + f.session.id, group = (await host.api(route + '/collaborations', input)).group;
assert.equal(group.children.length, 1); const action = operation => host.api(route + '/collaborations/' + group.id + '/' + operation, { fingerprint: group.fingerprint });
await action('prepare'); await until(() => f.store.core.collaboration.get(group.id).status === 'ready', 20000);
await action('start'); await until(() => f.store.core.collaboration.get(group.id).status === 'awaiting_review', 20000);
const childRoute = '/sessions/' + group.children[0].sessionId, view = await host.api(childRoute + '/engine-runs'), job = view[0];
assert.equal(job.status, 'completed'); assert.equal(job.nativeSummary.treeConfirmed, true); assert.equal(job.modelRequests, 2); assert.equal(job.toolRequests, 1);
const original = f.store.artifactStore.read(f.store.core.artifact.get(job.reportArtifactId));
const first = await host.api(childRoute + '/engine-runs/' + job.id + '/report?offset=0&limit=200');
assert.equal(first.content, original.slice(0, 200)); assert.equal(first.sha256, job.reportSha256); assert.equal(first.truncated, true);
const second = await host.api(childRoute + '/engine-runs/' + job.id + '/report?offset=200&limit=200'); assert.equal(second.content, original.slice(200, 400));
const raw = await fetch(host.url + '/api' + childRoute + '/engine-runs/' + job.id + '/download', { headers: { 'X-Workbench-Token': host.token } });
assert.equal(await raw.text(), original); assert.match(raw.headers.get('content-disposition'), /attachment/);
assert.equal((await host.api(childRoute + '/engine-runs/' + job.id + '/input?limit=200')).sha256, job.inputSha256);
assert.equal((await host.raw(childRoute + '/engine-runs/' + job.id + '/report?offset=999999')).status, 400);
assert.equal((await host.raw(childRoute + '/engine-runs', undefined, 'foreign')).status, 403);
assert.ok((await host.raw(route + '/engine-runs/' + job.id + '/report')).status >= 400);
assert.equal((await host.raw(childRoute + '/engines/jobs', { root })).status, 409);
assert.equal((await host.api(childRoute + '/execution')).engineRuns[0].id, job.id);
const artifact = f.store.core.artifact.get(job.reportArtifactId); fs.appendFileSync(path.join(f.store.artifactStore.root, artifact.relative), 'damage');
assert.ok((await host.raw(childRoute + '/engine-runs/' + job.id + '/report')).status >= 400);
const before = f.store.core.task.size; fs.appendFileSync(resolveControlledInstallation(f.root, root).controlledInputs[0].path, '// changed\n');
const another = f.store.createSession(f.project.id);
assert.equal((await host.raw('/sessions/' + another.id + '/collaborations', input)).status, 409);
assert.equal(f.store.core.task.size, before); assert.equal(host.requests.length, 2);
});