-- +goose Up CREATE SCHEMA governance; -- A model provider is an API endpoint that serves models: its provider class, -- its base URL and the credential that holds its API key. It is identified by -- its id, fixed when created; name is an editable label unique within the -- organisation. api_key_credential_id references connectors.credentials (id) -- without a cross-schema foreign key. A model alias refers to a provider by id -- and one of its models, so several aliases can share a provider. CREATE TABLE governance.model_providers ( id uuid PRIMARY KEY, org_id text NOT NULL, name text NOT NULL CHECK (name ~ '^[a-z0-9][a-z0-9_.-]{0,62}$'), class text NOT NULL, base_url text NOT NULL, api_key_credential_id uuid, updated_at timestamptz NOT NULL DEFAULT now(), UNIQUE (org_id, name), UNIQUE (org_id, id) ); -- A model role records the model an organisation uses for one job. The -- foreign key includes org_id so a role can only use its own organisation's -- provider. CREATE TABLE governance.model_roles ( org_id text NOT NULL, role text NOT NULL CHECK ((role = 'agent') OR ((role <> 'agent:default') AND (role ~ '^agent:[a-zA-Z0-9][a-zA-Z0-9_.-]{0,62}$'))), provider_id uuid NOT NULL, model text NOT NULL, updated_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (org_id, role), FOREIGN KEY (org_id, provider_id) REFERENCES governance.model_providers (org_id, id) ON DELETE RESTRICT ); -- 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 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 ( id text PRIMARY KEY, kind text NOT NULL, display_name text NOT NULL, lifecycle text NOT NULL, version bigint NOT NULL, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now() ); CREATE TABLE governance.identity_aliases ( alias text PRIMARY KEY, identity_id text NOT NULL REFERENCES governance.identities (id) ); CREATE UNIQUE INDEX idx_governance_identity_aliases_alias ON governance.identity_aliases (lower(alias)); CREATE TABLE governance.identity_events ( id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, identity_id text NOT NULL, event_type text NOT NULL, from_lifecycle text NOT NULL, to_lifecycle text NOT NULL, display_name text NOT NULL, actor_id text NOT NULL, source text NOT NULL, request_id text NOT NULL UNIQUE, at timestamptz NOT NULL, org_id text CHECK ((org_id IS NULL) OR (org_id <> '')) ); CREATE INDEX idx_governance_identity_events_identity ON governance.identity_events (identity_id, id); CREATE INDEX idx_governance_identity_events_org_at ON governance.identity_events (org_id, at DESC, id DESC); CREATE TABLE governance.identity_subjects ( issuer text NOT NULL, subject text NOT NULL, identity_id text NOT NULL REFERENCES governance.identities (id), bound_at timestamptz NOT NULL, name text NOT NULL DEFAULT '', expires_at timestamptz, last_used_at timestamptz, PRIMARY KEY (issuer, subject) ); CREATE INDEX idx_governance_identity_subjects_identity ON governance.identity_subjects (identity_id); CREATE TABLE governance.orgs ( id text PRIMARY KEY, name text NOT NULL, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), version bigint NOT NULL DEFAULT 1 ); CREATE TABLE governance.workspaces ( id text NOT NULL, org_id text NOT NULL REFERENCES governance.orgs (id), name text NOT NULL, environment text NOT NULL DEFAULT '', created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), version bigint NOT NULL DEFAULT 1, PRIMARY KEY (org_id, id) ); CREATE TABLE governance.memberships ( identity_id text NOT NULL REFERENCES governance.identities (id), org_id text NOT NULL REFERENCES governance.orgs (id), workspace_id text, role text NOT NULL, created_at timestamptz NOT NULL DEFAULT now(), FOREIGN KEY (org_id, workspace_id) REFERENCES governance.workspaces (org_id, id) ); CREATE UNIQUE INDEX idx_governance_memberships_identity ON governance.memberships (identity_id, org_id, COALESCE(workspace_id, '')); CREATE INDEX idx_governance_memberships_org ON governance.memberships (org_id); CREATE TABLE governance.org_agents ( identity_id text PRIMARY KEY REFERENCES governance.identities (id), org_id text NOT NULL REFERENCES governance.orgs (id), created_at timestamptz NOT NULL DEFAULT now() ); CREATE INDEX idx_governance_org_agents_org ON governance.org_agents (org_id); CREATE TABLE governance.accounts ( identity_id text PRIMARY KEY REFERENCES governance.identities (id), email text NOT NULL UNIQUE CHECK (email = lower(email)), password_hash text NOT NULL, recovery_hash text, failed_attempts integer NOT NULL DEFAULT 0, locked_until timestamptz, last_sign_in_at timestamptz ); CREATE TABLE governance.account_sessions ( token_hash text PRIMARY KEY, identity_id text NOT NULL REFERENCES governance.identities (id) ON DELETE CASCADE, expires_at timestamptz NOT NULL, issued_at timestamptz NOT NULL DEFAULT now() ); CREATE INDEX idx_governance_account_sessions_identity ON governance.account_sessions (identity_id); CREATE INDEX idx_governance_account_sessions_expiry ON governance.account_sessions (expires_at); CREATE TABLE governance.authentication_settings ( singleton boolean DEFAULT true PRIMARY KEY CHECK (singleton), public_url text NOT NULL, oidc_issuer text NOT NULL DEFAULT '', oidc_audience text NOT NULL DEFAULT '', oidc_client_id text NOT NULL DEFAULT '', trusted_proxies jsonb NOT NULL DEFAULT '[]' ); CREATE TABLE governance.passkeys ( credential_id text PRIMARY KEY, identity_id text NOT NULL REFERENCES governance.identities (id), name text NOT NULL, credential jsonb NOT NULL, created_at timestamptz NOT NULL DEFAULT now() ); CREATE INDEX idx_governance_passkeys_identity ON governance.passkeys (identity_id); CREATE TABLE governance.passkey_ceremonies ( token_hash text PRIMARY KEY, identity_id text NOT NULL, registration boolean NOT NULL, session_data jsonb NOT NULL, expires_at timestamptz NOT NULL, subject text NOT NULL DEFAULT '' ); CREATE INDEX idx_governance_passkey_ceremonies_expiry ON governance.passkey_ceremonies (expires_at); CREATE TABLE governance.access_policies ( level text NOT NULL CHECK (level IN ('instance', 'org', 'workspace', 'object')), org_id text NOT NULL DEFAULT '', workspace_id text NOT NULL DEFAULT '', object_kind text NOT NULL DEFAULT '', object_id text NOT NULL DEFAULT '', policy_id text NOT NULL, text text NOT NULL, updated_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (level, org_id, workspace_id, object_kind, object_id, policy_id) ); CREATE TABLE governance.access_policy_versions ( level text NOT NULL CHECK (level IN ('instance', 'org', 'workspace', 'object')), org_id text NOT NULL DEFAULT '', workspace_id text NOT NULL DEFAULT '', version bigint NOT NULL DEFAULT 1, updated_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (level, org_id, workspace_id) ); CREATE TABLE governance.access_policy_revision ( only_row boolean DEFAULT true PRIMARY KEY CHECK (only_row), version bigint NOT NULL DEFAULT 1, updated_at timestamptz NOT NULL DEFAULT now() ); CREATE TABLE governance.access_decisions ( id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, org_id text NOT NULL, principal text NOT NULL, action text NOT NULL, resource_kind text NOT NULL, resource_id text NOT NULL, level text NOT NULL, policies text[] NOT NULL DEFAULT '{}', minute_at timestamptz NOT NULL DEFAULT date_trunc ('minute', now()), count bigint NOT NULL DEFAULT 1, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), outcome text NOT NULL DEFAULT 'denied' CHECK (outcome IN ('allowed', 'denied')) ); CREATE UNIQUE INDEX idx_governance_access_decisions_dedup ON governance.access_decisions (org_id, principal, action, resource_kind, resource_id, outcome, level, policies, minute_at); -- Each change to an organisation or workspace name, with who made it and the -- old and new names. CREATE TABLE governance.org_events ( id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, org_id text NOT NULL REFERENCES governance.orgs (id), workspace_id text NOT NULL DEFAULT '', event_type text NOT NULL, actor_id text NOT NULL, actor_name text NOT NULL, request_id text NOT NULL UNIQUE, old_name text NOT NULL, new_name text NOT NULL, at timestamptz NOT NULL DEFAULT now() ); CREATE INDEX idx_governance_org_events_org_at ON governance.org_events (org_id, at DESC, id DESC); -- A link code is a single-use code that links a channel credential to the -- account that requested it. issuer is the connector that redeems the code. -- Only the code's hash is stored. CREATE TABLE governance.link_codes ( code_hash text PRIMARY KEY, identity_id text NOT NULL REFERENCES governance.identities (id), issuer text NOT NULL, expires_at timestamptz NOT NULL, used_at timestamptz ); CREATE INDEX idx_governance_link_codes_identity ON governance.link_codes (identity_id); -- The organisation's instructions to its agents. They are added to every -- agent's system prompt after Archie's own instructions. CREATE TABLE governance.instructions ( org_id text PRIMARY KEY, body text NOT NULL, updated_at timestamptz NOT NULL DEFAULT now() ); -- The model aliases workflows and chat may use. A null list allows any alias. CREATE TABLE governance.model_policy ( org_id text PRIMARY KEY, workflow_aliases jsonb, chat_aliases jsonb, 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 DROP SCHEMA governance CASCADE;