SHC vNext QR proof of concept

cycle-log

Rendered from codecs/cycle-log/README.md in the repository.

Menstrual-cycle and health tracking (readable built-in keywords, uniform { date, ... }[] arrays, FHIR R4 CodeableConcept escapes, and FHIR dateTime, spanning up to 10,000 days, bounded naturally by QR capacity) in one dense QR code.

  • The model, its FHIR mapping, the samples and the entropy targets are described in MODEL.md, MAPPING.md and EXPRESSIVENESS.md.
  • The codec is a Rust crate compiled to WebAssembly. Its wire format is in FORMAT.md; the measurements are below.

Codec files

filewhat
cycle-log.wasmthe codec, registry id 1. Exports memory, shc_alloc, shc_decode (card → canonical Bundle JSON) and shc_encode (model JSON → card), per CODEC-ABI.md v0.1.
cycle-log-tools.wasmthe same codec plus cy_trace (where the bits go), cy_sizes (modelled and plain sizes), cy_encode_mode (a forced, possibly non-canonical mode), cy_encode_p / cy_decode_p (alternative model constants) and cy_cost (decoding cost of a card or model, for warnings; FORMAT.md 5.4). Not a registry module.
rust/the crate. core/ holds everything, under #![no_std] and #![forbid(unsafe_code)]. shim.rs is the only unsafe code: the allocator and the ABI entry points. codec/ and tools/ are the two cdylibs.
FORMAT.mdthe wire format, precise enough for an independent implementation
codec.test.tsthe codec tests (about 13 minutes)
codec-host.ts, codec-fixtures.tsthe ABI host used by the tests, and the test inputs (with verbatim copies of the generator in cycle-log.test.ts, checked against it)
sizes.tsthe measurement tables
wasm-count.tscounts executed WebAssembly instructions (used for the worst-case figures in FORMAT.md 5.3)
node-decode.mjsdecodes with Node, for the second-engine check

What the encoder accepts

shc_encode takes the model as JSON text and refuses exactly what validateJson in model.ts rejects, with a message that starts with one of the field paths it reports (@<position> for a number spelling).

  • Any model validate accepts is encoded, normalized or not. The encoder applies normalize itself: for example, a CodeableConcept matching a built-in keyword (SymptomKind, MedicationKind, ConditionKind, etc.) is normalized to the keyword (stripping any display), non-built-in escape concepts preserve their display and text, text-only { text } concepts stay text-only, and Z / :00 timestamps are normalized to canonical +00:00 / minute precision.
  • The only other refusal is LIMIT, for a card that would exceed 4,096 bytes. A large Bundle or a slow decode is never refused; cy_cost reports them so tools can warn (FORMAT.md 5.3 and 5.4).

Build

rust/build.sh
  • The build needs the rustup stable toolchain with the wasm32-unknown-unknown standard library. A distribution's own rustc may lack it (for example, Arch's 1.100 has no wasm32 std).
  • It has no dependencies and needs no network.
  • Build output goes to $CARGO_TARGET_DIR, by default rust/target, which is git-ignored. The script copies both modules here and prints their SHA-256.
  • The crate is built at opt-level = 3, the decoder that executes the fewest instructions and stays panic-free. The codec is about 370 KiB (zero imports, zero floats, link-time zero-panic).
  • Target features are pinned in rust/.cargo/config.toml: target-cpu=mvp plus mutable-globals, sign-ext, bulk-memory and multivalue.

The panic handler refers to an undefined symbol. If any panic path survives optimisation, linking fails, so a module that builds has no reachable panic.

Test and regenerate

bun test codec.test.ts      # the codec
bun test                    # also the model tests
bun sizes.ts                # measurements (markdown)

The tests decode each sample and check it against golden.json. After the model or the samples change, regenerate that file with bun golden.ts --write, then rebuild and rerun the tests. Once cards have been shared, a change to the wire format needs a new codec id.

For quicker development:

CY_FIX=<dir of model/bundle json> cargo test --release -p cycle_log_core --test native

Measurements

From bun sizes.ts.

QR versions are for the codec input plus 3 bytes: the codec reference and the card's 2-byte check. The working lines for a phone screen are Version 25-L (1,250 bytes) and Version 30-L (1,703 bytes).

samplesetcodec Bcard BQR L / Mline
power-trackertuning1,2391,242Version 25-L / Version 30-M≤ Version 25-L
copper-iud (named)tuning317320Version 12-L / Version 14-M≤ Version 25-L
combined-pilltuning244247Version 10-L / Version 12-M≤ Version 25-L
pcosheld out281284Version 11-L / Version 13-M≤ Version 25-L
minimalheld out3134Version 3-L / Version 3-M≤ Version 25-L
perimenopauseheld out694697Version 18-L / Version 22-M≤ Version 25-L
near-emptyheld out1518Version 2-L / Version 2-M≤ Version 25-L
own-vocabulary (named)held out1,6161,619Version 30-L / Version 34-M≤ Version 30-L
wellness-diaryheld out1,8561,859Version 32-L / Version 36-Mover Version 30-L
calendar-onlyheld out5356Version 4-L / Version 5-M≤ Version 25-L
smart-cup-hmbheld out329332Version 12-L / Version 14-M≤ Version 25-L
wearableheld out686689Version 18-L / Version 21-M≤ Version 25-L
post-ablationheld out460463Version 15-L / Version 17-M≤ Version 25-L
vitals-follow-upheld out710713Version 19-L / Version 22-M≤ Version 25-L
timestampedheld out1,9571,960Version 33-L / Version 38-Mover Version 30-L
bp-twice-dailyheld out1,8121,815Version 32-L / Version 36-Mover Version 30-L
med-dosesheld out1,2431,246Version 25-L / Version 30-M≤ Version 25-L
bbt-exclusions-and-feverheld out597600Version 17-L / Version 19-M≤ Version 25-L

The tuning samples were looked at while designing the model; the others are held out (there are now 20 sample records in total, including text-tags for text-only CodeableConcept tags without synthetic app URIs and offline-displays for offline display and text on standard SNOMED CT / RxNorm escape codes; run bun sizes.ts for current live measurements across all 20). calendar-only, smart-cup-hmb, wearable and post-ablation, the sample records that exercise the source app, conditions, answers, measured volume and wearable temperature, were not looked at while modelling those fields. vitals-follow-up (heart rate, weight and blood pressure readings with components) was not looked at while modelling components: they were designed on synthetic readings added to the tuning sample power-tracker. timestamped (a time of day on every entry) was not looked at while modelling times of day, which were designed on synthetic times added to power-tracker (FORMAT.md 4.11); its card is 699 B with only each day's first entry and no times. bp-twice-daily (blood pressure morning and evening) was not looked at while modelling several entries a day, which were designed on synthetic readings added to power-tracker; with only the first reading of each day and no times its card is 706 B.

The full tables, with the per-category breakdown, random and adversarial models, and the sensitivity of every model constant, are printed by bun sizes.ts.