Skip to content

fix(mobile): reuse host tab when viewing a live terminal from phone - #10556

Closed
innocarpe wants to merge 2 commits into
stablyai:mainfrom
innocarpe:fix/mobile-view-no-duplicate-tab
Closed

innocarpe wants to merge 2 commits into
stablyai:mainfrom
innocarpe:fix/mobile-view-no-duplicate-tab

Conversation

@innocarpe

@innocarpe innocarpe commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #10486 — viewing an active host terminal from a paired mobile client no longer opens a second desktop tab with the same session.

When mobile focus/reveal asks the host to surface a live PTY, the renderer must adopt the existing tab that already owns that PTY. Ownership was only checked via tab.ptyId / a partial map path; if the binding lived only in terminalLayoutsByTabId (or the live ptyIdsByTabId map was not consulted by the resolver), createTab({ initialPtyId }) minted a duplicate UI tab on the same content.

Changes

  • resolveTerminalTabIdForPtyId also consults ptyIdsByTabId.
  • onCreateTerminal reuses a tab resolved via that full ownership check before creating.
  • Planner state for mobile tab mounts includes ptyIdsByTabId for consistent resolution.

Test plan

  • terminal-tab-for-pty-id.test.ts (incl. live map ownership)
  • mobile-terminal-tab-mount.test.ts
  • Manual: desktop agent terminal open → view from phone → return to desktop → only one tab for that session
  • Manual: split-pane terminal leaf viewed from mobile still targets the parent tab

ELI5

Viewing a live desktop terminal from mobile opened a second duplicate tab on the host. The host now reuses the existing tab that already owns that session instead of creating another one.

@coderabbitai

coderabbitai Bot commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@innocarpe, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 14 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 83ef5695-ff06-48c3-b8dd-b2a50dece33f

📥 Commits

Reviewing files that changed from the base of the PR and between e6dbdb751ef09a1e857a09d821d95ea1c9ed6ebc and fe88b883c5f59de298fd329c6b7b866fa75389bd.

📒 Files selected for processing (3)
  • src/renderer/src/hooks/useIpcEvents.ts
  • src/renderer/src/lib/terminal-tab-for-pty-id.test.ts
  • src/renderer/src/lib/terminal-tab-for-pty-id.ts
📝 Walkthrough

Walkthrough

PTY ownership resolution now considers both live terminal layout bindings and persisted per-tab PTY mappings. Related state types and test helpers include ptyIdsByTabId, with a test covering resolution when no saved layout exists. The terminal creation IPC handler resolves an owned tab for the incoming PTY and prioritizes that tab during reuse selection.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers the summary and tests, but omits required Screenshots, AI Review Report, Security Audit, and Notes sections. Add the missing template sections, including screenshots or 'No visual change', the testing checklist, AI review report, security audit, and notes.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the mobile terminal tab reuse fix introduced by the PR.
Linked Issues check ✅ Passed The code changes directly address #10486 by reusing the existing PTY-owned tab and consulting both live and persisted PTY ownership maps.
Out of Scope Changes check ✅ Passed All changes are scoped to the duplicate mobile terminal tab fix and its supporting tests; no unrelated edits are evident.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 0938959f-4ba3-4e83-a7d3-1b886a43bcec

📥 Commits

Reviewing files that changed from the base of the PR and between 505967e and e6dbdb751ef09a1e857a09d821d95ea1c9ed6ebc.

📒 Files selected for processing (5)
  • src/renderer/src/hooks/useIpcEvents.ts
  • src/renderer/src/lib/mobile-terminal-tab-mount.test.ts
  • src/renderer/src/lib/mobile-terminal-tab-mount.ts
  • src/renderer/src/lib/terminal-tab-for-pty-id.test.ts
  • src/renderer/src/lib/terminal-tab-for-pty-id.ts

Comment thread src/renderer/src/hooks/useIpcEvents.ts Resolved
@nwparker nwparker added the bug Something isn't working label Jul 27, 2026
Mobile focus/reveal was creating a second desktop tab for the same PTY when
ownership only lived in layout or ptyIdsByTabId. Resolve ownership through
all live bindings and adopt the existing tab instead of createTab (fixes stablyai#10486).
CodeRabbit on stablyai#10556: createTerminal skipped resolveTerminalTabIdForPtyId
when a direct tab match existed, so stale layout duplicates could adopt
the first match, and ambiguous ownership returned null which then created
another tab for the same PTY.

Use resolveTerminalTabPtyOwnership as the sole decision path: owned reuses
the tab, none may create, ambiguous fails instead of minting a duplicate.
@brennanb2025

Copy link
Copy Markdown
Contributor

Thanks for digging into this @innocarpe — your diagnosis was right, and the ownership resolver here is the shape the fix needed.

I've opened #11259 with a variant. Three things I ran into building on this:

  1. The tests don't cover the change that fixes the bug. All 96 stay green with useIpcEvents.ts reverted — the coverage is on the lib functions, not the handler wiring. This repo has enough vacuous tests that it's worth checking explicitly.
  2. A stale layout row can outrank the pre-minted tabId. existingOwnedTab wins over hintedPendingTab, so a row that outlived its pane adopts the wrong tab confidently.
  3. The Ambiguous terminal tab ownership throw reaches users. focusTerminal awaits revealTerminalSession with no catch, so the renderer's error reply rejects the phone's call.

#11259 ranks bindings instead: mounted pane (ptyIdsByTabId) > recorded (tab.ptyId or layout row) > the tabId hint, which is a fallback rather than a binding — main's pty.tabId is written once at spawn and never rewritten, so it goes stale as soon as a pane is dragged to another tab. Same-tier conflicts stay ambiguous so planMobileTerminalTabMount keeps failing closed, and nothing throws.

Happy to close this in favour of that one, or fold it in — your call.

@nwparker

nwparker commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Closing as superseded — the linked issue(s) for this PR were fixed by another change that has already been merged.

Superseding merge: #11259

Thank you for the contribution! If anything remains unaddressed, please open a new issue or PR against current main.

@nwparker nwparker closed this Aug 3, 2026
@innocarpe

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed follow-up, Brennan. I agree #11259 is the stronger fix—the handler-level regression coverage, binding ranking, and non-throwing ambiguity path address the gaps in #10556 well.

With #11259 landed, closing #10556 as superseded makes sense. Glad the original diagnosis helped, and I’ll make sure future handler fixes include a regression test that fails when the wiring is reverted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Viewing an active terminal from mobile creates a duplicate desktop terminal tab

3 participants