Skip to content

Commit 8cb0881

Browse files
author
Brain
committed
fix(desktop): retain interactive priority during reconnect
Signed-off-by: Brain <1a02c72794dcd0f07058a353bc3a81f4028b8c77c92c87fce6d5c8b85970a20b@buzz.block.builderlab.xyz>
1 parent 8842dbd commit 8cb0881

2 files changed

Lines changed: 134 additions & 16 deletions

File tree

‎desktop/src/features/huddle/lib/useTtsSubscription.transport.test.mjs‎

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,123 @@ for (const cooldown of [false, true]) {
238238
assert.ok(requests()[i].at - requests()[i - 1].at >= 250);
239239
});
240240
}
241+
242+
// Exercise the session's reconnect entry point; only replacement socket
243+
// establishment is fake. Hook registration, reset, replay and delivery are real.
244+
for (const visibleChannel of [null, "cold-295"]) {
245+
test(`companion TTS retains priority across reconnect (visible=${visibleChannel})`, async () => {
246+
await mountTts();
247+
await advance(250);
248+
const original = ttsRequests()[0].frame;
249+
const event = {
250+
id: "before-reconnect",
251+
kind: 9,
252+
pubkey: "agent",
253+
created_at: 1001,
254+
tags: [["h", "huddle"]],
255+
content: "Already spoken",
256+
sig: "",
257+
};
258+
await act(async () => {
259+
await deliver(["EVENT", original[1], event]);
260+
await deliver(["EOSE", original[1]]);
261+
await flush();
262+
});
263+
assert.equal(spoken.length, 1);
264+
relayClient.setVisibleChannelId(visibleChannel);
265+
relayClient.resetConnection(new Error("socket lost"));
266+
window.clearTimeout(relayClient.reconnectTimeout);
267+
relayClient.reconnectTimeout = null;
268+
relayClient.wsId = 8;
269+
writes.length = 0;
270+
await deliver(["NOTICE", "rate-limited: quota exceeded; retry in 4s"]);
271+
const replay = relayClient.replayLiveSubscriptions();
272+
await advance(3999);
273+
assert.equal(requests().length, 0, "priority cannot bypass cooldown");
274+
await advance(1);
275+
assert.equal(requests().length, 8, "retain the reconnect batch cap");
276+
const firstChannels = requests().map(({ frame }) => frame[2]["#h"][0]);
277+
assert.deepEqual(
278+
firstChannels.slice(0, visibleChannel ? 2 : 1),
279+
visibleChannel ? [visibleChannel, "huddle"] : ["huddle"],
280+
"visible and interactive tie in registration order ahead of cold work",
281+
);
282+
assert.deepEqual(
283+
ttsRequests()[0].frame,
284+
original,
285+
"retain replay filter and owner",
286+
);
287+
assert.equal(relayClient.visibleChannelId, visibleChannel);
288+
const replayStart = now;
289+
await act(async () => {
290+
await deliver(["EVENT", original[1], event]);
291+
await deliver([
292+
"EVENT",
293+
original[1],
294+
{ ...event, id: "after-reconnect", content: "New speech" },
295+
]);
296+
await deliver(["EOSE", original[1]]);
297+
await flush();
298+
});
299+
assert.deepEqual(
300+
spoken.map(({ text }) => text),
301+
["Already spoken", "New speech"],
302+
);
303+
await advance(49);
304+
assert.equal(requests().length, 8, "retain inter-batch delay");
305+
await advance(1);
306+
assert.equal(requests().length, 16);
307+
await deliver(["NOTICE", "rate-limited: quota exceeded; retry in 4s"]);
308+
await advance(3999);
309+
assert.equal(requests().length, 16, "recheck cooldown between batches");
310+
await advance(1);
311+
assert.equal(requests().length, 24);
312+
await advance(2000);
313+
await replay;
314+
relayClient.reconnectWaiters.settle();
315+
await flush();
316+
assert.equal(
317+
requests().length,
318+
297,
319+
"all background owners recover exactly once",
320+
);
321+
assert.equal(new Set(requests().map(({ frame }) => frame[1])).size, 297);
322+
for (let i = 8; i < requests().length; i += 8)
323+
assert.ok(requests()[i].at - requests()[i - 1].at >= 50);
324+
assert.equal(requests()[0].at, replayStart);
325+
await advance(1000);
326+
assert.equal(
327+
requests().length,
328+
297,
329+
"retired startup drain cannot duplicate replay",
330+
);
331+
});
332+
}
333+
334+
test("unmounting the companion TTS hook during reconnect cooldown cancels its owner", async () => {
335+
const hook = await mountTts();
336+
await advance(250);
337+
const id = ttsRequests()[0].frame[1];
338+
relayClient.resetConnection(new Error("socket lost"));
339+
window.clearTimeout(relayClient.reconnectTimeout);
340+
relayClient.reconnectTimeout = null;
341+
relayClient.wsId = 8;
342+
writes.length = 0;
343+
await deliver(["NOTICE", "rate-limited: quota exceeded; retry in 4s"]);
344+
const replay = relayClient.replayLiveSubscriptions();
345+
await act(async () => {
346+
hook.unmount();
347+
await flush();
348+
});
349+
assert.equal(relayClient.subscriptions.has(id), false);
350+
await advance(6000);
351+
await replay;
352+
relayClient.reconnectWaiters.settle();
353+
await flush();
354+
assert.equal(
355+
ttsRequests().length,
356+
0,
357+
"reconnect must not resurrect a departed huddle",
358+
);
359+
assert.equal(requests().length, 296);
360+
});

‎desktop/src/shared/api/relayReconnectReplay.ts‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -278,22 +278,20 @@ export async function replayLiveSubscriptions({
278278
};
279279
});
280280

281-
// Sort the visible channel's subscriptions first so the user sees their
282-
// active channel recover before others on degraded networks.
283-
if (visibleChannelId !== null) {
284-
replayRequests.sort((a, b) => {
285-
const aVis =
286-
(a.subscription.filter["#h"] as string[] | undefined)?.includes(
287-
visibleChannelId,
288-
) ?? false;
289-
const bVis =
290-
(b.subscription.filter["#h"] as string[] | undefined)?.includes(
291-
visibleChannelId,
292-
) ?? false;
293-
if (aVis === bVis) return 0;
294-
return aVis ? -1 : 1;
295-
});
296-
}
281+
// Visible timeline and interactive consumers (including off-screen huddle
282+
// speech) recover before cold work. Stable sort preserves registration order
283+
// within each tier, including ties between visible and interactive owners.
284+
const isForeground = (
285+
subscription: Extract<RelaySubscription, { mode: "live" }>,
286+
) =>
287+
subscription.priority === "interactive" ||
288+
(visibleChannelId !== null &&
289+
(subscription.filter["#h"]?.includes(visibleChannelId) ?? false));
290+
replayRequests.sort(
291+
(a, b) =>
292+
Number(isForeground(b.subscription)) -
293+
Number(isForeground(a.subscription)),
294+
);
297295

298296
// Send live REQs in capped batches with inter-batch delays to avoid
299297
// triggering per-pubkey admission control on degraded/recovering connections.

0 commit comments

Comments
 (0)