Skip to content

feat: limit effective group resolution to an organization - #31119

Merged
SasSwart merged 1 commit into
mainfrom
sas/aigov-715-limit-effective-group-resolution-to-an-organization
Oct 29, 2026
Merged

SasSwart merged 1 commit into
mainfrom
sas/aigov-715-limit-effective-group-resolution-to-an-organization

Conversation

@SasSwart

@SasSwart SasSwart commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

AI budget resolution searches all of a user's groups across every organization. Whichever group wins determines the organization. #31120 needs to resolve inside a known organization, and it needs to know which organization site-wide resolution picked.

Add an organizationID uuid.NullUUID argument to budget.ResolveUserAIBudget and budget.ResolveUserEffectiveGroup. When it is set, count only that organization's override, group budgets and Everyone group. An organization that is unknown, deleted or that the user is not a member of resolves to nothing. When it is null, resolve exactly as before. Return the winning group's organization in budget.EffectiveGroup.OrganizationID.

Read the override through a new resolution-only query, GetUserAIBudgetOverrideWithOrganization, so that the CRUD query GetUserAIBudgetOverride and the admin endpoints stay untouched. Add a nullable organization_id filter to GetHighestGroupAIBudgetByUser and GetUserEveryoneFallbackGroup.

Every caller passes null, so behaviour is unchanged. There is no migration and no proto change.

Scoping to an organization can lift a limit: a user capped in org B has no limit in org A if A has no budgets. AIGOV-711 intends this, and it takes effect in #31120.

Part of AIGOV-715.


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

@SasSwart
SasSwart force-pushed the sas/aigov-715-limit-effective-group-resolution-to-an-organization branch from b175ea2 to cb4e496 Compare October 26, 2026 10:38
@SasSwart
SasSwart added this pull request to stack #31264 October 26, 2026 12:00

@SasSwart SasSwart left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Initial Self Review

// Resolve the effective group for attribution, independent of whether the
// model is priced.
effectiveGroup, ok, err := budget.ResolveUserEffectiveGroup(ctx, s.store, intc.InitiatorID, s.budgetPolicy)
effectiveGroup, ok, err := budget.ResolveUserEffectiveGroup(ctx, s.store, intc.InitiatorID, uuid.NullUUID{}, s.budgetPolicy)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

as per my previous comment, I think we should propagate org awareness back up the call stack to the callers of resolveTokenUsageCost, but as a follow-up

@SasSwart
SasSwart marked this pull request as ready for review October 26, 2026 12:38

@SasSwart SasSwart left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Self review

@SasSwart
SasSwart force-pushed the sas/aigov-715-limit-effective-group-resolution-to-an-organization branch from cb4e496 to a37f1d1 Compare October 28, 2026 08:35
Base automatically changed from sas/aigov-712-store-the-organization-on-ai-gateway-interceptions to main October 28, 2026 08:52
@SasSwart
SasSwart force-pushed the sas/aigov-715-limit-effective-group-resolution-to-an-organization branch 2 times, most recently from db38ba8 to c81cdac Compare October 28, 2026 08:59

@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 👍 just a note that GetUserAIBudgetOverrideWithOrganization needs to take deleted orgs into account

Comment thread coderd/aibridge/budget/budget.go Outdated
Comment on lines +52 to +53
// and Everyone group count, and the organization must not be deleted. When it
// is null, groups from all of the user's organizations count.

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: maybe "When it is null, resolution is site-wide: the override, group budgets and Everyone groups from all of the user's organizations count"? That makes it clear the override and Everyone group take part too, not just group budgets.

FROM user_ai_budget_overrides
WHERE user_id = @user_id;

-- name: GetUserAIBudgetOverrideWithOrganization :one

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.

Could we reuse GetUserAIBudgetOverride with an optional organization_id, instead of adding a second query? It could also return the group's org for the resolver. Wdyt?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

If we merge them, the AND organizations.deleted = false check is awkward. Should you still be able to view and delete overrides for an organization that has been soft deleted?

I'm going to keep them separate for now, but I'm happy to merge them in a follow-up if you want to explore this further?

budget.ResolveUserAIBudget and budget.ResolveUserEffectiveGroup take an
optional organization. When it is set, only that organization's
override, group budgets and Everyone group count. When it is null,
resolution is unchanged. budget.EffectiveGroup also reports the
organization of the resolved group.

Budget resolution reads the override through a new query,
GetUserAIBudgetOverrideWithOrganization, so the CRUD query stays as it
is. All callers pass a null organization, so behaviour does not change.

Part of AIGOV-715.
@SasSwart
SasSwart force-pushed the sas/aigov-715-limit-effective-group-resolution-to-an-organization branch from c81cdac to 8705414 Compare October 28, 2026 13:25
@SasSwart
SasSwart merged commit eb38759 into main Oct 29, 2026
27 checks passed
@SasSwart
SasSwart deleted the sas/aigov-715-limit-effective-group-resolution-to-an-organization branch October 29, 2026 10:14
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 29, 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