Files
knox/internal/db/sqlcdb/entries.sql.go
T
david e3bbeabb6b
build / test (push) Successful in 3m19s
build / image (push) Successful in 18s
feat: always-on remote node — MCP over HTTP, TLS/token, Docker, CI, sqlc (#9)
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>
2026-11-05 02:14:33 +00:00

153 lines
3.9 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: entries.sql
package sqlcdb
import (
"context"
"database/sql"
)
const countEntries = `-- name: CountEntries :one
SELECT COUNT(*) FROM entries
`
func (q *Queries) CountEntries(ctx context.Context) (int64, error) {
row := q.db.QueryRowContext(ctx, countEntries)
var count int64
err := row.Scan(&count)
return count, err
}
const countObservations = `-- name: CountObservations :one
SELECT COUNT(*) FROM observations
`
// NOTE: the FTS5 search path is deliberately absent. sqlc ignores
// `CREATE VIRTUAL TABLE ... USING fts5`, so `entries_fts` and the bm25()/MATCH
// operators are invisible to it. Search stays hand-written in db.go.
func (q *Queries) CountObservations(ctx context.Context) (int64, error) {
row := q.db.QueryRowContext(ctx, countObservations)
var count int64
err := row.Scan(&count)
return count, err
}
const getEntry = `-- name: GetEntry :one
SELECT fingerprint, source_id, COALESCE(source_path,'') AS source_path,
COALESCE(project,'') AS project, COALESCE(content_type,'') AS content_type,
COALESCE(title,'') AS title, COALESCE(summary,'') AS summary,
COALESCE(first_seen,'') AS first_seen, COALESCE(last_seen,'') AS last_seen,
COALESCE(created_at,'') AS created_at, ref_count,
COALESCE(last_confidence,0.5) AS last_confidence
FROM entries
WHERE fingerprint = ?
`
type GetEntryRow struct {
Fingerprint string
SourceID string
SourcePath string
Project string
ContentType string
Title string
Summary string
FirstSeen string
LastSeen string
CreatedAt string
RefCount sql.NullInt64
LastConfidence float64
}
// Pilot queries for sqlc: the read paths behind the MCP tools.
//
// These are the highest-traffic reads and deliberately include the hardest
// case (FTS5 MATCH + bm25 ranking) so the pilot proves whether sqlc can cover
// the search path, not just the easy table scans.
func (q *Queries) GetEntry(ctx context.Context, fingerprint string) (GetEntryRow, error) {
row := q.db.QueryRowContext(ctx, getEntry, fingerprint)
var i GetEntryRow
err := row.Scan(
&i.Fingerprint,
&i.SourceID,
&i.SourcePath,
&i.Project,
&i.ContentType,
&i.Title,
&i.Summary,
&i.FirstSeen,
&i.LastSeen,
&i.CreatedAt,
&i.RefCount,
&i.LastConfidence,
)
return i, err
}
const listRecentEntries = `-- name: ListRecentEntries :many
SELECT fingerprint, source_id, COALESCE(source_path,'') AS source_path,
COALESCE(project,'') AS project, COALESCE(content_type,'') AS content_type,
COALESCE(title,'') AS title, COALESCE(summary,'') AS summary,
COALESCE(first_seen,'') AS first_seen, COALESCE(last_seen,'') AS last_seen,
COALESCE(created_at,'') AS created_at, ref_count,
COALESCE(last_confidence,0.5) AS last_confidence
FROM entries
ORDER BY COALESCE(NULLIF(created_at,''), last_seen) DESC
LIMIT ?
`
type ListRecentEntriesRow struct {
Fingerprint string
SourceID string
SourcePath string
Project string
ContentType string
Title string
Summary string
FirstSeen string
LastSeen string
CreatedAt string
RefCount sql.NullInt64
LastConfidence float64
}
func (q *Queries) ListRecentEntries(ctx context.Context, limit int64) ([]ListRecentEntriesRow, error) {
rows, err := q.db.QueryContext(ctx, listRecentEntries, limit)
if err != nil {
return nil, err
}
defer rows.Close()
items := []ListRecentEntriesRow{}
for rows.Next() {
var i ListRecentEntriesRow
if err := rows.Scan(
&i.Fingerprint,
&i.SourceID,
&i.SourcePath,
&i.Project,
&i.ContentType,
&i.Title,
&i.Summary,
&i.FirstSeen,
&i.LastSeen,
&i.CreatedAt,
&i.RefCount,
&i.LastConfidence,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}