Repository navigation
feat(keys): keep a key's attribution after its row is purged; v2.31.0 #245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fe0d4d9
test(keys): failing tests for key tombstones (#224)
ivndev001 d5cca27
feat(keys): keep a key's attribution after its row is purged (#224)
ivndev001 265547c
test(keys): move revoked-key purge safety tests onto purgeRevoked (#224)
ivndev001 5c52a4f
refactor(keys): remove ApiKeyRepository.deleteRevokedOlderThan (#224)
ivndev001 9d5fce4
fix(keys): review round 1 on tombstones (#224)
ivndev001 3ccd893
test(keys): tighten tombstone tests after review (#224)
ivndev001 89022b6
test(keys): admin-mutation test cleanup no longer uses removed ApiKey…
ivndev001 2440954
Merge remote-tracking branch 'origin/main' into feat/224-key-tombstones
ivndev001 ef75789
docs(keys): #245 review round 2: delete dialog, KEYS.md and comments …
ivndev001 1f8e074
test(keys): #245 review round 2: pin the operator-delete clock; snaps…
ivndev001 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,48 @@ | ||
| import type { ApiKeyRepository } from "@multi-ai-router/db" | ||
| import type { ApiKeyRepository, ApiKeyTombstoneRepository } from "@multi-ai-router/db" | ||
| import type { RoutingCatalogStore } from "../catalog" | ||
| import type { UsageLabelSets } from "./service" | ||
| import type { KeyLabel, UsageLabelSets } from "./service" | ||
|
|
||
| /** | ||
| * Naming the subjects of a usage report, from what the router already has. | ||
| * | ||
| * Accounts and pools come from the warm catalog, so the two axes an operator filters on cost | ||
| * nothing; keys need the one query, which is unremarkable on the admin plane and is why this is not | ||
| * a third cache. A miss is not an error — spend that happened is still spend, and a deleted subject | ||
| * keeps its totals under its id. | ||
| * nothing; keys need two queries, which is unremarkable on the admin plane and is why this is not | ||
| * a third cache. A miss is not an error — spend that happened is still spend, and a subject with | ||
| * no name anywhere keeps its totals under its id. | ||
| * | ||
| * A key is named by its live row, else by the tombstone its purge or delete left behind (the | ||
| * rollups outlive the row). Tombstones are read through their own method and merged here, never | ||
| * unioned into `keys.list()`: that list is the admin keys list, which must not show a purged key. | ||
| * | ||
| * The two reads are not one snapshot. A delete committing between them removes the row after | ||
| * `keys.list()` saw it, so for that one load the key is still named from the live read; a delete | ||
| * committing the other way round (row read as gone, tombstone read before the commit lands) shows | ||
| * a transient "deleted" for that key until the next load. Neither is worse than the label being | ||
| * decoration, so it is not worth a transaction. | ||
| */ | ||
| export function catalogLabels(deps: { | ||
| readonly keys: Pick<ApiKeyRepository, "list"> | ||
| readonly tombstones: Pick<ApiKeyTombstoneRepository, "listAll"> | ||
| readonly catalog: Pick<RoutingCatalogStore, "accounts" | "pools"> | ||
| }): () => Promise<UsageLabelSets> { | ||
| return async () => ({ | ||
| keys: new Map((await deps.keys.list()).map((key) => [key.id, key.name])), | ||
| accounts: new Map( | ||
| deps.catalog.accounts().map((account) => [account.id, account.snapshot.label]), | ||
| ), | ||
| pools: new Map(deps.catalog.pools().map((pool) => [pool.id, pool.name])), | ||
| }) | ||
| return async () => { | ||
| const [live, stones] = await Promise.all([deps.keys.list(), deps.tombstones.listAll()]) | ||
| const keys = new Map<string, KeyLabel>() | ||
| // Tombstones first, so a live row wins if the two ever coexist (a delete racing a read). | ||
| for (const row of [...stones, ...live]) { | ||
| keys.set(row.id, { | ||
| name: row.name, | ||
| owner: row.owner, | ||
| ownerKind: row.ownerKind, | ||
| tool: row.tool, | ||
| }) | ||
| } | ||
| return { | ||
| keys, | ||
| accounts: new Map( | ||
| deps.catalog.accounts().map((account) => [account.id, account.snapshot.label]), | ||
| ), | ||
| pools: new Map(deps.catalog.pools().map((pool) => [pool.id, pool.name])), | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀 Performance & Scalability | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: developerz-ai/multi-ai-router
Length of output: 4779
Do not load the full tombstone table for each usage read.
The usage loader calls
listAll()every time the usage service requests labels.listAll()performs an unrestricted query overapi_key_tombstones, so each admin usage or recent-attempt request can load every retained tombstone into memory, regardless of its IDs or date range. Bound the tombstone lookup to the keys needed by the requested usage data, or add an equivalent cache with an explicit invalidation contract.🤖 Prompt for AI Agents