Skip to content

fix(tests): bound the markdown parser's growth, not its milliseconds - #1974

Merged
devswha merged 1 commit into
mainfrom
fix/markdown-test-wall-clock
Oct 26, 2026
Merged

devswha merged 1 commit into
mainfrom
fix/markdown-test-wall-clock

Conversation

@devswha

@devswha devswha commented Oct 26, 2026

Copy link
Copy Markdown
Owner

Change

Fixes #1901. src/lib/markdown.test.ts ("text no one wrote by hand") guarded the parser against
quadratic input with within(1000, run): a 1000 ms budget of performance.now(). On GitHub-hosted
runners 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 rather
than 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 short
one 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

  • Four copies of the group on one CPU (taskset -c 5, --rerun-each 30): a wall-clock ratio failed
    192 of 960 runs (ratios 30-57); the CPU-time ratio passed 960 of 960.
  • bun test src/lib/markdown.test.ts 20 times in a row under dori heavy: 20/20 pass.
  • bun run check fast: pass, except five tests in server/legacy-env.test.ts, which failed with
    EMFILE on watch because this host is at its inotify instance limit. That file is not touched
    here, and every failure was that EMFILE; CI runs it.
  • Each guard fails when the bug it names comes back (the source restored after each):
    • inlineMarks trying the whole pattern from every opener: ratio 97.9, fails.
    • isTableSeparator back to the one regular expression: ratio 99.2, fails.
    • a list item copied at every line: fails.
    • an unclosed \[ searched again from every line: ratio 89.6, fails.
    • mathNestsTooDeep dropped from the KaTeX path: the spy fails.
  • Codex Sol review: the contention finding above came from it; fixed and re-checked.

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
@coderabbitai

coderabbitai Bot commented Oct 26, 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 5 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 7ab4bde and 51702ea.

Files selected for processing: 1
  • 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 26, 2026

Copy link
Copy Markdown
Owner Author

Merge pass review report. Overall: PASSED. This PR changes only src/lib/markdown.test.ts and fixes #1901. Being test-only, it was reviewed by omo-native-code-reviewer (claude-opus-5-5, PASS) and checked by me, with CI as the final check.

  • What changed: the 1000 ms wall-clock budget is replaced by linear(). It compares process CPU time on a tenth of the input with the whole input, and asserts the ratio is under 30. Linear parsing measures about 10, and the regressions these cases guard against measure about 100.
  • Still catches regressions:
    • My mutation made trimUrl quadratic, and the test failed at a ratio of 39.5.
    • The reviewer's mutation restored the copy-at-every-line bug (markdown.ts:311), and the test failed at 86.6.
  • No new flakiness:
    • The test passed 15/15 here under load. The reviewer's runs measured linear cases between 3.1 and 19.9.
    • It reads no wall clock and has no sleeps. The calibration loop ends after a few doublings.
    • conventions.test.ts passes 19/19, and typecheck is clean.
  • Assertions: every result asserted before is still asserted. The KaTeX case now also checks that renderToString sees only \frac{1}{2}.
  • LOW, not fixed in this PR:
    • Taking the best of three rounds can let a borderline quadratic slip through. It can't make a correct parser fail.
    • A real regression takes longer to fail.
    • Windows has coarse CPU-time granularity, but Windows CI doesn't run this file.

@devswha
devswha merged commit 4d2e360 into main Oct 26, 2026
11 checks passed
@devswha
devswha deleted the fix/markdown-test-wall-clock branch October 26, 2026 09:36
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]>
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.

Flaky: markdown.test.ts measures the parser against 1000 ms of wall clock

1 participant