Skip to content

Commit b4db0f3

Browse files
authored
Keep a hidden Bot's app grants on the Plugins screens (#658)
Hiding a Bot from your own roster took it off the Plugins screens: its row went from By Bot and from each tool's switches, and its own page said there was no such Bot, while its grants stayed in force with nothing able to take them away. The screens now follow the rule the Handoff panel already does: a hidden Bot is drawn when it holds one of the grants the screen is about, marked "Hidden from your roster".
1 parent 2afaadb commit b4db0f3

7 files changed

Lines changed: 203 additions & 14 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ or an interrupted browser session. Managed browsing now uses full Chromium in he
4646
mode. Local API deployments can opt into installed Chrome with dedicated per-Bot profiles, the same
4747
in-app viewer, a loopback-only computer endpoint, and host shell execution disabled.
4848

49+
### A hidden Bot's app grants stay on the Plugins screens
50+
51+
Hiding a Bot from your own roster took it off the Plugins screens too: its row went from By Bot and
52+
from each tool's switches, the counts could read "3 of 2 Bots", and its own page said there was no
53+
such Bot. Its grants stayed in force, and nothing on any screen could take them away. The Plugins
54+
screens now follow the rule the Handoff panel already does: a hidden Bot is shown when it holds one
55+
of the grants the screen is about, marked "Hidden from your roster", and its own page draws its
56+
grants. Nothing on the server changed.
57+
4958
### A wiped or restarted shared computer no longer leaves refs pointing at the dead page
5059

5160
Snapshots are ordered on the run of the browser that took them as well as the generation, so a

‎app/src/lib/agents/grant-roster.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Which Bots a connector screen draws a row for, when the rows are one app's or one tool's grants.
3+
*
4+
* THE RULE `handoffRoster` STATES FOR THE HANDOFF PANEL, AFTER THE SAME MISTAKE. The roster is
5+
* `GET /api/agents`, which drops every Bot the signed-in person has hidden. A tool's `grantedTo` is
6+
* not filtered by hiding at all, because hiding is a per-person display preference, one row per user
7+
* in `agent_preferences`, and not a fact about the Bot.
8+
*
9+
* So an administrator who tidied a Bot off their own roster stopped being shown it on the Plugins
10+
* screens. Its row went from By Bot and from every tool's switches, the counts above read "3 of 2
11+
* Bots", and its own page said there was no such Bot. The grants were still in force, and nothing on
12+
* any screen could take them away.
13+
*
14+
* - A Bot on your roster is drawn as it always was.
15+
* - A Bot you have hidden is drawn only if it holds one of the grants the screen is about. Hiding
16+
* is about clutter and a grant screen has no business undoing it, but a grant nobody can reach is
17+
* the one thing a grant screen must not have.
18+
*/
19+
export function grantRoster<T extends { id: string }>(input: {
20+
/** `GET /api/agents`: everybody this person can see and has not hidden. */
21+
roster: readonly T[];
22+
/** `GET /api/agents?hidden=true`: the ones this person has hidden from that roster. */
23+
hidden: readonly T[];
24+
/** The Bot ids holding any grant this screen shows, exactly as the server reports them. */
25+
holders: Iterable<string>;
26+
}): T[] {
27+
const holding = new Set(input.holders);
28+
/*
29+
* The two lists are mutually exclusive per person, since `list` filters on `hiddenAt` being null
30+
* or not, so this only stops a duplicate row and a duplicate React key if that ever changes.
31+
*/
32+
const shown = new Set(input.roster.map((bot) => bot.id));
33+
return [
34+
...input.roster,
35+
...input.hidden.filter((bot) => holding.has(bot.id) && !shown.has(bot.id)),
36+
];
37+
}

‎app/src/routes/_authed/admin/plugins/$key.tsx‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import {
3939
} from "@/components/ui/item";
4040
import { Separator } from "@/components/ui/separator";
4141
import { Switch } from "@/components/ui/switch";
42-
import { useBotNames } from "@/lib/agents/bot-names";
42+
import { grantRoster } from "@/lib/agents/grant-roster";
4343
import { agentListQueryOptions } from "@/lib/agents/queries";
4444
import { storeMcpToken } from "@/lib/credentials/mutations";
4545
import {
@@ -180,6 +180,8 @@ function RouteComponent() {
180180
*/
181181
const connections = useQuery(connectionsQueryOptions());
182182
const { data: agents } = useQuery(agentListQueryOptions());
183+
/* The ones this person has hidden, so a grant one of them holds is still on this page. */
184+
const { data: hiddenAgents } = useQuery(agentListQueryOptions(true));
183185
/*
184186
* The row itself rather than whether there is one, because the brokered row below wants what the
185187
* last re-check found and that is written on this same row. Asking a second time for it would be
@@ -209,7 +211,6 @@ function RouteComponent() {
209211
* `false` the brokered row was getting.
210212
*/
211213
const connectionsUnreadable = connections.error !== null;
212-
const nameFor = useBotNames();
213214

214215
const [error, setError] = useState<string | null>(null);
215216
const [dialog, setDialog] = useState<OpenDialog>(null);
@@ -280,9 +281,22 @@ function RouteComponent() {
280281
});
281282
const entry = plugins.data?.catalogue.find((item) => item.key === key);
282283
const server = plugins.data?.servers.find((item) => item.id === key);
283-
const bots = (agents ?? []).map((agent: { id: string }) => ({
284+
/*
285+
* Everybody on the roster, and anybody hidden from it who holds one of this app's grants,
286+
* withdrawn ones included: see `grantRoster`. Named from the roster rows themselves, because the
287+
* id-to-name lookup other admin screens use is built from the visible roster alone.
288+
*/
289+
const bots = grantRoster({
290+
roster: agents ?? [],
291+
hidden: hiddenAgents ?? [],
292+
holders: [
293+
...(server?.tools ?? []).flatMap((tool) => tool.grantedTo),
294+
...(server?.withdrawn ?? []).flatMap((held) => held.grantedTo),
295+
],
296+
}).map((agent) => ({
284297
id: agent.id,
285-
name: nameFor(agent.id),
298+
name: agent.name,
299+
hidden: agent.hidden,
286300
}));
287301

288302
const auth = connectionKindFor(server, entry?.auth);
@@ -946,7 +960,9 @@ function RouteComponent() {
946960
<ItemContent>
947961
<ItemTitle>{bot.name}</ItemTitle>
948962
<ItemDescription>
949-
Every action this app offers, switched one at a time.
963+
{bot.hidden
964+
? "Hidden from your roster. Every action this app offers, switched one at a time."
965+
: "Every action this app offers, switched one at a time."}
950966
</ItemDescription>
951967
</ItemContent>
952968
<ItemActions>

‎app/src/routes/_authed/admin/plugins/$key_.bots.$agentId.tsx‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ function RouteComponent() {
7777
const queryClient = useQueryClient();
7878
const plugins = useQuery(pluginsPageQueryOptions());
7979
const agents = useQuery(agentListQueryOptions());
80+
/*
81+
* The ones this person has hidden. A hidden Bot is still a Bot, and still holds whatever it was
82+
* granted; the connector page links here for it (see `grantRoster`), and this page must not then
83+
* say there is no such Bot.
84+
*/
85+
const hiddenAgents = useQuery(agentListQueryOptions(true));
8086
const nameFor = useBotNames();
8187
const [error, setError] = useState<string | null>(null);
8288
/** What the list is narrowed to, over action names. Never over what is switched on. */
@@ -103,7 +109,9 @@ function RouteComponent() {
103109
plugins.data?.catalogue.find((item) => item.key === key)?.title ??
104110
server?.title ??
105111
key;
106-
const bot = agents.data?.find((one) => one.id === agentId);
112+
const bot =
113+
agents.data?.find((one) => one.id === agentId) ??
114+
hiddenAgents.data?.find((one) => one.id === agentId);
107115

108116
const back = {
109117
label: appTitle,
@@ -140,7 +148,7 @@ function RouteComponent() {
140148
};
141149

142150
/* Nothing rather than a placeholder, so no sentence asserts anything while a fetch is open. */
143-
if (plugins.isPending || agents.isPending) {
151+
if (plugins.isPending || agents.isPending || hiddenAgents.isPending) {
144152
return <PageShell title="Bot">{null}</PageShell>;
145153
}
146154

@@ -195,7 +203,8 @@ function RouteComponent() {
195203
* exists for the same mistake on the two screens that shipped it. With no roster the grants
196204
* below are still the plugins query's own answer and still true; the name falls back to the id.
197205
*/
198-
if (agents.data && !bot) {
206+
// Both rosters, because a Bot absent from the visible one may simply be hidden.
207+
if (agents.data && hiddenAgents.data && !bot) {
199208
return (
200209
<PageShell
201210
backButton={back}
@@ -209,7 +218,8 @@ function RouteComponent() {
209218
);
210219
}
211220

212-
const botName = nameFor(agentId);
221+
// From the row itself where there is one: the id-to-name lookup is built from the visible roster.
222+
const botName = bot?.name ?? nameFor(agentId);
213223
const held = (tool: PluginTool) => tool.grantedTo.includes(agentId);
214224

215225
/*

‎app/src/routes/_authed/admin/plugins/$key_.tools.$tool.tsx‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
} from "@/components/ui/item";
1717
import { Separator } from "@/components/ui/separator";
1818
import { Switch } from "@/components/ui/switch";
19-
import { useBotNames } from "@/lib/agents/bot-names";
19+
import { grantRoster } from "@/lib/agents/grant-roster";
2020
import { agentListQueryOptions } from "@/lib/agents/queries";
2121
import { setPluginGrantMutationOptions } from "@/lib/plugins/mutations";
2222
import { pluginsPageQueryOptions } from "@/lib/plugins/queries";
@@ -44,6 +44,8 @@ function RouteComponent() {
4444
const queryClient = useQueryClient();
4545
const plugins = useQuery(pluginsPageQueryOptions());
4646
const { data: agents } = useQuery(agentListQueryOptions());
47+
/* The ones this person has hidden, so a grant one of them holds still has its switch here. */
48+
const { data: hiddenAgents } = useQuery(agentListQueryOptions(true));
4749
/*
4850
* Whether a call from this Bot could be authenticated at all.
4951
*
@@ -55,8 +57,9 @@ function RouteComponent() {
5557
const sharedCallback = plugins.data?.botsMayCallBack === true;
5658
const canCallBack = (bot: { id: string }) =>
5759
sharedCallback ||
58-
agents?.find((one) => one.id === bot.id)?.hasCallbackToken === true;
59-
const nameFor = useBotNames();
60+
[...(agents ?? []), ...(hiddenAgents ?? [])].find(
61+
(one) => one.id === bot.id,
62+
)?.hasCallbackToken === true;
6063
const [error, setError] = useState<string | null>(null);
6164

6265
const setGrant = useMutation({
@@ -100,9 +103,19 @@ function RouteComponent() {
100103
);
101104
}
102105

103-
const bots = (agents ?? []).map((agent: { id: string }) => ({
106+
/*
107+
* Everybody on the roster, and anybody hidden from it who holds this tool: see `grantRoster`.
108+
* Named from the roster rows themselves, because the id-to-name lookup other admin screens use is
109+
* built from the visible roster alone.
110+
*/
111+
const bots = grantRoster({
112+
roster: agents ?? [],
113+
hidden: hiddenAgents ?? [],
114+
holders: tool.grantedTo,
115+
}).map((agent) => ({
104116
id: agent.id,
105-
name: nameFor(agent.id),
117+
name: agent.name,
118+
hidden: agent.hidden,
106119
}));
107120

108121
return (
@@ -173,6 +186,7 @@ function RouteComponent() {
173186
<ItemContent>
174187
<ItemTitle>{bot.name}</ItemTitle>
175188
<ItemDescription>
189+
{bot.hidden ? "Hidden from your roster. " : ""}
176190
{held
177191
? canCallBack(bot)
178192
? "May call this tool. Every call is still checked against the boundaries and written to the audit trail."

‎app/tests/bot-app-grants-screen.test.tsx‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,49 @@ test("a failed REFETCH keeps the grant list it already had, not the error", asyn
318318
).toBeNull();
319319
});
320320

321+
/*
322+
* Hidden is a per-person roster preference, and a grant is a deployment-wide fact. The connector
323+
* page now links a hidden Bot that holds grants here, so this page has to find it in the hidden
324+
* roster rather than say there is no such Bot, which is what it said while the grants stayed on.
325+
*/
326+
test("a hidden Bot's own page draws its grants instead of saying there is no such Bot", async () => {
327+
global.fetch = Object.assign(
328+
async (path: Parameters<typeof fetch>[0]) => {
329+
const url = String(path);
330+
if (url.startsWith("/api/agents")) {
331+
const hiddenBot = {
332+
id: BOT_ID,
333+
name: "Tucked Bot",
334+
hidden: true,
335+
hasCallbackToken: false,
336+
};
337+
return Response.json({
338+
agents: url.includes("hidden=true") ? [hiddenBot] : [],
339+
});
340+
}
341+
return new Response(null, { status: 500 });
342+
},
343+
{ preconnect: originalFetch.preconnect },
344+
);
345+
const queryClient = stalePluginsClient(
346+
pluginsPage({
347+
servers: [
348+
server({
349+
id: APP_KEY,
350+
tools: [tool({ name: "list_channels", grantedTo: [BOT_ID] })],
351+
}),
352+
],
353+
}),
354+
);
355+
356+
const view = renderScreen(queryClient);
357+
await waitForFailedRefetch(queryClient);
358+
359+
const held = await view.findByLabelText("Let Tucked Bot call list_channels");
360+
expect(held.getAttribute("aria-checked")).toBe("true");
361+
expect(view.queryByText("Not a Bot")).toBeNull();
362+
});
363+
321364
/**
322365
* The actions end of one action's row: its switch, and whatever is drawn beside it.
323366
*

‎app/tests/grant-roster.test.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { grantRoster } from "../src/lib/agents/grant-roster";
3+
4+
/**
5+
* Which Bots a connector screen draws a row for.
6+
*
7+
* The grants the server reports are not filtered by anybody's roster preferences, and the roster the
8+
* browser holds is. Joining one against the other dropped live grants off the only screens that can
9+
* take them away. `handoff-roster.test.ts` is the same rule for the Handoff panel.
10+
*/
11+
12+
const bot = (id: string, hidden = false) => ({ id, hidden, name: id });
13+
14+
describe("who a connector screen draws a row for", () => {
15+
test("draws everybody on the roster, holding a grant or not", () => {
16+
const rows = grantRoster({
17+
roster: [bot("a"), bot("b")],
18+
hidden: [],
19+
holders: ["a"],
20+
});
21+
22+
expect(rows.map((row) => row.id)).toEqual(["a", "b"]);
23+
});
24+
25+
/*
26+
* The bug. Hiding is one row per person in `agent_preferences`, and a grant is a deployment-wide
27+
* fact an administrator set. Hide the grantee and its switches disappeared while the grant stayed
28+
* in force.
29+
*/
30+
test("still draws a hidden Bot that holds a grant, after the roster, so it can be revoked", () => {
31+
const rows = grantRoster({
32+
roster: [bot("a")],
33+
hidden: [bot("c", true), bot("d", true)],
34+
holders: ["c"],
35+
});
36+
37+
expect(rows.map((row) => row.id)).toEqual(["a", "c"]);
38+
expect(rows[1]?.hidden).toBe(true);
39+
});
40+
41+
test("leaves a hidden Bot that holds nothing hidden", () => {
42+
const rows = grantRoster({
43+
roster: [bot("a")],
44+
hidden: [bot("d", true)],
45+
holders: ["a"],
46+
});
47+
48+
expect(rows.map((row) => row.id)).toEqual(["a"]);
49+
});
50+
51+
test("never draws a Bot twice", () => {
52+
const rows = grantRoster({
53+
roster: [bot("a")],
54+
hidden: [bot("a", true)],
55+
holders: ["a", "a"],
56+
});
57+
58+
expect(rows.map((row) => row.id)).toEqual(["a"]);
59+
});
60+
});

0 commit comments

Comments
 (0)