confluence_web/agent/dev/boundary-checks.ts
Artur Mukhamadiev 38a8ca67f7 agent: pi runtime track handoff (contract revision 1)
Pinned pi SDK 0.85.1 bridge, Python supervisor, artifact exporter,
scripted backend peer, image checks and boundary checks under agent/**.
Review findings F1-F3 are recorded in docs/implementation/PI_AGENT_REVIEW.md.
2026-09-14 21:57:54 +03:00

53 lines
3.5 KiB
TypeScript

/** Host-side generated boundary frames; no runtime-private function calls. */
import { spawn } from 'node:child_process';
import { createInterface } from 'node:readline';
import assert from 'node:assert/strict';
import { dockerFlags } from './fake-backend.js';
const image = process.argv[2] || 'confluence-pi-agent:rev1';
const MiB = 1048576;
async function check(mode: string) {
const name = `pi-boundary-${process.pid}-${mode}`;
const child = spawn('docker', ['run', '--name', name, ...dockerFlags, image], { stdio: ['pipe', 'pipe', 'pipe'] });
const exited = new Promise<number | null>((resolve, reject) => { child.on('exit', resolve); child.on('error', reject); });
let seq = 0, complete = false, error: any, peerError: Error | undefined;
let stderr = '';
child.stderr.on('data', c => { stderr += c.toString().slice(0, Math.max(0, 2048 - stderr.length)); });
const send = (type: string, payload: any, reply_to?: string) => child.stdin.write(JSON.stringify({ v: 1, type, id: `b_${++seq}`, ...(reply_to ? { reply_to } : {}), payload }) + '\n');
let markdownBytes = 0;
const lines = createInterface({ input: child.stdout });
lines.on('line', line => {
try {
const f = JSON.parse(line);
if (f.type === 'model_request') {
assert.equal(Buffer.byteLength(f.payload.messages[0].content[0].text), 16 * MiB);
const result = { content: [{ type: 'text', text: '' }], stop_reason: 'stop', usage: { input_tokens: 1, output_tokens: 1 } };
const budget = 128 * MiB - Buffer.byteLength(JSON.stringify({ result, error: null }));
result.content[0].text = mode === 'response-exact' || mode === 'response-over' ? 'x'.repeat(budget + (mode === 'response-over' ? 1 : 0)) : 'Small answer.';
markdownBytes = Buffer.byteLength(result.content[0].text);
send('model_response', { result, error: null }, f.id);
} else if (f.type === 'collection_start') {
assert.equal(Buffer.byteLength(f.payload.markdown), markdownBytes);
send('collection_ready', {}, f.id);
} else if (f.type === 'complete') {
assert.equal(f.payload.accepted_transfer_count, 0); complete = true; child.stdin.end();
} else if (f.type === 'error') { error = f.payload; child.stdin.end(); }
else throw new Error('Unexpected boundary frame');
} catch (err) { peerError = err as Error; child.stdin.end(); }
});
const timer = setTimeout(() => { peerError = new Error('Boundary peer timeout'); child.stdin.end(); }, 125000);
// Control characters exercise sixfold JSON escaping at the decoded prompt limit.
send('start', { prompt: '\u0001'.repeat(16 * MiB + (mode === 'prompt-over' ? 1 : 0)), system_instruction: 'Dummy boundary test.', remaining_ms: 120000,
model: { id: 'scripted-boundary', context_window_tokens: 100000000, max_output_tokens: 1000000 } });
try {
const code = await exited;
if (peerError) throw peerError;
if (mode.endsWith('-over')) { assert(!complete); assert.notEqual(code, 0); assert(error, stderr || 'Runtime exited without a controlled boundary error'); }
else { assert(complete, stderr || JSON.stringify(error) || `Runtime exit ${code}; possible resource failure`); assert.equal(code, 0); }
console.log(`PASS image ${mode}; markdown_bytes=${markdownBytes}`);
} finally {
clearTimeout(timer); child.stdin.destroy();
await new Promise(resolve => { const cleanup = spawn('docker', ['rm', '-f', name], { stdio: 'ignore' }); cleanup.on('exit', resolve); });
}
}
for (const mode of ['prompt-exact', 'prompt-over', 'response-exact', 'response-over']) await check(mode);