Repository navigation
feat(cost): typed subscription plan config (#1132) - #1153
Conversation
Introduces a typed SubscriptionPlan model and a [[cost.subscription.plans]] TOML surface so per-cycle outlook, quota pressure, and overage detection (#995 / #870) have a single non-authoritative source of truth. - polylogue/cost/plans.py: SubscriptionPlan (frozen pydantic), QuotaBasis, OverageRule, PlanSource, PlanLookupError, cycle_for, load_plans, plan_by_name, resolve_plan, WELL_KNOWN_PLANS curated seed. - polylogue/config.py: subscription_plans property + [[cost.subscription.plans]] array-of-tables loader; user rows always override seed and carry source="user-config". - tests/unit/cost/test_plans.py: 17 cases covering seed coverage, user override beats seed, invalid row typed error, quota/overage coherence, currency normalization, cycle anchor roll-back, TOML round-trip. The curated seed (claude-pro/max-5x/max-20x, chatgpt-plus/pro, github-copilot-pro, gemini-advanced) is dated and tagged with a visible non-authoritative notice. No vendor pages scraped, no billing APIs called. Verification: - mypy polylogue/cost polylogue/config.py tests/unit/cost -> 0 errors - pytest tests/unit/cost/test_plans.py -q -> 17 passed - devtools verify --quick -> exit 0
|
Warning Rate limit exceeded
You’ve run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (7)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 57e3befe2b
ℹ️ 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".
| # Roll back one month. | ||
| prev_month_end = candidate.replace(day=1) - timedelta(days=1) | ||
| candidate = prev_month_end.replace(day=anchor, hour=0, minute=0, second=0, microsecond=0) | ||
| end = candidate + timedelta(days=plan.billing_cycle_days) |
There was a problem hiding this comment.
Compute cycle window so it always contains
now
cycle_for claims to return the cycle containing now, but end is derived as candidate + billing_cycle_days even though candidate is snapped to a calendar anchor day each month. For common billing_cycle_days=30 plans on 31-day months, this creates uncovered gaps (for example, an anchor on day 10 yields an end of June 9 while the next anchor is June 10), so dates in that gap are reported in a cycle that has already ended. This will misclassify per-cycle usage and overage status near month boundaries.
Useful? React with 👍 / 👎.
| if plan.cycle_anchor_day is None: | ||
| return None | ||
| anchor = plan.cycle_anchor_day | ||
| now_utc = now.astimezone(UTC) |
There was a problem hiding this comment.
Avoid local-time reinterpretation of naive
now
Using now.astimezone(UTC) on a naive datetime implicitly treats now as local system time, so the same input timestamp can resolve to different cycle windows across hosts/timezones. Since this function is used for deterministic quota/cycle accounting, naive inputs should be normalized explicitly (e.g., assume UTC) or rejected, rather than silently depending on machine locale.
Useful? React with 👍 / 👎.
Summary
Introduces a typed
SubscriptionPlanmodel and a[[cost.subscription.plans]]TOML surface so the cost cluster (#995 / #870, foundation for #1136-#1140) has
a single typed substrate for per-cycle outlook, quota pressure, and overage
detection. User-supplied plans always override the curated seed.
Problem
subscription_pricing.SUBSCRIPTION_TIERSandsubscription_models.SubscriptionPlanConfigwere thin: they modelled a small Claude Code credit catalogue with no
provider tagging, no cycle anchor math, no overage semantics, and no
user-config override path. Every downstream AC on #995 (per-cycle outlook,
quota pressure, overage detection) and #870 (subscription plan model,
quota forecasting, overage detection) was ungrounded without a typed plan
substrate.
Solution
New substrate package
polylogue/cost/:polylogue/cost/plans.pySubscriptionPlan(pydantic,frozen=True,extra="forbid"):name,provider,display_name,monthly_cost_usd,currency,billing_cycle_days,cycle_anchor_day(1-28 to avoid 29/30/31 edgecases), optional
quota/quota_basis,overage_rule,overage_rate_usd_per_unit,source,confidence,effective_date,notice.QuotaBasisenum:credits | tokens | messages | usd.OverageRuleenum:block | metered | soft | unknown.PlanLookupErrortyped error so unknown-plan resolution is loud, notsilently defaulted.
cycle_for(plan, now)returns the half-open[start, end)UTC ISO-8601instants, or
Nonewhen the plan declares no anchor (callers mustsurface "no cycle window" instead of fabricating one).
load_plans(rows)merges user rows over the curated seed and tagsthem
source="user-config".resolve_plan(name, *, user_rows=...)is the single resolver used bythe cost CLI.
WELL_KNOWN_PLANScurated seed: claude-pro, claude-max-5x,claude-max-20x, chatgpt-plus, chatgpt-pro, github-copilot-pro,
gemini-advanced. All tagged
source="polylogue-curated-seed"with a visible non-authoritative notice and
2026-05-17effectivedate. No vendor pages scraped, no billing APIs called.
polylogue/config.py:subscription_plansproperty exposes the raw rows from[[cost.subscription.plans]]. Validation lives at the cost-clusterboundary so malformed entries fail with a useful plan name rather
than silently dropping fields.
_merge_tomlparses the array-of-tables and stores it ascfg["subscription_plans"].tests/unit/cost/test_plans.py(17 cases): seed coverage, unknown planraises typed error, user override beats seed, invalid row typed error,
quota/basis coherence, overage-rate coherence, currency normalization,
missing-anchor cycle returns
None, anchor mid-month, anchor beforenowrolls back,resolve_plan(None)returnsNone,resolve_planuser-row plumbing, TOML round-trip via
load_polylogue_config,default-empty when no config file, frozen-plan mutation rejected.
Non-goals: scraping vendor pricing pages, real-time quota monitoring,
hard-coding current vendor prices into durable tests.
Verification
pytest tests/unit/cost/test_plans.py -q->17 passed in 7.7smypy polylogue/cost polylogue/config.py tests/unit/cost->Success: no issues found in 5 source filesdevtools verify --quick->exit_code: 0(16 steps green; stamp57e3befe)devtools verify-suppressions-> no new suppressions registered (theinitial
# type: ignore[arg-type]calls were eliminated by switchingto
SubscriptionPlan.model_validate(...))Closes #1132.