Repository navigation
#110: Extract plan-tier framing into core/framing.py - #117
Conversation
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
left a comment
There was a problem hiding this comment.
Review: ✅ Approve with nits
Acceptance criteria — all met:
- ✓
core/framing.pyexportscompute_framing,render_dollar,render_savingswith the spec'd signatures - ✓ CLI byte-identical —
cmd_optimize/cmd_tokenmaxxuse the new module as a pure drop-in; helpers (including the #106 global-config fallback) are unchanged - ✓ All 4 endpoints emit the
framingblock with the exact field names from the spec - ✓ Tests cover api / subscription / local / unknown, mixed window, empty data, and both render helpers
- ✓ No changes to
models.py/semconv.py(pricing-mode derivation untouched) - ✓ 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_mixshape fromcmd_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.pyfollow-up worth filing. The PR correctly identifies thatcmd_cost.pyhad no framing logic to extract pre-PR (only a deferral comment atcmd_cost.py:151-156). Legitimate scope cut. But now thatcore/framing.pyexists,tj cost's_render_diffstill 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.
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]>
|
Thanks for the review! Both nits addressed:
CI should re-run on the new commit. |
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.pyFramingdataclass (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)andrender_savings(value_usd, value_tokens, framing).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.pyandcmd_tokenmaxx.pyimport the helpers from the sharedmodule and drop their local copies.
cmd_cost.pydid not apply framingbefore and still doesn't (its rendering is untouched).
API
/api/v1/cost,/api/v1/cost/compare,/api/v1/optimize,/api/v1/budgetnow return a top-level
framingblock. Callers that ignore it keep working.Tests
tests/unit/test_framing.py— one path perpricing_mode(api / subscription / local / unknown), mixed windows, empty data, and both
render helpers.
tests/integration/test_api.py— framing block present on the endpoints andreflects a configured subscription plan.
mypyandruffclean.Scope guardrails honoured
SessionRecord.plan_tier/pricing_modederivation.