Skip to content

fix(#906): checkout release must prove holder identity - #907

Merged
admarble merged 7 commits into
mainfrom
feature/906-fix-locks-checkout-release-ignores-holder-iden
Aug 7, 2026
Merged

admarble merged 7 commits into
mainfrom
feature/906-fix-locks-checkout-release-ignores-holder-iden

Conversation

@admarble

@admarble admarble commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

CheckoutLock.release() skipped the holder-identity check entirely whenever the lock carried skipPidCheck — which every /fullsolve lock does. Acquire and release were asymmetric in exactly the two-session scenario #901 exists for: a second session's acquire was correctly refused while the holder was fresh, but its release succeeded and removed the holder's lock. Since every halt branch in the skill runs the release contract, the losing session finishing first was the common path — and the next session was then waved straight through the pre-tool.sh guard while the first was still mid-run.

Implements Design v2, plus the gaps found while validating it. Ownership is now one ordered predicate, isCheckoutOwner, shared by acquire and release so the two cannot disagree about who the holder is:

  1. cross-host → refuse (first, unconditional)
  2. both sessionIds present → equality decides, no fall-through
  3. pid + hostname match → release (live-process self-release)
  4. skipPidCheck lock and caller issue === holder issue → release
  5. otherwise refuse

Rule 4 restores for a constant-filename lock the property LockManager.releaseExternal gets free from its issue-keyed filename: naming the issue is the proof of ownership. It is a courtesy check against the accident — a blocked session carries a different issue by construction — not a security boundary; clear --force already exists.

Everything else this closes

The guard now recognizes the holder across the shell boundary. SEQUANT_ISSUE was supposed to identify the holder to pre-tool.sh and never could: PreToolUse runs outside and before the command's shell, so nothing a skill bash block exports reaches it — not even an export prepended to the same block as the guarded command — and the one path that does export it (sequant run) also sets SEQUANT_ORCHESTRATOR, where the guard stands down. The holder was routinely refused in its own tree, with a message naming itself as the blocker. Observed live while building this PR. The hook now records the session→issue binding when it observes locks checkout acquire --issue=<N>, keyed on session_id — the one identity that survives the shell boundary killing the acquiring PID — and consults it as the last rule. Clearing happens only on a release naming the session's own issue, so a refused release cannot strip the real holder's identity.

Path-restore is exempt whether or not the path is quoted. git checkout -- "src/foo.ts" was refused while the unquoted form passed. My first diagnosis of this was wrong and is corrected here: it is not about shell variables — unquoted $F always worked. emit_segments drops double-quoted regions, leaving the segment as git checkout -- with no trailing space against an exemption requiring -- . Now --( |$). --detach still doesn't match; quoted branch names are still blocked.

A corrupt checkout.lock was unclearable by any command. Reachable — the file is created and written in two steps and a group-SIGKILL lands between them (#856). clear read it, saw nothing, returned no-lock without unlinking (--force only ever reached the staleness check, never the read), while the hook blocked on it forever.

All three refusal messages recommended a bare clear, which refuses a fresh holder — and a leaked skill lock reads fresh for its full 6h TTL.

The tautology detector reported whole suites of real tests as tautological. Three blind spots, each measured on main rather than assumed:

File before after
checkout-lock.integration.test.ts 17/19 (89%) 0/29
checkout-lock.test.ts 7/20 (35%) 0/31
this PR's diff 31/54 (57.4%) 0/60

It extracted only arrow-const helpers (so function declarations made every caller read as import-less); seeded handles only on helpers that spawn build output (so makeLock() returning new CheckoutLock(...) didn't count); and treated dist/ as the only production code, excluding the hook scripts these very tests drive. Guard rails are explicit and tested: a helper reaching no production code, and one that only spawns git, are both still flagged.

/fullsolve's Phase 0.3 is corrected alongside: --issue at every release site, the duplicated per-issue acquire block deleted (a literal second acquire halts the session against its own lock — LockManager has no reentrancy check), Phase 0 declared unconditional on resumed runs, the Phase 5.0 branch gate now releasing before it exits, and the release contract no longer naming two branches that actually continue to Phase 5.

locks checkout ships in no released version, so failing closed costs no compatibility.

Test plan

  • npm test — 240/240 files, 5140 passed, 1 expected-fail, 2 skipped
  • npm run build, npm run lint (0 warnings), lint:skill-sync (44 synced, 0 diverged)
  • Motivating example replayed verbatim as an integration fixture: A holds feat(#22): Skill output reliability - validation layers and structured logging #23 → B refused → hook blocks B → B's release refuses → A's lock survives → hook still blocks B
  • Holder identity driven against the real hook: blocked before its acquire is observed → allowed after → a different session still blocked → a session that acquired a different issue still blocked → holder status dropped on its own release → kept through a refused release
  • Executed against the built CLI, not only unit tests:
A: acquire --issue=23 --skip-pid-check  -> ✓ exit 0
B: release --issue=77                   -> refused, exit 1, names #23 + clear --force
   release (bare)                        -> refused, exit 1, suggests --issue=23
A: release --issue=23                   -> ✓ Released, exit 0
   release --issue=23 (nothing held)    -> "No releasable checkout lock", exit 0
   release --issue=abc                  -> Invalid issue number, exit 2
corrupt (zero-byte) lock + clear        -> ✓ Cleared without --force; acquire works again

Mutation verification (per CLAUDE.md)

Eleven mutations, tree restored clean after each. The gate test was rewritten because its predecessor's count-pairing (8 >= 7) stayed green when a release site was deleted.

Mutation Result
Restore the skipPidCheck release bypass 4 fail, incl. the interleaving fixture
Strip --issue from one release site 2 fail, only for the mutated mirror
Delete a whole release line (the old test passed this) 1 fail
Remove the Phase 5.0 releases 2 fail
Re-introduce the duplicated acquire block 1 fail
Bare sequant … without npx 2 fail
Remove the hook's session→issue binding 3 fail
Revert the path-restore regex to -- 3 fail (quoted forms only — pinning the real cause)
Drop function-declaration helper extraction 5 fail
Drop the imported-production-function seed 4 fail
Drop project-script recognition 1 fail

Two process notes I'd rather state than bury

A git checkout -- during mutation testing wiped uncommitted work. Reverting a mutation reverted my not-yet-committed detector fixes with it. Caught only because the next "baseline" run failed 5 tests instead of passing. Reapplied, committed, then re-mutated — and the isolated re-run immediately showed the earlier batch had been measuring a corrupted baseline.

One fix looked right for the wrong reason. My first function-declaration pattern reported a clean 0/60, but was matching greedily past the helper body and anchoring on the next declaration's brace — attributing unrelated code as the helper body. A synthetic object-return-type test caught what the real file had masked. Replaced with a deterministic body-brace walk; that test now pins it.

One deliberate divergence from Design v2

v2 asked to add the issue rule to acquire's reentrancy check. I replaced isSameHolder with the single shared predicate instead, so symmetry is structural rather than maintained by two edits kept in step. Consequence worth a look: the sessionId branch now sits behind the hostname check, so a cross-host caller with a matching session id is no longer reentrant. One existing assertion changed accordingly and is now stated explicitly as rule 1.

Adjacent defects — filed, not fixed here

Design v2 scoped these out; verified against the code and filed with fix sketches and test plans: #908 (stale-lock steal is unlink-then-create, so two stealers can both win), #909 (locks dir is cwd-relative while the hook reads the git toplevel — acquiring from a subdirectory silently protects nothing), #910 (new-feature.sh bypasses the guard — the script the block message recommends), #911 (/merger and /release mutate the main checkout without participating).

Fixes #906

🤖 Generated with Claude Code

`CheckoutLock.release()` skipped the holder-identity check entirely when the
lock carried `skipPidCheck` — which every /fullsolve lock does. Acquire and
release were asymmetric in exactly the scenario #901 exists for: a second
session's acquire was correctly refused while the holder was fresh, but its
release succeeded and removed the holder's lock. Every halt branch in the
skill runs the release contract, so the losing session finishing first was
the common path.

Ownership is now one ordered predicate, `isCheckoutOwner`, shared by acquire
and release so they cannot disagree about who the holder is:

  1. cross-host -> refuse (first, unconditional)
  2. both sessionIds present -> equality decides, no fall-through
  3. pid+hostname match -> release (live-process self-release)
  4. skipPidCheck lock && caller issue === holder issue -> release
  5. otherwise refuse

Rule 4 restores for a constant-filename lock the property `releaseExternal`
gets for free from its issue-keyed *filename*: naming the issue is the proof.
It is a courtesy check against the accident (a blocked session carries a
different issue), not a security boundary — `clear --force` exists.

Also closed here:
- `clear()` now unlinks an unparseable checkout.lock. Reachable via the
  create-then-write window (#856 group-SIGKILL); it was previously
  unclearable by any command while the hook blocked on it forever.
- All three refusal messages recommend `clear --force`; plain `clear`
  refuses a fresh holder, and a leaked skill lock reads fresh for 6h.
- CLI: `locks checkout release --issue`, and refusal is distinguishable
  from a no-op (exit 1, names the holder's issue and the recovery command).

/fullsolve SKILL.md (all three mirrors): --issue on every release site;
duplicated per-issue acquire block deleted; Phase 0 declared unconditional
on resume; Phase 5.0 branch gate releases before exiting; release contract
no longer names two branches that continue to Phase 5; SEQUANT_ISSUE
exported so the hook stops blocking the holder with its own lock.

Three tests asserted the bug as the contract and are corrected. The
fullsolve gate test is rewritten per-site: its count-based pairing
(`8 >= 7`) already tolerated deleting a release site.

Fixes #906
…sees it

`test-tautology-detector` resolves helper indirection only through
block-bodied arrow consts, so a `function` declaration made every test
calling it read as import-less. These tests spawn the real dist CLI.

Pre-existing blind spot, measured: on origin/main this file already reads
17/19 (89%) tautological; the 17 that remain are #901's `function runHook`
tests, untouched here.
Two QA findings on my own diff.

1. The AC-12 guidance was wrong. The PreToolUse hook runs outside and
   before the command's shell, so it reads Claude Code's environment —
   not anything a skill bash block exports. Verified live: this very run
   was HOOK_BLOCKED by its own #906 lock, and re-running with
   `export SEQUANT_ISSUE=906` prepended to the same block changed
   nothing. The previous text told readers to set it "in that same
   block", which does not work either. Replaced with what does: the
   `git -C "$SEQUANT_WORKTREE"` form, and the literal-path caveat on
   path-restore (the exemption does not resolve `$VARS`, so
   `git checkout -- "$F"` is refused where a literal path is allowed).

   AC-12's stated consequence is not achievable by this mechanism at
   all — it needs holder identity surviving a shell boundary, which
   Design v2 itself rules out of scope. Flagged, not papered over.

2. The gate's whole-file negative anchored on `npx `, so a site written
   as bare `sequant locks checkout release` would have passed. Now
   scans bash fences instead of anchoring on the runner — which also
   keeps the §5.5 prose that merely names the command from matching.
   Mutation-verified: the bare-`sequant` form now fails the gate.
…d paths

Closes the two gaps QA left open.

1. AC-12 for real. `SEQUANT_ISSUE` could never identify the holder
   interactively: PreToolUse runs outside and before the command's shell,
   so nothing a skill bash block exports reaches it, and the one path that
   does export it (`sequant run`) also sets SEQUANT_ORCHESTRATOR, where
   the guard stands down. The fallback was unreachable in every real flow
   and the holder was routinely blocked by its own lock.

   The hook does see both the acquire and every later command of the same
   session, and session_id survives the shell boundary that kills the
   acquiring PID. It now records the session->issue binding when it
   observes `locks checkout acquire --issue=N`, and consults it as the
   last rule in the holder check. Clearing happens only on a release that
   names the session's OWN issue -- a refused release must not strip the
   real holder's identity.

2. Path-restore exemption. `git checkout -- "src/foo.ts"` was blocked
   while the unquoted form was allowed. My earlier claim that this was
   about shell variables was WRONG, and the correction matters: an
   unquoted `$F` always worked. The cause is quoting -- `emit_segments`
   drops double-quoted regions, leaving the segment as `git checkout --`
   with no trailing space, and the exemption required `-- `. Now
   `--( |$)`. `--detach` still does not match, and quoted branch names
   (`git checkout "main"`) are still blocked.

SKILL.md prose replaced: it described the pre-fix world and told readers
to work around a gap that no longer exists.

12 new integration tests against the real hook. The identity tests write
a lock with NO sessionId -- the shape production actually writes, since
acquire never passes --session-id -- because the default fixture stamps
one and would let the sessionId rule decide every case, passing them
vacuously.
The gate flagged 31/54 (57.4%) of this PR's test blocks. Measured on
origin/main, checkout-lock.integration.test.ts already read 17/19 (89%)
and checkout-lock.test.ts 7/20 -- every one a false positive. Both files
drive real production code. The earlier commit dodged this by reshaping
one helper into an arrow const, which left the gate at exactly 50.0%,
one test away from re-tripping.

Three blind spots, each with a measured effect:

1. Only arrow-const helpers were extracted, so a `function` declaration
   made every test calling it read as import-less. Added, but NOT by
   widening the regex: a greedy return-type subpattern ran past the body
   and anchored on the next declaration's brace, silently attributing
   somebody else's code as the helper body -- it made the numbers look
   right for the wrong reason. Now the regex stops at the closing paren
   and a deterministic walk picks the body brace, stepping over an object
   return-type annotation. The object-return-type test pins exactly this.

2. Handles were seeded only on helpers that spawn the build output, so a
   factory like `makeLock()` returning `new CheckoutLock(...)` did not
   count. Now an imported production function seeds a handle too.

3. "Production" meant `dist/` only. The checkout-lock suite spawns the
   real .claude/hooks/pre-tool.sh -- the enforcement half of the feature
   under test. Project scripts (hooks/*.sh, scripts/**) now count;
   spawning a system binary like `git` still does not.

Diff now reads 0/60. All 60 pre-existing detector tests unchanged and
passing; 7 added, including two negatives proving the widening did not
switch the detector off (a helper that reaches nothing, and a helper that
only spawns `git`, are both still flagged).
Mutation testing found the pattern unexercised: the runHook case reaches
production through the collected HOOK variable, so removing
PROJECT_SCRIPT_PATTERN failed nothing. The inline-literal form
(spawnSync('npx', ['tsx', 'scripts/migrate.ts'])) is the shape it
actually guards. M11 now fails exactly this test.
…T_ISSUE claim

The original entry credited the SEQUANT_ISSUE export with unblocking the
holder. It never did -- corrected, and the three new entries describe what
actually closes it.
@admarble
admarble merged commit 5a68930 into main Aug 7, 2026
8 checks passed
@admarble
admarble deleted the feature/906-fix-locks-checkout-release-ignores-holder-iden branch August 7, 2026 13:03
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.

fix(locks): checkout release ignores holder identity on skip-pid-check locks — a blocked session's release contract unlocks the tree mid-run

1 participant