Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

36.7 KB
Raw Blame
/**
 * The society frontend's data seam — every card, roster row and world walker
 * reads agents through the hooks in this file and nothing else.
 *
 * Source of truth is `/api/society` (dodaios/ui/web/society_routes.py, rows
 * typed in lib/societyApi.ts). A failed fetch (the backend restarting, a
 * dropped connection) never swaps in sample rows: the last known roster
 * stays on screen, or the rail keeps loading until the backend answers —
 * nothing else in the society components may know where agents come from.
 *
 * The enums here mirror the roster schema (MASTERPLAN §3.1, agent-definition.md
 * §2); the const arrays in lib/societyApi.ts are pinned to the Python enums by
 * the five-layer parity test (AP-4).
 */
import { useCallback, useEffect } from "react";
import { useQuery, useQueryClient, type QueryClient } from "@tanstack/react-query";
import type { MentionPlugin } from "./chat/mentionItems";

import type { AgentApprovalMode, AgentRuntime, BrowserMode, Checkpoint, SocietyAgentRow } from "@/lib/societyApi";
import type { CompanionAppearance } from "./companion/appearance";
import type { WaitingReason } from "./roster/rosterPreview";
import { useCreateAgentDialog } from "./create/createAgentStore";

import { NEUTRAL_ASSISTANT_NAME } from "@/lib/assistantNameCache";
import { societyChatGroupsQuery } from "@/lib/societyChatGroups";
import { useEventStore } from "@/store/events";
import { translate } from "@/i18n";

import { PALETTE_PRESETS, resolvePalette, type FigureRecipe } from "./figures/figureRecipe";
import { SAMPLE_ROSTER } from "./mockRoster";
import { beginRetirement, retirementRunning, retirementStageMounted } from "./world/retireStore";
import { announceSpawn } from "./world/spawnStore";

/** The create dialog named an agent that already exists. */
export class AgentNameTaken extends Error {
  constructor() {
    super("An agent with this name already exists");
    this.name = "AgentNameTaken";
  }
}

/** Hermes / OpenClaw cannot drive the chosen provider (422
 *  `runtime_provider_unsupported`); the UI shows its own translated sentence. */
export class RuntimeProviderUnsupported extends Error {
  constructor(detail: string) {
    super(detail);
    this.name = "RuntimeProviderUnsupported";
  }
}

/** The error a failed society request stands for: a typed one when the
 *  backend names a reason the UI explains itself, else its detail text. */
export function requestError(body: { detail?: unknown } | null, fallback: string): Error {
  const reason = body?.detail;
  if (typeof reason === "string") return new Error(reason);
  if (reason && typeof reason === "object") {
    const typed = reason as { reason?: unknown; detail?: unknown };
    const text = typeof typed.detail === "string" ? typed.detail : fallback;
    return typed.reason === "runtime_provider_unsupported" ? new RuntimeProviderUnsupported(text) : new Error(text);
  }
  return new Error(fallback);
}

/** MASTERPLAN §2.5 — exactly one lead (Jarvis), orchestrators may ASSIGN. */
export type AgentTier = "lead" | "orchestrator" | "specialist";

/**
 * Semantic place in the world (§2.7) — the backend owns this, never pixels.
 *
 * An ALIAS of the checked list, not a second copy of it: `CHECKPOINTS` in
 * lib/societyApi.ts is what the five-layer parity test pins to the Python
 * enum, and a hand-kept duplicate here is exactly the enum drift that keeps
 * costing bugs (docs/BUGS.md).
 */
export type AgentCheckpoint = Checkpoint;

/** Coarse run state for rows and badges; the event log holds the detail. */
export type AgentRunState = "idle" | "working" | "waiting" | "paused";

/** §6.2 — the unattended ceiling; "block" never appears (block is block). */
export type PermissionCeiling = "safe" | "monitor" | "ask";
export type { AgentApprovalMode };

/** agent-definition.md §3.2 — everything the tiers allow, or only an allow-list. */
export type GrantMode = "all" | "allowlist";

/**
 * Per-agent accent colours for flat UI (roster swatch, chat avatar ring).
 * Derived from the figure's palette so the rail and the character agree.
 */
export interface AgentPalette {
  primary: string;
  secondary: string;
  accent: string;
}

export interface AgentRoutine {
  id: string;
  label: string;
  /** Human-readable schedule ("daily 07:00"), not a cron string. */
  schedule: string;
  /** ISO timestamp of the next fire, when the scheduler knows one. */
  nextFire: string | null;
}

