Plan: a commit in another checkout of the session’s own repository (#679)
Closes: #679. Relates to: #462 (the refusal’s way out), #477 (helper worktrees, the precedent), #68 (why the review must not hash the wrong index), #236 (facts from the checkout’s root). Branch: feat/worktree-commits. Status: Done (2026-11-02) — U1-U3 on feat/worktree-commits
Erratum (2026-11-02): U1 keeps commitTargetsElsewhere refusing a sibling (naming its root with the #462 text), and U2 makes it the thin reading the plan describes in the same commit that makes the pause read the site, so no commit on the branch reviews the wrong index. CommitSite’s sibling also carries its checkout’s `root, for that refusal. elsewhereVerdict is replaced by an exported siteVerdict (its only callers were this file and test/workspace-failure.test.ts, whose cases keep their assertions through a local adapter).
Erratum (2026-11-02): applyCommitGuard keeps passing the session’s cwd, and guardGitCommit resolves the site itself with commitSite from it, reading the commit’s message file and branch at a sibling’s directory: handed the site’s cwd, its walk would apply the line’s cd`s a second time from there, and a relative one (`cd ../wt/sub) would land in wt/wt/sub. commitSite also skips the home and root reads for a line with no commit, since every git command reaches the pause and over SSH each read is a round trip.
Design
What the operator decided (2026-11-02)
Plan it now; the rule is symmetric. A commit is accepted in any checkout that shares the session repository’s git common dir: a linked worktree from a session in the primary, or the primary from a session in a worktree. What the guard reviews is that checkout’s index, branch and tree.
Why the refusal exists, and what changes
commitTargetsElsewhere (git-guard.ts) walks a line’s cd, pushd and git -C to the directory each commit runs in, resolves each to its checkout root, and refuses a commit whose root is not the session’s: the pause’s readers all take at = { ws, cwd: ctx.cwd }, the session’s cwd, so a commit elsewhere would have the wrong index hashed and read (#68), and the wrong HEAD judged for the main-branch rule. The refusal’s text says so and names the way out: a session there, or a fork (#462).
The change is not to teach each reader about a second directory. It is to resolve the commit’s site once, before anything reads, and when the site is a checkout of the same repository, make it at.cwd for the rest of the pause. Every reader then reads the site, as it would in a session started there. elsewhere keeps refusing a site in another repository and a site the walk cannot name.
Same repository: the git common dir
Two checkouts are one repository when their git directories' common dir is the same path. resolveGitDir follows a worktree’s .git file to its gitdir, and commonGitDir reads that gitdir’s commondir pointer (git-dir.ts); both read files on the workspace’s host, no subprocess, through the workspace as everything else does (#491). The session’s common dir is readRepoFacts(ws, ctx.cwd).commonDir. A site’s is the same two reads from its root. Equal under the workspace’s path rules (case-insensitive on win32, #538) means one repository. A submodule’s gitdir points under the parent’s .git/modules/, whose common dir is its own, so a submodule is still another repository, as now.
The site, resolved first
commitSite(ws, command, sessionCwd, sessionId) in git-guard.ts replaces commitTargetsElsewhere as the pause’s first question about a commit. It runs the same walk and returns one of:
-
{ kind: "here" }: no commit leaves the session’s checkout; -
{ kind: "sibling", cwd }: every commit on the line runs in one directory,cwd, inside a checkout whose common dir is the session repository’s; the site is that directory, not its root, since a relative-Fpath and a relative Review-Context path are relative to where the commit runs; -
{ kind: "elsewhere", reason }: a commit in another repository, a commit whose target the walk cannot name, or two commits on one line in different checkouts (reviewed as one diff they cannot be).
commitTargetsElsewhere stays as a thin reading of it for its existing callers and tests: `elsewhere’s reason, else undefined.
In pauseBashCommand (commit-pause.ts) the site is resolved before anything else reads the command, and the rest of the pause runs on one context whose cwd is the site: const here = { …ctx, cwd: site }, built once, and passed wherever ctx was passed. The pause’s readers take their directory from two places today, and both must be the site: at = { ws, cwd }, which commitShape, subjectRefusals, declaredContext and reviewKeyOf take; and ctx.cwd itself, which reviewStaged reads for readStagedForReview, createSnapshot and projectReviewRules, approvedPlanIn for splitApprovedPlan, and applyCommitGuard for the input it hands guardGitCommit. With at built from here and here passed to reviewCommit, approvedPlanIn, applyCommitGuard and proceedClean, no reader needs a change of its own, and none is left reading the session’s cwd by accident. ctx itself is still used for what is the session’s and not the checkout’s: ctx.ui, ctx.sessionManager, ctx.model, and the plan approval’s key (below).
The order matters for the message file: resolveNamedFiles places a relative -F path against cwd and admits it only under that directory or a scratch root (commit-message.ts), so cd ../wt && git commit -F .gadhs-commit-msg read against the session’s cwd would place the file in the wrong directory, or refuse it as outside the tree. Read against the site, it is the worktree’s own file, as it would be from a session there. subjectRefusals keeps its other refusals and takes the site’s elsewhere reason instead of calling the walk again.
What then reads the site through here, with no change of its own: findCommits and the message file; readRepoFacts for the branch rule (the worktree’s HEAD; main in a worktree is still refused, as #174 pinned); resolveReviewContext (declared paths relative to the site, read from the site’s index); stagedDiffKey and readStagedForReview (the site’s index and tree); createSnapshot; projectReviewRules (the site’s .pi rules, the same files on another branch). guardGitCommit is called by applyCommitGuard with here’s cwd, so its own `findCommits and readRepoFacts read the site too, and its commitTargetsElsewhere finds the site is here and not elsewhere. The Co-Authored-By splice is guardGitCommit’s own (`attributed), so it reads the site with it. U2 names each seam.
The approved plan
approvedPlanIn calls bridge.planApproval(ctx.cwd), keyed on the session’s cwd, because the approval is the session’s: the plan was approved in this conversation. That key stays the session’s cwd: approvedPlanIn takes the session’s ctx for the key and here for the split. The split itself (splitApprovedPlan, on here.cwd) reads the site’s staged files, so a plan committed from the worktree is matched by content hash as it is today. A commit of only the approved plan from a worktree skips its pause as it does from the primary.
The refusal that remains
A site in another repository gets the same text as today (#462, #519), with its last sentence changed from "One checkout per agent session." to "One repository per agent session." in both elsewhereReason and elsewhereOverSsh; the three tests that assert that sentence verbatim change with it (test/git-guard.test.ts around line 929, test/reflection-trigger.test.ts around line 361, test/workspace-failure.test.ts around line 237). The doc comment that said "Worktree commits stay refused - one checkout per agent session" goes. A site the walk cannot name gets ELSEWHERE_UNKNOWN. New: a line committing in two different checkouts is refused with its own sentence, since one review cannot read two indexes.
The pre-push hook
Unchanged. A push from the worktree runs that worktree’s hooks (core.hooksPath is a repository setting, shared), against that worktree’s tree, which is the commit’s. craig’s battery in the primary and a push from the worktree are two processes on two trees; that is the point of the worktree.
Risks
| Risk | Mode | Mitigation |
|---|---|---|
A reader still takes the session’s cwd after the site is known |
Fail-open: the primary’s index hashed and reviewed for a worktree commit (#68 again) |
One |
Two checkouts read as one repository when they are not |
Fail-open: another repository’s commit reviewed against the wrong facts |
Equality of common dirs resolved through |
A worktree on |
Fail-open on the branch rule |
The branch rule reads the site’s HEAD, so it is refused as a primary on main is (#174 already pins a worktree on main). |
Two commits on one line in two checkouts |
Fail-open: one reviewed, one not |
Refused by |
A path relative to the wrong directory |
Fail-closed for |
The site is the commit’s directory, not its root; tests for a relative |
Scope
Each unit one commit on feat/worktree-commits, pi-workflow’s suite green, the new tests mutation-checked. U3 closes #679.
U1: the site
git-guard.ts: commitSite and its result type; commitTargetsElsewhere as a reading of it; sameRepository(ws, a, b) on git-dir.ts (two roots' common dirs, compared under the workspace’s path rules); the two-checkouts refusal; the last sentence of elsewhereReason and elsewhereOverSsh, and the three verbatim assertions. guardGitCommit is unchanged in U1: it reads whatever cwd it is given, and U2 gives it the site.
Tests, test/git-guard.test.ts beside the #462 cases, each in a real temporary repository with git worktree add: unit — a session in the primary, cd <wt> && git commit is a sibling at <wt>; git -C <wt> commit too; the mirror, a session in the worktree committing in the primary; a subdirectory of the worktree is the site, not its root. Regression — a clone of the same repository is elsewhere; the worktrees-named clone of #174 is elsewhere; a submodule is elsewhere; cd "$X" && git commit is unknown; two commits in two checkouts are refused with the new reason. Unit — sameRepository on a primary and its worktree, on two clones, on a path with no repository.
U2: the pause reads the site
commit-pause.ts: pauseBashCommand resolves the site first, builds here = { …ctx, cwd: site } and at from it, and passes here to reviewCommit (so reviewStaged’s three reads), `applyCommitGuard, proceedClean and approvedPlanIn’s split, with the plan approval still keyed on `ctx.cwd; subjectRefusals takes the resolved site. Tests, test/worktree-session.test.ts, which already drives pi’s real session with the whole extension loaded against a temporary repository and a worktree (#477), gain a describe for a session in the primary committing in the worktree: integration — a worktree commit’s -F .gadhs-commit-msg under the worktree is read and reviewed (fails with the site resolved after commitShape); the review’s diff is the worktree’s staged change, not the primary’s (the primary has a different file staged); a worktree on main is refused; a Review-Context path relative to the worktree resolves; the approved plan committed from the worktree skips its pause as from the primary; a commit in a clone is refused with the #462 text.
U3: docs and release note
docs/modules/ROOT/pages/modes.adoc: the "How the guards read a command line" paragraph on relocations (around line 1577, "A session started in a worktree commits there normally") says the rule is one repository, that a commit in another checkout of it is judged there, and what still refuses; the helper-worktree section’s opening sentence (around line 3199, "one checkout per agent session") is reworded to match. CHANGELOG.adoc Unreleased, Changed. Closes #679.