Skip to content

The agent that grows with you

fix(webapp): show the sign-in view when a tab is signed out

emozillaf283e1e076bb
diff --git a/apps/desktop/src/app/gateway/hooks/primary-reauth-notice.test.ts b/apps/desktop/src/app/gateway/hooks/primary-reauth-notice.test.ts
new file mode 100644
index 00000000000..a003bce5626
--- /dev/null
+++ b/apps/desktop/src/app/gateway/hooks/primary-reauth-notice.test.ts
@@ -0,0 +1,52 @@
+import { afterEach, expect, it, vi } from 'vitest'
+
+import { WEBAPP_LAUNCH_REQUIRED } from '@/lib/browser-launch-session'
+import { $notifications, clearNotifications } from '@/store/notifications'
+
+import { notifyPrimaryReauth } from './primary-reauth-notice'
+
+const state = vi.hoisted(() => ({ active: true, failDesktopBoot: vi.fn() }))
+
+vi.mock('@/store/gateway', async importOriginal => ({
+ ...(await importOriginal<object>()),
+ isActivePrimary: () => state.active
+}))
+
+vi.mock('@/store/boot', async importOriginal => ({
+ ...(await importOriginal<object>()),
+ failDesktopBoot: state.failDesktopBoot
+}))
+
+afterEach(() => {
+ clearNotifications()
+ state.active = true
+ state.failDesktopBoot.mockReset()
+})
+
+const errorToasts = () => $notifications.get().filter(entry => entry.kind === 'error')
+
+it('leaves a signed-out Webapp tab to its sign-in view, with no toast', () => {
+ // A toast about a remote session would be wrong there, and screen readers announce it behind the view.
+ notifyPrimaryReauth(WEBAPP_LAUNCH_REQUIRED, () => WEBAPP_LAUNCH_REQUIRED)
+
+ expect(errorToasts()).toEqual([])
+})
+
+it('hands a foreground sign-in to the boot overlay with the error current at the click', () => {
+ notifyPrimaryReauth('Your remote gateway session has expired.', () => 'latest error')
+ const [toast] = errorToasts()
+
+ expect(toast.detail).toBe('Your remote gateway session has expired.')
+ toast.action?.onClick()
+ expect(state.failDesktopBoot).toHaveBeenCalledWith('latest error')
+})
+
+it('offers Gateways settings to a parked background primary, whatever the error', () => {
+ state.active = false
+
+ for (const detail of [WEBAPP_LAUNCH_REQUIRED, 'Your remote gateway session has expired.']) {
+ notifyPrimaryReauth(detail, () => detail)
+ }
+
+ expect(errorToasts().map(toast => toast.action?.label)).toEqual(['Open Gateways', 'Open Gateways'])
+})
diff --git a/apps/desktop/src/app/gateway/hooks/primary-reauth-notice.ts b/apps/desktop/src/app/gateway/hooks/primary-reauth-notice.ts
new file mode 100644
index 00000000000..f2fccf7c5c3
--- /dev/null
+++ b/apps/desktop/src/app/gateway/hooks/primary-reauth-notice.ts
@@ -0,0 +1,27 @@
+import { translateNow } from '@/i18n'
+import { WEBAPP_LAUNCH_REQUIRED } from '@/lib/browser-launch-session'
+import { failDesktopBoot } from '@/store/boot'
+import { isActivePrimary } from '@/store/gateway'
+import { notify, RECOVERY_ACTIONS } from '@/store/notifications'
+
+/**
+ * Plain "signed out" copy for a reconnect that needs a new sign-in; the raw ticket/HTTP text stays
+ * under Details. In the foreground the boot overlay carries the sign-in flow, so the button hands
+ * off to it (desktop-14), and a Webapp tab's sign-in view already says how to sign back in. A parked
+ * background primary no longer retries by itself, so it must still offer a way to Settings.
+ */
+export function notifyPrimaryReauth(detail: string, currentError: () => string): void {
+ const foreground = isActivePrimary()
+
+ if (foreground && detail === WEBAPP_LAUNCH_REQUIRED) {return}
+
+ notify({
+ kind: 'error',
+ title: translateNow('boot.errors.gatewaySignInRequired'),
+ message: translateNow('boot.errors.gatewaySignInRequiredDetail'),
+ detail,
+ action: foreground
+ ? { label: translateNow('boot.errors.signInAgain'), onClick: () => failDesktopBoot(currentError()) }
+ : RECOVERY_ACTIONS.openGateways()
+ })
+}
diff --git a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
index a013ec93356..04b310164c5 100644
--- a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
+++ b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
@@ -105,6 +105,7 @@ import { warnIfTerminalBackendUnavailable } from '@/store/terminal-backend-warni
import { isPeerInstanceWindow, windowProfileOverride } from '@/store/windows'
import { stashGatewaySurvivor, survivorIsStale, takeGatewaySurvivor } from './gateway-hmr-survivor'
+import { notifyPrimaryReauth } from './primary-reauth-notice'
import { useConnectionsRegistry } from './use-connections-registry'
import { useDefaultProfilePreference } from './use-default-profile-preference'
@@ -565,23 +566,7 @@ export function useGatewayBoot({
primaryReauthError = err instanceof Error ? err.message : String(err)
syncPrimaryReauthError()
reauthNotified = true
- // Plain "signed out" copy; the raw ticket/HTTP text stays under
- // Details. In the foreground the boot overlay carries the sign-in
- // flow, so the button hands off to it (desktop-14). A parked
- // background primary no longer retries by itself, so it must still
- // offer a way to Settings instead of failing silently.
- notify({
- kind: 'error',
- title: translateNow('boot.errors.gatewaySignInRequired'),
- message: translateNow('boot.errors.gatewaySignInRequiredDetail'),
- detail: primaryReauthError,
- action: isActivePrimary()
- ? {
- label: translateNow('boot.errors.signInAgain'),
- onClick: () => failDesktopBoot(primaryReauthError ?? '')
- }
- : RECOVERY_ACTIONS.openGateways()
- })
+ notifyPrimaryReauth(primaryReauthError, () => primaryReauthError ?? '')
}
} finally {
reconnecting = false
diff --git a/apps/desktop/src/lib/browser-connection.ts b/apps/desktop/src/lib/browser-connection.ts
index a16fe8a6436..8707236750e 100644
--- a/apps/desktop/src/lib/browser-connection.ts
+++ b/apps/desktop/src/lib/browser-connection.ts
@@ -1,7 +1,7 @@
import { LOCAL_CONNECTION_ID } from '@hermes/shared'
import type { HermesApiRequest, HermesConnection } from '@/global'
-import { WEBAPP_LAUNCH_REQUIRED } from '@/lib/browser-launch-session'
+import { webappSignInRequired } from '@/lib/browser-launch-session'
import {
type BrowserBootstrap,
BrowserReauthRequiredError,
@@ -22,7 +22,7 @@ export function requireBrowserConnection(connectionId?: null | string): void {
}
function connectionFor(bootstrap: BrowserBootstrap, profile?: null | string): HermesConnection {
- if (!bootstrap.authRequired && !bootstrap.token) {throw new Error(WEBAPP_LAUNCH_REQUIRED)}
+ if (!bootstrap.authRequired && !bootstrap.token) {throw webappSignInRequired()}
const baseUrl = `${window.location.origin}${bootstrap.basePath}`
diff --git a/apps/desktop/src/lib/browser-launch-session.test.ts b/apps/desktop/src/lib/browser-launch-session.test.ts
index f32078c1bfe..096783810e8 100644
--- a/apps/desktop/src/lib/browser-launch-session.test.ts
+++ b/apps/desktop/src/lib/browser-launch-session.test.ts
@@ -1,9 +1,10 @@
+import { isGatewayReauthRequired } from '@hermes/shared'
import { afterEach, expect, it, vi } from 'vitest'
import { pluginSocket } from '@/api/plugins'
import { installBrowserDesktopBridge } from './browser-desktop-bridge'
-import { consumeWebappSession, watchWebappLaunchLink } from './browser-launch-session'
+import { consumeWebappSession, watchWebappLaunchLink, WEBAPP_LAUNCH_REQUIRED } from './browser-launch-session'
const win = window as Window & {
__HERMES_UI_SURFACE__?: string
@@ -192,3 +193,30 @@ it.each(['window', 'session'] as const)('hands off a private %s through a one-us
expect(Channel.current.postMessage).toHaveBeenCalledWith({ error: expect.any(String) })
expect(Channel.current.close).toHaveBeenCalledOnce()
})
+
+it('asks a tab without a working credential to sign in, so reconnecting stops', async () => {
+ // The reconnect loop stops retrying only for an error that reads as a sign-in requirement;
+ // the startup overlay then shows the Webapp sign-in view for this message.
+ const failure = (request: Promise<unknown>) => request.then(() => null, (error: unknown) => error)
+ win.__HERMES_UI_SURFACE__ = 'webapp'
+ vi.stubGlobal('fetch', vi.fn(async () => new Response('Unauthorized', { status: 401 })))
+ expect(installBrowserDesktopBridge()).toBe(true)
+
+ for (const error of [
+ await failure(window.hermesDesktop!.getConnection()),
+ await failure(window.hermesDesktop!.api({ path: '/api/status' }))
+ ]) {
+ expect(isGatewayReauthRequired(error)).toBe(true)
+ expect((error as Error).message).toBe(WEBAPP_LAUNCH_REQUIRED)
+ }
+
+ // hermes webapp --sign-out replaced the credential this tab holds.
+ window.dispatchEvent(new Event('beforeunload'))
+ Reflect.deleteProperty(window, 'hermesDesktop')
+ window.history.replaceState(null, '', `/#hermes-session=${'d'.repeat(43)}`)
+ expect(installBrowserDesktopBridge()).toBe(true)
+ const signedOut = await failure(window.hermesDesktop!.api({ path: '/api/status' }))
+
+ expect(isGatewayReauthRequired(signedOut)).toBe(true)
+ expect((signedOut as Error).message).toBe(WEBAPP_LAUNCH_REQUIRED)
+})
diff --git a/apps/desktop/src/lib/browser-launch-session.ts b/apps/desktop/src/lib/browser-launch-session.ts
index 578f7f53422..0d9509e10e2 100644
--- a/apps/desktop/src/lib/browser-launch-session.ts
+++ b/apps/desktop/src/lib/browser-launch-session.ts
@@ -1,7 +1,17 @@
+import { GatewayReauthRequiredError } from '@hermes/shared'
+
const FRAGMENT_KEY = 'hermes-session'
export const WEBAPP_LAUNCH_REQUIRED = 'Run hermes webapp on this computer to sign this browser in.'
+/**
+ * This tab holds no credential the gateway accepts, and only a new launch link signs it in. The
+ * reconnect loop stops on this error and the startup overlay shows the Webapp sign-in view.
+ */
+export function webappSignInRequired(): GatewayReauthRequiredError {
+ return new GatewayReauthRequiredError(WEBAPP_LAUNCH_REQUIRED)
+}
+
/** Pasting the launch link over a bare URL is a same-document navigation. */
export function watchWebappLaunchLink(reload: () => void): void {
const onHash = () => {
diff --git a/apps/desktop/src/lib/browser-transport.ts b/apps/desktop/src/lib/browser-transport.ts
index cdfde517620..7733f221669 100644
--- a/apps/desktop/src/lib/browser-transport.ts
+++ b/apps/desktop/src/lib/browser-transport.ts
@@ -1,7 +1,7 @@
import { BROWSER_NATIVE_DIAL, buildHermesWebSocketUrl, normalizeBasePath } from '@hermes/shared'
import type { HermesApiRequest } from '@/global'
-import { consumeWebappSession, WEBAPP_LAUNCH_REQUIRED } from '@/lib/browser-launch-session'
+import { consumeWebappSession, webappSignInRequired } from '@/lib/browser-launch-session'
import { type BrowserHostGlobals, hasBrowserHostBootstrap } from '@/lib/platform'
export interface BrowserBootstrap {
@@ -143,7 +143,7 @@ export async function browserFetch(
bootstrap: BrowserBootstrap,
request: BrowserFetchRequest
): Promise<{ response: Response; text: string }> {
- if (!bootstrap.authRequired && !bootstrap.token) {throw new Error(WEBAPP_LAUNCH_REQUIRED)}
+ if (!bootstrap.authRequired && !bootstrap.token) {throw webappSignInRequired()}
const controller = request.timeoutMs === undefined ? null : new AbortController()
const timeout = controller ? window.setTimeout(() => controller.abort(), request.timeoutMs) : null
@@ -163,7 +163,7 @@ export async function browserFetch(
const text = await response.text()
if (!response.ok) {
- if (!bootstrap.authRequired && response.status === 401) {throw new Error(WEBAPP_LAUNCH_REQUIRED)}
+ if (!bootstrap.authRequired && response.status === 401) {throw webappSignInRequired()}
const authError = reauthError(bootstrap, response, text)