hermes-agent/ui-tui/src/lib/circularBuffer.ts
Brooklyn Nicholson e1ce7c6b1f fix(tui): address PR #13231 review comments
Six small fixes, all valid review feedback:

- gatewayClient: onTimeout is now a class-field arrow so setTimeout gets a
  stable reference — no per-request bind allocation (the whole point of
  the original refactor).
- memory: growth rate was lifetime average of rss/uptime, which reports
  phantom growth for stable processes. Now computed as delta since a
  module-load baseline (STARTED_AT). Sanity-checked: 0.00 MB/hr at
  steady-state, non-zero after an allocation.
- hermes_cli: NODE_OPTIONS merge is now token-aware — respects a
  user-supplied --max-old-space-size (don't downgrade a deliberate 16GB
  setting) and avoids duplicating --expose-gc.
- useVirtualHistory: if items shrink past the frozen range's start
  mid-freeze (/clear, compaction), drop the freeze and fall through to
  the normal range calc instead of collapsing to an empty mount.
- circularBuffer: throw on non-positive capacity instead of silently
  producing NaN indices.
- debug slash help: /heapdump mentions HERMES_HEAPDUMP_DIR override
  instead of hardcoding the default path.

Validation: tsc clean, eslint clean, vitest 102/102, growth-rate smoke
test confirms baseline=0 → post-alloc>0.
2026-04-20 19:09:09 -05:00

48 lines
994 B
TypeScript

export class CircularBuffer<T> {
private buf: T[]
private head = 0
private len = 0
constructor(private capacity: number) {
if (!Number.isInteger(capacity) || capacity <= 0) {
throw new RangeError(`CircularBuffer capacity must be a positive integer, got ${capacity}`)
}
this.buf = new Array<T>(capacity)
}
push(item: T) {
this.buf[this.head] = item
this.head = (this.head + 1) % this.capacity
if (this.len < this.capacity) {
this.len++
}
}
tail(n = this.len): T[] {
const take = Math.min(Math.max(0, n), this.len)
const start = this.len < this.capacity ? 0 : this.head
const out: T[] = new Array<T>(take)
for (let i = 0; i < take; i++) {
out[i] = this.buf[(start + this.len - take + i) % this.capacity]!
}
return out
}
drain(): T[] {
const out = this.tail()
this.clear()
return out
}
clear() {
this.buf = new Array<T>(this.capacity)
this.head = 0
this.len = 0
}
}