import test from 'node:test'; import assert from 'node:assert/strict'; import { mkdtempSync, mkdirSync, writeFileSync, rmSync, readFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { startBroker } from '../src/runtime.mjs'; import { Client } from '../src/client.mjs'; import { startTimeOf } from '../../seat/src/proc.mjs'; test('REQ-PLUG-1 runtime reaches transcript only through loader, over the real bus socket', async t => { const root = mkdtempSync(join(tmpdir(), 'usage-runtime-')); let runtime; t.after(async () => { await runtime?.close(); rmSync(root, { recursive: true, force: true }); }); const dir = join(root, 'launches/demo/run1/session'); mkdirSync(dir, { recursive: true }); const business = { id: 'demo', human: 'jason', plugins: { 'usage-source': ['transcript'], harness: ['pi'] }, roles: { coder: { authority: { withinRole: [], crossRole: [] } } }, arbiters: { technical: 'coder', delivery: 'coder' } }; runtime = await startBroker({ dataRoot: root, businesses: { demo: business }, launches: [{ // Row 65: the launch names where its harness keeps the session files. business: 'demo', role: 'coder', run: 'run1', harness: 'pi', provider: 'other-provider', model: 'other-model', transcript: dir, pid: process.pid, startTime: startTimeOf(process.pid), }] }); const client = new Client({ path: runtime.path, cap: runtime.launches[0].cap }); await client.call('role.claim'); const human = runtime.broker.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true }); const { id } = runtime.broker.request(human, { verb: 'message.send', args: { to: 'coder', body: 'Work' } }); await client.call('message.receive'); assert.deepEqual(await client.call('usage.begin', { turn: id }), { recorded: true }); writeFileSync(join(dir, 'session.jsonl'), JSON.stringify({ type: 'message', id: 'new', message: { role: 'assistant', usage: { input: 2, output: 3, cacheWrite: 4, cacheRead: 5 }, } }) + '\n'); assert.deepEqual(await client.call('usage.end', { turn: id }), { recorded: true }); const view = await client.call('usage'); assert.deepEqual(view.total, { input: 2, output: 3, cacheWrite: 4, cacheRead: 5, unknownTurns: 0 }); assert.equal(view.byProvider[0].provider, 'other-provider'); const source = readFileSync(new URL('../src/usage.mjs', import.meta.url), 'utf8'); assert.match(source, /createLoader/); assert.match(source, /loader.modules\('usage-source'\)/); assert.doesNotMatch(source, /import[^\n]*usage-source\/transcript/); });