Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(desktop): forward second-instance deep links to the running app #7180

Closedmaslinedwin(GitHub profile, opens in a new tab)wants to merge 6 commits intomain fromrefs/pull/7180/headopened View on GitHub

Fixes #5978. Second-instance argv and macOS open-url now extract t3code:// URLs and dispatch them instead of only focusing the window.


Note

Medium Risk Touches OAuth/SSO routing, macOS open-url handling, and main-window creation races; mistakes could drop callbacks, load links on the wrong window, or regress WSL splash behavior.

Overview Forwards custom-scheme deep links (t3code / t3code-dev) into the already-running desktop app instead of only focusing a window.

Adds desktopProtocolUrl: parse URLs from argv (last match wins), queue/apply a single pending link when no registered main exists, and per-window loaders so dev load retries keep the deep link instead of reverting to home.

DesktopClerk.configure now handles second-instance argv (Windows/Linux) and open-url (macOS, with preventDefault) via revealAndDispatch: load on electronWindow.main only (not the WSL splash), queue + createMainIfBackendReady when needed, and reveal with no URL when argv has no link.

DesktopWindow consumes the pending URL on first main load, tracks currentLoadUrl for retries/recovery, resets to app home after a deep link did-finish-load, and uses a Semaphore on createMain / ensureMain / createMainIfBackendReady so concurrent deep-link and backend-ready paths cannot open duplicate mains.

Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 7087b0d21129246b6679d8eff9737310b5a8c99e. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).

Note

  • On Windows/Linux second-instance events, extracts a custom-scheme URL from argv and loads it in the existing main window via new helpers in desktopProtocolUrl.ts.
  • On macOS, handles the open-url app event in DesktopClerk.ts, calls preventDefault(), and dispatches the URL the same way.
  • When no main window exists yet, the URL is queued and applied after the main window is created; stale queued URLs are replaced by newer ones.
  • DesktopWindow.ts now tracks the current deep-link URL, resets to the home URL after the deep link finishes loading, and serializes main window creation to prevent duplicate windows during races.
  • Risk: main window creation paths (createMain, ensureMain, createMainIfBackendReady) are now gated by a semaphore, which changes their concurrency behavior.
Macroscope summarized 7087b0d.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Important

      Review skipped

      Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

      ⚙️ Run configuration

      Configuration used: Repository UI

      Review profile: CHILL

      Plan: Pro Plus

      Run ID: 9f3a274a-0470-459d-acd4-3c93b3bc33a5

      You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

      Use the checkbox below for a quick retry:

      • 🔍 Trigger review

      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.
    2. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    3. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Approvability

      Verdict: Needs human review

      This PR introduces new runtime behavior for forwarding deep links to an already-running desktop instance, including new event handlers (second-instance, open-url), module-level state for queuing pending URLs, and semaphore-based window creation serialization. The scope and behavioral impact warrant human review.

      You can customize Macroscope's approvability policy. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    4. Reviewed the Effect service changes in DesktopClerk.ts, DesktopWindow.ts, and the new desktopProtocolUrl.ts against the Effect service conventions. One finding: the pending protocol URL is shared between two Effect services through module-level mutable state instead of the Effect environment.

      Posted via Macroscope — Effect Service Conventions
      @@ -0,0 +7,21 @@
      7+export function extractDesktopProtocolUrl(argv: readonly string[], scheme: string): string | null {
      8+ let found: string | null = null;
      9+ for (const arg of argv) {
      10+ if (arg.length === 0 || arg.startsWith("-") || !isDesktopProtocolUrl(arg, scheme)) {
      11+ continue;
      12+ }
      13+ found = arg;
      14+ }
      15+ return found;
      16+}
      17+
      18+export function loadDesktopProtocolUrl(
      19+ window: { readonly loadURL: (url: string) => unknown },
      20+ url: string,
      21+): void {
      22+ void Promise.resolve(window.loadURL(url)).catch(() => undefined);
      23+}
      24+
      25+// Latest protocol URL received before a real main window exists (cold launch
      26+// or WSL connecting splash). DesktopWindow.createMain applies it after setMain.
      27+let pendingDesktopProtocolUrl: string | null = null;
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      This module-level mutable slot is a hidden channel between two Effect services: DesktopClerk.make writes it via queuePendingDesktopProtocolUrl and DesktopWindow.createMain reads it via applyPendingDesktopProtocolUrl, so the dependency never appears in either service's environment or type (and the tests have to reset it in beforeEach).

      Consider holding this state in a Ref created inside DesktopWindow.make (alongside splashWindowRef/backendReadyRef) and exposing something like queuePendingProtocolUrl(url) on the DesktopWindow service; DesktopClerk.configure already acquires yield* DesktopWindow.DesktopWindow, so it can call that instead of the global. The pure helpers (isDesktopProtocolUrl, extractDesktopProtocolUrl, loadDesktopProtocolUrl) can stay in this module.

      Posted via Macroscope — Effect Service Conventions

      Acknowledged. The queue still has to be readable from DesktopWindow.createMain after setMain. I left the helpers in desktopProtocolUrl.ts and serialized the create path in Clerk; moving the slot onto DesktopWindow is a follow-up if you want the dependency in the service type.

      Sorry, I'm unable to act on this request because you do not have permissions within this repository.

    5. Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

      Fix All in Cursor ❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the [Cursor dashboard](https://www.cursor.com/dashboard/bugbot). Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 5526327fce7153e09ad8b5b5ce0a0117cd45dee3. Configure [here](https://www.cursor.com/dashboard/bugbot).
    6. One convention issue in apps/desktop/src/app/desktopProtocolUrl.ts: service state/behavior is exchanged through module globals rather than the Effect environment. The pending-URL slot was already raised earlier in this PR and acknowledged as a follow-up; the new window-loader registry added since then is flagged below.

      Posted via Macroscope — Effect Service Conventions
      @@ -0,0 +10,21 @@
      10+ if (arg.length === 0 || arg.startsWith("-") || !isDesktopProtocolUrl(arg, scheme)) {
      11+ continue;
      12+ }
      13+ found = arg;
      14+ }
      15+ return found;
      16+}
      17+
      18+type DesktopProtocolWindowLoader = (url: string) => void;
      19+
      20+// DesktopWindow registers the loader that also updates the window's intended
      21+// URL, so development did-fail-load retries cannot replace a deep link with
      22+// the default home URL.
      23+const desktopProtocolWindowLoaders = new WeakMap<object, DesktopProtocolWindowLoader>();
      24+
      25+export function registerDesktopProtocolWindowLoader(
      26+ window: object,
      27+ load: DesktopProtocolWindowLoader,
      28+): void {
      29+ desktopProtocolWindowLoaders.set(window, load);
      30+}
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      This adds a second module-global channel between the two services: DesktopWindow.make writes per-window loaders into this WeakMap and DesktopClerk reaches them indirectly through loadDesktopProtocolUrl, so the dependency is invisible in both service types (and in DesktopWindow's own state, which already keeps window bookkeeping in refs).

      Consider owning the "navigate the main window to a URL" behavior on the DesktopWindow service (e.g. a loadProtocolUrl(url) operation whose implementation closes over currentLoadUrl/clearDevelopmentLoadRetry for the window it created) and having DesktopClerk.configure call it via yield* DesktopWindow.DesktopWindow, which it already acquires. isDesktopProtocolUrl / extractDesktopProtocolUrl remain fine as pure helpers here.

      Posted via Macroscope — Effect Service Conventions

      Same follow-up as the pending-URL slot. The WeakMap only exists so development retries and crash recovery share the window's intended URL after Clerk applies a deep link. DesktopClerk already acquires DesktopWindow; moving loadProtocolUrl onto that service (and folding the pending slot in with it) is the cleanup if you want the dependency in the service type. Not changing behavior in this PR.

      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Sorry, I'm unable to act on this request because you do not have permissions within this repository.

    7. Note

      🤖 GPT-5.6 Sol responding on behalf of Theo

      We're closing this PR as we clean up the T3 Code backlog. Thank you for taking the time to put this together.

      We are keeping OPEN #8246 as the complete review path for desktop thread deep links. This branch forwards second-instance links but does not own the full environment and thread route.

      If you believe we closed this in error, please reopen the PR and leave a comment explaining what we missed.

Sign in to join this conversation.