Skip to content

Commit 1e1ad0b

Browse files
matt2eclaude
andauthored
fix(agents2): reconnect Retry to native agent-list reload (#921)
Signed-off-by: Matt Toohey <[email protected]> Co-authored-by: Claude Fable 5.1 <[email protected]>
1 parent ed62957 commit 1e1ad0b

10 files changed

Lines changed: 151 additions & 4 deletions

File tree

‎src/app/services.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ export function createServices() {
8282
localActivity,
8383
);
8484
ctx.effect(() =>
85-
localAgents.bind({ snapshot: agents.local, subscribe: agents.subscribe }),
85+
localAgents.bind({
86+
snapshot: agents.local,
87+
subscribe: agents.subscribe,
88+
refresh: () => agents.refresh(),
89+
}),
8690
);
8791
const notifications = new NotificationsService(
8892
ctx,

‎src/bundled/mentions/session-agents.test.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@ it.each(["picker", "completion"] as const)(
668668
listeners.delete(listener);
669669
};
670670
},
671+
refresh: async () => {},
671672
});
672673
const session = {
673674
...test.session,

‎src/features/agents/choices.test.ts‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect, it } from "vitest";
2+
import { createLocalAgents } from "./local";
23
import { localAgentsFixture } from "./local-testing";
34
import { createAgentLibrary } from "./library";
45
import { createAgentChoices, templateAgentChoices } from "./choices";
@@ -207,6 +208,76 @@ it("revokes failed local evidence without dropping ready legacy candidates, then
207208
library.dispose();
208209
});
209210

