Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Antigravity proxy: usage reads really go through the instance's proxy; email check restored
milind-soni5168decd218d
diff --git a/server/drivers/antigravity-account.test.ts b/server/drivers/antigravity-account.test.tsindex 921586e99b..e1118661dd 100644--- a/server/drivers/antigravity-account.test.ts+++ b/server/drivers/antigravity-account.test.ts@@ -64,8 +64,7 @@ it.each(["offline", "denied", "bad-token", "bad-email"])("keeps lookup failure nexpect(fetcher).toHaveBeenCalledTimes(calls);});-it("supports proxyUrl and creates proxied fetch correctly", async () => {- const customFetch = createProxiedFetch("http://127.0.0.1:30082");- expect(typeof customFetch).toBe("function");+it("goes through the instance's proxy when it sets one, and uses the global fetch otherwise", () => {expect(createProxiedFetch()).toBe(fetch);+ expect(createProxiedFetch("http://127.0.0.1:30082")).not.toBe(fetch);});diff --git a/server/drivers/antigravity-account.ts b/server/drivers/antigravity-account.tsindex cf38d90538..ef52aa5e79 100644--- a/server/drivers/antigravity-account.ts+++ b/server/drivers/antigravity-account.ts@@ -1,20 +1,24 @@import { createHash } from "node:crypto";import { readFile, stat } from "node:fs/promises";-import { ProxyAgent, fetch as undiciFetch } from "undici";+import type { Dispatcher } from "undici";type Identity = { email: string; method: "login" } | undefined;const identities = new Map<string, { fingerprint: string; expires: number; result: Promise<Identity> }>();export type AccessFetch = (url: string, init: { method?: "POST"; headers?: Record<string, string>; body?: string | URLSearchParams; redirect: "error" | "manual"; signal: AbortSignal }) => Promise<Pick<Response, "ok" | "json">>;-/** Returns a fetch function that routes traffic through an HTTP/HTTPS proxy via undici ProxyAgent.- * If proxyUrl is empty, falls back to globalThis.fetch. */-export function createProxiedFetch(proxyUrl?: string): AccessFetch {- if (!proxyUrl) return fetch as unknown as AccessFetch;- const agent = new ProxyAgent(proxyUrl);- return (async (url: string, init: any) => {- return undiciFetch(url, { ...init, dispatcher: agent });- }) as unknown as AccessFetch;+const proxies = new Map<string, Dispatcher>();++/** A fetch that goes through the instance's own proxy (its HTTPS_PROXY or HTTP_PROXY), so Google sees the same route the+ * engine takes; the global fetch without one. undici loads only once a proxy is set, and each proxy keeps one agent. */+export function createProxiedFetch(proxyUrl?: string): (url: string, init: RequestInit) => Promise<Response> {+ if (!proxyUrl) return fetch;+ return async (url, init) => {+ const { ProxyAgent, fetch: fetchVia } = await import("undici");+ let agent = proxies.get(proxyUrl);+ if (!agent) proxies.set(proxyUrl, agent = new ProxyAgent(proxyUrl));+ return await fetchVia(url, { ...init, dispatcher: agent } as Parameters<typeof fetchVia>[1]) as unknown as Response;+ };}/** The official ACP file stores refresh credentials, not the account email.@@ -69,15 +73,15 @@ export async function refreshAntigravityAccess(async function lookup(credentials: AntigravityStoredSignIn, proxyUrl?: string): Promise<Identity> {try {const signal = AbortSignal.timeout(3_000);- const fetcher = proxyUrl ? createProxiedFetch(proxyUrl) : fetch;- const access = await refreshAntigravityAccess(credentials, signal, fetcher as AccessFetch);+ const fetcher = createProxiedFetch(proxyUrl);+ const access = await refreshAntigravityAccess(credentials, signal, fetcher);if (!access) return undefined;const identity = await fetcher("https://www.googleapis.com/oauth2/v3/userinfo", {- headers: { Authorization: "Bearer " + access.accessToken }, redirect: "error", signal,+ headers: { Authorization: `Bearer ${access.accessToken}` }, redirect: "error", signal,});if (!identity.ok) return undefined;const user = await identity.json() as { email?: unknown };- if (typeof user.email !== "string" || user.email.length > 254 || !/^[^s@]+@[^s@]+.[^s@]+$/.test(user.email)) return undefined;+ if (typeof user.email !== "string" || user.email.length > 254 || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(user.email)) return undefined;return { email: user.email, method: "login" };} catch {// Identity lookup failure must not mark a working engine as signed out.diff --git a/server/drivers/antigravity-usage-limits.test.ts b/server/drivers/antigravity-usage-limits.test.tsindex b76507e961..67ea2e5451 100644--- a/server/drivers/antigravity-usage-limits.test.ts+++ b/server/drivers/antigravity-usage-limits.test.ts@@ -13,6 +13,7 @@ import { antigravityTokenPath } from "./antigravity-acp.ts";import {antigravityPlanUsage,antigravityPlanWindows,+ antigravityProxied,clearAntigravityUsageAccess,readAntigravityUsageWindows,} from "./antigravity-usage-limits.ts";@@ -78,7 +79,7 @@ function fixture(options: FixtureOptions = {}) {}expect(init.headers.Authorization).toBe("Bearer access-token");expect(JSON.parse(init.body!)).toEqual({ project: "project-id" });- expect(init.headers["User-Agent"]).toMatch(/^antigravity\/hub\/2\.33\.0 /);+ expect(init.headers["User-Agent"]).toMatch(/^antigravity\/hub\/2\.24\.0 /);const host = new URL(url).host;return url.endsWith(":retrieveUserQuotaSummary")? respond(options.summaryByHost?.[host] ?? options.summary ?? summary, options.summaryStatusByHost?.[host] ?? options.summaryStatus ?? 200)@@ -412,3 +413,11 @@ describe("the Antigravity row of the usage report", () => {expect(JSON.stringify(report)).not.toMatch(/client-secret|refresh-a|access-token/);});});++it("reads an instance's usage through the proxy the instance sets, and directly without one", () => {+ const deps = { fetch: vi.fn<PlanFetch>() };+ expect(antigravityProxied(deps, {})).toBe(deps);+ const proxied = antigravityProxied(deps, { HTTPS_PROXY: "http://127.0.0.1:9" });+ expect(proxied.fetch).not.toBe(deps.fetch);+ expect(antigravityProxied(deps, { HTTP_PROXY: "http://127.0.0.1:9" }).fetch).not.toBe(deps.fetch);+});diff --git a/server/drivers/antigravity-usage-limits.ts b/server/drivers/antigravity-usage-limits.tsindex a8a51a6cb5..54b8ff2094 100644--- a/server/drivers/antigravity-usage-limits.ts+++ b/server/drivers/antigravity-usage-limits.ts@@ -14,14 +14,14 @@ import { readFile, stat } from "node:fs/promises";import type { PlanExtra, PlanFetch, PlanModelUsage, PlanWindow, PlanWindows } from "../plan-usage.ts";import { antigravityTokenPath } from "./antigravity-acp.ts";-import { refreshAntigravityAccess } from "./antigravity-account.ts";+import { createProxiedFetch, refreshAntigravityAccess } from "./antigravity-account.ts";import { antigravityQuotaFamily } from "./antigravity-turn-outcome.ts";/** Whether Antigravity accounts are asked for their limits at all. Off, they have no row in the usage report. */export const ANTIGRAVITY_USAGE_LIMITS = true;export function antigravityUserAgent(platform: NodeJS.Platform, architecture: string): string {- return `antigravity/hub/2.33.0 (aidev_client; os_type=${platform}; arch=${architecture}; cl=963137146)`;+ return `antigravity/hub/2.24.0 (aidev_client; os_type=${platform}; arch=${architecture}; cl=963137146)`;}const DAILY_ENDPOINT = "https://daily-cloudcode-pa.googleapis.com";@@ -250,7 +250,12 @@ export interface AntigravityUsageDeps {fetch: PlanFetch;now?: () => number;timeoutMs?: number;- proxyUrl?: string;+}++/** The usage reads of an instance with a proxy of its own go through it, as its engine's traffic does. */+export function antigravityProxied(deps: AntigravityUsageDeps, environment: Record<string, string | undefined>): AntigravityUsageDeps {+ const proxyUrl = environment.HTTPS_PROXY || environment.HTTP_PROXY;+ return proxyUrl ? { ...deps, fetch: createProxiedFetch(proxyUrl) } : deps;}/** `work`, or a rejection once `signal` aborts: a request that ignores its signal does not hold the others' answers. */diff --git a/server/plan-usage.ts b/server/plan-usage.tsindex aa20b896b5..b828cf433c 100644--- a/server/plan-usage.ts+++ b/server/plan-usage.ts@@ -7,8 +7,7 @@ import { join } from "node:path";import type { InstanceConfigMap } from "./contracts.ts";import { resolveClaudeConfigDir } from "./drivers/claude.ts";import { codexHome } from "./drivers/codex-identity.ts";-import { antigravityPlanUsage } from "./drivers/antigravity-usage-limits.ts";-import { createProxiedFetch } from "./drivers/antigravity-account.ts";+import { antigravityPlanUsage, antigravityProxied } from "./drivers/antigravity-usage-limits.ts";import { harnessHome, sealedFixture } from "./env-path.ts";export type PlanDriver = "claude" | "codex" | "grok" | "opencode" | "antigravity";@@ -920,14 +919,7 @@ function okRow(account: PlanAccount, windows: PlanWindows): PlanProviderRow {async function fetchProvider(account: PlanAccount, deps: PlanUsageDeps): Promise<PlanProviderRow | null> { // null: OpenCode, in every fleet, with no key of its ownlet credential: PlanCredential;- if (account.driver === "antigravity") {- const proxyUrl = account.environment?.HTTPS_PROXY || account.environment?.HTTP_PROXY;- const effectiveDeps = proxyUrl && deps.fetch === fetch- ? { ...deps, fetch: createProxiedFetch(proxyUrl) as any, proxyUrl }- : { ...deps, proxyUrl };- const windows = await antigravityPlanUsage(account.id, effectiveDeps);- return windows && okRow(account, windows);- } // null: no sign-in, or Google gave nothing+ if (account.driver === "antigravity") { const windows = await antigravityPlanUsage(account.id, antigravityProxied(deps, account.environment)); return windows && okRow(account, windows); } // null: no sign-in, or Google gave nothingtry {credential = await deps.credentials.read(account);} catch {