@@ -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. */
128125const 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. */
131129interface 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 - o p e n c o d e x - g r o k [ " ' ] [ \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 - Z a - z 0 - 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 }
0 commit comments