Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

46.1 KB
Raw Blame
// SPDX-License-Identifier: FSL-1.1-MIT
// Copyright (c) 2026 Cua AI, Inc.

// The app's root model (the SwiftUI app's AppModel.swift): the core's Space
// list state, the creates, deletes and power changes running, the account
// and the app settings. It forwards every decision to the app core
// (`appRosterReduce`, `appSidebar`, `appSpaceDetail`, `appCreatesReduce`,
// ...); the bridge answers the page from what it exposes. Each change says
// what moved (`subscribe`), so the bridge tells the page.
import { randomUUID } from "node:crypto";
import type { Native } from "../native/load";
import type {
  AppCliInstallPlan,
  AppCreateAction,
  AppCreatesState,
  AppRuntimeKindInput,
  AppRuntimesInput,
  AppCreateSpaceArgs,
  AppGpuChoice,
  AppLocalStorage,
  AppMachinePageRow,
  AppMachinesPageInput,
  AppMachinesView,
  AppMachinesViewAbout,
  AppMachinesViewHealth,
  AppMainChrome,
  AppRosterAction,
  AppRosterState,
  AppSettings,
  AppSidebarView,
  AppSignInPhase,
  AppSpace,
  AppSpaceDetail,
  AppSpaceHost,
  AppSpaceOs,
  AppSpaceUsage,
  AppWizardEnv,
  LocalStorage,
  SpaceCreateProgress,
} from "../native/generated/index";
import type { BulkResult, CreatingElsewhereRow, ListRead, LocalRuntimes, SpacesBackend } from "./backend";

/** How many Spaces a bulk delete or leave works on at the same time. */
const BULK_CONCURRENCY = 3;
import { AgentsModel, type AgentSetupRunning, type ToolRunner } from "./agents";
import { ChecklistModel } from "./checklist";
import type { CloudModel } from "./cloud";
import type { DevicesModel } from "./devices";
import { TeamsModel } from "./teams";
import { NotchActivity } from "./activity";
import { createErrorVariant, daemonAnswered, isCancelled, sdkErrorKind, words } from "./errors";
import type { HostModel } from "./host";
import { brokerWords, KeyvaultModel } from "./keyvault";
import type { AccountRunning, SignInOptions, TelemetryRunning } from "./services";
import type { StartupModel } from "./startup";
import { SignInRuns, type SignInEnd } from "./sign-in";
import { SpaceThumbnails } from "./thumbnails";
import { noLocalRuntimeReason } from "./local-runtime";
import type { LinuxRuntime } from "./linux-runtime";
import type { RuntimeFix, RuntimeFixAction, WindowsRuntime, WindowsRuntimeAdvice } from "./windows-runtime";
import { TimeoutError, sleep, withTimeout } from "./time";

/** What changed, for the page's events. */
export type ModelChange = "spaces" | "session" | "settings" | "machines" | "agents" | "keyvault" | "checklist" | "update";

export interface AppModelDeps {
  native: Native;
  backend: SpacesBackend;
  startup: StartupModel;
  settingsPath: string;
  host: HostModel;
  devices: DevicesModel;
  cloud: CloudModel;
  account: AccountRunning | null;
  telemetry: TelemetryRunning | null;
  /** The coding agents on this machine (null: none can be set up in this build). */
  agentSetup?: AgentSetupRunning | null;
  /** The daemon's tools for the agents and their keys (default: the backend's; null: no daemon). */
  agentTools?: ToolRunner | null;
  /** The Keyvault (a model without a broker when omitted). */
  keyvault?: KeyvaultModel;
  /** The live services are in (the launch's stand-ins have them). */
  servicesIn: () => boolean;
  /** CPUs this machine has (the wizard's limit). */
  cpus: number;
  /** Where the getting-started checklist is kept (null: in memory). */
  checklistPath?: string | null;
  /** A checklist to start from when there is none yet (the SwiftUI app's, on macOS). */
  checklistSeedPath?: string | null;
  /** The system this app runs on (names This machine on the Machines page). */
  hostOs?: AppSpaceOs;
  /** What New Space offers when Windows has no local runtime (null elsewhere). */
  windowsRuntime?: WindowsRuntime | null;
  /** Linux: restarts the app under the docker group when a new install needs it (null elsewhere). */
  linuxRuntime?: LinuxRuntime | null;
  /** This computer's name, the app's version and the system's (This machine's Details on the Machines page). */
  about?: () => AppMachinesViewAbout;
}

/** The create the daemon never took. */
export class CreateNotAccepted extends Error {
  constructor(seconds: number) {
    super(`Cua's background service didn't start this create within ${seconds} s. Cua Spaces reconnected to it. Try again.`);
    this.name = "CreateNotAccepted";
  }
}

const TICK_MS = 250;
/** How often the refresh checks relay sharing against the sign-in (ms). */
const ACCOUNT_CHECK_MS = 120_000;

export class AppModel {
  readonly native: Native;
  readonly backend: SpacesBackend;
  readonly startup: StartupModel;
  readonly host: HostModel;
  readonly devices: DevicesModel;
  /** Settings, Teams: the account's organizations from the Spaces API. */
  readonly teams: TeamsModel;
  readonly cloud: CloudModel;
  readonly account: AccountRunning | null;
  readonly telemetry: TelemetryRunning | null;
  /** Agents: the persistent agents, a Space's runs, the provider keys and the coding agents. */
  readonly agents: AgentsModel;
  readonly keyvault: KeyvaultModel;
  /** The getting-started checklist: the app's one copy. */
  readonly checklist: ChecklistModel;
  /** Agent runs seen in any Space (the page's Agents page): "Run an agent". */
  agentRunsSeen = 0;
  /** This computer, for This machine's Details (`AppModelDeps.about`). */
  readonly about: (() => AppMachinesViewAbout) | undefined;
  readonly hostOs: AppSpaceOs;
  readonly settingsPath: string;
  settings: AppSettings;
  roster: AppRosterState;
  /** The selected Space (the sidebar's selection). */
  selectedSpaceId: string | null = null;
  /** The bundled CLI's install plan as last read (`cli.plan`, `settings.get`'s `cli`). */
  cliPlan: AppCliInstallPlan | null = null;
  loaded = false;
  /** The last list read failed: the page says so above every page, and the
   * rows already listed stay (the SwiftUI app's `rosterError`). Never the
   * error's own words: they may carry a server's body or a credential-bearing URL. */
  rosterError: string | null = null;
  /**
   * Another app keeps its own daemon running and this app uses it (the
   * supervisor yields to it after replacing it once): one line above every
   * page while it lasts. Null otherwise.
   */
  daemonNotice: string | null = null;
  banner: string | null = null;
  bannerIsError = false;
  /** The signed-in account. */
  identity: string | null;
  /** A Cua account is signed in (or client credentials are set). */
  cloudConfigured = false;
  signIn: AppSignInPhase;
  /** The page a waiting sign-in finishes on. */
  signInUrl: string | null = null;
  /** Sign-in attempts, a retry's included; the first one to store a session signs the app in. */
  readonly signIns: SignInRuns;
  /** The registry's Spaces (before This machine is added). */
  registrySpaces: AppSpace[] = [];
  /** Memory and storage use of the Spaces whose detail is showing. */
  readonly usage = new Map<string, AppSpaceUsage>();
  /** Each Space's latest thumbnail (the notch tiles and the page's previews). */
  readonly thumbnails: SpaceThumbnails;
  /** The notch's indicator: the hotspot, a transfer running (a teleport, files dropped on the notch). */
  readonly activity = new NotchActivity();
  creates: AppCreatesState = { pending: [], deleting: [], powering: [], streaks: [] };
  /** macOS VMs running on this Mac, as New Space last read them. */
  runningMacosVms: number | null = null;
  lumeSource: string | null = null;
  linuxSource: string | null = null;
  /** Settings, Advanced: each kind of Space's runtime setting and what is installed for it, once read. */
  runtimeKinds: AppRuntimeKindInput[] | null = null;
  /** Settings, Advanced: the runtime doctor's answer, once read. */
  runtimeDoctor: LocalRuntimes | null = null;
  /** Your machines that provide Spaces, as New Space last read them. */
  hosts: AppSpaceHost[] = [];
  /** Windows or Linux without a usable local runtime: why, and the fix New Space offers (null when one is ready). */
  runtimeAdvice: WindowsRuntimeAdvice | null = null;
  readonly windowsRuntime: WindowsRuntime | null;
  readonly linuxRuntime: LinuxRuntime | null;
  /** How long one read of the registry may take before a refresh gives up on it (s). */
  listTimeout = 20;
  /** How long the host's own reads in a refresh may take (s). */
  hostTimeout = 15;
  /** With no successful list read for this long, the daemon connection is made again (ms). */
  listStaleAfterMs = 45_000;
  lastListOk: number | null = null;
  /** The last time the daemon answered a list read, with the rows or with an error of its own
   * (the relay refused the sign-in, say): the connection works, so a new one would not help. */
  lastListAnswer: number | null = null;
  /** Makes a new SDK client of the daemon; false when it could not (set once the services are in). */
  reconnect: (() => Promise<boolean>) | null = null;
  reconnecting = false;
  reconnects = 0;
  /** How long the daemon may take to take a create (s). */
  createAcceptTimeout = 30;
  readonly maxCpus: number;
  readonly servicesInNow: () => boolean;

  private listWatchedSince = Date.now();
  private rowsInFlight: Promise<ListRead> | null = null;
  private hostInFlight: Promise<void> | null = null;
  private createTicker: NodeJS.Timeout | null = null;
  private readonly listeners = new Set<(change: ModelChange) => void>();
  /** The persistent agents were read for the checklist. */
  private checklistReadAgents = false;

  constructor(d: AppModelDeps) {
    this.native = d.native;
    this.backend = d.backend;
    this.startup = d.startup;
    this.host = d.host;
    this.devices = d.devices;
    this.teams = new TeamsModel({
      native: d.native as unknown as ConstructorParameters<typeof TeamsModel>[0]["native"],
      account: () => this.account,
      identity: () => this.identity,
      currentTeamId: () => this.settings.currentTeamId,
    });
    this.teams.onChange = () => this.changed("settings");
    this.cloud = d.cloud;
    this.account = d.account;
    this.telemetry = d.telemetry;
    this.agents = new AgentsModel(
      d.native,
      d.agentTools === undefined ? (tool, args) => d.backend.tool(tool, args) : d.agentTools,
      d.agentSetup ?? null,
      (id) => d.backend.agentRuns(id),
    );
    this.agents.onChange(() => this.changed("agents"));
    this.agents.persistent.thisMachine = () => {
      const id = this.sidebar.thisMachine?.id;
      return id?.startsWith("relay:") ? id : null;
    };
    this.keyvault = d.keyvault ?? new KeyvaultModel(d.native, null);
    this.hostOs = d.hostOs ?? d.native.AppSpaceOs.Macos;
    this.about = d.about;
    this.checklist = new ChecklistModel(d.native, d.checklistPath ?? null, d.checklistSeedPath ?? null);
    this.checklist.telemetry = d.telemetry;
    this.checklist.onChange = () => this.changed("checklist");
    this.settingsPath = d.settingsPath;
    this.windowsRuntime = d.windowsRuntime ?? null;
    this.linuxRuntime = d.linuxRuntime ?? null;
    this.servicesInNow = d.servicesIn;
    this.maxCpus = Math.max(2, Math.min(16, d.cpus));
    this.settings = d.native.appSettingsLoad(d.settingsPath);
    const policy = d.native.appThumbnailPolicy();
    this.thumbnails = new SpaceThumbnails(
      { openIntervalMs: Number(policy.openIntervalMs), backgroundIntervalMs: Number(policy.backgroundIntervalMs), maxDimension: policy.maxDimension },
      (id, maxAgeMs) => this.backend.thumbnail(id, maxAgeMs),
    );
    // Live Keyvault sign-ins show left of the notch (and in the menu).
    // Dismissed ones stay hidden across launches (the settings file).
    this.keyvault.dismissed = this.settings.dismissedAccess;
    this.keyvault.onDismissed = (ids) => {
      this.settings.dismissedAccess = ids;
      this.saveSettings();
    };
    this.keyvault.subscribe(() => this.changed("keyvault"));
    this.roster = d.native.appRosterInitial([]);
    this.signIn = new d.native.AppSignInPhase.Idle();
    this.signIns = new SignInRuns(() => this.account, {
      starting: () => this.setSignIn(new this.native.AppSignInPhase.Starting()),
      waiting: (attempt) => {
        this.signInUrl = attempt.url;
        this.setSignIn(new this.native.AppSignInPhase.Waiting({ userCode: attempt.userCode ?? undefined, opened: attempt.opened !== false }));
      },
      signedIn: (who) => this.signedIn(who),
      failed: (end) => this.signInFailed(end),
    });
    this.identity = d.account?.identity() ?? null;
    this.host.identity = this.identity;
    this.devices.signedIn = this.identity !== null;
    // An approval (the minute's read, Check again) reaches an open detail: its access
    // notice goes and its stream opens without reopening it.
    this.devices.onChange = () => this.changed("machines");
    this.host.onChange = () => {
      this.recompose();
      this.changed("machines");
    };
    // "Set up for access" signs in inline when relay setup has no account.
    this.host.signIn = async () => {
      await this.beginSignIn();
      return this.signInPhase() !== "Failed" && this.identity !== null;
    };
    // Which experiments are on: the day's `cua_app_active` carries them.
    this.telemetry?.record(d.native.appTelemetryExperimentsOn(this.settings.experiments));
  }

  /** Called with what changed; returns the unsubscribe. */
  subscribe(listener: (change: ModelChange) => void): () => void {
    this.listeners.add(listener);
    return () => this.listeners.delete(listener);
  }

  changed(change: ModelChange): void {
    for (const l of [...this.listeners]) l(change);
    // The checklist ticks off what the app shows, whichever page is open.
    if (change === "spaces" || change === "machines" || change === "agents") this.observeChecklist();
  }

  // MARK: Machines and getting started

  /** What each machine sharing its desktop (`relay:<machine>`) reported as its hostname, by machine id. */
  get relayHostnames(): Record<string, string> {
    const out: Record<string, string> = {};
    for (const space of this.spaces) {
      if (!space.id.startsWith("relay:") || space.host) continue;
      const name = this.backend.reportedHostname(space.id);
      if (name) out[space.id.slice(6)] = name;
    }
    return out;
  }

  /** What the Machines page is listed from (`machines::MachinesPageInput`), the same for its rows and its view. */
  get machinesPageInput(): AppMachinesPageInput {
    const devices = this.devices.snapshot === null ? [] : this.devices.input.devices.map((d) => ({
      id: d.id,
      name: d.name,
      platform: d.platform ?? "",
      lastSeen: d.lastSeen,
      current: d.current,
      state: d.state,
    }));
    return {
      hostOs: this.hostOs,
      spaces: this.spaces,
      devices,
      hostnames: new Map(Object.entries(this.relayHostnames)),
      presence: new Map(Object.entries(this.devices.relayOnline)),
      now: BigInt(Math.floor(Date.now() / 1000)),
    };
  }

  /** The Machines page's rows (the core's `machines::page_rows`, as the SwiftUI app lists them). */
  get machinePageRows(): AppMachinePageRow[] {
    return this.native.appMachinesPageRows(this.machinesPageInput);
  }

  /**
   * The Machines page as every UI draws it (the core's `machines_view::view`,
   * as the SwiftUI page draws it): the rows in groups with one line each,
   * and each machine's page, from the rows, what the relay says of each
   * machine (whose it is, its team, whether it provides Spaces), This
   * machine's host state and this computer.
   */
  /** What each machine's daemon last said of its health, by row id (`machines.checkHealth`). */
  readonly machineHealth = new Map<string, AppMachinesViewHealth>();

  get machinesView(): AppMachinesView {
    const relay = (this.devices.snapshot?.machines ?? []).map((m) => ({
      id: m.id,
      role: m.role,
      team: m.team,
      ownerEmail: m.ownerEmail,
      providesSpaces: m.providesSpaces,
    }));
    return this.native.appMachinesView({
      page: this.machinesPageInput,
      relay,
      host: this.host.state ?? undefined,
      about: this.about?.() ?? { computerName: "", appVersion: "", osVersion: "" },
      rows: undefined,
      health: [...this.machineHealth.values()],
      // The Electron app has no in-app Mac update to offer on this page.
      appUpdate: "",
      appUpdateReady: false,
    });
  }

  /** A Space's agent runs were read: `count` of them. */
  sawAgentRuns(count: number): void {
    if (count <= this.agentRunsSeen) return;
    this.agentRunsSeen = count;
    this.observeChecklist();
  }

  /** Ticks the checklist off from what the app shows (the core's one reading). */
  observeChecklist(): void {
    const seen = this.native.appChecklistObserveFrom({
      machines: this.machinePageRows,
      spaces: this.spaces,
      agents: this.agents.persistent.count,
      runs: this.agentRunsSeen,
    });
    this.checklist.send(seen);
  }

  get servicesIn(): boolean {
    return this.servicesInNow();
  }

  /** This machine's CPU architecture, as the image catalog spells it. */
  get hostArch(): string {
    return this.native.appHostArch();
  }

  static nowMs(): bigint {
    return BigInt(Date.now());
  }

  // MARK: Launch

  /** The live services are in: read the account again and show what the backend has. */
  attachLive(): void {
    this.identity = this.account?.identity() ?? null;
    this.host.identity = this.identity;
    this.devices.signedIn = this.identity !== null;
    this.listWatchedSince = Date.now();
    this.changed("session");
    void (async () => {
      await this.refresh();
      await this.keyvault.refresh();
      await this.devices.refresh();
      this.changed("machines");
    })();
  }

