Skip to content

feat(messages): let a plugin sort, filter and page a reader's rows - #3660

Closed
LordMelkor wants to merge 1 commit into
block:mainfrom
LordMelkor:clod/conversation-projection
Closed

LordMelkor wants to merge 1 commit into
block:mainfrom
LordMelkor:clod/conversation-projection

Conversation

@LordMelkor

@LordMelkor LordMelkor commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Plugins can now sort, filter and page a thread's replies or a channel's timeline in place. Core supplies only the capability. The feature itself lives in the plugin: which sorts exist, what a filter matches, the window size, paging, layout and wording.

The motivating case is a Reddit-like thread. A sort or filter must mean the same thing whether or not the app has already loaded the rows it needs. So a plugin reads the whole conversation itself, keeps live updates of what it read, and asks the reader to show the rows it chose.

Relay side: block/buzz#9632. It lets NIP-CW windows page oldest first (buzz-window-newer). This PR works without it too: the app reads older pages only, and the final rows are the same.

Core changes, measured against the plugin-first rule

Each core change is measured against this project rule: a feature a plugin can build belongs in a plugin, and core adds only the smallest general capability a plugin needs to own that feature.

Change Necessity: why a plugin cannot do it today Minimality: the smallest general hook
session.foldRows(channelId, events, rootId?) The fold that applies edits, reactions, deletions, signature proof, visibility and the viewer's own pending sends uses host-only inputs: canAccess, relayAuthor, archiveAuthority, the agent-owner directory and the outbox. An installed plugin is a blob module (src/plugins/modules.ts:23) and cannot import relay/fold.ts; copying it means about 1,500 lines that drift. One stateless function returning the same ChannelMessage rows the readers fold. No cache, no reads, no view.
session.retain() returning { set, dispose } A live reaction, edit or deletion passes visibility only when every target is already known (relay/event-access.ts:69-70). Rows a plugin read with snapshotRead are known to nobody, so their live updates are dropped for everyone. Edit, react and read marking also cannot find them. One evidence hold per handle, in one of the 64 existing view slots, with an 8 MiB bound. It feeds retainedEvent lookups and unread's message evidence, and nothing else: no accept, no cache, no recent, no unread counts. It grants no access, because every reference is checked on set and on each access change.
snapshotRead(filters, { priority }) A long scan through snapshotRead takes foreground read slots and delays the user's own reads. ReadOptions.priority already exists in the reader (relay/reader.ts:15) but is not reachable from snapshotRead. Pass the existing option through: 1 line plus a type line.
session.windowNewer Whether the relay advertises buzz-window-newer is NIP-11 data that only the host transports read. A boolean beside the existing searchSort, set by both hosts.
Page verification for direction: "newer", root_aux: false and channel 39006 windows A newer thread page fails verification today, and the reader does not check 39006 channel bounds at all. Plugins cannot change verification. Buzz checking its own protocol: bind the new fields into the existing checks.
placement: "reader" accessory, with present, presentation, limits and targetId No plugin surface can replace the rows a reader shows. Accessories mount only in the composer, message and response slots (conversation/service.tsx:389-392). Reuse the accessory contribution, with its ordering, error boundary and revocation. One new placement, one present call and three read-only props.
Reader presentation host (messages/arrangement.ts) and reader wiring (ThreadPanel.tsx, ChannelTimeline.tsx) Showing rows with live per-row truth, keeping the reader's place and reading only what was drawn are the reader's job. A plugin has no access to the scroller, read state or row components. It draws the presented rows with the ordinary message controls. While a presentation shows, its own unread line, jump button and paging are off, and it draws no day dividers and merges no rows into one bubble, because the order is the plugin's; each row's header carries its date. It has no sort, filter or paging policy of its own.
replyTree(rows, rootId?, displayParent?) Display nesting under a plugin-chosen parent goes through the reader's tree builder. An optional parent lookup. It keeps the cycle and unknown-parent guards, and a row nests only under an earlier row.
Partial reading (use-reading.ts, unread.ts) A presentation that is not the whole conversation must mark as read only the rows the person viewed. partial and endMessageId inputs to the existing reading lease.
Window admission fixes (relay/threads.ts, relay/store.ts) Existing races: a live reply inside pages a running read had already fetched was dropped, and channel page checks were not scoped to the window's channel. Fixes in place, each with a test that fails without it.

Edits to FOUNDATION files, all in the approved list:

  • relay/session.ts: foldRows, the retain handle, snapshotRead priority and windowNewer. The retain handle needs two internal wirings: heldEvent in retainedEvent, and evidence passed to createUnread.
  • conversation/contracts.ts: the reader placement and present.
  • conversation/service.tsx: one validation line.
  • plugins/author.ts: type exports.

