Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(markdown): a rejecting openExternal bridge never leaks an unhandled rejection

reviewe514b5956142
diff --git a/src/renderer/boot.tsx b/src/renderer/boot.tsx
index a4425564d..a57df30c6 100644
--- a/src/renderer/boot.tsx
+++ b/src/renderer/boot.tsx
@@ -7,7 +7,7 @@ import { ensureCodexCliCaps } from './state/codexCli'
import { initAgentResolver } from './state/agent-resolver'
import { refreshAgentEnv } from './lib/agentEnv'
import { applyWindowChrome } from './lib/windowChrome'
-import { installMarkdownLinkGuard, LOCAL_LINK_MESSAGE } from './lib/markdownLinks'
+import { installMarkdownLinkGuard, LOCAL_LINK_MESSAGE, openExternalQuietly } from './lib/markdownLinks'
import './styles.css'
import './tailwind.css'
@@ -21,10 +21,11 @@ applyWindowChrome()
// the kanban card modal) must never navigate this window: a relative link used to wipe the canvas
// on the desktop, and any link navigated the Server Edition's tab away. One delegated listener for
// every surface — contract and reasoning in lib/markdownLinks.ts. Web links go through the bridge
-// (system browser on desktop, a new tab in the Server Edition). The toast is kind 'error' because
+// (system browser on desktop, a new tab in the Server Edition), guarded so a rejecting bridge
+// cannot surface as an unhandled rejection. The toast is kind 'error' because
// that is the only kind Canvas renders — an 'info' toast would be a silent no-op.
installMarkdownLinkGuard(document, {
- openExternal: (url) => void window.nodeTerminal.shell.openExternal(url),
+ openExternal: (url) => openExternalQuietly((u) => window.nodeTerminal.shell.openExternal(u), url),
notifyLocal: () =>
window.dispatchEvent(
new CustomEvent('nodeterm:toast', { detail: { kind: 'error', message: LOCAL_LINK_MESSAGE } })
diff --git a/src/renderer/lib/markdownLinks.test.ts b/src/renderer/lib/markdownLinks.test.ts
index c4d46c33d..76ca3bbcf 100644
--- a/src/renderer/lib/markdownLinks.test.ts
+++ b/src/renderer/lib/markdownLinks.test.ts
@@ -5,6 +5,7 @@ import path from 'path'
import {
decideMarkdownLinkClick,
installMarkdownLinkGuard,
+ openExternalQuietly,
RENDERED_MARKDOWN_CONTAINERS
} from './markdownLinks'
@@ -245,3 +246,44 @@ describe('RENDERED_MARKDOWN_CONTAINERS contract', () => {
}
})
})
+
+describe('openExternalQuietly', () => {
+ // The bridge contract types openExternal as `void`, which still admits an implementation that
+ // returns a promise — and a rejected one, dropped by a `void` at the call site, surfaces as an
+ // unhandled rejection on every failed link click.
+ it('attaches a rejection handler to a promise-returning bridge', async () => {
+ // Asserted on the promise itself: whether the runner reports an unhandled rejection is its
+ // own business (vitest's did not reach a process listener here), so watch the handler land.
+ const p = Promise.reject(new Error('E_UNSUPPORTED'))
+ const then = vi.spyOn(p, 'then')
+ try {
+ // A plain function, NOT vi.fn: a vitest mock subscribes to a returned promise itself (to
+ // record its settled result), which would hand this test the very handler it looks for.
+ const seen: string[] = []
+ const open = (url: string) => (seen.push(url), p)
+ openExternalQuietly(open, 'https://example.com/')
+ expect(seen).toEqual(['https://example.com/'])
+ await new Promise((r) => setTimeout(r, 0))
+ expect(then.mock.calls.some((c) => typeof c[1] === 'function')).toBe(true)
+ } finally {
+ p.catch(() => {}) // never leak the rejection out of a RED run
+ }
+ })
+
+ it('accepts a bridge that returns nothing (desktop) and one that throws synchronously', () => {
+ const open = vi.fn(() => undefined)
+ expect(() => openExternalQuietly(open, 'https://a.test/')).not.toThrow()
+ expect(open).toHaveBeenCalledTimes(1)
+ expect(() =>
+ openExternalQuietly(() => {
+ throw new Error('bridge gone')
+ }, 'https://a.test/')
+ ).not.toThrow()
+ })
+
+ it('is what boot.tsx wires the guard to (no bare `void …openExternal(url)` left)', () => {
+ const src = fs.readFileSync(path.resolve(__dirname, '../boot.tsx'), 'utf8')
+ expect(src).toMatch(/openExternal: \(url\) => openExternalQuietly\(\(u\) => window\.nodeTerminal\.shell\.openExternal\(u\), url\)/)
+ expect(src).not.toMatch(/void window\.nodeTerminal\.shell\.openExternal/)
+ })
+})
diff --git a/src/renderer/lib/markdownLinks.ts b/src/renderer/lib/markdownLinks.ts
index 0ed9a546d..7c2792769 100644
--- a/src/renderer/lib/markdownLinks.ts
+++ b/src/renderer/lib/markdownLinks.ts
@@ -107,3 +107,20 @@ export function installMarkdownLinkGuard(doc: Document, deps: MarkdownLinkDeps):
/** The message shown for a local link. */
export const LOCAL_LINK_MESSAGE = "Local file links can't be opened from rendered markdown."
+
+/**
+ * Hand a web link to the bridge's `openExternal` without letting a failure escape as an unhandled
+ * rejection. The contract types it `void`, and both bridges today return nothing (the desktop
+ * preload is an IPC send, the Server Edition's is `window.open`) — but nothing stops an
+ * implementation from returning a promise (a `void` return type accepts one), and `void`-ing a
+ * rejected one at the call site is an unhandled rejection per failed click. `Promise.resolve` adopts a promise and wraps
+ * anything else, so `.catch` is never called on a non-promise; a synchronous throw is swallowed
+ * too. Nothing to report: the link simply does not open, as it would have after the rejection.
+ */
+export function openExternalQuietly(open: (url: string) => unknown, url: string): void {
+ try {
+ Promise.resolve(open(url)).catch(() => {})
+ } catch {
+ // A bridge that throws synchronously (torn down mid-click) — same outcome, no open.
+ }
+}