@@ -50,6 +50,17 @@ function stripExistingModelProvider(content: string): string {
5050 return out . join ( "\n" ) ;
5151}
5252
53+ function stripRootContextWindowOverrides ( content : string ) : string {
54+ const lines = content . split ( "\n" ) ;
55+ const firstTable = lines . findIndex ( l => / ^ \s * \[ / . test ( l ) ) ;
56+ return lines
57+ . filter ( ( line , i ) => {
58+ const isRoot = firstTable === - 1 || i < firstTable ;
59+ return ! isRoot || ! / ^ \s * m o d e l _ (?: c o n t e x t _ w i n d o w | a u t o _ c o m p a c t _ t o k e n _ l i m i t ) \s * = / . test ( line ) ;
60+ } )
61+ . join ( "\n" ) ;
62+ }
63+
5364/**
5465 * Insert `model_provider = "opencodex"` at the document ROOT — immediately before the first table
5566 * header (TOML root keys must precede all tables). If there are no tables, append it to the root body.
@@ -170,6 +181,7 @@ export async function injectCodexConfig(port: number, config?: OcxConfig): Promi
170181 }
171182 content = removeProfileSection ( content ) ;
172183 content = stripExistingModelProvider ( content ) ;
184+ content = stripRootContextWindowOverrides ( content ) ;
173185 content = normalizeServiceTier ( content ) ;
174186 content = ensureFastModeFeature ( content ) ;
175187
@@ -227,6 +239,7 @@ export function stripOpencodexConfig(content: string): string {
227239 // Regex (not exact-string) removal so compact `model_provider="opencodex"` is stripped too —
228240 // must match the detection regex above, or a detected line could survive un-removed.
229241 out = out . split ( "\n" ) . filter ( l => ! / ^ \s * m o d e l _ p r o v i d e r \s * = \s * " o p e n c o d e x " \s * $ / . test ( l ) ) . join ( "\n" ) ;
242+ out = stripRootContextWindowOverrides ( out ) ;
230243 out = stripDefaultCatalogPath ( out ) ;
231244 return out . replace ( / \n { 3 , } / g, "\n\n" ) . trimEnd ( ) + "\n" ;
232245}
@@ -243,6 +256,8 @@ export function removeCodexConfig(): { success: boolean; message: string } {
243256 const had = hasOpencodexRouting ( content ) ;
244257 if ( had ) {
245258 atomicWriteFile ( CODEX_CONFIG_PATH , stripOpencodexConfig ( content ) ) ;
259+ } else if ( stripRootContextWindowOverrides ( content ) !== content ) {
260+ atomicWriteFile ( CODEX_CONFIG_PATH , stripOpencodexConfig ( content ) ) ;
246261 }
247262 if ( existsSync ( CODEX_PROFILE_PATH ) ) unlinkSync ( CODEX_PROFILE_PATH ) ;
248263 return {
0 commit comments