Skip to content

AI-3735: no foreign app by organization id in teams - #2383

Merged
martinmitrevski merged 4 commits into
mainfrom
martinmitrevski/ai-3735-teams-org-id
Nov 24, 2026
Merged

martinmitrevski merged 4 commits into
mainfrom
martinmitrevski/ai-3735-teams-org-id

Conversation

@martinmitrevski

@martinmitrevski martinmitrevski commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Part of AI-3735.

Why

Follow-up to #2372 (merged), from its review.

GET /v1/teams has the same app-id/organization-id collision #2372 fixed in Spend.Subject. On a router with no usage service, or for an app in no organization, it sums the request rows of AppOrganizations.Apps(org). That list always includes the organization's own id as an app, so another customer's app whose id matches was answered as the caller's: its team names, spend and app id.

  • Billed organization: the app whose id is the organization's is counted.
  • App in no organization (organization is the app's own id): every app seen under an organization of that id is counted.

/v1/stats/spend?scope=organization also uses Apps, but it filters requests.organization_id as well, so a foreign row never matches. It is unchanged.

How it works

Spend.Teams(organization, ..., billed)
├─ billed  -> AppOrganizations.Members(organization)
│             (seen under it, or registered there and seen nowhere else)
└─ !billed -> [organization]   (an app in no organization: itself alone)
  • Members is Apps without the organization's own id prepended. Apps is now [organization] + Members, unchanged for its other callers.
  • teamSpend (account.go) already had billed and now passes it on.
  • An app whose id is the organization's still counts when it was seen under it: it is then one of its Members.

Validation

Test Defect it catches Fails before the fix
SelfHostedAccountSuite/TestTeamsLeaveOutAnAppWhoseIdIsTheOrganizations A billed organization's teams include the work of an app whose id is the organization's yes
SelfHostedLimitsSuite/TestAnAppInNoOrganizationsTeamsAreItsOwnWorks An app in no organization's teams include an app seen under an organization of its id yes
SelfHostedLimitsSuite/TestAnOrganizationsTeamsAreTheWorkOfEveryAppSeenUnderIt A billed organization's teams leave out an app seen under it: one whose id is the organization's, or any app read from registrations only no (passes before and after; it pins the fix from over-reaching)
StoreSuite/TestSpendQueryBudgets (unbilled Spend.Teams) An app in no organization's teams look up apps: one sum a shard, no lookup no (new budget)
StoreSuite/TestEveryHotTableIsReadByIndex/Spend.Teams of an organization A billed read's IN list of two apps is not read by requests_customer_spend_idx no (new plan case)
  • Fails before the fix: the first two were checked by swapping in the old Spend.Teams with go test -overlay.
  • Mutants: with go test -overlay, a Spend.Teams reading registered apps only, and one leaving out the organization's own id, both pass every earlier test and fail TestAnOrganizationsTeamsAreTheWorkOfEveryAppSeenUnderIt. An unbilled read that also looks up the organization's apps fails the new budget.
  • Passing billed through: a constant false fails the existing TestTeamsSumTheRequestsThisMonth, and a constant true fails the no-organization test above.
  • Explain seed: it now records customer-7 and customer-107 under org-7, which the seed's requests already name, so the billed case sums two apps. The unbilled Spend.Teams case stays.
  • Merged with main at 09bcd98 (refactor(store): remove code nothing in production calls #2377 removed Requests.Insert): the teams test and spent (limits_test.go) use insertRequest, the same change as test(api): insert limits test requests with insertRequest #2388 for spent.
  • Passed locally after the merge: SelfHostedAccountSuite, SelfHostedLimitsSuite, AccountSuite and LimitsSuite; the store's TestSpendQueryBudgets and the whole TestEveryHotTableIsReadByIndex; go vet -tags integration ./... in acceleration/.

🤖 Generated with Claude Code

@martinmitrevski
martinmitrevski marked this pull request as ready for review November 24, 2026 15:01
Base automatically changed from martinmitrevski/ai-3735-high-cross-tenant-billable-spend to main November 24, 2026 15:06
GET /v1/teams, on a router with no usage service or for an app in no
organization, sums the request rows of AppOrganizations.Apps, which
always holds the organization's own id as an app. So another customer's
app whose id matched was counted: for an organization, the app whose id
is the organization's; for an app in no organization, every app seen
under an organization of the app's id. Their teams, spend and app ids
were answered as the caller's.

Spend.Teams now takes billed, as Spend.Subject does: a billed
organization counts its Members (Apps without its own id), an app in no
organization itself alone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@martinmitrevski
martinmitrevski force-pushed the martinmitrevski/ai-3735-teams-org-id branch from 7c9ba14 to 58974dc Compare November 24, 2026 15:10

@thesyncim thesyncim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reviewed at 58974dc. The production change is right and I found no defect in it: both new tests fail on the old code, and 17 of 21 mutants were killed. What needs a change is the merge with current main (first inline comment); the rest is tests and nits. In the body, the route is /v1/stats/spend?scope=organization, not /v1/spend, and a Part of AI-3735 line would help.

Comment thread acceleration/internal/store/spend.go Resolved
martinmitrevski and others added 2 commits November 24, 2026 17:29
- insertRequest in the teams test and in spent, since #2377 removed
  Requests.Insert.
- A billed organization's teams count every app seen under it, the one
  whose id is the organization's among them. Leaving out the
  organization's own id, or reading registered apps only, passed before.
- The unbilled Spend.Teams read looks up no apps: one sum a shard.
- The explain seed records customer-7 and customer-107 under org-7, and
  a billed Spend.Teams case reads their IN list by index.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@martinmitrevski

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. All five threads have replies. From the review summary:

  • Merge with main: merged at 09bcd98 (d4f44fe), with the test follow-ups in f0aad6b.
  • Description: the route is now /v1/stats/spend?scope=organization, and the description opens with Part of AI-3735. Its validation table lists the new tests.
  • Rerun after the merge: SelfHostedAccountSuite, SelfHostedLimitsSuite, AccountSuite and LimitsSuite, the store's TestSpendQueryBudgets and the whole TestEveryHotTableIsReadByIndex all pass, as does go vet -tags integration ./... in acceleration/.

@thesyncim thesyncim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Re-reviewed at f0aad6b: every earlier comment is answered, the new tests kill the variants that passed before, and I have no open finding on the code or tests. The one thing left is the conflict with main (inline). I'll approve once it's resolved that way and CI is green.

@thesyncim thesyncim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No findings at f0aad6b (note: PR is still CONFLICTING with main on acceleration/internal/api/limits_test.go from #2386, as flagged on the open thread — just needs a merge from main before merging).

Checked and sound: all five prior review threads are addressed (insertRequest, TestAnOrganizationsTeamsAreTheWorkOfEveryAppSeenUnderIt, unbilled budget in TestSpendQueryBudgets, billed Spend.Teams of an organization explain case, and the pre-existing background-join question answered); Spend.Teams delegates to AppOrganizations.Members when billed and [organizationID] when !billed; generated.sh reports no spec/client drift; and all CI checks passed on f0aad6b.

spent keeps main's InsertMany and this branch's usage categories.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@martinmitrevski
martinmitrevski merged commit 3450569 into main Nov 24, 2026
13 checks passed
@martinmitrevski
martinmitrevski deleted the martinmitrevski/ai-3735-teams-org-id branch November 24, 2026 21:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants