Skip to content

Support sandbox session lookup after commits on detached HEAD#414

Merged
robinaugh merged 3 commits intomainfrom
jason/detached-head-ancestry-lookup
Mar 11, 2026
Merged

Support sandbox session lookup after commits on detached HEAD#414
robinaugh merged 3 commits intomainfrom
jason/detached-head-ancestry-lookup

Conversation

@robinaugh
Copy link
Contributor

@robinaugh robinaugh commented Mar 11, 2026

Summary

  • When tools like Codex work in detached HEAD worktrees and make commits, the HEAD SHA advances but the sandbox is still running. The session key includes detached@<short-sha>, so the CLI could no longer find the running sandbox.
  • Adds IsAncestor to the git client, wrapping git merge-base --is-ancestor.
  • Adds GetSessionByAncestry and GetSessionsForCwdBranchByAncestry to SandboxStorage, which fall back to ancestry-based lookup when an exact key match fails on a detached HEAD. Matching sessions are re-keyed to the current SHA to avoid repeated misses.
  • Wires up the ancestry fallback at all relevant call sites: CheckExistingSandbox, ExecSandbox (specific-config, no-config, and remote recovery paths), StopSandbox, and ResetSandbox.
  • Does not regress PR Support detached HEAD for sandbox operations #399's fix for session key collisions between unrelated detached commits — only ancestor SHAs match.

Test plan

  • Unit tests for IsAncestor (ancestor, descendant, unrelated, equal, non-repo)
  • Unit tests for GetSessionByAncestry (match + re-key, no match, wrong cwd/config, named branch, bare detached)
  • Unit tests for GetSessionsForCwdBranchByAncestry (multi-config match, non-ancestor filtered, empty)
  • Full ./internal/cli/ and ./internal/git/ test suites pass
  • Lint passes
  • Manual: create a detached HEAD worktree, start sandbox, make commits, verify rwx sandbox exec and rwx sandbox stop still find the sandbox

Adds git merge-base --is-ancestor support, which will be used
for ancestry-based sandbox session lookup on detached HEADs.
When a detached HEAD advances (e.g. Codex making commits in a worktree),
the session key no longer matches. GetSessionByAncestry and
GetSessionsForCwdBranchByAncestry fall back to git ancestor checks,
and re-key matching sessions to the current SHA to avoid repeated misses.
When on a detached HEAD and an exact session lookup misses, fall back
to ancestry-based matching. This handles the case where tools like
Codex commit on a detached HEAD, advancing the SHA while the sandbox
is still running. Applied to CheckExistingSandbox, ExecSandbox (both
specific-config and no-config paths, plus remote recovery), StopSandbox,
and ResetSandbox.
@robinaugh robinaugh self-assigned this Mar 11, 2026
@robinaugh robinaugh marked this pull request as ready for review March 11, 2026 15:53
@robinaugh robinaugh merged commit 48f4857 into main Mar 11, 2026
1 check passed
@robinaugh robinaugh deleted the jason/detached-head-ancestry-lookup branch March 11, 2026 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants