repositories / pi-ext

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)

+48 / −17 text lines

Diff layout

extensions/tps/README.md

Modified

+4 / −4 text lines

2626- `session_start`: shows `󰓅 —/s`.
2727- `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.
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`.
3434- Shared footer present → emits `footer:segment` in `llm` zone.
3535- No shared footer → falls back to `ctx.ui.setStatus("tps", ...)`.

extensions/tps/__tests__/tps.test.ts

Modified

+22 / −4 text lines

88}
1010describe("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;
1313 const run = new TpsRun(() => now);
1616 now = 1_000;
1717 run.startAssistant();
1818 now = 2_000;
19 run.addDelta(4);
20 now = 3_000;
1921 run.endAssistant();
2022 now = 11_000;
2224 expect(run.finish([assistant(100)])).toBe("100.0/s");
2325 });
25 it("sums multiple assistant spans and excludes tool gaps", () => {
27 it("sums multiple streamed spans and excludes tool gaps", () => {
2628 let now = 0;
2729 const run = new TpsRun(() => now);
2931 run.startAgent();
3032 run.startAssistant();
33 run.addDelta(4);
3134 now = 1_000;
3235 run.endAssistant();
3336 now = 5_000;
3437 run.startAssistant();
38 run.addDelta(4);
3539 now = 6_000;
3640 run.endAssistant();
3842 expect(run.finish([assistant(50), { role: "toolResult" }, assistant(50)])).toBe("50.0/s");
3943 });
41 it("closes an open assistant span at finish", () => {
45 it("closes an open streamed span at finish", () => {
46 let now = 0;
47 const run = new TpsRun(() => now);
49 run.startAgent();
50 run.startAssistant();
51 now = 1_000;
52 run.addDelta(4);
53 now = 3_000;
55 expect(run.finish([assistant(40)])).toBe("20.0/s");
56 });
58 it("falls back to assistant span when no deltas stream", () => {
4259 let now = 0;
4360 const run = new TpsRun(() => now);
4562 run.startAgent();
4663 run.startAssistant();
4764 now = 2_000;
65 run.endAssistant();
4967 expect(run.finish([assistant(40)])).toBe("20.0/s");
5068 });
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");
5472 expect(finalText(10, 0)).toBe("—/s");
5573 });

extensions/tps/index.ts

Modified

+22 / −9 text lines

6060class TpsRun {
6161 private assistantStartMs: number | null = null;
62 private assistantElapsedMs = 0;
62 private decodeStartMs: number | null = null;
63 private measuredElapsedMs = 0;
6364 private streamedChars = 0;
6465 private lastLiveUpdateMs = 0;
6869 startAgent(): void {
6970 this.assistantStartMs = null;
70 this.assistantElapsedMs = 0;
71 this.decodeStartMs = null;
72 this.measuredElapsedMs = 0;
7173 this.streamedChars = 0;
7274 this.lastLiveUpdateMs = 0;
7375 }
7880 }
8082 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);
88 }
8389 this.assistantStartMs = null;
90 this.decodeStartMs = null;
8491 }
8693 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;
8899 }
90101 shouldPublishLive(): boolean {
95106 }
97108 liveText(): string {
98 return liveText(this.streamedChars, this.currentAssistantElapsedMs());
109 return liveText(this.streamedChars, this.currentMeasuredElapsedMs());
99110 }
101112 finish(messages: readonly unknown[]): string {
102113 this.endAssistant();
103 return finalText(sumAssistantOutput(messages), this.assistantElapsedMs);
114 return finalText(sumAssistantOutput(messages), this.measuredElapsedMs);
104115 }
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;
108121 }