From ed925681cd1fbbc5aef34539976797dc717ed2aa Mon Sep 17 00:00:00 2001 From: Test Date: Thu, 2 Apr 2026 16:56:43 -0300 Subject: [PATCH 1/3] fix: auto-resume Claude session on genie spawn instead of creating new When `genie spawn` is called for an agent that previously had a running Claude Code session (now dead), it always created a new session, losing all conversation context. This adds auto-resume detection in handleWorkerSpawn before rejectDuplicateRole cleans up the dead worker entry, preserving the claudeSessionId for resume. Also fixes sessionExists() in team-lead-command.ts to check for the team-prefixed session name format ({name}-{name}) that Claude Code actually stores. --- src/lib/team-lead-command.ts | 6 +++++- src/term-commands/agents.ts | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/lib/team-lead-command.ts b/src/lib/team-lead-command.ts index 5b9db52dc..4c11ba09b 100644 --- a/src/lib/team-lead-command.ts +++ b/src/lib/team-lead-command.ts @@ -133,7 +133,11 @@ export function sessionExists(name: string, cwd?: string): boolean { } const needle = name.toLowerCase(); - return files.some((file) => fileHasSessionName(join(projectPath, file), needle)); + return files.some((file) => { + const full = join(projectPath, file); + // Check exact name and {team}-{name} format (CC stores team-prefixed names) + return fileHasSessionName(full, needle) || fileHasSessionName(full, `${needle}-${needle}`); + }); } catch { return false; } diff --git a/src/term-commands/agents.ts b/src/term-commands/agents.ts index 23e97c166..044f21fee 100644 --- a/src/term-commands/agents.ts +++ b/src/term-commands/agents.ts @@ -1192,6 +1192,25 @@ export async function handleWorkerSpawn(name: string, options: SpawnOptions): Pr console.error('Error: --team is required (or set GENIE_TEAM, or run inside a genie session)'); return process.exit(1) as never; } + // Auto-resume: if a dead worker exists with a Claude session, resume instead of fresh spawn. + // Must run BEFORE rejectDuplicateRole which would unregister the dead worker and lose the sessionId. + { + const existing = await registry.list(); + const deadResumable = existing.find( + (w) => w.role === effectiveRole && w.team === team && w.claudeSessionId && w.provider === 'claude', + ); + if (deadResumable) { + const alive = await isPaneAlive(deadResumable.paneId); + if (!alive) { + console.log( + `Resuming existing session for "${effectiveRole}" (session: ${deadResumable.claudeSessionId.slice(0, 8)}...)`, + ); + await resumeAgent(deadResumable); + return deadResumable.id; + } + } + } + await rejectDuplicateRole(team, effectiveRole); // 2b. Override CWD with team worktree path if available. From bfea41ec2eacdc94b82a11eaa77b6275435008d3 Mon Sep 17 00:00:00 2001 From: Test Date: Thu, 2 Apr 2026 16:57:29 -0300 Subject: [PATCH 2/3] refactor: extract findDeadResumable helper to reduce cognitive complexity Moves the dead-worker detection logic from inline in handleWorkerSpawn into a standalone findDeadResumable() helper, keeping the function under the lint complexity threshold. --- src/term-commands/agents.ts | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/term-commands/agents.ts b/src/term-commands/agents.ts index 044f21fee..625425d9d 100644 --- a/src/term-commands/agents.ts +++ b/src/term-commands/agents.ts @@ -973,6 +973,21 @@ function prependEnvVars(command: string, env?: Record): string { return `env ${envArgs} ${command}`; } +/** + * Find a dead worker with a resumable Claude session for the given role/team. + * Must run BEFORE rejectDuplicateRole which would unregister the dead worker + * and lose the claudeSessionId needed for resume. + */ +async function findDeadResumable(team: string, role: string): Promise { + const existing = await registry.list(); + const candidate = existing.find( + (w) => w.role === role && w.team === team && w.claudeSessionId && w.provider === 'claude', + ); + if (!candidate) return null; + const alive = await isPaneAlive(candidate.paneId); + return alive ? null : candidate; +} + /** * Reject spawn if a live worker with the same role already exists in the team. * Dead/suspended workers (pane gone) are auto-cleaned from registry — only live panes block. @@ -1193,22 +1208,13 @@ export async function handleWorkerSpawn(name: string, options: SpawnOptions): Pr return process.exit(1) as never; } // Auto-resume: if a dead worker exists with a Claude session, resume instead of fresh spawn. - // Must run BEFORE rejectDuplicateRole which would unregister the dead worker and lose the sessionId. - { - const existing = await registry.list(); - const deadResumable = existing.find( - (w) => w.role === effectiveRole && w.team === team && w.claudeSessionId && w.provider === 'claude', + const deadResumable = await findDeadResumable(team, effectiveRole); + if (deadResumable) { + console.log( + `Resuming existing session for "${effectiveRole}" (session: ${deadResumable.claudeSessionId!.slice(0, 8)}...)`, ); - if (deadResumable) { - const alive = await isPaneAlive(deadResumable.paneId); - if (!alive) { - console.log( - `Resuming existing session for "${effectiveRole}" (session: ${deadResumable.claudeSessionId.slice(0, 8)}...)`, - ); - await resumeAgent(deadResumable); - return deadResumable.id; - } - } + await resumeAgent(deadResumable); + return deadResumable.id; } await rejectDuplicateRole(team, effectiveRole); From 83fafb96fd6ff7dfb60e8ec876cb45527d96fad5 Mon Sep 17 00:00:00 2001 From: Test Date: Thu, 2 Apr 2026 16:57:43 -0300 Subject: [PATCH 3/3] fix: use optional chain instead of non-null assertion for claudeSessionId --- src/term-commands/agents.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/term-commands/agents.ts b/src/term-commands/agents.ts index 625425d9d..6b6b6cac2 100644 --- a/src/term-commands/agents.ts +++ b/src/term-commands/agents.ts @@ -1211,7 +1211,7 @@ export async function handleWorkerSpawn(name: string, options: SpawnOptions): Pr const deadResumable = await findDeadResumable(team, effectiveRole); if (deadResumable) { console.log( - `Resuming existing session for "${effectiveRole}" (session: ${deadResumable.claudeSessionId!.slice(0, 8)}...)`, + `Resuming existing session for "${effectiveRole}" (session: ${deadResumable.claudeSessionId?.slice(0, 8)}...)`, ); await resumeAgent(deadResumable); return deadResumable.id;