diff --git a/api/openapi.yaml b/api/openapi.yaml index ea4ecb57d..d8d9bb3ce 100644 --- a/api/openapi.yaml +++ b/api/openapi.yaml @@ -2811,6 +2811,8 @@ components: properties: answered_by: type: string + answered_by_id: + type: string approval: type: string at: @@ -2952,6 +2954,8 @@ components: type: string by: type: string + by_id: + type: string decision_id: type: string required: diff --git a/internal/platform/pg/migrations/00001_governance.sql b/internal/platform/pg/migrations/00001_governance.sql new file mode 100644 index 000000000..696f7d38f --- /dev/null +++ b/internal/platform/pg/migrations/00001_governance.sql @@ -0,0 +1,264 @@ +-- +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. A model alias refers +-- to a provider and one of its models, so several aliases can share a provider. +CREATE TABLE governance.model_providers ( + 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 text NOT NULL DEFAULT '', + updated_at timestamptz NOT NULL DEFAULT now(), + PRIMARY KEY (org_id, name) +); + +-- A model role records the model an organisation uses for one job. +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}$'))), + model text NOT NULL, + updated_at timestamptz NOT NULL DEFAULT now(), + provider text NOT NULL, + PRIMARY KEY (org_id, role), + FOREIGN KEY (org_id, provider) REFERENCES governance.model_providers (org_id, name) +); + +-- An organisation's settings are one row. When the row is missing, every +-- setting has its default value. +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 +); + +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() +); + +INSERT INTO governance.access_policy_revision (only_row) VALUES (true); + +-- +goose Down +DROP SCHEMA governance CASCADE; diff --git a/internal/platform/pg/migrations/00001_runs_engine.sql b/internal/platform/pg/migrations/00001_runs_engine.sql deleted file mode 100644 index 4bce78960..000000000 --- a/internal/platform/pg/migrations/00001_runs_engine.sql +++ /dev/null @@ -1,50 +0,0 @@ --- +goose Up -CREATE SCHEMA runs; - -CREATE TABLE runs.runs ( - id uuid PRIMARY KEY, - org_id text NOT NULL, - workflow text NOT NULL, - start_key text, - input jsonb NOT NULL, - state text NOT NULL CHECK (state IN ('queued', 'running', 'waiting', 'succeeded', 'failed', 'cancelled')), - lease_token uuid, - lease_owner text, - lease_expires_at timestamptz, - wake_at timestamptz, - waiting_for text, - cancel_requested boolean NOT NULL DEFAULT false, - attempts integer NOT NULL DEFAULT 0, - result jsonb, - error text, - created_at timestamptz NOT NULL DEFAULT now(), - updated_at timestamptz NOT NULL DEFAULT now(), - finished_at timestamptz, - UNIQUE (org_id, start_key) -); - --- Claiming scans only runs that could be claimed. -CREATE INDEX runs_claimable ON runs.runs (created_at) - WHERE state IN ('queued', 'waiting', 'running'); - -CREATE TABLE runs.steps ( - run_id uuid NOT NULL REFERENCES runs.runs (id) ON DELETE CASCADE, - seq integer NOT NULL, - name text NOT NULL, - outcome text NOT NULL CHECK (outcome IN ('done', 'failed', 'unknown')), - output jsonb, - error text, - recorded_at timestamptz NOT NULL DEFAULT now(), - PRIMARY KEY (run_id, seq) -); - -CREATE TABLE runs.signals ( - run_id uuid NOT NULL REFERENCES runs.runs (id) ON DELETE CASCADE, - key text NOT NULL, - payload jsonb NOT NULL, - received_at timestamptz NOT NULL DEFAULT now(), - PRIMARY KEY (run_id, key) -); - --- +goose Down -DROP SCHEMA runs CASCADE; diff --git a/internal/platform/pg/migrations/00002_connectors.sql b/internal/platform/pg/migrations/00002_connectors.sql new file mode 100644 index 000000000..e1526ee99 --- /dev/null +++ b/internal/platform/pg/migrations/00002_connectors.sql @@ -0,0 +1,49 @@ +-- +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 credential's fields are sealed with age. Each field holds a value or the +-- secret engine key the value is read from. +CREATE TABLE connectors.credentials ( + org_id text NOT NULL, + 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) +); + +-- 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. +CREATE TABLE connectors.catalogue_source ( + org_id text PRIMARY KEY, + location text NOT NULL, + public_key text NOT NULL, + newest_issued_at timestamptz, + updated_at timestamptz NOT NULL DEFAULT now() +); + +-- +goose Down +DROP SCHEMA connectors CASCADE; diff --git a/internal/platform/pg/migrations/00002_connectors_events.sql b/internal/platform/pg/migrations/00002_connectors_events.sql deleted file mode 100644 index 986b09dc6..000000000 --- a/internal/platform/pg/migrations/00002_connectors_events.sql +++ /dev/null @@ -1,70 +0,0 @@ --- +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(), - PRIMARY KEY (org_id, name) -); - -CREATE SCHEMA events; - -CREATE TABLE events.sources ( - org_id text NOT NULL, - name text NOT NULL, - connector text NOT NULL, - token_sha256 text NOT NULL, - created_at timestamptz NOT NULL DEFAULT now(), - updated_at timestamptz NOT NULL DEFAULT now(), - PRIMARY KEY (org_id, name) -); - -CREATE TABLE events.events ( - id uuid PRIMARY KEY, - org_id text NOT NULL, - source text NOT NULL, - type text NOT NULL, - source_event_id text NOT NULL, - link_key text, - linked_to uuid REFERENCES events.events (id), - fields jsonb NOT NULL, - occurred_at timestamptz, - received_at timestamptz NOT NULL DEFAULT now(), - dispatched_at timestamptz, - matched boolean NOT NULL DEFAULT false, - -- Dedupe is this constraint: a re-delivery conflicts instead of racing. - UNIQUE (org_id, source, source_event_id) -); - -CREATE INDEX events_undispatched ON events.events (received_at) WHERE dispatched_at IS NULL; -CREATE INDEX events_link ON events.events (org_id, source, link_key, received_at) WHERE link_key IS NOT NULL; - -CREATE TABLE events.triggers ( - org_id text NOT NULL, - name text NOT NULL, - source text NOT NULL, - event_type text NOT NULL, - filter jsonb NOT NULL DEFAULT '{}', - workflow text NOT NULL, - enabled boolean NOT NULL DEFAULT true, - updated_at timestamptz NOT NULL DEFAULT now(), - PRIMARY KEY (org_id, name) -); - -CREATE TABLE events.dispatches ( - event_id uuid NOT NULL REFERENCES events.events (id), - trigger text NOT NULL, - run_id uuid NOT NULL, - created_at timestamptz NOT NULL DEFAULT now(), - PRIMARY KEY (event_id, trigger) -); - --- +goose Down -DROP SCHEMA events CASCADE; -DROP SCHEMA connectors CASCADE; diff --git a/internal/platform/pg/migrations/00003_events.sql b/internal/platform/pg/migrations/00003_events.sql new file mode 100644 index 000000000..df5b1af8d --- /dev/null +++ b/internal/platform/pg/migrations/00003_events.sql @@ -0,0 +1,125 @@ +-- +goose Up +CREATE SCHEMA events; + +CREATE TABLE events.sources ( + org_id text NOT NULL, + name text NOT NULL, + connector text NOT NULL, + token_sha256 text NOT NULL, + created_at timestamptz NOT NULL DEFAULT now(), + updated_at timestamptz NOT NULL DEFAULT now(), + cursor text NOT NULL DEFAULT '', + key_fields jsonb NOT NULL DEFAULT '[]', + verification text NOT NULL DEFAULT 'token' CHECK (verification IN ('token', 'hmac', 'none')), + hmac_header text NOT NULL DEFAULT '', + hmac_prefix text NOT NULL DEFAULT '', + hmac_secret_sealed bytea NOT NULL DEFAULT '\x'::bytea, + display_name text NOT NULL DEFAULT '', + delivery_header text NOT NULL DEFAULT '', + unsigned_approved boolean NOT NULL DEFAULT false, + capture boolean NOT NULL DEFAULT true, + PRIMARY KEY (org_id, name) +); + +-- An event type identifies one kind of event from a source by its headers and +-- payload paths. schema lists the JSON paths of the example event it was +-- created from. +CREATE TABLE events.event_types ( + id uuid PRIMARY KEY, + org_id text NOT NULL, + source text NOT NULL, + name text NOT NULL, + rule jsonb NOT NULL, + schema jsonb NOT NULL DEFAULT '{}', + created_at timestamptz NOT NULL DEFAULT now(), + updated_at timestamptz NOT NULL DEFAULT now(), + UNIQUE (org_id, source, name) +); + +CREATE TABLE events.events ( + id uuid PRIMARY KEY, + org_id text NOT NULL, + source text NOT NULL, + type text NOT NULL, + source_event_id text NOT NULL, + link_key text, + linked_to uuid REFERENCES events.events (id), + fields jsonb NOT NULL, + occurred_at timestamptz, + received_at timestamptz NOT NULL DEFAULT now(), + dispatched_at timestamptz, + matched boolean NOT NULL DEFAULT false, + own boolean NOT NULL DEFAULT false, + origin_run_id uuid, + group_key text, + group_filter jsonb, + body bytea, + verification text NOT NULL DEFAULT '' CHECK (verification IN ('', 'signed', 'token', 'unsigned')), + headers jsonb, + remote_addr text NOT NULL DEFAULT '', + event_type_id uuid REFERENCES events.event_types (id) ON DELETE SET NULL, + UNIQUE (org_id, source, source_event_id) +); +CREATE INDEX events_undispatched ON events.events (received_at) WHERE (dispatched_at IS NULL); +CREATE INDEX events_link ON events.events (org_id, source, link_key, received_at) WHERE (link_key IS NOT NULL); + +-- A mapping lists the payload paths a workflow reads from events of one event +-- type. +CREATE TABLE events.mappings ( + id uuid PRIMARY KEY, + org_id text NOT NULL, + name text NOT NULL, + source_hint text NOT NULL DEFAULT '', + event_type_id uuid NOT NULL REFERENCES events.event_types (id) ON DELETE RESTRICT, + fields jsonb NOT NULL, + match_count bigint NOT NULL DEFAULT 0, + last_matched_at timestamptz, + created_at timestamptz NOT NULL DEFAULT now(), + updated_at timestamptz NOT NULL DEFAULT now() +); + +CREATE TABLE events.triggers ( + org_id text NOT NULL, + name text NOT NULL, + source text NOT NULL, + event_type text NOT NULL, + filter jsonb NOT NULL DEFAULT '{}', + workflow text NOT NULL, + enabled boolean NOT NULL DEFAULT true, + updated_at timestamptz NOT NULL DEFAULT now(), + schedule_every_s integer CHECK (schedule_every_s >= 60), + schedule_since timestamptz, + approved boolean NOT NULL DEFAULT true, + event_type_id uuid REFERENCES events.event_types (id) ON DELETE RESTRICT, + mapping_id uuid REFERENCES events.mappings (id) ON DELETE RESTRICT, + PRIMARY KEY (org_id, name) +); + +CREATE TABLE events.dispatches ( + event_id uuid NOT NULL REFERENCES events.events (id), + trigger text NOT NULL, + run_id uuid, + created_at timestamptz NOT NULL DEFAULT now(), + outcome text NOT NULL DEFAULT 'started' CHECK (outcome IN ('started', 'failed', 'denied', 'disabled')), + reason jsonb, + PRIMARY KEY (event_id, trigger), + CHECK ((outcome = 'started') = (run_id IS NOT NULL)) +); + +-- A group collects the unmatched events that share a key. Its source, event +-- type and filter describe the trigger it suggests. +CREATE TABLE events.groups ( + id uuid PRIMARY KEY, + org_id text NOT NULL, + key text NOT NULL, + source text NOT NULL, + event_type text NOT NULL, + filter jsonb NOT NULL, + count bigint NOT NULL, + first_seen timestamptz NOT NULL, + last_seen timestamptz NOT NULL, + UNIQUE (org_id, key) +); + +-- +goose Down +DROP SCHEMA events CASCADE; diff --git a/internal/platform/pg/migrations/00003_governance.sql b/internal/platform/pg/migrations/00003_governance.sql deleted file mode 100644 index 2eaba0158..000000000 --- a/internal/platform/pg/migrations/00003_governance.sql +++ /dev/null @@ -1,17 +0,0 @@ --- +goose Up -CREATE SCHEMA governance; - --- A model role is the model an organisation chose for one job. Only the agent --- role exists until guards need the judge role. -CREATE TABLE governance.model_roles ( - org_id text NOT NULL, - role text NOT NULL CHECK (role IN ('agent')), - class text NOT NULL, - base_url text NOT NULL, - model text NOT NULL, - updated_at timestamptz NOT NULL DEFAULT now(), - PRIMARY KEY (org_id, role) -); - --- +goose Down -DROP SCHEMA governance CASCADE; diff --git a/internal/platform/pg/migrations/00004_credentials.sql b/internal/platform/pg/migrations/00004_credentials.sql deleted file mode 100644 index 0d1e6853a..000000000 --- a/internal/platform/pg/migrations/00004_credentials.sql +++ /dev/null @@ -1,21 +0,0 @@ --- +goose Up --- A credential's fields are sealed with age: each holds a value or names the --- secret engine key it is resolved from. -CREATE TABLE connectors.credentials ( - org_id text NOT NULL, - name text NOT NULL, - kind text NOT NULL CHECK (kind IN ('machine', 'age')), - sealed bytea NOT NULL, - updated_at timestamptz NOT NULL DEFAULT now(), - PRIMARY KEY (org_id, name) -); - --- contract says which extension contract an accepted package serves; its --- credentials map each slot it declares to a credential by name. -ALTER TABLE connectors.accepted - ADD COLUMN contract text NOT NULL DEFAULT 'connector' CHECK (contract IN ('connector', 'secretengine')), - ADD COLUMN credentials jsonb NOT NULL DEFAULT '{}'; - --- +goose Down -ALTER TABLE connectors.accepted DROP COLUMN credentials, DROP COLUMN contract; -DROP TABLE connectors.credentials; diff --git a/internal/platform/pg/migrations/00004_runs.sql b/internal/platform/pg/migrations/00004_runs.sql new file mode 100644 index 000000000..0b41e6e3e --- /dev/null +++ b/internal/platform/pg/migrations/00004_runs.sql @@ -0,0 +1,93 @@ +-- +goose Up +CREATE SCHEMA runs; + +CREATE TABLE runs.runs ( + id uuid PRIMARY KEY, + org_id text NOT NULL, + workflow text NOT NULL, + start_key text, + input jsonb NOT NULL, + state text NOT NULL CHECK (state IN ('queued', 'running', 'waiting', 'succeeded', 'failed', 'cancelled', 'parked', 'waiting_approval', 'declined')), + lease_token uuid, + lease_owner text, + lease_expires_at timestamptz, + wake_at timestamptz, + waiting_for text, + cancel_requested boolean NOT NULL DEFAULT false, + attempts integer NOT NULL DEFAULT 0, + result jsonb, + error text, + created_at timestamptz NOT NULL DEFAULT now(), + updated_at timestamptz NOT NULL DEFAULT now(), + finished_at timestamptz, + workflow_version integer NOT NULL DEFAULT 1, + dry boolean NOT NULL DEFAULT false, + overlay jsonb, + detail text, + parent_run_id uuid REFERENCES runs.runs (id) ON DELETE CASCADE, + parent_signal text, + call_depth integer NOT NULL DEFAULT 0, + UNIQUE (org_id, start_key) +); +CREATE INDEX runs_claimable ON runs.runs (created_at) WHERE (state IN ('queued', 'waiting', 'running')); +CREATE INDEX runs_runs_parent ON runs.runs (parent_run_id) WHERE (parent_run_id IS NOT NULL); + +CREATE TABLE runs.steps ( + run_id uuid NOT NULL REFERENCES runs.runs (id) ON DELETE CASCADE, + seq integer NOT NULL, + name text NOT NULL, + outcome text NOT NULL CHECK (outcome IN ('done', 'failed', 'unknown')), + output json, + error text, + recorded_at timestamptz NOT NULL DEFAULT now(), + PRIMARY KEY (run_id, seq) +); + +CREATE TABLE runs.signals ( + run_id uuid NOT NULL REFERENCES runs.runs (id) ON DELETE CASCADE, + key text NOT NULL, + payload jsonb NOT NULL, + received_at timestamptz NOT NULL DEFAULT now(), + PRIMARY KEY (run_id, key) +); + +-- A workflow is a document, stored by name and version. A run records the +-- version it started with and executes that version. +CREATE TABLE runs.workflows ( + org_id text NOT NULL, + name text NOT NULL, + version integer NOT NULL, + body text NOT NULL, + created_by text NOT NULL, + created_at timestamptz NOT NULL DEFAULT now(), + PRIMARY KEY (org_id, name, version) +); + +-- A draft is the unpublished edit of a workflow. It is stored so the edit +-- survives a page reload, and no run uses it until it is published as the next +-- version. revision increments on every write, and a write that specifies an +-- older revision is refused, so two editors can't overwrite each other's +-- changes. base_version is the published version the draft started from, or 0 +-- for a new workflow. +CREATE TABLE runs.workflow_drafts ( + org_id text NOT NULL, + name text NOT NULL, + body text NOT NULL, + base_version integer NOT NULL, + revision integer NOT NULL DEFAULT 1, + updated_by text NOT NULL, + updated_at timestamptz NOT NULL DEFAULT now(), + PRIMARY KEY (org_id, name) +); + +-- A workflow with no row here is enabled. A disabled workflow can't be started +-- by a trigger, by another workflow or manually. +CREATE TABLE runs.workflow_settings ( + org_id text NOT NULL, + name text NOT NULL, + enabled boolean NOT NULL, + PRIMARY KEY (org_id, name) +); + +-- +goose Down +DROP SCHEMA runs CASCADE; diff --git a/internal/platform/pg/migrations/00005_judgement.sql b/internal/platform/pg/migrations/00005_judgement.sql new file mode 100644 index 000000000..743ddb6e1 --- /dev/null +++ b/internal/platform/pg/migrations/00005_judgement.sql @@ -0,0 +1,96 @@ +-- +goose Up +CREATE SCHEMA judgement; + +-- A decision is the judgement of one concrete action, with every verdict in +-- its body. key identifies the attempt to run the action, so a replayed step +-- reads the existing decision instead of judging the action again. connector, action, target_kind and environment +-- identify the action type the decision counts towards. +CREATE TABLE judgement.decisions ( + id uuid PRIMARY KEY, + org_id text NOT NULL, + run_id uuid NOT NULL, + key text NOT NULL, + connector text NOT NULL, + action text NOT NULL, + target_kind text NOT NULL, + environment text NOT NULL, + level text NOT NULL CHECK (level IN ('act_quietly', 'act_and_tell', 'propose_and_wait', 'escalate', 'never')), + body json NOT NULL, + created_at timestamptz NOT NULL DEFAULT now(), + dry boolean NOT NULL DEFAULT false, + UNIQUE (org_id, key) +); +CREATE INDEX decisions_by_run ON judgement.decisions (org_id, run_id, created_at); +CREATE INDEX decisions_by_class ON judgement.decisions (org_id, connector, action, target_kind, environment, created_at); + +CREATE TABLE judgement.action_types ( + id uuid PRIMARY KEY, + org_id text NOT NULL, + connector text NOT NULL, + action text NOT NULL, + target_kind text NOT NULL, + environment text NOT NULL, + read_only boolean NOT NULL, + level text NOT NULL CHECK (level IN ('act_quietly', 'act_and_tell', 'propose_and_wait', 'escalate', 'never')), + created_at timestamptz NOT NULL DEFAULT now(), + reset_at timestamptz, + tightened_at timestamptz, + tightened_because text, + UNIQUE (org_id, connector, action, target_kind, environment) +); + +CREATE TABLE judgement.action_type_resets ( + id uuid PRIMARY KEY, + org_id text NOT NULL, + action_type_id uuid NOT NULL REFERENCES judgement.action_types (id), + from_level text NOT NULL, + to_level text NOT NULL, + by text NOT NULL, + at timestamptz NOT NULL DEFAULT now() +); +CREATE INDEX action_type_resets_by_action_type ON judgement.action_type_resets (action_type_id, at); + +CREATE TABLE judgement.action_type_approvals ( + id uuid PRIMARY KEY, + org_id text NOT NULL, + action_type_id uuid NOT NULL REFERENCES judgement.action_types (id), + decision_id uuid NOT NULL UNIQUE, + by text NOT NULL, + by_id text NOT NULL DEFAULT '', + at timestamptz NOT NULL, + counted boolean NOT NULL DEFAULT true +); +CREATE INDEX action_type_approvals_counted ON judgement.action_type_approvals (action_type_id, at) WHERE counted; + +CREATE TABLE judgement.autonomy_proposals ( + id uuid PRIMARY KEY, + org_id text NOT NULL, + action_type_id uuid NOT NULL REFERENCES judgement.action_types (id), + from_level text NOT NULL, + to_level text NOT NULL, + evidence jsonb NOT NULL, + state text NOT NULL CHECK (state IN ('pending', 'approved', 'denied', 'expired', 'superseded')), + deadline timestamptz NOT NULL, + proposed_at timestamptz NOT NULL, + answered_by text, + answered_at timestamptz, + answered_by_id text +); +CREATE UNIQUE INDEX autonomy_proposals_one_pending ON judgement.autonomy_proposals (action_type_id) WHERE (state = 'pending'); +CREATE INDEX autonomy_proposals_by_action_type ON judgement.autonomy_proposals (action_type_id, proposed_at); + +-- Hard rules and change-request rules are Cedar policy sets, one per kind per +-- organisation. Every version is kept, and the highest version is in force. +CREATE TABLE judgement.policy_sets ( + id uuid PRIMARY KEY, + org_id text NOT NULL, + kind text NOT NULL CHECK (kind IN ('hard_rules', 'change_request')), + version integer NOT NULL, + body text NOT NULL, + created_by text NOT NULL, + created_at timestamptz NOT NULL DEFAULT now(), + UNIQUE (org_id, kind, version) +); + +-- +goose Down +DROP SCHEMA judgement CASCADE; diff --git a/internal/platform/pg/migrations/00005_mcp_servers.sql b/internal/platform/pg/migrations/00005_mcp_servers.sql deleted file mode 100644 index e9081737e..000000000 --- a/internal/platform/pg/migrations/00005_mcp_servers.sql +++ /dev/null @@ -1,29 +0,0 @@ --- +goose Up --- An accepted MCP server runs as published: a stdio binary with its arguments, --- or an HTTP endpoint. Tools are what its acceptance declares it may offer. -ALTER TABLE connectors.accepted - ADD COLUMN args jsonb NOT NULL DEFAULT '[]', - ADD COLUMN url text NOT NULL DEFAULT '', - ADD COLUMN tools jsonb NOT NULL DEFAULT '[]'; - -ALTER TABLE connectors.accepted DROP CONSTRAINT accepted_contract_check; -ALTER TABLE connectors.accepted - ADD CONSTRAINT accepted_contract_check - CHECK (contract IN ('connector', 'secretengine', 'mcp')); - --- An MCP server's credential slot is an HTTP auth scheme: basic or bearer. -ALTER TABLE connectors.credentials DROP CONSTRAINT credentials_kind_check; -ALTER TABLE connectors.credentials - ADD CONSTRAINT credentials_kind_check - CHECK (kind IN ('machine', 'age', 'basic', 'bearer')); - --- +goose Down -ALTER TABLE connectors.credentials DROP CONSTRAINT credentials_kind_check; -ALTER TABLE connectors.credentials - ADD CONSTRAINT credentials_kind_check CHECK (kind IN ('machine', 'age')); - -ALTER TABLE connectors.accepted DROP CONSTRAINT accepted_contract_check; -ALTER TABLE connectors.accepted - ADD CONSTRAINT accepted_contract_check CHECK (contract IN ('connector', 'secretengine')); - -ALTER TABLE connectors.accepted DROP COLUMN tools, DROP COLUMN url, DROP COLUMN args; diff --git a/internal/platform/pg/migrations/00006_approvals.sql b/internal/platform/pg/migrations/00006_approvals.sql new file mode 100644 index 000000000..09ffc5272 --- /dev/null +++ b/internal/platform/pg/migrations/00006_approvals.sql @@ -0,0 +1,52 @@ +-- +goose Up +CREATE SCHEMA approvals; + +-- A request is one judged action waiting for a person's answer. key identifies +-- the run's wait that the answer resumes. The decision columns bind the answer +-- to the judged action, so it can't be applied to a different action. +CREATE TABLE approvals.requests ( + id uuid PRIMARY KEY, + org_id text NOT NULL, + run_id uuid NOT NULL, + key text NOT NULL, + decision_id uuid NOT NULL, + digest text NOT NULL, + level text NOT NULL CHECK (level IN ('act_quietly', 'act_and_tell', 'propose_and_wait', 'escalate', 'never')), + summary text NOT NULL, + state text NOT NULL CHECK (state IN ('pending', 'approved', 'denied', 'expired', 'discarded')), + deadline timestamptz NOT NULL, + created_at timestamptz NOT NULL DEFAULT now(), + answered_at timestamptz, + answered_by text, + answered_via text, + connector text NOT NULL, + action text NOT NULL, + target_kind text NOT NULL, + target_id text NOT NULL, + kind text NOT NULL DEFAULT 'action' CHECK (kind IN ('action', 'autonomy')), + step integer NOT NULL DEFAULT 0, + step_deadline timestamptz, + change_request boolean NOT NULL DEFAULT false, + counted boolean NOT NULL DEFAULT false, + discarded_because text, + irreversible boolean NOT NULL DEFAULT false, + answered_by_id text, + UNIQUE (org_id, run_id, key) +); +CREATE INDEX requests_by_state ON approvals.requests (org_id, state, created_at); + +-- An answer that was refused, and the reason it was refused, so a request that +-- was never resolved can be explained. +CREATE TABLE approvals.refusals ( + id uuid PRIMARY KEY, + org_id text NOT NULL, + request_id uuid NOT NULL REFERENCES approvals.requests (id) ON DELETE CASCADE, + identity text NOT NULL, + via text NOT NULL, + reason text NOT NULL, + at timestamptz NOT NULL DEFAULT now() +); +CREATE INDEX refusals_by_request ON approvals.refusals (org_id, request_id, at); + +-- +goose Down +DROP SCHEMA approvals CASCADE; diff --git a/internal/platform/pg/migrations/00006_deliveries.sql b/internal/platform/pg/migrations/00006_deliveries.sql deleted file mode 100644 index a8e5c52bb..000000000 --- a/internal/platform/pg/migrations/00006_deliveries.sql +++ /dev/null @@ -1,23 +0,0 @@ --- +goose Up -CREATE SCHEMA deliveries; - --- A message is one notification on one channel. The composer reads its --- messages straight from here; channels that send (Telegram) get delivery --- attempts when they land. -CREATE TABLE deliveries.messages ( - seq bigserial PRIMARY KEY, - id uuid NOT NULL UNIQUE, - org_id text NOT NULL, - channel text NOT NULL CHECK (channel IN ('composer')), - kind text NOT NULL CHECK (kind IN ('notification')), - run_id uuid, - title text NOT NULL, - body text NOT NULL, - -- key makes a notification idempotent: a replayed step can't send twice. - key text NOT NULL, - created_at timestamptz NOT NULL DEFAULT now(), - UNIQUE (org_id, channel, key) -); - --- +goose Down -DROP SCHEMA deliveries CASCADE; diff --git a/internal/platform/pg/migrations/00007_deliveries.sql b/internal/platform/pg/migrations/00007_deliveries.sql new file mode 100644 index 000000000..df7e7277c --- /dev/null +++ b/internal/platform/pg/migrations/00007_deliveries.sql @@ -0,0 +1,46 @@ +-- +goose Up +CREATE SCHEMA deliveries; + +-- A message is one notification on one channel. The composer reads its +-- messages from this table. A channel that sends messages, such as Telegram, +-- records each delivery attempt in deliveries.attempts. +CREATE TABLE deliveries.messages ( + seq bigserial PRIMARY KEY, + id uuid NOT NULL UNIQUE, + org_id text NOT NULL, + channel text NOT NULL CHECK (channel ~ '^[a-z][a-z0-9-]{0,62}$'), + kind text NOT NULL CHECK (kind IN ('notification', 'approval_request', 'approval_answer')), + run_id uuid, + title text NOT NULL, + body text NOT NULL, + key text NOT NULL, + created_at timestamptz NOT NULL DEFAULT now(), + request_id uuid, + expires_at timestamptz, + attempts integer NOT NULL DEFAULT 0, + next_attempt_at timestamptz NOT NULL DEFAULT now(), + sent_at timestamptz, + external_id text, + last_error text, + failed_at timestamptz, + UNIQUE (org_id, channel, key) +); +CREATE INDEX messages_external ON deliveries.messages (org_id, channel, external_id) WHERE (external_id IS NOT NULL); +CREATE INDEX messages_due ON deliveries.messages (next_attempt_at) WHERE ((sent_at IS NULL) AND (failed_at IS NULL)); + +-- One row per delivery attempt, so every channel response and failure is kept, +-- not only the last error. Rows are never pruned: the volume is low, and the +-- attempts are the audit record of a delivery. +CREATE TABLE deliveries.attempts ( + message_seq bigint NOT NULL REFERENCES deliveries.messages (seq) ON DELETE CASCADE, + attempt integer NOT NULL, + at timestamptz NOT NULL DEFAULT now(), + ok boolean NOT NULL, + response text NOT NULL DEFAULT '', + error text NOT NULL DEFAULT '', + duration_ms bigint NOT NULL, + PRIMARY KEY (message_seq, attempt) +); + +-- +goose Down +DROP SCHEMA deliveries CASCADE; diff --git a/internal/platform/pg/migrations/00007_steps_output_json.sql b/internal/platform/pg/migrations/00007_steps_output_json.sql deleted file mode 100644 index 3d40173e8..000000000 --- a/internal/platform/pg/migrations/00007_steps_output_json.sql +++ /dev/null @@ -1,8 +0,0 @@ --- +goose Up --- A step's output must replay byte for byte: jsonb reorders keys and drops --- whitespace, so a pinned value (a tool schema) read back differed from the --- one a live attempt used, and a resumed agent diverged from its journal. -ALTER TABLE runs.steps ALTER COLUMN output TYPE json USING output::json; - --- +goose Down -ALTER TABLE runs.steps ALTER COLUMN output TYPE jsonb USING output::jsonb; diff --git a/internal/platform/pg/migrations/00008_drafts.sql b/internal/platform/pg/migrations/00008_drafts.sql new file mode 100644 index 000000000..e99791f21 --- /dev/null +++ b/internal/platform/pg/migrations/00008_drafts.sql @@ -0,0 +1,26 @@ +-- +goose Up +CREATE SCHEMA drafts; + +CREATE TABLE drafts.drafts ( + id uuid PRIMARY KEY, + org_id text NOT NULL, + group_id uuid NOT NULL, + name text NOT NULL, + source text NOT NULL, + event_type text NOT NULL, + filter jsonb NOT NULL, + document text NOT NULL, + policy_kind text NOT NULL CHECK (policy_kind IN ('hard_rules', 'change_request')), + policy text NOT NULL, + dry_run_id uuid, + dry_event_id uuid, + installed_version integer, + published_at timestamptz, + created_at timestamptz NOT NULL DEFAULT now(), + updated_at timestamptz NOT NULL DEFAULT now(), + UNIQUE (org_id, group_id), + UNIQUE (org_id, name) +); + +-- +goose Down +DROP SCHEMA drafts CASCADE; diff --git a/internal/platform/pg/migrations/00008_judgement.sql b/internal/platform/pg/migrations/00008_judgement.sql deleted file mode 100644 index a52bf0aba..000000000 --- a/internal/platform/pg/migrations/00008_judgement.sql +++ /dev/null @@ -1,27 +0,0 @@ --- +goose Up -CREATE SCHEMA judgement; - --- A decision is the judgement of one concrete action, with every verdict in --- its body. key identifies the attempt to run it, so a replayed step finds the --- decision already made. The class columns are what earned autonomy is --- counted against. -CREATE TABLE judgement.decisions ( - id uuid PRIMARY KEY, - org_id text NOT NULL, - run_id uuid NOT NULL, - key text NOT NULL, - connector text NOT NULL, - action text NOT NULL, - target_kind text NOT NULL, - environment text NOT NULL, - level text NOT NULL CHECK (level IN ('act_quietly', 'act_and_tell', 'propose_and_wait', 'escalate', 'never')), - body json NOT NULL, - created_at timestamptz NOT NULL DEFAULT now(), - UNIQUE (org_id, key) -); - -CREATE INDEX decisions_by_run ON judgement.decisions (org_id, run_id, created_at); -CREATE INDEX decisions_by_class ON judgement.decisions (org_id, connector, action, target_kind, environment, created_at); - --- +goose Down -DROP SCHEMA judgement CASCADE; diff --git a/internal/platform/pg/migrations/00009_approvals_requests.sql b/internal/platform/pg/migrations/00009_approvals_requests.sql deleted file mode 100644 index 53e3889cb..000000000 --- a/internal/platform/pg/migrations/00009_approvals_requests.sql +++ /dev/null @@ -1,28 +0,0 @@ --- +goose Up -CREATE SCHEMA approvals; - --- A request is one judged action waiting for a person's answer. key is the run --- wait an answer wakes, and the decision columns bind the answer to the action --- that was judged, so it can't be replayed onto another one. -CREATE TABLE approvals.requests ( - id uuid PRIMARY KEY, - org_id text NOT NULL, - run_id uuid NOT NULL, - key text NOT NULL, - decision_id uuid NOT NULL, - digest text NOT NULL, - level text NOT NULL CHECK (level IN ('act_quietly', 'act_and_tell', 'propose_and_wait', 'escalate', 'never')), - summary text NOT NULL, - state text NOT NULL CHECK (state IN ('pending', 'approved', 'denied', 'expired')), - deadline timestamptz NOT NULL, - created_at timestamptz NOT NULL DEFAULT now(), - answered_at timestamptz, - answered_by text, - answered_via text, - UNIQUE (org_id, run_id, key) -); - -CREATE INDEX requests_by_state ON approvals.requests (org_id, state, created_at); - --- +goose Down -DROP SCHEMA approvals CASCADE; diff --git a/internal/platform/pg/migrations/00009_chat.sql b/internal/platform/pg/migrations/00009_chat.sql new file mode 100644 index 000000000..04e1dbdb7 --- /dev/null +++ b/internal/platform/pg/migrations/00009_chat.sql @@ -0,0 +1,75 @@ +-- +goose Up +CREATE SCHEMA chat; + +-- A session is one conversation between the operator and Archie. +CREATE TABLE chat.sessions ( + id uuid PRIMARY KEY, + org_id text NOT NULL, + title text NOT NULL DEFAULT '', + created_at timestamptz NOT NULL DEFAULT now(), + updated_at timestamptz NOT NULL DEFAULT now(), + owner_id text NOT NULL, + tainted boolean NOT NULL DEFAULT false +); +CREATE INDEX sessions_by_owner ON chat.sessions (org_id, owner_id, updated_at DESC); + +-- A turn is one message from the operator and Archie's reply to it. It is +-- stored before it is acknowledged and queued until the turn before it in the +-- 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 +); +CREATE INDEX turns_by_session ON chat.turns (session_id, seq); +CREATE INDEX turns_queued ON chat.turns (seq) WHERE (state IN ('queued', 'running')); + +-- An event is one item a turn streamed: reasoning, reply text, a tool call or +-- its result, an approval request, or how the turn ended. A client resumes the +-- stream by reading the events after the last seq it received. +CREATE TABLE chat.events ( + seq bigserial PRIMARY KEY, + turn_id uuid NOT NULL REFERENCES chat.turns (id) ON DELETE CASCADE, + kind text NOT NULL, + data jsonb NOT NULL, + created_at timestamptz NOT NULL DEFAULT now() +); +CREATE INDEX events_by_turn ON chat.events (turn_id, seq); + +-- An action is a tool call by a turn's agent that judgement held for approval. +-- The answer arrives through approvals, and an approved action runs once. +CREATE TABLE chat.actions ( + id uuid PRIMARY KEY, + org_id text NOT NULL, + turn_id uuid NOT NULL REFERENCES chat.turns (id) ON DELETE CASCADE, + key text NOT NULL, + decision jsonb NOT NULL, + request_id uuid NOT NULL, + path jsonb NOT NULL, + step integer NOT NULL DEFAULT 0, + step_due timestamptz NOT NULL, + state text NOT NULL CHECK (state IN ('waiting', 'approved', 'running', 'succeeded', 'failed', 'unknown', 'denied', 'expired')), + answer jsonb, + result text NOT NULL DEFAULT '', + created_at timestamptz NOT NULL DEFAULT now(), + binding jsonb, + binding_digest text NOT NULL DEFAULT '', + UNIQUE (turn_id, key) +); +CREATE INDEX actions_open ON chat.actions (state) WHERE (state IN ('waiting', 'approved', 'running')); + +-- +goose Down +DROP SCHEMA chat CASCADE; diff --git a/internal/platform/pg/migrations/00010_deliveries_approval_requests.sql b/internal/platform/pg/migrations/00010_deliveries_approval_requests.sql deleted file mode 100644 index cb571f1e4..000000000 --- a/internal/platform/pg/migrations/00010_deliveries_approval_requests.sql +++ /dev/null @@ -1,15 +0,0 @@ --- +goose Up --- An approval request is a message a person answers, so a message grows the --- request it carries and when that request runs out of time. -ALTER TABLE deliveries.messages DROP CONSTRAINT messages_kind_check; -ALTER TABLE deliveries.messages ADD CONSTRAINT messages_kind_check - CHECK (kind IN ('notification', 'approval_request', 'approval_answer')); -ALTER TABLE deliveries.messages ADD COLUMN request_id uuid; -ALTER TABLE deliveries.messages ADD COLUMN expires_at timestamptz; - --- +goose Down -ALTER TABLE deliveries.messages DROP COLUMN expires_at; -ALTER TABLE deliveries.messages DROP COLUMN request_id; -ALTER TABLE deliveries.messages DROP CONSTRAINT messages_kind_check; -ALTER TABLE deliveries.messages ADD CONSTRAINT messages_kind_check - CHECK (kind IN ('notification')); diff --git a/internal/platform/pg/migrations/00010_memory.sql b/internal/platform/pg/migrations/00010_memory.sql new file mode 100644 index 000000000..93771f7a7 --- /dev/null +++ b/internal/platform/pg/migrations/00010_memory.sql @@ -0,0 +1,91 @@ +-- +goose Up +CREATE SCHEMA memory; + +-- An incident is one investigation of one target: what the event was, the cause +-- the investigation found and what was done about it. The row is created when +-- the cause is recorded, and its actions are added when the run settles them. +CREATE TABLE memory.incidents ( + id uuid PRIMARY KEY, + org_id text NOT NULL, + run_id uuid NOT NULL, + target_kind text NOT NULL, + target_id text NOT NULL, + cause text NOT NULL, + summary text NOT NULL, + event jsonb NOT NULL, + actions jsonb NOT NULL DEFAULT '[]', + at timestamptz NOT NULL DEFAULT now(), + UNIQUE (org_id, run_id) +); +CREATE INDEX incidents_by_target ON memory.incidents (org_id, target_kind, target_id, at DESC); + +-- A proposal asks for a root-cause fix when the same cause recurs on a target. +-- There is at most one per target and cause. +CREATE TABLE memory.proposals ( + id uuid PRIMARY KEY, + org_id text NOT NULL, + target_kind text NOT NULL, + target_id text NOT NULL, + cause text NOT NULL, + incidents integer NOT NULL, + body text NOT NULL, + created_at timestamptz NOT NULL DEFAULT now(), + UNIQUE (org_id, target_kind, target_id, cause) +); + +-- A note is one item of Archie's memory. class is its kind: working context for +-- one chat session, episodic history or enterprise retrieval in a collection, +-- or a durable fact. +CREATE TABLE memory.notes ( + id uuid PRIMARY KEY, + org_id text NOT NULL, + class text NOT NULL CHECK (class IN ('working_context', 'episodic_history', 'enterprise_retrieval', 'durable_fact')), + scope text NOT NULL CHECK (scope IN ('person', 'shared')), + state text NOT NULL CHECK (state IN ('pending', 'published')), + owner_id text NOT NULL, + shared_by text NOT NULL DEFAULT '', + purpose text NOT NULL CHECK (purpose <> ''), + text text NOT NULL CHECK (text <> ''), + session_id uuid, + collection text, + disabled_at timestamptz, + retain_until timestamptz, + version integer NOT NULL DEFAULT 0, + created_at timestamptz NOT NULL DEFAULT now(), + updated_at timestamptz NOT NULL DEFAULT now(), + CHECK ((scope <> 'person') OR (state = 'published')), + CHECK ((scope <> 'person') OR (owner_id <> '')), + CHECK ((scope <> 'shared') OR (shared_by <> '')), + CHECK (((state = 'pending') AND (version = 0)) OR ((state = 'published') AND (version > 0))), + CHECK ((class <> 'working_context') OR (session_id IS NOT NULL)), + CHECK ((class <> 'episodic_history') OR (collection IS NOT NULL)), + CHECK ((class <> 'enterprise_retrieval') OR (collection IS NOT NULL)), + CHECK ((class <> 'durable_fact') OR ((session_id IS NULL) AND (collection IS NULL))) +); +CREATE INDEX notes_by_person ON memory.notes (org_id, owner_id, class, created_at DESC) WHERE (scope = 'person'); +CREATE INDEX notes_published ON memory.notes (org_id, class, created_at DESC) WHERE ((scope = 'shared') AND (state = 'published')); +CREATE INDEX notes_pending ON memory.notes (org_id, created_at) WHERE ((scope = 'shared') AND (state = 'pending')); + +-- Every published version of a shared note, so a publication can be rolled +-- back and its publisher and purpose read. +CREATE TABLE memory.note_versions ( + note_id uuid NOT NULL REFERENCES memory.notes (id) ON DELETE CASCADE, + version integer NOT NULL CHECK (version > 0), + text text NOT NULL, + purpose text NOT NULL, + publisher_id text NOT NULL, + published_at timestamptz NOT NULL DEFAULT now(), + PRIMARY KEY (note_id, version) +); + +-- Each committed deletion is listed here. The deletion record kept outside the +-- database must contain every deletion listed here, so a restore detects a +-- missing or incomplete record instead of trusting it. +CREATE TABLE memory.deletion_witness ( + note_id uuid PRIMARY KEY, + org_id text NOT NULL, + at timestamptz NOT NULL DEFAULT now() +); + +-- +goose Down +DROP SCHEMA memory CASCADE; diff --git a/internal/platform/pg/migrations/00011_governance_settings.sql b/internal/platform/pg/migrations/00011_governance_settings.sql deleted file mode 100644 index 96f370462..000000000 --- a/internal/platform/pg/migrations/00011_governance_settings.sql +++ /dev/null @@ -1,12 +0,0 @@ --- +goose Up --- An organisation's settings are one row: what Archie does when nobody has --- decided otherwise. A missing row means every field keeps its default. -CREATE TABLE governance.settings ( - org_id text PRIMARY KEY, - -- approval_deadline is how long an approval request waits for an answer. - approval_deadline interval NOT NULL, - updated_at timestamptz NOT NULL DEFAULT now() -); - --- +goose Down -DROP TABLE governance.settings; diff --git a/internal/platform/pg/migrations/00012_governance_approval_deadline_ms.sql b/internal/platform/pg/migrations/00012_governance_approval_deadline_ms.sql deleted file mode 100644 index caabfcf97..000000000 --- a/internal/platform/pg/migrations/00012_governance_approval_deadline_ms.sql +++ /dev/null @@ -1,11 +0,0 @@ --- +goose Up --- A Postgres interval scans as text, so it can't be read straight into a Go --- duration. Milliseconds are exact and scan as a number. The default fills a --- row written before this change; it is dropped straight after. -ALTER TABLE governance.settings DROP COLUMN approval_deadline; -ALTER TABLE governance.settings ADD COLUMN approval_deadline_ms bigint NOT NULL DEFAULT 86400000; -ALTER TABLE governance.settings ALTER COLUMN approval_deadline_ms DROP DEFAULT; - --- +goose Down -ALTER TABLE governance.settings DROP COLUMN approval_deadline_ms; -ALTER TABLE governance.settings ADD COLUMN approval_deadline interval NOT NULL DEFAULT '24 hours'; diff --git a/internal/platform/pg/migrations/00013_approvals_request_action.sql b/internal/platform/pg/migrations/00013_approvals_request_action.sql deleted file mode 100644 index b052f2a09..000000000 --- a/internal/platform/pg/migrations/00013_approvals_request_action.sql +++ /dev/null @@ -1,17 +0,0 @@ --- +goose Up --- A request is read back without the decision it came from, so it keeps the --- action's own names: what the person is shown has to survive a restart. -ALTER TABLE approvals.requests ADD COLUMN connector text NOT NULL DEFAULT ''; -ALTER TABLE approvals.requests ADD COLUMN action text NOT NULL DEFAULT ''; -ALTER TABLE approvals.requests ADD COLUMN target_kind text NOT NULL DEFAULT ''; -ALTER TABLE approvals.requests ADD COLUMN target_id text NOT NULL DEFAULT ''; -ALTER TABLE approvals.requests ALTER COLUMN connector DROP DEFAULT; -ALTER TABLE approvals.requests ALTER COLUMN action DROP DEFAULT; -ALTER TABLE approvals.requests ALTER COLUMN target_kind DROP DEFAULT; -ALTER TABLE approvals.requests ALTER COLUMN target_id DROP DEFAULT; - --- +goose Down -ALTER TABLE approvals.requests DROP COLUMN connector; -ALTER TABLE approvals.requests DROP COLUMN action; -ALTER TABLE approvals.requests DROP COLUMN target_kind; -ALTER TABLE approvals.requests DROP COLUMN target_id; diff --git a/internal/platform/pg/migrations/00014_deliveries_channels.sql b/internal/platform/pg/migrations/00014_deliveries_channels.sql deleted file mode 100644 index 0622b4b12..000000000 --- a/internal/platform/pg/migrations/00014_deliveries_channels.sql +++ /dev/null @@ -1,28 +0,0 @@ --- +goose Up --- A message goes to the composer and to every accepted connector that delivers --- messages, one row per channel. A row a connector hasn't confirmed is retried --- from here, so a channel that is down delays a message rather than losing it. -ALTER TABLE deliveries.messages DROP CONSTRAINT messages_channel_check; -ALTER TABLE deliveries.messages ADD CONSTRAINT messages_channel_check - CHECK (channel ~ '^[a-z][a-z0-9-]{0,62}$'); -ALTER TABLE deliveries.messages - ADD COLUMN attempts int NOT NULL DEFAULT 0, - ADD COLUMN next_attempt_at timestamptz NOT NULL DEFAULT now(), - ADD COLUMN sent_at timestamptz, - ADD COLUMN external_id text, - ADD COLUMN last_error text; -UPDATE deliveries.messages SET sent_at = created_at; -CREATE INDEX messages_due ON deliveries.messages (next_attempt_at) WHERE sent_at IS NULL; - --- +goose Down -DROP INDEX deliveries.messages_due; -DELETE FROM deliveries.messages WHERE channel <> 'composer'; -ALTER TABLE deliveries.messages - DROP COLUMN last_error, - DROP COLUMN external_id, - DROP COLUMN sent_at, - DROP COLUMN next_attempt_at, - DROP COLUMN attempts; -ALTER TABLE deliveries.messages DROP CONSTRAINT messages_channel_check; -ALTER TABLE deliveries.messages ADD CONSTRAINT messages_channel_check - CHECK (channel IN ('composer')); diff --git a/internal/platform/pg/migrations/00015_events_source_cursor.sql b/internal/platform/pg/migrations/00015_events_source_cursor.sql deleted file mode 100644 index 17efe1b68..000000000 --- a/internal/platform/pg/migrations/00015_events_source_cursor.sql +++ /dev/null @@ -1,7 +0,0 @@ --- +goose Up --- A source whose connector listens resumes from the cursor after the last --- events it stored. -ALTER TABLE events.sources ADD COLUMN cursor text NOT NULL DEFAULT ''; - --- +goose Down -ALTER TABLE events.sources DROP COLUMN cursor; diff --git a/internal/platform/pg/migrations/00019_events_schedules.sql b/internal/platform/pg/migrations/00019_events_schedules.sql deleted file mode 100644 index 12fbeca47..000000000 --- a/internal/platform/pg/migrations/00019_events_schedules.sql +++ /dev/null @@ -1,9 +0,0 @@ --- +goose Up --- A trigger may fire on an interval instead of on an event. since is when the --- schedule began, so a new schedule doesn't fire the slot it was created in. -ALTER TABLE events.triggers - ADD COLUMN schedule_every_s integer CHECK (schedule_every_s >= 60), - ADD COLUMN schedule_since timestamptz; - --- +goose Down -ALTER TABLE events.triggers DROP COLUMN schedule_since, DROP COLUMN schedule_every_s; diff --git a/internal/platform/pg/migrations/00020_judgement_classes.sql b/internal/platform/pg/migrations/00020_judgement_classes.sql deleted file mode 100644 index efe203e59..000000000 --- a/internal/platform/pg/migrations/00020_judgement_classes.sql +++ /dev/null @@ -1,36 +0,0 @@ --- +goose Up --- An action class is what earned autonomy belongs to: one connector's action on --- one kind of target in one environment. A class is made the first time an --- action of it is judged, at the level its kind of action starts at, and its --- decisions stay in judgement.decisions under the same four names. A target --- that moves environment is a different class, which starts as any new one. -CREATE TABLE judgement.classes ( - id uuid PRIMARY KEY, - org_id text NOT NULL, - connector text NOT NULL, - action text NOT NULL, - target_kind text NOT NULL, - environment text NOT NULL, - read_only boolean NOT NULL, - level text NOT NULL CHECK (level IN ('act_quietly', 'act_and_tell', 'propose_and_wait', 'escalate', 'never')), - created_at timestamptz NOT NULL DEFAULT now(), - reset_at timestamptz, - UNIQUE (org_id, connector, action, target_kind, environment) -); - --- A reset returns a class to its default and keeps what led there. -CREATE TABLE judgement.class_resets ( - id uuid PRIMARY KEY, - org_id text NOT NULL, - class_id uuid NOT NULL REFERENCES judgement.classes (id), - from_level text NOT NULL, - to_level text NOT NULL, - by text NOT NULL, - at timestamptz NOT NULL DEFAULT now() -); - -CREATE INDEX class_resets_by_class ON judgement.class_resets (class_id, at); - --- +goose Down -DROP TABLE judgement.class_resets; -DROP TABLE judgement.classes; diff --git a/internal/platform/pg/migrations/00021_judgement_autonomy.sql b/internal/platform/pg/migrations/00021_judgement_autonomy.sql deleted file mode 100644 index f377543e0..000000000 --- a/internal/platform/pg/migrations/00021_judgement_autonomy.sql +++ /dev/null @@ -1,52 +0,0 @@ --- +goose Up --- A class earns autonomy from consecutive approvals. Each approval is kept as --- evidence and stops counting when the class is tightened or raised. -ALTER TABLE judgement.classes ADD COLUMN tightened_at timestamptz; -ALTER TABLE judgement.classes ADD COLUMN tightened_because text; - -CREATE TABLE judgement.class_approvals ( - id uuid PRIMARY KEY, - org_id text NOT NULL, - class_id uuid NOT NULL REFERENCES judgement.classes (id), - decision_id uuid NOT NULL UNIQUE, - by text NOT NULL, - at timestamptz NOT NULL, - counted boolean NOT NULL DEFAULT true -); - -CREATE INDEX class_approvals_counted ON judgement.class_approvals (class_id, at) WHERE counted; - --- A raise is Archie's proposal to loosen a class by one level. It is asked of a --- person as an approval request, and keeps the approvals that earned it. -CREATE TABLE judgement.class_raises ( - id uuid PRIMARY KEY, - org_id text NOT NULL, - class_id uuid NOT NULL REFERENCES judgement.classes (id), - from_level text NOT NULL, - to_level text NOT NULL, - evidence jsonb NOT NULL, - state text NOT NULL CHECK (state IN ('pending', 'approved', 'denied', 'expired', 'superseded')), - deadline timestamptz NOT NULL, - proposed_at timestamptz NOT NULL, - answered_by text, - answered_at timestamptz -); - -CREATE UNIQUE INDEX class_raises_one_pending ON judgement.class_raises (class_id) WHERE state = 'pending'; -CREATE INDEX class_raises_by_class ON judgement.class_raises (class_id, proposed_at); - --- An approval request is either for an action or for a raise; for a raise, --- decision_id holds the raise's id and no run waits on it. -ALTER TABLE approvals.requests ADD COLUMN kind text NOT NULL DEFAULT 'action' CHECK (kind IN ('action', 'raise')); - -ALTER TABLE governance.settings ADD COLUMN raise_after integer NOT NULL DEFAULT 3 CHECK (raise_after >= 1); -ALTER TABLE governance.settings ADD COLUMN frozen boolean NOT NULL DEFAULT false; - --- +goose Down -ALTER TABLE governance.settings DROP COLUMN frozen; -ALTER TABLE governance.settings DROP COLUMN raise_after; -ALTER TABLE approvals.requests DROP COLUMN kind; -DROP TABLE judgement.class_raises; -DROP TABLE judgement.class_approvals; -ALTER TABLE judgement.classes DROP COLUMN tightened_because; -ALTER TABLE judgement.classes DROP COLUMN tightened_at; diff --git a/internal/platform/pg/migrations/00022_memory_incidents.sql b/internal/platform/pg/migrations/00022_memory_incidents.sql deleted file mode 100644 index ee48a87e7..000000000 --- a/internal/platform/pg/migrations/00022_memory_incidents.sql +++ /dev/null @@ -1,41 +0,0 @@ --- +goose Up -CREATE SCHEMA memory; - --- An incident is one investigation of one target: what the event was, the cause --- the investigation found and what was done about it. The row is made when the --- cause is recorded and gets its actions when the run settles them. -CREATE TABLE memory.incidents ( - id uuid PRIMARY KEY, - org_id text NOT NULL, - run_id uuid NOT NULL, - target_kind text NOT NULL, - target_id text NOT NULL, - cause text NOT NULL, - summary text NOT NULL, - event jsonb NOT NULL, - actions jsonb NOT NULL DEFAULT '[]', - at timestamptz NOT NULL DEFAULT now(), - UNIQUE (org_id, run_id) -); - -CREATE INDEX incidents_by_target ON memory.incidents (org_id, target_kind, target_id, at DESC); - --- A proposal is the root-cause fix asked for once a cause keeps coming back on --- a target. One per target and cause. -CREATE TABLE memory.proposals ( - id uuid PRIMARY KEY, - org_id text NOT NULL, - target_kind text NOT NULL, - target_id text NOT NULL, - cause text NOT NULL, - incidents integer NOT NULL, - body text NOT NULL, - created_at timestamptz NOT NULL DEFAULT now(), - UNIQUE (org_id, target_kind, target_id, cause) -); - -ALTER TABLE governance.settings ADD COLUMN recurrence_threshold integer NOT NULL DEFAULT 5; - --- +goose Down -ALTER TABLE governance.settings DROP COLUMN recurrence_threshold; -DROP SCHEMA memory CASCADE; diff --git a/internal/platform/pg/migrations/00023_runs_workflow_version.sql b/internal/platform/pg/migrations/00023_runs_workflow_version.sql deleted file mode 100644 index c636967a3..000000000 --- a/internal/platform/pg/migrations/00023_runs_workflow_version.sql +++ /dev/null @@ -1,6 +0,0 @@ --- +goose Up --- Runs that exist before versions were declared started under version 1. -ALTER TABLE runs.runs ADD COLUMN workflow_version integer NOT NULL DEFAULT 1; - --- +goose Down -ALTER TABLE runs.runs DROP COLUMN workflow_version; diff --git a/internal/platform/pg/migrations/00024_connectors_guard_contract.sql b/internal/platform/pg/migrations/00024_connectors_guard_contract.sql deleted file mode 100644 index a063fa352..000000000 --- a/internal/platform/pg/migrations/00024_connectors_guard_contract.sql +++ /dev/null @@ -1,12 +0,0 @@ --- +goose Up --- A guard extension is an accepted package like a connector or a secret engine. -ALTER TABLE connectors.accepted DROP CONSTRAINT accepted_contract_check; -ALTER TABLE connectors.accepted - ADD CONSTRAINT accepted_contract_check - CHECK (contract IN ('connector', 'secretengine', 'mcp', 'guard')); - --- +goose Down -DELETE FROM connectors.accepted WHERE contract = 'guard'; -ALTER TABLE connectors.accepted DROP CONSTRAINT accepted_contract_check; -ALTER TABLE connectors.accepted - ADD CONSTRAINT accepted_contract_check CHECK (contract IN ('connector', 'secretengine', 'mcp')); diff --git a/internal/platform/pg/migrations/00026_governance_people.sql b/internal/platform/pg/migrations/00026_governance_people.sql deleted file mode 100644 index bd45117d7..000000000 --- a/internal/platform/pg/migrations/00026_governance_people.sql +++ /dev/null @@ -1,45 +0,0 @@ --- +goose Up --- A person is someone an organisation trusts to answer for it. An identity is --- an account on a channel that is verifiably theirs; one identity maps to one --- person, and an answer from an identity no person holds is refused. -CREATE TABLE governance.people ( - id uuid PRIMARY KEY, - org_id text NOT NULL, - name text NOT NULL, - authority boolean NOT NULL DEFAULT false, - created_at timestamptz NOT NULL DEFAULT now(), - UNIQUE (org_id, name) -); - -CREATE TABLE governance.identities ( - org_id text NOT NULL, - identity text NOT NULL, - person_id uuid NOT NULL REFERENCES governance.people (id) ON DELETE CASCADE, - PRIMARY KEY (org_id, identity) -); - -ALTER TABLE governance.settings ADD COLUMN default_owner text NOT NULL DEFAULT ''; -ALTER TABLE governance.settings ADD COLUMN escalation jsonb NOT NULL DEFAULT '[]'; -ALTER TABLE governance.settings ADD COLUMN attempts_per_run integer NOT NULL DEFAULT 20; - --- An answer that was refused, with why, so a request that never settled can --- be explained. -CREATE TABLE approvals.refusals ( - id uuid PRIMARY KEY, - org_id text NOT NULL, - request_id uuid NOT NULL, - identity text NOT NULL, - via text NOT NULL, - reason text NOT NULL, - at timestamptz NOT NULL DEFAULT now() -); - -CREATE INDEX refusals_by_request ON approvals.refusals (org_id, request_id, at); - --- +goose Down -DROP TABLE approvals.refusals; -ALTER TABLE governance.settings DROP COLUMN attempts_per_run; -ALTER TABLE governance.settings DROP COLUMN escalation; -ALTER TABLE governance.settings DROP COLUMN default_owner; -DROP TABLE governance.identities; -DROP TABLE governance.people; diff --git a/internal/platform/pg/migrations/00027_judgement_policies.sql b/internal/platform/pg/migrations/00027_judgement_policies.sql deleted file mode 100644 index 976d9a38b..000000000 --- a/internal/platform/pg/migrations/00027_judgement_policies.sql +++ /dev/null @@ -1,16 +0,0 @@ --- +goose Up --- Hard rules and change-request rules are Cedar policy sets, one per kind per --- organisation. Every version is kept; the highest is the one in force. -CREATE TABLE judgement.policy_sets ( - id uuid PRIMARY KEY, - org_id text NOT NULL, - kind text NOT NULL CHECK (kind IN ('hard_rules', 'change_request')), - version integer NOT NULL, - body text NOT NULL, - created_by text NOT NULL, - created_at timestamptz NOT NULL DEFAULT now(), - UNIQUE (org_id, kind, version) -); - --- +goose Down -DROP TABLE judgement.policy_sets; diff --git a/internal/platform/pg/migrations/00028_governance_guard_settings.sql b/internal/platform/pg/migrations/00028_governance_guard_settings.sql deleted file mode 100644 index f6ca63a01..000000000 --- a/internal/platform/pg/migrations/00028_governance_guard_settings.sql +++ /dev/null @@ -1,9 +0,0 @@ --- +goose Up -ALTER TABLE governance.settings ADD COLUMN timing jsonb NOT NULL DEFAULT '{}'; -ALTER TABLE governance.settings ADD COLUMN actions_per_run integer NOT NULL DEFAULT 10; -ALTER TABLE governance.settings ADD COLUMN actions_per_day integer NOT NULL DEFAULT 100; - --- +goose Down -ALTER TABLE governance.settings DROP COLUMN actions_per_day; -ALTER TABLE governance.settings DROP COLUMN actions_per_run; -ALTER TABLE governance.settings DROP COLUMN timing; diff --git a/internal/platform/pg/migrations/00029_events_groups.sql b/internal/platform/pg/migrations/00029_events_groups.sql deleted file mode 100644 index 3b6867be0..000000000 --- a/internal/platform/pg/migrations/00029_events_groups.sql +++ /dev/null @@ -1,43 +0,0 @@ --- +goose Up --- Provenance and the group key are fixed when an event arrives. own marks an --- event that is the echo of a delivery Archie sent, linked to the run that --- sent it; such an event never forms or joins a group. The key is the source, --- the type and the source's key fields. -ALTER TABLE events.events - ADD COLUMN own boolean NOT NULL DEFAULT false, - ADD COLUMN origin_run_id uuid, - ADD COLUMN group_key text, - ADD COLUMN group_filter jsonb; - --- A source's key fields are dotted paths into its events' fields; an object --- stands for its scalar members. Empty means the default list. -ALTER TABLE events.sources ADD COLUMN key_fields jsonb NOT NULL DEFAULT '[]'; - --- A group is the events no trigger matched that share a key. Its source, type --- and filter are the trigger it proposes. -CREATE TABLE events.groups ( - id uuid PRIMARY KEY, - org_id text NOT NULL, - key text NOT NULL, - source text NOT NULL, - event_type text NOT NULL, - filter jsonb NOT NULL, - count bigint NOT NULL, - first_seen timestamptz NOT NULL, - last_seen timestamptz NOT NULL, - UNIQUE (org_id, key) -); - --- The deliveries module recognises an echo by the id its channel gave the --- message. -CREATE INDEX messages_external ON deliveries.messages (org_id, channel, external_id) WHERE external_id IS NOT NULL; - --- +goose Down -DROP INDEX deliveries.messages_external; -DROP TABLE events.groups; -ALTER TABLE events.sources DROP COLUMN key_fields; -ALTER TABLE events.events - DROP COLUMN group_filter, - DROP COLUMN group_key, - DROP COLUMN origin_run_id, - DROP COLUMN own; diff --git a/internal/platform/pg/migrations/00030_approvals_escalation.sql b/internal/platform/pg/migrations/00030_approvals_escalation.sql deleted file mode 100644 index d26197d50..000000000 --- a/internal/platform/pg/migrations/00030_approvals_escalation.sql +++ /dev/null @@ -1,41 +0,0 @@ --- +goose Up --- An escalation step carries its own deadline. The path was a list of person --- ids; each entry becomes a person and the time that step waits, starting from --- the default approval deadline. -UPDATE governance.settings s -SET escalation = COALESCE(( - SELECT jsonb_agg(jsonb_build_object('person', e.person, 'deadline_ms', s.approval_deadline_ms) ORDER BY e.n) - FROM jsonb_array_elements_text(s.escalation) WITH ORDINALITY AS e (person, n) -), '[]'::jsonb); - --- A request records who has been asked so far and when the current step runs --- out, so an answer is accepted only from someone the request has reached. A --- discarded approval is one the action no longer matched when it came to run. --- counted is set once, before the approval counts toward a raise, so a retry --- can't count it twice. -ALTER TABLE approvals.requests DROP CONSTRAINT requests_state_check; -ALTER TABLE approvals.requests ADD CONSTRAINT requests_state_check - CHECK (state IN ('pending', 'approved', 'denied', 'expired', 'discarded')); -ALTER TABLE approvals.requests ADD COLUMN step integer NOT NULL DEFAULT 0; -ALTER TABLE approvals.requests ADD COLUMN step_deadline timestamptz; -ALTER TABLE approvals.requests ADD COLUMN asked jsonb NOT NULL DEFAULT '[]'; -ALTER TABLE approvals.requests ADD COLUMN change_request boolean NOT NULL DEFAULT false; -ALTER TABLE approvals.requests ADD COLUMN counted boolean NOT NULL DEFAULT false; -ALTER TABLE approvals.requests ADD COLUMN discarded_because text; - --- +goose Down -ALTER TABLE approvals.requests DROP COLUMN discarded_because; -ALTER TABLE approvals.requests DROP COLUMN counted; -ALTER TABLE approvals.requests DROP COLUMN change_request; -ALTER TABLE approvals.requests DROP COLUMN asked; -ALTER TABLE approvals.requests DROP COLUMN step_deadline; -ALTER TABLE approvals.requests DROP COLUMN step; -UPDATE approvals.requests SET state = 'denied' WHERE state = 'discarded'; -ALTER TABLE approvals.requests DROP CONSTRAINT requests_state_check; -ALTER TABLE approvals.requests ADD CONSTRAINT requests_state_check - CHECK (state IN ('pending', 'approved', 'denied', 'expired')); -UPDATE governance.settings s -SET escalation = COALESCE(( - SELECT jsonb_agg(e.step ->> 'person' ORDER BY e.n) - FROM jsonb_array_elements(s.escalation) WITH ORDINALITY AS e (step, n) -), '[]'::jsonb); diff --git a/internal/platform/pg/migrations/00031_drafts.sql b/internal/platform/pg/migrations/00031_drafts.sql deleted file mode 100644 index 47d467a9f..000000000 --- a/internal/platform/pg/migrations/00031_drafts.sql +++ /dev/null @@ -1,55 +0,0 @@ --- +goose Up --- A workflow is a document, stored by name and version. A run pins the version --- it started on and executes that document. -CREATE TABLE runs.workflows ( - org_id text NOT NULL, - name text NOT NULL, - version integer NOT NULL, - body text NOT NULL, - created_by text NOT NULL, - created_at timestamptz NOT NULL DEFAULT now(), - PRIMARY KEY (org_id, name, version) -); - --- A dry run judges every action and executes none that changes something. It --- carries the unpublished document and policy it runs under in overlay, and --- pins no stored version. -ALTER TABLE runs.runs - ADD COLUMN dry boolean NOT NULL DEFAULT false, - ADD COLUMN overlay jsonb; - --- A dry run's decisions are kept for its trail and counted toward nothing: --- not the action budget, not a class's history. -ALTER TABLE judgement.decisions ADD COLUMN dry boolean NOT NULL DEFAULT false; - --- A draft bundles the trigger an unmatched group proposes, the workflow that --- trigger would start and any policy it needs, with the dry run last made of --- it. One draft per group. -CREATE SCHEMA drafts; - -CREATE TABLE drafts.drafts ( - id uuid PRIMARY KEY, - org_id text NOT NULL, - group_id uuid NOT NULL, - name text NOT NULL, - source text NOT NULL, - event_type text NOT NULL, - filter jsonb NOT NULL, - document text NOT NULL, - policy_kind text NOT NULL CHECK (policy_kind IN ('hard_rules', 'change_request')), - policy text NOT NULL, - dry_run_id uuid, - dry_event_id uuid, - installed_version integer, - published_at timestamptz, - created_at timestamptz NOT NULL DEFAULT now(), - updated_at timestamptz NOT NULL DEFAULT now(), - UNIQUE (org_id, group_id), - UNIQUE (org_id, name) -); - --- +goose Down -DROP SCHEMA drafts CASCADE; -ALTER TABLE judgement.decisions DROP COLUMN dry; -ALTER TABLE runs.runs DROP COLUMN overlay, DROP COLUMN dry; -DROP TABLE runs.workflows; diff --git a/internal/platform/pg/migrations/00032_event_bodies.sql b/internal/platform/pg/migrations/00032_event_bodies.sql deleted file mode 100644 index 940d2fda1..000000000 --- a/internal/platform/pg/migrations/00032_event_bodies.sql +++ /dev/null @@ -1,5 +0,0 @@ --- +goose Up -ALTER TABLE events.events ADD COLUMN body bytea; - --- +goose Down -ALTER TABLE events.events DROP COLUMN body; diff --git a/internal/platform/pg/migrations/00033_delivery_attempts.sql b/internal/platform/pg/migrations/00033_delivery_attempts.sql deleted file mode 100644 index 333eb083d..000000000 --- a/internal/platform/pg/migrations/00033_delivery_attempts.sql +++ /dev/null @@ -1,17 +0,0 @@ --- +goose Up --- One row per delivery attempt, so a channel's response and its failures are --- kept rather than only the last error. Nothing is pruned: the volume is low --- and the record is what makes a delivery auditable. -CREATE TABLE deliveries.attempts ( - message_seq bigint NOT NULL REFERENCES deliveries.messages (seq) ON DELETE CASCADE, - attempt int NOT NULL, - at timestamptz NOT NULL DEFAULT now(), - ok boolean NOT NULL, - response text NOT NULL DEFAULT '', - error text NOT NULL DEFAULT '', - duration_ms bigint NOT NULL, - PRIMARY KEY (message_seq, attempt) -); - --- +goose Down -DROP TABLE deliveries.attempts; diff --git a/internal/platform/pg/migrations/00034_model_api_key_credential.sql b/internal/platform/pg/migrations/00034_model_api_key_credential.sql deleted file mode 100644 index 40b8d2f15..000000000 --- a/internal/platform/pg/migrations/00034_model_api_key_credential.sql +++ /dev/null @@ -1,7 +0,0 @@ --- +goose Up --- A model role names the credential whose token authenticates it. The token --- stays sealed in the connectors module; only the name is stored here. -ALTER TABLE governance.model_roles ADD COLUMN api_key_credential text NOT NULL DEFAULT ''; - --- +goose Down -ALTER TABLE governance.model_roles DROP COLUMN api_key_credential; diff --git a/internal/platform/pg/migrations/00035_governance_access.sql b/internal/platform/pg/migrations/00035_governance_access.sql deleted file mode 100644 index fc0fcb637..000000000 --- a/internal/platform/pg/migrations/00035_governance_access.sql +++ /dev/null @@ -1,211 +0,0 @@ --- +goose Up --- Free governance.identities for the persistent identity table while keeping --- the existing people-to-channel-identity table working until later slices --- retire it. -ALTER TABLE governance.identities RENAME TO person_identities; -ALTER INDEX governance.identities_pkey RENAME TO person_identities_pkey; -ALTER TABLE governance.person_identities - RENAME CONSTRAINT identities_person_id_fkey TO person_identities_person_id_fkey; - -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() -); - -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(), - 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() -); -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 -); - -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 PRIMARY KEY DEFAULT true 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 '' -); - -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 -); -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 PRIMARY KEY DEFAULT true CHECK (only_row), - version bigint NOT NULL DEFAULT 1, - updated_at timestamptz NOT NULL DEFAULT now() -); -INSERT INTO governance.access_policy_revision (only_row) VALUES (true); - -CREATE TABLE governance.access_denials ( - 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() -); -CREATE UNIQUE INDEX idx_governance_access_denials_dedup - ON governance.access_denials (org_id, principal, action, resource_kind, resource_id, level, policies, minute_at); - --- +goose Down -DROP INDEX governance.idx_governance_access_denials_dedup; -DROP TABLE governance.access_denials; -DROP TABLE governance.access_policy_revision; -DROP TABLE governance.access_policy_versions; -DROP TABLE governance.access_policies; -DROP TABLE governance.passkey_ceremonies; -DROP TABLE governance.passkeys; -DROP TABLE governance.authentication_settings; -DROP TABLE governance.account_sessions; -DROP TABLE governance.accounts; -DROP TABLE governance.org_agents; -DROP TABLE governance.memberships; -DROP TABLE governance.workspaces; -DROP TABLE governance.orgs; -DROP TABLE governance.identity_subjects; -DROP TABLE governance.identity_events; -DROP TABLE governance.identity_aliases; -DROP TABLE governance.identities; - -ALTER TABLE governance.person_identities - RENAME CONSTRAINT person_identities_person_id_fkey TO identities_person_id_fkey; -ALTER INDEX governance.person_identities_pkey RENAME TO identities_pkey; -ALTER TABLE governance.person_identities RENAME TO identities; diff --git a/internal/platform/pg/migrations/00036_agent_model_aliases.sql b/internal/platform/pg/migrations/00036_agent_model_aliases.sql deleted file mode 100644 index 905b7365c..000000000 --- a/internal/platform/pg/migrations/00036_agent_model_aliases.sql +++ /dev/null @@ -1,8 +0,0 @@ --- +goose Up -ALTER TABLE governance.model_roles DROP CONSTRAINT model_roles_role_check; -ALTER TABLE governance.model_roles ADD CONSTRAINT model_roles_role_check - CHECK (role = 'agent' OR (role <> 'agent:default' AND role ~ '^agent:[a-zA-Z0-9][a-zA-Z0-9_.-]{0,62}$')); - --- +goose Down -ALTER TABLE governance.model_roles DROP CONSTRAINT model_roles_role_check; -ALTER TABLE governance.model_roles ADD CONSTRAINT model_roles_role_check CHECK (role = 'agent'); diff --git a/internal/platform/pg/migrations/00037_chat_conversations.sql b/internal/platform/pg/migrations/00037_chat_conversations.sql deleted file mode 100644 index 66a338b95..000000000 --- a/internal/platform/pg/migrations/00037_chat_conversations.sql +++ /dev/null @@ -1,87 +0,0 @@ --- +goose Up -CREATE SCHEMA chat; - --- A session is one conversation between the operator and Archie. -CREATE TABLE chat.sessions ( - id uuid PRIMARY KEY, - org_id text NOT NULL, - title text NOT NULL DEFAULT '', - created_at timestamptz NOT NULL DEFAULT now(), - updated_at timestamptz NOT NULL DEFAULT now() -); - -CREATE INDEX sessions_by_org ON chat.sessions (org_id, updated_at DESC); - --- A turn is one message from the operator and Archie's reply to it. It is --- stored before it is acknowledged and queued until the turn before it in the --- same session has ended. A turn's id is the run id judgement and approvals --- record its actions under. -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 -); - -CREATE INDEX turns_by_session ON chat.turns (session_id, seq); -CREATE INDEX turns_queued ON chat.turns (seq) WHERE state IN ('queued', 'running'); - --- An event is one thing a turn streamed: thinking, reply text, a tool call or --- its result, an approval asked, or how the turn ended. A reader replays them --- after its cursor. -CREATE TABLE chat.events ( - seq bigserial PRIMARY KEY, - turn_id uuid NOT NULL REFERENCES chat.turns (id) ON DELETE CASCADE, - kind text NOT NULL, - data jsonb NOT NULL, - created_at timestamptz NOT NULL DEFAULT now() -); - -CREATE INDEX events_by_turn ON chat.events (turn_id, seq); - --- An action is one a turn's agent called that judgement held for approval. --- Its answer comes back through approvals; an approved action runs once. -CREATE TABLE chat.actions ( - id uuid PRIMARY KEY, - org_id text NOT NULL, - turn_id uuid NOT NULL REFERENCES chat.turns (id) ON DELETE CASCADE, - key text NOT NULL, - decision jsonb NOT NULL, - request_id uuid NOT NULL, - path jsonb NOT NULL, - step int NOT NULL DEFAULT 0, - step_due timestamptz NOT NULL, - state text NOT NULL CHECK (state IN ('waiting', 'approved', 'running', 'succeeded', 'failed', 'unknown', 'denied', 'expired')), - answer jsonb, - result text NOT NULL DEFAULT '', - created_at timestamptz NOT NULL DEFAULT now(), - UNIQUE (turn_id, key) -); - -CREATE INDEX actions_open ON chat.actions (state) WHERE state IN ('waiting', 'approved', 'running'); - --- A note is a fact the operator asked Archie to remember across --- conversations. -CREATE TABLE memory.notes ( - id uuid PRIMARY KEY, - org_id text NOT NULL, - text text NOT NULL, - created_at timestamptz NOT NULL DEFAULT now() -); - -CREATE INDEX notes_by_org ON memory.notes (org_id, created_at); - --- +goose Down -DROP TABLE memory.notes; -DROP SCHEMA chat CASCADE; diff --git a/internal/platform/pg/migrations/00038_chat_session_owner.sql b/internal/platform/pg/migrations/00038_chat_session_owner.sql deleted file mode 100644 index c5b8fa9ad..000000000 --- a/internal/platform/pg/migrations/00038_chat_session_owner.sql +++ /dev/null @@ -1,15 +0,0 @@ --- +goose Up --- A conversation belongs to the person who started it, and its turns act with --- that person's access. Conversations from before ownership were the shared --- administrator's, so they go to the system identity the admin token acts as. -ALTER TABLE chat.sessions ADD COLUMN owner_id text; -UPDATE chat.sessions SET owner_id = '00000000-0000-5000-8000-000000000001'; -ALTER TABLE chat.sessions ALTER COLUMN owner_id SET NOT NULL; - -DROP INDEX chat.sessions_by_org; -CREATE INDEX sessions_by_owner ON chat.sessions (org_id, owner_id, updated_at DESC); - --- +goose Down -DROP INDEX chat.sessions_by_owner; -CREATE INDEX sessions_by_org ON chat.sessions (org_id, updated_at DESC); -ALTER TABLE chat.sessions DROP COLUMN owner_id; diff --git a/internal/platform/pg/migrations/00039_governance_org_names.sql b/internal/platform/pg/migrations/00039_governance_org_names.sql deleted file mode 100644 index 1eaeb069f..000000000 --- a/internal/platform/pg/migrations/00039_governance_org_names.sql +++ /dev/null @@ -1,27 +0,0 @@ --- +goose Up --- A name is a display label: changing it never changes an ID, so a rename is --- an update guarded by the revision the caller read. -ALTER TABLE governance.orgs ADD COLUMN version bigint NOT NULL DEFAULT 1; -ALTER TABLE governance.workspaces ADD COLUMN version bigint NOT NULL DEFAULT 1; - --- Every rename is appended here, with who made it and what the name was, so a --- name change is attributable after the fact. -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); - --- +goose Down -DROP TABLE governance.org_events; -ALTER TABLE governance.workspaces DROP COLUMN version; -ALTER TABLE governance.orgs DROP COLUMN version; diff --git a/internal/platform/pg/migrations/00040_chat_session_taint.sql b/internal/platform/pg/migrations/00040_chat_session_taint.sql deleted file mode 100644 index 8f64d3477..000000000 --- a/internal/platform/pg/migrations/00040_chat_session_taint.sql +++ /dev/null @@ -1,8 +0,0 @@ --- +goose Up --- A conversation that has ingested untrusted content is tainted for the rest --- of its life: every later turn, including one a restarted process resumes, --- tightens controls. The flag is only ever set, never cleared. -ALTER TABLE chat.sessions ADD COLUMN tainted boolean NOT NULL DEFAULT false; - --- +goose Down -ALTER TABLE chat.sessions DROP COLUMN tainted; diff --git a/internal/platform/pg/migrations/00041_memory_note_scope.sql b/internal/platform/pg/migrations/00041_memory_note_scope.sql deleted file mode 100644 index 357025bdd..000000000 --- a/internal/platform/pg/migrations/00041_memory_note_scope.sql +++ /dev/null @@ -1,92 +0,0 @@ --- +goose Up --- memory.notes holds all four memory classes: the working context of one --- session, episodic history, enterprise retrieval, and durable preferences and --- facts. A note is readable only within its scope: a person's own notes, or the --- ones published to the org. Each class states what it must carry, so a row that --- names nobody and that nobody may read cannot be written. --- --- Notes written before scoping had no owner, and every conversation read them. --- The maintainer ruled they are deleted rather than carried into a scope. -DROP TABLE memory.notes; - -CREATE TABLE memory.notes ( - id uuid PRIMARY KEY, - org_id text NOT NULL, - class text NOT NULL, - scope text NOT NULL, - -- A person's note belongs to them and only they read it. A shared note - -- exists for everyone only once an administrator publishes it: until then - -- it is a pending proposal, and no turn reads it. - state text NOT NULL, - owner_id text NOT NULL, - -- shared_by is the person who contributed a shared note. Who published a - -- version of it is recorded on that version. - shared_by text NOT NULL DEFAULT '', - purpose text NOT NULL, - text text NOT NULL, - -- Lineage: the session a working context note came from, or the collection - -- an episodic or enterprise note was retrieved with. - session_id uuid, - collection text, - -- Lifecycle: a disabled note is kept but never read, and a note stops - -- being read at retain_until. - disabled_at timestamptz, - retain_until timestamptz, - version integer NOT NULL DEFAULT 0, - created_at timestamptz NOT NULL DEFAULT now(), - updated_at timestamptz NOT NULL DEFAULT now(), - CONSTRAINT notes_class CHECK (class IN ('working_context', 'episodic_history', 'enterprise_retrieval', 'durable_fact')), - CONSTRAINT notes_scope CHECK (scope IN ('person', 'shared')), - CONSTRAINT notes_state CHECK (state IN ('pending', 'published')), - CONSTRAINT notes_person_published CHECK (scope <> 'person' OR state = 'published'), - CONSTRAINT notes_person_owned CHECK (scope <> 'person' OR owner_id <> ''), - CONSTRAINT notes_shared_attributed CHECK (scope <> 'shared' OR shared_by <> ''), - CONSTRAINT notes_purpose CHECK (purpose <> ''), - CONSTRAINT notes_text CHECK (text <> ''), - CONSTRAINT notes_version CHECK ((state = 'pending' AND version = 0) OR (state = 'published' AND version > 0)), - CONSTRAINT notes_working_context CHECK (class <> 'working_context' OR session_id IS NOT NULL), - CONSTRAINT notes_episodic_history CHECK (class <> 'episodic_history' OR collection IS NOT NULL), - CONSTRAINT notes_enterprise_retrieval CHECK (class <> 'enterprise_retrieval' OR collection IS NOT NULL), - CONSTRAINT notes_durable_fact CHECK (class <> 'durable_fact' OR (session_id IS NULL AND collection IS NULL)) -); - -CREATE INDEX notes_by_person ON memory.notes (org_id, owner_id, class, created_at DESC) WHERE scope = 'person'; -CREATE INDEX notes_published ON memory.notes (org_id, class, created_at DESC) WHERE scope = 'shared' AND state = 'published'; -CREATE INDEX notes_pending ON memory.notes (org_id, created_at) WHERE scope = 'shared' AND state = 'pending'; - --- A shared note keeps every version published of it, so a publication can be --- rolled back and its provenance read. -CREATE TABLE memory.note_versions ( - note_id uuid NOT NULL REFERENCES memory.notes (id) ON DELETE CASCADE, - version integer NOT NULL, - text text NOT NULL, - purpose text NOT NULL, - publisher_id text NOT NULL, - published_at timestamptz NOT NULL DEFAULT now(), - PRIMARY KEY (note_id, version), - CONSTRAINT note_versions_version CHECK (version > 0) -); - --- A committed deletion is witnessed here, and the record outside the database --- has to account for every witness. The witness is not what a restore replays: --- it says which deletions exist, so a missing or short record is caught rather --- than believed. -CREATE TABLE memory.deletion_witness ( - note_id uuid NOT NULL PRIMARY KEY, - org_id text NOT NULL, - at timestamptz NOT NULL DEFAULT now() -); - --- +goose Down -DROP TABLE memory.deletion_witness; -DROP TABLE memory.note_versions; -DROP TABLE memory.notes; - -CREATE TABLE memory.notes ( - id uuid PRIMARY KEY, - org_id text NOT NULL, - text text NOT NULL, - created_at timestamptz NOT NULL DEFAULT now() -); - -CREATE INDEX notes_by_org ON memory.notes (org_id, created_at); diff --git a/internal/platform/pg/migrations/00042_events_source_verification.sql b/internal/platform/pg/migrations/00042_events_source_verification.sql deleted file mode 100644 index 8ef243a57..000000000 --- a/internal/platform/pg/migrations/00042_events_source_verification.sql +++ /dev/null @@ -1,10 +0,0 @@ --- +goose Up --- How a source's deliveries are verified. A token source checks the bearer --- token. An hmac source checks a signature over the raw body using a shared --- secret, which is stored sealed. A source set to none performs no check. -ALTER TABLE events.sources - ADD COLUMN verification text NOT NULL DEFAULT 'token' - CHECK (verification IN ('token', 'hmac', 'none')), - ADD COLUMN hmac_header text NOT NULL DEFAULT '', - ADD COLUMN hmac_prefix text NOT NULL DEFAULT '', - ADD COLUMN hmac_secret_sealed bytea NOT NULL DEFAULT ''; diff --git a/internal/platform/pg/migrations/00043_deliveries_failed.sql b/internal/platform/pg/migrations/00043_deliveries_failed.sql deleted file mode 100644 index 2eb6c5c27..000000000 --- a/internal/platform/pg/migrations/00043_deliveries_failed.sql +++ /dev/null @@ -1,12 +0,0 @@ --- +goose Up --- A delivery that the channel rejects permanently, or that still fails at the --- retry limit, is marked failed instead of retried. The worker does not claim --- rows with failed_at set. -ALTER TABLE deliveries.messages ADD COLUMN failed_at timestamptz; -DROP INDEX deliveries.messages_due; -CREATE INDEX messages_due ON deliveries.messages (next_attempt_at) WHERE sent_at IS NULL AND failed_at IS NULL; - --- +goose Down -DROP INDEX deliveries.messages_due; -CREATE INDEX messages_due ON deliveries.messages (next_attempt_at) WHERE sent_at IS NULL; -ALTER TABLE deliveries.messages DROP COLUMN failed_at; diff --git a/internal/platform/pg/migrations/00044_runs_endings.sql b/internal/platform/pg/migrations/00044_runs_endings.sql deleted file mode 100644 index 5a8059ef9..000000000 --- a/internal/platform/pg/migrations/00044_runs_endings.sql +++ /dev/null @@ -1,18 +0,0 @@ --- +goose Up --- Besides succeeding or failing, a run can now end parked, waiting for approval --- or declined. A failed step or a workflow.finish step parks a run for an --- operator to retry from a step; a workflow.finish step can also leave it --- waiting for approval or declined. detail explains the ending. Parked and --- waiting runs are not terminal. -ALTER TABLE runs.runs DROP CONSTRAINT runs_state_check; -ALTER TABLE runs.runs ADD CONSTRAINT runs_state_check - CHECK (state IN ('queued', 'running', 'waiting', 'succeeded', 'failed', 'cancelled', - 'parked', 'waiting_approval', 'declined')); -ALTER TABLE runs.runs ADD COLUMN detail text; - --- +goose Down -UPDATE runs.runs SET state = 'failed' WHERE state IN ('parked', 'waiting_approval', 'declined'); -ALTER TABLE runs.runs DROP COLUMN detail; -ALTER TABLE runs.runs DROP CONSTRAINT runs_state_check; -ALTER TABLE runs.runs ADD CONSTRAINT runs_state_check - CHECK (state IN ('queued', 'running', 'waiting', 'succeeded', 'failed', 'cancelled')); diff --git a/internal/platform/pg/migrations/00045_runs_calls.sql b/internal/platform/pg/migrations/00045_runs_calls.sql deleted file mode 100644 index a3a58818b..000000000 --- a/internal/platform/pg/migrations/00045_runs_calls.sql +++ /dev/null @@ -1,14 +0,0 @@ --- +goose Up --- A workflow.call step starts a child run. parent_run_id and parent_signal say --- which run, and which signal key, to wake when the child ends; call_depth --- bounds how deep calls nest. A run started by a handoff has no parent. -ALTER TABLE runs.runs ADD COLUMN parent_run_id uuid REFERENCES runs.runs (id) ON DELETE CASCADE; -ALTER TABLE runs.runs ADD COLUMN parent_signal text; -ALTER TABLE runs.runs ADD COLUMN call_depth integer NOT NULL DEFAULT 0; -CREATE INDEX runs_runs_parent ON runs.runs (parent_run_id) WHERE parent_run_id IS NOT NULL; - --- +goose Down -DROP INDEX runs.runs_runs_parent; -ALTER TABLE runs.runs DROP COLUMN call_depth; -ALTER TABLE runs.runs DROP COLUMN parent_signal; -ALTER TABLE runs.runs DROP COLUMN parent_run_id; diff --git a/internal/platform/pg/migrations/00046_connectors_capabilities.sql b/internal/platform/pg/migrations/00046_connectors_capabilities.sql deleted file mode 100644 index 006ad33ed..000000000 --- a/internal/platform/pg/migrations/00046_connectors_capabilities.sql +++ /dev/null @@ -1,9 +0,0 @@ --- +goose Up --- What a connector declared when it last loaded: its event types, actions, --- deliveries and lookups. Workflows and triggers are checked against it when --- they are saved. Null means the connector has never loaded. The value stays --- when the connector stops. -ALTER TABLE connectors.accepted ADD COLUMN capabilities jsonb; - --- +goose Down -ALTER TABLE connectors.accepted DROP COLUMN capabilities; diff --git a/internal/platform/pg/migrations/00047_connectors_effects.sql b/internal/platform/pg/migrations/00047_connectors_effects.sql deleted file mode 100644 index 33d2f6b98..000000000 --- a/internal/platform/pg/migrations/00047_connectors_effects.sql +++ /dev/null @@ -1,8 +0,0 @@ --- +goose Up --- The privileged effects an operator declared for an MCP server: a JSON object --- with privilege_change and secret_access. Null means nothing is declared, and --- chat doesn't offer the server's tools. -ALTER TABLE connectors.accepted ADD COLUMN effects jsonb; - --- +goose Down -ALTER TABLE connectors.accepted DROP COLUMN effects; diff --git a/internal/platform/pg/migrations/00048_chat_approvals_binding.sql b/internal/platform/pg/migrations/00048_chat_approvals_binding.sql deleted file mode 100644 index a71f09783..000000000 --- a/internal/platform/pg/migrations/00048_chat_approvals_binding.sql +++ /dev/null @@ -1,32 +0,0 @@ --- +goose Up --- A held chat action's approval is bound to the whole record of the action, not --- only its name and arguments. Actions held before the binding existed carry --- the narrow digest alone, so they are discarded rather than bound afterwards: --- the person asks again. An approval already given is discarded, a pending --- one expires, and the action stops. -UPDATE approvals.requests SET state = 'discarded', - discarded_because = 'it was approved before approvals were bound to the whole action' -WHERE state = 'approved' AND NOT counted - AND id IN (SELECT request_id FROM chat.actions WHERE state = 'approved'); -UPDATE approvals.requests SET state = 'expired' -WHERE state = 'pending' AND id IN (SELECT request_id FROM chat.actions WHERE state = 'waiting'); -UPDATE chat.actions SET state = 'expired', - result = 'It was held before approvals were bound to the whole action, so it didn''t run. Ask again.' -WHERE state = 'waiting'; -UPDATE chat.actions SET state = 'failed', - result = 'It was held before approvals were bound to the whole action, so it didn''t run. Ask again.' -WHERE state = 'approved'; - -ALTER TABLE chat.actions ADD COLUMN binding jsonb, ADD COLUMN binding_digest text NOT NULL DEFAULT ''; - --- Approving an action that can't be undone needs a fresh passkey assertion. -ALTER TABLE approvals.requests ADD COLUMN irreversible boolean NOT NULL DEFAULT false; - --- A confirmation ceremony is for one subject, such as an approval request. A --- sign-in or registration ceremony has none. -ALTER TABLE governance.passkey_ceremonies ADD COLUMN subject text NOT NULL DEFAULT ''; - --- +goose Down -ALTER TABLE governance.passkey_ceremonies DROP COLUMN subject; -ALTER TABLE approvals.requests DROP COLUMN irreversible; -ALTER TABLE chat.actions DROP COLUMN binding_digest, DROP COLUMN binding; diff --git a/internal/platform/pg/migrations/00049_governance_approvers_are_accounts.sql b/internal/platform/pg/migrations/00049_governance_approvers_are_accounts.sql deleted file mode 100644 index b5c8c9a12..000000000 --- a/internal/platform/pg/migrations/00049_governance_approvers_are_accounts.sql +++ /dev/null @@ -1,93 +0,0 @@ --- +goose Up --- Who may approve is decided by role, and a channel identity is a credential of --- an account, so the people records go. A channel identity moves to the account --- its person maps to without doubt: the account the person already answered --- from on the dashboard, or the operating org's only owner when that owner --- and the person are the only ones. Any other identity is dropped and linked --- again from the account page. The default owner and escalation steps follow --- the same mapping, and a step that maps nowhere is cleared. -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); - -CREATE TEMPORARY TABLE person_account ( - person_id uuid PRIMARY KEY, - account text NOT NULL -); - -INSERT INTO person_account (person_id, account) -SELECT pi.person_id, min(substr(pi.identity, 9)) -FROM governance.person_identities pi -JOIN governance.memberships m - ON m.identity_id = substr(pi.identity, 9) AND m.org_id = 'org-sys' AND m.workspace_id IS NULL -WHERE pi.identity LIKE 'account:%' -GROUP BY pi.person_id -HAVING count(*) = 1; - -INSERT INTO person_account (person_id, account) -SELECT p.id, sole.identity_id -FROM governance.people p -CROSS JOIN LATERAL ( - SELECT min(m.identity_id) AS identity_id, count(*) AS owners - FROM governance.memberships m - JOIN governance.identities i ON i.id = m.identity_id - WHERE m.org_id = 'org-sys' AND m.workspace_id IS NULL AND m.role = 'owner' AND i.kind = 'user' -) sole -WHERE sole.owners = 1 - AND (SELECT count(*) FROM governance.people) = 1 - AND p.id NOT IN (SELECT person_id FROM person_account); - -INSERT INTO governance.identity_subjects (issuer, subject, identity_id, bound_at, name) -SELECT split_part(pi.identity, ':', 1), substr(pi.identity, strpos(pi.identity, ':') + 1), pa.account, now(), '' -FROM governance.person_identities pi -JOIN person_account pa ON pa.person_id = pi.person_id -WHERE pi.identity NOT LIKE 'account:%' AND strpos(pi.identity, ':') > 1 -ON CONFLICT (issuer, subject) DO NOTHING; - -UPDATE governance.settings s -SET default_owner = COALESCE(( - SELECT pa.account FROM person_account pa WHERE pa.person_id::text = s.default_owner - ), ''), - escalation = COALESCE(( - SELECT jsonb_agg(jsonb_build_object('account', d.account, 'deadline_ms', d.deadline_ms) ORDER BY d.n) - FROM ( - SELECT DISTINCT ON (pa.account) pa.account, e.step -> 'deadline_ms' AS deadline_ms, e.n - FROM jsonb_array_elements(s.escalation) WITH ORDINALITY AS e (step, n) - JOIN person_account pa ON pa.person_id::text = e.step ->> 'person' - ORDER BY pa.account, e.n - ) d - ), '[]'::jsonb); - -DROP TABLE person_account; -DROP TABLE governance.person_identities; -DROP TABLE governance.people; - -ALTER TABLE approvals.requests DROP COLUMN asked; - --- +goose Down -ALTER TABLE approvals.requests ADD COLUMN asked jsonb NOT NULL DEFAULT '[]'; - -CREATE TABLE governance.people ( - id uuid PRIMARY KEY, - org_id text NOT NULL, - name text NOT NULL, - authority boolean NOT NULL DEFAULT false, - created_at timestamptz NOT NULL DEFAULT now(), - UNIQUE (org_id, name) -); -CREATE TABLE governance.person_identities ( - org_id text NOT NULL, - identity text NOT NULL, - person_id uuid NOT NULL REFERENCES governance.people (id) ON DELETE CASCADE, - CONSTRAINT person_identities_pkey PRIMARY KEY (org_id, identity) -); - -UPDATE governance.settings SET default_owner = '', escalation = '[]'::jsonb; - -DROP TABLE governance.link_codes; diff --git a/internal/platform/pg/migrations/00050_governance_last_sign_in.sql b/internal/platform/pg/migrations/00050_governance_last_sign_in.sql deleted file mode 100644 index 34fcefcdc..000000000 --- a/internal/platform/pg/migrations/00050_governance_last_sign_in.sql +++ /dev/null @@ -1,15 +0,0 @@ --- +goose Up --- The members page shows when each person last signed in. Sessions expire and --- are purged, so the time is kept on the account. -ALTER TABLE governance.accounts ADD COLUMN last_sign_in_at timestamptz; - -UPDATE governance.accounts a -SET last_sign_in_at = s.last -FROM ( - SELECT identity_id, max(issued_at) AS last - FROM governance.account_sessions GROUP BY identity_id -) s -WHERE s.identity_id = a.identity_id; - --- +goose Down -ALTER TABLE governance.accounts DROP COLUMN last_sign_in_at; diff --git a/internal/platform/pg/migrations/00051_governance_instructions.sql b/internal/platform/pg/migrations/00051_governance_instructions.sql deleted file mode 100644 index 3f807030f..000000000 --- a/internal/platform/pg/migrations/00051_governance_instructions.sql +++ /dev/null @@ -1,11 +0,0 @@ --- +goose Up --- The organisation's standing instructions to its agents, appended after how --- Archie works wherever an agent's system prompt is assembled. -CREATE TABLE governance.instructions ( - org_id text PRIMARY KEY, - body text NOT NULL, - updated_at timestamptz NOT NULL DEFAULT now() -); - --- +goose Down -DROP TABLE governance.instructions; diff --git a/internal/platform/pg/migrations/00052_events_captures_mappings_bindings.sql b/internal/platform/pg/migrations/00052_events_captures_mappings_bindings.sql deleted file mode 100644 index cb600aaeb..000000000 --- a/internal/platform/pg/migrations/00052_events_captures_mappings_bindings.sql +++ /dev/null @@ -1,67 +0,0 @@ --- +goose Up --- A source keeps a display name and the header its sender repeats on every --- retry of one delivery. A source that does no verification refuses deliveries --- until a person approves it, and may stop keeping the raw payload. -ALTER TABLE events.sources - ADD COLUMN display_name text NOT NULL DEFAULT '', - ADD COLUMN delivery_header text NOT NULL DEFAULT '', - ADD COLUMN unsigned_approved boolean NOT NULL DEFAULT false, - ADD COLUMN capture boolean NOT NULL DEFAULT true; -UPDATE events.sources SET unsigned_approved = true WHERE verification = 'none'; - --- A person names a kind of event on a source by the headers and payload paths --- that identify it. Its schema is the JSON paths of the example it came from. -CREATE TABLE events.event_types ( - id uuid PRIMARY KEY, - org_id text NOT NULL, - source text NOT NULL, - name text NOT NULL, - rule jsonb NOT NULL, - schema jsonb NOT NULL DEFAULT '{}', - created_at timestamptz NOT NULL DEFAULT now(), - updated_at timestamptz NOT NULL DEFAULT now(), - UNIQUE (org_id, source, name) -); - --- A mapping names the payload paths a workflow reads from one event type. -CREATE TABLE events.mappings ( - id uuid PRIMARY KEY, - org_id text NOT NULL, - name text NOT NULL, - source_hint text NOT NULL DEFAULT '', - event_type_id uuid NOT NULL REFERENCES events.event_types (id) ON DELETE RESTRICT, - fields jsonb NOT NULL, - match_count bigint NOT NULL DEFAULT 0, - last_matched_at timestamptz, - created_at timestamptz NOT NULL DEFAULT now(), - updated_at timestamptz NOT NULL DEFAULT now() -); - --- Each event records how its delivery was verified and the headers and sender --- address it arrived with. The headers and body are null when the source does --- not keep payloads. event_type_id is the type its headers and payload matched --- on arrival. -ALTER TABLE events.events - ADD COLUMN verification text NOT NULL DEFAULT '' - CHECK (verification IN ('', 'signed', 'token', 'unsigned')), - ADD COLUMN headers jsonb, - ADD COLUMN remote_addr text NOT NULL DEFAULT '', - ADD COLUMN event_type_id uuid REFERENCES events.event_types (id) ON DELETE SET NULL; - --- A trigger bound to an event type fires only for events identified as it; a --- bound mapping reads the event's payload for the run. Triggers --- saved before approval existed are approved. -ALTER TABLE events.triggers - ADD COLUMN approved boolean NOT NULL DEFAULT true, - ADD COLUMN event_type_id uuid REFERENCES events.event_types (id) ON DELETE RESTRICT, - ADD COLUMN mapping_id uuid REFERENCES events.mappings (id) ON DELETE RESTRICT; - --- +goose Down -ALTER TABLE events.triggers - DROP COLUMN mapping_id, DROP COLUMN event_type_id, DROP COLUMN approved; -ALTER TABLE events.events - DROP COLUMN event_type_id, DROP COLUMN remote_addr, DROP COLUMN headers, DROP COLUMN verification; -DROP TABLE events.mappings; -DROP TABLE events.event_types; -ALTER TABLE events.sources - DROP COLUMN capture, DROP COLUMN unsigned_approved, DROP COLUMN delivery_header, DROP COLUMN display_name; diff --git a/internal/platform/pg/migrations/00053_runs_workflow_drafts.sql b/internal/platform/pg/migrations/00053_runs_workflow_drafts.sql deleted file mode 100644 index d23e195c2..000000000 --- a/internal/platform/pg/migrations/00053_runs_workflow_drafts.sql +++ /dev/null @@ -1,29 +0,0 @@ --- +goose Up --- A draft is the unpublished edit of a workflow, kept so work survives a reload --- and never reaches a run until it is published as the next version. revision --- counts writes: a write that names an older one is refused, so two editors --- can't overwrite each other. base_version is the published version the draft --- began from; a new workflow's is 0. -CREATE TABLE runs.workflow_drafts ( - org_id text NOT NULL, - name text NOT NULL, - body text NOT NULL, - base_version integer NOT NULL, - revision integer NOT NULL DEFAULT 1, - updated_by text NOT NULL, - updated_at timestamptz NOT NULL DEFAULT now(), - PRIMARY KEY (org_id, name) -); - --- A workflow with no row here is enabled. A disabled workflow is never started, --- by a trigger, by another workflow or on demand. -CREATE TABLE runs.workflow_settings ( - org_id text NOT NULL, - name text NOT NULL, - enabled boolean NOT NULL, - PRIMARY KEY (org_id, name) -); - --- +goose Down -DROP TABLE runs.workflow_settings; -DROP TABLE runs.workflow_drafts; diff --git a/internal/platform/pg/migrations/00054_connectors_catalogue.sql b/internal/platform/pg/migrations/00054_connectors_catalogue.sql deleted file mode 100644 index f9c00bc3d..000000000 --- a/internal/platform/pg/migrations/00054_connectors_catalogue.sql +++ /dev/null @@ -1,24 +0,0 @@ --- +goose Up --- An extension installed from the catalogue is a row like any accepted one: its --- path and digest are the live version and its egress, env and credentials are --- what the operator accepted. package keeps the rest of its lifecycle: the --- grants each version declared, a pending update, the previous version and the --- update policy. It is null for an extension accepted by path. -ALTER TABLE connectors.accepted - ADD COLUMN enabled boolean NOT NULL DEFAULT true, - ADD COLUMN package jsonb; - --- Where an organisation's catalogue is read from, and the key that signs it. --- newest_issued_at is the newest catalogue accepted from it, so an older one --- replayed by the source or the network is refused. -CREATE TABLE connectors.catalogue_source ( - org_id text PRIMARY KEY, - location text NOT NULL, - public_key text NOT NULL, - newest_issued_at timestamptz, - updated_at timestamptz NOT NULL DEFAULT now() -); - --- +goose Down -DROP TABLE connectors.catalogue_source; -ALTER TABLE connectors.accepted DROP COLUMN package, DROP COLUMN enabled; diff --git a/internal/platform/pg/migrations/00055_governance_model_providers.sql b/internal/platform/pg/migrations/00055_governance_model_providers.sql deleted file mode 100644 index 1f644aa98..000000000 --- a/internal/platform/pg/migrations/00055_governance_model_providers.sql +++ /dev/null @@ -1,59 +0,0 @@ --- +goose Up --- A provider is an endpoint models are called on: its class, base URL and the --- credential whose token authenticates it. An alias names a provider and a --- model on it, so one provider serves every alias that uses it. -CREATE TABLE governance.model_providers ( - 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 text NOT NULL DEFAULT '', - updated_at timestamptz NOT NULL DEFAULT now(), - PRIMARY KEY (org_id, name) -); - -ALTER TABLE governance.model_roles ADD COLUMN provider text; - --- Each distinct endpoint an alias already used becomes a provider named for --- its class, with a number after the first. -INSERT INTO governance.model_providers (org_id, name, class, base_url, api_key_credential) -SELECT org_id, CASE WHEN n = 1 THEN class ELSE class || '-' || n END, class, base_url, api_key_credential -FROM ( - SELECT org_id, class, base_url, api_key_credential, - row_number() OVER (PARTITION BY org_id, class ORDER BY base_url, api_key_credential) AS n - FROM (SELECT DISTINCT org_id, class, base_url, api_key_credential FROM governance.model_roles) d -) numbered; - -UPDATE governance.model_roles r SET provider = p.name -FROM governance.model_providers p -WHERE p.org_id = r.org_id AND p.class = r.class AND p.base_url = r.base_url - AND p.api_key_credential = r.api_key_credential; - -ALTER TABLE governance.model_roles - ALTER COLUMN provider SET NOT NULL, - ADD FOREIGN KEY (org_id, provider) REFERENCES governance.model_providers (org_id, name), - DROP COLUMN class, - DROP COLUMN base_url, - DROP COLUMN api_key_credential; - --- Which aliases a workflow and chat may use. Null lets either use 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() -); - --- +goose Down -DROP TABLE governance.model_policy; -ALTER TABLE governance.model_roles - ADD COLUMN class text, - ADD COLUMN base_url text, - ADD COLUMN api_key_credential text NOT NULL DEFAULT ''; -UPDATE governance.model_roles r SET class = p.class, base_url = p.base_url, api_key_credential = p.api_key_credential -FROM governance.model_providers p WHERE p.org_id = r.org_id AND p.name = r.provider; -ALTER TABLE governance.model_roles - ALTER COLUMN class SET NOT NULL, - ALTER COLUMN base_url SET NOT NULL, - DROP COLUMN provider; -DROP TABLE governance.model_providers; diff --git a/internal/platform/pg/migrations/00056_governance_trusted_proxies.sql b/internal/platform/pg/migrations/00056_governance_trusted_proxies.sql deleted file mode 100644 index 9033921f8..000000000 --- a/internal/platform/pg/migrations/00056_governance_trusted_proxies.sql +++ /dev/null @@ -1,8 +0,0 @@ --- +goose Up --- Reverse proxies whose X-Forwarded-For header webhook intake uses as the --- client address, stored as a JSON array of addresses and CIDR ranges. -ALTER TABLE governance.authentication_settings - ADD COLUMN trusted_proxies jsonb NOT NULL DEFAULT '[]'; - --- +goose Down -ALTER TABLE governance.authentication_settings DROP COLUMN trusted_proxies; diff --git a/internal/platform/pg/migrations/00057_approvals_answered_by_id.sql b/internal/platform/pg/migrations/00057_approvals_answered_by_id.sql deleted file mode 100644 index 9a6177753..000000000 --- a/internal/platform/pg/migrations/00057_approvals_answered_by_id.sql +++ /dev/null @@ -1,7 +0,0 @@ --- +goose Up --- The identity ID of the account that answered a request. answered_by holds --- that account's display name, which is not unique. -ALTER TABLE approvals.requests ADD COLUMN answered_by_id text; - --- +goose Down -ALTER TABLE approvals.requests DROP COLUMN answered_by_id; diff --git a/internal/platform/pg/migrations/00058_events_dispatch_outcome.sql b/internal/platform/pg/migrations/00058_events_dispatch_outcome.sql deleted file mode 100644 index 20170fb21..000000000 --- a/internal/platform/pg/migrations/00058_events_dispatch_outcome.sql +++ /dev/null @@ -1,17 +0,0 @@ --- +goose Up --- A dispatch records the outcome of a trigger for an event: a started run or a --- skip. A skip's reason is a JSON document with a code and display details, --- such as the mapping fields that failed. -ALTER TABLE events.dispatches - ALTER COLUMN run_id DROP NOT NULL, - ADD COLUMN outcome text NOT NULL DEFAULT 'started' CHECK (outcome IN ('started', 'skipped')), - ADD COLUMN reason jsonb, - ADD CONSTRAINT dispatches_run_matches_outcome CHECK ((outcome = 'started') = (run_id IS NOT NULL)); - --- +goose Down -DELETE FROM events.dispatches WHERE outcome = 'skipped'; -ALTER TABLE events.dispatches - DROP CONSTRAINT dispatches_run_matches_outcome, - DROP COLUMN reason, - DROP COLUMN outcome, - ALTER COLUMN run_id SET NOT NULL; diff --git a/internal/platform/pg/migrations/00059_events_redact_link_codes.sql b/internal/platform/pg/migrations/00059_events_redact_link_codes.sql deleted file mode 100644 index 8abe93404..000000000 --- a/internal/platform/pg/migrations/00059_events_redact_link_codes.sql +++ /dev/null @@ -1,16 +0,0 @@ --- +goose Up --- Redact single-use link codes already stored in account.link events, in the --- fields and in the raw body. Issued codes are ASCII, and LATIN1 maps every --- byte to one character, so the body is edited without changing other bytes. -UPDATE events.events -SET body = CASE - WHEN body IS NOT NULL AND length(fields->>'code') >= 16 - THEN convert_to(replace(convert_from(body, 'LATIN1'), fields->>'code', '[redacted]'), 'LATIN1') - ELSE body - END, - fields = jsonb_set(fields, '{code}', '"[redacted]"') -WHERE type = 'account.link' AND fields ? 'code' AND fields->>'code' <> '[redacted]'; - --- +goose Down --- Redacted codes can't be restored. -SELECT 1; diff --git a/internal/platform/pg/migrations/00060_governance_access_decisions.sql b/internal/platform/pg/migrations/00060_governance_access_decisions.sql deleted file mode 100644 index 2df7678af..000000000 --- a/internal/platform/pg/migrations/00060_governance_access_decisions.sql +++ /dev/null @@ -1,16 +0,0 @@ --- +goose Up --- The table that held refusals now holds every authorization decision. A row --- records one outcome, coalesced per minute with identical requests, so rows --- that exist already are refusals. -ALTER TABLE governance.access_denials - ADD COLUMN outcome text NOT NULL DEFAULT 'denied' CHECK (outcome IN ('allowed', 'denied')); -DROP INDEX governance.idx_governance_access_denials_dedup; -CREATE UNIQUE INDEX idx_governance_access_denials_dedup - ON governance.access_denials (org_id, principal, action, resource_kind, resource_id, outcome, level, policies, minute_at); - --- +goose Down -DELETE FROM governance.access_denials WHERE outcome = 'allowed'; -DROP INDEX governance.idx_governance_access_denials_dedup; -CREATE UNIQUE INDEX idx_governance_access_denials_dedup - ON governance.access_denials (org_id, principal, action, resource_kind, resource_id, level, policies, minute_at); -ALTER TABLE governance.access_denials DROP COLUMN outcome; diff --git a/internal/platform/pg/migrations/00061_governance_rename_access_decisions.sql b/internal/platform/pg/migrations/00061_governance_rename_access_decisions.sql deleted file mode 100644 index e3b8c8305..000000000 --- a/internal/platform/pg/migrations/00061_governance_rename_access_decisions.sql +++ /dev/null @@ -1,13 +0,0 @@ --- +goose Up --- The table records every authorization decision, allowed or denied, so it is --- named for decisions. -ALTER TABLE governance.access_denials RENAME TO access_decisions; -ALTER TABLE governance.access_decisions RENAME CONSTRAINT access_denials_pkey TO access_decisions_pkey; -ALTER SEQUENCE governance.access_denials_id_seq RENAME TO access_decisions_id_seq; -ALTER INDEX governance.idx_governance_access_denials_dedup RENAME TO idx_governance_access_decisions_dedup; - --- +goose Down -ALTER INDEX governance.idx_governance_access_decisions_dedup RENAME TO idx_governance_access_denials_dedup; -ALTER SEQUENCE governance.access_decisions_id_seq RENAME TO access_denials_id_seq; -ALTER TABLE governance.access_decisions RENAME CONSTRAINT access_decisions_pkey TO access_denials_pkey; -ALTER TABLE governance.access_decisions RENAME TO access_denials; diff --git a/internal/platform/pg/migrations/00062_events_dispatch_no_run_outcomes.sql b/internal/platform/pg/migrations/00062_events_dispatch_no_run_outcomes.sql deleted file mode 100644 index 5f1167828..000000000 --- a/internal/platform/pg/migrations/00062_events_dispatch_no_run_outcomes.sql +++ /dev/null @@ -1,14 +0,0 @@ --- +goose Up --- A trigger that matched an event but started no run records why: failed (the --- event doesn't match the trigger's mapping), denied (access policy refused the --- start) or disabled (the workflow is disabled). -ALTER TABLE events.dispatches DROP CONSTRAINT dispatches_outcome_check; -UPDATE events.dispatches SET outcome = 'failed' WHERE outcome = 'skipped'; -ALTER TABLE events.dispatches - ADD CONSTRAINT dispatches_outcome_check CHECK (outcome IN ('started', 'failed', 'denied', 'disabled')); - --- +goose Down -DELETE FROM events.dispatches WHERE outcome IN ('denied', 'disabled'); -ALTER TABLE events.dispatches DROP CONSTRAINT dispatches_outcome_check; -UPDATE events.dispatches SET outcome = 'skipped' WHERE outcome = 'failed'; -ALTER TABLE events.dispatches ADD CONSTRAINT dispatches_outcome_check CHECK (outcome IN ('started', 'skipped')); diff --git a/internal/platform/pg/migrations/00063_judgement_action_types.sql b/internal/platform/pg/migrations/00063_judgement_action_types.sql deleted file mode 100644 index 4f1ab0507..000000000 --- a/internal/platform/pg/migrations/00063_judgement_action_types.sql +++ /dev/null @@ -1,90 +0,0 @@ --- +goose Up --- An action class is now an action type, and a proposal to loosen one is an --- autonomy proposal. The account that answers a proposal is stored by its --- identity ID; answered_by keeps the display name it had at the time. -ALTER TABLE judgement.classes RENAME TO action_types; -ALTER TABLE judgement.action_types RENAME CONSTRAINT classes_pkey TO action_types_pkey; -ALTER TABLE judgement.action_types RENAME CONSTRAINT classes_level_check TO action_types_level_check; -ALTER TABLE judgement.action_types - RENAME CONSTRAINT classes_org_id_connector_action_target_kind_environment_key - TO action_types_natural_key; - -ALTER TABLE judgement.class_resets RENAME TO action_type_resets; -ALTER TABLE judgement.action_type_resets RENAME COLUMN class_id TO action_type_id; -ALTER TABLE judgement.action_type_resets RENAME CONSTRAINT class_resets_pkey TO action_type_resets_pkey; -ALTER TABLE judgement.action_type_resets - RENAME CONSTRAINT class_resets_class_id_fkey TO action_type_resets_action_type_id_fkey; -ALTER INDEX judgement.class_resets_by_class RENAME TO action_type_resets_by_action_type; - -ALTER TABLE judgement.class_approvals RENAME TO action_type_approvals; -ALTER TABLE judgement.action_type_approvals RENAME COLUMN class_id TO action_type_id; -ALTER TABLE judgement.action_type_approvals RENAME CONSTRAINT class_approvals_pkey TO action_type_approvals_pkey; -ALTER TABLE judgement.action_type_approvals - RENAME CONSTRAINT class_approvals_class_id_fkey TO action_type_approvals_action_type_id_fkey; -ALTER TABLE judgement.action_type_approvals - RENAME CONSTRAINT class_approvals_decision_id_key TO action_type_approvals_decision_id_key; -ALTER INDEX judgement.class_approvals_counted RENAME TO action_type_approvals_counted; - -ALTER TABLE judgement.class_raises RENAME TO autonomy_proposals; -ALTER TABLE judgement.autonomy_proposals RENAME COLUMN class_id TO action_type_id; -ALTER TABLE judgement.autonomy_proposals RENAME CONSTRAINT class_raises_pkey TO autonomy_proposals_pkey; -ALTER TABLE judgement.autonomy_proposals - RENAME CONSTRAINT class_raises_class_id_fkey TO autonomy_proposals_action_type_id_fkey; -ALTER TABLE judgement.autonomy_proposals RENAME CONSTRAINT class_raises_state_check TO autonomy_proposals_state_check; -ALTER INDEX judgement.class_raises_one_pending RENAME TO autonomy_proposals_one_pending; -ALTER INDEX judgement.class_raises_by_class RENAME TO autonomy_proposals_by_action_type; -ALTER TABLE judgement.autonomy_proposals ADD COLUMN answered_by_id text; - -ALTER TABLE approvals.requests DROP CONSTRAINT requests_kind_check; -UPDATE approvals.requests SET kind = 'autonomy' WHERE kind = 'raise'; -ALTER TABLE approvals.requests ADD CONSTRAINT requests_kind_check CHECK (kind IN ('action', 'autonomy')); - -ALTER TABLE governance.settings RENAME COLUMN raise_after TO propose_autonomy_after; -ALTER TABLE governance.settings - RENAME CONSTRAINT settings_raise_after_check TO settings_propose_autonomy_after_check; - -UPDATE governance.access_policies SET object_kind = 'action_type' WHERE object_kind = 'class'; - --- +goose Down -UPDATE governance.access_policies SET object_kind = 'class' WHERE object_kind = 'action_type'; - -ALTER TABLE governance.settings - RENAME CONSTRAINT settings_propose_autonomy_after_check TO settings_raise_after_check; -ALTER TABLE governance.settings RENAME COLUMN propose_autonomy_after TO raise_after; - -ALTER TABLE approvals.requests DROP CONSTRAINT requests_kind_check; -UPDATE approvals.requests SET kind = 'raise' WHERE kind = 'autonomy'; -ALTER TABLE approvals.requests ADD CONSTRAINT requests_kind_check CHECK (kind IN ('action', 'raise')); - -ALTER TABLE judgement.autonomy_proposals DROP COLUMN answered_by_id; -ALTER INDEX judgement.autonomy_proposals_by_action_type RENAME TO class_raises_by_class; -ALTER INDEX judgement.autonomy_proposals_one_pending RENAME TO class_raises_one_pending; -ALTER TABLE judgement.autonomy_proposals RENAME CONSTRAINT autonomy_proposals_state_check TO class_raises_state_check; -ALTER TABLE judgement.autonomy_proposals - RENAME CONSTRAINT autonomy_proposals_action_type_id_fkey TO class_raises_class_id_fkey; -ALTER TABLE judgement.autonomy_proposals RENAME CONSTRAINT autonomy_proposals_pkey TO class_raises_pkey; -ALTER TABLE judgement.autonomy_proposals RENAME COLUMN action_type_id TO class_id; -ALTER TABLE judgement.autonomy_proposals RENAME TO class_raises; - -ALTER INDEX judgement.action_type_approvals_counted RENAME TO class_approvals_counted; -ALTER TABLE judgement.action_type_approvals - RENAME CONSTRAINT action_type_approvals_decision_id_key TO class_approvals_decision_id_key; -ALTER TABLE judgement.action_type_approvals - RENAME CONSTRAINT action_type_approvals_action_type_id_fkey TO class_approvals_class_id_fkey; -ALTER TABLE judgement.action_type_approvals RENAME CONSTRAINT action_type_approvals_pkey TO class_approvals_pkey; -ALTER TABLE judgement.action_type_approvals RENAME COLUMN action_type_id TO class_id; -ALTER TABLE judgement.action_type_approvals RENAME TO class_approvals; - -ALTER INDEX judgement.action_type_resets_by_action_type RENAME TO class_resets_by_class; -ALTER TABLE judgement.action_type_resets - RENAME CONSTRAINT action_type_resets_action_type_id_fkey TO class_resets_class_id_fkey; -ALTER TABLE judgement.action_type_resets RENAME CONSTRAINT action_type_resets_pkey TO class_resets_pkey; -ALTER TABLE judgement.action_type_resets RENAME COLUMN action_type_id TO class_id; -ALTER TABLE judgement.action_type_resets RENAME TO class_resets; - -ALTER TABLE judgement.action_types - RENAME CONSTRAINT action_types_natural_key - TO classes_org_id_connector_action_target_kind_environment_key; -ALTER TABLE judgement.action_types RENAME CONSTRAINT action_types_level_check TO classes_level_check; -ALTER TABLE judgement.action_types RENAME CONSTRAINT action_types_pkey TO classes_pkey; -ALTER TABLE judgement.action_types RENAME TO classes; diff --git a/internal/platform/pg/migrations/README.md b/internal/platform/pg/migrations/README.md index df8219ac8..deb1a0983 100644 --- a/internal/platform/pg/migrations/README.md +++ b/internal/platform/pg/migrations/README.md @@ -1,5 +1,7 @@ # Migrations One ordered series for the whole binary, applied once at boot by the owner role -under an advisory lock. Name files `NNNNN__.sql` and never edit -one that has shipped. +under an advisory lock. Each module's schema starts from one baseline file, +`NNNNN_.sql`. Until the first release, change a module's schema by +editing its baseline and recreating the database. After the first release, add +a file named `NNNNN__.sql` and never edit one that has shipped.