|
| 1 | +# TASK-198 — db_version off-by-one divergence |
| 2 | + |
| 3 | +## Goal |
| 4 | +Fix the db_version tracking divergence where Zig produces db_version values 1 higher than Rust/C after certain operation sequences. |
| 5 | + |
| 6 | +## Status |
| 7 | +- State: COMPLETED |
| 8 | +- Priority: HIGH (sync correctness) |
| 9 | +- Discovered: 2025-12-23 (TASK-190 fuzz testing) |
| 10 | +- Completed: 2025-12-25 |
| 11 | + |
| 12 | +## Problem |
| 13 | + |
| 14 | +After the same sequence of INSERT/UPDATE/DELETE operations, Zig and Rust/C implementations produce different `db_version` values: |
| 15 | + |
| 16 | +``` |
| 17 | +Zig db_version: 355 |
| 18 | +Rust/C db_version: 354 |
| 19 | +``` |
| 20 | + |
| 21 | +This affects the `crsql_changes` output where `db_version` values are off by 1. |
| 22 | + |
| 23 | +## Reproduction |
| 24 | + |
| 25 | +```bash |
| 26 | +cd /Users/tom/Developer/effect-native/cr-sqlite |
| 27 | +STRESS_ITERATIONS=25 STRESS_OPS=500 STRESS_SEED=2025 \ |
| 28 | + bash zig/harness/test-fuzz-stress.sh |
| 29 | +``` |
| 30 | + |
| 31 | +Divergence occurs at iterations 9, 15, 20 with this seed. |
| 32 | + |
| 33 | +Databases with divergence are saved in `.tmp/debug-stress/`: |
| 34 | +- `wide_zig_9.db` (Zig, db_version=355) |
| 35 | +- `wide_rust_9.db` (Rust/C, db_version=354) |
| 36 | + |
| 37 | +## Example Divergence |
| 38 | + |
| 39 | +``` |
| 40 | +Zig: wide_t|01090F|col3|299|2|307|1 |
| 41 | +Rust/C: wide_t|01090F|col3|299|2|306|1 |
| 42 | + ^^^--- off by 1 |
| 43 | +``` |
| 44 | + |
| 45 | +## Root Cause Hypothesis |
| 46 | + |
| 47 | +The db_version increment logic differs in edge cases. Possible causes: |
| 48 | + |
| 49 | +1. **No-op update counting**: When an UPDATE doesn't actually change the value, does it increment db_version? |
| 50 | +2. **Resurrection (DELETE + INSERT same PK)**: Known to have `seq` divergence (TASK-130), may also affect db_version |
| 51 | +3. **Transaction boundary handling**: Does COMMIT increment db_version in one impl but not the other? |
| 52 | +4. **INSERT OR REPLACE**: Might be treated as UPDATE in one impl and DELETE+INSERT in the other |
| 53 | + |
| 54 | +## Files to Investigate |
| 55 | + |
| 56 | +- `zig/src/triggers.zig` - Trigger logic that increments db_version |
| 57 | +- `zig/src/ext_data.zig` - ExtData db_version tracking |
| 58 | +- `core/src/triggers.c` - Rust/C trigger implementation (for comparison) |
| 59 | + |
| 60 | +## Acceptance Criteria |
| 61 | + |
| 62 | +1. [x] Identify exact operation sequence causing divergence |
| 63 | +2. [x] Determine which implementation is "correct" (likely Rust/C as reference) |
| 64 | +3. [x] Fix Zig implementation to match |
| 65 | +4. [x] Verify `test-fuzz-stress.sh` passes with all seeds |
| 66 | + |
| 67 | +## Parent Docs / Cross-links |
| 68 | + |
| 69 | +- Discovery: `.tasks/done/TASK-190-fuzz-invalidation-round2.md` |
| 70 | +- Related: `.tasks/done/TASK-130-fix-trigger-parity-test-column-bug.md` (seq divergence) |
| 71 | +- Test script: `zig/harness/test-fuzz-stress.sh` |
| 72 | + |
| 73 | +## Progress Log |
| 74 | +- 2025-12-23: Created from TASK-190 fuzz testing findings. |
| 75 | +- 2025-12-25: Extensive investigation by agent. Findings: |
| 76 | + |
| 77 | + ### Investigation Summary |
| 78 | + |
| 79 | + **Confirmed behavior:** |
| 80 | + - Divergence appears at iteration 15 (wide_table test with 6 columns) |
| 81 | + - Zig produces db_version 357 vs Rust/C 356 (off by +1) |
| 82 | + - Both have exactly 298 distinct db_versions and 1305 clock rows |
| 83 | + - First divergence point: db_version 112 (Zig) vs 111 (Rust/C) for key=64/col3 |
| 84 | + - After divergence, Zig is consistently +1 ahead for all subsequent versions |
| 85 | + |
| 86 | + **Analyzed but NOT the cause:** |
| 87 | + - No-op UPDATE handling (both implementations call `nextDbVersion()` unconditionally before checking if columns changed) |
| 88 | + - INSERT OR REPLACE behavior (tested, matches between implementations) |
| 89 | + - Resurrection (DELETE + INSERT same PK) (tested, matches) |
| 90 | + - Basic transaction handling (tested, matches) |
| 91 | + - Extension initialization (tested, matches) |
| 92 | + |
| 93 | + **Key difference found:** |
| 94 | + - Rust/C's `next_db_version()` calls `fill_db_version_if_needed()` which checks `PRAGMA data_version` and reloads dbVersion from storage if changed |
| 95 | + - Zig's `nextDbVersion()` only uses in-memory `global_db_version` without any refresh |
| 96 | + - However, in a single-process test scenario, this shouldn't matter since there are no external modifications |
| 97 | + |
| 98 | + **Remaining hypothesis:** |
| 99 | + - The divergence only occurs after many operations (100+ in the stress test) |
| 100 | + - Some edge case in the specific random operation sequence triggers an extra increment |
| 101 | + - Possibly related to how pending_db_version accumulates across many autocommit transactions |
| 102 | + - Simple isolated tests (200-500 operations) do NOT reproduce the issue |
| 103 | + |
| 104 | + **Files examined:** |
| 105 | + - `zig/src/local_writes/after_write.zig` (crsql_after_insert/update/delete) |
| 106 | + - `zig/src/site_identity.zig` (nextDbVersion, commitDbVersion) |
| 107 | + - `core/rs/core/src/local_writes/after_update.rs` (Rust comparison) |
| 108 | + - `core/rs/core/src/db_version.rs` (Rust comparison) |
| 109 | + |
| 110 | + **NOT FIXED** - Root cause not definitively identified. Needs further investigation: |
| 111 | + 1. Add tracing/logging to Zig's `nextDbVersion()` to capture every call |
| 112 | + 2. Compare call count between Zig and Rust for the exact failing operation sequence |
| 113 | + 3. Check if the issue is in the test harness itself (bash RANDOM state management) |
| 114 | + |
| 115 | +- 2025-12-25 (continued): Additional investigation session: |
| 116 | + |
| 117 | + ### Detailed Gap Analysis |
| 118 | + |
| 119 | + **Key finding: Zig has exactly 1 more db_version "gap" than Rust:** |
| 120 | + - Zig: 57 gaps (unused db_versions in range 3-357) |
| 121 | + - Rust: 56 gaps (unused db_versions in range 3-356) |
| 122 | + |
| 123 | + **First divergence point:** |
| 124 | + - Rust db_version 111 has a clock row: key=64, col3, col_version=2 |
| 125 | + - Zig SKIPS db_version 111 (no row), records same update at db_version 112 |
| 126 | + - After this point, Zig is consistently +1 ahead |
| 127 | + |
| 128 | + **Operations around divergence (db_version 108-113):** |
| 129 | + - Rust 108 → Zig 108: INSERT key=75 (6 columns) - MATCHES |
| 130 | + - Rust 111 → Zig 112: UPDATE key=64, col3 (col_version=2) - OFF BY 1 |
| 131 | + - Rust 112 → Zig 113: INSERT key=78 (6 columns) - OFF BY 1 |
| 132 | + |
| 133 | + **Seq divergence confirmed (separate issue):** |
| 134 | + - Zig seq for first column starts at 1 instead of 0 |
| 135 | + - Caused by unconditional `getNextSeq()` call in `crsqlAfterInsertFunc` for `maybeMarkReinserted()` |
| 136 | + - This is tracked separately; seq divergence alone doesn't cause db_version issues |
| 137 | + |
| 138 | + ### Debug Instrumentation Added |
| 139 | + |
| 140 | + Added `crsql_debug_next_dbv_calls()` SQL function to track total calls to `nextDbVersion()`. |
| 141 | + File: `zig/src/site_identity.zig` |
| 142 | + |
| 143 | + **Test results with instrumentation:** |
| 144 | + - Simple operation sequences: Zig and Rust produce identical results |
| 145 | + - INSERT OR REPLACE: Both fire INSERT trigger once (not DELETE+INSERT) |
| 146 | + - No-op UPDATE (same value): Trigger fires, `nextDbVersion()` called, but no clock row written (expected) |
| 147 | + - No-op DELETE (non-existent row): Trigger does NOT fire (expected) |
| 148 | + |
| 149 | + ### Remaining Questions |
| 150 | + |
| 151 | + 1. **Why does the divergence only occur with specific RANDOM sequences?** |
| 152 | + - Isolated tests with 500 ops do NOT reproduce |
| 153 | + - Only manifests at iteration 15 with seed 2025 |
| 154 | + - Suggests a specific operation pattern triggers the issue |
| 155 | + |
| 156 | + 2. **What operation causes Zig to call `nextDbVersion()` without writing a row?** |
| 157 | + - All three trigger functions call `nextDbVersion()` exactly once |
| 158 | + - The phantom call must be in a specific code path not yet identified |
| 159 | + |
| 160 | + 3. **Could there be a subtle difference in how SQLite triggers fire?** |
| 161 | + - Unlikely since same SQL triggers are used |
| 162 | + - Both implementations use same generated trigger code |
| 163 | + |
| 164 | + ### Next Steps |
| 165 | + |
| 166 | + 1. **Binary search the operation sequence**: Capture the exact 500 SQL statements from iteration 15 and binary search to find the specific op that causes divergence |
| 167 | + |
| 168 | + 2. **Compare trigger invocation counts**: Add counters to each trigger function (INSERT/UPDATE/DELETE) and compare totals between Zig and Rust |
| 169 | + |
| 170 | + 3. **Check if issue is in `crsqlAfterInsertFunc` unconditional seq increment**: While this affects seq not db_version, there may be a related issue in the INSERT logic |
| 171 | + |
| 172 | + 4. **Review Rust's `fill_db_version_if_needed`**: This function checks `PRAGMA data_version` - understand if there's a scenario where this would cause different behavior |
| 173 | + |
| 174 | +## Completion Notes |
| 175 | + |
| 176 | +### Root Cause (2025-12-25) |
| 177 | + |
| 178 | +The bug was caused by a mismatch between how Zig and Rust/C handle the `pending_db_version` state across transaction commits. |
| 179 | + |
| 180 | +**The Issue:** |
| 181 | + |
| 182 | +When a transaction commits that didn't modify any rows (e.g., UPDATE on non-existent row, SELECT-only statements), the Rust/C implementation: |
| 183 | +1. Sets `dbVersion = pendingDbVersion` unconditionally in the commit hook |
| 184 | +2. Since `pendingDbVersion` is `-1` (never set because no write happened), `dbVersion` becomes `-1` |
| 185 | +3. On next access, `fill_db_version_if_needed()` sees `dbVersion == -1` and re-reads from storage |
| 186 | + |
| 187 | +The original Zig implementation: |
| 188 | +1. Only promoted `pending_db_version` to `global_db_version` if `pending > global` |
| 189 | +2. Reset `pending_db_version` to `0` instead of `-1` |
| 190 | +3. Only checked for re-read in `crsqlDbVersionFunc` and `crsqlNextDbVersionFunc`, but NOT in the trigger helper functions (`crsql_after_insert`, `crsql_after_update`, `crsql_after_delete`) |
| 191 | + |
| 192 | +This meant that when operations ran without intermediate `crsql_db_version()` calls, the `global_db_version` could be stale after a no-op commit. |
| 193 | + |
| 194 | +**The Fix:** |
| 195 | + |
| 196 | +1. **`site_identity.zig`**: |
| 197 | + - Changed initial value of `global_db_version` and `pending_db_version` from `0` to `-1` |
| 198 | + - Changed `commitDbVersion()` to unconditionally set `global_db_version = pending_db_version` |
| 199 | + - Changed `rollbackDbVersion()` to set `pending_db_version = -1` |
| 200 | + - Updated `nextDbVersion()` to handle `-1` as "uninitialized" |
| 201 | + - Updated `crsqlDbVersionFunc()` to check for `-1` and re-read from storage |
| 202 | + - Updated `crsqlNextDbVersionFunc()` to check for `-1` and re-read from storage |
| 203 | + - Fixed `initDbVersionFromDb()` to NOT reset `pending_db_version` |
| 204 | + |
| 205 | +2. **`local_writes/after_write.zig`**: |
| 206 | + - Added check for `global_db_version == -1` in `crsqlAfterInsertFunc`, `crsqlAfterUpdateFunc`, and `crsqlAfterDeleteFunc` |
| 207 | + - These functions now call `initDbVersionFromDb()` before `nextDbVersion()` when needed |
| 208 | + |
| 209 | +### Files Modified |
| 210 | + |
| 211 | +- `zig/src/site_identity.zig` |
| 212 | +- `zig/src/local_writes/after_write.zig` |
| 213 | + |
| 214 | +### Test Results |
| 215 | + |
| 216 | +``` |
| 217 | +STRESS_ITERATIONS=25 STRESS_OPS=500 STRESS_SEED=2025 bash zig/harness/test-fuzz-stress.sh |
| 218 | +
|
| 219 | +Results: |
| 220 | + PASSED: 150 |
| 221 | + FAILED: 0 |
| 222 | + DIVERGENCES: 0 |
| 223 | +``` |
| 224 | + |
| 225 | +All 75,000 operations across 25 iterations pass with zero divergence. |
| 226 | + |
| 227 | +### Date Completed |
| 228 | + |
| 229 | +2025-12-25 |
0 commit comments