Auto-resume worker sessions on respawn for same issue
Problem
When an AO worker session dies (crash, rate limit, OOM, manual kill) and is respawned for the same issue, the new worker starts from scratch — zero conversation history, no knowledge of previous attempts. It re-reads the issue, re-analyzes the codebase, and potentially repeats the exact mistake that caused the previous failure.
The core gap: AO already has a fully working session restore system (ao session restore <id>) that can resume agent conversations via getRestoreCommand(). But spawn() never uses it. The automated respawn path always calls getLaunchCommand() (fresh start) — even when a previous session for the same issue exists in the archive with full conversation history on disk.
What Already Exists
restore()function (session-manager.ts:2199) — complete, tested flow that finds archived sessions, validates restorability, restores workspaces, callsgetRestoreCommand(), and resumes the agent conversationgetRestoreCommand()interface (types.ts:323) — optional method on theAgentplugin interface- Claude Code implementation — finds latest JSONL session file, returns
claude --resume <uuid> - Codex implementation — streams session file for thread ID, returns
codex resume <threadId> - Metadata archive system (
metadata.ts:184) — archives full session metadata (issue, branch, PR, agent, worktree, etc.) when sessions are killed/cleaned ao session restoreCLI command — user-facing manual restore that callsrestore()
What's Missing
1. spawn() never calls getRestoreCommand()
spawn() (session-manager.ts:1060) always uses getLaunchCommand(). It never checks for previous sessions for the same issue, never queries the archive, and never attempts to resume. The only exception is OpenCode's internal session ID reuse, which is agent-specific and doesn't go through getRestoreCommand().
2. No archive search by issue ID
There's no generic function to find archived sessions by issue ID. findOpenCodeSessionIds() does this but is hardcoded to agent=opencode. We need a general-purpose findArchivedSessionForIssue() that searches archived metadata by issue ID and agent name.
3. OpenCode and Aider lack getRestoreCommand()
Only Claude Code and Codex implement getRestoreCommand(). OpenCode has a different session reuse mechanism (via --session flag) that only works during spawn(). Aider has no resume mechanism at all.
4. No context injection fallback
When native resume isn't possible (agent doesn't support it, session files corrupted, agent was switched), there's no mechanism to tell the new worker what the previous session did. Git commits, PR info, and agent summaries are all available in the archive and on the branch — but never injected into the new worker's prompt.
Proposed Solution
Fallback chain in spawn():
1. workerRespawnStrategy == "fresh"? → skip, launch fresh (current behavior)
2. Previous archived session for this issue + agent? → NO → fresh
3. Agent implements getRestoreCommand()? → call it
├─ Returns command → native resume (claude --resume, codex resume)
└─ Returns null → fall through
4. Build context from archived metadata + git history → prepend to prompt
├─ Context available → launch with context injection
└─ No context → launch completely fresh
Changes needed:
findArchivedSessionForIssue()helper — search archives by issue ID + agent name, return most recent match- Modify
spawn()— add the fallback chain beforegetLaunchCommand()call buildPreviousSessionContext()helper — extract context from archived metadata (summary, status, PR) and git history (commits on branch, modified files)workerRespawnStrategyconfig —"resume"(default) |"context-inject"|"fresh"onProjectConfigresumedFrommetadata field — track session lineage (which session was resumed from)- (Optional) OpenCode
getRestoreCommand()— delegate togetLaunchCommand()with stored session ID
Files to modify:
packages/core/src/session-manager.ts—findArchivedSessionForIssue(),spawn()modificationpackages/core/src/session-context-builder.ts— new file forbuildPreviousSessionContext()packages/core/src/types.ts—workerRespawnStrategyonProjectConfigpackages/core/src/metadata.ts—resumedFromfieldpackages/plugins/agent-opencode/src/index.ts— optionalgetRestoreCommand()
Per-agent behavior after fix:
| Agent | Native Resume | Context Injection Fallback |
|---|---|---|
| Claude Code | claude --resume <uuid> |
If JSONL file missing/corrupt |
| Codex | codex resume <threadId> |
If session file missing |
| OpenCode | Via --session <id> (if added) |
Primary path otherwise |
| Aider | Not available | Always gets context injection |
Design Doc
Full design doc with code snippets, edge cases, testing strategy, and risks: docs/design-session-persistence-fix.md