Fix the 4 unresolved CodeRabbit threads on PR #910 (merged) — follow-up PR | SageOx
Fix the 4 unresolved CodeRabbit threads on PR #910 (merged) — follow-up PR
Context
PR #910 (fix(ledger): fail closed on invalid automatic commits) is already merged into main (eb1cd97d). Four CodeRabbit threads remain unresolved. Three are real defects in the shipped code; one is a test-rigor nit. All land in a new PR off main from this worktree (ryan/munich-v5).
git commit -- <pathspec> commits worktree bytes, not the index blobs ValidateStagedLedgerCommit inspected. An unlocked writer (raw git, an editor, the recorder) can swap meta.json after validation → unvalidated bytes published.
Real. Same class in daemon.go (murmur), sync.go (retract), ledger/github_push.go. .claude/rules/cache-only-design.md already names this trap.
2
internal/daemon/daemon.go:2121
Murmur commit stages/validates/commits all of data/murmurs/ — sweeps unrelated staged edits in that dir.
TestRetractOrphanedDrafts_RevalidatesHeartbeatAfterLockWait can pass via the ErrLockTimeout ("ledger busy") path without ever entering the locked closure.
Real but theoretical (2-min lock timeout vs ms test). Add a test hook + assertion, matching the existing gcAsyncTestHook pattern in sync.go.
4
internal/gitutil/conflict.go:115
git show :./path returns a symlink's target text as blob bytes → a symlink whose target is {"title":"Ready"} passes JSON validation and sessions/<n>/meta.json is published as a symlink. Gitlinks likewise.
Real. Validator must inspect index mode, not just content.
Threads 1 and 4 have the same root cause: validation and commit look at different objects. The fix is one engine that validates an immutable tree and commits that tree — which cmd/ox/ledger_commit_snapshot.go already does for the CLI (whole-index only). We generalize it into internal/gitutil, add path-scoping, and make every automatic Ledger writer use it.
Lives in a new file internal/gitutil/ledger_commit.go (conflict.go keeps the validators).
Loading diagram...
Mermaid diagram
No worktree read after git add. The temp index is built from the real index entries (git ls-files --stage -z -- <pathspecs>) layered over read-tree <parent>; deletions come from git diff-index --cached --name-only --diff-filter=D -z <parent> -- <pathspecs> emitted as mode-0 lines. All fed to git update-index -z --index-info with GIT_INDEX_FILE pointing at a temp file (os.MkdirTemp, removed on return). Unborn branch: read-tree --empty, no deletions.
No pathspecs = whole index (today's CLI behavior). That lets cmd/ox/commitLedgerSnapshot become a one-line wrapper.
CAS update-ref <branch> <commit> <parent> — a concurrent raw-git commit makes us fail and retry next cycle instead of silently interleaving. Unborn branch: no old-value (mirrors existing cmd/ox code).
Sacred mass-delete guard (internal/sacred, leaf package — no import cycle) moves in with the engine. For path-scoped daemon commits it is inert (finalize never deletes; retract deletes one meta.json), so no behavior change there.
cleanGitOutput grows a stdin/env-capable variant (runPlumbing(ctx, repo, stdin []byte, extraEnv []string, args...)); cleanGitOutput becomes a thin call to it.
Validator changes (internal/gitutil/conflict.go)
ValidateStagedLedgerCommit(ctx, repo, pathspecs...) keeps its signature/contract but is re-implemented as write-tree on the real index → validateLedgerTree(parent, tree, pathspecs...). One tree-based validator serves both the reconcile callers (no pathspec) and the engine. The git show :./path reader, splitNUL, and the unborn ls-files branch go away.
New ValidateLedgerEntryMode(path, mode string) error: for any path under sessions/ (not only meta.json — the whole session tree is written exclusively by ox writers as regular files, and a symlinked raw.jsonl breaks LFS pointer detection the same way), reject 120000 (symlink) and 160000 (gitlink); accept 100644/100755. Scoped to sessions/ deliberately: ValidateStagedLedgerCommit also vets reconcile's squash of hand-made unpushed history, and refusing a human's symlink elsewhere in the ledger would wedge push repair.
ValidateLedgerBlob stays content-only (still used by cmd/ox until the wrapper lands; after that only internally).
internal/lfs/reconcile.go (×3, no pathspec, bare git commit)
unchanged
unchanged — a bare git commit reads the index, not the worktree; it gets the mode check for free via ValidateStagedLedgerCommit.
Not in this PR (file a bead): cmd/ox/session_draft.gocommitDraftLocally / commitDraftRemoval have the same git commit -- <pathspec> footgun, mitigated today by assertDraftStillStaged. Migrating them to the engine lets that check be deleted; separate PR to keep this one reviewable.
Docs
.claude/rules/cache-only-design.md — the "git commit -- <pathspec> commits the working tree" trap paragraph: point at gitutil.CommitLedgerSnapshot as the required path.
CLAUDE.md Canonical Functions table: add gitutil.CommitLedgerSnapshot(ctx, repo, msg, pathspecs...) — "Use instead of git commit -- <pathspec> / git commit -m in any automatic Ledger writer".
stage valid sessions/x/meta.json; overwrite the worktree file with markers after staging; commit with pathspec → git show HEAD:sessions/x/meta.json == staged bytes; worktree still has markers (untouched)
replacing the engine's commit with git commit -- pathspec → HEAD carries markers
commit a regular sessions/x/meta.json; replace it with a symlink whose target string is {"title":"Ready"} (i.e. target text is valid JSON); git add; validate → error contains symbolic link. Skip if os.Symlink unsupported.
handler.afterStageTestHook overwrites meta.json in the worktree with markers between git add and commit; pushed==true (skipLFS, no remote → follow existing _RefusesConflictMarkers fixture pattern; assert HEAD blob, not push) → HEAD:sessions/<n>/meta.json is the clean staged JSON
pre-stage an unrelated data/murmurs/other.json (git add, not committed); writeAndCommitMurmur → git show --name-only HEAD lists only payload.RelPath; other.json still staged
set s.retractLockedTestHook = func(){ close(entered) } before starting the reaper; after <-retractDone, entered must be closed (select … default: t.Fatal("reaper never entered the locked closure")). Existing assertions stay.
make the reaper skip the closure (e.g. return early on IsRepoLockBusy) → hook never fires
cmd/ox/ledger_commit_snapshot_test.go / _sacred_test.go: the commitLedgerSnapshot(...)-driven tests stay as-is (wrapper); newLedgerTestRepo stays. Only the two plumbing-level tests move to gitutil.
Red-first for every new test per the tables above (neuter → observe fail text → restore → pass). Record both outputs for the PR body.
go test -race ./internal/gitutil/ ./internal/ledger/ ./internal/lfs/ ./internal/daemon/... ./cmd/ox/ -run 'CommitLedgerSnapshot|ValidateStagedLedgerCommit|ValidateLedgerEntry|GitCommitAndPush|WriteAndCommitMurmur|RetractOrphanedDrafts|CommitAndPushGitHubData|Reconcile'
make test (full fast tier) — must be green, including the existing sparse-checkout ledger fixtures in internal/ledger/*_sparse_test.go (temp-index path must work under cone-mode sparse-checkout; if index.sparse collapses directories, ls-files --stage expands them — verify with a sparse fixture, add one to ledger_commit_test.go if none exercises the engine).
make ready → READY: before push.
Open PR (ready, not draft) per repo rules; body leads with value (no automatic Ledger commit can publish bytes validation didn't see; symlink/gitlink metadata refused; murmur commits no longer sweep neighbors) + Mermaid above + red/green test evidence.
On PR #910: reply Fixed in #<new>. on each of the 4 threads and resolve via GraphQL resolveReviewThread.
Re-trigger Greptile on final state: gh pr comment <new> --body "@greptileai".
Process
Beads: bd create one task for this PR (claim in_progress); one follow-up task for migrating cmd/ox/session_draft.go to the engine and deleting assertDraftStillStaged.
Repo rule: confirm with Ryan before git commit / git push.
Implementation fan-out (exclusive file ownership): (a) go-expert → gitutil engine + validator + gitutil tests; then, once the gitutil API compiles, in parallel (b) daemon callers + tests (session_finalize.go, daemon.go, sync.go + their tests), (c) ledger/github_push.go + cmd/ox wrapper + moved tests + docs. I review every diff and run the combined gate.