Skip to content

Give the economy progress bars one shared model that keeps each run's start time and highest value - #1610

Merged
anth-volk merged 7 commits into
mainfrom
economy-progress-model
Nov 27, 2026
Merged

anth-volk merged 7 commits into
mainfrom
economy-progress-model

Conversation

@anth-volk

Copy link
Copy Markdown
Collaborator

Summary

The economy progress bars of a report's own year, its other budget-window years and bill reports now compute their value with one shared function. It uses the same clock curve (85% at the typical time, then easing toward 95%) as UK local areas and state district runs. Each run's query cache entry keeps the run's start time and the highest value its bar reached at a poll.

This is task 1.7 · 2 of the "Report runtimes" project. It is the app-side groundwork for the later task that moves the bars by the simulation API's measured stage durations. The function takes its duration estimate as a separate object ({ expectedMs }), so that task can add the server's stage durations.

What changes for the reader

  • Another year computing: its bar follows the clock from its first request. At half a typical US nationwide run it shows about 43%; on main it shows the stage's starting percent, 5%.
  • Several years computing: each year shows its own progress. On main all years shared one bar, so 2027 could show 2028's value.
  • Leaving a year and returning: its bar no longer drops. A US nationwide year held at 24% while queued stays at 24% after its run moves to loading data (3–4%). On main it showed 4%.
  • Leaving the page and returning: years and bills keep their start time for the session. On main their clock restarted.
  • The report's own year: it measures from the report's saved request time, also after a reload. Its value comes from the cached status, not from a synthetic timer that restarted on each mount.

How

  • The shared functions, in app/src/libs/calculations/runProgress.ts:
    • runProgressAt(state, now, estimate): the clock since the run's first request, held inside its current stage, and never below the highest value reached at a poll. For a given state it never decreases as time passes.
    • highestProgressAtPoll(...): the value the strategy keeps at each poll. It also counts the bar at that moment by the previous poll's stage, so a bar that moved between polls is never above the cached value. The previous state counts only for the same run, meaning the same start time.
    • economyRunStartedAt(cachedStatus): a computing run keeps its start time. A new run, or a retry after a failure, starts now.
    • clockProgress and runProgressValue move here from RunProgress.tsx, unchanged.
  • SocietyWideCalcStrategy: its progress comes from highestProgressAtPoll and the previous cached status, in place of the straight line capped at 95%.
  • calculationQueries.forEconomyRun:
    • the metadata function is called with the query's cached status;
    • gcTime: Infinity, as the UK local-area and state district queries already use, so a run's state outlasts a visit to another page.
  • The year and bill hooks:
    • useReportYearEconomies and useReportYearEconomy now share one query builder, and both return startedAt;
    • useBillEconomy keeps its start time in the cache instead of a useMemo, and returns it.
  • useCalculationStatus: the synthetic timer runs for household calculations only. Economy runs return their cached progress and message.
  • The report's own year: CalcStartConfig.startedAt carries the report's saved request time into CalcOrchestrator. A time ahead of the browser's clock starts now.
  • RunProgress:
    • with a start time, its value is runProgressAt, so a run's cached highest value is no longer limited to its current stage's range;
    • without one, the bar behaves as before (the budget-window card, household bars).
  • The pages:
    • each other year's bar gets key={year} and its start time;
    • the own year's bar reads its start time from the cached status;
    • the bill bar gets its start time.
  • pollRun: it computes its progress with runProgressAt, with the same results as before.

Intended behaviour changes outside the three bars

  • The older report-output page (SocietyWideReportOutput) reads the strategy's cached progress, so it now follows the clock curve: 85% at the typical time instead of 95% at 0.95 of it.

