Repository navigation
fix(core): prism.py loses def/comment lines after a backslash-continued string (ruby #{}, closes #1271) - #1276
Merged
squid-protocol merged 2 commits intoAug 11, 2026
Conversation
…ent stripper (#1271) Root cause was two independent, compounding bugs in prism.py's Ruby/ Python/micropython comment-and-string stripping, both stemming from the same blind spot: neither recognized a backslash-newline-continued string literal (legal in both languages) as a single multi-line span. 1. `_strip_python_docstrings`'s plain-string alternatives used `\\.` (can't match a literal newline without re.DOTALL on the whole pattern), so a backslash-continued string never matched as one atomic span. The regex then fell through to the bare-comment alternative for the continuation line(s), which -- for Ruby specifically -- can false-match a `#{...}` string-interpolation opener as if it were a real comment. Fixed by widening to `\\[\s\S]`, which explicitly matches a newline as part of an escaped character regardless of the DOTALL flag. 2. Even with that fixed, a SECOND pass (`_strip_single_line_comments`, shared by every "line_exclusive" language, python/ruby included) independently reintroduced the identical corruption: it processes text strictly per-line with no memory of a string opened on a previous line, so the continuation line(s) look like fresh code to it too. Fixed by carrying the open quote character forward across lines, gated behind `lang_id in ("python", "micropython", "ruby")` -- confirmed unsafe to widen to the rest of the ~20-language "line_exclusive" family: Perl's `y///`/`tr///`/`s///`/`m//` quote-like operators can contain a bare `"`/`'` that is NOT a string delimiter at all (e.g. `y/"//d`), and an ungated carry falsely opened on one and swallowed hundreds of subsequent lines of real Perl code before an unrelated later quote happened to close it (confirmed via language-crucible's perl/mojo/Template.pm -- verified byte-for-byte IDENTICAL to before this fix once gated). Both fixes are additive/narrowing (they can only prevent a previously- exposed delimiter-shaped token from being reached, never expose new content that was previously hidden), and #1184's original hazard (a stray quote inside a real COMMENT mis-pairing with a distant string) is provably not reintroduced: the carry can only ever open from the code portion of a line, split off before the comment portion is even considered. Results (language-crucible corpus, tree_sitter_accuracy_audit.py): - ruby: found_functions 111 -> 117/117 (100% recall, was 0/117 before #1262+#1271 together), args_exact_match 80 -> 85. - python: found_functions 2940 -> 2966/2976, args_exact_match 1859 -> 1868 -- python permits the same backslash-continuation shape #1271's own investigation predicted this would affect, now confirmed and fixed as a side effect of the same gated carry logic. Golden master fixtures re-blessed via crucible_check.py (both full-precision and zero-dependency modes, 349 differences each). Structurally verified (isolating every non-topological-coordinate, non-ruby, non-python, non-shell/brew diff) that the ripple is confined to: ruby/python/shell-brew's own per-file metrics (shell/brew ships embedded .rb files, a known confinement precedent from #1262), and corpus-wide derived aggregates (topological layout, ecosystem health averages) that naturally recompute from those per-file changes. Co-Authored-By: Claude Sonnet 5 <[email protected]>
Contributor
…AIL_PATTERN CodeQL flagged the pattern this PR introduced for exponential worst-case backtracking: its "plain char" alternative (`(?!\1).`) didn't exclude the backslash, so a run of `\x` pairs before a blocking character could be partitioned ambiguously between "one \\. escape" and "two separate single-char matches" -- confirmed empirically, a 26-char adversarial payload alone took >8s. A first attempt at fixing this by excluding the backslash from that alternative (matching SHIELD_PATTERN's own established-safe idiom) closed the ReDoS hole but silently broke the one case that actually matters: a line ending in a bare trailing backslash (the real line- continuation shape this exists to catch, e.g. Ruby's `"...text \`) has no partner character for `\\.` to pair with and isn't matchable by an alternative that excludes bare backslashes either -- regressed ruby's tree_sitter_accuracy_audit found_functions from 117/117 back to 111/117, caught before push. Replaced the whole escape-aware pattern with a bare character class (`["'`]`) instead: by the time `_mask_line_literals` hands back `code_part`, every COMPLETE quoted literal has already been consumed into a `__MASK_N__` placeholder, so any raw quote character still present is -- by construction of the pipeline -- not part of any complete pair. No escape-parsing is needed to prove that; finding its position is enough to know a literal opened there and never closed on this line. A fixed single-char-class match cannot backtrack at all. Re-verified: ruby back to 117/117 (tree_sitter_accuracy_audit), the adversarial payload that took >8s now resolves in ~3 microseconds, perl/mojo/Template.pm still byte-identical to pre-#1271 output, and both crucible_check.py modes PASS against the already-committed golden masters with zero diff (this is a pure implementation fix -- output is unchanged from what #1271 already blessed, just computed safely). Co-Authored-By: Claude Sonnet 5 <[email protected]>
squid-protocol
deleted the
fix/prism-backslash-continued-string-interpolation-1271
branch
August 11, 2026 21:31
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1271. Root cause was two independent, compounding bugs in
prism.py's Ruby/Python/micropython comment-and-string stripping, bothstemming from the same blind spot: neither recognized a backslash-newline-
continued string literal (legal in both languages) as a single multi-line
span.
_strip_python_docstrings(step 1, shared by python/micropython/ruby): its plain-string alternatives used
\\., which can't match aliteral newline without
re.DOTALLon the whole pattern. A backslash-continued string never matched as one atomic span, so the regex fell
through to the bare-comment alternative for the continuation line(s) --
which, for Ruby specifically, can false-match a
#{...}string-interpolation opener as if it were a real
#comment. Fixed bywidening to
\\[\s\S]._strip_single_line_comments(step 2, shared by every "line_exclusive" language -- ~20, including python/ruby): even with (1)
fixed, this SECOND pass independently reintroduced the identical
corruption, since it processes text strictly per-line with zero memory
of a string opened on a previous line. Fixed by carrying the open quote
character forward across lines -- gated to
lang_id in ("python", "micropython", "ruby"), not applied to therest of the "line_exclusive" family.
That gate isn't cosmetic -- it's load-bearing. Perl's
y////tr////s////m//quote-like operators can contain a bare"/'that isnot a string delimiter at all (
y/"//d, a real idiom thattransliterates away literal double-quotes). An earlier, ungated version
of this fix false-opened a carry on exactly that shape and swallowed
~400 lines of real Perl code (every comment in between) before an
unrelated later quote happened to close it -- caught via
crucible_check.pyagainstperl/mojo/Template.pm, not by any hand-written test. Verified the gated version produces byte-for-byte
identical output to pre-fix
prism.pyfor that file.Contiguous 'dead zones' of real functions silently lost from extraction on production files, independent of mid-file language drift #1184's original hazard (a stray quote inside a real comment
mis-pairing with a distant string) is provably not reintroduced either:
the carry can only ever open from the code portion of a line, split
off before the comment portion is even considered -- see the new
test_prism_line_exclusive_carry_over_does_not_reintroduce_1184test.Results (
tree_sitter_accuracy_audit.py, language-crucible corpus)Ruby's remaining 6/117 gap after #1262 traces exactly to this bug (this
PR closes it entirely: 111 -> 117). Python wasn't in #1271's original
scope but the issue explicitly predicted it ("Python also permits
backslash-continued string literals... untested") -- confirmed and fixed
as a side effect of the same gated carry logic. No language outside the
gate moved at all (verified perl/mojo/Template.pm byte-identical).
Golden master fixtures
Re-blessed via
crucible_check.py --mode both --update --yes(349differences in each mode -- both full-precision and zero-dependency).
Structurally verified (isolating every non-topological-coordinate,
non-ruby, non-python, non-shell/brew diff -- 16 remaining lines) that the
ripple is confined to:
embedded
.rbfiles, a known confinement precedent from [TIER 1] Ruby func_start matches 0/117 real methods in language-crucible corpus #1262).health averages) that naturally recompute once those per-file inputs
change -- same shape as [TIER 1] Ruby func_start matches 0/117 real methods in language-crucible corpus #1262's own confinement check.
Test plan
pytest tests/core_engine/test_prism.py-- 40 passed, including twonew regression tests: the exact
#{...}-after-backslash-continuationrepro, and a Contiguous 'dead zones' of real functions silently lost from extraction on production files, independent of mid-file language drift #1184-non-regression check
pytest tests/core_engine/test_detector.py tests/extraction/-- 6210passed, 2 skipped, 10 xfailed, 3 xpassed
pytest tests/(excludingtests/extraction, opt-ingolden_crucible) -- 762 passedpython tests/tools/audit_check.py-- ruff/mypy/dead-key/ast-accuracyall clear
python tests/tools/crucible_check.py --mode both-- both PASS afterre-blessing the golden masters
python tests/tools/tree_sitter_accuracy_audit.py --lang ruby --regenerate/--lang python --regenerate/--summary-table--baselines + summary table updated, no regressions
_strip_python_docstringsand_strip_single_line_commentsin isolation) that each of the twofixes is independently correct and that the Perl-gate gap-fix
produces byte-identical output to pre-fix
prism.py🤖 Generated with Claude Code