Repository navigation
feat(backend): add the Developer API key memory-grant backfill (dry-run only) - #10744
Merged
Merged
Conversation
Developer API keys created before the create-path grant seeding landed (8e03144, 2026-06-27 22:54:46 +0700) have no entry under `users/{uid}/memory_control/app_key_memory_grants`. The grant gate `authorize_memory_external_default_memory_read` fails closed on a missing grant, so those keys get a permanent 403 from GET /v1/dev/user/memories regardless of the scopes on the key, while /conversations and /action-items keep working — which reads to the developer as "the memories endpoint is broken". Nothing backfills them, so the state is unrecoverable without creating a new key. The script grants nothing new. It materialises the grant each key's own scopes already imply, making exactly the call the create path makes: grant_default_read = has_scope(scopes, Scopes.MEMORIES_READ) grant_write = has_scope(scopes, Scopes.MEMORIES_WRITE) if grant_default_read or grant_write: seed_developer_api_key_memory_grant(...) Scope resolution goes through `utils.scopes.has_scope`, the same authority the request path uses, rather than a literal membership test. That matters for one legacy shape the issue's filter would miss: `has_scope(None, ...)` resolves a key with no stored `scopes` field to the read-only set, which includes `memories:read`. Such a key can already read memories at the request layer, so it needs the grant too. Write is never inferred that way, since `memories:write` is not a read-only scope. Keys that already hold a grant for the same app/key are skipped, so re-running is safe. Dry run is the default and performs no writes. Modelled on scripts/backfill_mcp_key_full_access.py. This PR delivers the script and its dry-run mode only. It has not been run against production. Tests (tests/unit/test_backfill_developer_api_key_memory_grants.py, 12 cases) pin the two properties that make it safe to run: - dry run writes nothing at all - applying grants exactly the key's own capability: a read-scoped key gets default_read without write, a write-scoped key gets write without read, an absent scopes field resolves to read-only, and a key with no memory scope or created after seeding is left untouched plus an existing grant is never rewritten, a second key for the same user is still seeded after the per-uid grant cache is invalidated, an unusable created_at is skipped rather than guessed, a naive created_at is read as UTC instead of aborting the scan, --limit bounds the scan, and a key with no user_id is skipped. Verification (backend, aarch64, system python3.12 + pytest 9.0.3): BACKEND_UNIT_TEST_FILE_LIST=tests/unit/test_backfill_developer_api_key_memory_grants.py bash test.sh -> 12 passed, 0 failed
kodjima33
approved these changes
Jul 28, 2026
kodjima33
left a comment
Collaborator
There was a problem hiding this comment.
Dry-run-only backfill for missing Developer API key memory grants; clear root cause, test-covered.
undivisible
pushed a commit
to Archit-lal/omi
that referenced
this pull request
Jul 29, 2026
…Hardware#10744) Developer API keys created before the create-path grant seeding landed (8e03144, 2026-06-27 22:54:46 +0700) have no entry under `users/{uid}/memory_control/app_key_memory_grants`. The grant gate `authorize_memory_external_default_memory_read` fails closed on a missing grant, so those keys get a permanent 403 from GET /v1/dev/user/memories regardless of the scopes on the key, while /conversations and /action-items keep working — which reads to the developer as "the memories endpoint is broken". Nothing backfills them, so the state is unrecoverable without creating a new key. The script grants nothing new. It materialises the grant each key's own scopes already imply, making exactly the call the create path makes: grant_default_read = has_scope(scopes, Scopes.MEMORIES_READ) grant_write = has_scope(scopes, Scopes.MEMORIES_WRITE) if grant_default_read or grant_write: seed_developer_api_key_memory_grant(...) Scope resolution goes through `utils.scopes.has_scope`, the same authority the request path uses, rather than a literal membership test. That matters for one legacy shape the issue's filter would miss: `has_scope(None, ...)` resolves a key with no stored `scopes` field to the read-only set, which includes `memories:read`. Such a key can already read memories at the request layer, so it needs the grant too. Write is never inferred that way, since `memories:write` is not a read-only scope. Keys that already hold a grant for the same app/key are skipped, so re-running is safe. Dry run is the default and performs no writes. Modelled on scripts/backfill_mcp_key_full_access.py. This PR delivers the script and its dry-run mode only. It has not been run against production. Tests (tests/unit/test_backfill_developer_api_key_memory_grants.py, 12 cases) pin the two properties that make it safe to run: - dry run writes nothing at all - applying grants exactly the key's own capability: a read-scoped key gets default_read without write, a write-scoped key gets write without read, an absent scopes field resolves to read-only, and a key with no memory scope or created after seeding is left untouched plus an existing grant is never rewritten, a second key for the same user is still seeded after the per-uid grant cache is invalidated, an unusable created_at is skipped rather than guessed, a naive created_at is read as UTC instead of aborting the scan, --limit bounds the scan, and a key with no user_id is skipped. Verification (backend, aarch64, system python3.12 + pytest 9.0.3): BACKEND_UNIT_TEST_FILE_LIST=tests/unit/test_backfill_developer_api_key_memory_grants.py bash test.sh -> 12 passed, 0 failed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Developer API keys created before the create-path grant seeding landed (
8e03144a, 2026-06-27 22:54:46 +0700) have no entry underusers/{uid}/memory_control/app_key_memory_grants.authorize_memory_external_default_memory_readfails closed when no grant document is found, so those keys receive a permanent403fromGET /v1/dev/user/memoriesregardless of the scopes on the key:/v1/dev/user/conversationsand/v1/dev/user/action-itemsreturn200on the same key, so it reads to the developer as "the memories endpoint is broken" rather than "my key is missing a grant". Nothing backfills them, so the state is unrecoverable without creating a new key.This is a second, independent gate from the rollout gate fixed by #10094. The create-path defect itself is already fixed; what remains is the residual data.
What this PR delivers
The script and its dry-run mode only. It has not been run against production. Applying it touches roughly 1,417 accounts, which is the maintainer's call to make, not this PR's.
backend/scripts/backfill_developer_api_key_memory_grants.py, modelled on the existingbackfill_mcp_key_full_access.py:It walks
dev_api_keys, selects keys created before the seeding instant that carry a memories capability, and seeds the grant with exactly the call the create path makes:Dry run prints the same counts as an apply run without writing, so the cohort can be measured before anyone commits to a write.
Grants nothing new
The whole safety argument is that no key gains a capability it did not already have:
default_readcomes only frommemories:read,writeonly frommemories:write;archive_readis alwaysFalse.created_atcannot be read is skipped rather than guessed.One deviation from the issue's proposed filter
The issue says to select keys that "carry
memories:read/memories:write". Taken literally as a membership test, that misses a legacy shape. Scope resolution here goes throughutils.scopes.has_scope, the same authority the request path uses:READ_ONLY_SCOPESincludesScopes.MEMORIES_READ, so a key with no storedscopesfield can already read memories at the request layer — and therefore also 403s on this gate and needs the grant. A literal'memories:read' in scopestest would silently leave that population broken. Write is never inferred this way, becausememories:writeis not a read-only scope.That means the affected cohort may be slightly larger than the issue's measured 1,763 keys / 1,417 accounts, which counted explicit scopes. The dry run reports
absent_scopes_treated_as_read_onlyseparately so the difference is visible before applying.Tests
backend/tests/unit/test_backfill_developer_api_key_memory_grants.py— 12 cases against a path-keyed Firestore fake, pinning the two properties that make the script safe to run:default_readwithoutwrite; a write-scoped key getswritewithoutdefault_read; an absentscopesfield resolves to read-only; a key with no memory scope, or created after seeding, is left untouchedPlus: an existing grant is never rewritten (safe re-run), a second key for the same user is still seeded after the per-uid grant cache is invalidated by the first write, an unusable
created_atis skipped rather than guessed, a naivecreated_atis read as UTC instead of aborting the scan,--limitbounds the scan, and a key with nouser_idis skipped.Verification
Backend, aarch64, system
python3.12+pytest 9.0.3.Local runs used the
python3fallback documented intest.shbecause the pinned CPython 3.11.15 has nolinux-aarch64build, soscripts/sync-python-deps.shcannot create the pinned venv on this machine. CI on the pinned interpreter is the authoritative gate for this PR.No production run was performed, and the script was not executed against any live Firestore project. Its behavior is exercised only through the fake in the test above.
Closes #10734