Skip to content

fix(#138): run-rate cycle honors [budget.<provider>] cycle_start_day - #158

Merged
anilmurty merged 1 commit into
mainfrom
fix/138-cycle-start-day
Jun 19, 2026
Merged

anilmurty merged 1 commit into
mainfrom
fix/138-cycle-start-day

Conversation

@anilmurty

Copy link
Copy Markdown
Contributor

Closes #138 (follow-up from PR #137 / #134 review).

Problem

The Overview/Cost run-rate caption ("~$X by end of June") projected to the calendar-month boundary via cycleRemaining() in the UI, ignoring a user's configured non-calendar billing cycle ([budget.<provider>] cycle_start_day). With cycle_start_day = 15 the framing was off by ~half a month — the budget-projection analyzer already honors the cycle, so this was an honesty-parity gap.

Fix

  • tokenjam/core/cycle.py (new) — cycle_bounds() extracted from the budget-projection analyzer (now its single home — the analyzer imports it, no duplicated cycle math). Plus effective_cycle_start_day(config) to resolve the global caption's cycle: honors a single agreed cycle_start_day across configured provider budgets; falls back to calendar month (start_day=1) when none are configured or providers conflict (we don't guess between competing cycles). Per-provider projections are unaffected.
  • /api/v1/cost now returns a cycle block {start, end, days_remaining, start_day} so the server-side cycle config reaches the UI (the issue's suggested plumbing).
  • cycleRemaining(cycle) reads those bounds. Calendar cycles still read "by end of June"; custom cycles read a dated form ("by Jul 15"). Falls back to calendar-month math if the response carries no cycle block.

Tests

  • tests/unit/test_cycle.py — bounds for default + explicit cycle_start_day, the 28-day clamp, December year-wrap; config resolution including the conflict fallback.
  • tests/integration/test_api.py — /api/v1/cost surfaces the cycle block (defaults to start_day=1) and honors a configured provider cycle_start_day=15.
  • tests/unit/test_lens_ui_regression.py — guards the new server-driven caption + both call sites.

Full CI set green locally (761 passed), mypy clean on changed files, UI passes node --check.

Built in an isolated git worktree off main.

The Overview/Cost run-rate caption projected to the calendar-month boundary
via cycleRemaining() in the UI, ignoring a user's configured non-calendar
billing cycle ([budget.<provider>] cycle_start_day). With cycle_start_day=15
the "by end of <month>" framing was off by ~half a month — undercutting the
honesty parity the budget-projection analyzer already holds.

- New tokenjam/core/cycle.py: cycle_bounds() (extracted from the
  budget-projection analyzer, now its single home — DRY) + a new
  effective_cycle_start_day(config) that resolves the global caption's cycle
  from configured provider budgets (honors a single agreed cycle_start_day;
  falls back to calendar month when none configured or providers conflict).
- /api/v1/cost now returns a `cycle` block {start, end, days_remaining,
  start_day} so the cycle config (server-side) reaches the UI.
- cycleRemaining(cycle) reads those server bounds; calendar cycles still read
  "by end of June", custom cycles read a dated form ("by Jul 15"). Falls back
  to calendar month when the response carries no cycle block.

Tests: tests/unit/test_cycle.py (bounds for default + explicit start_day,
clamp, year-wrap; config resolution incl. conflict fallback); API tests that
/api/v1/cost surfaces the cycle block and honors a provider cycle_start_day;
UI regression guards for the new server-driven caption.

Closes #138

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@anilmurty
anilmurty merged commit 3a76ecc into main Jun 19, 2026
4 checks passed
@anilmurty
anilmurty deleted the fix/138-cycle-start-day branch June 19, 2026 22:37
HoomanDgtl pushed a commit to HoomanDigital/tokenjam that referenced this pull request Jun 20, 2026
TokenJam v0.4.1 — concurrency + cycle-aware run-rate + Reuse HTTP fallback +
quality follow-ups across the Lens UI.

## What's in 0.4.1

Six issues closed, all branched off main as separate PRs that landed cleanly:

| Issue | PR | What |
|---|---|---|
| Metabuilder-Labs#138 | Metabuilder-Labs#158 | Run-rate honors `[budget.<provider>] cycle_start_day` (per-tier `cycle_bounds` extracted into `core/cycle.py`, shared with `budget_projection`) |
| Metabuilder-Labs#124 | Metabuilder-Labs#161 | Daemon DB concurrency: `DuckDBBackend.conn` is now a per-thread DuckDB cursor (`threading.local`) over one shared database. Cursors are independent connections safe for concurrent use, so the Overview fan-out + sync read routes in Starlette's threadpool no longer crash under load. |
| Metabuilder-Labs#139 | Metabuilder-Labs#163 | `buildCostSeries` coarsens up an `hour → day → week` ladder instead of silently emptying long windows (closed-form bucket count, never allocates an oversized array). Same PR reverts the Overview's sequential-fetch workaround to `Promise.all` with deliberately-asymmetric error handling (`/cost` no-catch; the other five panels carry per-call fallbacks). |
| Metabuilder-Labs#147 | Metabuilder-Labs#164 | `tj status` shows active (compute) time alongside wall-clock elapsed (`session_active_seconds` = Σ span durations). Both CLI and Lens Status tile rendered, plus `--json` carries both fields. `fmtDurLong` reads multi-day spans as `2d 3h` instead of `3087m`. |
| Metabuilder-Labs#154 | Metabuilder-Labs#165 | New `GET /api/v1/reuse/clusters` endpoint + `ApiBackend.fetch_reuse_clusters` lets `tj report --reuse` work when the daemon holds the DB lock. Dedicated endpoint (not bolted onto `/optimize`) so per-cluster planning text isn't paid for on every 30s Overview poll. |
| Metabuilder-Labs#162 | Metabuilder-Labs#166 | Recoverable Waste tile consistency: title-cased fallback for unknown analyzer names (`reuse` now renders as `Reuse`), em-dash dropped from the not-ready content line, regression guard locks all three states so the bold-title issue can't recur. |

## Pre-release verification

Walked the focused 10-step v0.4.1 checklist via a sub-agent (`tests/agent-pre-release-v0.4.1.md`). Result: **10/10 PASS, 0 FAIL, 0 UNCLEAR**. Full log committed at
`tests/results/agent-pre-release-v0.4.1-20260620T002546Z.md`.

Validated end-to-end against the live daemon:

- 90 concurrent reads against the Overview endpoint set: no SIGABRT, no 500s (was the Metabuilder-Labs#124 reproduction)
- `tj cost` cache columns show non-zero values with daemon up (was 0 pre-Metabuilder-Labs#153)
- `/api/v1/cost` returns a `cycle` block; UI consumes it
- `tj status` shows `active 12m 3s · elapsed 2d 3h`; JSON carries both fields
- `tj report --reuse` writes HTML + Markdown with the daemon running (was the Metabuilder-Labs#154 error)
- Recoverable Waste tile titles render uniformly and "Reuse" is title-cased

## What's in this commit

- `pyproject.toml`: `version = "0.4.1"`
- `sdk-ts/package.json`: `"version": "0.4.1"`
- `tests/agent-pre-release-v0.4.1.md`: the focused 10-step checklist used to validate this release
- `tests/results/agent-pre-release-v0.4.1-20260620T002546Z.md`: the agent-run log proving 10/10 PASS

Tests: 689 pass locally; mypy + ruff clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
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.

[follow-up] Run-rate cycle should honor [budget.<provider>] cycle_start_day, not the calendar month

1 participant