interplane.Get Omega
Read in your language

cortex

aien-dev/cortex

Brought by drakestapleton

branch main Branches Tags History

cortex / tests / import / mkwal.sh

69 lines · 2767 bytes Raw Blame History
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.
13set -eu
14T=$(cd "$(dirname "$0")" && pwd)
15D=$1
16rm -rf "$D"; mkdir -p "$D"; cd "$D"
17mkfifo cmd
18/usr/bin/sqlite3 -batch -bail w.db < cmd > out.txt 2>&1 &
19pid=$!
20exec 3> cmd
21n=0
22sync_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
32sync_point
33cat >&3 <<'EOF'
34BEGIN;
35UPDATE entities SET metadata_json = '{"txnA":' || rowid || '}', revision = revision + 0;
36COMMIT;
37PRAGMA wal_checkpoint(PASSIVE);
38EOF
39sync_point
40cat >&3 <<'EOF'
41BEGIN;
42INSERT INTO spaces VALUES ('sp-t1', 'txn1-space', '2026-10-26');
43UPDATE entities SET confidence = 0.75, content = 'txn1 ' || rowid WHERE rowid % 10 = 0;
44COMMIT;
45EOF
46sync_point
47mkdir snap1; cp w.db w.db-wal snap1/
48cat >&3 <<'EOF'
49BEGIN;
50INSERT 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');
52DELETE FROM entities WHERE rowid % 10 = 3;
53UPDATE spaces SET created_at = 'txn2' WHERE id = 'sp-0';
54COMMIT;
55EOF
56sync_point
57cat >&3 <<'EOF'
58PRAGMA cache_size=5;
59BEGIN;
60UPDATE spaces SET slug = 'UNCOMMITTED' WHERE id = 'sp-0';
61INSERT 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;
64EOF
65sync_point
66mkdir snap3; cp w.db w.db-wal snap3/
67printf 'ROLLBACK;\n.quit\n' >&3
68exec 3>&-
69wait $pid