Skip to content

Extraction hardening: typescript (#815) - #862

Merged
squid-protocol merged 3 commits into
mainfrom
fix/815-typescript-extraction-hardening
Jul 30, 2026
Merged

squid-protocol merged 3 commits into
mainfrom
fix/815-typescript-extraction-hardening

Conversation

@squid-protocol

Copy link
Copy Markdown
Owner

Summary

Closes #815 (part of epic #813). Hardens all four extraction gauntlets for typescript (func_start, args, class_start, _dependency_capture) per the epic's methodology, and fixes 3 real bugs found via empirical verification of ~40 new valid/invalid/pathological cases:

  1. func_start: a type Foo = (a: T) => R; type alias false-positive-matched as a real arrow-function assignment (both share the identical IDENT = (...) => ... surface shape). Fixed with a fixed-width negative lookbehind for the type prefix.
  2. func_start: the assignment alternative required the identifier to be followed directly by =, missing the extremely common idiomatic pattern of an explicit type annotation between the two (const Foo: React.FC<Props> = ({ a, b }) => {). Added a bounded, ReDoS-safe optional type-annotation skip before the = check.
  3. class_start: the generic step-over between the class name and the extends/implements check was flat (<[^>]*>), truncating at the FIRST > — a one-level-nested generic bound (class Foo<T extends Comparable<T>> extends Bar {) silently lost the ENTIRE extends/implements capture even though the class name itself still matched fine. Widened to the established one-level-nesting idiom (Rule 11).
  4. _dependency_capture: side-effect-only imports (import "./styles.css";, extremely common for CSS/polyfill imports) have no from keyword and no parens — neither original alternative matched them. Added a third alternative for bare import "path";.

Two real ambiguities were investigated and deliberately not fixed, per the same judgment call already made for csharp (#789): a bare call statement at true line start (e.g. a Jest it('...', () => {...}) block) is structurally indistinguishable from a bare class-member method signature to a single-pass regex with no scope tracking. Fixing it would need scope-awareness this engine doesn't have, or a terminator requirement that would reintroduce the cross-language "extraction gauntlet expects bare fragments to match" conflict #789 hit. Documented as known limitations with dedicated tests, not silently ignored — same for 2+-level generic nesting and a function-type annotation containing its own =>.

New infrastructure

  • tests/extraction/_extraction_harness.py: shared assertion helpers lifted from the four original monolithic gauntlets, so per-language files don't duplicate the valid/invalid/pathological logic four times each.
  • tests/extraction/languages/test_typescript.py: the first per-language file per the epic's new layout (see tests/extraction/how_to_harden_extraction.md), covering all four rules for typescript in one place. typescript's entries removed (cutover, not duplicated) from the four old monolithic dict files.

Verification

  • All new/changed patterns ReDoS-swept (linear scaling confirmed at n up to 32k-100k).
  • crucible_check.py: real diff confirmed confined entirely to typescript files (no off-target ripple to any other language) before blessing.
  • Full core_engine/extraction suites pass (3284 passed; only the pre-existing, unrelated tiktoken-dependency environment failure).
  • ruff format/ruff_audit.py --ci/mypy_audit.py --ci/dead_key_audit.py --ci: clean.

Test plan

  • 3 real bugs fixed and regression-tested
  • 2 known limitations documented with dedicated tests (not silently ignored)
  • ReDoS sweep for every modified pattern
  • Migrated to the new per-language file layout
  • crucible_check.py clean, diff confirmed on-target
  • CI green

squid-protocol and others added 2 commits July 30, 2026 18:45
Hardens all four extraction gauntlets for typescript (func_start, args,
class_start, _dependency_capture) per the epic #813 methodology, and
fixes 3 real bugs found via empirical verification of ~40 new
valid/invalid/pathological cases:

1. func_start: a `type Foo = (a: T) => R;` type alias false-positive-
   matched as a real arrow-function assignment (both share the identical
   `IDENT = (...) => ...` surface shape). Fixed with a fixed-width
   negative lookbehind for the `type ` prefix.

2. func_start: the assignment alternative required the identifier to be
   followed directly by `=`, missing the extremely common idiomatic
   pattern of an explicit type annotation between the two (
   `const Foo: React.FC<Props> = ({ a, b }) => {`). Added a bounded,
   ReDoS-safe optional type-annotation skip before the `=` check.

3. class_start: the generic step-over between the class name and the
   extends/implements check was flat (`<[^>]*>`), truncating at the
   FIRST `>` -- a one-level-nested generic bound (`class Foo<T extends
   Comparable<T>> extends Bar {`, a realistic bounded-generic pattern)
   silently lost the ENTIRE extends/implements capture even though the
   class name itself still matched fine. Widened to the established
   one-level-nesting idiom used elsewhere in this file (Rule 11).

4. _dependency_capture: side-effect-only imports (`import "./styles.css";`,
   extremely common for CSS/polyfill imports) have no `from` keyword and
   no parens -- neither original alternative matched them, so this entire
   common import style produced zero dependency-graph edges. Added a
   third alternative for bare `import "path";`.

Two real ambiguities were investigated and deliberately NOT fixed, per
the same judgment call already made for csharp (#789): a bare call
statement at true line start (e.g. a Jest `it('...', () => {...})`
block) is structurally indistinguishable from a bare class-member method
signature to a single-pass regex with no scope tracking -- fixing this
would need either scope-awareness this engine doesn't have, or a
terminator requirement that would reintroduce the cross-language
"extraction gauntlet expects bare fragments to match" conflict #789 hit.
Documented as known limitations with dedicated tests, not silently
ignored. Also documented (not fixed): 2+-level generic nesting (the
one-level-nesting idiom's actual supported depth) and a function-type
annotation containing its own `=>` before the real assignment.

Also:
- New tests/extraction/_extraction_harness.py: shared assertion helpers
  lifted from the four original monolithic gauntlets, so per-language
  files don't duplicate the valid/invalid/pathological assertion logic
  four times each.
- New tests/extraction/languages/test_typescript.py: the first per-
  language file per the epic's new layout, covering all four rules for
  typescript in one place. typescript's entries removed (cutover, not
  duplicated) from the four old monolithic dict files.

Verification: all new/changed patterns ReDoS-swept (linear scaling
confirmed); crucible_check.py real diff confirmed confined entirely to
typescript files (no off-target ripple to any other language) before
blessing; full core_engine/extraction suites pass (3284 passed, only the
pre-existing unrelated tiktoken-dependency environment failure);
ruff/mypy/dead-key audits clean.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Adds 4 new recurring-bug-class entries found while hardening
typescript's extraction gauntlets: identifier-then-optional-type-
annotation-then-operator, Rule 11 applying to class_start's
extends/implements clause (not just args/return-types), the
bare-call-vs-bare-definition ambiguity as a documented accepted
limitation (not something to keep re-attempting), and the
crucible zero-diff interpretation note. Mirrors the same update
already pushed to the epic issue (#813).

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

🐦‍⬛ Muninn Security Scan

✅ No security issues found.

🐦‍⬛ Powered by Muninn · Skald Lab

…worked locally by accident

CI failed with "ModuleNotFoundError: No module named 'tests'" on
test_typescript.py's `from tests.extraction._extraction_harness import
...` -- this repo's tests/ tree has no __init__.py anywhere, so that
dotted import only worked in local dev because `python -m pytest` (what
I ran locally) prepends the current working directory to sys.path.
CI's smoke-test job invokes the `pytest` console script directly, which
doesn't do that.

Fixed by having test_typescript.py insert its own parent directory
(tests/extraction/) onto sys.path before importing _extraction_harness
as a plain top-level module, instead of relying on ambient CWD/pytest
invocation behavior. Verified by running `pytest` (not `python -m
pytest`) from an unrelated CWD (/tmp), matching how CI invokes it.

Also moved assert_redos_immune into _extraction_harness.py itself
(duplicated from test_language_standards_strict.py's identical helper,
deliberately, to avoid a second cross-test-module import of the same
fragile shape) rather than importing it from a 14,000-line unrelated
test module three separate times.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@squid-protocol
squid-protocol merged commit b742eeb into main Jul 30, 2026
27 checks passed
@squid-protocol
squid-protocol deleted the fix/815-typescript-extraction-hardening branch July 30, 2026 23:03
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.

Extraction hardening: typescript

1 participant