Skip to content

Commit aca3c02

Browse files
authored
Merge pull request #1897 from iF2007/agent/fix-gemini-antigravity-model-discovery
fix(antigravity): match live agy model discovery
2 parents 2b12521 + 38c25ae commit aca3c02

6 files changed

Lines changed: 292 additions & 30 deletions

File tree

‎src/adapters/google.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ export function createGoogleAdapter(provider: OcxProviderConfig): ProviderAdapte
393393
? resolveAntigravityEffortWireModel(
394394
parsed.modelId,
395395
mapReasoningEffort(provider, parsed.modelId, parsed.options.reasoning),
396+
provider.baseUrl,
396397
).wireModelId
397398
: provider.googleMode === "vertex"
398399
? parsed.modelId
@@ -452,7 +453,11 @@ export function createGoogleAdapter(provider: OcxProviderConfig): ProviderAdapte
452453
if (!project) throw new Error("Antigravity requires a discovered Cloud Code Assist project id (re-run `ocx login google-antigravity`).");
453454
const sessionId = antigravitySessionId(parsed);
454455
const mappedEffort = mapReasoningEffort(provider, parsed.modelId, parsed.options.reasoning);
455-
const { wireModelId, thinkingLevel } = resolveAntigravityEffortWireModel(parsed.modelId, mappedEffort);
456+
const { wireModelId, thinkingLevel } = resolveAntigravityEffortWireModel(
457+
parsed.modelId,
458+
mappedEffort,
459+
provider.baseUrl,
460+
);
456461
antigravityModel = wireModelId;
457462
antigravitySession = sessionId;
458463
// Effort → thinkingConfig for CCA (CLIProxyAPI proven: request.generationConfig.thinkingConfig).

‎src/codex/catalog/provider-fetch.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import {
3838
type CapturedServiceTierAdapterAuthority,
3939
} from "../../providers/service-tier";
4040
import { effectiveGoogleMode, getProviderRegistryEntry, providerMatchesRegistryTransport } from "../../providers/registry";
41-
import { parseAntigravityAvailableModels } from "../../providers/antigravity-models";
41+
import { parseAntigravityAvailableModels, registerAntigravityDiscoveredWireModels } from "../../providers/antigravity-models";
4242
import { applyProviderContextCap, providerContextCap } from "../../providers/context-cap";
4343
import { routedSlug, slugEquals, slugsEquivalent } from "../../providers/slug-codec";
4444
import { CODEX_GPT5_IDENTITY_LINE } from "../../adapters/identity";
@@ -1374,6 +1374,10 @@ async function fetchProviderModelsWithAuth(
13741374
if (!setCached(name, forCache, Date.now(), cacheGeneration)) {
13751375
return observed(withConfiguredRetention(configured), "degraded");
13761376
}
1377+
registerAntigravityDiscoveredWireModels(prov.baseUrl, antigravity, {
1378+
provider: name,
1379+
cacheGeneration,
1380+
});
13771381
markProviderDiscoveryOk(name, live.length);
13781382
return observed(withConfiguredRetention(forCache, { warnDrops: true }), "authoritative");
13791383
}

‎src/providers/antigravity-models.ts‎

Lines changed: 112 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { isValidModelDiscoveryModelId, MODEL_DISCOVERY_MAX_MODELS } from "./model-discovery-limits";
2+
import { isModelCacheGenerationCurrent } from "../codex/model-cache";
23

34
// Google Antigravity (Cloud Code Assist) bundled model list.
45
//
@@ -74,8 +75,12 @@ const ANTIGRAVITY_DISCOVERY_EFFORTS = ["low", "medium", "high"] as const;
7475

