This guide covers the revision-1 (MedEx.Contract.api_revision() == 1) data contracts, framing formats, durable stream recorder, crash recovery, rational clock alignment, and technical command interlocks in :med_ex.
1. Stream Descriptors, Modalities, And Provenance
Every regular waveform stream begins with an immutable %MedEx.StreamSpec{}:
- Identity and epoch:
acquisition_id,source_ref(authenticated device/gateway identity),boot_id(source boot session),stream_id, andsource_epoch(0..0xFFFFFFFF). Rebooting a source or changing descriptor parameters starts a newboot_id/source_epoch—never mutate an active descriptor in place. - Modality, encoding, and rate: Namespaced
modality(for example"cardiac:ecg"or"acoustic:stethoscope"), interleaved signed little-endian integerencoding(:s16le,:s24le, or:s32le), and exact rationalsample_rate({numerator, denominator}, such as{4_000, 1}or{500, 1}). - Channels and honest calibration: Ordered synchronous
channels(1..32unique channel IDs). Each channel declaresunit(:unknownor unit string) andcalibration(:unknownor%{zero: integer, scale_num: nonzero_int, scale_den: pos_int, reference: id}). Unknown calibration remains:unknown—MedExnever fabricates a1.0physical scale. Asynchronous sensors belong in separate streams rather than interleaved channels. Clock and processing provenance: Optional
clockdescriptor (%{domain_id, boot_id, generation, tick_rate, uncertainty_ticks}or:unknown),source_processing(:unknownor up to 32%{name, origin: :observed | :claimed, value}facts), andcapabilities(encodings,max_frame_samples,max_payload_bytesup to65_536,replay: :none | :durable_spool, andcommands).- Self-identifying
descriptor_ref: Compute the canonical SHA-256 digest over the descriptor map (excluding"descriptor_ref") withMedEx.Serialization.descriptor_ref/1, store it on the struct, validate withMedEx.StreamSpec.validate/1, and persist both the serialized descriptor (MedEx.Serialization.to_map/1) anddescriptor_refin the host's admission store. Reconstruct withMedEx.Serialization.stream_spec/1.
2. Wire Framing And Semantic Identity
MedEx separates wire transport parsing from authenticated stream admission:
- Binary waveform frames (
MedEx.Wire.FrameV1): Encodes and decodes the 48-byte big-endianMEX1v1 header ("MEX1", version1, flags0, header size48,stream_number,source_epoch,config_epochwith0xFFFFFFFFencoding:unknown, 64-bitsequence, 64-bitfirst_sample, 32-bitsample_count, 32-bitpayloadlength up to65_536bytes, 32-bit reserved0, followed by native interleaved sample bytes). - Bounded JSON control messages (
MedEx.Wire.Control): Encodes and decodesschema: "medex.device.v1"messages up to16_384bytes against a caller-supplied allowlist oftypestrings, rejecting duplicate JSON keys at every nesting depth ({:error, :duplicate_key}) and parsing exact decimaluint64/1strings. - Admitted
%MedEx.SampleFrame{}andMedEx.FrameFingerprint: A decodedMEX1frame is not yet admitted evidence. The host first authenticates the connection, mapsstream_numberto the reserved%MedEx.StreamSpec{}, constructs%MedEx.SampleFrame{}, and validates stride and bounds viaMedEx.SampleFrame.validate/2.MedEx.FrameFingerprint.of/1computes the transport-independent semantic digest (SHA-256("medex.frame.v1\0" <> canonical_json)); hosts may record raw wire SHA-256 alongside it as a separate transport observation.
3. Sparse Measurements And Device Status
Non-waveform device outputs use dedicated contracts rather than synthetic single-sample waveforms:
MedEx.MeasurementEvent: Represents discrete or spot-check measurements (such as pulse oximetry, blood pressure, or temperature) withevent_id,acquisition_id,source_ref,boot_id,stream_id,source_epoch,gateway_sequence,descriptor_ref, optionalsource_record_id,metrics(1..32entries of{:value, mantissa, exponent, unit}withexponent in -18..18or{:missing, reason}),raw({:bytes, binary}up to 16 KiB or{:sha256, digest}),device_status,device_time,receipt_time, andprovenancefacts. Serialize and restore without binary floating-point rounding viaMedEx.Serialization.to_map/1andMedEx.Serialization.measurement/1. Importantly,receipt_timenever substitutes for an:unknown device_time.MedEx.StatusEvent: Records technical device or gateway observations (code,sequence,observed_intervalas:unknownor%{clock_id, earliest, latest}, optionalsource_alarm_id,state, andacknowledgement). Status sequence numbers and alarm IDs are independent of waveform sample coordinates.MedEx.IntegritySpan: Attaches structured technical qualifiers (:source_gap,:clock_unavailable,:configuration_unknown,:device_status_invalid,:source_processing_unverified,:duplicate_conflict) to either a half-open sample range (first_sample..end_sample) or a non-empty list ofevent_ids.
4. Authoritative Lossless Capture, Receipts, Replay, And Gaps
Start one MedEx.StreamWriter per active authenticated stream attempt:
{:ok, writer} =
MedEx.StreamWriter.start_link(
owner: self(),
stream: spec,
artifact_dir: attempt_dir,
fence: fence,
limits: %{pending_frames: 16, pending_bytes: 262_144, checkpoint_ms: 250}
)
{:ok, accepted} = MedEx.StreamWriter.push(writer, fence, frame)
{:ok, durable} = MedEx.StreamWriter.checkpoint(writer)
{:ok, index} = MedEx.StreamWriter.finish(writer)
index_digest = MedEx.Serialization.digest(index)Ownership, Receipts, And Upstream Spool Truncation
- Caller and fence enforcement: Every
StreamWritercall requires the bound:ownerPID ({:error, :not_owner}otherwise).push/3andpush/4also verify the activefencetoken ({:error, :stale_fence}on mismatch). - Volatile versus durable positions: Push replies return a receipt containing
stream_id,source_epoch,descriptor_ref,fence,journal_id,journal_generation,journal_digest, volatileaccepted_sequence/accepted_sample, and journal-synceddurable_sequence/durable_sample(all exclusive next cursors). - Batching and async durability notifications: Frames accumulate in memory up to
pending_frames(default16),pending_bytes(default262_144), aconfig_epochchange, orcheckpoint_ms(default250ms). When an idle timer flushes a chunk, the owner receives{:med_ex_durable, writer, receipt}(or{:med_ex_storage_failed, writer, reason}if disk sync fails). Upstream device spools must delete retained frames only up todurable_sequence/durable_sampleof the active authenticated attempt.
Replay, Duplicate Conflicts, Explicit Gaps, And Termination
- Idempotent replay: Re-sending an already accepted or durable
sequencewhoseFrameFingerprintmatches returns{:ok, current_receipt}. - Out-of-order / future frames: Frames ahead of
next_sequenceornext_samplereturn{:error, {:replay, next_sequence, next_sample}}without mutating state so the sender can rewind or record an explicit gap. - Conflicting duplicates: Re-sending an existing
sequence(or gapid) with different content immediately stops admission, attempts to seal the stream withterminal: :failedand:duplicate_conflict, and returns{:error, :duplicate_conflict}(or{:error, {:outcome_unknown, reason}}if sealing fails). - Explicit irreversible gaps (
record_gap/2): Missing samples are never filled by interpolation. Record known source drops explicitly:
gap = %{
id: "gap-001",
irreversible: true,
first_sequence: expected_seq,
next_sequence: resumed_seq,
span: %MedEx.IntegritySpan{
stream_id: spec.stream_id,
source_epoch: spec.source_epoch,
code: :source_gap,
first_sample: expected_sample,
end_sample: resumed_sample,
source: "gateway",
context: %{"cause" => "radio_drop"}
}
}
{:ok, durable_receipt} = MedEx.StreamWriter.record_gap(writer, gap)record_gap/2 flushes any buffered preceding frames, commits the gap to the journal, and returns the updated durable receipt (idempotently returning the current receipt if the exact gap is replayed).
- Terminal states (
finish/1vsinterrupt/2) and capacity bounds: Callfinish/1only when the source confirms clean stream completion; useinterrupt(writer, reason)when a link drops, the source reboots, or tail loss is unknown. Default per-stream caps areduration_seconds: 1_800(30 minutes),artifact_bytes: 536_870_912(512 MiB),1_000_000frames, and100,000journal/span records; hitting a cap seals an:interruptedindex, sends{:med_ex_interrupted, writer, index}to:owner, and replies{:error, {:capacity, reason}}. Hosts enforce aggregate budgets across multiple streams.
5. POSIX Storage, Crash Recovery, And Predecessor Lineage
MedEx.Storage.File requires a trusted local POSIX filesystem supporting exclusive file creation, hard links, fsync on regular files, and directory fsync:
- Crash-safe write ordering: Each attempt receives its own fresh directory and exclusive
stream.mejournal. Content-addressed<sha256>.pcmchunk files are written to temporary files,fsynced, hard-linked to their final digest names, and directory-fsynced before the correspondingMEJ1journal record (up to 64 KiB, protected by both a header length checksum and a chained SHA-256 record digest) is appended andfsynced. Unreferenced.pcmor temporary files never grant coverage. - Fencing and quiescence:
MedEx.Storage.Fileuses a node-local:globallock to prevent concurrent writer/recovery access within one BEAM, but OS-level cross-process fencing belongs to the host (for example viaflockand PostgreSQL generation leases). Before recovering or replacing a writer, revoke old ingress, persist a newfence, and confirm the prior writer has exited. - Recovery and predecessor import:
{:ok, recovery} =
MedEx.Storage.File.recover(old_dir,
stream: spec,
fence: old_fence,
known_receipt: last_known_receipt
)
# Import the verified frozen prefix into a new fenced directory on the same filesystem:
predecessor = %{artifact_dir: old_dir, receipt: recovery.receipt}
{:ok, successor} =
MedEx.StreamWriter.start_link(
owner: self(),
stream: spec,
artifact_dir: new_dir,
fence: new_fence,
predecessor: predecessor
)recover/2 verifies the journal identity, hash chain, .pcm byte lengths and SHA-256 digests, sample/sequence coverage, and any known_receipt durable boundary. It ignores a torn trailing journal record without modifying old_dir and returns %{index, receipt, index_digest, torn_tail, sealed, terminal_recorded}.
- If
terminal_recordedistrueandsealedisfalse(crash between terminal journal sync and.index.mej1installation), callMedEx.Storage.File.reconcile_index/2under quiescence to finish installing<sha256>.index.mej1. - If the prior attempt was interrupted (
terminal_recorded == false), passrecovery.receipt(the latest verified durable prefix) inpredecessorwhen openingsuccessor.StreamWriterhard-links verified.pcmchunks and copies the verified journal bytes into an immutable<sha256>.prefixobject (supporting up to 32 predecessor generations). - Verified segment reads and call deadlines: Read finalized chunks with
MedEx.Storage.File.read_segment(dir, segment_ref), which re-verifies exact byte count and SHA-256 on every read. Default deadlines are 5 s for ordinary calls, 20 s forfinish/interrupt, and 30 s forstart_link/recover/reconcile_index. Any call timeout returns{:error, {:outcome_unknown, :timeout}}because an in-flight kernel write may still complete; revoke ingress, confirm quiescence, and runrecover/2before continuing.
6. Clock Uncertainty And Device-Control Interlocks
Affine Rational Clock Alignment (MedEx.ClockAlign)
Device clocks drift relative to host monotonic clocks and across network jitter. MedEx.ClockAlign computes exact rational bounds (affine_polytope_v1) without floating-point rounding:
- Collect four-timestamp round-trip probes (
%{generation, h1, d2, d3, h4}in integer microseconds) and supply a qualified policy (%{generation, a_min, a_max, epsilon_us, host_now_us, max_probe_age_us, valid_device_us: {low, high}, holdover_rate}). - Call
MedEx.ClockAlign.fit(probes, policy)to intersect up to the newest 64 valid probes into a convex rate/offset polygon, returning{:ok, estimate}or{:unavailable, reason}(:no_probes,:stale_probe,:contradictory_probes,:generation_mismatch, etc.). - Map a device timestamp (
at_device_time/2) or a sample index anchored by%{generation, sample_index, device_us, period_min_us, period_max_us, error_us}(interval/3) to an outward-rounded{earliest_host_us, latest_host_us}interval—or{:unavailable, :outside_validity}when queried outsidevalid_device_us. Persist raw probes, anchors, policy, and resulting intervals with the acquisition manifest so cross-stream correspondence remains auditable.
Technical Command Interlocks (MedEx.ControlInterlock)
When a host adjusts a technical device or monitor setting (such as gain or filter mode), MedEx.ControlInterlock prevents silent state desynchronization:
ControlInterlock.request(state, command, stream_watermarks)validatesboot_id,expected_epoch == state.current_epoch, and single in-flight mutation, returning{:ok, state, [{:persist_intent, command, watermarks}]}.- Only after the host commits that intent durably does
ControlInterlock.step(state, {:intent_committed, id})emit[{:dispatch, command}]. - On
{:applied, id, fingerprint, boot_id, next_epoch, boundary}, the interlock advancescurrent_epoch. On{:timeout, id}or{:boot_changed, new_boot, epoch}, it transitions the command to:outcome_unknown, records the ambiguous watermark span inhistorical_unknown, and emits{:query_actual_state, target}. A later{:actual_state, target, boot, epoch, observed}reconciles the current epoch while preserving:historical_boundary_unknown—because reading current state cannot establish when an unobserved transition occurred on the waveform timeline. - On the device side,
ControlInterlock.device_decide(ledger, command, actual_epoch)checks the durable command ledger bycommand.idbefore checkingexpected_epoch, ensuring retried commands after a lost acknowledgement return{:cached, result}instead of a spurious:stale_configurationerror.