Skip to content

Multi-head 3/7: run-based analysis engine - #98

Merged
vycdev merged 9 commits into
vycdev:developfrom
Bjohnson131:mph-split-3-color-runs
Oct 31, 2026
Merged

vycdev merged 9 commits into
vycdev:developfrom
Bjohnson131:mph-split-3-color-runs

Conversation

@Bjohnson131

Copy link
Copy Markdown
Contributor

Third of seven PRs splitting up #38 (Multi Print Head Capability).

Depends on #97. Until #97 merges, this diff also shows part 2; the new code is src/lib/multiHeadAnalysisColorFirst.ts, its tests and the MultiHeadRangeAssignment type.

What

  • Group the printer-layer stack into color runs (contiguous layers in one filament).
  • Find head windows with an iterative consensus search over filament orders. The search is deterministic and bounded: head count is capped at 8, and when a window's K^N order space exceeds the search-depth tier's budget it is searched with a fixed evenly spaced sample instead of exhaustively.
  • Compute per-color optimal assignments inside each window.
  • Choose nozzle-to-run assignments that minimize filament swaps across windows.

Nothing in the app calls it yet; the schedule and UI wiring follow in parts 4 and 5.

Tests

tests/multiHeadAnalysisColorFirst.test.ts (47 tests), including determinism, budget-vs-exhaustive equivalence and the head-count clamp. tsc is clean and the full unit suite passes (931 tests, 0 failures, 1 skipped).

