Skip to content

feat(coderd): add queries for organization AI model prices - #30718

Merged
SasSwart merged 7 commits into
mainfrom
sdlcgraph/aigov-612-3-store
Oct 22, 2026
Merged

SasSwart merged 7 commits into
mainfrom
sdlcgraph/aigov-612-3-store

Conversation

@SasSwart

@SasSwart SasSwart commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

Adds the store queries for organization AI model prices in ai_model_configs, with authorization. They cover basic CRUD on one price, keyed by (organization_id, provider_id, model).

  • GetOrganizationAIProviderModelPrice reads one price.
  • UpsertOrganizationAIProviderModelPrice sets one price, with typed params. It rewrites an existing row only when a price differs, so updated_at records the last change.
  • DeleteOrganizationAIProviderModelPrice soft deletes one price. Reads and upserts only match live rows, so setting a price after a delete inserts a new live row.

All three authorize against ResourceAiModelPrice.InOrg(org). The new AiModelPriceInOrg case in TestRolePermissions pins that only owners and org admins pass. A TODO in dbauthz notes that this should become a resource for AI Governance model config.

Each query has its own test. The upsert tests cover that identical prices leave updated_at unchanged, that changed prices advance it, and that an empty model is rejected.

Used later: #30719 seeds its lookup tests with the upsert. #30720 serves all three through the org price API.


This PR was generated by Coder Agents on behalf of @SasSwart.

@linear-code

linear-code Bot commented Oct 13, 2026

Copy link
Copy Markdown

AIGOV-612

@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 marked this pull request as ready for review October 15, 2026 08:14
@SasSwart
SasSwart requested a review from ssncferreira October 15, 2026 09:19
@SasSwart
SasSwart force-pushed the sdlcgraph/aigov-612-3-store branch from 83743f8 to 450a56b Compare October 15, 2026 11:12
@SasSwart SasSwart changed the title feat(coderd): add queries for organization AI model prices feat(coderd/database): add queries for organization AI model prices Oct 15, 2026
@SasSwart SasSwart changed the title feat(coderd/database): add queries for organization AI model prices feat(coderd): add queries for organization AI model prices Oct 15, 2026
Comment thread coderd/database/dbauthz/dbauthz.go Resolved
Comment thread coderd/database/querier_test.go Resolved
@SasSwart
SasSwart force-pushed the sdlcgraph/aigov-612-3-store branch 4 times, most recently from f1d0d8c to 04b9a40 Compare October 19, 2026 12:22

@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

Comment thread coderd/database/dbauthz/dbauthz.go Resolved
Comment thread coderd/database/dbauthz/dbauthz.go Resolved
Comment thread coderd/database/querier_test.go Resolved
Base automatically changed from sdlcgraph/aigov-612-2-schema to main October 21, 2026 06:17
SasSwart added a commit that referenced this pull request Oct 21, 2026
Adds `ai_model_configs`: an organization's AI Governance configuration
for one model on one configured provider. Token prices are its first
settings. Migration `000630` is additive and needs no backfill.

- Rows are soft deleted with a `deleted` flag. A surrogate `id` is the
primary key, and a partial unique index allows one live row per
`(organization_id, provider_id, model)`. Cascades on delete of the
organization or provider. The organization is a column, as in other
organization-scoped tables.
- A check constraint rejects an empty model.
- Pricing per provider ID is what lets `openai-compat` gateways be
priced, and lets providers of the same type have different prices.
- Site-wide prices per provider type stay in the existing
`ai_model_prices` table, unchanged.

Recording which price was used on each token usage record is deferred
until its design settles.

**Used later:** #30718 adds the queries that read and write the table,
and #30719 looks prices up from it. The rest of the diff is generated.

---