  // MARK: Space list

  get spaces(): AppSpace[] {
    return this.roster.spaces;
  }

  get sidebar(): AppSidebarView {
    return this.native.appSidebar(this.spaces, "", this.selectedSpaceId ?? "");
  }

  /** The detail as this machine sees it (`appSpaceDetailFor`). */
  detail(space: AppSpace): AppSpaceDetail {
    return this.native.appSpaceDetailFor(space, this.usage.get(space.id), this.hostArch, this.devices.accessNotice);
  }

  /** The Spaces whose desktop can be shown (running and reachable). */
  get streamableSpaceIds(): string[] {
    return this.spaces.filter((s) => this.detail(s).canStream).map((s) => s.id);
  }

  isDeleting(id: string): boolean {
    return this.native.appCreatesIsDeleting(this.creates, id);
  }

  send(action: AppRosterAction): void {
    this.roster = this.native.appRosterReduce(this.roster, action);
    this.changed("spaces");
  }

  select(id: string): void {
    this.selectedSpaceId = id;
    this.send(new this.native.AppRosterAction.Select({ id, now: AppModel.nowMs() }));
  }

  /** The roster: This machine first (when this app manages a host), then the registry's Spaces. */
  recompose(): void {
    const listed = this.native.appWithoutThisRelayMachine(this.registrySpaces, this.host.machineId ?? undefined);
    const spaces = this.host.host === null ? listed : this.native.appWithThisMachine(listed, this.host.summaryInput, AppModel.nowMs());
    this.send(new this.native.AppRosterAction.SyncSpaces({ spaces: this.native.appCreatesCompose(spaces, this.creates) }));
  }

  /** Advances the Spaces being created and redraws the list. */
  sendCreate(action: AppCreateAction): void {
    // A create started, reached ready or failed (how long it took).
    this.telemetry?.record(this.native.appTelemetryCreates(this.creates, action, AppModel.nowMs()));
    this.creates = this.native.appCreatesReduce(this.creates, action);
    this.recompose();
    this.tickWhileCreating();
  }

  /** While a create runs, time moves its bar within a phase that reports no fraction (4 Hz). */
  private tickWhileCreating(): void {
    const running = this.creates.pending.some((p) => p.error === undefined && p.spaceId === undefined);
    if (!running) {
      if (this.createTicker) clearInterval(this.createTicker);
      this.createTicker = null;
      return;
    }
    if (this.createTicker) return;
    this.createTicker = setInterval(() => {
      const now = AppModel.nowMs();
      const tick = new this.native.AppCreateAction.Tick({ now });
      this.telemetry?.record(this.native.appTelemetryCreates(this.creates, tick, now));
      this.creates = this.native.appCreatesReduce(this.creates, tick);
      this.recompose();
      if (!this.creates.pending.some((p) => p.error === undefined && p.spaceId === undefined)) this.tickWhileCreating();
    }, TICK_MS);
    this.createTicker.unref?.();
  }

  // MARK: List health

  /** The list poll: a bounded refresh every `intervalMs`, and a reconnect when the list went stale. Returns the stop. */
  startListPoll(intervalMs = 10_000): () => void {
    let stopped = false;
    void (async () => {
      while (!stopped) {
        await this.refresh();
        await this.checkListHealth();
        await sleep(intervalMs);
      }
    })();
    return () => {
      stopped = true;
    };
  }

  async checkListHealth(now = Date.now()): Promise<void> {
    if (!this.startup.isReady || !this.reconnect || this.reconnecting) return;
    const since = Math.max(this.lastListAnswer ?? this.listWatchedSince, this.listWatchedSince);
    if (now - since < this.listStaleAfterMs) return;
    await this.reconnectNow(`the Space list was not read for ${Math.round((now - since) / 1000)} s`);
  }

  /** A new connection to the daemon, then the list again. One at a time. */
  async reconnectNow(reason: string): Promise<void> {
    if (!this.reconnect || this.reconnecting) return;
    this.reconnecting = true;
    this.reconnects += 1;
    console.log(`[cua-spaces] reconnecting to the cua daemon (${reason})`);
    this.rowsInFlight = null;
    this.hostInFlight = null;
    const ok = await this.reconnect();
    this.listWatchedSince = Date.now();
    this.reconnecting = false;
    if (!ok) console.log("[cua-spaces] the reconnect did not make a new connection; trying again later");
    await this.refresh();
  }

  private async refreshHost(): Promise<void> {
    const task =
      this.hostInFlight ??
      (async () => {
        await this.host.refresh();
        // A session that expired for good (or `cua auth logout`) pauses relay sharing too.
        await this.host.reconcileAccount(ACCOUNT_CHECK_MS);
      })();
    this.hostInFlight = task;
    const done = await withTimeout(this.hostTimeout, () => task);
    if (done.ok && this.hostInFlight === task) this.hostInFlight = null;
  }

  private async readRows(): Promise<ListRead> {
    const b = this.backend;
    const task: Promise<ListRead> =
      this.rowsInFlight ?? (b.listRead ? b.listRead() : b.rows().then((rows) => ({ rows, incomplete: false, reaching: 0, stale: false, notice: "" })));
    this.rowsInFlight = task;
    const result = await withTimeout(this.listTimeout, () => task);
    // Still running after the timeout: the next refresh waits on the same read.
    if (!(!result.ok && result.error instanceof TimeoutError) && this.rowsInFlight === task) this.rowsInFlight = null;
    if (!result.ok) throw result.error;
    return result.value;
  }

