feat(governance): serve the organisation's instructions to every agent (#2023)

The organisation's standing instructions are stored per organisation and
join the system prompt of chat turns and workflow agent steps. The Org
configuration group is served: schedules, alert webhook, playbooks,
models, connections and credential bindings open the page that already
owns the concept.
This commit is contained in:
sam committed 2026-11-22 11:24:25 +11:00
1 parent 34c97ce8e8
commit eadb88d3bf
25 files changed
+280 -989

No files matched your search

+2
View File
@@ -30,6 +30,8 @@ import (
// Settings provides the agent model and the escalation path approvals follow.
type Settings interface {
AgentModelForAlias(ctx context.Context, alias string) (governance.Model, error)
// Instructions are the organisation's standing instructions for agents.
Instructions(ctx context.Context) (string, error)
AgentProvider(ctx context.Context, model governance.Model) (chat.Provider, chat.ModelInfo, error)
EscalationPath(ctx context.Context, owner string, needAuthority bool) ([]governance.PathStep, error)
}
+5 -1
View File
@@ -194,9 +194,13 @@ func (m *Module) generate(ctx context.Context, t store.Turn) (string, string, er
if err := m.allowed(ctx, d, governance.ActionRead, governance.KindMemory, ""); err == nil {
memory = m.memoryBlock(ctx, d, t.Owner)
}
instructions, err := d.Settings.Instructions(ctx)
if err != nil {
return "", "", fmt.Errorf("read the organisation's instructions: %w", err)
}
system := prompts.ChatSystem(prompts.ChatContext{
Tools: toolSummaries(tools), Channel: "dashboard", Page: page(t), Model: model.Model,
SessionID: t.SessionID.String(), Memory: memory, Now: time.Now(),
SessionID: t.SessionID.String(), Memory: memory, Instructions: instructions, Now: time.Now(),
})
stream, err := core.StreamText(ctx, guardedProvider{Provider: provider, m: m, d: d, t: t, offered: tools}, core.GenerateOptions{
Model: model.Model, ModelInfo: info, System: system, Messages: conversation(history, t),
+2 -2
View File
@@ -608,7 +608,7 @@ func (m *Module) capabilities(ctx context.Context, _ *struct{}) (*httpapi.Reply[
sections := map[string]bool{"chat": signedIn, "memory": signedIn}
for _, name := range []string{
"settings", "events", "workflows", "runs", "credentials", "extensions", "secret_engines", "models", "controls", "memory_admin",
"home", "system_status",
"home", "system_status", "org_config",
} {
sections[name] = p.InstanceAdmin()
}
@@ -616,7 +616,7 @@ func (m *Module) capabilities(ctx context.Context, _ *struct{}) (*httpapi.Reply[
"tasks", "work_requests", "logs", "skills", "curators", "channels",
"captures", "mappings", "bindings", "workflow_editor", "extension_catalogue", "model_aliases",
"task_execution", "organisations", "tools", "plugins", "container_runtime", "agent_profiles",
"org_agents", "org_audit", "org_usage", "org_config",
"org_agents", "org_audit", "org_usage", "org_work_intake",
} {
sections[name] = false
}
+1
View File
@@ -425,6 +425,7 @@ func (m *Module) Register(api huma.API) {
OperationID: "test-agent-model", Method: http.MethodPost, Path: "/api/settings/agent-model/test",
Summary: "Ask a model one question before saving it", Tags: tags,
}, m.testAgentModel)
m.registerInstructions(api)
}
func (m *Module) getSettings(ctx context.Context, _ *struct{}) (*httpapi.Reply[settingsRequest], error) {
+59
View File
@@ -0,0 +1,59 @@
package governance
import (
"context"
"net/http"
"strings"
"github.com/danielgtaylor/huma/v2"
"github.com/samcharles93/archie-core/internal/platform/httpapi"
)
// MaxInstructionsBytes bounds the organisation's instructions: they join every
// agent's system prompt, so an unbounded text would crowd out the task.
const MaxInstructionsBytes = 8 << 10
type instructionsBody struct {
Text string `json:"text" maxLength:"8192" doc:"Standing instructions given to every agent in the organisation"`
}
// Instructions returns the organisation's standing instructions, trimmed;
// empty when none are set.
func (m *Module) Instructions(ctx context.Context) (string, error) {
body, err := m.store.Instructions(ctx, m.orgID)
return strings.TrimSpace(body), err
}
func (m *Module) registerInstructions(api huma.API) {
tags := []string{"Governance"}
httpapi.Register(api, httpapi.AdminAuthorized(ActionRead, KindSettings), huma.Operation{
OperationID: "get-instructions", Method: http.MethodGet, Path: "/api/settings/instructions",
Summary: "Get the organisation's standing instructions", Tags: tags,
}, m.getInstructions)
httpapi.Register(api, httpapi.AdminAuthorized(ActionUpdate, KindSettings), huma.Operation{
OperationID: "put-instructions", Method: http.MethodPut, Path: "/api/settings/instructions",
Summary: "Change the organisation's standing instructions", Tags: tags,
}, m.putInstructions)
}
func (m *Module) getInstructions(ctx context.Context, _ *struct{}) (*httpapi.Reply[instructionsBody], error) {
text, err := m.Instructions(ctx)
if err != nil {
m.log.ErrorContext(ctx, "read instructions", "error", err)
return nil, huma.NewError(http.StatusInternalServerError, "couldn't read the instructions")
}
return httpapi.OK(instructionsBody{Text: text}), nil
}
func (m *Module) putInstructions(ctx context.Context, in *struct{ Body instructionsBody }) (*httpapi.Reply[instructionsBody], error) {
text := strings.TrimSpace(in.Body.Text)
if len(text) > MaxInstructionsBytes {
return nil, huma.NewError(http.StatusBadRequest, "instructions are limited to 8 KiB")
}
if err := m.store.PutInstructions(ctx, m.orgID, text); err != nil {
m.log.ErrorContext(ctx, "put instructions", "error", err)
return nil, huma.NewError(http.StatusInternalServerError, "couldn't save the instructions")
}
return httpapi.OK(instructionsBody{Text: text}), nil
}
@@ -150,3 +150,27 @@ func (s *Store) PutSettings(ctx context.Context, set Settings) error {
}
return nil
}
// Instructions returns the organisation's standing instructions, empty when
// none were saved.
func (s *Store) Instructions(ctx context.Context, orgID string) (string, error) {
var body string
err := s.db.GetContext(ctx, &body, `SELECT body FROM governance.instructions WHERE org_id = $1`, orgID)
if errors.Is(err, sql.ErrNoRows) {
return "", nil
}
if err != nil {
return "", fmt.Errorf("read instructions: %w", err)
}
return body, nil
}
// PutInstructions records the organisation's standing instructions.
func (s *Store) PutInstructions(ctx context.Context, orgID, body string) error {
if _, err := s.db.ExecContext(ctx, `
INSERT INTO governance.instructions (org_id, body) VALUES ($1, $2)
ON CONFLICT (org_id) DO UPDATE SET body = EXCLUDED.body, updated_at = now()`, orgID, body); err != nil {
return fmt.Errorf("put instructions: %w", err)
}
return nil
}
+2 -1
View File
@@ -53,7 +53,8 @@ func (s modelSettings) AgentProvider(context.Context, governance.Model) (chat.Pr
return s.model, chat.ModelInfo{}, nil
}
func (modelSettings) AttemptsPerRun(context.Context) (int, error) { return 6, nil }
func (modelSettings) AttemptsPerRun(context.Context) (int, error) { return 6, nil }
func (modelSettings) Instructions(context.Context) (string, error) { return "", nil }
// TestTriageClassificationUsesPinnedModel checks that a resumed step returns
// the same typed classification even if its model alias now resolves differently.
+20 -5
View File
@@ -31,6 +31,8 @@ import (
// Settings provides the organisation's model configuration and escalation policy.
type Settings interface {
AgentModelForAlias(ctx context.Context, alias string) (governance.Model, error)
// Instructions are the organisation's standing instructions for agents.
Instructions(ctx context.Context) (string, error)
// AgentProvider builds the chat client for a model, resolving the
// credential it names. The model is journaled; the client is rebuilt.
AgentProvider(ctx context.Context, model governance.Model) (chat.Provider, chat.ModelInfo, error)
@@ -126,6 +128,9 @@ type pinned struct {
models map[string]pinnedModel
threshold int
decls []connectors.Declaration
// instructions are the organisation's standing instructions when the run
// started, so a resumed run keeps the prompt it began with.
instructions string
}
// progress is what the steps of one execution have produced so far.
@@ -335,6 +340,13 @@ func (d Deps) pin(rc *Context, p *progress, aliases []string, withMemory bool) e
}
p.pin.threshold = threshold
}
instructions, err := rc.Step("org-instructions", func(ctx context.Context, _ string) (string, error) {
return d.Settings.Instructions(ctx)
})
if err != nil {
return err
}
p.pin.instructions = instructions
decls, err := rc.Step("agent-tools", func(context.Context, string) ([]connectors.Declaration, error) {
return d.Toolbox.Declarations(), nil
})
@@ -353,11 +365,14 @@ func (d Deps) pin(rc *Context, p *progress, aliases []string, withMemory bool) e
}
// agentSystem is an agent step's system prompt: how Archie works, then the
// workflow's instructions, then the step's mission.
func agentSystem(instructions, mission string) string {
// organisation's standing instructions, the workflow's instructions and the
// step's mission.
func agentSystem(org, instructions, mission string) string {
parts := []string{prompts.Agent()}
if strings.TrimSpace(instructions) != "" {
parts = append(parts, instructions)
for _, text := range []string{org, instructions} {
if strings.TrimSpace(text) != "" {
parts = append(parts, text)
}
}
return strings.Join(append(parts, mission), "\n\n")
}
@@ -395,7 +410,7 @@ func runAgent(d Deps, rc *Context, p *progress, name string, s agentSettings) (S
model := p.pin.models[modelAlias(s.Model)]
base := AgentRequest{
Provider: model.provider, Model: model.model.Model, ModelInfo: model.info,
System: agentSystem(p.instructions, s.system()), Tools: tools(),
System: agentSystem(p.pin.instructions, p.instructions, s.system()), Tools: tools(),
}
first := base
first.Name, first.Prompt, first.Budget = name, string(p.input), budget
+2 -1
View File
@@ -94,7 +94,8 @@ func (s investigateSettings) AgentProvider(context.Context, governance.Model) (c
return s.model, chat.ModelInfo{}, nil
}
func (investigateSettings) AttemptsPerRun(context.Context) (int, error) { return 8, nil }
func (investigateSettings) AttemptsPerRun(context.Context) (int, error) { return 8, nil }
func (investigateSettings) Instructions(context.Context) (string, error) { return "", nil }
// EscalationPath waits long enough that the first attempt suspends, as a
// person who has not answered yet does.
+1
View File
@@ -102,6 +102,7 @@ func (onePersonPath) EscalationPath(context.Context, string, bool) ([]governance
func (onePersonPath) RecurrenceThreshold(context.Context) (int, error) { return 0, nil }
func (onePersonPath) AttemptsPerRun(context.Context) (int, error) { return 1, nil }
func (onePersonPath) Instructions(context.Context) (string, error) { return "", nil }
type notices struct{ sent []deliveries.Notification }