Skip to content

fix(terminal): re-run deferred tab admission when a plan installs mid-activation - #20176

Merged
brennanb2025 merged 1 commit into
mainfrom
brennanb2025/fix-drain-wt
Sep 12, 2026
Merged

brennanb2025 merged 1 commit into
mainfrom
brennanb2025/fix-drain-wt

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​231 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​231
Prod 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​22 $\color{#cf222e}{\Huge{\mathbf{−}}}$​4 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​18

ELI5

A workspace can come up with its tab bar populated but no terminals drawn at all, and stay that way until you switch to another workspace and back. The tabs were deferred on purpose to keep the switch fast — but nothing ever came back to actually mount them.

What Changed

planColdActivationTabDeferral can install an empty allowed set, deferring every tab, so the pane filter renders none. The drain that undoes that — useActivationDeferredTabAdmission — depends only on backgroundMountRevision and renderedActiveWorktreeId, while reading the deferred set out of a mutable ref. The install bumps neither dep: the only two revision producers in the repo are the drain itself and the background-mount event path, which fires only for explicit wake/resume requests and which the activation plan never reaches.

The stranding pass is specific: the worktree is already rendered-active while the startup gate is closed, so the else branch resets lastActivationWorktreeIdRef to null; then the gate opens on the same worktree, the plan installs, and no dep changed. Nothing re-runs the drain until renderedActiveWorktreeId changes — i.e. until the user switches workspaces and back.

applyTerminalColdActivation now returns activationDeferralPlanRevision, backed by a ref in the parking foundation and incremented only when the plan actually installs (planColdActivationTabDeferral already returns true exactly when it sets the restriction, false on the threshold-miss path after deleting it). The admission effect takes that as a dep.

A ref rather than state because the cold-activation pass runs during render, where a setState would be a render-phase update. The returned number differs on the install render, so the effect re-runs after that render commits.

Why

The hook's own comment already anticipates this launch shape and relies on "re-reading on growth" — but the re-read only happens on a dep change, and the gate-open pass produces none. This closes that gap at the point where the state actually changes, rather than adding a poll or a timer.

The 4-tab admission cap (ACTIVATION_DEFERRED_ADMISSION_LIMIT) is deliberately untouched. It reproduces the warm set an eager activation used to mount, so steady-state pane, WebGL-context and heap population stay unchanged; above it, tabs still mount when revealed.

Linked Issue

No filed issue — found while diagnosing a report of a newly created workspace showing no terminal and no tabs. Note this is a second, independent defect with the same "switch away and back fixes it" signature; it is not the cause of that report, since it renders zero panes and therefore spawns no PTYs, whereas the reported occurrence did spawn them.

Visual Proof

Not captured. This needs the startup gate to open on an already-active worktree, which I could not drive through the real UI on a quiet machine. Evidence is the ablated unit test below, which exercises the real modules and the real store rather than mocks.

Happy to add a recording if a reviewer can suggest a way to force the gate ordering interactively.

Testing

New src/renderer/src/components/terminal/cold-activation-deferral-stranding.test.ts — real modules, real zustand store, no mocks of anything under test:

  1. precondition: both seeded tabs pass the real canWatcherCoverParkedTerminalTab with panes captured and authoritative snapshots primed
  2. the fix: gate-closed pass then gate-open pass on the same worktree installs the empty set, and the drain now admits the tabs without renderedActiveWorktreeId ever changing
  3. control: the same stranded state drains when the worktree bounces away and back

Ablation (measured, both arms):

  • change reverted → 1 failed / 2 passed, failing exactly at drains a plan installed by the gate-open pass without the active worktree changing (expected true to be false — deferred set survived the timers)
  • change restored → 3 passed

Other gates:

  • 11 pre-existing suites matching cold-activation|background-terminal-worktree-mount|activation-deferred: 82/82 passed
  • union of 19 affected suites with both this and the sibling creation-flow fix applied: 123/123 passed
  • pnpm tc:web: exit 0
  • pnpm run check:code-quality:changed: 0 findings across code quality, type-aware and React Doctor

One test-only edit: terminal-workspace-surface-ids.test.tsx builds a controller literal by hand and needed the new ref, or it throws at runtime.

Platform: macOS only. Renderer-only change with no platform-conditional code.

  • I manually tested these changes locally
  • Automated tests added/updated, or explained why not below

…-activation

planColdActivationTabDeferral can install an empty allowed set, deferring every
tab so the pane filter renders none. The drain that undoes that,
useActivationDeferredTabAdmission, depends only on backgroundMountRevision and
renderedActiveWorktreeId while reading the deferred set from a mutable ref, and
the install bumps neither: the only revision producers are the drain itself and
the background-mount event path, which the activation plan never reaches.

So this pass strands the workspace with zero panes: the worktree is already
rendered-active while the startup gate is closed, which resets
lastActivationWorktreeIdRef, then the gate opens on the same worktree and
installs a plan. Nothing re-runs the drain until the user switches workspaces
and back. The hook's own comment anticipates this launch shape and relies on
re-reading on growth, but that re-read only happens on a dep change.

applyTerminalColdActivation now returns activationDeferralPlanRevision, backed
by a ref in the parking foundation and incremented only when the plan actually
installs, which the admission effect takes as a dep. A ref rather than state
because the pass runs during render, where a setState would be a render-phase
update.

The 4-tab admission cap is deliberately untouched: it reproduces the warm set
an eager activation used to mount, so steady-state pane and WebGL-context
population is unchanged.

Ablated: with the change reverted the new suite's drain case fails on the
deferred set surviving the timers; the precondition and the away-and-back
control pass either way.
@coderabbitai

coderabbitai Bot commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

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: 064d3512-e516-4535-9634-c3a175cd3673

📥 Commits

Reviewing files that changed from the base of the PR and between 6252f81 and 9590017.

📒 Files selected for processing (5)
  • src/renderer/src/components/terminal-cold-activation.ts
  • src/renderer/src/components/terminal-workspace-surface-ids.test.tsx
  • src/renderer/src/components/terminal/cold-activation-deferral-stranding.test.ts
  • src/renderer/src/components/terminal/use-activation-deferred-tab-admission.ts
  • src/renderer/src/components/use-terminal-parking-foundation.ts

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


📝 Walkthrough

Walkthrough

The change adds a render-safe cold-activation plan revision ref to the terminal parking foundation. Cold activation increments the revision when it installs a deferral plan and returns the current revision. Deferred tab admission now re-runs when the revision changes. New tests cover reopening the startup gate with the same active worktree and the existing worktree-switch path.

Priority: ➖ Normal

Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to 95900

The startup-gate reopening path is covered without introducing an actionable merge risk.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description clearly explains the problem, implementation, rationale, testing, and platform scope. However, it does not provide the required linked issue, does not use the required N/A wording fo… Add a linked issue after Fixes # or document the repository-approved issue reference. For Visual Proof, provide the required before/after evidence or write exactly N/A with a brief reason if applicable. Complete the Review, Agent skill …
Docstring Coverage ⚠️ Warning Docstring coverage is 57.14% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 5 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main change: re-running deferred tab admission when a deferral plan installs during terminal activation.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description clearly explains the problem, implementation, rationale, testing, and platform scope. However, it does not provide the required linked issue, does not use the required N/A wording for visual proof, and omits the remaining template sections and checklist items.

Resolution

Add a linked issue after Fixes # or document the repository-approved issue reference. For Visual Proof, provide the required before/after evidence or write exactly N/A with a brief reason if applicable. Complete the Review, Agent skill upstream boundary, Notes, and Checklist sections.

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 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.

@pullfrog pullfrog 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.

✅ No new issues found.

Reviewed changes

This PR closes a stranding gap where a cold-activation deferral plan installs on the gate-open pass for an already-active worktree by mutating only refs, so the admission drain's effect deps never changed and the tabs stayed unmounted until the user switched workspaces and back.

  • Revision channel — applyTerminalColdActivation now captures planColdActivationTabDeferral's return, bumps a new activationDeferralPlanRevisionRef only on an actual install, and returns the count.
  • Drain dependency — useActivationDeferredTabAdmission adds activationDeferralPlanRevision as a third effect dep (with a why comment), so the drain re-runs on the install render.
  • Foundation ref — new activationDeferralPlanRevisionRef created and exported from useTerminalParkingFoundation; ref rather than state because the cold-activation pass runs during render.
  • Tests — new cold-activation-deferral-stranding.test.ts exercises the real modules and store (gate-closed pass → gate-open install → drain without any renderedActiveWorktreeId change, plus a bounce-away control), and the hand-built controller literal in terminal-workspace-surface-ids.test.tsx gains the new ref.

Verified this run: the new test passes 3/3 and an independent ablation (dropping the new dep) fails exactly the gate-open drain case (expected true to be false) while the precondition and control still pass, so the test can actually fail. Five related suites pass 45/45, pnpm tc:web exits 0, and check:code-quality:changed reports 0 findings across the five changed files.

Pullfrog  | View workflow run | Using openrouter/deepseek/deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@brennanb2025

Copy link
Copy Markdown
Contributor Author

Electron/CDP UI check (PR #20176)

Launched this worktree’s dev app hidden (ORCA_BACKGROUND_LAUNCH=1, isolated profile, CDP 127.0.0.1:9333) and attached with playwright-cli.

App identity (verified): name: "Orca: brennanb2025/fix-drain-wt", devBranch: "brennanb2025/fix-drain-wt", devWorktreeName: "fix-drain-wt", devRepoRoot: "/Users/brennanbenson/orca/workspaces/orca/fix-drain-wt".

Scenario attempted

The defect is a startup-gate ordering: the worktree is already rendered-active while the gate is closed (which resets lastActivationWorktreeIdRef), then the gate opens on the same worktree, planColdActivationTabDeferral installs an empty allowed set, and the admission drain used to never re-run because neither backgroundMountRevision nor renderedActiveWorktreeId changed.

That exact ordering could not be forced through the real UI on this quiet isolated launch. First paint of the isolated profile had no workspaces/tabs (hydrationSucceeded, workspaceSessionReady, and startupWorktreeRefreshCompleted were already true with activeWorktreeId: null).

What I could drive:

  1. Add a small fixture git repo via IPC (folder picker is native and not CDP-safe), then click the sidebar workspace.
  2. A Terminal 1 pane drew; a second tab Terminal 2 was created from the tab bar. Resource Manager went to 2 sessions.
  3. Switch to a second worktree and back (the old workaround / control): both tabs and the terminal input remained drawn.
  4. Renderer reload (window.api.app.reload()): first observable frame already had both tabs, 2 .xterm nodes, and a terminal input. No workspace switch was required after reload.

Visible evidence

Isolated launch (empty profile, this branch’s app):

01-empty-isolated-profile.png

Two tabs + drawn pane after activating the fixture workspace:

03-two-terminal-tabs.png

After switching away and back:

05-after-switch-back.png

After renderer reload, both tabs and the pane still drawn (no second workspace switch):

06-after-reload-both-tabs.png

Console / app errors

  • Playwright console: 0 errors. Warnings only: React tooltip controlled/uncontrolled, React Grab outdated, one xterm.js: task queue exceeded allotted deadline.
  • Main log: MaxListenersExceededWarning on BrowserWindow closed; FileListingCancelledError from fs:listFiles when switching workspaces (expected cancel). No renderer crash and no terminal-admission exception.

Gaps (do not treat this as proof of the mid-activation drain)

  • Did not observe the stranded UI (tab bar populated, zero panes) that this PR fixes. That needs the gate-open pass on an already-active worktree with an empty immediate-tab set; this machine never showed that frame.
  • Post-reload success does not isolate activationDeferralPlanRevision as the cause. Reload can also change renderedActiveWorktreeId, which was already a drain dep. The new unit suite remains the evidence for the install-without-worktree-change path.
  • Did not slow or intercept the startup gate from the UI, and did not run this against a restored real user profile (isolated profile on purpose).

No production code or tests were modified.

@brennanb2025

Copy link
Copy Markdown
Contributor Author

Review status: PASS — land\n\n- Independent same-model senior review completed clean; no production fixes were warranted and no scope expansion was made.\n- Readiness checklist passed at start and end with no proven P0/P1/P2 findings. No counsel packet was needed.\n- Full terminal suite: 488 files / 4,622 tests passed.\n- Focused regression suites: 3 files / 16 tests passed.\n- Web typecheck and changed-file oxlint passed; passed.\n- Review covered activation gating, worktree bounce, deferral-disabled paths, >4-tab admission cap, pruning/cleanup, cross-platform/remote boundaries, and performance. The change adds only a scalar revision/ref and the intended one-per-idle-tab drain; no new scans, IPC, retries, or resource lifetimes.\n- Electron/CDP evidence with full-window screenshots is in the UI validation comment. It verifies the intended worktree identity, terminal tabs rendering after activation, survival across workspace switching, and rendering after reload with 0 console errors. The exact startup-gate-open-on-an-already-active-worktree race was not reproducible in the isolated profile, so the new regression test remains the direct proof for that path.\n- No SSH/mobile/windows validation was needed: the diff is renderer-only and does not change RPC, relay, filesystem, or platform-specific contracts.\n\nFinal tree is clean and the PR head remains unchanged.

@brennanb2025
brennanb2025 merged commit 353a1c8 into main Sep 12, 2026
29 checks passed
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