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.
This commit is contained in:
@@ -0,0 +1,170 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// RecordSync upserts sweep state and SourceSync joins it with derived
|
||||
// per-source entry counts, computing a verdict the caller can act on.
|
||||
func TestRecordSyncAndStatus(t *testing.T) {
|
||||
k := newTestDB(t)
|
||||
|
||||
// Two entries for karakeep so the derived count shows up.
|
||||
kpObs := func(fp, title string) ObservationRecord {
|
||||
o := obs(fp, title, "body")
|
||||
o.SourceID = "karakeep"
|
||||
return o
|
||||
}
|
||||
if _, _, err := k.RecordObservation(kpObs("kp1", "Karakeep A")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, _, err := k.RecordObservation(kpObs("kp2", "Karakeep B")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := k.RecordSync(SyncRecord{
|
||||
SourceID: "karakeep", Status: SyncOK, Interval: 30 * time.Minute,
|
||||
New: 2, Unchanged: 5, Scanned: 7,
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := k.RecordSync(SyncRecord{
|
||||
SourceID: "gitea", Status: SyncError, Interval: 10 * time.Minute, Err: "boom",
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := k.RecordSync(SyncRecord{
|
||||
SourceID: "bookorbit", Status: SyncUnconfigured, Interval: 30 * time.Minute,
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rows, err := k.SourceSync()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
byID := map[string]SourceStatus{}
|
||||
for _, r := range rows {
|
||||
byID[r.ID] = r
|
||||
}
|
||||
|
||||
kp := byID["karakeep"]
|
||||
if kp.Status != SyncOK || !kp.Healthy {
|
||||
t.Fatalf("karakeep status = %q healthy = %v, want ok/true", kp.Status, kp.Healthy)
|
||||
}
|
||||
if kp.Entries != 2 {
|
||||
t.Fatalf("karakeep entries = %d, want 2 (derived from observations)", kp.Entries)
|
||||
}
|
||||
if kp.LastCounts["new"] != 2 || kp.LastCounts["scanned"] != 7 {
|
||||
t.Fatalf("karakeep counts = %v, want new=2 scanned=7", kp.LastCounts)
|
||||
}
|
||||
if kp.LastSuccess == "" || kp.LastAttempt == "" {
|
||||
t.Fatalf("karakeep timestamps missing: success=%q attempt=%q", kp.LastSuccess, kp.LastAttempt)
|
||||
}
|
||||
|
||||
gi := byID["gitea"]
|
||||
if gi.Status != SyncError || gi.Healthy {
|
||||
t.Fatalf("gitea status = %q healthy = %v, want error/false", gi.Status, gi.Healthy)
|
||||
}
|
||||
if gi.LastErrorDetail != "boom" {
|
||||
t.Fatalf("gitea error detail = %q, want boom", gi.LastErrorDetail)
|
||||
}
|
||||
|
||||
bo := byID["bookorbit"]
|
||||
if bo.Status != SyncUnconfigured || bo.Healthy {
|
||||
t.Fatalf("bookorbit status = %q healthy = %v, want unconfigured/false", bo.Status, bo.Healthy)
|
||||
}
|
||||
|
||||
// A seeded source that never swept and has no entries: never + unhealthy.
|
||||
fs := byID["filesystem"]
|
||||
if fs.Status != "never" || fs.Healthy {
|
||||
t.Fatalf("filesystem status = %q healthy = %v, want never/false", fs.Status, fs.Healthy)
|
||||
}
|
||||
|
||||
// A seeded source with entries but no sweeps (event-driven): never, healthy.
|
||||
if _, _, err := k.RecordObservation(ObservationRecord{
|
||||
Fingerprint: "ob1", SourceID: "obsidian", Title: "note", Confidence: 0.5, IngesterVersion: "t",
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rows, err = k.SourceSync()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, r := range rows {
|
||||
if r.ID == "obsidian" && (r.Status != "never" || !r.Healthy) {
|
||||
t.Fatalf("obsidian status = %q healthy = %v, want never/true", r.Status, r.Healthy)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A failed sweep must not clear the last success, and an overdue success is
|
||||
// reported stale rather than ok.
|
||||
func TestSyncKeepsLastSuccessAndStaleness(t *testing.T) {
|
||||
k := newTestDB(t)
|
||||
|
||||
if err := k.RecordSync(SyncRecord{SourceID: "karakeep", Status: SyncOK, Interval: 30 * time.Minute, New: 1}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := k.RecordSync(SyncRecord{SourceID: "karakeep", Status: SyncError, Interval: 30 * time.Minute, Err: "timeout"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// While failing: the earlier success survives, and the error is current.
|
||||
rows, err := k.SourceSync()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, r := range rows {
|
||||
if r.ID == "karakeep" {
|
||||
if r.LastSuccess == "" {
|
||||
t.Fatal("failed sweep cleared last_success_at, want it kept")
|
||||
}
|
||||
if r.LastErrorDetail != "timeout" {
|
||||
t.Fatalf("error detail = %q, want timeout", r.LastErrorDetail)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Recover, then backdate the success so it is overdue: the verdict must
|
||||
// become stale rather than stay ok.
|
||||
if err := k.RecordSync(SyncRecord{SourceID: "karakeep", Status: SyncOK, Interval: 30 * time.Minute}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rows, err = k.SourceSync()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, r := range rows {
|
||||
if r.ID == "karakeep" {
|
||||
if r.Status != SyncOK || !r.Healthy {
|
||||
t.Fatalf("karakeep status = %q healthy = %v after recovery, want ok/true", r.Status, r.Healthy)
|
||||
}
|
||||
if r.LastErrorDetail != "" {
|
||||
t.Fatalf("error detail = %q after recovery, want cleared", r.LastErrorDetail)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Backdate the last success past 2.5x the interval: verdict goes stale.
|
||||
old := time.Now().UTC().Add(-3 * time.Hour).Format(time.RFC3339)
|
||||
if _, err := k.db.Exec(`UPDATE sources SET last_success_at=? WHERE id='karakeep'`, old); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rows, err = k.SourceSync()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, r := range rows {
|
||||
if r.ID == "karakeep" {
|
||||
if r.Status != "stale" || r.Healthy {
|
||||
t.Fatalf("karakeep status = %q healthy = %v, want stale/false", r.Status, r.Healthy)
|
||||
}
|
||||
if !strings.Contains(r.Detail, "last success") {
|
||||
t.Fatalf("stale detail = %q, want it to mention last success", r.Detail)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user