From 7e701f9a50d4da10dcb622503384cba9ed876097 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 24 Nov 2026 02:28:56 +1100 Subject: [PATCH] feat(pg): add alerts, proposals, audit log, invites, chat limits and connector installations to the baselines (#2025, #1995, #2019, #2110, #2074, #2075, #2124, #2027) --- .../pg/migrations/00001_governance.sql | 124 ++++++++++++++++-- .../pg/migrations/00002_connectors.sql | 102 ++++++++++---- .../platform/pg/migrations/00009_chat.sql | 59 ++++++--- .../platform/pg/migrations/00011_alerts.sql | 38 ++++++ .../pg/migrations/00012_proposals.sql | 57 ++++++++ 5 files changed, 326 insertions(+), 54 deletions(-) create mode 100644 internal/platform/pg/migrations/00011_alerts.sql create mode 100644 internal/platform/pg/migrations/00012_proposals.sql diff --git a/internal/platform/pg/migrations/00001_governance.sql b/internal/platform/pg/migrations/00001_governance.sql index 696f7d38f..a33bfbd40 100644 --- a/internal/platform/pg/migrations/00001_governance.sql +++ b/internal/platform/pg/migrations/00001_governance.sql @@ -27,19 +27,33 @@ CREATE TABLE governance.model_roles ( -- An organisation's settings are one row. When the row is missing, every -- setting has its default value. +-- +-- The chat limits bound what one person and the whole organisation can spend: +-- tokens per turn, turns and tool calls per hour, and tokens per day. A request +-- over a limit is refused, never queued. CREATE TABLE governance.settings ( - org_id text PRIMARY KEY, - updated_at timestamptz NOT NULL DEFAULT now(), - approval_deadline_ms bigint NOT NULL, - propose_autonomy_after integer NOT NULL DEFAULT 3 CHECK (propose_autonomy_after >= 1), - frozen boolean NOT NULL DEFAULT false, - recurrence_threshold integer NOT NULL DEFAULT 5, - default_owner text NOT NULL DEFAULT '', - escalation jsonb NOT NULL DEFAULT '[]', - attempts_per_run integer NOT NULL DEFAULT 20, - timing jsonb NOT NULL DEFAULT '{}', - actions_per_run integer NOT NULL DEFAULT 10, - actions_per_day integer NOT NULL DEFAULT 100 + org_id text PRIMARY KEY, + updated_at timestamptz NOT NULL DEFAULT now(), + approval_deadline_ms bigint NOT NULL, + propose_autonomy_after integer NOT NULL DEFAULT 3 CHECK (propose_autonomy_after BETWEEN 1 AND 100), + frozen boolean NOT NULL DEFAULT false, + recurrence_threshold integer NOT NULL DEFAULT 5, + default_owner text NOT NULL DEFAULT '', + escalation jsonb NOT NULL DEFAULT '[]', + attempts_per_run integer NOT NULL DEFAULT 20, + timing jsonb NOT NULL DEFAULT '{}', + actions_per_run integer NOT NULL DEFAULT 10, + actions_per_day integer NOT NULL DEFAULT 100, + chat_turn_tokens integer NOT NULL DEFAULT 100000 CHECK (chat_turn_tokens > 0), + chat_person_turns_per_hour integer NOT NULL DEFAULT 60 CHECK (chat_person_turns_per_hour > 0), + chat_org_turns_per_hour integer NOT NULL DEFAULT 600 CHECK (chat_org_turns_per_hour > 0), + chat_person_tool_calls_per_hour integer NOT NULL DEFAULT 300 CHECK (chat_person_tool_calls_per_hour > 0), + chat_org_tool_calls_per_hour integer NOT NULL DEFAULT 3000 CHECK (chat_org_tool_calls_per_hour > 0), + chat_person_tokens_per_day bigint NOT NULL DEFAULT 2000000 CHECK (chat_person_tokens_per_day > 0), + chat_org_tokens_per_day bigint NOT NULL DEFAULT 20000000 CHECK (chat_org_tokens_per_day > 0), + CHECK (chat_person_turns_per_hour <= chat_org_turns_per_hour), + CHECK (chat_person_tool_calls_per_hour <= chat_org_tool_calls_per_hour), + CHECK (chat_person_tokens_per_day <= chat_org_tokens_per_day) ); CREATE TABLE governance.identities ( @@ -258,6 +272,92 @@ CREATE TABLE governance.model_policy ( updated_at timestamptz NOT NULL DEFAULT now() ); +-- The mail server the instance sends invitations through. The password is +-- sealed and never read back through the API. +CREATE TABLE governance.mail_settings ( + singleton boolean DEFAULT true PRIMARY KEY CHECK (singleton), + host text NOT NULL, + port integer NOT NULL CHECK (port BETWEEN 1 AND 65535), + security text NOT NULL CHECK (security IN ('tls', 'starttls', 'none')), + username text NOT NULL DEFAULT '', + password_sealed bytea NOT NULL DEFAULT '\x'::bytea, + from_address text NOT NULL, + updated_at timestamptz NOT NULL DEFAULT now() +); + +-- An invitation to join an organisation, sent to an email address that has no +-- account yet. Accepting it sets up a local account with the invited role. +-- Only the token's hash is stored, and an address has at most one open +-- invitation per organisation. +CREATE TABLE governance.invites ( + id uuid PRIMARY KEY, + org_id text NOT NULL REFERENCES governance.orgs (id), + workspace_id text, + email text NOT NULL CHECK (email = lower(email)), + role text NOT NULL, + token_hash text NOT NULL UNIQUE, + invited_by_id text NOT NULL REFERENCES governance.identities (id), + created_at timestamptz NOT NULL DEFAULT now(), + expires_at timestamptz NOT NULL, + sent_at timestamptz, + send_error text NOT NULL DEFAULT '', + accepted_at timestamptz, + accepted_identity_id text REFERENCES governance.identities (id), + revoked_at timestamptz, + FOREIGN KEY (org_id, workspace_id) REFERENCES governance.workspaces (org_id, id), + CHECK ((accepted_at IS NULL) = (accepted_identity_id IS NULL)), + CHECK ((accepted_at IS NULL) OR (revoked_at IS NULL)) +); +CREATE UNIQUE INDEX invites_open ON governance.invites (org_id, email) WHERE ((accepted_at IS NULL) AND (revoked_at IS NULL)); + +-- The audit log records every change on the platform: who or what made it, +-- what changed, why, and how it went. org_id is empty for a change to the +-- instance itself, such as accepting a package. +-- +-- Entries form one chain: each holds the digest of the entry before it, so a +-- missing or edited entry is detectable. The unique prev_digest keeps the +-- chain linear when two writers append at once, and only the first entry has +-- none. +-- +-- actor_name is the actor's name when the change was made, kept beside its ID +-- so the entry still reads correctly after a rename. request and response are +-- the payloads of a call that changed something outside Archie, without +-- credentials. why holds the cause, the guard verdicts, the approval and the +-- evidence the approver saw. context holds what is specific to the kind, such +-- as the chat session, the tool version and the policy revision. +CREATE TABLE governance.audit_log ( + seq bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, + id uuid NOT NULL UNIQUE, + org_id text NOT NULL, + at timestamptz NOT NULL DEFAULT now(), + kind text NOT NULL, + actor_kind text NOT NULL CHECK (actor_kind IN ('person', 'rule', 'guard', 'step', 'system')), + actor_id text NOT NULL CHECK (actor_id <> ''), + actor_name text NOT NULL DEFAULT '', + authority jsonb NOT NULL DEFAULT '{}', + target_kind text NOT NULL, + target_id text NOT NULL, + run_id uuid, + workflow_id uuid, + decision_id uuid, + before jsonb, + after jsonb, + request jsonb, + response jsonb, + why jsonb NOT NULL DEFAULT '{}', + context jsonb NOT NULL DEFAULT '{}', + outcome text NOT NULL CHECK (outcome IN ('succeeded', 'failed', 'refused', 'unknown')), + prev_digest bytea UNIQUE, + digest bytea NOT NULL UNIQUE +); +CREATE UNIQUE INDEX audit_log_first ON governance.audit_log ((true)) WHERE (prev_digest IS NULL); +CREATE INDEX audit_log_by_org ON governance.audit_log (org_id, at DESC, seq DESC); +CREATE INDEX audit_log_by_actor ON governance.audit_log (org_id, actor_id, at DESC); +CREATE INDEX audit_log_by_target ON governance.audit_log (org_id, target_kind, target_id, at DESC); +CREATE INDEX audit_log_by_kind ON governance.audit_log (org_id, kind, at DESC); +CREATE INDEX audit_log_by_run ON governance.audit_log (run_id, seq) WHERE (run_id IS NOT NULL); +CREATE INDEX audit_log_by_workflow ON governance.audit_log (workflow_id, at DESC) WHERE (workflow_id IS NOT NULL); + INSERT INTO governance.access_policy_revision (only_row) VALUES (true); -- +goose Down diff --git a/internal/platform/pg/migrations/00002_connectors.sql b/internal/platform/pg/migrations/00002_connectors.sql index e1526ee99..e83212ca9 100644 --- a/internal/platform/pg/migrations/00002_connectors.sql +++ b/internal/platform/pg/migrations/00002_connectors.sql @@ -1,44 +1,92 @@ -- +goose Up CREATE SCHEMA connectors; -CREATE TABLE connectors.accepted ( - org_id text NOT NULL, - name text NOT NULL, - path text NOT NULL, - sha256 text NOT NULL, - egress jsonb NOT NULL DEFAULT '[]', - env jsonb NOT NULL DEFAULT '[]', - settings jsonb NOT NULL DEFAULT '{}', - updated_at timestamptz NOT NULL DEFAULT now(), - contract text NOT NULL DEFAULT 'connector' CHECK (contract IN ('connector', 'secretengine', 'mcp', 'guard')), - credentials jsonb NOT NULL DEFAULT '{}', - args jsonb NOT NULL DEFAULT '[]', - url text NOT NULL DEFAULT '', - tools jsonb NOT NULL DEFAULT '[]', - capabilities jsonb, - effects jsonb, - enabled boolean NOT NULL DEFAULT true, - package jsonb, - PRIMARY KEY (org_id, name) +-- A package is an extension the instance has accepted: its name, contract, +-- binary and digest, and the manifest that declares its settings, capabilities +-- and effects. It holds no configuration and no credentials, so it is accepted +-- once for the whole instance and configured by its installations. +CREATE TABLE connectors.packages ( + id uuid PRIMARY KEY, + name text NOT NULL UNIQUE, + contract text NOT NULL CHECK (contract IN ('connector', 'secretengine', 'mcp', 'guard')), + path text NOT NULL, + sha256 text NOT NULL, + manifest jsonb, + capabilities jsonb, + effects jsonb, + accepted_by_id text NOT NULL, + accepted_at timestamptz NOT NULL DEFAULT now(), + updated_at timestamptz NOT NULL DEFAULT now() +); + +-- An installation is one configuration of a package, owned by a scope: the +-- instance, one organisation or one person. A binding names an installation or +-- inherits one, resolving the person's first, then the organisation's, then the +-- instance's. name is unique within its scope, so the same name can resolve to +-- a different installation for each organisation or person. tools lists the +-- tools an MCP server reported. +CREATE TABLE connectors.installations ( + id uuid PRIMARY KEY, + package_id uuid NOT NULL REFERENCES connectors.packages (id) ON DELETE RESTRICT, + scope text NOT NULL CHECK (scope IN ('instance', 'org', 'person')), + org_id text NOT NULL DEFAULT '', + person_id text NOT NULL DEFAULT '', + name text NOT NULL, + settings jsonb NOT NULL DEFAULT '{}', + egress jsonb NOT NULL DEFAULT '[]', + env jsonb NOT NULL DEFAULT '[]', + credentials jsonb NOT NULL DEFAULT '{}', + args jsonb NOT NULL DEFAULT '[]', + url text NOT NULL DEFAULT '', + tools jsonb NOT NULL DEFAULT '[]', + enabled boolean NOT NULL DEFAULT true, + created_at timestamptz NOT NULL DEFAULT now(), + updated_at timestamptz NOT NULL DEFAULT now(), + UNIQUE (scope, org_id, person_id, name), + CHECK ((scope <> 'instance') OR ((org_id = '') AND (person_id = ''))), + CHECK ((scope <> 'org') OR ((org_id <> '') AND (person_id = ''))), + CHECK ((scope <> 'person') OR ((org_id = '') AND (person_id <> ''))) +); +CREATE INDEX installations_by_package ON connectors.installations (package_id); + +-- An organisation or a person can switch off an installation it would +-- otherwise inherit from a broader scope. +CREATE TABLE connectors.installation_overrides ( + installation_id uuid NOT NULL REFERENCES connectors.installations (id) ON DELETE CASCADE, + scope text NOT NULL CHECK (scope IN ('org', 'person')), + org_id text NOT NULL DEFAULT '', + person_id text NOT NULL DEFAULT '', + disabled_by_id text NOT NULL, + disabled_at timestamptz NOT NULL DEFAULT now(), + PRIMARY KEY (installation_id, scope, org_id, person_id), + CHECK ((scope <> 'org') OR ((org_id <> '') AND (person_id = ''))), + CHECK ((scope <> 'person') OR ((org_id = '') AND (person_id <> ''))) ); -- A credential's fields are sealed with age. Each field holds a value or the --- secret engine key the value is read from. +-- secret engine key the value is read from. A credential is owned by a scope +-- and resolved like an installation. CREATE TABLE connectors.credentials ( - org_id text NOT NULL, + id uuid PRIMARY KEY, + scope text NOT NULL CHECK (scope IN ('instance', 'org', 'person')), + org_id text NOT NULL DEFAULT '', + person_id text NOT NULL DEFAULT '', name text NOT NULL, kind text NOT NULL CHECK (kind IN ('machine', 'age', 'basic', 'bearer')), sealed bytea NOT NULL, updated_at timestamptz NOT NULL DEFAULT now(), - PRIMARY KEY (org_id, name) + UNIQUE (scope, org_id, person_id, name), + CHECK ((scope <> 'instance') OR ((org_id = '') AND (person_id = ''))), + CHECK ((scope <> 'org') OR ((org_id <> '') AND (person_id = ''))), + CHECK ((scope <> 'person') OR ((org_id = '') AND (person_id <> ''))) ); --- The location of an organisation's extension catalogue and the public key that --- verifies its signature. --- newest_issued_at is the issue time of the newest catalogue accepted from it, --- so an older catalogue replayed by the source or the network is refused. +-- The location of the instance's extension catalogue and the public key that +-- verifies its signature. newest_issued_at is the issue time of the newest +-- catalogue accepted from it, so an older catalogue replayed by the source or +-- the network is refused. CREATE TABLE connectors.catalogue_source ( - org_id text PRIMARY KEY, + singleton boolean DEFAULT true PRIMARY KEY CHECK (singleton), location text NOT NULL, public_key text NOT NULL, newest_issued_at timestamptz, diff --git a/internal/platform/pg/migrations/00009_chat.sql b/internal/platform/pg/migrations/00009_chat.sql index 04e1dbdb7..5948317ac 100644 --- a/internal/platform/pg/migrations/00009_chat.sql +++ b/internal/platform/pg/migrations/00009_chat.sql @@ -18,21 +18,23 @@ CREATE INDEX sessions_by_owner ON chat.sessions (org_id, owner_id, updated_at DE -- same session has ended. Judgement and approvals record the turn's actions -- with the turn's id as their run id. CREATE TABLE chat.turns ( - seq bigserial PRIMARY KEY, - id uuid NOT NULL UNIQUE, - org_id text NOT NULL, - session_id uuid NOT NULL REFERENCES chat.sessions (id) ON DELETE CASCADE, - state text NOT NULL CHECK (state IN ('queued', 'running', 'succeeded', 'failed', 'cancelled')), - text text NOT NULL, - page text NOT NULL DEFAULT '', - page_label text NOT NULL DEFAULT '', - reply text NOT NULL DEFAULT '', - reasoning text NOT NULL DEFAULT '', - error text NOT NULL DEFAULT '', - cancel boolean NOT NULL DEFAULT false, - created_at timestamptz NOT NULL DEFAULT now(), - started_at timestamptz, - finished_at timestamptz + seq bigserial PRIMARY KEY, + id uuid NOT NULL UNIQUE, + org_id text NOT NULL, + session_id uuid NOT NULL REFERENCES chat.sessions (id) ON DELETE CASCADE, + state text NOT NULL CHECK (state IN ('queued', 'running', 'succeeded', 'failed', 'cancelled')), + text text NOT NULL, + page text NOT NULL DEFAULT '', + page_label text NOT NULL DEFAULT '', + reply text NOT NULL DEFAULT '', + reasoning text NOT NULL DEFAULT '', + error text NOT NULL DEFAULT '', + cancel boolean NOT NULL DEFAULT false, + created_at timestamptz NOT NULL DEFAULT now(), + started_at timestamptz, + finished_at timestamptz, + input_tokens bigint NOT NULL DEFAULT 0, + output_tokens bigint NOT NULL DEFAULT 0 ); CREATE INDEX turns_by_session ON chat.turns (session_id, seq); CREATE INDEX turns_queued ON chat.turns (seq) WHERE (state IN ('queued', 'running')); @@ -71,5 +73,32 @@ CREATE TABLE chat.actions ( ); CREATE INDEX actions_open ON chat.actions (state) WHERE (state IN ('waiting', 'approved', 'running')); +-- Usage counts the turns, tool calls and tokens of one person or one whole +-- organisation in a fixed window, so the limits in governance settings hold +-- across sessions, concurrent turns and restarts. A turn or tool call is +-- counted, under the limit, in the same statement that checks it. +CREATE TABLE chat.usage ( + org_id text NOT NULL, + subject_kind text NOT NULL CHECK (subject_kind IN ('person', 'org')), + subject_id text NOT NULL, + metric text NOT NULL CHECK (metric IN ('turns', 'tool_calls', 'tokens')), + window_start timestamptz NOT NULL, + count bigint NOT NULL CHECK (count >= 0), + PRIMARY KEY (org_id, subject_kind, subject_id, metric, window_start), + CHECK ((subject_kind <> 'org') OR (subject_id = org_id)) +); + +-- A tool switched off for chat. A disabled tool is left out of a turn's tool +-- list and refused if the model calls it anyway. An empty action switches off +-- every action of the installation. +CREATE TABLE chat.disabled_tools ( + installation_id uuid NOT NULL, + action text NOT NULL DEFAULT '', + reason text NOT NULL DEFAULT '', + disabled_by_id text NOT NULL, + disabled_at timestamptz NOT NULL DEFAULT now(), + PRIMARY KEY (installation_id, action) +); + -- +goose Down DROP SCHEMA chat CASCADE; diff --git a/internal/platform/pg/migrations/00011_alerts.sql b/internal/platform/pg/migrations/00011_alerts.sql new file mode 100644 index 000000000..2ac2aa2fc --- /dev/null +++ b/internal/platform/pg/migrations/00011_alerts.sql @@ -0,0 +1,38 @@ +-- +goose Up +CREATE SCHEMA alerts; + +-- An alert is a condition a source reported, followed from when it opens until +-- it resolves. key is the source's own identifier for the condition, such as +-- Alertmanager's fingerprint, so a repeat or a resolve from the source finds +-- the open alert instead of opening another. run_id is the run working it. +CREATE TABLE alerts.alerts ( + id uuid PRIMARY KEY, + org_id text NOT NULL, + source_id uuid NOT NULL, + key text NOT NULL, + state text NOT NULL CHECK (state IN ('open', 'acknowledged', 'resolved', 'suppressed')), + severity text NOT NULL CHECK (severity IN ('critical', 'high', 'medium', 'low', 'info')), + title text NOT NULL, + summary text NOT NULL DEFAULT '', + target_kind text NOT NULL, + target_id text NOT NULL, + labels jsonb NOT NULL DEFAULT '{}', + opened_event_id uuid NOT NULL, + resolved_event_id uuid, + run_id uuid, + opened_at timestamptz NOT NULL, + acknowledged_at timestamptz, + acknowledged_by_id text, + resolved_at timestamptz, + suppressed_until timestamptz, + updated_at timestamptz NOT NULL DEFAULT now(), + CHECK ((acknowledged_at IS NULL) = (acknowledged_by_id IS NULL)), + CHECK ((state <> 'resolved') OR (resolved_at IS NOT NULL)), + CHECK ((state <> 'suppressed') OR (suppressed_until IS NOT NULL)) +); +CREATE UNIQUE INDEX alerts_one_unresolved ON alerts.alerts (org_id, source_id, key) WHERE (state <> 'resolved'); +CREATE INDEX alerts_by_state ON alerts.alerts (org_id, state, opened_at DESC); +CREATE INDEX alerts_by_target ON alerts.alerts (org_id, target_kind, target_id, opened_at DESC); + +-- +goose Down +DROP SCHEMA alerts CASCADE; diff --git a/internal/platform/pg/migrations/00012_proposals.sql b/internal/platform/pg/migrations/00012_proposals.sql new file mode 100644 index 000000000..b201a2f80 --- /dev/null +++ b/internal/platform/pg/migrations/00012_proposals.sql @@ -0,0 +1,57 @@ +-- +goose Up +CREATE SCHEMA proposals; + +-- A proposal is a change Archie drafts with its evidence: a root-cause fix, a +-- new trigger and workflow, a workflow change or a raised ceiling. change holds +-- what publishing it writes, and base_version the version of the target it was +-- drafted against, so a target changed since is detected. A policy or ceiling +-- change needs a person with authority, and any other change needs the owner. +-- A rejection keeps its reason so the next proposal can take it into account. +-- prompted_by lists the events that led to it, which a published trigger runs +-- for. +CREATE TABLE proposals.proposals ( + id uuid PRIMARY KEY, + org_id text NOT NULL, + kind text NOT NULL CHECK (kind IN ('root_cause_fix', 'trigger', 'workflow_change', 'ceiling_raise', 'policy_change')), + state text NOT NULL CHECK (state IN ('pending', 'approved', 'rejected', 'published', 'superseded', 'withdrawn')), + needs text NOT NULL CHECK (needs IN ('owner', 'authority')), + title text NOT NULL, + summary text NOT NULL, + evidence jsonb NOT NULL, + change jsonb NOT NULL, + target_kind text NOT NULL, + target_id text NOT NULL DEFAULT '', + base_version integer, + prompted_by uuid[] NOT NULL DEFAULT '{}', + origin_run_id uuid, + proposed_by_id text NOT NULL, + created_at timestamptz NOT NULL DEFAULT now(), + answered_by_id text, + answered_at timestamptz, + answer_reason text NOT NULL DEFAULT '', + published_at timestamptz, + CHECK ((answered_at IS NULL) = (answered_by_id IS NULL)), + CHECK ((state IN ('pending', 'withdrawn', 'superseded')) OR (answered_at IS NOT NULL)), + CHECK ((state = 'published') = (published_at IS NOT NULL)) +); +CREATE INDEX proposals_by_state ON proposals.proposals (org_id, state, created_at DESC); +CREATE INDEX proposals_by_target ON proposals.proposals (org_id, target_kind, target_id, created_at DESC); + +-- The actions a proposal will take when it is approved, in order. Each is +-- judged when the proposal is made and shown with it, and approving the +-- proposal approves them. digest binds the approval to the exact action. +CREATE TABLE proposals.actions ( + proposal_id uuid NOT NULL REFERENCES proposals.proposals (id) ON DELETE CASCADE, + seq integer NOT NULL, + installation_id uuid NOT NULL, + action text NOT NULL, + target_kind text NOT NULL, + target_id text NOT NULL, + input jsonb NOT NULL, + digest text NOT NULL, + decision_id uuid NOT NULL, + PRIMARY KEY (proposal_id, seq) +); + +-- +goose Down +DROP SCHEMA proposals CASCADE;