package watch import ( "log" "strconv" "github.com/david/knox/internal/db" "github.com/david/knox/internal/ingest" ) // sessionWatermarkKey holds the newest session time_updated already swept, so a // repeated sync reads only sessions touched since. Without it every call would // re-read all 59k messages (~20s). const sessionWatermarkKey = "opencode_sync_max_updated" // IngestSessionsNow sweeps opencode.db once and records only the sessions whose // content changed. It is the same work the watcher does on its timer, exposed // so a caller (the opencode plugin, the CLI) can close the freshness gap // without waiting for the 2-minute tick. // // It deliberately does not reconcile: RecordObservation updates the entry row // for the session it touched, and a full rebuild is disproportionate for a // handful of new turns. func IngestSessionsNow(kdb *db.KnoxDB, full bool) (created, changed, unchanged int, err error) { since := int64(0) if !full { since, _ = strconv.ParseInt(kdb.Setting(sessionWatermarkKey), 10, 64) } results, err := ingest.NewOpenCodeDBIngester().IngestSince(since) if err != nil { return 0, 0, 0, err } watermark := since for _, result := range results { if result.SourceUpdated > watermark { watermark = result.SourceUpdated } if !sessionNeedsWrite(kdb, result) { unchanged++ continue } _, isNew, err := kdb.RecordObservation(db.ObservationRecord{ Fingerprint: result.Fingerprint, SourceID: result.SourceID, SourcePath: result.SourcePath, Project: result.Project, ContentType: result.ContentType, Title: result.Title, Summary: result.Summary, CreatedAt: result.CreatedAt, LineEnd: result.LineEnd, Confidence: result.Confidence, IngesterVersion: result.IngesterVersion, Trigger: "session_sync", Provenance: ingest.ProvenanceJSON(result.Provenance), ContentHash: result.ContentHash, Content: result.Content, }) if err != nil { continue } if isNew { created++ } else { changed++ } } // Only advance once the sweep completed, so a failed run re-reads its range. if watermark > since { if err := kdb.SetSetting(sessionWatermarkKey, strconv.FormatInt(watermark, 10)); err != nil { log.Printf("[knox] session watermark: %v", err) } } if created > 0 || changed > 0 { log.Printf("[knox] sessions: %d new, %d changed, %d unchanged", created, changed, unchanged) } return created, changed, unchanged, nil } // sessionNeedsWrite reports whether a sweep result differs from what the index // already holds. created_at alone cannot detect a resumed session (resuming // appends messages without touching the creation time), so the summary is // compared too, and the content hash catches edits that leave the prose intact // — newly indexed reasoning behind an unchanged summary. func sessionNeedsWrite(kdb *db.KnoxDB, result *ingest.IngestResult) bool { existing, err := kdb.FindEntry(result.Fingerprint) if err != nil || existing == nil { return true } if existing.CreatedAt != result.CreatedAt || existing.Summary != result.Summary { return true } if result.ContentHash == "" { return false } prev, err := kdb.LatestContentHash(result.Fingerprint) if err != nil { 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 }