Series

  1. Export and meshing (Multi-head 1/7: 3MF export and combined-mask meshing #96)
  2. Analysis core (Multi-head 2/7: window analysis core #97)
  3. Run-based analysis engine (mph-split-3-color-runs)
  4. Head schedule and plan conversion (mph-split-4-schedule-plan)
  5. Worker, UI and export wiring (mph-split-5-ui-wiring)
  6. Docs and diagrams (mph-split-6-docs)
  7. Color grouping mode (mph-split-7-color-grouping)

No changelog entry: internal groundwork with no user-visible change.

🤖 Generated with Claude Code

Bjohnson131 and others added 2 commits October 22, 2026 23:32
React-free primitives for multi-head printing: expand auto-paint zones
into printer layers, Beer-Lambert color stack, LUT/window scoring and
window selection. Not used by the app yet.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Group the printer-layer stack into color runs, find head windows with
an iterative consensus search over filament orders (bounded by a
per-depth combination budget, deterministic), compute per-color optimal
assignments, and optimize nozzle-to-run assignments to minimize swaps.
Adds the MultiHeadRangeAssignment type. Not wired into the app yet.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@Bjohnson131

Copy link
Copy Markdown
Contributor Author

Unit test results for this branch (npm test on mph-split-3-color-runs): 931 tests, 930 passed, 0 failed, 1 skipped. Full output: https://gist.github.com/Bjohnson131/58348b01975e2da17ab1f0eab5c7603f (file test-results-mph-split-3-color-runs.txt).

@vycdev vycdev left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated code review

Verdict: Needs changes · Findings: 2 (2 inline)

Scope: only what this PR adds on top of #97; the earlier PRs in this stack are reviewed in their own threads.

Reviewed only this PR's additions over PR #97. All 47 focused tests pass, and write-free TypeScript checks report no diagnostics; build-mode tsc was blocked from writing cache files. Two issues need correction: inconsistent color simulation across windows and excessive nozzle-optimizer complexity at supported head counts.


Reviewed by GPT 6.1 Sol

Comment thread src/lib/multiHeadAnalysisColorFirst.ts Outdated
for (const [prevKey, prevEntry] of dp) {
const prevState = prevKey.split('\x00');

for (let ii = 0; ii < injs.length; ii++) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[high] Bound the nozzle optimizer at supported head counts

For consecutive events requiring all N heads, dp contains N! states and injs contains N! injections, producing (N!)² transitions per boundary. Calling optimizeNozzleAssignments([f, f], 7) with seven distinct filament IDs took approximately 10.9 seconds here despite requiring zero swaps. At the supported eight-head cap, this trivial case executes 1,625,702,400 transitions. Neither the combination budget nor Fast mode limits this stage, so valid multi-window analyses can become impractically slow. Eliminate equivalent head permutations or replace/bound this search, and cover consecutive full-head windows at the cap.

Reviewed by GPT 6.1 Sol

Comment thread src/lib/multiHeadAnalysisColorFirst.ts Outdated
(r) => bestUniqueIndices!.indexOf(layers[r.startLayerIdx].filamentIdx)
);
const { assignments } = computeColorOptimalAssignments(
bestWindowRuns, wEnd, layers, colorAtLayer[wStart - 1], bestWindowFilaments, pixels,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[medium] Optimize each window against the color-specific stack

colorAtLayer represents the consensus stack, but the returned color sequences substitute each color's own assignments in earlier windows. Higher windows therefore use a prefix that differs from what will be printed, and the saved assignments are never reconciled. In a two-head fixture with 0.25-mm layers, one swatch per target layer, and selected windows [1,2] and [3,4], target #6b6b6b gets ΔE 25.41; changing only layer 3 to the other available filament reduces it to 22.53 (the original stack scored 23.05). Compute or reconcile assignments against each color's realized prefix and suffix, and add a regression exercising multiple selected windows.

Reviewed by GPT 6.1 Sol

- Reuse final printable layers and swatch surface mappings
- Preserve fitted optics and resolve substrates for candidate runs
- Add seven regression tests and update the unreleased changelog
- Reconcile per-color window assignments against realized stacks
- Replace factorial nozzle search with deterministic optimal scheduling
- Preserve merged target mappings and fitted run optics
- Add regression coverage and update the unreleased changelog
@vycdev

vycdev commented Oct 29, 2026

Copy link
Copy Markdown
Owner

Fixes for the automated review

Pushed b5307df to this branch.

This branch also merges the fixed lower layers (#97), so it builds on the corrected code.

Findings

  • ✅ Optimize each window against the color-specific stack: Reconcile assignments against each color group’s realized prefix and suffix in bounded passes, retaining its original stack unless quality improves. Added a two-window regression that independently evaluates alternative assignments.

  • ✅ Bound the nozzle optimizer at supported head counts: Replaced factorial permutation search with deterministic scheduling that reuses loaded filaments and evicts the filament needed farthest in the future. Added exhaustive comparisons across 2,401 small schedules and a consecutive eight-head regression.

Also fixed

  • Preserve merged lower-layer mapping and optics behavior: Use final-stack target mappings and shared cumulative run simulation, including fitted optics and changed substrates. Added mapping, weighted-centroid, and optical-model regressions.

Verification: All 83 multi-head analysis tests pass.


Fixed by GPT 6.1 Sol

@vycdev vycdev left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated code review: second pass

Verdict: Looks good · New or remaining findings: 0

Reviewed head b5307df, which includes the fixes for the first review. Scope: only what this PR adds on top of #97; the other PRs in this stack are reviewed in their own threads.

Both earlier findings are resolved, and I found no additional actionable defects in this PR’s changes over #97. All 83 focused multi-head tests, TypeScript checking, and targeted ESLint passed; 500 independent randomized optical comparisons also passed. No semantic conflicts with the newer develop changes were identified.

First-pass findings

  • ✅ Resolved: Optimize each window against the color-specific stack. Assignments are now reconciled against each color group’s realized prefix and suffix in bounded improvement passes. The two-window regression independently evaluates alternative assignments and checks that returned stacks preserve or improve baseline quality.
  • ✅ Resolved: Bound the nozzle optimizer at supported head counts. The factorial search has been replaced with deterministic reuse and farthest-future eviction scheduling. Exhaustive comparisons across 2,401 small schedules and the consecutive eight-head regression pass.

Reviewed by GPT 6.1 Sol

- Use snapshot baselines and resolve full candidate appearance corrections
- Preserve required foundation layers without changing LUT indices
- Add four regression tests and update the existing Added changelog entry
vycdev added a commit to Bjohnson131/Kromacut-mph that referenced this pull request Oct 29, 2026
@vycdev

vycdev commented Oct 29, 2026

Copy link
Copy Markdown
Owner

Second-round fixes merged in

Pushed 359f0c9 to this branch.

This branch merges the second-round fixes from the lower layers (#97), so it builds on the corrected code. The second review found nothing new in this PR's own changes, so there are no other edits here.

Verification: After the merge: full unit suite 947/947, tsc -b and lint pass. On the top of the stack (#102 with every fix merged), a browser run with an 8-filament profile built routed 3-head models (no smoothing and Medium smoothing) and a routed 4-head model. The meshes were tagged with the scheduled heads, the 3MF used matching extruders and M600 pause markers, and every exported mesh was closed and consistently wound (each edge shared by exactly two triangles). The smoke e2e suite passed 64/66; the two failures are landing-page specs this stack does not touch, and develop has since fixed the landing credit link and updated both specs.


Reviewed by GPT 6.1 Sol

- Cap each window at the color runs above the foundation instead of
  requiring exactly one run per head, so selecting more heads than runs
  no longer finds no window and falls back to single-head
- Keep the selected head count for scheduling; spare heads stay empty
- Add a regression comparing four heads with three on a three-run stack
vycdev added a commit to Bjohnson131/Kromacut-mph that referenced this pull request Oct 29, 2026
@vycdev

vycdev commented Oct 29, 2026

Copy link
Copy Markdown
Owner

Fix: more heads than color runs

Pushed c2f8d38 to this branch. This is a follow-up to investigating why multi-head results got worse at higher head counts.

  • ✅ Selecting more heads than color runs found no window: a window had to span exactly one run per selected head, and the first run is never reordered. With more heads than runs above it (for example 8 heads on an 8-run stack), there was no window to search, so the build fell back to single-head. Windows are now capped at the runs above the first run, the selected head count is kept for scheduling, and spare heads stay empty. 8 heads now gives the same result as 7.
  • Regression test: four heads on a three-run stack produce the same windows, layers and color paths as three heads, with an idle head in every window.

Known limitation, not changed here: when a window has more filament orders (K^N) than the search depth's budget, the deterministic sample can miss an improvement that a smaller, exhaustively searched window finds. On the browser check's 8-color image, 3–6 heads find an improving window, but 7 and 8 heads sample 65,536 of 7^7 orders, find none, and still fall back.

Verification: full unit suite 948/948, tsc -b and lint pass.


Fixed by Claude Opus 5.5

- When a window has more orders than the search budget, always evaluate
  every order that changes one block of neighbouring runs, sized to half
  the budget, before the evenly spaced samples; the evaluation count is
  unchanged
- Seven or eight heads previously sampled past improvements that windows
  of three to six heads find exhaustively, and fell back to single-head
- Add tests for exhaustive small windows and block coverage when sampling
vycdev added a commit to Bjohnson131/Kromacut-mph that referenced this pull request Oct 29, 2026
vycdev added a commit to Bjohnson131/Kromacut-mph that referenced this pull request Oct 29, 2026
vycdev added a commit to Bjohnson131/Kromacut-mph that referenced this pull request Oct 29, 2026
vycdev added a commit to Bjohnson131/Kromacut-mph that referenced this pull request Oct 29, 2026
@vycdev

vycdev commented Oct 29, 2026

Copy link
Copy Markdown
Owner

Search fix: large windows keep short reorders

Pushed 8498506 to this branch. This resolves the known limitation noted in my previous comment.

  • ✅ Seven or eight heads sampled past improvements that fewer heads find: when a window has more filament orders (K^N) than the search depth's budget, the scan sampled evenly spaced orders, which could miss an improvement that a smaller, exhaustively searched window finds. On the browser check's 8-color image, 3–6 heads found an improving window, but 7 and 8 heads sampled 65,536 of 7^7 orders, found none, and fell back to single-head. Sampled scans now first evaluate the current order and every order that changes one block of neighboring runs, using up to half the budget (four runs for Balanced with seven filaments), then evenly spaced orders for the rest. The evaluation count is unchanged.
  • Tests: windows within the budget stay exhaustive, and a sampled seven-run, seven-filament window includes every change within any four neighboring runs while evaluating exactly budget + 1 orders.

Verification: full unit suite 950/950, tsc -b and lint pass. Replaying the browser check's analysis request, 7 and 8 heads now find the same improving window as 3–6 heads at both Balanced and Fast depth. On the Rocco benchmark, 7 and 8 heads score the same as before (Layer-based CIE76 25.37, Color-based 22.08).


Fixed by Claude Opus 5.5

@vycdev
vycdev merged commit 7842bc4 into vycdev:develop Oct 31, 2026
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.

2 participants