Watch
0
0
Fork
You've already forked hyperhive
0

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.
This commit is contained in:
iris 2026-11-25 19:21:46 +01:00 • committed by mara
commit c7e559a09e
3 changed files with 66 additions and 7 deletions

View file

@ -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`;

View file

@ -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<ProblemDetails | null>(null);
const [attempt, setAttempt] = useState(0);
const [secrets, setSecrets] = useState("");
const [secretsStored, setSecretsStored] = useState<boolean | null>(null);
const configRun = useSubmit<string>();
@ -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 <strong>set API key</strong>.
</p>
<SectionHeading class="backend-env-heading">configuration</SectionHeading>
{loadProblem ? (
<>
<ApiErrorPanel
context="the stored configuration was not read"
problem={loadProblem}
/>
<ActionRow>
<Button type="button" onClick={() => setAttempt((n) => n + 1)}>
retry
</Button>
</ActionRow>
</>
) : null}
<FormStack
onSubmit={(e) => {
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}
/>
<ActionRow>

View file

@ -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");
});