Skip to content

Commit 9288fe4

Browse files
murderbotclaude
andcommitted
fix(relay): match artifact #d on mixed and kindless filters
- Push `#d` into SQL whenever a filter can select NIP-AR rows (no `kinds`, or kinds including 45010/45011), as `kind NOT IN (45010,45011) OR tags @> [["d", v]]`. A newer artifact no longer takes the `limit` slot from the requested one; other kinds keep the generic post-filter. - Drop the `retain_current_artifact` BEFORE DELETE trigger on events. The relay has no event retention; the community purge clears `artifact_heads` first. Future retention must skip head payloads. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Signed-off-by: murderbot <3754f8729004d95654c46dbab3129e4ab9ef05cc2534e2a3fbfc155983bd637b@buzz.block.builderlab.xyz>
1 parent e2fa80a commit 9288fe4

7 files changed

Lines changed: 112 additions & 91 deletions

File tree

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,14 @@ async fn lifecycle_cas_queries_move_redaction_and_retention() {
175175
assert_eq!(f.count(&f.peer, history.clone()).await, 3);
176176
assert_eq!(f.count(&f.owner, history.clone()).await, 4);
177177

178-
// Retention removes old payloads but reserves identity and the current payload.
179-
let retention = || {
180-
sqlx::query("DELETE FROM events WHERE community_id=$1 AND id IN ($2,$3)")
178+
// Expiring an earlier payload keeps its identity reserved.
179+
let retention = |id: nostr::EventId| {
180+
sqlx::query("DELETE FROM events WHERE community_id=$1 AND id=$2")
181181
.bind(f.community.as_uuid())
182-
.bind(create.id.as_bytes().as_slice())
183-
.bind(moved.id.as_bytes().as_slice())
182+
.bind(id.as_bytes().to_vec())
184183
};
185184
assert_eq!(
186-
retention()
185+
retention(create.id)
187186
.execute(&f.db.pool)
188187
.await
189188
.unwrap()
@@ -205,7 +204,7 @@ async fn lifecycle_cas_queries_move_redaction_and_retention() {
205204
assert_eq!(f.count(&f.owner, all.clone()).await, 0);
206205
assert_eq!(f.count(&f.owner, history.clone()).await, 2);
207206
assert_eq!(
208-
retention()
207+
retention(moved.id)
209208
.execute(&f.db.pool)
210209
.await
211210
.unwrap()

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

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ pub use crate::reminder::{
3232
/// the advertised ceiling and the enforced one cannot drift.
3333
pub const DEFAULT_MAX_PAGE_LIMIT: i64 = 1_000;
3434

35+
/// NIP-AR revision and removal kinds, whose stable identity is a `d` tag.
36+
pub const ARTIFACT_KINDS: [i32; 2] = [
37+
buzz_core::kind::KIND_ARTIFACT as i32,
38+
buzz_core::kind::KIND_ARTIFACT_REMOVAL as i32,
39+
];
40+
3541
/// Optional filters for [`query_events`].
3642
#[derive(Debug, Clone)]
3743
pub struct EventQuery {
@@ -78,9 +84,10 @@ pub struct EventQuery {
7884
/// Restrict results to events with an `e` tag referencing any of these event IDs (hex).
7985
/// Uses JSONB containment (`tags @> ...`) against the `tags` column.
8086
pub e_tags: Option<Vec<String>>,
81-
/// Restrict results to events with a `d` tag matching any of these values,
82-
/// via JSONB containment. For non-NIP-33 kinds whose `d_tag` column is NULL
83-
/// (NIP-AR artifacts), so identity lookups match before SQL `LIMIT`.
87+
/// Restrict artifact rows ([`ARTIFACT_KINDS`]) to those with a `d` tag
88+
/// matching any of these values, via JSONB containment. Their `d_tag`
89+
/// column is NULL (not NIP-33), so this lets identity lookups match before
90+
/// SQL `LIMIT`. Rows of other kinds are left to the caller's post-filter.
8491
pub d_tag_values: Option<Vec<String>>,
8592
/// Restrict results to events with an exact custom tag pair.
8693
/// Uses JSONB containment against `tags` before SQL `LIMIT`.
@@ -640,17 +647,7 @@ pub(crate) async fn query_events_on(
640647
}
641648

642649
if let Some(ref values) = q.d_tag_values {
643-
if !values.is_empty() {
644-
qb.push(" AND (");
645-
for (i, value) in values.iter().enumerate() {
646-
if i > 0 {
647-
qb.push(" OR ");
648-
}
649-
qb.push(format!("{col_prefix}tags @> "));
650-
qb.push_bind(serde_json::json!([["d", value]]));
651-
}
652-
qb.push(")");
653-
}
650+
push_artifact_d_tag_predicate(&mut qb, col_prefix, values);
654651
}
655652

656653
if let Some((ref name, ref value)) = q.custom_tag {
@@ -746,6 +743,27 @@ pub(crate) async fn query_events_on(
746743
Ok(out)
747744
}
748745

746+
/// Match `#d` on artifact rows before `LIMIT` while leaving other kinds to the
747+
/// caller's post-filter: `(kind NOT IN (artifact kinds) OR tags @> [["d", v]] ...)`.
748+
fn push_artifact_d_tag_predicate(
749+
qb: &mut QueryBuilder<sqlx::Postgres>,
750+
col_prefix: &str,
751+
values: &[String],
752+
) {
753+
if values.is_empty() {
754+
return;
755+
}
756+
let [revision, removal] = ARTIFACT_KINDS;
757+
qb.push(format!(
758+
" AND ({col_prefix}kind NOT IN ({revision}, {removal})"
759+
));
760+
for value in values {
761+
qb.push(format!(" OR {col_prefix}tags @> "));
762+
qb.push_bind(serde_json::json!([["d", value]]));
763+
}
764+
qb.push(")");
765+
}
766+
749767
pub(crate) fn row_to_stored_event(row: sqlx::postgres::PgRow) -> Result<Option<StoredEvent>> {
750768
let id_bytes: Vec<u8> = row.try_get("id")?;
751769
let pubkey_bytes: Vec<u8> = row.try_get("pubkey")?;
@@ -929,17 +947,7 @@ pub(crate) async fn count_events_on(conn: &mut sqlx::PgConnection, q: &EventQuer
929947
}
930948

931949
if let Some(ref values) = q.d_tag_values {
932-
if !values.is_empty() {
933-
qb.push(" AND (");
934-
for (i, value) in values.iter().enumerate() {
935-
if i > 0 {
936-
qb.push(" OR ");
937-
}
938-
qb.push(format!("{col_prefix}tags @> "));
939-
qb.push_bind(serde_json::json!([["d", value]]));
940-
}
941-
qb.push(")");
942-
}
950+
push_artifact_d_tag_predicate(&mut qb, col_prefix, values);
943951
}
944952

945953
if let Some(s) = q.since {

‎crates/buzz-relay/src/api/artifact_postgres_tests.rs‎

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -362,15 +362,53 @@ async fn generic_d_lookup_matches_before_limit() {
362362
let f = Fixture::new().await;
363363
let (a, b) = (Uuid::new_v4(), Uuid::new_v4());
364364
let now = nostr::Timestamp::now().as_secs();
365-
let older = resign(&f.revision(a, "create", None), &f.owner, &[], now - 60);
365+
// Kindless reads are p-gated to the reader, so every row tags the peer.
366+
let reader = f.peer.public_key().to_hex();
367+
let tag_reader = [["p", reader.as_str()]];
368+
let older = resign(
369+
&f.revision(a, "create", None),
370+
&f.owner,
371+
&tag_reader,
372+
now - 60,
373+
);
366374
f.publish(&older).await;
367-
f.publish(&f.revision(b, "create", None)).await;
368-
let filter = json!([{"kinds":[45010],"#h":[f.home],"#d":[a],"limit":1}]);
369-
let (status, body) = f.request("/query", filter.clone()).await;
370-
assert!(status.is_success(), "{status}: {body}");
371-
assert_eq!(body.as_array().unwrap().len(), 1, "{body}");
372-
assert_eq!(body[0]["id"], older.id.to_hex());
373-
let (status, body) = f.request("/count", filter).await;
375+
let newer = resign(&f.revision(b, "create", None), &f.owner, &tag_reader, now);
376+
f.publish(&newer).await;
377+
for filter in [
378+
json!([{"kinds":[45010],"#h":[f.home],"#d":[a],"limit":1}]),
379+
json!([{"kinds":[45010,45011],"#h":[f.home],"#d":[a],"limit":1}]),
380+
json!([{"#h":[f.home],"#p":[reader],"#d":[a],"limit":1}]),
381+
] {
382+
let (status, body) = f.request_as(&f.peer, "/query", filter.clone()).await;
383+
assert!(status.is_success(), "{status}: {body}");
384+
assert_eq!(body.as_array().unwrap().len(), 1, "{filter}: {body}");
385+
assert_eq!(body[0]["id"], older.id.to_hex(), "{filter}");
386+
let (status, body) = f.request_as(&f.peer, "/count", filter.clone()).await;
387+
assert!(status.is_success(), "{status}: {body}");
388+
assert_eq!(body["count"], 1, "{filter}: {body}");
389+
}
390+
391+
// Non-artifact rows still reach the generic `#d` post-filter.
392+
let message = EventBuilder::new(Kind::Custom(9), "d-tagged chat")
393+
.tags([
394+
Tag::parse(["h", &f.home.to_string()]).unwrap(),
395+
Tag::parse(["d", &a.to_string()]).unwrap(),
396+
Tag::parse(["p", &reader]).unwrap(),
397+
])
398+
.sign_with_keys(&f.owner)
399+
.unwrap();
400+
f.publish(&message).await;
401+
let filter = json!([{"#h":[f.home],"#p":[reader],"#d":[a]}]);
402+
let (status, body) = f.request_as(&f.peer, "/query", filter).await;
374403
assert!(status.is_success(), "{status}: {body}");
375-
assert_eq!(body["count"], 1, "{body}");
404+
let mut ids: Vec<_> = body
405+
.as_array()
406+
.unwrap()
407+
.iter()
408+
.map(|e| e["id"].as_str().unwrap().to_owned())
409+
.collect();
410+
ids.sort();
411+
let mut expected = vec![older.id.to_hex(), message.id.to_hex()];
412+
expected.sort();
413+
assert_eq!(ids, expected, "{body}");
376414
}

‎crates/buzz-relay/src/handlers/req.rs‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,15 +1056,14 @@ fn filter_to_query_params(
10561056
} else {
10571057
(None, None)
10581058
};
1059-
// NIP-AR artifacts carry their stable identity in `d` but are not NIP-33,
1060-
// so `d_tag` stays NULL; match the tag itself before `LIMIT`.
1061-
let filter_is_artifact_only = kinds.as_ref().is_some_and(|ks| {
1062-
!ks.is_empty()
1063-
&& ks
1064-
.iter()
1065-
.all(|&k| k as u32 == buzz_core::kind::KIND_ARTIFACT)
1059+
// NIP-AR revisions and removals carry their stable identity in `d` but are
1060+
// not NIP-33, so `d_tag` stays NULL; match the tag on artifact rows before
1061+
// `LIMIT` whenever the filter can select them.
1062+
let filter_can_match_artifacts = kinds.as_ref().is_none_or(|ks| {
1063+
ks.iter()
1064+
.any(|k| buzz_db::event::ARTIFACT_KINDS.contains(k))
10661065
});
1067-
let d_tag_values = filter_is_artifact_only
1066+
let d_tag_values = filter_can_match_artifacts
10681067
.then(|| filter.generic_tags.get(&d_tag_key))
10691068
.flatten()
10701069
.filter(|values| !values.is_empty())
@@ -1768,9 +1767,20 @@ mod tests {
17681767
assert_eq!(q.d_tag_values, Some(vec!["a".to_string()]));
17691768
assert_eq!(q.d_tag, None);
17701769

1771-
// Mixed kinds keep the generic post-filter path.
1772-
let mixed = artifact.clone().kind(nostr::Kind::Custom(9));
1770+
// Mixed and kindless filters that can select artifact rows push it too.
1771+
let mixed = artifact.clone().kind(nostr::Kind::Custom(45011));
17731772
let q = filter_to_query_params(&mixed, None, community);
1773+
assert_eq!(q.d_tag_values, Some(vec!["a".to_string()]));
1774+
let kindless = Filter::new().custom_tag(d, "a").limit(1);
1775+
let q = filter_to_query_params(&kindless, None, community);
1776+
assert_eq!(q.d_tag_values, Some(vec!["a".to_string()]));
1777+
1778+
// Filters that cannot select artifacts keep the generic post-filter path.
1779+
let other = Filter::new()
1780+
.kind(nostr::Kind::Custom(9))
1781+
.custom_tag(d, "a")
1782+
.limit(1);
1783+
let q = filter_to_query_params(&other, None, community);
17741784
assert_eq!(q.d_tag_values, None);
17751785
}
17761786

‎migrations/0051_channel_artifacts.sql‎

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,6 @@ CREATE TABLE artifact_revisions (
2222
SELECT attach_community_write_fence('artifact_heads');
2323
SELECT attach_community_write_fence('artifact_revisions');
2424

25-
-- Row retention may expire earlier payloads, never the live CAS head. Partition
26-
-- retirement must also preserve head payloads; this relay does not drop partitions.
27-
-- A redacted (soft-deleted) head payload may still be purged.
28-
CREATE FUNCTION retain_current_artifact() RETURNS TRIGGER LANGUAGE plpgsql AS $$
29-
BEGIN
30-
IF OLD.kind = 45010 AND OLD.deleted_at IS NULL AND EXISTS (
31-
SELECT 1 FROM artifact_heads h
32-
WHERE h.community_id=OLD.community_id AND h.event_id=OLD.id
33-
) THEN
34-
RETURN NULL;
35-
END IF;
36-
RETURN OLD;
37-
END;
38-
$$;
39-
CREATE TRIGGER retain_current_artifact BEFORE DELETE ON events
40-
FOR EACH ROW EXECUTE FUNCTION retain_current_artifact();
25+
-- The relay does not expire events. Any future row retention or partition
26+
-- retirement must skip payloads referenced by `artifact_heads.event_id`
27+
-- (NIP-AR: expiring earlier revisions MUST NOT remove the current revision).

‎schema/schema.sql‎

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,19 +1991,6 @@ CREATE TABLE artifact_revisions (
19911991
SELECT attach_community_write_fence('artifact_heads');
19921992
SELECT attach_community_write_fence('artifact_revisions');
19931993

1994-
-- Row retention may expire earlier payloads, never the live CAS head. Partition
1995-
-- retirement must also preserve head payloads; this relay does not drop partitions.
1996-
-- A redacted (soft-deleted) head payload may still be purged.
1997-
CREATE FUNCTION retain_current_artifact() RETURNS TRIGGER LANGUAGE plpgsql AS $$
1998-
BEGIN
1999-
IF OLD.kind = 45010 AND OLD.deleted_at IS NULL AND EXISTS (
2000-
SELECT 1 FROM artifact_heads h
2001-
WHERE h.community_id=OLD.community_id AND h.event_id=OLD.id
2002-
) THEN
2003-
RETURN NULL;
2004-
END IF;
2005-
RETURN OLD;
2006-
END;
2007-
$$;
2008-
CREATE TRIGGER retain_current_artifact BEFORE DELETE ON events
2009-
FOR EACH ROW EXECUTE FUNCTION retain_current_artifact();
1994+
-- The relay does not expire events. Any future row retention or partition
1995+
-- retirement must skip payloads referenced by `artifact_heads.event_id`
1996+
-- (NIP-AR: expiring earlier revisions MUST NOT remove the current revision).

‎scripts/reconcile-schema-after-pgschema.sql‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ BEGIN
1919
DROP TRIGGER IF EXISTS events_enqueue_push_match ON events_p_past;
2020
DROP TRIGGER IF EXISTS events_refresh_channel_ttl ON events_p_past;
2121
DROP TRIGGER IF EXISTS events_created_at_floor ON events_p_past;
22-
DROP TRIGGER IF EXISTS retain_current_artifact ON events_p_past;
2322
DROP TRIGGER IF EXISTS community_write_fence_events ON events_p_past;
2423
DROP TRIGGER IF EXISTS trg_events_guard_channel_roster_snapshot ON events_p_past;
2524
ALTER TABLE events ATTACH PARTITION events_p_past
@@ -34,7 +33,6 @@ BEGIN
3433
DROP TRIGGER IF EXISTS events_enqueue_push_match ON events_p2026_01;
3534
DROP TRIGGER IF EXISTS events_refresh_channel_ttl ON events_p2026_01;
3635
DROP TRIGGER IF EXISTS events_created_at_floor ON events_p2026_01;
37-
DROP TRIGGER IF EXISTS retain_current_artifact ON events_p2026_01;
3836
DROP TRIGGER IF EXISTS community_write_fence_events ON events_p2026_01;
3937
DROP TRIGGER IF EXISTS trg_events_guard_channel_roster_snapshot ON events_p2026_01;
4038
ALTER TABLE events ATTACH PARTITION events_p2026_01
@@ -49,7 +47,6 @@ BEGIN
4947
DROP TRIGGER IF EXISTS events_enqueue_push_match ON events_p2026_02;
5048
DROP TRIGGER IF EXISTS events_refresh_channel_ttl ON events_p2026_02;
5149
DROP TRIGGER IF EXISTS events_created_at_floor ON events_p2026_02;
52-
DROP TRIGGER IF EXISTS retain_current_artifact ON events_p2026_02;
5350
DROP TRIGGER IF EXISTS community_write_fence_events ON events_p2026_02;
5451
DROP TRIGGER IF EXISTS trg_events_guard_channel_roster_snapshot ON events_p2026_02;
5552
ALTER TABLE events ATTACH PARTITION events_p2026_02
@@ -64,7 +61,6 @@ BEGIN
6461
DROP TRIGGER IF EXISTS events_enqueue_push_match ON events_p2026_03;
6562
DROP TRIGGER IF EXISTS events_refresh_channel_ttl ON events_p2026_03;
6663
DROP TRIGGER IF EXISTS events_created_at_floor ON events_p2026_03;
67-
DROP TRIGGER IF EXISTS retain_current_artifact ON events_p2026_03;
6864
DROP TRIGGER IF EXISTS community_write_fence_events ON events_p2026_03;
6965
DROP TRIGGER IF EXISTS trg_events_guard_channel_roster_snapshot ON events_p2026_03;
7066
ALTER TABLE events ATTACH PARTITION events_p2026_03
@@ -79,7 +75,6 @@ BEGIN
7975
DROP TRIGGER IF EXISTS events_enqueue_push_match ON events_p2026_04;
8076
DROP TRIGGER IF EXISTS events_refresh_channel_ttl ON events_p2026_04;
8177
DROP TRIGGER IF EXISTS events_created_at_floor ON events_p2026_04;
82-
DROP TRIGGER IF EXISTS retain_current_artifact ON events_p2026_04;
8378
DROP TRIGGER IF EXISTS community_write_fence_events ON events_p2026_04;
8479
DROP TRIGGER IF EXISTS trg_events_guard_channel_roster_snapshot ON events_p2026_04;
8580
ALTER TABLE events ATTACH PARTITION events_p2026_04
@@ -94,7 +89,6 @@ BEGIN
9489
DROP TRIGGER IF EXISTS events_enqueue_push_match ON events_p2026_05;
9590
DROP TRIGGER IF EXISTS events_refresh_channel_ttl ON events_p2026_05;
9691
DROP TRIGGER IF EXISTS events_created_at_floor ON events_p2026_05;
97-
DROP TRIGGER IF EXISTS retain_current_artifact ON events_p2026_05;
9892
DROP TRIGGER IF EXISTS community_write_fence_events ON events_p2026_05;
9993
DROP TRIGGER IF EXISTS trg_events_guard_channel_roster_snapshot ON events_p2026_05;
10094
ALTER TABLE events ATTACH PARTITION events_p2026_05
@@ -109,7 +103,6 @@ BEGIN
109103
DROP TRIGGER IF EXISTS events_enqueue_push_match ON events_p2026_06;
110104
DROP TRIGGER IF EXISTS events_refresh_channel_ttl ON events_p2026_06;
111105
DROP TRIGGER IF EXISTS events_created_at_floor ON events_p2026_06;
112-
DROP TRIGGER IF EXISTS retain_current_artifact ON events_p2026_06;
113106
DROP TRIGGER IF EXISTS community_write_fence_events ON events_p2026_06;
114107
DROP TRIGGER IF EXISTS trg_events_guard_channel_roster_snapshot ON events_p2026_06;
115108
ALTER TABLE events ATTACH PARTITION events_p2026_06
@@ -124,7 +117,6 @@ BEGIN
124117
DROP TRIGGER IF EXISTS events_enqueue_push_match ON events_p_future;
125118
DROP TRIGGER IF EXISTS events_refresh_channel_ttl ON events_p_future;
126119
DROP TRIGGER IF EXISTS events_created_at_floor ON events_p_future;
127-
DROP TRIGGER IF EXISTS retain_current_artifact ON events_p_future;
128120
DROP TRIGGER IF EXISTS community_write_fence_events ON events_p_future;
129121
DROP TRIGGER IF EXISTS trg_events_guard_channel_roster_snapshot ON events_p_future;
130122
ALTER TABLE events ATTACH PARTITION events_p_future

0 commit comments

Comments
 (0)