Skip to content

Grok Build shows 200k context: opencodex-owned model entries orphaned outside the managed block are never adopted #511

Closed

Description

@lidge-jun

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:

  1. stripGrokConfig / the injector only rewrite the region between the markers, so anything above line 196 is never touched.
  2. 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 -2 suffixed 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-grok extra header and the opencodex-loopback api_key are candidates; the ocx- alias prefix alone is weaker.
  • Removing an orphan that [models] default (or fork_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 userModelAliases exists.

Environment

  • opencodex 2.7.41
  • macOS, Grok Build with ~/.grok/config.toml

Activity

  1. added
    bug
    on Jul 26, 2026
  2. github-actions commented on Jul 26, 2026

    @github-actions
    Contributor
  3. lidge-jun commented on Jul 26, 2026

    @lidge-jun
    OwnerAuthor

    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:

    • a plain [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;
    • a loopback base_url.

    A loopback base_url alone 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] default and [ui] fork_secondary_model at 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.toml returns nothing on the affected machine. findManagedRegion then returns null, 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 — so userModelAliases kept 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 own model.<alias>.* children.

    Verification

    Against a copy of the reporting machine's real ~/.grok/config.toml:

    before: 46 model tables, 40 without context_window, default -> an orphan
    after:   3 model tables, all with context_window, default still resolving
             0 model tables above the fence
    2nd run: changed=false, bytes identical (idempotent)
    

    [cli], [ui] (including a genuine user fork_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), default rewriting, 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.

  4. lidge-jun commented on Jul 27, 2026

    @lidge-jun
    OwnerAuthor

    Fixed on dev (8390cdb3, now merged as c05e88fd).

    The adoption sweep from 5ff20dc0/7ba0fec3 worked, 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.

    findOpencodexOrphans ended 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 the BEGIN marker 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:

    ADJACENT   run1: changed=true  BEGIN=1 END=2 tables=2 default=ocx-gpt-5-6-sol-2
               run2: changed=true  BEGIN=1 END=2 tables=2 default=ocx-gpt-5-6-sol
               run3: changed=true  BEGIN=1 END=2 tables=2 default=ocx-gpt-5-6-sol-2
    
    SEPARATED  run1: changed=true  BEGIN=1 END=1 tables=1
               run2: changed=false
    

    So the fence broke, the block was re-appended at EOF leaving a stranded END, and default alternated 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 userModelAliases keeps the alias reserved and pins default to the -2 form — 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-opencodex is now written for that path too, not only on a first-time fence write.

    Please re-run ocx sync and confirm the duplicate -2 aliases 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.md tells you to hand-write api_key = "opencodex-loopback" outside the fence — which is exactly the ownership predicate, so the sweep deletes it. Tracking those separately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions