Skip to content

tex-expansion round 2 - #98

Merged
jay3332 merged 8 commits into
mainfrom
agent/kabir-claude/tex-expansion-r2-opus
Sep 13, 2026
Merged

jay3332 merged 8 commits into
mainfrom
agent/kabir-claude/tex-expansion-r2-opus

Conversation

@GoKubar

@GoKubar GoKubar commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

Round 2 of KC-101, extending #88 (which stays open). Branch agent/kabir-claude/tex-expansion-r2-opus, based on the paused Fable WIP 7dae933. Only crates/tex-expansion/** is touched. Full details are in crates/tex-expansion/CONTRACT.md.

Review of 7dae933 (the Fable WIP)

The WIP built and its tests passed, but it had no tests for its new features: tests/incremental_tests.rs and examples/bench_incremental.rs were referenced but missing. Bugs found and fixed:

  • Incremental checkpoints recorded diagnostic and label offsets relative to the restore point.
  • \ifcsname and \@ifundefined were true after a group restore.
  • \detokenize did not double #.
  • print_cs spacing did not follow tex.web §262.
  • \protect leaked into the output.
  • A \outer token in parameter text caused an infinite loop.
  • There was no prefix error for \global x.
  • ^^ notation panicked past byte 0 (the pre-existing bug fix is also applied).
  • Expansion inside \edef did not count toward the step limit, so it could hang.
  • \newcommand on a defined name redefined it.
  • The optional-argument representation was not LaTeX's.

Results

  • Oracle: 415/415 cases pass against MacTeX tex/etex/pdflatex (round 1: 108). New modes: latex-write, latex-doc, and tex-err/etex-err/latex-err, which compare TeX's own ! ... error lines. Coverage includes the full requested list: \@ifundefined, \DeclareRobustCommand/\protected@edef, \unexpanded/\detokenize/\expanded, \ifdefined/\ifcsname, \scantokens, \afterassignment, \uppercase with \uccode, \chardef/\mathchardef, \futurelet idioms, \@for/\@tfor, \loop, \g@addto@macro, \AtBeginDocument, keyval, lengths, \refstepcounter/\@currentlabel, \csname relax, \string/\escapechar, \meaning of every token kind, \pdfstrcmp.
  • \long/\outer: enforced as in tex.web §336/§392-399, with TeX's messages and recovery. Covers runaway \par, forbidden control sequences in definitions, arguments, texts and skipped conditionals, extra }, and "doesn't match its definition"; all checked against the real logs.
  • Counters: [within], \@addtoreset/\@removefromreset, \counterwithin(*)/\counterwithout(*), and recursive resets are all oracle-checked. \fnsymbol produces Unicode (∗ † ‡ § ¶ ‖ ∗∗ †† ‡‡), which matches pdflatex's extracted text.
  • Grouping tokens: {/} (explicit or implicit), \begingroup and \endgroup are now emitted with spans, as requested by KC-110/WIP: engine PoC (KC-110, paused) #96. API-compatible, with is_group_token added.
  • Incremental: IncrementalExpander equals a full run (tokens with spans, labels and diagnostics) under random edits on all oracle docs, HW1/HW2, and a macro-heavy document. On a 500 KB synthetic document: p50 5.9 ms, p95 115 ms, max 379 ms per keystroke, with 536/600 edits converging early.
  • Smoke: all 44 .tex files under fixtures/ and tests/ on origin/main were run; 42 are clean and 2 have errors, both legitimate. The most common unmodelled primitives are \parindent, \parskip, \hfill, \input, \left/\right.
  • Kernel probe: real latex.ltx run from an INITEX-state engine gets through line 1147 of 22470 (5.1%) in 226 ms, reading texsys.cfg, expl3.ltx and expl3-code.tex. It stops in the expl3 bootstrap, which needs the full pdfTeX primitive set. Static gap: 130 of the 310 primitives latex.ltx references are missing (515 of 717 including expl3, 162 of them LuaTeX-only), and they are mostly box, font, math and alignment primitives. So loading the kernel needs a typesetting core, not just more expansion work (see CONTRACT.md for the estimate).
  • Lexer: \endlinechar (including -1 and active ^^M), CRLF, trailing-space stripping, and ^^ in control-sequence names. Engine::set_file_reader makes \input read files.

Tests

cargo test: 55 unit, 3 incremental-equivalence, 415 oracle.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu

GoKubar and others added 8 commits September 13, 2026 01:43
New crate crates/tex-expansion implementing catcodes+tokenizer,
def/edef/gdef/xdef/let/futurelet, expandafter/noexpand/csname, TeX and
e-TeX conditionals (if/ifx/ifnum/ifdim/ifodd/ifcase/newif/unless), grouping
with global/aftergroup, count/dimen/skip/toks registers with countdef family
and numexpr/dimexpr, and a LaTeX layer (newcommand/renewcommand/provide,
newenvironment, counters, @ifnextchar/@ifstar/@namedef/@nameuse). 45 unit
tests passing.

Implementation-Agent: Claude Fable 5.1 (subagent kabir-claude KC-101, mac-m5pro-kabir)
Commit-Executor: Claude Code direct Git
Co-Authored-By: Claude Fable 5.1 <[email protected]>
…orpus

