Skip to content

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

Merged
SasSwart merged 2 commits into
mainfrom
sas/aigov-712-store-the-organization-on-ai-gateway-interceptions
Oct 28, 2026
Merged

SasSwart merged 2 commits into
mainfrom
sas/aigov-712-store-the-organization-on-ai-gateway-interceptions

Conversation

@SasSwart

@SasSwart SasSwart commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

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.

@linear-code

linear-code Bot commented Oct 21, 2026

Copy link
Copy Markdown

AIGOV-712

Add nullable organization_id (FK to organizations, ON DELETE RESTRICT)
and organization_source (enum: workspace, chatd, effective_group)
columns to aibridge_interceptions, with a check constraint that keeps
them set or unset together.

RecordInterceptionRequest gains organization_id (18) and
organization_source (19), and the aibridged API moves to v1.5. The
server stores a valid pair and otherwise logs a warning and stores
NULLs, so a bad pair never fails the user's request. Nothing sends the
fields yet.

Part of AIGOV-712.
@SasSwart
SasSwart force-pushed the sas/aigov-712-store-the-organization-on-ai-gateway-interceptions branch from 7119bcc to b4350b4 Compare October 26, 2026 09:52
@SasSwart
SasSwart marked this pull request as ready for review October 26, 2026 10:38
@SasSwart
SasSwart requested a review from ssncferreira October 26, 2026 10:38
@SasSwart
SasSwart added this pull request to stack #31264 October 26, 2026 12:00

@ssncferreira ssncferreira left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Comment on lines +29 to +30
// API v1.5:
// - Adds organization_id and organization_source to RecordInterceptionRequest.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case we merge this before the release freeze, this can actually be included in v1.4

Comment on lines +985 to +989
case rawID == "":
return uuid.NullUUID{}, database.NullAIBridgeInterceptionOrganizationSource{}, xerrors.New("organization source set without organization ID")
case rawSource == "":
return uuid.NullUUID{}, database.NullAIBridgeInterceptionOrganizationSource{}, xerrors.New("organization ID set without organization source")
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I don't think these two checks are needed: an empty ID already fails uuid.Parse, and an empty source fails source.Valid(). I would suggest to remove them.

-- 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,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Should we add an index on organization_id? We'll need to filter interceptions by org later, for example when listing sessions in the API and UI. Can be done as a follow-up, but we should add it to linear, so it is not forgotten

Comment on lines +978 to +979
// interceptionOrganization parses the optional organization attribution. It
// returns both values valid or neither, so an invalid pair never fails the record.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very-nit: it can theoretically happen that the org is a valid UUID, but not an org, but that will fail the FK on insert. Can we say that the function only checks the format and that the caller is expected to send an existing org?

})
}

func TestAIBridgeInterceptionOrganizationColumns(t *testing.T) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: these subtests seem to follow a similar pattern, so a table driven test structure should be possible and make the tests easier to read. Wdyt?

Index aibridge_interceptions.organization_id, drop the redundant
one-sided empty checks in interceptionOrganization, document that it
checks only the format, and make the organization column test
table-driven.
@SasSwart
SasSwart merged commit 161b723 into main Oct 28, 2026
27 checks passed
@SasSwart
SasSwart deleted the sas/aigov-712-store-the-organization-on-ai-gateway-interceptions branch October 28, 2026 08:52
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 28, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants