feat(advisor): P1 outlets — findings push to SSE + MCP scheduler_advisor_findings

The advisor's findings previously lived only in its JSONL decision log on
its own host. P1 adds two observe-only outlets so diagnoses reach humans
and other agents live:

apiserver side:
- backend/broker.go: EventAdvisorFinding ("advisor.finding") event type +
  AdvisorFinding struct + Event.AdvisorFindings field.
- controllers/events.go: ReportFindings — POST /xce/v1/advisor/findings
  (generated_at/scenario/findings), publishes one broker event. Broadcast
  only: the decision log remains the store of record.
- router/routers.go: route behind RequirePermission("advisor:report").
- middleware/whitelist.go: admit /xce/v1/advisor/findings in the POST URL
  whitelist (the URL middleware 403s before the router's RBAC otherwise —
  same shape as bug-1489).
- config/rbac.yaml: advisor_agent role (advisor:report + the six *:view
  grants the snapshot needs) and advisor:report in the permission
  whitelist. mcp_readonly stays strictly *:view. New
  TestRBACYAML_AdvisorAgentRole_PinsReportSurface pins the role surface.

advisor side:
- internal/push: Pusher — best-effort POST of each round's findings, one
  retry, nil/unconfigured/empty findings are no-ops; failures log and
  never affect the observe loop.
- internal/agent: SetPusher wired after the decision-log append (the log
  is authoritative; push is a fan-out).
- internal/config + cmd: push default-on to the same XCE_API_URL with the
  same XCE_API_KEY (the key's role needs advisor:report); ADVISOR_PUSH=
  off|false|0 disables.

mcp-server side:
- internal/advisor.go: lenient JSONL reader for the advisor's decision
  log (ADVISOR_LOG env), tail-bounded, corrupt lines skipped.
- scheduler_advisor_findings tool: recent records with parsed findings;
  friendly errors when the log is missing/unconfigured. Passive reader —
  never talks to the advisor process.

Cluster verification (t1/t2/t3): redeployed apiserver closure, deployed
rbac.yaml, issued advisor_agent key. Forced-PEND job → pend_stall fired
on the real LLM (glm-5-3-flash, 19.4s, degraded=false) → 2 findings, all
evidence verbatim in the snapshot, unverified=false → SSE broadcast
captured on /xce/v1/events → MCP tool returned both records. Second PEND
job joining the stalled set fired a new decision within one round
(change-triggered semantics). Verification jobs killed; sanity job
submitted→ran→DONE; all daemons and 3 hosts healthy.

Design doc: architecture diagram gains the push branch; roadmap P1
marked done (CLI outlet not built).

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
lei.chen 2026-10-19 10:06:32 +00:00
parent 3e3b6d7947
commit 1691e49a05
17 changed files with 677 additions and 9 deletions

View file

@ -0,0 +1,99 @@
package push
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"advisor/internal/provider"
)
func sampleFindings() []provider.Finding {
return []provider.Finding{
{Kind: "diagnosis", Severity: provider.SevDegraded, Summary: "1 job stalled",
Evidence: []string{"73937053183639552", "unknown(0)"}, Confidence: 1.0},
}
}
func TestReport_PostsShapeAndAuth(t *testing.T) {
var gotAuth, gotPath string
var body map[string]any
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotAuth = r.Header.Get("Authorization")
gotPath = r.URL.Path
_ = json.NewDecoder(r.Body).Decode(&body)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"code":200,"msg":"Success","data":{"published":1}}`))
}))
defer srv.Close()
p := New(srv.URL, "xce_ak_test")
if err := p.Report(context.Background(), "pend_stall", "2026-10-19T08:00:00Z", sampleFindings()); err != nil {
t.Fatalf("Report: %v", err)
}
if gotPath != "/xce/v1/advisor/findings" {
t.Errorf("path = %q", gotPath)
}
if gotAuth != "xce_ak_test" {
t.Errorf("auth = %q", gotAuth)
}
if body["scenario"] != "pend_stall" {
t.Errorf("scenario = %v", body["scenario"])
}
fs, ok := body["findings"].([]any)
if !ok || len(fs) != 1 {
t.Fatalf("findings = %v", body["findings"])
}
}
func TestReport_RetriesOnceThenSucceeds(t *testing.T) {
var calls atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if calls.Add(1) == 1 {
w.WriteHeader(http.StatusBadGateway)
return
}
_, _ = w.Write([]byte(`{"code":200,"msg":"Success","data":{}}`))
}))
defer srv.Close()
p := New(srv.URL, "tok")
if err := p.Report(context.Background(), "pend_stall", "now", sampleFindings()); err != nil {
t.Fatalf("retry after 502 should succeed: %v", err)
}
if got := calls.Load(); got != 2 {
t.Fatalf("calls = %d, want 2", got)
}
}
func TestReport_GivesUpAfterTwoFailures(t *testing.T) {
var calls atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls.Add(1)
w.WriteHeader(http.StatusServiceUnavailable)
}))
defer srv.Close()
p := New(srv.URL, "tok")
if err := p.Report(context.Background(), "pend_stall", "now", sampleFindings()); err == nil {
t.Fatal("expected error after both attempts failed")
}
if got := calls.Load(); got != 2 {
t.Fatalf("calls = %d, want exactly 2 (one retry, no loop)", got)
}
}
func TestReport_EmptyFindingsAndUnconfiguredAreNoOps(t *testing.T) {
// nil pusher and empty findings must not touch the network.
p := New("", "tok")
if err := p.Report(context.Background(), "s", "now", nil); err != nil {
t.Fatalf("empty findings: %v", err)
}
var q *Pusher
if err := q.Report(context.Background(), "s", "now", sampleFindings()); err != nil {
t.Fatalf("nil pusher: %v", err)
}
}