store: auto-migrate old KGs (edges.origin_question); machine: abort honestly on dead LM

- KnowledgeStore._migrate(): idempotent additive migration on open — a
  pre-2026-10-19 KG no longer crashes the set gate with 'no such column'
- _lm_dead(): transport/timeout deaths abort the run with notes (no answer
  beats a made-up one); code bugs still crash loudly
- tests: migration from a hand-built old-schema store; LM-death abort (24/24)
This commit is contained in:
2026-10-19 12:11:59 +02:00
parent e5482bd092
commit b14915079e
4 changed files with 109 additions and 1 deletions
+11
View File
@@ -87,9 +87,20 @@ class KnowledgeStore:
self.path.parent.mkdir(parents=True, exist_ok=True)
self.db = sqlite3.connect(self.path)
self.db.executescript(SCHEMA)
self._migrate()
self.db.commit()
self._mirror: Path | None = None
def _migrate(self) -> None:
"""Idempotent, additive migrations. CREATE TABLE IF NOT EXISTS never
upgrades an existing file, so stores predating a column change must
be migrated here or every query touching the column crashes
(2026-10-19: edges.origin_question, added with the set gate)."""
cols = {r[1] for r in self.db.execute("PRAGMA table_info(edges)")}
if "origin_question" not in cols:
self.db.execute("ALTER TABLE edges ADD COLUMN origin_question TEXT")
self.db.commit()
# ---- documents & quotes ------------------------------------------------
def add_document(self, url: str, title: str, fetched_at: str, sha256: str,
trusted: bool = True, source: str = "wikipedia") -> int:
+43 -1
View File
@@ -85,6 +85,47 @@ def test_store_four_cases(tmp="/tmp/floop_kg_test.sqlite"):
print("ok test_store_four_cases")
def test_migration_old_store():
"""A pre-2026-10-19 KG (no origin_question column) must migrate on open,
not crash the set gate (Emili's 2026-10-19 home run)."""
import sqlite3
p = Path("/tmp/floop_kg_old.sqlite")
if p.exists():
p.unlink()
# hand-build an OLD-schema store: edges WITHOUT origin_question
db = sqlite3.connect(p)
db.executescript("""
CREATE TABLE documents (
id INTEGER PRIMARY KEY, url TEXT UNIQUE, source TEXT, fetched_at TEXT,
sha256 TEXT, trusted INTEGER, title TEXT);
CREATE TABLE quotes (
id INTEGER PRIMARY KEY, doc_id INTEGER, verbatim TEXT, url TEXT, access_date TEXT);
CREATE TABLE nodes (id INTEGER PRIMARY KEY, norm_name TEXT, sort TEXT,
UNIQUE(norm_name, sort));
CREATE TABLE edges (id INTEGER PRIMARY KEY, src_node INTEGER, rel TEXT,
dst_value TEXT, dst_node INTEGER, quote_id INTEGER, extraction_confidence REAL,
ref_date TEXT, supersedes INTEGER, preferred INTEGER DEFAULT 0,
retracted INTEGER DEFAULT 0, retraction_reason TEXT, corroboration INTEGER DEFAULT 1);
""")
db.execute("INSERT INTO nodes(norm_name, sort) VALUES('germany','PLAC'),"
"('european union','CONCEPT')")
nid = db.execute("SELECT id FROM nodes WHERE norm_name='germany'").fetchone()[0]
db.execute("INSERT INTO edges(src_node, rel, dst_value, quote_id) "
"VALUES(?, 'member_of', 'european union', NULL)", (nid,))
db.commit()
db.close()
ks = KnowledgeStore(p) # must migrate, not crash
origins = ks.set_origins() # the call that crashed on the old schema
# NULL-origin sets are NOT servable by the gate: without an origin question
# we cannot verify the set answers THIS question — safe re-gather instead.
# (The rows stay in the KG for non-enum probes; only the gate skips them.)
assert origins == [], f"NULL-origin sets must not be gate-servable: {origins}"
# and the store still answers normal probes after migration:
ks.close()
print("ok test_migration_old_store")
def test_table_parser_untruncated():
from knowledge.wiki import parse_tables
# vertical layout, 5 rows, header row -> total_rows MUST be 5, no truncation
@@ -197,6 +238,7 @@ if __name__ == "__main__":
test_chunker_sections()
test_containment()
test_store_four_cases()
test_migration_old_store()
test_list_ingest_aggregate()
test_table_parser_untruncated()
print(f"\n{test_table_parser_untruncated.__name__} etc — 5 knowledge tests passed")
print(f"\n{test_table_parser_untruncated.__name__} etc — 6 knowledge tests passed")