fix: harden gossip, HLC restarts, watcher races, MCP args, pagination

- gossip: validate push batches (4 MiB / 1000-row caps); reject rows
  claiming the local node id (vector-poisoning), empty ids, negative HCLs
- gossip: reconcile derived state after pulls (pulls only append to the
  observation log, so entry-count comparison could never trigger it)
- hlc: seek clock from persisted MAX(hcl) at Open so a restart with a
  regressed wall clock cannot reissue values (locator/cursor safety)
- db: serialize writers via BEGIN IMMEDIATE DSN, single conn per pool,
  and a per-KnoxDB mutex around RecordObservation's dedup
- watch: atomic ticker guards (was a cross-goroutine data race),
  trailing-edge per-path debounce, recursive directory watches,
  rename re-ingest, remove cancels pending ingests
- mcp: strict argument validation (no silent clamping), thread existence
  checks before writes, nil-safe golden-thread tool
- cli: --page 0 no longer panics; query/recent pagination actually pages
- tests: hlc SeekTo monotonicity, concurrent dedup race, push validation,
  batch caps, idempotency on observation counts
This commit is contained in:
2026-09-17 01:52:06 -07:00
parent 876d2aa45f
commit 6845975b7b
10 changed files with 733 additions and 116 deletions
+19 -6
View File
@@ -13,7 +13,7 @@ import (
func paginate(entries []db.Entry, page, limit int) []db.Entry {
start := (page - 1) * limit
if start >= len(entries) {
if start < 0 || start >= len(entries) {
return nil
}
end := start + limit
@@ -74,16 +74,22 @@ func NewQueryCmd(kdb *db.KnoxDB) *cobra.Command {
if query == "" {
return fmt.Errorf("search query required (positional arg or stdin pipe)")
}
results, err := kdb.Search(query, limit)
if page < 1 || limit < 1 {
return fmt.Errorf("page and limit must be >= 1 (got page=%d limit=%d)", page, limit)
}
// Fetch enough rows to cover the requested page: Search caps at its
// limit argument, so slicing a limit-sized result set could never
// reach page 2+.
all, err := kdb.Search(query, page*limit)
if err != nil {
return err
}
results = paginate(results, page, limit)
results := paginate(all, page, limit)
if len(results) == 0 {
fmt.Println("No results found.")
return nil
}
fmt.Printf("Found %d results for %q (page %d, %d per page):\n\n", len(results), query, page, limit)
fmt.Printf("Found %d results for %q — showing %d (page %d, %d per page):\n\n", len(all), query, len(results), page, limit)
for _, r := range results {
fmt.Printf(" %-8s %-30s [%s] %s\n", shortFP(r.Fingerprint), truncateStr(r.Title, 30), r.Project, r.SourceID)
if r.Summary != "" {
@@ -105,13 +111,20 @@ func NewRecentCmd(kdb *db.KnoxDB) *cobra.Command {
Use: "recent",
Short: "Show recent knowledge entries",
RunE: func(c *cobra.Command, args []string) error {
all, err := kdb.RecentEntries(limit * 10)
if page < 1 || limit < 1 {
return fmt.Errorf("page and limit must be >= 1 (got page=%d limit=%d)", page, limit)
}
all, err := kdb.RecentEntries(page * limit)
if err != nil {
return err
}
entries := paginate(all, page, limit)
if len(entries) == 0 {
fmt.Println("No entries yet. Run `knox watch` to start ingesting.")
if page > 1 {
fmt.Printf("No more entries on page %d (page %d of %d).\n", page, page, (len(all)+limit-1)/limit)
} else {
fmt.Println("No entries yet. Run `knox watch` to start ingesting.")
}
return nil
}
fmt.Printf("Recent %d entries (page %d, %d per page):\n\n", len(entries), page, limit)