Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(antigravity): saved Low/Medium/High Gemini Flash picks run again (#3807)
Milind Soni44dd7f67f885
diff --git a/server/drivers/acp/core.ts b/server/drivers/acp/core.tsindex f924a915e5..69312f2301 100644--- a/server/drivers/acp/core.ts+++ b/server/drivers/acp/core.ts@@ -24,8 +24,7 @@ import {type LogSignal, type ProcessSample, type QuietState,} from "./quiet-status.ts";import { readSessionReport } from "./session-reports.ts";-import { offeredModels } from "./config-options.ts";-import { SessionModelControls, type AcpModelSelection } from "./session-model-controls.ts";+import { pickSessionModel, SessionModelControls, type AcpModelSelection } from "./session-model-controls.ts";import { versionFromProbe } from "./version-probe.ts"; import { engineHomes } from "../../engine-homes/engine-homes.ts";export function skipSubscriptionAuthForLocalInject(model: string | undefined): boolean {@@ -229,6 +228,8 @@ export interface AcpSupport {model: string | undefined,env: Record<string, string | undefined>,): string | undefined;+ /** Map a saved selection onto the live session's catalog (a retired model id), once session/new or load answers. */+ normalizeSessionSelection?(turn: SendTurnInput, offeredModels: readonly string[]): SendTurnInput;/** Per-session settings between session/new (or load) and the first prompt. Some CLIs ignore argv* and take the model/mode over the wire (droid); a throw fails the turn rather than run another model. */configureSession?(ctx: {@@ -1360,34 +1361,17 @@ export function createAcpDriver(support: AcpSupport): ProviderDriver<AcpConfig>try {if (support.selectModel) {- const { configId } = support.selectModel;selectedModel = modelControls.modelOf(session.sessionConfigResult);- const wanted = cliTurn.model;- const offered = offeredModels(session.sessionConfigResult, configId);- if (support.sessionModelEnv && !session.launchModel && offered.length) accountModels = new Set(offered);- // A pooled process missing a catalog model predates the catalog: fail and respawn, never run another (the catalog holds picker ids; `wanted` may be the resolved wire id, so check both).- staleProcess = Boolean(support.fallbackModel && wanted && !launchedThisTurn && offered.length- && !offered.includes(wanted) && models.options.some((option) => option.id === wanted || option.id === turn.model));- if (support.fallbackModel && offered.length && !skipSubscriptionAuthForLocalInject(turn.model)- && !staleProcess && (!wanted || !offered.includes(wanted))) {- const fallback = support.fallbackModel(session.sessionConfigResult, models.default);- if (fallback && fallback !== wanted && offered.includes(fallback)) {- const notice = wanted ? `${wanted}\n${fallback}` : null;- if (notice && session.fallbackNotice !== notice) {- session.fallbackNotice = notice;- emit({- ...base(threadId, turnId),- type: "runtime.notice",- message: `${support.displayName} no longer offers ${wanted}, so this conversation uses ${fallback}. ` +- "Choose another model for this bot to stop seeing this.",- });- }- cliTurn = { ...cliTurn, model: fallback };- current.turn = cliTurn;- reportedModel = fallback;- variant = undefined;- }- }+ // Committed before the setter runs: a failed switch still reports the session and respawns a stale process.+ const pick = pickSessionModel(support, {+ configId: support.selectModel.configId, session, requested: turn, turn: cliTurn, variant, reportedModel,+ catalog: models, launchedThisTurn,+ });+ if (pick.accountModels) accountModels = pick.accountModels;+ staleProcess = pick.staleProcess;+ if (pick.notice) emit({ ...base(threadId, turnId), type: "runtime.notice", message: pick.notice });+ ({ turn: cliTurn, variant, reportedModel } = pick);+ current.turn = cliTurn;selectedModel = await modelControls.select(request, sessionId, INIT_TIMEOUT);}diff --git a/server/drivers/acp/session-model-controls.test.ts b/server/drivers/acp/session-model-controls.test.tsindex 655a3ef3b9..75a9d7c57d 100644--- a/server/drivers/acp/session-model-controls.test.ts+++ b/server/drivers/acp/session-model-controls.test.ts@@ -1,5 +1,5 @@import { describe, expect, it, vi } from "vitest";-import { SessionModelControls } from "./session-model-controls.ts";+import { pickSessionModel, SessionModelControls } from "./session-model-controls.ts";const thought = (value = "low") => ({ id: "thought_level", type: "select", category: "thought_level", currentValue: value,options: [{ value: "low", name: "Low" }, { value: "max", name: "Max" }] });@@ -89,3 +89,25 @@ describe("ACP session model controls", () => {expect(() => controls.variant("low")).toThrow("does not advertise variant");});});++describe("picking the session model before the setter runs", () => {+ const catalog = { default: "one", options: [{ id: "one", label: "One" }, { id: "two", label: "Two" }] };+ const pick = (support: Parameters<typeof pickSessionModel>[0], model: string, session: any = { sessionConfigResult: modern("one") }) =>+ pickSessionModel(support, { configId: "model", session, requested: { threadId: "t", text: "hi", model }, turn: { threadId: "t", text: "hi", model },+ variant: undefined, reportedModel: model, catalog, launchedThisTurn: true });++ it("maps a legacy pick onto the live catalog before the stale and fallback checks", () => {+ const normalizeSessionSelection = vi.fn((turn, offered: readonly string[]) => offered.includes("two") ? { ...turn, model: "two", variant: "max" } : turn);+ const picked = pick({ displayName: "Agent", normalizeSessionSelection, fallbackModel: () => "one" }, "two-max");+ expect(normalizeSessionSelection).toHaveBeenCalledWith(expect.objectContaining({ model: "two-max" }), ["one", "two"]);+ expect(picked).toMatchObject({ turn: { model: "two", variant: "max" }, variant: "max", reportedModel: "two-max", staleProcess: false, notice: null });+ });++ it("falls back to the driver's pick with one notice per session for a model the session no longer offers", () => {+ const session = { sessionConfigResult: modern("one") };+ const support = { displayName: "Agent", fallbackModel: () => "one" };+ expect(pick(support, "retired", session)).toMatchObject({ turn: { model: "one" }, reportedModel: "one", notice: expect.stringContaining("no longer offers retired") });+ expect(pick(support, "retired", session)).toMatchObject({ turn: { model: "one" }, notice: null });+ expect(pick({ displayName: "Agent" }, "retired", session)).toMatchObject({ turn: { model: "retired" }, notice: null });+ });+});diff --git a/server/drivers/acp/session-model-controls.ts b/server/drivers/acp/session-model-controls.tsindex 07a7333d4c..8d64d2b845 100644--- a/server/drivers/acp/session-model-controls.ts+++ b/server/drivers/acp/session-model-controls.ts@@ -1,5 +1,8 @@-import type { ModelVariantOption } from "../../contracts.ts";-import { acpModelOption, acpVariantOption } from "./config-options.ts";+import type { ModelCatalog, ModelVariantOption, SendTurnInput } from "../../contracts.ts";+import { decodeInjectId } from "../local-inject.ts";+import { acpModelOption, acpVariantOption, offeredModels } from "./config-options.ts";+import type { AcpSupport } from "./core.ts";+import type { AcpSession } from "./turn-state.ts";export interface AcpModelSelection {configId: string;@@ -94,3 +97,66 @@ export class SessionModelControls {return selected;}}++export interface SessionModelPick {+ turn: SendTurnInput;+ variant: string | undefined;+ /** The picker id the session's thought options are reported under. */+ reportedModel: string | undefined;+ /** The models this session offered, kept for the next launch's default-model env; null leaves the set alone. */+ accountModels: ReadonlySet<string> | null;+ /** A pooled process missing a catalog model: the turn fails and respawns rather than run another model. */+ staleProcess: boolean;+ /** Tell the person the session runs another model than the one picked; once per session for each such swap. */+ notice: string | null;+}++/** The model this turn asks the live session for, before the setter runs: a legacy id the driver maps+ * onto the session's catalog, or the driver's fallback for a model the session no longer offers. */+export function pickSessionModel(+ support: Pick<AcpSupport, "displayName" | "normalizeSessionSelection" | "fallbackModel" | "sessionModelEnv">,+ input: {+ configId: string;+ session: Pick<AcpSession, "sessionConfigResult" | "launchModel" | "fallbackNotice">;+ /** The turn as sent, with picker ids. */+ requested: SendTurnInput;+ /** The turn as the CLI runs it (a resolved wire model). */+ turn: SendTurnInput;+ variant: string | undefined;+ reportedModel: string | undefined;+ catalog: ModelCatalog;+ launchedThisTurn: boolean;+ },+): SessionModelPick {+ const { session, requested, catalog } = input;+ let { turn, variant, reportedModel } = input;+ const offered = offeredModels(session.sessionConfigResult, input.configId);+ const normalized = support.normalizeSessionSelection?.(turn, offered) ?? turn;+ if (normalized !== turn) {+ turn = normalized;+ variant = turn.variant;+ }+ const wanted = turn.model;+ const accountModels = support.sessionModelEnv && !session.launchModel && offered.length ? new Set(offered) : null;+ // A pooled process missing a catalog model predates the catalog: fail and respawn, never run another (the catalog holds picker ids; `wanted` may be the resolved wire id, so check both).+ const staleProcess = Boolean(support.fallbackModel && wanted && !input.launchedThisTurn && offered.length+ && !offered.includes(wanted) && catalog.options.some((option) => option.id === wanted || option.id === requested.model));+ let notice: string | null = null;+ // A local injected model is never swapped for the account's fallback.+ if (support.fallbackModel && offered.length && !decodeInjectId(requested.model)+ && !staleProcess && (!wanted || !offered.includes(wanted))) {+ const fallback = support.fallbackModel(session.sessionConfigResult, catalog.default);+ if (fallback && fallback !== wanted && offered.includes(fallback)) {+ const noticeKey = wanted ? `${wanted}\n${fallback}` : null;+ if (noticeKey && session.fallbackNotice !== noticeKey) {+ session.fallbackNotice = noticeKey;+ notice = `${support.displayName} no longer offers ${wanted}, so this conversation uses ${fallback}. ` ++ "Choose another model for this bot to stop seeing this.";+ }+ turn = { ...turn, model: fallback };+ reportedModel = fallback;+ variant = undefined;+ }+ }+ return { turn, variant, reportedModel, accountModels, staleProcess, notice };+}diff --git a/server/drivers/antigravity.test.ts b/server/drivers/antigravity.test.tsindex bce4df22d6..7c66536842 100644--- a/server/drivers/antigravity.test.ts+++ b/server/drivers/antigravity.test.ts@@ -38,6 +38,7 @@ import {AntigravityDriver,STATIC_ANTIGRAVITY_MODELS,antigravityPermissionMode,+ normalizeAntigravityTurn,} from "./antigravity.ts";const FAKE_ACP = join(dirname(fileURLToPath(import.meta.url)), "..", "testing", "fake-acp-cli.ts");@@ -74,11 +75,17 @@ afterEach(async () => {describe("official Antigravity catalog", () => {it("uses current Gemini 3.8 variants as the offline fallback", () => {expect(STATIC_ANTIGRAVITY_MODELS).toEqual({- default: "gemini-3.8-flash-high",+ default: "gemini-3.8-flash",options: [- { id: "gemini-3.8-flash-high", label: "Gemini 3.8 Flash (High)" },- { id: "gemini-3.8-flash-medium", label: "Gemini 3.8 Flash (Medium)" },- { id: "gemini-3.8-flash-low", label: "Gemini 3.8 Flash (Low)" },+ {+ id: "gemini-3.8-flash",+ label: "Gemini 3.8 Flash",+ variants: [+ { id: "low", label: "Low" },+ { id: "medium", label: "Medium" },+ { id: "high", label: "High" },+ ],+ },],});});@@ -717,9 +724,10 @@ describe("Antigravity driver over shared ACP", () => {GEMINI_API_KEY: "must-not-leak",GOOGLE_API_KEY: "also-must-not-leak",FAKE_ACP_AUTH_METHOD: "oauth-personal",- FAKE_ACP_MODELS: "gemini-3.8-flash-high,gemini-3.8-flash-low",+ FAKE_ACP_MODELS: "gemini-3.8-flash",FAKE_ACP_VARIANTS: JSON.stringify({- "gemini-3.8-flash-low": {+ "gemini-3.8-flash": {+ id: "thinking_level",currentValue: "medium",options: [{ value: "low", name: "Low" },@@ -743,8 +751,9 @@ describe("Antigravity driver over shared ACP", () => {text: "hello",approvalMode,resumeCursor: "fake-acp-session",- model: "gemini-3.8-flash-low",- variant: "high",+ // Existing bots can have the pre-variant fallback ID saved. The official+ // runtime only accepts the base model plus its thinking-level option.+ model: "gemini-3.8-flash-high",integrations: {custom: { docs: { command: "docs-mcp", args: ["serve"], env: { TOKEN: "scoped" } } },},@@ -762,9 +771,8 @@ describe("Antigravity driver over shared ACP", () => {expect(dumpState.env.AGY_ACP_DEFAULT_MODEL).toBeUndefined();const calls = JSON.parse(readFileSync(`${dump}.config.json`, "utf8"));expect(calls).toEqual([- { method: "session/set_config_option", params: { sessionId: "fake-acp-session", configId: "model", value: "gemini-3.8-flash-low" } },{ method: "session/set_config_option", params: { sessionId: "fake-acp-session", configId: "mode", value: approvalMode === "full" ? "yolo" : approvalMode === "edits" ? "auto_edit" : "default" } },- { method: "session/set_config_option", params: { sessionId: "fake-acp-session", configId: "effort", value: "high" } },+ { method: "session/set_config_option", params: { sessionId: "fake-acp-session", configId: "thinking_level", value: "high" } },]);const mcp = JSON.parse(readFileSync(`${dump}.mcp.json`, "utf8"));expect(mcp).toEqual([{ name: "docs", command: "docs-mcp", args: ["serve"], env: [{ name: "TOKEN", value: "scoped" }] }]);@@ -773,6 +781,77 @@ describe("Antigravity driver over shared ACP", () => {expect([dumpState.env.TEMP, dumpState.env.TMP]).toEqual([expectedTemp, expectedTemp]);});+ const legacyPickTurn = async (instanceId: string, sessionModels: string, model: string) => {+ ensureDirs();+ const fake = fakeRuntime();+ const dump = join(fake.directory, "dump.json");+ const tokenDirectory = join(antigravityProfileDirectory(instanceId), "antigravity-acp");+ mkdirSync(tokenDirectory, { recursive: true });+ writeFileSync(join(tokenDirectory, "acp_token.json"), "{}", { mode: 0o600 });+ instance = await AntigravityDriver.create({+ instanceId,+ displayName: "Antigravity",+ environment: {+ FAKE_ACP_AUTH_METHOD: "oauth-personal",+ FAKE_ACP_MODELS: sessionModels,+ FAKE_ACP_VARIANTS: JSON.stringify({+ "gemini-3.8-flash": {+ id: "thinking_level",+ currentValue: "high",+ options: [{ value: "low", name: "Low" }, { value: "medium", name: "Medium" }, { value: "high", name: "High" }],+ },+ }),+ FAKE_ACP_MODES: "default,yolo,auto_edit",+ FAKE_ACP_DUMP: dump,+ },+ enabled: true,+ config: { cli: fake.executable, fullAuto: false },+ });+ recorder = recordEvents(instance.adapter);+ const { turnId } = await instance.adapter.sendTurn({ threadId: "thread-legacy-pick", text: "hello", approvalMode: "ask", model, cwd: fake.directory });+ const done = await recorder.until((event) => event.type === "turn.completed" && event.turnId === turnId);+ const events = recorder.events.filter((event) => event.turnId === turnId);+ expect(events.filter((event) => event.type === "runtime.error")).toEqual([]);+ expect(done).toMatchObject({ ok: true });+ return {+ events,+ calls: JSON.parse(readFileSync(`${dump}.config.json`, "utf8")),+ selection: JSON.parse(readFileSync(`${dump}.selection.json`, "utf8")),+ };+ };++ it("runs a saved Gemini 3.8 Flash (Medium) pick as the base model at Medium thinking when the runtime offers only the base", async () => {+ const { events, calls, selection } = await legacyPickTurn("antigravity-legacy-medium", "gemini-3.8-flash", "gemini-3.8-flash-medium");+ expect(selection).toEqual({ sessionId: expect.any(String), model: "gemini-3.8-flash", variant: "medium" });+ // The session already runs the base model, so only the thinking level is set; never the retired id.+ expect(calls.filter((call: any) => call.params.configId !== "mode")).toEqual([+ { method: "session/set_config_option", params: { sessionId: selection.sessionId, configId: "thinking_level", value: "medium" } },+ ]);+ expect(events.find((event) => event.type === "session.started")).toMatchObject({ model: "gemini-3.8-flash" });+ // Thought options are still reported under the bot's saved pick, which the picker matches on.+ expect(events.filter((event) => event.type === "session.model-variants").at(-1)).toMatchObject({+ model: "gemini-3.8-flash-medium",+ variants: { currentValue: "medium" },+ });+ });++ it("keeps a suffixed Gemini 3.8 Flash pick unchanged when the runtime still offers it", async () => {+ const { calls, selection } = await legacyPickTurn("antigravity-suffixed-offered", "gemini-3.8-flash,gemini-3.8-flash-medium", "gemini-3.8-flash-medium");+ expect(selection).toEqual({ sessionId: expect.any(String), model: "gemini-3.8-flash-medium" });+ expect(calls.filter((call: any) => call.params.configId !== "mode")).toEqual([+ { method: "session/set_config_option", params: { sessionId: selection.sessionId, configId: "model", value: "gemini-3.8-flash-medium" } },+ ]);+ });++ it("maps only a retired suffixed id the session lacks, and never overrides a chosen thinking level", () => {+ const turn = { threadId: "t", text: "hi", model: "gemini-3.8-flash-low" };+ expect(normalizeAntigravityTurn(turn, ["gemini-3.8-flash"])).toEqual({ ...turn, model: "gemini-3.8-flash", variant: "low" });+ expect(normalizeAntigravityTurn(turn, ["gemini-3.8-flash", "gemini-3.8-flash-low"])).toBe(turn);+ expect(normalizeAntigravityTurn(turn, ["gemini-3.7-flash"])).toBe(turn);+ expect(normalizeAntigravityTurn({ ...turn, variant: "high" }, ["gemini-3.8-flash"])).toMatchObject({ model: "gemini-3.8-flash-low", variant: "high" });+ expect(normalizeAntigravityTurn({ ...turn, model: "gemini-3.8-pro-high" }, ["gemini-3.8-flash", "gemini-3.8-pro"])).toMatchObject({ model: "gemini-3.8-pro-high" });+ });+it("keeps one process across approval and model changes, starting on OMB's model", async () => {ensureDirs();const fake = fakeRuntime();diff --git a/server/drivers/antigravity.ts b/server/drivers/antigravity.tsindex 01828dcfcb..03be72ff9c 100644--- a/server/drivers/antigravity.ts+++ b/server/drivers/antigravity.ts@@ -8,6 +8,7 @@ import type {ProviderDriver,ProviderInstance,ProviderSnapshot,+ SendTurnInput,} from "../contracts.ts";import { createAcpDriver, type AcpConfig, type AcpSupport } from "./acp/core.ts";import {@@ -27,14 +28,31 @@ import { VERIFICATION_TEMP_KEY, scheduleAntigravityTempSweep } from "./antigraviimport { augmentedPath } from "../env-path.ts";export const STATIC_ANTIGRAVITY_MODELS: ModelCatalog = {- default: "gemini-3.8-flash-high",+ default: "gemini-3.8-flash",options: [- { id: "gemini-3.8-flash-high", label: "Gemini 3.8 Flash (High)" },- { id: "gemini-3.8-flash-medium", label: "Gemini 3.8 Flash (Medium)" },- { id: "gemini-3.8-flash-low", label: "Gemini 3.8 Flash (Low)" },+ {+ id: "gemini-3.8-flash",+ label: "Gemini 3.8 Flash",+ variants: [+ { id: "low", label: "Low" },+ { id: "medium", label: "Medium" },+ { id: "high", label: "High" },+ ],+ },],};+/** Bots saved before the thinking-level option hold `gemini-3.8-flash-{low,medium,high}`; the official runtime+ * offers only the base model with that option, so run the base at the saved level. A runtime that still offers+ * the suffixed id, or a turn that chose its own level, keeps its selection. */+export function normalizeAntigravityTurn(turn: SendTurnInput, offeredModels: readonly string[]): SendTurnInput {+ if (turn.variant !== undefined) return turn;+ const match = turn.model?.match(/^(gemini-3\.8-flash)-(low|medium|high)$/u);+ return match && offeredModels.includes(match[1]) && !offeredModels.includes(match[0])+ ? { ...turn, model: match[1], variant: match[2] }+ : turn;+}+/** Antigravity's session modes: `yolo` for Full, `auto_edit` for auto-accept edits, `default` otherwise (it has no reviewer for Auto). */export function antigravityPermissionMode(fullAuto: boolean, approvalMode?: ApprovalMode): "yolo" | "auto_edit" | "default" {if (fullAuto || approvalMode === "full") return "yolo";@@ -61,6 +79,7 @@ const support: AcpSupport = {spawnArgs: () => [],selectModel: { configId: "model" },modelVariants: true,+ normalizeSessionSelection: normalizeAntigravityTurn,// Google's server reads its new-session default from this variable (agy_acp_server 1.4.3// model_selection.get_default_model_id); a later switch re-fetches the model list (1.3-1.8 s).sessionModelEnv: "AGY_ACP_DEFAULT_MODEL",