diff --git a/docs/4-implementation/web-ui/swarm-ui.md b/docs/4-implementation/web-ui/swarm-ui.md index b07435163..955f7fca1 100644 --- a/docs/4-implementation/web-ui/swarm-ui.md +++ b/docs/4-implementation/web-ui/swarm-ui.md @@ -71,6 +71,9 @@ asks from inside a handler with the `useConfirm` hook, with no dialog state of i | `/messages` | redirects to `/swarm`, where the messages sit: a folded **message log** card with every message between agents, and to them from the operator, reminders and compaction, with a history window and a filter on sender or recipient; a folded **broadcast** card sends one message to more than one agent at once, and a folded **schedules** card holds the scheduled prompts: add, edit, pause, resume and remove | Everything it shows comes from [`swarm-controller`](../../../swarm-controller/README.md). +An agent, subagent or group name in an address that isn't valid percent-encoding +stays as typed, so the page opens, showing a disconnected terminal for that +name, instead of staying blank. Pages that refresh on a timer share one polling rule: the page sends no new request while the last one is still running, so an older answer never overtakes a slow one, and a failed refresh keeps the rows already on screen under an diff --git a/frontend/packages/swarm-ui/src/pages/agents/AgentGroupPage.tsx b/frontend/packages/swarm-ui/src/pages/agents/AgentGroupPage.tsx index 55e73c527..f97ecea1c 100644 --- a/frontend/packages/swarm-ui/src/pages/agents/AgentGroupPage.tsx +++ b/frontend/packages/swarm-ui/src/pages/agents/AgentGroupPage.tsx @@ -32,6 +32,7 @@ import { import { fetchJson, usePolled } from "../../ui/polled/usePolled.js"; import { useTermGroups } from "../../agent/terminal/useTermGroups.js"; import { useDynamicTabs } from "../../shell/useDynamicTabs.js"; +import { safeDecode } from "../../shell/routes.js"; import { Button } from "../../ui/button/Button.js"; import { PaneDropTarget } from "../../ui/pane-tree/PaneDropTarget.js"; import { PaneTree } from "../../ui/pane-tree/PaneTree.js"; @@ -40,7 +41,7 @@ import "./AgentGroupPage.css"; export function AgentGroupPage({ params, }: RouteComponentProps<{ id: string }>) { - const id = decodeURIComponent(params.id); + const id = safeDecode(params.id); const href = groupHref(id); const groups = useTermGroups(); const group = groups.find(id); diff --git a/frontend/packages/swarm-ui/src/pages/agents/AgentTerminalPage.tsx b/frontend/packages/swarm-ui/src/pages/agents/AgentTerminalPage.tsx index 1cf61e1bd..fe4a93ff8 100644 --- a/frontend/packages/swarm-ui/src/pages/agents/AgentTerminalPage.tsx +++ b/frontend/packages/swarm-ui/src/pages/agents/AgentTerminalPage.tsx @@ -28,6 +28,7 @@ import { } from "../../agent/terminal/split-layout.js"; import { useTermGroups } from "../../agent/terminal/useTermGroups.js"; import { useDynamicTabs } from "../../shell/useDynamicTabs.js"; +import { safeDecode } from "../../shell/routes.js"; import { PaneDropTarget } from "../../ui/pane-tree/PaneDropTarget.js"; import "./AgentTerminalPage.css"; @@ -38,11 +39,9 @@ export function AgentTerminalPage({ params, }: RouteComponentProps<{ name: string; subagent?: string }>) { const { open } = useDynamicTabs(); - const name = decodeURIComponent(params.name); + const name = safeDecode(params.name); const subagent = - params.subagent === undefined - ? undefined - : decodeURIComponent(params.subagent); + params.subagent === undefined ? undefined : safeDecode(params.subagent); const { page, label } = termPaths(name, subagent); // A subagent has no icon of its own: its tab wears its agent's. useAgentFavicon(name); diff --git a/frontend/packages/swarm-ui/src/pages/notifications/FlowEditorPage.tsx b/frontend/packages/swarm-ui/src/pages/notifications/FlowEditorPage.tsx index e38c49d28..1f31af88d 100644 --- a/frontend/packages/swarm-ui/src/pages/notifications/FlowEditorPage.tsx +++ b/frontend/packages/swarm-ui/src/pages/notifications/FlowEditorPage.tsx @@ -13,6 +13,7 @@ // on top of the latest. import { useCallback, useEffect, useState } from "preact/hooks"; import { usePageTitle } from "../../shell/page-title.js"; +import { safeDecode } from "../../shell/routes.js"; import { Link, useLocation, @@ -71,7 +72,7 @@ export function FlowEditorPage({ // The page for a flow not saved yet, which has no flow id. isNew?: boolean; }) { - const flowId = decodeURIComponent(params.flowId); + const flowId = safeDecode(params.flowId); const search = new URLSearchParams(useSearch()); const from = search.get("from"); // With `from` an archetype flow: the one agent this copy is for. diff --git a/frontend/packages/swarm-ui/src/shell/routes.ts b/frontend/packages/swarm-ui/src/shell/routes.ts index bfc581efb..027ff1ddd 100644 --- a/frontend/packages/swarm-ui/src/shell/routes.ts +++ b/frontend/packages/swarm-ui/src/shell/routes.ts @@ -28,6 +28,19 @@ export function isFullBleedRoute(location: string): boolean { // which starts a terminal group's path (`/agents/group/:id`). const SELECTED_AGENT_PATTERN = /^\/agents\/([^/]+)$/; +/** + * A path segment as text. A segment that isn't valid percent-encoding (a typed + * or mangled address) comes back as it is, so a page can say "no such agent" + * rather than throwing while it renders. + */ +export function safeDecode(segment: string): string { + try { + return decodeURIComponent(segment); + } catch { + return segment; + } +} + /** The agent a path selects on the agents page, or `null` for any other path. */ export function selectedAgent(location: string): string | null { const match = SELECTED_AGENT_PATTERN.exec(location); diff --git a/frontend/packages/swarm-ui/test/routes.test.mjs b/frontend/packages/swarm-ui/test/routes.test.mjs index f2d4687ae..ee7ddc0fa 100644 --- a/frontend/packages/swarm-ui/test/routes.test.mjs +++ b/frontend/packages/swarm-ui/test/routes.test.mjs @@ -7,6 +7,7 @@ import { isFullBleedRoute, movedHref, pageOf, + safeDecode, selectedAgent, visibleNavItems, } from "../src/shell/routes.ts"; @@ -113,3 +114,9 @@ test("an agent selected on the agents page is still the agents page", () => { assert.equal(pageOf("/agents/group/g1"), "/agents/group/g1"); assert.equal(pageOf("/jobs"), "/jobs"); }); + +test("a path segment decodes, and a malformed one comes back as typed", () => { + assert.equal(safeDecode("my%20agent"), "my agent"); + assert.equal(safeDecode("plain"), "plain"); + assert.equal(safeDecode("%E0%A4%A"), "%E0%A4%A"); +});