pi-ext
bugabingas pi extensions
owned by bugabinga
fix(tps): start decode timing on first delta
1bb29cc212122a99189d5b1a5909e3b16b9c53e1
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
Browse filesCompare with HEAD
Changed files (3)
extensions/tps/README.md
Modified+4 / −4 text lines
2626- `session_start`: shows ` —/s`.- `session_start`: shows ` —/s`. 2727- `agent_start`: starts timing; shows ` 0.0/s`.- `agent_start`: starts timing; shows ` 0.0/s`. 28-- `message_start`: starts assistant-generation timing. 29-- `message_update`: estimates live TPS from streamed delta chars / 4. 28+- `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. 3030- Live updates are throttled to every 250 ms.- Live updates are throttled to every 250 ms. 31-- `message_end`: stops assistant-generation timing. 32-- `agent_end`: shows final ` N.N/s` from assistant output tokens / assistant-generation seconds. 31+- `message_end`: stops decode timing, or assistant-span fallback when no deltas streamed. 32+- `agent_end`: shows final ` N.N/s` from assistant output tokens / measured decode seconds. 3333- No assistant output token usage → returns to ` —/s`.- No assistant output token usage → returns to ` —/s`. 3434- Shared footer present → emits `footer:segment` in `llm` zone.- Shared footer present → emits `footer:segment` in `llm` zone. 3535- No shared footer → falls back to `ctx.ui.setStatus("tps", ...)`.- No shared footer → falls back to `ctx.ui.setStatus("tps", ...)`. extensions/tps/__tests__/tps.test.ts
Modified+22 / −4 text lines
1010describe("tps calculation", () => {describe("tps calculation", () => { 11- it("uses assistant generation time, not whole agent wall time", () => { 11+ it("uses first streamed delta time, not provider start or whole agent wall time", () => { 1212 let now = 0; let now = 0; 1313 const run = new TpsRun(() => now); const run = new TpsRun(() => now); 1616 now = 1_000; now = 1_000; 1717 run.startAssistant(); run.startAssistant(); 1818 now = 2_000; now = 2_000; 1921 run.endAssistant(); run.endAssistant(); 2022 now = 11_000; now = 11_000; 2224 expect(run.finish([assistant(100)])).toBe("100.0/s"); expect(run.finish([assistant(100)])).toBe("100.0/s"); 25- it("sums multiple assistant spans and excludes tool gaps", () => { 27+ it("sums multiple streamed spans and excludes tool gaps", () => { 2628 let now = 0; let now = 0; 2729 const run = new TpsRun(() => now); const run = new TpsRun(() => now); 2931 run.startAgent(); run.startAgent(); 3032 run.startAssistant(); run.startAssistant(); 3134 now = 1_000; now = 1_000; 3235 run.endAssistant(); run.endAssistant(); 3336 now = 5_000; now = 5_000; 3437 run.startAssistant(); run.startAssistant(); 3539 now = 6_000; now = 6_000; 3640 run.endAssistant(); run.endAssistant(); 3842 expect(run.finish([assistant(50), { role: "toolResult" }, assistant(50)])).toBe("50.0/s"); expect(run.finish([assistant(50), { role: "toolResult" }, assistant(50)])).toBe("50.0/s"); 41- it("closes an open assistant span at finish", () => { 45+ it("closes an open streamed span at finish", () => { 47+ const run = new TpsRun(() => now); 50+ run.startAssistant(); 55+ expect(run.finish([assistant(40)])).toBe("20.0/s"); 58+ it("falls back to assistant span when no deltas stream", () => { 4259 let now = 0; let now = 0; 4360 const run = new TpsRun(() => now); const run = new TpsRun(() => now); 4562 run.startAgent(); run.startAgent(); 4663 run.startAssistant(); run.startAssistant(); 4764 now = 2_000; now = 2_000; 4967 expect(run.finish([assistant(40)])).toBe("20.0/s"); expect(run.finish([assistant(40)])).toBe("20.0/s"); 52- it("returns idle text without output tokens or assistant duration", () => { 70+ it("returns idle text without output tokens or measured duration", () => { 5371 expect(finalText(0, 1_000)).toBe("—/s"); expect(finalText(0, 1_000)).toBe("—/s"); 5472 expect(finalText(10, 0)).toBe("—/s"); expect(finalText(10, 0)).toBe("—/s"); extensions/tps/index.ts
Modified+22 / −9 text lines
6060class TpsRun {class TpsRun { 6161 private assistantStartMs: number | null = null; private assistantStartMs: number | null = null; 62- private assistantElapsedMs = 0; 62+ private decodeStartMs: number | null = null; 63+ private measuredElapsedMs = 0; 6364 private streamedChars = 0; private streamedChars = 0; 6465 private lastLiveUpdateMs = 0; private lastLiveUpdateMs = 0; 6869 startAgent(): void { startAgent(): void { 6970 this.assistantStartMs = null; this.assistantStartMs = null; 70- this.assistantElapsedMs = 0; 71+ this.decodeStartMs = null; 72+ this.measuredElapsedMs = 0; 7173 this.streamedChars = 0; this.streamedChars = 0; 7274 this.lastLiveUpdateMs = 0; this.lastLiveUpdateMs = 0; 8082 endAssistant(): void { endAssistant(): void { 81- if (this.assistantStartMs === null) return; 82- this.assistantElapsedMs += Math.max(0, this.now() - this.assistantStartMs); 83+ 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); 8389 this.assistantStartMs = null; this.assistantStartMs = null; 90+ this.decodeStartMs = null; 8693 addDelta(chars: number): void { addDelta(chars: number): void { 87- this.streamedChars += Math.max(0, chars); 94+ const safeChars = Math.max(0, chars); 95+ if (safeChars === 0) return; 96+ if (this.assistantStartMs === null) this.startAssistant(); 97+ this.decodeStartMs ??= this.now(); 98+ this.streamedChars += safeChars; 90101 shouldPublishLive(): boolean { shouldPublishLive(): boolean { 97108 liveText(): string { liveText(): string { 98- return liveText(this.streamedChars, this.currentAssistantElapsedMs()); 109+ return liveText(this.streamedChars, this.currentMeasuredElapsedMs()); 101112 finish(messages: readonly unknown[]): string { finish(messages: readonly unknown[]): string { 102113 this.endAssistant(); this.endAssistant(); 103- return finalText(sumAssistantOutput(messages), this.assistantElapsedMs); 114+ return finalText(sumAssistantOutput(messages), this.measuredElapsedMs); 106- private currentAssistantElapsedMs(): number { 107- return this.assistantElapsedMs + (this.assistantStartMs === null ? 0 : Math.max(0, this.now() - this.assistantStartMs)); 117+ 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); 120+ return this.measuredElapsedMs;