Docs native: gate seeds and mutation checks for each yrs patch
#147 findings 3 and 4: - The fuzz's gate seeds add 53 and 98 (causal) and 60 (shuffled). These fail with the redo delete or the redo-origins patch reverted, which seeds 1 to 6 did not catch. - Each step now also compares each plain yrs peer's own display, as it integrated the updates one by one, through `projection_of` (a test helper projecting any document). - check-mutations.py reverts each vendored patch (the GC keep check, the three integrate deletes, the redo origins) and expects its directed test to fail. A case may now name the crate whose tests run and the failure text it expects.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+33
−9
native/crdt/src/lib.rs+9 −0native/crdt/tests/gc_undo_kept_fuzz.rs+13 −6native/scripts/check-mutations.py+11 −3
diff --git a/native/crdt/src/lib.rs b/native/crdt/src/lib.rs| index 5d98b8c..9533553 100644 |
| --- a/native/crdt/src/lib.rs |
| +++ b/native/crdt/src/lib.rs |
| @@ -163,6 +163,15 @@ fn decode_updates(mut remaining: &[u8]) -> Result<Vec<Update>, &'static str> { |
| Ok(updates) |
| } |
| +/// The projection of any document, as `Editor::projection` draws it (its |
| +/// children only): lets tests read a plain yrs peer's own display. |
| +#[doc(hidden)] |
| +pub fn projection_of(doc: &Doc) -> Value { |
| + let root = doc.get_or_insert_xml_fragment("prosemirror"); |
| + let txn = doc.transact(); |
| + json!({"children": projection::children(&root, &txn)}) |
| +} |
| + |
| pub fn dispatch(bytes: &[u8]) -> Result<Vec<u8>, &'static str> { |
| // Merge pending writes without constructing a document: pending structs may |
| // depend on state already held by the server. Snapshotting an empty document |
diff --git a/native/crdt/tests/gc_undo_kept_fuzz.rs b/native/crdt/tests/gc_undo_kept_fuzz.rs| index 84622c8..72b9936 100644 |
| --- a/native/crdt/tests/gc_undo_kept_fuzz.rs |
| +++ b/native/crdt/tests/gc_undo_kept_fuzz.rs |
| @@ -11,7 +11,7 @@ |
| //! GC_UNDO_KEPT_SEEDS=1-400 GC_UNDO_KEPT_STEPS=150 cargo test --release \ |
| //! --manifest-path native/crdt/Cargo.toml --test gc_undo_kept_fuzz |
| //! GC_UNDO_KEPT_TRACE=1 prints each command. |
| -use docs_native_crdt::Editor; |
| +use docs_native_crdt::{projection_of, Editor}; |
| use serde_json::{json, Value}; |
| use std::collections::BTreeMap; |
| use yrs::updates::decoder::Decode; |
| @@ -194,6 +194,10 @@ fn run(seed: u64, steps: usize, in_order: bool) -> BTreeMap<String, (usize, usiz |
| assert_eq!(deletes(&ours), deletes(&theirs), "{context}: delete sets"); |
| assert_eq!(reread(&state(&gc[j])).0, shown, "{context}: plain yrs (gc on) differs"); |
| assert_eq!(reread(&state(&nogc[j])).0, shown, "{context}: plain yrs (gc off) differs"); |
| + // Each plain peer's own display, as it integrated the updates |
| + // one by one, not only a fresh integration of its state. |
| + assert_eq!(projection_of(&gc[j])["children"], shown, "{context}: plain yrs (gc on) shows otherwise"); |
| + assert_eq!(projection_of(&nogc[j])["children"], shown, "{context}: plain yrs (gc off) shows otherwise"); |
| } |
| } |
| for j in 0..n { for message in std::mem::take(&mut inbox[j]) { editors[j].apply(&message).unwrap(); } } |
| @@ -203,20 +207,23 @@ fn run(seed: u64, steps: usize, in_order: bool) -> BTreeMap<String, (usize, usiz |
| stats |
| } |
| -fn seeds() -> std::ops::RangeInclusive<u64> { |
| +/// The gate's seeds: 1 to 6, plus seeds that catch a yrs patch reverted |
| +/// (PATCHES.md): 53 the redo delete (causal), 60 the same (shuffled), 98 the |
| +/// redo origins (causal). insert_into_deleted_parent.rs covers each directly. |
| +fn seeds(in_order: bool) -> Vec<u64> { |
| match std::env::var("GC_UNDO_KEPT_SEEDS") { |
| - Ok(range) => { let (a, b) = range.split_once('-').unwrap_or((&range, &range)); a.parse().unwrap()..=b.parse().unwrap() }, |
| - Err(_) => 1..=6, |
| + Ok(range) => { let (a, b) = range.split_once('-').unwrap_or((&range, &range)); (a.parse().unwrap()..=b.parse().unwrap()).collect() }, |
| + Err(_) => (1..=6).chain(if in_order { vec![53, 98] } else { vec![60] }).collect(), |
| } |
| } |
| fn steps() -> usize { std::env::var("GC_UNDO_KEPT_STEPS").map(|s| s.parse().unwrap()).unwrap_or(120) } |
| fn fuzz(in_order: bool) { |
| let mut total = BTreeMap::<String, (usize, usize)>::new(); |
| - for seed in seeds() { |
| + for seed in seeds(in_order) { |
| for (op, (ok, err)) in run(seed, steps(), in_order) { let entry = total.entry(op).or_default(); entry.0 += ok; entry.1 += err; } |
| } |
| - eprintln!("in order {in_order}, seeds {:?}, {} steps: ok/refused per command {total:?}", seeds(), steps()); |
| + eprintln!("in order {in_order}, seeds {:?}, {} steps: ok/refused per command {total:?}", seeds(in_order), steps()); |
| } |
| #[test] |
diff --git a/native/scripts/check-mutations.py b/native/scripts/check-mutations.py| index 04ad65d..28742fc 100644 |
| --- a/native/scripts/check-mutations.py |
| +++ b/native/scripts/check-mutations.py |
| @@ -23,18 +23,26 @@ cases = [ |
| ('host/src/lib.rs', 'if self.failed || request.len() > MAX_BYTES', 'if request.len() > MAX_BYTES', 'failed_replica_cannot_be_reused'), |
| ('host/src/lib.rs', 'request.len()).min(self.ceiling)', 'request.len())', 'running_out_of_fuel_is_a_clean_failure_that_discards_the_instance'), |
| ('host/src/lib.rs', 'BASE_FUEL + FUEL_PER_MEMORY_BYTE * memory + FUEL_PER_REQUEST_BYTE * request', 'BASE_FUEL + 2 * FUEL_PER_MEMORY_BYTE * memory + FUEL_PER_REQUEST_BYTE * request', 'fuel_budget_grows_with_the_instance_but_stays_bounded'), |
| + # The vendored yrs patches (vendor/yrs-0.23.5/PATCHES.md), each reverted; |
| + # their tests live in crdt. Patch 1 panics rather than failing an assert. |
| + ('vendor/yrs-0.23.5/src/block.rs', ' if self.is_deleted() {\n self.content.gc(collector);', ' if self.is_deleted() && !self.info.is_keep() {\n self.content.gc(collector);', 'a_snapshot_after_a_peer_deletes_a_kept_items_paragraph_holds_what_the_editor_shows', 'crdt', 'Could not get parent branch info'), |
| + ('vendor/yrs-0.23.5/src/block.rs', ' txn.store_mut().blocks.push_block(redone_item);\n if should_delete {', ' txn.store_mut().blocks.push_block(redone_item);\n if false && should_delete {', 'an_item_undone_into_a_deleted_restored_parent_is_deleted_as_everywhere_else', 'crdt'), |
| + ('vendor/yrs-0.23.5/src/transaction.rs', ' if should_delete {\n self.delete(block_ptr);', ' if false && should_delete {\n self.delete(block_ptr);', 'local_inserts_into_a_type_a_peer_deleted_are_deleted_as_everywhere_else', 'crdt'), |
| + ('vendor/yrs-0.23.5/src/block_iter.rs', ' if should_delete {\n txn.delete(block_ptr);', ' if false && should_delete {\n txn.delete(block_ptr);', 'local_inserts_into_a_type_a_peer_deleted_are_deleted_as_everywhere_else', 'crdt'), |
| + ('vendor/yrs-0.23.5/src/block.rs', ' right = l.right;', ' let _ = l.right;', 'an_undo_whose_neighbours_trace_to_one_item_places_it_as_every_peer_does', 'crdt'), |
| ] |
| -for relative, old, new, test in cases: |
| +for relative, old, new, test, *rest in cases: |
| path = root / relative |
| original = path.read_text() |
| assert original.count(old) >= 1, (relative, old) |
| try: |
| path.write_text(original.replace(old, new)) |
| - crate = relative.split('/')[0] |
| + crate = rest[0] if rest else relative.split('/')[0] |
| result = subprocess.run([cargo, 'test', '--locked', '--offline', '--manifest-path', str(root / crate / 'Cargo.toml'), test, '--', '--exact'], capture_output=True, text=True) |
| output = result.stdout + result.stderr |
| (log_dir / ('docs-mutation-' + test + '.log')).write_text(output) |
| - assert result.returncode != 0 and ('assertion' in output or 'assert_eq' in output), (test, output) |
| + expected = rest[1:] or ['assertion', 'assert_eq'] |
| + assert result.returncode != 0 and any(marker in output for marker in expected), (test, output) |
| print(test + ': caught') |
| finally: |
| path.write_text(original) |