Fixes found via a 108-case oracle corpus run against real tex/etex/pdflatex:
bare grouping braces must not appear in the content-token stream (matches
real main-control semantics, not \write's scan_toks semantics); \the\dimen
must render via TeX's print_scaled algorithm, not raw sp; \numexpr division
rounds to nearest (ties away from zero); \ifcase must keep skipping on a
negative selector; \setcounter/\addtocounter values may contain expandable
constructs like \value{...}; \count's "..." hex parsing.

Adds tests/oracle/ (generator script + committed manifest.json + expected
fixtures, gitignored tmp/) and CONTRACT.md proposing the crates/compiler
adoption path, known deviations from real TeX, and what's done vs not.

47 unit tests + 108/108 oracle cases green.

Implementation-Agent: Claude Fable 5.1 (subagent kabir-claude KC-101, mac-m5pro-kabir)
Commit-Executor: Claude Code direct Git
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Uncommitted progress from the Fable round-2 session (KC-101 r2), saved
when the session was paused to stay inside the 5-hour usage window. Not
reviewed or re-tested; resume from here.

Implementation-Agent: Claude Fable 5.1 (headless agent kabir-claude KC-101 r2, mac-m5pro-kabir)
Commit-Executor: Claude Opus 5 (coordinator session, direct Git)
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
…-case oracle)

WIP, pushed early on coordinator request; not final. Known open item at this
commit: one unit/incremental test run hung after the conditional
test-phase change (under investigation); grouping braces not yet emitted.

Review fixes to 7dae933: \ifcsname/\@ifundefined true after group restore
(is_defined), print_cs spacing per tex.web 262, \detokenize doubles #,
\protect/let-to-relax emitted as \relax, incremental checkpoint diag/label
offsets were relative after restore, \outer recovery loop in parameter text
(Missing { inserted), prefix errors ("You can't use a prefix with"),
\newcommand optional args built as LaTeX does (\@Protected@testopt +
\\foo[#1]), \newcommand on a defined name keeps the old meaning, zero-arg
commands not \long, \expanded, \pdfstrcmp, \eTeXversion, coefficient
dimens (.5\dimen0), extra-} argument recovery, \fi during a test inserts
\relax, INITEX constructor. Adds oracle modes latex-write/latex-doc/*-err,
incremental equivalence tests, keystroke bench, smoke and kernel probes.

Implementation-Agent: Claude Opus 5 (subagent kabir-claude KC-101 r2, mac-m5pro-kabir)
Commit-Executor: Claude Code direct Git
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu
…sion

Grouping braces (explicit and implicit \bgroup/\egroup) and \begingroup/\endgroup are now emitted in the output stream with their source spans (coordinator request from KC-110/PR #96); \aftergroup tokens follow the closing brace. API-compatible: is_group_token() added for consumers that want content only. Nested expansion (edef bodies, macro calls) now counts toward max_expansion_steps, fixing a hang found by the incremental random-edit test; finished token lists are popped before pushing so tail recursion does not grow the input stack. Tests: 52 unit, 3 incremental equivalence, 415/415 oracle.

Implementation-Agent: Claude Opus 5 (subagent kabir-claude KC-101 r2, mac-m5pro-kabir)
Commit-Executor: Claude Code direct Git
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu
…/I-O, \input via host reader

Lexer: physical line breaks stand for \endlinechar (incl. -1 and non-EOL catcodes such as \obeylines), CRLF counts once, trailing spaces stripped (input_ln), ^^M inside a line discards the rest, ^^ notation resolved in control-sequence names, fixed a length underflow that made ^^ notation panic past byte 0. INITEX mode (kernel probe only): TeX/e-TeX/pdfTeX parameters as registers, \immediate/\write/\openout/\message/\errmessage/\read stand-ins, \eTeXversion/\eTeXrevision/\jobname. Engine::set_file_reader makes \input read files (otherwise it still passes through). Tests: 55 unit, 3 incremental equivalence, 415/415 oracle.

Implementation-Agent: Claude Opus 5 (subagent kabir-claude KC-101 r2, mac-m5pro-kabir)
Commit-Executor: Claude Code direct Git
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu
…oup tokens

Physical line breaks are recognised by the lexer, so the catcode tables no longer mark \n as end-of-line; ^^J inside \write/\typeout text is an ordinary character as in real TeX. With this the latex.ltx probe loads texsys.cfg, expl3.ltx and expl3-code.tex and reaches line 1147.

Implementation-Agent: Claude Opus 5 (subagent kabir-claude KC-101 r2, mac-m5pro-kabir)
Commit-Executor: Claude Code direct Git
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu
…e ignores group tokens

CONTRACT.md documents the round-2 API, output-stream contract (grouping tokens), oracle modes (415/415), \long/\outer, counters, \fnsymbol mapping, incremental design and latency (500 KB: p50 5.9 ms, p95 115 ms), smoke over origin/main (42/44 clean) and the latex.ltx kernel probe (line 1147/22470, 130/310 primitives missing, mostly typesetting).

Implementation-Agent: Claude Opus 5 (subagent kabir-claude KC-101 r2, mac-m5pro-kabir)
Commit-Executor: Claude Code direct Git
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu
@jay3332
jay3332 merged commit 7847e07 into main Sep 13, 2026
12 of 18 checks passed
@GoKubar
GoKubar deleted the agent/kabir-claude/tex-expansion-r2-opus branch September 13, 2026 18:12
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