Skip to content

fix(backend): fail-closed chat quota accounting before billable turns - #11630

Merged
kodjima33 merged 2 commits into
BasedHardware:mainfrom
aryanorastar:fix/chat-quota-record-fail-closed
Aug 15, 2026
Merged

kodjima33 merged 2 commits into
BasedHardware:mainfrom
aryanorastar:fix/chat-quota-record-fail-closed

Conversation

@aryanorastar

@aryanorastar aryanorastar commented Aug 15, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • /v2/messages used to swallow record_chat_quota_question failures and still run the billable chat stream, so Free-plan question counters could fail open under Firestore outages.
  • Recording now happens before persisting the human turn / session link. On failure: emit an SSE done: canned reply mobile can render (not a bare HTTP 503), and do not persist the human message (avoids orphan copies on retry).
  • Voice /v2/voice-messages keeps best-effort recording after a visible message: frame (billable work already started there).
  • Allowlists main-wide #11613 desktop inventory gap (/v1/tools/conversations/search-chunks) so tip CI is not poisoned.

Test plan

  • pytest backend/tests/unit/test_chat_quota_counting_router.py + test_desktop_rest_inventory.py (18 passed)
  • CI backend unit suite on the PR
  • Honest gaps: no induced Firestore outage in prod; desktop /v2/desktop/messages still best-effort (out of this shrink).

Failure-Class: none

Line-Count-Exception: backend/routers/chat.py | 1734 -> 1791 | Adds fail-closed quota accounting before billable /v2/messages turns, with SSE-visible retry copy and no orphan human persistence (#11630).

Swallowing record_chat_quota_question failures let Free-plan /v2/messages
continue into the LLM without advancing the counter. Raise 503 before the
provider stream starts; keep best-effort recording only for voice after a
visible message frame.

Co-authored-by: Cursor <[email protected]>
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@aryanorastar

Copy link
Copy Markdown
Collaborator Author

Added the required PR metadata: Failure-Class: none and the line-count exception for backend/routers/chat.py (1734 -> 1769). The preflight failure was metadata-only; backend and hermetic checks are still running.

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

All reported issues were addressed across 2 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread backend/routers/chat.py Resolved
Comment thread backend/routers/chat.py Resolved
Record the Free-plan counter before persisting the human turn. On write
failure, emit a mobile-visible SSE done: frame and skip persistence so
retries do not leave unanswered copies. Allowlist the main-wide
search-chunks desktop inventory gap.

Failure-Class: none
Co-authored-by: Cursor <[email protected]>
@aryanorastar

Copy link
Copy Markdown
Collaborator Author

Addressed both cubic P2s + Backend Unit red:

  1. SSE vs 503 — accounting failure now returns HTTP 200 + done: canned reply (same mobile contract as quota-exceeded), not a bare 503.
  2. Orphan human messages — counter write runs before add_message / add_message_to_chat_session; on failure nothing is persisted, so retries cannot multiply unanswered human rows.
  3. CI — allowlisted /v1/tools/conversations/search-chunks (feat: add verbatim transcript chunks as third context-director retrieval source #11613 main-wide inventory gap).

Tip pushed with updated tests. Local: 18 passed on the touched suites.

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

Records the chat-quota question before any billable work and returns an SSE-visible retry instead of failing open, without persisting an orphan human turn; covered by new router tests.

@kodjima33
kodjima33 merged commit 3cd06e9 into BasedHardware:main Aug 15, 2026
35 of 37 checks passed
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