feat(quota): cost-based monthly quota — per-allocation USD spend cap (Phase 33)#89
Merged
Conversation
…(Phase 33) Closes the governance gap where non-token endpoints (OCR/image/TTS/realtime/…) bypassed the token-only monthly quota entirely. Adds an optional per-allocation monthly USD cap that governs EVERY endpoint via the cost common denominator. - model: allocations.quota_cost_usd_per_month (migration 0020, additive nullable); CallOutcome.rejected_cost_quota_exceeded (VARCHAR enum, no migration). - quota service: current_month_cost (sum cost_usd, hits the existing index) + is_over_cost_quota; mirrors the token quota helpers. Unpriced (cost_usd NULL) calls coalesce to 0 → not counted, not blocked (honest; admin must price them). - preflight: cost check alongside the token check — either cap trips. Rejected calls are recorded with the new outcome (attributed to the allocation). - realtime: in-connection cost watch extends the revocation watcher — committed month cost + this connection's in-flight cost ≥ cap → close + bill accrued time (any close path bills, FR-004). New close_reason "cost_exceeded". - admin: create/patch accept quota_cost_usd_per_month (>=0, null clears); patch audits allocation_cost_quota_updated (FR-008). Frontend quota dialog gains the cost-cap field. - members: /me/allocations exposes quota_cost_usd_per_month + cost_used_this_month; the allocation card shows "本月花費 / 上限" with a near-cap warning. - adaptive pool isolation: the rebalance pool only touches token quota, so the cost cap is untouched —固化 by an integration test (SC-005). Fix: the 0015 migration-replay test seeded an allocation via the HEAD-schema ORM; switched to raw SQL so a later column (0020) doesn't break it (experience lesson). Full suite 759 passed (zero regression); ruff+mypy clean; frontend tsc + 164 vitest + build green. Existing quota contract tests untouched (SC-003). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- test_me_allocations: add the US3 /me/allocations cost-cap+spend assertion (the original append was lost to a tool glitch; the serialization was already shipped). - test_credential_migration: remove ORM imports left unused after switching the 0014-era seed to raw SQL (CI ruff caught them — re-run ruff after edits). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
階段 33(046):成本制配額(跨端點統一額度上限)
補上「非 token 端點(OCR/圖片/語音/即時字幕…)繞過 token 月配額」的治理缺口(原則 1)。每分配新增選填每月花費上限(USD),以花費為跨單位共同分母治理所有端點。
做了什麼
allocations.quota_cost_usd_per_month(migration0020純加 nullable 欄);CallOutcome.rejected_cost_quota_exceeded(VARCHAR enum、無 migration)。current_month_cost(sum(cost_usd)、命中既有索引)+is_over_cost_quota,對稱既有 token helper。未定價(cost_usdNULL)coalesce 0 → 不計入、不被擋(誠實)。committed 月花費 + 本連線 in-flight≥ cap → 主動 close + 已累計時長落帳(任何 close 都落帳)。>=0、null 清除);patch 寫allocation_cost_quota_updated稽核(FR-008);前端配額 Dialog 加欄。/me/allocations回上限 + 本月花費;分配卡顯示「本月花費 / 上限」+ 接近上限提示。測試
部署
有 migration →
--set migrationJob.enabled=true;驗alembic current = 0020。🤖 Generated with Claude Code