writeonce/database
shoney.arickathil 0b9f09fc12 fix(db2-keys): stage the schema head before the first offset capture
- `seed` of docs/examples/residency (`resident: keys`) on a FRESH WO_DATA
  segfaulted rc 139 in both the dir and the file form; pre-existing.
- Root cause: the databasev2 12 head record was staged lazily INSIDE the
  first `wo_wal_append_*` (wal.c `stage()`), after db.c:78/293 had read
  `koff = wo_wal_next_offset(w)`; the first keys-resident row was re-pointed
  at the schema record and its first read folded "record header is
  malformed"; `wo_idx_probe` borrows with `msg == NULL` -> zero-page write.
- Fix: one helper `stage_schema_head` shared by `stage()`,
  `wo_wal_ensure_schema` and `wo_wal_next_offset` (no longer a pure inline):
  the head is staged before any caller observes `off + len`. Still lazy,
  never for a log that stays empty; head-stage OOM is `wo_wal_stage_fatal`.
  db.c untouched; compaction/migrate stage the head explicitly, unaffected.
- Failing test first: `test_keys_resident_fresh_log_first_row` (test_wal.c),
  the db.c:78 sequence call for call, then read-by-id, `wo_idx_probe`,
  replay. Pre-fix: `koff != 0` FAIL, `wo_row_read` -1 "record header is
  malformed", ASan SEGV `wo_wal_fold_row_at wal.c:1871` via `table.c:373`.
- Gates: test_wal 6650/0 (was 6629); `make -C runtime test` 21 suites
  8452/0 (was 8431); wovm-asan clean; residency `seed` + restart `order`
  under wovm_asan rc 0 on a fresh dir AND a fresh app.db; a control build
  with wal.c/wal.h reverted reproduces the SEGV.
- CODE-LOGIC §Schema migrations: "Head before any offset capture" bullet.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
(cherry picked from commit 631007839451fb970e9dec83338d19c09b3043ab)
2026-09-15 01:16:13 +02:00
..
src fix(db2-keys): stage the schema head before the first offset capture 2026-09-15 01:16:13 +02:00