Skip to content

fix(store): count a request only toward its own organization's or app's grants - #2183

Merged
thesyncim merged 3 commits into
mainfrom
fix/ai-3475-grant-uses-own-organisation
Nov 23, 2026
Merged

thesyncim merged 3 commits into
mainfrom
fix/ai-3475-grant-uses-own-organisation

Conversation

@thesyncim

@thesyncim thesyncim commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Problem
addGrantUses added a request row's cost to each grant the row named, matching the grant by id alone, and the reconcile in LimitGrantUses.Reconcile did the same for every row it re-read. A row could therefore count toward a grant of another organization, or toward a grant scoped to another app. An imported requests row also kept the grant ids it was sent with, which name other grants at the destination.

Fix

  • addGrantUses counts a use only when the grant belongs to the row's organization, or to the row's app as the enforcer draws grants for it: the organization in app_organizations for that app, or the app's own id for an app in none. GrantUse carries the row's customer_id and organization_id, filled by GrantUsesOf on the write path and read by the reconcile.
  • The app's side is needed because an app in no organization keeps its grants under its own id while its rows carry no organization, and the writer leaves a row without its organization when the app cannot be looked up. The row's organization is kept beside it because the enforcer draws an organization's grant before the app's membership row is written in the background.
  • A grant scoped to an app (or to its key or user) counts only that app's rows.
  • A use refused on whose the grant is, and not counted before, adds to router_usage_grant_uses_refused_total, so a grant that pays for less than it admitted shows why. One case still lands there: an app in no organization draws its own grant and is recorded under an organization before its row is counted. The data cannot tell that apart from an app whose id equals another organization's id.
  • identity() for requests sets limit_grants to '{}', as it already sets to_ship to false: an imported request was drawn from the grants of the deployment that recorded it.

Tests

  • TestAWrittenRowCountsOnlyTowardItsOwnOrganizationsGrant (one database and sharded): a row of another organization, or of no organization and no app of the grant's, leaves the grant unchanged. Failed before the fix.
  • TestARowWithoutItsOrganizationCountsTowardItsAppsGrants (one database and sharded): a row with no organization counts toward its app's own grant and toward the grant of the organization its app is in. A row of another app, or of an app in another organization whose id is the grant's organization, counts nothing. Failed before this change.
  • TestAGrantScopedToAnAppCountsOnlyThatAppsRows (one database and sharded): a row of another app of the same organization counts nothing toward an app or key grant. Failed without the app match.
  • TestAUseRefusedOnWhoseTheGrantIsIsCountedOnMetrics: only a foreign use not counted before is a refusal; a counted use, a use counted earlier and one of a missing grant are not. Failed without the count, and with either exclusion dropped.
  • TestAReconcileCountsARowOnlyTowardItsOwnOrganizationsGrant and TestAReconcileCountsARowWithoutItsOrganizationTowardItsAppsGrants: the same in the reconcile. Each failed before the fix.
  • TestAnImportedRequestNamesNoGrant: an imported request comes back with no grant ids. Failed before the fix.

Fixes AI-3475

@thesyncim
thesyncim force-pushed the fix/ai-3475-grant-uses-own-organisation branch 2 times, most recently from 8a6f49f to 04a7820 Compare November 23, 2026 11:34
@thesyncim thesyncim changed the title fix(store): count a request only toward its own organization's grants fix(store): count a request only toward its own organization's or app's grants Nov 23, 2026
@thesyncim
thesyncim marked this pull request as ready for review November 23, 2026 12:12
@thesyncim thesyncim added the data-layer-review: ready Data layer review fix: passed automated review, merges cleanly, ready for a human review label Nov 23, 2026

@kanat kanat 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.

Reviewed af3d281 merged onto current main (129b3c4). CI on the head: all checks pass, integration (router) and integration (store) included.

[Question] R1.3 (no diff line): rows imported before this fix keep the grant ids they were sent with (and their sent organization_id, which #2181 addresses). The PR adds no cleanup, so whatever such rows already added to a grant's used_micros stays. The reconcile re-reads only rows after its mark, so they are not counted again. A read-only count on each deployment of imported request rows that name a grant outside their organization would show whether a cleanup is needed. Ticket.

Checked and sound: every crediting path goes through the new join: the one-database write in Requests.InsertMany, the sharded writer queue (GrantUsesOf to grantCounter) and reconcileShard. limit_grants and app_organizations are both in the shared database, so the join holds with shards. app_organizations is keyed by app_id, so the join cannot duplicate a use. A grant's organization is never empty, so a row with no organization matches only through its app. The owner rule matches how the enforcer picks the account it draws from (accountOf / limitsOrganization) and how the writer attributes the row's organization. 12 mutations of the new rule (each IN member, the coalesce order and its fallback, LEFT JOIN to JOIN, each new GrantUse field at both producers, the identity() change): all killed. No migration, so rollback is unaffected. Heads-up: #2181 and #2189 change the same identity() line.

Verdict: approve (GO). No Blocker or Should fix. R1.1, R1.2 and R1.3 are follow-up tickets.

A grant use now matches the grant on the request row's organization as
well as its id, on the write path and in the reconcile. An imported
request names no grant.
…nization

A row written without its organization, as an app in none has it or as the
writer leaves a row it could not attribute, now counts toward the grants of
its app: those of the organization the app was last seen under, or the
app's own id for an app in none. A grant of any other organization still
counts nothing.
@thesyncim

Copy link
Copy Markdown
Member Author

On R1.3: agreed, the fix only stops new imports from doing it. Rows that came in before it keep whatever they already added to a grant, and the reconcile never goes back for them.

I filed AI-3560 for the read-only count you describe, using the same join as this PR, on each deployment. If it comes back zero everywhere, we close it. If it finds rows, we write a one-off cleanup. I would rather not ship a cleanup before we know there is anything to clean.

R1.1 and R1.2 are going into this PR rather than tickets, since I am already in there.

@thesyncim
thesyncim force-pushed the fix/ai-3475-grant-uses-own-organisation branch from af3d281 to 560d345 Compare November 23, 2026 13:38
@thesyncim thesyncim added data-layer-review: ready Data layer review fix: passed automated review, merges cleanly, ready for a human review and removed data-layer-review: ready Data layer review fix: passed automated review, merges cleanly, ready for a human review labels Nov 23, 2026
@thesyncim
thesyncim merged commit 16ab0c6 into main Nov 23, 2026
13 checks passed
@thesyncim
thesyncim deleted the fix/ai-3475-grant-uses-own-organisation branch November 23, 2026 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data-layer-review: ready Data layer review fix: passed automated review, merges cleanly, ready for a human review docs project-info

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants