Skip to content

Shellper replay buffer stores the stream, not the screen: unbounded growth for full-screen TUIs (17MB+ replays) #1205

Closed

Description

@amrmelsayed

Problem

The shellper's replay buffer is bounded by line count (10,000 lines) plus an unbounded whole-kept partial. A full-screen TUI (Claude sessions) redraws in place via cursor addressing and emits almost no newlines, so an entire multi-day session accumulates as one line that never ends: the line cap never triggers and the buffer grows monotonically for the life of the session.

Measured impact (from the #1198 incident, 2026-07-19):

Byte-trimming the buffer from the front was deliberately rejected previously (#1047): a trim can cut mid-escape-sequence or drop the alt-screen-enter, corrupting replay rendering. So the buffer is whole-by-design, and its growth is unbounded-by-design — but the wire (16MB frame cap) and the UI (xterm parse cost per attach) are not.

Containment already in place (PR #1204)

These make the current design safe, not right:

  • Tower's frame parser discards oversized frames ('frame-skipped') instead of treating them as fatal stream errors.
  • New shellpers truncate the replay they send to an 8MB tail (REPLAY_PAYLOAD_MAX).
  • Adoption seeds at most a 1MB tail into the ring buffer (RING_SEED_MAX_BYTES); viewers rely on the post-connect resize nudge to repaint TUIs.

Every cap is a lossy tail-cut that can transiently render rough until the nudge repaints. The buffer itself still grows without bound in shellper memory.

Proposed direction

Make replay O(screen), not O(session lifetime): store something screen-shaped rather than stream-shaped.

  • Preferred: run PTY output through a headless terminal-state emulator inside the shellper and serve reconnecting clients the current screen state (plus bounded recent scrollback). Replay size becomes a few hundred KB at any session age, with no escape-sequence corruption risk, and the resize-nudge crutch becomes unnecessary.
  • Cheaper alternative: reset the buffer at detectable full-frame boundaries (clear-screen / cursor-home redraw sequences), keeping only bytes since the last full repaint.

Wire-compat constraint: running shellpers are old binaries that survive Tower upgrades, so Tower must keep tolerating old-style (potentially oversized) replays regardless.

Acceptance criteria

Refs: #1198, PR #1204.

Activity

  1. added
    area/terminal
    on Jul 19, 2026
  2. added a commit that references this issue on Jul 19, 2026
  3. waleedkadous commented on Jul 19, 2026

    @waleedkadous
    Contributor
  4. amrmelsayed commented on Jul 25, 2026

    @amrmelsayed
    CollaboratorAuthor

    Escalation + new findings folded in from #1253 (closing that as duplicate of this)

    Field escalation (2026-07-26 user report): the growth this issue predicted has reached multi-GB scale — opening an existing session was observed allocating several GB (up to ~10GB), causing memory pressure and sometimes process termination. This issue's 17MB data points were the early trajectory; heavy long-lived builder sessions (weeks of streamed test/tool output) are now the worst case.

    New mechanism finding — the open-time allocation spike is the user-facing event: getReplayData() runs Buffer.concat(this.chunks) over the ENTIRE accumulated history on every client connect (WELCOME path), transiently doubling the shellper's own footprint at exactly the moment a user opens the terminal. A ~5GB buffer → ~10GB peak → jetsam. The #1204/#1218 containment caps bound the WIRE (subarray applied after the concat) but not this allocation. This is why the symptom presents as 'opening a session consumes GBs' rather than gradual growth.

    Interim fix directions to consider alongside the preferred O(screen) emulator (which remains the right structural end-state):

    1. Cap-before-concat (cheapest, kills the spike even for legacy-accumulated buffers): walk chunks from the tail collecting only up to REPLAY_PAYLOAD_MAX, concat just those — O(cap) allocation per connect instead of O(history).
    2. Byte-cap eviction (maxBytes ~16-32MB alongside maxLines): bounds the buffer itself. The mid-escape-corruption objection this issue cites from tower + vscode: terminal freeze from oversized-replay reconnect storm (unbounded no-newline buffers + client backpressure loop) #1047 is already de-facto accepted at the wire — every existing containment cap is a lossy tail-cut relying on the post-connect resize nudge — so extending the same lossy-tail-plus-nudge contract to the buffer is consistent, as defense-in-depth until the emulator lands.
    3. Tower-side sibling: RingBuffer's whole-kept partial has the same no-newline growth (live monitor: ~2.6MB and +1MB/h on the busiest session at time of writing) and is copied per attach by getAll()/getSince(); same cap-plus-nudge treatment applies.

    Deployment note: fixes 1/2 land in the shellper binary — per the detached-shellper design, only shellpers spawned after the upgrade benefit; long-lived pre-upgrade shellpers keep their accumulated buffers until restarted (or reaped by #1227's husk sweep if they qualify). Release notes should recommend restarting heavy long-lived sessions after upgrading.

    Full elimination trail for the 10GB report (claude --resume transcripts: 570MB total on disk, none >50MB; disk logs: nothing reads them whole) is in #1253's body.

  5. amrmelsayed commented on Jul 26, 2026

    @amrmelsayed
    CollaboratorAuthor

    On it! Working on this with the PIR protocol (plan + dev-approval gates before PR).

  6. added 10 commits that reference this issue on Jul 26, 2026
  7. 9 remaining items

  8. added 11 commits that reference this issue on Aug 5, 2026
  9. amrmelsayed commented on Aug 5, 2026

    @amrmelsayed
    CollaboratorAuthor

    Closing with PR #1353 merged (7b0eb1b): 3 of 4 acceptance criteria met — bounded retention (byte-cap eviction, ESC-aligned cuts), bounded connect-time allocation (capped tail-walk with the contiguity property test), containment caps retained as defense-in-depth. The field-escalated failure (multi-GB allocation spikes opening aged sessions, up to ~10GB → memory-pressure kills) is resolved for shellpers spawned on the fixed binary.

    AC#2 (correct screen without the resize nudge) moves to #1354 — deliberately unmet here, and this issue's own proposal for it ('emulator inside the shellper') is now known-wrong on placement: clients read Tower's RingBuffer and never see the shellper REPLAY frame (arch.md corrected in the PR). #1354 carries the corrected Tower-side framing rather than leaving this body's misleading proposal as the tracker.

    Deployment caveat for release notes (recorded, will ship with the release entry): only shellpers spawned AFTER the upgrade benefit — long-lived pre-upgrade shellpers keep their accumulated buffers until restarted or husk-swept. The heavy long-lived sessions most affected by this bug are exactly the ones an upgrade alone does not fix; restart them after upgrading.

  10. added a commit that references this issue on Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/terminal

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions