Skip to content

Builder conversation resume: restore Claude session on recovery via jsonl discovery #831

Closed

Description

@amrmelsayed

Context

Extends #829 (afx workspace recover — process revival for builders after machine reboot). #829's initial scope was process revival only: respawn a fresh claude process in the right worktree and let porch state bridge the protocol-level continuity. Conversation context — what the builder was thinking, what was tried, what feedback the user had given — was explicitly out of scope and acknowledged as a "larger separate issue."

This issue covers the conversation-resume work that was ultimately bundled into the same PR. Tracked separately so the design and implementation history are easy to find.

Problem

After process revival, the respawned builder started with no memory of its prior session. Useful at approval gates (porch's external memory bridges the gap), but materially worse for mid-phase crashes — the builder had to reconstruct intent from the worktree diff and protocol state, often re-deriving conclusions or undoing partial work.

Mechanism

Claude Code automatically persists every interactive session to

~/.claude/projects/<encoded-cwd>/<session-uuid>.jsonl

where the encoding replaces both / and . in the absolute cwd with -. The data for every prior builder session is on disk; we just hadn't been looking.

On --resume, spawn.ts now:

  1. Computes the encoded directory for the worktree
  2. Picks the most-recently-modified *.jsonl by mtime
  3. Passes the UUID through to startBuilderSession
  4. The launch script runs claude --resume <uuid> (no role injection, no initial prompt — both are already part of the saved conversation)

Backwards-compatible by design: existing builders (spawned before this code shipped) already have their jsonl on disk and resume cleanly on the first revival.

If no jsonl exists (truly fresh worktree, deleted history, etc.), spawn falls back to the prior behavior — fresh session with a generic resume-notice prompt.

Why jsonl discovery over a sidecar

Discussed at length in the #829 conversation. Trade-offs in short:

  • No spawn-time state to maintain. Claude writes the jsonl as a side effect of the original session; we only read it. No sidecar file, no SQLite column, no schema migration.
  • Backwards compat for free. Existing builders work the first time the new code runs because their jsonl was already on disk.
  • One mechanism, not two. Same helper extends to architects (single-architect workspaces; multi-architect ambiguity is a known limitation tracked separately in Workspace recover: revive Tower-managed architects after machine reboot #830).

The trade-off: when multiple jsonl files live in the same encoded-cwd directory (a worktree was reused for unrelated work), the newest wins by mtime. For builder worktrees — protocol-scoped, typically host one logical conversation — this is right nearly always. The Claude session: <prefix>… (resuming conversation) log line surfaces which UUID was picked so any mismatch is visible.

Scope

Reference

  • afx workspace recover: revive builders after machine reboot/crash #829 — original afx workspace recover feature, where this conversation-resume implementation landed.
  • Helper module: packages/codev/src/agent-farm/utils/claude-session-discovery.ts.
  • Threading: packages/codev/src/agent-farm/commands/spawn.ts (read jsonl on --resume), packages/codev/src/agent-farm/commands/spawn-worktree.ts (different launch script when resumeSessionId is provided).
  • Tests: 9 unit tests in packages/codev/src/agent-farm/__tests__/claude-session-discovery.test.ts covering encoding, missing dir, no-jsonl dir, mtime selection, and ignored non-jsonl entries.

Activity

  1. self-assigned this
    on May 23, 2026
  2. added
    type:feature
    area/tower
    and removed
    type:feature
    on May 23, 2026
  3. amrmelsayed commented on May 23, 2026

    @amrmelsayed
    CollaboratorAuthor

    Status update — implementation landed in PR #833.

    Mechanism: Claude Code automatically persists every interactive session to ~/.claude/projects/<encoded-cwd>/<uuid>.jsonl, where the encoding replaces / and . with -. On afx spawn --resume, look up that directory for the worktree, pick the newest jsonl by mtime, and pass the UUID through to startBuilderSession. The launch script then runs claude --resume <uuid> (no role injection, no initial prompt — both already in the saved conversation).

    Backwards-compatible by design: existing builders' jsonl files were already on disk before this code landed, so revival picks them up automatically — no migration needed.

    Coverage: all five revivable protocols. SPIR/ASPIR/AIR go through spawnSpec; bugfix/PIR go through spawnIssueDrivenBuilder. Both spawn sites now call the shared discoverResumeSession helper (extracted during the round-2 cmap-3 review when reviewers caught that only spawnSpec had the resume wiring originally).

    Helper layering:

    • findLatestSessionId(cwd) — pure I/O utility in utils/claude-session-discovery.ts. No logging, no gates. Importable from Tower (server-side) too.
    • discoverResumeSession(worktreePath, isResume) — spawn-CLI wrapper. Gates on --resume, delegates to the utility, logs the result. Used by both spawn callsites.

    Tests: 9 unit tests in claude-session-discovery.test.ts cover encoding edge cases, missing dir, no-jsonl dir, multi-file mtime selection, and ignored non-jsonl entries.

    Closing once PR #833 merges.

  4. added a commit that references this issue on May 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/tower

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions