Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(antigravity): treat a quota failure on the harness stderr as a usage limit
AdityaUmale23cfe9f50457
diff --git a/server/drivers/acp/harness-failure.ts b/server/drivers/acp/harness-failure.tsindex e1b4003b74..a4248c53df 100644--- a/server/drivers/acp/harness-failure.ts+++ b/server/drivers/acp/harness-failure.ts@@ -13,6 +13,8 @@ export interface StderrFailure {line: RegExp;/** What the person is told. */message: string;+ /** The failure one such line stands for when it says more than that the process failed, e.g. a usage limit. */+ failure?: (line: string) => Error | undefined;}const MAX_LINE_CHARS = 64 * 1024;@@ -24,8 +26,10 @@ export function watchStderrFailure(host: AcpHost, spec: StderrFailure): void {const lines = (partial + String(chunk)).split("\n");partial = lines.pop() ?? "";if (partial.length > MAX_LINE_CHARS) partial = "";- if (failed.has(host) || !lines.some((line) => line.length <= MAX_LINE_CHARS && spec.line.test(line.replace(/\r$/u, "")))) return;- const error = Object.assign(new Error(spec.message), { acpSessionFailure: true });+ if (failed.has(host)) return;+ const line = lines.map((each) => each.replace(/\r$/u, "")).find((each) => each.length <= MAX_LINE_CHARS && spec.line.test(each));+ if (line === undefined) return;+ const error = Object.assign(spec.failure?.(line) ?? new Error(spec.message), { acpSessionFailure: true });failed.set(host, error);host.draining = true;for (const link of host.links) link.failAll(error);diff --git a/server/drivers/acp/turn-health.test.ts b/server/drivers/acp/turn-health.test.tsindex 0bbdee9ce8..c7a7d907dc 100644--- a/server/drivers/acp/turn-health.test.ts+++ b/server/drivers/acp/turn-health.test.ts@@ -8,6 +8,7 @@ import { afterEach, describe, expect, it } from "vitest";import type { ProviderInstance } from "../../contracts.ts";import { recordEvents } from "../../testing/events.ts";import { removeTempDir } from "../../testing/cleanup.ts";+import { antigravityHarnessLimit } from "../antigravity-turn-outcome.ts";import { ANTIGRAVITY_SESSION_SETUP_TIMEOUT_MS } from "../antigravity.ts";import { createAcpDriver, type AcpSupport } from "./core.ts";import { ANTIGRAVITY_HARNESS_FAILURE_LOG } from "./harness-failure.ts";@@ -85,4 +86,25 @@ describe("an agent whose own log says its local harness failed", () => {expect(Number(readFileSync(launches, "utf8"))).toBe(2);recorder.stop();});++ it("ends the turn on the usage limit a failure line reports, so a fallback model can carry on", async () => {+ const directory = folder();+ const quotaLine = "E1124 14:52:01.000001 4242 local_connection.py:551] harness stderr: Agent run failed: agent executor error: "+ + "Error 429, Message: Quota exhausted., Status: RESOURCE_EXHAUSTED, Details: [map[metadata:map[quotaResetDelay:38m0s] reason:QUOTA_EXHAUSTED]]";+ const driver = createAcpDriver({+ ...SUPPORT,+ stderrFailure: { line: ANTIGRAVITY_HARNESS_FAILURE_LOG, message: "The local agent connection failed.", failure: (line) => antigravityHarnessLimit("Turn Health Test", line) },+ });+ instance = await driver.create({+ instanceId: "turn-health-quota", displayName: "Turn Health", enabled: true, config: driver.defaultConfig(),+ environment: { FAKE_ACP_PROMPT_STDERR: quotaLine, FAKE_ACP_PROMPT_STDERR_THEN: "hang" },+ });+ const recorder = recordEvents(instance.adapter);+ const { turnId } = await instance.adapter.sendTurn({ threadId: "thread-quota", text: "go", cwd: directory });+ expect(await recorder.until((event) => event.type === "turn.completed" && event.turnId === turnId, 5_000)).toMatchObject({ ok: false, stopReason: "quota" });+ expect(recorder.events.find((event) => event.type === "runtime.error" && event.turnId === turnId)).toMatchObject({+ limit: "engine", message: expect.stringContaining("reached its usage limit"),+ });+ recorder.stop();+ });});diff --git a/server/drivers/antigravity-turn-outcome.test.ts b/server/drivers/antigravity-turn-outcome.test.tsindex 2c82bd3025..964d7e20e9 100644--- a/server/drivers/antigravity-turn-outcome.test.ts+++ b/server/drivers/antigravity-turn-outcome.test.ts@@ -14,7 +14,7 @@ import type { ProviderInstance } from "../contracts.ts";import { removeTempDir } from "../testing/cleanup.ts";import { recordEvents } from "../testing/events.ts";import { antigravityTokenPath } from "./antigravity-acp.ts";-import { antigravityLimitFacts, antigravityQuotaFamily, watchAntigravityPromptOutcome } from "./antigravity-turn-outcome.ts";+import { antigravityHarnessLimit, antigravityLimitFacts, antigravityQuotaFamily, watchAntigravityPromptOutcome } from "./antigravity-turn-outcome.ts";import { AntigravityDriver } from "./antigravity.ts";const USER_INPUT = 14;@@ -212,6 +212,16 @@ describe("watchAntigravityPromptOutcome", () => {expect(antigravityLimitFacts(undefined, known)).toEqual({ limit: "model" });});+ it("reads a harness failure line on stderr that reports an exhausted quota as a usage limit, and no other one", () => {+ const harness = (error: string) => `E1124 14:52:01.000001 4242 local_connection.py:551] harness stderr: Agent run failed: ${error}`;+ expect(antigravityHarnessLimit(NAME, harness(QUOTA_ERROR.split("\n")[1]!), 0))+ .toMatchObject({ acpUsageLimit: true, message: expect.stringContaining("has reached its usage limit"), facts: { limitResetsAt: 3_600_000 } });+ expect(antigravityHarnessLimit(NAME, harness("RESOURCE_EXHAUSTED (code 429): Quota exceeded. reason:QUOTA_EXHAUSTED quotaResetDelay:38m0s]"), 0))+ .toMatchObject({ acpUsageLimit: true, facts: { limitResetsAt: 38 * 60_000 } });+ expect(antigravityHarnessLimit(NAME, harness("failed to handle call hook response: no pending hook for ID hook_request_2"), 0)).toBeUndefined();+ expect(antigravityHarnessLimit(NAME, harness(UNAVAILABLE_ERROR.split("\n")[1]!), 0)).toBeUndefined();+ });+it("gives no failure for a session id that is no file name, a missing profile, or a log replaced during the prompt", async () => {const { directory, conversations } = profile();expect(await (await watchAntigravityPromptOutcome(NAME, directory, "../escape"))({ stopReason: "end_turn" })).toBeUndefined();diff --git a/server/drivers/antigravity-turn-outcome.ts b/server/drivers/antigravity-turn-outcome.tsindex 22db939e6b..87ef83e9e0 100644--- a/server/drivers/antigravity-turn-outcome.ts+++ b/server/drivers/antigravity-turn-outcome.ts@@ -191,6 +191,13 @@ export function antigravityTurnFailure(return restarting(new AcpReportedFailure(engineFailure(provider, message!.trim(), { kind: transient ? "server" : "refused", status: http })!, false));}+/** The usage limit a harness failure line on stderr reports, when it reports one; undefined for any other failure. */+export function antigravityHarnessLimit(provider: string, line: string, now: number = Date.now()): AcpReportedFailure | undefined {+ const apiError = API_ERROR.exec(line) ?? statusError(line);+ const exhausted = QUOTA_EXHAUSTED.test(line) || apiError?.[1] === "429" || apiError?.[3] === "RESOURCE_EXHAUSTED";+ return exhausted ? usageLimit(provider, line, now) : undefined;+}+/*** Antigravity reports an upstream API failure as a chat message and `end_turn`. Its session log ends that turn with an* error step, so the check reads only steps the current prompt added, read-only, from this session's own unlinkeddiff --git a/server/drivers/antigravity.ts b/server/drivers/antigravity.tsindex fd1dbb18e0..afcd0bedb0 100644--- a/server/drivers/antigravity.ts+++ b/server/drivers/antigravity.ts@@ -38,7 +38,7 @@ import {validateAntigravityRuntime,verifyAntigravityBrowserHelper,} from "./antigravity-acp.ts";-import { antigravityLimitFacts, watchAntigravityPromptOutcome } from "./antigravity-turn-outcome.ts";+import { antigravityHarnessLimit, antigravityLimitFacts, watchAntigravityPromptOutcome } from "./antigravity-turn-outcome.ts";import { splitAntigravityThinking } from "./antigravity-models.ts";import { AntigravityAuthController } from "./antigravity-auth.ts";import { AntigravityCliLogin, type LoginProcesses } from "./antigravity-native-login.ts";@@ -205,6 +205,8 @@ const support: AcpSupport = {stderrFailure: {line: ANTIGRAVITY_HARNESS_FAILURE_LOG,message: "Antigravity's local agent connection failed. Send the message again to continue in a fresh Antigravity process.",+ // A run that failed on an exhausted quota is a usage limit, so the bot's fallback model can carry on.+ failure: (line) => antigravityHarnessLimit(support.displayName, line),},clientFileSystem: true,clientReadRoots: (turn) => (turn.skillFiles ?? []).map(dirname),