Commit aa49871
fix(cli): resolve race condition causing provider switch during mode changes (#11205)
When using slash commands with `mode:` frontmatter (e.g., `/cli-release`
with `mode: code`), the CLI would fail with "Could not resolve
authentication method" from the Anthropic SDK, even when using a
non-Anthropic provider like `--provider roo`.
Root cause: In `markWebviewReady()`, the `webviewDidLaunch` message was
sent before `updateSettings`, creating a race condition. The
`webviewDidLaunch` handler's "first-time init" sync would read
`getState()` before CLI-provided settings were applied to the context
proxy. Since `getState()` defaults `apiProvider` to "anthropic" when
unset, this default was saved to the provider profile. When a slash
command triggered `handleModeSwitch()`, it found this corrupted profile
with `apiProvider: "anthropic"` (but no API key) and activated it,
overwriting the CLI's working roo provider configuration.
Fix:
1. Reorder `markWebviewReady()` to send `updateSettings` before
`webviewDidLaunch`, ensuring the context proxy has CLI-provided
values when the initialization handler runs.
2. Guard the first-time init sync with `checkExistKey(apiConfiguration)`
to prevent saving a profile with only the default "anthropic"
fallback and no actual API keys configured.
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>1 parent 6214f4c commit aa49871
File tree
2 files changed
+19
-9
lines changed- apps/cli/src/agent
- src/core/webview
2 files changed
+19
-9
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
428 | 428 | | |
429 | 429 | | |
430 | 430 | | |
431 | | - | |
432 | | - | |
433 | | - | |
434 | | - | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
435 | 435 | | |
436 | 436 | | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
437 | 441 | | |
438 | 442 | | |
439 | 443 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
498 | 498 | | |
499 | 499 | | |
500 | 500 | | |
501 | | - | |
502 | | - | |
503 | | - | |
504 | | - | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
505 | 510 | | |
506 | | - | |
| 511 | + | |
| 512 | + | |
507 | 513 | | |
508 | 514 | | |
509 | 515 | | |
| |||
0 commit comments