From 9c8ab5ec851164b02167a4b41fa4f1b3319490ec Mon Sep 17 00:00:00 2001 From: soul Date: Fri, 20 Nov 2026 04:49:28 +0100 Subject: [PATCH] aetherlight: TickMinutes owns its bound at decode, replay's clock clause goes TickMinutes was serde(transparent): a stored zero-minute tick (or a span above MAX_ROUTE_COST, which submit cannot build) decoded, and replay's clock clause alone judged it. The type now decodes through TryFrom (1..=MAX_ROUTE_COST), so a command, a routine period or a stored tick outside the bound cannot be read. With that the clock clause in verify_history is unreachable (only TimeAdvanced writes the clock, and a tick's span is at least one minute), so it is deleted rather than defended; so is the period clause in verify_state_shape. Tests: bounds at decode, and an export whose tick span is rewritten to 0 or over the bound does not open. Addresses clock-clause-not-redundant. Co-Authored-By: Claude Sonnet 5.5 --- native/aetherlight/src/clock.rs | 29 +++++++-- native/aetherlight/src/journal.rs | 100 ++++++++++++++++-------------- 2 files changed, 79 insertions(+), 50 deletions(-) diff --git a/native/aetherlight/src/clock.rs b/native/aetherlight/src/clock.rs index a1ba08f94..bde23bb79 100644 --- a/native/aetherlight/src/clock.rs +++ b/native/aetherlight/src/clock.rs @@ -26,14 +26,33 @@ impl FictionalMinutes { } } -/// The span one `AdvanceTime` names. Constructor-checked `1..=MAX_ROUTE_COST` -/// (one year of minutes), reusing pass 2's constant as a span bound rather than -/// an edge bound: an unbounded span lets one command jump the clock past every -/// due date in the world and makes the size of a commit a caller-chosen number. +/// The span one `AdvanceTime` names, `1..=MAX_ROUTE_COST` (one year of +/// minutes), reusing pass 2's constant as a span bound rather than an edge +/// bound: an unbounded span lets one command jump the clock past every due date +/// in the world and makes the size of a commit a caller-chosen number. +/// +/// The type owns its bound at every door: `new` and decoding both go through +/// it, so a command, a routine's period or a stored commit that names a span +/// outside the bound cannot be read at all, and no reducer or replay clause +/// re-checks it. #[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)] -#[serde(transparent)] +#[serde(try_from = "u32", into = "u32")] pub struct TickMinutes(u32); +impl TryFrom for TickMinutes { + type Error = &'static str; + + fn try_from(minutes: u32) -> Result { + Self::new(minutes).ok_or("a tick span is 1..=525600 minutes") + } +} + +impl From for u32 { + fn from(span: TickMinutes) -> u32 { + span.0 + } +} + impl TickMinutes { pub fn new(minutes: u32) -> Option { (1..=MAX_ROUTE_COST) diff --git a/native/aetherlight/src/journal.rs b/native/aetherlight/src/journal.rs index c4ce73c75..09e7ca7b9 100644 --- a/native/aetherlight/src/journal.rs +++ b/native/aetherlight/src/journal.rs @@ -170,7 +170,6 @@ pub(super) fn verify_history(commits: &[WorldCommit]) -> Result Result previous_now) != advanced { - return Err(JournalError::Corrupt( - "the world clock moved without a time-advanced commit, or ran backwards".into(), - )); - } + // The clock needs no chain check of its own: only `TimeAdvanced` writes + // it, `apply_effect` re-derives `to` from a `TickMinutes` that cannot + // be read outside `1..=MAX_ROUTE_COST`, so every tick moves it forward + // and nothing else moves it. replay.revision = commit.resulting_revision; let accumulator = replay.folded().map_err(kernel_error)?; replay.state_digest = accumulator.state_digest(replay.revision); @@ -407,9 +402,6 @@ pub(super) fn verify_state_shape(state: &WorldState) -> Result<(), JournalError> for (subject_id, held) in &state.commitments { let commitment_is_canonical = |commitment: &super::Commitment| { (commitment.kind == super::CommitmentKind::Routine) == commitment.period.is_some() - && commitment.period.is_none_or(|period| { - (1..=super::patch::MAX_ROUTE_COST).contains(&period.minutes()) - }) && (commitment.kind == super::CommitmentKind::Routine || commitment.checks.is_empty()) && match commitment.counterparty { @@ -961,6 +953,38 @@ mod tests { )); } + /// A stored tick whose span is zero or above the bound is not a world: the + /// export does not decode. This is the door the clock needs no check behind: + /// a zero-minute tick would be a commit that moves nothing, and an oversize + /// one a commit no `submit` could have built. + #[test] + fn a_stored_tick_with_a_span_outside_the_bound_does_not_open() { + let kernel = clock_history(); + let honest = export_of(&kernel.journal.commits); + // MessagePack names the span `minutes` and encodes the honest ones + // (1, 2, 30) as a single positive fixint byte, so each is rewritten in + // place, in the command and again in the effect. + let key = b"\xa7minutes"; + let at: Vec = (0..honest.len() - key.len()) + .filter(|&at| &honest[at..at + key.len()] == key) + .collect(); + assert!(at.len() >= 2, "a tick carries its span in the command and its effect"); + for forged_span in [vec![0x00], [&[0xce][..], &(crate::patch::MAX_ROUTE_COST + 1).to_be_bytes()].concat()] { + let mut bytes = honest.clone(); + for &start in at.iter().rev() { + let value = start + key.len(); + assert!(honest[value] < 0x80, "an honest span is a fixint"); + bytes.splice(value..=value, forged_span.iter().copied()); + } + let error = WorldJournal::open(&bytes).err().expect("a forged span opened"); + assert!( + error.to_string().contains("could not decode the export") + && error.to_string().contains("tick span"), + "{forged_span:?}: {error}" + ); + } + } + /// The replay-side future check reads only the subjects the transaction /// touched, and a key a subject already held is not newly born. #[test] @@ -2243,40 +2267,26 @@ mod custody_tests { let _ = subject; } - /// `TickMinutes` is `#[serde(transparent)]`, so a hand-written store row - /// can carry a period of zero — a routine that re-arms on the same minute - /// forever — without ever passing through `TickMinutes::new`. The shape - /// check now enforces the same `1..=MAX_ROUTE_COST` span bound the - /// constructor enforces, so the forged row is refused directly. + /// `TickMinutes` owns its bound at decode, so a span outside + /// `1..=MAX_ROUTE_COST` cannot be read in any position: a routine's period, + /// a command, or a tick's own effect. #[test] - fn soul_a_zero_period_is_refused_by_the_shape_check() { - let (kernel, subject) = ticking_world(); - - // The forgery an in-crate caller cannot write: `TickMinutes::new` - // refuses zero, and the field is private, so the only door is serde. - assert!(crate::TickMinutes::new(0).is_none()); - let zero: crate::TickMinutes = - rmp_serde::from_slice(&rmp_serde::to_vec(&0u32).unwrap()).expect("a transparent span"); - - let mut forged = kernel.state.clone(); - let key = *forged.commitments[&subject] - .iter() - .find(|(_, commitment)| commitment.period.is_some()) - .expect("the routine") - .0; - forged - .commitments - .get_mut(&subject) - .unwrap() - .get_mut(&key) - .unwrap() - .period = Some(zero); - - // The shape check refuses the forged span bound directly. - assert!( - matches!(verify_state_shape(&forged), Err(JournalError::Corrupt(_))), - "verify_state_shape admitted a period outside 1..=MAX_ROUTE_COST" - ); + fn a_span_outside_the_bound_cannot_be_decoded() { + for minutes in [0u32, crate::patch::MAX_ROUTE_COST + 1, u32::MAX] { + let raw = rmp_serde::to_vec(&minutes).unwrap(); + assert!( + rmp_serde::from_slice::(&raw).is_err(), + "{minutes} decoded as a span" + ); + assert!(serde_json::from_value::(minutes.into()).is_err()); + } + for minutes in [1u32, crate::patch::MAX_ROUTE_COST] { + let raw = rmp_serde::to_vec(&minutes).unwrap(); + assert_eq!( + rmp_serde::from_slice::(&raw).unwrap().minutes(), + minutes + ); + } } }