Skip to content

Mid-file language-switching (_partition_segments) can be permanently misled by a string literal that merely contains embedded-language delimiter text #1183

Closed

Description

@squid-protocol

What

StructuralExtractor._partition_segments() (the mid-file/embedded-language-switching logic
described in gitgalaxy/core/README.md) can be false-triggered by an ordinary string or regex
literal that contains embedded-language delimiter text as data, not actual embedded code —
and once triggered, every function defined in the rest of the file is misrouted to the wrong
language's rules and silently lost from extraction, for the remainder of the file.

Concrete repro

tests/core_engine/test_prism.py contains, as ordinary Python test fixture data (around line
258), a dict literal describing an embedded-JS trigger config:

"start": re.compile(r"<script>", re.I),
"end": re.compile(r"</script>", re.I),
"target": "javascript",

This is Python source testing the embedded-language detector — the <script> text is a
regex pattern used as test data, not real embedded JavaScript. But _partition_segments reads
it as a real trigger and flips the rest of the file into a javascript segment:

$ python3 -c "... ext._partition_segments(code_stream, 'python') ..."
num segments: 4
  lang='python'     offset=0     len=9672  preview='import re\nfrom unittest.mock import patch...'
  lang='javascript'  offset=258   len=59    preview='<script>", re.I),\n  "end": re.compile(r"</script>'
  lang='python'      offset=259   len=318   preview='", re.I),\n  "target": "javascript",\n  "p'
  lang='javascript'  offset=272   len=23840 preview='<script>let x = 1; // js"\n    res1 = prism_engine.split_stre'

The last segment — 23,840 characters, everything from roughly line 260 to the end of the file
— is now parsed under JavaScript's func_start rule instead of Python's. Every real Python
def test_... in that range vanishes from extraction: 39 real functions in the file (per
ast.parse ground truth) vs. 8 extracted.

Impact

Measured against GitGalaxy's own 228-file Python self-scan corpus: 11 of 228 files
(gitgalaxy/core/detector.py, gitgalaxy/standards/language_standards.py,
tests/core_engine/test_detector.py, tests/core_engine/test_language_lens.py,
tests/core_engine/test_prism.py, tests/extraction/languages/test_html.py,
tests/extraction/languages/test_html_strict.py,
tests/extraction/languages/test_objectivec_strict.py,
tests/extraction/languages/test_rust_strict.py,
tests/extraction/test_dependency_extraction.py,
tests/extraction/test_function_extraction.py) trigger this. Function recall on those files
drops from a ~63% baseline (see #1183 / the scope-loss issue) down to ~27%.

This isn't limited to test fixtures that embed language delimiters as data — any real file in
any language that happens to contain a string constant resembling another language's opening
delimiter (a URL, a code sample in a docstring, an error message quoting a tag) is a candidate
trigger.

Suggested fix

_partition_segments's embedded-trigger detection needs to be more conservative about what
counts as a real language switch — at minimum, requiring the trigger to appear outside a string
literal already shielded by prism.py's own literal-shielding pass (so test/data usages of
delimiter-shaped text don't count), and/or requiring some corroborating evidence of real
embedded syntax (e.g. an actual matching close-tag and plausible JS-shaped content in between,
not just the open-tag substring existing anywhere in scanned text).

How this was found

Diffing GitGalaxy's own self-scan of its own repo against ast-module ground truth on the same
files, then confirmed by direct instrumented calls to _partition_segments() on the actual
file content — not inferred from the output diff alone.

Activity

  1. added
    bug
    core-engine
    priority: high
    on Aug 9, 2026
  2. added this to the v2.4.8 milestone on Aug 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcore-enginepriority: high

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions