Skip to content

Fix: Retain streamed events only for the session being watched - #1589

Merged
huang195 merged 3 commits into
mainfrom
fix/stream-retention-1542
Oct 29, 2026
Merged

huang195 merged 3 commits into
mainfrom
fix/stream-retention-1542

Conversation

@huang195

@huang195 huang195 commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

Fixes #1542 with its test companion #1551 (the off-screen-streaming half). Reproduced first with the issue's fixture, which now pins the fixed behavior.

The leak

handleStreamEvent appended every /v1/events arrival to m.events[sessionID] for every session — never interned (a live request event carries the whole conversation, 275–537 KB) and never released — so an idle agentop grew at the wire rate: ~18 MB/min heap measured live, 1.5 GB after 13 hours, larger than the proxy it was watching. A heap profile put 92% of idle growth under the SSE event decoder. Reproduced with the issue's unit fixture: 150 streamed events for 3 never-opened sessions retained +18.9 MB — 31x the distinct content.

The fix

Streamed events are retained only for the session being watched — watchingSession:

  • selectedSess, pane-agnostic: it survives pane switches (only opening another session, erasing, or a server switch clears it), so walking events → sessions → events does not punch a gap in the live tail of the session you had open. The issue proposed a pane-scoped predicate; pane-agnostic is strictly safer for that continuity at the same bounded cost (one session).
  • or the timeline's session (openTimeline deliberately does not set selectedSess).

Every other session's events still update UpdatedAt, create their summary stub, and mark sessionsStale. The off-screen readers never needed the events, checked one by one:

  • Sessions table tokens: prefers the server's total.
  • CTX gauge: localContextFor's default: branch re-folds from the kept figure, and the server's promptContext keeps flowing from the 2s poll.
  • Opening a session: fresh interned snapshot — the one visible cost, a round-trip where the cache used to answer instantly (from the earlier benchmark: a 23.5 MB snapshot decodes with +0.0 MB HeapSys, ~2.3 MB interned).

Decisions made explicit (the issue asked for these to be decided, not drifted into)

  • Pipeline pane per-plugin counts are folded into model.pluginRuns at stream arrival for every session, watched or not, so they describe all traffic since attach (a snapshot's history contributes nothing; a pod switch resets them).
  • cachedOnlySessionIDs: a never-opened session no longer appears there once the server drops it; the archive's ?archived=true list is where such sessions surface. Comment updated.

Tests

  • TestStreamRetentionOffScreen — the issue's fixture: +0.0 MB retained (was +18.9), held-events == 0 for all three never-opened sessions, summaries still created. The heap bound is a fixed 2 MB (the bug's signature is an order above); the deterministic core is held-events == 0, because a full-suite run leaves more than the 0.6 MB distinct-content bound in uncollected garbage even after five GCs — found by a one-off flake, hardened for it.
  • TestStreamRetentionWatchedSessionStillAppends / TestStreamRetentionTimelineSessionStillAppends — both watch paths retain.
  • TestStreamRetentionTimelineStopsAfterDrillDown — the Enter drill-down clears the timeline's session; the drilled-in session stays watched through selectedSess, and stops streaming once the user opens another. Fails on the pre-fix head for the finding's reason.
  • TestStreamedEvents_AreRetainedWithoutACap (on-screen unbounded retention, by design) unchanged and passing; the e2e wire test now watches its session.
  • Full agentop module: 11 packages pass, three consecutive fresh runs; go vet, gofmt, go mod tidy -diff clean.

Quantified result

  • Idle growth: ~18 MB/min → ~0 (off-screen events dropped on arrival).
  • 13-hour idle run: 1.5 GB → near baseline.
  • Under load: only the watched session's stream is retained (measured wire rate says that's ~2 orders of magnitude less than all-sessions), released on session switch as before.

Deferred

  • The C (catalog) and A (agents) exits from the timeline pane intentionally keep timeline.session for their Esc return; navigation that never returns (e.g. catalog → plugin detail → esc → esc) strands it, and the timeline's old session keeps streaming for the rest of the run. Bounded to one session, same class as the drill-down gap closed here. Fix is not clearing on those exits — their Esc legitimately returns to the timeline — but clearing when the return path is dropped.

Assisted-By: Claude (Anthropic AI) [email protected]

…#1551)

handleStreamEvent appended every /v1/events arrival to
m.events[sessionID] for EVERY session — never interned (a live request
event carries the whole conversation, 275-537 KB) and never released —
so an idle agentop grew at the wire rate: ~18 MB/min heap measured live,
1.5 GB after 13h, larger than the proxy it was watching. 92% of idle
heap growth sat under the SSE event decoder.

Streamed events are now retained only for the session being watched:
selectedSess (pane-agnostic, so walking events -> sessions -> events
does not punch a gap in the live tail; only opening another session,
erasing, or a server switch clears it) or the timeline's session
(openTimeline does not set selectedSess). Every other session's events
still update UpdatedAt, create their summary stub, and mark
sessionsStale — the off-screen readers never needed the events:

- the Sessions table's token column prefers the server's total;
- the CTX gauge merges the local fold with the server's promptContext,
  which keeps flowing from the 2s poll;
- opening a session fetches a fresh interned snapshot (one round-trip
  where the cache used to answer instantly).

Two readers' semantics are decided rather than left to drift: the
pipeline pane's per-plugin counts now describe held events (the watched
session) rather than all traffic since attach, and a never-opened
session no longer appears in cachedOnlySessionIDs once the server drops
it — the archive's ?archived=true list is where such sessions surface.

Reproduced first with the issue's fixture: 150 streamed events for 3
never-opened sessions retained +18.9 MB — 31x the distinct content —
now +0.0 MB with held-events == 0 pinned deterministically. The watched
session still retains unbounded
(TestStreamedEvents_AreRetainedWithoutACap unchanged and passing), the
e2e wire test now watches its session, and the heap bound is a fixed
2 MB (the bug's signature is an order above; suite GC noise exceeded
the 0.6 MB distinct-content bound once).

Fixes #1542
Fixes #1551

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Hai Huang <[email protected]>
@huang195
huang195 requested a review from a team as a code owner October 29, 2026 22:20
@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 53 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2eac3d21-3c31-4f1a-8ef9-79d1d366fdc5
Commits

Reviewing files that changed from the base of the PR and between 9124472 and cb35ad2.

Files selected for processing: 9
  • CLAUDE.md
  • cmd/agentop/tui/app.go
  • cmd/agentop/tui/e2e_test.go
  • cmd/agentop/tui/keys.go
  • cmd/agentop/tui/pipeline_pane.go
  • cmd/agentop/tui/plugin_detail_pane.go
  • cmd/agentop/tui/sessions_pane.go
  • cmd/agentop/tui/stream_retention_test.go
  • cmd/agentop/tui/timeline_pane.go
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

…1589 review)

Three findings from the review round on #1589:

1. TestStreamRetentionOffScreen failed CI: the heap delta was computed as a
   uint64 subtraction, and HeapAlloc went DOWN between the two reads (the
   base held garbage from earlier tests), wrapping to ~2^64. Signed now; a
   negative delta passes, which is correct — the deterministic
   held-events==0 assertion is the real guard.

2. The pipeline pane's EVENTS column and the plugin-detail Activity line
   walked m.events, which since #1542 holds only the watched session — the
   pane was dead with nothing open. They now read model.pluginRuns, folded
   once per streamed arrival for every session. Counting at arrival cannot
   double-count the way a walk-plus-counter would (a snapshot of an
   un-watched session re-delivers events the counter already saw), at the
   cost that a snapshot's history contributes nothing: the figures describe
   live traffic since attach. Reset by backToPodsPane beside eventCt; kept
   across a clear, like the cost ledger.

