Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

fix: incorporate backend-specific validation into reconciliation to prevent premature terminal agent removal

0xken15acd36176c167
diff --git a/cli/src/lib/terminalAgentReconciler.spec.ts b/cli/src/lib/terminalAgentReconciler.spec.ts
index b9bd70df4..2509e6533 100644
--- a/cli/src/lib/terminalAgentReconciler.spec.ts
+++ b/cli/src/lib/terminalAgentReconciler.spec.ts
@@ -1,6 +1,7 @@
import { describe, expect, it, vi } from 'vitest'
import type { RegisteredSession } from './registry.js'
import type { DiscoveredTerminalAgent, TerminalAgentProbe } from './terminalAgentDiscovery.js'
+import type { TerminalBackend } from './terminalBackend.js'
import { TerminalAgentReconciler } from './terminalAgentReconciler.js'
import { terminalRouteKey } from './terminalRuntime.js'
import type { TerminalRuntimeRef } from './terminalTypes.js'
@@ -44,7 +45,7 @@ describe('composite terminal reconciliation', () => {
})
await reconciler.trigger()
await reconciler.trigger()
- expect(onDormant).toHaveBeenCalledTimes(2)
+ expect(onDormant).not.toHaveBeenCalled()
expect(onRemoved).not.toHaveBeenCalled()
})
@@ -77,8 +78,10 @@ describe('composite terminal reconciliation', () => {
it('removes only after two successful negative inventories', async () => {
const current = session([herdr])
const onRemoved = vi.fn()
+ const validate = vi.fn(async () => ({ state: 'gone' as const, reason: 'process exited' }))
+ const backend = { instanceId: 'herdr:endpoint-a', validate } as unknown as TerminalBackend
const reconciler = new TerminalAgentReconciler({
- current: () => [current], backends: [], backendOrder: ['herdr'], herdrSessionOrder: ['default'],
+ current: () => [current], backends: [backend], backendOrder: ['herdr'], herdrSessionOrder: ['default'],
onDiscovered: vi.fn(), onObserved: vi.fn(), onDormant: vi.fn(), onRemoved,
probe: async () => probe([{ instanceId: 'herdr:endpoint-a', result: { state: 'available', roots: [] } }]),
})
@@ -86,6 +89,47 @@ describe('composite terminal reconciliation', () => {
expect(onRemoved).not.toHaveBeenCalled()
await reconciler.trigger()
expect(onRemoved).toHaveBeenCalledWith(current, 'terminal runtime absent after 2 confirmed scans')
+ expect(validate).toHaveBeenCalledTimes(2)
+ })
+
+ it('keeps an agent active when pane-specific validation proves a process missed by the aggregate scan', async () => {
+ const current = session([tmux])
+ const onDormant = vi.fn()
+ const onRemoved = vi.fn()
+ const validate = vi.fn(async () => ({ state: 'alive' as const }))
+ const backend = { instanceId: 'tmux:default', validate } as unknown as TerminalBackend
+ const reconciler = new TerminalAgentReconciler({
+ current: () => [current], backends: [backend], backendOrder: ['tmux'], herdrSessionOrder: [],
+ onDiscovered: vi.fn(), onObserved: vi.fn(), onDormant, onRemoved,
+ probe: async () => probe([{ instanceId: 'tmux:default', result: { state: 'available', roots: [] } }]),
+ })
+
+ await reconciler.trigger()
+ await reconciler.trigger()
+ await reconciler.trigger()
+
+ expect(validate).toHaveBeenCalledTimes(3)
+ expect(onDormant).not.toHaveBeenCalled()
+ expect(onRemoved).not.toHaveBeenCalled()
+ })
+
+ it('keeps an agent active when pane-specific validation is inconclusive', async () => {
+ const current = session([tmux])
+ const onDormant = vi.fn()
+ const onRemoved = vi.fn()
+ const validate = vi.fn(async () => ({ state: 'unknown' as const, reason: 'process table timed out' }))
+ const backend = { instanceId: 'tmux:default', validate } as unknown as TerminalBackend
+ const reconciler = new TerminalAgentReconciler({
+ current: () => [current], backends: [backend], backendOrder: ['tmux'], herdrSessionOrder: [],
+ onDiscovered: vi.fn(), onObserved: vi.fn(), onDormant, onRemoved,
+ probe: async () => probe([{ instanceId: 'tmux:default', result: { state: 'available', roots: [] } }]),
+ })
+
+ await reconciler.trigger()
+ await reconciler.trigger()
+
+ expect(onDormant).not.toHaveBeenCalled()
+ expect(onRemoved).not.toHaveBeenCalled()
})
it('refreshes a moved Herdr route on the same process without changing agent ownership', async () => {
diff --git a/cli/src/lib/terminalAgentReconciler.ts b/cli/src/lib/terminalAgentReconciler.ts
index ff434f6ee..8e366a8c7 100644
--- a/cli/src/lib/terminalAgentReconciler.ts
+++ b/cli/src/lib/terminalAgentReconciler.ts
@@ -146,6 +146,28 @@ export class TerminalAgentReconciler {
nextRuntimes = mergeTerminalRuntimes(nextRuntimes, [replacement])
continue
}
+ // The aggregate inventory/process snapshot is deliberately cheap, but it is not stronger
+ // than a backend-specific check of this exact runtime and saved process identity. New-agent
+ // creation already proved the pane this way; without the same fallback here, one snapshot
+ // miss hid that freshly adopted agent on the very next reconciliation cycle even while its
+ // Claude trust prompt remained alive in tmux.
+ const backend = this.deps.backends.find((candidate) => candidate.instanceId === target.instanceId)
+ if (backend && current.processIdentity) {
+ const validation = await backend.validate(runtime, {
+ engine: current.engine,
+ processIdentity: current.processIdentity,
+ }).catch((error: unknown) => ({
+ state: 'unknown' as const,
+ reason: error instanceof Error ? error.message : 'terminal validation failed',
+ }))
+ if (validation.state === 'alive') {
+ this.misses.delete(missKey)
+ continue
+ }
+ // A timeout/unreadable backend is not evidence that the process exited. Preserve both the
+ // route and its active UI entry, just as a failed whole-process-table read does above.
+ if (validation.state === 'unknown') continue
+ }
const misses = (this.misses.get(missKey) ?? 0) + 1
if (misses < MISS_LIMIT) {
this.misses.set(missKey, misses)
@@ -167,23 +189,22 @@ export class TerminalAgentReconciler {
runtimes: mergeTerminalRuntimes(unknownRuntimes, [...nextRuntimes, ...observed.runtimes]),
}
await this.deps.onObserved(merged, current)
- } else if (nextRuntimes.length < current.runtimes.length && nextRuntimes.length > 0) {
- await this.deps.onObserved({
- engine: current.engine,
- cwd: current.cwd ?? '',
- processIdentity: current.processIdentity!,
- args: current.processIdentity?.executable ?? '',
- resumeSessionId: null,
- runtimes: nextRuntimes,
- primaryRuntimeKey: nextRuntimes.some((runtime) => terminalRouteKey(runtime) === current.primaryRuntimeKey)
- ? current.primaryRuntimeKey
- : terminalRouteKey(nextRuntimes[0]),
- }, current)
- await this.deps.onDormant(current, 'no terminal runtime currently verifies this process')
- } else if (nextRuntimes.length === 0) {
- await this.deps.onRemoved(current, `terminal runtime absent after ${MISS_LIMIT} confirmed scans`)
- } else {
- await this.deps.onDormant(current, 'no terminal runtime currently verifies this process')
+ } else if (nextRuntimes.length < current.runtimes.length) {
+ if (nextRuntimes.length === 0) {
+ await this.deps.onRemoved(current, `terminal runtime absent after ${MISS_LIMIT} confirmed scans`)
+ } else {
+ await this.deps.onObserved({
+ engine: current.engine,
+ cwd: current.cwd ?? '',
+ processIdentity: current.processIdentity!,
+ args: current.processIdentity?.executable ?? '',
+ resumeSessionId: null,
+ runtimes: nextRuntimes,
+ primaryRuntimeKey: nextRuntimes.some((runtime) => terminalRouteKey(runtime) === current.primaryRuntimeKey)
+ ? current.primaryRuntimeKey
+ : terminalRouteKey(nextRuntimes[0]),
+ }, current)
+ }
}
}