  /** Re-reads the registry (and the host). Bounded: a daemon that stopped answering leaves the list as it was. */
  async refresh(): Promise<void> {
    // The host's reads run beside the list, never ahead of it: this machine's
    // Spaces show as soon as the daemon lists them ("This machine" joins
    // them once the host answered).
    const hostRead = this.refreshHost().then(() => {
      if (this.loaded) {
        this.recompose();
        this.changed("spaces");
      }
    });
    const cloudRead = this.backend
      .loggedIn()
      .catch(() => false)
      .then((cloud) => {
        if (cloud !== this.cloudConfigured) {
          this.cloudConfigured = cloud;
          this.changed("session");
        }
      });
    try {
      await this.readList();
    } finally {
      await Promise.all([hostRead, cloudRead]);
      this.followIncompleteList();
    }
  }

  /** Some of your computers have not answered the last read yet (`Spaces.list_known`). */
  listIncomplete = false;
  /** How many computers are still being reached (0 when unknown). */
  listReaching = 0;
  /** Your other computers' rows are the last known ones, not yet confirmed. */
  listStale = false;
  /** What your other computers are creating now (CUA-2349), less what the list already has a row for. */
  creatingElsewhere: CreatingElsewhereRow[] = [];
  private incompleteFollow: ReturnType<typeof setTimeout> | null = null;

  /** While some of your computers have not answered, the list is read again
   * soon (each read waits a moment for them in the daemon), not at the next poll. */
  private followIncompleteList(): void {
    if (!this.listIncomplete || this.incompleteFollow) return;
    this.incompleteFollow = setTimeout(() => {
      this.incompleteFollow = null;
      void this.refresh();
    }, 500);
  }

  private async readList(): Promise<void> {
    try {
      const list = await this.readRows();
      this.lastListOk = Date.now();
      this.lastListAnswer = this.lastListOk;
      this.registrySpaces = this.native.appRowsToSpaces(list.rows, AppModel.nowMs());
      this.creates = this.native.appCreatesSettle(this.creates, this.registrySpaces);
      this.recompose();
      this.loaded = true;
      this.listIncomplete = list.incomplete;
      this.listReaching = list.reaching;
      this.listStale = list.stale;
      // A create this app asked another computer for already has its own pending row.
      this.creatingElsewhere = (list.creating ?? []).filter((c) => !this.spaces.some((s) => s.name === c.name && (s.host === c.machineId || this.native.appCreatesIsPending(s.id))));
      // Your other computers could not be read: their rows are the last known ones.
      this.rosterError = list.notice ? "Could not reach your other machines. Their Spaces may be out of date." : null;
      if (this.selectedSpaceId === null) {
        const first = this.sidebar.selectedId;
        if (first) this.selectedSpaceId = first;
      }
      this.changed("spaces");
      // The checklist's "Run an agent": read the persistent agents once
      // (the Agents page reads them again when it opens).
      const runAgent = this.native.AppChecklistStep.RunAgent;
      if (!this.checklistReadAgents && this.servicesIn && !this.checklist.state.done.includes(runAgent)) {
        this.checklistReadAgents = true;
        void this.agents.persistent.load();
      }
      // A preview for every running Space from the daemon's cache (it
      // survives restarts), before one is opened.
      void this.thumbnails.warm(this.streamableSpaceIds);
    } catch (error) {
      // The daemon is slow or gone: the list stays, and the poll reconnects when it stays stale.
      if (error instanceof TimeoutError) return;
      if (daemonAnswered(error)) this.lastListAnswer = Date.now();
      this.rosterError = this.loaded ? "Could not refresh Spaces. Previously loaded rows may be out of date." : "Could not load Spaces. Try refreshing again.";
      this.changed("spaces");
    }
  }

  // MARK: New Space

  /** What Settings, Advanced's runtime rows are laid out from (`runtimes::section`): every kind, "Checking…" until read. */
  runtimesInput(): AppRuntimesInput {
    const os = process.platform === "darwin" ? this.native.AppSpaceOs.Macos : process.platform === "win32" ? this.native.AppSpaceOs.Windows : this.native.AppSpaceOs.Linux;
    return {
      os,
      hostArch: this.hostArch,
      localBackends: this.runtimeDoctor?.ready,
      localDetails: this.runtimeDoctor?.details,
      kinds: this.runtimeKinds ?? undefined,
    };
  }

  /** Reads each kind's runtime setting, what is installed and the doctor (each bounded). */
  async readRuntimes(): Promise<void> {
    const [doctor, kinds] = await Promise.all([this.backend.localRuntimes(), this.backend.runtimeKinds()]);
    if (doctor) this.runtimeDoctor = doctor;
    if (kinds) this.runtimeKinds = kinds;
  }

  /** What New Space knows, freshly probed; every probe runs at once and each is bounded. */
  async newSpaceEnv(): Promise<AppWizardEnv> {
    const b = this.backend;
    const [runtimes, storage, gpus, hosts, lume, linux, vms] = await Promise.all([
      b.localRuntimes(),
      b.localStorage(),
      b.gpuChoices(),
      b.hosts(),
      b.lumeSource(),
      b.linuxSource(),
      b.runningMacosVms(),
      this.cloud.refresh(),
    ]);
    this.lumeSource = lume;
    this.linuxSource = linux;
    this.hosts = hosts;
    // Lume did not answer this time: keep what it said last.
    this.runningMacosVms = vms ?? this.runningMacosVms;
    await this.readRuntimeAdvice(runtimes);
    return this.wizardEnv(runtimes, storage, gpus);
  }

  /** Windows: what is missing for local Spaces and what the app can do about it. */
  private async readRuntimeAdvice(runtimes: LocalRuntimes | null): Promise<void> {
    if (!runtimes) return;
    const w = this.windowsRuntime;
    if (runtimes.ready.length > 0) {
      w?.settled();
      this.runtimeAdvice = null;
      return;
    }
    // Windows: Cua Spaces runs its own engine in WSL 2; when the doctor says why it cannot
    // (WSL 2 or virtualization off), that is the reason, not "install Docker Desktop".
    const managed = runtimes.details.get("managed");
    if (process.platform === "win32" && managed && /wsl|virtual|not enough disk space/i.test(managed)) {
      this.runtimeAdvice = {
        reason: managed,
        fix: /Windows feature|wsl --install/i.test(managed)
          ? { action: "open-windows-features", label: "Open Windows Features", note: "Turn on the listed features, then restart this PC and open Cua Spaces again.", busy: false, termsUrl: null }
          : null,
      };
      return;
    }
    if (w) this.runtimeAdvice = await w.advice().catch(() => null);
    else if (this.linuxRuntime) this.runtimeAdvice = await this.linuxRuntime.advice(runtimes.details.get("container")).catch(() => null);
  }

