#!/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)"