Repository navigation
fix(tests): bound the markdown parser's growth, not its milliseconds - #1974
Merged
Merged
Conversation
The "text no one wrote by hand" cases asserted each adversarial parse took under 1000 ms of wall clock, which GitHub-hosted runners now miss with no slowdown in the parser (#1901). Each case now checks that ten times the input takes under thirty times the CPU time (linear is ten, the quadratic search it guards against a hundred), with both inputs warmed and the small one repeated to 2 ms. The KaTeX case asserts with a spy that the formula nested too deep is never handed to KaTeX instead of timing it. Fixes #1901
|
Warning Review limit reachedYou'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 5 minutes. View limit details
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. Comment |
Owner
Author
|
Merge pass review report. Overall: PASSED. This PR changes only
|
This was referenced Oct 26, 2026
Merged
devswha
added a commit
that referenced
this pull request
Oct 29, 2026
…#2393) 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). Co-authored-by: sisyphus-dev-ai <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Change
Fixes #1901.
src/lib/markdown.test.ts("text no one wrote by hand") guarded the parser againstquadratic input with
within(1000, run): a 1000 ms budget ofperformance.now(). On GitHub-hostedrunners the linear parse alone took 1100-1560 ms, so Fast checks failed on main and on PRs that do
not touch markdown, release CI included.
The budget is replaced by
linear(text, size, parse), which measures how the time grows ratherthan how long it takes: the same parse on a tenth of the input and on the whole. Ten times the text
must take under thirty times as long; linear is about ten, the search of the rest of the line from
every character these cases guard against is about a hundred. It counts the process's CPU time
(
process.cpuUsage()), not the clock: on a busy runner the longer run is preempted while the shortone fits in a time slice, so a wall-clock ratio still flaked (see Validation). Both inputs are
parsed once untimed first, a parse of microseconds is repeated until a run takes 2 ms, and the best
of up to three rounds counts. Every assertion on what the parser returns is kept.
The KaTeX case is no longer timed: a spy checks that KaTeX is never given the formula nested too
deep, and is given the one beside it.
Inputs are smaller than before (the ratio needs no megabyte to show a quadratic), so the file runs
in about the same time. Test-only: no product code changes, so no changelog fragment.
Validation
taskset -c 5,--rerun-each 30): a wall-clock ratio failed192 of 960 runs (ratios 30-57); the CPU-time ratio passed 960 of 960.
bun test src/lib/markdown.test.ts20 times in a row underdori heavy: 20/20 pass.bun run check fast: pass, except five tests inserver/legacy-env.test.ts, which failed withEMFILE on
watchbecause this host is at its inotify instance limit. That file is not touchedhere, and every failure was that EMFILE; CI runs it.
inlineMarkstrying the whole pattern from every opener: ratio 97.9, fails.isTableSeparatorback to the one regular expression: ratio 99.2, fails.\[searched again from every line: ratio 89.6, fails.mathNestsTooDeepdropped from the KaTeX path: the spy fails.