@ftrdotdev/pi-tui 0.8.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/CHANGELOG.md +225 -0
  2. package/README.md +5 -0
  3. package/package.json +18 -15
  4. package/src/activity-service.test.ts +97 -0
  5. package/src/activity.test.ts +1 -1
  6. package/src/activity.ts +38 -0
  7. package/src/chrome/budget.test.ts +345 -0
  8. package/src/chrome/budget.ts +220 -0
  9. package/src/chrome/declarations.ts +303 -0
  10. package/src/chrome/facts.test.ts +165 -0
  11. package/src/chrome/facts.ts +267 -0
  12. package/src/chrome/frame.ts +45 -0
  13. package/src/chrome/laws.test.ts +231 -0
  14. package/src/chrome/port.test.ts +164 -0
  15. package/src/chrome/port.ts +67 -0
  16. package/src/chrome/service-provider.test.ts +383 -0
  17. package/src/chrome/service-provider.ts +55 -0
  18. package/src/commands/git-execution.test.ts +5 -2
  19. package/src/commands/git-execution.ts +27 -15
  20. package/src/commands/git-status.test.ts +47 -15
  21. package/src/commands/git-status.ts +83 -31
  22. package/src/components/bordered-box.test.ts +12 -15
  23. package/src/components/bordered-box.ts +11 -7
  24. package/src/components/define-component.ts +1 -1
  25. package/src/components/index.ts +7 -0
  26. package/src/components/row-list.test.ts +310 -0
  27. package/src/components/row-list.ts +272 -0
  28. package/src/config.test.ts +118 -3
  29. package/src/config.ts +46 -40
  30. package/src/frame/box-frame.test.ts +53 -7
  31. package/src/harness-policy.test.ts +47 -0
  32. package/src/icons.test.ts +19 -0
  33. package/src/icons.ts +50 -31
  34. package/src/index.test.ts +135 -0
  35. package/src/index.ts +118 -119
  36. package/src/layers.test.ts +94 -0
  37. package/src/layers.ts +92 -0
  38. package/src/runtime-service.test.ts +218 -0
  39. package/src/runtime-service.ts +103 -0
  40. package/src/runtime.test.ts +165 -66
  41. package/src/runtime.ts +127 -93
  42. package/src/session-lifecycle-service.test.ts +95 -0
  43. package/src/session-lifecycle.test.ts +77 -0
  44. package/src/session-lifecycle.ts +21 -0
  45. package/src/state.test.ts +132 -3
  46. package/src/state.ts +43 -13
  47. package/src/telemetry-service.test.ts +250 -0
  48. package/src/telemetry.test.ts +322 -0
  49. package/src/telemetry.ts +130 -23
  50. package/src/ui/editor-tint.test.ts +58 -45
  51. package/src/ui/editor-tint.ts +1 -1
  52. package/src/ui/editor.ts +117 -53
  53. package/src/ui/footer.test.ts +349 -120
  54. package/src/ui/footer.ts +147 -124
  55. package/src/ui/header.test.ts +203 -223
  56. package/src/ui/header.ts +32 -96
  57. package/src/ui/preview.test.ts +73 -70
  58. package/src/ui/preview.ts +48 -33
  59. package/src/ui/render-helpers.test.ts +82 -13
  60. package/src/ui/render-helpers.ts +110 -122
  61. package/src/ui/settings-command.test.ts +70 -11
  62. package/src/ui/settings-command.ts +30 -45
  63. package/src/ui/theme-color.test.ts +4 -3
  64. package/src/usage-totals-service.test.ts +230 -0
  65. package/src/usage-totals-service.ts +59 -0
  66. package/src/utils.test.ts +46 -50
  67. package/src/utils.ts +41 -239
  68. package/src/vim/editor-host.test.ts +201 -75
  69. package/src/vim/editor-state.ts +2 -2
  70. package/src/vim/mode-indicator.test.ts +6 -5
  71. package/src/vim/mode-indicator.ts +13 -9
  72. package/src/vim/selection-render.ts +8 -4
  73. package/src/vim/text-model.test.ts +52 -14
  74. package/src/vim/text-model.ts +1 -1
  75. package/src/vim/vim-router.test.ts +3 -0
  76. package/src/vim/vim-router.ts +1 -1
@@ -0,0 +1,250 @@
1
+ // Each test is its own entry point: it builds a fresh layer for one scenario and
2
+ // runs its effects inside it, so providing there is the pattern the rule allows.
3
+ /* oxlint-disable effecttsgo/strict-effect-provide */
4
+ import { assert, it } from "@effect/vitest";
5
+ import { Effect } from "effect";
6
+ import { TurnTelemetryService, type TurnTelemetry } from "./telemetry.ts";
7
+
8
+ // ---------------------------------------------------------------------------
9
+ // TurnTelemetryService — the same frozen measurements as the plain tracker
10
+ // (telemetry.test.ts), plus the session-boundary reset (D6).
11
+ // ---------------------------------------------------------------------------
12
+
13
+ const EXPECTED_TURN: TurnTelemetry = {
14
+ tps: 142.9,
15
+ ttftMs: 300,
16
+ totalMs: 2900,
17
+ inputTokens: 1200,
18
+ outputTokens: 400,
19
+ stallMs: 2000,
20
+ stallCount: 1,
21
+ rateUsdPerMTokens: 6.9,
22
+ generationMs: 2800,
23
+ totalTokens: 1740,
24
+ costUsd: 0.012,
25
+ measurementMs: 2800,
26
+ };
27
+
28
+ const EXPECTED_AGENT: TurnTelemetry = {
29
+ tps: 128.2,
30
+ ttftMs: 300,
31
+ totalMs: 5000,
32
+ inputTokens: 1500,
33
+ outputTokens: 500,
34
+ stallMs: 2000,
35
+ stallCount: 1,
36
+ rateUsdPerMTokens: 6.7,
37
+ generationMs: 3900,
38
+ totalTokens: 2240,
39
+ costUsd: 0.015,
40
+ measurementMs: 3900,
41
+ };
42
+
43
+ /**
44
+ * pi's event payloads carry fields the tracker never reads (turn indexes,
45
+ * entry ids, tool results). One documented cast keeps each script to the
46
+ * fields under test, as test-support/doubles.ts does for pi types.
47
+ */
48
+ const event = <T>(value: unknown): T => value as T;
49
+
50
+ interface ScriptedUsage {
51
+ input: number;
52
+ output: number;
53
+ totalTokens: number;
54
+ cost: number;
55
+ }
56
+
57
+ /** The assistant-message fields the tracker reads, as pi shapes them. */
58
+ const assistant = (usage: ScriptedUsage) => ({
59
+ role: "assistant",
60
+ usage: { ...usage, cost: { total: usage.cost } },
61
+ });
62
+
63
+ type TelemetryInput = Parameters<TurnTelemetryService["Service"]["handle"]>[0];
64
+ type Send = (instant: number, input: TelemetryInput) => TurnTelemetry | undefined;
65
+
66
+ /** Resolves the service over the test's mutable clock. */
67
+ const serviceOver = (clock: () => number) =>
68
+ Effect.service(TurnTelemetryService).pipe(Effect.provide(TurnTelemetryService.layerTest(clock)));
69
+
70
+ /** The first scripted turn: one assistant message with a 2000 ms stall. */
71
+ const scriptFirstTurn = (send: Send): TurnTelemetry | undefined => {
72
+ const message = assistant({ input: 1200, output: 400, totalTokens: 1740, cost: 0.012 });
73
+ send(0, event({ type: "agent_start" }));
74
+ send(100, event({ type: "turn_start" }));
75
+ send(100, event({ type: "message_start", message }));
76
+ send(
77
+ 400,
78
+ event({
79
+ type: "message_update",
80
+ message,
81
+ assistantMessageEvent: { type: "text_delta", delta: "Hello" },
82
+ }),
83
+ );
84
+ send(
85
+ 2400,
86
+ event({
87
+ type: "message_update",
88
+ message,
89
+ assistantMessageEvent: { type: "text_delta", delta: " world" },
90
+ }),
91
+ );
92
+ send(2900, event({ type: "message_end", message }));
93
+ return send(3000, event({ type: "turn_end" }));
94
+ };
95
+
96
+ it.effect("a scripted turn yields the frozen measurement", () => {
97
+ let now = 0;
98
+ return Effect.gen(function* () {
99
+ // Arrange
100
+ const svc = yield* serviceOver(() => now);
101
+ const send: Send = (instant, input) => {
102
+ now = instant;
103
+ return svc.handle(input);
104
+ };
105
+
106
+ // Act
107
+ const measured = scriptFirstTurn(send);
108
+
109
+ // Assert
110
+ assert.deepStrictEqual(measured, EXPECTED_TURN);
111
+ });
112
+ });
113
+
114
+ it.effect("a two-turn agent settles into one aggregate measurement", () => {
115
+ let now = 0;
116
+ return Effect.gen(function* () {
117
+ // Arrange
118
+ const svc = yield* serviceOver(() => now);
119
+ const send: Send = (instant, input) => {
120
+ now = instant;
121
+ return svc.handle(input);
122
+ };
123
+ scriptFirstTurn(send);
124
+ const message = assistant({ input: 300, output: 100, totalTokens: 500, cost: 0.003 });
125
+ send(3000, event({ type: "turn_start" }));
126
+ send(3000, event({ type: "message_start", message }));
127
+ send(
128
+ 3100,
129
+ event({
130
+ type: "message_update",
131
+ message,
132
+ assistantMessageEvent: { type: "text_delta", delta: "second" },
133
+ }),
134
+ );
135
+ send(4100, event({ type: "message_end", message }));
136
+ send(4200, event({ type: "turn_end" }));
137
+
138
+ // Act
139
+ const measured = send(5000, event({ type: "agent_settled" }));
140
+
141
+ // Assert
142
+ assert.deepStrictEqual(measured, EXPECTED_AGENT);
143
+ });
144
+ });
145
+
146
+ it.effect("a turn with no assistant output is not measured", () => {
147
+ let now = 0;
148
+ return Effect.gen(function* () {
149
+ // Arrange
150
+ const svc = yield* serviceOver(() => now);
151
+ const send: Send = (instant, input) => {
152
+ now = instant;
153
+ return svc.handle(input);
154
+ };
155
+ send(0, event({ type: "agent_start" }));
156
+ send(100, event({ type: "turn_start" }));
157
+
158
+ // Act/Assert: neither boundary invents a measurement.
159
+ assert.strictEqual(send(200, event({ type: "turn_end" })), undefined);
160
+ assert.strictEqual(send(300, event({ type: "agent_settled" })), undefined);
161
+ });
162
+ });
163
+
164
+ it.effect("a stall shorter than the threshold is not counted", () => {
165
+ let now = 0;
166
+ return Effect.gen(function* () {
167
+ // Arrange: the second update arrives 900 ms after the first.
168
+ const svc = yield* serviceOver(() => now);
169
+ const send: Send = (instant, input) => {
170
+ now = instant;
171
+ return svc.handle(input);
172
+ };
173
+ const message = assistant({ input: 1200, output: 400, totalTokens: 1740, cost: 0.012 });
174
+ send(0, event({ type: "agent_start" }));
175
+ send(100, event({ type: "turn_start" }));
176
+ send(100, event({ type: "message_start", message }));
177
+ send(
178
+ 400,
179
+ event({
180
+ type: "message_update",
181
+ message,
182
+ assistantMessageEvent: { type: "text_delta", delta: "Hello" },
183
+ }),
184
+ );
185
+ send(
186
+ 1300,
187
+ event({
188
+ type: "message_update",
189
+ message,
190
+ assistantMessageEvent: { type: "text_delta", delta: " world" },
191
+ }),
192
+ );
193
+ send(2900, event({ type: "message_end", message }));
194
+
195
+ // Act
196
+ const measured = send(3000, event({ type: "turn_end" }));
197
+
198
+ // Assert: the turn is still measured, without a stall.
199
+ assert.strictEqual(measured?.stallCount, 0);
200
+ assert.strictEqual(measured?.stallMs, 0);
201
+ assert.ok(measured !== undefined && Number.isFinite(measured.tps));
202
+ });
203
+ });
204
+
205
+ it.effect("a session boundary drops an open agent window (D6)", () => {
206
+ let now = 0;
207
+ return Effect.gen(function* () {
208
+ // Arrange: one collected turn inside an open agent window.
209
+ const svc = yield* serviceOver(() => now);
210
+ const send: Send = (instant, input) => {
211
+ now = instant;
212
+ return svc.handle(input);
213
+ };
214
+ scriptFirstTurn(send);
215
+
216
+ // Act: the session boundary resets, then a second session runs one turn.
217
+ svc.reset();
218
+ const message = assistant({ input: 10, output: 5, totalTokens: 15, cost: 0.001 });
219
+ send(10_000, event({ type: "agent_start" }));
220
+ send(10_000, event({ type: "turn_start" }));
221
+ send(10_000, event({ type: "message_start", message }));
222
+ send(
223
+ 10_100,
224
+ event({
225
+ type: "message_update",
226
+ message,
227
+ assistantMessageEvent: { type: "text_delta", delta: "again" },
228
+ }),
229
+ );
230
+ send(10_300, event({ type: "message_end", message }));
231
+ send(10_400, event({ type: "turn_end" }));
232
+ const measured = send(10_500, event({ type: "agent_settled" }));
233
+
234
+ // Assert: the settled measurement covers only the second session.
235
+ assert.deepStrictEqual(measured, {
236
+ tps: 16.7,
237
+ ttftMs: 100,
238
+ totalMs: 500,
239
+ inputTokens: 10,
240
+ outputTokens: 5,
241
+ stallMs: 0,
242
+ stallCount: 0,
243
+ rateUsdPerMTokens: 66.67,
244
+ generationMs: 300,
245
+ totalTokens: 15,
246
+ costUsd: 0.001,
247
+ measurementMs: 300,
248
+ });
249
+ });
250
+ });
@@ -0,0 +1,322 @@
1
+ import { assert, it } from "@effect/vitest";
2
+ import { themeDouble } from "@ftrdotdev/pi-test-kit";
3
+ import { stubChromePort } from "../test-support/doubles.ts";
4
+ import type { TelemetryConfig } from "./config.ts";
5
+ import { resolveGlyphs } from "./icons.ts";
6
+ import {
7
+ composeNotifyLine,
8
+ planNotify,
9
+ TurnTelemetryTracker,
10
+ type TurnTelemetry,
11
+ } from "./telemetry.ts";
12
+
13
+ // Identity theme: styling is a no-op so assertions read the plain text.
14
+ const theme = themeDouble();
15
+
16
+ /** Every turn-scoped quantity non-zero, so no field is hidden by its value. */
17
+ const telemetry: TurnTelemetry = {
18
+ tps: 42.3,
19
+ ttftMs: 400,
20
+ totalMs: 1500,
21
+ inputTokens: 1200,
22
+ outputTokens: 400,
23
+ stallMs: 3100,
24
+ stallCount: 2,
25
+ rateUsdPerMTokens: 3.5,
26
+ generationMs: 1200,
27
+ totalTokens: 1600,
28
+ costUsd: 0.0056,
29
+ measurementMs: 1200,
30
+ };
31
+
32
+ /** The thinned default: TPS, TTFT and stalls only. */
33
+ const defaultNotifyConfig: TelemetryConfig = {
34
+ enabled: true,
35
+ tps: true,
36
+ ttft: true,
37
+ duration: false,
38
+ tokens: false,
39
+ stalls: true,
40
+ cost: false,
41
+ };
42
+
43
+ const allOnNotifyConfig: TelemetryConfig = {
44
+ enabled: true,
45
+ tps: true,
46
+ ttft: true,
47
+ duration: true,
48
+ tokens: true,
49
+ stalls: true,
50
+ cost: true,
51
+ };
52
+
53
+ const notify = (config: TelemetryConfig, width: number) =>
54
+ composeNotifyLine(telemetry, { theme, config, iconMode: "ascii", width });
55
+
56
+ /** The first pinned notify table: 30, 80 and 120 columns under the defaults. */
57
+ const NOTIFY_EXPECTED: Record<number, string> = {
58
+ 30: "» TPS 42.3 tok/s",
59
+ 80: "» TPS 42.3 tok/s | ◷ TTFT 0.4s | ⊘ stall 2x / 3.1s",
60
+ 120: "» TPS 42.3 tok/s | ◷ TTFT 0.4s | ⊘ stall 2x / 3.1s",
61
+ };
62
+
63
+ /** A port whose only meaningful fact for the notify line is its width. */
64
+ const stubPort = (width: number) => stubChromePort({ notifyWidth: () => width });
65
+
66
+ it("the default notify line keeps three fields and omits duration, tokens and cost", () => {
67
+ // Act: compose at 80 under the thinned defaults.
68
+ const line = notify(defaultNotifyConfig, 80);
69
+
70
+ // Assert: exactly the three turn fields, in declaration order.
71
+ assert.deepStrictEqual(line.split(" | "), [
72
+ "» TPS 42.3 tok/s",
73
+ "◷ TTFT 0.4s",
74
+ "⊘ stall 2x / 3.1s",
75
+ ]);
76
+ assert.ok(!line.includes("$"));
77
+ assert.ok(!line.includes("↑"));
78
+ });
79
+
80
+ it("the notify line degrades by priority at narrow widths", () => {
81
+ // Act: compose at 22, below the narrow threshold.
82
+ const line = notify(defaultNotifyConfig, 22);
83
+
84
+ // Assert: TPS survives, TTFT and the stall chip yield, and the line fits.
85
+ assert.ok(line.includes("TPS"));
86
+ assert.ok(!line.includes("TTFT"));
87
+ assert.ok(!line.includes("stall"));
88
+ assert.ok(line.length <= 22);
89
+ });
90
+
91
+ it("the declared ceiling is the default set, not a cap on configuration", () => {
92
+ // Act: plan under the default and the all-on configuration.
93
+ const glyphs = resolveGlyphs("ascii");
94
+ const defaultDecisions = planNotify(telemetry, defaultNotifyConfig, 120, glyphs, theme);
95
+ const allOnDecisions = planNotify(telemetry, allOnNotifyConfig, 120, glyphs, theme);
96
+ const kept = (decisions: readonly { action: string }[]) =>
97
+ decisions.filter((decision) => decision.action !== "drop").length;
98
+
99
+ // Assert: the default set is the ceiling; a user-enabled extra still shows.
100
+ assert.strictEqual(kept(defaultDecisions), 3);
101
+ assert.strictEqual(kept(allOnDecisions), 6);
102
+ });
103
+
104
+ it("styles the notify separators with the theme's dim token", () => {
105
+ // Arrange: a marker theme so the dim styling is visible in the string.
106
+ const marker = themeDouble({ fg: (color, text) => `${color}:${text}` });
107
+
108
+ // Act: compose the default three fields at 80.
109
+ const line = composeNotifyLine(telemetry, {
110
+ theme: marker,
111
+ config: defaultNotifyConfig,
112
+ iconMode: "ascii",
113
+ width: 80,
114
+ });
115
+
116
+ // Assert: the field separators carry the dim token.
117
+ assert.ok(line.includes("dim: | "));
118
+ });
119
+
120
+ it("the notify width comes from the provider, not from the compositor", () => {
121
+ // Arrange: a provider stating 24 columns; the default fields need more.
122
+ const port = stubPort(24);
123
+
124
+ // Act: compose with the provider's width and compare with a wide render.
125
+ const narrow = notify(defaultNotifyConfig, port.facts.notifyWidth());
126
+ const wide = notify(defaultNotifyConfig, 120);
127
+
128
+ // Assert: the stub's width bounds the line and fewer fields survive.
129
+ assert.ok(narrow.length <= 24);
130
+ assert.ok(narrow.split(" | ").length < wide.split(" | ").length);
131
+ });
132
+
133
+ it("the notify table is frozen at 30/80/120 and encodes the default three fields", () => {
134
+ // Act/Assert: the pinned rows under the thinned default.
135
+ for (const width of [30, 80, 120] as const) {
136
+ assert.strictEqual(notify(defaultNotifyConfig, width), NOTIFY_EXPECTED[width]);
137
+ }
138
+ assert.strictEqual(NOTIFY_EXPECTED[80]!.split(" | ").length, 3);
139
+ });
140
+
141
+ it("the notify line is suppressed when its surface switch is off", () => {
142
+ // Act/Assert: the surface switch is the only thing that empties the line.
143
+ assert.strictEqual(notify({ ...defaultNotifyConfig, enabled: false }, 80), "");
144
+ });
145
+
146
+ // ---------------------------------------------------------------------------
147
+ // TurnTelemetryTracker — characterisation of the per-turn and per-agent
148
+ // measurements (D1). The clock is the test's input: every scripted event
149
+ // carries its instant, so each table is exact and deterministic.
150
+ // ---------------------------------------------------------------------------
151
+
152
+ /** The frozen measurement of the single scripted turn below. */
153
+ const EXPECTED_TURN: TurnTelemetry = {
154
+ tps: 142.9,
155
+ ttftMs: 300,
156
+ totalMs: 2900,
157
+ inputTokens: 1200,
158
+ outputTokens: 400,
159
+ stallMs: 2000,
160
+ stallCount: 1,
161
+ rateUsdPerMTokens: 6.9,
162
+ generationMs: 2800,
163
+ totalTokens: 1740,
164
+ costUsd: 0.012,
165
+ measurementMs: 2800,
166
+ };
167
+
168
+ /** The frozen aggregate of the two-turn agent below. */
169
+ const EXPECTED_AGENT: TurnTelemetry = {
170
+ tps: 128.2,
171
+ ttftMs: 300,
172
+ totalMs: 5000,
173
+ inputTokens: 1500,
174
+ outputTokens: 500,
175
+ stallMs: 2000,
176
+ stallCount: 1,
177
+ rateUsdPerMTokens: 6.7,
178
+ generationMs: 3900,
179
+ totalTokens: 2240,
180
+ costUsd: 0.015,
181
+ measurementMs: 3900,
182
+ };
183
+
184
+ /**
185
+ * pi's event payloads carry fields the tracker never reads (turn indexes,
186
+ * entry ids, tool results). One documented cast keeps each script to the
187
+ * fields under test, as test-support/doubles.ts does for pi types.
188
+ */
189
+ const event = <T>(value: unknown): T => value as T;
190
+
191
+ interface ScriptedUsage {
192
+ input: number;
193
+ output: number;
194
+ totalTokens: number;
195
+ cost: number;
196
+ }
197
+
198
+ /** The assistant-message fields the tracker reads, as pi shapes them. */
199
+ const assistant = (usage: ScriptedUsage) => ({
200
+ role: "assistant",
201
+ usage: { ...usage, cost: { total: usage.cost } },
202
+ });
203
+
204
+ type TelemetryInput = Parameters<TurnTelemetryTracker["handle"]>[0];
205
+ type Send = (instant: number, input: TelemetryInput) => TurnTelemetry | undefined;
206
+
207
+ /** The first scripted turn: one assistant message with a 2000 ms stall. */
208
+ const scriptFirstTurn = (send: Send): TurnTelemetry | undefined => {
209
+ const message = assistant({ input: 1200, output: 400, totalTokens: 1740, cost: 0.012 });
210
+ send(0, event({ type: "agent_start" }));
211
+ send(100, event({ type: "turn_start" }));
212
+ send(100, event({ type: "message_start", message }));
213
+ send(
214
+ 400,
215
+ event({
216
+ type: "message_update",
217
+ message,
218
+ assistantMessageEvent: { type: "text_delta", delta: "Hello" },
219
+ }),
220
+ );
221
+ send(
222
+ 2400,
223
+ event({
224
+ type: "message_update",
225
+ message,
226
+ assistantMessageEvent: { type: "text_delta", delta: " world" },
227
+ }),
228
+ );
229
+ send(2900, event({ type: "message_end", message }));
230
+ return send(3000, event({ type: "turn_end" }));
231
+ };
232
+
233
+ const trackerScript = () => {
234
+ let now = 0;
235
+ const tracker = new TurnTelemetryTracker(() => now);
236
+ const send: Send = (instant, input) => {
237
+ now = instant;
238
+ return tracker.handle(input);
239
+ };
240
+ return send;
241
+ };
242
+
243
+ it("a scripted turn yields the frozen measurement", () => {
244
+ // Arrange: the mutable clock is the input; the script advances it.
245
+ const send = trackerScript();
246
+
247
+ // Act
248
+ const measured = scriptFirstTurn(send);
249
+
250
+ // Assert
251
+ assert.deepStrictEqual(measured, EXPECTED_TURN);
252
+ });
253
+
254
+ it("a two-turn agent settles into one aggregate measurement", () => {
255
+ // Arrange: the first turn, then a second whose tokens differ.
256
+ const send = trackerScript();
257
+ scriptFirstTurn(send);
258
+ const message = assistant({ input: 300, output: 100, totalTokens: 500, cost: 0.003 });
259
+ send(3000, event({ type: "turn_start" }));
260
+ send(3000, event({ type: "message_start", message }));
261
+ send(
262
+ 3100,
263
+ event({
264
+ type: "message_update",
265
+ message,
266
+ assistantMessageEvent: { type: "text_delta", delta: "second" },
267
+ }),
268
+ );
269
+ send(4100, event({ type: "message_end", message }));
270
+ send(4200, event({ type: "turn_end" }));
271
+
272
+ // Act
273
+ const measured = send(5000, event({ type: "agent_settled" }));
274
+
275
+ // Assert
276
+ assert.deepStrictEqual(measured, EXPECTED_AGENT);
277
+ });
278
+
279
+ it("a turn with no assistant output is not measured", () => {
280
+ // Arrange
281
+ const send = trackerScript();
282
+ send(0, event({ type: "agent_start" }));
283
+ send(100, event({ type: "turn_start" }));
284
+
285
+ // Act/Assert: neither boundary invents a measurement.
286
+ assert.strictEqual(send(200, event({ type: "turn_end" })), undefined);
287
+ assert.strictEqual(send(300, event({ type: "agent_settled" })), undefined);
288
+ });
289
+
290
+ it("a stall shorter than the threshold is not counted", () => {
291
+ // Arrange: the second update arrives 900 ms after the first.
292
+ const send = trackerScript();
293
+ const message = assistant({ input: 1200, output: 400, totalTokens: 1740, cost: 0.012 });
294
+ send(0, event({ type: "agent_start" }));
295
+ send(100, event({ type: "turn_start" }));
296
+ send(100, event({ type: "message_start", message }));
297
+ send(
298
+ 400,
299
+ event({
300
+ type: "message_update",
301
+ message,
302
+ assistantMessageEvent: { type: "text_delta", delta: "Hello" },
303
+ }),
304
+ );
305
+ send(
306
+ 1300,
307
+ event({
308
+ type: "message_update",
309
+ message,
310
+ assistantMessageEvent: { type: "text_delta", delta: " world" },
311
+ }),
312
+ );
313
+ send(2900, event({ type: "message_end", message }));
314
+
315
+ // Act
316
+ const measured = send(3000, event({ type: "turn_end" }));
317
+
318
+ // Assert: the turn is still measured, without a stall.
319
+ assert.strictEqual(measured?.stallCount, 0);
320
+ assert.strictEqual(measured?.stallMs, 0);
321
+ assert.ok(measured !== undefined && Number.isFinite(measured.tps));
322
+ });