Skip to content

feat(cost): typed subscription plan config (#1132) - #1153

Merged
Sinity merged 1 commit into
masterfrom
feature/feat/typed-subscription-plans
May 17, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/feat/typed-subscription-plans

Conversation

@Sinity

@Sinity Sinity commented May 17, 2026

Copy link
Copy Markdown
Owner

Summary

Introduces a typed SubscriptionPlan model 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_TIERS and subscription_models.SubscriptionPlanConfig
were 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.py
    • SubscriptionPlan (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 edge
      cases), optional quota/quota_basis, overage_rule,
      overage_rate_usd_per_unit, source, confidence,
      effective_date, notice.
    • QuotaBasis enum: credits | tokens | messages | usd.
    • OverageRule enum: block | metered | soft | unknown.
    • PlanLookupError typed error so unknown-plan resolution is loud, not
      silently defaulted.
    • cycle_for(plan, now) returns the half-open [start, end) UTC ISO-8601
      instants, or None when the plan declares no anchor (callers must
      surface "no cycle window" instead of fabricating one).
    • load_plans(rows) merges user rows over the curated seed and tags
      them source="user-config".
    • resolve_plan(name, *, user_rows=...) is the single resolver used by
      the cost CLI.
    • WELL_KNOWN_PLANS curated 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-17 effective
      date. No vendor pages scraped, no billing APIs called.

polylogue/config.py:

  • New subscription_plans property exposes the raw rows from
    [[cost.subscription.plans]]. Validation lives at the cost-cluster
    boundary so malformed entries fail with a useful plan name rather
    than silently dropping fields.
  • _merge_toml parses the array-of-tables and stores it as
    cfg["subscription_plans"].

tests/unit/cost/test_plans.py (17 cases): seed coverage, unknown plan
raises 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 before
now rolls back, resolve_plan(None) returns None, resolve_plan
user-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.7s
  • mypy polylogue/cost polylogue/config.py tests/unit/cost -> Success: no issues found in 5 source files
  • devtools verify --quick -> exit_code: 0 (16 steps green; stamp 57e3befe)
  • devtools verify-suppressions -> no new suppressions registered (the
    initial # type: ignore[arg-type] calls were eliminated by switching
    to SubscriptionPlan.model_validate(...))

Closes #1132.

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

coderabbitai Bot commented May 17, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@Sinity has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 57 minutes and 4 seconds before requesting another review.

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 @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 2d38bc2a-c980-427b-8870-4f5c8f356343

📥 Commits

Reviewing files that changed from the base of the PR and between 1fe7487 and 57e3bef.

📒 Files selected for processing (7)
  • docs/plans/topology-target.yaml
  • docs/topology-status.md
  • polylogue/config.py
  • polylogue/cost/__init__.py
  • polylogue/cost/plans.py
  • tests/unit/cost/__init__.py
  • tests/unit/cost/test_plans.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/feat/typed-subscription-plans

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Sinity
Sinity merged commit b6e3a27 into master May 17, 2026
3 checks passed
@Sinity
Sinity deleted the feature/feat/typed-subscription-plans branch May 17, 2026 04:37

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 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".

Comment thread polylogue/cost/plans.py
# 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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

Comment thread polylogue/cost/plans.py
if plan.cycle_anchor_day is None:
return None
anchor = plan.cycle_anchor_day
now_utc = now.astimezone(UTC)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

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.

feat(cost): typed subscription plan config (cycle, quota, overage rules)

1 participant