Skip to content

Prism's embedded-language partitioner never fires mid-file: its handshake triggers compile without re.M #2848

Closed

Description

@squid-protocol

Summary

Prism's copy of the handshake registry compiles its triggers without re.M
(prism.py —
re.compile(trigger_config["trigger"], re.I)), while StructuralExtractor's copy compiles the
same patterns with re.I | re.M (detector.py, and #1183's comment says why: "re.M is required
for the ^ anchor to match at the start of any line rather than only the start of the whole file
— without it, a genuine mid-file <script> (the normal case) would never match either"
).

Every registry trigger is ^-anchored (^[ \t]*<script\b, ^[ \t]*<style\b,
^[ \t]*(?:asm!\s*\(|__asm__)), so Prism's _partition_embedded_languages only ever fires on a
file whose very first byte opens the block.
In every real file the embedded segment is never
formed, and the whole file is comment-stripped with the host language's rules.

#1183 fixed exactly this drift in the other direction (detector's copy had lost the ^ anchor);
the re.M half of the pair was never applied to Prism.

Evidence (micro-repro)

Identical <script> block, once at offset 0 and once where it normally lives:

mid = '<html>\n<body>\n<script>\n// eval("nope");\nvar x = 1;\n</script>\n</body>\n'
top = '<script>\n// eval("nope");\nvar x = 1;\n</script>\n'
code_stream keeps the JS comment? comment_stream coding/doc LOC recorded signals
mid-file yes — // eval("nope"); '' 7 / 0 high_risk_execution: 1 ← false positive
offset 0 no // eval("nope"); 3 / 1 —

The commented-out eval( is counted as executable risk in the normal case and correctly ignored
in the degenerate one. html's own block_exclusive <!-- --> rules are what ran over the
JavaScript body.

Impact

  • Comment/code stream misattribution. On the pinned language-crucible corpus, 12 of the 38
    html files carry 55 JavaScript // lines that survive into code_stream. doc_loc is
    derived as total_active_lines - coding_loc, so each one is counted as code and subtracted
    from documentation. Worst: vscode_webviews/webWorkerExtensionHostIframe.html (14),
    revealjs_decks/math.html (10), revealjs_decks/demo.html (8).
  • False signals from commented-out code, as above — the detector's own partition is correct,
    but it runs over a code_stream that Prism never cleaned.
  • Affects any host language with a line-anchored handshake: html/php <script>/<style>, and
    Rust/C asm!(/__asm__.

Not blocking anything today: it is orthogonal to #2549 (which is about which segment the tag
belongs to, in a partitioner that does run), and the fix is a corpus-wide re-bless of its own —
which is why it is filed rather than folded into that PR.

Fix shape

Add re.M to Prism's trigger compilation so the two copies agree, or better, derive both from one
place the way #1183 made the detector derive its registry from LENS_CONFIG — the two partitioners
have now drifted twice on the same three patterns. Then re-bless both golden masters; expect the
diff to be concentrated in Coding LOC/Documentation LOC and in signals that were reading
commented-out embedded code.

References

Activity

  1. added
    core-engine
    on Sep 7, 2026
  2. squid-protocol commented on Sep 10, 2026

    @squid-protocol
    OwnerAuthor

    PR #2943 open (CI running). Two notes beyond the issue as filed:

    A third copy. language_lens.py compiles the same registry too, also with re.I alone — so _detect_hybrids only ever reported a polyglot lang_mix for the degenerate offset-0 file. The fix takes the issue's "better" option (one source): _lens_config.COMPILED_HANDSHAKE_REGISTRY, shared by all three, with a test pinning the identity so a fourth copy cannot appear.

    No corpus re-bless is owed. The issue expected one; rosetta_audit reads 46/46 with 0 regressions. The manifests gate planted signal counts, and none move — the redistribution is entirely in derived LOC columns, which live in the bias report and regenerate on their own schedule. Reachability scan over both corpora: rosetta has mid-file triggers in html only; the crucible adds groovy, php, lua, c and zig.

    Golden diff is 194 substantive cells across html (101) and groovy (36): 11 html files move 64 JavaScript // lines out of Coding LOC into Documentation LOC, conserved per file. The groovy case is worth one reviewer's look and is described in the PR — gradle's EnrichedReportRenderer emits injected JavaScript from a triple-quoted string, and its ~61 lines of real JS logic had been recorded as documentation.

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

    core-engine

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions