Skip to content

Commit 572e30e

Browse files
committed
Merge main and preserve artifact sync and fencing regressions
Signed-off-by: Ravneet Arora <[email protected]>
2 parents 4aef33b + f3a7bab commit 572e30e

109 files changed

Lines changed: 7437 additions & 788 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎crates/buzz-core/src/artifact.rs‎

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ mod tests {
248248
);
249249
let query = parse_query(&json!({"artifact":"current","#project":["p"]})).unwrap();
250250
assert_eq!(query.view, ArtifactView::Current);
251+
let head = parse_query(&json!({"artifact":"head","#d":["x"]})).unwrap();
252+
assert_eq!(head.view, ArtifactView::Head);
251253
for invalid in [
252254
json!({"artifact":"lookup","#d":["x"]}),
253255
json!({"artifact":"history"}),
@@ -256,6 +258,8 @@ mod tests {
256258
json!({"artifact":"current","limit":1001}),
257259
json!({"artifact":"current","#assignee":[]}),
258260
json!({"artifact":"history","#d":[]}),
261+
json!({"artifact":"head"}),
262+
json!({"artifact":"sync","#h":["c"]}),
259263
] {
260264
assert!(parse_query(&invalid).is_err(), "{invalid}");
261265
}
@@ -276,6 +280,9 @@ pub enum ArtifactView {
276280
Current,
277281
/// Every stored, unredacted revision of the artifacts named by `#d`.
278282
History,
283+
/// The current revision of each artifact named by `#d`, including a
284+
/// deletion.
285+
Head,
279286
}
280287
/// Explicit HTTP artifact query. All `#name` predicates compare the first
281288
/// value of the same tag.
@@ -331,8 +338,38 @@ pub fn parse_query(value: &serde_json::Value) -> Result<ArtifactQuery, &'static
331338
let view = match object.get("artifact").and_then(|v| v.as_str()) {
332339
Some("current") => ArtifactView::Current,
333340
Some("history") => ArtifactView::History,
334-
_ => return Err("artifact must be \"current\" or \"history\""),
341+
Some("head") => ArtifactView::Head,
342+
_ => return Err("artifact must be \"current\", \"history\" or \"head\""),
335343
};
344+
let tags = parse_predicates(object, &["artifact", "kinds", "limit", "offset"])?;
345+
let number = |name: &str, default: u64| {
346+
object
347+
.get(name)
348+
.map(|v| v.as_u64().ok_or("invalid limit or offset"))
349+
.transpose()
350+
.map(|v| v.unwrap_or(default))
351+
};
352+
let limit = number("limit", 100)?;
353+
let offset = number("offset", 0)?;
354+
if limit == 0 || limit > MAX_PAGE_SIZE as u64 || offset > MAX_OFFSET {
355+
return Err("artifact page limit exceeded");
356+
}
357+
if view != ArtifactView::Current && !tags.iter().any(|(n, _)| n == "d") {
358+
return Err("history and head require #d");
359+
}
360+
Ok(ArtifactQuery {
361+
view,
362+
tags,
363+
limit: limit as i64,
364+
offset: offset as i64,
365+
})
366+
}
367+
/// Parse the `kinds` and `#name` predicates shared by every artifact filter.
368+
/// Keys outside `#name` and `allowed` are rejected.
369+
pub(crate) fn parse_predicates(
370+
object: &serde_json::Map<String, serde_json::Value>,
371+
allowed: &[&str],
372+
) -> Result<Vec<(String, Vec<String>)>, &'static str> {
336373
if let Some(kinds) = object.get("kinds") {
337374
if kinds.as_array().map(Vec::as_slice) != Some(&[serde_json::json!(45010)]) {
338375
return Err("artifact queries accept only kinds [45010]");
@@ -361,32 +398,12 @@ pub fn parse_query(value: &serde_json::Value) -> Result<ArtifactQuery, &'static
361398
parsed.push(value.into());
362399
}
363400
tags.push((name.into(), parsed));
364-
} else if !["artifact", "kinds", "limit", "offset"].contains(&name.as_str()) {
401+
} else if !allowed.contains(&name.as_str()) {
365402
return Err("unsupported artifact predicate");
366403
}
367404
}
368405
if tags.len() > MAX_PREDICATES || total > MAX_QUERY_VALUES || bytes > MAX_TAG_BYTES {
369406
return Err("artifact predicate limits exceeded");
370407
}
371-
let number = |name: &str, default: u64| {
372-
object
373-
.get(name)
374-
.map(|v| v.as_u64().ok_or("invalid limit or offset"))
375-
.transpose()
376-
.map(|v| v.unwrap_or(default))
377-
};
378-
let limit = number("limit", 100)?;
379-
let offset = number("offset", 0)?;
380-
if limit == 0 || limit > MAX_PAGE_SIZE as u64 || offset > MAX_OFFSET {
381-
return Err("artifact page limit exceeded");
382-
}
383-
if view == ArtifactView::History && !tags.iter().any(|(n, _)| n == "d") {
384-
return Err("history requires #d");
385-
}
386-
Ok(ArtifactQuery {
387-
view,
388-
tags,
389-
limit: limit as i64,
390-
offset: offset as i64,
391-
})
408+
Ok(tags)
392409
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
//! NIP-AR sync filter: `{"artifact":"current", "#h":[...], ...}` on a `REQ`.
2+
//! The same current-state filter as HTTP `/query`; the `REQ` transport makes
3+
//! it a sync (complete set, `EOSE`, then live heads).
4+
use crate::artifact::{canonical_uuid, parse_predicates};
5+
use uuid::Uuid;
6+
7+
/// Largest set one sync serves: it bounds each initial read and the held
8+
/// list of changed artifacts. Advertised in NIP-11 as `sync.max_events`.
9+
pub const MAX_SYNC_EVENTS: usize = 4096;
10+
11+
/// A parsed sync filter. `channels` are the `#h` values; `tags` holds every
12+
/// `#name` predicate, `#h` included, with NIP-AR match semantics.
13+
#[derive(Debug, Clone, PartialEq, Eq)]
14+
pub struct ArtifactSyncFilter {
15+
/// Named channels (`#h`), deduplicated in request order. Never empty.
16+
pub channels: Vec<Uuid>,
17+
/// Exact tag-name/value predicates (AND across names, OR within one).
18+
pub tags: Vec<(String, Vec<String>)>,
19+
}
20+
21+
impl ArtifactSyncFilter {
22+
/// Whether `head` matches every predicate with the NIP-AR match rule: a
23+
/// tag with that name whose first value is one of the requested values.
24+
/// The same rule the current-state query applies (NIP-AR sync rule 3).
25+
pub fn matches(&self, head: &nostr::Event) -> bool {
26+
self.tags.iter().all(|(name, values)| {
27+
head.tags.iter().any(|tag| match tag.as_slice() {
28+
[tag_name, value, ..] => tag_name == name && values.contains(value),
29+
_ => false,
30+
})
31+
})
32+
}
33+
}
34+
35+
/// Whether a raw `REQ` filter is a sync: the current-state view. Other
36+
/// artifact views have no live form and stay HTTP-only.
37+
pub fn is_sync_filter(value: &serde_json::Value) -> bool {
38+
value.get("artifact").and_then(|v| v.as_str()) == Some("current")
39+
}
40+
41+
/// Parse a sync filter. It requires `#h` with canonical channel UUIDs and
42+
/// accepts the current-state predicates and `kinds: [45010]`, but no `limit`
43+
/// or `offset`: a sync always serves the whole set.
44+
pub fn parse_sync(value: &serde_json::Value) -> Result<ArtifactSyncFilter, &'static str> {
45+
let object = value
46+
.as_object()
47+
.ok_or("artifact filter must be an object")?;
48+
if !is_sync_filter(value) {
49+
return Err("artifact must be \"current\"");
50+
}
51+
let tags = parse_predicates(object, &["artifact", "kinds"])?;
52+
let mut channels = Vec::new();
53+
for (_, values) in tags.iter().filter(|(name, _)| name == "h") {
54+
for value in values {
55+
let id = canonical_uuid(value).map_err(|_| "#h values must be channel UUIDs")?;
56+
if !channels.contains(&id) {
57+
channels.push(id);
58+
}
59+
}
60+
}
61+
if channels.is_empty() {
62+
return Err("sync requires #h");
63+
}
64+
Ok(ArtifactSyncFilter { channels, tags })
65+
}
66+
67+
#[cfg(test)]
68+
mod tests {
69+
use super::*;
70+
use serde_json::json;
71+
72+
#[test]
73+
fn sync_filter_requires_channels_and_rejects_paging() {
74+
let a = Uuid::new_v4().to_string();
75+
let parsed = parse_sync(
76+
&json!({"artifact":"current","#h":[a, a],"#type":["buzz.task"],"kinds":[45010]}),
77+
)
78+
.unwrap();
79+
assert_eq!(parsed.channels.len(), 1);
80+
assert_eq!(parsed.tags.len(), 2);
81+
for invalid in [
82+
json!({"artifact":"current"}),
83+
json!({"artifact":"current","#type":["buzz.task"]}),
84+
json!({"artifact":"current","#h":[a],"limit":10}),
85+
json!({"artifact":"current","#h":[a],"offset":0}),
86+
json!({"artifact":"current","#h":[a],"kinds":[45011]}),
87+
json!({"artifact":"current","#h":["not-a-uuid"]}),
88+
json!({"artifact":"current","#h":[a],"since":1}),
89+
json!({"artifact":"history","#h":[a]}),
90+
] {
91+
assert!(parse_sync(&invalid).is_err(), "{invalid}");
92+
}
93+
}
94+
95+
#[test]
96+
fn matches_applies_every_predicate() {
97+
let h = Uuid::new_v4().to_string();
98+
let filter = parse_sync(
99+
&json!({"artifact":"current","#h":[h],"#type":["work.set"],"#status":["open"]}),
100+
)
101+
.unwrap();
102+
let keys = nostr::Keys::generate();
103+
let head = |tags: &[[&str; 2]]| {
104+
nostr::EventBuilder::new(nostr::Kind::Custom(45010), "")
105+
.tags(tags.iter().map(|t| nostr::Tag::parse(t.to_vec()).unwrap()))
106+
.sign_with_keys(&keys)
107+
.unwrap()
108+
};
109+
assert!(filter.matches(&head(&[
110+
["h", &h],
111+
["type", "work.set"],
112+
["status", "open"]
113+
])));
114+
// A mutable tag that does not match, a missing tag, another type or
115+
// another channel: not in scope.
116+
for tags in [
117+
&[["h", &h], ["type", "work.set"], ["status", "done"]][..],
118+
&[["h", &h], ["type", "work.set"]][..],
119+
&[["h", &h], ["type", "work.goals"], ["status", "open"]][..],
120+
&[["h", "other"], ["type", "work.set"], ["status", "open"]][..],
121+
] {
122+
assert!(!filter.matches(&head(tags)), "{tags:?}");
123+
}
124+
// Any tag of the name can match, as in the query.
125+
assert!(filter.matches(&head(&[
126+
["h", &h],
127+
["type", "work.set"],
128+
["status", "done"],
129+
["status", "open"]
130+
])));
131+
}
132+
}

‎crates/buzz-core/src/lib.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,5 @@ pub mod test_helpers {
8484

8585
/// NIP-AR channel artifact envelope and limits.
8686
pub mod artifact;
87+
/// NIP-AR sync filter parsing and limits.
88+
pub mod artifact_sync;

‎crates/buzz-db/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ pub use event::{EventQuery, DEFAULT_MAX_PAGE_LIMIT};
8686
pub use nip_fi_binding::{NipFiBindingRecord, NipFiUnbind, RowCapExceeded};
8787
pub use reaction::ReactionEventInsertOutcome;
8888
pub use reminder::DueReminder;
89+
pub use store::{ArtifactSnapshot, SnapshotDenied};
8990
pub use usage::SessionLeader;
9091

9192
use buzz_core::CommunityId;

‎crates/buzz-db/src/store/artifact_postgres_tests.rs‎

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,3 +878,135 @@ async fn wait_for_artifact_blocker(pool: &PgPool, pid: i32) {
878878
}
879879
}).await.expect("artifact writer must wait on identity lock");
880880
}
881+
882+
/// Sync's complete-set read: keyset pages by `d` share one snapshot, so a
883+
/// change committed after it opens is in no page. Mutation: drop the
884+
/// `REPEATABLE READ` statement → the late create appears in a later page →
885+
/// RED. Mutation: drop the `a.artifact_id>` keyset bound → pages repeat → RED.
886+
#[tokio::test]
887+
#[ignore = "requires Postgres"]
888+
async fn sync_snapshot_pages_are_one_cut_and_head_includes_deletions() {
889+
let f = Fixture::new().await;
890+
let mut ids: Vec<Uuid> = (0..5).map(|_| Uuid::new_v4()).collect();
891+
ids.sort();
892+
let mut heads = vec![];
893+
for d in &ids {
894+
let create = f.revision(*d, "create", f.a, None, &f.owner, vec![]);
895+
assert!(matches!(
896+
f.accept(&create, None).await,
897+
ArtifactOutcome::Accepted(_)
898+
));
899+
heads.push(create);
900+
}
901+
// A deleted artifact leaves the set but stays visible to the head view.
902+
let deleted = f.revision(ids[4], "delete", f.a, Some(&heads[4]), &f.owner, vec![]);
903+
assert!(matches!(
904+
f.accept(&deleted, None).await,
905+
ArtifactOutcome::Accepted(_)
906+
));
907+
let reader = f.owner.public_key();
908+
let mut snapshot =
909+
f.db.open_artifact_snapshot(f.community, reader.as_bytes(), &[f.a])
910+
.await
911+
.unwrap()
912+
.unwrap();
913+
let first = snapshot.next_page(&[], 2).await.unwrap();
914+
// Committed after the snapshot opened, and its `d` sorts after the first
915+
// page, so only a fresh snapshot per page would return it.
916+
let late = loop {
917+
let d = Uuid::new_v4();
918+
if d > ids[1] {
919+
break d;
920+
}
921+
};
922+
let late_create = f.revision(late, "create", f.a, None, &f.owner, vec![]);
923+
assert!(matches!(
924+
f.accept(&late_create, None).await,
925+
ArtifactOutcome::Accepted(_)
926+
));
927+
let mut seen: Vec<_> = first.iter().map(|e| e.event.id).collect();
928+
loop {
929+
let page = snapshot.next_page(&[], 2).await.unwrap();
930+
if page.is_empty() {
931+
break;
932+
}
933+
seen.extend(page.iter().map(|e| e.event.id));
934+
}
935+
snapshot.finish().await.unwrap();
936+
assert_eq!(seen, heads[..4].iter().map(|e| e.id).collect::<Vec<_>>());
937+
938+
// Predicates apply inside the snapshot.
939+
let mut filtered =
940+
f.db.open_artifact_snapshot(f.community, reader.as_bytes(), &[f.a])
941+
.await
942+
.unwrap()
943+
.unwrap();
944+
let none = filtered
945+
.next_page(&[("type".into(), vec!["other".into()])], 10)
946+
.await
947+
.unwrap();
948+
assert!(none.is_empty());
949+
950+
// A private channel the reader cannot read refuses the whole sync.
951+
assert!(matches!(
952+
f.db.open_artifact_snapshot(f.community, f.peer.public_key().as_bytes(), &[f.a, f.b])
953+
.await
954+
.unwrap(),
955+
Err(super::artifact_query::SnapshotDenied::Restricted)
956+
));
957+
// So does a channel that does not exist.
958+
assert!(matches!(
959+
f.db.open_artifact_snapshot(f.community, reader.as_bytes(), &[Uuid::new_v4()])
960+
.await
961+
.unwrap(),
962+
Err(super::artifact_query::SnapshotDenied::Restricted)
963+
));
964+
965+
let (head, is_deletion) =
966+
f.db.artifact_head(f.community, reader.as_bytes(), ids[4], &[])
967+
.await
968+
.unwrap()
969+
.unwrap();
970+
assert_eq!(head.event.id, deleted.id);
971+
assert!(is_deletion);
972+
let (head, is_deletion) =
973+
f.db.artifact_head(f.community, reader.as_bytes(), ids[0], &[])
974+
.await
975+
.unwrap()
976+
.unwrap();
977+
assert_eq!(head.event.id, heads[0].id);
978+
assert!(!is_deletion);
979+
let (view, _) = f
980+
.query(
981+
&f.owner,
982+
serde_json::json!({"artifact":"head","#d":[ids[4], ids[0]]}),
983+
false,
984+
)
985+
.await;
986+
let mut view: Vec<_> = view.iter().map(|e| e.event.id).collect();
987+
view.sort();
988+
let mut expected = vec![deleted.id, heads[0].id];
989+
expected.sort();
990+
assert_eq!(view, expected);
991+
assert_eq!(
992+
f.count(
993+
&f.owner,
994+
serde_json::json!({"artifact":"current","#d":[ids[4]]})
995+
)
996+
.await,
997+
0
998+
);
999+
// A reader who cannot read the head's channel learns nothing.
1000+
let private = Uuid::new_v4();
1001+
let create = f.revision(private, "create", f.b, None, &f.owner, vec![]);
1002+
assert!(matches!(
1003+
f.accept(&create, None).await,
1004+
ArtifactOutcome::Accepted(_)
1005+
));
1006+
assert!(f
1007+
.db
1008+
.artifact_head(f.community, f.peer.public_key().as_bytes(), private, &[])
1009+
.await
1010+
.unwrap()
1011+
.is_none());
1012+
}

0 commit comments

Comments
 (0)