cortex
aien-dev/cortex
Brought by drakestapleton
cortex / tests / import / mkwal.sh
| 1 | #!/bin/sh |
| 2 | # mkwal.sh DIR : build WAL fixtures with ONE long-lived sqlite3 writer (oracle CLI 3.45.1) fed |
| 3 | # through a fifo, so the WAL is never checkpointed away by a closing connection. |
| 4 | # txn0 schema + fill.sql, then wal_checkpoint(TRUNCATE): the db file holds the base state. |
| 5 | # txnA large update (many frames), committed, wal_checkpoint(PASSIVE) backfills it; |
| 6 | # the next write restarts the WAL with new salts, so txnA frames stay behind as stale. |
| 7 | # txn1 small committed change -> snap1/ (db + wal copied, no shm) |
| 8 | # txn2 second committed change -> (frame counts recorded) |
| 9 | # txn3 uncommitted: cache_size=5 forces spilled, non-commit frames into the WAL -> snap3/ |
| 10 | # then ROLLBACK and close (that checkpoint makes w.db = committed state after txn2). |
| 11 | # Frame boundaries are not inferable from the file size (the restarted WAL keeps its old length); |
| 12 | # run.sh reads them with the independent walfix.py scanner. |
| 13 | set -eu |
| 14 | T=$(cd "$(dirname "$0")" && pwd) |
| 15 | D=$1 |
| 16 | rm -rf "$D"; mkdir -p "$D"; cd "$D" |
| 17 | mkfifo cmd |
| 18 | /usr/bin/sqlite3 -batch -bail w.db < cmd > out.txt 2>&1 & |
| 19 | pid=$! |
| 20 | exec 3> cmd |
| 21 | n=0 |
| 22 | sync_point() { |
| 23 | n=$((n + 1)); printf '.print SYNC%s\n' "$n" >&3 |
| 24 | i=0 |
| 25 | while ! grep -q "^SYNC$n\$" out.txt; do |
| 26 | i=$((i + 1)); if [ $i -gt 600 ]; then echo "mkwal: writer stuck at SYNC$n" >&2; cat out.txt >&2; exit 1; fi |
| 27 | sleep 0.1 |
| 28 | done |
| 29 | } |
| 30 | { echo "PRAGMA page_size=4096;"; echo "PRAGMA journal_mode=WAL;"; echo "PRAGMA wal_autocheckpoint=0;" |
| 31 | cat "$T/schema.sql" "$T/fill.sql"; echo "PRAGMA wal_checkpoint(TRUNCATE);"; } >&3 |
| 32 | sync_point |
| 33 | cat >&3 <<'EOF' |
| 34 | BEGIN; |
| 35 | UPDATE entities SET metadata_json = '{"txnA":' || rowid || '}', revision = revision + 0; |
| 36 | COMMIT; |
| 37 | PRAGMA wal_checkpoint(PASSIVE); |
| 38 | EOF |
| 39 | sync_point |
| 40 | cat >&3 <<'EOF' |
| 41 | BEGIN; |
| 42 | INSERT INTO spaces VALUES ('sp-t1', 'txn1-space', '2026-10-26'); |
| 43 | UPDATE entities SET confidence = 0.75, content = 'txn1 ' || rowid WHERE rowid % 10 = 0; |
| 44 | COMMIT; |
| 45 | EOF |
| 46 | sync_point |
| 47 | mkdir snap1; cp w.db w.db-wal snap1/ |
| 48 | cat >&3 <<'EOF' |
| 49 | BEGIN; |
| 50 | INSERT INTO claims (id, space_id, space_slug, subject_entity_id, predicate, created_at) |
| 51 | VALUES ('cl-t2', 'sp-t1', 'txn1-space', 'ent-00010', 'txn2', '2026-10-26'); |
| 52 | DELETE FROM entities WHERE rowid % 10 = 3; |
| 53 | UPDATE spaces SET created_at = 'txn2' WHERE id = 'sp-0'; |
| 54 | COMMIT; |
| 55 | EOF |
| 56 | sync_point |
| 57 | cat >&3 <<'EOF' |
| 58 | PRAGMA cache_size=5; |
| 59 | BEGIN; |
| 60 | UPDATE spaces SET slug = 'UNCOMMITTED' WHERE id = 'sp-0'; |
| 61 | INSERT INTO entities (id, space_id, space_slug, entity_type, canonical_name, content, created_at) |
| 62 | WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < 400) |
| 63 | SELECT 'unc-' || i, 'sp-0', 'atlas-memory', 'fact', 'uncommitted', hex(zeroblob(3000)), 'x' FROM n; |
| 64 | EOF |
| 65 | sync_point |
| 66 | mkdir snap3; cp w.db w.db-wal snap3/ |
| 67 | printf 'ROLLBACK;\n.quit\n' >&3 |
| 68 | exec 3>&- |
| 69 | wait $pid |