Skip to content

Ruby: methods no longer run to the end of the file after a keyword inside a literal - #3736

Merged
squid-protocol merged 1 commit into
mainfrom
claude/ruby-regex-span-3646
Sep 26, 2026
Merged

squid-protocol merged 1 commit into
mainfrom
claude/ruby-regex-span-3646

Conversation

@squid-protocol

Copy link
Copy Markdown
Owner

Summary

Fixes #3646 (part of #3641). The Ruby slicer (Mode D) pairs def/class/if/do with end. Three kinds of literal leaked a keyword or a delimiter into that count. Each one made a method run to the end of its file and take the following methods' calls: the ruby/call/outside-ts-function shape, 73 false positives.

  1. /…/ regex literals were never shielded. extract_anchors in rails' generator.rb has html.scan(/<p\s+class="footnote"\s+id="([^"]+)/). The class inside the regex opened a scope that no end closed. The issue guessed the unbalanced " was the trigger; bisecting showed it's the keyword, and the quote is harmless. x =~ /if|while|do/ breaks the same way.
    • Fix: _apply_literal_shield has a Ruby regex branch in its single atomic pass, so a string or comment that starts earlier still claims its span.
      • A / opens a regex only in value position: at line start, after ( , = ~ ! & | { [ ; ? :, or after an expression keyword (when, if, unless, return, …). Division (a / b, x/2, reduce(:/)) is never one.
      • The regex must fit on one line, a […] class may contain /, and escapes are atomic.
      • The branch sits after the raw-string branch, so that branch's \1 backreference keeps its group number.
  2. prism cut lines at #{ inside %r{…}. Fixing (1) exposed a second runaway in brew's download_strategy.rb, in fetch, which it had been hiding. Line 475 is url.sub(%r{^https?://#{GitHubPackages::URL_DOMAIN}/}o, "#{domain.chomp("/")}/"). prism's line masker shields only quoted strings, so it read the # of that #{ as a comment and cut the line. The " that followed was left open, and because Ruby strings may span lines, it swallowed every end below.
    • Fix: Ruby's # comment delimiter is now #(?!\{). Outside a recognised string, #{ in Ruby is an interpolation in a literal prism doesn't model, never a comment. Real comments, # {hash} included, are unaffected.
  3. @module = T.let( counted module as an opener (CVSDownloadStrategy#initialize ran to EOF).
    • Fix: the lookbehind on Ruby's openers and closer excludes the @ and $ sigils as well as : and .. @module, @class and $end are variable names, never keywords.

tests/dead_key_audit.py allowlists the new regex group name rx, like the existing heredoc/comment entries.

Type of change

  • Bug fix
  • New feature / language support
  • Parsing or engine logic (gitgalaxy/core/detector.py, language_standards.py, prism.py, or a per-language rule)
  • Docs, tooling, or CI only
  • Other (describe above)

CI checklist

  • python tests/tools/audit_check.py passes (bundles the ruff/mypy/dead-key baseline-gated audits + ruff format --check)
  • python -m pytest tests/ passes
  • Touches parsing/engine logic? python tests/tools/crucible_check.py passes: both fixtures re-blessed; see below
  • Touches parsing/engine logic? python tests/tools/tree_sitter_accuracy_audit.py --all --ci passes: 30 languages, all OK
  • Changes the CLI's output JSON schema? No.

Measurements

call_graph_accuracy.py (Level 1, against tree-sitter), on language-crucible v1.5.0:

ruby before after
precision 92.7% 100.0%
false positives 73 0
recall 92.9% 93.8%
missed calls 71 64
matched functions 338 340

No other language moved. The baseline is regenerated. The 64 misses left are calls inside string interpolation ("#{self.class.name}"), which the engine blanks as literal content (C7).

Fix (1) alone briefly read as a regression (92.1%), because it made fetch visible and so exposed runaway (2). That's why (2) and (3) are in this PR.

Golden masters, scoped diff: 235 differences.

  • Every non-coordinate difference is in three Ruby files: shell/brew/download_strategy.rb, shell/brew/brew.rb and ruby/rails/generator.rb. What moves there is magnitude, LOC, branches, the exposures, function analysis and archetypes.
  • The aggregates that roll those files up move too: the shell/brew and ruby/rails directory totals, Ruby's composition, the archetype distribution, total_loc (+54, the lines prism used to cut at #{ now count as code), and avg_tech_debt in the fourth decimal.
  • The rest are X/Y/Z coordinates. The layout re-solves corpus-wide when any file's mass changes, per CLAUDE.md's scoping note.

keyword-rosetta: rosetta_audit.py finds 51 languages, 0 regressions.

Differential Scan target

language-crucible ruby/rails/generator.rb, shell/brew/download_strategy.rb.

Verification

$ pytest tests/extraction/languages/test_ruby*.py tests/core_engine -k "ruby or prism or shield"  -- 236 passed
$ pytest tests/                                                           -- 11637 passed, 337 skipped
$ python tests/tools/audit_check.py                                       -- all clear
$ python tests/tools/call_graph_accuracy.py --ci --buckets 3              -- OK (ruby 92.7/92.9 -> 100.0/93.8)
$ python tests/tools/tree_sitter_accuracy_audit.py --ci --all             -- 30 languages, all OK
$ python tests/tools/crucible_check.py --mode both --update --yes         -- both updated; diff scoped to 3 ruby files + aggregates + XYZ
$ python tests/tools/rosetta_audit.py --corpus ../keyword-rosetta         -- 51 languages, 0 regressions

New test file: tests/extraction/languages/test_ruby_regex_literal_3646.py. It covers:

  • the rails shape end to end;
  • 8 regex-in-value-position cases, and 4 division decoys;
  • a regex inside a string or comment;
  • the %r{…#{…}…} shape through prism, and a real # comment staying a comment;
  • @module/$end variables;
  • linear-time payloads.

🤖 Generated with Claude Code

https://claude.ai/code/session_01GCpfMCUMpco98QGbsuv2sr


Generated by Claude Code

…al (#3646)

Three ways a keyword or delimiter inside a literal broke Mode-D `def`/`end`
pairing, each running a method to the end of its file and handing it the
next methods' calls:

- `/.../` regex literals were never shielded, so `class` in
  `/<p\s+class="footnote"/` (rails generator.rb) or `if` in `x =~ /if|do/`
  opened a scope. `_apply_literal_shield` blanks a ruby regex opened in value
  position (line start, after `( , = ~ ! & | { [ ; ? :` or an expression
  keyword); division never is one. The issue's guess (a quote inside the
  regex) was not the trigger.
- prism took the `#` of `#{...}` inside `%r{^https?://#{DOMAIN}/}o` for a
  comment, cut the line, and the `"` after it opened a multi-line string
  (brew download_strategy.rb `fetch`). Ruby's `#` delimiter skips `#{`.
- `@module = T.let(` counted `module` as an opener. ruby's opener/closer
  lookbehind now excludes the `@`/`$` sigils.

call_graph_accuracy (ruby): precision 92.7% -> 100.0% (FP 73 -> 0), recall
92.9% -> 93.8%. Goldens: download_strategy.rb, brew.rb, generator.rb and
their aggregates, plus the corpus-wide XYZ re-solve.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01GCpfMCUMpco98QGbsuv2sr
@github-actions

Copy link
Copy Markdown
Contributor

🐦‍⬛ Muninn Security Scan

✅ No security issues found.

🐦‍⬛ Powered by Muninn · Skald Lab

@squid-protocol
squid-protocol merged commit e4512e7 into main Sep 26, 2026
34 checks passed
@squid-protocol
squid-protocol deleted the claude/ruby-regex-span-3646 branch September 26, 2026 13:18
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.

ruby: a method's span runs past its end after a regex literal containing a quote

2 participants