Known limit

  • Affected: bills only.
  • When: a bill's bar appears when "Load full results" is clicked, before the run's first request, because the reform policy is created first.
  • Effect: the bar counts from when it appeared, as on main, so its elapsed time never steps back. A bar shown again for the same run, after a tab switch, is lower by the time spent before the request: about 2 points for 5 seconds in a 190-second state run.
  • Why it stays: the review offered a fix (show a spinner until the run's first status arrives), and it was not taken.

Out of scope

  • The saved-report flow (useV2ReportYearEconomies, V2EconomyReportPage) is not touched. It can reuse runProgress.ts.

Commits

  • d8a04d427 Add failing tests for one shared economy progress model. The new function's tests, a page test for the other years' bars and the own year's start time, and updated strategy, query, hook, RunProgress, orchestrator and bill page tests.
  • 155cc9015 Expect an economy run's cached progress from the status hook. One existing test asserted the economy synthetic timer's value.
  • 1b571532d Give the economy progress bars one shared model in the query cache. The change itself.
  • 7ab901928 Add failing tests for review round 1: cache lifetime and late first request. From the first review round.
  • 9ad6786b2 Keep economy runs in the cache for the session; measure from a late first request. The first part stays. The second part is reverted by 500c21306.
  • 720ded267 Expect a bar's elapsed time never to step back when its run starts late. Replaces the round 1 test for the reverted part.
  • 500c21306 Count a late-starting run's bar from when it appeared, as on main. The revert.

Each test was committed and run failing before the change that makes it pass. Two rounds of adversarial review ran on the diff. Round 1 found the 5-minute cache lifetime and a bill's late first request. Round 2 found no severe bugs; it confirmed the queued-at-24% case end to end and found the bill limit above.

Test plan

  • make typecheck
  • make lint
  • New test files, from app/:
    • bun run vitest -- src/tests/unit/libs/calculations/runProgress.test.ts
    • bun run vitest -- src/tests/unit/pages/flagship/ReportYearProgress.test.tsx
  • Modified test files, from app/, each run on its own:
    • src/tests/unit/libs/calculations/strategies/SocietyWideCalcStrategy.test.ts
    • src/tests/unit/libs/queries/calculationQueries.test.ts
    • src/tests/unit/hooks/useReportYearEconomy.test.tsx
    • src/tests/unit/hooks/useBillEconomy.test.tsx
    • src/tests/unit/hooks/useCalculationStatus.test.tsx
    • src/tests/unit/components/flagship/report/RunProgress.test.tsx
    • src/tests/unit/libs/calculations/CalcOrchestrator.test.ts
    • src/tests/unit/pages/flagship/BillReport.test.tsx
    • src/tests/unit/libs/calculations/polledRun.test.ts (import path only)
    • src/tests/unit/hooks/useStateDistrictRuns.test.tsx (import path only)
  • Manual check:
    1. Open a US nationwide report with a budget window and request two other years.
    2. While they compute, switch between them: each bar should advance and show its own value.
    3. Leave a year and return: its bar should not drop.

🤖 Generated with Claude Code

anth-volk and others added 7 commits November 28, 2026 00:12
Tests for a pure function that computes an economy run's bar from its
cached start time, highest value and stage; for the strategy, query
options and hooks that keep each run's first start time and highest
value in the query cache; and for each budget-window year, bill and own
year's bar using it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The existing pending-status test asserted that an economy run's progress
was the synthetic timer's. With the shared model it is the run's cached
highest value at a poll.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A report's own year, its other budget-window years and bill reports now
take their bar's value from one pure function in
libs/calculations/runProgress.ts, on the same clock curve as UK local
areas and state district runs:

- runProgressAt: the clock since the run's first request, held inside its
  current stage, never below the highest value reached at a poll.
- highestProgressAtPoll: what the strategy keeps in the query cache at
  each poll, counting the bar by the last poll's stage too, so a bar that
  moved between polls is never above the cached value.
- economyRunStartedAt: a computing run keeps its first start time; a new
  run, or a retry after a failure, starts now.

The strategy's straight-line estimate and, for economy runs only, the
synthetic timer in useCalculationStatus are replaced by it. The metadata
function forEconomyRun accepts now receives the cached status, so other
years and bills keep their start time across polls and remounts and
return it. The own year's run starts its clock from the report's saved
request time (CalcStartConfig.startedAt). RunProgress no longer holds a
run's cached highest value inside the current stage, and each other
year gets its own bar, so leaving a year and returning never lowers it.
pollRun computes its progress with the same function, unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…equest

- A computing year keeps its first start time after the page is left for
  longer than React Query keeps unused queries by default (5 minutes).
- forEconomyRun keeps its cache entry for the session.
- A bar shown before the run's first request measures from that request
  once it is sent, as a bar shown later for the same run does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…irst request

- forEconomyRun sets gcTime: Infinity, as UK local areas and state
  district runs do, so a computing year's or bill's start time and
  highest progress outlast a visit to another page longer than React
  Query's default 5 minutes.
- RunProgress measures from the run's start time once the clock reaches
  it, not only when it is earlier than the mount. A bill's bar that
  appears while its reform policy is still being created then agrees
  with a bar shown later for the same run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
At Anthony's choice (option A after review round 2), a bar that appears
before its run's first request keeps counting from when it appeared, as
on main. The round 1 test that expected it to switch to the request time
is replaced.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Reverts round 1's change to RunProgress's clock, at Anthony's choice
(option A). A start time later than the bar's mount, such as a bill's
first request sent after its reform policy is created, counts from the
mount again, so the elapsed time never steps back. Known limit, bills
only: a bar shown again for the same run is lower by the time spent
before the request (about 2 points for 5 s in a 190 s state run).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vercel

vercel Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
policyengine-calculator-next Ready Ready Preview Nov 27, 2026 9:04pm UTC
policyengine-website Ready Ready Preview Nov 27, 2026 9:04pm UTC

Request Review

@anth-volk
anth-volk marked this pull request as ready for review November 27, 2026 21:43
@anth-volk
anth-volk merged commit e8ec4e7 into main Nov 27, 2026
12 checks passed
@anth-volk
anth-volk deleted the economy-progress-model branch November 27, 2026 21:43

This branch was successfully deployed

2 active deployments
Preview – policyengine-website — 500c2130 Deployed Nov 27, 2026 by vercel[bot]
Preview – policyengine-calculator-next — 500c2130 Deployed Nov 27, 2026 by vercel[bot]
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.

1 participant