Skip to content

Commit 7ba0fec

Browse files
committed
fix(grok): sweep an orphan's sub-tables too, not just its parent
Field state from the reporting machine broke the first fix. Grok Build re-serializes the config into its own format: it drops our marker comments entirely, and it promotes inline extra_headers into a separate [model.<alias>.extra_headers] table. Removing only the parent left that child behind, and a leftover child still matches MODEL_TABLE_HEADER — so userModelAliases kept the alias reserved and the writer allocated a suffixed duplicate anyway. The sweep ran, reported success, and reproduced #511 one level down. Each orphan's span now swallows its own model.<alias>.* children. Verified against a copy of the live file: 46 model tables collapse to 3, all carrying context_window, with default still resolving and the user's [ui] settings intact.
1 parent b109fcf commit 7ba0fec

3 files changed

Lines changed: 94 additions & 2 deletions

File tree

‎devlog/_plan/260727_grok_orphan_adoption/031_sweep_receipt.md‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,34 @@ and `[marketplace]` with its sources are all intact after the sweep.
4646

4747
That Grok's TUI reads the corrected file. That requires the user to restart via
4848
`ocx service` and a visual check — `030` step 2, criterion `c-live`.
49+
50+
## Field state the fixture did not model (found at WP3, fixed)
51+
52+
Inspecting the live file before the restart turned up two things the synthetic fixture
53+
never reproduced. Both came from Grok Build re-serializing the config into its own
54+
format:
55+
56+
**1. The marker comments are GONE.** `rg '>>> opencodex managed block' ~/.grok/config.toml`
57+
returns nothing. Grok rewrote the file and dropped our comments. `findManagedRegion`
58+
then returns `null`, so the whole file is in scope and the ownership predicate is the
59+
ONLY thing protecting the user's own entries. That is survivable exactly because the
60+
predicate is conjunctive (`api_key` AND loopback `base_url` AND plain header) — a
61+
position-based rule would have had nothing left to stand on.
62+
63+
**2. Sub-tables kept the alias reserved.** Grok also promotes inline
64+
`extra_headers = { ... }` into a separate `[model.<alias>.extra_headers]` table. The
65+
first implementation removed the parent table only, leaving that child behind — and a
66+
leftover child still matches `MODEL_TABLE_HEADER`, so `userModelAliases` kept reserving
67+
the alias. The sweep removed the parent and STILL allocated a suffixed duplicate: the
68+
exact #511 loop, one level down.
69+
70+
Fixed by extending each orphan's span to swallow its own `model.<alias>.*` children.
71+
Pinned by a test that reproduces the real shape (no markers, parent + `.extra_headers`
72+
child + a hand-written model that must survive).
73+
74+
Re-verified against a copy of the live file: 46 model tables -> 3, all three carrying
75+
`context_window`, `default` still resolving, `[ui] fork_secondary_model` intact.
76+
77+
**Lesson for the next verifier:** the fixture passed 10/10 while the real file was still
78+
broken. Field state beat the synthetic case, which is why `030` insists on the live
79+
check rather than treating green tests as the finish line.

‎src/grok/inject.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,21 @@ function findOpencodexOrphans(content: string, region: ManagedRegion | null): Or
186186
if (header.array || header.segments.length !== 2 || header.segments[0] !== "model") continue;
187187
// Inside the fence the regular splice already owns it.
188188
if (region && header.index >= region.start && header.index < region.end) continue;
189-
const end = headers[position + 1]?.index ?? content.length;
190-
const keys = tableBodyKeys(content.slice(header.index + header.length, end));
189+
const bodyEnd = headers[position + 1]?.index ?? content.length;
190+
const keys = tableBodyKeys(content.slice(header.index + header.length, bodyEnd));
191191
if (keys.get("api_key") !== OPENCODEX_API_KEY) continue;
192192
if (!isLoopbackBaseUrl(keys.get("base_url"))) continue;
193+
// Swallow the entry's OWN sub-tables (`[model.<alias>.extra_headers]`). Grok writes
194+
// them when it re-serializes the file, and leaving one behind keeps the alias
195+
// reserved by `userModelAliases` — so the sweep would remove the parent and STILL
196+
// allocate a suffixed duplicate, which is the exact #511 loop we came to close.
197+
let end = bodyEnd;
198+
for (let next = position + 1; next < headers.length; next += 1) {
199+
const child = headers[next]!;
200+
if (child.segments.length <= 2) break;
201+
if (child.segments[0] !== "model" || child.segments[1] !== header.segments[1]) break;
202+
end = headers[next + 1]?.index ?? content.length;
203+
}
193204
orphans.push({ alias: header.segments[1]!, modelId: keys.get("model"), start: header.index, end });
194205
}
195206
return orphans;

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,56 @@ describe("Grok orphan adoption (#511)", () => {
184184
expect(content.replace(/\r\n/g, "")).not.toContain("\n");
185185
});
186186

187+
188+
// The state a REAL machine reached (#511 field evidence): Grok re-serialized the file
189+
// into its own format and dropped our marker COMMENTS entirely. findManagedRegion then
190+
// returns null, so the whole file is in scope and the ownership predicate is the only
191+
// thing standing between the sweep and the user's own models.
192+
test("still adopts safely when Grok has dropped the markers entirely", () => {
193+
writeFileSync(configPath, [
194+
"[ui]",
195+
'fork_secondary_model = "grok-build"',
196+
"",
197+
"[models]",
198+
'default = "ocx-gpt-5-6-sol"',
199+
"",
200+
"[model.ocx-gpt-5-6-sol]", // stale: no context_window
201+
'model = "gpt-5.6-sol"',
202+
'base_url = "http://127.0.0.1:10100/v1"',
203+
'api_key = "opencodex-loopback"',
204+
"",
205+
"[model.ocx-gpt-5-6-sol-2]", // the correct duplicate, also unfenced now
206+
'model = "gpt-5.6-sol"',
207+
'base_url = "http://127.0.0.1:10100/v1"',
208+
'api_key = "opencodex-loopback"',
209+
"context_window = 372000",
210+
"",
211+
"[model.ocx-gpt-5-6-sol-2.extra_headers]",
212+
'x-opencodex-grok = "1"',
213+
"",
214+
"[model.hand-written]", // must survive
215+
'model = "mine"',
216+
'base_url = "http://127.0.0.1:10100/v1"',
217+
'api_key = "not-ours"',
218+
"",
219+
].join("\n"));
220+
221+
const result = injectGrokConfig(10100, MODELS, { grokHome });
222+
expect(result).toMatchObject({ ok: true, changed: true });
223+
224+
const content = readFileSync(configPath, "utf8");
225+
// Both opencodex duplicates collapse into the single regenerated entry.
226+
expect(modelTables(content).filter(alias => alias.startsWith("ocx-"))).toHaveLength(1);
227+
expect(content).toContain("context_window = 372000");
228+
// The user's model and settings are untouched.
229+
expect(content).toContain("[model.hand-written]");
230+
expect(content).toContain('api_key = "not-ours"');
231+
expect(content).toContain('fork_secondary_model = "grok-build"');
232+
// default still resolves.
233+
const survivor = /^default = "([^"]+)"/m.exec(content)?.[1];
234+
expect(content).toContain(`[model.${survivor}]`);
235+
});
236+
187237
// F8: an ambiguous fence must refuse BEFORE the sweep, or "outside the region" could
188238
// mean the entire file.
189239
test("refuses to sweep when the end marker is missing", () => {

0 commit comments

Comments
 (0)