Compare commits

1 Commits

Author SHA1 Message Date
david 4dd1c2be09 feat: index reasoning and close the session freshness gap (#18)
build / test (push) Has been cancelled
build / image (push) Has been cancelled
Two changes that make the index reflect work as it happens.

Reasoning is now indexed. The model's own justification is where a decision is
explained, and it never appears in the text a reader sees. It is also enormous —
individual parts reach 175 KB — so each part is truncated to 1.2 KB and each
session is budgeted 4 KB. It lands in content (searchable) but stays out of the
summary, which is what search results and MCP output show.

Capturing it exposed a dedupe bug: sessions were skipped when created_at and
summary matched, so content that gained reasoning behind unchanged prose was
never recorded. IngestResult now carries a content hash and the sweep compares
it, which is what the observation layer already used internally.

Freshness: the watcher polls every two minutes, so a finished turn stayed
unsearchable that long. IngestSessionsNow shares the watcher's logic, sweeps
only sessions newer than a stored watermark, and runs in ~45 ms steady state
(a full sweep is ~21 s for 59k messages). The plugin calls it at prompt
admission, fire-and-forget.
2026-11-04 19:38:44 -08:00
4 changed files with 8 additions and 23 deletions
+1 -4
View File
@@ -27,10 +27,7 @@ on:
concurrency:
group: knox-build
# Queue rather than cancel: cancelling killed an in-flight image build
# mid-session, leaving the deployed node on an older binary than main. One
# deploy target means a queued build is cheap and a missing one is not.
cancel-in-progress: false
cancel-in-progress: true
jobs:
test:
+3 -6
View File
@@ -13,7 +13,6 @@ import (
func NewIngestCmd(kdb *db.KnoxDB) *cobra.Command {
var dirs []string
var sessionsFull bool
cmd := &cobra.Command{
Use: "ingest",
Short: "One-time full sweep of all sources into the knowledge index",
@@ -135,7 +134,7 @@ func NewIngestCmd(kdb *db.KnoxDB) *cobra.Command {
fmt.Printf("\nIngest complete: %d new entries, %d updated\n", totalNew, totalUpdated)
},
}
sessionsCmd := &cobra.Command{
cmd.AddCommand(&cobra.Command{
Use: "sessions",
Short: "Re-index opencode sessions only (fast; what the watcher timer does)",
Long: `Re-index opencode sessions only.
@@ -144,16 +143,14 @@ 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
being searchable, without paying for a full sweep.`,
RunE: func(c *cobra.Command, args []string) error {
created, changed, unchanged, err := watch.IngestSessionsNow(kdb, sessionsFull)
created, changed, unchanged, err := watch.IngestSessionsNow(kdb)
if err != nil {
return err
}
fmt.Printf("sessions: %d new, %d changed, %d unchanged\n", created, changed, unchanged)
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)")
return cmd
+3 -12
View File
@@ -21,11 +21,8 @@ const sessionWatermarkKey = "opencode_sync_max_updated"
// 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)
}
func IngestSessionsNow(kdb *db.KnoxDB) (created, changed, unchanged int, err error) {
since, _ := strconv.ParseInt(kdb.Setting(sessionWatermarkKey), 10, 64)
results, err := ingest.NewOpenCodeDBIngester().IngestSince(since)
if err != nil {
@@ -98,14 +95,8 @@ func sessionNeedsWrite(kdb *db.KnoxDB, result *ingest.IngestResult) bool {
return false
}
prev, err := kdb.LatestContentHash(result.Fingerprint)
if err != nil {
if err != nil || prev == "" {
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
}
+1 -1
View File
@@ -695,7 +695,7 @@ func (w *Watcher) ingestGit() {
// 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 {
if _, _, _, err := IngestSessionsNow(w.knoxDB); err != nil {
log.Printf("[knox] opencode.db ingest error: %v", err)
}
}