Skip to content

feat(tooling): add ptadiff, format-generic differential parse testing - #73

Merged
robcohen merged 1 commit into
mainfrom
feat/differential-tooling
Nov 27, 2026
Merged

robcohen merged 1 commit into
mainfrom
feat/differential-tooling

Conversation

@robcohen

@robcohen robcohen commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

What

Adds tooling/differential/, a Python package (ptadiff). It compares a parser's errors with a reference implementation's and turns the mismatches into named categories that CI can count and pin.

It is format-generic. Each format supplies a plugin: an oracle adapter, normalizers, a registry of category keys, generators, and shrink units. Comparing, minimizing, clustering and reporting are shared.

  • Oracle adapters.
    • beancount is called in process through its public API (parse_string, lex_iter_string). Each call runs in a forked worker (ptadiff.pool.run_forked) with a timeout. A segfault, exception or hang loses only that input, which is recorded as a crash.
    • ledger and hledger have CLI-oracle stubs; only parse_output is left to write.
  • Implementation protocol. JSON lines: {id, text} in, {id, errors: [{line, code, msg}]} out.
  • Compare. Two sides agree when they report errors on the same lines with the same count per line. The first line they disagree on names the mismatch.
  • Category keys.
    • coarse: side|who:kinds|line-kind, using the error kinds of the side that reports more on that line.
    • beancount shape (the default): coarse plus the divergent line's token shape, taken from lex_iter_string token names. That is the token just before the first invalid token (^ at the start of the line), or the line's first two tokens when it has none.
    • Keys are a registry, so a parser-state key from a clean-room grammar can be added later without changing any stage (README, "A parser-state key").
  • Generators. The G1–G3(b) beancount generators: fixed seeds, no hardcoded paths, sorted deterministic output. G3 draws from a bundled seed corpus, a frozen copy of this repo's examples and fuzzing seeds. The tests pin a digest of each generator's output, and output is identical on Python 3.11 and 3.13.
  • Minimizer. Delta debugging over lines, then tokens, then characters. A step is kept only if the input still mismatches with the same key, so the example stays in its category.
  • Clusterer and report. Mismatches are grouped by key, each group with its size, sources, labels and shortest or minimized example. The report is deterministic JSON (ptadiff-report/1) plus a markdown table.
  • CLI. python -m ptadiff generate | run | minimize.

Why

This is phase 1 of the parser plan: reproducible differential infrastructure that turns parse mismatches into named categories. rustledger's parse-error parity gate consumes it, pinned to a commit of this branch, in a companion PR.

License boundary

beancount is GPL-2.0-only. Nothing here is derived from its grammar.y, lexer.l, parser.c, tokens.c or tables generated from them. The oracle only calls beancount's public Python API, and token names come from what lex_iter_string returns at run time.

How to test

cd tooling/differential
pip install pytest beancount==3.2.3
PTADIFF_REQUIRE_BEANCOUNT=1 python -m pytest -q tests     # 46 tests

The new differential-tooling job in ci.yml runs the tests on Python 3.10 and 3.12 against beancount 3.2.3, with skips turned into failures. It also installs the package from its subdirectory, which is how consumers install it. ruff format --check . and ruff check . (0.16.4) are clean.

Example output

These are the top categories of rustledger against beancount 3.2.3 over 123,304 inputs, with the examples minimized:

# Inputs Category Minimized
1 287 impl-fewer|oracle:lex:token+syn:INDENT|indented|INDENT error "\t\ufeff"
2 239 impl-more|impl:P0012|top|^ error "\^A\r2"
3 167 impl-more|impl:P0012|top|DATE error "2229-1-7B\r!"

🤖 Generated with Claude Code

A Python package under tooling/differential that compares a parser's errors
with a reference implementation's and turns the mismatches into named
categories:

- oracle adapters: beancount via its public Python API (parse_string,
  lex_iter_string) in forked workers with timeouts and crash capture; CLI
  oracle stubs for ledger and hledger
- a JSON-lines protocol for the implementation under test:
  {id, text} in, {id, errors: [{line, code, msg}]} out
- per-format plugins: normalizers, a registry of category keys (beancount:
  `shape` from lex_iter_string token names, and `coarse`), generators
- the seeded G1-G3(b) beancount generators: fixed seeds, no hardcoded
  paths, deterministic sorted output (digests pinned in the tests), drawing
  G3 windows from a bundled seed corpus
- shared stages: a key-preserving minimizer (lines, tokens, characters),
  a clusterer, and deterministic JSON + markdown reports
- a CLI (`python -m ptadiff generate|run|minimize`), tests, a README, and a
  CI job running them on Python 3.10 and 3.12 against beancount 3.2.3

Nothing is derived from beancount's grammar, lexer rules or parser tables;
a key built from a clean-room grammar's parser states plugs into the key
registry once those tables exist.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen merged commit 3eacb4c into main Nov 27, 2026
20 of 22 checks passed
@robcohen
robcohen deleted the feat/differential-tooling branch November 27, 2026 04:35
robcohen added a commit to rustledger/rustledger that referenced this pull request Nov 27, 2026
…dards#73)

The tooling/differential tree is identical to the feature-branch commit
it replaces, so no count changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit to rustledger/rustledger that referenced this pull request Nov 27, 2026
…diff (#3056)

* feat(parity): name parse-error mismatches by category key through ptadiff

The parse-error parity harness now runs through pta-standards' differential
package (ptadiff, tooling/differential), pinned by commit in the manifest
(`#@ptadiff-rev`), and names every mismatch with ptadiff's beancount `shape`
category key: which side reports more on the first line the two disagree
on, that side's error kinds, the line's kind, and the line's token shape
from beancount's black-box lexer (lex_iter_string).

- parse_error_lines gains `--jsonl`, ptadiff's JSON-lines protocol; the
  harness talks to rledger through it, and runs beancount in ptadiff's
  forked workers (same crash capture as before)
- a new `gen` input source: 12000 inputs from ptadiff's seeded G1-G3(b)
  generators, pinned like the fuzz and matrix inputs (`#@generators`,
  `#@inputs gen`, per-input pins), identical across runs, worker counts
  and Python versions
- the manifest pins each category key's count over the fixed sources
  (`@ categories`, next to the symptom buckets); a changed count is a
  stale pin, and a different ptadiff revision, key or generator sample is
  a coverage error. Every existing pin is unchanged
- the report adds the category table, with the ten largest categories'
  examples minimized by ptadiff's key-preserving minimizer (`--minimize`);
  `--categories-json` writes it for the CI artifact
- a token-level drift test (`cst::bc_lexer_drift`) compares BcLexer with
  beancount 3.2.3's lex_iter_string, token name and byte span, over the
  tracked ledgers, fuzz regressions, pinned fuzz corpus, stray-character
  matrix and generator sample (123833 inputs, all agree), run in the
  python-gated-cargo-tests job with skips turned into failures

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* chore(compat): pin ptadiff to pta-standards main (rustledger/pta-standards#73)

The tooling/differential tree is identical to the feature-branch commit
it replaces, so no count changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[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.

1 participant