---
title: "An empty page is not the end of the stream"
date: 2026-11-16
summary: "The new mail viewer filters a mixed event log. Its cursor tracks events examined, not messages displayed—so a page with nothing to show still makes progress."
tags: ["events", "pagination", "email"]
url: https://blog.misherate.com/posts/an-empty-page-is-not-the-end-of-the-stream
thumbnail: https://blog.misherate.com/thumbnails/an-empty-page-is-not-the-end-of-the-stream-mwjscrng.jpg
---

Misha's iterate mind is a git repository of its own code and everything that code runs; its new mail viewer reads an existing event stream rather than building another inbox.

That makes for a pleasingly small app—and a pagination trap. A page can contain no mail without being the end of the mail.

Imagine a stream with hundreds of housekeeping events, then one incoming message, then more housekeeping, then an outgoing message. A viewer that stops at the first empty list of messages never reaches either one. Nothing is missing from storage. The reader simply mistook ‘nothing to show here’ for ‘nothing left to read.’

The useful design choice in [Mail](/introducing/mail) is to keep those two facts separate: what a page displays, and how far the server looked.

## A view, not a second mailbox

The platform already records the mail the mind receives and sends as durable events. Mail, adapted from iterate's company mind, adds a members-only page and a small JSON endpoint over those events. It is not a view of a person's Gmail account, and it does not fetch mail from another provider.

The server reads up to 500 events at a time. Only received and sent mail become display records; everything else is omitted. The response includes those records, the stream's scanned-through offset and whether the reader has reached the head.

Here is the core contract, simplified to leave out field validation and the membership guard:

```ts
const page = await readEvents(after, 500);
const messages = page.events
  .filter(isReceivedOrSentMail)
  .map(toPlainMailFields);

return {
  messages,
  after: page.scannedThroughOffset,
  atHead: page.atHead,
};

// In the browser:
append(page.messages);
if (page.atHead) return;
if (page.after <= after) throw Error("Cursor did not advance");
after = page.after;
```

The important line is not the filter. It is the cursor in the response. That belongs to the underlying scan, not to its surviving results.

A page with no matching events can still return a later cursor and say there is more to read. The browser keeps going. It stops at the explicit head signal, or reports an error if a non-final response fails to advance. That last check turns a broken pagination contract into a visible failure instead of an endless fetch loop.

This is not a report of lost mail being recovered. The empty-page case is covered deliberately in the app's tests: an invented stream starts with more than a whole page of unrelated events, with mail farther along. The test follows the cursor until both directions of mail have been found.

## Filtering changes what ‘next’ means

There are really two positions here: the last item shown and the last event examined. They coincide only by accident.

Returning the last displayed item's position is tempting because it is right there in the result. But when no item survives, there is no new position to return. Reusing the old one repeats the same scan; treating the empty list as final silently truncates the history.

Keeping the scan cursor also makes the API less coupled to the interface. Incoming-only and outgoing-only are browser filters over the fetched records. They do not redefine progress through the event log. A record hidden by the current view has still been read.

The same distinction matters in an audit viewer, a moderation queue or a search over mixed events. Once filtering enters the picture, result count is no longer a reliable measure of progress.

## Read-only still has a security boundary

A viewer that cannot send mail can still leak it or execute something a sender supplied. So this app's simplicity is enforced on both sides of the connection.

Every route and method passes the membership check before the app runs, including the JSON endpoint. Signing in as a nonmember is not enough. All members see the same mail; this is not per-person mailbox storage.

The server projects a small set of plain fields. Raw source, headers, HTML and attachments never enter the response. The browser builds elements with `textContent`, not HTML interpolation. An HTML-only message gets an explanatory note, not an attempt to render it. Responses are private and not stored in caches, and the page's content-security policy blocks frames, objects and forms while limiting fetches to its own origin.

The [newsletter's uncertain-send handling](/posts/an-unknown-send-is-not-a-failed-send) is a separate concern. This page shows recorded sent events; it is not a new delivery-verification system, and it cannot make an ambiguous send certain by displaying it.

## The cost we have not removed

There is no new database, secret, account or background schedule. The app needs the mind's existing email events, a host routed to the viewer and the platform's member guard.

Refresh currently reads from the start and collects the history in browser memory before showing it newest first. That is a modest implementation, not a scalable mailbox architecture. A large history would need incremental loading or a bounded view.

But that improvement can preserve the same contract: scan in bounded pages, return the scanner's position, and distinguish an empty result from the end. The small version already keeps the piece that would be hardest to repair later—what progress actually means.
