Skip to content
SlateGitOrgPublic

About

Packs patch jobs into hospital maintenance windows to minimise risk-weighted exposure days under reboot, redundancy and change-freeze limits, with exactness checked against brute force.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

patchwave

CI

patchwave

patchwave packs patch jobs into the short overnight maintenance windows of a hospital network so that the sum of risk-weighted exposure days is as small as possible. It respects reboot redundancy per service, pairs of services that must never be down together, change freezes, prerequisite updates and the real length of each window, including a window that crosses a daylight-saving change. Four solvers (current-practice fifo, a ratio-rule greedy, seeded local search and an exact branch and bound) are compared against a capacity-relaxed lower bound, and every schedule any of them produces is checked by a separate validator.

All data is synthetic: a seeded generator invents the hospital, its hosts and its patch jobs. Nothing is downloaded and nothing talks to the network.

The problem

Clinical systems (image servers, EHR application tiers, lab interface engines, pharmacy dispensing servers) can only be patched in short overnight windows. Each service can lose only so many hosts at once, some services must not reboot in the same window, change freezes block whole weeks at a site, and some updates need a prerequisite applied first. Jobs are usually worked in ticket order, so a fix for an actively exploited flaw on an internet-facing server can wait weeks behind routine updates, and nobody can say what a change freeze costs in exposure.

The cost of a job in window w is weight x days from release to the end of w. A job that is not scheduled pays weight x days from release to the horizon end plus a penalty of 30 days of weight (UNSCHEDULED_PENALTY_DAYS), so leaving a job out never looks cheaper than patching it in the last window. Assigning jobs to windows with time-dependent costs, lane capacity and side constraints is a generalised assignment problem, which is NP-hard, so the heuristics are judged against two anchors: an exact solver that must equal brute-force enumeration on small instances, and a lower bound that every result must respect.

How it works

flowchart TD
    S["Scenario JSON<br/>hosts, services, windows,<br/>freezes, patch jobs"] --> P["Problem<br/>eligible windows, costs,<br/>feasibility tests"]
    P --> F["fifo<br/>ticket order"]
    P --> G["greedy<br/>Smith ratio rule"]
    P --> L["local<br/>greedy + seeded<br/>move, swap, eject"]
    P --> E["exact<br/>branch and bound<br/>up to 25 jobs"]
    P --> B["Lower bound<br/>capacity and redundancy<br/>relaxed"]
    F --> A["Assignment<br/>job to window"]
    G --> A
    L --> A
    E --> A
    A --> V["Independent validator<br/>release, capacity, redundancy,<br/>anti-affinity, prerequisites, freeze"]
    A --> R["Plan<br/>lanes, offsets, risk-days,<br/>unscheduled reasons"]
    B --> C["compare<br/>gap to bound"]
    R --> C
    R --> W["whatif<br/>extra window or freeze"]
    W --> A
