security: add the capability and policy layer as the only path to action

Phase 12 (ADR-0005). Capabilities are registered in code with parameter
schemas, risk classes and default effects; sudo_shell and delete_database
are locked to DENY and cannot be relaxed. Policies are scoped globally or
per mind with capability, target glob, action kind and optional hour
window; evaluation is deterministic, most specific wins, and ties go to
the stricter effect. Invoke is the single entry point: it refuses
dreaming and replay contexts, validates parameters (unknown keys, non
http URLs and path traversal rejected), evaluates policy and session
grants, hashes the exact request and records a policy_decision event.
ALLOW executes with timeout and panic isolation and turns textual output
into a tool-trust observation; ASK creates an approval request bound to
the hash with an alert notification; DENY is counted and recorded.

Operators approve once, approve for a time-boxed session grant, deny
with a note, or modify parameters (re-validated, re-hashed, original
kept in the append-only decision log). The self-model's capability list
is recomputed from effective policy on start and on every policy change.
The act process maps fetch requests to http.fetch and the action handler
invokes through the layer; replies report capabilities and outcomes.

Policy matrix and approval centre pages, dashboard count, API, CLI
(approvals, policy, invoke) and docs/capabilities.md.
This commit is contained in:
Andy Dixon
2026-06-29 20:28:05 +01:00
parent 99a89ba847
commit b35dbe0299
29 changed files with 2318 additions and 23 deletions
+134
View File
@@ -0,0 +1,134 @@
package ui
import (
"encoding/json"
"net/http"
"strconv"
"strings"
"github.com/andydixon/noema/internal/auth"
"github.com/andydixon/noema/internal/capabilities"
"github.com/andydixon/noema/pkg/ids"
)
func (u *UI) capabilityRoutes(m *http.ServeMux) {
m.Handle("GET /policies", u.require(auth.RoleObserver, u.policiesPage))
m.Handle("POST /policies", u.require(auth.RoleAdministrator, u.policyCreate))
m.Handle("POST /policies/{id}/delete", u.require(auth.RoleAdministrator, u.policyDelete))
m.Handle("POST /policies/grants/{id}/revoke", u.require(auth.RoleOperator, u.grantRevoke))
m.Handle("GET /approvals", u.require(auth.RoleObserver, u.approvalsPage))
m.Handle("POST /approvals/{id}/decide", u.require(auth.RoleOperator, u.approvalDecide))
}
func (u *UI) mindNames(r *http.Request) (map[ids.ID]string, []any) {
list, _ := u.deps.Minds.List(r.Context(), true)
names := map[ids.ID]string{}
var live []any
for _, m := range list {
names[m.ID] = m.DisplayName
if m.Status == "active" {
live = append(live, m)
}
}
return names, live
}
func (u *UI) policiesPage(w http.ResponseWriter, r *http.Request) {
names, _ := u.mindNames(r)
mindsList, _ := u.deps.Minds.List(r.Context(), false)
caps := u.deps.Capabilities.Registry.List()
matrix := map[ids.ID]map[string]capabilities.InventoryItem{}
for _, m := range mindsList {
inv, _ := u.deps.Capabilities.Inventory(r.Context(), m.ID)
row := map[string]capabilities.InventoryItem{}
for _, i := range inv {
row[i.Capability.Name] = i
}
matrix[m.ID] = row
}
pols, _ := u.deps.Capabilities.Policies(r.Context(), ids.Nil)
grants, _ := u.deps.Capabilities.Grants(r.Context(), ids.Nil)
u.render(w, r, "policies", "Capabilities and policies", map[string]any{"Capabilities": caps, "Minds": mindsList, "MindNames": names, "Matrix": matrix, "Policies": pols, "Grants": grants})
}
func (u *UI) policyCreate(w http.ResponseWriter, r *http.Request) {
r.ParseForm() //nolint:errcheck
p := capabilities.Policy{Capability: r.PostFormValue("capability"), TargetPattern: r.PostFormValue("target_pattern"), Effect: capabilities.Effect(r.PostFormValue("effect")), Reason: r.PostFormValue("reason")}
if v := r.PostFormValue("mind_id"); v != "" {
p.MindID, _ = ids.Parse(v)
}
if hf, err := strconv.Atoi(r.PostFormValue("hours_from")); err == nil {
if ht, err := strconv.Atoi(r.PostFormValue("hours_to")); err == nil {
p.HoursFrom, p.HoursTo = &hf, &ht
}
}
if _, err := u.policyF(r, p); err != nil {
u.flash(w, r, "error", err.Error(), "/policies")
return
}
u.flash(w, r, "ok", "Rule added.", "/policies")
}
func (u *UI) policyDelete(w http.ResponseWriter, r *http.Request) {
id, err := ids.Parse(r.PathValue("id"))
if err != nil || u.deps.Capabilities.DeletePolicy(r.Context(), id) != nil {
u.flash(w, r, "error", "Rule not found.", "/policies")
return
}
u.deps.Audit.Log(r.Context(), "policy.delete", "policy", id.String(), nil)
u.flash(w, r, "ok", "Rule deleted.", "/policies")
}
func (u *UI) grantRevoke(w http.ResponseWriter, r *http.Request) {
id, err := ids.Parse(r.PathValue("id"))
if err != nil || u.deps.Capabilities.RevokeGrant(r.Context(), id) != nil {
u.flash(w, r, "error", "Grant not found.", "/policies")
return
}
u.deps.Audit.Log(r.Context(), "grant.revoke", "grant", id.String(), nil)
u.flash(w, r, "ok", "Session grant revoked.", "/policies")
}
func (u *UI) approvalsPage(w http.ResponseWriter, r *http.Request) {
names, _ := u.mindNames(r)
pending, _ := u.deps.Capabilities.Approvals(r.Context(), ids.Nil, "pending", 100)
all, _ := u.deps.Capabilities.Approvals(r.Context(), ids.Nil, "", 60)
var decided []capabilities.Approval
for _, a := range all {
if a.Status != "pending" {
decided = append(decided, a)
}
}
execs, _ := u.deps.Capabilities.Executions(r.Context(), ids.Nil, 30)
u.render(w, r, "approvals", "Approval centre", map[string]any{"Pending": pending, "Decided": decided, "Executions": execs, "MindNames": names})
}
func (u *UI) approvalDecide(w http.ResponseWriter, r *http.Request) {
id, err := ids.Parse(r.PathValue("id"))
if err != nil {
u.flash(w, r, "error", "Bad request id.", "/approvals")
return
}
r.ParseForm() //nolint:errcheck
var params map[string]any
if raw := strings.TrimSpace(r.PostFormValue("params")); raw != "" {
if err := json.Unmarshal([]byte(raw), &params); err != nil {
u.flash(w, r, "error", "Parameters are not a JSON object.", "/approvals")
return
}
// Only treat as a modification when it differs from the original.
if a, err := u.deps.Capabilities.GetApproval(r.Context(), id); err == nil {
orig, _ := json.Marshal(a.Params)
now, _ := json.Marshal(params)
if string(orig) == string(now) {
params = nil
}
}
}
decision := r.PostFormValue("decision")
if _, err := u.decideF(r, id, decision, r.PostFormValue("note"), params); err != nil {
u.flash(w, r, "error", err.Error(), "/approvals")
return
}
u.flash(w, r, "ok", "Decision recorded: "+decision+".", "/approvals")
}
+7 -1
View File
@@ -104,7 +104,13 @@ func (u *UI) dashboard(w http.ResponseWriter, r *http.Request) {
names[m.ID] = m.DisplayName
}
evs, _ := u.deps.Events.Query(r.Context(), evstore.Filter{MinSalience: 0.6, Limit: 12})
u.render(w, r, "dashboard", "Dashboard", map[string]any{"Minds": mindsList, "MindNames": names, "Events": evs, "Status": u.statusF(r), "Audit": entries})
pending := 0
if u.deps.Capabilities != nil {
if list, err := u.deps.Capabilities.Approvals(r.Context(), ids.Nil, "pending", 500); err == nil {
pending = len(list)
}
}
u.render(w, r, "dashboard", "Dashboard", map[string]any{"Minds": mindsList, "MindNames": names, "Events": evs, "Status": u.statusF(r), "Audit": entries, "Pending": pending})
}
func (u *UI) system(w http.ResponseWriter, r *http.Request) {
+12 -4
View File
@@ -21,6 +21,7 @@ import (
"github.com/andydixon/noema/internal/attention"
"github.com/andydixon/noema/internal/auth"
"github.com/andydixon/noema/internal/beliefs"
"github.com/andydixon/noema/internal/capabilities"
"github.com/andydixon/noema/internal/consolidation"
"github.com/andydixon/noema/internal/conversation"
"github.com/andydixon/noema/internal/goals"
@@ -58,6 +59,8 @@ type UI struct {
entF func(r *http.Request, mind, id ids.ID) (api.EntityDetail, error)
mergeF func(r *http.Request, mind ids.ID, e graph.Entity, other string) error
distinctF func(r *http.Request, mind ids.ID, e graph.Entity, other string) error
policyF func(r *http.Request, p capabilities.Policy) (capabilities.Policy, error)
decideF func(r *http.Request, id ids.ID, decision, note string, params map[string]any) (any, error)
}
// Actions groups the server-side operations the UI delegates to the API layer
@@ -80,6 +83,8 @@ type Actions struct {
Entity func(r *http.Request, mind, id ids.ID) (api.EntityDetail, error)
Merge func(r *http.Request, mind ids.ID, e graph.Entity, other string) error
Distinct func(r *http.Request, mind ids.ID, e graph.Entity, other string) error
Policy func(r *http.Request, p capabilities.Policy) (capabilities.Policy, error)
Decide func(r *http.Request, id ids.ID, decision, note string, params map[string]any) (any, error)
}
// View is the data passed to every template.
@@ -109,7 +114,7 @@ func New(deps app.Deps, a Actions) (*UI, error) {
deps.Log.Info("ui: serving templates and static files from ./web (development)")
}
}
u := &UI{deps: deps, static: staticFS, statusF: a.Status, ctxF: a.Context, wsF: a.Workspace, memF: a.Memory, belF: a.Belief, emitF: a.Emitter, postF: a.Post, goalF: a.Goal, goalUpdF: a.GoalUpdate, predF: a.Predict, resolveF: a.Resolve, resetF: a.ResetState, consolF: a.Consolidate, reflF: a.Reflect, entF: a.Entity, mergeF: a.Merge, distinctF: a.Distinct}
u := &UI{deps: deps, static: staticFS, statusF: a.Status, ctxF: a.Context, wsF: a.Workspace, memF: a.Memory, belF: a.Belief, emitF: a.Emitter, postF: a.Post, goalF: a.Goal, goalUpdF: a.GoalUpdate, predF: a.Predict, resolveF: a.Resolve, resetF: a.ResetState, consolF: a.Consolidate, reflF: a.Reflect, entF: a.Entity, mergeF: a.Merge, distinctF: a.Distinct, policyF: a.Policy, decideF: a.Decide}
if err := u.parse(tmplFS); err != nil {
return nil, err
}
@@ -164,7 +169,11 @@ func (u *UI) parse(fsys fs.FS) error {
"llmRoles": func() []llm.RoleInfo { return llm.Roles },
"pretty": prettyRaw,
"entityKinds": func() []graph.Kind { return graph.AllKinds },
"join": strings.Join,
"jsonOf": func(v any) json.RawMessage {
b, _ := json.Marshal(v)
return b
},
"join": strings.Join,
"outputs": func(raw json.RawMessage) []consolidation.Output {
var out []consolidation.Output
json.Unmarshal(raw, &out) //nolint:errcheck
@@ -369,11 +378,10 @@ func (u *UI) Register(m *http.ServeMux) {
u.motivationRoutes(m)
u.cognitionRoutes(m)
u.graphRoutes(m)
u.capabilityRoutes(m)
for _, p := range []struct{ path, title, note string }{
{"/approvals", "Approvals", "The approval centre arrives with the capability system."},
{"/integrations", "Integrations", "Integrations arrive in phase 13."},
{"/policies", "Policies", "The policy matrix arrives in phase 12."},
} {
m.Handle("GET "+p.path, u.require(auth.RoleObserver, func(w http.ResponseWriter, r *http.Request) { u.render(w, r, "placeholder", p.title, p.note) }))
}