Faithful port of mcp-server/relevance.ts (Rust observer's adjacency-
pollution filter). Same 5-signal scoring, same default threshold 0.3.
Adds POST /v1/matrix/relevance endpoint via matrixd.
Scoring signals (additive, can sign-flip):
path_match +1.0 chunk source/doc_id encodes focus.path
filename_match +0.6 chunk text mentions focus's filename
defined_match +0.6 chunk text mentions focus.defined_symbols
token_overlap +0.4 jaccard of non-stopword tokens
prefix_match +0.3 chunk source shares first-2-segment prefix
import_penalty -0.5 mentions ONLY imported symbols, no defined ones
What this does and doesn't do:
- DOES filter code-aware corpora (eventually lakehouse_arch_v1,
lakehouse_symbols_v1, scrum_findings_v1) — drops chunks about
code the focus file IMPORTS rather than DEFINES, the
"adjacency pollution" pattern that makes a reviewer LLM
hallucinate imported-crate internals as belonging to the focus
- DOES NOT meaningfully filter staffing data — the candidates
reality test 2026-04-29 had "exact skill match buried at #3"
which is a different problem (semantic-only ranking dominated
by secondary text). Staffing needs structured filtering
(status gates, location gates) that lives outside this
package — future work, not in SPEC §3.4 yet
Headline smoke assertion: focus = crates/queryd/src/db.go which
defines Connector and imports catalogd::Registry. The filter
scores:
Connector chunk: +0.68 (defined_match fires, kept)
Registry chunk: -0.46 (import_only penalty fires, dropped)
unrelated junk: 0.00 (no signals, dropped)
That's a 1.14-point gap between what we ARE and what we IMPORT —
the entire purpose of the filter.
Tests:
- 9 unit tests in internal/matrix/relevance_test.go covering
Tokenize, Jaccard, ExtractDefinedSymbols (Rust + TS),
ExtractImportedSymbols, FilePrefix, ScoreRelevance per-signal,
FilterChunks threshold splitting, and the headline
AdjacencyPollutionScenario
- scripts/relevance_smoke.sh integration smoke (3 assertions PASS):
adjacency-pollution scenario, empty-chunks 400, threshold honored
13-smoke regression sweep all green (D1-D6, G1, G1P, G2,
storaged_cap, pathway, matrix, relevance).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
154 lines
4.5 KiB
Go
154 lines
4.5 KiB
Go
// matrixd is the matrix indexer service. Wraps internal/matrix's
|
|
// Retriever with HTTP routes per docs/SPEC.md §3.4.
|
|
//
|
|
// Routes:
|
|
// POST /matrix/search — multi-corpus retrieve+merge
|
|
// GET /matrix/corpora — list known vectord indexes (proxy)
|
|
// POST /matrix/relevance — adjacency-pollution filter (CODE-aware;
|
|
// port of mcp-server/relevance.ts)
|
|
//
|
|
// matrixd talks to embedd (for query-text embedding) and vectord
|
|
// (for per-corpus search) via HTTP. Both URLs come from
|
|
// [matrixd] config; gateway sets them to its own upstream URLs so
|
|
// matrixd inherits the same provider topology.
|
|
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"flag"
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"git.agentview.dev/profit/golangLAKEHOUSE/internal/matrix"
|
|
"git.agentview.dev/profit/golangLAKEHOUSE/internal/shared"
|
|
)
|
|
|
|
const maxRequestBytes = 4 << 20 // 4 MiB cap on request bodies
|
|
|
|
func main() {
|
|
configPath := flag.String("config", "lakehouse.toml", "path to TOML config")
|
|
flag.Parse()
|
|
|
|
cfg, err := shared.LoadConfig(*configPath)
|
|
if err != nil {
|
|
slog.Error("config", "err", err)
|
|
os.Exit(1)
|
|
}
|
|
if cfg.Matrixd.EmbeddURL == "" || cfg.Matrixd.VectordURL == "" {
|
|
slog.Error("matrixd: embedd_url and vectord_url required in [matrixd]")
|
|
os.Exit(1)
|
|
}
|
|
|
|
retriever := matrix.New(cfg.Matrixd.EmbeddURL, cfg.Matrixd.VectordURL)
|
|
h := &handlers{r: retriever}
|
|
|
|
if err := shared.Run("matrixd", cfg.Matrixd.Bind, h.register, cfg.Auth); err != nil {
|
|
slog.Error("server", "err", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
type handlers struct {
|
|
r *matrix.Retriever
|
|
}
|
|
|
|
func (h *handlers) register(r chi.Router) {
|
|
r.Post("/matrix/search", h.handleSearch)
|
|
r.Get("/matrix/corpora", h.handleCorpora)
|
|
r.Post("/matrix/relevance", h.handleRelevance)
|
|
}
|
|
|
|
func (h *handlers) handleSearch(w http.ResponseWriter, r *http.Request) {
|
|
var req matrix.SearchRequest
|
|
if !decodeJSON(w, r, &req) {
|
|
return
|
|
}
|
|
resp, err := h.r.Search(r.Context(), req)
|
|
if err != nil {
|
|
writeMatrixError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, resp)
|
|
}
|
|
|
|
// relevanceRequest is the POST /matrix/relevance body. Threshold
|
|
// defaults to matrix.DefaultRelevanceThreshold when zero.
|
|
type relevanceRequest struct {
|
|
Focus matrix.FocusFile `json:"focus"`
|
|
Chunks []matrix.CandidateChunk `json:"chunks"`
|
|
Threshold float64 `json:"threshold,omitempty"`
|
|
}
|
|
|
|
func (h *handlers) handleRelevance(w http.ResponseWriter, r *http.Request) {
|
|
var req relevanceRequest
|
|
if !decodeJSON(w, r, &req) {
|
|
return
|
|
}
|
|
if len(req.Chunks) == 0 {
|
|
http.Error(w, "chunks must be non-empty", http.StatusBadRequest)
|
|
return
|
|
}
|
|
threshold := req.Threshold
|
|
if threshold == 0 {
|
|
threshold = matrix.DefaultRelevanceThreshold
|
|
}
|
|
res := matrix.FilterChunks(req.Focus, req.Chunks, threshold)
|
|
writeJSON(w, http.StatusOK, res)
|
|
}
|
|
|
|
func (h *handlers) handleCorpora(w http.ResponseWriter, r *http.Request) {
|
|
names, err := h.r.Corpora(r.Context())
|
|
if err != nil {
|
|
slog.Error("matrix corpora", "err", err)
|
|
http.Error(w, "vectord unavailable", http.StatusBadGateway)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"corpora": names, "count": len(names)})
|
|
}
|
|
|
|
func decodeJSON(w http.ResponseWriter, r *http.Request, v any) bool {
|
|
defer r.Body.Close()
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestBytes)
|
|
if err := json.NewDecoder(r.Body).Decode(v); err != nil {
|
|
var maxErr *http.MaxBytesError
|
|
if errors.As(err, &maxErr) || strings.Contains(err.Error(), "http: request body too large") {
|
|
http.Error(w, "body too large", http.StatusRequestEntityTooLarge)
|
|
return false
|
|
}
|
|
http.Error(w, "decode body: "+err.Error(), http.StatusBadRequest)
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func writeJSON(w http.ResponseWriter, code int, v any) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(code)
|
|
if err := json.NewEncoder(w).Encode(v); err != nil {
|
|
slog.Warn("matrix write json", "err", err)
|
|
}
|
|
}
|
|
|
|
// writeMatrixError maps internal/matrix sentinels to HTTP statuses.
|
|
// Corpus / embed failures bubble up as 502 (the upstream service is
|
|
// what's wrong); validation errors are 400.
|
|
func writeMatrixError(w http.ResponseWriter, err error) {
|
|
switch {
|
|
case errors.Is(err, matrix.ErrEmptyCorpora),
|
|
errors.Is(err, matrix.ErrEmptyQuery):
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
case errors.Is(err, matrix.ErrCorpus),
|
|
errors.Is(err, matrix.ErrEmbed):
|
|
slog.Warn("matrix upstream", "err", err)
|
|
http.Error(w, err.Error(), http.StatusBadGateway)
|
|
default:
|
|
slog.Error("matrix", "err", err)
|
|
http.Error(w, "internal", http.StatusInternalServerError)
|
|
}
|
|
}
|