// A daemon E2E cannot see client-only grouping or whether the web client hydrates // referenced history. Compile the real Svelte state, stubbing only its transports: // shell outcomes must survive reload and stay outside the preceding model turn. import assert from 'node:assert/strict'; import { readFile } from 'node:fs/promises'; import { stripTypeScriptTypes } from 'node:module'; import test from 'node:test'; import { compileModule } from 'svelte/compiler'; const moduleURL = (code) => `data:text/javascript;base64,${Buffer.from(code).toString('base64')}`; const apiURL = moduleURL(` export let page, fields, reads, shells = new Map(), shellReads = []; export function reset(items, content = {}) { page = { items, older: null }; fields = content; reads = []; shells = new Map(); shellReads = []; } export async function getShellCommand(session, id, signal) { shellReads.push({ id, signal }); if (!shells.has(id)) throw new Error("missing test receipt"); return shells.get(id); } export async function readHistory() { return page; } export async function readEntryContent(url) { reads.push(url); return fields[url]; } export async function readRequests() { return { items: [], next: null }; } export function sendMessage() { throw new Error('unexpected send'); } export function uuidv7() { throw new Error('unexpected input'); } `); const api = await import(apiURL); const streamURL = moduleURL(` let receiver; export function follow(session, onBatch) { receiver = onBatch; return () => { receiver = null; }; } export function emit(batch) { receiver?.(batch); } `); const stream = await import(streamURL); const moodsURL = moduleURL(stripTypeScriptTypes(await readFile(new URL('../src/lib/moods.ts', import.meta.url), 'utf8'))); const imports = new Map([ ['./api', apiURL], ['./stream', streamURL], ['./moods', moodsURL], ]); const source = await readFile(new URL('../src/lib/transcript.svelte.ts', import.meta.url), 'utf8'); let code = compileModule(stripTypeScriptTypes(source, { mode: 'transform' }), { filename: 'transcript.svelte.js', generate: 'client' }).js.code; code = code.replace(/from (['"])(.*?)\1/g, (_, _quote, path) => `from ${JSON.stringify(imports.get(path) ?? import.meta.resolve(path))}`); const { Transcript } = await import(moduleURL(code)); function entry(kind, position, seconds, turn = 'model-one', content = []) { return { id: `entry-${position}`, kind, position, turn_id: turn, created_at: new Date(seconds * 1000).toISOString(), content, content_complete: true }; } function message(kind, position, seconds, text, turn = 'model-one') { return entry(kind, position, seconds, turn, [{ kind: 'text', text }]); } function shell(position, seconds, id = `shell-${position}`) { return entry('shell_command', position, seconds, null, [{ kind: 'json', field: 'shell_command', value: { id, command: 'echo hello', status: 'completed', exit_code: 0, output: 'hello\n', output_bytes: 6, truncated: false, error: null, history_seq: position, } }]); } const conversation = () => [message('user', 1, 1, 'hello'), message('assistant', 2, 2, 'ready')]; test('idle shell commands follow one model sign-off without extending its duration', () => { const transcript = new Transcript('session'); transcript.entries = [...conversation(), shell(3, 120), shell(4, 180), message('user', 5, 240, 'next', 'model-two'), message('assistant', 6, 241, 'again', 'model-two')]; assert.deepEqual(transcript.blocks.map((b) => b.kind), ['user', 'assistant', 'end', 'shell', 'shell', 'user', 'assistant', 'end']); const ends = transcript.blocks.filter((b) => b.kind === 'end'); assert.deepEqual(ends.map((b) => b.elapsedMs), [1000, 1000]); assert.equal(ends[0].text, 'ready'); }); test('a concurrent transient shell does not sign off an active model turn', () => { const transcript = new Transcript('session'); transcript.entries = conversation(); transcript.running = true; transcript.trackShell(shell(3, 120).content[0].value); assert.deepEqual(transcript.blocks.map((b) => b.kind), ['user', 'assistant', 'shell']); transcript.running = false; assert.deepEqual(transcript.blocks.map((b) => b.kind), ['user', 'assistant', 'end', 'shell']); }); test('referenced shell receipts hydrate on load without fetching unrelated tool output', async () => { const receipt = shell(3, 120); receipt.content[0].value.output = 'x'.repeat(64 * 1024); receipt.content[0].value.output_bytes = 128 * 1024; const url = '/sessions/session/history/3'; const toolURL = '/sessions/session/history/4'; const referenced = { ...receipt, content_complete: false, content: [{ kind: 'reference', reference: { url, field: 'shell_command', bytes: 65000 } }] }; const tool = { ...entry('tool_result', 4, 121, null), content_complete: false, content: [{ kind: 'reference', reference: { url: toolURL, field: 'result', bytes: 1000000 } }] }; api.reset([...conversation(), referenced, tool], { [url]: { shell_command: JSON.stringify(receipt.content[0].value) } }); const transcript = new Transcript('session'); await transcript.load(); assert.equal(transcript.error, null); assert.deepEqual(api.reads, [url]); assert.equal(transcript.blocks.find((b) => b.kind === 'shell')?.command.command, 'echo hello'); assert.equal(transcript.entries.find((e) => e.id === receipt.id).content_complete, true); }); test('a mid-turn committed receipt does not split the turn or duplicate its sign-off', () => { const transcript = new Transcript('session'); const receipt = { ...shell(3, 4), turn_id: 'model-one' }; transcript.entries = [...conversation(), receipt, message('assistant', 4, 9, 'used the shell result')]; assert.deepEqual(transcript.blocks.map((b) => b.kind), ['user', 'assistant', 'shell', 'assistant', 'end']); const [end] = transcript.blocks.filter((b) => b.kind === 'end'); assert.equal(end.elapsedMs, 8000); assert.equal(end.text, 'used the shell result'); }); test('delivery events overlay stale history without losing preview or accepting a late admission downgrade', () => { const transcript = new Transcript('session'); const receipt = shell(3, 4); const command = { ...receipt.content[0].value, delivery: 'pending', output: 'a richer history preview\n' }; receipt.content[0].value = command; transcript.entries = [...conversation(), receipt]; transcript.attach(); try { assert.equal(transcript.blocks.find((b) => b.kind === 'shell').command.delivery, 'pending'); stream.emit({ events: [{ type: 'shell_command', data: { shell_command: { ...command, delivery: 'delivered', output: 'a' } } }] }); transcript.trackShell({ ...command, status: 'running', exit_code: null, history_seq: null, delivery: 'pending', output: '' }); const blocks = transcript.blocks.filter((b) => b.kind === 'shell'); assert.equal(blocks.length, 1); assert.equal(blocks[0].command.delivery, 'delivered'); assert.equal(blocks[0].command.status, 'completed'); assert.equal(blocks[0].command.output, command.output); assert.equal(blocks[0].command.history_seq, 3); } finally { transcript.detach(); } }); test('reset refreshes an older pending history receipt whose ACK was missed offline', async () => { const receipt = shell(3, 4); receipt.content[0].value.delivery = 'pending'; api.reset([receipt]); api.shells.set('shell-3', { ...receipt.content[0].value, delivery: 'delivered' }); const transcript = new Transcript('session'); transcript.entries = [receipt]; transcript.attach(); try { stream.emit({ events: [{ type: 'reset', data: {} }], snapshot: { shell_commands: [] } }); await new Promise((resolve) => setImmediate(resolve)); assert.equal(transcript.blocks[0].command.delivery, 'delivered'); assert.equal(transcript.blocks[0].command.output, receipt.content[0].value.output); assert.deepEqual(api.shellReads.map((read) => read.id), ['shell-3']); } finally { transcript.detach(); } }); test('initial hydration finishing after reset still refreshes a pending receipt', async () => { const receipt = shell(3, 4); receipt.content[0].value.delivery = 'pending'; api.reset([receipt]); api.shells.set('shell-3', { ...receipt.content[0].value, delivery: 'delivered' }); const transcript = new Transcript('session'); const loading = transcript.load(); transcript.attach(); try { stream.emit({ events: [{ type: 'reset', data: {} }], snapshot: { shell_commands: [] } }); await loading; await new Promise((resolve) => setImmediate(resolve)); assert.equal(transcript.blocks[0].command.delivery, 'delivered'); assert.deepEqual(api.shellReads.map((read) => read.id), ['shell-3']); } finally { transcript.detach(); } }); test('reset restores delivery over older canonical metadata and legacy rows stay unknown', () => { const transcript = new Transcript('session'); const receipt = shell(3, 4); transcript.entries = [receipt]; assert.equal(transcript.blocks[0].command.delivery, 'unknown'); transcript.attach(); try { for (const delivery of ['pending', 'delivered']) { stream.emit({ events: [{ type: 'reset', data: {} }], snapshot: { shell_commands: [{ ...receipt.content[0].value, delivery }] } }); assert.equal(transcript.blocks[0].command.delivery, delivery); assert.equal(transcript.blocks.filter((b) => b.kind === 'shell').length, 1); } } finally { transcript.detach(); } });