|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { |
| 4 | + AGENT_WIRE_PROTOCOL_VERSION, |
| 5 | + AgentRecords, |
| 6 | + InMemoryAgentRecordPersistence, |
| 7 | + type AgentRecord, |
| 8 | +} from '../../../src/agent/records'; |
| 9 | + |
| 10 | +describe('AgentRecords persistence metadata', () => { |
| 11 | + it('writes metadata before the first persisted record', async () => { |
| 12 | + const persistence = new InMemoryAgentRecordPersistence(); |
| 13 | + const records = new AgentRecords(() => {}, persistence); |
| 14 | + |
| 15 | + records.logRecord({ |
| 16 | + type: 'turn.prompt', |
| 17 | + input: [{ type: 'text', text: 'hello' }], |
| 18 | + origin: { kind: 'user' }, |
| 19 | + }); |
| 20 | + await records.flush(); |
| 21 | + |
| 22 | + expect(persistence.records).toHaveLength(2); |
| 23 | + expect(persistence.records[0]).toMatchObject({ |
| 24 | + type: 'metadata', |
| 25 | + protocol_version: AGENT_WIRE_PROTOCOL_VERSION, |
| 26 | + }); |
| 27 | + expect(persistence.records[1]?.type).toBe('turn.prompt'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('does not write metadata when replaying an empty stream', async () => { |
| 31 | + const persistence = new InMemoryAgentRecordPersistence(); |
| 32 | + const records = new AgentRecords(() => {}, persistence); |
| 33 | + |
| 34 | + await records.replay(); |
| 35 | + records.logRecord({ |
| 36 | + type: 'turn.prompt', |
| 37 | + input: [{ type: 'text', text: 'one' }], |
| 38 | + origin: { kind: 'user' }, |
| 39 | + }); |
| 40 | + await records.flush(); |
| 41 | + |
| 42 | + expect(persistence.records.map((record) => record.type)).toEqual([ |
| 43 | + 'metadata', |
| 44 | + 'turn.prompt', |
| 45 | + ]); |
| 46 | + }); |
| 47 | + |
| 48 | + it('rejects replaying a non-empty stream without metadata', async () => { |
| 49 | + const persistence = new InMemoryAgentRecordPersistence([ |
| 50 | + { |
| 51 | + type: 'turn.prompt', |
| 52 | + input: [{ type: 'text', text: 'one' }], |
| 53 | + origin: { kind: 'user' }, |
| 54 | + }, |
| 55 | + ]); |
| 56 | + const records = new AgentRecords(() => {}, persistence); |
| 57 | + |
| 58 | + await expect(records.replay()).rejects.toThrow( |
| 59 | + 'AgentRecords replay expected metadata as the first record', |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + it('does not duplicate metadata after replaying existing records', async () => { |
| 64 | + const persistence = new InMemoryAgentRecordPersistence([ |
| 65 | + { |
| 66 | + type: 'metadata', |
| 67 | + protocol_version: AGENT_WIRE_PROTOCOL_VERSION, |
| 68 | + created_at: 1, |
| 69 | + }, |
| 70 | + { |
| 71 | + type: 'turn.prompt', |
| 72 | + input: [{ type: 'text', text: 'one' }], |
| 73 | + origin: { kind: 'user' }, |
| 74 | + }, |
| 75 | + ]); |
| 76 | + const records = new AgentRecords(() => {}, persistence); |
| 77 | + |
| 78 | + await records.replay(); |
| 79 | + records.logRecord({ |
| 80 | + type: 'turn.prompt', |
| 81 | + input: [{ type: 'text', text: 'two' }], |
| 82 | + origin: { kind: 'user' }, |
| 83 | + }); |
| 84 | + await records.flush(); |
| 85 | + |
| 86 | + expect(persistence.records.map((record) => record.type)).toEqual([ |
| 87 | + 'metadata', |
| 88 | + 'turn.prompt', |
| 89 | + 'turn.prompt', |
| 90 | + ]); |
| 91 | + expect(persistence.records.filter((record) => record.type === 'metadata')).toHaveLength(1); |
| 92 | + }); |
| 93 | + |
| 94 | + it('does not rewrite records that already use the current wire version', async () => { |
| 95 | + const persistence = new RecordingInMemoryAgentRecordPersistence([ |
| 96 | + { |
| 97 | + type: 'metadata', |
| 98 | + protocol_version: AGENT_WIRE_PROTOCOL_VERSION, |
| 99 | + created_at: 1, |
| 100 | + }, |
| 101 | + { |
| 102 | + type: 'turn.prompt', |
| 103 | + input: [{ type: 'text', text: 'one' }], |
| 104 | + origin: { kind: 'user' }, |
| 105 | + }, |
| 106 | + ]); |
| 107 | + const records = new AgentRecords(() => {}, persistence); |
| 108 | + |
| 109 | + await records.replay(); |
| 110 | + |
| 111 | + expect(persistence.rewrites).toEqual([]); |
| 112 | + }); |
| 113 | + |
| 114 | + it('rejects replaying records from a newer wire version', async () => { |
| 115 | + const persistence = new InMemoryAgentRecordPersistence([ |
| 116 | + { |
| 117 | + type: 'metadata', |
| 118 | + protocol_version: '9.9', |
| 119 | + created_at: 1, |
| 120 | + }, |
| 121 | + ]); |
| 122 | + const records = new AgentRecords(() => {}, persistence); |
| 123 | + |
| 124 | + await expect(records.replay()).rejects.toThrow( |
| 125 | + `Unsupported wire protocol version: 9.9 (current: ${AGENT_WIRE_PROTOCOL_VERSION})`, |
| 126 | + ); |
| 127 | + }); |
| 128 | + |
| 129 | + it('rejects replaying records without a registered migration path', async () => { |
| 130 | + const persistence = new InMemoryAgentRecordPersistence([ |
| 131 | + { |
| 132 | + type: 'metadata', |
| 133 | + protocol_version: '0.9', |
| 134 | + created_at: 1, |
| 135 | + }, |
| 136 | + ]); |
| 137 | + const records = new AgentRecords(() => {}, persistence); |
| 138 | + |
| 139 | + await expect(records.replay()).rejects.toThrow('Missing wire migration for version 0.9'); |
| 140 | + }); |
| 141 | +}); |
| 142 | + |
| 143 | +class RecordingInMemoryAgentRecordPersistence extends InMemoryAgentRecordPersistence { |
| 144 | + readonly rewrites: AgentRecord[][] = []; |
| 145 | + |
| 146 | + override rewrite(records: readonly AgentRecord[]): void { |
| 147 | + this.rewrites.push([...records]); |
| 148 | + super.rewrite(records); |
| 149 | + } |
| 150 | +} |
0 commit comments