Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
import { randomBytes } from "node:crypto";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import type { IncomingMessage, ServerResponse } from "node:http";
import { join } from "node:path";
import { PassThrough } from "node:stream";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { failedTurnTool } from "../shared/failed-turn.ts";
import type { PlaceRow } from "../shared/place-view.ts";
import { TRIAL_CREDIT_REFUSED } from "../shared/trial-credit.ts";
import { CLOUD_HEALTH_PATH, CLOUD_PAIRING_PATH, CLOUD_PAIRING_SKEW_S, cloudPairingSignature, cloudRequestSignature } from "./cloud-home.ts";
import { CLOUD_HEALTH_MAX_BYTES, HEALTH_BUCKET_CAP, START_CODES, createCloudHealth, createCloudHealthLedger, type CloudHealthDeps } from "./cloud-health.ts";
import { createCloudSpeed } from "./cloud-speed.ts";
import { noteStartFailure } from "./engine-start-failures.ts";
import { DATA_DIR, EVENTS_DIR } from "./config.ts";
import { memoryStallText, memoryStopText } from "./engine-memory.ts";
import type { RuntimeEvent } from "./contracts.ts";
import { doctorAtStart } from "./doctor/start.ts";
import { toolFailureMessage } from "./drivers/tool-failure.ts";
import { STORAGE_HEALTH_MAX_BYTES, type StorageReport } from "./perf/disk-usage.ts";
import { ProviderAuthSessions } from "./provider-auth-sessions.ts";
import { createCloudSignedRoutes } from "./routes/cloud-signed.ts";
import { SessionRegistry } from "./sessions.ts";
import { createSignInTrace } from "./sign-in-trace.ts";
import { Store } from "./store.ts";
import { removeTempDir } from "./testing/cleanup.ts";
const START = 1_800_000_000_000;
const secret = "S".repeat(43);
beforeEach(() => {
rmSync(DATA_DIR, { recursive: true, force: true });
mkdirSync(EVENTS_DIR, { recursive: true });
});
/** A real store and the bus as the server wires them; the clock and every slow lookup are the test's. */
function world(overrides: Partial<CloudHealthDeps> = {}) {
let clock = START;
const now = () => clock;
const store = new Store(() => ({ instanceId: "claude", model: "claude-sonnet-5" }));
const listeners = new Set<(event: RuntimeEvent) => void>();
const bus = { subscribe: (listener: (event: RuntimeEvent) => void) => { listeners.add(listener); return () => listeners.delete(listener); } };
const health = createCloudHealth({
store, bus, dataDir: DATA_DIR, eventsDir: EVENTS_DIR, version: "0.1.120", image: `registry.fly.io/omb-cloud-home@sha256:${"b".repeat(64)}`,
engines: () => [{ instanceId: "claude", driverKind: "claudeAgent", snapshot: { state: "available", authenticated: true, version: "2.1.300" } }],
connectors: () => null, browser: () => ({ available: true, installing: false, interrupted: 0 }), now, ...overrides,
});
const bot = store.createBot({ name: "Tallyspoon Ops" });
const emit = (threadId: string, event: { type: string } & Record<string, unknown>, provider = "codex") => {
for (const listener of listeners) listener({ eventId: randomBytes(4).toString("hex"), provider, threadId, createdAt: new Date(clock).toISOString(), turnId: "t1", ...event } as RuntimeEvent);
};
const fail = (cause: string, flags: Parameters<typeof failedTurnTool>[1] = {}, threadId = bot.threadId) =>
store.appendMessage(threadId, { role: "bot", kind: "activity", at: clock, tool: failedTurnTool(cause, flags) });
return { store, bot, health, emit, fail, advance: (ms: number) => { clock += ms; }, now };
}
const incidents = (w: ReturnType<typeof world>, after?: string) => w.health.report(after).incidents.map(({ kind, code, engine, count }) => ({ kind, code, engine, count }));
const place = (state: PlaceRow["state"]): PlaceRow => ({ state, params: { bot: "Tallyspoon Ops", cause: "Boat said: quota exceeded for [email protected]." }, source: "works-on" });
describe("a Cloud home's storage in its health report", () => {
const report = (bots: number): StorageReport => ({
v: 1, at: START, ageS: 0, tookS: 60, running: false, progress: null, volume: { totalBytes: 84e9, freeBytes: 3e9, usedBytes: 81e9 }, walkedBytes: 80e9, unwalkedBytes: 1e9,
categories: [{ name: "engine_state", class: "precious", bytes: 50e9, files: 10 }], bots: Array.from({ length: bots }, (_, index) => ({ id: `${"b".repeat(60)}${index}`, bytes: 1e9, files: 1 })),
botsRest: { bytes: 0, files: 0, count: 0 }, other: null, skipped: null, linksCapped: false, linksUncheckedBytes: 0, resumes: 0, failed: false,
});
it("carries the last breakdown within its own budget, and leaves the field out where it is not measured or its read throws", () => {
expect(world({ storage: () => report(2) }).health.report(undefined)).toMatchObject({ storage: { v: 1, volume: { usedBytes: 81e9 }, categories: [{ name: "engine_state" }] } });
const big = world({ storage: () => report(200) }).health.report(undefined).storage!;
expect(JSON.stringify(big).length).toBeLessThanOrEqual(STORAGE_HEALTH_MAX_BYTES);
expect(big.botsRest.count).toBeGreaterThan(0);
expect(world().health.report(undefined)).not.toHaveProperty("storage");
expect(world({ storage: () => null }).health.report(undefined)).not.toHaveProperty("storage");
const warned: string[] = [];
const broken = world({ storage: () => { throw new RangeError("at /Users/ada/secret"); }, log: (line) => warned.push(line) });
expect(broken.health.report(undefined)).not.toHaveProperty("storage");
broken.health.report(undefined);
expect(warned).toEqual(["cloud health: storage left out: RangeError"]);
});
});
describe("a Cloud home's engine sign-ins in its health report", () => {
it("counts attempts and says how the last one ended, with no code, link, flow or account in it; left out where there is no trace or its read throws", async () => {
const dir = mkdtempSync(join(tmpdir(), "omb-health-sign-ins-"));
try {
const trace = createSignInTrace({ dataDir: dir, now: () => START, log: () => {} });
const sessions = new ProviderAuthSessions({ trace, now: () => START });
const auth = { phase: "waiting" as const, flowId: "flow-secret-1", authorizationUrl: "https://claude.com/cai/oauth/authorize?state=secret-state", expiresAt: null };
const instance = { instanceId: "claude-ada-work", driverKind: "claudeAgent", startAuthentication: async () => auth, getAuthentication: async () => auth,
completeAuthentication: async () => { throw Object.assign(new Error("Paste the whole code"), { reason: "wrong_code" }); }, cancelAuthentication: async () => {} };
for (let attempt = 0; attempt < 3; attempt++) {
await sessions.start(instance, "session-secret");
await expect(sessions.complete(instance.instanceId, "session-secret", "flow-secret-1", "SHORT")).rejects.toThrow();
if (attempt < 2) await sessions.cancel(instance.instanceId, "session-secret", "flow-secret-1");
}
const report = world({ signIns: () => trace.summary() }).health.report(undefined);
expect(report.signIns).toMatchObject({ attempts: 3, open: 1, engines: [{ kind: "claudeAgent", attempts: 3, completed: 0, last: { outcome: "wrong_code", at: START } }] });
expect(report.signIns!.recent.at(-1)).toEqual({ kind: "claudeAgent", method: "paste-code", outcome: "wrong_code", at: START });
for (const secret of ["SHORT", "flow-secret", "secret-state", "claude.com", "session-secret", "ada-work"]) expect(JSON.stringify(report)).not.toContain(secret);
} finally { await removeTempDir(dir); }
expect(world().health.report(undefined)).not.toHaveProperty("signIns");
const warned: string[] = [];
const broken = world({ signIns: () => { throw new TypeError("flow-secret-1"); }, log: (line) => warned.push(line) });
expect(broken.health.report(undefined)).not.toHaveProperty("signIns");
expect(warned).toEqual(["cloud health: signIns left out: TypeError"]);
});
});
describe("a Cloud home's memory pressure in its health report", () => {
it("carries the last day's counts where the home watches memory, and null where it does not", () => {
expect(world().health.report(undefined)).toMatchObject({ room: null });
const room = { waits: 3, waitMs: 360_000, stopped: 1, killed: 2, stallMinutes: 4 };
expect(world({ room: () => room }).health.report(undefined)).toMatchObject({ room });
});
});
describe("the doctor in a Cloud home's health report", () => {
it("carries the check ids and counts of this version's first start, and null before it ran", () => {
expect(world().health.report(undefined)).toMatchObject({ doctor: null });
writeFileSync(join(DATA_DIR, `config.json.31337.${"0b4d8f7a-3c2e-4f1a-9b6d-5e7c8a9f0d1e"}.tmp`), "{}");
doctorAtStart(DATA_DIR, "0.1.120", { now: () => START, pidAlive: () => false, log: () => {} });
const { doctor } = world().health.report(undefined);
expect(doctor).toMatchObject({ at: START, version: "0.1.120", fixed: expect.arrayContaining(["temp-files"]), problems: [], thisStart: true });
expect(JSON.stringify(doctor)).not.toContain(DATA_DIR);
});
});
describe("a Cloud home's speed in its health report", () => {
it("times a person's message to its engine's turn through the store and the bus, in a few numbers and fixed buckets", () => {
const w = world();
w.store.appendMessage(w.bot.threadId, { role: "user", kind: "text", text: "Sort my inbox, Tallyspoon", at: w.now() });
w.advance(700);
w.emit(w.bot.threadId, { type: "turn.started" });
const { speed } = w.health.report(undefined);
expect(speed).toMatchObject({ bootMs: null, wakeMs: null, turnStarts: [0, 1, 0, 0, 0, 0] });
expect(Object.keys(speed).sort()).toEqual(["bootMs", "loop", "psi", "steal", "turnStarts", "wakeMs", "windowS"]);
expect(JSON.stringify(speed)).not.toMatch(/Tallyspoon|inbox/);
expect(JSON.stringify(speed).length).toBeLessThan(200);
});
});
describe("a new Cloud's first hour in its health report", () => {
it("carries the milestones and the Chief's counts, and null where there are none", () => {
expect(world().health.report(undefined)).toMatchObject({ firstHour: null, value: null });
const firstHour = { startedAt: START, ms: { firstMessage: 40_000, firstApp: 95_000 }, apps: ["gmail" as const] };
const value = { sorted: 3, drafted: 1, prepped: 0, routines: 0, minutes: 8 };
expect(world({ firstHour: () => ({ firstHour, value }) }).health.report(undefined)).toMatchObject({ firstHour, value });
});
});
describe("the failure code a Cloud home reports", () => {
it("names a place that could not be used by its state", () => {
const w = world();
w.fail("Your cloud computer hours are used up.", { place: place("cc-no-hours") });
w.fail("Tallyspoon Ops' browser stopped.", { place: place("browser-cannot") });
expect(incidents(w)).toEqual([{ kind: "place", code: "cc-no-hours", engine: null, count: 1 }, { kind: "place", code: "browser-cannot", engine: null, count: 1 }]);
});
it("takes the engine's stop reason, unless it is generic and the cause says more", () => {
const w = world();
const started = (threadId: string, provider = "codex") => w.emit(threadId, { type: "turn.started" }, provider);
const threads = ["a", "b", "c", "d", "e"].map((title) => w.store.createTask(w.bot.id, title)!.threadId);
started(threads[0]!);
w.fail("codex app-server exited with signal 9", {}, threads[0]);
w.emit(threads[0]!, { type: "turn.completed", ok: false, stopReason: "rpc_error" });
started(threads[1]!);
w.fail("Your ChatGPT sign-in has expired (401 Unauthorized). Sign in again.", { setup: true }, threads[1]);
w.emit(threads[1]!, { type: "turn.completed", ok: false, stopReason: "auth_required" });
started(threads[2]!);
w.fail("You've hit your usage limit. Try again at 3pm.", {}, threads[2]);
w.emit(threads[2]!, { type: "turn.completed", ok: false, stopReason: "rpc_error" });
started(threads[3]!, "claudeAgent");
w.fail("Invalid API key · Please run /login", { setup: true }, threads[3]);
w.emit(threads[3]!, { type: "turn.completed", ok: false, stopReason: "auth_required" }, "claudeAgent");
// A turn the engine could not stop, with no failed row of its own.
started(threads[4]!);
w.emit(threads[4]!, { type: "turn.completed", ok: false, stopReason: "shutdown_timeout" });
expect(incidents(w)).toEqual([
{ kind: "turn", code: "rpc_error", engine: "codex", count: 1 },
{ kind: "turn", code: "auth_required", engine: "codex", count: 1 },
{ kind: "turn", code: "quota", engine: "codex", count: 1 },
{ kind: "turn", code: "auth_required", engine: "claudeAgent", count: 1 },
{ kind: "turn", code: "shutdown_timeout", engine: "codex", count: 1 },
]);
});
it("files a turn a failed tool call stopped under tool_error, not unknown", () => {
const w = world();
w.emit(w.bot.threadId, { type: "turn.started" }, "openai-compat");
w.fail(toolFailureMessage(["composio_CLARITY_GET_INSIGHTS"], []));
w.emit(w.bot.threadId, { type: "turn.completed", ok: false, stopReason: "tool_error" }, "openai-compat");
expect(incidents(w)).toEqual([{ kind: "turn", code: "tool_error", engine: "openai-compat", count: 1 }]);
});
it("files a row whose turn never reports a stop under its own class once it has waited", () => {
const w = world();
w.emit(w.bot.threadId, { type: "turn.started" });
w.fail("401 Unauthorized: missing bearer");
expect(incidents(w)).toEqual([]);
w.advance(6_000);
expect(incidents(w)).toEqual([{ kind: "turn", code: "auth", engine: "codex", count: 1 }]);
});
it("names the failures the server writes itself, and a trial credit's refusal", () => {
const w = world();
w.fail("no activity for 20 minutes — the turn was stopped");
w.fail("turn interrupted — OpenMausBot restarted");
w.fail("this thread could not start — the engine is not installed");
w.fail("queued message could not start — the engine is not installed");
w.fail(TRIAL_CREDIT_REFUSED.used_up);
w.fail(`${memoryStopText(false)} (opencodeGo exited null before the prompt result)`);
w.fail(memoryStallText("no activity for 20 minutes — the turn was stopped", 1));
w.fail(memoryStopText(true));
expect(incidents(w)).toEqual([
{ kind: "turn", code: "stalled", engine: null, count: 1 },
{ kind: "turn", code: "restart", engine: null, count: 1 },
{ kind: "turn", code: "could_not_start", engine: null, count: 2 },
{ kind: "turn", code: "trial_used_up", engine: null, count: 1 },
{ kind: "turn", code: "out_of_memory", engine: null, count: 3 },
]);
});
it("counts only failed-turn rows, and an engine stop that also wrote one counts once", () => {
const w = world();
w.store.appendMessage(w.bot.threadId, { role: "bot", kind: "activity", tool: { name: "Bash", ok: false } });
w.store.appendMessage(w.bot.threadId, { role: "bot", kind: "activity", tool: { name: "notice: retrying", ok: true } });
w.store.appendMessage(w.bot.threadId, { role: "user", kind: "text", text: "error: this is what the person typed" });
expect(incidents(w)).toEqual([]);
w.emit(w.bot.threadId, { type: "turn.started" });
w.fail("codex did not shut down after termination was requested");
w.emit(w.bot.threadId, { type: "turn.completed", ok: false, stopReason: "shutdown_timeout" });
w.emit(w.bot.threadId, { type: "turn.completed", ok: false, stopReason: "shutdown_timeout" });
w.emit(w.bot.threadId, { type: "turn.completed", ok: false, stopReason: "interrupted" });
expect(incidents(w)).toEqual([{ kind: "turn", code: "shutdown_timeout", engine: "codex", count: 1 }]);
});
});
describe("the ledger", () => {
it("dedupes by kind, code and engine, and a cursor reads only what changed since", () => {
const w = world();
for (let i = 0; i < 3; i++) { w.fail("turn interrupted — OpenMausBot restarted"); w.advance(60_000); }
const first = w.health.report(null);
expect(first.incidents).toEqual([expect.objectContaining({ code: "restart", count: 3, firstAt: START, lastAt: START + 120_000, botId: w.bot.id, threadId: w.bot.threadId })]);
expect(w.health.report(first.cursor).incidents).toEqual([]);
w.fail("this thread could not start — no engine");
w.fail("turn interrupted — OpenMausBot restarted");
const next = w.health.report(first.cursor);
expect(next.incidents.map(({ code, count }) => ({ code, count }))).toEqual([{ code: "could_not_start", count: 1 }, { code: "restart", count: 4 }]);
// Another boot's cursor, or one from the future, reads from the start.
expect(w.health.report(`${START - 1}.1`).incidents).toHaveLength(2);
expect(w.health.report(`${START}.999`).incidents).toHaveLength(2);
expect(w.health.report("junk").incidents).toHaveLength(2);
});
it("keeps at most HEALTH_BUCKET_CAP buckets, dropping the one quiet the longest", () => {
let clock = START;
const ledger = createCloudHealthLedger({ now: () => clock });
for (let i = 0; i <= HEALTH_BUCKET_CAP; i++) {
ledger.noteEvent({ eventId: "e", provider: `engine${i}`, threadId: `thread-${i}`, createdAt: "", type: "turn.started" });
ledger.noteMessage(`thread-${i}`, { at: clock, tool: failedTurnTool("turn interrupted — OpenMausBot restarted") });
clock += 1000;
}
const read = ledger.read(null);
expect(read.incidents).toHaveLength(HEALTH_BUCKET_CAP);
expect(read.incidents.map((incident) => incident.engine)).not.toContain("engine0");
expect(read.incidents.at(-1)!.engine).toBe(`engine${HEALTH_BUCKET_CAP}`);
});
it("rates turns over 15 minutes and a day", () => {
const w = world();
for (let i = 0; i < 4; i++) w.emit(w.bot.threadId, { type: "turn.started" });
w.fail("turn interrupted — OpenMausBot restarted");
w.advance(20 * 60_000);
w.emit(w.bot.threadId, { type: "turn.started" });
w.fail("no activity for 20 minutes — the turn was stopped");
expect(w.health.report(null).turns).toEqual({ started15m: 1, failed15m: 1, started24h: 5, failed24h: 2, boot: 5 });
w.advance(24 * 3600_000);
// The rolling windows empty out; the count since boot never falls (the Admin keeps each hour's increase).
expect(w.health.report(null).turns).toEqual({ started15m: 0, failed15m: 0, started24h: 0, failed24h: 0, boot: 5 });
});
it("files an engine that did not start under why, not as the provider-looking code its words would get; a sign-in keeps its auth code", () => {
const w = world(), thread = (name: string) => w.store.createBot({ name }).threadId;
const opencode = "Internal error: Internal service failure (session/new, ServiceUnavailableError)";
// Too busy: the plain line busy-start.ts shows (its notice key), and the process limit's.
const busy = thread("busy"), cpu = thread("cpu"), processes = thread("processes"), missing = thread("missing"), provider = thread("provider"), signedOut = thread("signed-out"), other = thread("other");
for (const id of [busy, cpu, processes, missing, provider, signedOut, other]) w.emit(id, { type: "turn.started" }, "opencodeGo");
w.fail(opencode, { text: { key: "chat.notice.cloudBusyStart" } }, busy);
w.fail(opencode, { text: { key: "chat.notice.cloudOutOfCpu" } }, cpu);
w.fail("There is no room to start another program (EAGAIN)", { text: { key: "chat.notice.outOfProcesses", params: { code: "EAGAIN" } } }, processes);
w.fail("`opencode` isn't installed, or isn't on this app's PATH", { setup: true }, missing);
// The engine's or its provider's own refusal of session/new, noted by the start (busy-start.ts) for this thread.
noteStartFailure(provider, "provider", w.now());
w.fail("500 internal server error", {}, provider);
w.fail("Authentication required: not logged in", { setup: true }, signedOut);
w.fail("500 internal server error", {}, other);
w.emit(signedOut, { type: "turn.completed", ok: false, stopReason: "auth_required" }, "opencodeGo");
w.emit(other, { type: "turn.completed", ok: false, stopReason: "rpc_error" }, "opencodeGo");
expect(incidents(w).map(({ code, count }) => [code, count]).sort()).toEqual([["auth_required", 1], ["server_error", 1], ["start_not_installed", 1], ["start_overloaded", 3], ["start_provider", 1]]);
// A note is the next row's only: a later failure on the thread is filed by its words again.
w.emit(provider, { type: "turn.started" }, "opencodeGo");
w.fail("500 internal server error", {}, provider);
w.emit(provider, { type: "turn.completed", ok: false, stopReason: "rpc_error" }, "opencodeGo");
expect(incidents(w).find((incident) => incident.code === "server_error")).toMatchObject({ count: 2 });
// A start that failed without a row of its own (its turn just ended) leaves nothing for the thread's next turn.
noteStartFailure(provider, "provider", w.now());
w.emit(provider, { type: "turn.completed", ok: false, stopReason: "rpc_error" }, "opencodeGo");
w.emit(provider, { type: "turn.started" }, "opencodeGo");
w.fail("500 internal server error", {}, provider);
w.emit(provider, { type: "turn.completed", ok: false, stopReason: "rpc_error" }, "opencodeGo");
expect(incidents(w).find((incident) => incident.code === "server_error")).toMatchObject({ count: 3 });
// Too busy, noted by the start, though the row came without the plain line (startup recovery words it as the engine did).
const recovered = thread("recovered");
w.emit(recovered, { type: "turn.started" }, "opencodeGo");
noteStartFailure(recovered, "overloaded", w.now());
w.fail(opencode, {}, recovered);
expect(incidents(w).find((incident) => incident.code === "start_overloaded")).toMatchObject({ count: 4 });
// A provider refusal noted, then the backup AI's start on the same thread refused for sign-in: the row stays an auth failure.
const backup = thread("backup");
w.emit(backup, { type: "turn.started" }, "opencodeGo");
noteStartFailure(backup, "provider", w.now());
w.fail("Authentication required: not logged in", { setup: true }, backup);
w.emit(backup, { type: "turn.completed", ok: false, stopReason: "auth_required" }, "opencodeGo");
expect(incidents(w).find((incident) => incident.code === "auth_required")).toMatchObject({ count: 2 });
expect(incidents(w).find((incident) => incident.code === "start_provider")).toMatchObject({ count: 1 });
expect(Object.values(START_CODES).every((code) => /^[a-z0-9][a-z0-9_-]{0,47}$/.test(code))).toBe(true);
});
it("carries the event loop's stalls and the counts since boot in a few dozen bytes, codes and counts only", () => {
const room = { waits: 3, waitMs: 360_000, stopped: 1, killed: 2, stallMinutes: 4 };
const plain = world({ room: () => room, speed: createCloudSpeed({ read: () => null, loop: () => null }) }).health.report(null);
const report = world({ room: () => ({ ...room, boot: { waits: 7, stopped: 2, killed: 2, stallMinutes: 9 } }),
speed: createCloudSpeed({ read: () => null, loop: () => ({ n: 12, worstMs: 11_234 }) }) }).health.report(null);
expect(report.speed.loop).toEqual({ n: 12, worstMs: 11_234 });
expect(report.room).toMatchObject({ boot: { waits: 7, stopped: 2, killed: 2, stallMinutes: 9 } });
expect(report.turns.boot).toBe(0);
const added = JSON.stringify(report).length - JSON.stringify(plain).length;
expect(added).toBeLessThanOrEqual(100);
});
it("reports each routine's last outcome and failures in a row as the saved run rows give them, so a restart hides none", () => {
const rows = [{ routineId: "routine-1", status: "missed" as const, at: START, consecutiveFailures: 2 }, { routineId: "not an id!", status: "failed" as const, at: START, consecutiveFailures: 9 }];
const caps: number[] = [], none = { ran: 0, failed: 0, missed: 0, late: 0, retried: 0 };
const schedule = { v: 1 as const, seq: 7, unreadable: true, nextDueAt: null, oldestUnconfirmed: null, lastConfirmed: null, due: { boot: none, day: none }, slowStops: 2 };
const deps = { routines: (cap: number) => { caps.push(cap); return rows; }, schedule: () => schedule };
for (const w of [world(deps), world(deps)]) {
const report = w.health.report(null);
expect(report.routines).toEqual([rows[0]]);
expect(report.schedule).toEqual(schedule);
}
expect(caps).toEqual([100, 100]);
expect(world().health.report(null)).toMatchObject({ routines: [], schedule: null });
vi.spyOn(console, "error").mockImplementation(() => {});
const broken = world({ routines: () => { throw new Error("Quarterly-Secret-Plan"); }, schedule: () => { throw new Error("Quarterly-Secret-Plan"); } }).health.report(null);
expect(broken).toMatchObject({ v: 1, routines: [], schedule: null, turns: { started15m: 0 } });
expect(JSON.stringify(vi.mocked(console.error).mock.calls)).not.toContain("Quarterly");
vi.restoreAllMocks();
});
});
describe("the report", () => {
it("never carries a failure's words, a name, a title, a prompt, an address, a URL or an engine's account", async () => {
const planted = ["Tallyspoon Ops", "Secret Merger Plans", "[email protected]", "Quarterly numbers for ACME", "https://intranet.acme.test/q3",
"Draft the ACME layoffs memo", "[email protected]", "Ada Org", "acct-7f3a-secret", "sk-ant-api03-planted", "Boat said: quota exceeded"];
const w = world({
engines: () => [{ instanceId: "claude", driverKind: "claudeAgent",
snapshot: { state: "available", authenticated: false, version: "2.1.300", reason: "Quarterly numbers for ACME", account: { email: "[email protected]", organization: "Ada Org", method: "login" } } }],
connectors: () => Promise.resolve({ gmail: { connected: true, accounts: [{ id: "acct-7f3a-secret", alias: "[email protected]" }] }, notion: { connected: false },
"https://intranet.acme.test/q3": { connected: false } }),
});
const task = w.store.createTask(w.bot.id, "Secret Merger Plans")!;
w.store.appendMessage(task.threadId, { role: "user", kind: "text", text: "Draft the ACME layoffs memo" });
w.emit(task.threadId, { type: "turn.started" });
w.fail("Quarterly numbers for ACME failed for [email protected] at https://intranet.acme.test/q3 (sk-ant-api03-planted)", {}, task.threadId);
w.emit(task.threadId, { type: "turn.completed", ok: false, stopReason: "Quarterly numbers for ACME" });
w.fail("Boat said: quota exceeded", { place: place("cc-provider") }, task.threadId);
w.health.report(null);
await new Promise((resolve) => setTimeout(resolve, 0));
const body = JSON.stringify(w.health.report(null));
for (const words of planted) expect(body, words).not.toContain(words);
const report = JSON.parse(body);
expect(report.incidents.map((incident: { code: string }) => incident.code)).toEqual(["unknown", "cc-provider"]);
expect(report.engines).toEqual([{ kind: "claudeAgent", used: true, available: true, authenticated: false, version: "2.1.300" }]);
expect(report.connectors.items).toEqual([{ slug: "gmail", connected: true }, { slug: "notion", connected: false }]);
});
it("answers at once and looks up connected apps in the background, at most every 30 minutes", async () => {
let lookups = 0;
let answer: (value: Record<string, { connected: boolean }>) => void = () => {};
const w = world({ connectors: () => { lookups++; return new Promise((resolve) => { answer = resolve; }); } });
expect(w.health.report(null).connectors).toBeNull();
expect(w.health.report(null).connectors).toBeNull();
expect(lookups).toBe(1);
answer({ slack: { connected: false } });
await new Promise((resolve) => setTimeout(resolve, 0));
expect(w.health.report(null).connectors).toEqual({ checkedAt: START, failed: false, items: [{ slug: "slack", connected: false }] });
w.advance(29 * 60_000);
w.health.report(null);
expect(lookups).toBe(1);
w.advance(60_000);
w.health.report(null);
expect(lookups).toBe(2);
});
it("stays under 64 KB, and the cursor picks up what did not fit", () => {
let clock = START;
const routines = Array.from({ length: 100 }, (_, i) => `r${String(i).padStart(63, "0")}`);
const w = world({ now: () => clock, routines: () => routines.map((routineId) => ({ routineId, status: "failed" as const, at: START, consecutiveFailures: 1 })) });
const ledger = w.health.ledger;
for (let i = 0; i < HEALTH_BUCKET_CAP; i++) {
const thread = `t${String(i).padStart(63, "0")}`;
ledger.noteEvent({ eventId: "e", provider: `e${String(i).padStart(63, "0")}`, threadId: thread, createdAt: "", type: "turn.started" });
ledger.noteMessage(thread, { at: clock, tool: failedTurnTool("cut off", { place: place("cc-on-my-cloud") }), from: { botId: `b${"0".repeat(63)}`, name: "x", color: "x" } });
clock += 1;
}
const first = w.health.report(null);
expect(JSON.stringify(first).length).toBeLessThanOrEqual(CLOUD_HEALTH_MAX_BYTES);
expect(first.truncated).toBe(true);
const rest = w.health.report(first.cursor);
expect(rest.truncated).toBe(false);
expect(first.incidents.length + rest.incidents.length).toBe(HEALTH_BUCKET_CAP);
expect(new Set([...first.incidents, ...rest.incidents].map((incident) => incident.engine)).size).toBe(HEALTH_BUCKET_CAP);
});
});
/** The route as the server runs it, before the session gate. */
function routes(w: ReturnType<typeof world>) {
const sessions = new SessionRegistry({ file: join(DATA_DIR, "sessions.json"), now: w.now });
const handle = createCloudSignedRoutes({ secret, sessions, health: w.health, now: w.now });
let counter = 0;
const sign = (target: string, options: { method?: string; key?: string; at?: number; nonce?: string; body?: string } = {}) => {
const timestamp = String(Math.floor((options.at ?? w.now()) / 1000)), nonce = options.nonce ?? `nonce-${String(++counter).padStart(12, "0")}`;
return { "x-omb-cloud-timestamp": timestamp, "x-omb-cloud-nonce": nonce,
"x-omb-cloud-signature": `v1=${cloudRequestSignature(options.key ?? secret, timestamp, nonce, options.method ?? "GET", target, options.body ?? "")}` };
};
const send = async (method: string, target: string, headers: Record<string, string>, body = "") => {
const req = Object.assign(new PassThrough(), { method, url: target, headers, socket: { remoteAddress: "127.0.0.1" } }) as unknown as IncomingMessage;
let status = 0, text = "";
const res = { setHeader() {}, writeHead(code: number) { status = code; }, end(data: string) { text = data; } } as unknown as ServerResponse;
const url = new URL(target, "http://localhost");
const answered = handle({ req, res, url, path: url.pathname, method });
(req as unknown as PassThrough).end(body);
expect(await answered).toBe(true);
return { status, body: JSON.parse(text) };
};
return { sessions, sign, send };
}
describe("GET /api/cloud/health", () => {
it("answers the Admin's signed read with the report", async () => {
const w = world();
w.fail("turn interrupted — OpenMausBot restarted");
const { sign, send } = routes(w);
const target = `${CLOUD_HEALTH_PATH}?after=0`;
const answer = await send("GET", target, sign(target));
expect(answer.status).toBe(200);
expect(answer.body).toMatchObject({ v: 1, version: "0.1.120", bootedAt: START, incidents: [{ kind: "turn", code: "restart", count: 1 }] });
const again = `${CLOUD_HEALTH_PATH}?after=${answer.body.cursor}`;
expect((await send("GET", again, sign(again))).body.incidents).toEqual([]);
});
it("still answers 200, with perf null and one warning a minute, when the performance recorder throws", async () => {
const warned: string[] = [];
const w = world({ perf: () => { throw new TypeError("boom at /Users/ada/secret"); }, log: (line) => warned.push(line) });
const { sign, send } = routes(w);
const target = `${CLOUD_HEALTH_PATH}?after=0`;
const answer = await send("GET", target, sign(target));
expect(answer.status).toBe(200);
expect(answer.body).toMatchObject({ v: 1, perf: null });
w.health.report(undefined);
expect(warned).toEqual(["cloud health: perf left out: TypeError"]);
});
it("refuses a wrong key, another path or query, a stale or replayed request, and anything but GET", async () => {
const w = world();
const { sign, send } = routes(w);
const target = `${CLOUD_HEALTH_PATH}?after=0`;
expect(await send("GET", target, sign(target, { key: "W".repeat(43) }))).toEqual({ status: 401, body: { error: "invalid_signature" } });
expect(await send("GET", target, sign(`${CLOUD_HEALTH_PATH}?after=5`))).toEqual({ status: 401, body: { error: "invalid_signature" } });
expect(await send("GET", target, sign(CLOUD_PAIRING_PATH))).toEqual({ status: 401, body: { error: "invalid_signature" } });
expect(await send("GET", target, sign(target, { method: "POST" }))).toEqual({ status: 401, body: { error: "invalid_signature" } });
expect(await send("GET", target, {})).toEqual({ status: 401, body: { error: "invalid_signature" } });
expect(await send("GET", target, sign(target, { at: w.now() - (CLOUD_PAIRING_SKEW_S + 1) * 1000 }))).toEqual({ status: 401, body: { error: "stale_request" } });
const once = sign(target);
expect((await send("GET", target, once)).status).toBe(200);
expect(await send("GET", target, once)).toEqual({ status: 401, body: { error: "replayed_request" } });
expect(await send("POST", target, sign(target, { method: "POST" }))).toEqual({ status: 405, body: { error: "method_not_allowed" } });
});
it("never locks the Admin out of pairing, and a health signature never opens a pairing window", async () => {
const w = world();
const { sign, send, sessions } = routes(w);
const target = `${CLOUD_HEALTH_PATH}?after=0`;
for (let i = 0; i < 20; i++) expect((await send("GET", target, sign(target, { key: "W".repeat(43) }))).status).toBe(401);
expect(sessions.failureSources()).toEqual([]);
const body = JSON.stringify({ label: "OpenMausBot app (Cloud)", ttlSeconds: 300 });
const healthSigned = sign(target);
expect((await send("POST", CLOUD_PAIRING_PATH, { ...healthSigned, "content-type": "application/json" }, body)).status).toBe(401);
const timestamp = String(Math.floor(w.now() / 1000)), nonce = "nonce-pairing-000001";
const paired = await send("POST", CLOUD_PAIRING_PATH, { "content-type": "application/json", "x-omb-cloud-timestamp": timestamp, "x-omb-cloud-nonce": nonce,
"x-omb-cloud-signature": `v1=${cloudPairingSignature(secret, timestamp, nonce, body)}` }, body);
expect(paired.status).toBe(200);
expect(paired.body.code).toMatch(/^[2-9A-HJ-NP-Z]{4}-[2-9A-HJ-NP-Z]{4}-[2-9A-HJ-NP-Z]{4}$/);
});
});