e3bbeabb6b
Deploys knox as a headless, always-on replica so agents keep their memory when the laptop is off. Closes #8. **Supersedes #5** (its hardening commit is merged in here; close #5 after this lands). ## What changed **MCP transport + SDK** - Moves from `mark3labs/mcp-go` v0.17 (stdio only, five releases behind) to the official `modelcontextprotocol/go-sdk` v1.8. Tool inputs are typed structs, so schemas are generated instead of hand-built. - Adds `knox mcp --http` (Streamable HTTP). stdio remains the default. **Transport security without a public name** - `KNOX_AUTH_TOKEN` guards gossip and MCP HTTP; `KNOX_TLS_CERT/KEY` serve TLS; `knox tls init` mints a self-signed cert whose SANs are whatever addresses clients dial. Verified: 401 without the token, 200 with, cert valid for the overlay IP. **Daemon hygiene** - SIGTERM shuts down the gossip and metrics servers. - `--version` reports the VCS revision. - `KNOX_LOCAL_SOURCES=none` makes a node ingest nothing but still derive. - Default watch dirs skip paths that do not exist. **Deployment topology fixes** (all found by actually running a replica) - `KNOX_LISTEN_ADDR` split from `KNOX_PEER_ADDR`: a container cannot bind the host's overlay IP, and previously the gossip server silently failed to start. - Reconcile now fires on push and on startup, not only on pull. Three separate bugs left `entries` empty on a push-only node. - Pushes batch by byte budget, not just row count — 500 content-heavy rows exceeded the 4 MiB body limit and the log stalled permanently at 3,000 observations. - Threads and snippets derive on replicas: gating them with the ingesters left the server holding 11.5k observations and 4.9k entries but 2 of 28 threads. **Packaging + CI** - Multi-stage Dockerfile (static, non-root, `/data` volume). - Gitea workflow builds on the dhg.lol daemon through the runner socket, tags `:latest` + `:<sha>`, and restarts the stack when installed. BuildKit cache mounts keep module downloads warm. **sqlc pilot** - Schema extracted to `internal/db/schema.sql` (embedded); read paths behind the MCP tools are generated. - The pilot found real drift: `entries.created_at` existed only as a runtime `ALTER`. It is now declared in the schema, and `sqlcdb_pilot_test.go` fails if generated code disagrees with `db.Open`. - FTS5 stays hand-written: sqlc ignores `CREATE VIRTUAL TABLE`, so `MATCH`/`bm25` cannot be generated. ## Verification - `go vet ./...`, `go test ./...`, `gofmt` clean - `scripts/conformance.sh` — official MCP suite, exit 0 against `conformance-baseline.yml` (documents the capabilities knox does not implement: resources, prompts, completions, fixture tools) - Live: both containers healthy on dhg.lol, ports bound only to the ZeroTier address, 11,553 observations / 4,943 entries replicated and reconciled ## Deploy Infrastructure lives in `tf-net/apps/knox` (separate repo); this branch is what is currently running in production. Reviewed-on: #9 Co-authored-by: David Gwilliam <dhgwilliam@gmail.com> Co-committed-by: David Gwilliam <dhgwilliam@gmail.com>
115 lines
2.6 KiB
Go
115 lines
2.6 KiB
Go
package metrics
|
|
|
|
import (
|
|
"io"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/david/knox/internal/db"
|
|
"github.com/prometheus/client_golang/prometheus/testutil"
|
|
)
|
|
|
|
func tmpKdb(t *testing.T) *db.KnoxDB {
|
|
t.Helper()
|
|
k, err := db.Open(filepath.Join(t.TempDir(), "index.db"))
|
|
if err != nil {
|
|
t.Fatalf("open db: %v", err)
|
|
}
|
|
t.Cleanup(func() { k.Close() })
|
|
return k
|
|
}
|
|
|
|
func seed(t *testing.T, k *db.KnoxDB, src string, n int) {
|
|
t.Helper()
|
|
for i := 0; i < n; i++ {
|
|
_, _, err := k.RecordObservation(db.ObservationRecord{
|
|
Fingerprint: "fp-" + src + "-" + string(rune('a'+i)),
|
|
SourceID: src,
|
|
SourcePath: src,
|
|
Project: "test",
|
|
ContentType: "test",
|
|
Title: src,
|
|
Summary: "s",
|
|
CreatedAt: "2026-08-29T00:00:00Z",
|
|
Confidence: 0.9,
|
|
IngesterVersion: "itest/v1",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("seed: %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestMetricsSnapshot(t *testing.T) {
|
|
k := tmpKdb(t)
|
|
seed(t, k, "git", 2)
|
|
seed(t, k, "browser-history", 3)
|
|
|
|
s, err := k.MetricsSnapshot()
|
|
if err != nil {
|
|
t.Fatalf("snapshot: %v", err)
|
|
}
|
|
if s.Observations != 5 {
|
|
t.Errorf("observations = %d, want 5", s.Observations)
|
|
}
|
|
if s.BySource["git"] != 2 || s.BySource["browser-history"] != 3 {
|
|
t.Errorf("by source = %v", s.BySource)
|
|
}
|
|
if s.KnowledgeVector[k.NodeID()] == 0 {
|
|
t.Errorf("knowledge vector missing own node")
|
|
}
|
|
if s.ByOriginNode[k.NodeID()] != 5 {
|
|
t.Errorf("by origin node = %v", s.ByOriginNode)
|
|
}
|
|
}
|
|
|
|
func TestMetricsScrape(t *testing.T) {
|
|
k := tmpKdb(t)
|
|
seed(t, k, "git", 2)
|
|
|
|
m := New(k, "testnode")
|
|
h := m.Handler()
|
|
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest("GET", "/metrics", nil))
|
|
|
|
body, _ := io.ReadAll(rec.Body)
|
|
out := string(body)
|
|
for _, want := range []string{
|
|
`knox_node_info{name="testnode"`,
|
|
`knox_observations{source_id="git"} 2`,
|
|
`knox_gossip_pulls_total 0`,
|
|
"go_goroutines",
|
|
"process_cpu_seconds_total",
|
|
} {
|
|
if !strings.Contains(out, want) {
|
|
t.Errorf("scrape output missing %q", want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestMetricsCounters(t *testing.T) {
|
|
k := tmpKdb(t)
|
|
m := New(k, "t")
|
|
|
|
// Manually drive counters through the Metrics API.
|
|
m.IncrementPull(3)
|
|
m.IncrementPush(7)
|
|
m.IncrementErrors()
|
|
|
|
if got := testutil.ToFloat64(m.pullsTotal); got != 1 {
|
|
t.Errorf("pullsTotal = %v, want 1", got)
|
|
}
|
|
if got := testutil.ToFloat64(m.obsPulledTotal); got != 3 {
|
|
t.Errorf("obsPulled = %v, want 3", got)
|
|
}
|
|
if got := testutil.ToFloat64(m.obsPushedTotal); got != 7 {
|
|
t.Errorf("obsPushed = %v, want 7", got)
|
|
}
|
|
if got := testutil.ToFloat64(m.errorsTotal); got != 1 {
|
|
t.Errorf("errorsTotal = %v, want 1", got)
|
|
}
|
|
}
|