Files
coder/coderd/database/migrations/000634_aibridge_interception_organization.up.sql
T
Sas Swart 161b7233d9 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.*
2026-10-28 10:52:35 +02:00

22 lines
1.1 KiB
SQL

CREATE TYPE aibridge_interception_organization_source AS ENUM (
'workspace',
'chatd',
'effective_group'
);
-- RESTRICT: organizations are soft-deleted, so this only blocks a manual hard
-- delete, and attribution must never silently vanish.
ALTER TABLE aibridge_interceptions
ADD COLUMN organization_id uuid REFERENCES organizations(id) ON DELETE RESTRICT,
ADD COLUMN organization_source aibridge_interception_organization_source,
ADD CONSTRAINT aibridge_interceptions_organization_source_check
CHECK ((organization_id IS NULL) = (organization_source IS NULL));
-- Supports filtering interceptions by organization, e.g. listing sessions.
CREATE INDEX idx_aibridge_interceptions_organization_id
ON aibridge_interceptions (organization_id)
WHERE organization_id IS NOT NULL;
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.';