// Package governance owns an organisation's settings. So far that is the // agent model: the provider class, base URL and model every agent loop uses. // Nothing an event or a tool result says can change where a model request // goes, because the client is built only from these settings. package governance import ( "context" "encoding/json/jsontext" json "encoding/json/v2" "errors" "fmt" "log/slog" "net/http" "net/url" "slices" "time" "github.com/jmoiron/sqlx" "github.com/samcharles93/ai-sdk/chat" "github.com/samcharles93/ai-sdk/runtime" "github.com/samcharles93/archie-core/internal/platform/httpjson" "github.com/samcharles93/archie-core/modules/governance/internal/store" ) // ErrNoAgentModel means the organisation hasn't chosen its agent model. There // is no built-in default. var ErrNoAgentModel = errors.New("no agent model is set for the organisation") const roleAgent = "agent" // DefaultApprovalDeadline is how long an approval request waits for an answer // when the organisation hasn't said. const DefaultApprovalDeadline = 24 * time.Hour // DefaultRaiseAfter is how many approvals in a row earn a proposal to raise an // action class when the organisation hasn't said. const DefaultRaiseAfter = 3 // maxRaiseAfter bounds the count: past it a class could never earn a raise. const maxRaiseAfter = 100 // DefaultRecurrenceThreshold is how many incidents with one cause on one target // it takes to propose a root-cause fix when the organisation hasn't said. const DefaultRecurrenceThreshold = 5 // The bounds on the recurrence threshold. One incident is no pattern; past a // hundred the history Archie keeps per target can't confirm it. const ( minRecurrenceThreshold = 2 maxRecurrenceThreshold = 100 ) // DefaultAttemptsPerRun is how many steps an agent may take in one run when the // organisation hasn't said. const DefaultAttemptsPerRun = 20 // The bounds on attempts per run. An agent needs a few steps to look anything // up; past two hundred a run is looping. const ( minAttemptsPerRun = 2 maxAttemptsPerRun = 200 ) // The defaults and bounds on the action budgets. const ( DefaultActionsPerRun = 10 DefaultActionsPerDay = 100 maxActionBudget = 10000 ) // The bounds on an approval deadline. Below a minute nobody could answer; past // a month the request outlives the incident it was raised for. const ( minApprovalDeadline = time.Minute maxApprovalDeadline = 30 * 24 * time.Hour ) // Settings is what the organisation decides about how Archie acts. type Settings struct { // ApprovalDeadline is how long an approval request waits for an answer // before the run gives up on it. ApprovalDeadline time.Duration // RaiseAfter is how many approvals in a row, since an action class was last // tightened, earn a proposal to raise it one level. RaiseAfter int // Frozen holds every action that changes something at propose and wait, // without changing any class's level. Frozen bool // RecurrenceThreshold is how many incidents with the same cause on one // target it takes, whatever their outcomes, to propose a root-cause fix. RecurrenceThreshold int // AttemptsPerRun is how many steps an agent may take in one run. AttemptsPerRun int // DefaultOwner is the id of the person who answers for what has no other // owner, or empty. DefaultOwner string // Escalation is the people an unanswered request moves through, in order, // each with how long the request waits on them. Escalation []Step // Timing is when changing actions may run without asking. Timing Timing // ActionsPerRun and ActionsPerDay are how many actions one run, and the // organisation in a day, may take before a changing action waits for approval. ActionsPerRun int ActionsPerDay int } // Step is one stop on the escalation path: a person and how long an unanswered // request waits on them before it moves to the next. type Step struct { // Person is the id of the person the request is passed to. Person string Deadline time.Duration } // stepRow is a step as stored. type stepRow struct { Person string `json:"person"` DeadlineMS int64 `json:"deadline_ms"` } // stepRequest is a step as the API shows and sets it. Its deadline travels as // a Go duration string, like the approval deadline. type stepRequest struct { Person string `json:"person"` Deadline string `json:"deadline"` } // settingsRequest is the settings as the API shows and sets them. A duration // travels as a Go duration string ("24h"), the same form every other duration // in the config takes. type settingsRequest struct { ApprovalDeadline string `json:"approval_deadline"` RaiseAfter int `json:"raise_after"` Frozen bool `json:"frozen"` RecurrenceThreshold int `json:"recurrence_threshold"` AttemptsPerRun int `json:"attempts_per_run"` DefaultOwner string `json:"default_owner"` Escalation []stepRequest `json:"escalation"` Timing Timing `json:"timing"` ActionsPerRun int `json:"actions_per_run"` ActionsPerDay int `json:"actions_per_day"` } func (s Settings) request() settingsRequest { return settingsRequest{ ApprovalDeadline: s.ApprovalDeadline.String(), RaiseAfter: s.RaiseAfter, Frozen: s.Frozen, RecurrenceThreshold: s.RecurrenceThreshold, AttemptsPerRun: s.AttemptsPerRun, DefaultOwner: s.DefaultOwner, Escalation: stepRequests(s.Escalation), Timing: s.Timing, ActionsPerRun: s.ActionsPerRun, ActionsPerDay: s.ActionsPerDay, } } func stepRequests(steps []Step) []stepRequest { out := make([]stepRequest, 0, len(steps)) for _, st := range steps { out = append(out, stepRequest{Person: st.Person, Deadline: st.Deadline.String()}) } return out } // stepsOf reads the steps of a request, each with a deadline inside the range // an answer can be given in. func stepsOf(req []stepRequest) ([]Step, error) { out := make([]Step, 0, len(req)) for _, r := range req { d, err := time.ParseDuration(r.Deadline) if err != nil { return nil, errors.New("each escalation deadline is a duration, such as 4h") } out = append(out, Step{Person: r.Person, Deadline: d}) } return out, nil } // Model is the model chosen for a role. type Model struct { Class string `json:"class"` BaseURL string `json:"base_url"` Model string `json:"model"` } // Provider builds the chat client for m. Only keyless providers work until // secret engines can supply a provider key. func (m Model) Provider(ctx context.Context) (chat.Provider, chat.ModelInfo, error) { rt := runtime.NewRuntime(runtime.Config{Providers: map[string]runtime.ProviderConfig{ roleAgent: {ID: roleAgent, Class: m.Class, BaseURL: m.BaseURL, Auth: runtime.AuthConfig{Type: runtime.AuthTypeNone}}, }}) ref := roleAgent + "/" + m.Model provider, _, err := rt.ChatProvider(ctx, ref) if err != nil { return nil, chat.ModelInfo{}, fmt.Errorf("agent model %s: %w", ref, err) } info, err := rt.ChatModelInfo(ref) if err != nil { return nil, chat.ModelInfo{}, fmt.Errorf("agent model %s: %w", ref, err) } return provider, info, nil } // Module serves one organisation's settings. type Module struct { orgID string store *store.Store log *slog.Logger } // NewModule returns the governance module for orgID. The ai-sdk provider // classes must already be registered. func NewModule(db *sqlx.DB, log *slog.Logger, orgID string) *Module { return &Module{orgID: orgID, store: store.New(db), log: log} } // AgentModel returns the organisation's agent model. func (m *Module) AgentModel(ctx context.Context) (Model, error) { role, ok, err := m.store.ModelRole(ctx, m.orgID, roleAgent) if err != nil { return Model{}, err } if !ok { return Model{}, ErrNoAgentModel } return Model{Class: role.Class, BaseURL: role.BaseURL, Model: role.Model}, nil } // Settings returns the organisation's settings, with a default for every one it // hasn't set. func (m *Module) Settings(ctx context.Context) (Settings, error) { row, ok, err := m.store.Settings(ctx, m.orgID) if err != nil { return Settings{}, err } if !ok { return Settings{ ApprovalDeadline: DefaultApprovalDeadline, RaiseAfter: DefaultRaiseAfter, RecurrenceThreshold: DefaultRecurrenceThreshold, AttemptsPerRun: DefaultAttemptsPerRun, Escalation: []Step{}, Timing: Timing{}.normalised(), ActionsPerRun: DefaultActionsPerRun, ActionsPerDay: DefaultActionsPerDay, }, nil } var rows []stepRow if err := json.Unmarshal(jsontext.Value(row.Escalation), &rows); err != nil { return Settings{}, fmt.Errorf("read escalation path: %w", err) } escalation := make([]Step, 0, len(rows)) for _, r := range rows { escalation = append(escalation, Step{Person: r.Person, Deadline: time.Duration(r.DeadlineMS) * time.Millisecond}) } var timing Timing if err := json.Unmarshal(jsontext.Value(row.Timing), &timing); err != nil { return Settings{}, fmt.Errorf("read timing: %w", err) } return Settings{ Timing: timing.normalised(), ActionsPerRun: row.ActionsPerRun, ActionsPerDay: row.ActionsPerDay, ApprovalDeadline: time.Duration(row.ApprovalDeadlineMS) * time.Millisecond, RaiseAfter: row.RaiseAfter, Frozen: row.Frozen, RecurrenceThreshold: row.RecurrenceThreshold, AttemptsPerRun: row.AttemptsPerRun, DefaultOwner: row.DefaultOwner, Escalation: escalation, }, nil } // RaiseAfter is how many approvals in a row earn a proposal to raise an action // class. func (m *Module) RaiseAfter(ctx context.Context) (int, error) { set, err := m.Settings(ctx) if err != nil { return 0, err } return set.RaiseAfter, nil } // Frozen reports whether the organisation has frozen its actions. func (m *Module) Frozen(ctx context.Context) (bool, error) { set, err := m.Settings(ctx) if err != nil { return false, err } return set.Frozen, nil } // RecurrenceThreshold is how many incidents with one cause on one target it // takes to propose a root-cause fix. func (m *Module) RecurrenceThreshold(ctx context.Context) (int, error) { set, err := m.Settings(ctx) if err != nil { return 0, err } return set.RecurrenceThreshold, nil } // AttemptsPerRun is how many steps an agent may take in one run. func (m *Module) AttemptsPerRun(ctx context.Context) (int, error) { set, err := m.Settings(ctx) if err != nil { return 0, err } return set.AttemptsPerRun, nil } // ApprovalDeadline is how long an approval request waits for an answer. func (m *Module) ApprovalDeadline(ctx context.Context) (time.Duration, error) { set, err := m.Settings(ctx) if err != nil { return 0, err } return set.ApprovalDeadline, nil } // Routes adds the settings API to mux behind guard. func (m *Module) Routes(mux *http.ServeMux, guard func(http.Handler) http.Handler) { mux.Handle("GET /api/settings/agent-model", guard(http.HandlerFunc(m.getAgentModel))) mux.Handle("PUT /api/settings/agent-model", guard(http.HandlerFunc(m.putAgentModel))) mux.Handle("GET /api/settings", guard(http.HandlerFunc(m.getSettings))) mux.Handle("PUT /api/settings", guard(http.HandlerFunc(m.putSettings))) mux.Handle("GET /api/people", guard(http.HandlerFunc(m.getPeople))) mux.Handle("PUT /api/people", guard(http.HandlerFunc(m.putPeople))) } func (m *Module) getSettings(w http.ResponseWriter, r *http.Request) { set, err := m.Settings(r.Context()) if err != nil { m.log.ErrorContext(r.Context(), "read settings", "error", err) httpjson.Error(w, http.StatusInternalServerError, "couldn't read the settings") return } httpjson.Write(w, http.StatusOK, set.request()) } func (m *Module) putSettings(w http.ResponseWriter, r *http.Request) { var req settingsRequest if err := httpjson.Read(r, &req); err != nil { httpjson.Error(w, http.StatusBadRequest, err.Error()) return } deadline, err := time.ParseDuration(req.ApprovalDeadline) if err != nil { httpjson.Error(w, http.StatusBadRequest, "approval deadline is a duration, such as 24h") return } steps, err := stepsOf(req.Escalation) if err != nil { httpjson.Error(w, http.StatusBadRequest, err.Error()) return } set := Settings{ ApprovalDeadline: deadline, RaiseAfter: req.RaiseAfter, Frozen: req.Frozen, RecurrenceThreshold: req.RecurrenceThreshold, AttemptsPerRun: req.AttemptsPerRun, DefaultOwner: req.DefaultOwner, Escalation: steps, Timing: req.Timing.normalised(), ActionsPerRun: req.ActionsPerRun, ActionsPerDay: req.ActionsPerDay, } if err := set.validate(); err != nil { httpjson.Error(w, http.StatusBadRequest, err.Error()) return } people, err := m.store.People(r.Context(), m.orgID) if err != nil { m.log.ErrorContext(r.Context(), "read people", "error", err) httpjson.Error(w, http.StatusInternalServerError, "couldn't save the settings") return } if err := set.knows(people); err != nil { httpjson.Error(w, http.StatusBadRequest, err.Error()) return } rows := make([]stepRow, 0, len(set.Escalation)) for _, st := range set.Escalation { rows = append(rows, stepRow{Person: st.Person, DeadlineMS: st.Deadline.Milliseconds()}) } escalation, err := json.Marshal(rows) if err != nil { httpjson.Error(w, http.StatusInternalServerError, "couldn't save the settings") return } timing, err := json.Marshal(set.Timing) if err != nil { httpjson.Error(w, http.StatusInternalServerError, "couldn't save the settings") return } if err := m.store.PutSettings(r.Context(), store.Settings{ OrgID: m.orgID, ApprovalDeadlineMS: set.ApprovalDeadline.Milliseconds(), RaiseAfter: set.RaiseAfter, Frozen: set.Frozen, RecurrenceThreshold: set.RecurrenceThreshold, AttemptsPerRun: set.AttemptsPerRun, DefaultOwner: set.DefaultOwner, Escalation: string(escalation), Timing: string(timing), ActionsPerRun: set.ActionsPerRun, ActionsPerDay: set.ActionsPerDay, }); err != nil { m.log.ErrorContext(r.Context(), "put settings", "error", err) httpjson.Error(w, http.StatusInternalServerError, "couldn't save the settings") return } httpjson.Write(w, http.StatusOK, set.request()) } // validate keeps a deadline inside the range an answer can be given in, the // count of approvals a raise needs inside the range a class can earn, and the // recurrence threshold inside the range that means a pattern. func (s Settings) validate() error { if s.RecurrenceThreshold < minRecurrenceThreshold || s.RecurrenceThreshold > maxRecurrenceThreshold { return fmt.Errorf("recurrence threshold must be between %d and %d", minRecurrenceThreshold, maxRecurrenceThreshold) } if s.ApprovalDeadline < minApprovalDeadline || s.ApprovalDeadline > maxApprovalDeadline { return fmt.Errorf("approval deadline must be between %s and %s", minApprovalDeadline, maxApprovalDeadline) } if s.RaiseAfter < 1 || s.RaiseAfter > maxRaiseAfter { return fmt.Errorf("raise after must be between 1 and %d approvals", maxRaiseAfter) } if s.AttemptsPerRun < minAttemptsPerRun || s.AttemptsPerRun > maxAttemptsPerRun { return fmt.Errorf("attempts per run must be between %d and %d", minAttemptsPerRun, maxAttemptsPerRun) } if err := validDeadlines(s.Escalation); err != nil { return err } if s.ActionsPerRun < 1 || s.ActionsPerRun > maxActionBudget || s.ActionsPerDay < s.ActionsPerRun || s.ActionsPerDay > maxActionBudget { return fmt.Errorf("actions per run must be at least 1, and actions per day at least that and at most %d", maxActionBudget) } return s.Timing.validate() } // validDeadlines keeps each escalation step's deadline inside the range the // approval deadline takes. func validDeadlines(path []Step) error { for _, st := range path { if st.Deadline < minApprovalDeadline || st.Deadline > maxApprovalDeadline { return fmt.Errorf("each escalation deadline must be between %s and %s", minApprovalDeadline, maxApprovalDeadline) } } return nil } // knows checks that the default owner and every person on the escalation path // is one of people, and that no one is on the path twice. func (s Settings) knows(people []store.Person) error { known := func(id string) bool { return slices.ContainsFunc(people, func(p store.Person) bool { return p.ID.String() == id }) } if s.DefaultOwner != "" && !known(s.DefaultOwner) { return errors.New("the default owner must be one of the people") } seen := map[string]bool{} for _, st := range s.Escalation { if !known(st.Person) { return errors.New("everyone on the escalation path must be one of the people") } if seen[st.Person] { return errors.New("a person can be on the escalation path once") } seen[st.Person] = true } return nil } func (m *Module) getAgentModel(w http.ResponseWriter, r *http.Request) { model, err := m.AgentModel(r.Context()) switch { case errors.Is(err, ErrNoAgentModel): httpjson.Error(w, http.StatusNotFound, err.Error()) case err != nil: m.log.ErrorContext(r.Context(), "read agent model", "error", err) httpjson.Error(w, http.StatusInternalServerError, "couldn't read the agent model") default: httpjson.Write(w, http.StatusOK, model) } } // putAgentModel sets the agent model. An empty base URL takes the class's // published default, and the stored value is always explicit. func (m *Module) putAgentModel(w http.ResponseWriter, r *http.Request) { var req Model if err := httpjson.Read(r, &req); err != nil { httpjson.Error(w, http.StatusBadRequest, err.Error()) return } model, err := settle(req) if err != nil { httpjson.Error(w, http.StatusBadRequest, err.Error()) return } if _, _, err := model.Provider(r.Context()); err != nil { httpjson.Error(w, http.StatusBadRequest, err.Error()) return } if err := m.store.PutModelRole(r.Context(), store.ModelRole{ OrgID: m.orgID, Role: roleAgent, Class: model.Class, BaseURL: model.BaseURL, Model: model.Model, }); err != nil { m.log.ErrorContext(r.Context(), "put agent model", "error", err) httpjson.Error(w, http.StatusInternalServerError, "couldn't save the agent model") return } httpjson.Write(w, http.StatusOK, model) } func settle(req Model) (Model, error) { if _, ok := runtime.GetClass(req.Class); !ok { return Model{}, fmt.Errorf("unknown provider class %q", req.Class) } if req.Model == "" { return Model{}, errors.New("a model is required") } if req.BaseURL == "" { base, ok := runtime.DefaultBaseURL(req.Class) if !ok { return Model{}, fmt.Errorf("provider class %q has no default base URL; give one", req.Class) } req.BaseURL = base } u, err := url.Parse(req.BaseURL) if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" { return Model{}, fmt.Errorf("base URL %q must be an http or https URL", req.BaseURL) } return req, nil }