orca icon indicating copy to clipboard operation
orca copied to clipboard

"Hide sleeping" never hides a workspace with an open agent session (#7197)

Open brennanb2025 opened this issue 5 months ago • 2 comments

Problem

Closes #7197.

The Hide sleeping sidebar filter is meant to hide only workspaces with no open session, but it judged a workspace "active" solely on whether it had a live PTY (ptyIdsByTabId) or a browser tab. A workspace with a running agent whose live-PTY entry was momentarily absent got classified as sleeping and disappeared while its session was still open. That happens routinely:

  • an SSH/remote disconnect bulk-clears live PTY ids while the relay keeps the session alive (grace window),
  • a remote terminal surface reported in a non-ready state yields an empty ptyIds,
  • a background/unmounted pane (or a post-restart tab not yet re-seeded) has no live-PTY entry though its daemon session is alive,
  • an orchestration worker reports agent status before its terminal tab is mirrored in the renderer.

The smart sort already treats a fresh agentStatusByPaneKey entry as "working" independent of live-PTY, so the filter and the sort disagreed — a workspace could sort to the top as working yet be filtered out as sleeping.

Fix

Add getWorktreeIdsWithLiveAgent, which derives the set of worktrees with an open agent session from the live agentStatusByPaneKey map (attributed by the main-stamped worktreeId, falling back to the paneKey's tabId). Presence in that map is a reliable liveness signal: sleep and teardown drop every entry attributed to the worktree via dropAgentStatusByWorktree (completed rows move to the separate retainedAgentsByPaneKey), so a slept or hibernated workspace still hides.

hasActiveWorkspaceActivity now also returns true when the worktree is in that set. Wired through the three surfaces that share the filter: the sidebar list (computeVisibleWorktreeIds), the Cmd+J jump palette, and the workspace kanban board — each subscribing in a way that avoids re-rendering on every hook ping.

Performance

  • Sidebar (WorktreeList): the live-agent set is built from a getState() snapshot inside the existing visibleWorktrees memo — agent-status changes already bump sortEpoch, so the memo recomputes through sortedIds without subscribing the sidebar to the full agent-status map (the perf design behind EMPTY_AGENT_STATUS_BY_PANE_KEY). The primary bug path — a PTY drops while the agent keeps running — recomputes immediately because ptyIdsByTabId is a direct memo dep.
  • Kanban board: subscribes via useShallow to the sorted worktree-id membership, so re-renders are gated on membership change, not on every ping.

Tests

  • [x] pnpm typecheck (web project) — clean
  • [x] pnpm lint (oxlint + react-doctor) — clean
  • [x] Unit tests: worktree-activity-state.test.ts (new getWorktreeIdsWithLiveAgent cases + running-agent visibility + still-hides-slept), visible-worktrees.test.ts (running-agent worktree stays visible with no live PTY), full sidebar/store suites — 1427 passed
  • [x] Manual e2e in Electron (dev worktree, demo-project scoped): with Hide sleeping ON, a workspace with no tab/PTY/agent is hidden → inject a live agent (real setAgentStatus) → workspace becomes visible → remove the agent → workspace hides again. main (live PTY) stayed visible throughout; 0 console errors.

Related: #4692, #4573, #5209.

Made with Orca 🐋

brennanb2025 avatar Jul 06 '26 06:07 brennanb2025

Review Change Stack

📝 Walkthrough

Walkthrough

This change adds a new helper, getWorktreeIdsWithLiveAgent, that derives worktree IDs with active agent sessions from agent status and tab data. hasActiveWorkspaceActivity and isInactiveWorkspace are updated to optionally accept this set to factor live-agent presence into activity determination. computeVisibleWorktreeIds requires a new worktreeIdsWithLiveAgent option, which is computed and supplied by WorktreeList, use-visible-workspace-kanban-worktree-ids, WorktreeJumpPalette, and getVisibleWorktreeIds. Corresponding tests were added or updated.

Sequence Diagram(s)

sequenceDiagram
  participant UIComponent
  participant getWorktreeIdsWithLiveAgent
  participant computeVisibleWorktreeIds
  participant isInactiveWorkspace
  participant hasActiveWorkspaceActivity

  UIComponent->>getWorktreeIdsWithLiveAgent: agentStatusByPaneKey, tabsByWorktree
  getWorktreeIdsWithLiveAgent-->>UIComponent: Set<worktreeId>
  UIComponent->>computeVisibleWorktreeIds: opts including worktreeIdsWithLiveAgent
  computeVisibleWorktreeIds->>isInactiveWorkspace: worktreeIdsWithLiveAgent
  isInactiveWorkspace->>hasActiveWorkspaceActivity: worktreeIdsWithLiveAgent
  hasActiveWorkspaceActivity-->>isInactiveWorkspace: hasLiveAgent || other activity
  isInactiveWorkspace-->>computeVisibleWorktreeIds: inactive boolean
  computeVisibleWorktreeIds-->>UIComponent: visible worktree ids

Related PRs: None found in provided data.

Suggested labels: renderer, sidebar, agent-status

Suggested reviewers: None specified

Poem

A rabbit peeked at panes asleep, but agents humming, none too deep, so through the sets it hopped with care, marking worktrees "still there!" Now sidebars know who's truly awake — no sleeping tab a mistake. 🐇

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description omits required template sections like Summary, Screenshots, AI Review Report, Security Audit, and Notes. Reformat the PR description to match the template and add the missing sections, especially screenshots, AI review, security audit, and notes.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes implement #7197 by treating live agent sessions as activity so workspaces with open sessions remain visible under Hide sleeping.
Out of Scope Changes check ✅ Passed The diff stays focused on workspace visibility and supporting tests; no unrelated changes stand out.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title clearly summarizes the main change: keeping sleeping workspaces visible when they have an open agent session.
✨ Finishing Touches
📝 Generate docstrings
  • [ ] Create stacked PR
  • [ ] Commit on current branch

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.

coderabbitai[bot] avatar Jul 06 '26 06:07 coderabbitai[bot]

Maintainer review complete.

  • Unified fresh live-agent attribution across the sidebar, workspace board, jump palette, paired web, and mobile runtime paths.
  • Verified changed and unchanged snapshot scheduling, source freshness precedence, SSH/paired-web lifecycle cleanup, and bounded tab/agent scaling.
  • Full typecheck, lint/reliability gates, focused renderer/runtime suites, production web build, and Electron behavior validation passed.

GitHub does not allow the PR author to submit an approval review, so this comment records the completed maintainer review.

brennanb2025 avatar Jul 12 '26 19:07 brennanb2025

Reporter of #7197 here — thanks for picking this up, and for tracking down the real root cause. The filter/sort disagreement (agentStatusByPaneKey says working, the filter says sleeping) is exactly the thing I couldn't have pinpointed from the outside.

I've now read through the diff. The running-agent axis looks correctly closed, and gating the set on isFreshNonDoneAgentStatus so slept/hibernated workspaces still hide is the right call. Two scope questions, because I think #7197 is a bit wider than what this lands.

1. A terminal-only workspace with a transient PTY gap is still hidden

#7197 asks that a workspace with an open terminal and/or a running agent never be hidden. This PR narrows that to "an open agent session" (title included), and the code matches that narrower scope:

const hasLiveTerminal =
  ptyIdsByTabId != null && tabs.some((tab) => tabHasLivePty(ptyIdsByTabId, tab.id))
const hasBrowser = (browserTabsByWorktree?.[worktreeId] ?? []).length > 0
const hasLiveAgent = worktreeIdsWithLiveAgent.has(worktreeId)
return hasLiveTerminal || hasBrowser || hasLiveAgent

The terminal path is unchanged — it still requires a live PTY id (tabHasLivePty), and tab-has-live-pty.ts isn't touched by this PR. So the only new rescue is the agent set.

But the PR description itself lists PTY-gap causes that are not agent-specific:

  • an SSH/remote disconnect bulk-clears live PTY ids while the relay keeps the session alive (grace window),
  • a background/unmounted pane, or a post-restart tab not yet re-seeded, has no live-PTY entry though its daemon session is alive.

Those hit a workspace with just an open terminal and no agent identically: hasLiveTerminal is false, there's no agent to set hasLiveAgent, and the workspace disappears while its session is still open.

That's not hypothetical for me — most of my workspaces are on SSH/remote hosts, so the disconnect grace window is a routine path.

Is that intentionally out of scope, or should hasActiveWorkspaceActivity also treat a live daemon/relay session as activity, independent of the renderer's live-PTY map?

2. The signal is pane-scoped, so agents outside an Orca pane can't be attributed

getWorktreeIdsWithLiveAgent derives from agentStatusByPaneKey, and those entries only exist for panes Orca spawned: the installed hook posts paneKey=%ORCA_PANE_KEY% (agent-hooks/installer-utils.ts) and the server rejects anything that doesn't parse as ${tabId}:${leafUuid} (agent-hooks/server.ts). No ORCA_PANE_KEY, no entry.

That means a Claude Code session that isn't running in an Orca-spawned PTY can't produce a live-agent entry for its own worktree. Concretely, for how I work:

  • I run Claude Code's Agent View (claude agents), where each background session is its own process, not an Orca pane.
  • Those sessions routinely work in worktrees Claude Code created (.claude/worktrees/*).
  • That worktree has no tab, no PTY, no browser tab — and no agent-status entry.

So it stays classified as sleeping and hidden, even after this PR, while an agent is actively working in it.

The entry.worktreeId fallback in resolveAgentStatusWorktreeId doesn't rescue this either: it's stamped from the pane's worktree. So even if a dispatched background session inherited ORCA_PANE_KEY from the pane Agent View was launched in, it would be attributed to that pane's worktree — not the .claude/worktrees/* worktree it's actually working in.

I filed #7911 for tracking these sessions, and it could serve as the missing signal source here: claude agents --json [--cwd <path>] lists background sessions without needing a TTY and returns sessionId / cwd / status per session — e.g.

[{ "pid": 4968, "cwd": "/path/to/worktree", "kind": "interactive",
   "sessionId": "a0d8dcbf-…", "name": "orca-16", "status": "busy" }]

cwd is exactly the worktree attribution that paneKey can't provide for these.


Neither point is a blocker on what's here — the agent-in-a-pane case is real and this fixes it cleanly. I'd just like to know whether #7197 should stay open for (1) and (2), or whether you'd prefer those split into their own issues.

Related: #7911, #7912, #7913 (umbrella: #7918).

minho-pinta-sh avatar Jul 13 '26 05:07 minho-pinta-sh

Thanks for the careful review — both distinctions are correct.

This PR intentionally fixes the in-Orca-pane agent-status disagreement, and we are keeping it focused on that case. I updated the description from Closes #7197 to Partially addresses #7197.

The terminal-only SSH/reconnect case remains part of #7197: we still need an authoritative runtime/relay liveness signal for periods when the renderer PTY map is temporarily empty, without treating every retained terminal tab as live.

Agents running outside Orca panes require a separate discovery and worktree-attribution source, so we will keep that work under #7911 and the related umbrella issues rather than expanding this PR.

Thanks again for confirming the in-pane agent path and for separating the remaining cases so clearly.

brennanb2025 avatar Jul 13 '26 08:07 brennanb2025