Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ work-completion checks, not new timing counters. The workload covers populated
thread mounting and panel reflow, not video playback, gallery interaction,
expanding nested replies or live agent streaming.

`thread-unread-open-60` and `thread-unread-open-200` open a strict-window thread
of 60 or 200 replies that are all unread. The reader pages older to the first
unread reply before it reveals the thread. The production broker answers each
older page after a modeled 150ms round trip. They report `firstRowsMs` and
`olderReads`.

## Thread scrolling

`--scenario thread-scroll` measures the long-thread workload behind the
Expand Down
10 changes: 7 additions & 3 deletions docs/relay-queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,10 @@ including empty pages whose raw scan cursor does not occur among delivered event

Strict root admission/validation runs once before each load or refresh traversal,
not between the pages of a retained-range repair: repairing N pages uses one root
read plus N independently verified window reads. Each separate scrollback load
still reads the root. Root deletion observed live and access revocation retain their
read plus N independently verified window reads. A load given a read frontier
pages until it reaches a reply at or before that time the same way, so opening a
thread at its first unread reply reads the root once. Each separate scrollback
load still reads the root. Root deletion observed live and access revocation retain their
existing session paths; a later refresh/load revalidates the root. Neither the root
lookup nor the page sequence provides an atomic snapshot.

Expand All @@ -243,7 +245,9 @@ fallback. Non-UUID channels retain the legacy path. No capability cache persists
across owners or connections.

