Skip to content

Commit ccba496

Browse files
committed
fix(grok): harden orphan ownership classification
1 parent 9a9bace commit ccba496

7 files changed

Lines changed: 406 additions & 69 deletions

File tree

‎src/grok/catalog.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { comboPublicModelId } from "../combos";
2+
import {
3+
filterCatalogVisibleModels,
4+
nativeContextLimits,
5+
nativeOpenAiContextWindow,
6+
nativeOpenAiSlugs,
7+
visibleNativeSlugs,
8+
type CatalogModel,
9+
} from "../codex/catalog";
10+
import type { OcxConfig } from "../types";
11+
import type { GrokInjectModel } from "./inject";
12+
13+
export interface GrokCatalogProjection {
14+
models: GrokInjectModel[];
15+
catalogModelIds: ReadonlySet<string>;
16+
disabledProviderNamespaces: ReadonlySet<string>;
17+
comboPublicModelIds: ReadonlySet<string>;
18+
}
19+
20+
/**
21+
* Project one fetched catalog into both emitted Grok rows and orphan-classification evidence.
22+
* Keeping this shared prevents `ocx start` and the management toggle from disagreeing.
23+
*/
24+
export function projectGrokCatalog(
25+
allRouted: CatalogModel[],
26+
config: OcxConfig,
27+
): GrokCatalogProjection {
28+
const routed = filterCatalogVisibleModels(allRouted, config);
29+
const limits = nativeContextLimits(config);
30+
return {
31+
catalogModelIds: new Set([
32+
...nativeOpenAiSlugs(),
33+
...allRouted.map(model => model.alias ?? `${model.provider}/${model.id}`),
34+
]),
35+
disabledProviderNamespaces: new Set(
36+
Object.entries(config.providers)
37+
.filter(([, provider]) => provider?.disabled === true)
38+
.map(([name]) => name),
39+
),
40+
comboPublicModelIds: new Set(
41+
Object.entries(config.combos ?? {})
42+
.map(([id, combo]) => comboPublicModelId(id, combo)),
43+
),
44+
models: [
45+
...visibleNativeSlugs(config).map(id => {
46+
const contextWindow = nativeOpenAiContextWindow(id, limits);
47+
return { id, ...(contextWindow !== undefined ? { contextWindow } : {}) };
48+
}),
49+
...routed.map(model => ({
50+
id: model.alias ?? `${model.provider}/${model.id}`,
51+
...(model.contextWindow !== undefined ? { contextWindow: model.contextWindow } : {}),
52+
})),
53+
],
54+
};
55+
}

‎src/grok/inject.ts‎

Lines changed: 94 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,17 @@ function userModelAliases(content: string, region: ManagedRegion | null): Set<st
121121
return aliases;
122122
}
123123

124-
/**
125-
* The api_key literal every generated entry carries. It is the STRONG ownership signal:
126-
* a value we mint, that a human has no reason to type by hand.
127-
*/
124+
/** The api_key literal every generated entry carries. It is necessary, but not ownership alone. */
128125
const OPENCODEX_API_KEY = "opencodex-loopback";
126+
const OPENCODEX_GROK_MARKER = "x-opencodex-grok";
129127

130128
/** A plain `[model.<alias>]` table outside the fence that opencodex itself wrote. */
131129
interface OrphanTable {
132130
alias: string;
133131
/** The model id this entry routes to — used to find its replacement alias. */
134-
modelId: string | undefined;
132+
modelId: string;
133+
/** Explicit markers authorize teardown; legacy fingerprints authorize replacement only. */
134+
ownership: "explicit" | "legacy";
135135
/** Offsets into the NORMALIZED content: header start .. next header start (or EOF). */
136136
start: number;
137137
end: number;
@@ -164,6 +164,44 @@ function isLoopbackBaseUrl(value: string | undefined): boolean {
164164
}
165165
}
166166

