File size: 9,940 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import { readFile } from 'node:fs/promises';
import { resolve } from 'node:path';

import { renderToStaticMarkup } from 'react-dom/server';
import { describe, expect, it } from 'vitest';

import { WIRE_RENDERERS } from '../src/components/wire/renderers';

type CheckpointRecord = Parameters<
  (typeof WIRE_RENDERERS)['file_history.checkpoint']['headline']
>[0];
type TrackedRecord = Parameters<
  (typeof WIRE_RENDERERS)['file_history.tracked']['headline']
>[0];
type CompactionRecord = Parameters<
  (typeof WIRE_RENDERERS)['context.apply_compaction']['headline']
>[0];

function checkpointRecord(overrides: Record<string, unknown> = {}): CheckpointRecord {
  return {
    type: 'file_history.checkpoint',
    agentId: 'main',
    turnId: 7,
    phase: 'start',
    entries: {},
    ...overrides,
  } as unknown as CheckpointRecord;
}

function trackedRecord(overrides: Record<string, unknown> = {}): TrackedRecord {
  return {
    type: 'file_history.tracked',
    agentId: 'main',
    turnId: 7,
    path: '/workspace/example.txt',
    entry: { key: 'snapshot-7', version: 2 },
    ...overrides,
  } as unknown as TrackedRecord;
}

function compactionRecord(overrides: Record<string, unknown> = {}): CompactionRecord {
  return {
    type: 'context.apply_compaction',
    agentId: 'main',
    summary: 'compact summary',
    compactedCount: 4,
    ...overrides,
  } as unknown as CompactionRecord;
}

const HISTORICAL_OR_HEADER_TYPES = new Set([
  'metadata',
  'context.update_token_count',
  'micro_compaction.apply',
  'staleGuard.recorded',
  'staleGuard.cleared',
  'prompt.accepted',
]);

