Skip to content

#110: Extract plan-tier framing into core/framing.py - #117

Merged
anilmurty merged 3 commits into
mainfrom
feature/110-framing-module
Jun 18, 2026
Merged

anilmurty merged 3 commits into
mainfrom
feature/110-framing-module

Conversation

@anilmurty

Copy link
Copy Markdown
Contributor

Closes #110.

Foundation for the Lens UI work: a single source of truth for plan-tier-aware
rendering, consumed by both the CLI and the REST API so the web UI can render
identical dollar/token figures without re-deriving the rules in JavaScript.

What changed

New module tokenjam/core/framing.py

  • Framing dataclass (pricing_mode, plan_tier, plan_label,
    plan_monthly_usd, subscription_share_pct, api_share_pct,
    display_rule, qualifier_text, plus window totals).
  • compute_framing(config, window_summary, by_provider_breakdown).
  • render_dollar(value, framing) and render_savings(value_usd, value_tokens, framing).
  • Shared helpers pricing_mode_for / dominant_plan / config_declared_plan
    (with the v0.3.5 pre-release playbook findings #106 global-config fallback) / plan_tier_mix.

CLI migration (output unchanged)

  • cmd_optimize.py and cmd_tokenmaxx.py import the helpers from the shared
    module and drop their local copies. cmd_cost.py did not apply framing
    before and still doesn't (its rendering is untouched).

API

  • /api/v1/cost, /api/v1/cost/compare, /api/v1/optimize, /api/v1/budget
    now return a top-level framing block. Callers that ignore it keep working.

Tests

  • tests/unit/test_framing.py — one path per pricing_mode
    (api / subscription / local / unknown), mixed windows, empty data, and both
    render helpers.
  • tests/integration/test_api.py — framing block present on the endpoints and
    reflects a configured subscription plan.
  • Full suite: 665 passed; mypy and ruff clean.

Scope guardrails honoured

  • No change to SessionRecord.plan_tier / pricing_mode derivation.
  • No new config field.
  • CLI behaviour unchanged.

Introduce tokenjam/core/framing.py as the single source of truth for
plan-tier-aware rendering rules (which dollar figures are shown verbatim,
suppressed in favour of token-share framing, or annotated with a qualifier).

- New Framing dataclass + compute_framing/render_dollar/render_savings, plus
  the shared helpers pricing_mode_for / dominant_plan / config_declared_plan
  (with the #106 global-config fallback) / plan_tier_mix.
- cmd_optimize and cmd_tokenmaxx now import these from the shared module and
  drop their local copies. CLI output is unchanged.
- /api/v1/cost, /cost/compare, /optimize, /budget responses now carry a
  top-level `framing` block so the local web UI can render identical numbers
  without re-deriving the rules in JS. Existing callers that ignore the field
  keep working.
- Unit tests for every pricing_mode (api/subscription/local/unknown), mixed
  windows, empty data, and render_dollar/render_savings. API tests assert the
  framing block is present and reflects a configured subscription plan.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

@anilmurty anilmurty left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Review: ✅ Approve with nits

Acceptance criteria — all met:

  1. ✓ core/framing.py exports compute_framing, render_dollar, render_savings with the spec'd signatures
  2. ✓ CLI byte-identical — cmd_optimize / cmd_tokenmaxx use the new module as a pure drop-in; helpers (including the #106 global-config fallback) are unchanged
  3. ✓ All 4 endpoints emit the framing block with the exact field names from the spec
  4. ✓ Tests cover api / subscription / local / unknown, mixed window, empty data, and both render helpers
  5. ✓ No changes to models.py / semconv.py (pricing-mode derivation untouched)
  6. ✓ No new config keys

Nits / discussion points:

  • F-string in SQL at tokenjam/core/framing.py:325 — f"SELECT … FROM sessions{where}". Values are properly parameterized via $N; only the WHERE skeleton is interpolated from a fixed set of hardcoded clauses, so behavior is correct and it matches the original _plan_tier_mix shape from cmd_optimize.py. Technically still touches CLAUDE.md Critical Rule 7 ("Parameterised SQL only — never use f-string SQL"). Minor — would be nicer as a constant lookup or two-branch conditional. Not a blocker.

  • cmd_cost.py follow-up worth filing. The PR correctly identifies that cmd_cost.py had no framing logic to extract pre-PR (only a deferral comment at cmd_cost.py:151-156). Legitimate scope cut. But now that core/framing.py exists, tj cost's _render_diff still renders raw dollars regardless of plan tier — subscription users see dollar figures they shouldn't. Worth a follow-up issue rather than expanding this one.

No blockers. Dependency rule clean (no cli/api imports from core), no datetime.now() usage, no debug prints or TODOs, module well-organized (constants → helpers → dataclasses → core function → renderers → DB helper).

CI green on 3.10 / 3.11 / 3.12 + TS. Good to merge.

anilmurty and others added 2 commits June 17, 2026 15:57
Build the plan_tier_mix query by concatenating static fragments instead of an
f-string; every value still binds through a $N parameter. Addresses the PR #117
review nit re: CLAUDE.md Critical Rule 7.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@anilmurty

Copy link
Copy Markdown
Contributor Author

Thanks for the review! Both nits addressed:

  1. F-string SQL (Rule 7) — fixed in 24c9795. plan_tier_mix now builds the query by concatenating static fragments; every value still binds through a $N parameter, nothing interpolated. Rebased onto the updated branch.
  2. cmd_cost framing gap — filed as [follow-up] Apply plan-tier framing to tj cost --compare (_render_diff) #120 (follow-up), keeping this PR a pure refactor as you suggested.

CI should re-run on the new commit.

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.

[feature] Extract plan-tier-aware rendering into core/framing.py

1 participant