buzz icon indicating copy to clipboard operation
buzz copied to clipboard

fix(buzz-db): return an empty page from get_channel_window when limit is 0

Open hasky00 opened this issue 4 months ago • 1 comments

Summary

get_channel_window in buzz-db can violate its own documented pagination contract when called with limit == 0.

ChannelWindow::next_cursor is documented as Some iff has_more (crates/buzz-db/src/thread.rs). The function derives has_more from a limit + 1 probe row before truncation, but derives next_cursor from the truncated row set:

q = q.bind(limit as i64 + 1); // limit == 0 → binds 1, fetches the probe row
let mut db_rows = q.fetch_all(pool).await?;
let has_more = db_rows.len() > limit as usize; // 1 > 0 → true
db_rows.truncate(limit as usize);              // → 0 rows
let next_cursor = if has_more {
    match db_rows.last() { Some(row) => …, None => None } // empty → None
} else { None };

With limit == 0 and at least one matching row, this returns has_more = true and next_cursor = None, breaking the invariant. A caller that trusts the contract (window.next_cursor.expect("has_more implies next_cursor") — exactly what the module's own pagination test does) panics, and a keyset-pagination loop would spin forever returning empty pages.

The sole current production caller clamps the limit with .max(1), so this isn't reachable from the HTTP surface today — but it's a latent contract violation in a public Database API that any future caller can trip.

Fix

Guard limit == 0 at the top of get_channel_window and return an empty, exhausted page (rows: [], has_more: false, next_cursor: None). This honors the invariant and also skips a pointless database round-trip for a zero-row request.

Test

Adds channel_window_zero_limit_reports_empty_exhausted_page, mirroring the existing channel_window_* Postgres tests: it inserts rows, requests a zero-limit window, and asserts the page is empty, has_more is false, and next_cursor is None. (Marked #[ignore = "requires Postgres"] like its siblings.)

Verification

  • cargo fmt -p buzz-db -- --check — clean
  • cargo check -p buzz-db --tests — clean
  • cargo clippy -p buzz-db --tests — clean

The new test requires Postgres and follows the repo's existing #[ignore = "requires Postgres"] convention.


🤖 Generated with Claude Code

hasky00 avatar Jul 29 '26 13:07 hasky00

i checked and no caller can currently depend on the limit 'limit ==0' behaviour as sentinel : the only production path with external limit is the relay bridge ('crates/buzz-realy/src/api/bridge.rs') which clamps before calling into 'buzz-db' :

  let limit = filter
      .limit
      .map(|l| (l as u32).min(BRIDGE_WINDOW_MAX_LIMIT))
      .unwrap_or(BRIDGE_WINDOW_DEFAULT_LIMIT)
      .max(1);

So limit == 0 is floored to 1 and never reaches get_channel_window_with_session.

  • The desktop command (desktop/src-tauri/src/commands/channel_window.rs) builds its filter with limit_rows.unwrap_or(50).min(200) and goes through the same bridge, so it hits that same .max(1) floor.

  • All remaining call sites are tests in crates/buzz-db and buzz-test-client, all with limits ≥ 1.

So the fix only changes behavior for direct library consumers calling with limit == 0, where the previous result (has_more == true with next_cursor == None) violated the documented invariant rather than serving as any sanctioned sentinel. Should be safe to merge.

hasky00 avatar Aug 06 '26 06:08 hasky00

it should me merged by now

hasky00 avatar Aug 19 '26 05:08 hasky00

🔐 Codex Security Review

Status: review required for the current range.

The current range is 571c1902d0ca55cfd4ccf6b91eeb731909cc10be...4459acfbbf2a9ad43fc298f6a83ebcaaf135363c. A new review must complete for this exact range. When manual authorization is required, a Block organization member must comment exactly @buzz-security-review 4459acfbbf2a9ad43fc298f6a83ebcaaf135363c to authorize a new review. Any previous review applies only to its recorded range.

github-actions[bot] avatar Aug 28 '26 15:08 github-actions[bot]

have a good day everyone

hasky00 avatar Sep 16 '26 17:09 hasky00

Superseded by #8002.

This PR was closed while it still included an unrelated workflow change. #8002 contains only the buzz-db zero-limit fix, as a single signed-off commit.

hasky00 avatar Sep 30 '26 21:09 hasky00