about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-11-16 15:57:20 +0000
committerPjotr <wrk@bosa.lan>2026-11-16 15:57:20 +0000
commit4e3a785e6aec4f8b2eafad82cb35524359751946 (patch)
tree0f38a5033228046d65d9ad8d356bbb9998d073ac
parent345dccb3ea63ac6aa0d9c7d17132bc5e614e545f (diff)
downloadai-memory-4e3a785e6aec4f8b2eafad82cb35524359751946.tar.gz
db: never lower the schema stamp, and refuse a store a newer binary wrote
Two failures found by using the store from more than one binary at once.

A stale ai-memory opened a version 5 store and left it stamped version 3.
SQLite cannot drop a column, so the data survived (gist_source was still
there), but the stamp lied -- and the next newer binary then re-ran data
steps it believed were pending.  The migration chain ends at the version the
*binary* knows and wrote that number unconditionally, which is what did it.

So: schema_version() reads the version out of the SCHEMA the binary embeds,
so the number lives in schema.sql alone and the two cannot disagree; the
stamp only ever moves forward (`if version > started_at`); and a store whose
version is greater than this binary's is refused before anything is touched:

    ai-memory: this store is schema v9; this binary knows v5
    ai-memory: it was written by a newer ai-memory -- upgrade this binary
               (scripts/install.sh).  Nothing was changed.

The second failure was the migration order: a store at version 3 that a
newer binary had already written gists into died on every command with
"no such column: gist_source", because the version 3 -> 4 backfill referenced
a column the version 4 -> 5 step adds.  The gist columns are now ensured
(guarded by the table existing, so a fresh store still gets them from SCHEMA)
before any backfill runs, and the skew hint fires on any "no such column" or
"no such table", not only on an empty error message.

Verified: a v9 store is refused with the stamp untouched; a store the stale
binary knocked back to v3 upgrades to v5 with its assessed gists intact; the
gist immutability test still passes; and all six stores on this box are now at
v5 after the new binary opened them.  schema_version() needed care in Odin:
SCHEMA is a compile-time constant, so it is copied to a string before being
walked by index.
-rw-r--r--db.odin88
1 files changed, 78 insertions, 10 deletions
diff --git a/db.odin b/db.odin
index 7f7d44a..dc10cab 100644
--- a/db.odin
+++ b/db.odin
@@ -12,6 +12,32 @@ DEFAULT_DB_PATH :: "ai-memory.sqlite"
 
 SCHEMA :: #load("schema.sql", string)
 
+// schema_version is the version SCHEMA declares, so the number exists in one
+// place only: the binary and the schema it creates cannot disagree.
+schema_version :: proc() -> int {
+	// A copy up front: SCHEMA is a compile-time constant and Odin only allows
+	// constant indices into one.
+	schema: string = SCHEMA
+	marker :: "pragma user_version = "
+	index := strings.index(schema, marker)
+	if index < 0 {
+		return 0
+	}
+	// SCHEMA is a compile-time constant, so it is walked by index rather than
+	// sliced: Odin will not slice a constant with a runtime offset.
+	version := 0
+	at := index + len(marker)
+	for at < len(schema) {
+		c := schema[at]
+		if c < '0' || c > '9' {
+			break
+		}
+		version = version * 10 + int(c - '0')
+		at += 1
+	}
+	return version
+}
+
 Db :: struct {
 	handle: ^sqlite3.Connection,
 }
@@ -74,7 +100,9 @@ db_report :: proc(db: ^Db, rc: sqlite3.Result_Code, err: cstring) {
 	} else {
 		fmt.eprintfln("ai-memory: sql error (%v): %s", rc, msg)
 	}