3. Esc from the timeline pane left m.timeline.session set, so a session
   glanced at once kept streaming its events for the rest of the run — the
   #1542 leak, one session wide. Esc clears it; the drill-down paths keep
   their session watched through selectedSess.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Hai Huang <[email protected]>

@clawgenti clawgenti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

Solid fix for #1542. The retention predicate (watchingSession) is small and the doc comments argue it well. Moving the pipeline counts to arrival-time pluginRuns in the follow-up commit is the right call: no double counting, and the pane still works with nothing open. I ran go vet and the full cmd/agentop/tui suite locally on 0563e8a and both are clean; TestStreamRetentionOffScreen reports +0.0 MB retained. I found one gap in the retention predicate (inline, a suggestion that doesn't block) and a couple of nits.

Author: huang195 (MEMBER, maintainer)
Areas reviewed: Go (agentop TUI), tests, docs (CLAUDE.md prose)
Agent/IDE config (.claude/.vscode): none
Commits: 2 commits, all signed-off: yes
CI status: passing

Nit (PR body): the "Pipeline pane per-plugin counts now describe held events, the watched session" bullet describes the first commit. Since 0563e8a they count all traffic since attach, so it's worth updating the body before merge so the squash message doesn't contradict the code.

Comment thread cmd/agentop/tui/app.go
// the run). This is the retention predicate for streamed events (#1542):
// everything else arrives, updates its summary, and is dropped.
func (m *model) watchingSession(id string) bool {
return m.selectedSess == id || m.timeline.session == id

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: timeline.session can still outlive the timeline. Clearing it on Esc (keys.go) covers backing out, but not drilling down. Timeline [enter] calls openEventsAtSeq(B, seq), which sets selectedSess = B and switches to the Events pane while timeline.session stays B. If the user then presses Esc to go to Sessions and opens session C, selectedSess becomes C but timeline.session is still B. The picker release drops B's cached events, but the stream starts appending them again, and they stay for the rest of the run, until the next T or an Esc out of a timeline.

I confirmed this with a throwaway test on 0563e8a: after timeline enter, then Esc, then selectedSess = "C", five streamed events for B are retained (len(m.events["B"]) == 5). It's limited to one extra session, so it's not the #1542 blow-up, but it is exactly the "glanced at once keeps its stream for the rest of the run" case the keys.go comment says can't happen.

The simplest fix is to clear m.timeline.session in openEventsAtSeq, since selectedSess takes over as the retention key at that point, and Esc from Events goes to Sessions, not back to the timeline. The only cost is a refetch on the next T. A probe like the one above would make a good fifth StreamRetention* test.

Comment thread cmd/agentop/tui/sessions_pane.go Outdated
// current list omits, sorted so the picker does not reshuffle under the cursor
// on each refresh. Empty caches are skipped: a row advertising zero events
// helps nobody, and snapshotLoadedMsg can create the key with an empty slice.
// cachedOnlySessionIDs lists sessions agentop holds events for that the

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this adds a second doc paragraph that opens with the same cachedOnlySessionIDs lists sessions... sentence as the one just above, so the comment reads as two headers. Merge the #1542 note into the existing paragraph.

Fixes review: timeline.session outlived the pane via openEventsAtSeq — after
drilling into events and opening another session, the timeline's old session
kept streaming into m.events for the rest of the run; merged the duplicate
cachedOnlySessionIDs doc paragraph.
Files: cmd/agentop/tui/timeline_pane.go, cmd/agentop/tui/keys.go,
cmd/agentop/tui/sessions_pane.go, cmd/agentop/tui/stream_retention_test.go

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Hai Huang <[email protected]>
@huang195
huang195 merged commit cdd75e9 into main Oct 29, 2026
41 checks passed
@huang195
huang195 deleted the fix/stream-retention-1542 branch October 29, 2026 23:16
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bug: agentop keeps every streamed event for every session, growing ~18 MB/min idle

2 participants