Require a token for writes and quarantine what nobody vouches for

Sixteen mutating routes now refuse a caller who cannot present the token the
server started with; queries stay open, because a question is not a write. The
environment variable is ELY_EYE_AUTH_TOKEN, following the repo's env_prefix
convention — the shorter name I reached for first returned 403 where the smoke
expected 401, and the server printed the reason.

Words from a stranger are quarantined on the way in: an HTTP upload is
third_party by definition, and a sentence carrying an instruction-shaped phrase
is quarantined whoever wrote it. A quarantined atom stays findable and stays out
of the compiled context until a second, live, unquarantined source agrees with
its rarest words. Corroboration is computed at read time because it is a
property of the corpus now, not of the moment a memory was written; it is
term-level, and the docstring says so.

forget now erases the source row and the compiled contexts beside the atoms, so
a forgotten memory leaves nothing that can still speak. The semantic cache key
carries a store revision, which fixes a real staleness: a context compiled
before new evidence arrived kept its old quarantine decision forever.

bench.audit --product still pointed at evals.py, which the p1-6 split moved into
backend/src/ely_eye/proofs/. It failed fast, but the anti-self-justification
rule had quietly stopped auditing the product; it now walks the package, and a
test pins that the package exists and holds no re-roll.

Gates: pytest 306 passed; ruff clean; mypy cold cache 24 errors in 9 files, the
recorded baseline, net zero; pyright 0 errors on the 23 backend files and the 2
bench files; go build/vet/test and gofmt clean; npm run build 221.39 kB with
eslint exit 0; bench/filesize_check clean; bench.audit --product clean over 31
harness files; scripts/mcp_write_smoke.sh PASS with the gate live across two
servers and the MCP child.
This commit is contained in:
ZacharyZhang-NY committed 2026-10-08 23:05:15 -04:00
1 parent 8b974e6626
commit 9145a23f18
32 files changed
+750 -58

No files matched your search

+12
View File
@@ -11,6 +11,7 @@ import (
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
@@ -21,6 +22,7 @@ const maxErrorBody = 4096
type Client struct {
BaseURL string
Token string
HTTP *http.Client
}
@@ -30,16 +32,25 @@ func New(baseURL string) *Client {
}
return &Client{
BaseURL: strings.TrimRight(baseURL, "/"),
Token: strings.TrimSpace(os.Getenv("ELY_EYE_AUTH_TOKEN")),
HTTP: &http.Client{Timeout: 30 * time.Second},
}
}
// authorize attaches the write token; the server refuses a write without it.
func (c *Client) authorize(request *http.Request) {
if c.Token != "" {
request.Header.Set("Authorization", "Bearer "+c.Token)
}
}
// Get fetches a JSON resource into out.
func (c *Client) Get(ctx context.Context, path string, out any) error {
request, err := http.NewRequestWithContext(ctx, http.MethodGet, c.BaseURL+path, nil)
if err != nil {
return fmt.Errorf("cannot build the request for %s: %w", path, err)
}
c.authorize(request)
return c.do(request, path, out)
}
@@ -78,6 +89,7 @@ func (c *Client) Post(ctx context.Context, path string, body any, out any) error
return fmt.Errorf("cannot build the request for %s: %w", path, err)
}
request.Header.Set("Content-Type", "application/json")
c.authorize(request)
return c.do(request, path, out)
}
@@ -77,6 +77,52 @@ func TestUnreachableAPIPointsAtTheServerThatShouldBeRunning(t *testing.T) {
}
}
func TestTheWriteTokenRidesOnEveryRequest(t *testing.T) {
seen := make(chan string, 2)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
seen <- r.Header.Get("Authorization")
_, _ = w.Write([]byte(`{}`))
}))
defer server.Close()
client := New(server.URL)
client.Token = "sekret"
if err := client.Post(context.Background(), "/api/memory", map[string]any{}, &struct{}{}); err != nil {
t.Fatalf("Post failed: %v", err)
}
if err := client.Get(context.Background(), "/api/status", &struct{}{}); err != nil {
t.Fatalf("Get failed: %v", err)
}
for _, path := range []string{"POST", "GET"} {
if got := <-seen; got != "Bearer sekret" {
t.Errorf("%s Authorization = %q, want the bearer token the server demands", path, got)
}
}
}
func TestNoTokenMeansNoHeader(t *testing.T) {
seen := make(chan string, 1)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
seen <- r.Header.Get("Authorization")
_, _ = w.Write([]byte(`{}`))
}))
defer server.Close()
if err := New(server.URL).Get(context.Background(), "/api/status", &struct{}{}); err != nil {
t.Fatalf("Get failed: %v", err)
}
if got := <-seen; got != "" {
t.Errorf("Authorization = %q, want no header when no token is configured", got)
}
}
func TestTheTokenComesFromTheEnvironment(t *testing.T) {
t.Setenv("ELY_EYE_AUTH_TOKEN", " sekret ")
if got := New("http://127.0.0.1:8765").Token; got != "sekret" {
t.Errorf("Token = %q, want the environment's value, trimmed", got)
}
}
func TestNewDefaultsToLocalServe(t *testing.T) {
if got := New("").BaseURL; got != "http://127.0.0.1:8765" {
t.Errorf("BaseURL = %q, want the default serve address", got)
+6 -4
View File
@@ -27,10 +27,12 @@ type ForgetReport struct {
AtomsDeleted int `json:"atoms_deleted"`
EdgesDeleted int `json:"edges_deleted"`
CacheEntriesCleared int `json:"cache_entries_cleared"`
VectorsDropped int `json:"vectors_dropped"`
VisualIndexDropped bool `json:"visual_index_dropped"`
AffectedCartridges []any `json:"affected_cartridges"`
Reason string `json:"reason"`
// SemanticCacheCleared counts compiled contexts deleted by the same forget.
SemanticCacheCleared int `json:"semantic_cache_cleared"`
VectorsDropped int `json:"vectors_dropped"`
VisualIndexDropped bool `json:"visual_index_dropped"`
AffectedCartridges []any `json:"affected_cartridges"`
Reason string `json:"reason"`
}
type CacheLayer struct {