File size: 7,728 Bytes
7c5ed63 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | import { describe, it, expect, afterEach } from 'vitest';
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { buildSessionFixture } from '../fixtures/build';
import { readAgentWire } from '../../src/lib/wire-reader';
describe('wire-reader', () => {
let cleanup: (() => Promise<void>) | null = null;
afterEach(async () => {
if (cleanup) await cleanup();
cleanup = null;
});
it('reads main agent wire and assigns line numbers', async () => {
const { sessionDir, cleanup: c } = await buildSessionFixture('sample-main');
cleanup = c;
const result = await readAgentWire(join(sessionDir, 'agents', 'main', 'wire.jsonl'));
expect(result.metadata.protocolVersion).toBe('1.1');
expect(result.records[0]!.lineNo).toBe(2); // metadata is line 1, first record is line 2
expect(result.records.at(-1)!.lineNo).toBe(10);
expect(result.records.map((r) => r.data.type)).toEqual([
'config.update',
'tools.set_active_tools',
'permission.set_mode',
'turn.prompt',
'context.append_message',
'context.append_loop_event',
'context.append_loop_event',
'context.append_loop_event',
'usage.record',
]);
// No vis annotation should leak into the data/raw bodies.
for (const entry of result.records) {
expect(entry.data).not.toHaveProperty('_lineNo');
expect(entry.raw as object).not.toHaveProperty('_lineNo');
}
});
it('accepts v1.0 wire and migrates nested tool calls to flat shape', async () => {
const dir = await mkdtemp(join(tmpdir(), 'vis-v10-'));
const path = join(dir, 'wire.jsonl');
const lines = [
JSON.stringify({ type: 'metadata', protocol_version: '1.0', created_at: 1 }),
JSON.stringify({
type: 'context.append_message',
message: {
role: 'assistant',
content: [{ type: 'text', text: 'calling' }],
toolCalls: [
{
type: 'function',
id: 'call_1',
function: { name: 'Read', arguments: '{"path":"/x"}' },
},
],
},
}),
];
await writeFile(path, lines.join('\n') + '\n');
try {
const result = await readAgentWire(path);
expect(result.metadata.protocolVersion).toBe('1.0');
const entry = result.records[0]!;
expect(entry.data.type).toBe('context.append_message');
// `data` carries the migrated (flat) shape.
const dataMsg = (entry.data as { message: { toolCalls: unknown[] } }).message;
expect(dataMsg.toolCalls[0]).toEqual({
type: 'function',
id: 'call_1',
name: 'Read',
arguments: '{"path":"/x"}',
});
expect(dataMsg.toolCalls[0]).not.toHaveProperty('function');
// `raw` keeps the on-disk (nested) shape — this is what the "as
// written" view in the detail panel relies on.
const rawMsg = (entry.raw as { message: { toolCalls: Array<{ function: unknown; name?: unknown }> } }).message;
expect(rawMsg.toolCalls[0]).toHaveProperty('function');
expect(rawMsg.toolCalls[0]!.function).toEqual({ name: 'Read', arguments: '{"path":"/x"}' });
expect(rawMsg.toolCalls[0]).not.toHaveProperty('name');
} finally {
await rm(dir, { recursive: true, force: true });
}
});
it('best-effort parses unknown protocol versions with a warning', async () => {
const { sessionDir, cleanup: c } = await buildSessionFixture('sample-main');
cleanup = c;
const path = join(sessionDir, 'agents', 'main', 'wire.jsonl');
const { writeFile, readFile } = await import('node:fs/promises');
const lines = (await readFile(path, 'utf8')).split('\n');
lines[0] = '{"type":"metadata","protocol_version":"0.9","created_at":1}';
await writeFile(path, lines.join('\n'));
const result = await readAgentWire(path);
expect(result.metadata.protocolVersion).toBe('0.9');
expect(result.records.length).toBeGreaterThan(0);
expect(result.warnings.some((w) => /unrecognised protocol_version.*0\.9/i.test(w))).toBe(
true,
);
});
it('passes v1.5 records through unchanged with no warnings', async () => {
const dir = await mkdtemp(join(tmpdir(), 'vis-v15-'));
const path = join(dir, 'wire.jsonl');
const records = [
{ type: 'metadata', protocol_version: '1.5', created_at: 1 },
{ type: 'token_counting.measured', agentId: 'main', length: 4, tokens: 1234, time: 2 },
{
type: 'cron.add',
agentId: 'main',
task: { id: '01ARZ3NDEKTSV4RRFFQ69G5FAV', cron: '* * * * *', prompt: 'p', createdAt: 3 },
time: 3,
},
];
await writeFile(path, records.map((r) => JSON.stringify(r)).join('\n') + '\n');
try {
const result = await readAgentWire(path);
expect(result.metadata.protocolVersion).toBe('1.5');
expect(result.warnings).toEqual([]);
expect(result.records).toHaveLength(2);
// data === raw for current-protocol records (no migration applied).
expect(result.records[0]!.data).toEqual(result.records[0]!.raw);
expect(result.records[1]!.data).toEqual(result.records[1]!.raw);
} finally {
await rm(dir, { recursive: true, force: true });
}
});
it('recovers a headerless journal with the same v1.4 assumption as core-v2', async () => {
const { sessionDir, cleanup: c } = await buildSessionFixture('sample-main');
cleanup = c;
const path = join(sessionDir, 'agents', 'main', 'wire.jsonl');
await writeFile(
path,
JSON.stringify({
type: 'goal.create',
agentId: 'main',
goalId: 'goal-1',
objective: 'ship',
time: 40,
}) + '\n',
);
const result = await readAgentWire(path);
expect(result.metadata).toEqual({ protocolVersion: '1.4', createdAt: 0 });
expect(result.warnings).toEqual([
'line 1: missing metadata header — assuming protocol_version "1.4"',
]);
expect(result.records[0]).toMatchObject({
lineNo: 1,
data: { type: 'goal.create', wallClockResumedAt: 40 },
});
});
it('normalizes legacy plan revision paths to the current storage key', async () => {
const { sessionDir, cleanup: c } = await buildSessionFixture('sample-main');
cleanup = c;
const path = join(sessionDir, 'agents', 'main', 'wire.jsonl');
await writeFile(
path,
[
JSON.stringify({ type: 'metadata', protocol_version: '1.5', created_at: 1 }),
JSON.stringify({
type: 'plan.revision',
agentId: 'main',
id: 'demo-plan',
version: 2,
path: 'sessions/workspace/session_demo/agents/main/plan/demo-plan/v2.md',
sha256: 'abc',
bytes: 10,
time: 2,
}),
].join('\n') + '\n',
);
const result = await readAgentWire(path);
expect(result.records[0]!.data).toMatchObject({
type: 'plan.revision',
key: 'plan/demo-plan/v2.md',
});
expect(result.records[0]!.data).not.toHaveProperty('path');
expect(result.records[0]!.raw).toHaveProperty(
'path',
'sessions/workspace/session_demo/agents/main/plan/demo-plan/v2.md',
);
});
it('collects warnings for malformed body lines', async () => {
const { sessionDir, cleanup: c } = await buildSessionFixture('sample-main');
cleanup = c;
const path = join(sessionDir, 'agents', 'main', 'wire.jsonl');
const { appendFile } = await import('node:fs/promises');
await appendFile(path, 'not json\n');
const result = await readAgentWire(path);
expect(result.warnings.length).toBe(1);
expect(result.warnings[0]).toMatch(/line 11/);
});
});
|