export interface AgentStats {
  runs: number;
  totalCostUsd: number;
  /**
   * Spent since midnight UTC. The daily budget is a real gate — the scheduler
   * refuses a dispatch once this reaches `dailyBudgetUsd` — so the card shows
   * the two together rather than a cap with nothing beside it.
   */
  spentTodayUsd: number;
  /** Epoch ms of the last completed run; null for a fresh agent. */
  lastActiveMs: number | null;
}

/** Permission rules under the ceiling (agent-definition §3.4); require wins. */
export interface ApprovalRules {
  requireApproval: string[];
  alwaysAllow: string[];
}

/** One roster row — the model card's whole world (MASTERPLAN §3.1, agent-definition §2). */
export interface SocietyAgent {
  agentId: string;
  name: string;
  /** One job line ("Research scout"). */
  title: string;
  /** Standing instructions, Markdown — the agent's own rulebook. */
  description: string;
  tier: AgentTier;
  /** Provider id as the agent-chat catalog knows it (e.g. "anthropic"); "" = Jarvis' default brain. */
  provider: string;
  /** Display label for the provider, until the catalog lookup is wired. */
  providerLabel: string;
  model: string;
  effort: string;
  /** Stored subscription login; empty means the platform's active account. */
  accountId?: string;
  /** Where the agent runs: null/absent = this computer, else a connected computer id. */
  computerId?: string | null;
  executionEnvironment?: "local" | "sandbox";
  /** `own` = the agent's own browser profile; `attach` = the person's running Chrome. */
  browserMode?: BrowserMode;
  /** Sites its browser may open; empty = no restriction. */
  browserAllowedDomains?: string[];
  /** The agent loop that runs its turns (docs/agent-runtimes.md); absent = Jarvis. */
  runtime?: AgentRuntime;
  /** The character: archetype, base, parts, palette. null = the palette tile. */
  figure: FigureRecipe | null;
  palette: AgentPalette;
  grantMode: GrantMode;
  /** Capability ids on the allow-list (read only when grantMode = allowlist). */
  toolGrants: string[];
  /** Capability ids the agent reaches for first (derived from the description, editable). */
  focus: string[];
  /** Capability ids never offered to this agent. */
  denies: string[];
  approvalRules: ApprovalRules;
  permissionCeiling: PermissionCeiling;
  approvalMode?: AgentApprovalMode | null;
  dailyBudgetUsd: number;
  /** The seat pays per token (an API key). False on a subscription or local
   *  model, where no money flows per run; absent reads as true. */
  billedPerToken?: boolean;
  checkpoint: AgentCheckpoint;
  state: AgentRunState;
  /** The durable lifecycle state behind `state` ("active" | "paused" | "archived"). */
  lifecycle: "active" | "paused" | "archived";
  createdMs: number;
  /** How many runs may be in flight under this identity at once (the scheduler enforces it). */
  maxConcurrentRuns: number;
  /** Where its files live, relative to the data dir — its own sandbox. */
  workspaceDir: string;
  /** Where its notes land in the shared wiki. */
  wikiNamespace: string;
  /** The canonical agent_chat session bound to this agent; null on sample rows. */
  chatSessionId: string | null;
  /** What it waits on the person for right now (a question, an approval, ...); null when nothing. */
  waitingReason?: WaitingReason | null;
  routines: AgentRoutine[];
  stats: AgentStats;
}

/** Why an agent waits on the person (the society payload's `waiting_reason`). */
export type { WaitingReason };

export interface RosterData {
  agents: SocietyAgent[];
  /** True while rows come from the sample roster rather than society.db. */
  sample: boolean;
}

/**
 * Sample rows retired in this window. The sample roster is a frozen module
 * constant, so a retirement there is remembered here instead of mutating it.
 */
const RETIRED_SAMPLE = new Set<string>();

// ---------------------------------------------------------------------------
// row ↔ agent
// ---------------------------------------------------------------------------

/** A default look for a backend row that never chose one: deterministic per id, so it stays. */
export function defaultFigureFor(agentId: string, tier: AgentTier): FigureRecipe {
  if (tier === "lead") {
    // The lead is Jarvis, and Jarvis is Gigi — the app's own mascot.
    return { contract: 1, archetype: "spirit", base: "gigi", parts: {}, style: "spirit" };
  }
  let h = 0;
  for (let i = 0; i < agentId.length; i++) h = (h * 31 + agentId.charCodeAt(i)) | 0;
  const preset = PALETTE_PRESETS[Math.abs(h) % PALETTE_PRESETS.length];
  return { contract: 1, archetype: "biped", base: "rogue", parts: {}, palette: { ...preset.palette } };
}

function recipeFromAvatar(avatar: Record<string, unknown> | null | undefined): FigureRecipe | null {
  if (!avatar || avatar.contract !== 1 || typeof avatar.base !== "string") return null;
  return avatar as unknown as FigureRecipe;
}

