Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.
Rust agentai-agentapplecomputer-usecomputer-use-agentcontainerizationcuadesktop-automationhacktoberfestlumemacosmanusoperatorswiftvirtualizationvirtualization-frameworkwindowswindows-sandbox
fix(spaces): Ask-first Home never sends a task to this Mac's own desktop; plain errors, Didn't start, retry (CUA-2385) (#6027)
cua-agents[bot]ee8157bf47f6
diff --git a/apps/cua-spaces-web/src/__tests__/ask-first-start-failed.test.tsx b/apps/cua-spaces-web/src/__tests__/ask-first-start-failed.test.tsxnew file mode 100644index 0000000000..76cb0d0578--- /dev/null+++ b/apps/cua-spaces-web/src/__tests__/ask-first-start-failed.test.tsx@@ -0,0 +1,66 @@+// SPDX-License-Identifier: FSL-1.1-MIT+// Copyright (c) 2026 Cua AI, Inc.++// The "Ask first" Home sent a task to "This computer" and agents.start got+// the pseudo-id `this-mac`: the core answered with its own jargon+// ("use its id, such as local:<name> ... add_space") and the page said+// "Stopped" over a screen that said "Finished". A start that fails now says+// "Didn't start" everywhere, in plain words, with Change computer and Try+// again in the card and the core's words under Details.++import { act, cleanup, fireEvent, screen, waitFor } from "@testing-library/react";+import { afterEach, beforeEach, describe, expect, it } from "vitest";++import { setFlagOverride } from "@/bridge";+import { wasmBuilt } from "@/bridge/__tests__/testCore";+import { addAskTask, askTask, resetAskTasks } from "@/lib/ask-first";++import { mountApp, stubBrowserApis } from "./app-harness";++const CORE_NOT_FOUND = 'Space "this-mac" (use its id, such as local:<name> or cloud:<name>, or add it with add_space) not found';++afterEach(cleanup);+stubBrowserApis();+beforeEach(() => {+ localStorage.clear();+ resetAskTasks();+ setFlagOverride("spaces_ask_first_home", true);+});++describe.skipIf(!wasmBuilt)("a task that couldn't start (real routes, wasm core)", () => {+ it("says Didn't start in plain words, and Change computer starts it on a new Linux computer", async () => {+ const task = addAskTask({ prompt: "Hey!", agent: "openai-codex", os: "macos", spaceId: "this-mac", error: CORE_NOT_FOUND });+ const { router } = await mountApp(`/home/${task.id}`, { failsCreates: false, listsGhost: false });+ await screen.findByText("Agents can't work on this Mac itself");+ expect(router.state.location.pathname).toBe(`/home/${task.id}`);+ // The header and the screen agree.+ expect(screen.getAllByText("Didn't start").length).toBeGreaterThanOrEqual(2);+ expect(screen.queryByText("Finished")).toBeNull();+ expect(screen.queryByText("Stopped")).toBeNull();+ // Jargon only under Details.+ const details = screen.getByText("Details").closest("details")!;+ expect(details.textContent).toContain("add_space");+ expect(document.querySelector("[data-ask-start-failed]")!.textContent!.replace(details.textContent!, "")).not.toMatch(/add_space|local:</);+ // Try again can't help a target that can't work: only Change computer.+ expect(screen.queryByRole("button", { name: /Try again/ })).toBeNull();++ fireEvent.click(screen.getByRole("button", { name: /Change computer/ }));+ const linux = await screen.findByRole("menuitem", { name: /A new Linux computer on this Mac/ });+ expect(screen.getByText(/Agents don't work on this Mac itself/)).toBeTruthy();+ expect(screen.queryByRole("menuitem", { name: /This computer/ })).toBeNull();+ await act(async () => {+ fireEvent.click(linux);+ });+ await waitFor(() => expect(askTask(task.id)?.runId).toBeTruthy(), { timeout: 5000 });+ expect(askTask(task.id)).toMatchObject({ error: null, os: "linux" });+ expect(askTask(task.id)?.spaceId).not.toBe("this-mac");+ });++ it("offers Try again for an error that may pass", async () => {+ const task = addAskTask({ prompt: "Run the tests", agent: "claude-code", os: "linux", spaceId: "local:agent-sandbox", error: "agent-sandbox is not running" });+ await mountApp(`/home/${task.id}`, { failsCreates: false, listsGhost: false });+ await screen.findByText("Couldn't reach that computer");+ expect(screen.getByRole("button", { name: /Try again/ })).toBeTruthy();+ expect(screen.getByRole("button", { name: /Change computer/ })).toBeTruthy();+ });+});diff --git a/apps/cua-spaces-web/src/components/ask-first/ask-box.tsx b/apps/cua-spaces-web/src/components/ask-first/ask-box.tsxindex af8fd2642f..9da51ad4bc 100644--- a/apps/cua-spaces-web/src/components/ask-first/ask-box.tsx+++ b/apps/cua-spaces-web/src/components/ask-first/ask-box.tsx@@ -8,8 +8,11 @@ import { useEffect, useRef, type ReactElement, type ReactNode } from "react";import type { Space, SpaceOs } from "@/bridge";import { AppMarkImage } from "@/components/agents/harness-mark";import { OsIcon } from "@/components/os-icon";+import type { NewKind, TaskWhere } from "@/lib/ask-first";import { cn } from "@/lib/utils";+export type { TaskWhere } from "@/lib/ask-first";+/** The coding apps a task can run with (`agent_start`'s harnesses), in the picker's order. */export const TASK_AGENTS: { id: string; name: string; setupId: string }[] = [{ id: "claude-code", name: "Claude Code", setupId: "claude-code" },@@ -22,9 +25,6 @@ export const TASK_AGENTS: { id: string; name: string; setupId: string }[] = [export const agentName = (id: string) => TASK_AGENTS.find((a) => a.id === id)?.name ?? id;-/** Where the job runs: a new computer of a kind on this Mac, or a Space you already have. */-export type TaskWhere = { kind: "new"; os: SpaceOs } | { kind: "space"; space: Space };-const OS_WORD: Record<SpaceOs, string> = { linux: "Linux", macos: "Mac", windows: "Windows", unknown: "" };export function whereLabel(where: TaskWhere): string {@@ -39,6 +39,9 @@ export function AppMark({ agent, size = 32 }: { agent: string; size?: number }));}+/** Why this Mac itself isn't in the list. */+export const WHERE_FOOTER = "Agents don't work on this Mac itself. Each job gets its own computer, so yours stays untouched.";+function Picker({ trigger, title, children, footer }: { trigger: ReactElement; title: string; children: ReactNode; footer?: ReactNode }) {return (<MenuPrimitive.Root>@@ -104,8 +107,8 @@ export function AskBox({where: TaskWhere;onWhere: (w: TaskWhere) => void;/** The kinds of new computer this Mac can make, with a line each ("about 2 min"). */- kinds: { os: SpaceOs; note: string; blocked: string | null }[];- /** Running Spaces to use instead. */+ kinds: NewKind[];+ /** Running Spaces to use instead (never this Mac's own desktop: `pickableSpaces`). */spaces: Space[];busy: boolean;onSubmit: () => void;@@ -152,8 +155,11 @@ export function AskBox({))}</Picker><span className="px-1 text-[14px] text-muted-foreground">on</span>- <Picker- title="Where it runs"+ <WherePicker+ where={where}+ onWhere={onWhere}+ kinds={kinds}+ spaces={spaces}trigger={<button type="button" className={pickerButton} data-picker="where">{where.kind === "new" ? <OsIcon os={where.os} className="size-4 text-muted-foreground" /> : <MonitorIcon className="size-4 text-muted-foreground" />}@@ -161,37 +167,7 @@ export function AskBox({<ChevronDownIcon className="size-4 text-muted-foreground" /></button>}- >- {kinds.map((k) => (- <PickerRow- key={k.os}- icon={- <span className="flex size-7 items-center justify-center rounded-md border text-muted-foreground">- <OsIcon os={k.os} className="size-4" />- </span>- }- title={`A new ${OS_WORD[k.os]} computer on this Mac`}- note={k.blocked ?? k.note}- selected={where.kind === "new" && where.os === k.os}- onSelect={() => onWhere({ kind: "new", os: k.os })}- />- ))}- {spaces.length ? <p className="px-3 pt-3 pb-1 text-[12px] font-medium text-muted-foreground">Or use a computer you already have</p> : null}- {spaces.slice(0, 5).map((s) => (- <PickerRow- key={s.id}- icon={- <span className="flex size-7 items-center justify-center rounded-md border text-muted-foreground">- {s.provider === "local" ? <LaptopIcon className="size-4" /> : <OsIcon os={s.os} className="size-4" />}- </span>- }- title={s.name}- note="On now"- selected={where.kind === "space" && where.space.id === s.id}- onSelect={() => onWhere({ kind: "space", space: s })}- />- ))}- </Picker>+ /><div className="flex-1" /><buttontype="button"@@ -206,3 +182,56 @@ export function AskBox({</div>);}++/**+ * "Where it runs": the kinds of new computer this Mac can make, then the+ * running Spaces you have. This Mac's own desktop is never offered (the+ * footer says why): `agent_start` runs only in a Space.+ */+export function WherePicker({+ where,+ onWhere,+ kinds,+ spaces,+ trigger,+}: {+ where: TaskWhere | null;+ onWhere: (w: TaskWhere) => void;+ kinds: NewKind[];+ spaces: Space[];+ trigger: ReactElement;+}) {+ return (+ <Picker title="Where it runs" trigger={trigger} footer={WHERE_FOOTER}>+ {kinds.map((k) => (+ <PickerRow+ key={k.os}+ icon={+ <span className="flex size-7 items-center justify-center rounded-md border text-muted-foreground">+ <OsIcon os={k.os} className="size-4" />+ </span>+ }+ title={`A new ${OS_WORD[k.os]} computer on this Mac`}+ note={k.blocked ?? k.note}+ selected={where?.kind === "new" && where.os === k.os}+ onSelect={() => onWhere({ kind: "new", os: k.os })}+ />+ ))}+ {spaces.length ? <p className="px-3 pt-3 pb-1 text-[12px] font-medium text-muted-foreground">Or use a computer you already have</p> : null}+ {spaces.slice(0, 5).map((s) => (+ <PickerRow+ key={s.id}+ icon={+ <span className="flex size-7 items-center justify-center rounded-md border text-muted-foreground">+ {s.provider === "local" ? <LaptopIcon className="size-4" /> : <OsIcon os={s.os} className="size-4" />}+ </span>+ }+ title={s.name}+ note="On now"+ selected={where?.kind === "space" && where.space.id === s.id}+ onSelect={() => onWhere({ kind: "space", space: s })}+ />+ ))}+ </Picker>+ );+}diff --git a/apps/cua-spaces-web/src/components/ask-first/home.tsx b/apps/cua-spaces-web/src/components/ask-first/home.tsxindex 2bde486d16..0de7ac7630 100644--- a/apps/cua-spaces-web/src/components/ask-first/home.tsx+++ b/apps/cua-spaces-web/src/components/ask-first/home.tsx@@ -3,19 +3,20 @@import { Link } from "@tanstack/react-router";import { ChevronRightIcon } from "lucide-react";-import { useEffect, useMemo, useState } from "react";+import { useEffect, useState } from "react";-import { useAgents, useFirstSpaceOffers, useSpaces, useSpaceThumbnail, type AgentRunSummary, type Space, type SpaceOs } from "@/bridge";+import { useAgents, useSpaces, useSpaceThumbnail, type AgentRunSummary, type Space } from "@/bridge";import { CuaKey } from "@/components/cua-key";import { OsIcon } from "@/components/os-icon";import { SpaceCanvas } from "@/components/space-canvas";import { Button } from "@/components/ui/button";-import { mostUsedAgent, orderTasks, SUGGESTIONS, taskState, TASK_STATE_TEXT, taskTitle, useAskTasks, type AskTask, type TaskState } from "@/lib/ask-first";-import { spaceState } from "@/lib/spaces";+import { currentWhere, mostUsedAgent, orderTasks, startErrorView, SUGGESTIONS, taskState, TASK_STATE_TEXT, taskTitle, useAskTasks, type AskTask, type TaskState } from "@/lib/ask-first";+import { realSpaces, spaceState } from "@/lib/spaces";import { cn } from "@/lib/utils";-import { AppMark, AskBox, TASK_AGENTS, type TaskWhere } from "./ask-box";+import { agentName, AppMark, AskBox, TASK_AGENTS, type TaskWhere } from "./ask-box";import { useStartTask } from "./use-start-task";+import { useWhereOptions } from "./where-options";/** The run a task follows, from the runs in your running Spaces. */export function taskRun(runs: readonly AgentRunSummary[] | undefined, task: AskTask): AgentRunSummary | undefined {@@ -24,7 +25,7 @@ export function taskRun(runs: readonly AgentRunSummary[] | undefined, task: AskTexport function StateDot({ state }: { state: TaskState }) {const tone =- state === "working" ? "bg-success" : state === "needs-you" || state === "stuck" ? "bg-warning" : state === "failed" ? "bg-destructive" : state === "done" ? "bg-success" : "bg-muted-foreground/40";+ state === "working" ? "bg-success" : state === "needs-you" || state === "stuck" ? "bg-warning" : state === "failed" || state === "didnt-start" ? "bg-destructive" : state === "done" ? "bg-success" : "bg-muted-foreground/40";return (<span className={cn("relative inline-flex size-2 shrink-0 rounded-full", tone)}>{state === "working" ? <span className="absolute inset-0 animate-ping rounded-full bg-success/60" /> : null}@@ -41,8 +42,8 @@ export function AskFirstHome() {const tasks = useAskTasks();const { data: spaces } = useSpaces();const { data: agents, agentSetup } = useAgents();- const { offers } = useFirstSpaceOffers();- const start = useStartTask();+ const { kinds, spaces: pickable } = useWhereOptions();+ const { start } = useStartTask();const [installed, setInstalled] = useState<ReadonlySet<string>>(new Set());useEffect(() => {let live = true;@@ -62,16 +63,9 @@ export function AskFirstHome() {const [text, setText] = useState("");const [agent, setAgent] = useState<string | null>(null);const chosenAgent = agent ?? mostUsed ?? TASK_AGENTS.find((a) => installed.has(a.id))?.id ?? "claude-code";- const [where, setWhere] = useState<TaskWhere>({ kind: "new", os: "linux" });- const kinds = useMemo(() => {- const k: { os: SpaceOs; note: string; blocked: string | null }[] = offers.map((o) => ({- os: o.os,- note: `Free. Ready in ${o.time}${o.size ? ` (${o.size})` : ""}.`,- blocked: o.blocked,- }));- return k.length ? k : [{ os: "linux" as const, note: "Free. Ready in a few minutes.", blocked: null }];- }, [offers]);- const running = (spaces ?? []).filter((s) => spaceState(s) === "running");+ // A new Linux computer unless you pick; a pick that's no longer offered falls back to it.+ const [picked, setWhere] = useState<TaskWhere | null>(null);+ const where = currentWhere(picked, kinds, pickable);const submit = (prompt = text, w = where) => {if (!prompt.trim()) return;start({ prompt, agent: chosenAgent, where: w });@@ -100,7 +94,7 @@ export function AskFirstHome() {where={where}onWhere={setWhere}kinds={kinds}- spaces={running}+ spaces={pickable}busy={false}onSubmit={() => submit()}compact={!first}@@ -132,7 +126,7 @@ export function AskFirstHome() {) : (<TaskList rows={rows} spaces={spaces ?? []} />)}- {!first && (spaces?.length ?? 0) > 0 ? <YourSpaces spaces={spaces ?? []} /> : null}+ {!first && realSpaces(spaces ?? []).length > 0 ? <YourSpaces spaces={realSpaces(spaces ?? [])} /> : null}</div></div>);@@ -165,7 +159,12 @@ function ago(ms: number, now = Date.now()): string {function TaskRow({ task, run, state, space }: { task: AskTask; run: AgentRunSummary | undefined; state: TaskState; space: Space | undefined }) {const thumb = useSpaceThumbnail(task.spaceId ?? "", Boolean(space) && task.ending !== "thrown");- const line = state === "needs-you" && run?.needsYou ? run.needsYou.title : state === "failed" && task.error ? `Stopped: ${task.error}` : TASK_STATE_TEXT[state];+ const line =+ state === "needs-you" && run?.needsYou+ ? run.needsYou.title+ : state === "didnt-start" && task.error+ ? `Didn't start: ${startErrorView(task.error, agentName(task.agent)).title}`+ : TASK_STATE_TEXT[state];const where = task.ending === "thrown" ? "Thrown away" : (space?.name ?? "");return (<Linkdiff --git a/apps/cua-spaces-web/src/components/ask-first/task-view.tsx b/apps/cua-spaces-web/src/components/ask-first/task-view.tsxindex 8834245a44..e3eb6781c5 100644--- a/apps/cua-spaces-web/src/components/ask-first/task-view.tsx+++ b/apps/cua-spaces-web/src/components/ask-first/task-view.tsx@@ -2,7 +2,7 @@// Copyright (c) 2026 Cua AI, Inc.import { Link } from "@tanstack/react-router";-import { ArrowLeftIcon, ArrowUpIcon, CheckIcon, EyeIcon, HandIcon, Loader2Icon, Maximize2Icon, Minimize2Icon, Trash2Icon, TriangleAlertIcon, UserPlusIcon } from "lucide-react";+import { ArrowLeftIcon, ArrowUpIcon, CheckIcon, ChevronDownIcon, EyeIcon, HandIcon, Loader2Icon, Maximize2Icon, Minimize2Icon, RotateCwIcon, Trash2Icon, TriangleAlertIcon, UserPlusIcon } from "lucide-react";import { useEffect, useMemo, useRef, useState, type ReactNode } from "react";import { useAgents, useAgentTimeline, useSession, useShareSheet, useSpaceDetail, useSpaces, type AgentTimeline, type Space } from "@/bridge";@@ -13,12 +13,14 @@ import { StreamSurface } from "@/components/space-detail/stream-surface";import { ConfirmDialog } from "@/components/ui/alert-dialog";import { Button } from "@/components/ui/button";import { toastError } from "@/components/ui/toast";-import { keepTranscript, taskState, TASK_STATE_TEXT, taskTitle, updateAskTask, useAskTasks, type AskTask, type TaskState } from "@/lib/ask-first";+import { keepTranscript, startErrorView, taskState, TASK_STATE_TEXT, taskTitle, updateAskTask, useAskTasks, type AskTask, type TaskState, type TaskWhere } from "@/lib/ask-first";import { createProgress, spaceState } from "@/lib/spaces";import { cn } from "@/lib/utils";-import { agentName, AppMark } from "./ask-box";+import { agentName, AppMark, WherePicker } from "./ask-box";import { StateDot, taskRun } from "./home";+import { useStartTask } from "./use-start-task";+import { useWhereOptions } from "./where-options";/** What the agent is told when the person hands the computer back. *//** How long a sent reply may take to show in the run before the thread trusts the run's status again (ms). */@@ -52,6 +54,7 @@ function TaskView({ task }: { task: AskTask }) {const { data: agents, sendMessage } = useAgents();const { data: session } = useSession();const sharing = useShareSheet();+ const { retry } = useStartTask();const space = spaces?.find((s) => s.id === task.spaceId);const pending = task.pendingId ? spaces?.find((s) => s.id === task.pendingId) : undefined;const ref = task.runId && task.spaceId && task.ending !== "thrown" ? { spaceId: task.spaceId, runId: task.runId } : null;@@ -122,6 +125,7 @@ function TaskView({ task }: { task: AskTask }) {onAnswer={() => replyRef.current?.focus()}onKeep={keep}onThrow={() => setThrowing(true)}+ onRetry={(where) => retry(task, where)}/>);const hasThread = timeline.items.length > 0;@@ -265,6 +269,53 @@ function Starting({ pending, space, name }: { pending: Space | undefined; space:);}+/**+ * A start that failed, in plain words: what went wrong, Try again and+ * Change computer right in the card, and the core's own words under+ * Details.+ */+export function StartFailed({ error, name, onRetry }: { error: string; name: string; onRetry: (where?: TaskWhere) => void }) {+ const view = startErrorView(error, name);+ const { kinds, spaces } = useWhereOptions();+ return (+ <CuaLine tone="bad">+ <div data-ask-start-failed={view.action}>+ <div className="mb-1 flex items-center gap-2 text-[15px] font-semibold text-foreground">+ <TriangleAlertIcon className="size-4 text-destructive" /> {view.title}+ </div>+ <p>{view.body}</p>+ <div className="mt-3 flex flex-wrap items-center gap-2">+ {view.action === "add-key" ? (+ <Button className="h-10 px-4" render={<Link to="/settings/connected-apps" />}>+ Add your key+ </Button>+ ) : null}+ {view.retry ? (+ <Button variant={view.action === "retry" ? "default" : "outline"} className="h-10 px-4" onClick={() => onRetry()}>+ <RotateCwIcon /> Try again+ </Button>+ ) : null}+ <WherePicker+ where={null}+ onWhere={(w) => onRetry(w)}+ kinds={kinds}+ spaces={spaces}+ trigger={+ <Button variant={view.action === "change-computer" ? "default" : "outline"} className="h-10 px-4" data-ask-change-computer="">+ Change computer <ChevronDownIcon />+ </Button>+ }+ />+ </div>+ <details className="mt-3 text-[12px]">+ <summary className="cursor-pointer select-none">Details</summary>+ <p className="mt-1 font-mono break-words whitespace-pre-wrap">{error}</p>+ </details>+ </div>+ </CuaLine>+ );+}+function ThreadFooter({task,state,@@ -275,6 +326,7 @@ function ThreadFooter({onAnswer,onKeep,onThrow,+ onRetry,}: {task: AskTask;state: TaskState;@@ -285,26 +337,11 @@ function ThreadFooter({onAnswer: () => void;onKeep: () => void;onThrow: () => void;+ onRetry: (where?: TaskWhere) => void;}) {return (<div className="flex flex-col gap-5" data-ask-footer="">- {task.error ? (- <CuaLine tone="bad">- <div className="mb-1 flex items-center gap-2 text-[15px] font-semibold text-foreground">- <TriangleAlertIcon className="size-4 text-destructive" /> This task couldn't start- </div>- {task.error}- {/key|sign|auth|login|credential/i.test(task.error) ? (- <p className="mt-2">- {name} needs your key to work on the new computer. Add it in{" "}- <Link to="/settings/connected-apps" className="underline">- Settings → Connected apps- </Link>- , then ask again.- </p>- ) : null}- </CuaLine>- ) : null}+ {task.error ? <StartFailed error={task.error} name={name} onRetry={onRetry} /> : null}{state === "working" ? (<p className="flex items-center gap-2 text-xs text-muted-foreground" role="status"><StateDot state="working" />@@ -390,8 +427,22 @@ function Screen({onBig: () => void;}) {const detail = useSpaceDetail(space, null).detail;- const on = space ? spaceState(space) === "running" : false;- const caption = !space ? (task.ending === "thrown" ? "Thrown away" : "Not ready yet") : state === "needs-you" ? "Waiting for you" : state === "working" ? "Live" : "Finished";+ // A task that never started shows no screen: there is nothing of its to watch.+ const on = space && state !== "didnt-start" ? spaceState(space) === "running" : false;+ const caption =+ state === "didnt-start"+ ? TASK_STATE_TEXT["didnt-start"]+ : !space+ ? task.ending === "thrown"+ ? "Thrown away"+ : "Not ready yet"+ : state === "needs-you"+ ? "Waiting for you"+ : state === "working" || state === "stuck"+ ? "Live"+ : state === "failed"+ ? TASK_STATE_TEXT.failed+ : "Finished";return (<div className={cn("overflow-hidden rounded-xl border bg-card shadow-xs", state === "needs-you" && mode === "watch" && "ring-2 ring-warning/70", mode === "control" && "ring-2 ring-brand")} data-ask-screen={mode}>{space && on ? (diff --git a/apps/cua-spaces-web/src/components/ask-first/use-start-task.ts b/apps/cua-spaces-web/src/components/ask-first/use-start-task.tsindex 31630710f0..97f03efb7a 100644--- a/apps/cua-spaces-web/src/components/ask-first/use-start-task.ts+++ b/apps/cua-spaces-web/src/components/ask-first/use-start-task.ts@@ -4,43 +4,62 @@import { useNavigate } from "@tanstack/react-router";import { newPendingId, useAgents, useFeatureRecorder, useFirstSpaceOffers, useSpaces } from "@/bridge";-import { addAskTask, updateAskTask } from "@/lib/ask-first";--import type { TaskWhere } from "./ask-box";+import { addAskTask, askTask, HOST_TARGET_ERROR, retryTarget, taskTarget, updateAskTask, type AskTask, type TaskTarget, type TaskWhere } from "@/lib/ask-first";/*** Starts a task: records it, opens its conversation at once, makes the* computer (or uses the one picked), then starts the coding app on it with* the job as its prompt. A failure is kept on the task and shown in its- * thread.+ * thread, which offers Try again (`retry`) and Change computer+ * (`retry(task, where)`).*/-export function useStartTask(): (job: { prompt: string; agent: string; where: TaskWhere }) => void {+export function useStartTask(): {+ start: (job: { prompt: string; agent: string; where: TaskWhere }) => void;+ retry: (task: AskTask, where?: TaskWhere) => void;+} {const navigate = useNavigate();- const { createSpace } = useSpaces();+ const { data: spaces, createSpace } = useSpaces();const { startRun } = useAgents();const { offers } = useFirstSpaceOffers();const record = useFeatureRecorder();- return ({ prompt, agent, where }) => {- const os = where.kind === "new" ? where.os : where.space.os;- const task = addAskTask({ prompt: prompt.trim(), agent, os, spaceId: where.kind === "space" ? where.space.id : null });- record("ask_first_task_started");- void navigate({ to: "/home/$taskId", params: { taskId: task.id } });++ const run = (id: string, target: TaskTarget) =>void (async () => {try {- let spaceId = task.spaceId;+ if (target.kind === "host") throw new Error(HOST_TARGET_ERROR);+ let spaceId = target.kind === "space" ? target.spaceId : null;if (!spaceId) {+ const os = target.kind === "create" ? target.os : "linux";const pendingId = newPendingId();- updateAskTask(task.id, { pendingId });+ updateAskTask(id, { pendingId });const offer = offers.find((o) => o.os === os);const space = await createSpace(offer ? { ...offer.args, os } : { os }, pendingId);spaceId = space.id;- updateAskTask(task.id, { spaceId, pendingId: null });+ updateAskTask(id, { spaceId, pendingId: null });}- const run = await startRun({ spaceId, agent, prompt: task.prompt });- updateAskTask(task.id, { runId: run.run_id });+ const prompt = askTask(id)?.prompt ?? "";+ const started = await startRun({ spaceId, agent: askTask(id)?.agent ?? "", prompt });+ updateAskTask(id, { runId: started.run_id });} catch (e) {- updateAskTask(task.id, { error: e instanceof Error ? e.message : String(e) });+ updateAskTask(id, { error: e instanceof Error ? e.message : String(e), pendingId: null });}})();++ return {+ start: ({ prompt, agent, where }) => {+ const target = taskTarget(where);+ const os = where.kind === "new" ? where.os : where.space.os;+ const task = addAskTask({ prompt: prompt.trim(), agent, os, spaceId: target.kind === "space" ? target.spaceId : null });+ record("ask_first_task_started");+ void navigate({ to: "/home/$taskId", params: { taskId: task.id } });+ run(task.id, target);+ },+ retry: (task, where) => {+ const target = where ? taskTarget(where) : retryTarget(task, spaces);+ const os = where ? (where.kind === "new" ? where.os : where.space.os) : task.os;+ updateAskTask(task.id, { error: null, runId: null, pendingId: null, os, spaceId: target.kind === "space" ? target.spaceId : where ? null : task.spaceId });+ record("ask_first_task_retried");+ run(task.id, target);+ },};}diff --git a/apps/cua-spaces-web/src/components/ask-first/where-options.ts b/apps/cua-spaces-web/src/components/ask-first/where-options.tsnew file mode 100644index 0000000000..e35ea24197--- /dev/null+++ b/apps/cua-spaces-web/src/components/ask-first/where-options.ts@@ -0,0 +1,19 @@+// SPDX-License-Identifier: FSL-1.1-MIT+// Copyright (c) 2026 Cua AI, Inc.++import { useMemo } from "react";++import { useFirstSpaceOffers, useSpaces, type Space } from "@/bridge";+import { pickableSpaces, type NewKind } from "@/lib/ask-first";++/** What "Where it runs" offers: the kinds of new computer this Mac can make, and the running Spaces you have. */+export function useWhereOptions(): { kinds: NewKind[]; spaces: Space[] } {+ const { data } = useSpaces();+ const { offers } = useFirstSpaceOffers();+ const kinds = useMemo<NewKind[]>(() => {+ const k = offers.map((o) => ({ os: o.os, note: `Free. Ready in ${o.time}${o.size ? ` (${o.size})` : ""}.`, blocked: o.blocked }));+ return k.length ? k : [{ os: "linux", note: "Free. Ready in a few minutes.", blocked: null }];+ }, [offers]);+ const spaces = useMemo(() => pickableSpaces(data), [data]);+ return { kinds, spaces };+}diff --git a/apps/cua-spaces-web/src/lib/ask-first.test.ts b/apps/cua-spaces-web/src/lib/ask-first.test.tsindex ddb1d80ff0..5ca6a29ec7 100644--- a/apps/cua-spaces-web/src/lib/ask-first.test.ts+++ b/apps/cua-spaces-web/src/lib/ask-first.test.ts@@ -5,7 +5,29 @@ import { beforeEach, describe, expect, it } from "vitest";import { flagOn, setFlagOverride } from "@/bridge";-import { addAskTask, askTasks, mostUsedAgent, orderTasks, resetAskTasks, STUCK_MS, taskState, taskTitle, updateAskTask, type AskTask } from "./ask-first";+import type { Space } from "@/bridge";++import {+ addAskTask,+ askTasks,+ currentWhere,+ defaultWhere,+ HOST_TARGET_ERROR,+ mostUsedAgent,+ orderTasks,+ pickableSpaces,+ resetAskTasks,+ retryTarget,+ startErrorView,+ STUCK_MS,+ taskState,+ TASK_STATE_TEXT,+ taskTarget,+ taskTitle,+ updateAskTask,+ type AskTask,+ type NewKind,+} from "./ask-first";beforeEach(() => {localStorage.clear();@@ -43,7 +65,8 @@ describe("Ask-first tasks", () => {expect(taskState(running, { status: "idle", needsYou: false, lastEventMs: now }, now)).toBe("done");expect(taskState({ ...running, ending: "kept" }, { status: "idle", needsYou: false, lastEventMs: now }, now)).toBe("kept");expect(taskState({ ...running, ending: "thrown" }, null)).toBe("thrown");- expect(taskState({ ...t, error: "no room" }, null)).toBe("failed");+ expect(taskState({ ...t, error: "no room" }, null)).toBe("didnt-start");+ expect(taskState({ ...running, error: "lost the run" }, null)).toBe("failed");});it("put what needs you first, then running ones, then the rest", () => {@@ -64,3 +87,81 @@ describe("Ask-first tasks", () => {expect(taskTitle("x".repeat(100), 10)).toBe(`${"x".repeat(9)}…`);});});++const row = (id: string, more: Partial<Space> = {}): Space => ({ id, name: id, os: "linux", status: "running", detail: "", lastUsedAt: 0, scene: "linux-terminal", ...more });+const thisMac = row("this-mac", { name: "This computer", os: "macos", status: "local", provider: "local" });+const LINUX: NewKind = { os: "linux", note: "Free.", blocked: null };+const MAC: NewKind = { os: "macos", note: "Free.", blocked: null };++describe("where a task runs", () => {+ it("maps each pick to what agents.start is pointed at", () => {+ // A new Linux Space is made first, then the run starts in it.+ expect(taskTarget({ kind: "new", os: "linux" })).toEqual({ kind: "create", os: "linux" });+ // A local Space and a cloud Space are passed by their own ids.+ expect(taskTarget({ kind: "space", space: row("local:aurora", { provider: "local" }) })).toEqual({ kind: "space", spaceId: "local:aurora" });+ expect(taskTarget({ kind: "space", space: row("cloud:ci-box", { provider: "direct", cloud: "aws" }) })).toEqual({ kind: "space", spaceId: "cloud:ci-box" });+ expect(taskTarget({ kind: "space", space: row("relay:mac-mini/qa-windows", { provider: "relay", host: "mac-mini" }) })).toEqual({ kind: "space", spaceId: "relay:mac-mini/qa-windows" });+ // This Mac's own desktop (and another machine's) is never passed as a Space id.+ expect(taskTarget({ kind: "space", space: thisMac })).toEqual({ kind: "host" });+ expect(taskTarget({ kind: "space", space: row("relay:96fedb7e1be65c3d", { provider: "relay" }) })).toEqual({ kind: "host" });+ });++ it("retries on the task's own computer, or makes a new one", () => {+ expect(retryTarget({ spaceId: null, os: "linux" }, [])).toEqual({ kind: "create", os: "linux" });+ expect(retryTarget({ spaceId: "local:aurora", os: "linux" }, [row("local:aurora")])).toEqual({ kind: "space", spaceId: "local:aurora" });+ expect(retryTarget({ spaceId: "this-mac", os: "macos" }, undefined)).toEqual({ kind: "host" });+ });++ it("never offers this Mac's own desktop, or a Space that isn't on", () => {+ const spaces = [thisMac, row("local:aurora"), row("local:off", { power: { control: "stop", off: true } as Space["power"] }), row("relay:96fedb7e1be65c3d", { provider: "relay" })];+ expect(pickableSpaces(spaces).map((s) => s.id)).toEqual(["local:aurora"]);+ });++ it("defaults to a new Linux computer on this Mac", () => {+ expect(defaultWhere([MAC, LINUX])).toEqual({ kind: "new", os: "linux" });+ expect(defaultWhere([])).toEqual({ kind: "new", os: "linux" });+ expect(defaultWhere([{ ...LINUX, blocked: "Needs Docker" }, MAC])).toEqual({ kind: "new", os: "macos" });+ // Running Spaces, this Mac included, never change the default.+ expect(currentWhere(null, [LINUX, MAC], [row("local:aurora")])).toEqual({ kind: "new", os: "linux" });+ });++ it("keeps a pick while it's offered, and falls back when it goes", () => {+ const aurora = row("local:aurora");+ expect(currentWhere({ kind: "space", space: aurora }, [LINUX], [aurora])).toEqual({ kind: "space", space: aurora });+ expect(currentWhere({ kind: "space", space: aurora }, [LINUX], [])).toEqual({ kind: "new", os: "linux" });+ expect(currentWhere({ kind: "space", space: thisMac }, [LINUX], pickableSpaces([thisMac]))).toEqual({ kind: "new", os: "linux" });+ expect(currentWhere({ kind: "new", os: "macos" }, [LINUX, MAC], [])).toEqual({ kind: "new", os: "macos" });+ expect(currentWhere({ kind: "new", os: "windows" }, [LINUX], [])).toEqual({ kind: "new", os: "linux" });+ });+});++describe("a start that failed", () => {+ const CORE_NOT_FOUND = 'Space "this-mac" (use its id, such as local:<name> or cloud:<name>, or add it with add_space) not found';++ it("reads as Didn't start, the same in the header and on the screen", () => {+ expect(TASK_STATE_TEXT["didnt-start"]).toBe("Didn't start");+ });++ it("says what went wrong in plain words, with one thing to do", () => {+ const thisMacView = startErrorView(CORE_NOT_FOUND, "Codex");+ expect(thisMacView).toMatchObject({ title: "Agents can't work on this Mac itself", action: "change-computer", retry: false });+ expect(startErrorView(HOST_TARGET_ERROR, "Codex").title).toBe("Agents can't work on this Mac itself");+ expect(startErrorView('Space "local:gone" (use its id, such as local:<name> or cloud:<name>, or add it with add_space) not found', "Codex")).toMatchObject({+ title: "Couldn't find that computer",+ body: "It may have been deleted or renamed. Pick another one.",+ action: "change-computer",+ });+ expect(startErrorView("Cua Cloud has ended; use a local sandbox or your own computer.", "Codex").action).toBe("change-computer");+ expect(startErrorView("OPENAI_API_KEY is not set", "Codex")).toMatchObject({ title: "Codex needs your key", action: "add-key", retry: true });+ expect(startErrorView("aurora is not running", "Codex")).toMatchObject({ title: "Couldn't reach that computer", action: "retry" });+ expect(startErrorView("Not enough disk space left", "Codex").action).toBe("retry");+ expect(startErrorView("boom", "Codex")).toMatchObject({ title: "Codex couldn't start", action: "retry", retry: true });+ });++ it("never shows the core's jargon in the headline or the line under it", () => {+ for (const raw of [CORE_NOT_FOUND, "Space \"x\" not found", "boom", "OPENAI_API_KEY is not set"]) {+ const v = startErrorView(raw, "Codex");+ expect(`${v.title} ${v.body}`).not.toMatch(/add_space|local:<|cloud:<|this-mac|API_KEY/);+ }+ });+});diff --git a/apps/cua-spaces-web/src/lib/ask-first.ts b/apps/cua-spaces-web/src/lib/ask-first.tsindex c12befcfc6..63677f767d 100644--- a/apps/cua-spaces-web/src/lib/ask-first.ts+++ b/apps/cua-spaces-web/src/lib/ask-first.ts@@ -14,7 +14,9 @@import { useSyncExternalStore } from "react";-import type { AgentRunStatus, SpaceOs, TranscriptItem } from "@/bridge";+import type { AgentRunStatus, Space, SpaceOs, TranscriptItem } from "@/bridge";++import { isMachineDesktop, spaceState } from "./spaces";export type TaskEnding = "kept" | "thrown";@@ -148,8 +150,111 @@ export function taskTitle(prompt: string, max = 80): string {return [...line].length <= max ? line : `${[...line].slice(0, max - 1).join("").trimEnd()}…`;}+/** Where the job runs, as picked: a new computer of a kind on this Mac, or a Space you already have. */+export type TaskWhere = { kind: "new"; os: SpaceOs } | { kind: "space"; space: Space };++/**+ * What `agents.start` is pointed at. The core's `agent_start` only runs in a+ * Space (a `local:`, `cloud:`, `direct:` or `relay:` id), auto-approved+ * because "the Space is the sandbox". A machine's own desktop (this Mac's+ * `this-mac` row, or another of your machines sharing its screen) is not a+ * Space: the core can't resolve it, and running an auto-approved agent on+ * a real computer would break the promise that yours stays untouched. So+ * that target is refused here, before anything is called.+ */+export type TaskTarget = { kind: "create"; os: SpaceOs } | { kind: "space"; spaceId: string } | { kind: "host" };++/** The id of this Mac's own row in the Spaces list. */+export const THIS_MAC_ID = "this-mac";++/** The raw error kept on a task that was pointed at a machine's own desktop. */+export const HOST_TARGET_ERROR = `${THIS_MAC_ID}: agents run in a Space, not on a computer's own desktop`;++export function taskTarget(where: TaskWhere): TaskTarget {+ if (where.kind === "new") return { kind: "create", os: where.os };+ return isMachineDesktop(where.space) ? { kind: "host" } : { kind: "space", spaceId: where.space.id };+}++/** A kept task's target, for Try again: its computer if it has one (the row, when listed, decides), else a new one. */+export function retryTarget(task: Pick<AskTask, "spaceId" | "os">, spaces: readonly Space[] | undefined): TaskTarget {+ if (!task.spaceId) return { kind: "create", os: task.os };+ const row = spaces?.find((s) => s.id === task.spaceId);+ if (task.spaceId === THIS_MAC_ID || (row && isMachineDesktop(row))) return { kind: "host" };+ return { kind: "space", spaceId: task.spaceId };+}++/** A kind of new computer this Mac can make, with its line ("Free. Ready in about 2 min."). */+export interface NewKind {+ os: SpaceOs;+ note: string;+ blocked: string | null;+}++/** The computers you already have that a job can use: running Spaces, never a machine's own desktop. */+export function pickableSpaces(spaces: readonly Space[] | undefined): Space[] {+ return (spaces ?? []).filter((s) => !isMachineDesktop(s) && spaceState(s) === "running");+}++/** Where a job runs unless you pick: a new Linux computer on this Mac (or the first kind this Mac can make). */+export function defaultWhere(kinds: readonly NewKind[]): TaskWhere {+ const ok = kinds.filter((k) => !k.blocked);+ const os = ok.find((k) => k.os === "linux")?.os ?? ok[0]?.os ?? "linux";+ return { kind: "new", os };+}++/** The pick if it's still offered, else the default (a picked Space that went away, or this Mac's own desktop, falls back). */+export function currentWhere(picked: TaskWhere | null, kinds: readonly NewKind[], spaces: readonly Space[]): TaskWhere {+ if (picked?.kind === "new" && kinds.some((k) => k.os === picked.os && !k.blocked)) return picked;+ if (picked?.kind === "space") {+ const row = spaces.find((s) => s.id === picked.space.id);+ if (row) return { kind: "space", space: row };+ }+ return defaultWhere(kinds);+}++/** What a failed start offers in its card. */+export type StartErrorAction = "change-computer" | "add-key" | "retry";++export interface StartErrorView {+ title: string;+ body: string;+ /** The one action that helps most. */+ action: StartErrorAction;+ /** Whether Try again can help (not for a target that can't work). */+ retry: boolean;+}++/**+ * A core error from making the computer or starting the agent, in plain+ * words with the one thing to do next. The raw text stays on the task and+ * shows under Details.+ */+export function startErrorView(raw: string, agent: string): StartErrorView {+ const e = raw.toLowerCase();+ if (e.includes(THIS_MAC_ID))+ return {+ title: "Agents can't work on this Mac itself",+ body: `${agent} gets its own computer, so yours stays untouched. Pick a new computer or one you already have.`,+ action: "change-computer",+ retry: false,+ };+ if (/cloud has ended/.test(e))+ return { title: "That computer is no longer available", body: "Cua Cloud has ended. Pick a computer on this Mac instead.", action: "change-computer", retry: false };+ if (/more than one|ambiguous/.test(e))+ return { title: "More than one computer has that name", body: "Pick the one you mean.", action: "change-computer", retry: false };+ if (/not found|no such|unknown space|add_space/.test(e))+ return { title: "Couldn't find that computer", body: "It may have been deleted or renamed. Pick another one.", action: "change-computer", retry: false };+ if (/api key|key|sign in|signed in|auth|login|credential|unauthori[sz]ed|401/.test(e))+ return { title: `${agent} needs your key`, body: `Add it in Settings → Connected apps, then try again.`, action: "add-key", retry: true };+ if (/not running|space_off|stopped|suspended|offline|unreachable|timed out|timeout|connection|refused/.test(e))+ return { title: "Couldn't reach that computer", body: "Check it's on, then try again. Or pick another one.", action: "retry", retry: true };+ if (/disk|space left|no room|memory|quota|limit/.test(e))+ return { title: "This Mac doesn't have room for a new computer", body: "Free some disk space or turn off a computer you're not using, then try again.", action: "retry", retry: true };+ return { title: `${agent} couldn't start`, body: "Something went wrong. Try again, or pick another computer.", action: "retry", retry: true };+}+/** Where a task stands, in the words Home and the thread use. */-export type TaskState = "starting" | "working" | "needs-you" | "stuck" | "done" | "failed" | "kept" | "thrown";+export type TaskState = "starting" | "working" | "needs-you" | "stuck" | "done" | "didnt-start" | "failed" | "kept" | "thrown";/** No new event for this long while a turn runs reads as stuck. */export const STUCK_MS = 10 * 60_000;@@ -160,7 +265,7 @@ export function taskState(now = Date.now(),): TaskState {if (task.ending === "thrown") return "thrown";- if (task.error) return "failed";+ if (task.error) return task.runId ? "failed" : "didnt-start";if (!task.runId || !run || run.status === null) return task.ending === "kept" ? "kept" : "starting";if (run.needsYou) return "needs-you";if (run.status === "running") return run.lastEventMs !== null && now - run.lastEventMs > STUCK_MS ? "stuck" : "working";@@ -174,6 +279,7 @@ export const TASK_STATE_TEXT: Record<TaskState, string> = {"needs-you": "Needs you",stuck: "No progress for 10 minutes",done: "Done",+ "didnt-start": "Didn't start",failed: "Stopped",kept: "Done. Computer kept",thrown: "Done. Computer thrown away",