about summary refs log tree commit diff
path: root/test/gist_immutable.sh
blob: 0b3b45b3f45680c911cefdc68beb896e5ecc282e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/sh
# An assessed gist must survive a full re-migration; a derived one may be
# recomputed.  This is the invariant gist_source exists for, and it used to be
# broken: db_backfill_gist updated every row, so `--gist` was one migration away
# from being replaced by the body's first sentence.
#
# Usage: test/gist_immutable.sh [path-to-ai-memory]
set -eu
BIN=${1:-${AI_MEMORY:-bin/ai-memory}}
WORK=$(mktemp -d)
trap 'rm -rf "$WORK"' EXIT
DB=$WORK/store.sqlite

$BIN --db "$DB" -C . init >/dev/null

# One assessed gist, one derived (no --gist), and one assessed gist whose body's
# first sentence is different -- so a re-derivation is detectable.
$BIN --db "$DB" -C . record --session g --step 1 --kind conclusion \
     --commit none --gist "ASSESSED claim about the first thing" \
     --body "First sentence here. And a second one that says more." >/dev/null
$BIN --db "$DB" -C . record --session g --step 2 --kind conclusion \
     --commit none --body "Derived sentence. Another sentence follows." >/dev/null

q() { python3 - "$DB" "$1" <<'PY'
import sqlite3, sys
db, sql = sys.argv[1], sys.argv[2]
print(sqlite3.connect(db).execute(sql).fetchone())
PY
}

before1=$(q "select gist, gist_source from reasoning where step = 1")
before2=$(q "select gist, gist_source from reasoning where step = 2")

# Force the re-migration path: a store that looks like version 3 (before gist
# existed) makes the backfill run on the next open.
q "select 1" >/dev/null
python3 - "$DB" <<'PY'
import sqlite3, sys
c = sqlite3.connect(sys.argv[1]); c.execute('pragma user_version = 3'); c.commit()
PY
$BIN --db "$DB" -C . status >/dev/null

after1=$(q "select gist, gist_source from reasoning where step = 1")
after2=$(q "select gist, gist_source from reasoning where step = 2")

if [ "$before1" != "$after1" ]; then
	echo "FAIL: the assessed gist changed: $before1 -> $after1" >&2
	exit 1
fi
if [ "$before2" != "$after2" ]; then
	echo "FAIL: the derived gist changed: $before2 -> $after2" >&2
	exit 1
fi
echo "ok: assessed gist frozen ($after1); derived gist kept ($after2)"