Loading
  • patchwave.model holds the scenario: hosts (service, site), services (min_up), anti-affinity pairs, patch jobs, windows with timezone-aware starts and ends (via zoneinfo, with tzdata so every platform has the same rules) and freezes. Lane capacity is the real length of the window in minutes, taken from UTC instants.
  • patchwave.problem compiles a scenario to indexes and answers feasibility questions fast: is a job allowed in a window at all, do the jobs of one window fit on its lanes, does a window respect redundancy and anti-affinity, are prerequisites placed first.
  • patchwave.solvers implements fifo (ticket order, earliest feasible window), greedy (jobs by weight per minute, Smith's ratio rule, earliest feasible window; a prerequisite inherits the best ratio among its dependents) and local (greedy, then a seeded search that proposes moves, swaps and one-job ejections and keeps only strictly improving feasible ones).
  • patchwave.exact is a branch and bound over job-to-window assignments. Its bound is the sum of each remaining job's cheapest eligible window, which relaxes capacity, redundancy and anti-affinity but still respects release, site, freezes and prerequisite timing. It refuses more than 25 jobs and stops with a clear error after 1,000,000 search nodes (MAX_EXACT_NODES), so the size cap is not the only bound on runtime; compare then leaves the exact row out.
  • patchwave.layout places the jobs of a window on lanes. Jobs linked by a prerequisite inside the window form a chain that runs on one lane in order, and chains are packed by first-fit decreasing.
  • patchwave.validate is the independent checker. It reads only the scenario and the assignment, shares no code with the solvers, and returns every violation.
  • patchwave.whatif prices an extra window or a new freeze. --add-window restarts local search from the baseline schedule, which stays feasible when a window is added, and accepts only improvements, so the exposure can never go up. --freeze removes the jobs in the frozen windows and their dependents, re-inserts them greedily and then runs local search.

Quick start

python -m venv .venv && . .venv/bin/activate
pip install -e ".[dev]"

patchwave generate --seed 7 --out scenario.json
patchwave plan scenario.json --solver local --seed 3 --week 1
patchwave compare scenario.json --seed 3
patchwave whatif scenario.json --freeze north:2026-11-23..2026-11-29 --seed 3
patchwave whatif scenario.json --add-window "2026-11-19T22:00 America/Chicago 240m site=south" --seed 3

patchwave plan scenario.json --format json > plan.json
patchwave validate scenario.json plan.json

generate --no-freeze leaves out the week-5 freeze at north, which is the baseline for pricing that freeze with whatif.

Example output

The seeded scenario has 400 jobs, 118 hosts in 18 services, 56 windows over eight weeks (the Saturday window at imaging on 2026-10-31 spans the clock change and has 300 minutes per lane) and a change freeze at north in week 5 (2026-11-16 to 2026-11-22).

patchwave compare scenario.json --seed 3:

Scenario `regional-hospital-network`: 400 jobs, local-search seed 3

| solver | exposure (risk-days) | unscheduled | gap to lower bound |
|---|---:|---:|---:|
| fifo | 7884.638 | 1 | 122.9% |
| greedy | 4558.680 | 3 | 28.9% |
| local | 4538.721 | 2 | 28.3% |
| lower bound | 3537.430 | | |

local search removes 42.4% of fifo's exposure

Greedy and local search leave more jobs unscheduled than fifo because they place jobs with a high weight per minute first, so jobs with a low ratio lose windows to them, and for such a job the 30-day penalty is cheaper than the delay its place would cost the others; total exposure is what falls.

The gap is (exposure - bound) / bound. The remaining gap is what lane capacity, redundancy and anti-affinity cost, since the bound ignores all three. exact is left out of the table because the scenario has more than 25 jobs (it is also left out when its node limit is hit).

Under fifo the KEV-listed fix on the internet-facing portal host (released on a Monday) waits until the 2026-11-24 window, because nine routine reboot jobs for the same service, which may lose only one host per window, were ticketed first. Under greedy and local it goes into the first window after its release, 2026-10-20.

patchwave plan scenario.json --solver local --seed 3 --week 5 (rows trimmed with ...; no north window appears because of the freeze):

Plan for regional-hospital-network: solver local, seed 3

Tue 2026-11-17 22:00 CST  imaging-20261117  240 min x 2 lanes  6 jobs
  lane 1  +  0m  J-304  dicom-router-03  (dicom-router)  45m  CVE-2026-3356
  lane 1  + 45m  J-108  dicom-router-03  (dicom-router)  30m  CVE-2026-4181
  lane 1  + 75m  J-168  radiology-viewer-02  (radiology-viewer)  30m  CVE-2026-6812,CVE-2026-4775,CVE-2026-5381
  lane 1  +105m  J-032  radiology-viewer-04  (radiology-viewer)  20m  CVE-2026-8841,CVE-2026-3645
  ...

Tue 2026-11-17 22:00 CST  south-20261117  240 min x 2 lanes  12 jobs
  lane 1  +  0m  J-036  clinic-desktops-19  (clinic-desktops)  30m  CVE-2026-4612
  lane 1  + 30m  J-043  clinic-desktops-19  (clinic-desktops)  45m  CVE-2026-6340
  ...

Thu 2026-11-19 22:00 CST  south-20261119  240 min x 2 lanes  13 jobs
  lane 1  +  0m  J-274  scheduling-01  (scheduling)  90m  CVE-2026-8678,CVE-2026-6946,CVE-2026-5743
  lane 1  + 90m  J-391  patient-portal-06  (patient-portal)  90m  CVE-2026-7233
  ...

Exposure: 4538.721 risk-days
  scheduled jobs: 4109.586
  unscheduled jobs (with penalty): 429.135
Scheduled 398 of 400 jobs
Unscheduled by reason:
  exceeds-window-capacity: 1  (J-017)
  no-feasible-window: 1  (J-359)

Pricing the week-5 freeze with patchwave whatif nofreeze.json --freeze north:2026-11-16..2026-11-22 --seed 3 (where nofreeze.json comes from generate --no-freeze), extending it by a week, and adding a Thursday window at south:

What-if: freeze north 2026-11-16..2026-11-22
  baseline exposure: 4480.721 risk-days (2 unscheduled)
  after exposure:    4538.721 risk-days (2 unscheduled)
  change:            +58.000 risk-days
  jobs moved:        12
What-if: freeze north 2026-11-23..2026-11-29
  baseline exposure: 4538.721 risk-days (2 unscheduled)
  after exposure:    4630.221 risk-days (2 unscheduled)
  change:            +91.500 risk-days
  jobs moved:        16
What-if: add window extra-1 Thu 2026-11-19 22:00 CST
  baseline exposure: 4538.721 risk-days (2 unscheduled)
  after exposure:    4444.763 risk-days (1 unscheduled)
  change:            -93.958 risk-days
  jobs moved:        20

Design decisions

  • The validator is a separate implementation. validate.py re-derives capacity, redundancy, anti-affinity, prerequisite, release, site and freeze rules from the scenario alone. The cost is some duplicated logic (the lane packing exists twice); the benefit is that a bug in the solvers' fast feasibility code cannot hide, and a test checks that the two agree on thousands of random assignments. A conftest fixture makes every plan a test builds through solve, compare or whatif pass through it, and test_every_solver_schedule_validates_clean checks the plans of the shared seeded fixture explicitly.
  • Prerequisites travel as chains. A job whose prerequisite is in the same window must be later on the same lane. Treating each connected chain as one unit packed by first-fit decreasing makes that rule part of the capacity test instead of a second constraint. The cost is that a chain can need a lane to itself even when the lanes together have room.
  • A freeze blocks by wall date. A window is blocked when any wall date from its start date to its end date falls in the freeze, so a window that starts the evening before a freeze and ends after midnight inside it is blocked. The seeded Tuesday, Thursday and Saturday 22:00 windows never cross midnight into a freeze.
  • Windows in the repeated hour cannot be saved. JSON holds wall-clock text, which cannot say which of the two 01:00 hours is meant, so writing a window that ends there raises a ScenarioError instead of shifting it by 60 minutes. Generated windows never end there.
  • Constraints are checked per window. Redundancy and anti-affinity compare jobs in the same window. That is exact when a service lives at one site, which the generated hospital guarantees; services spread over sites with simultaneous windows would need a time-overlap rule.
  • Local search accepts only strict improvements. That makes local <= greedy and the add-window monotonicity hold by construction, at the price of getting stuck in local optima. Single-job ejections (move a job into a full window and send one resident elsewhere) widen the neighbourhood without needing a tabu list or annealing schedule.
  • The lower bound relaxes capacity, not time. Each job is charged its cheapest eligible window, with prerequisite chains pushing earliest start forward. It is cheap and valid, and it also makes the exact solver's pruning effective on small instances, but it is loose on congested instances like the seeded one.
  • Fixed-size exactness. Branch and bound is tested against brute force on 200 small instances and refuses more than 25 jobs. The size cap alone does not bound the runtime, because the bound relaxes capacity and redundancy and a congested 25-job instance can take very long, so the search also stops with an error after 1,000,000 nodes rather than hanging the CLI.

Testing

pytest -q

124 tests, all passing, with warnings treated as errors. They include: exact equals brute-force enumeration to 1e-9 on 200 seeded instances of up to 8 jobs and 3 windows, with fifo, greedy and local never below the optimum or the lower bound; hand-built schedules that each break one rule and yield exactly that violation; the 300-minute DST window; the seeded orderings and measured figures; the planted cases; determinism of the scenario and plan bytes; a Hypothesis property that adding a window never increases exposure; and CLI behaviour.

Project layout

src/patchwave/
  model.py       scenario dataclasses, JSON load and save, cost functions
  problem.py     indexed problem, feasibility tests, lower bound, unscheduled reasons
  solvers.py     fifo, greedy, seeded local search
  exact.py       branch and bound (up to 25 jobs)
  layout.py      lane layout and first-fit decreasing packing
  validate.py    independent schedule checker
  plan.py        plan objects, text and JSON rendering
  engine.py      run a named solver
  whatif.py      extra-window and freeze pricing
  compare.py     solver comparison with the lower bound
  generator.py   seeded hospital scenario with planted cases
  testing.py     small instances and brute-force optimum
  cli.py         Typer commands
tests/           unit, property and CLI tests

License

MIT, see LICENSE.

About

Packs patch jobs into hospital maintenance windows to minimise risk-weighted exposure days under reboot, redundancy and change-freeze limits, with exactness checked against brute force.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages