Something went wrong. Try again.
small gleam coding and (not yet) persistent agent daemon with a detachable cli forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149// Cursor merges and old-daemon negotiation run in the browser, outside daemon E2E.import assert from 'node:assert/strict';import { readFile } from 'node:fs/promises';import { stripTypeScriptTypes } from 'node:module';import test from 'node:test';
const source = stripTypeScriptTypes((await readFile(new URL('../src/lib/trace.ts', import.meta.url), 'utf8')).replace(/^import .*;$/gm, ''));let imports = 0;const iso = (ms) => new Date(ms).toISOString();const session = (id = 'root') => ({ id, name: id, created_at: iso(1000), activity_at: iso(1000), status: { phase: 'generating' } });const entry = (position, kind, extra = {}) => ({ id: `e${position}`, position, kind, created_at: iso(1000 + position), turn_id: 'turn', turn_type: 'user', content: [], ...extra });const row = (sequence, kind = 'turn') => ({ sequence, kind, started_at: iso(1000), ended_at: iso(1100), model: 'model', prompt_tokens: {}, completion_tokens: {}, cached_tokens: {} });
async function fixture({ old = false } = {}) { const calls = []; const histories = new Map([['root', [entry(1, 'user'), entry(2, 'tool_call', { tool: { tool_call_id: 'call', name: 'python' } })]]]); const requests = new Map([['root', [row(1)]]]); const children = new Map(); const content = new Map(); let fail = false; class ApiError extends Error { constructor(status) { super('unsupported'); this.status = status; } } const api = { ApiError, busy: (s) => s?.status?.phase === 'generating', jsonField: () => undefined, readContentBatch: async (_id, ids) => { calls.push(['content', ids]); return new Map(ids.map((id) => [id, content.get(id)])); }, listChildren: async (id) => { calls.push(['children', id]); return children.get(id) ?? []; }, listJobs: async (id) => { calls.push(['jobs', id]); return []; }, readHistory: async (id, options = {}) => { calls.push(['history', id, options]); if (fail) throw new Error('offline'); const all = histories.get(id) ?? []; const items = all.filter((e) => options.after === undefined || e.position > options.after); const limit = options.limit ?? 100; return { items: options.after === undefined ? items.slice(-limit) : items.slice(0, limit), newer: items.length > limit, older: false }; }, readRequests: async (id, after, limit, _signal, since) => { calls.push(['requests', id, after, since]); if (old && since !== undefined) throw new ApiError(400); const rows = (requests.get(id) ?? []).filter((r) => r.sequence > after); return { items: rows.slice(0, limit), next: rows.length > limit ? 'more' : null }; }, }; globalThis.__traceAPI = api; const prelude = 'const {ApiError,busy,jsonField,listChildren,listJobs,readHistory,readRequests,readContentBatch} = globalThis.__traceAPI;'; const mod = await import(`data:text/javascript;base64,${Buffer.from(prelude + source).toString('base64')}#${++imports}`); delete globalThis.__traceAPI; const cursor = new mod.TraceCursor(); const read = (from = 1, root = session()) => mod.readTrace(root, from, 1, true, undefined, undefined, cursor); return { calls, histories, requests, children, content, details: mod.details, cursor, read, fail: () => { fail = true; } };}
test('refresh reads after cursors, closes a pending tool, adds marks, and advances past background calls', async () => { const f = await fixture(); const first = await f.read(); assert.equal(first.lanes[0].steps.find((s) => s.kind === 'tool').open, true); f.histories.get('root').push(entry(3, 'tool_result', { tool: { tool_call_id: 'call' } }), entry(4, 'user', { turn_id: 'next' })); f.requests.get('root').push(row(2), row(3, 'background')); f.calls.length = 0; const next = await f.read(); assert.equal(next.lanes[0].steps.length, 3); assert.equal(next.lanes[0].steps.find((s) => s.kind === 'tool').result, 'e3'); assert.equal(next.marks.length, 1); assert.equal(f.calls.find((c) => c[0] === 'history')[2].after, 2); assert.equal(f.calls.find((c) => c[0] === 'requests')[2], 1); f.calls.length = 0; await f.read(); assert.equal(f.calls.find((c) => c[0] === 'requests')[2], 3);});
test('discovers new children and retains completed children without rereading their work', async () => { const f = await fixture(); await f.read(); const child = { ...session('child'), activity_at: iso(1100), status: { phase: 'idle' } }; f.children.set('root', [child]); f.histories.set('child', [entry(1, 'tool_call', { tool: { tool_call_id: 'c' } }), entry(2, 'tool_result', { tool: { tool_call_id: 'c' } })]); const next = await f.read(); assert.equal(next.lanes.length, 2); f.calls.length = 0; assert.equal((await f.read()).lanes.length, 2); assert.equal(f.calls.some((c) => c[1] === 'child'), false);});
test('negotiates an old daemon once across lanes and uses sequence reads thereafter', async () => { const f = await fixture({ old: true }); f.children.set('root', [session('child')]); await f.read(); await f.read(); assert.equal(f.calls.filter((c) => c[0] === 'requests' && c[3] !== undefined).length, 1);});
test('earlier turns and failed reads discard cursors', async () => { const f = await fixture(); await f.read(); f.calls.length = 0; await f.read(0); assert.equal(f.calls.find((c) => c[0] === 'history')[2].after, -1); f.fail(); await assert.rejects(f.read(0), /offline/); assert.equal(f.cursor.lanes.size, 0);});
test('a followed idle parent includes its still-working child on the first read and refresh', async () => { const f = await fixture(); f.children.set('root', [{ ...session('child'), activity_at: iso(1005) }]); f.histories.set('child', [entry(5, 'tool_call', { tool: { tool_call_id: 'c' } })]); f.requests.set('child', [{ ...row(2), started_at: iso(1004), ended_at: iso(1010) }]); const root = { ...session(), status: { phase: 'idle' } }; const first = await f.read(1, root); const next = await f.read(1, root); assert.equal(first.lanes[1].steps.length, 2); assert.equal(next.lanes[1].steps.length, 2); assert.equal(first.lanes[1].steps[0].id, next.lanes[1].steps[0].id);});
test('long tasks paginate once, then request only the tail', async () => { const f = await fixture(); f.histories.set('root', Array.from({ length: 401 }, (_, i) => entry(i + 1, i ? 'assistant' : 'user'))); f.requests.set('root', Array.from({ length: 401 }, (_, i) => row(i + 1))); const first = await f.read(); assert.equal(first.lanes[0].steps.length, 401); assert.equal(f.calls.filter((c) => c[0] === 'history').length, 3); assert.equal(f.calls.filter((c) => c[0] === 'requests').length, 3); f.calls.length = 0; const next = await f.read(); assert.equal(next.lanes[0].steps.length, 401); assert.equal(f.calls.filter((c) => c[0] === 'history').length, 1); assert.equal(f.calls.filter((c) => c[0] === 'requests').length, 1); assert.equal(f.calls.find((c) => c[0] === 'history')[2].after, 401); assert.equal(f.calls.find((c) => c[0] === 'requests')[2], 401);});
test('hydration keeps an earlier input when its result arrives and skips filled details', async () => { const f = await fixture(); const step = { id: 'tool', entry: 'call', name: 'python' }; const trace = { lanes: [{ id: 'root', steps: [step] }] }; f.content.set('call', { arguments: '{"code":"print(1)"}' }); const known = await f.details(trace); assert.equal(known.get('tool').input.line, 'print(1)'); step.traced = 'result'; f.content.set('result', { trace: '{"activities":[],"changes":[]}' }); const completed = await f.details(trace, undefined, known); assert.equal(completed.get('tool').input.line, 'print(1)'); assert.deepEqual(completed.get('tool').did, { activities: [], changes: [] }); f.calls.length = 0; assert.equal((await f.details(trace, undefined, completed)).size, 0); assert.equal(f.calls.length, 0);});