summaryrefslogtreecommitdiffhomepage
path: root/src/core/metrics/placement-scenarios.test.ts
blob: ce3ed5bd02fae8f36a9f4e09d0dbe1e2c3f4f109 (plain)
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// End-to-end placement scenarios: real transcript + metrics reducers fed real
// AgentEvents, rendered through groupRenderedChunks → interleaveTurnMetrics.
// The invariant under test: at EVERY point of a turn (not just after it ends),
// each turn's metrics sit under that turn — never under a neighbour.
import type { AgentEvent, StepId, StoredChunk, TurnMetrics, Usage } from "@dispatch/wire";
import { describe, expect, it } from "vitest";
import {
  appendUserMessage,
  applyHistory,
  foldEvent,
  groupRenderedChunks,
  initialState,
  selectChunks,
  type TranscriptState,
  trimTranscript,
} from "../chunks";
import { interleaveTurnMetrics } from "./place";
import {
  applyDurableMetrics,
  foldMetricsEvent,
  initialMetricsState,
  selectOrderedTurnMetrics,
} from "./reducer";
import type { MetricsRow, MetricsState } from "./types";

const C = "c1";
const u = (i: number, o: number): Usage => ({ inputTokens: i, outputTokens: o });

/** Ordered list of "what follows each user text bubble / which turn rows appear where". */
function layout(rows: readonly MetricsRow[]): string[] {
  const out: string[] = [];
  for (const r of rows) {
    if (r.kind === "turn-metrics") out.push(`[turn ${r.turn.turnId}]`);
    else if (r.kind === "step-metrics") out.push(`[step ${r.step.stepId}]`);
    else if (r.group.kind === "single") {
      const c = r.group.chunk.chunk;
      if (c.type === "text") out.push(`${r.group.chunk.role}:${c.text}`);
      else if (c.type === "image") out.push(`${r.group.chunk.role}:<img>`);
      else if (c.type === "error") out.push(`error:${c.message}`);
    } else {
      out.push(`tool:${r.group.entry.toolCallId}`);
    }
  }
  return out;
}

class Sim {
  t: TranscriptState = initialState();
  m: MetricsState = initialMetricsState();
  seq = 0;
  /** Durable server-side log (what loadSince returns). */
  log: StoredChunk[] = [];
  durable: TurnMetrics[] = [];

  constructor(private readonly legacy = false) {}

  private ev(e: AgentEvent): void {
    this.t = foldEvent(this.t, e);
    this.m = foldMetricsEvent(this.m, e);
  }

  private persist(role: StoredChunk["role"], chunk: StoredChunk["chunk"], turnId: string): void {
    this.seq++;
    this.log.push(
      this.legacy ? { seq: this.seq, role, chunk } : { seq: this.seq, role, chunk, turnId },
    );
  }

  /** The sender types + sends (optimistic echo), optionally with an image. */
  send(text: string, image = false): void {
    this.t = appendUserMessage(
      this.t,
      text,
      image ? [{ url: "data:image/png;base64,AA", mimeType: "image/png" }] : undefined,
    );
  }

  start(turnId: string, text: string, image = false): void {
    this.ev({ type: "turn-start", conversationId: C, turnId } as AgentEvent);
    this.ev({ type: "user-message", conversationId: C, turnId, text } as AgentEvent);
    this.persist("user", { type: "text", text }, turnId);
    if (image) this.persist("user", { type: "image", url: "/images/c1/x.png" }, turnId);
  }

  text(turnId: string, text: string): void {
    this.ev({ type: "text-delta", conversationId: C, turnId, delta: text } as AgentEvent);
  }

  toolStep(turnId: string, step: number, id: string): void {
    const stepId = `${turnId}#${step}` as StepId;
    this.ev({
      type: "tool-call",
      conversationId: C,
      turnId,
      stepId,
      toolCallId: id,
      toolName: "t",
      input: {},
    } as AgentEvent);
    this.completeStep(turnId, step);
    this.ev({
      type: "tool-result",
      conversationId: C,
      turnId,
      stepId,
      toolCallId: id,
      toolName: "t",
      content: "ok",
      isError: false,
    } as AgentEvent);
    this.persist(
      "assistant",
      { type: "tool-call", toolCallId: id, toolName: "t", input: {}, stepId },
      turnId,
    );
    this.persist(
      "tool",
      { type: "tool-result", toolCallId: id, toolName: "t", content: "ok", isError: false, stepId },
      turnId,
    );
  }

  completeStep(turnId: string, step: number): void {
    const stepId = `${turnId}#${step}` as StepId;
    this.ev({ type: "usage", conversationId: C, turnId, stepId, usage: u(100, 10) } as AgentEvent);
    this.ev({
      type: "step-complete",
      conversationId: C,
      turnId,
      stepId,
      ttftMs: 100,
      decodeMs: 100,
      genTotalMs: 200,
    } as AgentEvent);
  }

  steer(turnId: string, text: string): void {
    this.ev({ type: "steering", conversationId: C, turnId, text } as AgentEvent);
    this.persist("user", { type: "text", text }, turnId);
  }

  /** Finish a turn: final text step, done, seal, then history + metrics sync. */
  finish(turnId: string, text: string, finalStep: number): void {
    this.text(turnId, text);
    this.completeStep(turnId, finalStep);
    this.ev({
      type: "done",
      conversationId: C,
      turnId,
      reason: "stop",
      durationMs: 500,
      usage: u(100, 10),
    } as AgentEvent);
    this.persist("assistant", { type: "text", text }, turnId);
    this.ev({ type: "turn-sealed", conversationId: C, turnId } as AgentEvent);
    this.sync(turnId, finalStep);
  }

  /** A turn that errors (never seals); backend persists its partial metrics. */
  fail(turnId: string, message: string, steps: number): void {
    this.ev({ type: "error", conversationId: C, turnId, message } as AgentEvent);
    this.persist("assistant", { type: "error", message }, turnId);
    this.t = applyHistory(this.t, this.log);
    if (steps > 0) this.syncMetricsFor(turnId, steps - 1);
  }

  private syncMetricsFor(turnId: string, lastStep: number): void {
    const steps = [];
    for (let i = 0; i <= lastStep; i++) {
      steps.push({ stepId: `${turnId}#${i}` as StepId, usage: u(100, 10), decodeMs: 100 });
    }
    this.durable.push({ turnId, usage: u(100 * steps.length, 10 * steps.length), steps });
    this.m = applyDurableMetrics(this.m, this.durable);
  }

  private sync(turnId: string, lastStep: number): void {
    this.t = applyHistory(this.t, this.log);
    this.syncMetricsFor(turnId, lastStep);
  }

  trim(limit: number): void {
    this.t = trimTranscript(this.t, limit);
  }

  layout(): string[] {
    const groups = groupRenderedChunks(selectChunks(this.t));
    return layout(interleaveTurnMetrics(groups, selectOrderedTurnMetrics(this.m)));
  }
}

/** Build a history of N text-only turns (t1..tN), each "qK" → "aK". */
function textHistory(sim: Sim, n: number): void {
  for (let k = 1; k <= n; k++) {
    sim.send(`q${k}`);
    sim.start(`t${k}`, `q${k}`);
    sim.finish(`t${k}`, `a${k}`, 0);
  }
}

