From 6a587c31bfc1c7b21fad0bc7a01af2355a4fa3aa Mon Sep 17 00:00:00 2001 From: tv Date: Sun, 15 Nov 2026 20:23:20 +0000 Subject: store: one reading of the policy's place Whether values are ordered by `seq` (`signs_values`) read the policy's place itself, the first value alone and a map by Automerge's choice of each field, while the policy as judged (`policy::of`) read it through the store, which orders by `seq` only where `signs_values` says so, and merges every map there. They parted: a signed policy that isn't strict beside a newer plain one that is read as not strict for the one and strict for the other, and two maps, the newer without the field asking for signed values, the other way round. `signs_values` now asks `policy::of`, and the store orders the policy's own place by `seq` wherever any value there asks for signed values, and what is within the policy as written, so the two are one reading. Asked from within that reading, it answers false rather than ask again. --- src/document/store.rs | 123 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 75 insertions(+), 48 deletions(-) (limited to 'src') diff --git a/src/document/store.rs b/src/document/store.rs index 27edf49..a9d4379 100644 --- a/src/document/store.rs +++ b/src/document/store.rs @@ -224,61 +224,77 @@ fn signed_order(value: &Value<'_>) -> Option<(u64, [u8; 32])> { Some((payload.seq, Sha256::digest(text.as_bytes()).into())) } -/// Whether `doc`'s policy asks for signed values (`access::policy`: `"values": -/// "signed"`), as a read of it shows it: below every reading that goes by it -/// (`candidates`). Its own place is read by `seq` wherever any value there asks for -/// signed values, so an older policy put back beside a strict one can't undo it; every -/// other place, as the policy that read shows says. Read here as written, not through -/// `candidates`, which asks this. +/// Whether `doc`'s policy asks for signed values (`access::policy`), as the policy reads +/// (`policy::of`): below every reading that goes by it (`candidates`). That reading +/// orders the policy's own place by `seq` wherever any value there asks for signed +/// values (`policy_by_seq`), so an older policy put back beside a strict one can't undo +/// it, and reads what is within the policy as written; so asking this from within it +/// answers false, rather than ask again. fn signs_values(doc: &impl ReadDoc) -> bool { - let mut maps = vec![automerge::ROOT]; - for key in [super::meta::ROOT, super::meta::ACCESS] { - maps = maps + thread_local! { + static READING: std::cell::Cell = const { std::cell::Cell::new(false) }; + } + if READING.get() { + return false; + } + READING.set(true); + let strict = crate::access::policy::of(doc).strict(); + READING.set(false); + strict +} + +/// Whether the values at `key` of `parents` are ordered by `seq`: at the policy's place, +/// where any of them (`all`) asks for signed values; within the policy, never; elsewhere, +/// where the policy does (`signs_values`). +fn by_seq( + doc: &impl ReadDoc, + parents: &[ObjId], + key: &str, + all: &[(Value<'static>, ObjId)], +) -> bool { + let mut access = vec![automerge::ROOT]; + for step in [super::meta::ROOT, super::meta::ACCESS] { + access = access .iter() - .flat_map(|map| doc.get_all(map, key).unwrap_or_default()) + .flat_map(|map| doc.get_all(map, step).unwrap_or_default()) .filter_map(|(value, id)| matches!(value, Value::Object(ObjType::Map)).then_some(id)) .collect(); } - let policy = |value: &Value<'_>, id: &ObjId| -> Option { - match value { - Value::Object(ObjType::Map) => { - let mut fields = serde_json::Map::new(); - for key in doc.keys(id) { - if let Ok(Some((Value::Scalar(scalar), _))) = doc.get(id, key.as_str()) - && let Some(field) = typed::scalar_to_json(&scalar, JsonForm::default()) - { - fields.insert(key, field); - } - } - Some(JsonValue::Object(fields)) - } - Value::Scalar(scalar) => match scalar.as_ref() { - ScalarValue::Str(text) => super::signed::read(text).map(|payload| payload.value), - _ => None, - }, - _ => None, - } - }; - let asks = |json: Option| { - json.is_some_and(|json| json.get("values") == Some(&JsonValue::from("signed"))) - }; - let mut all: Vec<(Value<'static>, ObjId)> = maps + if key == "policy" && parents.iter().all(|parent| access.contains(parent)) { + return all.iter().any(|(value, id)| policy_asks(doc, value, id)); + } + let policies: Vec = access .iter() .flat_map(|map| doc.get_all(map, "policy").unwrap_or_default()) - .map(|(value, id)| (value.into_owned(), id)) + .filter_map(|(value, id)| matches!(value, Value::Object(ObjType::Map)).then_some(id)) .collect(); - if !all.iter().any(|(value, id)| asks(policy(value, id))) { + if parents.iter().all(|parent| policies.contains(parent)) { return false; } - // As `candidates` orders them where the policy asks for signed values. - all.sort_by(|a, b| op_order(&b.1).cmp(&op_order(&a.1))); - all.sort_by_key(|(value, _)| match signed_order(value) { - Some((seq, digest)) => (false, std::cmp::Reverse(seq), digest), - None => (true, std::cmp::Reverse(0), [0; 32]), - }); - all.first() - .and_then(|(value, id)| policy(value, id)) - .is_some_and(|json| crate::access::policy::read(&json).strict()) + signs_values(doc) +} + +/// Whether `value` (`id`), at the policy's place, asks for signed values (`"values": +/// "signed"`), written as a map or signed. +fn policy_asks(doc: &impl ReadDoc, value: &Value<'_>, id: &ObjId) -> bool { + match value { + Value::Object(ObjType::Map) => { + doc.get_all(id, "values") + .unwrap_or_default() + .iter() + .any(|(value, _)| { + matches!(value, Value::Scalar(scalar) + if matches!(scalar.as_ref(), ScalarValue::Str(text) if text == "signed")) + }) + } + Value::Scalar(scalar) => match scalar.as_ref() { + ScalarValue::Str(text) => super::signed::read(text).is_some_and(|payload| { + payload.value.get("values") == Some(&JsonValue::from("signed")) + }), + _ => false, + }, + _ => false, + } } /// Every change a document holds, by its writer and the numbers of its operations, with @@ -390,9 +406,11 @@ fn candidates( } } all.sort_by(|a, b| op_order(&b.1).cmp(&op_order(&a.1))); - if all.len() > 1 && matches!(prop.into(), Prop::Map(_)) { + if all.len() > 1 + && let Prop::Map(key) = prop.into() + { let orders: Vec<_> = all.iter().map(|(value, _)| signed_order(value)).collect(); - if orders.iter().any(Option::is_some) && signs_values(doc) { + if orders.iter().any(Option::is_some) && by_seq(doc, parents, &key, &all) { let mut ordered: Vec<_> = orders.into_iter().zip(all).collect(); // Stable: the others stay newest first. ordered.sort_by_key(|(order, _)| match order { @@ -1906,7 +1924,16 @@ mod tests { #[test] fn values_go_by_the_policy_as_read() { let doc = |actor: &[u8]| crate::document::Document::new().with_actor(ActorId::from(actor)); - let (mut ours, mut theirs) = (doc(b"a"), doc(b"b")); + // The signed one written by either actor: newer or older by Automerge's order. + for (strict_by, signed_by) in [(b"a", b"b"), (b"b", b"a")] { + values_by_policy(doc(strict_by), doc(signed_by)); + } + } + + fn values_by_policy( + mut ours: crate::document::Document, + mut theirs: crate::document::Document, + ) { try_change(&mut ours, |doc| { write_path( doc, -- cgit v1.3.1