Skip to content

Commit 000495d

Browse files
author
Larry
committed
fix(agents): retain naming inventory demand across reconnects
Signed-off-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz>
1 parent 6cc0dd0 commit 000495d

7 files changed

Lines changed: 149 additions & 6 deletions

File tree

‎src/bundled/agents/directory.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ it("scopes native names to the session community and follows edits and disposal"
4949
}),
5050
subscribe: () => () => {},
5151
refresh: async () => {},
52+
retain: () => () => {},
5253
},
5354
};
5455
const names = bindNames(source, {

‎src/bundled/agents/directory.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ export function createAgentDirectory(
1818
id: "agents",
1919
...(control ? { subscribe: control.subscribe } : {}),
2020
activate(source) {
21-
void source.agentLibrary.refresh();
2221
void control?.refresh();
22+
return source.agentLibrary.retain();
2323
},
2424
resolve(source, pubkey) {
2525
const key = pubkey.toLowerCase();

‎src/bundled/profiles/ProfilePanel.test.tsx‎

Lines changed: 125 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
// @vitest-environment jsdom
2-
import { act, cleanup, render, screen } from "@testing-library/react";
3-
import { afterEach, expect, it } from "vitest";
2+
import { act, cleanup, render, screen, waitFor } from "@testing-library/react";
3+
import { afterEach, expect, it, vi } from "vitest";
44
import { createRelaySession } from "../../features/relay/session";
55
import type { RelayData } from "../../features/relay/service";
66
import { bindNames } from "../../features/identity-names/service";
77
import { profileTarget } from "../../features/profiles/target";
88
import { ProfilePanel } from "./ProfilePanel";
99

10+
import { agentDirectory } from "../agents/directory";
11+
import type { LiveCallbacks } from "../../features/relay/live";
12+
import { keypair, profile } from "../../features/relay/testing";
13+
1014
const key = "a".repeat(64);
1115
afterEach(cleanup);
1216
it("updates a mounted profile from the shared name view without replacing its identity", async () => {
@@ -17,7 +21,15 @@ it("updates a mounted profile from the shared name view without replacing its id
1721
register() {},
1822
bind(source) {
1923
return bindNames(source, {
20-
snapshot: () => [{ id: "test", activate() {}, resolve: () => name }],
24+
snapshot: () => [
25+
{
26+
id: "test",
27+
activate() {
28+
return undefined;
29+
},
30+
resolve: () => name,
31+
},
32+
],
2133
subscribe(listener) {
2234
notify = listener;
2335
return () => {};
@@ -54,6 +66,116 @@ it("updates a mounted profile from the shared name view without replacing its id
5466
expect(screen.getByRole("heading", { name })).toBeTruthy();
5567
expect(owner.session.profiles.snapshot().size).toBe(0);
5668
} finally {
69+
vi.useRealTimers();
70+
cleanup();
71+
owner.dispose();
72+
}
73+
});
74+
75+
it("recovers provider-owned names in the same live session and releases demand when disabled", async () => {
76+
const person = keypair();
77+
const relayKey = keypair();
78+
const publicProfile = profile(person, { name: "Public Carl" });
79+
let live!: LiveCallbacks;
80+
let entries = [agentDirectory];
81+
let changed = () => {};
82+
const read = vi.fn(async () => ({
83+
definitions: [],
84+
identities: [{ pubkey: person.pubkey, name: "Configured Carl" }],
85+
}));
86+
const owner = createRelaySession(
87+
{
88+
viewer: person.pubkey,
89+
relayAuthor: relayKey.pubkey,
90+
scope: "wss://relay.example.test",
91+
query: async () => [publicProfile],
92+
media: () => undefined,
93+
readAgentLibrary: read,
94+
subscribe(callbacks) {
95+
live = callbacks;
96+
return { update() {}, retry() {}, dispose() {} };
97+
},
98+
},
99+
{
100+
identityNames: {
101+
register() {},
102+
bind(source) {
103+
return bindNames(source, {
104+
snapshot: () => entries,
105+
subscribe(listener) {
106+
changed = listener;
107+
return () => {};
108+
},
109+
});
110+
},
111+
},
112+
},
113+
);
114+
const snapshot = {
115+
status: "ready" as const,
116+
generation: 1,
117+
session: owner.session,
118+
};
119+
const relay: RelayData = {
120+
snapshot: () => snapshot,
121+
subscribe: () => () => {},
122+
retry() {},
123+
disconnect() {},
124+
clearCache: async () => {},
125+
};
126+
const reconnect = () => {
127+
live.state({ status: "retrying", routes: [] });
128+
live.state({ status: "connected", routes: [] });
129+
live.established();
130+
};
131+
try {
132+
render(
133+
<ProfilePanel
134+
relay={relay}
135+
target={profileTarget(person.pubkey) ?? ""}
136+
close={() => {}}
137+
/>,
138+
);
139+
await screen.findByRole("heading", { name: "Configured Carl" });
140+
await waitFor(() =>
141+
expect(owner.session.profiles.snapshot().get(person.pubkey)?.name).toBe(
142+
"Public Carl",
143+
),
144+
);
145+
act(() => live.state({ status: "connected", routes: [] }));
146+
act(() => live.state({ status: "retrying", routes: [] }));
147+
expect(screen.getByRole("heading", { name: "Public Carl" })).toBeTruthy();
148+
expect(owner.session.agentLibrary.snapshot().status).toBe("idle");
149+
expect(read).toHaveBeenCalledTimes(1);
150+
act(() => {
151+
live.state({ status: "connected", routes: [] });
152+
live.established();
153+
});
154+
await screen.findByRole("heading", { name: "Configured Carl" });
155+
expect(read).toHaveBeenCalledTimes(2);
156+
vi.useFakeTimers();
157+
act(() => {
158+
entries = [];
159+
changed();
160+
reconnect();
161+
});
162+
await act(async () => {
163+
await vi.advanceTimersByTimeAsync(0);
164+
});
165+
vi.useRealTimers();
166+
expect(screen.getByRole("heading", { name: "Public Carl" })).toBeTruthy();
167+
expect(read).toHaveBeenCalledTimes(2);
168+
act(() => {
169+
entries = [agentDirectory];
170+
changed();
171+
});
172+
await screen.findByRole("heading", { name: "Configured Carl" });
173+
expect(read).toHaveBeenCalledTimes(3);
174+
owner.dispose();
175+
act(reconnect);
176+
expect(read).toHaveBeenCalledTimes(3);
177+
} finally {
178+
vi.useRealTimers();
57179
cleanup();
58180
owner.dispose();
59181
}

‎src/features/agents/library.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export function createAgentLibrary(
2525
notify = (listener: () => void) => listener(),
2626
) {
2727
let closed = false;
28+
const demands = new Set<object>();
2829
let controller: AbortController | undefined;
2930
let pending: Promise<void> | undefined;
3031
let snapshot: AgentLibrarySnapshot = {
@@ -79,6 +80,14 @@ export function createAgentLibrary(
7980
queries: Object.freeze({
8081
snapshot: () => snapshot,
8182
refresh,
83+
retain() {
84+
const demand = {};
85+
demands.add(demand);
86+
void refresh();
87+
return () => {
88+
demands.delete(demand);
89+
};
90+
},
8291
subscribe(listener: () => void) {
8392
if (closed) return () => {};
8493
listeners.add(listener);
@@ -88,8 +97,12 @@ export function createAgentLibrary(
8897
},
8998
}),
9099
clear,
100+
reconnect() {
101+
if (demands.size) void refresh();
102+
},
91103
dispose() {
92104
closed = true;
105+
demands.clear();
93106
clear();
94107
listeners.clear();
95108
},

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ it("rebinds a live name view on plugin replacement and disable", async () => {
8787
manifest: { id: "test.agents", name: "Agents", apiVersion: 1 as const },
8888
source: "bundled" as const,
8989
enabled: true,
90+
reloadable: false,
9091
revision,
9192
previous: null,
9293
error: null,

‎src/features/identity-names/service.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ export type NameSource = {
1010
snapshot(): AgentLibrary & { status: string };
1111
subscribe(listener: () => void): () => void;
1212
refresh(): Promise<void>;
13+
retain(): () => void;
1314
};
1415
};
1516
export type NameProvider = {
1617
id: string;
1718
resolve(source: NameSource, pubkey: string): string | undefined;
18-
activate(source: NameSource): void;
19+
activate(source: NameSource): undefined | (() => void);
1920
subscribe?(listener: () => void): () => void;
2021
};
2122
export type IdentityName = Readonly<{
@@ -78,6 +79,7 @@ export function bindNames(
7879
let provider: NameProvider | undefined;
7980
let stopLibrary: (() => void) | undefined;
8081
let stopProvider: (() => void) | undefined;
82+
let deactivate: (() => void) | undefined;
8183
const listeners = new Set<() => void>();
8284
const emit = () => {
8385
if (closed) return;
@@ -90,6 +92,8 @@ export function bindNames(
9092
// Competing providers do not acquire last-writer authority.
9193
const next = entries.length === 1 ? entries[0] : undefined;
9294
if (provider === next) return;
95+
deactivate?.();
96+
deactivate = undefined;
9397
stopLibrary?.();
9498
stopLibrary = undefined;
9599
stopProvider?.();
@@ -98,7 +102,7 @@ export function bindNames(
98102
if (provider) {
99103
stopLibrary = source.agentLibrary.subscribe(emit);
100104
stopProvider = provider.subscribe?.(emit);
101-
provider.activate(source);
105+
deactivate = provider.activate(source);
102106
}
103107
emit();
104108
};
@@ -128,6 +132,7 @@ export function bindNames(
128132
dispose() {
129133
closed = true;
130134
provider = undefined;
135+
deactivate?.();
131136
stopLibrary?.();
132137
stopProvider?.();
133138
for (const stop of stops) stop();

‎src/features/relay/session.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,6 +1266,7 @@ export function createRelaySession(
12661266
const timer = setTimeout(() => {
12671267
timers.delete(timer);
12681268
if (!closed) {
1269+
agentLibrary.reconnect();
12691270
emoji.reconnect();
12701271
unread.reconnect();
12711272
for (const refresh of refreshers) void refresh();

0 commit comments

Comments
 (0)