fix(integrity): safe hydration --repair with snapshot + drift detection + re-emit (#602)#607
Merged
Merged
Conversation
…tion + re-emit (#602) `crosslink integrity hydration --repair` used to call `clear_shared_data()` followed by `hydrate_to_sqlite()`. Any SQLite row not represented in JSON was silently destroyed in the clear step, and the drift-detection logic above the repair only compared issue/milestone *counts*, so content-level divergence (e.g. a `dependencies (a, b)` row in SQLite, not in either issue's JSON `blockers`) never registered as drift at all. This commit implements all four mitigations from the issue, in priority order: (1) **Always snapshot before destroying state.** Every `--repair` run writes `.crosslink/integrity/hydration-backup-<utc-ts>.sqlite` via SQLite's `VACUUM INTO`. The snapshot is self-contained (works in WAL mode), reversible (drop it in place), and unconditional (runs even when re-emit succeeds, as the audit trail of what state the repair was applied against). (2) **Detect drift by content, not by count.** New module `commands::integrity_drift` hydrates JSON into a temp SQLite file, `ATTACH`-es it to the main connection, and SQL-diffs every shared table (issues, labels, dependencies, relations, milestone_issues, comments, time_entries). Returns a categorized `HydrationDriftReport`. (3) **Re-emit SQLite-only state back to JSON when possible.** `integrity_drift::re_emit` walks the drift report and calls `SharedWriter::{add_label, add_blocker, add_relation, set_milestone_on_issues}` for each row, writing it back through the git event log so both sides converge to the union. Comments and time entries cannot be re-emitted (no writer API for time entries; re-emitting comments would lose original UUIDs) — the snapshot is their recovery path. (4) **`--accept-data-loss` flag gates destructive repair.** When drift contains rows that re-emit cannot represent, or when no `SharedWriter` is available, `--repair` refuses without the new flag and points the user at the snapshot path. With the flag, proceeds destructively. Snapshot is always written either way. New files: - `crosslink/src/db/snapshot.rs` — `snapshot_to_integrity_dir` - `crosslink/src/commands/integrity_drift.rs` — detect + re_emit Tests: 2875 pass (+9). Three snapshot tests, five drift-detection tests (including the issue's reproducer scenario), one end-to-end re-emit integration test using a real `SharedWriter` + hub cache. Clippy clean -D warnings. `tempfile` moved from `[dev-dependencies]` to `[dependencies]` because the drift detector uses a temp SQLite file at runtime. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Apply rustfmt to integrity_drift.rs and snapshot.rs — minor whitespace tweaks only (one closure body wrap, one with_context collapse). No behavior change. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
`cargo clippy -- -D warnings -W clippy::unwrap_used -W clippy::expect_used` (the CI bar) now clean. Fixes: - 26 × clippy::doc_markdown — backtick `SQLite`, `JSON`, `JSON`-derived, `JSON`-known, and field-name references in doc comments on the new integrity_drift module, snapshot module, and the new --accept-data-loss flag's clap doc. - 3 × clippy::missing_const_for_fn — mark `HydrationDriftReport::is_empty`, `has_unrecoverable_loss`, `is_fully_re_emittable`, and `ReEmitStats::total` as `const fn`. They take only `&self` and read pod fields. - 1 × clippy::map_unwrap_or — replace `.map(...).unwrap_or_else(...)` on the snapshot_rel path strip with `.map_or_else(...)`. No behavior change. Co-Authored-By: Claude Opus 4.7 (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.
Summary
Fixes GH#602.
crosslink integrity hydration --repairused to callclear_shared_data()followed byhydrate_to_sqlite(). Any SQLite row not represented in JSON was silently destroyed in the clear step. And the drift detection above the repair only compared issue/milestone counts, so content-level divergence (e.g. adependencies (a, b)row in SQLite that no JSON file mentions) never registered as drift at all.Implements all four mitigations from the issue, in the order it ranked them.
(1) Always snapshot before destroying state
Every
--repairrun writes.crosslink/integrity/hydration-backup-<utc-ts>.sqlitevia SQLite'sVACUUM INTO. Self-contained (works in WAL mode), reversible (drop it in place), and unconditional — runs even when the repair was non-destructive, as an audit trail of what state the repair was applied against.New module:
crosslink/src/db/snapshot.rs.(2) Detect drift by content, not by count
New module
commands::integrity_drift.detect()hydrates JSON into an isolated temp SQLite file (reusing the productionhydrate_to_sqlite),ATTACH-es that file to the main connection, and SQL-diffs every shared table:Returns a categorized
HydrationDriftReportwithis_empty(),has_unrecoverable_loss(), andis_fully_re_emittable()semantics. The reproducer scenario from the issue (INSERT INTO dependenciesdirectly into SQLite) now shows up insqlite_only_dependencieswhere the old count-based check was blind to it.(3) Re-emit SQLite-only state back to JSON when possible
re_emit()walks the drift report and callsSharedWriter::{add_label, add_blocker, add_relation, set_milestone_on_issues}for each row. The SharedWriter idempotency short-circuits from #600 (PR #605) mean these calls are safe even if the JSON side raced ahead between detection and re-emit.Comments and time entries are NOT re-emitted:
crosslink timer. Snapshot is the recovery handle.(4)
--accept-data-lossflag gates destructive repairNew CLI flag on
crosslink integrity hydration. When drift contains rows that re-emit cannot represent, or when noSharedWriteris available (no agent.json / no hub branch),--repairrefuses without the flag, prints the snapshot path, and exits non-zero. With the flag, proceeds destructively (snapshot is still written).Behavior the user will notice
--repairalways writes a snapshot at.crosslink/integrity/hydration-backup-<utc-ts>.sqlite(was: silent destroy).--repairnow tries to re-emit SQLite-only labels/deps/relations/milestone-links back to JSON via the writer — each becomes a real git commit on the hub branch.--repairrefuses when SQLite has comments or time entries that aren't in JSON, unless--accept-data-lossis also given.--repair) now surfaces content-level drift (e.g. "1 sqlite-only dependency(ies)") that the old count check missed.Dependency change
tempfilemoved from[dev-dependencies]to[dependencies]— the drift detector uses a temp SQLite file at runtime for the JSON-view ATTACH. No new transitive deps; the crate was already in the dev tree.Out of scope
.crosslink/integrity/. Could add a--prune-snapshotsflag or a TTL in a follow-up.#427self-heal inhydrate_to_sqlitealready preservescreated_by IS NULLissues; full re-emit for SharedWriter-created issues with no JSON would mean a recursivecreate_issuepath (parents, comments, the full graph), better as its own design.Test plan
cargo test --lib --bin crosslink— 2875 passed (+9 new)cargo clippy --lib --bin crosslink --tests -- -D warnings— cleantest_re_emit_writes_sqlite_only_dependency_to_json): realSharedWriter+ hub cache, injects SQLite-only dep, runsdetect+re_emit, asserts JSON now contains the row anddetectreturns cleanRelated
crosslink issue blockexits non-zero when the blocker is already set #600 (PR fix(shared-writer): make blocker/label/relation mutations idempotent (#600) #605) — SharedWriter idempotency. This PR's re-emit path relies on those short-circuits being in place.git_commit_in_cache_with_argsreports empty error messages for the most common failure mode #601 (PR fix(shared-writer,sync): include stdout in git cache-failure errors (#601) #606) — git error messages. Same code area, separate concern.add_blockerflow is not transactional across JSON / git / SQLite #604 —add_blockertransactionality across JSON/git/SQLite. Structurally complementary: that one is about how drift is produced, this one is about how drift is destroyed.🤖 Generated with Claude Code