Skip to content

Commit 9a9bace

Browse files
committed
fix(grok): close orphan lifecycle review gaps
1 parent 0df51fd commit 9a9bace

6 files changed

Lines changed: 261 additions & 37 deletions

File tree

‎src/grok/inject.ts‎

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,11 @@ function tableBodyKeys(body: string): Map<string, string> {
144144
const match = /^[ \t]*([A-Za-z0-9_-]+)[ \t]*=[ \t]*(.*?)[ \t]*$/.exec(line);
145145
if (!match) continue;
146146
const raw = match[2]!;
147-
const value = raw.startsWith('"') && raw.endsWith('"') && raw.length >= 2
147+
const value = raw.length >= 2 && raw.startsWith('"') && raw.endsWith('"')
148148
? decodeTomlBasicString(raw.slice(1, -1))
149-
: raw;
149+
: raw.length >= 2 && raw.startsWith("'") && raw.endsWith("'")
150+
? raw.slice(1, -1) // TOML literal strings do not process escapes.
151+
: raw;
150152
if (!keys.has(match[1]!)) keys.set(match[1]!, value);
151153
}
152154
return keys;
@@ -344,7 +346,13 @@ export function buildGrokManagedBlock(
344346
export function injectGrokConfig(
345347
port: number,
346348
models: GrokInjectModel[],
347-
opts: { grokHome?: string; hostname?: string; excluded?: ReadonlySet<string> } = {},
349+
opts: {
350+
grokHome?: string;
351+
hostname?: string;
352+
excluded?: ReadonlySet<string>;
353+
/** Unfiltered known ids used only to distinguish hidden current models from retired ones. */
354+
catalogModelIds?: ReadonlySet<string>;
355+
} = {},
348356
): GrokInjectResult {
349357
const grokHome = resolveGrokHome(opts.grokHome);
350358
if (!isDirectory(grokHome)) {
@@ -395,9 +403,11 @@ export function injectGrokConfig(
395403
// Adopt our own pre-fence entries (#511) BEFORE reserving user aliases, so the stale
396404
// duplicate is replaced instead of routed around forever. Runs inside the normalized
397405
// window so the user's dominant EOL is still restored below.
398-
// Use the full catalog, not the emitted subset: an explicitly excluded current model must
399-
// still lose its stale unfenced table, or that table would bypass the user's exclusion.
400-
const catalogModelIds = new Set(models.map(model => model.id));
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.
410+
const catalogModelIds = opts.catalogModelIds ?? new Set(models.map(model => model.id));
401411
const orphans = findOpencodexOrphans(originalContent, originalRegion)
402412
.filter(orphan => orphan.modelId !== undefined && catalogModelIds.has(orphan.modelId));
403413
const content = removeOrphanTables(originalContent, orphans);
@@ -481,29 +491,52 @@ export function stripGrokConfig(opts: { grokHome?: string } = {}): GrokInjectRes
481491
const rawContent = readFileSync(configPath, "utf8");
482492
const eol = dominantEol(rawContent);
483493
const content = applyEol(rawContent, "\n");
484-
const region = findManagedRegion(content);
485-
if (!region) {
486-
return { ok: true, changed: false, message: "No opencodex managed block found in Grok config." };
494+
const originalRegion = findManagedRegion(content);
495+
if (originalRegion?.orphaned) return orphanedMarkerResult("cleanup");
496+
497+
// Remove the fence against its ORIGINAL offsets first. A pre-fence orphan's span is clamped
498+
// at the fence start and can include the separator newline injection added. Sweeping that
499+
// orphan first and then applying this separator undo would remove one additional USER newline.
500+
let stripped: string;
501+
let orphanCount = 0;
502+
if (originalRegion) {
503+
let removalEnd = originalRegion.end;
504+
if (content.startsWith("\n", removalEnd)) removalEnd += 1;
505+
let prefix = content.slice(0, originalRegion.start);
506+
const restOfFile = content.slice(removalEnd);
507+
// Undo the single separator newline injection added. Two cases, mirroring inject:
508+
// "X\n" -> "X\n" + "\n" + block => prefix ends "\n\n", drop one.
509+
// "X" -> "X" + "\n" + block => prefix ends "\n" at EOF, drop it.
510+
// A block the user has appended content after is left alone: we never shrink their bytes.
511+
if (prefix.endsWith("\n\n")) prefix = prefix.slice(0, -1);
512+
else if (restOfFile.length === 0 && prefix.endsWith("\n")) prefix = prefix.slice(0, -1);
513+
// Keep the old fence boundary while sweeping. Concatenating first would let the last
514+
// 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);
517+
orphanCount = prefixOrphans.length + tailOrphans.length;
518+
stripped = removeOrphanTables(prefix, prefixOrphans)
519+
+ removeOrphanTables(restOfFile, tailOrphans);
520+
} else {
521+
// Retired or otherwise non-emitted OpenCodex tables may intentionally remain outside the
522+
// fence while the integration is enabled. Teardown owns those strictly identified tables
523+
// even after Grok has re-serialized the file and dropped our marker comments.
524+
const orphans = findOpencodexOrphans(content, null);
525+
if (orphans.length === 0) {
526+
return { ok: true, changed: false, message: "No opencodex managed block found in Grok config." };
527+
}
528+
orphanCount = orphans.length;
529+
stripped = removeOrphanTables(content, orphans);
487530
}
488-
if (region.orphaned) return orphanedMarkerResult("cleanup");
489-
490-
let removalEnd = region.end;
491-
if (content.startsWith("\n", removalEnd)) removalEnd += 1;
492-
let prefix = content.slice(0, region.start);
493-
const restOfFile = content.slice(removalEnd);
494-
// Undo the single separator newline injection added. Two cases, mirroring inject:
495-
// "X\n" -> "X\n" + "\n" + block => prefix ends "\n\n", drop one.
496-
// "X" -> "X" + "\n" + block => prefix ends "\n" at EOF, drop it.
497-
// A block the user has appended content after is left alone: we never shrink their bytes.
498-
if (prefix.endsWith("\n\n")) prefix = prefix.slice(0, -1);
499-
else if (restOfFile.length === 0 && prefix.endsWith("\n")) prefix = prefix.slice(0, -1);
500-
const stripped = prefix + restOfFile;
531+
if (orphanCount > 0) copyBackupOnce(configPath, join(grokHome, "config.toml.bak-opencodex"));
501532
atomicWriteFile(configPath, applyEol(stripped, eol));
502533

503534
return {
504535
ok: true,
505536
changed: true,
506-
message: "Removed the opencodex managed block from Grok config.",
537+
message: originalRegion
538+
? "Removed the opencodex managed block from Grok config."
539+
: "Removed stale opencodex-managed model entries from Grok config.",
507540
};
508541
} catch (error) {
509542
return errorResult("strip", error);

‎src/grok/sync.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
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, type CatalogModel } from "../codex/catalog";
9+
import { visibleNativeSlugs, filterCatalogVisibleModels, nativeContextLimits, nativeOpenAiContextWindow, nativeOpenAiSlugs, type CatalogModel } from "../codex/catalog";
1010
import type { OcxConfig } from "../types";
1111
import { injectGrokConfig, type GrokInjectModel, type GrokInjectResult } from "./inject";
1212

@@ -33,8 +33,14 @@ export async function syncGrokConfig(
3333
deps: GrokSyncDeps = { fetchAllModels: defaultFetchAllModels, injectGrokConfig },
3434
): Promise<GrokInjectResult> {
3535
let models: GrokInjectModel[];
36+
let catalogModelIds: Set<string>;
3637
try {
37-
const routed = filterCatalogVisibleModels(await deps.fetchAllModels(config), config);
38+
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+
]);
3844
models = [
3945
// Native slugs carry their context window too. Without it Grok falls back to its own
4046
// default (200k) and understates models like gpt-5.6-sol, which is 372k. This is the same
@@ -62,5 +68,6 @@ export async function syncGrokConfig(
6268
...(opts.hostname !== undefined ? { hostname: opts.hostname } : {}),
6369
...(opts.grokHome !== undefined ? { grokHome: opts.grokHome } : {}),
6470
excluded: new Set(config.grokExcludedModels ?? []),
71+
catalogModelIds,
6572
});
6673
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020
import { loadConfig, saveConfigPreservingClaudeCode } from "../../config";
2121
import { readRuntimePort } from "../../config/process-state";
22-
import { desktopVisibleNativeSlugs, filterCatalogVisibleModels, nativeContextLimits, nativeOpenAiContextWindow, visibleNativeSlugs } from "../../codex/catalog";
22+
import { desktopVisibleNativeSlugs, filterCatalogVisibleModels, nativeContextLimits, nativeOpenAiContextWindow, nativeOpenAiSlugs, visibleNativeSlugs } 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";
@@ -503,8 +503,14 @@ async function handleGrokToggle(ctx: ManagementContext): Promise<Response> {
503503
*/
504504
const fetchModels = deps.fetchAllModels ?? defaultFetchAllModels;
505505
let models: GrokInjectModel[];
506+
let catalogModelIds: Set<string>;
506507
try {
507-
const routed = filterCatalogVisibleModels(await fetchModels(config), config);
508+
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+
]);
508514
models = [
509515
// Native slugs carry their context window: without it Grok falls back
510516
// to its own 200k default and understates a 372k model.
@@ -535,6 +541,9 @@ async function handleGrokToggle(ctx: ManagementContext): Promise<Response> {
535541
// writer allocates aliases over everything, so a model's alias never
536542
// depends on its neighbours' switches.
537543
excluded: new Set(config.grokExcludedModels ?? []),
544+
// Visibility filters decide what to emit, not whether an owned pre-fence table is still
545+
// current. Otherwise a hidden model is mistaken for retired state and survives outside.
546+
catalogModelIds,
538547
});
539548

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

‎tests/grok-orphan-adoption.test.ts‎

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { afterEach, beforeEach, describe, expect, test } from "bun:test";
22
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
33
import { tmpdir } from "node:os";
44
import { join } from "node:path";
5-
import { injectGrokConfig } from "../src/grok/inject";
5+
import { injectGrokConfig, stripGrokConfig } from "../src/grok/inject";
66

77
/**
88
* #511 — Grok Build reported 200k for every model.
@@ -180,12 +180,118 @@ describe("Grok orphan adoption (#511)", () => {
180180
"",
181181
].join("\n"));
182182

183-
injectGrokConfig(10100, MODELS, { grokHome });
183+
const result = injectGrokConfig(10100, MODELS, { grokHome });
184+
expect(result).toMatchObject({ ok: true, changed: true });
184185
const content = readFileSync(configPath, "utf8");
185186
expect(content).toContain("[model.ocx-unknown]");
186187
expect(content).toContain('api_key = "opencodex-loopback"');
187188
});
188189

190+
test("removes a hidden current orphan but preserves a genuinely retired one", () => {
191+
writeFileSync(configPath, [
192+
"[model.ocx-hidden]",
193+
'model = "hidden/model"',
194+
'base_url = "http://127.0.0.1:10100/v1"',
195+
'api_key = "opencodex-loopback"',
196+
"",
197+
"[model.ocx-retired]",
198+
'model = "retired/model"',
199+
'base_url = "http://127.0.0.1:10100/v1"',
200+
'api_key = "opencodex-loopback"',
201+
"",
202+
].join("\n"));
203+
204+
const result = injectGrokConfig(10100, MODELS, {
205+
grokHome,
206+
catalogModelIds: new Set(["gpt-5.6-sol", "hidden/model"]),
207+
});
208+
expect(result).toMatchObject({ ok: true, changed: true });
209+
const content = readFileSync(configPath, "utf8");
210+
expect(content).not.toContain("[model.ocx-hidden]");
211+
expect(content).not.toContain('model = "hidden/model"');
212+
expect(content).toContain("[model.ocx-retired]");
213+
expect(content).toContain('model = "retired/model"');
214+
});
215+
216+
test("adopts a current orphan whose TOML model id uses literal quotes", () => {
217+
writeOrphanedConfig();
218+
writeFileSync(
219+
configPath,
220+
readFileSync(configPath, "utf8").replace('model = "gpt-5.6-sol"', "model = 'gpt-5.6-sol'"),
221+
);
222+
223+
const result = injectGrokConfig(10100, MODELS, { grokHome });
224+
expect(result).toMatchObject({ ok: true, changed: true });
225+
const content = readFileSync(configPath, "utf8");
226+
expect(modelTables(content)).toEqual(["ocx-gpt-5-6-sol"]);
227+
expect(content).not.toContain("model = 'gpt-5.6-sol'");
228+
expect(content).toContain('model = "gpt-5.6-sol"');
229+
});
230+
231+
test("teardown removes a preserved retired orphan and restores user bytes exactly", () => {
232+
for (const eol of ["\n", "\r\n"]) {
233+
const userPrefix = [`theme = "${eol === "\n" ? "lf" : "crlf"}"`, "", ""].join(eol);
234+
const orphan = [
235+
"[model.ocx-retired]",
236+
'model = "retired/model"',
237+
'base_url = "http://127.0.0.1:10100/v1"',
238+
'api_key = "opencodex-loopback"',
239+
"",
240+
].join(eol);
241+
writeFileSync(configPath, userPrefix + orphan);
242+
expect(injectGrokConfig(10100, MODELS, { grokHome }))
243+
.toMatchObject({ ok: true, changed: true });
244+
expect(readFileSync(configPath, "utf8")).toContain("[model.ocx-retired]");
245+
246+
const stripped = stripGrokConfig({ grokHome });
247+
expect(stripped).toMatchObject({ ok: true, changed: true });
248+
expect(readFileSync(configPath, "utf8")).toBe(userPrefix);
249+
}
250+
});
251+
252+
test("teardown preserves a comment-only tail beyond the fence byte-for-byte", () => {
253+
for (const eol of ["\n", "\r\n"]) {
254+
const userPrefix = [`theme = "${eol === "\n" ? "lf" : "crlf"}"`, "", ""].join(eol);
255+
const orphan = [
256+
"[model.ocx-retired]",
257+
'model = "retired/model"',
258+
'base_url = "http://127.0.0.1:10100/v1"',
259+
'api_key = "opencodex-loopback"',
260+
"",
261+
].join(eol);
262+
const tail = ["# keep this post-fence note", "bare_user_key = true", ""].join(eol);
263+
writeFileSync(configPath, userPrefix + orphan);
264+
expect(injectGrokConfig(10100, MODELS, { grokHome }))
265+
.toMatchObject({ ok: true, changed: true });
266+
writeFileSync(configPath, readFileSync(configPath, "utf8") + tail);
267+
268+
expect(stripGrokConfig({ grokHome })).toMatchObject({ ok: true, changed: true });
269+
expect(readFileSync(configPath, "utf8")).toBe(userPrefix + tail);
270+
}
271+
});
272+
273+
test("markerless teardown removes only ownership-proven orphan tables", () => {
274+
const owned = [
275+
"[model.ocx-retired]",
276+
'model = "retired/model"',
277+
'base_url = "http://127.0.0.1:10100/v1"',
278+
'api_key = "opencodex-loopback"',
279+
"",
280+
].join("\n");
281+
const userOwned = [
282+
"[model.user-owned]",
283+
'model = "user/model"',
284+
'base_url = "http://127.0.0.1:10100/v1"',
285+
'api_key = "not-ours"',
286+
"",
287+
].join("\n");
288+
writeFileSync(configPath, owned + userOwned);
289+
290+
const stripped = stripGrokConfig({ grokHome });
291+
expect(stripped).toMatchObject({ ok: true, changed: true });
292+
expect(readFileSync(configPath, "utf8")).toBe(userOwned);
293+
});
294+
189295
test("still removes a catalog orphan when that model is excluded", () => {
190296
writeOrphanedConfig();
191297

‎tests/grok-sync.test.ts‎

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { afterEach, describe, expect, test } from "bun:test";
2-
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
2+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
33
import { tmpdir } from "node:os";
44
import { join } from "node:path";
5-
import { injectGrokConfig } from "../src/grok/inject";
5+
import { injectGrokConfig, type GrokInjectModel } from "../src/grok/inject";
66
import { syncGrokConfig } from "../src/grok/sync";
7-
import { nativeOpenAiContextWindow, visibleNativeSlugs } from "../src/codex/catalog";
7+
import { nativeOpenAiContextWindow, nativeOpenAiSlugs, visibleNativeSlugs } from "../src/codex/catalog";
88
import type { CatalogModel } from "../src/codex/catalog";
99
import {
1010
resetCodexModelEntitlementCacheForTests,
@@ -46,6 +46,62 @@ describe("syncGrokConfig", () => {
4646
}
4747
});
4848

49+
test("classifies provider-hidden models from the unfiltered catalog without emitting them", async () => {
50+
const { root, grokHome } = tempGrokHome();
51+
try {
52+
const config = {
53+
...baseConfig,
54+
providers: { stub: { selectedModels: ["visible"] } },
55+
} as unknown as OcxConfig;
56+
const catalog = [
57+
{ id: "visible", provider: "stub" } as CatalogModel,
58+
{ id: "hidden", provider: "stub" } as CatalogModel,
59+
];
60+
writeFileSync(join(grokHome, "config.toml"), [
61+
"[model.ocx-stub-hidden]",
62+
'model = "stub/hidden"',
63+
'base_url = "http://127.0.0.1:10100/v1"',
64+
'api_key = "opencodex-loopback"',
65+
"",
66+
].join("\n"));
67+
68+
const result = await syncGrokConfig(10190, config, { grokHome }, {
69+
fetchAllModels: async () => catalog,
70+
injectGrokConfig,
71+
});
72+
expect(result).toMatchObject({ ok: true, changed: true });
73+
const content = readFileSync(join(grokHome, "config.toml"), "utf8");
74+
expect(content).toContain('model = "stub/visible"');
75+
expect(content).not.toContain("[model.ocx-stub-hidden]");
76+
expect(content).not.toContain('model = "stub/hidden"');
77+
} finally {
78+
rmSync(root, { recursive: true, force: true });
79+
}
80+
});
81+
82+
test("keeps disabled native ids in the orphan-classification catalog", async () => {
83+
const hiddenNative = nativeOpenAiSlugs()[0]!;
84+
let emitted: GrokInjectModel[] | undefined;
85+
let catalogModelIds: ReadonlySet<string> | undefined;
86+
const result = await syncGrokConfig(
87+
10190,
88+
{ ...baseConfig, disabledModels: [hiddenNative] } as OcxConfig,
89+
{},
90+
{
91+
fetchAllModels: async () => [],
92+
injectGrokConfig: ((port: number, models: GrokInjectModel[], opts: Parameters<typeof injectGrokConfig>[2]) => {
93+
void port;
94+
emitted = models;
95+
catalogModelIds = opts.catalogModelIds;
96+
return { ok: true, changed: false, message: "captured" };
97+
}) as typeof injectGrokConfig,
98+
},
99+
);
100+
expect(result.ok).toBe(true);
101+
expect(emitted?.some(model => model.id === hiddenNative)).toBe(false);
102+
expect(catalogModelIds?.has(hiddenNative)).toBe(true);
103+
});
104+
49105
// Native slugs used to be injected as a bare { id }, so no `context_window` line was written
50106
// and Grok fell back to its own 200k default — understating gpt-5.6-sol, which is 372k. The
51107
// window comes from the same accessor the dashboard uses, so the two surfaces agree.

0 commit comments

Comments
 (0)