Consensus runners
A consensus runner reads a corpus file, asks several parsers about every document in it, and writes their answers
to a results file. consensus report then scores each answer against the corpus and against the other parsers, and
writes the consensus report.
The YAML runner, in the yaml/consensus module, is the only one so far. A runner for JSON parsers will follow the
same design: the sections below separate what any runner does from what is particular to YAML.
How a run works
consensus run takes a corpus file and runs its sources one after another. A source is one parser, with an id
such as go-yaml.
For each source, the runner starts one or more driver processes, consensus drive <id>, and sends each case to a
driver as one line of JSON on its stdin. The driver answers with one line on its stdout. The protocol is in
internal/parsers/README.md.
An answer holds:
- the verdict,
accept,refuseorerror, and for a refusal the stage that refused: parser, composer or loader; - the number of documents and each document’s value as canonical JSON, when the parser builds values;
- the round trip, when the runner can drive the parser’s encoder: decode, encode, decode again, and compare;
- the first line of the parser’s message, for a refusal or an error;
- how long the driver took, in milliseconds.
A parser fails in its own driver and nowhere else:
- a Go panic while answering one document becomes an
erroranswer for that document; - a driver that gives no answer within
-timeoutplus 5 seconds is killed, and the case answerstimed out; - a driver that exits, or writes something other than an answer, is started again at the next case;
- after 10 crashes the runner drops the source. The results header lists it under
dropped, with the reason.
A slow source runs several drivers side by side, each taking every n-th case. With the CI variable set, as on a
GitHub runner, the counts suit 4 CPUs; elsewhere the runner uses up to localDrivers, capped at the host’s CPU count.
Both are in cmd/consensus/sources.go.
Use -cache DIR to keep answers between runs. The cache keys each answer by the SHA-256 of the document, in one
file per source and version, so a new parser version starts with an empty cache. Raise cacheFormat in
cmd/consensus/run.go when a driver starts writing its answers differently.
The results file holds one header line, naming the corpus and each source with its version and cost, then one line
per case with every source’s answer. consensus report reads only this file and the stance files: rendering a
report runs no parser.
Stance files
Each source has a stance file, internal/stances/<id>.yaml, embedded in the command. It records the parser’s
positions on the corpus’s tags, and the departures that explain where it differs from the corpus: a position, a
leniency or a known defect. A departure explains a valid document the parser rejects (rejects: valid), an invalid
one it accepts (accepts: invalid), a value other than the corpus states (differs: value), or a value that does
not come back the same through the parser’s encoder (differs: roundtrip). It matches on the case’s tags or source,
or a named check of the source in checks.go, and on the parser’s message or value; for a round trip, on the
encoder’s or decoder’s error and on the YAML written. The report counts a difference as explained when a departure matches it. See
Tags and stances.
A stance file states facts about one parser’s version. Update it with the pin that changes them.
The YAML runner
Layout
| path | holds |
|---|---|
cmd/consensus | the check, run, drive and report commands, and the report’s templates |
internal/consensus | the answer type, canonical JSON, and the round-trip comparison |
internal/parsers/<name> | each parser’s adapter, and the client of its batch process where it has one |
internal/parsers/goopenapi/goyaml-json | the go-yaml program, a module of its own |
internal/parsers/hsyaml/hsyaml-json | the HsYAML program, in Haskell, GPL-2.0-or-later |
internal/jsonlines | the long-lived child process behind each driver and each batch process |
internal/stances | the stance files |
goyaml, libfyaml, perlref | the few calls yaml/calibrate makes, re-exported |
docker/Dockerfile | the consensus image, with every parser pinned |
goyaml-json is a module of its own so that the consensus module does not require go-openapi/go-yaml: the parser
under test changes far more often than the parsers it is compared with. hsyaml-json links HsYAML, which is
GPL-2.0-or-later, so it carries that license in its own directory; the runner talks to it over a pipe, and the
image builds it in a GHC stage. NOTICE names HsYAML and its source.
The sources
| id | parser | how its driver runs it | answers |
|---|---|---|---|
perlref | the YAML reference parser, generated from the specification’s grammar (Perl) | one perl process for the whole corpus | verdict, events |
libfyaml | libfyaml (C) | fy-tool --testsuite per document for the verdict, then one python3 process with the binding for the values | verdict, events, values |
yamlstar | YAMLStar with its reference parser plugin (Clojure, a native binary) | its yaml command once per document, and yaml -D parse again to count the documents of an answer written as a JSON array | verdict, events, values |
yaml.v3 | go.yaml.in/yaml/v3 (Go) | inside the driver | verdict, values, round trip |
go-yaml | github.com/go-openapi/go-yaml (Go) | one goyaml-json -lines process for the whole corpus | verdict, values, round trip |
hsyaml | HsYAML (Haskell), with the library’s defaults | one hsyaml-json process for the whole corpus | verdict, events, values |
The report reads no event stream, so the drivers leave events out of the answers unless consensus run -events
asks for them.
Running it
Build the image from the repository root, and check that it finds every source:
Run it on a corpus file in the current directory, as your own user so that the cache and the results are yours:
Use -limit N for a quick look at the first cases, and -sources go-yaml,yaml.v3 to run some sources only.
To score a local checkout of go-openapi/go-yaml instead of the pinned version, give the build a goyaml context:
The cache keys a local build by the digest of its goyaml-json binary, so two checkouts never share answers.
Publishing the report
consensus.yml builds the image, publishes it to ghcr.io, runs it on the compact corpus, and attaches
consensus-results.jsonl.gz to a release. tag-release.yml calls it for a new release. A release cut by
bump-release.yml starts no other workflow, so a daily schedule runs it for the latest release when that release
has no results file yet.
update-doc.yml renders the report from the results file of the latest release, into /consensus/ on this site.
Each page of the report links back to the consensus report page.
To render a report yourself, into this site or into any directory:
The script also writes the results header to hack/doc-site/hugo/data/consensus.json, which the reference page
reads. Both outputs are gitignored.
The report is static HTML: a summary, the grids by tag and by feature, a page per parser, per reason and per tag or
feature, and one page, case.html, for every case. case.html#<id> shows one case with viewer.js, from
cases/<xx>.js, where xx is the first two hex digits of the id. These files are scripts, not JSON, so a report
opened from disk works without a web server; the case page needs JavaScript. cases/meta.js lists the parsers, the
statuses and the facts that cases share, which the cases name by number. The viewer knows nothing of YAML, so a
report on another grammar can reuse it.
A list of cases stops at 500, and the list of one tag or feature at 100; each says so at the top.
Bumping a pin
Every parser is pinned in docker/Dockerfile: the Debian and Go images, the reference parser’s commits, libfyaml’s
release and checksum, the binding’s version, and YAMLStar’s release and checksum. The Dockerfile’s header lists them
with their dates. go-openapi/go-yaml is pinned in two go.mod files, which move together:
internal/parsers/goopenapi/goyaml-json/go.mod for the image, and yaml/calibrate/go.mod for the calibration tests.
HsYAML is pinned in internal/parsers/hsyaml/hsyaml-json/hsyaml-json.cabal, the Hackage index in its
cabal.project, and the GHC image in the Dockerfile; update NOTICE with the HsYAML version.
A bump changes the source’s version, so the next run asks every case again. Run the image, read the report’s unexplained differences for that source, and update its stance file in the same change.
Adding a source
- Write the adapter in
internal/parsers/<name>: a function that reads one document and returns what the parser makes of it. A parser that takes long to start gets a batch process, asperlref,libfyamlandgoopenapihave, behindjsonlines.Child. - Add the source to
allSourcesincmd/consensus/sources.go: its id, what it is, which answers it gives, how to tell that it is installed, its version, and the function that answers. - Install and pin it in
docker/Dockerfile, and set the variable its adapter reads to find it. - Write its stance file,
internal/stances/<id>.yaml, from a first run: every unexplained difference is either a departure to record or a defect to report to the parser.
A runner for another grammar
Most of the runner does not depend on YAML: the driver protocol, the crash and timeout handling, the cache, the
results file, and the report’s scoring by stance file. The sources, their adapters, the stance files and the image
are particular to YAML. Today all of it lives in yaml/consensus.