Files
david 65fdcbc789
build / test (push) Successful in 3m21s
build / image (push) Successful in 35s
feat: per-source sync status (#54)
Ingest sweeps reported outcome only to the log: a failing sweep, or one that
silently no-ops because its env config is missing, was invisible from the
CLI/MCP surface. Extend the (previously dead) sources registry table with
per-source sweep state and expose it.

- db: RecordSync upserts one outcome per sweep (status ok/error/unconfigured,
  timestamps kept monotonic, last success survives a later failure); the
  columns are declared in schema.sql and added by migration for existing DBs.
- db: SourceSync reads sweep state joined with derived per-source counts from
  observations, and computes a verdict: ok, stale (>2.5x interval since last
  success), error, unconfigured, never.
- watch: record every sweep of browser-history, gitea, git, opencode-session,
  karakeep, bookorbit and brevier — including failures and not-configured
  no-ops, which are exactly the invisible cases.
- cmd: knox sync-status [--json] and the knox_sync_status MCP tool.

Motivating case: this node's watch container has no KARAKEEP_URL/API_KEY, so
its karakeep ingester was a permanent silent no-op.
2026-11-16 23:41:12 -08:00

265 lines
12 KiB
SQL

-- IMMUTABLE LOG: every observation is append-only, never mutated
CREATE TABLE IF NOT EXISTS observations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
fingerprint TEXT NOT NULL,
source_id TEXT NOT NULL,
source_path TEXT,
project TEXT,
content_type TEXT,
title TEXT,
summary TEXT,
collected_at TEXT NOT NULL DEFAULT (datetime('now')),
created_at TEXT,
line_start INTEGER,
line_end INTEGER,
confidence REAL DEFAULT 0.5,
ingester_version TEXT DEFAULT '1',
trigger TEXT,
provenance TEXT DEFAULT '{}',
content_hash TEXT DEFAULT '',
content TEXT DEFAULT '',
node_id TEXT NOT NULL DEFAULT '',
hcl INTEGER,
received_at TEXT
);
CREATE INDEX IF NOT EXISTS idx_obs_fingerprint ON observations(fingerprint);
CREATE INDEX IF NOT EXISTS idx_obs_collected ON observations(collected_at);
CREATE INDEX IF NOT EXISTS idx_obs_created ON observations(created_at);
CREATE INDEX IF NOT EXISTS idx_obs_source ON observations(source_id, source_path);
CREATE INDEX IF NOT EXISTS idx_obs_project ON observations(project);
CREATE INDEX IF NOT EXISTS idx_obs_trigger ON observations(trigger);
-- MATERIALIZED CACHE: current state, reconstructable from observations
CREATE TABLE IF NOT EXISTS entries (
fingerprint TEXT PRIMARY KEY,
source_id TEXT NOT NULL,
source_path TEXT,
project TEXT,
content_type TEXT,
title TEXT,
summary TEXT,
content TEXT DEFAULT '',
first_seen TEXT,
last_seen TEXT,
ref_count INTEGER DEFAULT 1,
last_confidence REAL DEFAULT 0.5,
-- Added by the ALTER TABLE migrations in db.go; declared here so the file
-- is the single description of the current schema (sqlc reads this file).
created_at TEXT DEFAULT ''
);
CREATE INDEX IF NOT EXISTS idx_entries_project ON entries(project);
CREATE INDEX IF NOT EXISTS idx_entries_source ON entries(source_id);
CREATE INDEX IF NOT EXISTS idx_entries_seen ON entries(last_seen);
-- FULL-TEXT SEARCH: external-content FTS5 over entries, kept in sync by
-- triggers. BM25-ranked; entries_vocab exposes the term list for typo fallback.
CREATE VIRTUAL TABLE IF NOT EXISTS entries_fts USING fts5(title, project, summary, content, content='entries', content_rowid='rowid');
CREATE VIRTUAL TABLE IF NOT EXISTS entries_vocab USING fts5vocab(entries_fts, 'row');
CREATE TRIGGER IF NOT EXISTS entries_ai AFTER INSERT ON entries BEGIN
INSERT INTO entries_fts(rowid, title, project, summary, content) VALUES (new.rowid, new.title, new.project, new.summary, new.content);
END;
CREATE TRIGGER IF NOT EXISTS entries_ad AFTER DELETE ON entries BEGIN
INSERT INTO entries_fts(entries_fts, rowid, title, project, summary, content) VALUES ('delete', old.rowid, old.title, old.project, old.summary, old.content);
END;
CREATE TRIGGER IF NOT EXISTS entries_au AFTER UPDATE ON entries BEGIN
INSERT INTO entries_fts(entries_fts, rowid, title, project, summary, content) VALUES ('delete', old.rowid, old.title, old.project, old.summary, old.content);
INSERT INTO entries_fts(rowid, title, project, summary, content) VALUES (new.rowid, new.title, new.project, new.summary, new.content);
END;
-- PROVENANCE LINKS: multi-hop lineage between entries
CREATE TABLE IF NOT EXISTS provenance_links (
id INTEGER PRIMARY KEY AUTOINCREMENT,
descendant_fp TEXT NOT NULL,
ancestor_fp TEXT NOT NULL,
hop_distance INTEGER NOT NULL,
relation TEXT NOT NULL,
created_at TEXT DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_pl_descendant ON provenance_links(descendant_fp);
CREATE INDEX IF NOT EXISTS idx_pl_ancestor ON provenance_links(ancestor_fp);
-- REFLECTION STATE: per-session tracking
CREATE TABLE IF NOT EXISTS sessions (
session_id TEXT PRIMARY KEY,
project TEXT,
title TEXT,
status TEXT DEFAULT 'active',
agent_count INTEGER DEFAULT 0,
started_at TEXT,
ended_at TEXT,
last_seen TEXT DEFAULT (datetime('now')),
indexed INTEGER DEFAULT 0
);
-- SOURCE REGISTRY: also records per-source sync health. The watcher upserts
-- one row per sweep via RecordSync — including failures and "not configured" —
-- so ingest problems stay visible from the shared DB after restarts (and from
-- the MCP process, which shares the file but not memory). Empty status means
-- "no sweep recorded yet".
CREATE TABLE IF NOT EXISTS sources (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
enabled INTEGER DEFAULT 1,
last_event TEXT,
created_at TEXT DEFAULT (datetime('now')),
status TEXT DEFAULT '',
last_attempt_at TEXT DEFAULT '',
last_success_at TEXT DEFAULT '',
last_error_at TEXT DEFAULT '',
last_error TEXT DEFAULT '',
last_counts TEXT DEFAULT '',
interval_seconds INTEGER DEFAULT 0
);
INSERT OR IGNORE INTO sources (id, name) VALUES ('opencode-session', 'Opencode Session Diffs');
INSERT OR IGNORE INTO sources (id, name) VALUES ('opencode-log', 'Opencode Log Files');
INSERT OR IGNORE INTO sources (id, name) VALUES ('git', 'Git History');
INSERT OR IGNORE INTO sources (id, name) VALUES ('filesystem', 'File System Watcher');
INSERT OR IGNORE INTO sources (id, name) VALUES ('browser-history', 'Browser History');
INSERT OR IGNORE INTO sources (id, name) VALUES ('gitea', 'Gitea Activity');
INSERT OR IGNORE INTO sources (id, name) VALUES ('karakeep', 'Karakeep Bookmarks');
INSERT OR IGNORE INTO sources (id, name) VALUES ('bookorbit', 'BookOrbit Highlights');
INSERT OR IGNORE INTO sources (id, name) VALUES ('brevier', 'Brevier Reading Archive');
INSERT OR IGNORE INTO sources (id, name) VALUES ('obsidian', 'Obsidian Vault');
INSERT OR IGNORE INTO sources (id, name) VALUES ('skills-catalog', 'Skills Catalog');
-- KNOWLEDGE THREADS: why observations exist, what questions they answer
CREATE TABLE IF NOT EXISTS threads (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
motivation TEXT,
status TEXT DEFAULT 'active',
priority TEXT DEFAULT 'medium',
created_at TEXT DEFAULT (datetime('now')),
updated_at TEXT DEFAULT (datetime('now')),
resolved_at TEXT,
tags TEXT DEFAULT '[]',
provenance TEXT DEFAULT '{}',
cluster_key TEXT,
retained INTEGER DEFAULT 0,
summary TEXT DEFAULT '',
summarized_at TEXT DEFAULT '',
-- Replication identity and last-write clock. uid is opaque and node-
-- generated; the integer id is local and cannot address a thread across
-- nodes. superseded_by points at the uid that absorbed this thread.
uid TEXT,
hcl INTEGER,
superseded_by TEXT
);
-- SETTINGS: key-value store for runtime configuration
CREATE TABLE IF NOT EXISTS settings (
key TEXT PRIMARY KEY,
value TEXT
);
-- PEERS: other knox nodes known to this one (gossip ring)
-- CLAIM LEDGER: numeric assertions the agent has made, with how often they were
-- restated versus actually re-verified. Standard SM-2 expands an interval on
-- successful recall; here the sign is inverted, because repeating a figure
-- without re-checking it is the failure, not the achievement.
CREATE TABLE IF NOT EXISTS claims (
fingerprint TEXT PRIMARY KEY, -- hash of unit + value
assertion TEXT NOT NULL, -- as last written, e.g. "6,884 lines"
unit TEXT NOT NULL,
value INTEGER NOT NULL,
status TEXT NOT NULL, -- sourced | stale | unsupported
reuse_count INTEGER NOT NULL DEFAULT 0, -- restatements since last verification
verify_count INTEGER NOT NULL DEFAULT 0, -- times a tool backed it
ef REAL NOT NULL DEFAULT 2.5,
interval_days REAL NOT NULL DEFAULT 1,
due_at TEXT, -- date re-verification is due
first_seen TEXT DEFAULT (datetime('now')),
last_seen TEXT DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_claims_due ON claims(due_at);
CREATE TABLE IF NOT EXISTS peers (
peer_id TEXT PRIMARY KEY, -- remote node_id
addr TEXT, -- e.g. http://192.168.1.20:8931
name TEXT,
last_handshake TEXT,
cursor INTEGER DEFAULT 0, -- remote's max_hcl we've seen (knowledge)
created_at TEXT DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_threads_status ON threads(status);
CREATE TABLE IF NOT EXISTS thread_observations (
thread_id INTEGER NOT NULL REFERENCES threads(id) ON DELETE CASCADE,
observation_id INTEGER NOT NULL REFERENCES observations(id) ON DELETE CASCADE,
relevance TEXT,
added_at TEXT DEFAULT (datetime('now')),
PRIMARY KEY (thread_id, observation_id)
);
CREATE TABLE IF NOT EXISTS thread_notes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
thread_id INTEGER NOT NULL REFERENCES threads(id) ON DELETE CASCADE,
note TEXT NOT NULL,
created_at TEXT DEFAULT (datetime('now'))
);
-- TOPICS: TF-IDF clustered groups of related observations
CREATE TABLE IF NOT EXISTS topics (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
keywords TEXT,
score REAL DEFAULT 0,
created_at TEXT DEFAULT (datetime('now'))
);
-- SNIPPETS: code lifted from sessions that demonstrably worked. Two-phase by
-- design: the daemon deterministically extracts *candidates* (no LLM) using
-- conservative success signals (tool completed, bash exit 0, not later
-- overwritten), and an agent curates them via the MCP encode/reject tools.
-- status: candidate | encoded | rejected. superseded_by hides dead ends.
CREATE TABLE IF NOT EXISTS snippets (
fingerprint TEXT PRIMARY KEY, -- content hash of code+language
language TEXT DEFAULT '',
code TEXT NOT NULL,
title TEXT DEFAULT '',
description TEXT DEFAULT '',
tags TEXT DEFAULT '[]',
status TEXT DEFAULT 'candidate',
source_session TEXT DEFAULT '',
source_path TEXT DEFAULT '', -- file the snippet touched (edit/write)
source_entry TEXT DEFAULT '', -- session entry fingerprint
project TEXT DEFAULT '',
tool TEXT DEFAULT '', -- edit | write | bash
model TEXT DEFAULT '',
patch_hash TEXT DEFAULT '',
evidence TEXT DEFAULT '{}', -- JSON: why it counts as "worked"
superseded_by TEXT DEFAULT '', -- later full rewrite of the same file
skill_name TEXT DEFAULT '', -- set when encoded into a ~/.skills entry
reject_reason TEXT DEFAULT '',
junk INTEGER DEFAULT 0, -- heuristic: imports/directives/single-line/empty-body stub
kind TEXT DEFAULT 'code', -- code | technique (how you explored/troubleshot)
discovered_at TEXT DEFAULT (datetime('now')),
reviewed_at TEXT DEFAULT '',
encoded_at TEXT DEFAULT ''
);
CREATE INDEX IF NOT EXISTS idx_snips_status ON snippets(status);
CREATE INDEX IF NOT EXISTS idx_snips_lang ON snippets(language);
CREATE INDEX IF NOT EXISTS idx_snips_project ON snippets(project);
CREATE INDEX IF NOT EXISTS idx_snips_session ON snippets(source_session);
-- FULL-TEXT SEARCH over snippets, kept in sync by triggers (same pattern as entries_fts).
CREATE VIRTUAL TABLE IF NOT EXISTS snippets_fts USING fts5(title, description, code, language, tags, content='snippets', content_rowid='rowid');
CREATE TRIGGER IF NOT EXISTS snippets_ai AFTER INSERT ON snippets BEGIN
INSERT INTO snippets_fts(rowid, title, description, code, language, tags) VALUES (new.rowid, new.title, new.description, new.code, new.language, new.tags);
END;
CREATE TRIGGER IF NOT EXISTS snippets_ad AFTER DELETE ON snippets BEGIN
INSERT INTO snippets_fts(snippets_fts, rowid, title, description, code, language, tags) VALUES ('delete', old.rowid, old.title, old.description, old.code, old.language, old.tags);
END;
CREATE TRIGGER IF NOT EXISTS snippets_au AFTER UPDATE ON snippets BEGIN
INSERT INTO snippets_fts(snippets_fts, rowid, title, description, code, language, tags) VALUES ('delete', old.rowid, old.title, old.description, old.code, old.language, old.tags);
INSERT INTO snippets_fts(rowid, title, description, code, language, tags) VALUES (new.rowid, new.title, new.description, new.code, new.language, new.tags);
END;