function paletteFor(figure: FigureRecipe | null): AgentPalette {
  const p = resolvePalette(figure);
  return { primary: p.primary, secondary: p.secondary, accent: p.accent };
}

/** Provider labels that are words, not brand names: translated when a row is read. */
const PROVIDER_LABEL_KEYS: Record<string, string> = {
  ollama: "society.provider_label.ollama",
  "local-openai": "society.provider_label.local_server",
};

const PROVIDER_LABELS: Record<string, string> = {
  anthropic: "Anthropic",
  openai: "OpenAI",
  google: "Google",
  gemini: "Google Gemini",
  xai: "xAI",
  groq: "Groq",
  mistral: "Mistral",
  deepseek: "DeepSeek",
  "claude-api": "Anthropic Claude",
  "openai-codex": "OpenAI Codex",
  grok: "xAI Grok",
  openrouter: "OpenRouter",
  nvidia: "NVIDIA NIM",
};

function providerLabelFor(provider: string): string {
  const key = PROVIDER_LABEL_KEYS[provider];
  return key ? translate(key) : PROVIDER_LABELS[provider] ?? provider;
}

export function rowToAgent(row: SocietyAgentRow): SocietyAgent {
  const tier = row.tier as AgentTier;
  // A new agent's avatar may hold only its companion (the backend's random
  // one, or the create dialog's pick): it rides on the default figure.
  const stored = recipeFromAvatar(row.avatar);
  const companion = row.avatar && typeof row.avatar === "object" ? row.avatar.companion : undefined;
  const figure = stored
    ?? (companion
      ? ({ ...defaultFigureFor(row.agent_id, tier), companion } as FigureRecipe)
      : defaultFigureFor(row.agent_id, tier));
  const runState: AgentRunState =
    row.state === "paused" ? "paused" : ((row.run_state as AgentRunState | undefined) ?? "idle");
  return {
    agentId: row.agent_id,
    name: row.name,
    title: row.title,
    description: row.description,
    tier,
    provider: row.provider,
    providerLabel: row.provider ? providerLabelFor(row.provider) : "",
    model: row.model,
    accountId: row.account_id,
    computerId: row.computer_id ?? null,
    executionEnvironment: row.execution_environment ?? "local",
    browserMode: row.browser_mode ?? "own",
    browserAllowedDomains: row.browser_allowed_domains ?? [],
    runtime: row.runtime ?? "jarvis",
    effort: row.effort,
    figure,
    palette: paletteFor(figure),
    grantMode: row.grant_mode as GrantMode,
    toolGrants: row.grants ?? [],
    focus: row.focus ?? [],
    denies: row.denies ?? [],
    approvalRules: {
      requireApproval: row.approval_rules?.require_approval ?? [],
      alwaysAllow: row.approval_rules?.always_allow ?? [],
    },
    permissionCeiling: row.permission_ceiling as PermissionCeiling,
    approvalMode: row.approval_mode ?? null,
    dailyBudgetUsd: row.daily_budget_usd,
    billedPerToken: row.billed_per_token ?? true,
    checkpoint: row.checkpoint as AgentCheckpoint,
    state: runState,
    lifecycle: row.state,
    createdMs: row.created_ms,
    maxConcurrentRuns: row.max_concurrent_runs ?? 1,
    workspaceDir: row.workspace_dir ?? "",
    wikiNamespace: row.wiki_namespace ?? "",
    chatSessionId: row.session_id || null,
    waitingReason: row.waiting_reason
      ? {
        kind: row.waiting_reason.kind,
        summary: row.waiting_reason.summary,
        sessionId: row.waiting_reason.session_id,
        refId: row.waiting_reason.ref_id,
      }
      : null,
    routines: [],
    stats: {
      runs: row.stats?.runs ?? 0,
      totalCostUsd: row.stats?.total_cost_usd ?? 0,
      spentTodayUsd: row.stats?.spent_today_usd ?? 0,
      lastActiveMs: row.stats?.last_active_ms ?? null,
    },
  };
}

// ---------------------------------------------------------------------------
// queries
// ---------------------------------------------------------------------------

const ROSTER_QUERY_KEY = ["society", "roster"] as const;
const CAPABILITIES_QUERY_KEY = ["society", "capabilities"] as const;

/**
 * The roster as the backend has it. A failure throws, so the query keeps the
 * last roster it had (and retries) instead of showing anything made up; an
 * empty answer right after a non-empty one is a backend still starting, not
 * an empty society, and is treated the same way.
 */
