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.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
fix(cli): report a resume's real verdict, and retake a reservation nobody holds
0xken152800dafa86fe
diff --git a/cli/src/lib/resumeAgentService.spec.ts b/cli/src/lib/resumeAgentService.spec.tsindex 3d5e45701..5017f57a6 100644--- a/cli/src/lib/resumeAgentService.spec.ts+++ b/cli/src/lib/resumeAgentService.spec.ts@@ -1,8 +1,9 @@import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'-import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'+import { existsSync, mkdtempSync, readFileSync, rmSync, utimesSync, writeFileSync } from 'node:fs'import { join } from 'node:path'import { tmpdir } from 'node:os'import { createResumeAgentService, type ResumeAgentServiceDeps } from './resumeAgentService.js'+import { RESUME_READINESS_BUDGET_MS } from './resumeStoppedAgent.js'import { registry, validTranscriptPath, type RegisteredSession } from './registry.js'import { env } from '../config/env.js'import { StoppedAgentStore } from './stoppedAgents.js'@@ -150,6 +151,18 @@ describe('production resume handler', () => {expect(await start()).toMatchObject({ error: 'RESUME_UNCONFIRMED' })expect(deps.relaunchOverrides).not.toHaveBeenCalled(); expect(deps.prepareSessionResume).not.toHaveBeenCalled()})+ // The other half of the rule: a reservation older than the readiness budget cannot still belong to+ // a running resume, and leaving it in place made the harness permanently unresumable — every Enter+ // refused before it looked at anything.+ it('takes over a reservation older than the readiness budget', async () => {+ deps.stoppedAgents.beginResume(saved.agentId)+ const held = join(dir, 'saved', `${saved.agentId}.resume`)+ const stale = Date.now() - RESUME_READINESS_BUDGET_MS - 60_000+ utimesSync(held, new Date(stale), new Date(stale))+ expect(await start()).toMatchObject({ ok: true })+ expect(create).toHaveBeenCalled()+ })+it.each(['provider', 'prepare', 'cancel', 'conversation'] as const)('releases the reservation before allocation after %s failure', async reason => {if (reason === 'provider') vi.mocked(deps.relaunchOverrides).mockResolvedValue({ ok: false, error: 'PROVIDER_FAILED', detail: 'fixture' })if (reason === 'prepare') vi.mocked(deps.prepareSessionResume).mockImplementation(() => { throw new Error('fixture') })@@ -256,7 +269,7 @@ describe('existing runtime and readiness verification', () => {it('does not re-verify a live process that reported another conversation', async () => {live({ processIdentity: identity, launch: { state: 'failed', error: 'RESUME_SESSION_MISMATCH', detail: 'fixture' } })vi.mocked(checkPidRuntime).mockResolvedValue({ state: 'alive' })- expect(await start()).toMatchObject({ error: 'RESUME_UNCONFIRMED' })+ expect(await start()).toMatchObject({ error: 'RESUME_SESSION_MISMATCH', detail: 'fixture' })expect(registry.byAgent(saved.agentId)?.launch).toMatchObject({ state: 'failed', error: 'RESUME_SESSION_MISMATCH' })expect(deps.announceSession).not.toHaveBeenCalled()})diff --git a/cli/src/lib/resumeAgentService.ts b/cli/src/lib/resumeAgentService.tsindex d1182218a..fa1910a2e 100644--- a/cli/src/lib/resumeAgentService.ts+++ b/cli/src/lib/resumeAgentService.ts@@ -3,7 +3,7 @@ import { isTerminalEngine } from '../engines/types.js'import { installedDsh } from '../dsh/installed.js'import { engineKeepsTranscriptFile, registry as liveRegistry, validTranscriptPath, type RegisteredSession } from './registry.js'import type { StoppedAgentStore } from './stoppedAgents.js'-import { resumeStoppedAgent, waitForResumedAgent, resumeChanged, resumeUnconfirmed } from './resumeStoppedAgent.js'+import { resumeStoppedAgent, waitForResumedAgent, resumeChanged, resumeUnconfirmed, RESUME_READINESS_BUDGET_MS } from './resumeStoppedAgent.js'import { checkPidRuntime } from './deleteAgentFallback.js'import { checkSessionRuntime, clearPaneRemainOnExit, resolvePaneEngineProcess, tmuxPaneState } from './tmux.js'import { listTmuxPanes } from './tmuxAgentDiscovery.js'@@ -39,6 +39,30 @@ export function createResumeAgentService(deps: ResumeAgentServiceDeps) {const resumeConversations = new Set<string>()/** Same short form every other `[…]` line in the daemon logs uses. */const sid = (id: string) => id.slice(0, 8)++ /**+ * A reservation nothing can still be using, taken over.+ *+ * `beginResume` reserves an agent before tmux allocation so that a crash in the window between+ * allocating a pane and persisting its row cannot be followed by a second process for the same+ * conversation. It is deliberately NOT released on an unverified outcome — which is right, and+ * which also means a reservation whose owner died leaves the harness unresumable for good: every+ * later Enter is refused before it looks at anything, with "the saved conversation has not been+ * confirmed yet" over a harness nobody is confirming.+ *+ * Age settles it. The readiness wait is the longest a resume can legitimately hold this, so an+ * older reservation belongs to an operation that is over. By then the crash window it guards is+ * closed too: a pane that outlived it has had many reconcile passes to be discovered, given a row+ * and bound to its conversation, and `canLaunch` — which has already returned true to get here —+ * checks both the registry and the saved process before any of this runs.+ */+ const retakeStaleReservation = (agentId: string): string | null => {+ const heldSince = stoppedAgents.resumeReservedAt(agentId)+ if (heldSince === null || Date.now() - heldSince <= RESUME_READINESS_BUDGET_MS) return null+ console.log(`[resume] ${sid(agentId)} taking over a reservation held since ${new Date(heldSince).toISOString()} · its owner is gone`)+ stoppedAgents.finishResume(agentId)+ return stoppedAgents.beginResume(agentId)+ }const waitForResume = async (entry: RegisteredSession, current: () => boolean) => {// Registry observations may update the same object; pin the route being verified.const saved = { ...entry }@@ -148,6 +172,7 @@ export function createResumeAgentService(deps: ResumeAgentServiceDeps) {// Reserve before preparing history or rewriting launch configuration: an unknown previous// allocation may still have a writer using those files.token = stoppedAgents.beginResume(agentId)+ if (!token) token = retakeStaleReservation(agentId)if (!token) return resumeUnconfirmedconst built = await relaunchOverrides(saved)if (!built.ok) return { ok: false, error: built.error, detail: built.detail }diff --git a/cli/src/lib/resumeStoppedAgent.spec.ts b/cli/src/lib/resumeStoppedAgent.spec.tsindex 2d28f0d9b..345ab2b4c 100644--- a/cli/src/lib/resumeStoppedAgent.spec.ts+++ b/cli/src/lib/resumeStoppedAgent.spec.ts@@ -263,9 +263,11 @@ describe('resume refusal and readiness edge cases', () => {expect(deps.waitForReady).toHaveBeenCalledWith(existing)expect(deps.launch).not.toHaveBeenCalled(); expect(deps.retain).not.toHaveBeenCalled()})- it('does not call a live process that reported another conversation ready', async () => {- const deps = fixture(); deps.live.mockReturnValue({ ...saved, resumeOnly: true, launch: { state: 'failed', error: 'RESUME_SESSION_MISMATCH' } })- expect(await resumeStoppedAgent(deps)).toMatchObject({ error: 'RESUME_UNCONFIRMED' })+ // Reported as itself, not as "not confirmed yet": the verdict is the only thing that explains the+ // pane the person is looking at, and relabelling it sent them to check a terminal that is fine.+ it('repeats the real verdict of a live process that reported another conversation', async () => {+ const deps = fixture(); deps.live.mockReturnValue({ ...saved, resumeOnly: true, launch: { state: 'failed', error: 'RESUME_SESSION_MISMATCH', detail: 'fixture detail' } })+ expect(await resumeStoppedAgent(deps)).toMatchObject({ ok: false, error: 'RESUME_SESSION_MISMATCH', detail: 'fixture detail' })expect(deps.waitForReady).not.toHaveBeenCalled(); expect(deps.launch).not.toHaveBeenCalled()})it.each(['cancelled', 'removed', 'failed', 'different engine', 'no process', 'different start', 'missing launch'] as const)('handles readiness: %s', async mode => {diff --git a/cli/src/lib/resumeStoppedAgent.ts b/cli/src/lib/resumeStoppedAgent.tsindex 2d451b819..c874e7504 100644--- a/cli/src/lib/resumeStoppedAgent.ts+++ b/cli/src/lib/resumeStoppedAgent.ts@@ -16,6 +16,10 @@ export interface ResumeStoppedDeps {launch: (saved: RegisteredSession, resumeSessionId: string | undefined) => Promise<RestartAgentReply>}+/** The longest a resume waits for its replacement process before giving up — and so the longest one+ * can legitimately hold the reservation `stoppedAgents.beginResume` takes for it. */+export const RESUME_READINESS_BUDGET_MS = 10 * 60_000+export const resumeChanged = { ok: false, error: 'AGENT_CHANGED', detail: 'The harness changed while opening. Select it again.' } as constexport const resumeUnconfirmed = { ok: false, error: 'RESUME_UNCONFIRMED', detail: 'The saved conversation has not been confirmed yet. Check the terminal, then select the harness again to check its status.' } as const@@ -36,9 +40,13 @@ export async function resumeStoppedAgent(deps: ResumeStoppedDeps): Promise<Restaif (runtime.state === 'unknown') return resumeUnconfirmedif (runtime.state === 'alive') {if (existing.resumeOnly && existing.launch?.state === 'failed') {- // Still running and never disproved — only never confirmed (its startup hook was dropped).- // Selecting it again asks for confirmation again rather than repeating the old verdict.- return existing.launch.error === 'RESUME_UNCONFIRMED' ? deps.waitForReady(existing) : resumeUnconfirmed+ // Never confirmed is not the same as disproved: the engine is running, so ask for+ // confirmation again rather than repeating the old verdict.+ if (existing.launch.error === 'RESUME_UNCONFIRMED') return deps.waitForReady(existing)+ // Any other verdict is reported as ITSELF. Relabelling them all "not confirmed yet" told+ // somebody whose resume had reopened the wrong conversation to go and check the terminal,+ // and hid the one fact that would have explained what they were looking at.+ return { ok: false, error: existing.launch.error, detail: existing.launch.detail }}return { ok: true, session: existing, resumed: true }}@@ -84,7 +92,7 @@ export interface ResumeReadinessDeps {*/export async function waitForResumedAgent(saved: RegisteredSession, deps: ResumeReadinessDeps): Promise<RestartAgentReply> {const now = deps.now ?? Date.now- const until = now() + (deps.budgetMs ?? 10 * 60_000)+ const until = now() + (deps.budgetMs ?? RESUME_READINESS_BUDGET_MS)while (now() < until) {if (!deps.current()) return resumeChangedconst process = await deps.process()diff --git a/cli/src/lib/stoppedAgents.ts b/cli/src/lib/stoppedAgents.tsindex cf20c228b..a14db21b9 100644--- a/cli/src/lib/stoppedAgents.ts+++ b/cli/src/lib/stoppedAgents.ts@@ -1,5 +1,5 @@/** Stopped work is durable history, separate from the registry of live terminal routes. */-import { closeSync, constants, fsyncSync, openSync, readdirSync, unlinkSync, writeFileSync } from 'node:fs'+import { closeSync, constants, fsyncSync, openSync, readdirSync, statSync, unlinkSync, writeFileSync } from 'node:fs'import { randomUUID } from 'node:crypto'import { dirname, join } from 'node:path'import { env } from '../config/env.js'@@ -101,6 +101,18 @@ export class StoppedAgentStore {return token}+ /**+ * When the reservation for this agent was taken, or null if there is none.+ *+ * The reservation's whole job is to outlive a crash, so the caller needs to know how long it has+ * been held: one taken by an operation that cannot still be running is protecting nothing, and a+ * harness whose reservation is never released can never be resumed again.+ */+ resumeReservedAt(agentId: string): number | null {+ if (!SAFE_ID.test(agentId)) return null+ try { return statSync(join(this.directory, `${agentId}.resume`)).mtimeMs } catch { return null }+ }+/** Clear only a verified outcome. Unknown allocation/readiness keeps its reservation. */finishResume(agentId: string, token?: string): void {if (!SAFE_ID.test(agentId)) return