Skip to content

feat(backend): add the Developer API key memory-grant backfill (dry-run only) - #10744

Merged
undivisible merged 1 commit into
mainfrom
fix/backfill-developer-api-memory-grants
Jul 29, 2026
Merged

undivisible merged 1 commit into
mainfrom
fix/backfill-developer-api-memory-grants

Conversation

@mdmohsin7

@mdmohsin7 mdmohsin7 commented Jul 27, 2026 •

Copy link
Copy Markdown
Member

Problem

Developer API keys created before the create-path grant seeding landed (8e03144a, 2026-06-27 22:54:46 +0700) have no entry under users/{uid}/memory_control/app_key_memory_grants. authorize_memory_external_default_memory_read fails closed when no grant document is found, so those keys receive a permanent 403 from GET /v1/dev/user/memories regardless of the scopes on the key:

{"detail":{"enabled":false,"reason":"missing_app_key_scope_grant","consumer":"developer_api"}}

/v1/dev/user/conversations and /v1/dev/user/action-items return 200 on 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 existing backfill_mcp_key_full_access.py:

python scripts/backfill_developer_api_key_memory_grants.py              # dry run (default)
python scripts/backfill_developer_api_key_memory_grants.py --limit 200  # bounded dry run
python scripts/backfill_developer_api_key_memory_grants.py --apply      # write grants

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:

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(uid, key_id, default_read=..., write=..., db_client=db)

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_read comes only from memories:read, write only from memories:write; archive_read is always False.
  • A key with neither memory scope is skipped entirely.
  • A key that already holds a grant for the same app/key is skipped, so re-running is safe.
  • A key whose created_at cannot 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 through utils.scopes.has_scope, the same authority the request path uses:

def has_scope(user_scopes, required_scope):
    """Check if user has required scope. None scopes are treated as read-only."""
    if user_scopes is None:
        return required_scope in READ_ONLY_SCOPES
    return required_scope in user_scopes

READ_ONLY_SCOPES includes Scopes.MEMORIES_READ, so a key with no stored scopes field can already read memories at the request layer — and therefore also 403s on this gate and needs the grant. A literal 'memories:read' in scopes test would silently leave that population broken. Write is never inferred this way, because memories:write is 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_only separately 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:

  • dry run writes nothing at all (asserted on the fake's write log, not just the printed counts)
  • applying grants exactly the key's own capability: a read-scoped key gets default_read without write; a write-scoped key gets write without default_read; an absent scopes field resolves to read-only; a key with no memory scope, or created after seeding, is left untouched

Plus: 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_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

Local runs used the python3 fallback documented in test.sh because the pinned CPython 3.11.15 has no linux-aarch64 build, so scripts/sync-python-deps.sh cannot 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

Review in cubic

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
@mdmohsin7 mdmohsin7 added bug Something isn't working backend Backend Task (python) labels Jul 27, 2026

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

Dry-run-only backfill for missing Developer API key memory grants; clear root cause, test-covered.

@undivisible
undivisible merged commit f0b4eec into main Jul 29, 2026
46 of 48 checks passed
@undivisible
undivisible deleted the fix/backfill-developer-api-memory-grants branch July 29, 2026 04:37
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Backend Task (python) bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Developer API keys created before 2026-06-27 have no memory grant and 403 permanently — no backfill exists

3 participants