export async function fetchSocietyRoster(previous?: RosterData): Promise<RosterData> {
  const res = await fetch("/api/society/agents");
  if (!res.ok) throw new Error(`GET /api/society/agents answered ${res.status}`);
  const body = (await res.json()) as { agents?: SocietyAgentRow[] };
  const rows = body.agents ?? [];
  if (rows.length === 0 && previous && !previous.sample && previous.agents.length > 0) {
    throw new Error("GET /api/society/agents answered an empty roster while starting");
  }
  return { agents: rows.map(rowToAgent), sample: false };
}

/** Retry a failed roster fetch calmly (1 s, 2 s, 4 s … at most 10 s apart) until the backend is back. */
const ROSTER_RETRY = import.meta.env.MODE === "test" ? false : true;
const rosterRetryDelay = (attempt: number) => Math.min(1_000 * 2 ** attempt, 10_000);

/**
 * Apply a change to the cached roster right away, then refresh it in the
 * background. A create or a retirement shows on the rail the moment the
 * server said yes, instead of after a second full `GET /agents`.
 */
function patchRoster(
  client: QueryClient,
  change: (agents: readonly SocietyAgent[]) => SocietyAgent[],
): void {
  client.setQueryData<RosterData>(ROSTER_QUERY_KEY, (prev) =>
    prev && !prev.sample ? { ...prev, agents: change(prev.agents) } : prev,
  );
  void client.invalidateQueries({ queryKey: ROSTER_QUERY_KEY });
}

/** A list this fresh is not asked for again by `warmSocietyLists`. */
const WARM_FRESH_MS = 2_000;

/**
 * Load the roster and the teams ahead of the views that show them.
 *
 * The Agents section starts some twenty requests when it mounts and a browser
 * sends six at a time to one host, so the two lists, asked last, waited over a
 * second for answers that take milliseconds: new agents were missing and every
 * team member stood alone until then. Asked here (at app start and on the
 * switch to the section, before it renders) both are there for its first paint.
 */
export async function warmSocietyLists(client: QueryClient): Promise<void> {
  // A refetch mid-ceremony would delete the figure being carried to the mine.
  if (retirementRunning()) return;
  const rosterQuery = {
    queryKey: ROSTER_QUERY_KEY,
    queryFn: () => fetchSocietyRoster(client.getQueryData<RosterData>(ROSTER_QUERY_KEY)),
    staleTime: WARM_FRESH_MS,
  };
  const teams = () => client.prefetchQuery({ ...societyChatGroupsQuery, staleTime: WARM_FRESH_MS });
  // The sample roster has no teams; a roster known to be real asks for both at once.
  const known = client.getQueryData<RosterData>(ROSTER_QUERY_KEY);
  if (known && !known.sample) {
    await Promise.all([client.prefetchQuery(rosterQuery), teams()]);
    return;
  }
  // A warm-up never waits on a backend that is down: the view's own query retries.
  await client.prefetchQuery(rosterQuery);
  const roster = client.getQueryData<RosterData>(ROSTER_QUERY_KEY);
  if (roster && !roster.sample) await teams();
}

/**
 * The lead is the assistant itself, so it wears the name the user gave it
 * through the wake phrase ("George"), not the roster row's seeded name. The
 * neutral fallback never replaces a real row name while the seed is pending.
 */
export function withLeadName(data: RosterData, assistantName: string): RosterData {
  const name = assistantName.trim();
  if (!name || name === NEUTRAL_ASSISTANT_NAME) return data;
  if (!data.agents.some((a) => a.tier === "lead" && a.name !== name)) return data;
  return { ...data, agents: data.agents.map((a) => (a.tier === "lead" ? { ...a, name } : a)) };
}

export function useSocietyRoster(enabled = true) {
  const client = useQueryClient();
  const assistantName = useEventStore((s) => s.assistantName) ?? "";
  const select = useCallback((data: RosterData) => withLeadName(data, assistantName), [assistantName]);
  return useQuery({
    queryKey: ROSTER_QUERY_KEY,
    queryFn: () => fetchSocietyRoster(client.getQueryData<RosterData>(ROSTER_QUERY_KEY)),
    retry: ROSTER_RETRY,
    retryDelay: rosterRetryDelay,
    enabled,
    select,
    staleTime: 15_000,
    // A refetch mid-ceremony would delete the figure being carried to the
    // mine out from under the animation; the commit invalidates instead.
    refetchInterval: () => (retirementRunning() ? false : 30_000),
  });
}

/** One agent by id, riding the roster query so both share one fetch. */
export function useSocietyAgent(agentId: string | null) {
  const roster = useSocietyRoster();
  const agent =
    agentId === null ? null : (roster.data?.agents.find((a) => a.agentId === agentId) ?? null);
  return { ...roster, agent };
}

