diff --git a/docs/4-implementation/web-ui/swarm-ui.md b/docs/4-implementation/web-ui/swarm-ui.md index e21b3f85c..ce77e3428 100644 --- a/docs/4-implementation/web-ui/swarm-ui.md +++ b/docs/4-implementation/web-ui/swarm-ui.md @@ -111,9 +111,9 @@ store has the login, the dialog closes and a short toast in the top-right corner says so. The credentials go to the agent through the secret store ([`claude-login.md`](../../2-handbook/swarm/claude-login.md)). The agent's own web UI login still works too. The **inference** section of an agent's details -has a **claude login** row: when the swarm stored the agent's login, or that -none is, with a **log in** button (**log in again** when the swarm holds one) -that opens the same dialog, and a **delete login** action that asks first. +has a **claude login** row: when the swarm stored the login, or that none is, +with a **log in** button (**log in again** when the swarm holds one) that opens +the same dialog, and a **delete login** action that asks first. A card shows its freshness when the agent isn't **fresh**: **stale** with how long ago the hive last reported it, **never reported**, or **not in swarm identity**. A fresh agent shows nothing, so the @@ -199,14 +199,13 @@ starts open, the other five folded: shows its first five rows with a **show all N** button under them; expanded, the rows scroll inside a box and **show fewer** folds them again. - **instructions**: the agent's AGENTS.md, as an editor you can type in. -- **inference**: how the agent thinks and what it costs: its model and effort - and the other inference settings, its resource caps, and the claude login and - API key (the login's state, **log in**, and **set API key**), since the key is - how the agent reaches claude. -- **access**: what the agent may reach: internet, allowed domains, repositories, - SSH and permissions. Internet, allowed domains, repositories, SSH and - permissions show a badge; select it to open the control that changes it - (described below). +- **inference**: how the agent thinks and what it costs, as an editor in place: + its model and effort, the subagent settings and the two context limits, then + the claude login and API key (the login's state, **log in**, and **set API + key**), since the key is how the agent reaches claude. +- **access**: what the agent may reach and use: internet, allowed domains, + repositories, SSH, permissions and its resource caps. Each shows a badge; + select it to open the control that changes it (described below). - **accounts**: the agent's linked external accounts, and the menu that links another. @@ -321,13 +320,13 @@ with no icon keeps the hyperhive mark. Everywhere else the swarm UI shows an age The tab also has an agent badge, with the agent's icon (carrying the connection light) and name, ahead of the state badges. It opens the agent's details in a popover under the badge: the same identity card and folded **instructions**, -**inference**, **access** and **accounts** sections as the details on `/agents`, -without the terminal and the activity. The popover doesn't dim the page. -Selecting outside it, or Escape, closes it. From the keyboard, opening a popover -moves focus into it, and Escape puts focus back on the badge. The filter -popovers on table headers work the same way. You can stop or pause the agent, or -link an account, without leaving the terminal. A subagent's tab opens its -agent's details. +**inference**, **access** and **accounts** sections as the +details on `/agents`, without the terminal and the activity. The popover doesn't +dim the page. Selecting outside it, or Escape, closes it. From the keyboard, +opening a popover moves focus into it, and Escape puts focus back on the badge. +The filter popovers on table headers work the same way. You can stop or pause +the agent, or link an account, without leaving the terminal. A subagent's tab +opens its agent's details. #### Groups of terminals @@ -882,29 +881,29 @@ text stays. ### Inference settings -An agent's detail pane shows, in its **inference** section, its model and -effort, each with where it comes from: the swarm, its agent.nix, its own pick, -or the default. Below them are the subagent model list the swarm sets, or "its -own" when it sets none, the subagent cap, which reads "no cap" when unset, and -the fresh-session threshold and the soft context limit, which each read "its -own" when unset. **edit** opens a dialog with the fields, filled with what the -swarm stores; saving does a `PUT /api/agents/{agent}/inference` of the whole -value. A field you leave empty stays out of it, so the agent keeps its own value -for that field; an empty max subagents is no cap, and the two context limits -share one slider with two handles on the model's context window, from 5 to 100 -percent: a turn that finds the prompt cache cold starts a fresh session once the -context passes the left (green) handle, and the agent compacts at the end of a -turn once it passes the right (purple) one, and a turn can run past it. The -handles can't cross. A handle the swarm hasn't set sits at the harness's own -value (50 and 75), dimmed; **use its own** on its line puts it back there, which -leaves that field out of the `PUT`. **clear the swarm's setting** sends the -`DELETE`. The model is a dropdown of the models the swarm already knows (`GET -/api/models`: those in any agent's stored settings and those the agents report -running on), with the agent's own as the empty choice; **other…** opens a box -for any other model name, and a stored name the list lacks opens it too. The -subagent models stay a typed list. The card reads `GET -/api/agents/{agent}/inference`; before the agent's first report reaches the -controller, it shows the swarm's stored values only. +An agent's detail pane holds, in its **inference** section, the editor itself, +with no dialog: a line says what the agent runs on now and where the model and +the effort come from (the swarm, its agent.nix, its own pick, or the default), +and below it the fields sit in one row where there is room, filled with what the +swarm stores: model, effort, the subagent model list and the subagent cap. +**save** does a `PUT /api/agents/{agent}/inference` of the whole value, and is +off until a field changes. **reset** puts the boxes back to the stored value. +Unsaved edits stay when you switch to another agent and come back. A field you +leave empty stays out of it, so the agent keeps its own value for that field; an +empty max subagents is no cap, and the two context limits share one slider with +two handles on the model's context window, from 5 to 100 percent: a turn that +finds the prompt cache cold starts a fresh session once the context passes the +left (green) handle, and the agent compacts at the end of a turn once it passes +the right (purple) one, and a turn can run past it. The handles can't cross. A +handle the swarm hasn't set sits at the harness's own value (50 and 75), dimmed; +**use its own** on its line puts it back there, which leaves that field out of +the `PUT`. **clear the swarm's setting** sends the `DELETE`. The model is a +dropdown of the models the swarm already knows (`GET /api/models`: those in any +agent's stored settings and those the agents report running on), with the +agent's own as the empty choice; **other…** opens a box for any other model +name, and a stored name the list lacks opens it too. The subagent models stay a +typed list. The editor reads `GET /api/agents/{agent}/inference`; before the +agent's first report reaches the controller, it shows no running line. [inference](../swarm/inference.md) describes the settings themselves. ### Editing an agent's instructions @@ -927,7 +926,7 @@ meanwhile. A reload of the page drops it. ### Resource caps -An agent's detail pane shows, in its **inference** section, its CPU quota and +An agent's detail pane shows, in its **access** section, its CPU quota and memory max under **resource caps**. A cap is the swarm's, so it follows the agent to whichever hive runs it, and it replaces what the hive says for that field. A field the swarm leaves out shows as the hive deciding it. **edit** @@ -1164,10 +1163,10 @@ reads back and its box starts with the stored text. The secrets never read back, so their box starts empty and replaces the stored secrets only when filled and 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 +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 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/AgentDetail.tsx b/frontend/packages/swarm-ui/src/agent/detail/AgentDetail.tsx index aa8ab5ad8..ac80a5039 100644 --- a/frontend/packages/swarm-ui/src/agent/detail/AgentDetail.tsx +++ b/frontend/packages/swarm-ui/src/agent/detail/AgentDetail.tsx @@ -2,11 +2,11 @@ // is doing, `AgentIdentity`), then folded sections (`sections.ts`), one for // each question an operator comes with: the terminal, what it is working on // (activity), its instructions, how it thinks (inference, which holds the -// claude login and API key), what it may reach (access) and which accounts it -// is linked to. Each section builds when first opened. In the details popover -// on a terminal page (`AgentDetailPopover`) it is the identity card without -// the name, then the four settings sections, since the page is already beside -// a terminal. +// claude login and API key), what it may reach and use (access, which holds +// the CPU and memory caps) and which accounts it is linked to. Each section +// builds when first opened. In the details popover on a terminal page +// (`AgentDetailPopover`) it is the identity card without the name, then the +// four settings sections, since the page is already beside a terminal. // // Takes the agent's *live* row and the shared `AgentActions`, so a state // change or a link made here shows wherever else the same row is drawn. @@ -15,7 +15,11 @@ import { Hint } from "../../ui/hint/Hint.js"; import type { AgentActions } from "../roster/AgentActions.js"; import { AgentApiKey } from "./AgentApiKey.js"; import { AgentClaudeLogin } from "./AgentClaudeLogin.js"; -import { AgentInference } from "./AgentInference.js"; +import { + AgentInference, + InferencePair, + InferenceRows, +} from "./AgentInference.js"; import { AgentInstructions } from "./AgentInstructions.js"; import { AgentLimits } from "./AgentLimits.js"; import { AgentSsh } from "./AgentSsh.js"; @@ -43,27 +47,17 @@ import { BashTasks } from "../terminal/BashTasks.js"; import { SubagentTerms } from "../terminal/SubagentTerms.js"; import "./AgentDetail.css"; -function InferenceRows({ - agent, - hive, -}: { - agent: string; - hive: string | null; -}) { +// How the agent signs in to claude: the login and, on a hive, its API key. +function ClaudeRows({ agent, hive }: { agent: string; hive: string | null }) { return ( - <> -
resource caps
-
- -
-
claude login
-
+ + {hive ? ( ) : null} -
- + + ); } @@ -168,15 +162,17 @@ function SectionBody({ return ( <> -
- -
+ ); case "access": return (
+
resource caps
+
+ +
); case "accounts": diff --git a/frontend/packages/swarm-ui/src/agent/detail/AgentInference.css b/frontend/packages/swarm-ui/src/agent/detail/AgentInference.css index 73c78b0da..c636a4fd4 100644 --- a/frontend/packages/swarm-ui/src/agent/detail/AgentInference.css +++ b/frontend/packages/swarm-ui/src/agent/detail/AgentInference.css @@ -1,22 +1,42 @@ -/* — the settings in one wrapping line: each badge, then where it comes from. */ +/* — the settings as labelled values, two to a line where there + is room, each value followed by where it comes from. */ .ui-agent-inference { display: flex; flex-direction: column; align-items: flex-start; - gap: 0.3em; + gap: 0.5em; } -.ui-agent-inference-list { - display: contents; - list-style: none; +.ui-agent-inference-rows { + display: grid; + grid-template-columns: repeat(auto-fit, minmax(22em, 1fr)); + gap: 0.3em 2em; + align-self: stretch; margin: 0; - padding: 0; } -.ui-agent-inference-list li { - display: contents; +.ui-agent-inference-pair { + display: grid; + grid-template-columns: 12em minmax(0, 1fr); + gap: 0 1em; } -.ui-agent-inference-source { - margin-left: -0.25em; +.ui-agent-inference-pair dt { + color: var(--muted); +} +.ui-agent-inference-pair dd { + margin: 0; } .ui-agent-inference-source { color: var(--muted); } +.ui-agent-inference-now { + margin: 0; +} +.ui-agent-inference .ui-form-stack { + align-items: stretch; + align-self: stretch; + max-width: 60em; + margin-top: 0; +} +/* The claude login row under the editor. */ +.ui-agent-inference + .ui-agent-inference-rows { + margin-top: 1em; +} diff --git a/frontend/packages/swarm-ui/src/agent/detail/AgentInference.tsx b/frontend/packages/swarm-ui/src/agent/detail/AgentInference.tsx index af4dfbe7c..67ab1964d 100644 --- a/frontend/packages/swarm-ui/src/agent/detail/AgentInference.tsx +++ b/frontend/packages/swarm-ui/src/agent/detail/AgentInference.tsx @@ -1,29 +1,28 @@ -// — one agent's inference settings in an agent's details: the -// model and effort it runs on now, with where each came from, and an editor -// for the value the swarm sets. +// — one agent's inference settings in an agent's details, as +// the editor itself: boxes filled with the value the swarm stores, and above +// them the model and effort the agent runs on now, with where each came from. // // Reads `GET /api/agents/{name}/inference`, which answers // `{ configured, effective }`: `configured` is the swarm's stored value (null // when none), `effective` what the agent reported running on (null until its -// first report reaches the controller). The editor PUTs the whole value and -// the clear action DELETEs it; a cleared field puts the agent back on its own -// value, which is why an empty field is left out of the body rather than sent. +// first report reaches the controller). Save PUTs the whole value and the clear +// action DELETEs it; a cleared field puts the agent back on its own value, +// which is why an empty field is left out of the body rather than sent. Unsaved +// edits are kept per agent, so switching to another agent and back keeps them. // // Its state belongs to one agent: callers key it by agent name, so switching // agents remounts it instead of showing the previous agent's settings. -import { useEffect, useState } from "preact/hooks"; +import type { ComponentChildren } from "preact"; +import { useEffect, useRef, useState } from "preact/hooks"; import { ApiErrorPanel } from "@hive/shared/api-error-panel.js"; import { readApiError, type ProblemDetails } from "@hive/shared/api-error.js"; -import { Badge } from "@hive/shared/badge.js"; import { ActionRow } from "../../ui/action-row/ActionRow.js"; -import { Dialog } from "../../ui/dialog/Dialog.js"; +import { FieldGrid, FieldGridFull } from "../../ui/field-grid/FieldGrid.js"; import { Hint } from "../../ui/hint/Hint.js"; -import { Panel } from "../../ui/panel/Panel.js"; import { SelectField } from "../../ui/select-field/SelectField.js"; import { TextField } from "../../ui/text-field/TextField.js"; import { Button } from "../../ui/button/Button.js"; import { FormStack } from "../../ui/form-stack/FormStack.js"; -import "../../ui/badge-row/BadgeRow.css"; import "./AgentInference.css"; import { ContextLimits } from "./ContextLimits.js"; import { @@ -33,6 +32,13 @@ import { modelSelectValue, useKnownModels, } from "./model-choice.js"; +import { + draftOf, + EMPTY_INFERENCE_DRAFT, + rememberDraft, + sameDraft, + type InferenceDraft, +} from "./inference-draft.js"; import { MAX_SUBAGENTS, maxSubagentsValue } from "./max-subagents.js"; import { MAX_SOFT_CONTEXT_PERCENT, @@ -97,25 +103,7 @@ function inferenceUrl(agent: string): string { return `/api/agents/${encodeURIComponent(agent)}/inference`; } -// The inference boxes as typed. Shared by the editor here and the -// create-agent form, which send what `draftBody` makes of them. -export interface InferenceDraft { - model: string; - effort: string; - subagents: string; - maxSubagents: string; - softContext: string; - freshSession: string; -} - -export const EMPTY_INFERENCE_DRAFT: InferenceDraft = { - model: "", - effort: "", - subagents: "", - maxSubagents: "", - softContext: "", - freshSession: "", -}; +export { EMPTY_INFERENCE_DRAFT, type InferenceDraft }; // Why the draft would be refused, or null when it is fine to send. export function draftProblem(d: InferenceDraft): string | null { @@ -220,50 +208,98 @@ export function InferenceFields({ const problem = draftProblem(draft); return ( <> - onChange({ ...draft, model })} - /> - onChange({ ...draft, effort })} - /> - onChange({ ...draft, subagents })} - /> - onChange({ ...draft, maxSubagents })} - /> - onChange({ ...draft, ...limits })} - /> + + onChange({ ...draft, model })} + /> + onChange({ ...draft, effort })} + /> + onChange({ ...draft, subagents })} + /> + onChange({ ...draft, maxSubagents })} + /> + + onChange({ ...draft, ...limits })} + /> + + {problem ? {problem} : null} ); } +/** Labelled values, two to a line where there is room. */ +export function InferenceRows({ children }: { children: ComponentChildren }) { + return
{children}
; +} + +/** One label and its value. */ +export function InferencePair({ + label, + title, + valueTitle, + children, +}: { + label: string; + /** What the setting does, on the label. */ + title?: string; + /** Why the value is what it is, on the value. */ + valueTitle?: string; + children: ComponentChildren; +}) { + return ( +
+
{label}
+
{children}
+
+ ); +} + +// Where a value came from, after the value. +function Source({ source }: { source: Source }) { + return ( + ({SOURCE_LABEL[source]}) + ); +} + +// The unsaved edits of this visit, by agent: switching to another agent and +// back keeps what was typed. Read once when an agent's editor mounts and +// written as the draft changes. +const drafts = new Map(); + export function AgentInference({ agent }: { agent: string }) { const [view, setView] = useState(null); const [error, setError] = useState(null); // Bumped after a save or a clear, so the settings are fetched again. const [reload, setReload] = useState(0); - const [editing, setEditing] = useState(false); + const [draft, setDraft] = useState(null); + const [busy, setBusy] = useState(false); + const [problem, setProblem] = useState(null); + // Set by a save or a clear: the next read replaces the boxes with the stored + // value instead of keeping the typed one. + const resetOnLoad = useRef(false); useEffect(() => { let cancelled = false; @@ -275,9 +311,14 @@ export function AgentInference({ agent }: { agent: string }) { } const body = (await r.json()) as Partial; if (cancelled) return; - setView({ - configured: body.configured ?? null, - effective: body.effective ?? null, + const configured = body.configured ?? null; + setView({ configured, effective: body.effective ?? null }); + setDraft((current) => { + if (resetOnLoad.current || current === null) { + resetOnLoad.current = false; + return drafts.get(agent) ?? draftOf(configured); + } + return current; }); setError(null); })().catch((e: unknown) => { @@ -288,6 +329,13 @@ export function AgentInference({ agent }: { agent: string }) { }; }, [agent, reload]); + const base = view === null ? null : draftOf(view.configured); + const dirty = draft !== null && base !== null && !sameDraft(draft, base); + useEffect(() => { + if (draft === null || base === null) return; + rememberDraft(drafts, agent, draft, base); + }, [agent, draft, base]); + // Shown as text, not a hover title. Settings already loaded stay under a // failed refetch, so a save that worked is not hidden by a read that did not. const errorPanel = error ? ( @@ -296,155 +344,25 @@ export function AgentInference({ agent }: { agent: string }) { problem={error} /> ) : null; - if (view === null) { + if (view === null || draft === null || base === null) { return errorPanel ?? …; } const { configured, effective } = view; - const swarmModels = configured?.subagent_models ?? []; - return ( -
- {errorPanel} -
-
    -
  • - {effective ? ( - <> - - - {SOURCE_LABEL[effective.model_source]} - - - ) : ( - - )} -
  • -
  • - {effective ? ( - <> - - - {SOURCE_LABEL[effective.effort_source]} - - - ) : ( - - )} -
  • -
  • - 0 ? swarmModels.join(", ") : "its own" - } - /> -
  • -
  • - -
  • -
  • - -
  • -
  • - -
  • -
- setEditing(true)} - /> -
- setEditing(false)} - label="edit inference settings" - plain - > - {/* Remounted per open, so a cancelled edit leaves nothing behind. */} - {editing ? ( - setEditing(false)} - onSaved={() => { - setEditing(false); - setReload((n) => n + 1); - }} - /> - ) : null} - -
- ); -} - -function InferenceForm({ - agent, - configured, - onClose, - onSaved, -}: { - agent: string; - configured: Settings | null; - onClose: () => void; - onSaved: () => void; -}) { - const [draft, setDraft] = useState({ - model: configured?.model ?? "", - effort: configured?.effort ?? "", - subagents: (configured?.subagent_models ?? []).join(", "), - maxSubagents: configured?.max_subagents?.toString() ?? "", - softContext: configured?.soft_context_percent?.toString() ?? "", - freshSession: configured?.fresh_session_percent?.toString() ?? "", - }); - const [busy, setBusy] = useState(false); - const [problem, setProblem] = useState(null); - const formProblem = draftProblem(draft); - const empty = draftIsEmpty(draft); async function send(init: RequestInit) { setBusy(true); + setProblem(null); try { const r = await fetch(inferenceUrl(agent), init); if (!r.ok) { setProblem(await readApiError(r)); return; } - onSaved(); + drafts.delete(agent); + resetOnLoad.current = true; + setReload((n) => n + 1); } catch (e: unknown) { setProblem({ detail: String(e) }); } finally { @@ -457,23 +375,20 @@ function InferenceForm({ void send({ method: "PUT", headers: { "content-type": "application/json" }, - body: JSON.stringify(draftBody(draft)), + body: JSON.stringify(draftBody(draft as InferenceDraft)), }); } return ( - -

- Sets what the swarm makes {agent} run on. A field left empty stays the - agent's own value; an empty max subagents is no cap. The two context - limits are shares of the context window: a turn that finds the prompt - cache cold starts a fresh session once the context is past the - fresh-session threshold, and the agent compacts at the end of a turn - once it is past the soft limit (a turn can run past it). The agent picks - a change up within seconds; a turn already running finishes on what it - started with, and subagents already running past a lowered cap keep - running. -

+
+ {errorPanel} + {effective ? ( + + running {effective.model} + , effort {effective.effort} + + + ) : null} {problem ? ( @@ -483,18 +398,28 @@ function InferenceForm({ - + {dirty ? ( + + ) : null} + {configured !== null ? ( + + ) : null} - +
); } diff --git a/frontend/packages/swarm-ui/src/agent/detail/inference-draft.ts b/frontend/packages/swarm-ui/src/agent/detail/inference-draft.ts new file mode 100644 index 000000000..3b2eb2151 --- /dev/null +++ b/frontend/packages/swarm-ui/src/agent/detail/inference-draft.ts @@ -0,0 +1,72 @@ +// The inference boxes as typed, and the bookkeeping around them, without a DOM: +// what a stored value fills them with, whether they differ from it, and which +// unsaved drafts are kept while the operator looks at another agent. Shared by +// the editor in an agent's details and the create-agent form. + +/** The inference boxes as typed; every one is text, empty for "its own". */ +export interface InferenceDraft { + model: string; + effort: string; + subagents: string; + maxSubagents: string; + softContext: string; + freshSession: string; +} + +export const EMPTY_INFERENCE_DRAFT: InferenceDraft = { + model: "", + effort: "", + subagents: "", + maxSubagents: "", + softContext: "", + freshSession: "", +}; + +/** The swarm's stored value, every field optional. */ +export interface StoredInference { + model?: string; + effort?: string; + subagent_models?: string[]; + max_subagents?: number; + soft_context_percent?: number; + fresh_session_percent?: number; +} + +/** The boxes as a stored value fills them; none stored fills them empty. */ +export function draftOf(stored: StoredInference | null): InferenceDraft { + return { + model: stored?.model ?? "", + effort: stored?.effort ?? "", + subagents: (stored?.subagent_models ?? []).join(", "), + maxSubagents: stored?.max_subagents?.toString() ?? "", + softContext: stored?.soft_context_percent?.toString() ?? "", + freshSession: stored?.fresh_session_percent?.toString() ?? "", + }; +} + +/** Whether two drafts hold the same text in every box. */ +export function sameDraft(a: InferenceDraft, b: InferenceDraft): boolean { + return ( + a.model === b.model && + a.effort === b.effort && + a.subagents === b.subagents && + a.maxSubagents === b.maxSubagents && + a.softContext === b.softContext && + a.freshSession === b.freshSession + ); +} + +/** + * Keeps `draft` as this agent's unsaved edit while it differs from `base` + * (what the swarm stores), and drops the kept one when it doesn't, so an agent + * with nothing unsaved leaves nothing behind. + */ +export function rememberDraft( + kept: Map, + agent: string, + draft: InferenceDraft, + base: InferenceDraft, +): void { + if (sameDraft(draft, base)) kept.delete(agent); + else kept.set(agent, draft); +} diff --git a/frontend/packages/swarm-ui/test/inference-draft.test.mjs b/frontend/packages/swarm-ui/test/inference-draft.test.mjs new file mode 100644 index 000000000..4e1b5d509 --- /dev/null +++ b/frontend/packages/swarm-ui/test/inference-draft.test.mjs @@ -0,0 +1,59 @@ +// The inference boxes and the drafts kept per agent. Run by `npm test`, which +// Node runs with its built-in test runner and type stripping. +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { + EMPTY_INFERENCE_DRAFT, + draftOf, + rememberDraft, + sameDraft, +} from "../src/agent/detail/inference-draft.ts"; + +const stored = { + model: "sonnet", + effort: "high", + subagent_models: ["haiku", "sonnet"], + max_subagents: 0, + soft_context_percent: 70, + fresh_session_percent: 40, +}; + +test("a stored value fills the boxes, and none stored fills them empty", () => { + assert.deepEqual(draftOf(stored), { + model: "sonnet", + effort: "high", + subagents: "haiku, sonnet", + maxSubagents: "0", + softContext: "70", + freshSession: "40", + }); + assert.deepEqual(draftOf(null), EMPTY_INFERENCE_DRAFT); + assert.deepEqual(draftOf({}), EMPTY_INFERENCE_DRAFT); +}); + +test("drafts are the same only when every box matches", () => { + const base = draftOf(stored); + assert.ok(sameDraft(base, { ...base })); + for (const box of Object.keys(base)) { + assert.ok(!sameDraft(base, { ...base, [box]: base[box] + "x" }), box); + } +}); + +test("an edited draft is kept for its agent, and dropped once it matches again", () => { + const kept = new Map(); + const base = draftOf(stored); + const edited = { ...base, effort: "low" }; + rememberDraft(kept, "iris", edited, base); + assert.deepEqual(kept.get("iris"), edited); + rememberDraft(kept, "iris", { ...base }, base); + assert.equal(kept.has("iris"), false); +}); + +test("one agent's draft does not touch another's", () => { + const kept = new Map(); + const base = draftOf(stored); + rememberDraft(kept, "iris", { ...base, model: "opus" }, base); + rememberDraft(kept, "argus", { ...base }, base); + assert.equal(kept.size, 1); + assert.equal(kept.get("iris").model, "opus"); +});