From c7e559a09e8b8bc448912ac9fe6801e5f17b2ddd Mon Sep 17 00:00:00 2001 From: iris Date: Wed, 25 Nov 2026 19:21:46 +0100 Subject: [PATCH] swarm-ui: lock the backend configuration box when its stored text could not be read Only a 404 means nothing is stored. Any other failed read used to leave an empty, editable box with save enabled, so saving one line replaced a configuration that was never read. The box now stays locked behind an error with a retry button. The decision is a small tested function, and the swarm-ui doc describes it. --- docs/4-implementation/web-ui/swarm-ui.md | 5 ++- .../swarm-ui/src/agent/detail/account-api.ts | 14 ++++++ .../swarm-ui/src/pages/LinkBackendEnvForm.tsx | 45 ++++++++++++++++--- .../swarm-ui/test/account-api.test.mjs | 9 ++++ 4 files changed, 66 insertions(+), 7 deletions(-) diff --git a/docs/4-implementation/web-ui/swarm-ui.md b/docs/4-implementation/web-ui/swarm-ui.md index d05cf0294..b07435163 100644 --- a/docs/4-implementation/web-ui/swarm-ui.md +++ b/docs/4-implementation/web-ui/swarm-ui.md @@ -1177,7 +1177,10 @@ saved. The **claude login** row of an agent's details also sets the API key: **set API key** (or **replace API key**) sends the key as `ANTHROPIC_API_KEY=` to the agent's secrets, **remove API key** deletes them, and **advanced** opens the -form above. The row shows whether the store holds a key and never the key. A +form above. The row shows whether the store holds a key and never the key. The +form reads the stored configuration first: a 404 means the store holds none, and any +other failed read shows the error with a **retry** button and keeps **save** +disabled, so a save can't replace text that was never read. A change reaches the agent at its next restart, and an agent with a key tries it instead of waiting for a claude login. diff --git a/frontend/packages/swarm-ui/src/agent/detail/account-api.ts b/frontend/packages/swarm-ui/src/agent/detail/account-api.ts index 34be78507..08f59f709 100644 --- a/frontend/packages/swarm-ui/src/agent/detail/account-api.ts +++ b/frontend/packages/swarm-ui/src/agent/detail/account-api.ts @@ -76,6 +76,20 @@ export function backendEnvUrl(hive: string, agent: string): string { return `${agentBase(hive, agent)}/backend-env`; } +/** + * What a read of the stored backend configuration answered: `stored` (200), + * `none` (404, nothing is stored, so an empty box is right) or `failed` (any + * other answer, which must not unlock the box: a save would replace text that + * was never read). + */ +export function configReadKind( + status: number, + ok: boolean, +): "stored" | "none" | "failed" { + if (ok) return "stored"; + return status === 404 ? "none" : "failed"; +} + /** GET (is any stored), PUT and DELETE target for the agent's backend secrets. */ export function backendSecretsUrl(hive: string, agent: string): string { return `${agentBase(hive, agent)}/backend-secrets`; diff --git a/frontend/packages/swarm-ui/src/pages/LinkBackendEnvForm.tsx b/frontend/packages/swarm-ui/src/pages/LinkBackendEnvForm.tsx index 18a4757bc..4f3e2c29e 100644 --- a/frontend/packages/swarm-ui/src/pages/LinkBackendEnvForm.tsx +++ b/frontend/packages/swarm-ui/src/pages/LinkBackendEnvForm.tsx @@ -8,6 +8,8 @@ // box starts empty, and the stored secrets change only when it is filled and // saved (or removed). Whether any are stored shows beside the box. import { useEffect, useState } from "preact/hooks"; +import { ApiErrorPanel } from "@hive/shared/api-error-panel.js"; +import { readApiError, type ProblemDetails } from "@hive/shared/api-error.js"; import { Panel } from "../ui/panel/Panel.js"; import { TextAreaField } from "../ui/text-area-field/TextAreaField.js"; import { Button } from "../ui/button/Button.js"; @@ -21,6 +23,7 @@ import { useSubmit } from "../ui/submit/useSubmit.js"; import { backendEnvUrl, backendSecretsUrl, + configReadKind, } from "../agent/detail/account-api.js"; import "./LinkBackendEnvForm.css"; @@ -39,6 +42,11 @@ export function LinkBackendEnvForm({ }) { const [config, setConfig] = useState(""); const [loaded, setLoaded] = useState(false); + // Why the stored configuration could not be read. Only a 404 means "nothing + // stored"; any other answer leaves the box locked, so a save cannot replace + // a configuration that was never read. + const [loadProblem, setLoadProblem] = useState(null); + const [attempt, setAttempt] = useState(0); const [secrets, setSecrets] = useState(""); const [secretsStored, setSecretsStored] = useState(null); const configRun = useSubmit(); @@ -48,19 +56,25 @@ export function LinkBackendEnvForm({ // The stored configuration, or nothing when none is stored (a 404). useEffect(() => { let live = true; + setLoadProblem(null); void (async () => { try { const r = await fetch(backendEnvUrl(hive, agent)); - if (live && r.ok) setConfig(((await r.json()) as { env: string }).env); - } catch { - // The box stays empty; a save shows the real problem. + if (!live) return; + const kind = configReadKind(r.status, r.ok); + if (kind === "stored") { + setConfig(((await r.json()) as { env: string }).env); + } + if (kind === "failed") setLoadProblem(await readApiError(r)); + else setLoaded(true); + } catch (e) { + if (live) setLoadProblem({ detail: String(e) }); } - if (live) setLoaded(true); })(); return () => { live = false; }; - }, [hive, agent]); + }, [hive, agent, attempt]); async function loadSecretsStored() { try { @@ -169,6 +183,19 @@ export function LinkBackendEnvForm({ of the same name. For just an API key, use set API key.

configuration + {loadProblem ? ( + <> + + + + + + ) : null} { e.preventDefault(); @@ -188,7 +215,13 @@ export function LinkBackendEnvForm({ value={config} required rows={5} - placeholder={loaded ? "ANTHROPIC_BASE_URL=…" : "loading…"} + placeholder={ + loaded + ? "ANTHROPIC_BASE_URL=…" + : loadProblem + ? "not read" + : "loading…" + } onInput={setConfig} /> diff --git a/frontend/packages/swarm-ui/test/account-api.test.mjs b/frontend/packages/swarm-ui/test/account-api.test.mjs index 256969667..ed8f5200a 100644 --- a/frontend/packages/swarm-ui/test/account-api.test.mjs +++ b/frontend/packages/swarm-ui/test/account-api.test.mjs @@ -9,6 +9,7 @@ import { backendSecretsUrl, checkApiKey, checkLink, + configReadKind, deleteUrl, linkBody, linkUrl, @@ -98,3 +99,11 @@ test("a key the controller would refuse is caught first, and a plain one is not" assert.notEqual(checkApiKey(bad), null, bad); } }); + +test("only a 404 means no configuration is stored; any other failure locks the box", () => { + assert.equal(configReadKind(200, true), "stored"); + assert.equal(configReadKind(404, false), "none"); + assert.equal(configReadKind(500, false), "failed"); + assert.equal(configReadKind(403, false), "failed"); + assert.equal(configReadKind(0, false), "failed"); +});