Skip to content

WIP: engine PoC (KC-110, paused) - #96

Closed
GoKubar wants to merge 17 commits into
mainfrom
agent/kabir-claude/engine-poc
Closed

GoKubar wants to merge 17 commits into
mainfrom
agent/kabir-claude/engine-poc

Conversation

@GoKubar

@GoKubar GoKubar commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

Status: WIP, paused for usage (KC-110). Nothing has been compiled or oracle-checked in crates/tex-engine-poc yet.

Merges done (merge commits, no conflicts)

What exists (8d24313, not compiled)

  • src/fonts.rs: TFM loading. It uses tex-text-encoding ScaledFont, looks in the committed tfm/ copies first, then kpsewhich. It also has the OT1 cmr NFSS table from ot1cmr.fd and implements the tex-boxes CharMetrics trait.
  • src/linebreak.rs: a port of tex.web line breaking in integer sp over tex-boxes nodes.
    • §829–§873 line_break with delta offsets.
    • §894–§899 word selection with Liang positions and simplified reconstitution: breaks inside ligatures are skipped and counted.
    • §877–§889 post_line_break, packing lines with tex-boxes hpack.
  • src/stomach.rs: the main control.
    • A format prelude of marker macros, then the tex-expansion token stream.
    • OT1 characters via lig_kern_run, sfcode and make_accent.
    • \section/\subsection/\subsubsection per latex.ltx \@startsection/\@sect/\@xsect/\@afterheading, with specs from class-geometry.
    • \emph/\textbf/\textit with \maybe@ic, \\, ~, \vspace(*), \pagebreak[n], \newpage, \clearpage.
    • page-builder VListBuilder feeding PageBuilder + LatexOutput, then tex-boxes ship_out for glyph positions plus the plain page-number footer.
  • src/bin/ftpoc.rs: prints glyph JSON; --bench times a cold run and a one-word-edit re-run.

Verified so far

  • The merged crates build together as path dependencies (examples/dump_tokens.rs ran).
  • tex-expansion's output on a sample article was inspected by hand.
  • pdflatex content streams can be parsed for glyph positions (reuse tex-text-encoding/tools/generate_oracle.py page_glyphs).

Seams found so far (proposals, no shared crate edited)

  1. tex-expansion drops grouping braces. Argument extents (\vspace{12pt} Text) and {\bfseries ...} scopes are invisible to a stomach. Fix: emit BeginGroup/EndGroup tokens, or a callback. The PoC works around it with marker macros.
  2. BoxEngine cannot drive paragraphs from a real character layer. It has no public append for ligature, font-kern or accent-kern nodes, and LineBreaker does not receive \hangindent, \interlinepenalty, \clubpenalty or prev_graf. Fix: add append_node and pass a paragraph-parameters struct to LineBreaker.
  3. Two node models. tex-boxes Node/BoxNode/GlueSpec (with GlueOrder) and page-builder Node/BoxNode{id}/GlueSpec (with Order) duplicate scaled/pack/vpack. Fix: page-builder depends on tex-boxes, as the tex-boxes CONTRACT already proposes.
  4. Unit and glue types don't match. class-geometry uses Sp(i64) and Glue with no orders; the others use i32 sp. tex-text-encoding has its own sfcode::GlueSpec and layout::Node. Fix: a shared tex-units crate.
  5. paragraph-layout breaks in f64 points over its own items, so a sp-exact tex.web breaker had to be written here. Fix: generalize it into a shared line_break over tex-boxes nodes.
  6. VListBuilder's addvspace/addpenalty and BoxEngine's LaTeX para hooks are both unverified by their own oracles and model the same kernel code twice.
  7. The microtype expansion hook is a placeholder in tex-boxes hpack, so microtype is not wired.

Resume steps

  1. export CARGO_BUILD_JOBS=3; cargo build --manifest-path crates/tex-engine-poc/Cargo.toml and fix compile errors.
  2. Run cargo run --bin ftpoc -- s1.tex on a sample and compare it with pdflatex '\pdfcompresslevel=0\pdfobjcompresslevel=0\input{s1}' glyphs.
  3. Write tools/corpus.py: at least 25 documents (10/11/12pt, a4/letter, geometry, headings, emph, accents, \newcommand). Write tools/oracle.py: PDF glyph extraction to expected/*.json.
  4. tests/oracle.rs: page count, per-line text, glyph x/y within 0.1pt; report pass counts without running TeX.
  5. Timings via ftpoc --bench; finalize SEAMS.md and README.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu

GoKubar and others added 17 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]>
Why: every glyph's position depends on the class/geometry page frame;
render-pipeline only models article + margin= in floating point.

Behavior:
- New standalone crate crates/class-geometry (zero deps). TeX integer
  arithmetic (round_decimals, xn_over_d, \divide, \@settopoint,
  print_scaled) so lengths match pdflatex to the scaled point.
- article/report/book x 10/11/12pt x a4/a5/b5/letter/legal/executive,
  landscape, oneside/twoside, onecolumn/twocolumn, titlepage, openright/
  openany, fleqn/leqno, draft (size1x.clo + bk1x.clo, cited by line).
- geometry v6.0 macro-by-macro: keys, \Gm@detall cases 0-7, adjustbody/
  adjustmp, includehead/foot/mp, ratios, scale, lines, heightrounded,
  bindingoffset, landscape/paper keys, sequential \geometry calls.
- PageFrame: odd/even text left, head/foot/first baselines, columns,
  marginpar side, PDF MediaBox (engine default letter without geometry).
- \@startsection specs, \chapter/\chapter*/\part, secnumdepth/tocdepth,
  page styles (empty/plain/headings/myheadings) and mark rules.
- DocumentSetup::from_preamble scanner; CONTRACT.md proposes the
  render-pipeline integration (render-pipeline not edited).
Oracle: oracle/generate.py (pdflatex 1.40.29, TeX Live 2026, oracle only)
-> tests/data/oracle.txt, 96 fixtures; cargo test: 96/96 fixtures,
2881/2881 lengths exact, 2112/2112 \pdfsavepos positions exact (0sp).
Validation: cargo test, cargo clippy --all-targets (0 warnings), cargo fmt.
Excluded (other branches): \maketitle/title layout, page-control
commands, list spacing.

