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..6b6b6cac2 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. @@ -1192,6 +1207,16 @@ 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. + const deadResumable = await findDeadResumable(team, effectiveRole); + if (deadResumable) { + 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.