Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

feat(wiki): journal basis/salience data model (migration 0009, five-layer parity)

Personal Jarvis Maintainerdecab47b954f
diff --git a/jarvis/memory/migrations/0009_wiki_candidate_basis.sql b/jarvis/memory/migrations/0009_wiki_candidate_basis.sql
new file mode 100644
index 0000000000..6f0791e089
--- /dev/null
+++ b/jarvis/memory/migrations/0009_wiki_candidate_basis.sql
@@ -0,0 +1,25 @@
+-- Persist the evidence basis and personal-salience score per candidate fact.
+--
+-- basis records HOW a fact is grounded: "explicit" (the user asserted it),
+-- "behavioral" (first-person lived-experience report without a literal
+-- preference assertion), or "inferred" (reserved for a future cross-session
+-- reflection pass; nothing writes it yet). salience scores how central the
+-- fact is to the user's own life (1 = trivia .. 5 = core identity) and drives
+-- the configurable Stage-1 floor.
+
+-- Keep both values in their own one-to-one table. Unlike ALTER TABLE ADD
+-- COLUMN, CREATE TABLE IF NOT EXISTS is safe when CandidateJournal initialized
+-- its standalone schema before RecallStore later replays numbered migrations.
+
+BEGIN;
+
+CREATE TABLE IF NOT EXISTS wiki_candidate_basis (
+ candidate_id INTEGER PRIMARY KEY
+ REFERENCES wiki_candidate_journal(id) ON DELETE CASCADE,
+ basis TEXT NOT NULL DEFAULT 'explicit'
+ CHECK (basis IN ('explicit', 'behavioral', 'inferred')),
+ salience INTEGER NOT NULL DEFAULT 3
+ CHECK (salience BETWEEN 1 AND 5)
+);
+
+COMMIT;
diff --git a/jarvis/memory/wiki/constants.py b/jarvis/memory/wiki/constants.py
index 0333869e19..2fad367a25 100644
--- a/jarvis/memory/wiki/constants.py
+++ b/jarvis/memory/wiki/constants.py
@@ -23,14 +23,26 @@ CandidateStatus = Literal["pending", "consolidated", "rejected", "skipped"]
CURATOR_DECISIONS: tuple[str, ...] = ("add", "update", "noop", "invalidate")
CuratorDecision = Literal["add", "update", "noop", "invalidate"]
+# Evidence basis of one candidate fact ("explicit" = user asserted it,
+# "behavioral" = first-person lived-experience report, "inferred" = reserved
+# for a future cross-session reflection pass — nothing emits it yet). SQL
+# CHECK lives in 0009_wiki_candidate_basis.sql. Like the decisions, these
+# strings never render in the UI, so there is no TypeScript layer; add the TS
+# mirror + extend the parity test before ever surfacing them (BUG-008).
+FACT_BASES: tuple[str, ...] = ("explicit", "behavioral", "inferred")
+FactBasis = Literal["explicit", "behavioral", "inferred"]
+
# Runtime drift assertions (mirror the jarvis/memory/constants.py pattern):
# importing this module with a drifted tuple/Literal pair fails immediately.
assert set(CANDIDATE_STATUSES) == set(CandidateStatus.__args__) # type: ignore[attr-defined]
assert set(CURATOR_DECISIONS) == set(CuratorDecision.__args__) # type: ignore[attr-defined]
+assert set(FACT_BASES) == set(FactBasis.__args__) # type: ignore[attr-defined]
__all__ = [
"CANDIDATE_STATUSES",
"CURATOR_DECISIONS",
+ "FACT_BASES",
"CandidateStatus",
"CuratorDecision",
+ "FactBasis",
]
diff --git a/jarvis/memory/wiki/journal.py b/jarvis/memory/wiki/journal.py
index 93a774ff39..8728a42741 100644
--- a/jarvis/memory/wiki/journal.py
+++ b/jarvis/memory/wiki/journal.py
@@ -9,7 +9,7 @@ file (cross-platform, no new dependency — CLOUD.md doctrine) and survives
restarts, so a crash between extraction and consolidation never loses a
fact.
-Schema source of truth: migrations ``0005`` through ``0008`` under
+Schema source of truth: migrations ``0005`` through ``0009`` under
``jarvis/memory/migrations`` (applied by ``run_migrations`` for
RecallStore-opened databases, and executed idempotently here for standalone
opens — the same pattern as ``fts_index``).
@@ -35,7 +35,7 @@ from typing import Literal
from jarvis.core.redact import safe_preview
-from .constants import CandidateStatus, CuratorDecision
+from .constants import FACT_BASES, CandidateStatus, CuratorDecision
from .secret_guard import contains_secret
log = logging.getLogger(__name__)
@@ -46,6 +46,7 @@ _MIGRATION_FILES = (
_MIGRATIONS_DIR / "0006_wiki_extraction_audit.sql",
_MIGRATIONS_DIR / "0007_wiki_candidate_capture.sql",
_MIGRATIONS_DIR / "0008_wiki_candidate_evidence_excerpt.sql",
+ _MIGRATIONS_DIR / "0009_wiki_candidate_basis.sql",
)
CaptureStatus = Literal["started", "filtered", "empty", "candidates", "failed"]
@@ -85,6 +86,10 @@ _KNOWN_ERROR_CODES = frozenset(
)
+_DEFAULT_BASIS = "explicit"
+_DEFAULT_SALIENCE = 3
+
+
@dataclass(frozen=True, slots=True)
class CandidateFact:
"""One atomic fact proposed by the Stage-1 extractor."""
@@ -94,6 +99,8 @@ class CandidateFact:
subjects: tuple[str, ...] = ()
evidence_turn_id: str = ""
evidence_excerpt: str = ""
+ basis: str = _DEFAULT_BASIS
+ salience: int = _DEFAULT_SALIENCE
@dataclass(frozen=True, slots=True)
@@ -112,6 +119,8 @@ class JournalRow:
session_id: str
review_key: str
status: CandidateStatus
+ basis: str = _DEFAULT_BASIS
+ salience: int = _DEFAULT_SALIENCE
class CandidateJournal:
@@ -325,6 +334,11 @@ class CandidateJournal:
),
),
)
+ conn.execute(
+ "INSERT INTO wiki_candidate_basis "
+ "(candidate_id, basis, salience) VALUES (?, ?, ?)",
+ (candidate_id, fact.basis, fact.salience),
+ )
if review_key is not None:
conn.execute(
"INSERT INTO wiki_candidate_capture (candidate_id, review_key) "
@@ -363,13 +377,15 @@ class CandidateJournal:
"j.kind, j.subjects, COALESCE(e.evidence_turn_id, ''), "
"COALESCE(x.evidence_excerpt, ''), "
"COALESCE(a.session_id, ''), COALESCE(c.review_key, ''), "
- "j.status "
+ "j.status, "
+ "COALESCE(b.basis, 'explicit'), COALESCE(b.salience, 3) "
"FROM wiki_candidate_journal AS j "
"LEFT JOIN wiki_candidate_evidence AS e ON e.candidate_id = j.id "
"LEFT JOIN wiki_candidate_evidence_excerpt AS x "
"ON x.candidate_id = j.id "
"LEFT JOIN wiki_candidate_capture AS c ON c.candidate_id = j.id "
"LEFT JOIN wiki_extraction_audit AS a ON a.review_key = c.review_key "
+ "LEFT JOIN wiki_candidate_basis AS b ON b.candidate_id = j.id "
)
if keys is None:
cur = conn.execute(
@@ -406,6 +422,8 @@ class CandidateJournal:
session_id=row[9],
review_key=row[10],
status=row[11],
+ basis=row[12],
+ salience=int(row[13]),
)
)
return out
@@ -740,6 +758,8 @@ def _normalise_candidates(
candidate.evidence_excerpt,
max_chars=_MAX_EVIDENCE_CHARS,
).strip(),
+ basis=_safe_basis(candidate.basis),
+ salience=_clamp_salience(candidate.salience),
)
)
if rejected_secret:
@@ -750,6 +770,19 @@ def _normalise_candidates(
return tuple(accepted)
+def _safe_basis(value: object) -> str:
+ basis = str(value or "").strip().lower()
+ return basis if basis in FACT_BASES else _DEFAULT_BASIS
+
+
+def _clamp_salience(value: object) -> int:
+ try:
+ salience = int(value) # type: ignore[arg-type]
+ except (TypeError, ValueError):
+ return _DEFAULT_SALIENCE
+ return max(1, min(5, salience))
+
+
def normalise_subjects(subjects: Sequence[object]) -> tuple[str, ...]:
"""Return bounded kebab-case slugs safe for vault-neighbour lookup."""
accepted: list[str] = []
diff --git a/tests/unit/memory/wiki/test_candidate_journal.py b/tests/unit/memory/wiki/test_candidate_journal.py
index cd0cf17cab..78c595fe37 100644
--- a/tests/unit/memory/wiki/test_candidate_journal.py
+++ b/tests/unit/memory/wiki/test_candidate_journal.py
@@ -53,9 +53,38 @@ def test_append_then_pending_roundtrip(journal: CandidateJournal) -> None:
assert rows[0].session_id == ""
assert rows[1].evidence_turn_id == ""
assert rows[0].source_label == "voice-fact:123"
+ assert rows[0].basis == "explicit"
+ assert rows[0].salience == 3
assert journal.backlog_count() == 2
+def test_basis_and_salience_roundtrip_and_normalisation(
+ journal: CandidateJournal,
+) -> None:
+ journal.append(
+ [
+ CandidateFact(
+ fact="The user plays golf actively with friends.",
+ kind="activity",
+ subjects=("ruben", "golf"),
+ basis="behavioral",
+ salience=4,
+ ),
+ CandidateFact(
+ fact="The user drinks water.",
+ basis="made-up-basis",
+ salience=99,
+ ),
+ ],
+ source_label="s",
+ turn_hash="basis-roundtrip",
+ )
+ rows = journal.pending()
+ assert (rows[0].basis, rows[0].salience) == ("behavioral", 4)
+ # Unknown basis coerces to explicit; salience clamps into 1..5.
+ assert (rows[1].basis, rows[1].salience) == ("explicit", 5)
+
+
def test_append_blocks_secret_shaped_candidate_defense_in_depth(
journal: CandidateJournal,
) -> None:
@@ -107,7 +136,7 @@ def test_migration_0008_adds_grounding_excerpt_store_to_existing_database(
)
conn.execute("PRAGMA user_version = 7")
- assert run_migrations_sync(conn) == 8
+ assert run_migrations_sync(conn) == 9
columns = {
row[1]
@@ -120,6 +149,43 @@ def test_migration_0008_adds_grounding_excerpt_store_to_existing_database(
conn.close()
+def test_migration_0009_adds_basis_store_to_existing_database(
+ tmp_path: Path,
+) -> None:
+ db = tmp_path / "legacy-basis.db"
+ conn = sqlite3.connect(db)
+ try:
+ conn.execute("PRAGMA user_version = 8")
+
+ assert run_migrations_sync(conn) == 9
+
+ columns = {
+ row[1]
+ for row in conn.execute(
+ "PRAGMA table_info(wiki_candidate_basis)"
+ ).fetchall()
+ }
+ assert {"candidate_id", "basis", "salience"} <= columns
+ finally:
+ conn.close()
+
+
+def test_legacy_rows_without_basis_read_back_as_explicit(tmp_path: Path) -> None:
+ """Rows journaled before migration 0009 default to explicit/3 on read."""
+ db = tmp_path / "legacy-rows.db"
+ journal = CandidateJournal(db)
+ try:
+ journal.append(_facts()[:1], source_label="s", turn_hash="h")
+ journal._conn.execute( # noqa: SLF001 - simulate a pre-0009 row
+ "DELETE FROM wiki_candidate_basis"
+ )
+ journal._conn.commit() # noqa: SLF001
+ row = journal.pending()[0]
+ assert (row.basis, row.salience) == ("explicit", 3)
+ finally:
+ journal.close()
+
+
def test_standalone_journal_then_numbered_migrations_is_idempotent(
tmp_path: Path,
) -> None:
@@ -140,8 +206,8 @@ def test_standalone_journal_then_numbered_migrations_is_idempotent(
)
conn.executescript(schema.read_text(encoding="utf-8"))
- assert run_migrations_sync(conn) == 8
- assert run_migrations_sync(conn) == 8
+ assert run_migrations_sync(conn) == 9
+ assert run_migrations_sync(conn) == 9
finally:
conn.close()
@@ -203,7 +269,7 @@ def test_development_column_excerpt_is_copied_without_migration_collision(
/ "schema.sql"
)
conn.executescript(schema.read_text(encoding="utf-8"))
- assert run_migrations_sync(conn) == 8
+ assert run_migrations_sync(conn) == 9
finally:
conn.close()
@@ -331,6 +397,14 @@ def test_sql_check_rejects_invalid_status(journal: CandidateJournal) -> None:
conn.execute(
"UPDATE wiki_candidate_journal SET decision = 'delete'"
)
+ with pytest.raises(sqlite3.IntegrityError):
+ conn.execute(
+ "UPDATE wiki_candidate_basis SET basis = 'vibes'"
+ )
+ with pytest.raises(sqlite3.IntegrityError):
+ conn.execute(
+ "UPDATE wiki_candidate_basis SET salience = 6"
+ )
def test_capture_claim_is_atomic_across_connections(tmp_path: Path) -> None:
diff --git a/tests/unit/memory/wiki/test_curator_decision_parity.py b/tests/unit/memory/wiki/test_curator_decision_parity.py
index cdd896eb9e..db3797f852 100644
--- a/tests/unit/memory/wiki/test_curator_decision_parity.py
+++ b/tests/unit/memory/wiki/test_curator_decision_parity.py
@@ -13,14 +13,17 @@ from pathlib import Path
from jarvis.memory.wiki.constants import (
CANDIDATE_STATUSES,
CURATOR_DECISIONS,
+ FACT_BASES,
CandidateStatus,
CuratorDecision,
+ FactBasis,
)
-_MIGRATION = (
- Path(__file__).resolve().parents[4]
- / "jarvis" / "memory" / "migrations" / "0005_wiki_candidate_journal.sql"
+_MIGRATIONS_DIR = (
+ Path(__file__).resolve().parents[4] / "jarvis" / "memory" / "migrations"
)
+_MIGRATION = _MIGRATIONS_DIR / "0005_wiki_candidate_journal.sql"
+_BASIS_MIGRATION = _MIGRATIONS_DIR / "0009_wiki_candidate_basis.sql"
def _check_values(sql: str, column: str) -> set[str]:
@@ -37,6 +40,7 @@ def _check_values(sql: str, column: str) -> set[str]:
def test_python_tuple_matches_literal() -> None:
assert set(CANDIDATE_STATUSES) == set(CandidateStatus.__args__) # type: ignore[attr-defined]
assert set(CURATOR_DECISIONS) == set(CuratorDecision.__args__) # type: ignore[attr-defined]
+ assert set(FACT_BASES) == set(FactBasis.__args__) # type: ignore[attr-defined]
def test_sql_check_matches_python_tuples() -> None:
@@ -45,6 +49,11 @@ def test_sql_check_matches_python_tuples() -> None:
assert _check_values(sql, "decision") == set(CURATOR_DECISIONS)
+def test_basis_sql_check_matches_python_tuple() -> None:
+ sql = _BASIS_MIGRATION.read_text(encoding="utf-8")
+ assert _check_values(sql, "basis") == set(FACT_BASES)
+
+
def test_telemetry_counter_names_follow_decisions() -> None:
"""Counter naming contract: wiki_consolidator_<decision> for every decision
except noop-free shortcuts — all four decisions get a counter (B8)."""