pi-ext bugabingas pi extensions
owned by bugabinga
README Source History Refs Reviews Compare Notes Search Review guide fix(tps): ignore baseline stream chunk
52eced7b26b96d6abdcde4376742b424d2ee6699
Oliver Jan Krylow <oliver@bugabinga.net > · 25 May 2026
unsigned
Browse files Compare with HEAD
Changed files (3) Diff layout Auto Unified Split
extensions/tps/README.md Modified +1 / −1 text lines
26 26 - `session_start`: shows ` —/s`.- `session_start`: shows ` —/s`.27 27 - `agent_start`: starts timing; shows ` 0.0/s`.- `agent_start`: starts timing; shows ` 0.0/s`.28 28 - `message_start`: records assistant span start for non-streaming fallback.- `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.30 30 - Live updates are throttled to every 250 ms.- Live updates are throttled to every 250 ms.31 31 - `message_end`: stops decode timing, or assistant-span fallback when no deltas streamed.- `message_end`: stops decode timing, or assistant-span fallback when no deltas streamed.32 32 - `agent_end`: shows final ` N.N/s` from assistant output tokens / measured decode seconds.- `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
10 10 describe ("tps calculation" , () => {describe ("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" , () => {12 12 let now = 0 ; let now = 0 ;13 13 const run = new TpsRun (() => now); const run = new TpsRun (() => now);16 16 now = 1_000 ; now = 1_000 ;17 17 run.startAssistant (); run.startAssistant ();18 18 now = 2_000 ; now = 2_000 ;20 20 now = 3_000 ; now = 3_000 ;21 22 run.endAssistant (); run.endAssistant ();22 23 now = 11_000 ; now = 11_000 ;24 - expect (run.finish ([assistant (100 )])).toBe ("100 .0/s" );25 + expect (run.finish ([assistant (101 )])).toBe ("1 .0/s" );27 28 it ("sums multiple streamed spans and excludes tool gaps" , () => { it ("sums multiple streamed spans and excludes tool gaps" , () => {32 33 run.startAssistant (); run.startAssistant ();33 34 run.addDelta (4 ); run.addDelta (4 );34 35 now = 1_000 ; now = 1_000 ;35 37 run.endAssistant (); run.endAssistant ();36 38 now = 5_000 ; now = 5_000 ;37 39 run.startAssistant (); run.startAssistant ();38 40 run.addDelta (4 ); run.addDelta (4 );39 41 now = 6_000 ; now = 6_000 ;40 43 run.endAssistant (); 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" );45 48 it ("closes an open streamed span at finish" , () => { it ("closes an open streamed span at finish" , () => {51 54 now = 1_000 ; now = 1_000 ;52 55 run.addDelta (4 ); run.addDelta (4 );53 56 now = 3_000 ; now = 3_000 ;55 - expect (run.finish ([assistant (40 )])).toBe ("20.0/s" );59 + expect (run.finish ([assistant (40 )])).toBe ("10.0/s" );62 + it ("falls back to assistant span when only one delta streams" , () => {64 + const run = new TpsRun (() => now);67 + run.startAssistant ();73 + expect (run.finish ([assistant (40 )])).toBe ("13.3/s" );58 76 it ("falls back to assistant span when no deltas stream" , () => { it ("falls back to assistant span when no deltas stream" , () => {extensions/tps/index.ts Modified +25 / −10 text lines
60 60 class TpsRun {class TpsRun {61 61 private assistantStartMs: number | null = null ; private assistantStartMs: number | null = null ;62 62 private decodeStartMs: number | null = null ; private decodeStartMs: number | null = null ;
63 + private assistantElapsedMs = 0 ;63 64 private measuredElapsedMs = 0 ; private measuredElapsedMs = 0 ;64 65 private streamedChars = 0 ; private streamedChars = 0 ;
66 + private measuredChars = 0 ;67 + private spanDeltaCount = 0 ;65 68 private lastLiveUpdateMs = 0 ; private lastLiveUpdateMs = 0 ;67 70 constructor (private readonly now: Clock = Date .now) {} constructor (private readonly now: Clock = Date .now) {}69 72 startAgent (): void { startAgent (): void {70 73 this.assistantStartMs = null ; this.assistantStartMs = null ;71 74 this.decodeStartMs = null ; this.decodeStartMs = null ;
75 + this.assistantElapsedMs = 0 ;72 76 this.measuredElapsedMs = 0 ; this.measuredElapsedMs = 0 ;73 77 this.streamedChars = 0 ; this.streamedChars = 0 ;
78 + this.measuredChars = 0 ;79 + this.spanDeltaCount = 0 ;74 80 this.lastLiveUpdateMs = 0 ; this.lastLiveUpdateMs = 0 ;77 83 startAssistant (): void { startAssistant (): void {78 84 if (this.assistantStartMs !== null ) return ; if (this.assistantStartMs !== null ) return ;79 85 this.assistantStartMs = this.now (); this.assistantStartMs = this.now ();
86 + this.decodeStartMs = null ;87 + this.spanDeltaCount = 0 ;82 90 endAssistant (): void { endAssistant (): void {83 91 const now = this.now (); 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);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);89 94 this.assistantStartMs = null ; this.assistantStartMs = null ;90 95 this.decodeStartMs = null ; this.decodeStartMs = null ;
96 + this.spanDeltaCount = 0 ;93 99 addDelta (chars: number): void { addDelta (chars: number): void {94 100 const safeChars = Math .max (0 , chars); const safeChars = Math .max (0 , chars);95 101 if (safeChars === 0 ) return ; if (safeChars === 0 ) return ;96 102 if (this.assistantStartMs === null ) this.startAssistant (); if (this.assistantStartMs === null ) this.startAssistant ();97 - this.decodeStartMs ??= this.now ();
98 103 this.streamedChars += safeChars; this.streamedChars += safeChars;
104 + this.spanDeltaCount++ ;105 + if (this.spanDeltaCount === 1 ) {106 + this.decodeStartMs = this.now ();109 + this.measuredChars += safeChars;101 112 shouldPublishLive (): boolean { shouldPublishLive (): boolean {108 119 liveText (): string { liveText (): string {109 - return liveText (this.streamedChars , this.currentMeasuredElapsedMs ());120 + return liveText (this.measuredChars , this.currentMeasuredElapsedMs ());112 123 finish (messages: readonly unknown[]): string { finish (messages: readonly unknown[]): string {113 124 this.endAssistant (); 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);129 + return finalText (outputTokens, this.assistantElapsedMs);117 132 private currentMeasuredElapsedMs (): number { 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);120 135 return this.measuredElapsedMs; return this.measuredElapsedMs;