refactor(planning): run planner messages through a swappable job with an in-process implementation #336

Closed
opened 2026-11-10 17:53:48 +01:00 by nessar · 0 comments
Owner

Parent

#335

What to build

This is the prefactoring step of workstream A (decided): make the job that carries planner messages (WorkerJob) an explicit, swappable seam that jobs implemented outside the lifecycle module can use fully, and add the third implementation the rest of workstream A needs. It adds the seam and the new pieces, deletes no calculation path and changes no behavior seen by users, so every later ticket can be green on its own.

  • Extract the planner worker's message handling into a function that takes a post callback and returns a message handler. It handles the four request kinds (plan, cancel, counterfactual, explain-checkpoint) and posts the existing response kinds with the existing meaning. The handler keeps its own active-calculation state per handler instance, not in module state, because several in-process jobs can live at once. The worker entry becomes a thin wrapper that binds the handler to the worker's message port. The planning server bundles the worker entry unchanged, and the specs' real-worker harness assigns the message handler when the worker entry is imported, so keep that contract or update the harness in this ticket.
  • Make the WorkerJob factory a parameter of the calculation entry point (and of the module's other entry that wraps it, if it keeps one). Without a factory the default is today's selection, unchanged: a socket to the planning server when a build identifier and WebSocket exist, falling back to the local worker; otherwise the local worker. A calculation uses its factory for every job it starts: the plan, a counterfactual date analysis and a checkpoint explanation.
  • State the rule for an explicit factory plainly and implement it: when a factory is passed, the entry point runs the worker-path lifecycle with it, whatever the globals say (a window, a Worker, a WebSocket or none of them), and counterfactual analysis uses it too instead of its main-thread fallback. The global checks, including the two old non-worker paths that still exist in this ticket, apply only when no factory is passed. Without this rule, a spec under the default test setup (a window but no Worker) that passed the in-process job would still land in the old branch for a window without a Worker.
  • Move supersession registration into the lifecycle. Today the local-worker job and the socket job each add their supersede callback to a module-private set (and the old no-window branch does the same), and a job implemented outside the module cannot reach that set, so a calculation on the in-process job or a scripted job would never be superseded by a newer calculation. The lifecycle now registers and releases supersession for every job it starts (the plan, a counterfactual date analysis, a checkpoint explanation), and job implementations do transport only. Behavior is unchanged for the two existing jobs. The old no-window branch keeps using the same set until the last A ticket deletes it.
  • Add the in-process job as a permanent third implementation (for tests and non-browser callers, not a temporary shim). It runs the extracted handler. It delivers responses in order, asynchronously (never re-entrantly from post) and without timer-based delay, so the benchmark gate is not inflated. Its release is idempotent and silences later responses. Put it in its own module that the default selection does not import, so production still loads the planner core only in the worker bundle.
  • Add two spec helpers: a scripted job (implements the job interface, records posts, lets a test emit responses and failures, counts releases), and a helper that creates a calculation backed by the in-process job. Later tickets use them to replace window stubbing and fake Worker classes.
  • Do not delete either non-worker path in this ticket, and do not edit existing specs except where the worker restructure or the supersession move forces it.

Acceptance criteria

  • bun run lint, bun run format:check, bun run typecheck and bun run test pass, with no existing spec changed (a change forced by the worker restructure or the supersession move is named in the PR description). The existing worker-lifetime and remote-socket specs pass.
  • Under the default test setup, with no window stub (a window is present and Worker is not), a calculation created with the in-process job completes with a plan through the worker-path lifecycle; and a calculation created with a scripted job posts its counterfactual and checkpoint-explanation requests to that scripted job rather than to a main-thread fallback.
  • A newer calculation supersedes a calculation that is running on a scripted job and one running on the in-process job: each settles with the stale-calculation interruption and its job is released, and the checkpoint is kept (shown with the scripted job, which emits a checkpoint first). Counterfactual analyses and checkpoint explanations running on a scripted job are superseded too.
  • A new spec runs the same requests through the in-process job and through the handler hosted in the real worker harness, and shows identical response-kind sequences and identical payloads for result, artifacts, explanation, counterfactual-result and error responses (heartbeat counts excluded). The requests cover a completed plan, a planning conflict, invalid input, a cancel in the middle of a run, a counterfactual analysis and a checkpoint explanation.
  • New specs for the in-process job show: ordered delivery, no delivery inside post, idempotent release that silences later responses, two jobs running at once without shared state, and an error response (with the code the worker uses) when the handler faults.
  • New specs show that the factory reaches the plan job, the counterfactual job and the checkpoint-explanation job, and that omitting it keeps today's selection.
  • The frozen-engine differential check passes unchanged (bun test --isolate src/app/services/frozen-engine.spec.ts) and no frozen-engine fixture file changes.
  • The benchmark gate passes with its multipliers unchanged (bun test src/app/benchmark --timeout=240000).
  • bun run build -- --configuration production passes, and the browser output does not contain the in-process job (compare with the build output on develop).
  • The server planning spec passes, which shows the server still runs the unchanged worker entry.
  • The diff deletes no calculation path; a reviewer can confirm both non-worker branches are still present.
  • Delivered on a feature/* branch from the current origin/develop, PR to develop with a Conventional Commit title, merge commit.

Blocked by

None — can start immediately.

## Parent #335 ## What to build This is the prefactoring step of workstream A (decided): make the job that carries planner messages (WorkerJob) an explicit, swappable seam that jobs implemented outside the lifecycle module can use fully, and add the third implementation the rest of workstream A needs. It adds the seam and the new pieces, deletes no calculation path and changes no behavior seen by users, so every later ticket can be green on its own. - Extract the planner worker's message handling into a function that takes a post callback and returns a message handler. It handles the four request kinds (plan, cancel, counterfactual, explain-checkpoint) and posts the existing response kinds with the existing meaning. The handler keeps its own active-calculation state per handler instance, not in module state, because several in-process jobs can live at once. The worker entry becomes a thin wrapper that binds the handler to the worker's message port. The planning server bundles the worker entry unchanged, and the specs' real-worker harness assigns the message handler when the worker entry is imported, so keep that contract or update the harness in this ticket. - Make the WorkerJob factory a parameter of the calculation entry point (and of the module's other entry that wraps it, if it keeps one). Without a factory the default is today's selection, unchanged: a socket to the planning server when a build identifier and WebSocket exist, falling back to the local worker; otherwise the local worker. A calculation uses its factory for every job it starts: the plan, a counterfactual date analysis and a checkpoint explanation. - State the rule for an explicit factory plainly and implement it: when a factory is passed, the entry point runs the worker-path lifecycle with it, whatever the globals say (a window, a Worker, a WebSocket or none of them), and counterfactual analysis uses it too instead of its main-thread fallback. The global checks, including the two old non-worker paths that still exist in this ticket, apply only when no factory is passed. Without this rule, a spec under the default test setup (a window but no Worker) that passed the in-process job would still land in the old branch for a window without a Worker. - Move supersession registration into the lifecycle. Today the local-worker job and the socket job each add their supersede callback to a module-private set (and the old no-window branch does the same), and a job implemented outside the module cannot reach that set, so a calculation on the in-process job or a scripted job would never be superseded by a newer calculation. The lifecycle now registers and releases supersession for every job it starts (the plan, a counterfactual date analysis, a checkpoint explanation), and job implementations do transport only. Behavior is unchanged for the two existing jobs. The old no-window branch keeps using the same set until the last A ticket deletes it. - Add the in-process job as a permanent third implementation (for tests and non-browser callers, not a temporary shim). It runs the extracted handler. It delivers responses in order, asynchronously (never re-entrantly from post) and without timer-based delay, so the benchmark gate is not inflated. Its release is idempotent and silences later responses. Put it in its own module that the default selection does not import, so production still loads the planner core only in the worker bundle. - Add two spec helpers: a scripted job (implements the job interface, records posts, lets a test emit responses and failures, counts releases), and a helper that creates a calculation backed by the in-process job. Later tickets use them to replace window stubbing and fake Worker classes. - Do not delete either non-worker path in this ticket, and do not edit existing specs except where the worker restructure or the supersession move forces it. ## Acceptance criteria - [ ] `bun run lint`, `bun run format:check`, `bun run typecheck` and `bun run test` pass, with no existing spec changed (a change forced by the worker restructure or the supersession move is named in the PR description). The existing worker-lifetime and remote-socket specs pass. - [ ] Under the default test setup, with no window stub (a window is present and Worker is not), a calculation created with the in-process job completes with a plan through the worker-path lifecycle; and a calculation created with a scripted job posts its counterfactual and checkpoint-explanation requests to that scripted job rather than to a main-thread fallback. - [ ] A newer calculation supersedes a calculation that is running on a scripted job and one running on the in-process job: each settles with the stale-calculation interruption and its job is released, and the checkpoint is kept (shown with the scripted job, which emits a checkpoint first). Counterfactual analyses and checkpoint explanations running on a scripted job are superseded too. - [ ] A new spec runs the same requests through the in-process job and through the handler hosted in the real worker harness, and shows identical response-kind sequences and identical payloads for result, artifacts, explanation, counterfactual-result and error responses (heartbeat counts excluded). The requests cover a completed plan, a planning conflict, invalid input, a cancel in the middle of a run, a counterfactual analysis and a checkpoint explanation. - [ ] New specs for the in-process job show: ordered delivery, no delivery inside post, idempotent release that silences later responses, two jobs running at once without shared state, and an error response (with the code the worker uses) when the handler faults. - [ ] New specs show that the factory reaches the plan job, the counterfactual job and the checkpoint-explanation job, and that omitting it keeps today's selection. - [ ] The frozen-engine differential check passes unchanged (`bun test --isolate src/app/services/frozen-engine.spec.ts`) and no frozen-engine fixture file changes. - [ ] The benchmark gate passes with its multipliers unchanged (`bun test src/app/benchmark --timeout=240000`). - [ ] `bun run build -- --configuration production` passes, and the browser output does not contain the in-process job (compare with the build output on `develop`). - [ ] The server planning spec passes, which shows the server still runs the unchanged worker entry. - [ ] The diff deletes no calculation path; a reviewer can confirm both non-worker branches are still present. - [ ] Delivered on a `feature/*` branch from the current `origin/develop`, PR to `develop` with a Conventional Commit title, merge commit. ## Blocked by None — can start immediately.
nessar added the ready-for-agent label 2026-11-10 17:53:48 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: nessar/vac-optimizer#336