Skip to content

Commit f3d6ac8

Browse files
committed
fix(relay): bound incremental read scope
Signed-off-by: Daniel Rouleau <[email protected]>
1 parent 24166e7 commit f3d6ac8

6 files changed

Lines changed: 193 additions & 31 deletions

File tree

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@ pub enum DbError {
8181
#[error("artifact sync limit exceeded")]
8282
ArtifactSyncLimit,
8383

84-
/// Sync scope contains a channel with expiring content.
85-
#[error("artifact sync scope requires permanent channels")]
86-
ArtifactSyncUnsupportedScope,
87-
8884
/// A complete read-state snapshot exceeds its bounded resource budget.
8985
#[error("read-state snapshot exceeds event or byte limit")]
9086
ReadStateSnapshotTooLarge,

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ impl Db {
8585
.bind(named)
8686
.bind((PAGE_ROWS + 1) as i64)
8787
.bind(buzz_core::channel::LEGACY_CHANNEL_TYPES.to_vec())
88+
.bind(query.tags.iter().any(|(name, _)| name == "h"))
8889
.fetch_all(&mut *tx)
8990
.await?;
9091
let candidates = rows
@@ -135,6 +136,7 @@ impl Db {
135136
let mut qb = QueryBuilder::new("SELECT e.id,e.pubkey,e.created_at,e.kind,e.tags,e.content,e.sig,e.received_at,e.channel_id FROM events e ");
136137
super::artifact_query::predicates(&mut qb, community, reader, query);
137138
qb.push(" AND a.artifact_id=ANY(").push_bind(ids).push(")");
139+
qb.push(" AND EXISTS (SELECT 1 FROM channels c WHERE c.community_id=e.community_id AND c.id=e.channel_id AND c.ttl_seconds IS NULL)");
138140
qb.push(" LIMIT ").push_bind(PAGE_ROWS as i64);
139141
let mut events = Vec::new();
140142
let mut bytes = 0;
@@ -153,15 +155,6 @@ impl Db {
153155
}
154156
}
155157
}
156-
let homes = events
157-
.iter()
158-
.filter_map(|e| e.channel_id)
159-
.collect::<Vec<_>>();
160-
let ttl:bool=sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM channels WHERE community_id=$1 AND id=ANY($2) AND ttl_seconds IS NOT NULL)")
161-
.bind(community.as_uuid()).bind(homes).fetch_one(&mut *tx).await?;
162-
if ttl {
163-
return Err(DbError::ArtifactSyncUnsupportedScope);
164-
}
165158
tx.commit().await?;
166159
Ok(events)
167160
}
@@ -172,7 +165,8 @@ impl Db {
172165
const CANDIDATES: &str = r#"
173166
WITH readable AS NOT MATERIALIZED (
174167
SELECT c.id FROM channels c
175-
WHERE c.community_id=$1 AND c.deleted_at IS NULL
168+
WHERE c.community_id=$1 AND c.deleted_at IS NULL AND c.ttl_seconds IS NULL
169+
AND (NOT $12::boolean OR c.id=ANY($9))
176170
AND (c.channel_type::text=ANY($11) OR c.id=ANY($9))
177171
AND ((c.visibility='open' AND (NOT c.unlisted OR c.id=ANY($9))) OR EXISTS (
178172
SELECT 1 FROM channel_members m WHERE m.community_id=c.community_id
@@ -183,12 +177,14 @@ WITH readable AS NOT MATERIALIZED (
183177
FROM artifact_heads a
184178
WHERE a.community_id=$1 AND a.modified_at >= COALESCE($3::timestamptz,'epoch'::timestamptz)
185179
AND a.modified_at <= $4 AND a.channel_id IN (SELECT id FROM readable)
180+
AND ($3::timestamptz IS NOT NULL OR NOT a.deleted)
186181
AND (cardinality($8::text[])=0 OR a.artifact_type=ANY($8))
187182
UNION ALL
188183
SELECT m.sync_modified_at,0,m.channel_id::text,NULL::uuid,m.channel_id
189184
FROM channel_members m WHERE m.community_id=$1 AND m.pubkey=$2
190185
AND $3::timestamptz IS NOT NULL AND m.sync_modified_at >= $3
191186
AND m.sync_modified_at <= $4 AND m.removed_at IS NOT NULL
187+
AND (NOT $12::boolean OR m.channel_id=ANY($9))
192188
AND NOT EXISTS (SELECT 1 FROM readable r WHERE r.id=m.channel_id)
193189
UNION ALL
194190
SELECT m.sync_modified_at,2,a.artifact_id::text,a.artifact_id,NULL::uuid

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

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ async fn sync_rejoin_replays_old_heads_without_resetting_on_task_edits() {
129129
async fn sync_ignores_unchanged_visibility_and_resets_on_ttl_changes() {
130130
let _guard = SERIAL.lock().await;
131131
let f = fixture().await;
132-
accept(&f, &f.revision(Uuid::new_v4(), "create", None)).await;
132+
let permanent_id = Uuid::new_v4();
133+
let permanent = f.revision(permanent_id, "create", None);
134+
accept(&f, &permanent).await;
133135
let first = sync(&f, None).await;
134136
buzz_db::channel::update_channel(
135137
&f.pool,
@@ -166,19 +168,58 @@ async fn sync_ignores_unchanged_visibility_and_resets_on_ttl_changes() {
166168
assert_eq!(reset["reason"], "policy_changed");
167169
let next = sync(&f, None).await;
168170
assert!(next["checkpoint"].is_string());
169-
accept(
170-
&f,
171-
&f.revision_as(&f.owner, f.private, Uuid::new_v4(), "create", None),
172-
)
173-
.await;
174-
let (status, body) = f
171+
let expiring_id = Uuid::new_v4();
172+
let expiring = f.revision_as(&f.owner, f.private, expiring_id, "create", None);
173+
accept(&f, &expiring).await;
174+
for checkpoint in [None, Some(&next["checkpoint"])] {
175+
let page = sync(&f, checkpoint).await;
176+
assert_eq!(page["changes"].as_array().unwrap().len(), 1);
177+
assert_eq!(page["changes"][0]["artifactId"], permanent_id.to_string());
178+
assert_eq!(page["changes"][0]["type"], "upsert");
179+
assert!(page["checkpoint"].is_string());
180+
}
181+
let (status, current) = f
175182
.request(
176183
"/query",
177-
json!([{"artifact":"incremental","query":query()}]),
184+
json!([{
185+
"artifact":"incremental","action":"current","query":query(),
186+
"artifactIds":[permanent_id,expiring_id]
187+
}]),
188+
)
189+
.await;
190+
assert!(status.is_success(), "{status}: {current}");
191+
assert_eq!(current["changes"][0]["type"], "upsert");
192+
assert_eq!(
193+
current["changes"][1],
194+
json!({"type":"remove","artifactId":expiring_id})
195+
);
196+
let (status, named) = f
197+
.request(
198+
"/query",
199+
json!([{
200+
"artifact":"incremental","query":{"artifact":"current","#h":[f.private]}
201+
}]),
202+
)
203+
.await;
204+
assert!(status.is_success(), "{status}: {named}");
205+
assert_eq!(named["changes"], json!([]));
206+
let moved = f.revision_as(&f.owner, f.private, permanent_id, "move", Some(&permanent));
207+
accept(&f, &moved).await;
208+
let delta = sync(&f, Some(&next["checkpoint"])).await;
209+
assert_eq!(
210+
delta["changes"],
211+
json!([{"type":"remove","artifactId":permanent_id}])
212+
);
213+
let (status, scoped) = f
214+
.request(
215+
"/query",
216+
json!([{
217+
"artifact":"incremental","query":{"artifact":"current","#h":[f.home]},
218+
}]),
178219
)
179220
.await;
180-
assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY);
181-
assert_eq!(body["code"], "incremental_unsupported_scope");
221+
assert!(status.is_success(), "{status}: {scoped}");
222+
assert_eq!(scoped["changes"], json!([]));
182223
}
183224

184225
#[tokio::test]

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

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,132 @@ async fn sync_move_cursor_hides_private_and_redacted_revision_ids() {
100100
}
101101
}
102102
}
103+
104+
#[tokio::test]
105+
#[ignore = "requires Postgres"]
106+
async fn sync_channel_scope_limits_candidates_and_keeps_source_removals() {
107+
let _guard = SERIAL.lock().await;
108+
let f = fixture().await;
109+
buzz_db::channel::update_channel(
110+
&f.pool,
111+
f.community,
112+
f.home,
113+
buzz_db::channel::ChannelUpdate {
114+
visibility: Some("private".into()),
115+
..Default::default()
116+
},
117+
)
118+
.await
119+
.unwrap();
120+
for _ in 0..101 {
121+
accept(
122+
&f,
123+
&f.revision_as(&f.owner, f.private, Uuid::new_v4(), "create", None),
124+
)
125+
.await;
126+
}
127+
let deleted_id = Uuid::new_v4();
128+
let deleted = f.revision(deleted_id, "create", None);
129+
accept(&f, &deleted).await;
130+
accept(&f, &f.revision(deleted_id, "delete", Some(&deleted))).await;
131+
let id = Uuid::new_v4();
132+
let created = f.revision(id, "create", None);
133+
accept(&f, &created).await;
134+
let scoped_query = json!({"artifact":"current","#type":["buzz.task"],"#h":[f.home]});
135+
let (status, initial) = f
136+
.request(
137+
"/query",
138+
json!([{
139+
"artifact":"incremental","query":scoped_query
140+
}]),
141+
)
142+
.await;
143+
assert!(status.is_success(), "{status}: {initial}");
144+
assert_eq!(initial["changes"].as_array().unwrap().len(), 1);
145+
assert_eq!(initial["changes"][0]["event"]["id"], created.id.to_hex());
146+
assert!(initial["nextCursor"].is_null());
147+
assert!(initial["checkpoint"].is_string());
148+
let moved = f.revision_as(&f.owner, f.private, id, "move", Some(&created));
149+
accept(&f, &moved).await;
150+
let (status, delta) = f
151+
.request(
152+
"/query",
153+
json!([{
154+
"artifact":"incremental","query":scoped_query,"checkpoint":initial["checkpoint"]
155+
}]),
156+
)
157+
.await;
158+
assert!(status.is_success(), "{status}: {delta}");
159+
assert!(delta["changes"]
160+
.as_array()
161+
.unwrap()
162+
.contains(&json!({"type":"remove","artifactId":id})));
163+
assert!(!delta.to_string().contains(&f.private.to_string()));
164+
let restored = f.revision(id, "move", Some(&moved));
165+
accept(&f, &restored).await;
166+
for channel in [f.home, f.private] {
167+
sqlx::query("INSERT INTO channel_members(community_id,channel_id,pubkey,role) VALUES($1,$2,$3,'owner')")
168+
.bind(f.community.as_uuid()).bind(channel).bind(f.peer.public_key().as_bytes().as_slice())
169+
.execute(&f.pool).await.unwrap();
170+
}
171+
buzz_db::channel_members::remove_member(
172+
&f.pool,
173+
f.community,
174+
f.private,
175+
f.owner.public_key().as_bytes(),
176+
f.owner.public_key().as_bytes(),
177+
)
178+
.await
179+
.unwrap();
180+
let (status, returned) = f
181+
.request(
182+
"/query",
183+
json!([{
184+
"artifact":"incremental","query":scoped_query,"checkpoint":delta["checkpoint"]
185+
}]),
186+
)
187+
.await;
188+
assert!(status.is_success(), "{status}: {returned}");
189+
assert!(returned["changes"]
190+
.as_array()
191+
.unwrap()
192+
.iter()
193+
.any(|c| c["event"]["id"] == restored.id.to_hex()));
194+
assert!(returned["changes"]
195+
.as_array()
196+
.unwrap()
197+
.iter()
198+
.all(|c| c["type"] != "removeChannel"));
199+
buzz_db::channel_members::remove_member(
200+
&f.pool,
201+
f.community,
202+
f.home,
203+
f.owner.public_key().as_bytes(),
204+
f.owner.public_key().as_bytes(),
205+
)
206+
.await
207+
.unwrap();
208+
let (status, removed) = f
209+
.request(
210+
"/query",
211+
json!([{
212+
"artifact":"incremental","query":scoped_query,"checkpoint":returned["checkpoint"]
213+
}]),
214+
)
215+
.await;
216+
assert!(status.is_success(), "{status}: {removed}");
217+
assert_eq!(
218+
removed["changes"],
219+
json!([{"type":"removeChannel","channelId":f.home}])
220+
);
221+
let (status, invalid_scope) = f
222+
.request(
223+
"/query",
224+
json!([{
225+
"artifact":"incremental","query":{"artifact":"current","#h":["not-a-uuid"]}
226+
}]),
227+
)
228+
.await;
229+
assert!(status.is_success(), "{status}: {invalid_scope}");
230+
assert_eq!(invalid_scope["changes"], json!([]));
231+
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,6 @@ fn reset(reason: &str) -> (StatusCode, Json<Value>) {
7070
fn db_error(e: buzz_db::DbError) -> (StatusCode, Json<Value>) {
7171
match e {
7272
buzz_db::DbError::ArtifactSyncLimit => limit(),
73-
buzz_db::DbError::ArtifactSyncUnsupportedScope => error(
74-
StatusCode::UNPROCESSABLE_ENTITY,
75-
"incremental_unsupported_scope",
76-
),
7773
error => {
7874
tracing::warn!(%error,"incremental artifact read failed");
7975
unavailable()

‎docs/nips/NIP-AR.md‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,13 @@ server permits four concurrent operations per process, a two-second SQL
231231
statement budget, and an eight-second operation budget. There is no
232232
identity-wide membership count limit on this path.
233233

234-
Only matching readable artifacts in permanent channels are supported.
235-
A matching TTL artifact returns HTTP 422 `incremental_unsupported_scope`. An
236-
unrelated TTL channel does not block incremental task reads. HTTP 400 `incremental_invalid`
234+
Incremental reads include only matching readable artifacts in permanent channels.
235+
TTL channels do not contribute discovery candidates. A known-ID read returns
236+
`remove` for an artifact in a TTL channel. Moves into a TTL channel return
237+
removals for the permanent source. An explicit `#h` scope limits candidates
238+
to those channels, including retained move sources and membership removals.
239+
Initial reads omit deleted heads. Later reads retain deletion removals.
240+
HTTP 400 `incremental_invalid`
237241
rejects invalid requests or tokens. HTTP 503 `incremental_unavailable` reports a
238242
service or work-budget failure. These failures do not advance progress.
239243

0 commit comments

Comments
 (0)