Files
stack/packages/cli/tests/model-event-guard.test.mjs

49 lines
2.9 KiB
JavaScript

// Real confined runner and adapter, real local model stream; only fixture children are stopped.
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { spawnSync } from 'node:child_process';
import { join } from 'node:path';
import { sandboxProblem } from '../../seat/src/sandbox.mjs';
import { managedLaunchFixture, pause, waitFor } from './model-managed-launch-fixture.mjs';
const skip = sandboxProblem() ?? false;
const claude = spawnSync('sh', ['-c', 'command -v claude'], {encoding:'utf8'}).stdout.trim();
for (const harness of ['pi', 'claude-code']) {
for (const mode of ['stream', 'headers', 'keepalive']) {
test(`797 ${harness} ${mode}: real model stream controls the managed guard`, {skip: skip || (harness === 'claude-code' && !claude ? 'claude is not on PATH' : false), timeout:40000}, async t => {
const modelStream = { mode, seconds:6, duration:8500 };
const f = await managedLaunchFixture(t, { harness, real:[harness], modelStream,
hostEnv: harness === 'claude-code' ? {MOSAIC_CLAUDE_BIN:claude} : {} });
const launched = await f.launcher.launchPm();
const session = f.sessions().find(s => s.run === launched.run);
assert.ok(session?.live);
const sent = f.send('Respond to the test.');
try { await waitFor(() => f.state.modelRequests, 'model request reached the local server', 10000); }
catch (e) {
const file = join(session.runDir, 'turns', '0001.stderr');
let diagnostic = fs.existsSync(file) ? fs.readFileSync(file, 'utf8') : 'no adapter stderr';
for (const key of f.state.keys) diagnostic = diagnostic.replaceAll(key, '[fixture key]');
t.diagnostic(`adapter stderr: ${diagnostic}; launcher log: ${f.logs.join('; ')}`);
throw e;
}
if (mode === 'stream') {
await waitFor(() => fs.existsSync(join(session.runDir,'proxy-first-output')) || f.trace.stalls.length, 'model delta or stall observed', 10000);
assert.equal(f.trace.stalls.length, 0, 'streaming must not be stopped before its final output');
assert.equal(fs.existsSync(join(session.runDir,'proxy-first-output')), true, 'real model delta produces marker');
assert.equal(fs.readFileSync(join(session.runDir,'proxy-first-output'),'utf8'), 'output\n');
await pause(7000);
assert.equal(f.trace.stalls.length, 0, 'streaming past the window never requests a stop');
assert.equal(f.sessions().some(s => s.run === session.run && s.live), true);
const reply = await f.reply(sent);
assert.match(reply.body, /first/);
} else {
await waitFor(() => f.trace.stalls.length === 1, 'silent model triggers trusted stop', 12000);
assert.equal(fs.existsSync(join(session.runDir,'proxy-first-output')), false);
assert.equal(f.trace.stalls[0].stopped, true);
}
f.valueFree();
});
}
}