feat(coderd): store the organization on AI Gateway interceptions (#31118)

Adds `organization_id` (FK to `organizations`, `ON DELETE RESTRICT`) and
`organization_source` (new enum: `workspace`, `chatd`,
`effective_group`) to `aibridge_interceptions`, with a check constraint
that keeps them set or unset together. Cost and pricing run later, in
`RecordTokenUsage`, so they need the organization that AIGOV-716a
resolves once per request stored on the interception row.

`RecordInterceptionRequest` gains `organization_id` (18) and
`organization_source` (19), and the aibridged API moves to v1.5.
`RecordInterception` stores a valid pair; any other combination is
logged as a warning and stored as NULLs, so a gateway bug never turns
into an HTTP 500 for the user. A well-formed ID for an organization that
does not exist fails on the FK, like an unknown `initiator_id`. Nothing
sends the fields yet, so behaviour is unchanged until AIGOV-716a.

**Malformed values are stored as empty, not rejected.** A failed
`RecordInterception` returns HTTP 500 to the user, so the server logs a
warning and stores NULLs instead. The one exception is a well-formed ID
for an organization that does not exist: the foreign key rejects it, the
same as an unknown `initiator_id` today.

Part of AIGOV-712

*This PR was generated by Coder Agents on behalf of @SasSwart.*
This commit is contained in:
Sas Swart authored and GitHub committed 2026-10-28 10:52:35 +02:00
1 parent c440977e4a
commit 161b7233d9
17 files changed
+744 -376

No files matched your search

+19 -1
View File
@@ -42,6 +42,12 @@ CREATE TYPE aibridge_interception_error_type AS ENUM (
'unknown'
);
CREATE TYPE aibridge_interception_organization_source AS ENUM (
'workspace',
'chatd',
'effective_group'
);
CREATE TYPE api_key_scope AS ENUM (
'coder:all',
'coder:application_connect',
@@ -1843,7 +1849,10 @@ CREATE TABLE aibridge_interceptions (
agent_firewall_sequence_number integer,
error_type aibridge_interception_error_type,
error_message character varying(1024),
workspace_id uuid
workspace_id uuid,
organization_id uuid,
organization_source aibridge_interception_organization_source,
CONSTRAINT aibridge_interceptions_organization_source_check CHECK (((organization_id IS NULL) = (organization_source IS NULL)))
);
COMMENT ON TABLE aibridge_interceptions IS 'Audit log of requests intercepted by AI Bridge';
@@ -1874,6 +1883,10 @@ COMMENT ON COLUMN aibridge_interceptions.error_message IS 'Raw terminal upstream
COMMENT ON COLUMN aibridge_interceptions.workspace_id IS 'The workspace in which the agent ran. NULL when no workspace context is available.';
COMMENT ON COLUMN aibridge_interceptions.organization_id IS 'The organization the interception was attributed to. NULL when no organization was resolved.';
COMMENT ON COLUMN aibridge_interceptions.organization_source IS 'How organization_id was chosen. NULL exactly when organization_id is NULL.';
CREATE TABLE aibridge_model_thoughts (
interception_id uuid NOT NULL,
content text NOT NULL,
@@ -5052,6 +5065,8 @@ CREATE INDEX idx_aibridge_interceptions_initiator_id ON aibridge_interceptions U
CREATE INDEX idx_aibridge_interceptions_model ON aibridge_interceptions USING btree (model);
CREATE INDEX idx_aibridge_interceptions_organization_id ON aibridge_interceptions USING btree (organization_id) WHERE (organization_id IS NOT NULL);
CREATE INDEX idx_aibridge_interceptions_provider ON aibridge_interceptions USING btree (provider);
CREATE INDEX idx_aibridge_interceptions_session_id ON aibridge_interceptions USING btree (session_id) WHERE (ended_at IS NOT NULL);
@@ -5509,6 +5524,9 @@ ALTER TABLE ONLY ai_seat_state
ALTER TABLE ONLY aibridge_interceptions
ADD CONSTRAINT aibridge_interceptions_initiator_id_fkey FOREIGN KEY (initiator_id) REFERENCES users(id);
ALTER TABLE ONLY aibridge_interceptions
ADD CONSTRAINT aibridge_interceptions_organization_id_fkey FOREIGN KEY (organization_id) REFERENCES organizations(id) ON DELETE RESTRICT;
ALTER TABLE ONLY api_keys
ADD CONSTRAINT api_keys_user_id_uuid_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;