Repository navigation
Commit 9c57a2b
committed
fix(agent): scope OPENAI_COMPAT_API parsing + add one-shot chat→responses auto-upgrade
Two follow-ups from review on #604.
1. Anthropic startup hardening (Max #1)
`OPENAI_COMPAT_API` was parsed unconditionally, so a stray bad value
in an Anthropic-only env broke startup. Parse it only inside the
`Provider::OpenAi` arm of `Config::from_env`. Anthropic gets a
placeholder `OpenAiApi::ChatCompletions` it never reads. New tests
pin the parser behavior without touching process env.
2. One-shot chat→responses auto-upgrade (Max #2, Tyler "automatic
detection/fallthrough")
When `OPENAI_COMPAT_API=auto` and the provider replies to a Chat
Completions request with a body that explicitly names `/v1/responses`
(or the prose "use the Responses API"), latch a process-wide
sticky-cached upgrade and re-issue the same request on `/v1/responses`.
Subsequent calls skip the chat attempt entirely. Pinned values
(`OPENAI_COMPAT_API=chat`|`responses`) never auto-upgrade.
Signal matcher (`is_responses_required_error`) is intentionally
narrow — only matches the literal path `/v1/responses` or specific
prose phrases, so we don't get fooled by unrelated 4xx bodies.
New `Config.openai_api_auto: bool` records whether the operator
resolved-by-auto vs. pinned, so we know when to enable the upgrade.
`Llm` gains an `AtomicBool` for the sticky upgrade, plus three
small helpers (`effective_openai_api`, `should_try_auto_upgrade`,
`latch_responses_upgrade`) so the dispatch reads straight through.
Logged at WARN once per process: `"openai chat-completions endpoint
reported that this model requires the Responses API; auto-upgrading
subsequent OpenAI calls to /v1/responses for the rest of this
process"`, with the provider error body attached.
Tests:
- 4 new unit tests for `is_responses_required_error` covering the
Databricks GPT-5.5 signal, OpenAI prose phrasing, and explicit
non-matches for `invalid_api_key`, generic `unsupported_parameter`,
and empty body.
- 3 new unit tests for `parse_openai_api` covering unset-defaults-to-auto,
case-insensitive explicit values with whitespace, and rejected garbage.
- New integration test `tests/openai_auto_upgrade.rs` spawns a fake
provider that 400s on `/chat/completions` with the Databricks signal
and 200s on `/responses`. Drives sprout-agent through ACP and asserts
`stopReason=end_turn` plus chat-hit-once / responses-hit-once.
65 tests pass, 0 fail. clippy `-D warnings` clean. cargo fmt clean.
Live smoke against api.openai.com with gpt-5-mini still 3/3 PASS.
Signed-off-by: Tyler Longwell <[email protected]>1 parent 8486536 commit 9c57a2b
3 files changed
Lines changed: 538 additions & 52 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
76 | 83 | | |
77 | 84 | | |
78 | 85 | | |
| |||
82 | 89 | | |
83 | 90 | | |
84 | 91 | | |
85 | | - | |
| 92 | + | |
86 | 93 | | |
87 | 94 | | |
88 | 95 | | |
89 | 96 | | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
90 | 104 | | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
96 | 128 | | |
97 | | - | |
98 | 129 | | |
99 | 130 | | |
100 | 131 | | |
| |||
110 | 141 | | |
111 | 142 | | |
112 | 143 | | |
| 144 | + | |
113 | 145 | | |
114 | 146 | | |
115 | 147 | | |
| |||
203 | 235 | | |
204 | 236 | | |
205 | 237 | | |
206 | | - | |
207 | | - | |
208 | | - | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
209 | 247 | | |
210 | 248 | | |
211 | 249 | | |
| |||
431 | 469 | | |
432 | 470 | | |
433 | 471 | | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
434 | 511 | | |
435 | 512 | | |
436 | 513 | | |
| |||
0 commit comments