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: re-verify unconfirmed agent resumes and correct transcript paths for Claude Code

0xken1530e9d7d7adca
diff --git a/cli/src/hookServer.spec.ts b/cli/src/hookServer.spec.ts
index fa0b9a2a0..f9b3a1c8a 100644
--- a/cli/src/hookServer.spec.ts
+++ b/cli/src/hookServer.spec.ts
@@ -1,6 +1,10 @@
import type { Server } from 'node:http'
-import { afterEach, describe, expect, it, vi } from 'vitest'
-import { startHookServer, type HookServerHandlers, chooseHookAgent } from './hookServer.js'
+import { afterAll, afterEach, describe, expect, it, vi } from 'vitest'
+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+import { startHookServer, type HookServerHandlers, chooseHookAgent, knownTranscriptFor } from './hookServer.js'
+import type { RegisteredSession } from './lib/registry.js'
import { env } from './config/env.js'
import { readHookCredential } from './lib/hookAuth.js'
import { CommandBarService } from './lib/commandBar.js'
@@ -243,6 +247,36 @@ describe('the Harness Store proxy', () => {
})
})
+describe('knownTranscriptFor', () => {
+ const sessionId = 'eae0ba40-3d0a-4340-9dd5-0a56ecbc080c'
+ const root = mkdtempSync(join(tmpdir(), 'hook-transcript-'))
+ const known = join(root, 'openharness', `${sessionId}.jsonl`)
+ const announced = join(root, 'openharness-cli', `${sessionId}.jsonl`)
+ mkdirSync(join(root, 'openharness'))
+ writeFileSync(known, '{}\n')
+ const row = { sessionId, transcriptPath: known } as RegisteredSession
+ afterAll(() => rmSync(root, { recursive: true, force: true }))
+
+ it('takes the resumed row\'s transcript when Claude Code announced one under the wrong project dir', () => {
+ expect(knownTranscriptFor({ engine: 'claude', sessionId, transcriptPath: announced }, row)).toBe(known)
+ })
+
+ it('keeps an announced transcript that exists', () => {
+ expect(knownTranscriptFor({ engine: 'claude', sessionId, transcriptPath: known }, { ...row, transcriptPath: join(root, 'other.jsonl') })).toBe(known)
+ })
+
+ it.each<[string, Parameters<typeof knownTranscriptFor>[0], RegisteredSession | undefined]>([
+ ['another conversation', { engine: 'claude', sessionId: 'other-conversation', transcriptPath: announced }, row],
+ ['another engine', { engine: 'codex', sessionId, transcriptPath: announced }, row],
+ ['a row whose file is not named by the conversation', { engine: 'claude', sessionId, transcriptPath: announced }, { ...row, transcriptPath: join(root, 'openharness', 'renamed.jsonl') }],
+ ['a row whose file is gone', { engine: 'claude', sessionId, transcriptPath: announced }, { ...row, transcriptPath: join(root, 'gone', `${sessionId}.jsonl`) }],
+ ['no row', { engine: 'claude', sessionId, transcriptPath: announced }, undefined],
+ ['no announcement', { engine: 'claude', sessionId }, row],
+ ])('leaves the announcement alone for %s', (_case, body, agent) => {
+ expect(knownTranscriptFor(body, agent)).toBe(body.transcriptPath)
+ })
+})
+
describe('chooseHookAgent', () => {
it('prefers caller ancestry, the evidence that cannot be guessed at', () => {
expect(chooseHookAgent(['strong'], ['weak'], 'codex')).toEqual({ agent: 'strong', reason: 'ancestry' })
diff --git a/cli/src/hookServer.ts b/cli/src/hookServer.ts
index af0c2e5fb..a44a87813 100644
--- a/cli/src/hookServer.ts
+++ b/cli/src/hookServer.ts
@@ -5,7 +5,7 @@
*/
import { existsSync } from 'node:fs'
-import { join } from 'node:path'
+import { basename, join } from 'node:path'
import http from 'node:http'
import type { AddressInfo } from 'node:net'
import { readCodexRolloutMeta } from './engines/codex/rollout.js'
@@ -288,6 +288,23 @@ function registeredHookProcess(body: RegisterInput, engine: AgentEngine): Regist
return body.tmuxPane ? registry.byPaneEngine(body.tmuxPane, engine) : undefined
}
+/**
+ * `claude --resume` from a folder other than the conversation's own: Claude Code announces a transcript
+ * under the CURRENT cwd's project dir, then keeps writing the original file (measured: a resume of
+ * 73f090ca from `cli/` announced `…-openharness-cli/73f090ca.jsonl`, and every later turn still landed
+ * in `…-openharness/73f090ca.jsonl`). The announced file never appears, the hook is dropped, and the
+ * resume is never confirmed — "Start failed" over a pane that is working. The row being resumed already
+ * knows the real file; take it when it names this very conversation.
+ */
+export function knownTranscriptFor(body: RegisterInput, agent: RegisteredSession | undefined): string | undefined {
+ const announced = body.transcriptPath
+ if (!announced || existsSync(announced) || (body.engine ?? 'claude') !== 'claude') return announced
+ const known = agent?.transcriptPath
+ if (!known || !body.sessionId || agent.sessionId !== body.sessionId) return announced
+ if (basename(known) !== `${body.sessionId}.jsonl` || !existsSync(known)) return announced
+ return known
+}
+
/**
* Register once the announced transcript exists.
*
@@ -299,8 +316,10 @@ async function awaitTranscript(body: RegisterInput, handlers: HookServerHandlers
for (let i = 0; i < TRANSCRIPT_WAIT_TRIES; i++) {
await new Promise((resolve) => { const t = setTimeout(resolve, TRANSCRIPT_WAIT_MS); t.unref?.() })
const engine = body.engine ?? 'claude'
- if (!registeredHookProcess(body, engine)) return
+ const processAgent = registeredHookProcess(body, engine)
+ if (!processAgent) return
if (isRecentlyDeleted(body.sessionId)) return
+ body.transcriptPath = knownTranscriptFor(body, processAgent)
if (!body.transcriptPath || !existsSync(body.transcriptPath)) continue
const result = registry.register(body)
if (!result) return
@@ -446,6 +465,7 @@ export function startHookServer(
body.processIdentity = processAgent.processIdentity ?? undefined
body.runtimes = processAgent.runtimes
body.primaryRuntimeKey = processAgent.primaryRuntimeKey
+ body.transcriptPath = knownTranscriptFor(body, processAgent)
// Deleting an agent no longer kills its pane, so the engine lives on for a moment and its catch
// hook still fires — and the exact process may remain alive during SIGTERM grace. Without
// this the tile the user just deleted re-registers itself and comes back.
diff --git a/cli/src/lib/resumeAgentService.spec.ts b/cli/src/lib/resumeAgentService.spec.ts
index e99bbe35a..2457c896d 100644
--- a/cli/src/lib/resumeAgentService.spec.ts
+++ b/cli/src/lib/resumeAgentService.spec.ts
@@ -208,6 +208,22 @@ describe('existing runtime and readiness verification', () => {
it('rechecks a pending allocation after reconnect without spawning again', async () => {
live(); expect(await start()).toMatchObject({ ok: true }); expect(create).not.toHaveBeenCalled()
})
+ it('asks a live unconfirmed resume for confirmation again, withdrawing the old verdict first', async () => {
+ live({ processIdentity: identity, launch: { state: 'failed', error: 'RESUME_UNCONFIRMED', detail: 'fixture' } })
+ vi.mocked(checkPidRuntime).mockResolvedValue({ state: 'alive' })
+ // The registry hands out its live row, so read the state as each announcement is made.
+ const announced: string[] = []; vi.mocked(deps.announceSession).mockImplementation(row => { announced.push(row.launch?.state ?? '') })
+ expect(await start()).toMatchObject({ ok: true, resumed: true, session: { launch: { state: 'ready' } } })
+ expect(create).not.toHaveBeenCalled()
+ expect(announced).toEqual(['starting', 'ready'])
+ })
+ 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(registry.byAgent(saved.agentId)?.launch).toMatchObject({ state: 'failed', error: 'RESUME_SESSION_MISMATCH' })
+ expect(deps.announceSession).not.toHaveBeenCalled()
+ })
it('waits for a real readiness observation before replying', async () => {
vi.useFakeTimers(); vi.mocked(resolvePaneEngineProcess).mockResolvedValueOnce(null)
const result = start(); await vi.advanceTimersByTimeAsync(500)
diff --git a/cli/src/lib/resumeAgentService.ts b/cli/src/lib/resumeAgentService.ts
index 4ba3da2ed..5399ff8cf 100644
--- a/cli/src/lib/resumeAgentService.ts
+++ b/cli/src/lib/resumeAgentService.ts
@@ -41,6 +41,13 @@ export function createResumeAgentService(deps: ResumeAgentServiceDeps) {
// Registry observations may update the same object; pin the route being verified.
const saved = { ...entry }
const ownsRoute = () => current() && registry.byAgent(saved.agentId)?.tmuxPane === saved.tmuxPane
+ // A second look at an unconfirmed resume: the verdict is withdrawn before the check, or the
+ // readiness probe would return it straight back. The desk sees the tile leave "Start failed".
+ const unconfirmed = ownsRoute() ? registry.byAgent(saved.agentId) : undefined
+ if (unconfirmed?.launch?.state === 'failed' && unconfirmed.launch.error === 'RESUME_UNCONFIRMED') {
+ const starting = registry.setLaunch(saved.agentId, { state: 'starting' })
+ if (starting) announceSession(starting)
+ }
const result = await waitForResumedAgent(saved, {
current: ownsRoute,
session: () => registry.byAgent(saved.agentId),
diff --git a/cli/src/lib/resumeStoppedAgent.spec.ts b/cli/src/lib/resumeStoppedAgent.spec.ts
index bce9fa610..9e86a94fa 100644
--- a/cli/src/lib/resumeStoppedAgent.spec.ts
+++ b/cli/src/lib/resumeStoppedAgent.spec.ts
@@ -203,9 +203,18 @@ describe('resume refusal and readiness edge cases', () => {
}
expect(await resumeStoppedAgent(deps)).toMatchObject({ error: 'AGENT_CHANGED' }); expect(deps.launch).not.toHaveBeenCalled()
})
- it('does not call an unconfirmed failed process ready', async () => {
- const deps = fixture(); deps.live.mockReturnValue({ ...saved, resumeOnly: true, launch: { state: 'failed', error: 'RESUME_UNCONFIRMED' } })
+ it('asks a live but unconfirmed process for confirmation again instead of repeating the verdict', async () => {
+ const deps = fixture()
+ const existing: RegisteredSession = { ...saved, resumeOnly: true, launch: { state: 'failed', error: 'RESUME_UNCONFIRMED' } }
+ deps.live.mockReturnValue(existing)
+ expect(await resumeStoppedAgent(deps)).toMatchObject({ ok: true, resumed: true })
+ 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' })
+ 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 => {
const process = { pid: 4, startMarker: 'now', executable: 'codex' }
diff --git a/cli/src/lib/resumeStoppedAgent.ts b/cli/src/lib/resumeStoppedAgent.ts
index bdaa8aa9b..5d963f2e4 100644
--- a/cli/src/lib/resumeStoppedAgent.ts
+++ b/cli/src/lib/resumeStoppedAgent.ts
@@ -34,7 +34,11 @@ export async function resumeStoppedAgent(deps: ResumeStoppedDeps): Promise<Resta
if (!deps.current() || deps.live() !== existing) return resumeChanged
if (runtime.state === 'unknown') return resumeUnconfirmed
if (runtime.state === 'alive') {
- if (existing.resumeOnly && existing.launch?.state === 'failed') return resumeUnconfirmed
+ 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
+ }
return { ok: true, session: existing, resumed: true }
}
saved = { ...existing }