// Track custody of a field sample across dispatch and receipt with a matching seal number. use std::cli; pub fn sample_custody_start() -> vector { [0, 0, 0] } pub fn sample_custody_step(snapshot: vector, event: str, value: f64) -> vector { if len(snapshot) != 3 { return []; } let phase = at(snapshot, 0); let saved = at(snapshot, 1); let previous = at(snapshot, 2); if !(phase >= 0.0 && phase < 6.0 && phase % 1.0 == 0.0 && previous >= 0.0 && previous < 7.0 && previous % 1.0 == 0.0 && saved >= 0.0 && saved <= 9007199254740991.0 && saved % 1.0 == 0.0) { return []; } if !(value >= 0.0 && value <= 1000000000.0 && value % 1.0 == 0.0) { return [phase, saved, 0]; } // idle if phase == 0.0 { if event == "label" && value>0.0 { return [1, value, 1]; // print_seal } } // sealed if phase == 1.0 { if event == "dispatch" && value==saved { return [2, saved, 2]; // log_departure } } // transit if phase == 2.0 { if event == "receive" && value==saved { return [3, saved, 3]; // confirm_receipt } if event == "receive" && value!=saved { return [4, saved, 4]; // quarantine_sample } } // stored if phase == 3.0 { if event == "archive" && value==saved { return [5, saved, 6]; // archive_record } } // held if phase == 4.0 { if event == "reconcile" && value==saved { return [3, saved, 5]; // release_quarantine } } [phase, saved, 0] } pub fn sample_custody_command(snapshot: vector) -> str { if len(snapshot) != 3 { return "invalid"; } let action = at(snapshot, 2); if action == 0.0 { return "reject"; } if action == 1.0 { return "print_seal"; } if action == 2.0 { return "log_departure"; } if action == 3.0 { return "confirm_receipt"; } if action == 4.0 { return "quarantine_sample"; } if action == 5.0 { return "release_quarantine"; } if action == 6.0 { return "archive_record"; } "invalid" } // Stop at the first rejected event. No host effects run during replay. pub fn sample_custody_replay(events: arglist, values: vector) -> vector { if args_len(events) != len(values) { return []; } let mut state = sample_custody_start(); let mut i = 0; while i < len(values) { state = sample_custody_step(state, args_at(events, i), at(values, i)); if at(state, 2) == 0.0 { return []; } i += 1; } state }