cultmesh-rs: pin the snapshot pull's refusal of a read-only key; changelog
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
bfa8128207
commit
6daf15b937
2 changed files with 54 additions and 1 deletions
|
|
@ -10,7 +10,8 @@ only in git history.
|
|||
|
||||
Semver: this is a breaking change in the 0.x line (0.3.x to 0.4.0).
|
||||
|
||||
- A record whose payload carries unread slots is read-only. A payload with a slot past its type's declared slots, at any depth (record, nested record, list element, union arm, unit arm), still decodes, and every write or removal of its key is now refused before any store is touched: `put`, `put_envelope`, `put_raw_envelope`, `put_prepared_batch` (the whole batch) and `delete`, so `update` too. The refusal is `SchemaConflictError` with the new `unread_slots`; its message names the type and both slot counts and never a slot value. Holders of such a record used to overwrite it and drop the extra slots silently. The payload nesting refusal still runs first. A held payload the registered type cannot decode at all is not judged, and stays replaceable and removable.
|
||||
- A record whose payload carries unread slots is read-only. A payload with a slot past its type's declared slots, at any depth (record, nested record, list element, union arm, unit arm), still decodes, and every write or removal of its key is now refused before any store is touched: `put`, `put_envelope`, `put_raw_envelope`, `put_prepared_batch` (the whole batch) and `delete`, so `update` too. The refusal is `SchemaConflictError` with the new `unread_slots`; its message names the type and both slot counts and never a slot value. Holders of such a record used to overwrite it and drop the extra slots silently. The payload nesting refusal still runs first. A held payload this build cannot read at all (an arm it lacks from a newer writer, or a corrupt record) is read-only too: every write or removal is refused with an error naming the type and key (repair it with the build that wrote it), and `is_writable` answers false. `load_soa` is a write of every held key it omits or changes, so it refuses to drop or replace a read-only record. `is_writable` also answers false for a key a store carries as a foreign record and for a type other than the one holding the key, as the writes do. Each write over a held key decodes the held payload once to ask (about 40% of a `put` of a 200,000-element record, measured in release); keys nothing holds pay nothing.
|
||||
- cultmesh-rs `pull_rudp_catalog_snapshot` applies documents with `put_raw_envelope`, so a snapshot document for a key the node holds read-only ends the pull at that document (documents before it stay applied), as C# CultMesh, which holds no per-key handling either. Per-key isolation is follow-up `mesh-unread-slots`.
|
||||
- `SchemaConflictError` gains the public field `unread_slots: Option<UnreadSlots>`; code that builds it with a struct literal must set it (`None`).
|
||||
- A unit arm with slots (`[N, [extra...]]`) decodes as the unit arm and marks the record, where the derive refused it.
|
||||
- The registration map's value changes from the schema name to a private struct holding the schema name and the unread-slots check.
|
||||
|
|
|
|||
|
|
@ -732,6 +732,17 @@ impl CultMeshNode {
|
|||
let cultnet_rs::CultNetMessage::SnapshotResponseRaw { documents, .. } = response else {
|
||||
anyhow::bail!("expected cultnet.snapshot_response_raw.v0");
|
||||
};
|
||||
self.apply_raw_snapshot_documents(documents)
|
||||
}
|
||||
|
||||
/// Applies each snapshot document of a registered schema with `put_raw_envelope`. The first refusal ends the pull
|
||||
/// and the documents before it stay applied; a key this node holds read-only (unread slots from a newer peer, or a
|
||||
/// payload this build cannot read) is refused like any other, as C# CultMesh holds no per-key handling either
|
||||
/// (follow-up mesh-unread-slots).
|
||||
fn apply_raw_snapshot_documents(
|
||||
&mut self,
|
||||
documents: Vec<cultnet_rs::CultNetRawDocumentRecord>,
|
||||
) -> Result<usize> {
|
||||
let mut applied = 0usize;
|
||||
for document in documents {
|
||||
let Some(document_type) =
|
||||
|
|
@ -1199,6 +1210,47 @@ mod tests {
|
|||
Note => "cultmesh.test.note.v0",
|
||||
});
|
||||
|
||||
fn raw_note(key: &str, payload: Vec<u8>) -> cultnet_rs::CultNetRawDocumentRecord {
|
||||
cultnet_rs::CultNetRawDocumentRecord {
|
||||
schema_id: "cultmesh.test.note.v0".to_string(),
|
||||
record_key: key.to_string(),
|
||||
stored_at: "2026-01-01T00:00:00Z".to_string(),
|
||||
payload_encoding: cultnet_rs::CultNetRawPayloadEncoding::Messagepack,
|
||||
payload,
|
||||
source_runtime_id: None,
|
||||
source_agent_id: None,
|
||||
source_role: None,
|
||||
tags: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_snapshot_document_for_a_read_only_key_ends_the_pull_and_keeps_the_applied_ones() -> Result<()> {
|
||||
let temp = tempfile::tempdir()?;
|
||||
let mut node = CultMesh::create_node(temp.path().join("m.cc"), TestDocuments, Default::default())?;
|
||||
// A newer peer's note: one slot past the declared one.
|
||||
let newer = rmp_serde::to_vec(&("newer body", "extra slot"))?;
|
||||
let plain = |body: &str| rmp_serde::to_vec(&(body,));
|
||||
assert_eq!(node.apply_raw_snapshot_documents(vec![raw_note("held", newer)])?, 1);
|
||||
assert_eq!(node.get_required::<Note>("held")?.body, "newer body");
|
||||
|
||||
let error = node
|
||||
.apply_raw_snapshot_documents(vec![
|
||||
raw_note("before", plain("b")?),
|
||||
raw_note("held", plain("overwrite")?),
|
||||
raw_note("after", plain("a")?),
|
||||
])
|
||||
.unwrap_err();
|
||||
let conflict = error.downcast_ref::<cultcache_rs::SchemaConflictError>().expect("typed refusal");
|
||||
assert_eq!(conflict.record_key, "held");
|
||||
assert!(conflict.unread_slots.is_some());
|
||||
assert!(!error.to_string().contains("extra slot"));
|
||||
assert_eq!(node.get_required::<Note>("before")?.body, "b");
|
||||
assert_eq!(node.get_required::<Note>("held")?.body, "newer body");
|
||||
assert!(node.get::<Note>("after")?.is_none());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn node_round_trips_registered_documents() -> Result<()> {
|
||||
let temp = tempfile::tempdir()?;
|
||||
|
|
|
|||
Loading…
Reference in a new issue