Skip to content

Fix .SortTokens() crash for states only seen inside a polymorphism - #142

Merged
ms609 merged 1 commit into
cpp-searchfrom
feature/sorttokens-orphan-state
Aug 7, 2026
Merged

Fix .SortTokens() crash for states only seen inside a polymorphism#142
ms609 merged 1 commit into
cpp-searchfrom
feature/sorttokens-orphan-state

Conversation

@ms609

@ms609 ms609 commented Aug 5, 2026

Copy link
Copy Markdown

Summary

  • .SortTokens() (R/Consistency.R) left mapping at 0 for any single state that only ever appears inside an ambiguous/polymorphic token (e.g. "(12)"), never on its own.
  • wholes[x] then summed to 0 instead of a real combined code when remapping that ambiguous token, corrupting the effective state count and crashing ExpectedLength()'s downstream tabulate/sample pipeline (names<- length mismatch), which Consistency()'s rhi column depends on.
  • Fix: after assigning codes to standalone ("whole") tokens, scan the full whole-token bit range for any position still unassigned and give it its own unused code, so ambiguous-token sums are always meaningful.

Repro:

library(TreeTools)
tr <- BalancedTree(paste0("t", 1:4))
dat <- StringToPhyDat("00(12)(12)", TipLabels(tr))
Consistency(dat, tr, nRelabel = 20)
# Error in names(object) <- nm : 'names' attribute [4] must be the same length as the vector [2]

Fixes #135

Test plan

  • Added a direct .SortTokens() unit test for a state with no whole-token row
  • Added an ExpectedLength()-level regression test reproducing the original crash
  • testthat::test_file("tests/testthat/test-Consistency.R"): 25/25 pass
  • spelling::spell_check_package(): clean
  • GHA agent-check.yml on this branch: PASS

🤖 Generated with Claude Code

A state that never appears as its own unambiguous token -- only ever
inside an ambiguous (polymorphic) token like "(12)" -- had no row in
`mapping`, so `wholes` silently held 0 for it. Summing `wholes[x]` for
the ambiguous token then produced 0 instead of a real combined code,
corrupting the state count and crashing ExpectedLength()'s downstream
tabulate/sample pipeline with a names<- length mismatch.

Fixes #135

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@ms609
ms609 merged commit de519af into cpp-search Aug 7, 2026
14 checks passed
@ms609
ms609 deleted the feature/sorttokens-orphan-state branch August 7, 2026 13:01
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.

A14: .SortTokens() zeroes a state that only ever occurs inside a polymorphism, crashing ExpectedLength()

1 participant