  /** The fix New Space shows beside the reason (Windows only). */
  get runtimeFix(): RuntimeFix | null {
    return this.runtimeAdvice?.fix ?? null;
  }

  /** Runs a New Space fix ("Install Docker Desktop"); the page reads the options again to follow it. */
  fixRuntime(action: RuntimeFixAction): void {
    if (action === "restart-app") {
      if (!this.linuxRuntime) throw new Error("no runtime fix on this system");
      this.linuxRuntime.run(action);
      return;
    }
    if (!this.windowsRuntime) throw new Error("no runtime fix on this system");
    this.windowsRuntime.run(action);
  }

  /** What New Space knows without asking anything: the env while the live services are still starting. */
  knownNewSpaceEnv(): AppWizardEnv {
    return this.wizardEnv(null, null, null);
  }

  private wizardEnv(runtimes: LocalRuntimes | null, storage: LocalStorage | null, gpus: AppGpuChoice[] | null): AppWizardEnv {
    const backends = runtimes?.ready;
    const env: AppWizardEnv = {
      defaultLocation: this.settings.defaultLocation,
      localAvailable: backends ? backends.length > 0 : true,
      localReason:
        backends && backends.length === 0
          ? (this.runtimeAdvice?.reason ?? noLocalRuntimeReason(process.platform, runtimes?.details.get("container"), runtimes?.details.get("managed")))
          : undefined,
      localBackends: backends,
      localDetails: runtimes?.details,
      maxCpus: this.maxCpus,
      hostArch: this.hostArch,
      lumeSource: this.lumeSource ?? undefined,
      linuxSource: this.linuxSource ?? undefined,
      storage: storage ? wizardStorage(storage) : undefined,
      clouds: this.cloud.clouds,
      hosts: this.hosts,
      experiments: this.settings.experiments,
      gpus: gpus ?? undefined,
    };
    if (this.native.appIsCloudWord(this.cloud.defaultOn ?? "")) env.defaultLocation = this.native.AppLocation.Yours;
    return env;
  }

  /**
   * Runs a create from the core's create arguments (New UI's `spaces.create`,
   * with the page's pending id): the pending row shows at once, follows the
   * SDK's progress (also handed to `progress`) and hands over to the
   * registry's row when it is ready; returns its id. A failure stays on its
   * row; a cancelled create only leaves the list. Either way it throws.
   */
  async runCreate(
    args: AppCreateSpaceArgs,
    os: AppSpaceOs,
    pendingId: string,
    progress?: (p: SpaceCreateProgress) => void,
    retrying?: (error: string, errorVariant: string) => void,
  ): Promise<string> {
    this.startCreate(args, os, pendingId);
    // A failure that usually passes (the core's `fail_decision`: a runtime
    // that did not start this time, a network drop during the download, a
    // VM that did not answer in time) runs the create once more by itself,
    // the row saying "Retrying…"; `retrying` hears of it first (the page
    // applies the same failure). Only the final outcome throws.
    for (;;) {
      try {
        return await this.followCreate(args, pendingId, progress);
      } catch (error) {
        if (isCancelled(error)) throw error;
        const row = this.creates.pending.find((p) => p.id === pendingId);
        if (!row?.retryPending || row.cancelling) throw error;
        retrying?.(words(error), createErrorVariant(error));
      }
    }
  }

  /** A new pending id (`pending:<uuid>`). */
  static pendingId(): string {
    return `pending:${randomUUID()}`;
  }

  private startCreate(args: AppCreateSpaceArgs, os: AppSpaceOs, pendingId: string): void {
    // A Space on this machine is reached over the local network (a macOS VM
    // on vmnet): ask now, while the person who pressed Create is here.
    if (args.on === "local") this.host.requestLocalNetwork();
    const machine = args.on.startsWith("host:") ? args.on.slice("host:".length) : undefined;
    const provider = args.on === "local" ? this.native.AppSpaceProvider.Local : this.native.AppSpaceProvider.Relay;
    this.sendCreate(
      new this.native.AppCreateAction.Start({
        id: pendingId,
        name: args.name ?? "",
        os,
        provider,
        now: AppModel.nowMs(),
        image: args.image,
        kind: args.kind,
        hostArch: this.hostArch,
        gpu: args.gpu !== undefined,
        host: machine,
        hostName: machine ? this.machineName(machine) : undefined,
      }),
    );
  }

  /** The name of one of your machines, from the Spaces it provides (or its own entry on the relay). */
  private machineName(id: string): string | undefined {
    return this.spaces.find((s) => s.id === `relay:${id}`)?.name ?? this.spaces.find((s) => s.host === id && s.hostName)?.hostName;
  }

  private async followCreate(args: AppCreateSpaceArgs, pendingId: string, progress?: (p: SpaceCreateProgress) => void): Promise<string> {
    try {
      let accepted = false;
      const create = this.backend.create(args, pendingId, (p) => {
        accepted = true;
        this.sendCreate(
          new this.native.AppCreateAction.Progress({
            id: pendingId,
            phase: p.phase,
            fraction: p.fraction,
            now: AppModel.nowMs(),
            bytesDone: p.bytesDone,
            bytesTotal: p.bytesTotal,
            bytesPerSecond: p.bytesPerSecond,
          }),
        );
        progress?.(p);
      });
      const id = await this.awaitAccepted(create, () => accepted, pendingId);
      this.sendCreate(new this.native.AppCreateAction.Finish({ id: pendingId, spaceId: id }));
      await this.refresh();
      if (this.selectedSpaceId === pendingId) this.select(id);
      // The registry lists it now: drop the pending row from the state too.
      this.sendCreate(new this.native.AppCreateAction.Dismiss({ id: pendingId }));
      return id;
    } catch (error) {
      if (isCancelled(error)) this.sendCreate(new this.native.AppCreateAction.CancelDone({ id: pendingId }));
      else this.sendCreate(new this.native.AppCreateAction.Fail({ id: pendingId, error: words(error), errorVariant: createErrorVariant(error), now: AppModel.nowMs() }));
      throw error;
    }
  }

  /** The create's result, or `CreateNotAccepted` when the daemon reported nothing within `createAcceptTimeout`. */
  private async awaitAccepted(create: Promise<string>, accepted: () => boolean, pendingId: string): Promise<string> {
    const first = await withTimeout(this.createAcceptTimeout, () => create);
    if (first.ok) return first.value;
    if (!(first.error instanceof TimeoutError)) throw first.error;
    if (accepted()) return create;
    console.log(`[cua-spaces] the cua daemon did not take create ${pendingId} within ${this.createAcceptTimeout} s`);
    // Best effort: it may still reach the daemon over the old connection; never wait on it.
    void withTimeout(30, () => this.backend.cancelCreate(pendingId));
    void this.reconnectNow("a create was not taken");
    create.catch(() => {});
    throw new CreateNotAccepted(Math.ceil(this.createAcceptTimeout));
  }

  /** "Connect by address": the SDK's handshake, then the list read again. */
  async addByAddress(url: string, token: string | null, name: string | null): Promise<void> {
    await this.backend.add(url, token, name);
    await this.refresh();
  }

  /** Cancel on a Space still being created: the row shows Cancelling until the SDK stopped it, then goes. */
  cancelCreate(pendingId: string): void {
    if (!this.native.appCreatesIsPending(pendingId) || !this.creates.pending.some((p) => p.id === pendingId && !p.cancelling)) return;
    this.sendCreate(new this.native.AppCreateAction.CancelStart({ id: pendingId }));
    void (async () => {
      try {
        await this.backend.cancelCreate(pendingId);
        this.sendCreate(new this.native.AppCreateAction.CancelDone({ id: pendingId }));
      } catch (error) {
        this.sendCreate(new this.native.AppCreateAction.CancelFail({ id: pendingId, error: words(error) }));
      }
    })();
  }

  /**
   * Delete (after the page's confirmation): the row shows Deleting at once,
   * and goes when the SDK's delete returns; a failure restores it with the
   * banner. `removeOnly` forgets the Space and keeps it running.
   */
  delete(space: AppSpace, removeOnly = false): void {
    // A failed create is only removed from the list.
    if (this.native.appCreatesIsPending(space.id)) {
      this.sendCreate(new this.native.AppCreateAction.Dismiss({ id: space.id }));
      return;
    }
    if (this.isDeleting(space.id)) return;
    this.recordFeature("space_delete");
    const detail = this.native.appSpaceDetail(space);
    this.sendCreate(new this.native.AppCreateAction.DeleteStart({ id: space.id, now: AppModel.nowMs() }));
    this.usage.delete(space.id);
    this.thumbnails.remove(space.id);
    void (async () => {
      try {
        await this.backend.remove(space.id, removeOnly || detail.removeOnly);
        this.sendCreate(new this.native.AppCreateAction.DeleteDone({ id: space.id }));
        await this.refresh();
      } catch (error) {
        this.sendCreate(new this.native.AppCreateAction.DeleteFail({ id: space.id }));
        this.show(this.native.appDeleteFailedText(space.name, words(error)), true);
      }
    })();
  }

  /**
   * Bulk delete or leave (after the page's confirmation): every row shows
   * Deleting at once and goes as its own result comes back, at most three
   * at a time; one failure never stops the rest. One result per id, in
   * order; ids that are not listed answer not found.
   */
  async bulk(ids: string[], leave: boolean): Promise<BulkResult[]> {
    const results = new Map<string, BulkResult>();
    const todo: AppSpace[] = [];
    for (const id of ids) {
      const space = this.spaces.find((s) => s.id === id);
      if (!space) results.set(id, { id, ok: false, error: `No Space ${id}.` });
      else if (this.isDeleting(id)) results.set(id, { id, ok: false, error: `${space.name} is already going.` });
      else todo.push(space);
    }
    if (todo.length > 0 && !leave) this.recordFeature("space_delete");
    for (const space of todo) {
      this.sendCreate(new this.native.AppCreateAction.DeleteStart({ id: space.id, now: AppModel.nowMs() }));
      this.usage.delete(space.id);
      this.thumbnails.remove(space.id);
    }
    const one = async (space: AppSpace) => {
      let r: BulkResult;
      try {
        const [first] = leave ? await this.backend.leave([space.id]) : await this.backend.deleteMany([space.id]);
        r = first ?? { id: space.id, ok: false, error: "No answer." };
      } catch (error) {
        r = { id: space.id, ok: false, error: words(error) };
      }
      results.set(space.id, { ...r, id: space.id });
      this.sendCreate(new (r.ok ? this.native.AppCreateAction.DeleteDone : this.native.AppCreateAction.DeleteFail)({ id: space.id }));
    };
    const queue = [...todo];
    await Promise.all(
      Array.from({ length: Math.min(BULK_CONCURRENCY, queue.length) }, async () => {
        for (let next = queue.shift(); next; next = queue.shift()) await one(next);
      }),
    );
    if (todo.length > 0) await this.refresh();
    return ids.map((id) => results.get(id) ?? { id, ok: false, error: "No answer." });
  }

  /**
   * Gives a Space a new name (its id stays the same), then reads the list
   * again so every view shows it. A refusal (a Space shared with you, a
   * name taken or against the rule) throws in plain words; nothing changes.
   */
  async rename(space: AppSpace, name: string): Promise<void> {
    try {
      await this.backend.rename(space.id, name);
    } catch (error) {
      // The SDK's sentence without its kind ("permission denied: ...").
      throw new Error(brokerWords(error));
    }
    await this.refresh();
  }

