Skip to content

Commit e4177d0

Browse files
kalvinnchaupeonclaude
authored
fix(relay): keep a ready thread quiet through stream-establishment repair (#2124)
## Problem Every minute, live renewal re-establishes each channel subscription. Each open thread view then ran its establishment repair as a visible cycle: it published `loading`, re-read the window, then published `ready`. That happened even when no row changed. `ThreadPanel` subscribes to the whole snapshot, so every renewal re-rendered the entire thread subtree twice. ## Change `src/features/relay/threads.ts`: - An establishment repair of a view that is already `ready` no longer publishes `loading`. The view stays ready and publishes only if the repair changes a field. - `publish`/`publishStatus` skip the snapshot swap and the notification when every field is identical. - If an older page (`loadMore`) is requested while a background repair runs, it is queued and runs after the repair. Before this change the busy guard dropped it, which was invisible on main because the view showed `loading` during the repair. The queued flag is cleared wherever `again` is cleared (`purge` and the retain-limit path). ## No behavior or traffic changes - The relay sees identical traffic: same reads, filters and timing, and the 60 s renewal interval is unchanged. Live REQs are equal in every cell below. - The repair still runs and still corrects the view. A repair that changes rows publishes the new snapshot as before. - The only intentional difference: a ready thread no longer flashes `loading` during a silent repair. - A background repair that fails still publishes `error`, as on main. ## Tests - New: `threads.test.ts` "repairs a ready thread on stream establishment without notifying when nothing changed, then runs an older page requested meanwhile". It fails on main, where the snapshot goes to `loading`. - `thread-window.test.ts` used `status: "ready"` as its "repair finished" signal. Because the view now stays ready throughout the repair, the test waits on the repaired rows instead. - On this head: the relay suite passes (2,549), and tsc and biome are clean. ## Evidence Browser scale harness: 400 channels, 4 tabs, 125 s idle with a renewal tick. It ran on Linux and macOS workstations, 3 runs per cell; the table shows medians. | OS | engine | commits main → change | renders main → change | idle script ms main → change | live REQs | |---|---|---|---|---|---| | Linux | Chromium | 123 → 58 | 10,974 → 5,069 | 533 → 366 | 166 / 166 | | Linux | WebKit | 122 → 56 | 10,974 → 5,069 | n/a | 166 / 166 | | macOS | Chromium | 123 → 57 | 10,974 → 5,069 | 1,031 → 890 | 166 / 166 | | macOS | WebKit | 122 → 58 | 10,974 → 5,069 | n/a | 166 / 166 | - Commits and renders drop 53–54% on every OS and engine. - Over-budget frame time is within run-to-run noise in both variants. - WebKit does not report script time. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Kalvin Chau <[email protected]> Co-authored-by: peon <9ac6794b000690b7e814eb1805ad32405d0bec7d52838de3a86cf967565dacc0@buzz.block.builderlab.xyz> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 7f793e9 commit e4177d0

3 files changed

Lines changed: 86 additions & 19 deletions

File tree

‎src/features/relay/thread-window.test.ts‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -589,19 +589,20 @@ it("refresh after establishment drops a retained reply it cannot reach within th
589589
await h.view.refresh();
590590
reconnected = true;
591591
h.traffic.established(channel);
592-
await vi.waitFor(() =>
593-
expect(h.view.snapshot()).toMatchObject({
594-
status: "ready",
595-
limited: false,
596-
canLoadMore: true,
597-
}),
598-
);
599592
// Ten pages never reached `old`: it goes rather than sitting below a gap.
600593
// The pages read stay, but the reader had one page, so the range settles
601594
// on one: a later refresh re-reads one page, not ten, and older pages
602-
// continue from its bound.
595+
// continue from its bound. The repair stays ready throughout, so wait on
596+
// its rows.
603597
const shown = fresh.map((row) => row.id).reverse();
604-
expect(h.view.snapshot().replies.map((row) => row.id)).toEqual(shown);
598+
await vi.waitFor(() => {
599+
expect(h.view.snapshot().replies.map((row) => row.id)).toEqual(shown);
600+
expect(h.view.snapshot()).toMatchObject({
601+
status: "ready",
602+
limited: false,
603+
canLoadMore: true,
604+
});
605+
});
605606
let before = h.requests.length;
606607
await h.view.refresh();
607608
expect(h.requests.slice(before)).toMatchObject([

‎src/features/relay/threads.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,56 @@ it("repairs retained thread pages through the real channel-established catch-up
648648
]);
649649
});
650650

651+
it("repairs a ready thread on stream establishment without notifying when nothing changed, then runs an older page requested meanwhile", async () => {
652+
const h = seeded();
653+
const first = h.view.refresh();
654+
h.next().respond([root, reply("old", 10)]);
655+
await first;
656+
const ready = h.view.snapshot();
657+
const statuses: string[] = [];
658+
const stop = h.view.subscribe(() => statuses.push(h.view.snapshot().status));
659+
660+
h.traffic.established("a");
661+
const repair = h.next();
662+
expect(h.view.snapshot()).toBe(ready);
663+
const more = h.view.loadMore();
664+
expect(h.pending).toHaveLength(0);
665+
repair.respond([root, reply("old", 10)]);
666+
await flush();
667+
// The unchanged repair published nothing; only the queued page shows loading.
668+
expect(statuses).toEqual(["loading"]);
669+
expect(h.view.snapshot()).toMatchObject({ readKind: "older" });
670+
expect(h.view.snapshot().replies).toBe(ready.replies);
671+
672+
const page = h.next();
673+
expect(page.filters[1]?.thread_cursor).toBe(10);
674+
page.respond([root, reply("later", 20)]);
675+
await more;
676+
await flush();
677+
expect(h.view.snapshot().replies.map((row) => row.content)).toEqual([
678+
"old",
679+
"later",
680+
]);
681+
expect(statuses.at(-1)).toBe("ready");
682+
stop();
683+
});
684+
685+
it("keeps a failed establishment repair visible and drops the older page requested meanwhile", async () => {
686+
const h = seeded();
687+
const first = h.view.refresh();
688+
h.next().respond([root, reply("old", 10)]);
689+
await first;
690+
691+
h.traffic.established("a");
692+
const repair = h.next();
693+
void h.view.loadMore();
694+
repair.fail(new Error("offline"));
695+
await flush();
696+
expect(h.pending).toHaveLength(0);
697+
expect(h.view.snapshot()).toMatchObject({ status: "error" });
698+
expect(h.view.snapshot().error).toContain("offline");
699+
});
700+
651701
it("offers retry on failures and rejects a non-advancing cursor without erasing readable rows", async () => {
652702
const h = seeded();
653703
const first = h.view.refresh();

‎src/features/relay/threads.ts‎

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ export function createThreadView({
172172
let controller: AbortController | undefined;
173173
/** A refresh requested while a read runs; the stream's return carries over. */
174174
let again: false | "refresh" | "established" = false;
175+
/** An older page requested during a background repair, which stays ready. */
176+
let older = false;
175177
let snapshot: ThreadSnapshot = Object.freeze({
176178
status: "idle",
177179
root: undefined,
@@ -227,12 +229,16 @@ export function createThreadView({
227229
...new Map(batches.flat().map((event) => [event.id, event])).values(),
228230
];
229231
}
230-
function notifyListeners() {
232+
/** A fold that changed nothing keeps the snapshot and notifies no one. */
233+
function emit(next: ThreadSnapshot) {
234+
const keys = Object.keys(next) as (keyof ThreadSnapshot)[];
235+
if (keys.every((key) => next[key] === snapshot[key])) return;
236+
snapshot = Object.freeze(next);
231237
for (const listener of listeners) notify(listener);
232238
}
233239
function publishStatus(patch: Partial<ThreadSnapshot>) {
234240
if (disposed) return;
235-
snapshot = Object.freeze({
241+
emit({
236242
...snapshot,
237243
...patch,
238244
...(exact
@@ -242,7 +248,6 @@ export function createThreadView({
242248
}
243249
: {}),
244250
});
245-
notifyListeners();
246251
}
247252
/** This thread's local operations; others never reach its fold. */
248253
function localOperations() {
@@ -314,7 +319,7 @@ export function createThreadView({
314319
snapshot.replies.every((row, index) => row === nextReplies[index])
315320
? snapshot.replies
316321
: Object.freeze(nextReplies);
317-
snapshot = Object.freeze({
322+
emit({
318323
...snapshot,
319324
...patch,
320325
root: !rootUnavailable
@@ -323,7 +328,6 @@ export function createThreadView({
323328
replies,
324329
...(exact ? { target, targetStatus } : {}),
325330
});
326-
notifyListeners();
327331
}
328332
function retain(events: readonly RelayEvent[], commit = true) {
329333
if (events.length > MAX_EVENTS || listByteSize(events) > MAX_BYTES) {
@@ -337,6 +341,7 @@ export function createThreadView({
337341
cursor = undefined;
338342
pages = 0;
339343
again = false;
344+
older = false;
340345
if (exact) targetStatus = "error";
341346
publish({
342347
status: "error",
@@ -370,6 +375,7 @@ export function createThreadView({
370375
selectionStaged = [];
371376
controller = undefined;
372377
again = false;
378+
older = false;
373379
staged = [];
374380
if (exact) targetStatus = "loading";
375381
if (clear || !canAccess()) {
@@ -536,6 +542,7 @@ export function createThreadView({
536542
if (controller) {
537543
if (established) again = "established";
538544
else if (replace) again ||= "refresh";
545+
else older = true;
539546
return;
540547
}
541548
if (!canAccess()) {
@@ -557,11 +564,14 @@ export function createThreadView({
557564
// Repair retains already-verified presentation; only a new/unavailable
558565
// selection waits for its initial fold. Never unmount a reader on reconnect.
559566
if (exact && replace && targetStatus !== "ready") targetStatus = "loading";
560-
publishStatus({
561-
status: "loading",
562-
error: undefined,
563-
readKind: replace ? "refresh" : "older",
564-
});
567+
// A stream establishment (a reconnect, or a minute renewal) repairs a ready
568+
// view in the background: it stays ready unless the repair changes it.
569+
if (!established || snapshot.status !== "ready")
570+
publishStatus({
571+
status: "loading",
572+
error: undefined,
573+
readKind: replace ? "refresh" : "older",
574+
});
565575
try {
566576
if (exact && replace) {
567577
const revision = selectionRevision;
@@ -797,6 +807,9 @@ export function createThreadView({
797807
});
798808
} catch (error) {
799809
if (active()) {
810+
// A failed repair stays visible with Retry; the page queued behind it
811+
// would clear that error.
812+
older = false;
800813
if (targetStatus === "loading") targetStatus = "error";
801814
publishStatus({ status: "error", error: String(error) });
802815
}
@@ -808,6 +821,9 @@ export function createThreadView({
808821
const established = again === "established";
809822
again = false;
810823
void run(true, established);
824+
} else if (older && !disposed) {
825+
older = false;
826+
void run(false);
811827
}
812828
}
813829
}

0 commit comments

Comments
 (0)