Skip to content

fix(sidebar): stop Hide sleeping dropping a remote workspace row on a transient PTY gap - #16001

Closed
mmarabel wants to merge 3 commits into
stablyai:mainfrom
mmarabel:fix/hide-sleeping-transient-workspace-drop
Closed

mmarabel wants to merge 3 commits into
stablyai:mainfrom
mmarabel:fix/hide-sleeping-transient-workspace-drop

Conversation

@mmarabel

Copy link
Copy Markdown
Contributor

Fixes #15996.

What this changes

With Hide sleeping workspaces on, a remote workspace's sidebar row could vanish for a single frame when a different workspace was activated. Every row below it — including the next project group header — jumped up by one row height and snapped back. The reporter measured it frame by frame on a 30 fps capture: seven workspace rows on every frame except one, where the row count read six and the msn-studio header moved 750 px → 659 px → 750 px.

After this change the row stays put.

Why it happened

The hide-sleeping sweep is the only sidebar membership filter that reads volatile runtime state; every other filter is static configuration. It keeps a row visible when hasActiveWorkspaceActivity holds, which needs one of a live PTY (tabHasLivePty(ptyIdsByTabId, tab.id)), an open browser tab, or an agent whose status is fresh and non-done.

A workspace with an open-but-idle session satisfies none of those except the first, so it hangs on ptyIdsByTabId alone. That map is emptied and refilled whenever a pane rebinds its PTY, and a single commit where the entry is empty is enough to sweep the row out and back.

worktree-activity-state.ts already anticipates this class of gap:

// Why: a running agent keeps the workspace visible through brief PTY gaps
// such as an SSH reconnect or an unmounted remote pane. #7197

…but that mitigation only covers workspaces whose agent is currently working. An idle session falls through.

Approach

Removal hysteresis, not an in-flight flag.

Direct-SSH panes do expose an in-flight signal (directSshPaneRetryByTabId), but remote-runtime panes keep their recovery state in RemoteRuntimePtyRecoveryState, a class held outside the zustand store, so no sidebar selector can observe a rebind in progress. Since the reported case is a runtime host rather than direct SSH, keying on a flag would not have covered it. Delaying removal covers every rebind path without requiring the producer to advertise itself.

  • sidebar/sleeping-sweep-retention.ts — a pure state machine. A workspace that was observed active and now reads inactive gets a SLEEPING_SWEEP_SETTLE_MS (1.5 s) grace window; a workspace that was already asleep when the sidebar first saw it gets none, so this never resurrects a row that should have been hidden from the start. It reports the soonest expiry so the caller can wake.
  • visible-worktrees.ts — new sleepingSweepExemptWorktreeIds option, applied only inside the !showSleepingWorkspaces block. Deliberately not folded into the existing forcedVisibleWorktreeIds, which bypasses the repo, host and kind filters as well — a grace window must never outrank a filter the user set. Pinned by a test.
  • worktree-list/listing/use-visible-worktrees.ts — derives the retained set from the same hasActiveWorkspaceActivity predicate the sweep uses, so there is no second copy of the liveness logic to drift. A timer wakes the sweep at expiry; without it the final grace window would hold its row until some unrelated store change happened to recompute.

Only removal is delayed. A workspace appearing is still immediate.

Trade-off

Closing a workspace's last session now leaves its row visible for up to 1.5 s before it sweeps. That direction was chosen deliberately: overshooting costs a row that lingers a beat, undershooting reproduces the jitter this fixes. It is a single exported constant if it wants tuning.

Tests

  • sleeping-sweep-retention.test.ts — the state machine: single-pass drop is retained; a sustained drop sweeps at the deadline; a workspace asleep on first sight gets no grace; the update is idempotent across a double-invoked render; the soonest of several open windows is reported; grace state is dropped when a workspace leaves the candidate set.
  • hide-sleeping-transient-pty-gap.test.ts — the issue's scenario end to end through computeVisibleWorktrees: the row survives a one-commit gap in ptyIdsByTabId, still sweeps once the workspace is genuinely asleep, and a grace window does not survive the repo filter.

Both new tests were confirmed to fail against the unpatched sweep before being committed.

pnpm lint, pnpm typecheck, pnpm test and pnpm build all pass.

Agent review notes

  • Cross-platform — renderer-only, no platform branches, no path or shell handling. No metaKey/ctrlKey or accelerator surface is touched.
  • SSH / remote — this is the remote-facing half of the change. The bug is only reachable when a pane rebinds its PTY, which in practice means SSH reconnects and remote-runtime pane re-attach; the fix is host-agnostic and does not read or assume a host type. Nothing here reports on, stops or lists remote work, so the live / unverifiable / exited verdict vocabulary is not involved: an empty ptyIdsByTabId entry is explicitly not treated as evidence the session died — that is precisely the misreading being corrected.
  • Folder workspaces — the sweep operates on worktree ids without assuming a git worktree; folder workspaces flow through the same path and the existing isSleepingSweepExemptWorkspace entry-point exemption is untouched.
  • Wire compatibility — no RPC params, stream frames or published content change. Renderer-local state only, so a mixed client/host pair is unaffected.
  • Git compatibility — no Git commands added or changed.
  • Git provider — no provider-specific code; nothing GitHub-shaped.
  • Agent / integration compat — the forcedVisibleWorktreeIds path used by the agent send target is untouched and composes with the new option rather than replacing it.
  • Performance — one Set and one Map keyed by worktree id, both pruned to the candidate set each pass, so memory is bounded by workspace count. The retention pass is O(workspaces) and runs inside the memo that already recomputes on the same inputs. The wake timer is at most one outstanding setTimeout, cleared on change and on unmount, and it stops arming as soon as no grace window is open.
  • UI quality — the user-visible effect is the absence of a one-frame reflow, which a static screenshot cannot show and which does not reproduce on demand; it is covered by unit tests instead. No markup, layout, token or styling change, so docs/STYLEGUIDE.md has no bearing here.
  • Security — no new inputs, IPC surface, persistence or external calls.

Follow-up

#15999 tracks the producer side — identifying which commit empties ptyIdsByTabId for a remote pane mid-session. This PR makes the sidebar robust to it; it does not explain it.

… transient PTY gap

The hide-sleeping sweep is the only sidebar membership filter reading
volatile state. A workspace with an open-but-idle session hangs on
ptyIdsByTabId alone, so a single commit where a rebinding remote pane's
entry is empty swept the row out and back in one frame.

Delay removal instead of keying on an in-flight flag: direct-SSH panes
expose one, but remote-runtime recovery lives outside the store where no
selector can see it.

Fixes stablyai#15996

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 684c0dea-b6b4-46b1-89d1-a25e1ccb8c27
📥 Commits

Reviewing files that changed from the base of the PR and between 81be8ed and 8d6c16c.

📒 Files selected for processing (2)
  • src/renderer/src/components/sidebar/worktree-list/listing/use-visible-worktrees-retention.test.tsx
  • src/renderer/src/components/sidebar/worktree-list/listing/use-visible-worktrees.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/renderer/src/components/sidebar/worktree-list/listing/use-visible-worktrees.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

Added sleeping-sweep retention state with a 1.5-second grace period for previously active workspaces. Integrated activity detection, expiry scheduling, retained worktree IDs, and sleeping-workspace visibility filtering. Added tests for retention transitions, expiry, cleanup, transient PTY gaps, and repository filtering.

Priority: ➖ Normal

Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to 8d6c1

This change keeps a recently active workspace visible for up to 1.5 seconds when its terminal binding briefly empties, which fixes the flickering sidebar rows. No merge-blocking risk was identified.

Security Architecture Review

Security architecture risk: ⚪ Minimal · up to 8d6c1

The change temporarily preserves recently active sidebar rows without adding privileged actions. Retention remains separate from other workspace filters, and its state and timers are confined to the displayed list.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The assessed change is confined to sidebar membership for worktrees already present in renderer state. PTY activity influences row retention but does not acquire new execution authority through this path.

Trust Boundaries and Controls

  • observed — Retention exempts a row only from inactivity removal. It does not bypass the preceding device, host, repository, or explicit hide filters. These are display-selection rules; the inspected path does not turn them into authorization controls.

Resilience and Maintainability Implications

  • observed — Removed candidate IDs are pruned before retention evaluation, expiry clears their retained state, and render snapshots do not directly mutate committed state. These mechanisms contain stale-state effects to the local visibility lifecycle.
🚥 Pre-merge checks | ✅ 2 | ❌ 1 | ❓ 2

❌ Failed checks (1 warning, 2 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 30.77% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 13 functions across 6 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Linked Issues check ❓ Inconclusive The linked issue assessment could not be completed. Retry the assessment when the required review evidence is available.
Out of Scope Changes check ❓ Inconclusive The linked issue assessment could not be completed. Retry the assessment when the required review evidence is available.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the fix: preventing a remote workspace row from disappearing during a transient PTY gap when Hide sleeping is enabled.
Description check ✅ Passed The description explains the user-visible problem, cause, approach, trade-off, linked issue, tests, and reported validation. It also explains why visual proof is not practical for this transient behav…
  • Fix all pre-merge checks with AI

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 coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (2)
src/renderer/src/components/sidebar/sleeping-sweep-retention.ts (1)

1-29: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Condense the module comment.

Keep one concise comment that states the transient-PTY purpose and issue reference. The current block repeats implementation details and timing rationale.

As per coding guidelines: "Comments must be concise, non-obvious, and brief—prefer one line; do not explain obvious behavior or walk through code."

Source: Coding guidelines

src/renderer/src/components/sidebar/hide-sleeping-transient-pty-gap.test.ts (1)

22-30: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use platform-independent fixture paths.

These fixture paths hardcode /. Build them with path.join, such as path.join(tmpdir(), id), so the test data uses native paths on Windows.

As per coding guidelines: "Use path.join or Electron/Node path utilities for file paths; never assume / or \."

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 5a9516ad-e364-4bff-9e7f-1dc0304d0b19

📥 Commits

Reviewing files that changed from the base of the PR and between 4828c6b and 22f3414.

📒 Files selected for processing (5)
  • src/renderer/src/components/sidebar/hide-sleeping-transient-pty-gap.test.ts
  • src/renderer/src/components/sidebar/sleeping-sweep-retention.test.ts
  • src/renderer/src/components/sidebar/sleeping-sweep-retention.ts
  • src/renderer/src/components/sidebar/visible-worktrees.ts
  • src/renderer/src/components/sidebar/worktree-list/listing/use-visible-worktrees.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 8 remain after this review.

@nwparker

Copy link
Copy Markdown
Contributor

Thank you @mmarabel for the careful diagnosis and the hysteresis approach. Main had moved on a lot since this branch, so I rebuilt it on current main in #29325 with you credited as co-author. It's now merged. The rebuild also sweeps a workspace right away when it's deliberately slept or its last tab is closed. Closing this one in favour of it.

@nwparker nwparker closed this Oct 16, 2026
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.

[Bug]: Hide sleeping transiently drops a remote workspace row from the sidebar for one frame when switching workspaces

2 participants