mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-12-02 10:09:02 +01:00
No Router built by the internal/api tests was ever freed. At the end of a run
all 1,358 were still reachable, with 26,000 goroutines and 2.86 GiB live, so
peak memory grew with every new test until the release correctness job ran out
of memory on its 7 GiB runner.
Most test sites never stopped their Router's workers. The few that did were
pinned anyway, because the setup-token cleanup worker in ConfigHandlers had no
way to stop and holds callbacks back into the Router. That worker now joins
the Router's shutdown, and package tests build Routers through newTestRouter,
which stops them with the test. Two new tests fail if a Router stays reachable
after shutdown or a package test calls NewRouter directly.
Default command policies now share one compiled pattern set. A private copy
cost 230 KiB and 1.4 ms per call and is now 2.2 KiB and 1.6 us. It accounted
for a quarter of the live test heap. In production each organization's AI
service holds one, so this also saves about 230 KiB per organization.
Measured on pulse-dev at 88c2a21f65 with the same go test -count=1 -p 1 run:
peak RSS of the largest child 6.41 GiB before, 2.44 GiB after. That largest
child is now the compiler: a cold test build alone peaks at 2.34 GiB and the
test binary alone at 1.48 GiB. All package tests pass. CPU time fell about 5%;
wall clock followed the host's load and is not comparable between these runs.
Contract-Neutral: Stops test Router workers and shares compiled policy patterns; no API, policy decision or storage change.
60 lines
2.5 KiB
Go
60 lines
2.5 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/config"
|
|
)
|
|
|
|
// The attention prefix serves reads, lifecycle mutations and action planning.
|
|
// The pinned capability inventory says a lifecycle POST needs monitoring:write
|
|
// and a read needs monitoring:read, so a token must be admitted by the scope
|
|
// that matches what it is doing, not by the read scope for everything.
|
|
func TestAttentionRoutesGateEachOperationOnItsOwnScope(t *testing.T) {
|
|
const readToken = "attention-scope-read.12345678"
|
|
const writeToken = "attention-scope-write.12345678"
|
|
cfg := newTestConfigWithTokens(t,
|
|
newTokenRecord(t, readToken, []string{config.ScopeMonitoringRead}, nil),
|
|
newTokenRecord(t, writeToken, []string{config.ScopeMonitoringWrite}, nil),
|
|
)
|
|
router := newTestRouter(t, cfg, nil, nil, nil, nil, "1.0.0")
|
|
|
|
do := func(token, method, path string) *httptest.ResponseRecorder {
|
|
req := httptest.NewRequest(method, path, strings.NewReader(`{}`))
|
|
req.Header.Set("X-API-Token", token)
|
|
rec := httptest.NewRecorder()
|
|
router.Handler().ServeHTTP(rec, req)
|
|
return rec
|
|
}
|
|
refused := func(rec *httptest.ResponseRecorder) bool {
|
|
return rec.Code == http.StatusForbidden || rec.Code == http.StatusUnauthorized
|
|
}
|
|
|
|
const mutation = "/api/ai/patrol/attention/item-1/acknowledge"
|
|
if rec := do(writeToken, http.MethodPost, mutation); refused(rec) {
|
|
t.Errorf("monitoring:write token POST %s refused with %d: %s", mutation, rec.Code, rec.Body.String())
|
|
}
|
|
if rec := do(readToken, http.MethodPost, mutation); rec.Code != http.StatusForbidden {
|
|
t.Errorf("monitoring:read token POST %s = %d, want 403", mutation, rec.Code)
|
|
}
|
|
|
|
const detail = "/api/ai/patrol/attention/item-1"
|
|
if rec := do(readToken, http.MethodGet, detail); refused(rec) {
|
|
t.Errorf("monitoring:read token GET %s refused with %d: %s", detail, rec.Code, rec.Body.String())
|
|
}
|
|
if rec := do(writeToken, http.MethodGet, detail); rec.Code != http.StatusForbidden {
|
|
t.Errorf("monitoring:write token GET %s = %d, want 403 (write does not imply read)", detail, rec.Code)
|
|
}
|
|
|
|
// Action planning shares the prefix but is not a lifecycle mutation: it
|
|
// keeps the read-side outer gate plus its own authorization, so write
|
|
// scope alone must not admit it.
|
|
const plan = "/api/ai/patrol/attention/item-1/actions/restart/plan"
|
|
if rec := do(writeToken, http.MethodPost, plan); rec.Code != http.StatusForbidden {
|
|
t.Errorf("monitoring:write token POST %s = %d, want 403", plan, rec.Code)
|
|
}
|
|
}
|