From 5bb4343f1165006c138091c23e89e0e7f29315b4 Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Wed, 4 Nov 2026 02:50:37 -0600 Subject: [PATCH] fix(id): extract checkpoint parts from world/core.rs into core/checkpoint_parts.rs (#5358) pkgs/id/src/world/core.rs was 2515 production lines, over the 2500 production-line-guard limit (main CI run 41790193563). Move the checkpoint part splitting/reassembly machinery (CheckpointEventPartKind, split_checkpoint_snapshot, split_checkpoint_events, CheckpointPartAccumulator, PendingCheckpointParts) verbatim into a new submodule pkgs/id/src/world/core/checkpoint_parts.rs; core.rs drops to ~2195 lines. No behavior change: cargo check passes and the world test suite (225 tests) passes on the laptop; the guard passes on a clean tree. --- pkgs/id/src/world/core.rs | 377 ++------------------- pkgs/id/src/world/core/checkpoint_parts.rs | 343 +++++++++++++++++++ 2 files changed, 371 insertions(+), 349 deletions(-) create mode 100644 pkgs/id/src/world/core/checkpoint_parts.rs diff --git a/pkgs/id/src/world/core.rs b/pkgs/id/src/world/core.rs index e319214030..4695edc9d8 100644 --- a/pkgs/id/src/world/core.rs +++ b/pkgs/id/src/world/core.rs @@ -13,332 +13,12 @@ use anyhow::{Context, Result, ensure}; use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque}; use std::sync::Arc; -/// Which variable-size checkpoint field a sequence of parts carries. -#[derive(Clone, Copy)] -enum CheckpointEventPartKind { - Retained, - Admin, -} - -fn checkpoint_part_json_len(entry: &JournalEntry) -> Result { - Ok(serde_json::to_vec(entry) - .context("serialize checkpoint part")? - .len()) -} - -fn checkpoint_event_part( - kind: CheckpointEventPartKind, - sequence: u64, - part_index: u32, - part_count: u32, - events: Vec, -) -> JournalEntry { - match kind { - CheckpointEventPartKind::Retained => JournalEntry::CheckpointEventsPart { - sequence, - part_index, - part_count, - events, - }, - CheckpointEventPartKind::Admin => JournalEntry::CheckpointAdminPart { - sequence, - part_index, - part_count, - events, - }, - } -} - -fn checkpoint_part_count(count: usize) -> Result { - u32::try_from(count).context("checkpoint has too many parts") -} - -fn split_checkpoint_snapshot(sequence: u64, snapshot: &str) -> Result> { - let mut chunks = Vec::new(); - if snapshot.is_empty() { - chunks.push(String::new()); - } else { - let mut start = 0; - while start < snapshot.len() { - let max_end = floor_char_boundary( - snapshot, - start - .saturating_add(MAX_CHECKPOINT_PART_BYTES) - .min(snapshot.len()), - ); - let mut low = start + 1; - let mut high = max_end; - let mut best_end = start; - while low <= high { - let middle = low + (high - low) / 2; - let end = floor_char_boundary(snapshot, middle); - if end <= start { - // The midpoint fell inside the first character, so the - // floored prefix is empty: this says nothing about the - // size limit, and the search must advance to a larger - // endpoint instead of excluding the character's end. - low = middle.saturating_add(1); - continue; - } - let fits = checkpoint_part_json_len(&JournalEntry::CheckpointSnapshotPart { - sequence, - // Maximal-width placeholders ensure the final indices - // and count can only make the serialized entry smaller. - part_index: u32::MAX, - part_count: u32::MAX, - bytes: snapshot[start..end].to_owned(), - })? <= MAX_CHECKPOINT_PART_BYTES; - if fits { - best_end = end; - low = middle.saturating_add(1); - } else { - high = middle.saturating_sub(1); - } - } - ensure!( - best_end > start, - "one UTF-8 character cannot fit in a checkpoint snapshot part" - ); - chunks.push(snapshot[start..best_end].to_owned()); - start = best_end; - } - } - - let part_count = checkpoint_part_count(chunks.len())?; - chunks - .into_iter() - .enumerate() - .map(|(part_index, bytes)| { - let entry = JournalEntry::CheckpointSnapshotPart { - sequence, - part_index: u32::try_from(part_index).context("checkpoint part index overflow")?, - part_count, - bytes, - }; - ensure!( - checkpoint_part_json_len(&entry)? <= MAX_CHECKPOINT_PART_BYTES, - "checkpoint snapshot part exceeds the encoded size limit" - ); - Ok(entry) - }) - .collect() -} +mod checkpoint_parts; -fn split_checkpoint_events( - sequence: u64, - events: &[WorldEvent], - kind: CheckpointEventPartKind, -) -> Result> { - // Size each event once and accumulate: re-serializing the growing - // candidate chunk per event would make splitting quadratic in the - // window size. The accumulated length is conservative -- maximal-width - // index placeholders for the entry overhead and one comma byte per - // event -- and every finished part is re-checked exactly below. - let overhead = checkpoint_part_json_len(&checkpoint_event_part( - kind, - sequence, - u32::MAX, - u32::MAX, - Vec::new(), - ))?; - let mut chunks: Vec> = Vec::new(); - let mut current: Vec = Vec::new(); - let mut current_len = overhead; - for event in events { - let event_len = serde_json::to_vec(event) - .context("serialize checkpoint event")? - .len() - + 1; - ensure!( - overhead + event_len <= MAX_CHECKPOINT_PART_BYTES, - "one event cannot fit in a checkpoint part" - ); - if current_len + event_len > MAX_CHECKPOINT_PART_BYTES { - chunks.push(std::mem::take(&mut current)); - current_len = overhead; - } - current.push(event.clone()); - current_len += event_len; - } - if !current.is_empty() || chunks.is_empty() { - chunks.push(current); - } - - let part_count = checkpoint_part_count(chunks.len())?; - chunks - .into_iter() - .enumerate() - .map(|(part_index, events)| { - let entry = checkpoint_event_part( - kind, - sequence, - u32::try_from(part_index).context("checkpoint part index overflow")?, - part_count, - events, - ); - ensure!( - checkpoint_part_json_len(&entry)? <= MAX_CHECKPOINT_PART_BYTES, - "checkpoint event part exceeds the encoded size limit" - ); - Ok(entry) - }) - .collect() -} - -fn floor_char_boundary(text: &str, mut index: usize) -> usize { - while !text.is_char_boundary(index) { - index -= 1; - } - index -} - -#[derive(Default)] -struct CheckpointPartAccumulator { - part_count: Option, - next_index: u32, - parts: Vec, -} - -impl CheckpointPartAccumulator { - fn push(&mut self, part_index: u32, part_count: u32, part: T, field: &str) -> Result<()> { - ensure!( - part_count > 0, - "checkpoint {field} part_count must be positive" - ); - ensure!( - part_index < part_count, - "checkpoint {field} part index is outside part_count" - ); - if let Some(expected) = self.part_count { - ensure!( - part_count == expected, - "checkpoint {field} part_count does not match" - ); - } else { - self.part_count = Some(part_count); - } - ensure!( - part_index == self.next_index, - "checkpoint {field} parts are duplicate or out of order" - ); - self.next_index = self - .next_index - .checked_add(1) - .context("checkpoint part index overflow")?; - self.parts.push(part); - Ok(()) - } - - fn is_complete(&self) -> bool { - self.part_count == Some(self.next_index) - } -} - -#[derive(Default)] -struct PendingCheckpointParts { - sequence: Option, - phase: u8, - snapshot: CheckpointPartAccumulator, - events: CheckpointPartAccumulator>, - admin: CheckpointPartAccumulator>, -} - -impl PendingCheckpointParts { - fn has_parts(&self) -> bool { - self.sequence.is_some() - } - - fn record_sequence(&mut self, sequence: u64) -> Result<()> { - if let Some(expected) = self.sequence { - ensure!( - sequence == expected, - "checkpoint part sequence does not match" - ); - } else { - self.sequence = Some(sequence); - } - Ok(()) - } - - fn push_snapshot( - &mut self, - sequence: u64, - part_index: u32, - part_count: u32, - bytes: String, - ) -> Result<()> { - ensure!(self.phase <= 1, "checkpoint parts are out of order"); - self.phase = 1; - self.record_sequence(sequence)?; - self.snapshot - .push(part_index, part_count, bytes, "snapshot") - } - - fn push_events( - &mut self, - sequence: u64, - part_index: u32, - part_count: u32, - events: Vec, - ) -> Result<()> { - if self.phase == 1 { - ensure!( - self.snapshot.is_complete(), - "checkpoint snapshot parts are missing" - ); - self.phase = 2; - } - ensure!(self.phase == 2, "checkpoint parts are out of order"); - self.record_sequence(sequence)?; - self.events.push(part_index, part_count, events, "events") - } - - fn push_admin( - &mut self, - sequence: u64, - part_index: u32, - part_count: u32, - events: Vec, - ) -> Result<()> { - if self.phase == 2 { - ensure!( - self.events.is_complete(), - "checkpoint retained-event parts are missing" - ); - self.phase = 3; - } - ensure!(self.phase == 3, "checkpoint parts are out of order"); - self.record_sequence(sequence)?; - self.admin.push(part_index, part_count, events, "admin") - } - - fn assemble(self, sequence: u64) -> Result<(String, Vec, Vec)> { - ensure!( - self.sequence == Some(sequence), - "checkpoint part sequence does not match checkpoint" - ); - ensure!( - self.phase == 3, - "checkpoint is missing one or more part groups" - ); - ensure!( - self.snapshot.is_complete(), - "checkpoint snapshot parts are missing" - ); - ensure!( - self.events.is_complete(), - "checkpoint retained-event parts are missing" - ); - ensure!( - self.admin.is_complete(), - "checkpoint admin-history parts are missing" - ); - let snapshot = self.snapshot.parts.join(""); - let events = self.events.parts.into_iter().flatten().collect(); - let admin = self.admin.parts.into_iter().flatten().collect(); - Ok((snapshot, events, admin)) - } -} +use checkpoint_parts::{ + CheckpointEventPartKind, PendingCheckpointParts, checkpoint_part_json_len, + split_checkpoint_events, split_checkpoint_snapshot, +}; /// Encoded byte size of one admin-history event, or `None` when the event /// cannot be serialized at all. Serialization failure is deliberate in the @@ -1543,30 +1223,29 @@ impl WorldCore { // binaries keep reading it. Only a checkpoint whose assembled // encoding exceeds the bound is split into part entries (which bump // the journal to version 6 via `journal_version_required_by`). - let make_checkpoint = |snapshot: String, - events: Vec, - admin_events: Vec| { - JournalEntry::Checkpoint { - sequence: self.sequence, - module_hash: module_hash.clone(), - seed: self.active_seed, - provenance: self.active_module_provenance.clone(), - execution_profile: Some(self.execution_profile), - caps_ceiling: caps_ceiling.clone(), - schedules: schedules.clone(), - transfers: transfers.clone(), - snapshot, - events, - admin_history: Some(JournalAdminHistory { - events: admin_events, - complete: self.admin_history_complete, - }), - // `Some(empty)` means the writer knows no participants are - // open; `None` is reserved for legacy checkpoints whose - // state is unknown. - open_presence: Some(self.open_presence.iter().copied().collect()), - } - }; + let make_checkpoint = + |snapshot: String, events: Vec, admin_events: Vec| { + JournalEntry::Checkpoint { + sequence: self.sequence, + module_hash: module_hash.clone(), + seed: self.active_seed, + provenance: self.active_module_provenance.clone(), + execution_profile: Some(self.execution_profile), + caps_ceiling: caps_ceiling.clone(), + schedules: schedules.clone(), + transfers: transfers.clone(), + snapshot, + events, + admin_history: Some(JournalAdminHistory { + events: admin_events, + complete: self.admin_history_complete, + }), + // `Some(empty)` means the writer knows no participants are + // open; `None` is reserved for legacy checkpoints whose + // state is unknown. + open_presence: Some(self.open_presence.iter().copied().collect()), + } + }; let inline_checkpoint = make_checkpoint( snapshot.clone(), checkpoint_events.clone(), diff --git a/pkgs/id/src/world/core/checkpoint_parts.rs b/pkgs/id/src/world/core/checkpoint_parts.rs new file mode 100644 index 0000000000..30bd32b482 --- /dev/null +++ b/pkgs/id/src/world/core/checkpoint_parts.rs @@ -0,0 +1,343 @@ +//! Checkpoint part splitting and reassembly for world snapshots. +//! +//! A checkpoint whose encoded fields exceed the journal part limit is +//! written as a sequence of parts; this module splits a snapshot into +//! parts on write and accumulates parts back into the whole on restore. +//! Extracted from `core.rs` for the production-line guard (#5358). + +use super::super::*; + +use anyhow::{Context, Result, ensure}; + +/// Which variable-size checkpoint field a sequence of parts carries. +#[derive(Clone, Copy)] +pub(super) enum CheckpointEventPartKind { + Retained, + Admin, +} + +pub(super) fn checkpoint_part_json_len(entry: &JournalEntry) -> Result { + Ok(serde_json::to_vec(entry) + .context("serialize checkpoint part")? + .len()) +} + +fn checkpoint_event_part( + kind: CheckpointEventPartKind, + sequence: u64, + part_index: u32, + part_count: u32, + events: Vec, +) -> JournalEntry { + match kind { + CheckpointEventPartKind::Retained => JournalEntry::CheckpointEventsPart { + sequence, + part_index, + part_count, + events, + }, + CheckpointEventPartKind::Admin => JournalEntry::CheckpointAdminPart { + sequence, + part_index, + part_count, + events, + }, + } +} + +fn checkpoint_part_count(count: usize) -> Result { + u32::try_from(count).context("checkpoint has too many parts") +} + +pub(super) fn split_checkpoint_snapshot( + sequence: u64, + snapshot: &str, +) -> Result> { + let mut chunks = Vec::new(); + if snapshot.is_empty() { + chunks.push(String::new()); + } else { + let mut start = 0; + while start < snapshot.len() { + let max_end = floor_char_boundary( + snapshot, + start + .saturating_add(MAX_CHECKPOINT_PART_BYTES) + .min(snapshot.len()), + ); + let mut low = start + 1; + let mut high = max_end; + let mut best_end = start; + while low <= high { + let middle = low + (high - low) / 2; + let end = floor_char_boundary(snapshot, middle); + if end <= start { + // The midpoint fell inside the first character, so the + // floored prefix is empty: this says nothing about the + // size limit, and the search must advance to a larger + // endpoint instead of excluding the character's end. + low = middle.saturating_add(1); + continue; + } + let fits = checkpoint_part_json_len(&JournalEntry::CheckpointSnapshotPart { + sequence, + // Maximal-width placeholders ensure the final indices + // and count can only make the serialized entry smaller. + part_index: u32::MAX, + part_count: u32::MAX, + bytes: snapshot[start..end].to_owned(), + })? <= MAX_CHECKPOINT_PART_BYTES; + if fits { + best_end = end; + low = middle.saturating_add(1); + } else { + high = middle.saturating_sub(1); + } + } + ensure!( + best_end > start, + "one UTF-8 character cannot fit in a checkpoint snapshot part" + ); + chunks.push(snapshot[start..best_end].to_owned()); + start = best_end; + } + } + + let part_count = checkpoint_part_count(chunks.len())?; + chunks + .into_iter() + .enumerate() + .map(|(part_index, bytes)| { + let entry = JournalEntry::CheckpointSnapshotPart { + sequence, + part_index: u32::try_from(part_index).context("checkpoint part index overflow")?, + part_count, + bytes, + }; + ensure!( + checkpoint_part_json_len(&entry)? <= MAX_CHECKPOINT_PART_BYTES, + "checkpoint snapshot part exceeds the encoded size limit" + ); + Ok(entry) + }) + .collect() +} + +pub(super) fn split_checkpoint_events( + sequence: u64, + events: &[WorldEvent], + kind: CheckpointEventPartKind, +) -> Result> { + // Size each event once and accumulate: re-serializing the growing + // candidate chunk per event would make splitting quadratic in the + // window size. The accumulated length is conservative -- maximal-width + // index placeholders for the entry overhead and one comma byte per + // event -- and every finished part is re-checked exactly below. + let overhead = checkpoint_part_json_len(&checkpoint_event_part( + kind, + sequence, + u32::MAX, + u32::MAX, + Vec::new(), + ))?; + let mut chunks: Vec> = Vec::new(); + let mut current: Vec = Vec::new(); + let mut current_len = overhead; + for event in events { + let event_len = serde_json::to_vec(event) + .context("serialize checkpoint event")? + .len() + + 1; + ensure!( + overhead + event_len <= MAX_CHECKPOINT_PART_BYTES, + "one event cannot fit in a checkpoint part" + ); + if current_len + event_len > MAX_CHECKPOINT_PART_BYTES { + chunks.push(std::mem::take(&mut current)); + current_len = overhead; + } + current.push(event.clone()); + current_len += event_len; + } + if !current.is_empty() || chunks.is_empty() { + chunks.push(current); + } + + let part_count = checkpoint_part_count(chunks.len())?; + chunks + .into_iter() + .enumerate() + .map(|(part_index, events)| { + let entry = checkpoint_event_part( + kind, + sequence, + u32::try_from(part_index).context("checkpoint part index overflow")?, + part_count, + events, + ); + ensure!( + checkpoint_part_json_len(&entry)? <= MAX_CHECKPOINT_PART_BYTES, + "checkpoint event part exceeds the encoded size limit" + ); + Ok(entry) + }) + .collect() +} + +fn floor_char_boundary(text: &str, mut index: usize) -> usize { + while !text.is_char_boundary(index) { + index -= 1; + } + index +} + +#[derive(Default)] +struct CheckpointPartAccumulator { + part_count: Option, + next_index: u32, + parts: Vec, +} + +impl CheckpointPartAccumulator { + fn push(&mut self, part_index: u32, part_count: u32, part: T, field: &str) -> Result<()> { + ensure!( + part_count > 0, + "checkpoint {field} part_count must be positive" + ); + ensure!( + part_index < part_count, + "checkpoint {field} part index is outside part_count" + ); + if let Some(expected) = self.part_count { + ensure!( + part_count == expected, + "checkpoint {field} part_count does not match" + ); + } else { + self.part_count = Some(part_count); + } + ensure!( + part_index == self.next_index, + "checkpoint {field} parts are duplicate or out of order" + ); + self.next_index = self + .next_index + .checked_add(1) + .context("checkpoint part index overflow")?; + self.parts.push(part); + Ok(()) + } + + fn is_complete(&self) -> bool { + self.part_count == Some(self.next_index) + } +} + +#[derive(Default)] +pub(super) struct PendingCheckpointParts { + sequence: Option, + phase: u8, + snapshot: CheckpointPartAccumulator, + events: CheckpointPartAccumulator>, + admin: CheckpointPartAccumulator>, +} + +impl PendingCheckpointParts { + pub(super) fn has_parts(&self) -> bool { + self.sequence.is_some() + } + + pub(super) fn record_sequence(&mut self, sequence: u64) -> Result<()> { + if let Some(expected) = self.sequence { + ensure!( + sequence == expected, + "checkpoint part sequence does not match" + ); + } else { + self.sequence = Some(sequence); + } + Ok(()) + } + + pub(super) fn push_snapshot( + &mut self, + sequence: u64, + part_index: u32, + part_count: u32, + bytes: String, + ) -> Result<()> { + ensure!(self.phase <= 1, "checkpoint parts are out of order"); + self.phase = 1; + self.record_sequence(sequence)?; + self.snapshot + .push(part_index, part_count, bytes, "snapshot") + } + + pub(super) fn push_events( + &mut self, + sequence: u64, + part_index: u32, + part_count: u32, + events: Vec, + ) -> Result<()> { + if self.phase == 1 { + ensure!( + self.snapshot.is_complete(), + "checkpoint snapshot parts are missing" + ); + self.phase = 2; + } + ensure!(self.phase == 2, "checkpoint parts are out of order"); + self.record_sequence(sequence)?; + self.events.push(part_index, part_count, events, "events") + } + + pub(super) fn push_admin( + &mut self, + sequence: u64, + part_index: u32, + part_count: u32, + events: Vec, + ) -> Result<()> { + if self.phase == 2 { + ensure!( + self.events.is_complete(), + "checkpoint retained-event parts are missing" + ); + self.phase = 3; + } + ensure!(self.phase == 3, "checkpoint parts are out of order"); + self.record_sequence(sequence)?; + self.admin.push(part_index, part_count, events, "admin") + } + + pub(super) fn assemble( + self, + sequence: u64, + ) -> Result<(String, Vec, Vec)> { + ensure!( + self.sequence == Some(sequence), + "checkpoint part sequence does not match checkpoint" + ); + ensure!( + self.phase == 3, + "checkpoint is missing one or more part groups" + ); + ensure!( + self.snapshot.is_complete(), + "checkpoint snapshot parts are missing" + ); + ensure!( + self.events.is_complete(), + "checkpoint retained-event parts are missing" + ); + ensure!( + self.admin.is_complete(), + "checkpoint admin-history parts are missing" + ); + let snapshot = self.snapshot.parts.join(""); + let events = self.events.parts.into_iter().flatten().collect(); + let admin = self.admin.parts.into_iter().flatten().collect(); + Ok((snapshot, events, admin)) + } +}