What the reference plugin shows

tests/fixtures/conversation-arranger/plugin.ts is test-only and not shipped. It uses only ctx and the author types. It has:

  • Sorts: Top (distinct accounts that reacted) and Oldest.
  • Filter: a text filter across every row it has read. It finishes reading first, so results are never partial.
  • Paging: Next and Previous move the shown window along its ranked list, past the reader's 460-row (thread) and 2,400-row (channel) limits.
  • Live: new replies and channel messages, reactions, edits and deletions re-rank and re-filter as they arrive.
  • Reconnect: once the live connection drops (session.live), it marks its result as not exact. After it reconnects, it reads the conversation again, but only while its window is visible, and after a random wait of up to 10 seconds, so a relay restart does not make every viewer read at once. Meanwhile the reader keeps the shown rows live.
  • Also: More, a return to Buzz order, and recovery after ended.

The tests compare its output against an oracle built from raw events, over a 2,000-reply thread and a 5,000-row channel.

Size

Production: 1,396 added and 154 removed lines, comments included. The largest parts are arrangement.ts (489), ThreadPanel.tsx (+183), ChannelTimeline.tsx (+174) and session.ts (+142). Tests and fixtures: about 6,300 lines. Docs: 110 lines.

Known limits

  • Live "Top" at very large scale. A live, exact "Top" needs every reply's events held. That fits a few thousand replies; a 50,000-reply thread is about 250 pages and 65 MB per viewer, past the 8 MiB hold bound. Because foldRows is stateless, a plugin can still rank exactly at scan time without holding everything, but live changes then reach only the rows it holds. The reference plugin holds everything it reads, so past the bound it stops with an error. A true live "Top" at that size needs a relay-ranked query, which is a separate relay capability.
  • Reconnects re-read. The reference plugin reads the whole conversation again after each reconnect (about 13 page reads for 2,500 replies), because changes made while the connection was down may never arrive live. It does so only for a visible window, after a random wait, at background priority, and a failed read waits for the person to pick a sort again.
  • Scans through readPage. A readPage scan of more than about 2,400 replies trims or empties the open channel window. This predates this change. The docs send scans to snapshotRead, which feeds no projection.
  • Historical multi-target deletions. A deletion naming two rows, when only one of them is presented, fails the all-targets visibility check and does not apply. The relay rejects new deletions with more than one target, so this needs older data.
  • Two reader accessories at once. A held accessory gets no live evidence while another one is shown. When the shown one leaves, the held one is shown from its last presentation, so a deletion made in the meantime can reappear until it presents again.

Earlier review points

An earlier version of this PR added a headless registerArranger({ include, compare }) with no UI of its own. Its review asked for a host-visible sign that rows are hidden, noted that a filter matching nothing left a blank list, and noted that channel read marking used the unfiltered rows. In this version:

  • A plugin can present only from a mounted reader accessory. The host draws that accessory in the reader bar, directly above the rows, and withdraws its presentation when it unmounts. The plugin owns what the bar says, including an empty result: the reference plugin shows "No replies match". An installed plugin is trusted code that can already send and delete as the user, so core adds no separate indicator or wording.
  • Read marking follows the presented rows: a presentation is partial unless the plugin says it is complete, and a partial one marks as read only the rows the person viewed.
  • include/compare and loadAll are gone.

Related issue

None found.

Testing

