Skip to content

Commit 7da936f

Browse files
tlongwell-blocknpub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757Tyler Longwellnpub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d
authored
fix(read-path): reach complete threads, dense-second timelines, and all people in the GUI (#1418)
Signed-off-by: Tyler Longwell <[email protected]> Co-authored-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Tyler Longwell <[email protected]> Co-authored-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@sprout-oss.stage.blox.sqprod.co>
1 parent a504ad6 commit 7da936f

27 files changed

Lines changed: 2118 additions & 109 deletions

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

Lines changed: 338 additions & 16 deletions
Large diffs are not rendered by default.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ pub async fn search_users(
231231
let escaped = escape_like(&normalized);
232232
let contains_pattern = format!("%{escaped}%");
233233
let prefix_pattern = format!("{escaped}%");
234-
let limit = limit.clamp(1, 50) as i64;
234+
let limit = limit.clamp(1, 500) as i64;
235235

236236
let rows = sqlx::query_as::<_, (Vec<u8>, Option<String>, Option<String>, Option<String>)>(
237237
r#"

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

Lines changed: 178 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,41 @@ fn extract_depth_limit(raw: &Value) -> Option<u32> {
205205
.and_then(|n| u32::try_from(n).ok())
206206
}
207207

208+
/// Extract a thread pagination cursor from the raw filter JSON.
209+
///
210+
/// The desktop pages `get_thread_replies` forward with a keyset cursor derived
211+
/// transparently from the last reply it has already loaded — no server-issued
212+
/// token. The cursor is a composite of that reply's `created_at` (Unix seconds,
213+
/// field `thread_cursor`/`threadCursor`) and its hex event id (field
214+
/// `thread_cursor_id`/`threadCursorId`). The event id is the tiebreak that lets
215+
/// pagination cross replies sharing the same `created_at` second — without it,
216+
/// a timestamp-only cursor silently drops every tied reply past the page limit
217+
/// (the exact "missed messages" bug this work exists to fix).
218+
///
219+
/// Wire → DB encoding: 8-byte big-endian i64 seconds, followed by the raw
220+
/// event-id bytes when present. `get_thread_replies` decodes this layout back
221+
/// into its `(timestamp, event_id)` keyset. A bare timestamp (no id) is still
222+
/// accepted and paginates on time alone (unsafe across same-second ties).
223+
fn extract_thread_cursor(raw: &Value) -> Option<Vec<u8>> {
224+
let secs = raw
225+
.get("thread_cursor")
226+
.or_else(|| raw.get("threadCursor"))?
227+
.as_i64()?;
228+
let mut bytes = secs.to_be_bytes().to_vec();
229+
230+
if let Some(id_hex) = raw
231+
.get("thread_cursor_id")
232+
.or_else(|| raw.get("threadCursorId"))
233+
.and_then(Value::as_str)
234+
{
235+
if let Ok(id_bytes) = hex::decode(id_hex) {
236+
bytes.extend_from_slice(&id_bytes);
237+
}
238+
}
239+
240+
Some(bytes)
241+
}
242+
208243
fn extract_feed_types(raw: &Value) -> Option<Vec<String>> {
209244
let arr = raw.get("feed_types")?.as_array()?;
210245
let types: Vec<String> = arr
@@ -229,6 +264,35 @@ fn extract_search_mode(raw: &Value) -> buzz_search::SearchMode {
229264
}
230265
}
231266

267+
fn extract_search_page(raw: &Value) -> u32 {
268+
raw.get("page")
269+
.or_else(|| raw.get("search_page"))
270+
.or_else(|| raw.get("searchPage"))
271+
.and_then(Value::as_u64)
272+
.and_then(|value| u32::try_from(value).ok())
273+
.filter(|value| *value > 0)
274+
.unwrap_or(1)
275+
}
276+
277+
/// Compute the SQL `OFFSET` for a raw `page` extension on a non-search general
278+
/// query, or `None` if paging shouldn't apply.
279+
///
280+
/// `page` is 1-based: page 1 → offset 0 (no change), page N → `(N-1) * limit`.
281+
/// Returns `None` when `page` is absent or ≤ 1 (so unrelated general queries
282+
/// keep their default offset) and when `limit` is missing (can't size a page).
283+
/// This mirrors the FTS path's `page`/`per_page` for the non-search directory
284+
/// listing (empty-query kind:0), whose deterministic `created_at DESC, id ASC`
285+
/// ordering in `query_events` makes offset paging stable.
286+
fn extract_page_offset(raw: &Value, limit: Option<i64>) -> Option<i64> {
287+
let page = raw
288+
.get("page")
289+
.and_then(Value::as_u64)
290+
.and_then(|value| i64::try_from(value).ok())
291+
.filter(|value| *value > 1)?;
292+
let per_page = limit.filter(|l| *l > 0)?;
293+
page.checked_sub(1)?.checked_mul(per_page)
294+
}
295+
232296
fn event_in_accessible_channel(se: &buzz_core::StoredEvent, accessible: &[uuid::Uuid]) -> bool {
233297
match se.channel_id {
234298
Some(ch_id) => accessible.contains(&ch_id),
@@ -547,9 +611,16 @@ pub async fn query_events(
547611
.limit
548612
.unwrap_or(100)
549613
.min(BRIDGE_THREAD_MAX_LIMIT as usize) as u32;
614+
let thread_cursor = extract_thread_cursor(raw);
550615
let thread_replies = state
551616
.db
552-
.get_thread_replies(tenant.community(), &root_bytes, Some(depth), limit, None)
617+
.get_thread_replies(
618+
tenant.community(),
619+
&root_bytes,
620+
Some(depth),
621+
limit,
622+
thread_cursor.as_deref(),
623+
)
553624
.await
554625
.map_err(|e| internal_error(&format!("thread query error: {e}")))?;
555626

@@ -594,6 +665,18 @@ pub async fn query_events(
594665
query.before_id = Some(bid);
595666
}
596667

668+
// Honor `page` on non-search general queries so offset paging works for
669+
// the empty-query people directory (kind:0 listing). The FTS path
670+
// (`handle_bridge_search`) has its own `page`/`per_page`; a filter with
671+
// no `search` field lands here instead, where paging would otherwise be
672+
// dropped and the directory would terminate at its first page. Deterministic
673+
// ordering in `query_events` (`created_at DESC, id ASC`) makes offset paging
674+
// stable. `page` defaults to 1 → offset 0, so unrelated general queries are
675+
// unaffected.
676+
if let Some(offset) = extract_page_offset(raw, query.limit) {
677+
query.offset = Some(offset);
678+
}
679+
597680
match state.db.query_events(&query).await {
598681
Ok(stored_events) => {
599682
for se in stored_events {
@@ -867,7 +950,7 @@ fn search_hit_accepted(
867950
}
868951

869952
/// Handle search filters by routing to Postgres FTS, then fetching full events
870-
/// from DB. Returns first page of results (no pagination for bridge MVP).
953+
/// from DB. Supports a bridge-only `page` extension over the FTS result set.
871954
async fn handle_bridge_search(
872955
state: &AppState,
873956
raw_filters: &[Value],
@@ -893,6 +976,7 @@ async fn handle_bridge_search(
893976

894977
for (raw, filter) in raw_filters.iter().zip(filters) {
895978
let search_mode = extract_search_mode(raw);
979+
let search_page = extract_search_page(raw);
896980
let search_text = match &filter.search {
897981
Some(s) if !s.is_empty() => s.clone(),
898982
_ => continue,
@@ -946,7 +1030,7 @@ async fn handle_bridge_search(
9461030
authors,
9471031
since,
9481032
until,
949-
page: 1,
1033+
page: search_page,
9501034
per_page: limit,
9511035
mode: search_mode,
9521036
};
@@ -1832,12 +1916,103 @@ mod tests {
18321916
assert!(extract_before_id(&raw).is_none());
18331917
}
18341918

1919+
#[test]
1920+
fn extract_page_offset_absent_is_none() {
1921+
// No `page` → default offset (unrelated general queries untouched).
1922+
let raw = serde_json::json!({ "kinds": [0], "limit": 50 });
1923+
assert_eq!(extract_page_offset(&raw, Some(50)), None);
1924+
}
1925+
1926+
#[test]
1927+
fn extract_page_offset_page_one_is_none() {
1928+
// Page 1 is the first page → offset 0, expressed as no override.
1929+
let raw = serde_json::json!({ "kinds": [0], "limit": 50, "page": 1 });
1930+
assert_eq!(extract_page_offset(&raw, Some(50)), None);
1931+
}
1932+
1933+
#[test]
1934+
fn extract_page_offset_computes_offset_from_page_and_limit() {
1935+
// Empty people-directory contract: page N → (N-1) * limit.
1936+
let raw = serde_json::json!({ "kinds": [0], "limit": 50, "page": 3 });
1937+
assert_eq!(extract_page_offset(&raw, Some(50)), Some(100));
1938+
}
1939+
1940+
#[test]
1941+
fn extract_page_offset_missing_limit_is_none() {
1942+
// Can't size a page without a limit.
1943+
let raw = serde_json::json!({ "kinds": [0], "page": 2 });
1944+
assert_eq!(extract_page_offset(&raw, None), None);
1945+
}
1946+
18351947
#[test]
18361948
fn extract_depth_limit_valid() {
18371949
let raw = serde_json::json!({ "depth_limit": 3 });
18381950
assert_eq!(extract_depth_limit(&raw), Some(3));
18391951
}
18401952

1953+
#[test]
1954+
fn extract_thread_cursor_valid() {
1955+
// Timestamp-only cursor: 8-byte BE seconds, no tiebreak id.
1956+
let raw = serde_json::json!({ "thread_cursor": 1_782_866_946_i64 });
1957+
assert_eq!(
1958+
extract_thread_cursor(&raw),
1959+
Some(1_782_866_946_i64.to_be_bytes().to_vec())
1960+
);
1961+
}
1962+
1963+
#[test]
1964+
fn extract_thread_cursor_camel_case() {
1965+
let raw = serde_json::json!({ "threadCursor": 42_i64 });
1966+
assert_eq!(
1967+
extract_thread_cursor(&raw),
1968+
Some(42_i64.to_be_bytes().to_vec())
1969+
);
1970+
}
1971+
1972+
#[test]
1973+
fn extract_thread_cursor_composite() {
1974+
// Composite cursor: 8-byte BE seconds followed by the raw event-id bytes.
1975+
let id_hex = "aa".repeat(32);
1976+
let raw = serde_json::json!({
1977+
"thread_cursor": 1_782_866_946_i64,
1978+
"thread_cursor_id": id_hex,
1979+
});
1980+
let mut expected = 1_782_866_946_i64.to_be_bytes().to_vec();
1981+
expected.extend_from_slice(&[0xaa; 32]);
1982+
assert_eq!(extract_thread_cursor(&raw), Some(expected));
1983+
}
1984+
1985+
#[test]
1986+
fn extract_thread_cursor_composite_camel_case() {
1987+
let id_hex = "bb".repeat(32);
1988+
let raw = serde_json::json!({
1989+
"threadCursor": 7_i64,
1990+
"threadCursorId": id_hex,
1991+
});
1992+
let mut expected = 7_i64.to_be_bytes().to_vec();
1993+
expected.extend_from_slice(&[0xbb; 32]);
1994+
assert_eq!(extract_thread_cursor(&raw), Some(expected));
1995+
}
1996+
1997+
#[test]
1998+
fn extract_thread_cursor_ignores_bad_id_hex() {
1999+
// A malformed id falls back to timestamp-only rather than erroring.
2000+
let raw = serde_json::json!({
2001+
"thread_cursor": 5_i64,
2002+
"thread_cursor_id": "not-hex",
2003+
});
2004+
assert_eq!(
2005+
extract_thread_cursor(&raw),
2006+
Some(5_i64.to_be_bytes().to_vec())
2007+
);
2008+
}
2009+
2010+
#[test]
2011+
fn extract_thread_cursor_absent() {
2012+
let raw = serde_json::json!({ "depth_limit": 3 });
2013+
assert!(extract_thread_cursor(&raw).is_none());
2014+
}
2015+
18412016
#[test]
18422017
fn extract_depth_limit_zero() {
18432018
let raw = serde_json::json!({ "depth_limit": 0 });

‎desktop/playwright.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export default defineConfig({
5858
"**/reminders.spec.ts",
5959
"**/virtualization.spec.ts",
6060
"**/scroll-history.spec.ts",
61+
"**/channel-dense-second-reach.spec.ts",
6162
"**/overscroll-boundary.spec.ts",
6263
"**/cold-switch-longtask.perf.ts",
6364
"**/timeline-no-shift.spec.ts",

‎desktop/scripts/check-file-sizes.mjs‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ const overrides = new Map([
6161
// commands add ~40 lines. Queued to split.
6262
// branch cut; override bumped to cover the merged total. Queued to split.
6363
["src-tauri/src/commands/agents.rs", 1437],
64+
// #1418 read-path fix: get_thread_replies' blocker fix (shared TIMELINE_KINDS
65+
// const + build_thread_replies_filter helper, mirroring the channel sibling so
66+
// the two p-gate filters can't drift) plus two guard unit tests. The file was
67+
// already at 995; this load-bearing correctness fix crossed 1000. Not generic
68+
// debt growth. Approved override; queued to split with the rest of this list.
69+
["src-tauri/src/commands/messages.rs", 1082],
6470
// Residual repos_dir integration in ensure_nest_at: REPOS is provisioned
6571
// outside NEST_DIRS (it may be a symlink), so it needs its own create +
6672
// chmod-only-when-real-dir handling plus integration test coverage. The
@@ -82,8 +88,13 @@ const overrides = new Map([
8288
// threaded through Tauri invokes for configurable repos_dir, plus the
8389
// harness-persona-sync `harnessOverride` create-input bit — load-bearing
8490
// parameter plumbing, not generic debt growth. Approved override; still
85-
// queued to split.
86-
["src/shared/api/tauri.ts", 1235],
91+
// queued to split. Read-path lanes 1+2 add server-side fetch bindings
92+
// (getThreadReplies + getChannelMessagesBefore) and paged people-search
93+
// reachability — load-bearing reachability plumbing, not generic debt.
94+
// #1418 read-path fix: +3 doc-only lines correcting the getThreadReplies
95+
// contract (replies-only, root excluded — the query keys on root_event_id,
96+
// which root rows lack). Documentation accuracy, not code growth.
97+
["src/shared/api/tauri.ts", 1340],
8798
// harness-persona-sync feature growth, queued to split in the resolver-unify
8899
// refactor followup. discovery.rs is dominated by the new test module
89100
// (the effective_agent_command / divergent / create-time override matrix);

0 commit comments

Comments
 (0)