// observerd is the autonomous-iteration witness service. Port of // the load-bearing pieces of mcp-server/observer.ts (Rust system). // // Routes (all under /observer): // GET /observer/health — service liveness + ring size // GET /observer/stats — aggregate counters + recent scenarios // POST /observer/event — record one observed op // // Deferred to follow-up commits (see internal/observer doc): // - POST /observer/review (cloud-LLM hand review fall-back) // - background loops (analyzeErrors, consolidatePlaybooks, // tailOverseerCorrections) // - failure-cluster escalation to LLM Team // // /relevance was already ported to internal/matrix in 9588bd8 and is // not duplicated here. package main import ( "context" "encoding/json" "errors" "flag" "fmt" "log/slog" "net/http" "os" "strings" "time" "github.com/go-chi/chi/v5" "git.agentview.dev/profit/golangLAKEHOUSE/internal/observer" "git.agentview.dev/profit/golangLAKEHOUSE/internal/shared" "git.agentview.dev/profit/golangLAKEHOUSE/internal/workflow" ) 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) } // Persistence is optional — empty path = ephemeral (matches the // pathwayd pattern). Production sets a stable path under // /var/lib/lakehouse/observer/ops.jsonl. var persistor *observer.Persistor if cfg.Observerd.PersistPath != "" { persistor, err = observer.NewPersistor(cfg.Observerd.PersistPath) if err != nil { slog.Error("observer persistor", "err", err) os.Exit(1) } } store := observer.NewStore(persistor) if persistor != nil { n, err := store.Load() if err != nil { slog.Warn("observer load", "err", err, "loaded", n) } else { slog.Info("observer loaded", "ops", n, "path", cfg.Observerd.PersistPath) } } runner := workflow.NewRunner() registerBuiltinModes(runner, store) h := &handlers{store: store, runner: runner} if err := shared.Run("observerd", cfg.Observerd.Bind, h.register, cfg.Auth); err != nil { slog.Error("server", "err", err) os.Exit(1) } } type handlers struct { store *observer.Store runner *workflow.Runner } func (h *handlers) register(r chi.Router) { r.Get("/observer/stats", h.handleStats) r.Post("/observer/event", h.handleEvent) r.Post("/observer/workflow/run", h.handleWorkflowRun) r.Get("/observer/workflow/modes", h.handleWorkflowModes) } func (h *handlers) handleStats(w http.ResponseWriter, _ *http.Request) { writeJSON(w, http.StatusOK, h.store.Stats()) } func (h *handlers) handleEvent(w http.ResponseWriter, r *http.Request) { var op observer.ObservedOp if !decodeJSON(w, r, &op) { return } if err := h.store.Record(op); err != nil { if errors.Is(err, observer.ErrInvalidOp) { http.Error(w, err.Error(), http.StatusBadRequest) return } slog.Error("observer record", "err", err) http.Error(w, "internal", http.StatusInternalServerError) return } stats := h.store.Stats() writeJSON(w, http.StatusOK, map[string]any{ "accepted": true, "ring_size": stats.Total, }) } // workflowRunRequest is the POST /observer/workflow/run body — a // Workflow definition in JSON form (matches Archon's YAML shape but // JSON-serialized for the HTTP path). type workflowRunRequest struct { Workflow workflow.Workflow `json:"workflow"` } func (h *handlers) handleWorkflowRun(r http.ResponseWriter, req *http.Request) { var body workflowRunRequest if !decodeJSON(r, req, &body) { return } res, err := h.runner.Run(req.Context(), body.Workflow) // Record per-node provenance into the observer ring AS the // workflow runs — same shape as any other ObservedOp so the // existing /observer/stats aggregation surfaces workflow ops // alongside scenario ops without a schema change. for _, n := range res.Nodes { op := observer.ObservedOp{ Endpoint: "/observer/workflow/run/" + body.Workflow.Name + "/" + n.NodeID, InputSummary: fmt.Sprintf("workflow=%s node=%s mode=%s", body.Workflow.Name, n.NodeID, n.Mode), Success: n.Error == "", DurationMs: n.DurationMs, OutputSummary: summarizeOutput(n.Output), Source: observer.Source("workflow"), Error: n.Error, Timestamp: n.StartedAt.UTC().Format(time.RFC3339Nano), } if recErr := h.store.Record(op); recErr != nil { slog.Warn("workflow run: provenance record failed", "err", recErr) } } if err != nil { // Aborting errors (cycle, missing dep, unknown mode) — surface // as 4xx because the workflow definition itself is wrong. slog.Warn("workflow run aborted", "err", err) writeJSON(r, http.StatusBadRequest, map[string]any{ "error": err.Error(), "result": res, }) return } writeJSON(r, http.StatusOK, res) } func (h *handlers) handleWorkflowModes(w http.ResponseWriter, _ *http.Request) { modes := h.runner.Modes() writeJSON(w, http.StatusOK, map[string]any{ "modes": modes, "count": len(modes), }) } // summarizeOutput renders a workflow node's output map for the // ObservedOp's OutputSummary string. Best-effort — long values get // truncated rather than ballooning the ring buffer's memory. func summarizeOutput(output map[string]any) string { if output == nil { return "(nil)" } bs, err := json.Marshal(output) if err != nil { return fmt.Sprintf("(marshal err: %v)", err) } if len(bs) > 256 { return string(bs[:256]) + "...(truncated)" } return string(bs) } // registerBuiltinModes wires the modes the runner knows about. v0 // ships with fixture.echo + fixture.upper for testing the runner // mechanics; real-mode integrations (matrix.search, distillation. // score, drift.scorer, llm.chat) land in follow-up commits. // // Each mode's signature matches workflow.Mode. The store parameter // is reserved for modes that need to record their own ObservedOps // (most don't — the runner records per-node provenance generically). func registerBuiltinModes(r *workflow.Runner, _ *observer.Store) { r.RegisterMode("fixture.echo", func(_ workflow.Context, input map[string]any) (map[string]any, error) { // Verbatim copy of input → output. Useful for ref-substitution // chains in smokes. out := make(map[string]any, len(input)) for k, v := range input { out[k] = v } return out, nil }) r.RegisterMode("fixture.upper", func(_ workflow.Context, input map[string]any) (map[string]any, error) { // Returns {"upper": strings.ToUpper(input["prompt"])}. Toy // mode for proving DAG ref substitution end-to-end. prompt, _ := input["prompt"].(string) return map[string]any{"upper": strings.ToUpper(prompt)}, nil }) } // stub to silence "imported and not used" until a real mode uses it var _ = context.Background 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("observer write json", "err", err) } }