feat: per-source sync status (#54)
build / test (push) Successful in 3m21s
build / image (push) Successful in 35s

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.
This commit is contained in:
2026-11-16 23:41:12 -08:00
parent 6242f21e3d
commit 65fdcbc789
8 changed files with 500 additions and 7 deletions
+53 -5
View File
@@ -33,6 +33,8 @@ const (
brevierInterval = 1 * time.Hour
healthInterval = 15 * time.Minute
snippetInterval = 10 * time.Minute
giteaInterval = 10 * time.Minute
gitInterval = 10 * time.Minute
)
// fileState is a cheap identity for a file: an unchanged size+mtime means the
@@ -227,8 +229,8 @@ func (w *Watcher) Start() error {
debounce := newFileDebouncer()
browserTicker := time.NewTicker(browserInterval)
giteaTicker := time.NewTicker(10 * time.Minute)
gitTicker := time.NewTicker(10 * time.Minute)
giteaTicker := time.NewTicker(giteaInterval)
gitTicker := time.NewTicker(gitInterval)
opencodeTicker := time.NewTicker(opencodeInterval)
karakeepTicker := time.NewTicker(karakeepInterval)
bookorbitTicker := time.NewTicker(bookorbitInterval)
@@ -572,12 +574,28 @@ func (w *Watcher) recordResult(result *ingest.IngestResult, trigger string) {
}
}
// recordSync persists a sweep outcome for `knox sync-status` / knox_sync_status.
// Recording must happen for failures and no-op sweeps too — those are exactly
// the outcomes that are otherwise invisible. A recording failure is logged but
// never fails the sweep itself.
func (w *Watcher) recordSync(source string, interval time.Duration, status string, newN, upN, unchN, scanned int, err error) {
msg := ""
if err != nil {
msg = err.Error()
}
rec := db.SyncRecord{SourceID: source, Status: status, Interval: interval, New: newN, Updated: upN, Unchanged: unchN, Scanned: scanned, Err: msg}
if err := w.knoxDB.RecordSync(rec); err != nil {
log.Printf("[knox] sync status record error (%s): %v", source, err)
}
}
func (w *Watcher) ingestBrowserHistory() {
log.Printf("[knox] periodic browser history ingest...")
ing := ingest.NewBrowserHistoryIngester()
results, err := ing.IngestAll()
if err != nil {
log.Printf("[knox] browser ingest error: %v", err)
w.recordSync("browser-history", browserInterval, db.SyncError, 0, 0, 0, 0, err)
return
}
@@ -613,6 +631,7 @@ func (w *Watcher) ingestBrowserHistory() {
if newCount > 0 || skipped > 0 {
log.Printf("[knox] browser history: %d new, %d unchanged", newCount, skipped)
}
w.recordSync("browser-history", browserInterval, db.SyncOK, newCount, 0, skipped, len(results), nil)
}
func (w *Watcher) ingestGitea() {
@@ -621,6 +640,7 @@ func (w *Watcher) ingestGitea() {
results, err := ing.IngestAll()
if err != nil {
log.Printf("[knox] gitea ingest error: %v", err)
w.recordSync("gitea", giteaInterval, db.SyncError, 0, 0, 0, 0, err)
return
}
@@ -657,6 +677,7 @@ func (w *Watcher) ingestGitea() {
if newCount > 0 || skipped > 0 {
log.Printf("[knox] gitea: %d new, %d unchanged", newCount, skipped)
}
w.recordSync("gitea", giteaInterval, db.SyncOK, newCount, 0, skipped, len(results), nil)
}
func (w *Watcher) ingestGit() {
@@ -675,6 +696,7 @@ func (w *Watcher) ingestGit() {
results, err := ing.IngestAll()
if err != nil {
log.Printf("[knox] git ingest error: %v", err)
w.recordSync("git", gitInterval, db.SyncError, 0, 0, 0, 0, err)
return
}
@@ -711,26 +733,40 @@ func (w *Watcher) ingestGit() {
if newCount > 0 || skipped > 0 {
log.Printf("[knox] git: %d new, %d unchanged", newCount, skipped)
}
w.recordSync("git", gitInterval, db.SyncOK, newCount, 0, skipped, len(results), nil)
}
// ingestOpenCodeDB sweeps opencode.db, indexing one entry per session. The
// sweep is full every time; RecordObservation collapses unchanged sessions, so
// no watermark is needed and repeated runs are idempotent.
func (w *Watcher) ingestOpenCodeDB() {
if _, _, _, err := IngestSessionsNow(w.knoxDB, false); err != nil {
created, changed, unchanged, err := IngestSessionsNow(w.knoxDB, false)
if err != nil {
log.Printf("[knox] opencode.db ingest error: %v", err)
w.recordSync("opencode-session", opencodeInterval, db.SyncError, 0, 0, 0, 0, err)
return
}
w.recordSync("opencode-session", opencodeInterval, db.SyncOK, created, changed, unchanged, created+changed+unchanged, nil)
}
// ingestKarakeep pulls bookmarks (and their crawled article text) from a
// self-hosted Karakeep instance configured via KARAKEEP_URL/KARAKEEP_API_KEY.
func (w *Watcher) ingestKarakeep() {
results, err := ingest.NewKarakeepIngester().IngestAll()
ing := ingest.NewKarakeepIngester()
if ing.BaseURL == "" || ing.APIKey == "" {
// Not configured: record it so the missing env is visible in
// sync-status instead of surfacing as an empty source.
w.recordSync("karakeep", karakeepInterval, db.SyncUnconfigured, 0, 0, 0, 0, nil)
return
}
results, err := ing.IngestAll()
if err != nil {
log.Printf("[knox] karakeep ingest error: %v", err)
w.recordSync("karakeep", karakeepInterval, db.SyncError, 0, 0, 0, 0, err)
return
}
if len(results) == 0 {
w.recordSync("karakeep", karakeepInterval, db.SyncOK, 0, 0, 0, 0, nil)
return
}
var newCount, skipped int
@@ -766,18 +802,26 @@ func (w *Watcher) ingestKarakeep() {
if newCount > 0 || skipped > 0 {
log.Printf("[knox] karakeep: %d new, %d unchanged", newCount, skipped)
}
w.recordSync("karakeep", karakeepInterval, db.SyncOK, newCount, 0, skipped, len(results), nil)
}
// ingestBookOrbit pulls reading highlights (one entry per book chapter) from a
// self-hosted BookOrbit instance configured via BOOKORBIT_URL,
// BOOKORBIT_USERNAME and BOOKORBIT_PASSWORD.
func (w *Watcher) ingestBookOrbit() {
results, err := ingest.NewBookOrbitIngester().IngestAll()
bo := ingest.NewBookOrbitIngester()
if bo.BaseURL == "" || bo.Username == "" || bo.Password == "" {
w.recordSync("bookorbit", bookorbitInterval, db.SyncUnconfigured, 0, 0, 0, 0, nil)
return
}
results, err := bo.IngestAll()
if err != nil {
log.Printf("[knox] bookorbit ingest error: %v", err)
w.recordSync("bookorbit", bookorbitInterval, db.SyncError, 0, 0, 0, 0, err)
return
}
if len(results) == 0 {
w.recordSync("bookorbit", bookorbitInterval, db.SyncOK, 0, 0, 0, 0, nil)
return
}
// No mtime-style pre-check here: a highlight edited in place keeps the
@@ -813,6 +857,7 @@ func (w *Watcher) ingestBookOrbit() {
if newCount > 0 || updated > 0 {
log.Printf("[knox] bookorbit: %d new, %d updated", newCount, updated)
}
w.recordSync("bookorbit", bookorbitInterval, db.SyncOK, newCount, updated, 0, len(results), nil)
}
// ingestBrevier pulls the local Brevier archive (pages that were read) and any
@@ -821,9 +866,11 @@ func (w *Watcher) ingestBrevier() {
results, err := ingest.NewBrevierIngester().IngestAll()
if err != nil {
log.Printf("[knox] brevier ingest error: %v", err)
w.recordSync("brevier", brevierInterval, db.SyncError, 0, 0, 0, 0, err)
return
}
if len(results) == 0 {
w.recordSync("brevier", brevierInterval, db.SyncOK, 0, 0, 0, 0, nil)
return
}
var newCount, updated int
@@ -856,6 +903,7 @@ func (w *Watcher) ingestBrevier() {
if newCount > 0 || updated > 0 {
log.Printf("[knox] brevier: %d new, %d updated", newCount, updated)
}
w.recordSync("brevier", brevierInterval, db.SyncOK, newCount, updated, 0, len(results), nil)
}
// checkSkillsHealth warns when the skills git repo looks broken — the skills