diff --git a/src/renderer/src/lib/browser/palette/open-client-hosted-link-beside-page.ts b/src/renderer/src/lib/browser/palette/open-client-hosted-link-beside-page.ts index 2930eebe7fa..7b0a98fa3c3 100644 --- a/src/renderer/src/lib/browser/palette/open-client-hosted-link-beside-page.ts +++ b/src/renderer/src/lib/browser/palette/open-client-hosted-link-beside-page.ts @@ -1,6 +1,5 @@ import { showClientCreationActionError } from '@/lib/workspace/creation/surface-policy/client-creation-action-error' -import { runtimeEnvironmentIdOrLocalUnlessTwin } from '@/lib/workspace/ownership/worktree-runtime-owner' -import { createWebRuntimeSessionBrowserTab } from '@/runtime/browser/creation/web-runtime-browser-creation' +import { createWebRuntimeSessionBrowserTabOnHost } from '@/runtime/browser/creation/web-runtime-browser-creation' import { useAppStore } from '@/store/index' import { isClientHostedBrowserPageHandle } from '@/store/slices/browser/hosts/browser-host-state' import { findPage, findWorkspace } from '@/store/slices/browser/pages/browser-page-records' @@ -24,10 +23,7 @@ export function openClientHostedUrlBesideBrowserPage(args: { const sourcePage = findPage(store.browserPagesByWorkspace, args.sourcePageId) const handle = store.remoteBrowserPageHandlesByPageId?.[args.sourcePageId] const environmentId = - handle?.environmentId?.trim() || - runtimeEnvironmentIdOrLocalUnlessTwin(store, args.worktreeId) || - sourcePage?.browserRuntimeEnvironmentId?.trim() || - null + handle?.environmentId?.trim() || sourcePage?.browserRuntimeEnvironmentId?.trim() || null // A client-hosted row with no named runtime cannot ask the paired host, so keep the local opener. if (!environmentId) { openUrlInTabBesideBrowserPage(args) @@ -41,19 +37,21 @@ export function openClientHostedUrlBesideBrowserPage(args: { const sourceUnifiedTab = resolveBrowserSourceUnifiedTab(store, args.sourcePageId, args.worktreeId) // Why not createBrowserTab: that lands in the streamed pane, which a headless runtime cannot show. // Omit placementPreference so preparation uses the same auto choice as the new-tab button. - void createWebRuntimeSessionBrowserTab({ - worktreeId: args.worktreeId, - environmentId, - url: args.url, - ...(profileId ? { profileId } : {}), - ...(sourceUnifiedTab ? { clientAfterTabId: sourceUnifiedTab.id } : {}), - ...(args.onOpened ? { onMaterialized: args.onOpened } : {}), - focusOnCreate: args.activate, - selectWorktree: false, - stagedTitle: redactKagiSessionToken(args.url), - stagedFocusAddressBar: false, - failureLogMode: 'operation-only' - }).catch(showClientCreationActionError) + void createWebRuntimeSessionBrowserTabOnHost( + { + worktreeId: args.worktreeId, + url: args.url, + ...(profileId ? { profileId } : {}), + ...(sourceUnifiedTab ? { clientAfterTabId: sourceUnifiedTab.id } : {}), + ...(args.onOpened ? { onMaterialized: args.onOpened } : {}), + focusOnCreate: args.activate, + selectWorktree: false, + stagedTitle: redactKagiSessionToken(args.url), + stagedFocusAddressBar: false, + failureLogMode: 'operation-only' + }, + environmentId + ).catch(showClientCreationActionError) } /** Local pages keep createBrowserTab. Client-hosted pages on a paired server follow the new-tab route. */ diff --git a/src/renderer/src/runtime/browser/creation/web-runtime-browser-creation.ts b/src/renderer/src/runtime/browser/creation/web-runtime-browser-creation.ts index 767c9680917..98e37681f60 100644 --- a/src/renderer/src/runtime/browser/creation/web-runtime-browser-creation.ts +++ b/src/renderer/src/runtime/browser/creation/web-runtime-browser-creation.ts @@ -45,7 +45,15 @@ export async function createWebRuntimeSessionBrowserTab( args.environmentId?.trim() ?? useAppStore.getState().settings?.activeRuntimeEnvironmentId?.trim() ?? null - if (!environmentId || !isWebRuntimeSessionActive(environmentId)) { + return environmentId ? createWebRuntimeSessionBrowserTabOnHost(args, environmentId) : false +} + +/** Creates the tab on the named runtime, for callers that already route by the page's owner. */ +export async function createWebRuntimeSessionBrowserTabOnHost( + args: CreateWebRuntimeSessionBrowserTabArgs, + environmentId: string +): Promise { + if (!isWebRuntimeSessionActive(environmentId)) { return false } const context = createWebRuntimeBrowserCreationContext(args, environmentId) diff --git a/tests/e2e/paired-client-hosted-browser-popups.spec.ts b/tests/e2e/paired-client-hosted-browser-popups.spec.ts index 94821701e41..43d84a80902 100644 --- a/tests/e2e/paired-client-hosted-browser-popups.spec.ts +++ b/tests/e2e/paired-client-hosted-browser-popups.spec.ts @@ -1,5 +1,4 @@ import { createServer, type Server } from 'node:http' -import type { AddressInfo } from 'node:net' import type { Page } from '@stablyai/playwright-test' import { expect, test } from './helpers/orca-app' import { launchHeadlessPairedRuntimeHost } from './helpers/headless-paired-runtime-host' @@ -45,7 +44,11 @@ async function startFixture(): Promise<{ server: Server; origin: string }> { } }) await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)) - return { server, origin: `http://127.0.0.1:${(server.address() as AddressInfo).port}` } + const address = server.address() + if (!address || typeof address === 'string') { + throw new Error('fixture server has no port') + } + return { server, origin: `http://127.0.0.1:${address.port}` } } function readPagePlacements(page: Page, worktreeId: string): Promise> { @@ -105,9 +108,9 @@ async function clickInGuest( const guest = webContents .getAllWebContents() .find((contents) => contents.getType() === 'webview' && contents.getURL() === url)! - const { x, y } = (await guest.executeJavaScript( + const { x, y }: { x: number; y: number } = await guest.executeJavaScript( `(() => { const r = document.querySelector(${JSON.stringify(selector)}).getBoundingClientRect(); return { x: Math.round(r.x + r.width / 2), y: Math.round(r.y + r.height / 2) } })()` - )) as { x: number; y: number } + ) guest.sendInputEvent({ type: 'mouseDown', x, y, button: 'left', clickCount: 1 }) guest.sendInputEvent({ type: 'mouseUp', x, y, button: 'left', clickCount: 1 }) }, @@ -118,6 +121,7 @@ async function clickInGuest( test('a This-device page opens new tabs on this device and keeps OAuth popups working', async ({ testRepoPath }, testInfo) => { + // Boots a headless host and a paired client, then waits on two tab creations and a popup round trip. test.setTimeout(300_000) const { server, origin } = await startFixture() const host = await launchHeadlessPairedRuntimeHost()