refactor(api-proxy): decompose handleUpstreamResponse into focused helpers#5421
Conversation
…d setupTokenTracking Extract two focused helpers from handleUpstreamResponse in upstream-response.js: - handle400WithRetry: handles all three 400 retry/diagnostic branches (a) deprecated beta-header retry for anthropic/copilot (b) transient model-not-supported catalogue retry (copilot, up to MAX_MODEL_NOT_SUPPORTED_RETRIES) (c) model_unavailable diagnostic when retries are exhausted - setupTokenTracking: parses requestModel from the request body and wires trackTokenUsage with OTEL span callbacks (onUsage, onSpanEnd) handleUpstreamResponse is reduced from 138 lines to ~50 lines. No behaviour changes; all 1277 existing tests continue to pass. Closes #5401
There was a problem hiding this comment.
Pull request overview
This PR refactors the api-proxy upstream response handling to make handleUpstreamResponse a smaller coordinator by extracting the 400-response retry/forwarding logic and the token-tracking wiring into focused helper functions within createUpstreamResponseHandlers.
Changes:
- Extracts buffered-400 handling (deprecated-header retry, transient model-not-supported retry, and model_unavailable diagnostic + forwarding) into
handle400WithRetry(...). - Extracts request-body model parsing and
trackTokenUsage+ OTEL callbacks intosetupTokenTracking(...). - Simplifies
handleUpstreamResponse(...)to orchestrate buffering/streaming flow and delegate to the helpers.
Show a summary per file
| File | Description |
|---|---|
| containers/api-proxy/upstream-response.js | Decomposes handleUpstreamResponse into two inner helper functions for buffered-400 retry/forwarding and token-usage tracking wiring. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 1/1 changed files
- Comments generated: 3
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
✅ Copilot review passed with no inline comments. @copilot Add the |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
Chroot tests passed! Smoke Chroot - All security and functionality tests succeeded. |
|
✅ Smoke Copilot BYOK completed. Copilot BYOK mode operational. 🔓 |
|
📡 Smoke OTel Tracing completed. All tracing scenarios validated. ✅ |
|
🔌 Smoke Services — All services reachable! ✅ |
|
✅ Smoke Claude passed |
|
✅ Build Test Suite completed successfully! |
|
✨ The prophecy is fulfilled... Smoke Codex has completed its mystical journey. The stars align. 🌟 |
|
✅ Smoke Gemini completed. All facets verified. 💎 Gemini Smoke Test started |
|
🔑 Smoke Copilot PAT PAT auth validated. All systems operational. ✅ |
|
✅ Smoke Copilot BYOK AOAI (api-key) completed. Copilot AOAI BYOK (api-key) mode operational. 🔓 |
|
❌ Smoke Copilot BYOK AOAI (Entra) reports failed. AOAI BYOK (Entra) mode investigation needed... |
✅ Coverage Check PassedOverall Coverage
📁 Per-file Coverage Changes (1 files)
Coverage comparison generated by |
|
📰 VERDICT: Smoke Copilot has concluded. All systems operational. This is a developing story. 🎤 |
🔐 Smoke Test: Copilot BYOK (Direct) ModeStatus: PASS ✅
Inference requests routed through api-proxy sidecar with real credentials held server-side; agent sees placeholder only. cc:
|
Smoke Test: Claude Engine Validation
Overall result: PASS
|
Smoke Test Results — Auth mode: PAT (COPILOT_GITHUB_TOKEN)
Overall: FAIL cc
|
|
Reviewed: Warning Firewall blocked 1 domainThe following domain was blocked by the firewall during workflow execution:
network:
allowed:
- defaults
- "registry.npmjs.org"See Network Configuration for more information.
|
Smoke Test: API Proxy OpenTelemetry Tracing
All 5 scenarios passed. OTEL tracing integration is fully functional.
|
🔍 Chroot Smoke Test Results
Result: ❌ Tests did not pass — Python and Node.js versions differ between host and chroot environments.
|
Smoke Test ResultsPR: refactor(api-proxy): decompose handleUpstreamResponse into focused helpers
Overall: FAIL — Pre-step output variables were not substituted; HTTP and File test results could not be verified.
|
🏗️ Build Test Suite Results
Overall: 8/8 ecosystems passed — ✅ PASS
|
|
Smoke test result: FAIL Warning Firewall blocked 1 domainThe following domain was blocked by the firewall during workflow execution:
network:
allowed:
- defaults
- "localhost"See Network Configuration for more information.
|
Smoke Test: GitHub Actions Services Connectivity
Overall: FAIL —
|
|
PASS: Running in direct BYOK mode (COPILOT_PROVIDER_API_KEY + COPILOT_PROVIDER_BASE_URL) via api-proxy → Azure OpenAI (Foundry, o4-mini-aw) #aw_smoke
|
handleUpstreamResponseinupstream-response.jsmixed three unrelated concerns in 138 lines, making the control flow hard to follow and the retry logic untestable in isolation.Changes
handle400WithRetry(proxyRes, requestHeaders, responseBody, opts)— extracts the entireshouldBuffer400branch: deprecated-header retry (a), transient model-not-supported catalogue retry (b), and model_unavailable diagnostic (c). Returnstruewhen a retry was triggered.setupTokenTracking(proxyRes, body, opts)— extracts request-body model parsing andtrackTokenUsagewiring with OTEL span callbacks (onUsage,onSpanEnd).handleUpstreamResponsereduced from 138 → ~50 lines; now just orchestrates the error handler,shouldBuffer400decision, and delegates to the two helpers.Both helpers are inner functions of
createUpstreamResponseHandlers, so they share the factory closure without any new public API surface.