fix(mcp): fold parent snapshot id into forget/compile cursor hashes - #246
Merged
Conversation
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
radimsem
added a commit
that referenced
this pull request
Jun 6, 2026
Closes #247. ## Problem The background rescan purge path (`pkg/mcp/rescan/rescan.go` `reconcileDeleted`) emitted its deleted-file snapshot with a **content-only** `diff.CursorHashFlat(synthetic)`. `snapshots.cursor_hash` is `CHAR(16) NOT NULL UNIQUE`, so a sequence that purges the *same* file set twice with an identical recreate in between (delete → purge → recreate+compile → delete same files → purge) reproduces the earlier purge hash and collides. The failure is swallowed (`r.logger.Error` + `return false`), so the purge snapshot is silently dropped rather than surfaced. This is the last `emitter.Emit*` call site that was still on a parent-unaware digest — follow-up to #238 (write/summarize) and #244/#246 (compile/forget). ## Fix Route the purge through parent-folded hashing, the model #238 adopted: - Read `prevHeadID` via `GetHeadSnapshotID` — already inside the `OpMu` critical section, so the head-id read and the `emitter.Emit` (which advances HEAD) stay atomic. - Swap `diff.CursorHashFlat(synthetic)` → `diff.CursorHashForChange(prevHeadID, deltas)`. - Drop the now-unused `synthetic []*parser.ContextNode` scaffold (it existed only to feed `CursorHashFlat`) and the orphaned `parser` import. ## Tests - `pkg/mcp/rescan`: `TestRescanLoop_RepeatedDeleteOfSameFileSet` — delete the same file set twice with an identical recreate in between; asserts two distinct purge snapshots. Confirmed to fail pre-fix (`purge snapshots = 1, want 2` — second purge swallowed by the `UNIQUE` collision) and pass after. - Full suite green: build, `go vet`, **1186 tests**, gofmt, golangci-lint (0 issues). ## Audit (closes the #247 follow-up note) All five `emitter.Emit*` snapshot sites now fold the parent id: `compiler`→`CursorHashForCompile`, `rollback`→`CursorHashForRollback`, write/summarize+`forget`+`rescan`→`CursorHashForChange`. `CursorHashFlat`/`CursorHash` remain only for non-snapshot, diff-internal uses. No further emit path on a bare content/delta hash. ## Reviewers go-style and Codex reviews both clean. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #244.
Problem
snapshots.cursor_hashisCHAR(16) NOT NULL UNIQUE. #238 fixedMemoryWrite/MemorySummarizeby folding the monotonic parent snapshot id into the cursor hash. Two snapshot-emitting paths were left on content/delta-only digests that do not fold the parent id, so the same collision class still applied:MemoryCompileuseddiff.CursorHashFlat(flat)(pure content digest) — an edit→compile→revert→compile that lands the post-state on a prior content state reproduces the earlier hash.MemoryForgetuseddiff.CursorHashForDeltas(deltas)(op + id + old/new hashes, no parent id) — a re-add-then-forget reproducing an identical delta set collides.Either case fails with the opaque
UNIQUE constraint failed: snapshots.cursor_hashand a fullTxrollback.Fix
Route both through parent-folded hashing, the model #238 adopted:
MemoryForgetnow readsGetHeadSnapshotID(underOpMu) and usesdiff.CursorHashForChange(prevHeadID, deltas).MemoryCompilenow readsGetHeadSnapshotIDTxinside its existingStore.Tx, beforeEmitTx(which advances HEAD), and uses the newdiff.CursorHashForCompile(prevHeadID, flat). Still oneEmitTxin oneTx— §7 one-snapshot-per-call preserved.writeID/writeFlatPairs/writeDeltaPairs/sumHexhelpers;CursorHashForChange/CursorHashForRollback/CursorHashFlatare byte-identical to before. Removed the now-unusedCursorHashForDeltas.Tests
pkg/diff:TestCursorHashForCompile_DistinctAcrossRepeatState,_OrderIndependentpkg/mcp/tools:TestHandleForget_ReAddThenForget(write+forget ×2 → 4 distinct snapshots)pkg/compiler:TestCompile_RevertToPriorStateSucceeds(A→B→A → 3 distinct snapshots)Each regression test was confirmed to fail with
UNIQUE constraint failed: snapshots.cursor_hashagainst the pre-fix hashing and pass after. Full suite green: build,go vet, 1185 tests, golangci-lint,make check-skills.Reviewers
go-style, mcp-surface, generic-correctness, and Codex reviews all clean.
Note
Docs (
docs/versioning.md,skills/remind/references/snapshots-diffs.md) already describecursor_hashas an opaque, unique-per-snapshot fingerprint (corrected in #238), so no doc change is needed.A follow-up:
pkg/mcp/rescan/rescan.gopurge emit is still on content-onlyCursorHashFlat— same latent class, outside this issue's scope; will file separately.🤖 Generated with Claude Code