fix(tui): bound retained state against idle OOM

Guards four unbounded growth paths reachable at idle — the shape matches
reports of the TUI hitting V8's 2GB heap limit after ~1m of idle with 0
tokens used (Mark-Compact freed ~6MB of 2045MB → pure retention).

- `GatewayClient.logs` + `gateway.stderr` events: 200-line cap is bytes-
  uncapped; a chatty Python child emitting multi-MB lines (traceback,
  dumped config, unsplit JSON) retains everything. Truncate at 4KB/line.
- `GatewayClient.bufferedEvents`: unbounded until `drain()` fires. Cap
  at 2000 so a pre-mount event storm can't pin memory indefinitely.
- `useMainApp` gateway `exit` handler: didn't reset `turnController`, so
  a mid-stream crash left `bufRef`/`reasoningText` alive forever.
- `pasteSnips` count-capped (32) but byte-uncapped. Add a 4MB total cap
  and clear snips in `clearIn` so submitted pastes don't linger.
- `StylePool.transitionCache`: uncapped `Map<number,string>`. Full-clear
  at 32k entries (mirrors `charCache` pattern).
This commit is contained in:
Brooklyn Nicholson 2026-04-19 19:41:25 -05:00 committed by Teknium
parent 424e9f36b0
commit 0d353ca6a8
4 changed files with 44 additions and 5 deletions

View file

@ -121,6 +121,8 @@ const YELLOW_FG_CODE: AnsiCode = {
endCode: '\x1b[39m'
}
const MAX_TRANSITION_CACHE = 32768
export class StylePool {
private ids = new Map<string, number>()
private styles: AnsiCode[][] = []
@ -160,7 +162,9 @@ export class StylePool {
/**
* Returns the pre-serialized ANSI string to transition from one style to
* another. Cached by (fromId, toId) zero allocations after first call
* for a given pair.
* for a given pair. Full-clear at MAX_TRANSITION_CACHE guards against
* unbounded growth from ever-expanding id spaces; cache repopulates from
* the next frame's actual transitions.
*/
transition(fromId: number, toId: number): string {
if (fromId === toId) {
@ -171,6 +175,10 @@ export class StylePool {
let str = this.transitionCache.get(key)
if (str === undefined) {
if (this.transitionCache.size >= MAX_TRANSITION_CACHE) {
this.transitionCache.clear()
}
str = ansiCodesToString(diffAnsiCodes(this.get(fromId), this.get(toId)))
this.transitionCache.set(key, str)
}