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.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465// Shell history receipts are human messages; storage-only output must not inflate the context graph.import assert from 'node:assert/strict';import { readFile } from 'node:fs/promises';import { stripTypeScriptTypes } from 'node:module';import test from 'node:test';
const source = await readFile(new URL('../src/lib/context.ts', import.meta.url), 'utf8');const code = stripTypeScriptTypes(source, { mode: 'transform' }).replace(/import type .*?;\n/g, '');const moduleURL = `data:text/javascript;base64,${Buffer.from(code).toString('base64')}`;const { computeSegments } = await import(moduleURL);let historyId = 0;const receipt = (output, id = `history-${++historyId}`, command = 'cat file') => ({ kind: 'shell_command', id, content: [{ kind: 'json', field: 'shell_command', value: { id: 'shell-1', command, status: 'completed', exit_code: 0, output, output_bytes: output.length * 4, truncated: false, error: null, },}] });const tool = (text = 'tool result') => ({ kind: 'tool_result', id: `tool-${++historyId}`, content: [{ kind: 'text', text }] });const segments = (entries, total = 1_000_000_000) => computeSegments(total, null, entries);const cats = (items) => items.map(({ key, tokens }) => [key, tokens]);const tokens = (items, key) => items.find((part) => part.key === key)?.tokens ?? 0;
test('shell command previews count as messages alongside real tool output', () => { const result = segments([receipt('short'), tool('tool output '.repeat(1000))]); assert.ok(tokens(result, 'msg') > 0); assert.ok(tokens(result, 'tool') > 0);});
test('shell output beyond the 16 KiB model preview does not change its graph weight', () => { const mixedTool = tool('tool result '.repeat(1000)); assert.deepEqual(cats(segments([receipt('x'.repeat(64 * 1024)), mixedTool])), cats(segments([receipt('x'.repeat(16_384)), mixedTool])));});
test('longer output below the preview cap weighs more in a message/tool mixture', () => { const mixedTool = tool('tool result '.repeat(1000)); const short = segments([receipt('x'.repeat(100)), mixedTool]); const longer = segments([receipt('x'.repeat(2000)), mixedTool]); assert.ok(tokens(longer, 'msg') > tokens(short, 'msg'));});
test('multibyte output is bounded by UTF-8 bytes in hydrated receipts', () => { const mixedTool = tool('tool result '.repeat(1000)); assert.deepEqual(cats(segments([receipt('é'.repeat(10_000)), mixedTool])), cats(segments([receipt('é'.repeat(8_192)), mixedTool])));});
test('UTF-8 preview cap excludes a codepoint that crosses the byte boundary', () => { const mixedTool = tool('tool result '.repeat(1000)); for (let pad = 0; pad < 4; pad++) { const command = `cat file${' '.repeat(pad)}`; assert.deepEqual(cats(segments([receipt(`${'x'.repeat(16_383)}😀`, undefined, command), mixedTool])), cats(segments([receipt('x'.repeat(16_383), undefined, command), mixedTool]))); }});
test('referenced shell receipts are remeasured after same-id hydration', () => { const referenced = { kind: 'shell_command', id: 'same-entry', content: [{ kind: 'reference', reference: { field: 'shell_command', bytes: 65_000, url: '/history/1' }, }] }; const hydrated = receipt('visible output', 'same-entry'); const before = segments([referenced, tool('tool result '.repeat(1000))]); const after = segments([hydrated, tool('tool result '.repeat(1000))]); assert.ok(!before.some((part) => part.key === 'msg')); assert.ok(tokens(after, 'msg') > 0); assert.ok(tokens(after, 'tool') > 0);});