Fix Claude restore cwd drift for session resume

Merged
AU
5mo ago
+2460-133

Summary

  • Reproduced #6194 locally in cmux96: running claude --resume / cmux claude-teams --resume for a valid Claude session from a drifted cwd reports No conversation found with session ID.
  • Normalize restored agent-hook bindings through AgentResumeWorkingDirectory so directory-namespaced agents like Claude resume from the captured launch cwd instead of a drifted runtime cwd.
  • Add a regression test covering stale Claude hook bindings during session restore.

Testing

  • git show --check --stat --oneline HEAD && git show --check --stat --oneline HEAD~1
  • Not run: local test suite per task instructions.

Fixes #6194


View with Codesmith Autofix with Codesmith
Need help on this PR? Tag /codesmith with what you need. Autofix is disabled.


Summary by cubic

Fix Claude session resume when cwd/workspace/surface or CLAUDE_CONFIG_DIR drift, and preserve auth selection on resume. Restores reliably return to the intended conversation and avoid “No conversation found”. Fixes #6194.

  • Bug Fixes
    • Retarget agent‑hook resume bindings to the snapshot’s restorable cwd (fallback to launch cwd); non‑agent bindings unchanged.
    • Surface resolution: strict --surface with localized errors; invalid explicit → mapped session else error. No flags: mapped session > Claude process binding (TTY, then PID with socket auth) > caller TTY > env. Ambient TTY is ignored when workspace/surface flags are set; stale TTY workspace bindings are ignored.
    • Session start prefers the Claude TTY over leaked env; if no TTY marker exists, PID binding is used. Persist workspace_id.
    • cmux-claude-wrapper: on --resume <id>, self‑heal CLAUDE_CONFIG_DIR to the root that holds the transcript only when the current root lacks it; validate the id token before searching; bound the resume lookup to avoid scanning unrelated paths; tolerate value flags before --resume; stop parsing at prompt text and after --; preserve safe auth selection values on resume; define the resume parser before passthrough.
    • Workspace: drop incompatible restored agent snapshots when the resume binding changes to prevent mismatched restores.
    • zsh integration reinstalls the claude/grok wrappers after startup so user functions don’t override wrapper dispatch.

Summary by CodeRabbit

  • New Features / Improvements
    • Improved terminal session restoration for agent-hook resumes by retargeting the restored working directory to match the launch context, even when the runtime working directory has drifted.
    • Enhanced Claude hook surface resolution so an explicitly provided --surface reliably overrides conflicting environment and TTY-derived candidates, with clearer and more deterministic fallback behavior.
  • Tests
    • Added regression coverage for agent-hook auto-resume working-directory normalization and session-start surface binding precedence (including leaked ambient values and invalid explicit --surface).