Repository navigation
Description
Symptom
Grok Build shows a 200k context window for models that are much larger (e.g. gpt-5.6-sol is 372k, xai/grok-4.5 is 500k). 200k is Grok's own fallback default, which it uses whenever a model entry carries no context_window key.
Root cause
~/.grok/config.toml ends up with two entries for the same model: an orphaned one above the opencodex managed block, and the current one inside it.
default = "ocx-gpt-5-6-sol" # <- points at the ORPHAN
[model.ocx-gpt-5-6-sol] # line 23, OUTSIDE the managed block
model = "gpt-5.6-sol"
name = "OCX gpt-5.6-sol"
# no context_window -> Grok falls back to 200k
# >>> opencodex managed block — do not edit >>> (line 196)
[model.ocx-gpt-5-6-sol-2] # line 197, INSIDE the managed block
model = "gpt-5.6-sol"
context_window = 372000 # correct value
# <<< opencodex managed block <<<The orphans come from an opencodex version that wrote model entries before the managed-block markers existed (or before context_window was emitted). Two behaviours then keep them alive forever:
stripGrokConfig/ the injector only rewrite the region between the markers, so anything above line 196 is never touched.userModelAliases(src/grok/inject.ts:93-103) scans everything outside the managed region and treats those aliases as user-owned. The writer then avoids colliding with them and allocates-2suffixed aliases instead — so every re-sync writes a correct duplicate next to a stale original rather than replacing it.
Net effect: ocx sync looks successful, the managed block is perfectly correct, and the user still sees 200k because Grok's default (and the model picker) resolve to the alias without context_window.
On this machine 46 [model.*] entries exist, roughly half of them stale duplicates.
Why it is not a user config problem
The orphan entries were written by opencodex itself, carry the ocx- alias prefix and the OCX display-name prefix, and point at the opencodex loopback base URL with api_key = "opencodex-loopback". They are unambiguously opencodex-owned, but the current code has no way to recognise that: alias ownership is decided purely by position relative to the markers.
Expected
An opencodex-owned entry outside the managed block should be adopted (migrated into the block) rather than treated as a foreign user model to route around. After a sync there should be exactly one entry per model, carrying the authoritative context_window, and default should keep resolving to a live alias.
Notes / risks for the fix
- Ownership must be detected by a signal that cannot false-positive on a genuine hand-written entry. The
x-opencodex-grokextra header and theopencodex-loopbackapi_key are candidates; theocx-alias prefix alone is weaker. - Removing an orphan that
[models] default(orfork_secondary_model) points at must rewrite that reference to the surviving alias, otherwise the user's Grok loses its default model. - A hand-written user model must still be preserved untouched — that is the whole reason
userModelAliasesexists.
Environment
- opencodex 2.7.41
- macOS, Grok Build with
~/.grok/config.toml
Activity
github-actions commentedon Jul 26, 2026
fix(grok): sweep an orphan's sub-tables too, not just its parent
lidge-jun commentedon Jul 26, 2026
Fixed on
dev/preview/main(7ba0fec3)Injection now adopts opencodex-owned entries that sit outside the managed block, instead of reserving their aliases as user-owned and allocating a suffixed duplicate beside them.
Ownership is conjunctive, because a false positive would delete a hand-written model:
[model.<alias>]header (never[[model.x]]or[model.x.sub]— those spellings mark human authorship and stay reserved);api_key = "opencodex-loopback", a literal we mint;base_url.A loopback
base_urlalone is deliberately not sufficient — pointing your own model at the local proxy is a legitimate, documented thing to do. Removing an adopted entry also repoints[models] defaultand[ui] fork_secondary_modelat the surviving alias; if a removed model has no replacement in the catalog, the entry is left in place rather than leaving a dangling reference.Two things the synthetic fixture missed
The first implementation passed 10/10 unit tests while the real file was still broken. Inspecting the live config before verifying turned up field state the fixture never modelled — both caused by Grok Build re-serializing the file into its own format:
1. The marker comments are gone.
rg '>>> opencodex managed block' ~/.grok/config.tomlreturns nothing on the affected machine.findManagedRegionthen returnsnull, so the entire file is in scope and the ownership predicate is the only thing protecting user entries. That is survivable precisely because the predicate is conjunctive; a position-based rule would have had nothing left to stand on.2. Sub-tables kept the alias reserved. Grok 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 the model-header scan — souserModelAliaseskept reserving the alias and the writer allocated a suffixed duplicate anyway. The sweep reported success and reproduced this very bug one level down. Each orphan's span now swallows its ownmodel.<alias>.*children.Verification
Against a copy of the reporting machine's real
~/.grok/config.toml:[cli],[ui](including a genuine userfork_secondary_model) and[marketplace]survive untouched.Regression coverage:
tests/grok-orphan-adoption.test.ts, 11 cases mapped 1:1 onto the failure-mode inventory — user-model preservation (loopback-only and remote-api_key variants),defaultrewriting, array-of-table reservation, neighbouring-table integrity, orphan-without-replacement, idempotence, CRLF, orphaned-marker refusal, and the marker-less + sub-table field shape. Full suite 4829 pass / 0 fail.Note for anyone verifying
rg -c '^\[model\.'over-counts: it also matches[model.<alias>.extra_headers]sub-tables. Use'^\[model\.[^.]+\]$'for real model tables — the wrong pattern makes a correct sweep look broken.Existing configs repair themselves on the next sync; no manual edit is needed.
docs(devlog): retract the #511 closure — the sweep destroys an adjace…
docs(devlog): plan the Grok fence-clamp fix for the #511 follow-up
docs(devlog): fix the clamp plan's child-scan guard before it reintro…
fix(grok): stop the orphan sweep from eating the fence it sits next to
lidge-jun commentedon Jul 27, 2026
Fixed on
dev(8390cdb3, now merged asc05e88fd).The adoption sweep from
5ff20dc0/7ba0fec3worked, but only when another table separated the orphan from the fence. On the layout this issue actually reported — model tables running up to line 195 with the fence at 196 — it made things worse.findOpencodexOrphansended an orphan's span at the next table header, and the managed block opens with a comment. With nothing in between, the span ran past theBEGINmarker and the sweep deleted the fence opener. Reproduced over three consecutive syncs, the two fixtures differing only by whether a[cli]table sits between the orphan and the fence:So the fence broke, the block was re-appended at EOF leaving a stranded
END, anddefaultalternated between the two aliases on every sync — the duplicate-allocation loop this issue is about, one layer down. All 55 tests passed throughout, because no fixture placed an orphan directly above the marker.The fix clamps a pre-fence orphan's span at the fence. The parent qualifier on the child scan matters: without it, an orphan below the fence breaks on its first sub-table (every index is past the fence), stranding the sub-table so
userModelAliaseskeeps the alias reserved and pinsdefaultto the-2form — reintroducing exactly this bug. Six regression cases now cover adjacency, both-sides orphans, the below-fence sub-table, and three-run convergence.Backups also widened: the sweep deletes a table out of your file, so
config.toml.bak-opencodexis now written for that path too, not only on a first-time fence write.Please re-run
ocx syncand confirm the duplicate-2aliases are gone and the context window reads 372k / 500k instead of Grok's 200k fallback. Reopen if anything persists.Three related defects in the same sweep are documented but not yet fixed, the sharpest being that
docs-site/.../grok-build.mdtells you to hand-writeapi_key = "opencodex-loopback"outside the fence — which is exactly the ownership predicate, so the sweep deletes it. Tracking those separately.fix(grok): sweep an orphan's sub-tables too, not just its parent
docs(devlog): retract the lidge-jun#511 closure — the sweep destroys …
docs(devlog): plan the Grok fence-clamp fix for the lidge-jun#511 fol…
docs(devlog): fix the clamp plan's child-scan guard before it reintro…
fix(grok): stop the orphan sweep from eating the fence it sits next to