task-0230 Plan — Bounded Nightly Summary Caching

Outcome

Make summaries:run_nightly_summaries complete predictably without changing any KPI formula or displayed summary value. Nightly work will be an explicit, database-backed, deduplicated list rather than “render every summary for every company code.”

Review the old/new workflow diagram

The supporting measurements are in the investigation artifact.

What Is Broken Today

The current task builds every Summary once with no ccode and again for every ccode. In standard POS, that starts 1,593 complete summary builds. Nested summaries and entity/location/position/employee sections multiply the work further, and ignore_cache: true makes every repeated KPI execute again.

This means the process behaves like a hang even when no individual reporting query is stuck. The existing advisory lock prevents overlap, but it leaves one effectively permanent run holding the lock while every later night skips.

Implementation Plan

1. Let administrators choose what is warmed nightly

This makes the selection authoritative and database-backed. The code will not contain a hard-coded summary list.

2. Plan cache work without rendering whole summaries

Introduce a dedicated planner responsible for turning enabled summary definitions into cache requests.

3. Execute the bounded worklist safely

4. Update operating guidance

Verification Plan

Add focused automated coverage for:

Per repository policy, reset the test database and run Rails tests only inside Docker. Run focused Rails and React tests after the implementation batch, followed by the dependency-closure and required parity gates before review.

Controlled Production Rollout

  1. Deploy the code with no summaries enabled and keep the nightly cache cron paused.
  2. Run the production dry-run mode. Confirm it performs no cache writes and review its root/context/unique-work counts.
  3. Use Summary Settings to enable a deliberately small initial set selected by the operator—not by a source-code default.
  4. Run one supervised cache execution while observing duration, database activity, failures, memory, and the advisory lock.
  5. Compare refreshed cache rows and visible summary values with on-demand calculations for representative contexts.
  6. Expand the enabled set only after each supervised run completes comfortably before the next scheduled window.
  7. Re-enable cron after the approved set completes predictably and the failure path has been observed to release its lock.

Rollback is simple: disable the nightly flags or pause cron. On-demand summary behavior and existing cache rows remain available.

Review Setup

This adds a setting to the React Summary Builder. Before moving the build to review:

Approval Boundary

Approving this plan authorizes a local task branch/worktree and implementation through the building -> review gate. It does not authorize a remote push, CI transition, PR, production deployment, cron change, or enabling any production summary.