Skip to content

The agent that grows with you

fix(free-tier): a failed stdio announce still shows the challenge link

kshitijk4poor4ca6f9e91c22
diff --git a/hermes_cli/anon_challenge.py b/hermes_cli/anon_challenge.py
index 31aec3ece00..98c3f439cd6 100644
--- a/hermes_cli/anon_challenge.py
+++ b/hermes_cli/anon_challenge.py
@@ -324,8 +324,13 @@ def _announce(challenge: BrowserChallenge) -> bool:
server = sys.modules.get("tui_gateway.server")
if server is None:
return False
- # The broadcast contains a wedged peer itself (per-transport); anything else is a real bug.
- server._broadcast_global_event(CHALLENGE_EVENT, challenge.as_payload())
+ try:
+ server._broadcast_global_event(CHALLENGE_EVENT, challenge.as_payload())
+ except (OSError, ValueError) as exc:
+ # The stdio channel re-raises host I/O errors (ENOSPC, a locale that can't encode the
+ # server's copy); the caller falls back to the terminal instead of losing the link.
+ logger.debug("%s not broadcast: %s", CHALLENGE_EVENT, exc)
+ return False
return True
diff --git a/tests/hermes_cli/test_anon_challenge.py b/tests/hermes_cli/test_anon_challenge.py
index face57177f3..d0d921f0db3 100644
--- a/tests/hermes_cli/test_anon_challenge.py
+++ b/tests/hermes_cli/test_anon_challenge.py
@@ -387,6 +387,17 @@ class TestPresenting:
assert sent == [(anon_challenge.CHALLENGE_EVENT, challenge.as_payload())]
assert capsys.readouterr().err == ""
+ def test_a_stdio_write_failure_falls_back_to_the_terminal(self, monkeypatch):
+ """The stdio channel re-raises host I/O errors; the link must still reach the user."""
+ from tui_gateway import server
+ opened = []
+ monkeypatch.setattr(server, "_stdio_is_rpc_channel", True)
+ monkeypatch.setattr(server, "_broadcast_global_event", lambda *_: (_ for _ in ()).throw(OSError(28, "ENOSPC")))
+ monkeypatch.setattr(anon_challenge, "_present_in_terminal", opened.append)
+ challenge = anon_challenge.BrowserChallenge(f"{PORTAL}/challenge?code=t", True, 600, 2, "m")
+ anon_challenge.present(challenge)
+ assert opened == [challenge]
+
def test_a_terminal_opens_one_tab_per_ticket_however_many_attempts_resume_it(self, monkeypatch, capsys):
import webbrowser
from hermes_cli import auth_device_flow
diff --git a/ui-tui/src/__tests__/gatewayBrowserLinks.test.ts b/ui-tui/src/__tests__/gatewayBrowserLinks.test.ts
index 0357aed439b..9b56415d182 100644
--- a/ui-tui/src/__tests__/gatewayBrowserLinks.test.ts
+++ b/ui-tui/src/__tests__/gatewayBrowserLinks.test.ts
@@ -1,7 +1,13 @@
import { describe, expect, it, vi } from 'vitest'
+import { createGatewayEventHandler } from '../app/createGatewayEventHandler.js'
import { createFreeTierChallengePresenter } from '../app/gatewayBrowserLinks.js'
+const openExternalUrlMock = vi.fn((_url: string) => true)
+vi.mock('../lib/openExternalUrl.js', () => ({
+ openExternalUrl: (url: string) => openExternalUrlMock(url)
+}))
+
const url = 'https://portal.example/challenge?code=t'
const challenge = (required: boolean) => ({
@@ -13,7 +19,7 @@ const challenge = (required: boolean) => ({
url
})
-describe('free_tier.challenge presenter', () => {
+describe('free_tier.challenge', () => {
it('shows the link and opens one tab per ticket; an optional check opens nothing', () => {
const sys = vi.fn()
const open = vi.fn()
@@ -28,4 +34,23 @@ describe('free_tier.challenge presenter', () => {
expect(sys.mock.calls.map(c => c[0]).join('\n')).toContain(url)
expect(open).toHaveBeenCalledTimes(1)
})
+
+ it('the gateway event handler routes the event to the presenter', () => {
+ const sys = vi.fn()
+
+ const ctx = {
+ composer: {},
+ gateway: { gw: { request: vi.fn() }, rpc: vi.fn() },
+ session: {},
+ submission: { submitRef: { current: vi.fn() } },
+ system: { bellOnComplete: false, sys },
+ transcript: {},
+ voice: {}
+ } as any
+
+ createGatewayEventHandler(ctx)({ payload: challenge(true), type: 'free_tier.challenge' } as any)
+
+ expect(sys).toHaveBeenCalledWith(url)
+ expect(openExternalUrlMock).toHaveBeenCalledWith(url)
+ })
})
diff --git a/ui-tui/src/app/createGatewayEventHandler.ts b/ui-tui/src/app/createGatewayEventHandler.ts
index 861b2eaff3a..433c944670e 100644
--- a/ui-tui/src/app/createGatewayEventHandler.ts
+++ b/ui-tui/src/app/createGatewayEventHandler.ts
@@ -30,7 +30,7 @@ import type { Msg, SessionInfo, SubagentProgress } from '../types.js'
import { applyConnectionRequest, applyConnectionUpdate } from './connectionOperationStore.js'
import { applyDelegationStatus, getDelegationState } from './delegationStore.js'
-import { createFreeTierChallengePresenter, presentBillingVerification } from './gatewayBrowserLinks.js'
+import { createBillingVerificationPresenter, createFreeTierChallengePresenter } from './gatewayBrowserLinks.js'
import { applyGoalSnapshot } from './goalStatus.js'
import type { GatewayEventHandlerContext, NoticeLevel } from './interfaces.js'
import { getOverlayState, patchOverlayState, SENSITIVE_PROMPTS } from './overlayStore.js'
@@ -466,7 +466,7 @@ export function createGatewayEventHandler(ctx: GatewayEventHandlerContext): (ev:
// paths can't both persist the same prompt twice.
const persistedAbandonedClarify = new Set<string>()
const showChallenge = createFreeTierChallengePresenter(sys, openExternalUrl)
- const showBillingVerification = presentBillingVerification(sys, openExternalUrl)
+ const showBillingVerification = createBillingVerificationPresenter(sys, openExternalUrl)
// When a clarify prompt is dismissed without an answer (the backend request
// timed out and returned no answer), the live ClarifyPrompt overlay is
diff --git a/ui-tui/src/app/gatewayBrowserLinks.ts b/ui-tui/src/app/gatewayBrowserLinks.ts
index 4490b827d1e..5f38c5b08ca 100644
--- a/ui-tui/src/app/gatewayBrowserLinks.ts
+++ b/ui-tui/src/app/gatewayBrowserLinks.ts
@@ -13,7 +13,7 @@ type OpenUrl = (url: string) => unknown
* billing.step_up RPC is still polling (and may outlive its 120s timeout), so the link, not the
* RPC result, is the source of truth.
*/
-export function presentBillingVerification(
+export function createBillingVerificationPresenter(
sys: Sys,
openExternalUrl: OpenUrl
): (payload: BillingStepUpVerificationPayload | undefined) => void {