describe('wire renderers', () => {
  it('covers every durable record in the current core-v2 wire manifest', async () => {
    const manifestPath = resolve(
      import.meta.dirname,
      '../../../../packages/agent-core-v2/docs/wire-manifest.d.ts',
    );
    const manifest = await readFile(manifestPath, 'utf8');
    const index = /\/\/ Index \(\d+ record types\)\n((?:\/\/   .*\n)+)/.exec(manifest)?.[1];
    expect(index).toBeDefined();

    const upstreamTypes = [...(index ?? '').matchAll(/^\/\/   (\S+)/gm)]
      .map((match) => match[1])
      .toSorted();
    const renderedCurrentTypes = Object.keys(WIRE_RENDERERS)
      .filter((type) => !HISTORICAL_OR_HEADER_TYPES.has(type))
      .toSorted();

    expect(renderedCurrentTypes).toEqual(upstreamTypes);
  });

  it('distinguishes oversized snapshots from files that did not exist', () => {
    const renderer = WIRE_RENDERERS['file_history.tracked'];
    const oversized = trackedRecord({
      entry: { key: null, version: 2, oversize: true, size: 10_000_000 },
    });
    const oversizedHeadline = renderer.headline(oversized);
    const oversizedDetail = renderer.detail?.(oversized);

    expect(renderToStaticMarkup(oversizedHeadline.right)).toContain('oversize');
    expect(renderToStaticMarkup(oversizedHeadline.right)).not.toContain('new');
    expect(renderToStaticMarkup(oversizedDetail)).toContain('not captured: oversized');
    expect(renderToStaticMarkup(oversizedDetail)).not.toContain('file did not exist');

    const missing = trackedRecord({ entry: { key: null, version: 2 } });
    const missingHeadline = renderer.headline(missing);
    const missingDetail = renderer.detail?.(missing);

    expect(renderToStaticMarkup(missingHeadline.right)).toContain('new');
    expect(renderToStaticMarkup(missingDetail)).toContain('file did not exist');
  });

  it.each([
    ['missing', undefined],
    ['null', null],
    ['scalar', 'broken'],
    ['array', []],
  ])('renders a checkpoint with %s entries without throwing', (_label, entries) => {
    const renderer = WIRE_RENDERERS['file_history.checkpoint'];
    const record = checkpointRecord({
      turnId: { unexpected: true },
      phase: { unexpected: true },
      entries,
    });
    const headline = renderer.headline(record);
    const detail = renderer.detail?.(record);

    expect(renderToStaticMarkup(headline.main)).toContain('entries unavailable');
    expect(renderToStaticMarkup(headline.main)).toContain('invalid');
    expect(renderToStaticMarkup(headline.right)).toContain('invalid');
    expect(() => renderToStaticMarkup(detail)).not.toThrow();
  });

  it.each([
    ['missing', undefined],
    ['null', null],
    ['scalar', 'broken'],
    ['array', []],
  ])('renders a tracked record with a %s entry without throwing', (_label, entry) => {
    const renderer = WIRE_RENDERERS['file_history.tracked'];
    const record = trackedRecord({
      turnId: { unexpected: true },
      path: { unexpected: true },
      entry,
    });
    const headline = renderer.headline(record);
    const detail = renderer.detail?.(record);

    expect(renderToStaticMarkup(headline.main)).toContain('invalid');
    expect(renderToStaticMarkup(headline.right)).toContain('entry unavailable');
    expect(renderToStaticMarkup(detail)).toContain('entry unavailable');
  });

  it('renders malformed tracked fields as readable text', () => {
    const renderer = WIRE_RENDERERS['file_history.tracked'];
    const malformed = { unexpected: true };
    const record = trackedRecord({
      turnId: malformed,
      path: malformed,
      entry: {
        key: malformed,
        version: malformed,
        contentHash: malformed,
        size: malformed,
        mtimeMs: malformed,
        oversize: malformed,
      },
    });
    const headline = renderer.headline(record);
    const detail = renderer.detail?.(record);

    expect(renderToStaticMarkup(headline.main)).toContain('invalid');
    expect(renderToStaticMarkup(headline.right)).toContain('invalid entry');
    expect(renderToStaticMarkup(detail)).toContain('invalid');
    expect(renderToStaticMarkup(detail)).not.toContain('[object Object]');
  });

  it('renders a valid compaction wire-line range in the headline and detail', () => {
    const renderer = WIRE_RENDERERS['context.apply_compaction'];
    const record = compactionRecord({ wireLines: { start: 12, end: 34 } });
    const headline = renderer.headline(record);
    const detail = renderer.detail?.(record);

    expect(renderToStaticMarkup(headline.right)).toContain('L12–34');
    expect(renderToStaticMarkup(detail)).toContain('wireLines');
    expect(renderToStaticMarkup(detail)).toContain('12–34');
  });

  it('preserves each compaction summary variant and legacy field names', () => {
    const renderer = WIRE_RENDERERS['context.apply_compaction'];
    const dual = compactionRecord({
      summary: 'raw summary',
      contextSummary: 'model summary',
    });
    const dualMarkup = renderToStaticMarkup(renderer.detail?.(dual));
    expect(dualMarkup).toContain('raw summary');
    expect(dualMarkup).toContain('contextSummary');
    expect(dualMarkup).toContain('model summary');

    const contextOnly = compactionRecord({
      summary: undefined,
      contextSummary: 'context-only summary',
    });
    expect(renderToStaticMarkup(renderer.headline(contextOnly).main)).toContain(
      'contextSummary',
    );
    expect(renderToStaticMarkup(renderer.detail?.(contextOnly))).toContain(
      'context-only summary',
    );

    const legacy = compactionRecord({
      summary: {
        role: 'user',
        content: [
          { type: 'text', text: 'first' },
          { type: 'image_url', imageUrl: { url: 'data:image/png;base64,AAAA' } },
          { type: 'text', text: 'second' },
        ],
        toolCalls: [{ type: 'function', id: 'call-1', name: 'Read', arguments: '{}' }],
        origin: { kind: 'compaction_summary' },
      },
      compactedCount: undefined,
      count: 3,
      legacyTail: true,
    });
    const legacyMarkup = renderToStaticMarkup(renderer.detail?.(legacy));
    expect(legacyMarkup).toContain('firstsecond');
    expect(legacyMarkup).toContain('summaryMessage');
    expect(legacyMarkup).toContain('toolCalls');
    expect(legacyMarkup).toContain('count');
    expect(legacyMarkup).not.toContain('compactedCount');
    expect(legacyMarkup).toContain('legacyTail');
    expect(legacyMarkup).toContain('true');
  });

  it.each([
    ['null', null],
    ['scalar', 'broken'],
    ['array', [12, 34]],
    ['missing start', { end: 34 }],
    ['missing end', { start: 12 }],
    ['NaN start', { start: Number.NaN, end: 34 }],
    ['NaN end', { start: 12, end: Number.NaN }],
    ['infinite start', { start: Number.POSITIVE_INFINITY, end: 34 }],
    ['negative start', { start: -1, end: 34 }],
    ['fractional end', { start: 12, end: 34.5 }],
  ])('omits a compaction wire-line range with %s', (_label, wireLines) => {
    const renderer = WIRE_RENDERERS['context.apply_compaction'];
    const record = compactionRecord({ wireLines });
    const headline = renderer.headline(record);
    const detail = renderer.detail?.(record);

    const headlineMarkup = renderToStaticMarkup(headline.right);
    const detailMarkup = renderToStaticMarkup(detail);
    expect(headlineMarkup).toBe('');
    expect(headlineMarkup).not.toContain('Lundefined');
    expect(detailMarkup).not.toContain('wireLines');
    expect(detailMarkup).not.toContain('undefined');
  });

  it('renders malformed compaction scalars as readable text', () => {
    const renderer = WIRE_RENDERERS['context.apply_compaction'];
    const malformed = { unexpected: true };
    const record = compactionRecord({
      summary: malformed,
      contextSummary: malformed,
      compactedCount: malformed,
      tokensBefore: malformed,
      tokensAfter: malformed,
      summaryOutputTokens: malformed,
      keptUserMessageCount: malformed,
      keptHeadUserMessageCount: malformed,
      droppedCount: malformed,
    });

    const headline = renderer.headline(record);
    const detail = renderer.detail?.(record);
    const markup = [headline.main, headline.right, detail]
      .map((node) => renderToStaticMarkup(node))
      .join('');

    expect(markup).toContain('invalid');
    expect(markup).not.toContain('[object Object]');
  });
});