Skip to content

fix(#966): scope the conflict-abort restore to paths unchanged since the pre-quit snapshot - #968

Merged
fdaviddpt merged 2 commits into
mainfrom
fix/966
Oct 7, 2026
Merged

fdaviddpt merged 2 commits into
mainfrom
fix/966

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

_grc_report_conflict in hooks.d/after_save/60-git-reconcile.sh previously restored (via unconditional git checkout HEAD -- / git rm -f) every path in _GRC_TOUCHED_PATHS -- not scoped to $SLUG/. Only this slug's own RC_LOCK_DIR/CONSOLIDATION_LOCK_DIR locks were held, never another slug's. A different slug writing a tracked file the remote side also changed, in the TOCTOU window while git rebase --quit ran, was silently discarded, while the hook still logged "aborted, the tree is unchanged".

Adds _grc_path_hash (git hash-object, no -w, so it never writes to the object store; returns the sentinel ABSENT if the path doesn't exist, HASH-ERROR if the hash call itself fails). Right before calling git rebase --quit, every touched path's hash is snapshotted. After quit, only the subset of _GRC_TOUCHED_PATHS whose current hash still equals the pre-quit snapshot -- and where neither hash is the HASH-ERROR sentinel, so a could-not-hash reading never compares equal to itself and reads as "provably unchanged" -- feeds the restore/remove steps. A path whose content changed (or could not be verified) during that window is left exactly as it was.

git rebase --quit touches neither the working tree, the index, nor HEAD (git-rebase(1)) -- only the rebase-merge/rebase-apply state directory -- so a difference between the pre-quit and post-quit snapshot can only come from something else writing to that path during that specific window.

New test: tests/test_git_reconcile_903.py::TestScopedAbort::test_write_in_the_toctou_window_inside_the_restore_set_survives_the_abort, with fixture _diverged_with_in_set_foreign_write (the remote-side divergent commit also touches other-slug/now.md, landing INSIDE the restore set, unlike the sibling pre-existing TOCTOU test which uses a foreign path outside the restore set entirely).

RED (against the pre-fix hook, new test only): AssertionError: a write landing in the TOCTOU window on a path that IS inside the restore set was discarded by the unconditional checkout/rm over the whole set -- #966.

GREEN: python3 -m pytest tests/test_git_reconcile_903.py -q --no-cov -> 24 passed (also re-verified green after the follow-up commit).

Also green: bash -n hooks.d/after_save/60-git-reconcile.sh; python3 .oss/assemble_changelog.py --check (11 fragments, all parse).

Self-review (two spawns, Explore + oss:auditor, both confirmed clean via tree-snapshot): both independently found the same HASH-ERROR sentinel-collision class, fixed in the follow-up commit. Both also independently found that the fix only narrows the race to the rebase --quit call itself and does not close the wider window from this run's own rebase attempt starting through the conflict firing -- deliberately not moved earlier, since snapshotting before the rebase attempt would also capture this run's own legitimate pre-rebase content and make every touched path look "changed", defeating the restore entirely. This residual gap, plus a second narrower one (per-path hashes are all read before any path's own checkout/rm runs), is logged in trap.d/966.residual-race.md for /oss:curate rather than fixed here -- does not close this residual race, only the pre-quit-to-post-quit window named above. Also confirmed: the guarded git diff --diff-filter=A / checkout HEAD -- branches correctly skip when _grc_safe_paths is empty (an unguarded, pathspec-less git diff --diff-filter=A HEAD -- would diff the whole tree); the win32 pytestmark skip still applies; macOS/Linux CI legs run this file with no GNU/BSD divergence in the new bash constructs; no .gitattributes/core.autocrlf to introduce a byte-level hashing gap.

docs_targets (README.md): checked, no mention of "reconcile" or restore-scope behavior exists to update -- no-change-needed.

Closes #966.

[AI-generated]

fdaviddpt and others added 2 commits October 7, 2026 21:27
…the pre-quit snapshot

`_grc_report_conflict` in `hooks.d/after_save/60-git-reconcile.sh` (since
d431fb2, #952/#962) restores every path touched by this run's own commits
and the remote's commits since their common base, not scoped to `$SLUG/`
as #943 originally promised. `git checkout HEAD --` and `git rm -f` ran
over that whole set unconditionally, with only this slug's own
consolidation lock held -- not another slug's. A different slug writing a
tracked file the remote side also changed, in the TOCTOU window while
this slug's `git rebase --quit` runs, was silently discarded, while the
hook still logged "aborted, the tree is unchanged".

`git rebase --quit` touches neither the working tree, the index, nor
HEAD (git-rebase(1)) -- it only drops the rebase-merge/rebase-apply
state directory. So a content snapshot of every touched path taken
immediately before that call, compared against the same paths right
before the restore runs, can only ever differ because something else
wrote to one of them in between. The restore now only acts on the
subset whose content still matches that snapshot; a path that changed is
left exactly as the concurrent write put it, which the existing
`_grc_foreign_changes` porcelain scan already reports correctly once
that write is no longer clobbered.

New regression test
test_write_in_the_toctou_window_inside_the_restore_set_survives_the_abort
puts the foreign path INSIDE the restore set (the remote side's own
commit also touches it), unlike the existing TOCTOU test which only
covers a foreign path outside it -- confirmed red against current main,
green after the fix. The sibling tests in TestScopedAbort (positive
control: a normal scoped abort with no race) and the rest of
test_git_reconcile_903.py (24/24) still pass unchanged.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
… claim, log residual gap

Self-review (Explore + oss:auditor spawns) on the previous commit found:

- A could-not-hash sentinel ("HASH-ERROR") compared equal to itself was
  being treated as "content provably unchanged", so a path whose
  `git hash-object` failed identically before and after `rebase --quit`
  (e.g. a static permission/IO problem) would still be restored instead
  of being treated as unverifiable and left alone. Fixed: either side
  reading HASH-ERROR now excludes the path from the safe set.

- The code comment and changelog fragment overclaimed the guarantee --
  "can only ever differ because of something else writing ... in
  between" is only true for the window from immediately-before
  `rebase --quit` through the restore, not for the whole function or the
  rebase attempt that precedes it. Reworded both to say what is actually
  covered, and logged the un-closed, wider window (plus a second, much
  narrower gap between each path's own hash check and its own
  checkout/rm) to trap.d/966.residual-race.md for /oss:curate to weigh,
  rather than silently shipping a broader claim than the fix delivers.

Re-verified: bash -n clean, python3 .oss/assemble_changelog.py --check
passes (11 fragments, all parse), tests/test_git_reconcile_903.py 24/24
still green.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@fdaviddpt
fdaviddpt merged commit 1556c3b into main Oct 7, 2026
19 checks passed
@fdaviddpt
fdaviddpt deleted the fix/966 branch October 31, 2026 17:18
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: scoped conflict-abort restore in 60-git-reconcile.sh can destroy another slug's concurrent write (regression in #952/#962)

1 participant