Skip to content

fix(backend): write proactive-noti in-process rate-limit key under app.id - #8313

Closed
ZachL111 wants to merge 2 commits into
BasedHardware:mainfrom
ZachL111:fix/proactive-noti-key-mismatch
Closed

ZachL111 wants to merge 2 commits into
BasedHardware:mainfrom
ZachL111:fix/proactive-noti-key-mismatch

Conversation

@ZachL111

@ZachL111 ZachL111 commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The proactive notification rate limiter has two layers: an in-process cache (mem_db) and Redis. _set_proactive_noti_sent_at in backend/utils/app_integrations.py records the last-sent timestamp in both. It writes the Redis entry keyed by app.id but writes the in-process entry keyed by the whole App object, so the two layers store the same value under different keys. The reader, _hit_proactive_notification_rate_limits, looks up the in-process entry by app.id, which never matches what was written, so the in-process limiter is silently disabled and only the Redis layer enforces the limit. This PR keys the in-process write by app.id so both layers agree and the reader hits.

Root cause

In backend/utils/app_integrations.py, _set_proactive_noti_sent_at writes the in-process timestamp with the App object as the key instead of app.id:

def _set_proactive_noti_sent_at(uid: str, app: App):
    ts = time.time()
    mem_db.set_proactive_noti_sent_at(uid, app, int(ts), ttl=PROACTIVE_NOTI_LIMIT_SECONDS)
    redis_db.set_proactive_noti_sent_at(uid, app.id, int(ts), ttl=PROACTIVE_NOTI_LIMIT_SECONDS)

The Redis write on the next line already uses app.id, and the reader keys its in-process lookup by app.id as well:

def _hit_proactive_notification_rate_limits(uid: str, app: App):
    sent_at = mem_db.get_proactive_noti_sent_at(uid, app.id)
    ...
    if ttl > 0:
        mem_db.set_proactive_noti_sent_at(uid, app.id, int(time.time() + ttl), ttl=ttl)

Because the write uses the App object and the read uses app.id, the in-process key never lines up. The lookup returns None, so the in-process check is skipped and the limiter falls through to Redis on every call. No exception is raised; the result is just wrong. The in-process layer exists to absorb load before hitting Redis, and it stops doing that.

Fix

Pass app.id to the in-process write so it matches the Redis write and the reader:

def _set_proactive_noti_sent_at(uid: str, app: App):
    ts = time.time()
    mem_db.set_proactive_noti_sent_at(uid, app.id, int(ts), ttl=PROACTIVE_NOTI_LIMIT_SECONDS)
    redis_db.set_proactive_noti_sent_at(uid, app.id, int(ts), ttl=PROACTIVE_NOTI_LIMIT_SECONDS)

One argument changes, from app to app.id. Behavior for valid input is unchanged except that the in-process layer now records under the key the reader uses, so it starts matching as intended.

Testing

Added backend/tests/unit/test_proactive_noti_key_mismatch.py, registered in backend/test.sh. The module under test (utils.app_integrations) has a heavy import graph, so the test installs a meta-path stub finder for the heavy dependencies and excludes the target module so it loads from disk, then patches mem_db and redis_db on the imported module to capture the exact key each layer writes under. One case backs the in-process store with a real dict keyed by f'{uid}:{key}' and asserts that a read keyed by app.id hits what the write stored, and that the positional key handed to mem_db.set_proactive_noti_sent_at is app.id and not the App object. A second case asserts the mem_db and redis_db write keys are equal and both equal app.id. Verified red to green: the test fails on current main and passes with this change.

PR status check

No open PR changes this logic. The app.id form on this in-process write does not appear anywhere in the history of backend/utils/app_integrations.py, so this is not a previously reverted fix being resubmitted; the only app.id change in the file's past was a plugin.id to app.id rename inside _hit_proactive_notification_rate_limits, not this write. No filed GitHub issue matches.

Review in cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

No issues found across 3 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Tip: cubic could auto-approve low-risk PRs like this, if it thinks it's safe to merge. Learn more

Re-trigger cubic

@Git-on-my-level

Copy link
Copy Markdown
Member

Hi @ZachL111 — thank you for finding and writing up this retrieval/output semantic correctness issue. We consolidated this group into a smaller aggregate PR with shared semantic models/helpers and focused tests: #8366.

Your original discovery, reproduction details, and candidate fix are credited in the new PR body and commit metadata. I’m closing this individual PR as superseded by #8366, not because the issue is invalid. Thanks again for surfacing it.

@Git-on-my-level

Copy link
Copy Markdown
Member

Superseded by #8366. Thanks again @ZachL111 — credited there for the original discovery and candidate fix.

@github-actions

Copy link
Copy Markdown
Contributor

Hey @ZachL111 👋

Thank you so much for taking the time to contribute to Omi! We truly appreciate you putting in the effort to submit this pull request.

After careful review, we've decided not to merge this particular PR. Please don't take this personally — we genuinely try to merge as many contributions as possible, but sometimes we have to make tough calls based on:

  • Project standards — Ensuring consistency across the codebase
  • User needs — Making sure changes align with what our users need
  • Code best practices — Maintaining code quality and maintainability
  • Project direction — Keeping aligned with our roadmap and vision

Your contribution is still valuable to us, and we'd love to see you contribute again in the future! If you'd like feedback on how to improve this PR or want to discuss alternative approaches, please don't hesitate to reach out.

Thank you for being part of the Omi community! 💜

Git-on-my-level added a commit that referenced this pull request Jun 26, 2026
## Summary

Supersedes #8290, #8291, #8306, #8310, #8311, #8313, #8319, #8320.

Original issue discovery, reproductions, and candidate fixes by
@ZachL111.

This aggregates the retrieval/output semantic correctness fixes into one
focused backend PR:

- adds shared conversation metadata keys/model and uses them for vector
metadata production + reranking
- fixes metadata entity extraction so entities no longer copy topics
- refactors RAG chunking through a pure `build_conversation_context(...)
-> str | None` path so short conversations are included consistently
- separates calendar deletion successes from failures before building
user-visible output
- normalizes merge/photo timestamps through a UTC-aware helper and keeps
malformed/missing photo dates with a structured warning
- makes proactive-notification cache setters keyword-only for `app_id`
to prevent passing an `App` object as the cache key
- validates daily-summary LLM JSON with a Pydantic payload, falls back
only on JSON/model-validation failures, and stops logging raw LLM output
- safely handles Google contacts with absent names while preserving
email extraction

## Included Zach PRs

Included/redesigned in this aggregate: #8290, #8291, #8306, #8310,
#8311, #8313, #8319, #8320.

Deferred from this aggregate: none.

## Tests

- `cd backend && ./scripts/sync-python-deps.sh`
- `cd backend && source .venv/bin/activate && pytest
tests/unit/test_retrieval_semantics.py
tests/unit/test_daily_summary_regenerate.py
tests/unit/test_daily_summary_race_condition.py
tests/unit/test_conversations_to_string.py`
- `cd backend && source .venv/bin/activate && python -m py_compile
models/conversation_metadata.py models/calendar_mutation.py
models/daily_summary_payload.py utils/conversations/datetime_utils.py
utils/conversations/merge_conversations.py utils/retrieval/rag.py
utils/llm/chat.py utils/llm/external_integrations.py
utils/retrieval/tools/calendar_tools.py database/mem_db.py
database/redis_db.py database/vector_db.py
tests/unit/test_retrieval_semantics.py`
- `cd backend && bash testing/e2e/run.sh` — 73 passed, 6 skipped

Also ran `python backend/scripts/scan_async_blockers.py` from repo root;
it exits 0 and reports existing async-blocker findings unrelated to this
PR. Running it from `backend/` currently fails with `Error:
backend/routers not found`, so root invocation was used.


<!-- This is an auto-generated description by cubic. -->
<a
href="https://cubic.dev/pr/BasedHardware/omi/pull/8366?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="Review in cubic"
src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->
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