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
|
import type { AgentEvent, StepId, TurnMetrics } from "@dispatch/wire";
import { describe, expect, it } from "vitest";
import type { RenderGroup } from "../chunks";
import {
CACHE_TIMER_MAX_MS,
CACHE_TTL_MS,
type CacheTimerState,
foldCacheTimerEvent,
initialCacheTimerState,
lastTurnRowIndex,
seedTurnGap,
stopTurnGap,
viewCacheTimer,
} from "./cache-timer";
import type { MetricsRow } from "./types";
const SEC = 1000;
const MIN = 60 * SEC;
const C = "conv";
const ev = {
turnStart: (turnId: string): AgentEvent => ({ type: "turn-start", conversationId: C, turnId }),
userMessage: (turnId: string): AgentEvent => ({
type: "user-message",
conversationId: C,
turnId,
text: "hi",
}),
text: (turnId: string): AgentEvent => ({
type: "text-delta",
conversationId: C,
turnId,
delta: "x",
}),
reasoningEnd: (turnId: string): AgentEvent => ({
type: "reasoning-end",
conversationId: C,
turnId,
}),
call: (turnId: string, stepId: string, toolCallId: string): AgentEvent => ({
type: "tool-call",
conversationId: C,
turnId,
stepId: stepId as StepId,
toolCallId,
toolName: "t",
input: {},
}),
result: (
turnId: string,
stepId: string,
toolCallId: string,
durationMs?: number,
): AgentEvent => ({
type: "tool-result",
conversationId: C,
turnId,
stepId: stepId as StepId,
toolCallId,
toolName: "t",
content: "ok",
isError: false,
...(durationMs !== undefined ? { durationMs } : {}),
}),
stepComplete: (turnId: string, stepId: string): AgentEvent => ({
type: "step-complete",
conversationId: C,
turnId,
stepId: stepId as StepId,
}),
usage: (turnId: string, stepId: string): AgentEvent => ({
type: "usage",
conversationId: C,
turnId,
stepId: stepId as StepId,
usage: { inputTokens: 1, outputTokens: 1 },
}),
done: (turnId: string): AgentEvent => ({
type: "done",
conversationId: C,
turnId,
reason: "end-turn",
}),
error: (turnId: string): AgentEvent => ({
type: "error",
conversationId: C,
turnId,
message: "boom",
}),
};
const noTurn = () => undefined;
/** Fold `[time, event]` pairs from the initial state. */
function run(
steps: readonly (readonly [number, AgentEvent])[],
start: CacheTimerState = initialCacheTimerState(),
lastTurnId: () => string | undefined = noTurn,
): CacheTimerState {
return steps.reduce((s, [at, e]) => foldCacheTimerEvent(s, e, at, lastTurnId), start);
}
describe("foldCacheTimerEvent — step gaps", () => {
it("runs from step-complete while tools execute, and stamps when the last result returns", () => {
const s1 = run([
[0, ev.turnStart("t1")],
[100, ev.call("t1", "t1#0", "a")],
[110, ev.call("t1", "t1#0", "b")],
[1_000, ev.stepComplete("t1", "t1#0")],
]);
expect(s1.running).toMatchObject({
kind: "step",
stepId: "t1#0",
startedAt: 1_000,
toolCount: 2,
});
const s2 = run([[4_000, ev.result("t1", "t1#0", "a")]], s1);
expect(s2.running?.kind).toBe("step"); // still waiting on "b"
expect(s2.stepStamps.size).toBe(0);
const s3 = run([[13_000, ev.result("t1", "t1#0", "b")]], s2);
expect(s3.running).toBeNull();
expect(s3.stepStamps.get("t1#0")).toBe(12_000);
});
it("stamps each step of a multi-step turn separately", () => {
const s = run([
[0, ev.call("t1", "t1#0", "a")],
[1_000, ev.stepComplete("t1", "t1#0")],
[3_000, ev.result("t1", "t1#0", "a")],
[5_000, ev.call("t1", "t1#1", "b")],
[6_000, ev.stepComplete("t1", "t1#1")],
[66_000, ev.result("t1", "t1#1", "b")],
]);
expect(s.stepStamps.get("t1#0")).toBe(2_000);
expect(s.stepStamps.get("t1#1")).toBe(60_000);
});
it("uses the backend tool duration when it exceeds the wall clock (late-join replay burst)", () => {
const s = run([
[5_000, ev.call("t1", "t1#0", "a")],
[5_000, ev.stepComplete("t1", "t1#0")],
[5_000, ev.result("t1", "t1#0", "a", 42_000)],
]);
expect(s.stepStamps.get("t1#0")).toBe(42_000);
});
it("stops at the next step's output when a result never matched (fallback)", () => {
const s = run([
[0, ev.call("t1", "t1#0", "a")],
[1_000, ev.stepComplete("t1", "t1#0")],
[9_000, ev.text("t1")],
]);
expect(s.running).toBeNull();
expect(s.stepStamps.get("t1#0")).toBe(8_000);
});
it("treats a reasoning-end boundary as resumed output too", () => {
const s = run([
[0, ev.call("t1", "t1#0", "a")],
[1_000, ev.stepComplete("t1", "t1#0")],
[9_000, ev.reasoningEnd("t1")],
]);
expect(s.running).toBeNull();
expect(s.stepStamps.get("t1#0")).toBe(8_000);
});
it("stops on a tool-call or usage from a DIFFERENT step, not its own", () => {
const base = run([
[0, ev.call("t1", "t1#0", "a")],
[1_000, ev.stepComplete("t1", "t1#0")],
]);
expect(run([[2_000, ev.usage("t1", "t1#0")]], base).running?.kind).toBe("step");
expect(run([[3_000, ev.usage("t1", "t1#1")]], base).stepStamps.get("t1#0")).toBe(2_000);
expect(run([[4_000, ev.call("t1", "t1#1", "z")]], base).stepStamps.get("t1#0")).toBe(3_000);
});
it("discards a step with no tool calls (the final step of a turn)", () => {
const s = run([
[1_000, ev.stepComplete("t1", "t1#0")],
[1_050, ev.done("t1")],
]);
expect(s.stepStamps.size).toBe(0);
expect(s.running).toEqual({ kind: "turn", turnId: "t1", startedAt: 1_050 });
});
it("stamps a step whose tools were still running when the turn ended (stopped)", () => {
const s = run([
[0, ev.call("t1", "t1#0", "a")],
[1_000, ev.stepComplete("t1", "t1#0")],
[21_000, ev.done("t1")],
]);
expect(s.stepStamps.get("t1#0")).toBe(20_000);
expect(s.running?.kind).toBe("turn");
});
});
describe("foldCacheTimerEvent — turn gaps", () => {
it("starts at done and is stamped on that turn when the next turn starts", () => {
const s = run([
[1_000, ev.done("t1")],
[91_000, ev.userMessage("t2")],
[91_500, ev.turnStart("t2")],
]);
expect(s.running).toBeNull();
expect(s.turnStamps.get("t1")).toBe(90_000);
});
it("an error also ends the turn", () => {
expect(run([[7, ev.error("t1")]]).running).toEqual({
kind: "turn",
turnId: "t1",
startedAt: 7,
});
});
it("stopTurnGap stamps on a local send", () => {
const s = stopTurnGap(run([[1_000, ev.done("t1")]]), 4_000, noTurn);
expect(s.running).toBeNull();
expect(s.turnStamps.get("t1")).toBe(3_000);
});
it("stopTurnGap is a no-op while a step gap runs", () => {
const base = run([
[0, ev.call("t1", "t1#0", "a")],
[1_000, ev.stepComplete("t1", "t1#0")],
]);
expect(stopTurnGap(base, 2_000, noTurn)).toBe(base);
});
});
describe("seedTurnGap", () => {
it("seeds a turn gap with no turnId, stamped on the most recent turn when stopped", () => {
const seeded = seedTurnGap(initialCacheTimerState(), 1_000);
expect(seeded.running).toEqual({ kind: "turn", turnId: null, startedAt: 1_000 });
const s = stopTurnGap(seeded, 61_000, () => "t9");
expect(s.turnStamps.get("t9")).toBe(60_000);
});
it("drops a seeded gap when the most recent turn is unknown", () => {
const s = stopTurnGap(seedTurnGap(initialCacheTimerState(), 1_000), 2_000, noTurn);
expect(s.running).toBeNull();
expect(s.turnStamps.size).toBe(0);
});
it("keeps the later start, never overrides a step gap, ignores non-finite", () => {
const live = run([[5_000, ev.done("t1")]]);
expect(seedTurnGap(live, 4_000).running).toEqual({
kind: "turn",
turnId: "t1",
startedAt: 5_000,
});
expect(seedTurnGap(live, 6_000).running).toEqual({
kind: "turn",
turnId: "t1",
startedAt: 6_000,
});
const step = run([
[0, ev.call("t1", "t1#0", "a")],
[1_000, ev.stepComplete("t1", "t1#0")],
]);
expect(seedTurnGap(step, 9_000)).toBe(step);
expect(seedTurnGap(initialCacheTimerState(), Number.NaN).running).toBeNull();
});
});
describe("viewCacheTimer", () => {
it("formats m:ss up to 10 minutes", () => {
expect(viewCacheTimer(0).label).toBe("0:00");
expect(viewCacheTimer(9 * SEC + 999).label).toBe("0:09");
expect(viewCacheTimer(MIN + 5 * SEC).label).toBe("1:05");
expect(viewCacheTimer(5 * MIN + 30 * SEC).label).toBe("5:30");
expect(viewCacheTimer(9 * MIN + 59 * SEC).label).toBe("9:59");
expect(viewCacheTimer(9 * MIN + 59 * SEC).capped).toBe(false);
});
it("reads '10+ min' and is capped at and past 10 minutes", () => {
expect(viewCacheTimer(CACHE_TIMER_MAX_MS)).toEqual({
label: "10+ min",
level: "error",
capped: true,
});
expect(viewCacheTimer(3 * 60 * MIN).label).toBe("10+ min");
});
it("colours by the 5-minute cache TTL", () => {
expect(viewCacheTimer(3 * MIN + 59 * SEC).level).toBe("success");
expect(viewCacheTimer(4 * MIN).level).toBe("warning");
expect(viewCacheTimer(CACHE_TTL_MS).level).toBe("error");
expect(viewCacheTimer(7 * MIN).level).toBe("error");
});
it("clamps negative / non-finite elapsed (clock skew) to 0:00", () => {
expect(viewCacheTimer(-5 * SEC).label).toBe("0:00");
expect(viewCacheTimer(Number.NaN).label).toBe("0:00");
});
});
function userRow(text = "hi"): MetricsRow {
const group: RenderGroup = {
kind: "single",
chunk: { seq: null, role: "user", chunk: { type: "text", text }, provisional: true },
};
return { kind: "group", group };
}
function assistantRow(turnId: string): MetricsRow {
const group: RenderGroup = {
kind: "single",
chunk: {
seq: 1,
role: "assistant",
chunk: { type: "text", text: "ok" },
provisional: false,
turnId,
},
};
return { kind: "group", group };
}
function turnRow(turnId: string, turnNumber: number): MetricsRow {
const usage = { inputTokens: 10, outputTokens: 5 };
const turn: TurnMetrics = { turnId, usage, steps: [{ stepId: `${turnId}#0` as StepId, usage }] };
return {
kind: "turn-metrics",
turn,
turnNumber,
cumulativeUsage: usage,
prevLastStepUsage: null,
};
}
describe("lastTurnRowIndex", () => {
it("picks the LAST turn-metrics row", () => {
const rows = [
userRow(),
assistantRow("t1"),
turnRow("t1", 1),
userRow(),
assistantRow("t2"),
turnRow("t2", 2),
];
expect(lastTurnRowIndex(rows)).toBe(5);
});
it("is null once a user message follows the last turn", () => {
expect(lastTurnRowIndex([userRow(), turnRow("t1", 1), userRow("next")])).toBeNull();
});
it("skips trailing non-user rows after the turn row", () => {
expect(lastTurnRowIndex([userRow(), turnRow("t1", 1), assistantRow("t1")])).toBe(1);
});
it("is null with no turn-metrics rows", () => {
expect(lastTurnRowIndex([])).toBeNull();
expect(lastTurnRowIndex([userRow(), assistantRow("t1")])).toBeNull();
});
});
|