211+
it("reads the local list again from Retry and clears its error once a read succeeds", async () => {
212+
// The error shape Agents reports when `app_agent_list` fails at startup.
213+
const native = localAgentsFixture([], "error");
214+
native.set({
215+
status: "error",
216+
error: "Could not read saved agent identities",
217+
});
218+
const bound = createLocalAgents();
219+
bound.bind(native);
220+
const library = createAgentLibrary(async () => ({
221+
definitions: [],
222+
identities: [],
223+
}));
224+
const lifetime = new AbortController();
225+
const choices = createAgentChoices({
226+
scope,
227+
library: library.queries,
228+
native: bound.agents,
229+
archives: archiveFixture(),
230+
signal: lifetime.signal,
231+
});
232+
try {
233+
await library.queries.refresh();
234+
expect(choices.snapshot()).toMatchObject({
235+
status: "ready",
236+
error: "Could not read saved agent identities",
237+
identities: [],
238+
});
239+
expect(choices.snapshot().templates.complete).toBe(false);
240+
// The read problem is resolved before Retry; only Retry reads again.
241+
native.onRefresh(() => native.set({ status: "ready", agents: [agent] }));
242+
await choices.refresh();
243+
expect(native.reads()).toBe(1);
244+
const state = choices.snapshot();
245+
expect(state.error).toBeUndefined();
246+
expect(state.identities).toEqual([
247+
{
248+
pubkey: agent.pubkey,
249+
name: agent.name,
250+
managed: true,
251+
managedName: agent.name,
252+
},
253+
]);
254+
expect(state.templates).toMatchObject({
255+
status: "ready",
256+
complete: true,
257+
pending: false,
258+
});
259+
expect(state.templates.error).toBeUndefined();
260+
// Template refreshes read whichever source they use, and only that one.
261+
await choices.refresh("templates");
262+
expect(native.reads()).toBe(2);
263+
const unavailable = localAgentsFixture([], "unavailable");
264+
const legacyOnly = createAgentChoices({
265+
scope,
266+
library: library.queries,
267+
native: unavailable,
268+
archives: archiveFixture(),
269+
signal: lifetime.signal,
270+
});
271+
await legacyOnly.refresh("templates");
272+
expect(unavailable.reads()).toBe(0);
273+
await legacyOnly.refresh(false);
274+
expect(unavailable.reads()).toBe(1);
275+
} finally {
276+
lifetime.abort();
277+
library.dispose();
278+
}
279+
});
280+
210281
it("uses legacy roster choices only on hosts without local agents", async () => {
211282
const legacy = { pubkey: "bc".repeat(32), name: "Carl" };
212283
const library = createAgentLibrary(async () => ({

‎src/features/agents/choices.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ export function createAgentChoices({
181181
source === "templates"
182182
? !native || native.snapshot().status === "unavailable"
183183
: source;
184+
// Booleans only toggle the legacy inventory; local agents are always chosen.
185+
const usesLocal = (source: SelectionSource) =>
186+
!!native && (source !== "templates" || !usesLegacy(source));
184187
return Object.freeze({
185188
snapshot,
186189
subscribe(listener: () => void) {
@@ -213,6 +216,7 @@ export function createAgentChoices({
213216
if (signal.aborted) return;
214217
await Promise.all([
215218
usesLegacy(source) ? library.refresh() : undefined,
219+
usesLocal(source) ? native?.refresh() : undefined,
216220
// Archive reads stay lazy; templates always need fresh evidence.
217221
source === "templates" || archives?.snapshot().status !== "idle"
218222
? archives?.refresh()

‎src/features/agents/local-testing.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export function localAgentsFixture(
77
) {
88
let state: LocalAgentsSnapshot = { status, agents };
99
const listeners = new Set<() => void>();
10+
let reads = 0;
11+
let read: () => void | Promise<void> = () => {};
1012
return {
1113
snapshot: () => state,
1214
subscribe(listener: () => void) {
@@ -15,6 +17,15 @@ export function localAgentsFixture(
1517
listeners.delete(listener);
1618
};
1719
},
20+
/** Counts reload demand; `onRefresh` scripts what each read reports. */
21+
async refresh() {
22+
reads++;
23+
await read();
24+
},
25+
reads: () => reads,
26+
onRefresh(next: () => void | Promise<void>) {
27+
read = next;
28+
},
1829
/** An error lasts only as long as the update that reports it. */
1930
set(next: Partial<LocalAgentsSnapshot>) {
2031
state = {

‎src/features/agents/local.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export type LocalAgentsSnapshot = Readonly<{
1515
export type LocalAgents = {
1616
snapshot(): LocalAgentsSnapshot;
1717
subscribe(listener: () => void): () => void;
18+
/** Reads the list again, as Retry asks; the producer owns the read. */
19+
refresh(): Promise<void>;
1820
};
1921

2022
const unavailable: LocalAgentsSnapshot = Object.freeze({
@@ -32,6 +34,7 @@ export function createLocalAgents() {
3234
};
3335
const agents: LocalAgents = {
3436
snapshot: () => source?.snapshot() ?? unavailable,
37+
refresh: () => source?.refresh() ?? Promise.resolve(),
3538
subscribe(listener) {
3639
listeners.add(listener);
3740
return () => {

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,53 @@ it("keeps an agent created while identities are still loading", async () => {
13201320
expect(storage.getItem("buzz.agents.v1")).toContain(bot);
13211321
});
13221322

1323+
it("reads the list again on refresh and clears the error a failed read left", async () => {
1324+
const ctx = new Context();
1325+
ctx.provide("pluginStatus", {
1326+
isActive: () => true,
1327+
subscribe: () => () => {},
1328+
});
1329+
const identity = {
1330+
pubkey: bot,
1331+
relay: "wss://relay.example.test",
1332+
owner: viewer,
1333+
type: "example/echo",
1334+
name: "Echo",
1335+
deleted: false,
1336+
};
1337+
const native = fakeNative([identity]);
1338+
native.list.mockRejectedValueOnce(
1339+
new Error("Could not read saved agent identities"),
1340+
);
1341+
const service = new AgentsService(
1342+
ctx,
1343+
fakeCommunities(fakeRelay()).communities,
1344+
native,
1345+
memoryStorage(),
1346+
);
1347+
await vi.waitFor(() =>
1348+
expect(service.local()).toEqual({
1349+
status: "error",
1350+
agents: [],
1351+
error: "Could not read saved agent identities",
1352+
}),
1353+
);
1354+
await service.refresh();
1355+
expect(native.list).toHaveBeenCalledTimes(2);
1356+
expect(service.local()).toEqual({
1357+
status: "ready",
1358+
agents: [
1359+
{
1360+
pubkey: bot,
1361+
name: "Echo",
1362+
relayUrl: "wss://relay.example.test",
1363+
owner: viewer,
1364+
},
1365+
],
1366+
});
1367+
expect(service.snapshot().error).toBeUndefined();
1368+
});
1369+
13231370
it("fires a due timer once, then again an interval after it ran", async () => {
13241371
vi.useFakeTimers({ now: 1_000_000 });
13251372
const { service, run, storage } = await setup();

‎src/features/agents/service.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,16 +1547,18 @@ export class AgentsService extends Service implements Agents {
15471547
}
15481548

15491549
/** Reads the agent list from native. Every change is made there first and
1550-
* then read back here, so the list has one source. */
1551-
private async refresh() {
1550+
* then read back here, so the list has one source; Retry reads it again. */
1551+
async refresh() {
15521552
if (!this.native) return;
15531553
const turn = ++this.listed;
15541554
try {
15551555
const identities = await this.native.list();
15561556
// A newer list is on its way; this one may predate a change.
15571557
if (turn !== this.listed) return;
15581558
this.identities = identities;
1559-
this.state = Object.freeze({ ...this.state, status: "ready" });
1559+
// A read that succeeds ends the failure the last one reported.
1560+
const { error: _, ...state } = this.state;
1561+
this.state = Object.freeze({ ...state, status: "ready" });
15601562
// Settings whose agent is gone do nothing.
15611563
const live = new Set(
15621564
identities

‎src/features/direct-messages/NewMessage.test.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ function setup() {
160160
listeners.add(listener);
161161
return () => listeners.delete(listener);
162162
},
163+
refresh: async () => {},
163164
};
164165
Object.assign(session, {
165166
agentChoices: createAgentChoices({

‎src/features/identity-names/directory.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ it("scopes local agent names to the session community and follows edits and disp
3131
listeners.delete(listener);
3232
};
3333
},
34+
refresh: async () => {},
3435
});
3536
const source: NameSource = {
3637
relayUrl: "https://here.example",
@@ -165,6 +166,7 @@ it("recomputes collisions for local agent edits, community scope, and profile fa
165166
listeners.delete(listener);
166167
};
167168
},
169+
refresh: async () => {},
168170
});
169171
const names = bindNames(source, {
170172
snapshot: () => [provider],
@@ -233,6 +235,7 @@ it("includes local-only identities but ignores other-community and unready local
233235
const provider = createAgentDirectory({
234236
snapshot: () => native,
235237
subscribe: () => () => {},
238+
refresh: async () => {},
236239
});
237240
for (const key of [a, b])
238241
expect(provider.scope(source)(key)?.name).toBe(

0 commit comments

Comments
 (0)