Skip to content

feat(relay): read NIP-CW windows oldest first - #9632

Closed
LordMelkor wants to merge 1 commit into
block:mainfrom
LordMelkor:clod/thread-window-newer
Closed

LordMelkor wants to merge 1 commit into
block:mainfrom
LordMelkor:clod/thread-window-newer

Conversation

@LordMelkor

@LordMelkor LordMelkor commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

NIP-CW windows can now be read oldest first. Both window modes accept direction: "newer". A newer page starts at the oldest row and is continued by after/after_id from the previous page's cursor. Thread windows also accept root_aux: false, which leaves the root's reactions and edits out of the closure for a reader that already shows the root.

The relay advertises this as buzz-window-newer in NIP-11. Clients that don't ask for it see no change: default requests keep their exact binding, and older 39006 bounds are byte-identical.

This is the relay half of block/buzz-app#3660. That PR lets a plugin sort, filter and page a thread's or channel's rows. Reading oldest first lets an "Oldest" view paint its first window after one page. Without it, it has to read the whole conversation newest first. The app works without this relay change, but its scans are slower.

What changed

  • Thread mode scans idx_thread_metadata_window backward, so it needs no migration. It binds the direction and cursor into the tw:1 hash and rejects the other direction's cursor fields.
  • Channel mode walks the monthly partitions forward two months at a time, mirroring the newest-first split, and stops at the first window that fills the page.
    • The head page starts at the month the channel was created, less the write path's created_at tolerance (channel_floor), so it never plans older partitions.
    • Newer 39006 bounds carry a newer: d-tag prefix and a direction field.
  • Routing: every newer page routes like an older read's head page (RoutePredicate::Bounded): to a replica only within replica_read_max_age, otherwise to the writer. An older continuation page can use a replica because its cursor puts an upper bound on the rows it reads, and the replica fence can cover that bound. A newer page reads up to the present, so no fence covers it.
  • Docs: NIP-11 advertises buzz-window-newer, and NIP-CW and the bridge channel window doc describe both directions.

Known limits

  • Rows older than the channel floor are missed silently. A row stored with a created_at before the channel's creation month (less the write tolerance) is not found by a newer channel read. Examples are rows from restores or raw SQL. The write path rejects such rows, so this needs data that bypassed it.
  • Short channels. A channel with fewer rows than the page limit plus one reads forward to the time ceiling.
  • Sparse long channels. These cost one statement per two months walked.
  • No channel row. If the channel's row is missing, there is no floor, and the read is unbounded as before.
  • Oldest-first scans load the writer when no replica staleness budget is set. Without replica_read_max_age, every newer page goes to the writer, not only the first page. The app's reference plugin keeps this bounded at the client: it reads at background priority, and after a reconnect it reads again only while its window is visible, after a random wait of up to 10 s.

Related issue

None found.

Testing

All runs were on a separate Linux machine at 605ba9884 (tree identical to this PR's head 73d76b3; only the commit message trailers changed), on main c01d329:

  • cargo fmt --check, and cargo clippy -p buzz-core -p buzz-db -p buzz-relay --all-targets -D warnings: clean.
  • cargo nextest (buzz-core, buzz-db, buzz-relay): 2,379 passed.
  • Postgres tests (scripts/postgres-test-run.sh, same crates): 1,559 passed.
  • Pre-push lanes at 73d76b3: branch skew and file-size check pass; just test-unit passes for the changed crates, but three unrelated crates (buzz-voice, buzz-acp, buzz-agent) could not build there because the sherpa-onnx-sys build script failed to download its library; desktop Tauri clippy and tests could not build without glib/GTK on that machine. No desktop, agent or voice code uses the changed buzz-core thread-window API (git grep).
  • End-to-end, by the maintainer's agent: the relay was built from this branch (605ba9884, tree identical to the head) and run locally, and the real Buzz browser app from feat(messages): let a plugin sort, filter and page a reader's rows buzz-app#3660 (at its head, 2966a32a9) was used against it. A 2,500-reply thread and a 3,000-message channel were read oldest first (direction: "newer") and newest first. Oldest paged forward to the last reply and to the last message, past the app's 2,400-row channel window, and every window matched a database oracle. All 33 checks passed.

Made with human guidance from the maintainer driving this work.

🤖 Generated with Claude Code

Add `direction: "newer"` to both NIP-CW window modes. A newer window pages
from the oldest row in `created_at ASC, id DESC` order, continued by
`after`/`after_id`; the other direction's cursor fields are rejected.

- Thread mode scans idx_thread_metadata_window backward (no migration),
  binds the direction and cursor into the tw:1 hash, and accepts
  `root_aux: false` to leave the root's aux out of the closure. Default
  requests keep their exact binding.
- Channel mode walks the monthly partitions forward two months at a time
  (`oldest_for`, `Forward::advance`), mirroring the recent-first split, and
  stops at the first window that fills the page. The head page starts at the
  month the channel was created, less the write path's created_at tolerance
  (`channel_floor`), so it never plans the older partitions. Newer 39006
  bounds carry a `newer:` d-tag prefix and a `direction` field; older bounds
  are byte-identical.
- Newer pages route to the writer: no cursor bounds the rows they read.
- NIP-11 advertises `buzz-window-newer`; NIP-CW and the bridge channel
  window doc describe both directions.

Made with human guidance from the maintainer driving this work.

Co-authored-by: Clod <c7b21872e9ddb79ca62967d0e21e6a4518a669eb04648e963cdfb2426e62fafb@buzz.block.builderlab.xyz>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Krish Ray <kray@block.xyz>
@LordMelkor

Copy link
Copy Markdown
Contributor Author

🤖 Replaced by #9641: the same commit, on a branch in this repo instead of a fork.

@LordMelkor LordMelkor closed this Nov 18, 2026

This branch was previously deployed

1 inactive deployment
codex-review — 73d76b31 Deployed Nov 18, 2026 by LordMelkor via Run Codex Security Review #15946
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant