Multiplayer agent harness for work.
Tell the user when Agent37 refuses an agent computer (#1677)
Vishnu2190891979f2
diff --git a/adrs/surfacing-a-refused-agent-computer.md b/adrs/surfacing-a-refused-agent-computer.mdnew file mode 100644index 000000000..731b44949--- /dev/null+++ b/adrs/surfacing-a-refused-agent-computer.md@@ -0,0 +1,26 @@+# Surfacing a refused agent computer++Vishnu from Agent37 again, short one.++A free-plan user asked his qm for something that needed an agent computer.++What we sent back:++> HTTP 402 `insufficient_balance`+> This instance costs $0.0070 per hour, metered per minute; creating it requires at least one day of balance ($0.17). Add balance to your workspace (a new workspace can add a card to unlock $5 of free credit) and try again.++What he saw:++> That turn failed and couldn't be completed. The details are in the operator error log.++He has no operator error log. He tried twice more and stopped.++What we would like him to see:++> Agent37 refused this agent computer: This instance costs $0.0070 per hour, metered per minute; creating it requires at least one day of balance ($0.17). Add balance to your workspace (a new workspace can add a card to unlock $5 of free credit) and try again.++Our fault: our backend throws a plain `Error` for every non-2xx, so the run retries a 402 that can never succeed and then falls back to the generic string, since only a `NonRetryableTurnError` is shown. That default is right for an internal failure, but not for a refusal we wrote for a human to read.++One helper now builds the error for every failed call in `src/sandbox/agent37-sandbox.ts`. Our permanent refusal codes become a `NonRetryableTurnError` carrying our message; everything else keeps its exact text, request id included.++The same swallow exists in every sandbox backend, so this probably belongs in the shared layer. We left the others alone rather than guess at each provider's error shape. Happy to follow your lead if you want it hoisted.diff --git a/src/sandbox/agent37-sandbox.ts b/src/sandbox/agent37-sandbox.tsindex 0db15f3fe..68d6ce8ff 100644--- a/src/sandbox/agent37-sandbox.ts+++ b/src/sandbox/agent37-sandbox.ts@@ -2,7 +2,8 @@ import { randomUUID } from "node:crypto";import type { WorkspaceLayer } from "../types.ts";import type { WorkspaceStore } from "../workspace/workspace-store.ts";import { createKeyedQueue, fetchWithRetry, sleep } from "../util/async.ts";-import { httpFailure, swallowAs, errMessage } from "../util/errors.ts";+import { withRequestId, swallowAs, errMessage } from "../util/errors.ts";+import { NonRetryableTurnError } from "../core/turn-error.ts";import { shq } from "../util/shell.ts";import { nonInteractiveShellPrefix } from "./sandbox-env.ts";import { createExecProcessSessions, type ExecProcessIo } from "./exec-process-session.ts";@@ -53,6 +54,39 @@ const DEFAULT_DISK_GB = 8;const STARTABLE_STATES = new Set(["stopped", "sleeping"]);const GONE_STATES = new Set(["deleting", "deleted"]);const DEAD_STATES = new Set(["failed", ...GONE_STATES]);+const REFUSAL_CODES = new Set([+ "insufficient_balance",+ "instance_limit_reached",+ "free_hours_exhausted",+ "subscription_required",+ "tier_limit",+ "invalid_api_key",+ "ip_not_allowed",+ "forbidden",+]);++function refusalMessage(body: string): string | null {+ let parsed: { error?: { code?: string; message?: string } };+ try {+ parsed = JSON.parse(body);+ } catch {+ return null;+ }+ const { code, message } = parsed?.error ?? {};+ return code && message && REFUSAL_CODES.has(code) ? message : null;+}++async function apiFailure(action: string, res: Response): Promise<{ detail: string; error: Error }> {+ const body = await res.text().catch(() => "");+ const detail = withRequestId(`http ${res.status} ${body.slice(0, 200)}`, res.headers);+ const refused = refusalMessage(body);+ return {+ detail,+ error: refused+ ? new NonRetryableTurnError(`Agent37 refused this agent computer: ${refused}`)+ : new Error(`agent37 ${action}: ${detail}`),+ };+}interface InstanceInfo {id: string;@@ -137,7 +171,7 @@ export function createAgent37Sandbox(workspace: WorkspaceStore, opts: Agent37Sanasync function apiJson<T>(method: string, path: string, body?: unknown, timeoutMs = 60_000): Promise<T> {const res = await api(method, path, body, timeoutMs);if (!res.ok) {- throw new Error(`agent37 ${method} ${path}: ${await httpFailure(res)}`);+ throw (await apiFailure(`${method} ${path}`, res)).error;}return (await res.json()) as T;}@@ -159,7 +193,7 @@ export function createAgent37Sandbox(workspace: WorkspaceStore, opts: Agent37Sanconst res = await api("POST", `/v1/instances/${encodeURIComponent(id)}/start`, undefined, START_TIMEOUT_MS);if (res.ok) continue;if (res.status !== 400 && res.status !== 409) {- throw new Error(`agent37 start ${id}: ${await httpFailure(res)}`);+ throw (await apiFailure(`start ${id}`, res)).error;}}await sleep(READY_POLL_MS);@@ -173,7 +207,7 @@ export function createAgent37Sandbox(workspace: WorkspaceStore, opts: Agent37San"refused",{ timeoutMs: CREATE_TIMEOUT_MS },);- if (!res.ok) throw new Error(`agent37 create ${name}: ${await httpFailure(res)}`);+ if (!res.ok) throw (await apiFailure(`create ${name}`, res)).error;const info = (await res.json()) as InstanceInfo;await ensureRunning(info.id);return info;@@ -196,7 +230,7 @@ export function createAgent37Sandbox(workspace: WorkspaceStore, opts: Agent37San}const res = await api("DELETE", `/v1/instances/${encodeURIComponent(found)}`, undefined, 120_000);if (!res.ok && res.status !== 404) {- throw new Error(`agent37 delete ${name}: ${await httpFailure(res)}`);+ throw (await apiFailure(`delete ${name}`, res)).error;}idByName.delete(name);}@@ -208,18 +242,18 @@ export function createAgent37Sandbox(workspace: WorkspaceStore, opts: Agent37Sanconst first = await send("POST", `/v1/instances/${encodeURIComponent(id)}/exec`, body, timeoutMs);let res = first;if (!first.ok) {- const detail = await httpFailure(first);+ const { detail, error } = await apiFailure(`exec ${name}`, first);if (first.status === 404) idByName.delete(name);const notRunning = first.status === 400 && /running instances/i.test(detail);const freezing = first.status === 409;if (first.status !== 404 && !notRunning && !freezing) {- throw new Error(`agent37 exec ${name}: ${detail}`);+ throw error;}const retryId = await instanceIdFor(name);await ensureRunning(retryId);res = await send("POST", `/v1/instances/${encodeURIComponent(retryId)}/exec`, body, timeoutMs);if (!res.ok) {- throw new Error(`agent37 exec ${name}: ${await httpFailure(res)}`);+ throw (await apiFailure(`exec ${name}`, res)).error;}}const parsed = (await res.json()) as InstanceExecResponse;diff --git a/test/agent37-sandbox.test.ts b/test/agent37-sandbox.test.tsindex 74eeec70a..e357d0218 100644--- a/test/agent37-sandbox.test.ts+++ b/test/agent37-sandbox.test.ts@@ -12,6 +12,7 @@ import { scopeId } from "../src/types.ts";import { mintCapabilityToken, EGRESS_PROXY_AUD } from "../src/auth/capability-token.ts";import { createMemoryAdvisoryLock } from "../src/persistence/advisory-lock.ts";import { installFakeAgent37, FAKE_AGENT37_API_KEY, type FakeAgent37 } from "./support/fake-agent37.ts";+import { NonRetryableTurnError } from "../src/core/turn-error.ts";import type { Sandbox } from "../src/sandbox/sandbox.ts";let fake: FakeAgent37;@@ -324,3 +325,44 @@ test("instance listing retries 429 with Retry-After; exec is never retried and nawait assert.rejects(sandbox.run(h, "echo hi"), /agent37 exec .*: http 502 .*\[request id req-exec\]/);assert.equal(fake.calls.filter((c) => c.path.endsWith("/exec")).length, before + 1);});++test("a workspace refusal reaches the user; an unrecognised failure stays retryable", async () => {+ const message =+ "This instance costs $0.0070 per hour, metered per minute; creating it requires at least one day of " ++ "balance ($0.17). Add balance to your workspace (a new workspace can add a card to unlock $5 of free " ++ "credit) and try again.";+ fake.failNext(402, {+ body: JSON.stringify({ error: { code: "insufficient_balance", message } }),+ match: (c) => c.method === "POST" && c.path === "/v1/instances",+ });+ await assert.rejects(sandbox.provision(layers), (e: Error) => {+ assert.ok(e instanceof NonRetryableTurnError);+ assert.match(e.message, /add a card to unlock \$5 of free credit/);+ return true;+ });++ fake.failNext(402, {+ body: JSON.stringify({ error: { code: "provisioning_failed", message: "Failed to create the instance." } }),+ match: (c) => c.method === "POST" && c.path === "/v1/instances",+ });+ await assert.rejects(sandbox.provision(layers), (e: Error) => {+ assert.ok(!(e instanceof NonRetryableTurnError));+ assert.match(e.message, /agent37 create .*: http 402 /);+ return true;+ });+});++test("a refusal on exec parks the turn instead of failing it generically", async () => {+ const h = await sandbox.provision(layers);+ fake.failNext(402, {+ body: JSON.stringify({+ error: { code: "insufficient_balance", message: "This instance is suspended for non-payment." },+ }),+ match: (c) => c.path.endsWith("/exec"),+ });+ await assert.rejects(sandbox.run(h, "echo hi"), (e: Error) => {+ assert.ok(e instanceof NonRetryableTurnError);+ assert.match(e.message, /suspended for non-payment/);+ return true;+ });+});diff --git a/test/support/fake-agent37.ts b/test/support/fake-agent37.tsindex ddb580c3f..2c7e883e8 100644--- a/test/support/fake-agent37.ts+++ b/test/support/fake-agent37.ts@@ -22,6 +22,7 @@ interface FakeInstance {export interface InjectedFailure {headers?: Record<string, string>;+ body?: string;match?: (call: { method: string; path: string }) => boolean;}@@ -141,7 +142,10 @@ export function installFakeAgent37(): FakeAgent37 {const at = injected.findIndex((f) => !f.match || f.match({ method, path: url.pathname }));if (at >= 0) {const [next] = injected.splice(at, 1);- return new Response(`injected ${next!.status}`, { status: next!.status, headers: next!.headers ?? {} });+ return new Response(next!.body ?? `injected ${next!.status}`, {+ status: next!.status,+ headers: next!.headers ?? {},+ });}if (url.pathname === "/v1/instances" && method === "GET") {return Response.json({ data: live().map(info) });