/** One capability of the catalog (agent-definition §3.1) as GET /api/society/capabilities lists it. */
export interface Capability {
  id: string;
  kind: "plugin" | "cli" | "mcp" | "skill" | "core" | string;
  label: string;
  one_liner: string;
  risk_tier: string;
  connected: boolean;
  tool_name: string;
  /** Extra words the @-picker may match (server name, aliases). */
  aliases?: string[];
}

async function fetchCapabilities(): Promise<Capability[]> {
  const res = await fetch("/api/society/capabilities", { cache: "no-store" });
  if (!res.ok) throw new Error(`capabilities ${res.status}`);
  const body = (await res.json()) as { capabilities?: Capability[] };
  return body.capabilities ?? [];
}

export function useSocietyCapabilities(enabled = true, mentionOpen = false) {
  const query = useQuery({
    queryKey: CAPABILITIES_QUERY_KEY,
    queryFn: fetchCapabilities,
    enabled,
    staleTime: 60_000,
    retry: false,
  });
  const plugins = useQuery({
    queryKey: ["marketplace-plugins"],
    queryFn: async () => {
      const res = await fetch("/api/marketplace/plugins", { cache: "no-store" });
      if (!res.ok) throw new Error(`plugins ${res.status}`);
      return res.json() as Promise<{ plugins: MentionPlugin[] }>;
    },
    enabled: enabled && mentionOpen,
    staleTime: 0,
    retry: false,
  });
  const { refetch } = query;
  useEffect(() => {
    if (enabled && mentionOpen) void refetch();
  }, [enabled, mentionOpen, refetch]);
  return { ...query, plugins: plugins.data?.plugins ?? [], inventoryError: query.isError || plugins.isError,
    retryInventory: () => { void refetch(); void plugins.refetch(); } };
}

/**
 * Every plus in the society: opens the "new agent" dialog (name, runtime,
 * companion) and settles with the created agent. Closing the dialog rejects
 * with `CreateAgentCancelled` (`isCreateCancelled`), which is not an error.
 */
export function useQuickCreateAgent() {
  return useCallback(() => useCreateAgentDialog.getState().request(), []);
}

/** What the create dialog sends; every field may be left out. */
export interface NewAgentChoice {
  executionEnvironment?: "local" | "sandbox";
  /** Empty = a placeholder name; the agent proposes its own in its first chat. */
  name?: string;
  /** Fixed for the agent's life (docs/agent-runtimes.md). */
  runtime?: AgentRuntime;
  /** Required for Hermes / OpenClaw; empty on Jarvis = the last chat seat. */
  provider?: string;
  model?: string;
  /** The subscription login, or a reserved access value on a dual row
   *  (`create/seatChoice.ts`: API key or subscription). */
  accountId?: string;
  /** The small companion bot that follows the agent. */
  companion?: CompanionAppearance;
  /** One line on its job: the structured brief's mission (`society/brief.py`). */
  mission?: string;
}

/**
 * `POST /api/society/agents`: the backend fills in what is left out (a
 * placeholder name, a random companion, the lead chat's current seat). A
 * name that already exists is refused rather than adopting that agent. A
 * failure throws with the server's own reason — there is no stand-in row.
 */
export function useCreateSocietyAgent() {
  const client = useQueryClient();
  return useCallback(async (choice: NewAgentChoice = {}): Promise<SocietyAgent> => {
    const body: Record<string, unknown> = { tier: "specialist" };
    if (choice.executionEnvironment) body.execution_environment = choice.executionEnvironment;
    if (choice.name?.trim()) body.name = choice.name.trim();
    if (choice.runtime && choice.runtime !== "jarvis") body.runtime = choice.runtime;
    if (choice.provider) {
      body.provider = choice.provider;
      if (choice.model) body.model = choice.model;
      if (choice.accountId) body.account_id = choice.accountId;
    }
    if (choice.companion) body.avatar = { companion: choice.companion };
    if (choice.mission?.trim()) body.mission = choice.mission.trim();
    const res = await fetch("/api/society/agents", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(body),
    });
    if (!res.ok) {
      throw requestError((await res.json().catch(() => null)) as { detail?: unknown } | null, `create ${res.status}`);
    }
    const created = (await res.json()) as { agent: SocietyAgentRow; created?: boolean };
    if (created.created === false) throw new AgentNameTaken();
    // The island owes this row an entrance: it walks out of the foundry.
    announceSpawn(created.agent.agent_id);
    const agent = rowToAgent(created.agent);
    patchRoster(client, (agents) => [...agents.filter((a) => a.agentId !== agent.agentId), agent]);
    return agent;
  }, [client]);
}

/**
 * Decide a configuration proposal the agent made in its chat
 * (`POST /api/society/proposals/{id}/resolve`). A yes APPLIES the change on
 * the roster, so the roster query is refreshed afterwards.
 */
