Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6994ea6
feat: support 10,000 joined task channels
dr0uleau Oct 22, 2026
402b154
fix: preserve live delivery during batch recovery
dr0uleau Oct 22, 2026
0405ed4
test: verify broker and discovery limits at scale
dr0uleau Oct 22, 2026
8a8ef51
fix: prioritize batches without an established source
dr0uleau Oct 22, 2026
fcecf15
feat: scale live delivery with resource budgets
dr0uleau Oct 23, 2026
53808c8
fix: preserve live alerts and replay recovery
dr0uleau Oct 29, 2026
57b7bb1
chore: merge main into scale channel delivery
dr0uleau Oct 29, 2026
d161637
test: measure live setup across bounded replay
dr0uleau Oct 29, 2026
4a28711
test: model the relay channel bound
dr0uleau Oct 29, 2026
da26d30
chore: merge main for channel delivery
dr0uleau Oct 29, 2026
bf1f5dd
chore: merge discovery access changes
dr0uleau Oct 29, 2026
ee11b7d
test: choose uncached metadata for byte overflow
dr0uleau Oct 29, 2026
b6f0aaf
fix: keep replay receipts through finite reads
dr0uleau Oct 29, 2026
bf3825a
fix: reject stale preview recovery after overflow
dr0uleau Oct 29, 2026
690f416
fix: retain completed proof reads across revocation
dr0uleau Oct 29, 2026
670e605
chore: merge relay transport updates
dr0uleau Oct 30, 2026
77748a8
chore: merge public directory refresh
dr0uleau Oct 30, 2026
34a77c1
fix: share relay limits with profile sync
dr0uleau Nov 2, 2026
a639803
fix: preserve scale delivery across continuous reconnects
dr0uleau Nov 2, 2026
8187a50
fix: retain bounded replay with grant refresh
dr0uleau Nov 2, 2026
b4fd23b
test: renew broker batches through signed grants
dr0uleau Nov 2, 2026
e958e75
docs: describe grant and timer batch renewal
dr0uleau Nov 3, 2026
26774bd
fix: integrate bounded roster recovery
dr0uleau Nov 3, 2026
60859cc
fix(relay): retain re-added routes before drain
dr0uleau Nov 3, 2026
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
2 changes: 1 addition & 1 deletion browser-host/relay-broker-api.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,7 @@ test.each([
],
["/api/relay/archive", 4096, "bytes", "Archive request too large"],
["/api/relay/stream-presence", 256, "bytes", "Live control too large"],
["/api/relay/stream", 300000, "bytes", "Live interests too large"],
["/api/relay/stream", 64 * 1024 * 1024, "bytes", "Live interests too large"],
["/api/relay/query", 65536, "chars", "Filter body too large"],
["/api/relay/presence-snapshot", 20 * 1024, "bytes", "Filter body too large"],
])(
Expand Down
266 changes: 245 additions & 21 deletions browser-host/relay-broker-live.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,14 @@ async function harness(
const filter = filters[0];
frames.push({ kind, id, at: performance.now(), socket });
if (kind === "AUTH" && !options.holdAuth)
queueMicrotask(() => this.receive(["OK", id.id, true]));
setImmediate(() => this.receive(["OK", id.id, true]));
if (kind === "EVENT") publications.push({ event: id, socket });
if (kind !== "REQ") return;
requests.push({ at: performance.now(), id, filter, filters, socket });
const refused = requests.length === refuseAt;
// WS replies arrive on an I/O turn, which also lets HTTP readers drain.
if (!options.holdSetup)
queueMicrotask(() =>
setImmediate(() =>
this.receive(refused ? ["CLOSED", id, reason] : ["EOSE", id]),
);
},
Expand All @@ -94,7 +95,7 @@ async function harness(
},
};
sockets.push(socket);
queueMicrotask(() => socket.receive(["AUTH", "fixture"]));
setImmediate(() => socket.receive(["AUTH", "fixture"]));
return socket;
},
});
Expand Down Expand Up @@ -125,7 +126,7 @@ async function harness(
const response = await fetch(`${base}/api/relay/stream`, {
method: "POST",
headers: { Origin: origin, "Content-Type": "application/json" },
body: JSON.stringify({ channels }),
body: JSON.stringify({ channels, statusDeltas: options.statusDeltas }),
signal: controller.signal,
});
return { response, abort: () => controller.abort() };
Expand Down Expand Up @@ -259,7 +260,7 @@ test("real HTTP accepts the 1022-channel body and rejects invalid/oversized/orig
large.abort();
await until(() => h.sockets.every((s) => s.readyState === 3));
const count = h.sockets.length;
for (const invalid of [[""], Array(1025).fill("a"), ["x".repeat(150001)]]) {
for (const invalid of [[""], ["x".repeat(150001)]]) {
const { response } = await h.post(invalid);
expect([400, 413]).toContain(response.status);
await response.text();
Expand Down Expand Up @@ -344,11 +345,11 @@ test.each([null, 1])(
.filter((r) => r.status === "limited")
.map((r) => r.channelId)
.sort(),
).toEqual(ids.slice(enabled ? 1021 : 1022));
// Status retains every interest, but only 1024 routes may have a wire.
).toEqual(ids.slice(enabled ? 1020 : 1021));
// One wire stays free for the silence probe. All interests stay visible.
expect(
snapshot.routes.filter((r) => r.status !== "limited"),
).toHaveLength(1024);
).toHaveLength(1023);
expect(snapshot.routes.some((r) => r.id === "observer")).toBe(enabled);
sockets ??= h.sockets.length;
posts ??= streamPosts();
Expand Down Expand Up @@ -1488,16 +1489,21 @@ test("joined batches survive the real HTTP boundary and retirement only rebuilds
(_, i) => `c${String(i).padStart(3, "0")}`,
);
traffic.update([...joined, "preview"], joined);
await until(() => established.mock.calls.length === 29);
expect(h.requests).toHaveLength(29);
await until(
() => established.mock.calls.length === 4 && h.requests.length === 30,
);
expect(h.requests).toHaveLength(30);
const batches = h.requests.filter(
(r) => r.filter["#h"] && r.filter.limit === 0,
);
const replays = h.requests.filter(
(r) => r.filter["#h"] && r.filters.length > 1,
);
expect(batches.map((r) => r.filters.length)).toEqual([
expect(replays.map((r) => r.filters.length)).toEqual([
...Array(25).fill(10),
6,
]);
for (const r of batches)
for (const r of replays)
for (const f of r.filters) {
expect(f["#h"]).toHaveLength(1);
expect(f.limit).toBe(500);
Expand All @@ -1506,7 +1512,7 @@ test("joined batches survive the real HTTP boundary and retirement only rebuilds
established.mock.calls
.filter(([ids]) => Array.isArray(ids))
.map(([ids]) => ids.length),
).toEqual([...Array(25).fill(10), 6]);
).toEqual([256]);
// Globals carry their continuity across the host boundary. A first
// connection has no cursor, so neither proves it missed nothing.
expect(
Expand All @@ -1533,16 +1539,17 @@ test("joined batches survive the real HTTP boundary and retirement only rebuilds
});
expect(malformed.status).toBe(400);
await malformed.text();
expect(h.requests).toHaveLength(29);
expect(h.requests).toHaveLength(30);
const retained = joined.slice(1);
traffic.update([...retained, "preview"], retained);
await until(() => established.mock.calls.length === 30);
expect(h.requests).toHaveLength(30);
expect(h.frames.filter((f) => f.kind === "CLOSE").map((f) => f.id)).toEqual(
[old.id],
);
await until(() => established.mock.calls.length === 5);
expect(established).toHaveBeenCalledTimes(5);
expect(h.requests).toHaveLength(31);
expect(
h.frames.filter((f) => f.kind === "CLOSE" && f.id === old.id),
).toHaveLength(1);
expect(h.requests.at(-1).filters.flatMap((f) => f["#h"])).toEqual(
joined.slice(1, 10),
joined.slice(1),
);
const event = finalizeEvent(
{
Expand All @@ -1558,7 +1565,7 @@ test("joined batches survive the real HTTP boundary and retirement only rebuilds
await until(() => receive.mock.calls.length === 1);
expect(receive).toHaveBeenCalledExactlyOnceWith([event], {
phase: "live",
sourceChannels: joined.slice(1, 10),
sourceChannels: joined.slice(1),
});
expect(h.sockets).toHaveLength(1);
} finally {
Expand Down Expand Up @@ -1679,6 +1686,60 @@ test("startup response ABA retires an old wire before applying the latest intere
}
});

test("a re-added pending route replaces its queued removal before drain", async () => {
let response;
let backpressured = false;
const deltas = [];
const h = await harness(0, undefined, {
holdSetup: true,
statusDeltas: true,
response(res) {
response = res;
Object.defineProperty(res, "writableNeedDrain", {
get: () => backpressured,
});
const write = res.write.bind(res);
res.write = (chunk, ...args) => {
const text = String(chunk);
if (text.startsWith("event: state-delta\n"))
deltas.push(JSON.parse(text.split("data: ")[1]));
return write(chunk, ...args);
};
},
});
try {
const opened = await h.post(["a"]);
await until(() => h.requests.some((r) => r.filter["#h"]?.includes("a")));
const control = async (channels, interestRevision) => {
const result = await fetch(`${h.base}/api/relay/stream-interests`, {
method: "POST",
headers: { Origin: h.base, "Content-Type": "application/json" },
body: JSON.stringify({
streamId: opened.response.headers.get("x-buzz-live-id"),
channels,
interestRevision,
}),
});
expect(result.status).toBe(200);
await result.text();
};
backpressured = true;
await control([], 1);
await control(["a"], 2);
const before = deltas.length;
backpressured = false;
response.emit("drain");
const drained = deltas.slice(before);
expect(drained.flatMap((d) => d.removed)).not.toContain("channel:a");
expect(drained.flatMap((d) => d.routes)).toContainEqual(
expect.objectContaining({ id: "channel:a", status: "pending" }),
);
expect(drained.every((d) => d.interestRevision === 2)).toBe(true);
} finally {
await h.close();
}
});

test.each(["disconnect", "close"])(
"backpressured route state drains latest revision and fences observer transitions and %s",
async (ending) => {
Expand Down Expand Up @@ -2027,6 +2088,169 @@ test("live streams request no agent telemetry without plugin demand", async () =
}
});

test("real broker keeps 100,000 joined channels and delivers outside the first batch", async () => {
const h = await harness();
const nativeFetch = globalThis.fetch;
let traffic;
try {
vi.stubGlobal("fetch", (input, init) =>
nativeFetch(input, {
...init,
headers: {
...init?.headers,
...(init?.method === "POST" ? { Origin: h.base } : {}),
},
}),
);
const transport = await connectBrokerTransport(h.base);
let snapshot;
const received = [];
const established = new Set();
traffic = transport.subscribe({
receive(events) {
received.push(...events);
},
established(ids) {
for (const id of typeof ids === "string" ? [ids] : (ids ?? []))
established.add(id);
},
denied() {},
state(value) {
snapshot = value;
},
});
const ids = Array.from(
{ length: 100_000 },
(_, i) => `${String(i).padStart(6, "0")}-${"c".repeat(121)}`,
);
traffic.update(ids, ids);
await vi.waitFor(
() => {
expect(
established.size,
JSON.stringify({
status: snapshot?.status,
error: snapshot?.error,
requests: h.requests.length,
sockets: h.sockets.length,
}),
).toBe(100_000);
expect(snapshot?.status).toBe("connected");
expect(
snapshot.routes.filter(
(route) => route.channelId && route.status === "live",
),
).toHaveLength(100_000);
},
{ timeout: 30_000 },
);
expect(h.sockets).toHaveLength(1);
expect(
h.requests.filter((r) => r.filters.some((f) => f["#h"] && f.limit === 0)),
).toHaveLength(49);
const last = ids.at(-1);
const route = h.requests.find((request) =>
request.filters.some((filter) => filter["#h"]?.includes(last)),
);
expect(route).toBeDefined();
const event = finalizeEvent(
{
kind: 9,
content: "last task input",
created_at: Math.floor(Date.now() / 1000),
tags: [["h", last]],
},
h.key,
);
await route.socket.receive(["EVENT", route.id, event]);
await vi.waitFor(() =>
expect(received.map((item) => item.id)).toContain(event.id),
);
const next = [...ids.slice(0, -1), "new-task"];
traffic.update(next, next);
await vi.waitFor(
() => {
expect(established.has("new-task")).toBe(true);
expect(snapshot.routes.some((route) => route.channelId === last)).toBe(
false,
);
},
{ timeout: 30_000 },
);
const active = new Set();
for (const { kind, id } of h.frames) {
if (kind === "REQ") active.add(id);
if (kind === "CLOSE") active.delete(id);
expect(active.size).toBeLessThanOrEqual(1024);
}
} finally {
traffic?.dispose();
vi.unstubAllGlobals();
await h.close();
}
}, 60_000);

test("broker renewals retain establishment signals while channel status stays live", async () => {
const h = await harness();
let traffic;
try {
browserFetch(h.base);
const transport = await connectBrokerTransport(h.base);
const established = vi.fn();
const states = [];
traffic = transport.subscribe({
...callbacks,
established,
state: (state) => states.push(state),
});
const ids = Array.from({ length: 20 }, (_, i) => `c${i}`);
traffic.update(ids, ids);
await until(
() =>
established.mock.calls.filter(([value]) => Array.isArray(value))
.length === 1,
);
const membership = h.requests.find((request) =>
request.filters.some((filter) => filter.kinds.includes(44100)),
);
expect(membership).toBeDefined();
const before = states.length;
const notice = finalizeEvent(
{
kind: 44100,
content: "",
created_at: Math.floor(Date.now() / 1000),
tags: [
["p", getPublicKey(h.key)],
["h", ids[0]],
],
},
h.key,
);
await membership.socket.receive(["EVENT", membership.id, notice]);
await until(
() =>
established.mock.calls.filter(([value]) => Array.isArray(value))
.length === 2,
);
expect(
states
.slice(before)
.every((state) =>
state.routes
.filter((route) => route.channelId)
.every((route) => route.status === "live"),
),
).toBe(true);
expect(established.mock.calls.at(-1)).toEqual([ids.sort()]);
expect(h.sockets).toHaveLength(1);
} finally {
traffic?.dispose();
vi.unstubAllGlobals();
await h.close();
}
});

test("a sync close that overtakes its open keeps the late open from taking a host slot", async () => {
const h = await harness();
const post = (path, body) =>
Expand Down
Loading
Loading