Repository navigation
feat(firestore-reads): 120s action-item page reuse and record_pending head read (levers default off) - #24624
Conversation
…n-item page reuse Two read cuts on polled list endpoints, each behind its own default-off FIRESTORE_READ_LEVERS key: - held_audio_rows_cache: GET /v1/users/me/subscription re-ran the newest-holds query (~15 reads) on every poll. Reuse the page from Redis for 60s while the held-audio summary document is unchanged (every hold mutation rewrites the summary, so a change is a new key). Display-only; fail-open to Firestore. - action_items_ttl120: under action_items_page, the version-bumped page cache defaults to a 120s window instead of 30s. Every action-item write bumps the version, so the longer window only lengthens reuse of an unchanged list. ACTION_ITEMS_PAGE_CACHE_TTL_SECONDS and the 0 rollback still win. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… plus a capped count Lever lifecycle_record_pending_head (key = namespace, default off). The alarm sampler streamed up to 1,000 job documents per tick only to take the oldest row's age and a full-page flag. The ordered cohort's first row gives the same cohort_age, and a capped count() gives the same flag. Equivalence is tested against the legacy path across empty, 999/1000/6000-row and future-clock cohorts. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1a7de6696c
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| except Exception: | ||
| pass |
There was a problem hiding this comment.
Instrument the Redis failure paths before falling back
When held_audio_rows_cache is enabled and Redis reads or writes fail, both exception handlers silently discard the error. Subscription polling then resumes repeated Firestore page reads without any indication that the read-saving lever is ineffective, hiding the failure this rollout needs to detect. Call the shared record_fallback helper for these exceptional fail-open paths; ordinary cache misses need no fallback event. This follows the explicit new-fallback requirement in backend/AGENTS.md.
AGENTS.md reference: backend/AGENTS.md:L322-L322
Useful? React with 👍 / 👎.
|
|
||
| @lru_cache(maxsize=1) | ||
| def _rows_redis(): | ||
| from database import redis_db |
There was a problem hiding this comment.
Move the Redis module import to module scope
The new _rows_redis helper imports redis_db inside the function, contrary to the explicit requirement in backend/AGENTS.md that all imports be at module scope. Move this import alongside the other imports while keeping client construction inside the cached getter; lazy client initialization does not require a function-local module import.
AGENTS.md reference: backend/AGENTS.md:L166-L168
Useful? React with 👍 / 👎.
| """ | ||
| if not lever_enabled('held_audio_rows_cache', uid): | ||
| return _load_rows(uid) | ||
| key = f'{_ROWS_CACHE_PREFIX}:{uid}:{_summary_fingerprint(summary)}' |
There was a problem hiding this comment.
Key held-audio pages on a mutation revision rather than totals
With this lever enabled, recovering a hold and adding another hold of the same duration within the 60-second cache window reuses the old recording page. cap_hold.summary_after() returns only version, seconds, and count, with version fixed at schema version 2; _publish() likewise only increments the totals. Consequently, different sets of recordings can produce the same fingerprint even though both mutations update the summary. For example, replacing one 30-second hold with another returns the completed hold's ID and timestamp while omitting the newly held recording. Include a monotonically changing mutation revision or the summary snapshot's update time in the cache key so mutations cannot resurrect an earlier page.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
2 issues found across 7 files
Confidence score: 3/5
- In
lifecycle_metrics_queries.py,lever_enableduses the same namespace key for every pod, so a 5% rollout can become all-on or all-off across the fleet instead of a partial rollout. Use a pod-specific key. - In
test_held_audio_summary.py, the lever-off check cannot distinguish caching enabled from disabled because Redis fails on every call. Use a working Redis fake for the lever-off calls and assert the expected cache behavior.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="backend/tests/unit/test_held_audio_summary.py">
<violation number="1" location="backend/tests/unit/test_held_audio_summary.py:461">
P3: The lever-off check in this test cannot fail: `Down` makes Redis error on every call, so caching-enabled and caching-disabled both read Firestore each time. Use a working fake Redis for the lever-off calls and assert nothing is cached.</violation>
</file>
<file name="backend/database/lifecycle_metrics_queries.py">
<violation number="1" location="backend/database/lifecycle_metrics_queries.py:17">
P2: `lever_enabled` receives the constant `namespace` as its key, so every pod gets the same rollout bucket. At 5%, the lever is either fully on or fully off for the whole fleet, so the planned 5% stage cannot be a partial rollout. Key it per pod like `queue_age_sample_budget` and `finjob_candidate_queries` do, and update the `key=namespace` note in `KNOWN_LEVERS`.</violation>
</file>
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
| ) | ||
| rows = [doc.to_dict() for doc in query.stream(timeout=10, retry=None)] | ||
| cohort = _where(stock, 'enqueued_at', '>=', max(COHORT_START, now - COHORT_SECONDS)).order_by('enqueued_at') | ||
| if lever_enabled('lifecycle_record_pending_head', namespace): |
There was a problem hiding this comment.
P2: lever_enabled receives the constant namespace as its key, so every pod gets the same rollout bucket. At 5%, the lever is either fully on or fully off for the whole fleet, so the planned 5% stage cannot be a partial rollout. Key it per pod like queue_age_sample_budget and finjob_candidate_queries do, and update the key=namespace note in KNOWN_LEVERS.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At backend/database/lifecycle_metrics_queries.py, line 17:
<comment>`lever_enabled` receives the constant `namespace` as its key, so every pod gets the same rollout bucket. At 5%, the lever is either fully on or fully off for the whole fleet, so the planned 5% stage cannot be a partial rollout. Key it per pod like `queue_age_sample_budget` and `finjob_candidate_queries` do, and update the `key=namespace` note in `KNOWN_LEVERS`.</comment>
<file context>
@@ -11,16 +13,21 @@ def _where(query, field, op, value):
- )
- rows = [doc.to_dict() for doc in query.stream(timeout=10, retry=None)]
+ cohort = _where(stock, 'enqueued_at', '>=', max(COHORT_START, now - COHORT_SECONDS)).order_by('enqueued_at')
+ if lever_enabled('lifecycle_record_pending_head', namespace):
+ # The page is ordered oldest first from the cohort's lower bound, so its
+ # first row alone determines cohort_age. A capped count reproduces the
</file context>
| monkeypatch.setenv('FIRESTORE_READ_LEVERS', '{}') | ||
| summary._load_rows_cached('synthetic', {'count': 1}) | ||
| assert len(loads) == 3 |
There was a problem hiding this comment.
P3: The lever-off check in this test cannot fail: Down makes Redis error on every call, so caching-enabled and caching-disabled both read Firestore each time. Use a working fake Redis for the lever-off calls and assert nothing is cached.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At backend/tests/unit/test_held_audio_summary.py, line 461:
<comment>The lever-off check in this test cannot fail: `Down` makes Redis error on every call, so caching-enabled and caching-disabled both read Firestore each time. Use a working fake Redis for the lever-off calls and assert nothing is cached.</comment>
<file context>
@@ -407,3 +407,57 @@ def test_held_summary_is_present_with_legacy_dispatch_alone(monkeypatch):
+ monkeypatch.setenv('FIRESTORE_READ_LEVERS', '{"held_audio_rows_cache":100}')
+ summary._load_rows_cached('synthetic', {'count': 1})
+ summary._load_rows_cached('synthetic', {'count': 1})
+ monkeypatch.setenv('FIRESTORE_READ_LEVERS', '{}')
+ summary._load_rows_cached('synthetic', {'count': 1})
+ assert len(loads) == 3
</file context>
| monkeypatch.setenv('FIRESTORE_READ_LEVERS', '{}') | |
| summary._load_rows_cached('synthetic', {'count': 1}) | |
| assert len(loads) == 3 | |
| monkeypatch.setenv('FIRESTORE_READ_LEVERS', '{}') | |
| fake = _FakeRowsRedis() | |
| monkeypatch.setattr(summary, '_rows_redis', lambda: fake) | |
| summary._load_rows_cached('synthetic', {'count': 1}) | |
| summary._load_rows_cached('synthetic', {'count': 1}) | |
| assert fake.values == {} and len(loads) == 4 |
…page TTL; drop held-audio cache Luna r1 on #24624: - P2: prod deploys ACTION_ITEMS_PAGE_CACHE_TTL_SECONDS=30, which overrode the lever's 120s default. The lever now raises any positive TTL to at least 120s; 0 still disables. A test pins the deployed-30 case. - P2: the held-audio summary has no monotonic writer generation (version, seconds and count can repeat after a complete-then-hold), so a key on it can show a finished recording for up to 60s. held_audio_rows_cache is removed until the summary carries a generation. Failure-Class: none Co-Authored-By: Claude Opus 5.5 <[email protected]>
…; memory_list_index_window 25 (#24639) ## What Config only, one batched lever change: | Key | Change | Why | |---|---|---| | `FSR_SUBSCRIPTION_TTL_SECONDS` | 60 -> 55 (all services) | cl2-replay's verified privacy replay (1,049 faulted transitions, 2026-11-04 20:12Z) found that, with both invalidations lost, the Redis `EX 60` subscription/BYOK entry is still served at exactly +60.000s: 37 mismatches at 0s margin. 55s gives 0 with 5s margin. | | `action_items_ttl120` | 0 -> 5 | Ordinary lever from #24624: the version-bumped action-item page cache reuses pages for at least 120s instead of the deployed 30s. | | `memory_list_index_window` | 0 -> 25 | Back to the cohort that soaked 48h clean (#23536). The #24596/#24601 revert objected to the 0->100 jump, not the lever. | | `backend/docs/runbooks/external-list-reads.md` | wording | States the live 5% polling-cache cohort from #24623 (Luna r1 minor on #24623). | Luna r1 removed `lifecycle_record_pending_head` from this change: it cohorts on a single namespace key, so 5% would be off on every replica. A follow-up keys it per replica. This also satisfies lead2's enforce requirement for #24622 (`1104-b059ba`): the subscription TTL becomes 55s. The new privacy caches already use a strict `< 60.000s` bound. ## Rollout David's 2026-11-04 policy for ordinary read levers: 5% for 1-2h with the firestore-reads 5xx/latency guards, then 100% the same day in a follow-up. Rollback: set any key back to its previous value. ## Verification - `pytest tests/unit/test_render_backend_runtime_env.py tests/unit/test_verify_pusher_config_references.py tests/unit/test_firestore_read_levers.py tests/unit/test_backend_runtime_env_validator.py`: 294 passed. - `pytest tests/unit/test_fsr_s4_caches.py`: passes (TTL ceiling 60 unchanged; 55 is within it). - Local push gate ran with `BACKEND_UNIT_FILE_BUDGET_SECONDS=900` (host load ~50); CI keeps its own 300s budget. ## Product invariants affected - INV-MEM-4 (path match on `backend/deploy/runtime_env.yaml` only; no memory promotion behavior changes) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
…ng replica (#24640) ## What `lifecycle_record_pending_head` (from #24624) now cohorts per serving replica: `HOSTNAME`, or `K_REVISION:INSTANCE_ID`, the same identity as `fsr_read_cache.serving_config_key`. It falls back to the namespace. The logic is inlined so the module's import closure doesn't grow. The lever stays off; no config change here. ## Why Luna r1 P2 on #24639: the lever keyed on the fixed namespace `batch_pipeline_prod` (bucket 18), so any percentage switched the whole fleet together, and 5% was off everywhere with no canary. ## Verification - `pytest tests/unit/test_lifecycle_alarm_metrics.py tests/unit/test_firestore_read_levers.py`: pass. A new test shows a 50% lever splits 40 synthetic replica hostnames into both cohorts and falls back to the namespace without replica env. ## Product invariants affected None (`scripts/pr-preflight --suggest`). 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated description by cubic. --> <a href="https://cubic.dev/pr/BasedHardware/omi/pull/24640?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="View guided diff" src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a> <a href="https://www.cubic.dev/action/auto-fix/pr/BasedHardware/omi/24640?returnTo=https%3A%2F%2Fgithub.com%2FBasedHardware%2Fomi%2Fpull%2F24640&source=description" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"><img alt="Turn on auto-fix" src="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"></picture></a> <!-- End of auto-generated description by cubic. -->
…ing_head 5, memory_list_index_window 0 -> 25 Ordinary read levers under David's 2026-11-04 policy (5% for 1-2h, then 100): - action_items_ttl120 / lifecycle_record_pending_head (code BasedHardware#24624) start at 5%. - memory_list_index_window returns to 25, where it soaked 48h clean before the BasedHardware#24596/BasedHardware#24601 revert; that revert objected to the 0->100 jump, not the lever. Rollback: set any key back to 0. Co-Authored-By: Claude Opus 5.5 <[email protected]>
What
Two Firestore read cuts. Each sits behind its own default-off
FIRESTORE_READ_LEVERSkey, so nothing changes until the levers are configured:action_items_ttl120database.action_items:_stream_action_items_bounded~0.67M reads/haction_items_page, the version-bumped page cache reuses pages for at least 120s instead of the deployed 30s (ACTION_ITEMS_PAGE_CACHE_TTL_SECONDS=30in prod). Every action-item write bumps the version, so this only lengthens reuse of an unchanged list. Larger overrides still win, and 0 (ACTION_ITEMS_LIST_CACHE_TTL_SECONDS=0or the page override) still disables.lifecycle_record_pending_headdatabase.lifecycle_metrics_queries:record_pending~0.17M reads/h (backend-integration)count(), with identical output.Why
These are David's 2026-11-04 12-hour Firestore read savings (cl2-infracost). Scope:
/Volumes/Ephemeral/scratch/cl2-infracost/scopes/lists.mdandbatchstore.md(R1).Verification
pytest tests/unit/test_held_audio_summary.py tests/unit/test_fsr_s5_action_items.py tests/unit/test_firestore_read_levers.py tests/unit/test_user_subscription_wire_contract.py tests/unit/test_dev_action_items_read_cost.py: 152 passed.pytest tests/unit/test_lifecycle_alarm_metrics.py tests/unit/test_firestore_read_levers.py: 39 passed. A new parametrized equivalence test compares the lever against the legacy path for empty, 999, 1000 and 6000-row cohorts, old plus future clocks, and one document read.Luna r1 removed
held_audio_rows_cache: the held-audio summary has no monotonic writer generation, so a summary-keyed cache could show a just-finished recording for up to 60s.Rollout
Ordinary read levers under David's 2026-11-04 policy: 5% for 1-2h with the 5xx/latency guards, then 100%. The config ships in cl2-infracost's batched lever PR. Rollback: set the lever to 0.
BACKEND_UNIT_FILE_BUDGET_SECONDS=900: at host load ~50,test_firestore_query_shapes.pyhit the default 300s wall twice (fixture setup alone took 202s; it passes 544/544 when run directly). CI keeps its own 300s budget.Product invariants affected
None (
scripts/pr-preflight --suggest: no locked invariant path globs matched).Failure class (fixes)
Failure-Class: none
The
fix:commits repair review findings inside this PR's own new code before merge, not a shipped failure class.🤖 Generated with Claude Code