From c7bcd32190f0355ad3d6ba5a87ca7eec3fcf2604 Mon Sep 17 00:00:00 2001 From: Pjotr Date: Mon, 16 Nov 2026 00:39:24 +0000 Subject: gists: freeze what was assessed, recompute only what was derived A gist is a judgement about what an entry claims, so it must not drift once made. The store did not hold that invariant: db_backfill_gist ran update reasoning set gist = ?1 where id = ?2 for every row, so any migration that re-derived gists would have replaced every assessed --gist with the body's first sentence. It only escaped notice because the backfill happens to run once, on the version 3 -> 4 upgrade. reasoning.gist_source now says how each gist came about -- 'assessed' when the caller said what the entry claims (--gist), 'derived' when it is the body's first sentence -- and the backfill is guarded: where gist is null or gist_source = 'derived' so a derived gist may be recomputed when the derivation improves, and an assessed one is frozen for the life of the entry. Version 5 of the schema adds the column; a store that predates it cannot say which of its gists were assessed, and the two mistakes are not equal -- labelling an assessed gist derived lets a later migration overwrite a judgement, while labelling a derived one assessed merely freezes a first sentence -- so existing gists become 'assessed' and only rows with no gist are left to the backfill. test/gist_immutable.sh pins it: record one assessed and one derived gist, set user_version back to 3 so the next open re-runs the migration, and assert both are byte-identical afterwards. Against the old backfill the assessed one changes and the test fails. Measured on the production store (139 entries, version 4): the migration to 5 kept every gist and labelled all 139 assessed, no gist was blanked, and listings (log, why, search) are unchanged. Two new entries came out as expected: ('Derived one', derived) and ('Assessed one', assessed). --- test/gist_immutable.sh | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100755 test/gist_immutable.sh (limited to 'test') diff --git a/test/gist_immutable.sh b/test/gist_immutable.sh new file mode 100755 index 0000000..0b3b45b --- /dev/null +++ b/test/gist_immutable.sh @@ -0,0 +1,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)" -- cgit 1.4.1