All runs were on a separate Linux machine, at this PR's head, 2966a32, on main 46a09db.

  • Unit and component tests: pnpm typecheck, the types, design-system and rust-clippy pre-push lanes, and the full vitest run (1,148 files passed, 8 skipped, no failures) all pass.
  • Review findings fixed, each with a test that fails when the fix is reverted:
    • a re-present could swallow the next live refold;
    • the presentation host dropped a NIP-09 deletion that carried no h tag, so the deleted row stayed visible (snapshot and live);
    • the reference plugin stayed "exact" after a reconnect, and repaired only the shown rows;
    • the reference plugin's channel Top ignored new live messages;
    • after that fix, an Oldest list could show live rows the read had not reached yet, out of order, and a plugin with no sort chosen held every new channel message;
    • while a presentation showed, the readers still drew day dividers and merged rows into one bubble by Buzz order (thread and channel);
    • a held presentation kept the plugin's whole read, not only the rows the reader selected;
    • the reference plugin read the conversation again at once after a reconnect, also in a hidden window.
  • End-user test by the maintainer's agent: the real Buzz browser app against a real local relay built from feat(relay): read NIP-CW windows oldest first buzz#9632 (605ba9884, tree identical to its head 73d76b314), with a 2,500-reply thread and a 3,000-message channel. A Playwright script checks every window against a database oracle and keeps 28 screenshots. All 33 checks pass, with no console or page errors outside the deliberate connection cut:
    • Oldest: the first window is replies 1–200 (about 1.0 s). Next reads forward to "2,401–2,500 of 2,500" (0.4–1.0 s per page).
    • Top: ranks across all 2,500 replies (first sorted window 3.2 s). Windows 1–3 equal the oracle. The list has no day dividers and no merged rows, in the thread and in the channel; Buzz order shows its dividers again.
    • Filter: broad, narrow and cleared filters match the oracle, including one typed after only 200 rows were read.
    • Live: one reaction moves rank 201 to first in about 0.3 s. 30 replies posted while Oldest is open appear in order at the end.
    • Reconnect: the live connection is cut at the TCP level. The status shows "of 2,500+" within 1.3 s. While it is down, a reply gets 40 reactions and the first shown row is deleted. 10.3 s after the connection returns, the status is exact again (this includes the random wait of up to 10 s), and window 1 equals the oracle.
    • Channel: Oldest pages past row 2,400 to the last message. Top windows equal the oracle; no seeded channel message has reactions, so this checks the tie-break, and ranking by reactions is covered by the live check and the unit tests. A new message with 40 reactions comes first in 0.54 s.
    • Buzz order returns the reader's own rows.
    • Plugin loading: the reference plugin is built, unmodified, with the Vite config that buzzodz plugin init scaffolds, into one self-contained plugin.js, and Buzz loads it through its ordinary module loader (src/plugins/modules.ts, a blob import). Browser builds cannot install external plugins, so a small shim, applied only on the test machine and not part of this PR, replaces the desktop plugin storage: it lists the plugin in the catalog and returns its code. The Tauri desktop app was not run, because that machine cannot build it.
    • Screenshots show the reference plugin, which is a test fixture: its controls are plain, unstyled buttons.

buzz-review-completed is not added.

Made with human guidance from the maintainer driving this work.

🤖 Generated with Claude Code

@LordMelkor
LordMelkor requested a review from a team as a code owner November 16, 2026 22:54

@joahg joahg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Commenting rather than approving. This changes what users see in the conversation UI and touches FOUNDATION files, so it needs a design or core-team reviewer. Please get a maintainer sign-off on the three FOUNDATION files: src/features/conversation/contracts.ts, src/features/conversation/service.tsx and src/plugins/author.ts. The edits are additive (an optional arrangers extension, registerArranger, type exports), but they add a new plugin power, so the contract itself is worth a deliberate decision.

