agent-orchestrator icon indicating copy to clipboard operation
agent-orchestrator copied to clipboard

Auto-resume worker sessions on respawn for same issue

Open whoisasx opened this issue 8 months ago • 0 comments

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, calls getRestoreCommand(), and resumes the agent conversation
  • getRestoreCommand() interface (types.ts:323) — optional method on the Agent plugin 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 restore CLI command — user-facing manual restore that calls restore()

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:

  1. findArchivedSessionForIssue() helper — search archives by issue ID + agent name, return most recent match
  2. Modify spawn() — add the fallback chain before getLaunchCommand() call
  3. buildPreviousSessionContext() helper — extract context from archived metadata (summary, status, PR) and git history (commits on branch, modified files)
  4. workerRespawnStrategy config — "resume" (default) | "context-inject" | "fresh" on ProjectConfig
  5. resumedFrom metadata field — track session lineage (which session was resumed from)
  6. (Optional) OpenCode getRestoreCommand() — delegate to getLaunchCommand() with stored session ID

Files to modify:

  • packages/core/src/session-manager.ts — findArchivedSessionForIssue(), spawn() modification
  • packages/core/src/session-context-builder.ts — new file for buildPreviousSessionContext()
  • packages/core/src/types.ts — workerRespawnStrategy on ProjectConfig
  • packages/core/src/metadata.ts — resumedFrom field
  • packages/plugins/agent-opencode/src/index.ts — optional getRestoreCommand()

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

whoisasx avatar Mar 30 '26 13:03 whoisasx