Skip to content

perf(memory): hibernate idle background tabs and completed subagent transcripts - #2560

Merged
Hona merged 2 commits into
devfrom
perf/hibernate-inactive-tabs
Dec 6, 2026
Merged

Hona merged 2 commits into
devfrom
perf/hibernate-inactive-tabs

Conversation

@Hona

@Hona Hona commented Dec 6, 2026

Copy link
Copy Markdown
Member

Summary

Evicts reloadable heavy session payloads for idle background tabs and completed non-visible subagents via an explicit state machine in apps/desktop/src/shell/tab_hibernate.rs, bounding steady-state live heap without altering tab state, scroll position, composer drafts, or animation framerate.

Evidence

In live profiling (opencode-desktop.exe v0.0.0-dev-543), Heap live climbed from 81.9 MB (cold start, 8 tabs) to 211.4 MB (+129.5 MB) over 6.8 min across 9 open tabs, with single-tab jumps of +17.8 MB, +14.7 MB, +22.1 MB, and +37.2 MB. memory_reclaim::sweep_current_server keeps full transcripts, page caches, context windows, review diffs, and every completed descendant subagent transcript resident for all open tabs indefinitely.

Changes

  • apps/desktop/src/shell/tab_hibernate.rs (419 lines, #![forbid(unsafe_code)]):
    • Completed subagents (60 s grace): Unloads finished, non-visible subagent sessions (SessionInfo.parent.is_some() and not open as their own tab) even while their parent tab remains active or runs sibling subagents. Active/running subagents protect their ancestor chain (s.ancestors(id)), leaving completed siblings eligible.
    • Untouched background tabs (180 s grace): Hibernates open session tabs outside the 2 most-recently-visited background tabs per window (WARM_BACKGROUND_TABS = 2). Viewing a child subagent tracks its root tab as active_tab so the parent tab does not consume one of the 2 MRU slots.
    • Explicit state machines:
      • TabWarmth (Active, Busy, WarmMru { left_at }, Cooling { idle_since, kind }) via step_warmth.
      • SweepPhase (Idle, Armed { due }) via step_phase — arms a single tx.wake_at(due) for the earliest cooling deadline and returns to Idle (zero wakeups) when nothing is cooling.
    • Zero steady-state overhead: Compares an untracked Fingerprint (route, tabs, running, pending, requests, inboxes, checkout versions, transcripts.len(), opening.is_some(), live, online). Omits transcripts version so streaming token deltas never trigger scans, allocations, or DerefMut bumps. Always returns false (offscreen eviction alters no pixels).
    • Protected sets (never evicted):
      • Active route, opening.id / opening.from, and all ancestors.
      • Sessions shown in any other window (windows::other_windows_shown), and all servers while windows::any_other_window_unreported is true.
      • 2 most-recently-visited background session tabs across active and parked servers.
      • Sessions that are running, stopping (stops), optimistic starts (queue.starts), queued (inboxes), awaiting pending permission/form prompts or requests, holding unsent prompts, local echoes, held submits (queue.submit), active queue ops (QUEUE_OPS), busy rewinds (REWIND), open fork dialog (FORK), running shells/stopping items/open shell panel, or running background jobs (SHELLS).
      • Draft tabs (TabKind::Draft).
      • Offline, disconnected, fixture, or storybook sessions (!Source::Live, !pages.has_pool(), reconnect::down, or fixture_run.stage.is_some()).
    • Evicted (reloadable) payloads only: s.pages.forget(sid), s.transcript_edits.remove(&mut s.transcripts, sid), s.context_windows.forget(server, sid), stale STREAM_GAPS entries, per-window review::REVIEW.sessions + prune_cache(&warm_ids, &warm_dirs) and turn_diffs (only when not warm on any server half), global retain_user_messages, and purge_free_pages_soft() on native targets.
    • Preserved intact: App::tabs, App::sessions (titles, badges, unread/failed dots), saved scroll places (restore::RESTORE / KEPT_PLACES, timeline_fx.ends), and composer drafts & attachments.
  • apps/desktop/src/shell/mod.rs (+1 line): Export pub mod tab_hibernate;.
  • apps/desktop/src/kernel.rs (+3 lines): Register HIBERNATE slot and schedule tab_hibernate.poll in stage::PERSIST_FLUSH after windows.persist.

Rehydration proof (no tab-switch edits required)

  1. Tab click / shortcut / Ctrl+Tab / ⌘1–9: App::select_tab (shell/tabs/actions.rs:377) calls servers::runtime::switch then App::open (session/meta/open.rs:11). With pages.has_pool() && !transcripts.contains_key(id), held is true, invoking App::begin_open (session/pages/mod.rs:412).
  2. begin_open -> open_now + open_page: Holds the prior frame during HOLD (zero blank flash), restores scroll mode (timeline_fx.ends.reopen), composer draft (show_draft), context_windows entry (window_opened), and Review source/refresh (plugin_host::native::diff::prepare_route -> set_session_change_mode -> ensure_session or immediate refresh). open_page enqueues a deduplicated foreground explicit read (pages.request(key, true)).
  3. Page arrival (poll_pages / merge_joined / poll_open): Repopulates transcripts and context_windows (window_read), marks pages.sync.server_read, clears opening, restores exact saved scroll offset (land_place), and schedules history pagination (schedule_history).
  4. History nav / subagent click / direct route: App::navigate (shell/nav/history.rs:157) and open_subagent_session route through App::open; any direct route assignment falls back to App::read_shown_session (session/pages/shown.rs:11).
  5. Lazy diffs: TurnDiffs::view re-fetches lazily on draw; reopening the Review panel triggers open_review -> ensure_session.
  6. Incoming SSE events: events/mod.rs:307 uses transcript_edits.edit (no-op if absent), and poll_pages (session/pages/mod.rs:684) ignores speculative pages for unrouted absent transcripts.

Verification

  • cargo check -p opencode-desktop --no-default-features --features knobs — pass (0 warnings in touched files)
  • cargo check -p opencode-web --target wasm32-unknown-unknown — pass (0 warnings in touched files)
  • pwsh scripts/guardrails.ps1 — pass (12/12 passed)
  • git diff --check — clean
  • tab_hibernate.rs: 419 lines (<= 450), zero tests, zero .cursor(...), zero std::time::Instant / SystemTime::now

@Hona
Hona merged commit 0d884e9 into dev Dec 6, 2026
8 checks passed
@Hona
Hona deleted the perf/hibernate-inactive-tabs branch December 6, 2026 05:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant