Skip to content

fix(core): prism.py loses def/comment lines after a backslash-continued string (ruby #{}, closes #1271) - #1276

Merged
squid-protocol merged 2 commits into
mainfrom
fix/prism-backslash-continued-string-interpolation-1271
Aug 11, 2026
Merged

squid-protocol merged 2 commits into
mainfrom
fix/prism-backslash-continued-string-interpolation-1271

Conversation

@squid-protocol

Copy link
Copy Markdown
Owner

Summary

Fixes #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 (step 1, shared by python/micropython/
    ruby): its plain-string alternatives used \\., which can't match a
    literal newline without re.DOTALL on 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 by
    widening to \\[\s\S].

  2. _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 the
    rest 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 is
    not a string delimiter at all (y/"//d, a real idiom that
    transliterates 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.py against perl/mojo/Template.pm, not by any hand-
    written test. Verified the gated version produces byte-for-byte
    identical output to pre-fix prism.py for 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_1184 test.

Results (tree_sitter_accuracy_audit.py, language-crucible corpus)

lang metric before after
ruby found_functions 111/117 117/117 (100%)
ruby args_exact_match 80 85
python found_functions 2940/2976 2966/2976
python args_exact_match 1859 1868

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 (349
differences 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:

Test plan

  • pytest tests/core_engine/test_prism.py -- 40 passed, including two
    new regression tests: the exact #{...}-after-backslash-continuation
    repro, 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/ -- 6210
    passed, 2 skipped, 10 xfailed, 3 xpassed
  • pytest tests/ (excluding tests/extraction, opt-in
    golden_crucible) -- 762 passed
  • python tests/tools/audit_check.py -- ruff/mypy/dead-key/ast-accuracy
    all clear
  • python tests/tools/crucible_check.py --mode both -- both PASS after
    re-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
  • Manually verified (bisecting old/new _strip_python_docstrings and
    _strip_single_line_comments in isolation) that each of the two
    fixes is independently correct and that the Perl-gate gap-fix
    produces byte-identical output to pre-fix prism.py

🤖 Generated with Claude Code

…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]>
Comment thread gitgalaxy/core/prism.py Fixed
@github-actions

Copy link
Copy Markdown
Contributor

🐦‍⬛ Muninn Security Scan

✅ No security issues found.

🐦‍⬛ Powered by Muninn · Skald Lab

…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
squid-protocol merged commit 3db4aeb into main Aug 11, 2026
30 checks passed
@squid-protocol
squid-protocol deleted the fix/prism-backslash-continued-string-interpolation-1271 branch August 11, 2026 21:31
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.

prism.py: Ruby #{...} string interpolation misread as a comment after a backslash-continued string

2 participants