export function useResolveProposal() {
  const client = useQueryClient();
  return useCallback(
    async (proposalId: string, approve: boolean, note = ""): Promise<void> => {
      const res = await fetch(`/api/society/proposals/${encodeURIComponent(proposalId)}/resolve`, {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ approve, note }),
      });
      if (!res.ok) throw new Error(`proposal ${res.status}`);
      await client.invalidateQueries({ queryKey: ROSTER_QUERY_KEY });
    },
    [client],
  );
}

/**
 * Edit an agent's standing instructions (`PATCH /api/society/agents/{id}` with
 * the description only). The route keeps the focus and approval rules the
 * agent earned in its chat.
 */
export function useUpdateAgentDescription() {
  const client = useQueryClient();
  return useCallback(
    async (agent: SocietyAgent, description: string): Promise<void> => {
      const sample = SAMPLE_ROSTER.includes(agent);
      if (!sample) {
        const res = await fetch(`/api/society/agents/${encodeURIComponent(agent.agentId)}`, {
          method: "PATCH",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({ description }),
        });
        if (!res.ok) throw new Error(`description ${res.status}`);
      } else {
        agent.description = description;
      }
      await client.invalidateQueries({ queryKey: ROSTER_QUERY_KEY });
    },
    [client],
  );
}

/** The identity an `identity` proposal replaced, as its outcome card carries it. */
export interface PreviousIdentity {
  name: string;
  title: string;
  description: string;
  focus: string[];
  /** Present when the identity also wrote derived approval rules. */
  approval_rules?: { require_approval: string[]; always_allow: string[] };
}

/**
 * Undo an applied `identity` proposal: write the previous name, title,
 * description, focus and approval rules back. The explicit focus keeps the route from
 * re-deriving one from the restored text.
 */
export function useRestoreIdentity() {
  const client = useQueryClient();
  return useCallback(
    async (agentId: string, previous: PreviousIdentity): Promise<void> => {
      const res = await fetch(`/api/society/agents/${encodeURIComponent(agentId)}`, {
        method: "PATCH",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(previous),
      });
      if (!res.ok) throw new Error(`identity ${res.status}`);
      await client.invalidateQueries({ queryKey: ROSTER_QUERY_KEY });
    },
    [client],
  );
}

/** Persist the roster choice and re-seat its canonical chat without losing history. */
export function useUpdateAgentModel() {
  const client = useQueryClient();
  return useCallback(async (agentId: string, choice: {
    provider: string; model: string; effort: string; account_id: string;
    /** Switch the agent loop with the model; omitted keeps the current one. */
    runtime?: AgentRuntime;
  }) => {
    const res = await fetch(`/api/society/agents/${encodeURIComponent(agentId)}/model`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(choice),
    });
    if (!res.ok) {
      // A 422 explains itself; a runtime that cannot drive the provider is typed.
      throw requestError((await res.json().catch(() => null)) as { detail?: unknown } | null, `HTTP ${res.status}`);
    }
    const body = await res.json() as { agent: SocietyAgentRow };
    const updated = rowToAgent(body.agent);
    client.setQueryData<RosterData>(ROSTER_QUERY_KEY, (previous) => previous ? {
      ...previous,
      agents: previous.agents.map((agent) => agent.agentId === agentId ? updated : agent),
    } : previous);
    await client.invalidateQueries({ queryKey: ROSTER_QUERY_KEY });
  }, [client]);
}

/** The knobs the card lets a person turn after the agent exists. */
export interface AgentLimits {
  /** 0 means no cap — the scheduler skips the budget gate entirely. */
  dailyBudgetUsd: number;
  permissionCeiling: PermissionCeiling;
  approvalMode?: AgentApprovalMode | null;
  maxConcurrentRuns: number;
}

/**
 * Change what the agent is allowed to spend, decide and run at once.
 *
 * All three are enforced — the budget by `society/scheduler`, the ceiling by
 * `society/approvals.decide`, the concurrency by the scheduler's run count —
 * and all three were settable only at creation, behind the Advanced
 * disclosure, and never afterwards (maintainer, 2026-09-03). `PATCH` has
 * always accepted them; nothing was calling it with them.
 *
 * The description is deliberately NOT sent along: the route re-derives focus
 * whenever a title or description arrives, so a budget edit must not carry
 * prose with it.
 */
