fleet-kb-index: fix stats() grouping and the text-only write path (nix-config#1876) #2850

Merged
hermes merged 1 commit from kb/NCFG-1876-kb-stats-fix into main 2026-11-21 11:10:49 -06:00
Collaborator

Follow-up on #2847 (kanban nix-config#1876), found by exercising the merged package on vpn.

1. fleet-kb stats / kb_stats crashed — GroupingError: column "chunk.commit" must appear in the GROUP BY clause. The per-source commitment query selected a bare commit beside max(indexed_at); it is grouped as max(commit) now (exact: a source is one file, so its chunks share one commit).

2. --no-embed wrote nothing into an empty store — the no-vector path was an UPDATE, so a text-only run matched zero rows while the summary still reported chunks=N. Now an INSERT ... ON CONFLICT DO UPDATE that omits embedding, so a text-only run populates the table and an existing vector survives a text-only refresh.

Evidence (scratch PG 15.19 + pgvector 0.8.6 cluster on vpn, packaged schema applied twice rc=0):

  • fleet-kb stats returns total_chunks/embedded/by_corpus/sources JSON (was a traceback).
  • --no-embed --corpus note on an EMPTY store: 1412 rows written where the old code wrote 0; second run skipped=1412.
  • --no-embed refresh over 26 embedded skill chunks: 26 rows / 26 embeddings before and after.
Follow-up on #2847 (kanban nix-config#1876), found by exercising the merged package on vpn. **1. `fleet-kb stats` / `kb_stats` crashed** — `GroupingError: column "chunk.commit" must appear in the GROUP BY clause`. The per-source commitment query selected a bare `commit` beside `max(indexed_at)`; it is grouped as `max(commit)` now (exact: a source is one file, so its chunks share one commit). **2. `--no-embed` wrote nothing into an empty store** — the no-vector path was an `UPDATE`, so a text-only run matched zero rows while the summary still reported `chunks=N`. Now an `INSERT ... ON CONFLICT DO UPDATE` that omits `embedding`, so a text-only run populates the table and an existing vector survives a text-only refresh. **Evidence** (scratch PG 15.19 + pgvector 0.8.6 cluster on vpn, packaged schema applied twice rc=0): - `fleet-kb stats` returns total_chunks/embedded/by_corpus/sources JSON (was a traceback). - `--no-embed --corpus note` on an EMPTY store: 1412 rows written where the old code wrote 0; second run `skipped=1412`. - `--no-embed` refresh over 26 embedded skill chunks: 26 rows / 26 embeddings before and after.
fleet-kb-index: fix stats() grouping and the text-only write path
All checks were successful
PR base guard / Base drift / conflict check (pull_request) Successful in 7s
Policy Gates / Worker-scope reaper worker signature (mutation-tested) (pull_request) Successful in 6s
Policy Gates / Kanban pool capacity knob (runtime override + clamp, mutation-tested) (pull_request) Successful in 3s
Policy Gates / Sentinel pass budget contract (prompt forbids the full board walk, mutation-tested) (pull_request) Successful in 3s
Secret Scan / gitleaks scan (pull_request) Successful in 13s
Policy Gates / CI-scratch reaper class table (mutation-tested) (pull_request) Successful in 4s
Policy Gates / Fleet cache sweep class table (mutation-tested) (pull_request) Successful in 6s
NixOS Build Check / Scope changed deployable hosts (pull_request) Successful in 41s
NixOS Build Check / Flake input regression guard (pull_request) Successful in 7s
foremerge intent (advisory) all 2 changed path(s) covered by a live foremerge intent
Policy Gates / foremerge accept-order contract (real binary) (pull_request) Successful in 1m13s
NixOS Build Check / Hermes runtime gates (one eval of vpn's runtime) (pull_request) Successful in 1m8s
Policy Gates / Scheduled foremerge reap command line (mutation-tested) (pull_request) Successful in 4s
Policy Gates / .forgejo/tests python suites (hermetic) (pull_request) Successful in 55s
NixOS Build Check / Build changed host closures (per-host budget) (pull_request) Successful in 2m28s
Policy Gates / Fleet control ledger (sealed profile fails the build, mutation-tested) (pull_request) Successful in 23s
NixOS Build Check / Gate summary (pull_request) Successful in 0s
Repo Gates / Repo gates (script-only battery) (pull_request) Successful in 4m20s
377cf8a693
Both defects were found by running the MERGED package on vpn against a real
Postgres 15 + pgvector 0.8.6 store, not by reading it:

* `fleet-kb stats` (and the `kb_stats` MCP tool) died with
  `GroupingError: column "chunk.commit" must appear in the GROUP BY clause` —
  the per-source commitment query selected a bare `commit` beside
  `max(indexed_at)`. Grouped as `max(commit)`, which is exact here: a source is
  one file, so its chunks share one commit.

* `--no-embed` wrote NOTHING into an empty store: the no-vector path was an
  `UPDATE`, so it matched zero rows while the summary still reported
  `chunks=N`. It is now an INSERT ... ON CONFLICT DO UPDATE that omits
  `embedding`, so a text-only run populates the table and an existing vector
  survives a text-only refresh. Measured on the scratch store: 1412 note
  chunks written where the old code wrote 0, second run skipped=1412, and a
  --no-embed refresh over 26 embedded skill chunks left 26 embeddings intact.
hermes merged commit 2e7aa920c0 into main 2026-11-21 11:10:48 -06:00
hermes deleted branch kb/NCFG-1876-kb-stats-fix 2026-11-21 11:10:57 -06:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ymrtech/nix-config!2850
No description provided.