paletteIndexForColor returns wrong index for non-adjacent duplicate-color regions #98
Labels
No labels
blocked:commit-ready-failed
blocked:gate-fetch
blocked:gate-lint
blocked:gate-plan
blocked:gate-research
blocked:needs-attention
blocked:needs-decision
blocked:needs-human
blocked:no-changes
blocked:verify-failed
bug
deferred
feature-request
in-progress
needs-decompose
needs-triage
quickfix
security/medium
triage-approved
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Mark/Kimi_Agent_Brother_10_Digitizing_App#98
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Where:
src/components/workspace/useWorkspace.ts:768-781(paletteIndexForColor).Bug:
paletteIndexForColoris supposed to return the index ofcolorin theunique-color ordering of
regions. The implementation usesseen.sizeforthe returned index but also calls
seen.add(region.color)in the non-matchingbranch of the loop, so
seen.sizeends up being the total number of uniquecolors including ones encountered after this color in the iteration, not the
position of this color in the unique sequence.
For
regions = [{color: "red"}, {color: "blue"}, {color: "red"}]:call
paletteIndexForColor("red", regions):index = seen.size = 0. add red → seen={red}. return 0. ✓but
regions.map(r => paletteIndexForColor(r.color, regions)):index = seen.size = 1. add blue. return 1. ✓index = seen.size = 2. add red. return 2. ✗So the returned array is
[0, 1, 2]instead of[0, 1, 0]. A red region thatappears after any non-red region gets the wrong
paletteIndex, which thenmisaligns it with
BROTHER_PR10_COLORSwhen computing hex codes or matchingagainst the thread bar.
Repro:
positions (e.g. multiple red regions separated by another color).
thread color / wrong position in the BottomThreadBar.
Fix:
Return the count of preceding unique colors, not the full
seen.sizeafteralso processing trailing colors. Two small edits:
The inner
seen.add(color)is no longer needed (thereturnruns first).Acceptance:
paletteIndexForColorreturns[0, 1, 0]for[{red}, {blue}, {red}].runAutoDigitizeend-to-end: digitize afixture with non-adjacent duplicate-color regions, assert
colorRegions[i].paletteIndexmatches the unique-position, not theoccurrence index.
same fixture.
False positive — closing.
Repro script (extracted function verbatim, ran with node 22):
My original trace in the issue body was wrong: the
return indexinside thematching branch exits the loop immediately, so
seen.add(region.color)in thenon-matching branch only runs for colors that appear before the match, not
after. The function does what it says. Closing as wont-fix / not-a-bug.
Recommend we add a unit test for this function while we're in the area so a
regression of the algorithm can't slip through silently — happy to file that
separately.
Adding the regression test you suggested (and a couple of friends) on top of the dead-code tidy:
paletteIndexForColorfromuseWorkspace.ts(matches the file's existing convention of exportinginitialState,derivePESNameFromUpload).seen.add(color)after the earlyreturnin the matching branch.src/components/workspace/issue-98.test.tswith 7 cases covering: first occurrence, preceding-unique-count, the[red, blue, red]→[0, 1, 0]case from the body, longer non-adjacent sequences ([red, blue, green, red, blue, yellow, green]→[0, 1, 2, 0, 1, 3, 2]), missing color, empty list, and an explicit guard against trailing-color inflation.Landed as
b87c4b3onmain. npm test → 312/312 pass, lint clean, build succeeds. No CI workflow is configured on this repo, so no Actions run to wait on.Behavior is unchanged from the false-positive fix; this is purely a regression guard.