@@ -130,6 +130,61 @@ $RefDir = Join-Path $StateDir "refs"
130130$PidFile = Join-Path $StateDir " proxy.pid"
131131$OwnerRef = Join-Path $RefDir " $PID "
132132
133+ # Preserve the user-global model across the session. Claude Code persists an
134+ # in-session /model pick (Enter in the picker) to settings.json as the `model`
135+ # key (v2.1.153+), so picking a proxy-only id would become your global default
136+ # and break a plain `claude` run outside the proxy. We snapshot that key before
137+ # launch and restore it on exit (ref-count-safe). The JSON edit is done via
138+ # Python (not ConvertTo-Json) so the rest of settings.json is left byte-for-key
139+ # intact. Disable with UC_PRESERVE_GLOBAL_MODEL=0.
140+ $CfgDir = if ($env: CLAUDE_CONFIG_DIR ) { $env: CLAUDE_CONFIG_DIR } else { Join-Path $HOME " .claude" }
141+ $GlobalSettings = Join-Path $CfgDir " settings.json"
142+ $SavedModelFile = Join-Path $StateDir " saved_global_model.json"
143+ $PreserveGlobalModel = ($env: UC_PRESERVE_GLOBAL_MODEL -ne " 0" )
144+ $SnapPy = @'
145+ import json,sys
146+ src,dst=sys.argv[1],sys.argv[2]
147+ try: s=json.load(open(src))
148+ except Exception: s={}
149+ json.dump({"had":"model" in s,"model":s.get("model")},open(dst,"w"))
150+ '@
151+ $RestorePy = @'
152+ import json,sys
153+ settings_f,saved_f=sys.argv[1],sys.argv[2]
154+ try: saved=json.load(open(saved_f))
155+ except Exception: saved={}
156+ try: s=json.load(open(settings_f))
157+ except Exception: s=None
158+ if isinstance(s,dict):
159+ if saved.get("had"): s["model"]=saved.get("model")
160+ else: s.pop("model",None)
161+ f=open(settings_f,"w"); json.dump(s,f,indent=2); f.write("\n"); f.close()
162+ '@
163+
164+ function Invoke-UcPy {
165+ param ([string ]$Code , [string []]$Rest )
166+ $a = @ ()
167+ if ($PyCmd.Count -gt 1 ) { $a += $PyCmd [1 .. ($PyCmd.Count - 1 )] }
168+ $a += @ (" -c" , $Code ) + $Rest
169+ & $PyCmd [0 ] @a 2> $null
170+ }
171+
172+ function Save-GlobalModel {
173+ if (-not $PreserveGlobalModel ) { return }
174+ # Clean start: refresh the snapshot. Other session live: keep theirs so we
175+ # never capture a mid-session pick as the "original".
176+ if ((Test-RefsActive ) -and (Test-Path $SavedModelFile )) { return }
177+ try { Invoke-UcPy $SnapPy @ ($GlobalSettings , $SavedModelFile ) } catch {}
178+ }
179+
180+ function Restore-GlobalModel {
181+ if (-not $PreserveGlobalModel ) { return }
182+ if (-not (Test-Path $SavedModelFile )) { return }
183+ if (Test-RefsActive ) { return } # other sessions live; last one out restores
184+ try { Invoke-UcPy $RestorePy @ ($GlobalSettings , $SavedModelFile ) } catch {}
185+ Remove-Item $SavedModelFile - Force - ErrorAction SilentlyContinue
186+ }
187+
133188function Test-ProxyHealthy {
134189 try { return (Invoke-WebRequest - Uri " $BaseUrl /healthz" - UseBasicParsing - TimeoutSec 2 ).StatusCode -eq 200 }
135190 catch { return $false }
@@ -152,6 +207,7 @@ function Test-RefsActive {
152207
153208function Stop-ProxyIfLast {
154209 Remove-Item $OwnerRef - Force - ErrorAction SilentlyContinue
210+ Restore-GlobalModel
155211 if (Test-RefsActive ) { return }
156212 if (Test-Path $PidFile ) {
157213 $stopId = Get-Content $PidFile - ErrorAction SilentlyContinue | Select-Object - First 1
@@ -161,6 +217,7 @@ function Stop-ProxyIfLast {
161217}
162218
163219New-Item - ItemType Directory - Force - Path $RefDir | Out-Null
220+ Save-GlobalModel
164221New-Item - ItemType File - Force - Path $OwnerRef | Out-Null
165222
166223if (Test-ProxyHealthy ) {
@@ -190,7 +247,6 @@ if (Test-ProxyHealthy) {
190247# ----- seed Claude Code's gateway-models cache (first-launch visibility) -----
191248# Seed stock Claude + your configured models so real Claude and your picks all
192249# show on the very first /model open (before Claude Code re-fetches /v1/models).
193- $CfgDir = if ($env: CLAUDE_CONFIG_DIR ) { $env: CLAUDE_CONFIG_DIR } else { Join-Path $HOME " .claude" }
194250$GwCache = Join-Path (Join-Path $CfgDir " cache" ) " gateway-models.json"
195251try {
196252 New-Item - ItemType Directory - Force - Path (Split-Path $GwCache ) | Out-Null
@@ -217,6 +273,7 @@ try {
217273
218274if ($ProxyOnly ) {
219275 Remove-Item $OwnerRef - Force - ErrorAction SilentlyContinue
276+ Restore-GlobalModel
220277 $shownPid = if (Test-Path $PidFile ) { Get-Content $PidFile - ErrorAction SilentlyContinue | Select-Object - First 1 } else { " <pid>" }
221278 Write-Host " "
222279 Write-Host " Proxy running. Connect Claude Code with:"
0 commit comments