Skip to content

Commit 88d2f6f

Browse files
Honeyclaude
andcommitted
fix(read-state): list up to 25 threads per channel row
Five thread rows hid a mention in the sixth unread thread. Raise the cap to 25, the most that keeps a POST's rows for MAX_INTENTS channels within the 1 MiB response limit (50 would be ~1.4 MB, a 503 that hides committed outcomes); a unit test pins that bound. The cap tests now follow the constant. The doc now says the v1 shape is stable while what the counts include may change pre-release, so clients replace rows with each response. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Honey <8e307ae0076a4dab6b94b036ea3edc7e08f823a625269c1e6919e881a048b4d2@buzz.block.builderlab.xyz>
1 parent 509bf6c commit 88d2f6f

6 files changed

Lines changed: 76 additions & 38 deletions

File tree

‎crates/buzz-db/src/store/personal_read/model.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ pub(super) struct ReadAccount {
5454

5555
/// Maximum channel summaries in one sidebar page.
5656
pub const MAX_CHANNELS: usize = 20;
57-
/// Maximum thread rows per channel row.
58-
pub const MAX_THREAD_SUMMARIES: usize = 5;
57+
/// Maximum thread rows per channel row. A POST can return rows for
58+
/// `MAX_INTENTS` channels, and they must fit the 1 MiB response limit.
59+
pub const MAX_THREAD_SUMMARIES: usize = 25;
5960
/// Latest-probe event budget per channel, before eligibility filtering.
6061
pub const MAX_CHANNEL_SCAN: usize = 256;
6162
/// Unread-window work budget per channel, before eligibility/ancestry joins.

‎crates/buzz-db/src/store/personal_read/participation_postgres_tests.rs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,11 +433,12 @@ async fn threads_are_capped_after_replies_that_do_not_count_are_removed() {
433433
let w = World::new().await;
434434
let c = w.channel().await;
435435
let old = w.now - 40 * DAY;
436-
// The actor's thread has the oldest unread reply. Five unjoined threads
437-
// are newer and would fill the list if the cap came first.
436+
// The actor's thread has the oldest unread reply. A cap's worth of
437+
// unjoined threads are newer and would fill the list if the cap came
438+
// first.
438439
let mine = w.post(c, &w.actor, old, vec![]).await;
439440
let answer = w.reply(c, &w.peer, &mine, Some(&mine), w.now, vec![]).await;
440-
for i in 1..=5 {
441+
for i in 1..=MAX_THREAD_SUMMARIES as u64 {
441442
let root = w.post(c, &w.peer, old, vec![]).await;
442443
w.reply(c, &w.peer, &root, Some(&root), w.now + i, vec![])
443444
.await;

‎crates/buzz-db/src/store/personal_read/threads_postgres_tests.rs‎

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,10 @@ async fn thread_summaries_order_cap_and_anchor() {
197197
let (db, pool, community, channel, actor, fixture_root) = fixture().await;
198198
// After the fixture root, so marking the newest root covers every root.
199199
let base = fixture_root.created_at.as_secs() + 1;
200+
// One more thread than the cap.
201+
let n = MAX_THREAD_SUMMARIES as u64 + 1;
200202
let mut roots = Vec::new();
201-
for i in 0..6 {
203+
for i in 0..n {
202204
let root = post(&db, community, channel, base + i, vec![]).await;
203205
join(&db, &pool, community, channel, &actor, &root).await;
204206
roots.push(root);
@@ -211,11 +213,13 @@ async fn thread_summaries_order_cap_and_anchor() {
211213
channel_mark(channel, newest_root.id.to_hex()),
212214
)
213215
.await;
214-
// Threads 0 and 1 tie on newest reply time; thread 2 has one directed
215-
// reply and two that arrive last, together.
216+
// Threads 0 and 1 tie on newest reply time and the rest follow, newest
217+
// first. Thread 2 has one directed reply and two that arrive last,
218+
// together.
219+
let offset = |i: u64| if i < 2 { n * 10 } else { (n - i) * 10 };
216220
let mut anchors = Vec::new();
217-
for (i, root) in roots.iter().enumerate() {
218-
let at = base + 100 + [50, 50, 40, 30, 20, 10][i];
221+
for (i, root) in (0..).zip(&roots) {
222+
let at = base + 100 + offset(i);
219223
anchors.push(reply(&db, &pool, community, channel, root, at, vec![]).await);
220224
}
221225
reply(
@@ -234,7 +238,7 @@ async fn thread_summaries_order_cap_and_anchor() {
234238
community,
235239
channel,
236240
&roots[2],
237-
base + 140,
241+
base + 100 + offset(2),
238242
vec![],
239243
)
240244
.await;
@@ -257,17 +261,10 @@ async fn thread_summaries_order_cap_and_anchor() {
257261
let order = |row: &ChannelReadSummary| -> Vec<_> {
258262
row.threads.iter().map(|t| t.root_id.clone()).collect()
259263
};
260-
// Six unread threads exceed the cap of five.
261-
assert_eq!(
262-
order(&row),
263-
vec![
264-
tied[0].clone(),
265-
tied[1].clone(),
266-
roots[2].id.to_hex(),
267-
roots[3].id.to_hex(),
268-
roots[4].id.to_hex(),
269-
]
270-
);
264+
let ids = |roots: &[nostr::Event]| roots.iter().map(|r| r.id.to_hex()).collect::<Vec<_>>();
265+
let last = roots.len() - 1;
266+
// The oldest unread thread is past the cap.
267+
assert_eq!(order(&row), [tied.to_vec(), ids(&roots[2..last])].concat());
271268
let third = &row.threads[2];
272269
assert_eq!(third.mentions, 3);
273270
assert_eq!(
@@ -276,7 +273,7 @@ async fn thread_summaries_order_cap_and_anchor() {
276273
"equal arrivals break toward the smaller ID"
277274
);
278275

279-
// Reading one listed thread through its anchor lists the sixth.
276+
// Reading one listed thread through its anchor lists the oldest.
280277
let first = row.threads[0].clone_target(channel);
281278
assert_eq!(
282279
apply(&db, community, &actor, first).await,
@@ -285,16 +282,12 @@ async fn thread_summaries_order_cap_and_anchor() {
285282
let row = sidebar(&db, community, &actor).await;
286283
assert_eq!(
287284
order(&row),
288-
vec![
289-
tied[1].clone(),
290-
roots[2].id.to_hex(),
291-
roots[3].id.to_hex(),
292-
roots[4].id.to_hex(),
293-
roots[5].id.to_hex(),
294-
]
285+
[vec![tied[1].clone()], ids(&roots[2..])].concat()
295286
);
296287
let mentions: Vec<_> = row.threads.iter().map(|t| t.mentions).collect();
297-
assert_eq!(mentions, [1, 3, 1, 1, 1]);
288+
let mut expected = vec![1; MAX_THREAD_SUMMARIES];
289+
expected[1] = 3;
290+
assert_eq!(mentions, expected);
298291
}
299292

300293
impl ThreadReadSummary {

‎crates/buzz-relay/src/api/buzz_v1/auth.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,12 @@ pub(super) async fn recheck(
161161
Ok(())
162162
}
163163

164+
/// Largest serialized API response; a larger one is a 503.
165+
pub(super) const MAX_RESPONSE_BYTES: usize = 1024 * 1024;
166+
164167
pub(super) fn response(value: impl serde::Serialize) -> Result<Response, Error> {
165168
let bytes = serde_json::to_vec(&value).map_err(|_| Error::unavailable())?;
166-
if bytes.len() > 1024 * 1024 {
169+
if bytes.len() > MAX_RESPONSE_BYTES {
167170
return Err(Error::unavailable());
168171
}
169172
Ok((

‎crates/buzz-relay/src/api/buzz_v1/handlers.rs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,39 @@ pub(super) async fn write_intent(
208208
Err(_) => json!({"status":"unknown","retryable":true}),
209209
}
210210
}
211+
212+
#[cfg(test)]
213+
mod tests {
214+
use super::*;
215+
use buzz_db::personal_read::{ThreadReadSummary, MAX_THREAD_SUMMARIES};
216+
217+
/// A POST that applied to `MAX_INTENTS` channels, each with a full thread
218+
/// list, must still fit: an oversized body is a 503 that hides committed
219+
/// outcomes.
220+
#[test]
221+
fn largest_write_response_fits_the_response_limit() {
222+
let id = || Some("f".repeat(64));
223+
let channels: Vec<_> = (0..MAX_INTENTS)
224+
.map(|_| ChannelReadSummary {
225+
channel_id: Uuid::max(),
226+
unread: true,
227+
mentions: u32::MAX,
228+
read_through_id: id(),
229+
latest_id: id(),
230+
threads: (0..MAX_THREAD_SUMMARIES)
231+
.map(|_| ThreadReadSummary {
232+
root_id: "f".repeat(64),
233+
unread: true,
234+
mentions: u32::MAX,
235+
read_through_id: id(),
236+
latest_id: "f".repeat(64),
237+
})
238+
.collect(),
239+
})
240+
.collect();
241+
let outcomes = vec![json!({"status":"unknown","retryable":true}); MAX_INTENTS];
242+
let body = json!({ "outcomes": outcomes, "channels": channels });
243+
let bytes = serde_json::to_vec(&body).unwrap().len();
244+
assert!(bytes <= auth::MAX_RESPONSE_BYTES, "{bytes} bytes");
245+
}
246+
}

‎docs/buzz-v1-read-state.md‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ bounded scans found no top-level message, not that the channel is empty.
8989
`read_through_id` may name a message that has since been deleted.
9090

9191
`threads` lists threads with unread replies that count, newest unread reply
92-
first by author time (then `root_id`), at most 5. Threads past the fifth are
92+
first by author time (then `root_id`), at most 25. Threads past the 25th are
9393
omitted with their mentions, and nothing signals the omission: after marking
9494
the listed threads read, a refresh can list the next ones. A thread row's
9595
`unread` is always true in this version. No message bytes are included.
@@ -201,7 +201,8 @@ device-local.
201201

202202
## Bounds and deployment
203203

204-
- 20 sidebar rows and 100 intents per request; 5 thread rows per channel.
204+
- 20 sidebar rows and 100 intents per request; 25 thread rows per channel, so
205+
a POST's rows for 100 channels fit the response limit.
205206
- 64 KiB write body; 1 MiB serialized API response.
206207
- 4096 raw events per channel inside the horizon, before eligibility.
207208
- The latest fallback probes 256 events; a long ineligible or reply-only tail
@@ -232,10 +233,13 @@ local seed is not a DAU/concurrency or p95/p99 production acceptance result.
232233

233234
## Compatibility and extension rules
234235

235-
Within `/buzz/v1`, clients must ignore unknown response object fields. Existing
236-
required fields, status variants and their meanings remain stable; additive
237-
fields do not authorize silently changing `mentions` or frontier semantics.
238-
Breaking changes require an explicitly negotiated contract or a new API version.
236+
Within `/buzz/v1`, clients must ignore unknown response object fields. The
237+
shape is stable: existing fields, their types, status variants and what a
238+
`mark_through` does remain. What the counts include is not yet: which messages
239+
are unread or directed, which threads are listed, and the bounds may change
240+
while v1 is pre-release. A client therefore replaces its rows with each
241+
response rather than reconciling them with its own counts. Breaking shape
242+
changes require an explicitly negotiated contract or a new API version.
239243
Requests remain strict: send new parameters or intent types only after the relay
240244
advertises the corresponding capability. Missing optional data means unsupported
241245
or not requested, never an empty list, zero count or unchanged revision.

0 commit comments

Comments
 (0)