for (const legacy of [false, true]) {
  // `legacy`: the persisted rows carry NO turnId (history written before the
  // backend stamped it). Live chunks always carry it.
  describe(`placement stays exact through a turn (${legacy ? "legacy rows" : "turnId rows"})`, () => {
    it("plain text turns", () => {
      const sim = new Sim(legacy);
      textHistory(sim, 3);
      expect(sim.layout()).toEqual([
        "user:q1",
        "assistant:a1",
        "[turn t1]",
        "user:q2",
        "assistant:a2",
        "[turn t2]",
        "user:q3",
        "assistant:a3",
        "[turn t3]",
      ]);
    });

    it("never shifts while a new turn is sent and streams — even after older turns were unloaded", () => {
      const sim = new Sim(legacy);
      textHistory(sim, 4);
      sim.trim(5); // 8 chunks > 5 → unloads whole quarters: q1 a1 q2 a2
      const before = sim.layout();
      expect(before).toEqual([
        "user:q3",
        "assistant:a3",
        "[turn t3]",
        "user:q4",
        "assistant:a4",
        "[turn t4]",
      ]);

      const expectStable = () => expect(sim.layout().slice(0, before.length)).toEqual(before);

      sim.send("q5");
      expectStable(); // just sent (optimistic echo, no turn yet)
      sim.start("t5", "q5");
      expectStable(); // turn started
      sim.toolStep("t5", 0, "k1");
      expectStable(); // tool step completed (metrics entry exists, not done)
      sim.text("t5", "partial…");
      expectStable(); // streaming text
      sim.finish("t5", "a5", 1);
      expect(sim.layout()).toEqual([
        ...before,
        "user:q5",
        "tool:k1",
        "[step t5#0]",
        "assistant:a5",
        "[turn t5]",
      ]);
    });

    it("image attachments never shift other turns' metrics", () => {
      const sim = new Sim(legacy);
      sim.send("q1");
      sim.start("t1", "q1");
      sim.finish("t1", "a1", 0);
      sim.send("q2", true);
      sim.start("t2", "q2", true);
      sim.finish("t2", "a2", 0);
      sim.send("q3");
      sim.start("t3", "q3");
      sim.finish("t3", "a3", 0);
      expect(sim.layout()).toEqual([
        "user:q1",
        "assistant:a1",
        "[turn t1]",
        "user:q2",
        "user:<img>",
        "assistant:a2",
        "[turn t2]",
        "user:q3",
        "assistant:a3",
        "[turn t3]",
      ]);
    });

    it("a mid-turn steering message keeps the turn total at the END of the turn", () => {
      const sim = new Sim(legacy);
      textHistory(sim, 1);
      sim.send("q2");
      sim.start("t2", "q2");
      sim.toolStep("t2", 0, "k1");
      sim.steer("t2", "steer!");
      // live, mid-turn: t1 untouched, steering bubble shows no totals
      expect(sim.layout()).toEqual([
        "user:q1",
        "assistant:a1",
        "[turn t1]",
        "user:q2",
        "tool:k1",
        "[step t2#0]",
        "user:steer!",
      ]);
      sim.toolStep("t2", 1, "k2");
      sim.finish("t2", "a2", 2);
      textHistoryFrom(sim, 3);
      expect(sim.layout()).toEqual([
        "user:q1",
        "assistant:a1",
        "[turn t1]",
        "user:q2",
        "tool:k1",
        "[step t2#0]",
        "user:steer!",
        "tool:k2",
        "[step t2#1]",
        "assistant:a2",
        "[turn t2]",
        "user:q3",
        "assistant:a3",
        "[turn t3]",
      ]);
    });

    it("an errored turn gets its own metrics and never shifts the others", () => {
      const sim = new Sim(legacy);
      textHistory(sim, 1);
      sim.send("q2");
      sim.start("t2", "q2");
      sim.text("t2", "partial");
      sim.completeStep("t2", 0);
      sim.fail("t2", "boom", 1);
      textHistoryFrom(sim, 3);
      const l = sim.layout();
      expect(l.indexOf("[turn t1]")).toBe(l.indexOf("assistant:a1") + 1);
      expect(l.indexOf("[turn t2]")).toBeGreaterThan(l.indexOf("error:boom"));
      expect(l.indexOf("[turn t2]")).toBeLessThan(l.indexOf("user:q3"));
      expect(l.indexOf("[turn t3]")).toBe(l.indexOf("assistant:a3") + 1);
    });

    it("an errored turn with NO metrics never steals another turn's", () => {
      const sim = new Sim(legacy);
      textHistory(sim, 1);
      sim.send("q2");
      sim.start("t2", "q2");
      sim.fail("t2", "boom", 0); // failed before any step → no metrics row
      textHistoryFrom(sim, 3);
      expect(sim.layout()).toEqual([
        "user:q1",
        "assistant:a1",
        "[turn t1]",
        "user:q2",
        "error:boom",
        "user:q3",
        "assistant:a3",
        "[turn t3]",
      ]);
    });

    it("a turn cut in half by the chat limit still shows its total under its visible tail", () => {
      const sim = new Sim(legacy);
      sim.send("q1");
      sim.start("t1", "q1");
      sim.toolStep("t1", 0, "k1");
      sim.toolStep("t1", 1, "k2");
      sim.finish("t1", "a1", 2);
      textHistoryFrom(sim, 2);
      // log: q1 call result call result a1 | q2 a2  → keep newest 6
      sim.trim(7);
      const l = sim.layout();
      expect(l).not.toContain("user:q1");
      expect(l.indexOf("[turn t1]")).toBe(l.indexOf("assistant:a1") + 1);
      expect(l.indexOf("[turn t2]")).toBe(l.indexOf("assistant:a2") + 1);
    });
  });
}

/** Continue a text history from turn `from` (one more turn). */
function textHistoryFrom(sim: Sim, from: number): void {
  sim.send(`q${from}`);
  sim.start(`t${from}`, `q${from}`);
  sim.finish(`t${from}`, `a${from}`, 0);
}