Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248// 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, batches = 0, shells = new Map(), shellReads = [], continues = [], nextId = 0; export function reset(items, content = {}) { page = { items, older: null }; fields = content; reads = []; batches = 0; shells = new Map(); shellReads = []; continues = []; nextId = 0; } 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 readContentBatch(session, ids) { batches++; const found = new Map(); for (const id of ids) { const url = '/sessions/' + session + '/history/' + id; reads.push(url); if (fields[url]) found.set(id, fields[url]); } return found; } export async function readRequests() { return { items: [], next: null }; } export async function sendContinue(session, id) { continues.push({ session, id }); } export function sendMessage() { throw new Error('unexpected send'); } export function uuidv7() { return 'input-' + (++nextId); }`);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('collapsed traces fold prose once work follows it, and keep the newest prose out while it streams', () => { const transcript = new Transcript('session', () => true); const steps = [message('user', 1, 1, 'hello'), message('assistant', 2, 2, 'looking'), message('thinking', 3, 3, 'hmm'), message('assistant', 4, 4, 'done')]; transcript.entries = steps; transcript.running = true; assert.deepEqual(transcript.groups.map((g) => g.kind), ['user', 'work', 'assistant']); assert.deepEqual(transcript.groups[1].steps.map((s) => s.text), ['looking', 'hmm']); transcript.entries = [...steps, message('thinking', 5, 5, 'more')]; assert.deepEqual(transcript.groups.map((g) => g.kind), ['user', 'work']); assert.deepEqual(transcript.groups[1].steps.map((s) => s.text), ['looking', 'hmm', 'done', 'more']); transcript.entries = steps; transcript.running = false; assert.deepEqual(transcript.groups.map((g) => g.kind), ['user', 'work', 'assistant', 'end']); assert.equal(transcript.groups[2].text, 'done');});
test('collapsed traces follow the daemon labels over position', () => { const transcript = new Transcript('session', () => true); const labelled = (position, text, phase) => ({ ...message('assistant', position, position, text), phase }); transcript.entries = [message('user', 1, 1, 'hello'), labelled(2, 'early answer', 'final'), message('thinking', 3, 3, 'hmm'), labelled(4, 'one more look', 'commentary')]; assert.deepEqual(transcript.groups.map((g) => g.kind), ['user', 'assistant', 'work', 'end']); assert.equal(transcript.groups[1].text, 'early answer'); assert.deepEqual(transcript.groups[2].steps.map((s) => s.text), ['hmm', 'one more look']);});
test('a turn signs off below its answer once the live text commits', () => { const transcript = new Transcript('session'); const ran = { ...entry('tool_result', 2, 3), tool: { tool_call_id: 'call', name: 'python' } }; transcript.entries = [message('user', 1, 1, 'hello'), ran]; transcript.live = [{ id: 'm', kind: 'text', text: 'the answer' }]; assert.deepEqual(transcript.blocks.map((b) => b.kind), ['user', 'tool', 'assistant']); transcript.entries = [...transcript.entries, message('assistant', 3, 5, 'the answer')]; transcript.live = []; assert.deepEqual(transcript.blocks.map((b) => b.kind), ['user', 'tool', 'assistant', 'end']); assert.equal(transcript.blocks.at(-1).elapsedMs, 4000);});
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 page's referenced messages load in one batch read", async () => { const long = (position) => ({ ...message('assistant', position, position, ''), content_complete: false, content: [{ kind: 'reference', reference: { url: `/sessions/session/history/${position}`, field: 'text', bytes: 70000 } }] }); api.reset([message('user', 1, 1, 'hello'), long(2), long(3)], { '/sessions/session/history/2': { text: 'first' }, '/sessions/session/history/3': { text: 'second' } }); const transcript = new Transcript('session'); await transcript.load(); assert.equal(api.batches, 1); assert.deepEqual(transcript.blocks.filter((b) => b.kind === 'assistant').map((b) => b.text), ['first', 'second']);});
test('rows that did not change keep their objects when a page arrives or text streams in', () => { const transcript = new Transcript('session'); transcript.entries = conversation(); transcript.running = true; const before = transcript.groups; // Equal entries as new objects, as a poll or an expansion hands over. transcript.entries = [...conversation(), message('user', 3, 3, 'more', 'model-two')]; transcript.live = [{ id: 'live-1', kind: 'text', text: 'stream' }]; const after = transcript.groups; assert.deepEqual(after.map((group) => group.kind), ['user', 'assistant', 'end', 'user', 'assistant']); before.forEach((group, index) => assert.equal(after[index], group));});
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(); }});
test('sending a lone dot submits a continue input without adding a user bubble or restoring on cancel', async () => { api.reset([]); const transcript = new Transcript('session'); transcript.entries = conversation(); await transcript.send('.'); assert.deepEqual(api.continues, [{ session: 'session', id: 'input-1' }]); assert.deepEqual(transcript.blocks.map((b) => b.kind), ['user', 'assistant', 'end']); assert.deepEqual(transcript.take(['input-1']), []); assert.equal(transcript.pending.length, 0);});