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.

feat(pane): a bounded probe for src/core, mirroring the renderer's queryPaneWithin

enesb7ec2faef181
diff --git a/src/core/agents/pane-probe.test.ts b/src/core/agents/pane-probe.test.ts
new file mode 100644
index 000000000..dba65549f
--- /dev/null
+++ b/src/core/agents/pane-probe.test.ts
@@ -0,0 +1,70 @@
+import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
+import { probeWithin, PANE_PROBE_TIMEOUT_MS } from './pane-probe'
+
+describe('probeWithin', () => {
+ beforeEach(() => {
+ vi.useFakeTimers()
+ })
+ afterEach(() => {
+ vi.useRealTimers()
+ })
+
+ it('answers the value when the read finishes in time', async () => {
+ await expect(probeWithin(async () => 'claude', 1000)).resolves.toBe('claude')
+ })
+
+ it('answers null at the deadline when the read never finishes', async () => {
+ const p = probeWithin(() => new Promise<string>(() => {}), 1000)
+ await vi.advanceTimersByTimeAsync(999)
+ let settled = false
+ void p.then(() => {
+ settled = true
+ })
+ await Promise.resolve()
+ expect(settled).toBe(false) // still waiting — the bound is a deadline, not a poll
+ await vi.advanceTimersByTimeAsync(1)
+ await expect(p).resolves.toBeNull()
+ })
+
+ it('answers null when the read throws, rather than rejecting into the main process', async () => {
+ await expect(
+ probeWithin(async () => {
+ throw new Error('ssh: connection closed')
+ }, 1000)
+ ).resolves.toBeNull()
+ })
+
+ it('answers null when the read rejects asynchronously too', async () => {
+ await expect(probeWithin(() => Promise.reject(new Error('EPIPE')), 1000)).resolves.toBeNull()
+ })
+
+ it('gives each call its own deadline — a slow one does not shorten the next', async () => {
+ const slow = probeWithin(() => new Promise<string>(() => {}), 1000)
+ await vi.advanceTimersByTimeAsync(900)
+ const second = probeWithin(() => new Promise<string>(() => {}), 1000)
+ await vi.advanceTimersByTimeAsync(100)
+ await expect(slow).resolves.toBeNull()
+
+ let secondSettled = false
+ void second.then(() => {
+ secondSettled = true
+ })
+ await Promise.resolve()
+ expect(secondSettled).toBe(false) // 900ms of its own 1000 still to run
+ await vi.advanceTimersByTimeAsync(900)
+ await expect(second).resolves.toBeNull()
+ })
+
+ it('does not hold a timer open after a fast answer', async () => {
+ await expect(probeWithin(async () => 42, 60_000)).resolves.toBe(42)
+ // A live 60s timer here would keep the process (and, under fake timers, the suite) waiting.
+ expect(vi.getTimerCount()).toBe(0)
+ })
+
+ it('defaults to the gate deadline, which is bounded in seconds not minutes', async () => {
+ const p = probeWithin(() => new Promise<string>(() => {}))
+ await vi.advanceTimersByTimeAsync(PANE_PROBE_TIMEOUT_MS)
+ await expect(p).resolves.toBeNull()
+ expect(PANE_PROBE_TIMEOUT_MS).toBe(2000)
+ })
+})
diff --git a/src/core/agents/pane-probe.ts b/src/core/agents/pane-probe.ts
new file mode 100644
index 000000000..a29094a07
--- /dev/null
+++ b/src/core/agents/pane-probe.ts
@@ -0,0 +1,56 @@
+/**
+ * A bounded pane probe for `src/core`.
+ *
+ * `PtyManager.paneOwner` and `paneCommand` deliberately carry no deadline of their own: they are
+ * two (or one) `execFile` round-trips, and on the SSH leg those ride a ControlMaster that can be
+ * half-dead — the link is gone but the socket has not noticed. Unbounded, a wedged tmux server or a
+ * stalled master would hang the caller forever, and a delivery gate that never answers is worse
+ * than one that refuses.
+ *
+ * This is the renderer's `queryPaneWithin` (`src/renderer/terminal/agent-restart.ts`) as a generic,
+ * living in core because Global Constraint 4 forbids core importing from the renderer. The renderer
+ * keeps its own copy of the same bounded-race shape — one is bound to `Promise<string | null>` and
+ * wired into the restart poll, this one is generic and used by the delivery gate. Two callers of
+ * the same three lines, not a duplicated rule: if the shape ever changes, it changes because the
+ * hazard changed, and both should change together.
+ */
+
+/**
+ * How long the gate waits for a pane read before treating it as unknown.
+ *
+ * 2s: long enough for two ssh round-trips over a healthy ControlMaster (measured elsewhere in this
+ * repo at well under a second each), short enough that a wedged one is reported rather than sat on.
+ * The timeout is a REFUSAL, not a retry — `probeWithin` answers null and `isAgentPane(null)` is
+ * `unknown`, which the caller may retry deliberately rather than by hanging.
+ */
+export const PANE_PROBE_TIMEOUT_MS = 2000
+
+/**
+ * Run `fn`, but never wait longer than `timeoutMs`.
+ *
+ * Null on lapse AND on throw, because from the gate's point of view they are the same fact: we
+ * cannot see the pane. A rejection must never propagate — a probe failure would otherwise surface
+ * as an unhandled rejection in the main process (the same hazard `writeToSession` guards).
+ *
+ * The deadline belongs to THIS call: a second call gets its own, and a lapsed call does not poison
+ * a later one. The timer is always cleared, so a fast answer does not hold the event loop open for
+ * the remainder of the deadline.
+ */
+export async function probeWithin<T>(
+ fn: () => Promise<T>,
+ timeoutMs: number = PANE_PROBE_TIMEOUT_MS
+): Promise<T | null> {
+ let lapse: ReturnType<typeof setTimeout> | undefined
+ try {
+ return await Promise.race([
+ fn(),
+ new Promise<null>((resolve) => {
+ lapse = setTimeout(() => resolve(null), timeoutMs)
+ })
+ ])
+ } catch {
+ return null // transient failure — the same reading as "cannot see it"
+ } finally {
+ clearTimeout(lapse)
+ }
+}