export function useUpdateAgentLimits() {
  const client = useQueryClient();
  return useCallback(
    async (agent: SocietyAgent, limits: AgentLimits): Promise<void> => {
      const sample = SAMPLE_ROSTER.includes(agent);
      const body = {
        daily_budget_usd: Math.max(0, limits.dailyBudgetUsd),
        permission_ceiling: limits.permissionCeiling,
        ...(limits.approvalMode ? { approval_mode: limits.approvalMode } : {}),
        max_concurrent_runs: Math.max(1, Math.round(limits.maxConcurrentRuns)),
      };
      if (!sample) {
        const res = await fetch(`/api/society/agents/${encodeURIComponent(agent.agentId)}`, {
          method: "PATCH",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify(body),
        });
        if (!res.ok) throw new Error(`limits ${res.status}`);
      } else {
        agent.dailyBudgetUsd = body.daily_budget_usd;
        agent.permissionCeiling = body.permission_ceiling;
        if (limits.approvalMode) agent.approvalMode = limits.approvalMode;
        agent.maxConcurrentRuns = body.max_concurrent_runs;
      }
      await client.invalidateQueries({ queryKey: ROSTER_QUERY_KEY });
    },
    [client],
  );
}

/**
 * Any subset of the fields the agent pane edits after creation. Each maps onto
 * one `PATCH /api/society/agents/{id}` field; the roster validators
 * (`society/roster.py::_coerce`) stay the only judge of what is allowed.
 */
export interface AgentConfigPatch {
  name?: string;
  title?: string;
  description?: string;
  grantMode?: GrantMode;
  toolGrants?: string[];
  focus?: string[];
  denies?: string[];
  approvalMode?: AgentApprovalMode;
  permissionCeiling?: PermissionCeiling;
  approvalRules?: ApprovalRules;
  dailyBudgetUsd?: number;
  maxConcurrentRuns?: number;
  browserMode?: BrowserMode;
  browserAllowedDomains?: string[];
}

/** The wire body for a config patch: only the keys that are set, in snake case. */
export function agentConfigBody(patch: AgentConfigPatch): Record<string, unknown> {
  const body: Record<string, unknown> = {};
  if (patch.name !== undefined) body.name = patch.name;
  if (patch.title !== undefined) body.title = patch.title;
  if (patch.description !== undefined) body.description = patch.description;
  if (patch.grantMode !== undefined) body.grant_mode = patch.grantMode;
  if (patch.toolGrants !== undefined) body.grants = patch.toolGrants;
  if (patch.focus !== undefined) body.focus = patch.focus;
  if (patch.denies !== undefined) body.denies = patch.denies;
  if (patch.approvalMode !== undefined) body.approval_mode = patch.approvalMode;
  if (patch.permissionCeiling !== undefined) body.permission_ceiling = patch.permissionCeiling;
  if (patch.approvalRules !== undefined) {
    body.approval_rules = {
      require_approval: patch.approvalRules.requireApproval,
      always_allow: patch.approvalRules.alwaysAllow,
    };
  }
  if (patch.dailyBudgetUsd !== undefined) body.daily_budget_usd = Math.max(0, patch.dailyBudgetUsd);
  if (patch.maxConcurrentRuns !== undefined) body.max_concurrent_runs = Math.max(1, Math.round(patch.maxConcurrentRuns));
  if (patch.browserMode !== undefined) body.browser_mode = patch.browserMode;
  if (patch.browserAllowedDomains !== undefined) body.browser_allowed_domains = patch.browserAllowedDomains;
  return body;
}

/**
 * Save part of an agent's configuration from the agent pane. The updated row
 * goes straight into the roster cache, so the pane never flickers back to
 * the old value while the refetch runs. Sample rows change in place.
 */
export function useUpdateAgentConfig() {
  const client = useQueryClient();
  return useCallback(
    async (agent: SocietyAgent, patch: AgentConfigPatch): Promise<SocietyAgent> => {
      const body = agentConfigBody(patch);
      if (Object.keys(body).length === 0) return agent;
      if (SAMPLE_ROSTER.includes(agent)) {
        Object.assign(agent, patch);
        await client.invalidateQueries({ queryKey: ROSTER_QUERY_KEY });
        return agent;
      }
      const res = await fetch(`/api/society/agents/${encodeURIComponent(agent.agentId)}`, {
        method: "PATCH",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(body),
      });
      if (!res.ok) {
        throw requestError((await res.json().catch(() => null)) as { detail?: unknown } | null, `HTTP ${res.status}`);
      }
      const updated = rowToAgent(((await res.json()) as { agent: SocietyAgentRow }).agent);
      const merged = { ...updated, state: agent.state, routines: agent.routines };
      client.setQueryData<RosterData>(ROSTER_QUERY_KEY, (prev) => prev ? {
        ...prev,
        agents: prev.agents.map((row) => (row.agentId === agent.agentId ? { ...merged, name: row.tier === "lead" ? row.name : merged.name } : row)),
      } : prev);
      void client.invalidateQueries({ queryKey: ROSTER_QUERY_KEY });
      return merged;
    },
    [client],
  );
}

/** Move an agent to another computer (`PATCH computer_id`); "" = this computer. */
export function useSetAgentComputer() {
  const client = useQueryClient();
  return useCallback(
    async (agent: SocietyAgent, computerId: string): Promise<void> => {
      const sample = SAMPLE_ROSTER.includes(agent);
      if (sample) {
        agent.computerId = computerId || null;
      } else {
        const res = await fetch(`/api/society/agents/${encodeURIComponent(agent.agentId)}`, {
          method: "PATCH",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({ computer_id: computerId }),
        });
        if (!res.ok) {
          const detail = (await res.json().catch(() => null)) as { detail?: unknown } | null;
          const inner = detail?.detail as { detail?: unknown } | string | undefined;
          const message = typeof inner === "string" ? inner : typeof inner?.detail === "string" ? inner.detail : "";
          throw new Error(message || `computer ${res.status}`);
        }
      }
      await client.invalidateQueries({ queryKey: ROSTER_QUERY_KEY });
    },
    [client],
  );
}

/** Pause or resume an agent (`PATCH /api/society/agents/{id}`); sample rows flip locally. */
export function useSetAgentPaused() {
  const client = useQueryClient();
  return useCallback(
    async (agent: SocietyAgent, paused: boolean): Promise<void> => {
      const sample = SAMPLE_ROSTER.includes(agent);
      if (!sample) {
        const res = await fetch(`/api/society/agents/${encodeURIComponent(agent.agentId)}`, {
          method: "PATCH",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({ state: paused ? "paused" : "active" }),
        });
        if (!res.ok) throw new Error(`pause ${res.status}`);
      } else {
        agent.state = paused ? "paused" : "idle";
        agent.lifecycle = paused ? "paused" : "active";
      }
      await client.invalidateQueries({ queryKey: ROSTER_QUERY_KEY });
    },
    [client],
  );
}

/**
 * Retire an agent: `DELETE /api/society/agents/{id}` archives the row, and the
 * island plays the execution (`world/retirement.ts`) before the roster is
 * refreshed and the figure leaves.
 *
 * The order matters. The deletion goes FIRST, so a refusal — the lead cannot
 * be archived, the backend is down — is an error the person sees straight
 * away, instead of a twenty-second ceremony ending in a figure that quietly
 * comes back. The roster query is invalidated only when the body is in the
 * mine; the refetch interval is paused for the same reason.
 */
export function useRetireAgent() {
  const client = useQueryClient();
  return useCallback(
    async (agent: SocietyAgent, executioner: SocietyAgent | null): Promise<void> => {
      if (agent.tier === "lead") throw new Error("the lead cannot be retired");
      const sample = SAMPLE_ROSTER.includes(agent);
      if (sample) {
        RETIRED_SAMPLE.add(agent.agentId);
      } else {
        const res = await fetch(`/api/society/agents/${encodeURIComponent(agent.agentId)}`, {
          method: "DELETE",
        });
        if (!res.ok) {
          const detail = (await res.json().catch(() => null)) as { detail?: unknown } | null;
          const reason =
            detail && typeof detail.detail === "object" && detail.detail !== null
              ? String((detail.detail as { reason?: unknown }).reason ?? res.status)
              : String(detail?.detail ?? res.status);
          throw new Error(`retire ${reason}`);
        }
      }
      // No island on screen (the Agents ledger, the office): there is no
      // ceremony to wait for, so the row leaves the rail now.
      if (sample || !retirementStageMounted()) {
        patchRoster(client, (agents) => agents.filter((a) => a.agentId !== agent.agentId));
        if (sample) await client.invalidateQueries({ queryKey: ROSTER_QUERY_KEY });
        return;
      }
      const started = beginRetirement({
        agentId: agent.agentId,
        name: agent.name,
        figure: agent.figure,
        palette: agent.palette,
        executionerId: executioner?.agentId ?? null,
        commit: () => {
          void client.invalidateQueries({ queryKey: ROSTER_QUERY_KEY });
        },
      });
      // Another retirement already has the lead: the row is gone either way,
      // so the roster is refreshed at once rather than waiting for a ceremony
      // that will never run for this agent.
      if (!started) await client.invalidateQueries({ queryKey: ROSTER_QUERY_KEY });
    },
    [client],
  );
}

/** The one lead of the society (MASTERPLAN §2.5) — the agent who carries out a retirement. */
export function findLead(agents: readonly SocietyAgent[]): SocietyAgent | null {
  return agents.find((a) => a.tier === "lead") ?? null;
}