|
| 1 | +# TASK-202 — Fix INSERT INTO crsql_changes Failure (CRITICAL) |
| 2 | + |
| 3 | +## Goal |
| 4 | +Fix the critical bug where `INSERT INTO crsql_changes` fails in the Zig implementation, completely breaking cross-device sync. |
| 5 | + |
| 6 | +## Status |
| 7 | +- State: triage |
| 8 | +- Priority: **P0 CRITICAL** (sync is completely broken) |
| 9 | +- Discovered: 2025-12-25 (TASK-194 real-world app simulation) |
| 10 | + |
| 11 | +## Problem |
| 12 | + |
| 13 | +When applying changes from another device via `INSERT INTO crsql_changes`, the Zig implementation fails: |
| 14 | + |
| 15 | +``` |
| 16 | +debug(changes_vtab): changesUpdate INSERT: table=todos, cid=title... |
| 17 | +debug(changes_vtab): changesUpdate: no local row, inserting new row |
| 18 | +debug(changes_vtab): changesUpdate: insertOrUpdateColumn failed |
| 19 | +Error: stepping, SQL logic error |
| 20 | +``` |
| 21 | + |
| 22 | +**This is the core sync mechanism of cr-sqlite. Without it, the extension is non-functional.** |
| 23 | + |
| 24 | +## Reproduction |
| 25 | + |
| 26 | +```bash |
| 27 | +cd /Users/tom/Developer/effect-native/cr-sqlite |
| 28 | +bash zig/harness/test-app-todo.sh |
| 29 | +``` |
| 30 | + |
| 31 | +The test creates a todo on "Device A", exports changes via `SELECT * FROM crsql_changes`, then tries to apply them to "Device B" via `INSERT INTO crsql_changes`. Rust/C succeeds, Zig fails. |
| 32 | + |
| 33 | +Minimal reproduction: |
| 34 | +```sql |
| 35 | +-- Device A (Zig) |
| 36 | +CREATE TABLE todos(id TEXT PRIMARY KEY, title TEXT); |
| 37 | +SELECT crsql_as_crr('todos'); |
| 38 | +INSERT INTO todos VALUES ('1', 'Test'); |
| 39 | + |
| 40 | +-- Export changes |
| 41 | +SELECT * FROM crsql_changes; |
| 42 | +-- Returns: todos|1|title|Test|1|1|<site_id>|1|1 |
| 43 | + |
| 44 | +-- Device B (Zig) - THIS FAILS |
| 45 | +CREATE TABLE todos(id TEXT PRIMARY KEY, title TEXT); |
| 46 | +SELECT crsql_as_crr('todos'); |
| 47 | +INSERT INTO crsql_changes VALUES ('todos', '1', 'title', 'Test', 1, 1, X'...', 1, 1); |
| 48 | +-- Error: SQL logic error |
| 49 | +``` |
| 50 | + |
| 51 | +## Root Cause (Suspected) |
| 52 | + |
| 53 | +The error occurs in `zig/src/changes_vtab.zig` in the `changesUpdate` function, specifically in the `insertOrUpdateColumn` code path when the local row doesn't exist. |
| 54 | + |
| 55 | +Possible causes: |
| 56 | +1. SQL statement preparation failure (wrong column names/types) |
| 57 | +2. Missing table/column in PKS table lookup |
| 58 | +3. Incorrect binding of site_id blob |
| 59 | +4. Schema mismatch between clock table and expected format |
| 60 | + |
| 61 | +## Files to Investigate |
| 62 | + |
| 63 | +- `zig/src/changes_vtab.zig` — `changesUpdate` function, especially "no local row" branch |
| 64 | +- `zig/src/merge_insert.zig` — `insertOrUpdateColumn` and related functions |
| 65 | +- `zig/harness/test-app-todo.sh` — Reproduction script |
| 66 | + |
| 67 | +## Acceptance Criteria |
| 68 | + |
| 69 | +1. [ ] `INSERT INTO crsql_changes` succeeds for new rows |
| 70 | +2. [ ] `bash zig/harness/test-app-todo.sh` passes on Zig |
| 71 | +3. [ ] All 3 app simulation tests pass (todo, chat, inventory) |
| 72 | +4. [ ] Existing parity tests continue to pass |
| 73 | + |
| 74 | +## Parent Docs / Cross-links |
| 75 | + |
| 76 | +- Discovered in: `.tasks/active/TASK-194-real-world-app-simulation.md` |
| 77 | +- Test scripts: `zig/harness/test-app-*.sh` |
| 78 | +- Gap backlog: `research/zig-cr/92-gap-backlog.md` |
| 79 | + |
| 80 | +## Progress Log |
| 81 | +- 2025-12-25: Created from TASK-194 findings. This is P0 blocker. |
| 82 | + |
| 83 | +## Completion Notes |
| 84 | +(Empty until done.) |
0 commit comments