7576
function pickerModelIdForDiscoveredWireId(
7677
wireId: string,
78+
info: Record<string, unknown>,
7779
available: ReadonlyMap<string, Record<string, unknown>>,
7880
): string {
81+
const displayModelId = antigravityDisplayModelId(info.displayName, wireId);
82+
if (displayModelId) return displayModelId;
83+
7984
const explicitPickerId = Object.hasOwn(ANTIGRAVITY_PICKER_MODEL_BY_WIRE_ID, wireId)
8085
? ANTIGRAVITY_PICKER_MODEL_BY_WIRE_ID[wireId]
8186
: undefined;
@@ -240,6 +245,8 @@ export const ANTIGRAVITY_MODEL_INPUT_MODALITIES: Record<string, string[]> = {
240245

241246
export interface AntigravityAvailableModel {
242247
id: string;
248+
/** CCA model id used by the agent envelope when `id` comes from display metadata. */
249+
wireModelId: string;
243250
contextWindow?: number;
244251
inputModalities?: string[];
245252
}
@@ -254,6 +261,86 @@ function antigravityPositiveInteger(value: unknown): number | undefined {
254261
return typeof value === "number" && Number.isSafeInteger(value) && value > 0 ? value : undefined;
255262
}
256263

264+
interface DiscoveredWireModelMapping {
265+
readonly models: ReadonlyMap<string, string>;
266+
readonly generation?: { provider: string; cacheGeneration: string };
267+
}
268+
269+
const discoveredWireModelsByBaseUrl = new Map<string, DiscoveredWireModelMapping>();
270+
271+
function antigravityBaseUrlKey(baseUrl: string | undefined): string | undefined {
272+
if (typeof baseUrl !== "string" || !baseUrl.trim()) return undefined;
273+
const trimmed = baseUrl.trim().replace(/\/+$/, "");
274+
try {
275+
const url = new URL(trimmed);
276+
url.hash = "";
277+
url.search = "";
278+
return url.toString().replace(/\/+$/, "").toLowerCase();
279+
} catch {
280+
return trimmed.toLowerCase();
281+
}
282+
}
283+
284+
/** Remember the wire ids returned by one live CCA discovery for request routing. */
285+
export function registerAntigravityDiscoveredWireModels(
286+
baseUrl: string | undefined,
287+
models: readonly AntigravityAvailableModel[],
288+
generation?: { provider: string; cacheGeneration: string },
289+
): void {
290+
const key = antigravityBaseUrlKey(baseUrl);
291+
if (!key) return;
292+
const wireModels = new Map<string, string>();
293+
for (const model of models) wireModels.set(model.id, model.wireModelId);
294+
discoveredWireModelsByBaseUrl.set(key, {
295+
models: wireModels,
296+
...(generation ? { generation } : {}),
297+
});
298+
}
299+
300+
function discoveredAntigravityWireModelId(
301+
modelId: string,
302+
baseUrl: string | undefined,
303+
): string | undefined {
304+
const key = antigravityBaseUrlKey(baseUrl);
305+
if (!key) return undefined;
306+
const mapping = discoveredWireModelsByBaseUrl.get(key);
307+
if (!mapping) return undefined;
308+
if (mapping.generation
309+
&& !isModelCacheGenerationCurrent(mapping.generation.provider, mapping.generation.cacheGeneration)) {
310+
discoveredWireModelsByBaseUrl.delete(key);
311+
return undefined;
312+
}
313+
return mapping.models.get(modelId);
314+
}
315+
316+
/**
317+
* Convert the CCA display label used by `agy` into its public model selector.
318+
*
319+
* The wire id is authoritative for requests, while the label is authoritative for the
320+
* user-facing selector when Google has renamed or re-tiered a model. Keep both instead
321+
* of maintaining a provider-specific list of known model names.
322+
*/
323+
function antigravityDisplayModelId(displayName: unknown, wireId: string): string | undefined {
324+
if (typeof displayName !== "string") return undefined;
325+
const label = displayName.trim();
326+
if (!label || label.length > 512) return undefined;
327+
const slug = (replaceDots: boolean): string => label
328+
.normalize("NFKC")
329+
.toLowerCase()
330+
.replace(replaceDots ? /\./g : /\s+/g, replaceDots ? "-" : " ")
331+
.replace(/[^a-z0-9.-]+/g, "-")
332+
.replace(/-+/g, "-")
333+
.replace(/^-|-$/g, "");
334+
const preserved = slug(false);
335+
const compact = slug(true);
336+
if (!isValidModelDiscoveryModelId(preserved) && !isValidModelDiscoveryModelId(compact)) return undefined;
337+
if (preserved === wireId || compact === wireId
338+
|| preserved === `${wireId}-thinking` || compact === `${wireId}-thinking`) {
339+
return wireId;
340+
}
341+
return isValidModelDiscoveryModelId(preserved) ? preserved : compact;
342+
}
343+
257344
/**
258345
* Extract the CCA models that are valid for agent requests. The endpoint also returns tab,
259346
* command, commit-message, transcription, and standalone image-generation models; those are not
@@ -288,13 +375,6 @@ export function parseAntigravityAvailableModels(
288375
}
289376
}
290377
}
291-
// This model is exposed by Antigravity's agent chat surface even though it is grouped under
292-
// image generation in the discovery response.
293-
if (Array.isArray(body.imageGenerationModelIds)
294-
&& body.imageGenerationModelIds.includes("gemini-3.1-flash-image")) {
295-
if (ids.length >= limit) return null;
296-
ids.push("gemini-3.1-flash-image");
297-
}
298378
// Newer CCA responses identify tiered Flash models through this index instead of
299379
// adding their synthetic wire ids to agentModelSorts.
300380
const tieredModelIds = antigravityRecord(body.tieredModelIds);
@@ -305,6 +385,12 @@ export function parseAntigravityAvailableModels(
305385
|| !Object.hasOwn(models, id)
306386
|| !antigravityRecord(models[id])
307387
|| ids.length >= limit) return null;
388+
const baseId = id.endsWith("-tiered") ? id.slice(0, -"-tiered".length) : id;
389+
if (ids.some(agentId =>
390+
agentId === id
391+
|| agentId === baseId
392+
|| ANTIGRAVITY_DISCOVERY_EFFORTS.some(effort => agentId === `${baseId}-${effort}`)
393+
)) continue;
308394
ids.push(id);
309395
}
310396
}
@@ -313,23 +399,18 @@ export function parseAntigravityAvailableModels(
313399
for (const wireId of ids) {
314400
const info = antigravityRecord(models[wireId]);
315401
if (!info || available.has(wireId)) continue;
316-
// Legacy compatibility aliases are deliberately routed to newer wire ids for saved
317-
// selections. They are not safe as independently discovered picker rows.
318-
const alias = Object.hasOwn(ANTIGRAVITY_MODEL_ALIASES, wireId)
319-
? ANTIGRAVITY_MODEL_ALIASES[wireId]
320-
: undefined;
321-
if (alias && alias !== wireId) continue;
322402
available.set(wireId, info);
323403
}
324404

325405
const out: AntigravityAvailableModel[] = [];
326406
const seen = new Set<string>();
327407
for (const [wireId, info] of available) {
328-
const id = pickerModelIdForDiscoveredWireId(wireId, available);
408+
const id = pickerModelIdForDiscoveredWireId(wireId, info, available);
329409
if (seen.has(id)) continue;
330410
seen.add(id);
331411
out.push({
332412
id,
413+
wireModelId: wireId,
333414
...(antigravityPositiveInteger(info.maxTokens) ? { contextWindow: antigravityPositiveInteger(info.maxTokens) } : {}),
334415
// Tri-state, deliberately not a ternary: `true` asserts image support,
335416
// `false` asserts against it, and ABSENT is unknown. Collapsing absent into
@@ -347,7 +428,9 @@ export function parseAntigravityAvailableModels(
347428
return out;
348429
}
349430

350-
export function resolveAntigravityWireModelId(modelId: string): string {
431+
export function resolveAntigravityWireModelId(modelId: string, baseUrl?: string): string {
432+
const discovered = discoveredAntigravityWireModelId(modelId, baseUrl);
433+
if (discovered) return discovered;
351434
return Object.hasOwn(ANTIGRAVITY_MODEL_ALIASES, modelId)
352435
? ANTIGRAVITY_MODEL_ALIASES[modelId]
353436
: modelId;
@@ -380,7 +463,19 @@ export function retiredAntigravityFlashTier(modelId: string): string | undefined
380463
export function resolveAntigravityEffortWireModel(
381464
modelId: string,
382465
effort?: string,
466+
baseUrl?: string,
383467
): { wireModelId: string; thinkingLevel?: string } {
468+
const discoveredWireModelId = discoveredAntigravityWireModelId(modelId, baseUrl);
469+
if (discoveredWireModelId && (discoveredWireModelId !== modelId || isAntigravitySuffixModelId(modelId))) {
470+
const defaultLevel = ANTIGRAVITY_THINKING_LEVEL_MODELS[modelId];
471+
return {
472+
wireModelId: discoveredWireModelId,
473+
...(defaultLevel
474+
? { thinkingLevel: effort ? resolveAntigravityThinkingLevel(effort) ?? defaultLevel : defaultLevel }
475+
: {}),
476+
};
477+
}
478+
384479
// Rule 0: retired Flash id — Google has taken the wire id offline, so route to the
385480
// current generation and carry the tier the retired id encoded. This runs BEFORE the
386481
// suffix check because those ids are aliases, and rule 1 would drop the tier.
@@ -394,7 +489,7 @@ export function resolveAntigravityEffortWireModel(
394489

395490
// Rule 1: suffix/compat alias — suffix IS the effort.
396491
if (isAntigravitySuffixModelId(modelId)) {
397-
return { wireModelId: resolveAntigravityWireModelId(modelId) };
492+
return { wireModelId: resolveAntigravityWireModelId(modelId, baseUrl) };
398493
}
399494

400495
// Rule 1b: single-wire-id Gemini model whose tiers ride on thinkingLevel. Without
@@ -424,7 +519,7 @@ export function resolveAntigravityEffortWireModel(
424519
}
425520

426521
// Rule 5: everything else.
427-
return { wireModelId: resolveAntigravityWireModelId(modelId) };
522+
return { wireModelId: resolveAntigravityWireModelId(modelId, baseUrl) };
428523
}
429524

430525

‎tests/gemini-37-flash-migration.test.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ describe("3.7 reasoning control", () => {
110110
});
111111
});
112112

113-
describe("stale discovery cannot republish a retired model", () => {
114-
test("a CCA payload still listing 3.6 tiers yields no retired picker row", () => {
113+
describe("live discovery follows the CCA agent catalog", () => {
114+
test("a CCA payload still listing 3.6 tiers preserves those live rows", () => {
115115
const payload = {
116116
models: Object.fromEntries(
117117
["gemini-3.6-flash-low", "gemini-3.6-flash-medium", "gemini-3.6-flash-high", "gemini-3.7-flash"]
@@ -124,10 +124,12 @@ describe("stale discovery cannot republish a retired model", () => {
124124
}],
125125
};
126126
const ids = parseAntigravityAvailableModels(payload)?.map(model => model.id) ?? [];
127-
expect(ids).toContain("gemini-3.7-flash");
128-
for (const retired of Object.keys(RETIRED_TIERS)) {
129-
expect(ids).not.toContain(retired);
130-
}
127+
expect(ids).toEqual([
128+
"gemini-3.6-flash-low",
129+
"gemini-3.6-flash-medium",
130+
"gemini-3.6-flash-high",
131+
"gemini-3.7-flash",
132+
]);
131133
});
132134
});
133135

0 commit comments

Comments
 (0)