Each strict thread starts with 10 traversal rows plus their auxiliary events; later pages
request 50 traversal rows, with at most ten pages per repair/load range. Legacy
request 50 traversal rows, with at most ten pages per repair/load range. A load to a
read frontier requests up to four pages (200 rows, the relay's `thread_window`
maximum) per window and counts each as four of its ten. Legacy
mode still requests 50 rows per page. The shared panel positions after the first
strict page and demand-loads older pages on scrollback. Legacy mode still
automatically walks its bounded oldest-first range and cannot promise the newest
Expand Down
1 change: 1 addition & 0 deletions src/bundled/channels/useConversationTabs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2132,6 +2132,7 @@ it("a hidden retained thread tab's rows re-render for new panels, not channel-li
snapshot: () => undefined,
subscribe: () => () => {},
attention: () => ({ unread: false }),
threadReadThrough: () => -1,
},
} as unknown as RelaySession;
let available: RegisteredPanel[] = [];
Expand Down
1 change: 1 addition & 0 deletions src/features/conversation/EmbeddedThread.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ function fixture(
snapshot: () => undefined,
subscribe: () => () => {},
attention: () => ({ unread: false }),
threadReadThrough: () => -1,
enterChannel: vi.fn(async () => {}),
leaveChannel: vi.fn(),
setChannelActive: vi.fn(),
Expand Down
1 change: 1 addition & 0 deletions src/features/messages/ThreadPanel.nesting.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ function setup(messageId = "root") {
subscribe: () => () => {},
snapshot: () => undefined,
attention: () => ({ unread: false }),
threadReadThrough: () => -1,
},
} as unknown as RelaySession;
render(
Expand Down
9 changes: 5 additions & 4 deletions src/features/messages/ThreadPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1081,16 +1081,17 @@ function ThreadMessages({
(row) => row && session.unread.attention(channelId, row.id).unread,
).length;
const unreadOlder = (threadUnread?.observedCount ?? 0) > loadedUnread;
const readThrough =
since ?? session.unread.threadReadThrough(channelId, messageId);
if (
known &&
snapshot.direction === "older" &&
(snapshot.canLoadMore || snapshot.limited) &&
oldest &&
(unreadOlder ||
oldest.createdAt >
(since ?? session.unread.threadReadThrough(channelId, messageId)))
(unreadOlder || oldest.createdAt > readThrough)
) {
if (snapshot.canLoadMore) void view.loadMore();
// Page to the frontier in one read, not one page per decision.
if (snapshot.canLoadMore) void view.loadMore(readThrough);
// Past the page limit the first unread reply stays unknown.
else setBoundary({ ...boundary, id: null });
return;
Expand Down
86 changes: 86 additions & 0 deletions src/features/relay/thread-window.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,92 @@ it("pages 303 tied replies newest-first, deduplicates live rows and repairs reta
h.view.snapshot().replies.find((r) => r.id === oldest.id)?.content,
).toBe(oldest.content);
});
it("pages older to a read frontier in one read, revalidating the root once", async () => {
const newest = Array.from({ length: 303 }, (_, n) => reply(n)).sort(
(a, b) => b.created_at - a.created_at || a.id.localeCompare(b.id),
);
const h = await setup((f) => {
if (f.ids) return [root];
const eligible = newest.filter(
(e) =>
f.until === undefined ||
e.created_at < f.until ||
(e.created_at === f.until && e.id > (f.before_id ?? "")),
);
const page = eligible.slice(0, f.limit);
const last = page.at(-1);
assert.exists(last);
return [
...page,
bounds(
f,
eligible.length > page.length
? { created_at: last.created_at, id: last.id }
: null,
),
];
});
await h.view.refresh();
const before = h.requests.length;
await h.view.loadMore(50);
const read = h.requests.slice(before);
expect(read[0]).toEqual([{ ids: [root.id], "#h": [channel], limit: 1 }]);
expect(read.slice(1).map(([f]) => f?.limit)).toEqual([200]);
expect(h.view.snapshot()).toMatchObject({
status: "ready",
canLoadMore: true,
});
expect(h.view.snapshot().replies[0]?.createdAt).toBeLessThanOrEqual(50);

// Requested during a refresh, the load keeps its frontier.
const queued = h.requests.length;
const refreshing = h.view.refresh();
void h.view.loadMore(12);
await refreshing;
await vi.waitFor(() => {
expect(h.view.snapshot().replies[0]?.createdAt).toBeLessThanOrEqual(12);
expect(h.view.snapshot().status).toBe("ready");
});
// One root read for the refresh, one for the queued load.
expect(h.requests.slice(queued).filter(([f]) => f?.ids)).toHaveLength(2);
});
it("keeps a frontier load within the ten-page range", async () => {
const newest = Array.from({ length: 500 }, (_, n) => reply(n)).sort(
(a, b) => b.created_at - a.created_at || a.id.localeCompare(b.id),
);
const h = await setup((f) => {
if (f.ids) return [root];
const eligible = newest.filter(
(e) =>
f.until === undefined ||
e.created_at < f.until ||
(e.created_at === f.until && e.id > (f.before_id ?? "")),
);
const page = eligible.slice(0, f.limit);
const last = page.at(-1);
assert.exists(last);
return [
...page,
bounds(
f,
eligible.length > page.length
? { created_at: last.created_at, id: last.id }
: null,
),
];
});
await h.view.refresh();
const before = h.requests.length;
await h.view.loadMore(-1);
expect(h.requests.slice(before + 1).map(([f]) => f?.limit)).toEqual([
200, 200, 50,
]);
expect(h.view.snapshot()).toMatchObject({
canLoadMore: false,
limited: true,
});
expect(h.view.snapshot().replies).toHaveLength(460);
});
it("uses raw scan progress across empty pages without deriving cursors from rows", async () => {
let pages = 0;
const h = await setup((f) =>
Expand Down
42 changes: 31 additions & 11 deletions src/features/relay/threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ const AUX = new Set([5, 7, 9005, 40003, 39005, 39006]);
const PAGE_SIZE = 50;
const INITIAL_WINDOW_SIZE = 10;
const MAX_PAGES = 10;
/** A load to a read frontier asks for this many pages per window: 200 rows,
* the relay's `thread_window` limit. */
const FRONTIER_PAGES = 4;
const MAX_EVENTS = 2000;
const MAX_BYTES = 4 * 1024 * 1024;
/** The relay's `thread_window` row allowlist excludes legacy diffs (kind 40008). */
Expand Down Expand Up @@ -95,7 +98,9 @@ export type ThreadView = {
snapshot(): ThreadSnapshot;
subscribe(listener: () => void): () => void;
refresh(): Promise<void>;
loadMore(): Promise<void>;
/** Reads older replies; with `through`, keeps paging in this one read until
* a reply at or before that time is loaded. */
loadMore(through?: number): Promise<void>;
/** Change exact selection only when verified ancestry belongs to this thread. */
retarget(messageId: string, signal: AbortSignal): Promise<ThreadRetarget>;
dispose(): void;
Expand Down Expand Up @@ -237,8 +242,9 @@ export function createThreadView({
let controller: AbortController | undefined;
/** A refresh requested while a read runs; the stream's return carries over. */
let again: false | "refresh" | "established" = false;
/** An older page requested during a background repair, which stays ready. */
let older = false;
/** An older load requested during a background repair, which stays ready:
* the earliest read frontier asked for, or Infinity for one page. */
let older: false | number = false;
/** Resumed rows shown beside the evidence until the first refresh settles.
* That refresh reads as a fresh view would, in the background. */
let shown: ThreadResume | undefined;
Expand Down Expand Up @@ -667,12 +673,16 @@ export function createThreadView({
}
}

async function run(replace: boolean, established = false) {
async function run(replace: boolean, established = false, through?: number) {
if (disposed) return;
if (controller) {
if (established) again = "established";
else if (replace) again ||= "refresh";
else older = true;
else
older = Math.min(
older === false ? Infinity : older,
through ?? Infinity,
);
return;
}
if (!canAccess()) {
Expand Down Expand Up @@ -754,13 +764,18 @@ export function createThreadView({
limit: PAGE_SIZE,
include_aux: true,
};
// The pages the next strict window spends of this load's range.
const span = () =>
through === undefined || nextPages === 0
? 1
: Math.min(FRONTIER_PAGES, MAX_PAGES - nextPages);
const windowRead = (signal: AbortSignal) =>
reader.read(
[
{
...filter,
kinds: STRICT_WINDOW_KINDS,
limit: nextPages === 0 ? INITIAL_WINDOW_SIZE : PAGE_SIZE,
limit: nextPages === 0 ? INITIAL_WINDOW_SIZE : PAGE_SIZE * span(),
thread_window: true,
...(nextCursor
? { until: nextCursor.created_at, before_id: nextCursor.id }
Expand Down Expand Up @@ -813,10 +828,12 @@ export function createThreadView({
// Where the loop passed the range the reader had, so a fill that never
// reaches a pre-refresh reply can settle back on it.
let range: { cursor: ThreadCursor | undefined } | undefined;
let oldest = Infinity;
for (
let page = 0;
page < targetPages ||
(mode === "older" && !reached && page < MAX_PAGES);
(mode === "older" && !reached && page < MAX_PAGES) ||
(through !== undefined && oldest > through && nextPages < MAX_PAGES);
page++
) {
let response: readonly RelayEvent[];
Expand Down Expand Up @@ -892,6 +909,8 @@ export function createThreadView({
// other content kinds, and appended auxiliaries do not consume its page limit.
const replies = events.filter(isReply).sort(compare);
reached ||= replies.some((event) => kept.has(event.id));
for (const event of replies)
oldest = Math.min(oldest, event.created_at);
if (
!bounds &&
replies.some((event) => nextCursor && compare(event, nextCursor) <= 0)
Expand All @@ -902,7 +921,7 @@ export function createThreadView({
nextCursor = bounds
? (bounds.cursor ?? undefined)
: (replies.at(-1) ?? nextCursor);
nextPages++;
nextPages += mode === "legacy" ? 1 : span();
fetched = union(fetched, related(events));
if (fetched.length > MAX_EVENTS || listByteSize(fetched) > MAX_BYTES) {
retain(fetched);
Expand Down Expand Up @@ -976,9 +995,10 @@ export function createThreadView({
const established = again === "established";
again = false;
void run(true, established);
} else if (older && !disposed) {
} else if (older !== false && !disposed) {
const through = older;
older = false;
void run(false);
void run(false, false, through);
}
}
}
Expand Down Expand Up @@ -1073,7 +1093,7 @@ export function createThreadView({
},
retarget,
refresh: () => run(true),
loadMore: () => run(false),
loadMore: (through?: number) => run(false, false, through),
dispose() {
listeners.clear();
purge(true);
Expand Down
1 change: 1 addition & 0 deletions tests/browser/fixture.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2219,6 +2219,7 @@ export const test = base.extend({
searchProfiles,
report,
pending,
olderPageDelayMs,
// The production broker advertises read-state writes for every session,
// not only tests opting into complete snapshot reads.
acceptPublication: acceptReadPublication,
Expand Down
2 changes: 2 additions & 0 deletions tests/browser/perf/run.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const cases = [
"thread-scroll-reverse",
"background-traffic",
"background-traffic-large-sidebar",
"thread-unread-open-60",
"thread-unread-open-200",
];
// A case name selects only that case; a group selects every case it prefixes.
const groups = [
Expand Down
48 changes: 48 additions & 0 deletions tests/browser/perf/thread-unread.perf.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { test, expect, repeats, timeline } from "./runner.mjs";
import { populateThread, THREAD_CHANNEL } from "./thread-corpus.mjs";
import { hoverMessage } from "../message-actions.mjs";

// Opening a thread whose replies are all unread: the reader pages older to the
// first unread reply before it shows anything. Older pages arrive after a
// modeled relay round trip.
const ROUND_TRIP_MS = 150;
const historySelector = '[aria-label="Thread messages"]';
for (const [name, replies] of [
["thread-unread-open-60", 60],
["thread-unread-open-200", 200],
])
test.describe(name, () => {
test.use({
productionBroker: true,
readState: true,
inboxThreadWindow: true,
olderPageDelayMs: ROUND_TRIP_MS,
channelIds: ["alpha", THREAD_CHANNEL],
historyCounts: { alpha: 1, [THREAD_CHANNEL]: 0 },
});
for (let i = 1; i <= repeats; i++)
test(`${name} sample ${i}`, async ({ page, app, perf }) => {
test.setTimeout(120_000);
const root = populateThread(app, { replies });
const ids = [root.id, ...app.inboxWindow.replies.map((e) => e.id)];
await perf.setup(THREAD_CHANNEL);
const row = page
.locator(timeline(THREAD_CHANNEL))
.locator(`[data-message-id="${root.id}"]`);
await hoverMessage(row);
const reads = app.pending.length;
const sample = await perf.measure(THREAD_CHANNEL, {
click:
'[role="group"][aria-label="Message actions"]:popover-open button[aria-label="Reply"]',
selector: historySelector,
ids,
timeoutMs: 30_000,
work: { replies, roundTripMs: ROUND_TRIP_MS },
});
sample.work.olderReads = app.pending.length - reads;
await expect(page.locator(historySelector)).not.toHaveAttribute(
"data-positioning",
);
await perf.save(sample);
});
});
9 changes: 7 additions & 2 deletions tests/browser/policy-relay.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export function policyRelay({
acceptPublication,
latencyMs = 0,
holdOlder = true,
// Release each held older page after this delay instead of on demand.
olderPageDelayMs = null,
}) {
const sockets = [];
// A page can close between sending a publication and its acceptance. The
Expand Down Expand Up @@ -584,7 +586,7 @@ export function policyRelay({
return new Promise((resolve, reject) => {
const abort = () => reject(init.signal.reason);
init.signal.addEventListener("abort", abort, { once: true });
pending.push({
const receipt = {
community,
channel,
filter,
Expand All @@ -593,7 +595,10 @@ export function policyRelay({
init.signal.removeEventListener("abort", abort);
resolve(Response.json(result));
},
});
};
pending.push(receipt);
if (olderPageDelayMs !== null)
setTimeout(() => receipt.release(), olderPageDelayMs);
});
return Response.json(result);
} catch (error) {
Expand Down
Loading