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>
159 lines
4.2 KiB
Go
159 lines
4.2 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/david/knox/internal/db"
|
|
"github.com/david/knox/internal/watch"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// NewGossipCmd exposes peer status/diff for the gossip protocol.
|
|
func NewGossipCmd(kdb *db.KnoxDB) *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "gossip",
|
|
Short: "Inspect peer-to-peer observation sync",
|
|
}
|
|
cmd.AddCommand(
|
|
&cobra.Command{
|
|
Use: "status",
|
|
Short: "Show known peers and knowledge vectors",
|
|
RunE: func(c *cobra.Command, args []string) error {
|
|
peers, err := kdb.ListPeers()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
vector, err := kdb.KnowledgeVector()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Printf("node_id: %s\n", kdb.NodeID())
|
|
fmt.Printf("peers (static: %d configured):\n", len(watch.PeerAddrs()))
|
|
for _, p := range peers {
|
|
fmt.Printf(" %-24s %-28s last=%s vector=%d\n", p.PeerID, p.Addr, p.LastHandshake, p.Cursor)
|
|
}
|
|
if len(peers) == 0 {
|
|
fmt.Println(" (none — set KNOX_PEERS to sync with other nodes)")
|
|
}
|
|
fmt.Println("knowledge vector (node_id -> max hcl):")
|
|
for nid, hcl := range vector {
|
|
if nid == kdb.NodeID() {
|
|
continue
|
|
}
|
|
fmt.Printf(" %-24s -> %d\n", nid, hcl)
|
|
}
|
|
return nil
|
|
},
|
|
},
|
|
&cobra.Command{
|
|
Use: "diff <peer-url>",
|
|
Short: "Show observation/thread divergence with a peer",
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(c *cobra.Command, args []string) error {
|
|
return gossipDiff(kdb, args[0])
|
|
},
|
|
},
|
|
&cobra.Command{
|
|
Use: "sync",
|
|
Short: "Run one anti-entropy sweep against configured peers",
|
|
RunE: func(c *cobra.Command, args []string) error {
|
|
peers := watch.PeerAddrs()
|
|
if len(peers) == 0 {
|
|
return fmt.Errorf("no peers configured (set KNOX_PEERS)")
|
|
}
|
|
watch.Run(kdb, nil, peers)
|
|
created, linked, err := watch.Reconcile(kdb)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Printf("sync done: %d threads created, %d observations linked\n", created, linked)
|
|
return nil
|
|
},
|
|
},
|
|
)
|
|
return cmd
|
|
}
|
|
|
|
// gossipDiff compares this node's observation log + auto-threads with a peer's
|
|
// via /v1/diff. Never writes; it is the preview for what a sync would adopt.
|
|
func gossipDiff(kdb *db.KnoxDB, peerAddr string) error {
|
|
c := &watch.Client{Addr: peerAddr, Timeout: 10 * time.Second}
|
|
remote, err := c.Diff()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
local, err := kdb.DistinctFingerprints()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
remoteFP := make(map[string]bool, len(remote.Fingerprints))
|
|
for _, f := range remote.Fingerprints {
|
|
remoteFP[f] = true
|
|
}
|
|
|
|
var peerOnly, localOnly []string
|
|
for f := range remoteFP {
|
|
if !local[f] {
|
|
peerOnly = append(peerOnly, f)
|
|
}
|
|
}
|
|
for f := range local {
|
|
if !remoteFP[f] {
|
|
localOnly = append(localOnly, f)
|
|
}
|
|
}
|
|
|
|
fmt.Printf("peer: %s (%s)\n", remote.Name, remote.NodeID)
|
|
fmt.Printf("this node: %s\n", kdb.NodeID())
|
|
fmt.Printf("fingerprints: peer=%d this=%d\n", len(remoteFP), len(local))
|
|
fmt.Printf(" peer-only (would pull): %d\n", len(peerOnly))
|
|
for _, f := range peerOnly[:min(10, len(peerOnly))] {
|
|
fmt.Printf(" %s\n", f)
|
|
}
|
|
fmt.Printf(" local-only (would lose): %d\n", len(localOnly))
|
|
for _, f := range localOnly[:min(10, len(localOnly))] {
|
|
fmt.Printf(" %s\n", f)
|
|
}
|
|
|
|
// Tombstone divergence on auto-threaded threads.
|
|
localThreads, err := kdb.ThreadStatusByCluster()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
remoteThreads := remote.ThreadStatus
|
|
var resolvedPeer, revokedLocal []string
|
|
for k, st := range remoteThreads {
|
|
if st == "resolved" {
|
|
if ls, ok := localThreads[k]; !ok || ls != "resolved" {
|
|
resolvedPeer = append(resolvedPeer, k)
|
|
}
|
|
}
|
|
}
|
|
for k, st := range localThreads {
|
|
if st == "resolved" {
|
|
if rs, ok := remoteThreads[k]; !ok || rs != "resolved" {
|
|
revokedLocal = append(revokedLocal, k)
|
|
}
|
|
}
|
|
}
|
|
if len(resolvedPeer) > 0 {
|
|
fmt.Printf("threads resolved on peer but not here (tombstone to adopt): %d\n", len(resolvedPeer))
|
|
}
|
|
if len(revokedLocal) > 0 {
|
|
fmt.Printf("threads resolved here but not on peer (would resurrect on sync): %d\n", len(revokedLocal))
|
|
}
|
|
if len(peerOnly) == 0 && len(localOnly) == 0 && len(resolvedPeer) == 0 && len(revokedLocal) == 0 {
|
|
fmt.Println("in sync.")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func min(a, b int) int {
|
|
if a < b {
|
|
return a
|
|
}
|
|
return b
|
|
}
|