Skip to content

fix(G1): capture/ref inside parallel blocks - named rejection in ruby, rule-local refs in py - #848

Merged
ronaldtse merged 2 commits into
mainfrom
wo18-g1-capture-parallel
Nov 13, 2026
Merged

ronaldtse merged 2 commits into
mainfrom
wo18-g1-capture-parallel

Conversation

@ronaldtse

Copy link
Copy Markdown
Contributor

Summary

WO18 G1 from the maps parity audit (docs/parity/README.md): capture(...)/ref(N) inside a parallel block was broken in all engines except rs. This PR fixes the two halves that live here:

  • Ruby engine: Item::CaptureGroup/CaptureRef had no max_length, so the tree path's deliberate rejection (SystemConversionError) never surfaced — the megaregexp fallback crashed earlier with an accidental NoMethodError: undefined method 'max_length' inside deterministic_sort_by_max_length. Both classes now define max_length (mirroring the rs/ts runtime: a capture's length is its innards', a backreference counts 1), so the sort terminates and the named SystemConversionError — "Can't use CaptureRef in parallel mode" — surfaces. A capture in from with a ref-free to now runs the megaregexp fallback and produces output instead of crashing.
  • interscript-py: parallel_megaregexp wraps each rule in a named group and handed the GLOBAL group list to the replacement callback, so ref(1) resolved to the wrapper's whole match instead of the rule's first capture (ax → axX, k3a → kk3a; rs: aX, 3ka). Each rule's pattern is now compiled on its own to get its exact numbered-group count, and the replacement receives only the rule-owned slice of the global groups.

Semantics per runtime

  • py: correct output (rs parity) — the offset math is exact because each pattern's group count comes from regex.compile(pattern).groups (the same grammar the alternation embeds). The docstrings that codified the shifted numbering are corrected.
  • ruby: named rejection — matches the repo's fail-loud convention and the audit's ruby reference behavior: the tree path already rejects captures by design; the megaregexp fallback now reaches that named error instead of the accidental crash. Capture-from with a ref-free to still executes correctly through the fallback.

Test plan (TDD)

  • py: TestMegaregexp + TestParallel probe vectors (ax→aX, axb→aXB, k3a→3ka) — red before (audit values reproduced: axX/axXB/kk3a), green after; full suite unchanged vs a pristine origin/main worktree baseline (same single pre-existing test_api::test_transliterate_known_vector failure on both; that suite is skipped in CI — SKIP_PYTHON=1 — and is unrelated to G1; INTERSCRIPT_MAPS_IR with the extracted sha256-verified 3.2.0 artifact does not change it)
  • ruby: the WO80 spec that pinned the accidental NoMethodError is rewritten to the named rejection; new specs pin max_length and the ref-free fallback pass — red before (NoMethodError reproduced), green after; full SKIP_JS=1 SKIP_PYTHON=1 bundle exec rake 411 examples, 0 failures, 11 env-gated pending; standardrb clean
  • maps corpus: no corpus map has capture/ref inside a parallel block except to-position captures (literal, offset-immune), so golden rows are unaffected

…rallel fails named

WO18 G1 (maps parity audit): the tree path rejects captures from a
parallel context by design, but the megaregexp fallback crashed with an
accidental NoMethodError (undefined method 'max_length') because the
deterministic parallel sort calls Sub#max_length on every item class
and Item::CaptureGroup/CaptureRef did not define one.

Define both (mirroring the rs/ts runtime: a capture's length is its
innards' length, a backreference counts 1) so the sort terminates and
the intended SystemConversionError surfaces instead: "Can't use
CaptureRef in parallel mode". A capture in `from` with a ref-free `to`
now runs the megaregexp fallback and produces output.

The WO80 spec that pinned the accidental NoMethodError is rewritten to
the named rejection; new specs pin max_length and the fallback pass.
WO18 G1 (maps parity audit): parallel_megaregexp wraps every rule in a
named group, and the callback handed the template resolver the GLOBAL
group list — so a ref(1) template resolved to the wrapper's whole
match instead of the rule's first capture (ax -> axX, k3a -> kk3a).

parallel_megaregexp now compiles each rule's pattern on its own to get
its exact numbered-group count and hands the replacement only the
rule-owned slice of the global groups, matching rs interpreter
semantics. The docstrings that codified the shifted numbering are
corrected; probe vectors from the audit battery pin the canonical
outputs (aX / aXB / 3ka).
@ronaldtse
ronaldtse merged commit 096b85a into main Nov 13, 2026
13 checks passed
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.

1 participant