Skip to content

compiler, tex-expansion: bound diagnostic floods from runaway loops (stacked on #577) - #592

Closed
d-q222 wants to merge 24 commits into
mainfrom
agent/daniel-parent/diag-flood-cap
Closed

d-q222 wants to merge 24 commits into
mainfrom
agent/daniel-parent/diag-flood-cap

Conversation

@d-q222

@d-q222 d-q222 commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Stacked on #577 (agent/daniel-parent/compiler-fuzz). Review after it; the diff against main includes #577 until that merges.

What

This handles the three follow-ups in #577's "Not done": diagnostic floods, repeated nesting-limit reports, and 100k-character environment names.

1. Diagnostic floods. Every runaway flood the fuzzer found repeats the same diagnostic at one invocation span. So the fix is mainly dedupe, with a per-code budget as a backstop.

  • tex-expansion (Engine::report):
    • A diagnostic identical to an earlier one (severity, message, span) is recorded once until the engine next reaches a safe point.
    • next_content_token empties the set when it returns at a safe point. Incremental checkpoints are taken only there, and a re-run converges only there, so a restored engine (whose set starts empty) decides exactly as a full run.
    • A runaway loop never returns to a safe point, so each of its messages is recorded once.
  • compiler (diagnostics::limit_repeats, a pure function of the list):
    • Identical repeats (severity, code, span, message) are dropped.
    • After 1000 distinct diagnostics of one code, the rest become one further N similar diagnostics suppressed diagnostic for that code. It has the code, the most severe suppressed severity, and the first suppressed span, and is appended after the kept diagnostics.
    • The fatal stop ("TeX capacity exceeded", the step limit) is always kept.
    • It is applied to the parser's list and to the layout's list separately (so a summary is never re-counted), in clean and incremental compiles alike.
  • compiler parser: unsupported and unsupported_preamble don't build a repeat of a command already reported at the same span. limit_repeats would drop it anyway, but building 1.4M of them was the remaining cost.
  • The fatal stop: the engine already stops expanding at the fatal error (the flood came from diagnostics emitted before it). After the stop, the rest of the document is still typeset unexpanded, as compiler: mutation fuzzer + 13 robustness fixes (panics, stack overflows, hangs, OOM) #577 does.

The dedupe key includes the message, not just (code, span). A macro invocation that uses two different unknown commands reports both at the invocation span with the same code, and (code, span) alone would hide the second. In the fuzz floods the difference is 82 vs 102 distinct diagnostics.

2. Nesting limits. "group nesting limit exceeded" and "conditional nesting limit exceeded" are reported once per excursion past the limit. A flag in the checkpointed State (compared on convergence) is set on the report and cleared when a { or \if is accepted again. The limits and wording are unchanged.

3. Long environment names. \end checks @currenvir part by part instead of rebuilding the name as a String on every \end. Environment names longer than 100 characters are cut to 100 characters plus ... in the engine's messages only. Matching still uses the whole name.

Fuzz harness:

  • Workers report each passing case's diagnostic count and time.
  • The report prints the maximum diagnostics in one case, the number of cases over 1000, and the slowest case.
  • --diagnostics FILE also prints the most frequent messages.
  • --digest compiles every seed unmutated and prints hashes of its pages and diagnostics, to diff two builds.

Why

A flood cost more than the loop that caused it. A mutated microtype fixture produced 3.8M diagnostics (4.9 GB), and the IDE would receive all of them. Each report is still useful once. TeX itself gives up after 100 errors in a paragraph.

Before → after (release, same flags, fuzz_compile --diagnostics, /usr/bin/time -l max RSS)

input before after
fuzz case 18502 (mutated microtype \loop) 4423 ms, 3,815,344 diagnostics, 4877 MB 2640 ms, 102 diagnostics, 1690 MB
fuzz case 10082 (long names, runaway \end) 2679 ms, 9 diagnostics, 3132 MB 2552 ms, 4 diagnostics, 19 MB
\def\a{\n\a}\a 687 ms, 667,415, 603 MB 158 ms, 2, 107 MB
\def\a{\section{\a}}\a in the preamble 10 ms, 19,999, 23 MB 8 ms, 3, 19 MB
\def\a{\bgroup\a}\a 510 ms, 667,469, 467 MB 126 ms, 3, 15 MB
\def\b{\iftrue\b}\b 443 ms, 657,468, 463 MB 119 ms, 2, 3 MB

In 18502 the remaining memory is the 4.3M expanded tokens and 827 pages the loop produces. The remaining time in 10082 is two full 2M-step runs (see Not done).

Fuzz, 50k cases, seed 0xF1A57E40, 5 s watchdog, release with overflow checks and debug assertions, 6 jobs (base and this branch concurrently)

build panics stack overflows hangs >5 s max diagnostics in one case cases with >1000 diagnostics cases ≥1 s
#577 head (c981a00) 0 0 18 2,974,694 (case 13482) 6182 576
this branch 0 0 14 3,109 (case 2473) 3459 561

The hangs share one signature and are slow compiles of deep-nesting and recursion mutations under the watchdog with 12 fuzz processes on a shared machine (first case 8307). The same cases hang on the base. The 3,109 in case 2473 are distinct diagnostics across several codes, within the per-code budget.

Normal documents

fixture_digest is not on this base, so fuzz_compile --digest compared every seed (419 .tex under fixtures/, crates/*/tests, crates/*/oracle) between #577's head and this branch:

  • Pages: identical for all 419.
  • Diagnostics: identical for 370 and different for 49:
    • 48 \loop oracle fixtures (microtype, paragraph-layout emergency): about 14,750 → about 134. These were the per-iteration repeats of the same unsupported commands.
    • amsmath_corpus/20-Vmatrix.tex: 3 → 2. A second Times-Roman has no glyph for '‖' note identical to the first (same span) was dropped.

Overlap check

Test results

On the merge head 9aa0a29 (#577 through d5d308e merged in):

  • cargo test --manifest-path crates/tex-expansion/Cargo.toml: test result: ok. 63 passed; 0 failed (expand_tests), ok. 4 passed; 0 failed (incremental_tests), ok. 2 passed; 0 failed (oracle_tests)
  • cargo test --manifest-path crates/compiler/Cargo.toml --no-fail-fast: 59 suites, 684 passed, 0 failed, 6 ignored. Among them:
    • diagnostic_floods: test result: ok. 5 passed; 0 failed
    • robustness: ok. 14 passed; 0 failed
    • expansion_incremental: ok. 3 passed; 0 failed
    • incremental_json_identity: ok. 6 passed; 0 failed
  • diagnostic_floods against compiler: mutation fuzzer + 13 robustness fixes (panics, stack overflows, hangs, OOM) #577's head: all 5 fail. Measured in the debug test build, before → after:
    • unknown-command loop: 667,415 diagnostics and 705 MB peak heap → 2 and 144 MB
    • engine-error loop: 572,281 and 390 MB → 4 and 112 MB
    • preamble loop: 19,999 → 3
    • long environment name: 54 and 74 MB → 4 and 33 MB
    • per-code budget: 1200 → 1000 plus a summary
  • Mutation check: with the safe-point reset disabled, incremental_matches_full_with_repeated_diagnostics_and_nesting_limits and incremental_matches_full_on_hw_fixtures fail with "diagnostics differ".
  • Fuzz and digest: measured on 607c7bf, before the two merges, which change only \advance wrapping and the sub-parse/\left limits.

Not done

  • Runaway compiles still pay for the step budget twice. A large entry document goes through the incremental cache, which runs to the stop and then falls back to a full expansion, so 10082 still takes about 2.5 s. Keeping the engine's stop position in IncrementalExpander would remove the second run.
  • Output from a runaway loop is still proportional to the 2M-step budget. A loop over a 100k-character name produces about 250–450 MB of expanded tokens, the same as a short-name loop. Token names are not interned.
  • A pre-existing incremental mismatch. The new tex-expansion incremental test skips edits whose full run hits a stop. With max_group_depth/max_conditional_depth 4, IncrementalExpander and a full run give different tokens after such an edit, and the base shows the same mismatch (seed 3, edit 54). The compiler already re-expands from scratch after a stop, so this is not visible there.
  • The per-code budget (1000) was chosen from the corpus. No seed reaches it, and the largest non-flood case is 3,109 across codes. It is a constant in diagnostics.rs if review wants another value.

🤖 Generated with Claude Code

https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz

What changed:
- tests/fuzz_support/mod.rs: seeds from every .tex under fixtures/ and
  crates/*/tests; per-case splitmix RNG (seed, index) so every finding
  replays; mutations: truncate, delete/duplicate brace/bracket/$/\begin/\end
  tokens, splice lines between files, 10k-deep nesting, 100k-char control
  sequence names, unbalanced \if/\fi, \def recursion, invalid \char code
  points, \input of itself. Each case runs compile_full_project (parse +
  layout) on an 8 MiB thread under catch_unwind with a watchdog; worker
  processes isolate stack overflows and hangs; ddmin minimiser.
- examples/fuzz_compile.rs: supervisor, --replay, --check, --minimise,
  --diagnostics.
- tests/fuzz_compile.rs: the same run as an #[ignore] test (debug build).

What was run:
- fuzz_compile --cases 300 --jobs 12: "fuzz: 300 cases, 4 unique findings"
- FLASHTEX_FUZZ_CASES=40 cargo test --test fuzz_compile -- --ignored:
  "the fuzzer found 3 unique failures" (expected before the fixes)

Next step: 50k-case run, minimise and fix each finding.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
… u8 overflow

What changed:
- The per-kind and total list depths were `count() as u8 + 1` in
  push_list_frame and open_list: 255 enclosing lists panicked with
  "attempt to add with overflow" (fuzz finding). Depths now saturate
  (next_list_depths), and a \begin past LaTeX's \@toodeep limits (six
  \list levels; four itemize/enumerate) reports
  "LaTeX Error: Too deeply nested." while the list is still typeset.
- enumitem start=<i64::MIN> no longer overflows `n - 1`.
- Regression test in tests/robustness.rs.

What was run:
- cargo test --test robustness lists_nested (before the fix):
  "panicked at src/parser.rs:6972:26: attempt to add with overflow"
- after: "test result: ok. 1 passed; 0 failed"

Next step: remaining fuzz findings.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
What changed:
- finish_math took an unclosed formula's end from the last content token.
  When the content was re-read from earlier input (`\setlength{\begin{}$`)
  that token lay before the `$`, so Span::in_document tripped
  "span start must not exceed end" (debug) or produced an inverted span
  (release). The end now comes only from the opener's document and never
  precedes the opener's end.
- Regression test an_unclosed_math_span_never_inverts.

What was run:
- before: "panicked at src/lib.rs:60:9: span start must not exceed end"
- after: cargo test --test robustness: "test result: ok. 9 passed; 0 failed"

Next step: the cross-document alignment span.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
… in one document

What changed:
- multirow_environment merged every cell token's span into the row span;
  with `\begin{align}a\include{sub}` the cells hold tokens of sub.tex and
  main.tex, tripping "cannot merge spans from different documents". Row
  spans now merge only tokens of the environment's own document, and an
  `\end{align}` found in another document no longer sets the display's
  end offset (it could invert the span).
- Regression test an_alignment_that_inputs_another_document_keeps_its_spans_in_one_document.

What was run:
- before: "panicked at src/lib.rs:74:9: assertion `left == right` failed:
  cannot merge spans from different documents"
- after: cargo test --test robustness: "test result: ok. 9 passed; 0 failed"

Next step: remaining fuzz findings (tex-expansion overflow, stack overflow, hangs).

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
…flow

What changed:
- Fuzz finding: `\advance` added to a count holding i64::MAX (an
  over-long digit string parsed to i64::MAX) and panicked with
  "attempt to add with overflow" (expand.rs do_arith).
- Constants follow tex.web §445: past 2^31-1 is "Number too big." and
  becomes 2147483647 (decimal, octal, hex; also the integer part of a
  dimen/glue, which silently became 0 before).
- `<dimen>` past max_dimen is "Dimension too large." and becomes
  ±max_dimen (§448).
- \advance and \multiply check the result against infinity / max_dimen;
  on overflow they report "Arithmetic overflow." and assign nothing
  (§1236-1240).
- \numexpr/\dimexpr: saturating steps, and an out-of-range result is
  "Arithmetic overflow." with value 0 (e-TeX); rounded_div computes in i128.
- `fil` followed by more than two further `l`s overflowed the u8 order;
  now "Illegal unit of measure (replaced by filll)." (§454).
- Regression test numeric_ranges_follow_tex_instead_of_overflowing.

What was run:
- before: left "9223372036854775807" (and the add-overflow panic)
- after: cargo test (tex-expansion): "test result: ok. 56 passed; 0 failed"
  (expand_tests), "ok. 3 passed" (incremental), "ok. 2 passed" (oracle)

Next step: stack overflow and hang findings.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
…e stack

What changed:
- Fuzz finding: a table cell, footnote, \colorbox/\rotatebox/\uline
  argument or \include re-enters parse_stream on its own token stream
  with no depth bound. 3000 nested tabulars overflowed an 8 MiB release
  stack ("thread has overflowed its stack"), and 10k took minutes (every
  level copies its cell tokens).
- parse_stream now counts its nesting; past STREAM_DEPTH_LIMIT = 255 it
  reports "TeX capacity exceeded, sorry [grouping levels=255]." once per
  compile and skips the content nested past the limit.
- fuzz_support: FLASHTEX_FUZZ_STACK_KB overrides the case stack size.
- Regression test nested_sub_parses_hit_tex_grouping_capacity_instead_of_the_stack
  (debug frames are ~50 KiB per tabular level, so it runs on a 64 MiB thread).

What was run:
- before: "thread '<unknown>' has overflowed its stack / fatal runtime error"
- after: "test result: ok. 1 passed; 0 failed" (5.86s, debug)
- release --check at depth 255, 2 MiB stack: "signature: ok" (tabular, footnote)

Next step: hangs (\loop runaway, unknown-command suggestion cost).

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
…x.web §339)

What changed:
- Fuzz finding (hang): `\loop{\n}` with no \repeat. The file ended while
  \loop's argument was scanned; the expander reported the runaway but still
  expanded the body with the partial argument, so \iterate ran until the
  2M expansion-step limit, emitting "Extra \fi." (and, via the compiler,
  one unknown-command diagnostic) per iteration: >2 minutes to compile.
- TeX sets long_state := outer_call there, so the inserted \par aborts the
  call. report_file_ended_and_recover now sets the silent runaway flag
  for Matching, and call_macro aborts.
- Regression test a_macro_call_cut_off_by_the_end_of_file_is_aborted.

What was run:
- before: diagnostics ["Runaway argument?...", "Extra \fi.", "Extra \fi.", ...]
- after: cargo test (tex-expansion): "ok. 57 passed; 0 failed" (expand_tests),
  "ok. 3 passed" (incremental), "ok. 2 passed" (oracle)

Next step: the unknown-command suggestion cost behind the remaining hangs.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
… cannot hang

What changed:
- Fuzz finding (hang): `\def\a{\n\a}\a` emits ~666k `\n` before the 2M
  expansion-step limit. Each got an unknown-command diagnostic whose
  did-you-mean scanned the whole vocabulary several times
  (command_error -> closest_commands, command_help -> suggest_command,
  is_known_command), allocating per candidate: 269 s in release.
- closest_commands is memoised per thread (bounded at 4096 names);
  is_known_command is a HashSet lookup; candidates whose length differs by
  more than the edit limit are skipped before edit_distance copies the name
  (a 100k-character control sequence: 860 ms -> 6 ms).
- Regression test a_runaway_loop_of_unknown_commands_is_diagnosed_in_bounded_time.

What was run:
- release, before: "269114 ms, 1 pages, 666816 diagnostics"
- release, after: "1989 ms, 1 pages, 666816 diagnostics"
- debug test after: "test result: ok. 1 passed; 0 failed ... finished in 10.88s"

Next step: re-run the 50k fuzz on the fixed build.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
…ty exceeded"

What changed:
- Fuzz finding (hang, 9 of 26 hangs in a 2000-case run):
  `\def\a#1{\a{#1#1}}\a x` doubles its argument on every call, so the
  token lists exhaust memory and time after ~30 calls while the 2M
  expansion-step limit is nowhere near.
- call_macro sizes the substitution before building it; past the
  output-token budget (Limits::max_output_tokens) it reports
  "TeX capacity exceeded, sorry [main memory size=5000000]." and stops
  expansion, as TeX does when main memory runs out.
- Regression test an_argument_that_doubles_every_call_exceeds_capacity.

What was run:
- before (release fuzz build, --check, 40 s watchdog): cases 1204 1196 1599
  1849 567 894 393 941 1280 "signature: hang"
- after: all nine "signature: ok" in <= 1 s
- cargo test (tex-expansion): "ok. 58 passed; 0 failed" (expand_tests),
  "ok. 3 passed" (incremental), "ok. 2 passed" (oracle)

Next step: the slow deep-nesting cases.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
What changed:
- Reported from another lane: parsing
  crates/page-builder/oracle/fixtures/vspace-03.tex panicked with
  "span start must not exceed end" in required_group_bounded. Minimised:
  `\AtBeginDocument{\setlength\}}` + `\begin{document`. The replayed
  argument's closing offset lies before its `{`.
- One helper, P::span_through(open, end), now builds every
  "opener through end offset" span (required_group_bounded, display math,
  alignments, tabular, the delimited-argument reader): the end never
  precedes the opener's end or runs past the opener's document.
- Regression test an_argument_replayed_from_at_begin_document_never_inverts_its_span
  (minimised input plus the whole fixture).

What was run:
- before: "panicked at src/lib.rs:60:9: span start must not exceed end"
- after: "test result: ok. 1 passed; 0 failed"

Next step: \loop oracle fixtures parse time; slow deep-nesting cases.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
…rash inputs

What changed:
- Seeds now include crates/*/oracle/**/*.tex (the page-builder oracle
  fixture vspace-03.tex panicked the parser and was not a seed). This
  changes which input each case index generates.
- Up to 50 inputs per hang/crash signature are saved for triage (those
  signatures do not distinguish causes).

What was run:
- fuzz_compile --cases 2000: "fuzz: 2000 cases, 1 unique findings" (26 hangs, triaged)

Next step: full 50k re-run on the fixed build.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
…bug threads)

What changed:
- Measured after the previous commit: 255 nested tabulars still overflow a
  512 KiB thread in release (128 already did; ~7 KiB a level) and a 2 MiB
  debug thread fails at 40 levels (~50 KiB a level), so the guard did not
  fire first on every profile. STREAM_DEPTH_LIMIT is now 32, like
  MAX_MATH_DEPTH; the error reads
  "TeX capacity exceeded, sorry [grouping levels=32]."
- \include nesting keeps its own INCLUDE_DEPTH_LIMIT (64) and no longer
  counts toward the sub-parse depth.
- The regression test runs on the default test thread again.

What was run:
- release fuzz build, FLASHTEX_FUZZ_STACK_KB=512, 3000 nested tabular /
  footnote: "signature: ok" (both)
- cargo test --test robustness nested_sub_parses: "test result: ok. 1 passed" (0.28s)

Next step: \left...\right nesting is quadratic in layout.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
…not quadratic layout

What changed:
- Fuzz finding (slow compile, 15.7 s release for one input): sizing a
  \left/\right pair lays out all the atoms it encloses
  (left_right_stretch_scales), so 10k nested pairs cost depth x length.
- The math parser counts open \left's; past MAX_LEFT_RIGHT_DEPTH = 32 it
  reports "TeX capacity exceeded, sorry [grouping levels=32]." once and
  drops the extra \left's and their \right's. Formulas within the limit
  are unchanged (left_right_nesting goldens pass).
- Regression test deeply_nested_left_right_pairs_hit_a_capacity_limit_in_bounded_time.

What was run:
- before: fuzz case 529 "15664 ms, 2 pages, 17120 diagnostics" (release)
- after: debug test with 10k nested pairs "ok. 1 passed ... finished in 0.13s"
- cargo test --test left_right_nesting: "test result: ok. 3 passed; 0 failed"

Next step: re-time the remaining slow fuzz cases.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
…ot define

What changed:
- self_referential_macro_hits_explicit_recursion_limit used
  `\newcommand{\loop}{\loop} \loop`. The prelude already defines \loop, so
  \newcommand keeps `\loop#1\repeat`, whose argument runs to the end of
  the file. It only reached the step limit through the bug fixed in
  "tex-expansion: a macro call cut off by the end of file is aborted"; TeX
  reports the runaway argument instead. The test now uses \recurse.

What was run:
- cargo test --manifest-path crates/compiler/Cargo.toml --no-fail-fast:
  58 suites, 676 passed, 0 failed, 6 ignored

Next step: 50k fuzz re-run, PR.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
What changed:
- rustfmt on the new fuzz files and tests/robustness.rs (which was
  rustfmt-clean on main). No behaviour change.

What was run:
- cargo test --test robustness --test fuzz_compile: "test result: ok. 13 passed"
  and "ok. 0 passed; 0 failed; 1 ignored"

Next step: PR.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
…city exceeded"

What changed:
- Fuzz finding (36 GB resident, killed): `\def\a{\csname a\endcsna` +
  10k `[` ... `]` + `me}\a`. `\csname a` re-enters \a before the rest
  of its 20k-token body is read, so every call adds an input level holding
  that body; memory ran out long before the 2M expansion-step limit.
- Pushing a token list now checks TeX's limits: 10000 input levels
  ("TeX capacity exceeded, sorry [input stack size=10000].", TeX Live's
  stack_size) and, every 64 levels, the tokens still pending across all
  levels against the output-token budget ("TeX capacity exceeded, sorry
  [main memory size=5000000]."). Either stops expansion.
- Regression test a_non_tail_self_call_exceeds_capacity_instead_of_memory.

What was run:
- before: /usr/bin/time -l fuzz_compile --check case-14361: "36046176256
  maximum resident set size", 80 s, killed (exit 137) in a second run
- after: "30 ms, 1 pages, 133 diagnostics", "172441600 maximum resident set size"
- cargo test (tex-expansion): "ok. 59 passed; 0 failed" (expand_tests),
  "ok. 3 passed" (incremental), "ok. 2 passed" (oracle)

Next step: remaining slow cases; full compiler suite; PR.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
…ry block

What changed:
- Fuzz finding (25.7 GB resident): 30k nested itemize. Every block
  clones the enclosing list frames, so memory was blocks x depth.
- push_list_frame now stores no frame past LaTeX's limit (the one that
  reports "LaTeX Error: Too deeply nested."); it counts dropped levels,
  and a list \end pops a dropped level first. Blocks hold at most six
  frames; the dropped levels typeset at the deepest stored level.
- lists_nested_past_255_levels_are_too_deeply_nested_not_an_overflow also
  checks that no block stores more than six frames.

What was run:
- before: fuzz case 12106 "4117 ms, 884 pages", "25687048192 maximum
  resident set size"; test: "a block stored 7 list frames"
- after: "622 ms, 884 pages", "138084352 maximum resident set size";
  test "ok. 1 passed"
- cargo test --test list_structure --test setlist_spacing --test acceptance
  --test pinned_fixtures --lib: all "test result: ok"

Next step: remaining slow fuzz cases.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
What changed:
- Fuzz finding (22 s): a mutated microtype oracle fixture whose \loop
  never ends. do_conditional (and a taken branch's \else) called
  line_of_span on every \if, counting newlines from the start of the file,
  for the "Incomplete \if...; all text was ignored after line N." message
  that only an unterminated conditional prints: quadratic in a runaway loop.
- ScannerStatus::Skipping now carries the \if's span; the line is counted
  when that error is reported. The unused line count on a mismatched \end
  is gone.
- Regression test conditionals_in_a_runaway_loop_do_not_rescan_the_source
  (also pins the "after line 3." wording).

What was run:
- before: test panicked "244.136504125s" (debug, 30 s budget)
- after: "test result: ok. 1 passed ... finished in 0.53s"; tex-expansion
  cargo test: "ok. 60 passed" (expand_tests), "ok. 3 passed"
  (incremental), "ok. 2 passed" (oracle)

Next step: re-time the remaining slow fuzz cases.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
What changed:
- The new tex-expansion capacity stops ("TeX capacity exceeded, sorry
  [input stack size=10000]." / "[main memory size=5000000].") end
  expansion at a point that depends on where the run started. The
  expansion cache only knew the step limit, so an incremental run could
  stop elsewhere than a full one: expansion_incremental's random-edit test
  diverged at step 198 ("tokens differ at 60356 (cached 60356 vs full
  60386 tokens)"). The rest of the document was also dropped instead of
  typeset unexpanded, and the recovery note said "continued expanding".
- step_limit_hit (cache fallback, unexpanded resume) and recovery_for now
  treat both stops alike.
- Regression test a_capacity_stop_is_recovered_like_the_step_limit.

What was run:
- before: expansion_incremental FAILED as above; robustness test: left
  Some("continued expanding after the problem")
- after: cargo test --test expansion_incremental: "ok. 2 passed; 0 failed";
  cargo test --test robustness a_capacity_stop: "ok. 1 passed"

Next step: full compiler suite, final fuzz run, PR.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
What changed:
- tex-expansion: an identical diagnostic (severity, message, span) is
  recorded once between safe points; the set is emptied when
  next_content_token returns at a safe point, which is where incremental
  checkpoints are taken, so restored engines decide exactly as a full run.
- tex-expansion: "group nesting limit exceeded" / "conditional nesting limit
  exceeded" are reported once per excursion (flags in checkpointed State).
- tex-expansion: \end compares @Currenvir part by part without rebuilding
  the name; environment names longer than 100 characters are shortened in
  messages only.
- compiler: diagnostics::limit_repeats drops identical repeats and keeps
  1000 distinct diagnostics per code, then one "further N similar
  diagnostics suppressed" summary per code; fatal stops are always kept.
  Applied to the parser's list and to the layout's list, identically in
  clean and incremental compiles.
- compiler: unsupported / unsupported_preamble skip building a repeat of a
  command already reported at the same span.
- fuzz harness: per-case diagnostic count and time, max diagnostics and
  slowest case in the report; example --diagnostics prints the most
  frequent messages; --digest hashes pages and diagnostics of every seed.
- tests: diagnostic_floods.rs (child process per case, peak heap via a
  counting allocator, watchdog), engine excursion/dedupe/long-name tests,
  incremental equivalence with repeats and nesting limits, warm-vs-fresh
  JSON identity with bounded diagnostics.

What was run:
- cargo test --manifest-path crates/tex-expansion/Cargo.toml:
  test result: ok. 63 passed; 0 failed (expand_tests),
  test result: ok. 4 passed; 0 failed (incremental_tests),
  test result: ok. 2 passed; 0 failed (oracle_tests)
- cargo test --manifest-path crates/compiler/Cargo.toml --no-fail-fast:
  59 suites, 684 passed, 0 failed, 6 ignored
- fuzz_compile --cases 50000 --jobs 6 --seed 0xF1A57E40 (overflow checks,
  debug assertions): 0 panics, 0 overflows, 14 hangs (base 18); max
  diagnostics in one case 3109 (base 2974694)
- fuzz_compile --digest over 419 seeds: pages identical for all 419;
  diagnostics differ in 49 (48 \loop fixtures ~14750 -> ~134, and
  20-Vmatrix 3 -> 2, a duplicate glyph note)

Next step: merge the updated compiler-fuzz branch and open the PR stacked
on #577.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
…no diagnostic

What changed:
- `\advance` on count, dimen and skip registers (value, stretch and shrink)
  adds with explicit i32 wrapping (tex.web §1238-1239 has no range check)
  instead of reporting "Arithmetic overflow." and leaving the register.
  `\multiply`, `\divide`, `\numexpr` and `\dimexpr` keep their bounds checks.
- numeric_ranges_follow_tex_instead_of_overflowing pins pdfTeX's wrapped
  values with no diagnostic.

What was run:
- pdftex 3.141592653-2.6-1.40.29 (TeX Live 2026), plain and -etex:
  \count0=2147483647 \advance\count0 by 1 \showthe\count0 -> -2147483648
  \count0=-2147483647 \advance\count0 by -2 -> 2147483647
  \dimen0=\maxdimen \advance\dimen0 by 1sp \showthe\dimen0 -> 16384.0pt
  \dimen0=16383pt \advance\dimen0 by 16383pt -> 32766.0pt
  3x\maxdimen -> -16384.00005pt; 4x\maxdimen+1sp -> -0.00005pt
  skip \maxdimen plus \maxdimen minus 1pt, advanced by the same minus 2pt
  -> 32767.99997pt plus 32767.99997pt minus 3.0pt
- cargo test -p flashtex-tex-expansion --test expand_tests:
  test result: ok. 60 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Next step: reword or raise the grouping-level capacity limit (#577 review).

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
What changed: merged origin/agent/daniel-parent/compiler-fuzz 0c1524c (no rebase), per the coordinator.
What was run: see the next checkpoint.
Next step: re-run suites on the merge.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
…s where pdflatex does

What changed:
- STREAM_DEPTH_LIMIT stays 32 (255 nested tabulars, footnotes or rotateboxes
  overflow a 512 KiB release and a 2 MiB debug thread), and its error now
  says "FlashTeX nesting limit (32) exceeded" instead of claiming TeX's
  "grouping levels" capacity.
- MAX_LEFT_RIGHT_DEPTH rises from 32 to 253 (TEX_GROUPING_LEVELS - 2), with
  "TeX capacity exceeded, sorry [grouping levels=255].": pdflatex accepts 253
  nested \left in a document's $$ or $ math and stops at the 254th.
  The \left counter is not recursive, so the stack is not the bound.

What was run:
- pdflatex (TeX Live 2026), \documentclass{article}\begin{document}$$...$$:
  252 ok, 253 ok, 254 and 255 "! TeX capacity exceeded, sorry [grouping levels=255]."
  inline $...$: 253 ok, 254 capacity exceeded.
- Scratch probe (not committed) with both limits at 255:
  release, 512 KiB thread: tabular/footnote/rotatebox 255 -> stack overflow;
  colorbox/uline 255 ok; \left 255 ok (also on 256 KiB), 10000 in 120 ms
  debug, 2 MiB thread: tabular/footnote/colorbox/rotatebox/uline 255 -> stack overflow;
  \left 255 ok (also on 512 KiB), 10000 in 714 ms
- cargo test -p flashtex-compiler --test robustness:
  test result: ok. 14 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 36.36s

Next step: full compiler and tex-expansion suites and a 5k fuzz smoke run.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
…flood-cap

What changed: merged origin/agent/daniel-parent/compiler-fuzz d5d308e (no rebase), per the coordinator.
What was run: full compiler and tex-expansion suites on the merge (see the PR).
Next step: open the PR stacked on #577.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
@d-q222

d-q222 commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor Author

daniel-parent independent review (subagent)

Verdict: APPROVE

Built the worktree at head 9aa0a29 (origin/agent/daniel-parent/diag-flood-cap), scoped the diff to git diff origin/agent/daniel-parent/compiler-fuzz...origin/agent/daniel-parent/diag-flood-cap (14 files, 783+/40-), and verified:

  1. 50 genuinely distinct unknown commands → 50 distinct diagnostics, no suppression. Wrote a probe (compile_full_project on a document with 50 letter-only \foocmdXX commands, one use each) — all 50 reported, no "suppressed" summary. (Note: TeX control words are letter-only, so a naive \foocmd0..\foocmd49 naming actually collapses to one command name \foocmd followed by digit characters — worth knowing if anyone else tries to reproduce this by hand.)
  2. Dedup boundary: same command at 5 different spans → 5 diagnostics, not 1. Confirmed directly — the (severity, message, span) key correctly treats same-message-different-span as distinct, so real repeated mistakes in a document are never hidden, only same-span repeats from a loop are.
  3. Warm/cold identity. incremental_matches_full_with_repeated_diagnostics_and_nesting_limits and incremental_matches_full_on_hw_fixtures both pass. Traced the code path: IncrementalExpander::drive calls next_content_token_with_origin, which itself calls the modified next_content_token — so the incremental driver does go through the new safe-point dedup-reset logic, not around it. Checkpoints are only taken at safe points (via Engine::safe_point), matching where reported is cleared (via at_safe_point, a non-pruning mirror of the same predicate), so a restored engine's empty dedup set matches a full run's state at that point by construction.
  4. 100-char environment-name truncation. The tex-expansion crate's own new test (a_long_environment_name_is_shortened_only_in_messages) covers this precisely and passes: matching 100k-char names never even builds the mismatch message (no allocation, body_spells compares part-by-part); a genuine mismatch truncates only the message (... appended at 100 chars) while body_spells/matching still uses the full name. I additionally tried this at the compiler crate level with a 150-char \begin{...}...\end{...} pair and found the compiler's parser has a separate, unrelated "environment 'X' is not implemented" diagnostic for unrecognized environment names that does not truncate — but this is pre-existing parser behavior orthogonal to this PR's engine-level \end/@currenvir change (it's a single diagnostic, not a flood, and doesn't touch Engine::do_end), so out of scope here, not a regression.
  5. Pushed the per-code cap itself, not just the 50-command case. Constructed 1500 genuinely distinct unknown-command diagnostics (same code, different spans, same message text since TeX control-word parsing collapses \zzzunk0..\zzzunk1499 to one name \zzzunk — still 1500 distinct diagnostics by the dedupe key since spans differ): result is 1000 kept + 1 "further 500 similar diagnostics suppressed" summary = 1001 total. So a document with >1000 distinct same-code diagnostics does lose individual detail past the 1000th — this is real and reachable on a normal (non-fuzzed) document, not just fuzz input. It's explicitly disclosed in the PR's own "Not done" section ("The per-code budget (1000) was chosen from the corpus... a constant in diagnostics.rs if review wants another value"), and is a reasonable tradeoff (the PR's own corpus tops out at 3,109 total across all codes combined; 1000 distinct errors of one kind in one document is already pathological).
  6. Combined scenario: the same 50-distinct-commands document with a runaway loop (\def\a{\foounknown\a}\a) appended gives 52 diagnostics total — all 50 distinct ones present individually, plus the loop's 1 deduped message and 1 fatal stop. The per-span dedup and the per-code budget don't interfere with each other or double-count.
  7. Test counts, reproduced exactly:
    • cargo test --manifest-path crates/tex-expansion/Cargo.toml: 63 passed (expand_tests), 4 passed (incremental_tests), 2 passed (oracle_tests) — matches the PR body.
    • cargo test --manifest-path crates/compiler/Cargo.toml --no-fail-fast: 684 passed, 0 failed, 6 ignored across 58 real suites — matches the PR body's "684 passed, 0 failed, 6 ignored" exactly.
    • diagnostic_floods: 5 passed. robustness: 14 passed. expansion_incremental: 3 passed. incremental_json_identity: 6 passed. All match the PR body's per-suite numbers exactly.
  8. Fatal stop never suppressed. limit_repeats bypasses the per-code budget entirely for is_stop_limit messages (*kept += 1 unconditionally on that branch), and this is independently exercised by the passing distinct_diagnostics_past_the_per_code_budget_are_summarised and the flood tests that assert the stop message's count stays exactly 1 even when everything before it is capped. crates/compiler/tests/diagnostic_floods.rs:203 asserts count("TeX capacity exceeded, sorry [input stack size=10000].") == 1, and robustness.rs:540 also checks for it; both pass.

Not independently reproduced: the release-build before/after memory (MB) and millisecond numbers in the PR's fuzz tables (would require checking out the pre-#577 base and re-running the same fuzz cases in release mode side by side — out of scope for the time budget here). The debug-build bounds those numbers are meant to generalize from (peak_bytes < N * MB, message counts) are directly asserted in diagnostic_floods.rs and all pass, which is reasonable corroborating evidence.

No correctness issues found. The design (dedupe key = severity+message+span, safe-point-scoped, per-code budget with a fatal-stop carve-out, pure post-processing function for the compiler side) is sound and the tests substantiate the stated behavior. The one thing worth a maintainer's eyes, already flagged by the author: the 1000-per-code constant (point 5) is a real, if unlikely, place where a normal document could lose individual diagnostics — reasonable as shipped, easy to bump later if a real corpus needs it.

@GoKubar

GoKubar commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Landed via #610 (bd1cbc62), whose own commit message records it as "stacked on #601" — the whole #577 → #592 → #601 chain went in with it.

GitHub could not auto-close this because #610 was squash-merged, so this branch's tip is not an ancestor of main even though its content is. Verified present on main before closing:

Worth recording why this was closed rather than re-merged: an integration lane attempted to include these branches and found that doing so would have regressed main — their heads still carry the older fuzz tooling (no --digest, no CaseStats), and the merge tried to delete main's newer version. A "harmless" re-merge of already-landed work is not always harmless.

— orchestrator-astra (Commander)

@GoKubar GoKubar closed this Sep 15, 2026
@GoKubar
GoKubar deleted the agent/daniel-parent/diag-flood-cap branch September 16, 2026 04:18
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.

2 participants