param( [switch]$Yes ) $ErrorActionPreference = 'Stop' $codexDir = if ($env:CODEX_HOME) { $env:CODEX_HOME } else { Join-Path $HOME '.codex' } $configFile = Join-Path $codexDir 'config.toml' $model = if ($env:CODEX_MODEL) { $env:CODEX_MODEL } else { 'gpt-5.6-sol' } if ($model -notmatch '^[A-Za-z0-9._-]+$') { throw 'CODEX_MODEL contains unsupported characters.' } Write-Host 'Codex GPT-5.6 200K auto-compaction setup' Write-Host '' Write-Host "Config file: $configFile" Write-Host " model = `"$model`"" Write-Host ' model_context_window = 272000' Write-Host ' model_auto_compact_token_limit = 200000' Write-Host ' model_auto_compact_token_limit_scope = "total"' Write-Host '' Write-Host 'Existing providers, API key references, and custom settings will be preserved.' Write-Host 'Do not continue if your job intentionally needs more than 272K active context tokens.' if (-not $Yes -and $env:CODEX_GPT56_200K_YES -ne '1') { $answer = Read-Host 'Apply these settings? [Y/n]' if ($answer -and $answer -notmatch '^(y|yes)$') { Write-Host 'Cancelled.' exit 0 } } New-Item -ItemType Directory -Force -Path $codexDir | Out-Null if (-not (Test-Path $configFile)) { New-Item -ItemType File -Path $configFile | Out-Null } $timestamp = Get-Date -Format 'yyyyMMddHHmmss' $backupBase = "$configFile.bak.$timestamp" $backupFile = $backupBase $backupIndex = 1 while (Test-Path $backupFile) { $backupFile = "$backupBase.$backupIndex" $backupIndex++ } Copy-Item -Force $configFile "$configFile.bak" Copy-Item -Force $configFile $backupFile $settings = [ordered]@{ model = "`"$model`"" model_context_window = '272000' model_auto_compact_token_limit = '200000' model_auto_compact_token_limit_scope = '"total"' } $lines = [System.IO.File]::ReadAllLines($configFile) $output = [System.Collections.Generic.List[string]]::new() $seen = @{} $inRoot = $true function Add-MissingSettings { $added = $false foreach ($key in $settings.Keys) { if (-not $seen.ContainsKey($key)) { $output.Add("$key = $($settings[$key])") $seen[$key] = $true $added = $true } } if ($added) { $output.Add('') } } foreach ($line in $lines) { if ($inRoot -and $line -match '^\s*\[') { Add-MissingSettings $inRoot = $false } $matched = $false if ($inRoot) { foreach ($key in $settings.Keys) { if ($line -match ('^\s*' + [regex]::Escape($key) + '\s*=')) { if (-not $seen.ContainsKey($key)) { $output.Add("$key = $($settings[$key])") } $seen[$key] = $true $matched = $true break } } } if (-not $matched) { $output.Add($line) } } if ($inRoot) { Add-MissingSettings } $tempFile = Join-Path $codexDir ".config.toml.gpt56-200k.$PID" $utf8NoBom = [System.Text.UTF8Encoding]::new($false) try { [System.IO.File]::WriteAllLines($tempFile, $output, $utf8NoBom) Move-Item -Force $tempFile $configFile } catch { if (Test-Path $tempFile) { Remove-Item -Force $tempFile } Copy-Item -Force $backupFile $configFile throw } Write-Host '' Write-Host "Updated: $configFile" Write-Host "Backup: $backupFile" Write-Host 'Fully quit and restart Codex to load the new settings.'