Refactor routing internals and archive retry benchmarks

This commit is contained in:
Jan Petykiewicz 2026-11-26 19:58:46 -08:00
commit ea5cfe7f31

View file

@ -0,0 +1,59 @@
# Routing refactor plan
Make routing behavior easier to measure, test, and change through independent refactors. Start with shared benchmark configuration and parity checks, then make temporary obstacle cleanup exception-safe. Keep the [public routing API](../DOCS.md) and current planner behavior as the baseline for planned changes.
## Planned
### Benchmark reproducibility and router state
1. **Share the corpus and benchmark profiles.** Give the comparison CLI, pytest, and the performance snapshot tools one source for case groups and named configurations. The CLI's matched-budget defaults currently use two congestion iterations and a 600-node seed budget. The corpus test uses three iterations and a 500-node seed budget. Make those settings explicit and preserve the existing configurations when moving them. Store expected coverage with the fixtures, including the bounds for `overloaded-gate` and `lidar-clements-8x8`. [Benchmark CLI](../scripts/compare_topological_routing.py) · [Corpus tests](../inire/tests/test_routing_benchmarks.py)
2. **Make exact parity a reusable benchmark command.** Compare runs by case and effective profile. Check verified net IDs, path seeds, per-net costs, selected stages and stage order, stable counters, and callback payloads. Report the first differing case, stage, or net and compare elapsed time separately. Use the command when checking subsequent behavior-preserving work.
3. **Scope temporary obstacle reservations.** Greedy warm starts remove temporary obstacles on normal completion, and corridor seeding already uses `finally`. Share the cleanup rule while preserving insertion order and cache invalidation. Test that an interrupted greedy search leaves the routing world reusable. [Warm starts and corridor seeds](../inire/router/_router.py)
4. **Consolidate routing counter definitions.** Define counter defaults, reset behavior, and snapshot aggregation once. Keep direct attribute increments in the search loop, preserve the distinction between per-search and cumulative counters, and retain the public `RouteMetrics` fields. Check that runs and reports expose the same counter values. [Mutable counters](../inire/router/_astar_types.py) · [Public metrics](../inire/results.py)
### Routing decisions and diagnostics
5. **Extract trace formatting from `PathFinder`.** Start with pure formatters for conflict and iteration entries. Keep trace capture points visible in the routing loop, including the stages around best-iteration restoration and pair-local repair. Compare the serialized traces before and after. [Trace capture](../inire/router/_router.py)
6. **Extract negotiated-congestion decisions.** Make reroute selection, late search caps, and stop conditions testable against an iteration review and recent search work. Use small tests to exercise those rules independently of A*, with corpus parity to check their integration. [Congestion routing](../inire/router/_router.py)
7. **Group the per-search congestion caches.** Give the six congestion cache dictionaries one owner for each A* search. Simplify the signatures between the search loop, move expansion, and admission while preserving cache keys and lifetime. Compare stable counters and runtime. [Search loop](../inire/router/_search.py) · [Move admission](../inire/router/_astar_admission.py)
8. **Consolidate direct-policy settings.** Define each direct policy's connectivity checks, lookahead, budget splitting, and unlocked probe in one internal table. Keep the attempt sequence and early exits explicit and preserve policies exposed by the benchmark CLI. [Direct and residual policies](../inire/router/_routing_policy.py)
9. **Make automatic trial acceptance explicit.** Distinguish locally feasible candidates, whole-board validity, and eligibility to replace an incumbent. Preserve the rule that cost ranks alternatives with the same verified net IDs. Record when residual results are checked and when unresolved results are merged so stage metadata remains consistent. [Trial selection](../inire/router/_routing_policy.py) · [Automatic orchestration](../inire/router/_automatic.py)
10. **Make capacity bounds independently testable.** Extract the cut-capacity calculation and the disjoint port-conflict check. State the geometry assumptions beside each bound and test when a bound applies separately from the policy decision to stop. Preserve reported cuts, conflict pairs, and routing targets. [Capacity and port checks](../inire/router/_automatic.py)
11. **Separate topological planning from corridor construction.** Give endpoint access, mesh paths, lane assignments, and conversion to `Corridor` explicit internal boundaries. That lets lane-allocation tests exercise choices and tie-breaking independently of the lattice router. [Topological corridors](../inire/router/_topology.py)
12. **Separate column scheduling from seed generation.** Distinguish opening and track assignment, feasible S-bend schedules, and the conversion of a schedule to seeds. Preserve candidate order and full-board verification. Exercise individual transition failures so new passage patterns can be tested in isolation. [Column planning](../inire/router/_columns.py)
13. **Reuse verification details in final diagnostics.** Share final verification details with conflict and frontier reporting where they cover the same net population and installed geometry. Compare exact routes and trace payloads, then measure the change in verification calls and runtime. [Final verification and frontier capture](../inire/router/_router.py)
### Geometry and tooling cleanup
14. **Expose live raw static obstacles through `RoutingWorld`.** Give pair-local search and corridor setup an ordered iterator so both can use current obstacles, including obstacles added after problem creation. Keep index storage details inside the world. [Routing world](../inire/geometry/collision.py) · [Pair-local search](../inire/router/_pair_local.py)
15. **Centralize cardinal coordinate transforms.** Share forward and lateral projection across the refiner, perimeter planner, and direct-route planner. Cover all four headings and leave snapping at its current points. [Port primitives](../inire/geometry/primitives.py) · [Refiner](../inire/router/refiner.py)
16. **Move shared geometry helpers out of planner modules.** Give helpers such as exact S-bend spans a neutral home so compact routing can reuse them directly. Preserve exact integer spans for constructive routing and floating-point reach calculations for search. [Compact routing](../inire/router/_compact.py) · [Column planning](../inire/router/_columns.py)
17. **Share component construction between seed replay and A*.** Reuse the mapping from move parameters to straight, 90-degree bend, and S-bend geometry. Preserve A* cache keys, handling of infeasible moves, and the geometry options applied during seed replay. [Seed materialization](../inire/router/_seed_materialization.py) · [Move admission](../inire/router/_astar_admission.py)
18. **Name scoring objectives by purpose.** Make search cost, refinement cost, automatic trial ranking, and benchmark-weighted length clear in code and reports. Reuse the weighted-length calculation where it is identical and label native and benchmark costs explicitly. [Automatic scoring](../inire/router/_automatic.py) · [Benchmark reporting](../scripts/compare_topological_routing.py)
19. **Share trace recorder boilerplate.** Reuse scenario selection, result summaries, command-line arguments, and artifact writing across conflict, frontier, pre-pair, and iteration recorders. Keep each report's renderer and schema in its own script. [Frontier recorder](../scripts/record_frontier_trace.py) · [Iteration recorder](../scripts/record_iteration_trace.py)
20. **Keep fixtures and routing docs easy to maintain.** Organize large fixture modules by family while preserving stable case names and corpus order. Keep feasibility witnesses separate from timed inputs. Update the architecture map for pair-local search and label the original routing goal as a historical milestone. [Benchmark fixtures](../inire/tests/routing_benchmarks.py) · [Architecture](architecture.md) · [Original routing goal](routing-policy-goal.md)
21. **Resolve the remaining lint findings.** Review and fix the reported issues in `inire` and `scripts`, including redundant comprehensions and long lines. Review suggested fixes individually where they touch search or benchmark behavior. [Lint configuration](../pyproject.toml)
## Future-looking
22. **Consider a common residual routing interface when a new recovery strategy needs it.** Identify which operations can be shared across direct locks, incumbent seeding, perimeter recovery, and pair-local search. Preserve their distinct roles for frozen obstacles, seed hints, and merge-time verification. [Residual policies](../inire/router/_routing_policy.py) · [Pair-local search](../inire/router/_pair_local.py)
23. **Split collision queries further when profiling or geometry work identifies a reason.** Candidate lookup, exact overlap, and ray casting are possible boundaries. Compare clearance, boundary touches, physical and proxy geometry, and cache behavior before adopting a new split. [Collision queries](../inire/geometry/collision.py)
24. **Revisit public option grouping with a compatibility plan.** Consider grouping planner settings or bend geometry when an API change is warranted. Account for legacy custom bend polygons and separate physical and proxy geometry. Handle changes to defaults, validation, and public names as explicit API decisions. [Public options](../inire/model.py)
## Verification
Use the current 67-board corpus as the regression baseline for routing changes. The `corpus-regression` and `public-defaults` profiles reach every board's routing bound and verify 433 of 437 nets each. For behavior-preserving changes, compare exact paths, per-net costs, bounds, planner stage order and metadata, and relevant callback and trace payloads. For changes that intentionally eliminate work, such as item 13, record the expected counter differences while checking route and trace parity. [Corpus results and reproduction](topological-routing.md)
Capture the same cases and options from the baseline and the changed checkout, then compare them with the parity command. Its default capture uses `corpus-regression` (the 500-node, three-iteration pytest configuration) and `public-defaults`. Use `--profile matched-budget` for the original comparison CLI configuration. Use `--case` to select a case or group, `--callbacks` to add runs with live callbacks, and `--diagnostics` to capture detailed traces.
```sh
python -m scripts.routing_benchmark_parity capture /tmp/routing-before.json
python -m scripts.routing_benchmark_parity capture /tmp/routing-after.json
python -m scripts.routing_benchmark_parity compare /tmp/routing-before.json /tmp/routing-after.json
```
### Refactor validation
The 134 corpus runs matched the baseline exactly. In 20 runs with diagnostics enabled, the routes, costs, stage metadata, traces, and callback results also matched. Reusing final verification details eliminated 172 path verification calls, 524 static buffer operations, and 48 dynamic candidate-net checks across those diagnostic runs.
On a 32-net seeded straight-route layout with conflict and frontier diagnostics enabled, verification calls fell from 224 to 160 and median routing time fell from 19.05 ms to 15.28 ms. Each checkout ran 27 measured samples in alternating batches after warmup. The corpus timing totals stayed close at 105.04 s before and 105.74 s after, measured as the sum of individual runs.