  /** The power button: turns the Space off (suspended or stopped, as its provider can) or back on. */
  setPower(space: AppSpace, on: boolean): void {
    if (this.native.appCreatesIsPowering(this.creates, space.id)) return;
    this.sendCreate(new this.native.AppCreateAction.PowerStart({ id: space.id, on, now: AppModel.nowMs() }));
    if (!on) {
      this.usage.delete(space.id);
      this.thumbnails.remove(space.id);
    }
    void (async () => {
      try {
        await this.backend.setPower(space.id, on);
        this.sendCreate(new this.native.AppCreateAction.PowerDone({ id: space.id }));
        await this.refresh();
      } catch (error) {
        this.sendCreate(new this.native.AppCreateAction.PowerFail({ id: space.id, error: words(error) }));
      }
    })();
  }

  // MARK: Settings and banners

  saveSettings(): void {
    try {
      this.native.appSettingsSave(this.settingsPath, this.settings);
    } catch (error) {
      console.warn(`[cua-spaces] could not save ${this.settingsPath}: ${words(error)}`);
    }
    this.changed("settings");
  }

  /** Sets the daemon notice (`daemonNotice`); the page hears it with the Spaces. */
  setDaemonNotice(text: string | null): void {
    if (text === this.daemonNotice) return;
    this.daemonNotice = text;
    if (text) console.log(`[cua-spaces] ${text}`);
    this.changed("spaces");
  }

  /** The banner (the SwiftUI app's `show(error:)` and `show(info:)`); logged here, as the page has no banner. */
  show(text: string, isError: boolean): void {
    this.banner = text;
    this.bannerIsError = isError;
    console[isError ? "warn" : "log"](`[cua-spaces] ${text}`);
  }

  /** Records a Spaces app feature (a fixed name). */
  recordFeature(name: string): void {
    this.telemetry?.record(this.native.appTelemetryFeature(name));
  }

  /** The count the menu bar item and the notch show. */
  get statusLine(): string {
    return this.native.appOnStatusLine(this.native.appOnCount(this.spaces));
  }

  /** The window chrome (account line, New Space, empty state). */
  get chrome(): AppMainChrome {
    return this.native.appMainChrome({
      identity: this.identity ?? undefined,
      cloudConfigured: this.cloudConfigured,
      canSignIn: this.account !== null,
      experiments: this.settings.experiments,
    });
  }

  // MARK: Account

  /** "Sign in": the browser flow (opened by the account), the code while it
   * waits. Starting again while it waits is a retry: a new attempt, and
   * whichever attempt the browser finishes signs the app in. */
  async beginSignIn(opts?: SignInOptions): Promise<void> {
    if (!this.account || this.signInPhase() === "Starting") return;
    await this.signIns.begin(opts);
  }

  /** A session is stored (by a sign-in here or `cua auth login`). */
  private async signedIn(who: string | null): Promise<void> {
    if (!this.account) return;
    this.identity = who ?? this.account.identity();
    this.host.identity = this.identity;
    this.devices.signedIn = this.identity !== null;
    this.teams.reset();
    this.signInUrl = null;
    this.setSignIn(new this.native.AppSignInPhase.Idle());
    // Relay sharing paused while signed out comes back for its owner.
    await this.host.reconcileAccount();
    await this.devices.refresh();
    this.changed("machines");
  }

  /** The sign-in's phase (`Idle`, `Starting`, `Waiting`, `Failed`). */
  signInPhase(): string {
    return this.signIn.tag;
  }

  private setSignIn(phase: AppSignInPhase): void {
    this.signIn = phase;
    this.changed("session");
  }

  /** Shown with its reason in the sidebar's foot (and a toast there);
   * counted by its kind only (the core keeps the words). */
  private signInFailed(end: SignInEnd): void {
    const n = this.native;
    const phase =
      "timedOut" in end
        ? n.appSignInFailed(n.AppSignInFailure.NotConfirmed, undefined)
        : n.appSignInFailedFromError(sdkErrorKind(end.error) ?? "", words(end.error));
    this.signInUrl = null;
    this.setSignIn(phase);
    const reason = phase.tag === "Failed" ? phase.inner.reason : n.AppSignInFailure.Unknown;
    this.telemetry?.record(n.appTelemetrySignInFailure(reason));
  }

  /** Stops waiting for the browser (the page can start again). */
  cancelSignIn(): void {
    if (this.signInPhase() === "Starting" || this.signInPhase() === "Waiting") {
      this.signIns.cancel();
      this.telemetry?.record(this.native.appTelemetrySignInFailure(undefined));
    }
    this.signInUrl = null;
    this.setSignIn(new this.native.AppSignInPhase.Idle());
  }

  async signOut(): Promise<void> {
    try {
      await this.account?.signOut();
    } catch (error) {
      console.warn(`[cua-spaces] sign out: ${words(error)}`);
    }
    this.identity = null;
    this.host.identity = null;
    this.devices.signedIn = false;
    this.teams.reset();
    // Signed out: relay sharing stops (the setup stays to resume).
    await this.host.reconcileAccount();
    await this.devices.refresh();
    this.setSignIn(new this.native.AppSignInPhase.Idle());
    this.changed("machines");
  }
}

/** The SDK's storage probe as the wizard env takes it. */
export function wizardStorage(s: LocalStorage): AppLocalStorage {
  const volume = (v: LocalStorage["lume"]) => (v ? { availableBytes: v.availableBytes, totalBytes: v.totalBytes, name: v.name } : undefined);
  return { reserveBytes: s.reserveBytes, lume: volume(s.lume), qemu: volume(s.qemu), container: volume(s.container), pulled: s.pulled, linuxRuntimeDownloadBytes: s.linuxRuntimeDownloadBytes };
}