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.

refactor: extract workspace validation into shared helper and apply across agent relaunch paths

0xken15b28cdacb07a0
diff --git a/cli/src/cli.ts b/cli/src/cli.ts
index 7d5274726..390f40de1 100644
--- a/cli/src/cli.ts
+++ b/cli/src/cli.ts
@@ -72,6 +72,7 @@ import { ENGINE_CLI_COMMANDS, ENGINES, PROCESS_ENGINES, engineBin, enginePathOve
import { isTerminalEngine, type AgentEngine } from './engines/types.js'
import { engineInstallRecipe } from './lib/engineInstall.js'
import { buildEngineCommandArgv, buildEngineLaunchArgv, commandAvailableInInteractiveShell, commandSupportsFlagInInteractiveShell, namedAgentArgs, permissionModeFlags } from './lib/engineLaunch.js'
+import { workspaceMissing } from './lib/workspaceCheck.js'
import { buildGridEngineLaunch, describeGridLaunch, gridConflictingEnvToClear, gridEnvVarNames, type GridLaunchMachine, type GridWebSearchStatus } from './lib/gridLaunch.js'
import { HERMES_SYSTEM_MANAGED_DIR } from './lib/gridWebMcp.js'
import { writeGridConfigDir } from './lib/gridConfigDir.js'
@@ -4048,6 +4049,10 @@ async function runForeground(session: AuthSession): Promise<void> {
return inventory.ok && inventory.panes.some((pane) => pane.tmuxPane === runtime.paneId)
},
buildLaunch: async (entry, opts) => {
+ // A folder that went away with the reboot (an unmounted volume, a workspace deleted while the
+ // daemon was down) is a named failure on the tile, not a pane that prints an error and exits.
+ const missing = workspaceMissing(entry.cwd)
+ if (missing) return { error: missing.error, detail: missing.detail }
// Mirrors `agent_create`: the same grid env/argv (and the same vendor variables cleared), or
// the same Codex profile with its hooks installed; the install check runs inside the pane's
// own shell.
@@ -4765,6 +4770,10 @@ async function runForeground(session: AuthSession): Promise<void> {
detail: 'the sqlite3 CLI is not on PATH, and a resumed opencode session keeps its model unless its store is rewritten — install sqlite3 and retry',
}
}
+ // The replacement enters the row's folder before it execs: a folder that is gone is refused here,
+ // with the other refusals, before the pane is touched or its control taken.
+ const missing = workspaceMissing(session.cwd)
+ if (missing) return missing
// Mid-turn is the one state where restarting costs real work: the conversation comes back but
// whatever the engine was doing does not. The app is told which agents these are so the user can
// move them once they are done, rather than being asked to choose between losing a turn and losing
@@ -4926,6 +4935,10 @@ async function runForeground(session: AuthSession): Promise<void> {
const current = () => operationCurrent() && sameRestartTarget(target)
const changed = { ok: false, error: 'AGENT_CHANGED', detail: 'The agent changed or stopped during restart.' } as const
if (!current()) return changed
+ // Both branches below `cd` into the row's folder before they exec, and both have already killed
+ // (or respawned over) the old process by the time that `cd` fails. Ask first, over a live agent.
+ const missing = workspaceMissing(session.cwd)
+ if (missing) return missing
const pane = session.tmuxPane
const engine = session.engine
const runtime: TmuxRuntimeRef = { backend: 'tmux', paneId: pane }
diff --git a/cli/src/lib/resumeAgentService.ts b/cli/src/lib/resumeAgentService.ts
index 5399ff8cf..3eb9e2255 100644
--- a/cli/src/lib/resumeAgentService.ts
+++ b/cli/src/lib/resumeAgentService.ts
@@ -1,5 +1,4 @@
/** Exact-resume lifecycle shared by the daemon and its isolated acceptance tests. */
-import { statSync } from 'node:fs'
import { isTerminalEngine } from '../engines/types.js'
import { installedDsh } from '../dsh/installed.js'
import { registry as liveRegistry, validTranscriptPath, type RegisteredSession } from './registry.js'
@@ -11,6 +10,7 @@ import { listTmuxPanes } from './tmuxAgentDiscovery.js'
import { enginePathOverride } from './engineBin.js'
import { engineInstallRecipe } from './engineInstall.js'
import { buildEngineLaunchArgv } from './engineLaunch.js'
+import { workspaceMissing } from './workspaceCheck.js'
import { buildHarnessSessionLabel } from './harnessSessionLabel.js'
import { createAndRegisterPane, type CreateAgentPaneDeps } from './createAgentPane.js'
import type { LaunchOverrides, LaunchOverridesResult } from './launchOverrides.js'
@@ -111,9 +111,9 @@ export function createResumeAgentService(deps: ResumeAgentServiceDeps) {
if (!tmuxBackend) return { ok: false, error: 'TMUX_UNAVAILABLE' }
if (saved.grid && !saved.gridLaunch) return { ok: false, error: 'GRID_CREDENTIAL_REQUIRED', detail: 'The saved provider configuration is unavailable.' }
if (saved.dsh && !installedDsh(saved.dsh)) return { ok: false, error: 'INVALID_DSH', detail: 'Install this harness from the Harness Store before resuming it.' }
- try {
- if (!saved.cwd || !statSync(saved.cwd).isDirectory()) return { ok: false, error: 'CWD_NOT_FOUND', detail: 'The saved project folder is no longer available.' }
- } catch { return { ok: false, error: 'CWD_NOT_FOUND', detail: 'The saved project folder is no longer available.' } }
+ if (!saved.cwd) return { ok: false, error: 'CWD_NOT_FOUND', detail: 'The saved project folder is no longer available.' }
+ const missing = workspaceMissing(saved.cwd)
+ if (missing) return missing
if (resumeSessionId && (!saved.transcriptPath || !validTranscriptPath(saved.engine, saved.transcriptPath, saved.codexHome ?? undefined))) {
return { ok: false, error: 'RESUME_UNAVAILABLE', detail: 'The saved conversation file is unavailable. Start a new conversation separately.' }
}
diff --git a/cli/src/lib/workspaceCheck.spec.ts b/cli/src/lib/workspaceCheck.spec.ts
new file mode 100644
index 000000000..c39f35574
--- /dev/null
+++ b/cli/src/lib/workspaceCheck.spec.ts
@@ -0,0 +1,28 @@
+import { describe, it, expect } from 'vitest'
+import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+import { workspaceMissing } from './workspaceCheck.js'
+
+describe('workspaceMissing', () => {
+ it('accepts a directory that exists', () => {
+ const dir = mkdtempSync(join(tmpdir(), 'harness-ws-'))
+ try { expect(workspaceMissing(dir)).toBeNull() } finally { rmSync(dir, { recursive: true, force: true }) }
+ })
+ it('is not a refusal for a row with no folder on record', () => {
+ expect(workspaceMissing(null)).toBeNull()
+ expect(workspaceMissing(undefined)).toBeNull()
+ expect(workspaceMissing('')).toBeNull()
+ })
+ it('refuses a folder that is gone', () => {
+ const dir = mkdtempSync(join(tmpdir(), 'harness-ws-'))
+ rmSync(dir, { recursive: true, force: true })
+ expect(workspaceMissing(dir)).toEqual({ ok: false, error: 'CWD_NOT_FOUND', detail: 'The saved project folder is no longer available.' })
+ })
+ it('refuses a path that is a file', () => {
+ const dir = mkdtempSync(join(tmpdir(), 'harness-ws-'))
+ const file = join(dir, 'f')
+ writeFileSync(file, '')
+ try { expect(workspaceMissing(file)?.error).toBe('CWD_NOT_FOUND') } finally { rmSync(dir, { recursive: true, force: true }) }
+ })
+})
diff --git a/cli/src/lib/workspaceCheck.ts b/cli/src/lib/workspaceCheck.ts
new file mode 100644
index 000000000..486f1562c
--- /dev/null
+++ b/cli/src/lib/workspaceCheck.ts
@@ -0,0 +1,23 @@
+import { statSync } from 'node:fs'
+
+export interface WorkspaceMissing { ok: false; error: 'CWD_NOT_FOUND'; detail: string }
+
+/**
+ * The refusal a relaunch gets when the row's working folder is gone.
+ *
+ * Every relaunch — resume, restart, retarget, a post-reboot restore — enters the row's `cwd` in the pane's
+ * shell before exec'ing the engine (`buildEngineLaunchArgv`'s `cd -- "$1" || exit 1`). Discovering that
+ * folder missing from inside the pane is the worst place to learn it: restart has already killed the
+ * running process by then, and what the frame shows is a generic "did not come back up". Asked here, before
+ * anything is signalled, it is a named refusal over an agent that is still running.
+ *
+ * `null` (no folder on record — a row written before create persisted one) is not a refusal: that row
+ * launches without a `cd`, exactly as it has been.
+ */
+export function workspaceMissing(cwd: string | null | undefined): WorkspaceMissing | null {
+ if (!cwd) return null
+ try {
+ if (statSync(cwd).isDirectory()) return null
+ } catch { /* fall through */ }
+ return { ok: false, error: 'CWD_NOT_FOUND', detail: 'The saved project folder is no longer available.' }
+}