Skip to content

test(markdown): bound the parser's searches by count, not by CPU time - #2393

Merged
devswha merged 1 commit into
mainfrom
test/markdown-search-counts
Oct 29, 2026
Merged

devswha merged 1 commit into
mainfrom
test/markdown-search-counts

Conversation

@devswha

@devswha devswha commented Oct 29, 2026

Copy link
Copy Markdown
Owner

Found by the flake-hunt lane: src/lib/markdown.test.ts "text no one wrote by hand > looks once for the end of a formula that never closes" and "... looks once for what closes a bracket, an inline formula or an underscore" failed in Fast checks twice in the 417 CI runs from 2026-10-28 03:26Z to 2026-10-29 18:22Z. Each time the linear() CPU-time ratio came out just over its bound (30.7 and 33.7 against < 30), after #1974 and #2101 had already tuned the clock and the sampling. A rerun passed.

Cause (test flake)

Both tests proved the parser linear by timing it: a small and a 10x input, comparing thread CPU time. CPU time still moves with load (allocation and GC on the test's own thread), so a linear parser could cross the bound on a busy runner. Raising the bound or taking more samples would make it rarer, not load-independent.

Change

The two tests now count the work they bound, instead of timing it. Their output assertions stay.

  • Unclosed display formula: the characters every indexOf("\\]") search reads, from its start position to the match or the end, must stay within the text's length plus a few per line. One search per opener over the rest of the text is quadratic in characters, even when it makes no more calls.
  • Inline closers: the characters scanned by the forward-only opener and closer searches are summed against nine times the text length, and the unbounded token pattern is refused.

Both spies wrap the real String.prototype methods and are restored in finally. Other tests in the file still use linear(); they are left as they are.

Proof

  • Fail-first against the historical parser (before the "looks once" fixes): both new checks fail, with 3202 searches over the cap and one forbidden unbounded search.
  • Character count: a scratch rewrite searches the whole remaining text once per \[ line. The new check fails it, with 28791 characters read against a cap of 22404.
  • Current parser: the file passed 10/10 runs (48/48 each), then 3/3 after the review change.
  • Local checks: bun run check fast ok.

Test-only.

Reviews

  • omo-native-code-reviewer: APPROVE (WATCH).
    • MEDIUM, fixed: the display-formula check counted indexOf calls, not characters. It now sums the span each search reads.
    • MEDIUM, kept: the inline check's list of allowed searches copies the parser's regex sources. A harmless edit to one of them fails the test loudly, which costs maintenance, not correctness.
    • LOW: the display cap had no slack. It now allows a few characters per line.
  • Test-only, so no Codex review (notes/pr-policy.md).

The elapsed CPU ratio occasionally crossed its bound under shared-runner load even after clock and sampling tuning. Count display-math indexOf searches directly, and bound inline closer work by the characters scanned while rejecting the old unbounded token regex.

Fail-first restored the pre-optimization paths: display math failed at 3202 searches against a 3201 cap, and inline parsing attempted one forbidden unbounded search. The current parser passed 10/10 full markdown.test.ts runs, 48 tests each, plus bun run check fast.

Co-authored-by: sisyphus-dev-ai <[email protected]>
@coderabbitai

coderabbitai Bot commented Oct 29, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 33 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 4 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration
Commits

Reviewing files that changed from the base of the PR and between 1db9c72 and b67a3ba.

Files selected for processing: 2
  • changelog.d/markdown-search-counts.md
  • src/lib/markdown.test.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@devswha

devswha commented Oct 29, 2026

Copy link
Copy Markdown
Owner Author

Merging b67a3ba. Test-only: src/lib/markdown.test.ts bounds the parser's worst-case searches by counting them instead of timing CPU, so a loaded runner cannot fail it. Diff read by the merge pass; no CodeRabbit review needed.

@devswha
devswha merged commit 3a65b1b into main Oct 29, 2026
11 checks passed
@devswha
devswha deleted the test/markdown-search-counts branch October 29, 2026 22:05
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.

1 participant