repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

fix(tps): ignore baseline stream chunk

52eced7b26b96d6abdcde4376742b424d2ee6699

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (3)

+49 / −16 text lines

Diff layout

extensions/tps/README.md

Modified

+1 / −1 text lines

2626- `session_start`: shows `󰓅 —/s`.
2727- `agent_start`: starts timing; shows `󰓅 0.0/s`.
2828- `message_start`: records assistant span start for non-streaming fallback.
29- `message_update`: starts decode timing on first non-empty streamed delta and estimates live TPS from streamed delta chars / 4.
29- `message_update`: starts decode timing on the first non-empty streamed delta, excludes that baseline chunk from TPS, and estimates live TPS from later streamed delta chars / 4.
3030- Live updates are throttled to every 250 ms.
3131- `message_end`: stops decode timing, or assistant-span fallback when no deltas streamed.
3232- `agent_end`: shows final `󰓅 N.N/s` from assistant output tokens / measured decode seconds.

extensions/tps/__tests__/tps.test.ts

Modified

+23 / −5 text lines

88}
1010describe("tps calculation", () => {
11 it("uses first streamed delta time, not provider start or whole agent wall time", () => {
11 it("uses first streamed delta as baseline, not numerator", () => {
1212 let now = 0;
1313 const run = new TpsRun(() => now);
1616 now = 1_000;
1717 run.startAssistant();
1818 now = 2_000;
19 run.addDelta(4);
19 run.addDelta(400);
2020 now = 3_000;
21 run.addDelta(4);
2122 run.endAssistant();
2223 now = 11_000;
24 expect(run.finish([assistant(100)])).toBe("100.0/s");
25 expect(run.finish([assistant(101)])).toBe("1.0/s");
2526 });
2728 it("sums multiple streamed spans and excludes tool gaps", () => {
3233 run.startAssistant();
3334 run.addDelta(4);
3435 now = 1_000;
36 run.addDelta(4);
3537 run.endAssistant();
3638 now = 5_000;
3739 run.startAssistant();
3840 run.addDelta(4);
3941 now = 6_000;
42 run.addDelta(4);
4043 run.endAssistant();
42 expect(run.finish([assistant(50), { role: "toolResult" }, assistant(50)])).toBe("50.0/s");
45 expect(run.finish([assistant(50), { role: "toolResult" }, assistant(50)])).toBe("25.0/s");
4346 });
4548 it("closes an open streamed span at finish", () => {
5154 now = 1_000;
5255 run.addDelta(4);
5356 now = 3_000;
57 run.addDelta(4);
55 expect(run.finish([assistant(40)])).toBe("20.0/s");
59 expect(run.finish([assistant(40)])).toBe("10.0/s");
60 });
62 it("falls back to assistant span when only one delta streams", () => {
63 let now = 0;
64 const run = new TpsRun(() => now);
66 run.startAgent();
67 run.startAssistant();
68 now = 1_000;
69 run.addDelta(400);
70 now = 3_000;
71 run.endAssistant();
73 expect(run.finish([assistant(40)])).toBe("13.3/s");
5674 });
5876 it("falls back to assistant span when no deltas stream", () => {

extensions/tps/index.ts

Modified

+25 / −10 text lines

6060class TpsRun {
6161 private assistantStartMs: number | null = null;
6262 private decodeStartMs: number | null = null;
63 private assistantElapsedMs = 0;
6364 private measuredElapsedMs = 0;
6465 private streamedChars = 0;
66 private measuredChars = 0;
67 private spanDeltaCount = 0;
6568 private lastLiveUpdateMs = 0;
6770 constructor(private readonly now: Clock = Date.now) {}
6972 startAgent(): void {
7073 this.assistantStartMs = null;
7174 this.decodeStartMs = null;
75 this.assistantElapsedMs = 0;
7276 this.measuredElapsedMs = 0;
7377 this.streamedChars = 0;
78 this.measuredChars = 0;
79 this.spanDeltaCount = 0;
7480 this.lastLiveUpdateMs = 0;
7581 }
7783 startAssistant(): void {
7884 if (this.assistantStartMs !== null) return;
7985 this.assistantStartMs = this.now();
86 this.decodeStartMs = null;
87 this.spanDeltaCount = 0;
8088 }
8290 endAssistant(): void {
8391 const now = this.now();
84 if (this.decodeStartMs !== null) {
85 this.measuredElapsedMs += Math.max(0, now - this.decodeStartMs);
86 } else if (this.assistantStartMs !== null) {
87 this.measuredElapsedMs += Math.max(0, now - this.assistantStartMs);
88 }
92 if (this.assistantStartMs !== null) this.assistantElapsedMs += Math.max(0, now - this.assistantStartMs);
93 if (this.decodeStartMs !== null && this.spanDeltaCount > 1) this.measuredElapsedMs += Math.max(0, now - this.decodeStartMs);
8994 this.assistantStartMs = null;
9095 this.decodeStartMs = null;
96 this.spanDeltaCount = 0;
9197 }
9399 addDelta(chars: number): void {
94100 const safeChars = Math.max(0, chars);
95101 if (safeChars === 0) return;
96102 if (this.assistantStartMs === null) this.startAssistant();
97 this.decodeStartMs ??= this.now();
98103 this.streamedChars += safeChars;
104 this.spanDeltaCount++;
105 if (this.spanDeltaCount === 1) {
106 this.decodeStartMs = this.now();
107 return;
108 }
109 this.measuredChars += safeChars;
99110 }
101112 shouldPublishLive(): boolean {
106117 }
108119 liveText(): string {
109 return liveText(this.streamedChars, this.currentMeasuredElapsedMs());
120 return liveText(this.measuredChars, this.currentMeasuredElapsedMs());
110121 }
112123 finish(messages: readonly unknown[]): string {
113124 this.endAssistant();
114 return finalText(sumAssistantOutput(messages), this.measuredElapsedMs);
125 const outputTokens = sumAssistantOutput(messages);
126 if (this.measuredChars > 0 && this.streamedChars > 0 && this.measuredElapsedMs > 0) {
127 return finalText(outputTokens * (this.measuredChars / this.streamedChars), this.measuredElapsedMs);
128 }
129 return finalText(outputTokens, this.assistantElapsedMs);
115130 }
117132 private currentMeasuredElapsedMs(): number {
118 if (this.decodeStartMs !== null) return this.measuredElapsedMs + Math.max(0, this.now() - this.decodeStartMs);
119 if (this.streamedChars === 0 && this.assistantStartMs !== null) return this.measuredElapsedMs + Math.max(0, this.now() - this.assistantStartMs);
133 if (this.decodeStartMs !== null && this.spanDeltaCount > 1) return this.measuredElapsedMs + Math.max(0, this.now() - this.decodeStartMs);
134 if (this.streamedChars === 0 && this.assistantStartMs !== null) return this.assistantElapsedMs + Math.max(0, this.now() - this.assistantStartMs);
120135 return this.measuredElapsedMs;
121136 }