Implementation-Agent: Claude Opus 5 (subagent kabir-claude KC-103, 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
New crate crates/microtype (no dependencies, never runs TeX):

- config: parses microtype.cfg / mt-cmr.cfg (bundled unmodified, LPPL) and
  resolves per NFSS font the \lpcode/\rpcode/\efcode tables and
  \pdffontexpand limits exactly as microtype v3.2d does (font sets, list
  lookup order with family aliases, load= chains, T1 inheritance,
  \MT@scale rounding). Verified slot-for-slot against pdfTeX dumps for 96
  fonts.
- pdftex: protrusion (char_pw), char/kern stretch and shrink, the try_break
  shortfall rule, hpack's cal_expand_ratio and per-glyph expansion,
  transcribed from pdftex.web 1.40.27 and utils.c.
- arith: pdfTeX/e-TeX integer arithmetic.
- tests/common: reference pdfTeX line_break + post_line_break + hpack.
- tests/oracle: 32 pdflatex fixtures (EC and Latin Modern, 10/11/12pt,
  none/protrusion/expansion/both). All 32: identical breaks, identical
  per-glyph expansion, identical margin kerns, glyph x error <= 0.00018pt.
  MT_MUTATE switches show each pdfTeX rule is needed.
- CONTRACT.md: proposed hooks for paragraph-layout and render-pipeline, and
  the HW1/HW2 with/without-microtype delta (HW1: no break changes; HW2: two
  paragraphs reflow, 80 -> 79 lines).

Implementation-Agent: Claude Opus 5 (subagent kabir-claude KC-102, 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
…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]>
… and space factor (KC-105)

New crate modelling UTF-8 input (.dfu tables), OT1/T1/TS1/OMS/OML text commands and composites, NFSS cmr/lmr font selection and glyph names, exact TFM scaling, the TeX main-loop lig/kern algorithm, \accent positioning and LaTeX space factor. A pdflatex oracle (177 fixtures x 5 preambles = 885 cases) replays 885/885 in cargo test: exact node lists, kerns, glue and box dimensions in sp; glyph positions within 0.0049pt. CONTRACT.md holds the adoption proposal; no other crate is touched.

Implementation-Agent: Claude Opus 5 (subagent kabir-claude KC-105, 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
… (KC-106)

New crate crates/page-builder: append_to_vlist (§679), paragraph and
display penalties (§890, §1203-1205), vert_break/vsplit (§967-979), the
page builder with insertions and \tracingpages (§980-1028), \end
(§1054), and LaTeX \pagebreak/\nopagebreak/\newpage/\clearpage/
\enlargethispage plus \@makecol/\@doclearpage (latex.ltx 2026,
footnotes-floats-legacy plug).

pdflatex oracle (oracle/gen.py, cargo tests never run TeX): 61 fixtures,
338 pages, 11718 lines replayed from captured main vertical lists; all
\tracingpages lines, output penalties and box255 contents identical,
max baseline deviation 0.00021pt. CONTRACT.md proposes adoption in
render-pipeline.

Implementation-Agent: Claude Opus 5 (subagent kabir-claude KC-106, 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
New crate crates/tex-boxes: scaled arithmetic and units (tex.web 99-109,
453-458), node model, badness/hpack/vpackage with exact over/underfull
report text (108, 644-678), show_box/short_display (173-198), shipout
positions with cur_g/cur_glue rounding and leaders (619-637), a primitive
box engine (nest, save stack, box contexts, vtop, lastbox, unpackage,
vcenter, single-line paragraph hook), and the latex.ltx box layer
(makebox/framebox/fbox/raisebox/parbox/minipage/rule/hspace/vspace/
phantom/smash/laps/savebox/settodim/lrbox). Oracle generator, 151
fixtures and generated expected.txt; DSL-driven oracle test not yet run.

Implementation-Agent: Claude Opus 5 (subagent kabir-claude KC-107, 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
- generate.py: expand nested <<N*...>> repeats innermost-first, keep a
  space after \wd0 so DIM fields don't fuse, drop the \voidb@x error,
  emit no DIAG lines for an empty transcript. pdflatex log now has no
  TeX errors; axisheight regenerated (163840sp).
- DSL: nested repeats, simple {..} groups inside box bodies.
- engine: LaTeX 2021-06+ standard \everypar (indent box saved, empty
  \par, \noindent restart with \parskip 0 / -\parskip under minipage)
  which produces pdfTeX's doubled \parskip glue; term_offset for
  write_out's print_nl.
- scaled: .3\baselineskip = 235932sp (round_decimals 19661).

Implementation-Agent: Claude Opus 5 (subagent kabir-claude KC-107 r2, mac-m5pro-kabir)
Commit-Executor: Claude Code direct Git
Co-Authored-By: Claude Opus 5 <[email protected]>
… r2)

write_out's print_nl breaks the line when the terminal is mid-line. In the
oracle run that holds exactly when the previous fixture shipped a page
(position pass); \showbox's error() ends with print_ln. Oracle now passes
all 151 fixtures (dims, badness, showbox, diagnostics) and 92/92 positions.

Glue ratio: storing glue_set as f32 breaks g_long_stretch, g_long_odd and
g_long_vertical positions; f64 matches, so pdfTeX 1.40.29 uses double.

Implementation-Agent: Claude Opus 5 (subagent kabir-claude KC-107 r2, mac-m5pro-kabir)
Commit-Executor: Claude Code direct Git
Co-Authored-By: Claude Opus 5 <[email protected]>
… r2)

Documents oracle coverage (151 fixtures exact; 92 positions), the glue
ratio (double) and LaTeX para-hook findings, adoption for render-pipeline /
compiler, and plug-in points for KC-101 tex-expansion (main-control
mapping, register ownership), KC-102 microtype (LineBreaker, margin kerns;
expansion hook is a stub) and KC-106 page-builder (PageBuilder, module
de-duplication).

Implementation-Agent: Claude Opus 5 (subagent kabir-claude KC-107 r2, mac-m5pro-kabir)
Commit-Executor: Claude Code direct Git
Co-Authored-By: Claude Opus 5 <[email protected]>
…builder/class-geometry (KC-110, paused)

Not yet compiled: fonts (TFM + OT1 cmr NFSS), Knuth-Plass line_break port
over tex-boxes nodes with Liang hyphenation, stomach (headings, emph/bf,
accents, vspace, pagebreak), page building via page-builder LatexOutput,
glyph JSON via tex-boxes ship_out. No oracle yet.

Implementation-Agent: Claude Opus 5 (subagent kabir-claude KC-110, mac-m5pro-kabir)
Commit-Executor: Claude Code direct Git
Co-Authored-By: Claude Opus 5 <[email protected]>
GoKubar added a commit that referenced this pull request Sep 13, 2026
…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
@GoKubar

GoKubar commented Sep 15, 2026

Copy link
Copy Markdown
Contributor Author

Closing as obsolete. This draft is a merge aggregation of #88, #93, #90, #85 and #86 plus an uncompiled crates/tex-engine-poc ("Status: WIP, paused for usage (KC-110). Nothing has been compiled or oracle-checked"). All five constituents have since merged independently — crates/tex-expansion, crates/tex-boxes, crates/class-geometry and crates/microtype are all on main — so 438 files / +250,515 lines of this diff is landed code being re-proposed. The PoC crate itself was never compiled and nothing depends on it; branch agent/kabir-claude/engine-poc keeps it if the experiment is ever resumed.

@GoKubar GoKubar closed this Sep 15, 2026
@GoKubar
GoKubar deleted the agent/kabir-claude/engine-poc 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.

1 participant