167+
/** Exact marker emitted inside every modern generated model table. */
168+
function hasInlineOwnershipMarker(value: string | undefined): boolean {
169+
return value !== undefined
170+
&& /^\{[ \t]*["']x-opencodex-grok["'][ \t]*=[ \t]*["']1["'][ \t]*\}$/.test(value);
171+
}
172+
173+
/** Historical deterministic alias, including collision suffixes allocated by the writer. */
174+
function isGeneratedAliasForModel(alias: string, modelId: string): boolean {
175+
const base = `ocx-${modelId.replace(/[^A-Za-z0-9_-]/g, "-")}`;
176+
if (alias === base) return true;
177+
if (!alias.startsWith(`${base}-`)) return false;
178+
const suffix = alias.slice(base.length + 1);
179+
return /^[1-9][0-9]*$/.test(suffix) && Number(suffix) >= 2;
180+
}
181+
182+
/** Pre-marker auto-generated row shape. Manual rows never carried the generated name. */
183+
function isLegacyGeneratedTable(alias: string, keys: ReadonlyMap<string, string>): boolean {
184+
const modelId = keys.get("model");
185+
return modelId !== undefined
186+
&& modelId.length > 0
187+
&& keys.get("api_backend") === "chat_completions"
188+
&& keys.get("name") === `OCX ${modelId}`
189+
&& isGeneratedAliasForModel(alias, modelId);
190+
}
191+
192+
/** Classify a direct provider/model id without stealing a slash-shaped configured combo alias. */
193+
function isDisabledProviderModelId(
194+
modelId: string,
195+
disabledProviderNamespaces: ReadonlySet<string> | undefined,
196+
comboPublicModelIds: ReadonlySet<string> | undefined,
197+
): boolean {
198+
if (!disabledProviderNamespaces || comboPublicModelIds?.has(modelId)) return false;
199+
const slash = modelId.indexOf("/");
200+
return slash > 0
201+
&& slash < modelId.length - 1
202+
&& disabledProviderNamespaces.has(modelId.slice(0, slash));
203+
}
204+
167205
/**
168206
* Model tables OUTSIDE the fence that opencodex itself wrote (#511).
169207
*
@@ -174,12 +212,16 @@ function isLoopbackBaseUrl(value: string | undefined): boolean {
174212
* resolves the original, finds no `context_window`, and falls back to its own 200k.
175213
*
176214
* Ownership is CONJUNCTIVE and deliberately strict, because a false positive deletes a
177-
* hand-written user model:
215+
* hand-written user model. The public manual recipe intentionally uses the same loopback key,
216+
* endpoint, and Responses backend, so those fields are not ownership proof. We additionally
217+
* require either the durable generated marker or the exact pre-marker legacy fingerprint:
178218
* - a plain `[model.x]` header (never `[[model.x]]` / `[model.x.sub]` — those spellings
179219
* mark human authorship and stay reserved);
180220
* - `api_key` equal to our own literal;
181221
* - a loopback `base_url`, so an entry that merely copied our key while pointing at a
182222
* remote host is left alone.
223+
* - `x-opencodex-grok = "1"` in generated inline/child extra_headers, OR the historical
224+
* chat_completions + `name = "OCX <model>"` + deterministic generated alias shape.
183225
* A loopback base_url ALONE is not enough: aiming your own model at the local proxy is a
184226
* legitimate thing to do.
185227
*/
@@ -213,6 +255,9 @@ function findOpencodexOrphans(content: string, region: ManagedRegion | null): Or
213255
const keys = tableBodyKeys(content.slice(header.index + header.length, bodyEnd));
214256
if (keys.get("api_key") !== OPENCODEX_API_KEY) continue;
215257
if (!isLoopbackBaseUrl(keys.get("base_url"))) continue;
258+
const modelId = keys.get("model");
259+
if (!modelId) continue;
260+
let hasOwnershipMarker = hasInlineOwnershipMarker(keys.get("extra_headers"));
216261
// Swallow the entry's OWN sub-tables (`[model.<alias>.extra_headers]`). Grok writes
217262
// them when it re-serializes the file, and leaving one behind keeps the alias
218263
// reserved by `userModelAliases` — so the sweep would remove the parent and STILL
@@ -226,9 +271,22 @@ function findOpencodexOrphans(content: string, region: ManagedRegion | null): Or
226271
if (fenceStart >= 0 && header.index < fenceStart && child.index >= fenceStart) break;
227272
if (child.segments.length <= 2) break;
228273
if (child.segments[0] !== "model" || child.segments[1] !== header.segments[1]) break;
229-
end = clampEnd(header.index, headers[next + 1]?.index ?? content.length);
274+
const childEnd = clampEnd(header.index, headers[next + 1]?.index ?? content.length);
275+
if (!child.array && child.segments.length === 3 && child.segments[2] === "extra_headers") {
276+
const childKeys = tableBodyKeys(content.slice(child.index + child.length, childEnd));
277+
if (childKeys.get(OPENCODEX_GROK_MARKER) === "1") hasOwnershipMarker = true;
278+
}
279+
end = childEnd;
230280
}
231-
orphans.push({ alias: header.segments[1]!, modelId: keys.get("model"), start: header.index, end });
281+
const legacyGenerated = isLegacyGeneratedTable(header.segments[1]!, keys);
282+
if (!hasOwnershipMarker && !legacyGenerated) continue;
283+
orphans.push({
284+
alias: header.segments[1]!,
285+
modelId,
286+
ownership: hasOwnershipMarker ? "explicit" : "legacy",
287+
start: header.index,
288+
end,
289+
});
232290
}
233291
return orphans;
234292
}
@@ -352,6 +410,10 @@ export function injectGrokConfig(
352410
excluded?: ReadonlySet<string>;
353411
/** Unfiltered known ids used only to distinguish hidden current models from retired ones. */
354412
catalogModelIds?: ReadonlySet<string>;
413+
/** Canonical provider keys disabled in config and therefore absent from catalog fetching. */
414+
disabledProviderNamespaces?: ReadonlySet<string>;
415+
/** Configured combo public ids that may syntactically resemble provider/model ids. */
416+
comboPublicModelIds?: ReadonlySet<string>;
355417
} = {},
356418
): GrokInjectResult {
357419
const grokHome = resolveGrokHome(opts.grokHome);
@@ -403,13 +465,25 @@ export function injectGrokConfig(
403465
// Adopt our own pre-fence entries (#511) BEFORE reserving user aliases, so the stale
404466
// duplicate is replaced instead of routed around forever. Runs inside the normalized
405467
// window so the user's dominant EOL is still restored below.
406-
// Use the full UNFILTERED catalog, not the emitted subset: explicitly excluded and otherwise
407-
// hidden current models must still lose stale unfenced tables, or those tables would bypass
408-
// the user's visibility choice. Direct callers that do not have a separate catalog keep the
409-
// historical `models` behavior.
468+
// Durably marked rows use the full UNFILTERED catalog: explicitly excluded and otherwise
469+
// hidden current models must still lose stale generated tables. Ambiguous pre-marker legacy
470+
// rows are migrated only when this write emits their replacement. Direct callers that do not
471+
// have a separate catalog keep the historical `models` behavior.
410472
const catalogModelIds = opts.catalogModelIds ?? new Set(models.map(model => model.id));
473+
const emittedModelIds = new Set(models
474+
.filter(model => !opts.excluded?.has(model.id))
475+
.map(model => model.id));
411476
const orphans = findOpencodexOrphans(originalContent, originalRegion)
412-
.filter(orphan => orphan.modelId !== undefined && catalogModelIds.has(orphan.modelId));
477+
.filter(orphan => orphan.ownership === "legacy"
478+
// A legacy fingerprint is not durable deletion authority. Migrate it only when this
479+
// same write will replace the row with a marked managed table.
480+
? emittedModelIds.has(orphan.modelId)
481+
: catalogModelIds.has(orphan.modelId)
482+
|| isDisabledProviderModelId(
483+
orphan.modelId,
484+
opts.disabledProviderNamespaces,
485+
opts.comboPublicModelIds,
486+
));
413487
const content = removeOrphanTables(originalContent, orphans);
414488
// Removing bytes above the fence MOVES it: recompute rather than adjust arithmetic,
415489
// so the splice below cannot cut the file in the wrong place.
@@ -443,7 +517,7 @@ export function injectGrokConfig(
443517
}
444518
const renames = new Map<string, string>();
445519
for (const orphan of orphans) {
446-
const replacement = orphan.modelId === undefined ? undefined : survivors.get(orphan.modelId);
520+
const replacement = survivors.get(orphan.modelId);
447521
if (replacement && replacement !== orphan.alias) renames.set(orphan.alias, replacement);
448522
}
449523
nextContent = rewriteAliasReferences(nextContent, renames);
@@ -512,16 +586,19 @@ export function stripGrokConfig(opts: { grokHome?: string } = {}): GrokInjectRes
512586
else if (restOfFile.length === 0 && prefix.endsWith("\n")) prefix = prefix.slice(0, -1);
513587
// Keep the old fence boundary while sweeping. Concatenating first would let the last
514588
// pre-fence orphan absorb comment-only or bare-key user content appended after the fence.
515-
const prefixOrphans = findOpencodexOrphans(prefix, null);
516-
const tailOrphans = findOpencodexOrphans(restOfFile, null);
589+
const prefixOrphans = findOpencodexOrphans(prefix, null)
590+
.filter(orphan => orphan.ownership === "explicit");
591+
const tailOrphans = findOpencodexOrphans(restOfFile, null)
592+
.filter(orphan => orphan.ownership === "explicit");
517593
orphanCount = prefixOrphans.length + tailOrphans.length;
518594
stripped = removeOrphanTables(prefix, prefixOrphans)
519595
+ removeOrphanTables(restOfFile, tailOrphans);
520596
} else {
521597
// Retired or otherwise non-emitted OpenCodex tables may intentionally remain outside the
522598
// fence while the integration is enabled. Teardown owns those strictly identified tables
523599
// even after Grok has re-serialized the file and dropped our marker comments.
524-
const orphans = findOpencodexOrphans(content, null);
600+
const orphans = findOpencodexOrphans(content, null)
601+
.filter(orphan => orphan.ownership === "explicit");
525602
if (orphans.length === 0) {
526603
return { ok: true, changed: false, message: "No opencodex managed block found in Grok config." };
527604
}

‎src/grok/sync.ts‎

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
*
77
* Deps are injectable (mirrors src/codex/sync.ts) so tests can run without a live proxy.
88
*/
9-
import { visibleNativeSlugs, filterCatalogVisibleModels, nativeContextLimits, nativeOpenAiContextWindow, nativeOpenAiSlugs, type CatalogModel } from "../codex/catalog";
9+
import type { CatalogModel } from "../codex/catalog";
1010
import type { OcxConfig } from "../types";
11-
import { injectGrokConfig, type GrokInjectModel, type GrokInjectResult } from "./inject";
11+
import { projectGrokCatalog } from "./catalog";
12+
import { injectGrokConfig, type GrokInjectResult } from "./inject";
1213

1314
export interface GrokSyncDeps {
1415
fetchAllModels: (config: OcxConfig) => Promise<CatalogModel[]>;
@@ -32,28 +33,10 @@ export async function syncGrokConfig(
3233
opts: { hostname?: string; grokHome?: string } = {},
3334
deps: GrokSyncDeps = { fetchAllModels: defaultFetchAllModels, injectGrokConfig },
3435
): Promise<GrokInjectResult> {
35-
let models: GrokInjectModel[];
36-
let catalogModelIds: Set<string>;
36+
let projection: ReturnType<typeof projectGrokCatalog>;
3737
try {
3838
const allRouted = await deps.fetchAllModels(config);
39-
const routed = filterCatalogVisibleModels(allRouted, config);
40-
catalogModelIds = new Set([
41-
...nativeOpenAiSlugs(),
42-
...allRouted.map(model => model.alias ?? `${model.provider}/${model.id}`),
43-
]);
44-
models = [
45-
// Native slugs carry their context window too. Without it Grok falls back to its own
46-
// default (200k) and understates models like gpt-5.6-sol, which is 372k. This is the same
47-
// accessor the dashboard's native rows use, so the two cannot disagree.
48-
...visibleNativeSlugs(config).map(id => {
49-
const contextWindow = nativeOpenAiContextWindow(id, nativeContextLimits(config));
50-
return { id, ...(contextWindow !== undefined ? { contextWindow } : {}) };
51-
}),
52-
...routed.map(m => ({
53-
id: m.alias ?? `${m.provider}/${m.id}`,
54-
...(m.contextWindow !== undefined ? { contextWindow: m.contextWindow } : {}),
55-
})),
56-
];
39+
projection = projectGrokCatalog(allRouted, config);
5740
} catch (err) {
5841
return {
5942
ok: false,
@@ -64,10 +47,12 @@ export async function syncGrokConfig(
6447
// Pass the FULL list plus the exclusion set: the writer allocates aliases over
6548
// everything and emits only what is switched on, so a model's alias never depends on
6649
// its neighbours' switches. Absent/empty selection keeps today's behaviour exactly.
67-
return deps.injectGrokConfig(port, models, {
50+
return deps.injectGrokConfig(port, projection.models, {
6851
...(opts.hostname !== undefined ? { hostname: opts.hostname } : {}),
6952
...(opts.grokHome !== undefined ? { grokHome: opts.grokHome } : {}),
7053
excluded: new Set(config.grokExcludedModels ?? []),
71-
catalogModelIds,
54+
catalogModelIds: projection.catalogModelIds,
55+
disabledProviderNamespaces: projection.disabledProviderNamespaces,
56+
comboPublicModelIds: projection.comboPublicModelIds,
7257
});
7358
}

‎src/server/management/native-integration-routes.ts‎

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
*/
2020
import { loadConfig, saveConfigPreservingClaudeCode } from "../../config";
2121
import { readRuntimePort } from "../../config/process-state";
22-
import { desktopVisibleNativeSlugs, filterCatalogVisibleModels, nativeContextLimits, nativeOpenAiContextWindow, nativeOpenAiSlugs, visibleNativeSlugs } from "../../codex/catalog";
22+
import { desktopVisibleNativeSlugs, filterCatalogVisibleModels, nativeContextLimits } from "../../codex/catalog";
2323
import { providerContextCap } from "../../providers/context-cap";
2424
import { OPENAI_CODEX_PROVIDER_ID } from "../../providers/openai-tiers";
2525
import { inspectDesktop3pConfigLibrary, removeDesktop3pStandardPivot, writeDesktop3pConfig } from "../../claude/desktop-3p";
26-
import { injectGrokConfig, stripGrokConfig, type GrokInjectModel } from "../../grok/inject";
26+
import { projectGrokCatalog } from "../../grok/catalog";
27+
import { injectGrokConfig, stripGrokConfig } from "../../grok/inject";
2728
import { inspectGrokConfig } from "../../grok/inspect";
2829
import { grokConfigPath } from "../../grok/status";
2930
import { assertNativeTeardownOwned } from "../../integrations/native/ownership-preflight";
@@ -502,27 +503,10 @@ async function handleGrokToggle(ctx: ManagementContext): Promise<Response> {
502503
* synchronous from entry (012 §One preflight is not enough).
503504
*/
504505
const fetchModels = deps.fetchAllModels ?? defaultFetchAllModels;
505-
let models: GrokInjectModel[];
506-
let catalogModelIds: Set<string>;
506+
let projection: ReturnType<typeof projectGrokCatalog>;
507507
try {
508508
const allRouted = await fetchModels(config);
509-
const routed = filterCatalogVisibleModels(allRouted, config);
510-
catalogModelIds = new Set([
511-
...nativeOpenAiSlugs(),
512-
...allRouted.map(model => model.alias ?? `${model.provider}/${model.id}`),
513-
]);
514-
models = [
515-
// Native slugs carry their context window: without it Grok falls back
516-
// to its own 200k default and understates a 372k model.
517-
...visibleNativeSlugs(config).map(id => {
518-
const contextWindow = nativeOpenAiContextWindow(id, nativeContextLimits(config));
519-
return { id, ...(contextWindow !== undefined ? { contextWindow } : {}) };
520-
}),
521-
...routed.map(m => ({
522-
id: m.alias ?? `${m.provider}/${m.id}`,
523-
...(m.contextWindow !== undefined ? { contextWindow: m.contextWindow } : {}),
524-
})),
525-
];
509+
projection = projectGrokCatalog(allRouted, config);
526510
} catch (error) {
527511
// A catalog failure must never write an empty fence (syncGrokConfig
528512
// guards this; the route inherits the rule). Nothing was written.
@@ -535,15 +519,17 @@ async function handleGrokToggle(ctx: ManagementContext): Promise<Response> {
535519
if (recheck.kind === "orphaned_marker") return postCommitRefusal(409, "grok", "orphaned_marker", ORPHANED_MARKER_MESSAGE, { desiredEnabled });
536520

537521
const inject = deps.injectGrokConfig ?? injectGrokConfig;
538-
const result = inject(port, models, {
522+
const result = inject(port, projection.models, {
539523
...(hostname !== undefined ? { hostname } : {}),
540524
// The FULL list plus the exclusion set, never a pre-filtered list: the
541525
// writer allocates aliases over everything, so a model's alias never
542526
// depends on its neighbours' switches.
543527
excluded: new Set(config.grokExcludedModels ?? []),
544528
// Visibility filters decide what to emit, not whether an owned pre-fence table is still
545529
// current. Otherwise a hidden model is mistaken for retired state and survives outside.
546-
catalogModelIds,
530+
catalogModelIds: projection.catalogModelIds,
531+
disabledProviderNamespaces: projection.disabledProviderNamespaces,
532+
comboPublicModelIds: projection.comboPublicModelIds,
547533
});
548534

549535
if (result.skippedReason === "non-loopback") {

0 commit comments

Comments
 (0)