*This PR was generated by Coder Agents on behalf of @SasSwart.*
Organization prices are only per configured provider, so remove the
upsert and delete queries for provider type prices and their listing
branch.
Org admins edit prices one model at a time, so the upsert takes typed
params instead of a JSON batch. The org price listing tells organization
and site-wide rows apart by provider_id instead of a price kind.
Replace GetOrganizationAIModelPrices, which listed every price that
applies to an organization, with GetOrganizationAIProviderModelPrice,
which reads one price by organization, provider and model. Queries
target the renamed ai_model_configs table.

Split the tests per query, and cover that an identical upsert leaves
updated_at unchanged and that an empty model is rejected.

Leave a TODO to authorize these queries against a resource for AI
Governance model config rather than ResourceAiModelPrice.
DeleteOrganizationAIProviderModelPrice now flags the live row as deleted
instead of removing it. Reads and upserts only match live rows, so an
upsert after a delete inserts a new live row and leaves the soft-deleted
one untouched.
@SasSwart
SasSwart force-pushed the sdlcgraph/aigov-612-3-store branch from cacf787 to 1e4ae6b Compare October 21, 2026 06:38
…elper

The get helper captured the parent test's context and testing.T, which
paralleltestctx flags once the subtests call t.Parallel.
SasSwart added a commit that referenced this pull request Oct 21, 2026
…#31116)

Adds @ssncferreira and @SasSwart as code owners of `ai_providers.sql`,
`ai_provider_keys.sql` and `aicostcontrol.sql`, alongside @ibetitsmike
and @johnstcn. The `codersdk/aiproviders*.go` rules are unchanged.

Both current owners are on PTO, which leaves the organization AI model
price stack (#30718, #30719) without anyone who can give the required
code owner review on `aicostcontrol.sql`.

> Generated by Coder Agents on behalf of @SasSwart.
@SasSwart
SasSwart requested a review from ssncferreira October 21, 2026 08:36

@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 👍 I think we should just update the query names to Config, instead of Price

Comment thread coderd/database/dbauthz/dbauthz.go Outdated
// only its prices. Authorize it, and the other organization model price
// queries, against a resource that represents that configuration instead of
// ResourceAiModelPrice.
func (q *querier) DeleteOrganizationAIProviderModelPrice(ctx context.Context, arg database.DeleteOrganizationAIProviderModelPriceParams) (int64, error) {

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: since this is for the whole AI Governance model, remove Price from the query (same for the other dbauthz methods):

Suggested change
func (q *querier) DeleteOrganizationAIProviderModelPrice(ctx context.Context, arg database.DeleteOrganizationAIProviderModelPriceParams) (int64, error) {
func (q *querier) DeleteOrganizationAIProviderModelConfig(ctx context.Context, arg database.DeleteOrganizationAIProviderModelPriceParams) (int64, error) {

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.

$Renamed the three queries, their params, and the related comments to Config in 7f60cf3.\n\nReply generated by Coder Agents on behalf of @SasSwart.

CASE WHEN source = 'custom' THEN 0 ELSE 1 END ASC
LIMIT 1;

-- name: GetOrganizationAIProviderModelPrice :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.

Same as above, as well as the other queries, and the comments:

Suggested change
-- name: GetOrganizationAIProviderModelPrice :one
-- name: GetOrganizationAIProviderModelConfig :one

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.

$Renamed the three queries, their params, and the related comments to Config in 7f60cf3.\n\nReply generated by Coder Agents on behalf of @SasSwart.


-- name: GetOrganizationAIProviderModelPrice :one
-- Returns the organization's live price for one model on one configured
-- provider. Soft-deleted rows are ignored.

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 this join ai_providers and filter out deleted providers? This way models from deleted providers would not be returned.


// upsertOrgProviderPrice sets an organization's price for a model on a
// configured provider, with only an input price.
func upsertOrgProviderPrice(ctx context.Context, t *testing.T, db database.Store, orgID, providerID uuid.UUID, model string, inputPrice int64) {

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 this to dbgen?

@SasSwart
SasSwart merged commit da78c88 into main Oct 22, 2026
29 checks passed
@SasSwart
SasSwart deleted the sdlcgraph/aigov-612-3-store branch October 22, 2026 13:58
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 22, 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