Skip to content

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

Closed

Description

@huang195

What happens

agentop's memory grows without limit while it sits idle on any pane. On a laptop, the user's agentop reached 1.5 GB after 12h46m, larger than the proxy it was watching, and grew 1.68 GB → 1.92 GB in 6 minutes with no keypress.

Why

handleStreamEvent (cmd/agentop/tui/app.go:2262) appends every event from /v1/events to m.events[sessionID]. It does that for every session, not just the one on screen:

if !m.pagedBack(e.SessionID) {
	buf := append(m.events[e.SessionID], e)
	m.events[e.SessionID] = buf
}

Two things make that expensive:

  1. Streamed events are never interned. Snapshots go through session.Interner (apiclient/snapshot.go:167), which collapses the conversation that each LLM request re-sends. The stream path (apiclient/sse.go) decodes each event fresh and keeps it whole. A live request event is 275–537 KB, because it carries the whole conversation (up to 180 messages and 26 tools).
  2. Nothing releases it. The only release is in tui/keys.go:654, when the user opens a different session, and it only covers sessions the server still lists. The comment at app.go:1745 already says m.events is "unbounded in BOTH dimensions".

Reproduction

Unit test. Stream 150 events (3 background sessions × 50 turns, each turn re-sending n 4 KB messages) into a model sitting on the Sessions pane:

150 streamed events for 3 sessions not on screen: heap +18.9 MB retained; distinct content 0.6 MB; held events 50/50/50

That is 31x the distinct content, for sessions the user never opened.

Test source (drop into cmd/agentop/tui/; reuses newRetentionModel)
package tui

import (
	"fmt"
	"runtime"
	"strings"
	"testing"
	"time"

	"github.com/rossoctl/cortex/cmd/agentop/apiclient"
	"github.com/rossoctl/cortex/core/pipeline"
)

func TestStreamRetentionOffScreen(t *testing.T) {
	m := newRetentionModel(t, "on-screen", 0)
	m.pane = paneSessions
	hp := func() uint64 { runtime.GC(); runtime.GC(); var s runtime.MemStats; runtime.ReadMemStats(&s); return s.HeapAlloc }
	base := hp()
	for n := 1; n <= 50; n++ {
		for k := 0; k < 3; k++ {
			msgs := make([]pipeline.InferenceMessage, n)
			for i := range msgs {
				msgs[i] = pipeline.InferenceMessage{Role: "user", Content: strings.Clone(fmt.Sprintf("s%d-m%d-", k, i) + strings.Repeat("x", 4096))}
			}
			m.handleStreamEvent(apiclient.StreamEvent{Event: &pipeline.SessionEvent{
				At: time.Now(), SessionID: fmt.Sprintf("bg-%d", k), Direction: pipeline.Outbound, Phase: pipeline.SessionRequest,
				Inference: &pipeline.InferenceExtension{Messages: msgs},
			}})
		}
	}
	t.Logf("heap +%.1f MB retained; distinct content %.1f MB", float64(hp()-base)/1e6, float64(3*50*4110)/1e6)
}

Live. A worktree build driven headless against a live laptop proxy, with no session opened:

  • Wire rate: 60 s of /v1/events carried 11.9 MB in 48 events.
  • Heap: went from 4 MB to 88 MB in 283 s while idle on the Sessions pane, about 18 MB/min. That matches the wire rate.
  • RSS: went from 37 MB to 336 MB over a 10-minute run, with the same slope on every pane (sessions, events, timeline, usage). Leaving a session or a pane released nothing.
  • Heap profile: over 150 s idle, 92% of heap growth was under apiclient.parseSSE → SessionEvent.UnmarshalJSON.

Possible fix

Append a streamed event only when a pane is showing that session (m.selectedSess on the events or detail pane, or m.timeline.session). For every other session, keep doing what the handler already does after the append: bump UpdatedAt and set sessionsStale.

That is safe because the off-screen readers do not need the events:

  • The Sessions table's tokens column prefers the server's total (sessionTokens, sessions_pane.go:1237).
  • The CTX gauge already survives released events. localContextFor re-folds from the figure it kept (sessions_context.go:72), and the server publishes promptContext.
  • Opening a session fetches a fresh snapshot, interned.

Two readers must be checked or adapted:

  • countEventsPerPlugin (pipeline_pane.go:169) counts over every cached event. Under this fix it would count fewer.
  • cachedOnlySessionIDs (sessions_pane.go:459) lists sessions held only in the cache. A session agentop never opened would no longer appear there after the server drops it. The archive now lists those sessions (?archived=true), so this may be fine, but it should be decided rather than changed by accident.

Alternative, if every session must keep appending: run streamed events through one session.Interner per session, as snapshots do. That cuts the cost per event but leaves the growth unbounded. It also depends on the interleaving fix (#1541) to work for sessions with subagents.

Acceptance

  • A test along the lines of the one above: streamed events for sessions not on screen do not grow retained heap past a small constant.
  • The existing event_retention_test.go and sessions_context*_test.go pass. TestStreamedEvents_AreRetainedWithoutACap is about the on-screen session and should still hold.

Found by an experiment-driven audit of main at 43cf553; every number above was re-measured on that commit.

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

Activity

  1. added
    bug
    laptop
    on Oct 29, 2026
  2. huang195 commented on Oct 29, 2026

    @huang195
    MemberAuthor

    CI test for this bug class: #1551. Add the off-screen streaming test with the fix.

  3. self-assigned this
    on Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

buglaptop

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions