diff options
Diffstat (limited to 'src/profile')
| -rw-r--r-- | src/profile/bind.ts | 16 | ||||
| -rw-r--r-- | src/profile/command.ts | 24 |
2 files changed, 40 insertions, 0 deletions
diff --git a/src/profile/bind.ts b/src/profile/bind.ts index 95cffaa..5382547 100644 --- a/src/profile/bind.ts +++ b/src/profile/bind.ts @@ -1,6 +1,8 @@ import type { BrowserClient } from "../client"; import type { ResultOf } from "../cdp/commands"; import { type CdpError, cdpError } from "../cdp/errors"; +import { currentCdpTarget } from "../cdp/current-target"; +import { describeCdpTarget } from "../cdp/endpoint"; import { type Result, err, ok } from "../util/result"; import { detectRunningBrowser, resolveBrowserExecutable } from "./browser-process"; import { openProfileWindow } from "./launch"; @@ -25,6 +27,20 @@ export const seedProfileWindow = async ( client: BrowserClient, pin: ProfilePin, ): Promise<Result<string, CdpError>> => { + // A pin left over from a local session is not actionable against a browser on + // another host: the launch below would start a browser HERE and then poll the + // REMOTE one for a sentinel that cannot appear, failing only after the seed + // deadline. Refuse up front and say why. + const target = await currentCdpTarget(); + if (target.kind === "remote") { + return err(cdpError( + "discovery_failed", + `the selected profile "${pin.label}" belongs to a browser on this machine, but the harness is driving ` + + `${describeCdpTarget(target)}. Run /browser-profile and clear the selection, or /browser-target local to ` + + "drive this machine's browser.", + )); + } + const connectedDir = client.userDataDir(); if (connectedDir && connectedDir !== pin.userDataDir) { return err(cdpError( diff --git a/src/profile/command.ts b/src/profile/command.ts index 270b42f..ea0ddcc 100644 --- a/src/profile/command.ts +++ b/src/profile/command.ts @@ -1,6 +1,8 @@ import type { ExtensionAPI, ExtensionContext } from "@mariozechner/pi-coding-agent"; import type { BrowserClient } from "../client"; import { discoverEndpoint } from "../cdp/discovery"; +import { currentCdpTarget } from "../cdp/current-target"; +import { describeCdpTarget } from "../cdp/endpoint"; import { type Result, err, ok } from "../util/result"; import { type BrowserProfile, listProfiles } from "./list"; import { browserNameForUserDataDir, userDataDirCandidates } from "./paths"; @@ -60,6 +62,21 @@ const resolveUserDataDir = async (client: BrowserClient): Promise<Result<string, const endpoint = await discoverEndpoint(); if (endpoint.success && endpoint.data.userDataDir) return ok(endpoint.data.userDataDir); + // A remote browser has no user-data-dir on THIS disk. Falling through to the + // local candidates below would enumerate whatever Chromium browsers happen to + // be installed here and offer their profiles for a browser on another host — + // profiles the harness is not connected to and cannot open a window in. Worse, + // pinning one sends seedProfileWindow() off to launch a local browser whose + // sentinel page can never appear over this connection. + const target = await currentCdpTarget(); + if (target.kind === "remote") { + return err( + `Profile selection needs a browser on this machine, but the harness is driving ${describeCdpTarget(target)}. ` + + "Choosing a profile works by launching the browser with --profile-directory, which only reaches a local browser. " + + "Run /browser-target local to drive this machine's browser, then /browser-profile.", + ); + } + const withProfiles: string[] = []; for (const dir of userDataDirCandidates()) { if ((await listProfiles(dir)).length > 0) withProfiles.push(dir); @@ -164,6 +181,13 @@ export const promptForProfileIfUnset = async ( client.setProfilePin(stored); return undefined; } + // Never prompt for a remote browser. Profile selection cannot act on one (see + // resolveUserDataDir), so asking would be a blocking question with no useful + // answer — and every cancelled prompt stores nothing, so it would come back + // every session. Silence is correct here: the browser keeps its own default + // profile, and /browser-status already reports which browser is being driven. + if ((await currentCdpTarget()).kind === "remote") return undefined; + if (!ctx?.hasUI) { return "No browser profile selected — using whichever window is focused. Run /browser-profile to pin one."; } |
