Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/lib/team-lead-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
25 changes: 25 additions & 0 deletions src/term-commands/agents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,21 @@ function prependEnvVars(command: string, env?: Record<string, string>): 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<registry.Agent | null> {
const existing = await registry.list();
const candidate = existing.find(
(w) => w.role === role && w.team === team && w.claudeSessionId && w.provider === 'claude',
);
Comment on lines +983 to +985

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If multiple dead workers exist for the same role and team, find() will return the first one encountered in the registry list, which may not be the most recent session. Additionally, we should avoid resuming workers that reached a done state. Sorting by startedAt descending ensures the most recent context is resumed.

Suggested change
const candidate = existing.find(
(w) => w.role === role && w.team === team && w.claudeSessionId && w.provider === 'claude',
);
const candidate = existing
.filter((w) => w.role === role && w.team === team && w.claudeSessionId && w.provider === 'claude' && w.state !== 'done')
.sort((a, b) => b.startedAt.localeCompare(a.startedAt))[0];

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.
Expand Down Expand Up @@ -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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The auto-resume logic should respect the autoResume option. If a user explicitly passes --no-auto-resume (setting options.autoResume to false), they likely intend to start a fresh session rather than resuming a dead one.

Suggested change
const deadResumable = await findDeadResumable(team, effectiveRole);
const deadResumable = options.autoResume !== false ? await findDeadResumable(team, effectiveRole) : null;

if (deadResumable) {
Comment on lines +1211 to +1212

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Respect explicit provider override before auto-resume

Auto-resume runs before provider resolution and does not consider options.provider, so a dead Claude worker with the same role/team will be resumed even when the user explicitly requested --provider codex. This returns early and bypasses the normal spawn flow, making provider selection ineffective in this scenario. Auto-resume should be conditional on the requested/resolved provider being Claude.

Useful? React with 👍 / 👎.

console.log(
`Resuming existing session for "${effectiveRole}" (session: ${deadResumable.claudeSessionId?.slice(0, 8)}...)`,
);
await resumeAgent(deadResumable);
return deadResumable.id;
Comment on lines +1216 to +1217

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Skip auto-resume when tmux context is unavailable

This branch always calls resumeAgent for a dead Claude worker, but resumeAgent hard-exits when process.env.TMUX is unset. As a result, running genie spawn from a non-tmux shell now fails whenever a stale dead worker exists, instead of falling back to normal spawn behavior (including cases where the caller intended a fresh spawn). Gate auto-resume on a valid tmux context and continue through the regular spawn path when resume cannot run.

Useful? React with 👍 / 👎.

}

await rejectDuplicateRole(team, effectiveRole);

// 2b. Override CWD with team worktree path if available.
Expand Down
Loading