Contest engine
The contest engine is pure code in shared/contest-engine/. A bug here changes contest results.
A contest uses either the legacy engine (from before 0.4, and the default) or v1, set in contests.engine. Moving a contest between them re-scores it in one transaction and is audited. Moving it back restores the old scores exactly. The legacy engine is shared/contests.ts and server/lib/cabrillo.ts. Write new contests as v1.
Templates
Section titled “Templates”A template is JSON that describes periods, bands and modes, categories, the exchange, validity, dupes, points, multipliers, bonuses, the score formula, checks and the Cabrillo output. example.template.json shows the shape.
A TypeBox schema in schema.ts produces the types, the validator and template.schema.json. Regenerate the file with npm run build:schema and commit it. A test fails if it is stale.
validateTemplate() migrates old versions, resolves extends, checks references, tokens and Cabrillo columns, and validates each variant as merged. It keeps unknown keys and reports them as rules that are not applied.
- A rule check produces a warning. The entry form refuses only
strictclosed-list fields and bands or modes that the contest does not have. - Score formulas are
points*mults,…*poweror…*objectives, each with an optional+bonus. - Reference lists are named and versioned, for example
lists/arrl-sections@2026.json. - The schema is at version 4. Version 2 adds objectives,
weightBy, thelocationscope, bonusmaxUnits, theqsycheck and highest power. Version 3 addsreference, and version 4 addsschedule(RRULE). Each is opt-in with a migration, and older golden tests must pass unchanged. - The combination
(id, version, source)is immutable. An edit creates a new version, and a contest moves to it only when someone re-pins it. The source (builtin,localorhub) is part of a template’s identity.
Pipeline
Section titled “Pipeline”Score a log with scoreLog(template, entry, qsos), or step through it with startScoring, append and finish. checkQso and preview check a single QSO. For each QSO the engine resolves the entity, chooses the variant (once per entry), validates, checks for a dupe, awards points, applies multipliers, runs the checks and aggregates.
scoreLog folds append in time order, so incremental and full scoring agree. A QSO earlier than the last one re-scores everything. An invalid QSO stays in the log with an invalidReason and scores 0, and so does a dupe. Off-time, hour-cap and outside-period QSOs only produce warnings.
Entity resolution uses countryfiles/cty.json (DXCC, WAE, continent and zones). callsign.ts handles / forms, and wpx.ts handles prefixes. fillEntity() never overwrites a value that was typed or looked up. Rebuild the data with node scripts/build-cty.ts.
Cabrillo
Section titled “Cabrillo”cabrillo.ts writes the log from output.cabrillo: the contest name, headers with tokens such as {category.catPower}, and either columns or a preset (arrl-ss, cqp, naqp, arrl-fd or standard). It truncates frequency to whole kHz and never rounds. Times are UTC. A QSO that scored nothing for a stated reason becomes X-QSO. Header categories come from categories.derive. cabrillo-read.ts does the inverse.
In the app
Section titled “In the app”- Built-in templates live in
shared/contest-engine/templates/*.json, and the app installs them asbuiltinat start. A changed file needs a new version. A new template needs embeddedtests[]and an import. CI runs all of them. - A v1 contest pins
template_id,template_versionandtemplate_source. - Writes go through
createContact. A backdated QSO rewrites the stored results inside its own transaction. Edits, deletes, imports and the bulk tools callrecomputeContest. transmitter_id,my_locationandcontact_extrasare reserved for multi-operator logs, QTCs and rovers. Never remove a reserved hook because it looks unused.- The scripts on the Reference card fill in live from the entry form, and the app generates them when a template has none. Band plans in
shared/bandplans/*.jsonare original work, never copied from a league chart. - The template editor needs
contests.templates. Importing a template from JSON or from an N1MM+.udcfile produces a draft only.
To score a past log offline, run:
npm run contest:replay -- --template <id> --cabrillo <log>The command uses no database. --start <ISO> shifts the contest period, and --expect <score> turns the run into a check.
The goldens in shared/golden/ and server/golden*.test.ts pin the legacy engine, and golden-v1.test.ts holds the v1 ports to them. cabrillo-v1.test.ts compares output byte for byte, and GOLDEN_UPDATE=1 regenerates the expected files. Add a golden test before you change scoring.