Skip to content

Commit 27d581e

Browse files
fix: Stop Builderlab agent setup after settings card ownership ends (#639)
Signed-off-by: Jarrod Sibbison <[email protected]>
1 parent a07fa8f commit 27d581e

4 files changed

Lines changed: 70 additions & 7 deletions

File tree

‎src/bundled/builderlab/agents/RemoteAgents.test.tsx‎

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @vitest-environment jsdom
22
import "@testing-library/jest-dom/vitest";
3-
import { act, cleanup, render, screen } from "@testing-library/react";
3+
import { act, cleanup, render, screen, waitFor } from "@testing-library/react";
44
import userEvent from "@testing-library/user-event";
55
import { afterEach, expect, it, vi } from "vitest";
66
import type { HostRequest, HostResponse } from "../../../features/host/service";
@@ -199,3 +199,59 @@ it("does not attest or show a held registration after sign-out", async () => {
199199
screen.queryByRole("region", { name: "Remote agents" }),
200200
).not.toBeInTheDocument();
201201
});
202+
203+
it.each(["registration", "signing"])(
204+
"does not continue creation when card ownership is lost during %s before effect cleanup",
205+
async (stage) => {
206+
const h = await fixture();
207+
let active = true;
208+
render(<RemoteAgents {...h} active={() => active} />);
209+
await screen.findByText("Helper · Active");
210+
const registered = {
211+
status: 200,
212+
headers: {},
213+
body: JSON.stringify({
214+
status: 1,
215+
agent_id: "two",
216+
agent_pubkey: row.agent_pubkey,
217+
}),
218+
};
219+
const registration = deferred<HostResponse>();
220+
const signing = deferred<Awaited<ReturnType<typeof h.authorize>>>();
221+
const proof = ["auth", "cd".repeat(32), "", "ef".repeat(64)] as const;
222+
if (stage === "registration")
223+
h.request.mockReturnValueOnce(registration.promise);
224+
else {
225+
h.request.mockResolvedValueOnce(registered);
226+
h.authorize.mockReturnValueOnce(signing.promise);
227+
}
228+
try {
229+
const user = userEvent.setup();
230+
await user.type(
231+
screen.getByRole("textbox", { name: "Agent name" }),
232+
"Another",
233+
);
234+
await user.click(screen.getByRole("button", { name: "Create agent" }));
235+
await waitFor(() => {
236+
expect(h.request).toHaveBeenCalledTimes(2);
237+
expect(h.authorize).toHaveBeenCalledTimes(stage === "signing" ? 1 : 0);
238+
});
239+
// Revoke ownership without unmounting or signing out: the signal is still live.
240+
active = false;
241+
await act(async () => {
242+
registration.resolve(registered);
243+
signing.resolve(proof);
244+
});
245+
expect(h.request).toHaveBeenCalledTimes(2);
246+
expect(h.authorize).toHaveBeenCalledTimes(stage === "signing" ? 1 : 0);
247+
expect(screen.queryAllByText("Another · Unattested")).toHaveLength(
248+
stage === "signing" ? 1 : 0,
249+
);
250+
expect(screen.queryByText("Another · Active")).not.toBeInTheDocument();
251+
expect(screen.queryByRole("alert")).not.toBeInTheDocument();
252+
} finally {
253+
registration.resolve(registered);
254+
signing.resolve(proof);
255+
}
256+
},
257+
);

‎src/bundled/builderlab/agents/RemoteAgents.tsx‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ function AgentList({
6666
const change = async (agent?: RemoteAgent) => {
6767
const signal = operation.current?.signal;
6868
if (!signal || signal.aborted || busy || loading || !active()) return;
69+
const current = () => !signal.aborted && active();
6970
setBusy(true);
7071
setError(undefined);
7172
const show = (row: RemoteAgent) =>
@@ -75,20 +76,20 @@ function AgentList({
7576
]);
7677
try {
7778
const registered = agent ?? (await client.register(name, signal));
78-
if (signal.aborted) return;
79+
if (!current()) return;
7980
show(registered);
8081
if (!agent) setName("");
81-
const ready = await client.attest(registered, signal);
82-
if (!signal.aborted) show(ready);
82+
const ready = await client.attest(registered, signal, active);
83+
if (current()) show(ready);
8384
} catch (reason) {
84-
if (!signal.aborted)
85+
if (current())
8586
setError(
8687
reason instanceof Error
8788
? reason.message
8889
: "Could not create the agent. Retry the same name.",
8990
);
9091
} finally {
91-
if (!signal.aborted) setBusy(false);
92+
if (current()) setBusy(false);
9293
}
9394
};
9495
return (

‎src/bundled/builderlab/agents/client.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ it("registers with a saved UUID and attests through the existing owner signer",
184184
agent_name: "Helper",
185185
idempotency_key: expect.stringMatching(/^[0-9a-f-]{36}$/),
186186
});
187-
expect(await h.client.attest(agent, h.signal)).toMatchObject({
187+
expect(await h.client.attest(agent, h.signal, () => true)).toMatchObject({
188188
status: "Active",
189189
});
190190
expect(h.host.prepareRemoteAgentAuthorization).toHaveBeenCalledWith(
@@ -313,6 +313,7 @@ it.each(["sign-out", "cancel"])(
313313
status: "Unattested",
314314
},
315315
controller.signal,
316+
() => true,
316317
);
317318
if (action === "cancel") controller.abort();
318319
else h.session.signOut();

‎src/bundled/builderlab/agents/client.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,11 @@ export function createAgentClient(host: Host, session: OAuthSession) {
164164
async attest(
165165
agent: RemoteAgent,
166166
signal: AbortSignal,
167+
active: () => boolean,
167168
): Promise<RemoteAgent> {
168169
signal.throwIfAborted();
170+
if (!active())
171+
throw new DOMException("Builderlab card is inactive.", "AbortError");
169172
const credential = session.credential();
170173
if (!host.prepareRemoteAgentAuthorization)
171174
throw new Error(
@@ -176,6 +179,8 @@ export function createAgentClient(host: Host, session: OAuthSession) {
176179
signal,
177180
);
178181
check(credential, signal);
182+
if (!active())
183+
throw new DOMException("Builderlab card is inactive.", "AbortError");
179184
const result = await request(
180185
"attest-agent",
181186
{

0 commit comments

Comments
 (0)