Skip to content

Commit 43f3c3b

Browse files
Honeyclaude
andcommitted
fix(agents2): retry a failed unselected community connection
A first connect that fails leaves the session in error, and only the selected community has a Retry the viewer can press. Agents2 now tries an unselected community's failed connection again every 30 seconds, so its agents recover without the viewer opening it. Also covers Communities.open directly. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Honey <8e307ae0076a4dab6b94b036ea3edc7e08f823a625269c1e6919e881a048b4d2@buzz.block.builderlab.xyz>
1 parent 2e36587 commit 43f3c3b

4 files changed

Lines changed: 70 additions & 3 deletions

File tree

‎docs/communities.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,10 @@ community origin and viewer; channel IDs alone are not sufficient keys.
114114
## Session lifetime
115115

116116
Selecting a saved membership lazily acquires its session. Sessions survive page
117-
navigation and switching; startup opens only the selected one, plus each joined
118-
community where the viewer has an Agents2 agent, which `open` connects without
119-
selecting so its agents keep running. Opened communities
117+
navigation and switching; startup opens only the selected one. Once its agent
118+
list loads, Agents2 also opens each joined community where the viewer has an
119+
agent, through `open`, which connects without selecting so those agents keep
120+
running, and retries one whose connection failed while it is not selected. Opened communities
120121
retain their existing per-session query/cache budgets. Arbitrary destinations mean
121122
there is **no longer a two-session maximum**: retained session count grows with
122123
communities opened until app disposal. This slice does not add background eviction

‎src/features/agents2/service.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ function fakeRelay(origin = "https://relay.example.test") {
9393
changes.add(listener);
9494
return () => changes.delete(listener);
9595
},
96+
retry: vi.fn(),
9697
} as unknown as RelayData,
9798
/** Drops the connection, or restores it with a new session. */
9899
connect(connected: boolean) {
@@ -101,6 +102,11 @@ function fakeRelay(origin = "https://relay.example.test") {
101102
: { status: "disconnected", generation: 2, session };
102103
for (const listener of changes) listener();
103104
},
105+
/** Fails the connection, as a first connect that timed out does. */
106+
fail() {
107+
snapshot = { status: "error", generation: 4, session, error: "down" };
108+
for (const listener of changes) listener();
109+
},
104110
/** Replaces the session, as after the transport was lost. */
105111
reconnecting() {
106112
snapshot = { status: "connecting", generation: 3, session };
@@ -251,6 +257,8 @@ async function setup({
251257
run,
252258
emit: fake.emit,
253259
connect: fake.connect,
260+
fail: fake.fail,
261+
relay: fake.relay,
254262
reconnecting: fake.reconnecting,
255263
recover: fake.recover,
256264
archives: fake.archives,
@@ -532,6 +540,22 @@ it("keeps an agent running while its community reconnects, and stops it on disco
532540
expect(service.relay(bot)).toBeUndefined();
533541
});
534542

543+
it("tries a failed unselected community again every so often, and leaves the selected one's Retry to the viewer", async () => {
544+
vi.useFakeTimers();
545+
const { service, relay, fail, second, select } = await setup();
546+
await service.create({ type: "example/echo", name: "Echo" });
547+
fail();
548+
await vi.advanceTimersByTimeAsync(10_000);
549+
expect(relay.retry).not.toHaveBeenCalled();
550+
select(second.origin);
551+
await vi.advanceTimersByTimeAsync(5_000);
552+
expect(relay.retry).toHaveBeenCalledTimes(1);
553+
await vi.advanceTimersByTimeAsync(20_000);
554+
expect(relay.retry).toHaveBeenCalledTimes(1);
555+
await vi.advanceTimersByTimeAsync(10_000);
556+
expect(relay.retry).toHaveBeenCalledTimes(2);
557+
});
558+
535559
it("is not woken by reactions, deletions or DMs", async () => {
536560
const { service, run, emit } = await setup();
537561
await service.create({ type: "example/echo", name: "Echo" });

‎src/features/agents2/service.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ const RATE_LIMIT = 60;
194194
const RATE_WINDOW_MS = 60_000;
195195
const TIMEOUT_LIMIT_MS = 30 * 60_000;
196196
const TIMER_TICK_MS = 5_000;
197+
/** How often a failed connection to an unselected community is tried again. */
198+
const RETRY_MS = 30_000;
197199
const EMPTY = Object.freeze({});
198200
const blank = (pubkey: string): AgentRecord => ({
199201
pubkey,
@@ -235,6 +237,8 @@ type Bound = {
235237
relay: RelayData;
236238
stop: () => void;
237239
binding?: Binding | undefined;
240+
/** When its failed connection was last tried again. */
241+
retried?: number;
238242
};
239243
/** The origin a relay URL or community ID names, if it is a valid one. */
240244
const originOf = (value: string) => {
@@ -323,6 +327,7 @@ export class Agents2Service extends Service implements Agents2 {
323327
// Wakes each runner, which starts any timer that is due.
324328
const tick = setInterval(() => {
325329
for (const runner of this.runners.values()) void this.drain(runner);
330+
this.reconnect();
326331
}, TIMER_TICK_MS);
327332
return () => {
328333
clearInterval(tick);
@@ -640,6 +645,21 @@ export class Agents2Service extends Service implements Agents2 {
640645
this.bind(origin);
641646
}
642647
}
648+
/** Tries each unselected community whose connection failed again, since
649+
* nobody is there to press its Retry; its agents wait on it. */
650+
private reconnect() {
651+
const selected = originOf(this.communities.snapshot().selected ?? "");
652+
const now = Date.now();
653+
for (const [origin, entry] of this.bound)
654+
if (
655+
origin !== selected &&
656+
entry.relay.snapshot().status === "error" &&
657+
now - (entry.retried ?? 0) >= RETRY_MS
658+
) {
659+
entry.retried = now;
660+
entry.relay.retry();
661+
}
662+
}
643663
private unbind(origin: string) {
644664
const entry = this.bound.get(origin);
645665
this.bound.delete(origin);

‎src/features/communities/service.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,28 @@ it("restores only the selected membership on launch", async () => {
133133
]);
134134
});
135135

136+
it("opens a joined community without selecting it, and never one that is not joined", async () => {
137+
const client = setup({
138+
profile: { name: "Local", picture: "" },
139+
memberships: [
140+
{ id: "primary", name: "Primary" },
141+
{ id: "secondary", name: "Secondary" },
142+
],
143+
selected: "secondary",
144+
});
145+
await flush();
146+
await flush();
147+
const primary = client.open("primary");
148+
await flush();
149+
expect(client.snapshot().selected).toBe("secondary");
150+
expect(primary?.snapshot().status).toBe("ready");
151+
expect(client.open("primary")).toBe(primary);
152+
expect(client.open("https://unjoined.example.test")).toBeUndefined();
153+
expect(requests.filter((url) => url.endsWith("/session"))).toEqual([
154+
"/api/relay/secondary/session",
155+
"/api/relay/primary/session",
156+
]);
157+
});
136158
it("a failing community does not replace its healthy sibling or the local profile", async () => {
137159
const client = setup();
138160
await flush();

0 commit comments

Comments
 (0)