Worst issue: P2, no blocker found. What I checked:

  • Contract: arrangers is optional on ConversationExtensions and existing plugins are unaffected. A plugin built against the new types will throw on an older host (registerArranger is undefined), so authors should feature-detect. ThreadView.loadAll is a required addition to the view type, so any other ThreadView fake or implementer needs it too; the PR updates one test fake and CI is still running.
  • Misbehaving plugin: a throwing include or a throwing/non-finite compare is caught, and the arranger is contained by ContributionBoundary and revoked on retire. A hung include/compare still blocks the main thread, as any plugin code can. A filter that matches nothing can drive the channel fill loop past the 5-page cap, but it is bounded by historyLimited; thread loads are bounded by the 2,000 event / 4 MiB budget.
  • What a plugin can now do: silently hide any reply in a thread or channel (other than the root, the selected message and the viewer's own sends) and reorder thread replies, with no host-drawn indicator that a filter or order is active, and a channel filter that hides everything shows a blank list. Message renderers can already mislead somewhat, but this is a stronger and quieter lever. A visible host indicator (or a way to see hidden counts) seems worth deciding before this ships as a generic extension point.
  • Read state: the channel timeline still passes unfiltered window.rows to useReading (latestMessageId), while the boundary and rendering use the filtered rows. If the newest row is hidden, catch-up never fires (unread badge can stick); if the newest row is shown but earlier ones are hidden, reaching the bottom catches up rows the user never saw. Mentions and broadcasts are not read by catch-up, so those stay unread. The description says unread logic is unchanged; I would like that behaviour confirmed as intended, with a test.
  • Paging and anchoring: the place rules, nearest-row hand-off, StrictMode replay handling and the refiltered guard against counting re-shown rows as new read plausibly. I could not verify real-browser Virtua behaviour; the author notes it is untested and the tests use a mocked virtualizer.
  • Tests: substantial unit and viewport coverage. CI was still running when I looked (JavaScript, Browser journeys, Flake checks, Rust), so the result is unknown.

Not reviewed in depth: the loadAll staging and budget logic in relay/threads.ts beyond a read-through, the full ThreadPanel scroll-placement effect, the ~1,100 lines of test code, and the docs wording.

!window.hasMore && !window.historyLimited,
)
: window.rows,
includedReplies(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 include filters here, but useReading below still gets latestMessageId from the unfiltered window.rows. If the newest row is hidden the channel never catches up; if it is shown while earlier rows are hidden, catch-up marks rows the user never saw. Is that the intended read behaviour with a filter, and is there a test for it?

Comment thread src/features/conversation/contracts.ts Outdated
* applies an order once the whole thread is loaded. A throwing `include` shows
* every reply; a throwing or non-finite `compare` keeps host order. */
export type ConversationArrangement = Readonly<{
include?(row: ChannelMessage): boolean;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Design question for a maintainer: any plugin with conversation can now hide replies with no host-visible indicator that a filter is active (and a channel filter that hides everything shows a blank list). Should the host show a minimal active-arrangement marker, or a hidden-row count, so a plugin cannot hide messages silently?

Comment thread src/features/messages/ThreadPanel.tsx Outdated
if (next === HOST_ARRANGEMENT) setOwnSent(new Set());
// Reading starts in this commit, so a thread held at the page limit is
// not ordered before the rest of it arrives.
else void view.loadAll();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 This reads the whole thread (up to 2,000 events / 4 MiB) for any arrangement, including an include-only filter that does not need the whole thread. Consider calling loadAll only when compare is set, or confirm the filter case needs it.

@joahg joahg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Followup on 63f8f80. The fix looks right. Judging a change against the arrangement the reader last rendered, rather than the last one published, turns an effect's cleanup and setup in the same pass into a single change, so the same order keeps the reader's place. Own sends are now cleared only when the host order actually renders, so a withdraw-and-republish no longer drops them either. The new test covers the authoring example's lifecycle.

My earlier points still apply: channel read marking uses the unfiltered rows, there's no host-visible sign that an arrangement is hiding messages, and loadAll runs for include-only filters. This is still Buzz UI that touches FOUNDATION files, so it needs a design or core-team reviewer and a maintainer's sign-off, and I'm commenting rather than approving.

@LordMelkor
LordMelkor marked this pull request as draft November 17, 2026 04:16
@LordMelkor
LordMelkor force-pushed the clod/conversation-projection branch from 63f8f80 to b18c1da Compare November 18, 2026 00:32
@LordMelkor LordMelkor changed the title feat(conversation): let plugins sort, filter and page conversation rows in place feat(messages): let a plugin sort, filter and page a reader's rows Nov 18, 2026
@LordMelkor
LordMelkor force-pushed the clod/conversation-projection branch 3 times, most recently from eda010a to 6e7315a Compare November 18, 2026 05:28
Give plugins the general capability to arrange a thread's replies or a
channel's timeline in place, while the feature (sorts, filters, window size,
paging, wording) lives in the plugin.

Core hooks:
- `session.foldRows(channelId, events, rootId?)`: the rows the readers fold
  from proven events, with the viewer's own pending sends and delivery state.
- `session.retain()`: one `{ set, dispose }` hold of proven, visible events so
  live edits, reactions and deletions of scanned rows pass visibility. It grants
  no access and feeds no cache, unread count or view; unread uses it as evidence.
- `snapshotRead(filters, { priority: "background" })` and
  `session.windowNewer` for relays that page NIP-CW windows oldest first.
- A `"reader"` accessory placement with `present({ ids, events, parents?,
  open?, complete?, place? })`, `presentation`, `limits` and `targetId`.

The thread and channel readers show a presentation with their own controls,
limits and reading, draw no day dividers or grouped rows while it shows, and
return to the host rows at the reader's place on withdraw. Window admission
fixes: live replies inside pages a running read has fetched are admitted,
channel page verification is scoped to the window's channel, a list that turns
partial retakes its reading leases, and display parents nest only under
earlier rows.

A test-only reference plugin sorts (Top, Oldest), filters across every scanned
row and pages past the reader's limits over a 2,000-reply thread and a
5,000-row channel.

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: LordMelkor <kray@squareup.com>
@LordMelkor

Copy link
Copy Markdown
Contributor Author

🤖 Replaced by #3818: the same commit, on a branch in this repo instead of a fork. The review here covered an earlier design; #3818's "Earlier review points" section says how each point was handled.

@LordMelkor LordMelkor closed this Nov 18, 2026
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.

2 participants