fix: backfill sessions indexed before content hashing (#20)
build / test (push) Has been cancelled
build / image (push) Has been cancelled

The watermark means a sweep only ever revisits sessions updated since the last
run, so sessions indexed before content hashing existed would never pick up
reasoning. Also treats a missing stored hash as 'cannot prove current' rather
than 'unchanged'.

Adds 'knox ingest sessions --all' to ignore the watermark for a one-time
backfill.
This commit is contained in:
2026-11-04 19:41:41 -08:00
parent 84f30f293b
commit 8d3851e638
3 changed files with 19 additions and 7 deletions
+6 -3
View File
@@ -13,6 +13,7 @@ import (
func NewIngestCmd(kdb *db.KnoxDB) *cobra.Command { func NewIngestCmd(kdb *db.KnoxDB) *cobra.Command {
var dirs []string var dirs []string
var sessionsFull bool
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "ingest", Use: "ingest",
Short: "One-time full sweep of all sources into the knowledge index", Short: "One-time full sweep of all sources into the knowledge index",
@@ -134,7 +135,7 @@ func NewIngestCmd(kdb *db.KnoxDB) *cobra.Command {
fmt.Printf("\nIngest complete: %d new entries, %d updated\n", totalNew, totalUpdated) fmt.Printf("\nIngest complete: %d new entries, %d updated\n", totalNew, totalUpdated)
}, },
} }
cmd.AddCommand(&cobra.Command{ sessionsCmd := &cobra.Command{
Use: "sessions", Use: "sessions",
Short: "Re-index opencode sessions only (fast; what the watcher timer does)", Short: "Re-index opencode sessions only (fast; what the watcher timer does)",
Long: `Re-index opencode sessions only. Long: `Re-index opencode sessions only.
@@ -143,14 +144,16 @@ Reads opencode.db and records just the sessions whose content changed. Used by
the OpenCode plugin to close the gap between a finished turn and the knowledge the OpenCode plugin to close the gap between a finished turn and the knowledge
being searchable, without paying for a full sweep.`, being searchable, without paying for a full sweep.`,
RunE: func(c *cobra.Command, args []string) error { RunE: func(c *cobra.Command, args []string) error {
created, changed, unchanged, err := watch.IngestSessionsNow(kdb) created, changed, unchanged, err := watch.IngestSessionsNow(kdb, sessionsFull)
if err != nil { if err != nil {
return err return err
} }
fmt.Printf("sessions: %d new, %d changed, %d unchanged\n", created, changed, unchanged) fmt.Printf("sessions: %d new, %d changed, %d unchanged\n", created, changed, unchanged)
return nil return nil
}, },
}) }
sessionsCmd.Flags().BoolVar(&sessionsFull, "all", false, "Ignore the watermark and sweep every session (one-time backfill)")
cmd.AddCommand(sessionsCmd)
cmd.Flags().StringSliceVarP(&dirs, "dir", "d", nil, "Directories to scan (default: opencode storage/log + skills)") cmd.Flags().StringSliceVarP(&dirs, "dir", "d", nil, "Directories to scan (default: opencode storage/log + skills)")
return cmd return cmd
+12 -3
View File
@@ -21,8 +21,11 @@ const sessionWatermarkKey = "opencode_sync_max_updated"
// It deliberately does not reconcile: RecordObservation updates the entry row // It deliberately does not reconcile: RecordObservation updates the entry row
// for the session it touched, and a full rebuild is disproportionate for a // for the session it touched, and a full rebuild is disproportionate for a
// handful of new turns. // handful of new turns.
func IngestSessionsNow(kdb *db.KnoxDB) (created, changed, unchanged int, err error) { func IngestSessionsNow(kdb *db.KnoxDB, full bool) (created, changed, unchanged int, err error) {
since, _ := strconv.ParseInt(kdb.Setting(sessionWatermarkKey), 10, 64) since := int64(0)
if !full {
since, _ = strconv.ParseInt(kdb.Setting(sessionWatermarkKey), 10, 64)
}
results, err := ingest.NewOpenCodeDBIngester().IngestSince(since) results, err := ingest.NewOpenCodeDBIngester().IngestSince(since)
if err != nil { if err != nil {
@@ -95,8 +98,14 @@ func sessionNeedsWrite(kdb *db.KnoxDB, result *ingest.IngestResult) bool {
return false return false
} }
prev, err := kdb.LatestContentHash(result.Fingerprint) prev, err := kdb.LatestContentHash(result.Fingerprint)
if err != nil || prev == "" { if err != nil {
return false return false
} }
if prev == "" {
// No stored hash: nothing proves the indexed content is current. Write
// once, then the hash makes later sweeps cheap. Without this, sessions
// indexed before hashing existed would never pick up new content.
return true
}
return prev != result.ContentHash return prev != result.ContentHash
} }
+1 -1
View File
@@ -695,7 +695,7 @@ func (w *Watcher) ingestGit() {
// sweep is full every time; RecordObservation collapses unchanged sessions, so // sweep is full every time; RecordObservation collapses unchanged sessions, so
// no watermark is needed and repeated runs are idempotent. // no watermark is needed and repeated runs are idempotent.
func (w *Watcher) ingestOpenCodeDB() { func (w *Watcher) ingestOpenCodeDB() {
if _, _, _, err := IngestSessionsNow(w.knoxDB); err != nil { if _, _, _, err := IngestSessionsNow(w.knoxDB, false); err != nil {
log.Printf("[knox] opencode.db ingest error: %v", err) log.Printf("[knox] opencode.db ingest error: %v", err)
} }
} }