Repository navigation
Fix: Retain streamed events only for the session being watched - #1589
Conversation
…#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]>
|
Warning Review limit reachedYou'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. View limit details
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. Comment |
…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
left a comment
There was a problem hiding this comment.
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.
| // 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 |
There was a problem hiding this comment.
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.
| // 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 |
There was a problem hiding this comment.
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]>
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
handleStreamEventappended every/v1/eventsarrival tom.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).openTimelinedeliberately does not setselectedSess).Every other session's events still update
UpdatedAt, create their summary stub, and marksessionsStale. The off-screen readers never needed the events, checked one by one:localContextFor'sdefault:branch re-folds from the kept figure, and the server'spromptContextkeeps flowing from the 2s poll.Decisions made explicit (the issue asked for these to be decided, not drifted into)
model.pluginRunsat 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=truelist 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 throughselectedSess, 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.go vet,gofmt,go mod tidy -diffclean.Quantified result
Deferred
C(catalog) andA(agents) exits from the timeline pane intentionally keeptimeline.sessionfor 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]