pi-ext bugabingas pi extensions
owned by admin
README Source History Refs Compare Notes Search footer extension: trim useless fat
05f6232ededb75eb90bf9c70a1bc0f4010d2fd91
Oliver Krylow <o.krylow@isp-insoft.de > · 18 Sep 2026
SSH signature · not checked
Browse files Compare with HEAD
Changed files (11) 6 + - Declutter the status bar: show context usage as the bar alone, drop unreadable cache-read and cache-write totals in favour of the cache hit rate, scale token counts and costs through `k` and `M`, drop the provider name from the active quota segment that the model icon already identifies, and replace glyph labels with spelled-out status text outside TUI mode
6 8 - Recover Decay from partial classification with up to two gap-fill rounds over only the missing chunks, tolerate exact duplicate chunk records, and salvage a deterministic summary from ranked atoms when the summarizer fails instead of discarding the whole run to default compaction
7 9 - Keep Klaus models in sync with Pi's Anthropic catalog: inherit the human's model overrides, never inherit transport-owned fields, clamp an inherited context window or output maximum to the builtin catalog value the Agent SDK path can actually deliver, re-project the catalog at each turn boundary, and re-register the provider only when the projection changed
8 10 - Expose Klaus tools to Claude under their Pi names instead of the `mcp__klaus__` prefix, while still accepting prefixed names from resumed sessions and older runtimes
extensions/context-size/README.md Modified 37 - Shows current context-window usage as a token ratio plus a 10-cell bar.
37 + Shows current context-window usage as a 10-cell bar.
39 39 If `@bugabinga/pi-ext-footer` is loaded, publishes a `context-size` footer
40 40 segment in the `llm` zone with order `1`.
41 41 Otherwise uses Pi status text with id `context-size`.
43 - The footer segment includes shortened variants:
46 - - percent plus token ratio
48 - Token counts are displayed in thousands, rounded.
43 + The footer segment is the bar alone.
44 + Its length encodes the percentage and its color encodes the pressure, so
45 + percent and token counts would only repeat it.
57 54 The bar uses `dim` when the text color is muted. Otherwise it uses the same
57 + Outside TUI mode the bar is replaced by plain status text such as
58 + `context 26% used, 738k left`, because other clients render the status line as
59 + text in an unknown font.
60 + Token counts there use `k` and `M` units.
60 62 No UI update runs when UI is unavailable.
62 64 Clears footer/status when context usage is unavailable.
extensions/context-size/__tests__/harness.test.ts Modified 1 1 import { afterEach, describe, expect, it }
from "vitest" ;
2 2 import { createTestSession, type TestSession }
from "../../../test/harness" ;
3 - import contextSizeExtension
from "../index" ;
3 + import contextSizeExtension, { __test }
from "../index" ;
5 5 describe (
"context-size pi-test-harness" , ()
=> {
6 6 let t
: TestSession | undefined ;
16 16 expect (t.session.extensionRunner.extensions).
toHaveLength (
1 );
19 + it (
"renders the tui view as a bar alone" , ()
=> {
20 + expect (__test.
renderThinBar (
26 )).
toBe (
"━━━───────" );
21 + expect (__test.
renderThinBar (
null )).
toBe (
"──────────" );
24 + it (
"spells out the non-tui label with human-readable headroom" , ()
=> {
26 + __test.
buildPlainLabel ({
28 + contextWindow:
1_000_000 ,
31 + ).
toBe (
"context 26% used, 738k left" );
34 + __test.
buildPlainLabel ({
36 + contextWindow:
1_000_000 ,
39 + ).
toBe (
"context unknown" );
42 + it (
"scales token counts through k and M" , ()
=> {
43 + expect (__test.
formatTokens (
940 )).
toBe (
"940" );
44 + expect (__test.
formatTokens (
1_180 )).
toBe (
"1.2k" );
45 + expect (__test.
formatTokens (
262_000 )).
toBe (
"262k" );
46 + expect (__test.
formatTokens (
1_618_000 )).
toBe (
"1.6M" );
47 + expect (__test.
formatTokens (
121_527_000 )).
toBe (
"122M" );
extensions/context-size/index.ts Modified 22 - const TOKEN_UNIT
= 1000 ;
64 62 return BAR.full.
repeat (filled)
+ BAR.empty.
repeat (BAR.
width - filled);
67 - function formatTokenCount (tokens
: number | null )
: string {
68 - if (tokens
=== null )
return "?" ;
69 - return `${ Math . round (tokens / TOKEN_UNIT)}k` ;
72 - function formatTokenRatio (
73 - tokens
: number | null ,
74 - contextWindow
: number ,
76 - return `${ formatTokenCount (tokens)}/${ formatTokenCount (contextWindow)}` ;
66 + function formatTokens (tokens
: number )
: string {
67 + if (tokens
< 1_000 )
return `${ Math . round (tokens)}` ;
68 + if (tokens
< 10_000 )
return `${(tokens / 1_000). toFixed (1)}k` ;
69 + if (tokens
< 1_000_000 )
return `${ Math . round (tokens / 1_000)}k` ;
70 + if (tokens
< 10_000_000 )
return `${(tokens / 1_000_000). toFixed (1)}M` ;
71 + return `${ Math . round (tokens / 1_000_000)}M` ;
79 78 function buildSegment (ctx
: ExtensionContext , usage
: ContextUsage )
: SegmentView {
80 79 const percent
= usage.percent
?? null ;
81 80 const textTone
= toneForPercent (percent);
82 - const ratio
= formatTokenRatio (usage.tokens, usage.contextWindow);
83 - const bar
= ctx.ui.theme.
fg (
barTone (textTone),
renderThinBar (percent));
84 - const pct
= percent
=== null ? "?%" : `${ Math . round (percent)}%` ;
87 - text:
`${bar} ${pct} ${ratio}` ,
83 + text: ctx.ui.theme.
fg (
barTone (textTone),
renderThinBar (percent)),
92 + function buildPlainLabel (usage
: ContextUsage )
: string {
93 + const percent
= usage.percent
?? null ;
94 + if (percent
=== null || usage.tokens
=== null )
return "context unknown" ;
95 + const left
= Math .
max (
0 , usage.contextWindow
- usage.tokens);
96 + return `context ${ Math . round (percent)}% used, ${ formatTokens (left)} left` ;
92 99 function offerFooterSegment (pi
: ExtensionAPI , segment
: object )
: boolean {
93 100 let accepted
= false ;
94 101 pi.events.
emit (
"footer:segment" , {
133 + if (ctx.mode
!== "tui" ) {
135 + ctx.ui.
setStatus (STATUS_ID,
buildPlainLabel (usage));
126 139 const view
= buildSegment (ctx, usage);
128 - ctx.mode
=== "tui" && 129 - offerFooterSegment (pi, {
133 - view.
text .
replace (
/\s + \S + \/\S +$ / ,
"" ),
134 - view.
text .
replace (
/ ^ \S + \s + / ,
"" ),
140 + const accepted
= offerFooterSegment (pi, {
139 146 if (
! accepted)
clearSegment ();
140 147 ctx.ui.
setStatus (STATUS_ID, accepted
? undefined : view.
text );
167 + export const __test
= { buildPlainLabel, formatTokens, renderThinBar };
extensions/cost/README.md Modified 46 46 Shows cumulative session token usage, turn cost, session cost, and current-month cost.
48 48 Token totals include assistant messages, nested tool usage, compactions, and branch summaries across the session.
49 - The token label uses input, output, cache-read, and cache-write counts.
50 - Cache reads use `◐`, cache writes use `W`, and `` shows the latest assistant prompt's cache-hit percentage.
49 + The token label uses input (`↑`) and output (`↓`) counts, plus the cache-hit percentage of the latest assistant prompt.
50 + Cache-read and cache-write totals are deliberately absent: as raw counts they are unreadable, while the hit percentage carries the decision-relevant part.
51 + Cache-hit percentage is prefixed with the cache icon.
51 52 No token usage shows `↑0`.
54 + Counts use `k` and `M` units; costs of `$1000` or more use `k` as well.
56 + Outside TUI mode the status text is spelled out, for example
57 + `in 1.6M, out 448k, cache hit 100%, turn $0.16, session $70.70, month $3.1k`,
58 + because other clients render the status line as text in an unknown font.
59 + Without UI, nothing is published.
53 61 Uses provider-reported `usage.cost.total` when present and greater than zero.
54 62 If reported cost is zero or missing, estimates cost only for built-in `zai` and
55 63 `minimax` pricing rows.
extensions/cost/__tests__/harness.test.ts Modified 342 - it (
"formats cumulative token and cache usage in the existing visual style" , ()
=> {
342 + it (
"shows only input, output, and cache hit rate in the tui label" , ()
=> {
344 344 __test.
tokenLabel (
353 - ).
toBe (
"↑1.1k ↓978 ◐ 109k W2.1k 98%" );
353 + ).
toBe (
"↑1.1k ↓978 98%" );
356 + it (
"scales large token counts into millions" , ()
=> {
359 + { input:
1_618_000 , output:
448_000 , cacheRead:
0 , cacheWrite:
0 },
362 + ).
toBe (
"↑1.6M ↓448k 100%" );
365 + it (
"spells out the non-tui label without glyphs" , ()
=> {
367 + __test.
plainLabel ({
371 + cacheRead:
121_527_000 ,
372 + cacheWrite:
1_381_000 ,
377 + monthlyCost:
3146.2 ,
380 + "in 1.6M, out 448k, cache hit 100%, turn $0.16, session $70.7, month $3.1k" ,
extensions/cost/index.ts Modified 23 23 const PRICE_UNIT_TOKENS
= 1_000_000 ;
24 24 const COUNT_DECIMAL_THRESHOLD
= 1_000 ;
25 25 const COUNT_ROUND_THRESHOLD
= 10_000 ;
26 + const MILLION_THRESHOLD
= 1_000_000 ;
27 + const MILLION_DECIMAL_THRESHOLD
= 10_000_000 ;
26 28 const LOW_COST_THRESHOLD
= 0.01 ;
27 29 const MEDIUM_COST_THRESHOLD
= 10 ;
30 + const HIGH_COST_THRESHOLD
= 1_000 ;
28 31 const SESSION_EXT
= ".jsonl" ;
29 32 const SESSION_MONTH_RE
= / ^ (\d {4} )-(\d {2} )/ ;
329 333 function formatCount (value
: number )
: string {
330 334 if (value
< COUNT_DECIMAL_THRESHOLD)
return `${value}` ;
331 335 if (value
< COUNT_ROUND_THRESHOLD)
332 336 return `${(value / COUNT_DECIMAL_THRESHOLD). toFixed (1)}k` ;
333 - return `${ Math . round (value / COUNT_DECIMAL_THRESHOLD)}k` ;
337 + if (value
< MILLION_THRESHOLD)
338 + return `${ Math . round (value / COUNT_DECIMAL_THRESHOLD)}k` ;
339 + if (value
< MILLION_DECIMAL_THRESHOLD)
340 + return `${(value / MILLION_THRESHOLD). toFixed (1)}M` ;
341 + return `${ Math . round (value / MILLION_THRESHOLD)}M` ;
336 344 function formatCost (value
: number )
: string {
337 345 if (value
< LOW_COST_THRESHOLD)
return `$${value. toFixed (4)}` ;
338 346 if (value
< MEDIUM_COST_THRESHOLD)
return `$${value. toFixed (2)}` ;
339 - return `$${value. toFixed (1)}` ;
347 + if (value
< HIGH_COST_THRESHOLD)
return `$${value. toFixed (1)}` ;
348 + return `$${(value / HIGH_COST_THRESHOLD). toFixed (1)}k` ;
342 356 function tokenLabel (usage
: TokenUsage , cacheHitRate
?: number )
: string {
344 358 usage.
input === 0 && 351 365 const parts
: string []
= [];
352 366 if (usage.
input > 0 ) parts.
push (
`↑${ formatCount (usage. input )}` );
353 367 if (usage.output
> 0 ) parts.
push (
`↓${ formatCount (usage.output)}` );
354 - if (usage.cacheRead
> 0 ) parts.
push (
`◐ ${ formatCount (usage.cacheRead)}` );
355 - if (usage.cacheWrite
> 0 ) parts.
push (
`W${ formatCount (usage.cacheWrite)}` );
356 368 if (cacheHitRate
!== undefined )
357 369 parts.
push (
`${CACHE_ICON} ${ Math . round (cacheHitRate)}%` );
358 370 return parts.
join (
" " );
396 409 function statusLabel (snapshot
: Snapshot )
: string {
398 411 tokenLabel (snapshot.usage, snapshot.cacheHitRate),
422 + function plainLabel (snapshot
: Snapshot )
: string {
423 + const { input, output }
= snapshot.usage;
424 + const parts
= [
`in ${ formatCount (input)}` ,
`out ${ formatCount (output)}` ];
425 + if (snapshot.cacheHitRate
!== undefined )
426 + parts.
push (
`cache hit ${ Math . round (snapshot.cacheHitRate)}%` );
427 + if (snapshot.turnCost
> 0 )
428 + parts.
push (
`turn ${ formatCost (snapshot.turnCost)}` );
429 + if (snapshot.sessionCost
> 0 )
430 + parts.
push (
`session ${ formatCost (snapshot.sessionCost)}` );
431 + if (snapshot.monthlyCost
> snapshot.sessionCost)
432 + parts.
push (
`month ${ formatCost (snapshot.monthlyCost)}` );
433 + return parts.
join (
", " );
405 436 function publishSnapshot (
406 437 pi
: ExtensionAPI ,
407 438 ctx
: ExtensionContext ,
408 439 snapshot
: Snapshot ,
410 - let accepted
= false ;
411 - if (ctx.mode
=== "tui" ) {
412 - const tokensAccepted
= emitSegment (
415 - tokenLabel (snapshot.usage, snapshot.cacheHitRate),
417 - const costAccepted
= emitSegment (pi,
"cost" ,
costLabel (snapshot));
418 - accepted
= tokensAccepted
&& costAccepted;
441 + if (ctx.mode
!== "tui" ) {
443 + ctx.ui.
setStatus (STATUS_ID,
plainLabel (snapshot));
447 + const tokensAccepted
= emitSegment (
450 + tokenLabel (snapshot.usage, snapshot.cacheHitRate),
452 + const costAccepted
= emitSegment (pi,
"cost" ,
costLabel (snapshot));
453 + const accepted
= tokensAccepted
&& costAccepted;
421 455 if (
! accepted)
clearSegments (pi);
422 456 ctx.ui.
setStatus (STATUS_ID, accepted
? undefined : statusLabel (snapshot));
571 605 calculateMonthlyCost,
573 607 latestAssistantCacheHitRate,
extensions/footer/__bench__/render.bench.ts Modified 183 - text:
"↑18.2k ↓4.1k ◐ 96k W2.4k 82%" ,
183 + text:
"↑18.2k ↓4.1k 82%" ,
extensions/quota/README.md Modified 58 58 - Active provider marker:
59 59 `●`; inactive marker:
61 + - The active provider's segment omits the provider name, because the model
62 + segment already shows that provider's icon.
63 + Inactive providers keep their name.
64 + - Outside TUI mode, the status text is spelled out, for example
65 + `Anthropic quota 4% used, resets in 4h50m`.
66 + - Without UI, no status is published and no quota requests are made.
62 68 OpenAI Codex needs both an auth token and an account id.
63 69 The account id is read from Pi auth storage first, then `~/.codex/auth.json`.
extensions/quota/__tests__/harness.test.ts Modified 3 3 import path
from "node:path" ;
4 4 import { afterEach, describe, expect, it }
from "vitest" ;
5 5 import { createTestSession, type TestSession }
from "../../../test/harness" ;
6 6 import { __test }
from "../index.ts" ;
9 + const RESET_AT
= Date .
now ()
+ 4 * 3_600_000 + 50 * 60_000 + 30_000 ;
8 11 describe (
"quota pi-test-harness" , ()
=> {
9 12 let t
: TestSession | undefined ;
86 89 weekly : {
used :
20 ,
total :
100 ,
remaining :
80 },
93 + it (
"drops the provider name for the active provider only" , ()
=> {
96 + fiveH: { used:
4 , total:
100 , remaining:
96 , resetAt: RESET_AT },
97 + weekly: { used:
0 , total:
0 , remaining:
0 },
100 + expect (__test.
segmentText (anthropic,
true )).
toEqual ([
105 + expect (__test.
segmentText (anthropic,
false )).
toEqual ([
106 + "Anthropic 4% (4h50m)" ,
112 + it (
"keeps the name in status fallbacks and spells out the non-tui label" , ()
=> {
113 + const providers
= [
116 + fiveH: { used:
4 , total:
100 , remaining:
96 , resetAt: RESET_AT },
117 + weekly: { used:
0 , total:
0 , remaining:
0 },
121 + expect (__test.
quotaStatusLabel (providers,
"Anthropic" )).
toBe (
122 + "Anthropic 4% (4h50m)" ,
124 + expect (__test.
quotaPlainLabel (providers,
"Anthropic" )).
toBe (
125 + "Anthropic quota 4% used, resets in 4h50m" ,
127 + expect (__test.
quotaPlainLabel ([],
"Anthropic" )).
toBeUndefined ();
extensions/quota/index.ts Modified 438 438 return used
< 50 ? "accent" : used
< 80 ? "warning" : "error" ;
443 + function visibleRows (
444 + providers
: readonly ProviderUsage [],
445 + active
: string | null ,
446 + )
: ProviderUsage [] {
448 + .
filter ((p)
=> p.fiveH.total
> 0 )
449 + .
filter ((p)
=> ! active
|| p.
name .
startsWith (active));
452 + function fiveHPercent (p
: ProviderUsage )
: number {
453 + return Math .
round ((p.fiveH.used
/ p.fiveH.total)
* 100 );
456 + function shortName (p
: ProviderUsage )
: string {
457 + return p.
name .
split (
" " )[
0 ];
464 + function segmentText (p
: ProviderUsage , isActive
: boolean )
: string [] {
465 + const used
= fiveHPercent (p);
466 + const reset
= fmtReset (p.fiveH.resetAt);
467 + const prefix
= isActive
? "" : `${ shortName (p)} ` ;
469 + `${prefix}${used}%${reset ? ` (${reset})` : ""}` ,
470 + `${prefix}${used}%` ,
479 + function quotaStatusLabel (
480 + providers
: readonly ProviderUsage [],
481 + active
: string | null ,
482 + )
: string | undefined {
483 + const rows
= visibleRows (providers, active).
map ((p)
=> {
484 + const reset
= fmtReset (p.fiveH.resetAt);
485 + return `${ shortName (p)} ${ fiveHPercent (p)}%${reset ? ` (${reset})` : ""}` ;
487 + return rows.
length > 0 ? rows.
join (
" · " )
: undefined ;
491 + function quotaPlainLabel (
492 + providers
: readonly ProviderUsage [],
493 + active
: string | null ,
494 + )
: string | undefined {
495 + const rows
= visibleRows (providers, active).
map ((p)
=> {
496 + const reset
= fmtReset (p.fiveH.resetAt);
497 + const suffix
= reset
? `, resets in ${reset}` : "" ;
498 + return `${ shortName (p)} quota ${ fiveHPercent (p)}% used${suffix}` ;
500 + return rows.
length > 0 ? rows.
join (
"; " )
: undefined ;
443 505 export default function usageExtension (pi
: ExtensionAPI ) {
616 678 footerIds.
clear ();
619 - function quotaStatusLabel (ctx
: ExtensionContext )
: string | undefined {
620 - const active
= activeProvider (ctx);
621 - const rows
= providers
622 - .
filter ((p)
=> p.fiveH.total
> 0 )
623 - .
filter ((p)
=> ! active
|| p.
name .
startsWith (active))
625 - const used
= Math .
round ((p.fiveH.used
/ p.fiveH.total)
* 100 );
626 - const label
= p.
name .
split (
" " )[
0 ];
627 - const reset
= fmtReset (p.fiveH.resetAt);
628 - return `${label} ${used}%${reset ? ` (${reset})` : ""}` ;
630 - return rows.
length > 0 ? rows.
join (
" · " )
: undefined ;
633 681 function updateFooter (ctx
: ExtensionContext ) {
682 + if (
! ctx.hasUI)
return ;
634 683 const active
= activeProvider (ctx);
636 685 if (ctx.mode
!== "tui" ) {
638 - ctx.ui.
setStatus (STATUS_ID,
quotaStatusLabel (ctx));
687 + ctx.ui.
setStatus (STATUS_ID,
quotaPlainLabel (providers, active));
643 692 let accepted
= true ;
644 693 const seen
= new Set <
string >();
646 - for (
const p
of providers
647 - .
filter ((p)
=> p.fiveH.total
> 0 )
648 - .
filter ((p)
=> ! active
|| p.
name .
startsWith (active))) {
649 - const used
= Math .
round ((p.fiveH.used
/ p.fiveH.total)
* 100 );
695 + for (
const p
of visibleRows (providers, active)) {
696 + const used
= fiveHPercent (p);
650 697 const isActive
= !! active
&& p.
name .
startsWith (active);
651 - const label
= p.
name .
split (
" " )[
0 ];
652 - const reset
= fmtReset (p.fiveH.resetAt);
698 + const [text,
... variants]
= segmentText (p, isActive);
653 699 const id
= `quota:${p. name }` ;
655 701 footerIds.
add (id);
659 705 offerFooterSegment ({
661 707 icon: isActive
? "●" : "◌" ,
662 - text:
`${label} ${used}%${reset ? ` (${reset})` : ""}` ,
663 - variants: [
`${label} ${used}%` ,
`${used}%` ],
664 710 color: isActive
? color (used)
: "muted" ,
677 723 accepted
= offered
&& accepted;
678 724 if (
! accepted)
clearFooter ();
679 - ctx.ui.
setStatus (STATUS_ID, accepted
? undefined : quotaStatusLabel (ctx));
727 + accepted
? undefined : quotaStatusLabel (providers, active),
773 822 widgetVisible
= false ;
826 + if (
! ctx.hasUI)
return ;
775 827 ctx.ui.
setStatus (STATUS_ID,
undefined );
776 828 startRefresh (ctx, ticket);
777 829 void (
async ()
=> {
791 843 pi.
on (
"agent_start" ,
async (_e, ctx)
=> updateFooter (ctx));
792 844 pi.
on (
"agent_end" ,
async (_e, ctx)
=> {
845 + if (
! ctx.hasUI)
return ;
794 847 await getOrFetch (ctx);
795 848 updateFooter (ctx);
798 851 pi.
on (
"model_select" ,
async (_e, ctx)
=> {
852 + if (
! ctx.hasUI)
return ;
800 854 await getOrFetch (ctx,
true );
801 855 updateFooter (ctx);
815 - export const __test
= { parseCopilot, parseGemini, parseMinimax };
869 + export const __test
= {