-	if extended == .Error && len(msg) == 0 {
+	if (extended == .Error && len(msg) == 0) ||
+	   strings.contains(msg, "no such column") ||
+	   strings.contains(msg, "no such table") {
 		// SQLITE_ERROR with no message is what a *statement* failure looks like
 		// when the schema is older than this binary: a column the code writes
 		// does not exist yet.  The store is fine; the schema needs upgrading.
@@ -172,6 +200,20 @@ db_migrate :: proc(db: ^Db) -> bool {
 	// and an unconditional ALTER then fails with "duplicate column name" -- which
 	// reads as corruption when it is only version skew.
 	version := db_user_version(db)
+	// A newer ai-memory may have written this store, and this binary does not
+	// know its schema.  Refuse before touching anything: the alternative is what
+	// a stale binary did here -- it silently rewrote user_version from 5 to 3,
+	// because its migration chain ends at the version *it* knows.  The data was
+	// fine (SQLite cannot drop a column, so gist_source survived), but the stamp
+	// lied, and the next newer binary re-ran data steps it thought were pending.
+	if version > schema_version() {
+		fmt.eprintfln("ai-memory: this store is schema v%d; this binary knows v%d",
+		              version, schema_version())
+		fmt.eprintln("ai-memory: it was written by a newer ai-memory -- upgrade " +
+		             "this binary (scripts/install.sh). Nothing was changed.")
+		return false
+	}
+	started_at := version
 
 	// Version 2: basis and supersedes on reasoning.
 	if version == 1 {
@@ -196,13 +238,26 @@ db_migrate :: proc(db: ^Db) -> bool {
 		version = 3
 	}
 
+	// The gist columns are ensured before any data step above uses them, because
+	// db_add_column is inspection-guarded and the backfills below depend on both
+	// existing whatever the store's recorded version says.  A store that sat at
+	// version 3 while a newer binary wrote gists into it failed exactly here: the
+	// version 3 -> 4 backfill referenced gist_source before version 5 had added
+	// it, and every command then died on "no such column: gist_source", not just
+	// the first.  Guarded by the table's existence: a fresh store is version 0
+	// with no tables at all, and gets both columns from SCHEMA at the end.
+	if db_has_table(db, "reasoning") {
+		if !db_add_column(db, "reasoning", "gist", "text") ||
+		   !db_add_column(db, "reasoning", "gist_source", "text") {
+			return false
+		}
+	}
+
 	// Version 4: reasoning.gist, the one-line claim.  Entries written before the
 	// column existed get a first-sentence gist.
 	if version == 3 {
-		if db_add_column(db, "reasoning", "gist", "text") {
-			if !db_backfill_gist(db) {
-				return false
-			}
+		if !db_backfill_gist(db) {
+			return false
 		}
 		version = 4
 	}
@@ -213,15 +268,15 @@ db_migrate :: proc(db: ^Db) -> bool {
 	// the safe answer: marking it 'assessed' means no later migration may touch
 	// it.  Rows with no gist at all are left alone for the backfill to fill.
 	if version == 4 {
-		if db_add_column(db, "reasoning", "gist_source", "text") {
-			if !db_label_existing_gists(db) {
-				return false
-			}
+		if !db_label_existing_gists(db) {
+			return false
 		}
 		version = 5
 	}
 
-	if version > 0 {
+	// Only ever move the stamp forward.  Writing it unconditionally is how the
+	// downgrade happened: a binary that knows no steps past v3 still stamped 3.
+	if version > started_at {
 		if !db_exec(db, fmt.tprintf("pragma user_version = %d", version)) {
 			return false
 		}
@@ -241,6 +296,19 @@ db_add_column :: proc(db: ^Db, table, column, decl: string) -> bool {
 	return true
 }
 
+// db_has_table reports whether the store has TABLE.  A fresh store has none:
+// `init' creates them from SCHEMA after the migration steps have run.
+db_has_table :: proc(db: ^Db, table: string) -> bool {
+	s, ok := stmt(db, "select 1 from sqlite_master where type = 'table' and name = ?1")
+	if !ok {
+		return false
+	}
+	stmt_bind_text(&s, 1, table)
+	found := stmt_step(&s) == .Row
+	stmt_close(&s)
+	return found
+}
+
 // db_has_column reports whether TABLE already has COLUMN.
 db_has_column :: proc(db: ^Db, table, column: string) -> bool {
 	s, ok := stmt(db, fmt.tprintf("pragma table_info(%s)", table))