Skip to content

U2: Routing aggressive overhaul#15

Open
jaturapornchai wants to merge 1 commit into
mainfrom
worktree-agent-a476a85f
Open

U2: Routing aggressive overhaul#15
jaturapornchai wants to merge 1 commit into
mainfrom
worktree-agent-a476a85f

Conversation

@jaturapornchai

Copy link
Copy Markdown
Owner

Summary

  • Hedge top-3 cloud candidates (was 2); hedge window widened to estTokens<=20K
  • Retry budget expanded: MAX_RETRIES 10->25; TOTAL_TIMEOUT_MS 30/60/90/120s
  • Per-attempt timeout scaled: 12/20/35/60s (Ollama still 30s)
  • Wire upstreamAgent (undici RetryAgent) as fetch dispatcher for provider calls
  • Circuit breaker recovery window 120s -> 30s for faster bounce-back
  • Context-aware provider filter: for >20K tok requests, require 1.5x headroom with fallback

Test plan

  • rtk npx next build -> Errors: 0 | Warnings: 0
  • Grep verification: MAX_RETRIES=25, upstreamAgent, slice(0,3), CTX-FILTER all present
  • Deploy and observe hedge win rate + p50 latency on large context requests

Note: existing test failures in scanner.test.ts are pre-existing (db mock issue) and unrelated to this change.

Generated with Claude Code

…onn pool, circuit recovery)

- Hedge top-3 cloud candidates (was top-2), expand window to estTokens<=20K
- hedgeRace generalized to N candidates with AbortController per attempt
- MAX_RETRIES 10 -> 25; TOTAL_TIMEOUT_MS scaled up (30/60/90/120s)
- Per-attempt timeout scaled: 12/20/35/60s (was 8/12/20/30)
- Wire upstreamAgent as undici dispatcher on provider fetch
- Circuit breaker recovery window 120s -> 30s
- Context-aware filter: for >20K tok requests, require 1.5x context headroom

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings April 9, 2026 21:45

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts the chat completions routing strategy to be more aggressive and resilient under large-context and flaky-upstream conditions by expanding retry/timeout budgets, widening hedging, and using an undici RetryAgent dispatcher for upstream fetches.

Changes:

  • Increase retry budget (MAX_RETRIES) and total retry timeouts, and scale per-attempt timeouts by request size.
  • Expand hedging from top-2 to top-3 candidates (when eligible) and widen the hedge window to estTokens <= 20k.
  • Add a large-context candidate filter (1.5× headroom) and wire upstreamAgent as the fetch dispatcher; shorten circuit-breaker open window.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 215 to +219
if (successRate < 0.3) {
// Open circuit for 2 minutes
const cbKey = `cb:open:${provider}`;
await redis.set(cbKey, "open", "EX", 120);
console.log(`[CIRCUIT-OPEN] ${provider} — success rate ${(successRate * 100).toFixed(0)}% < 30% → circuit open 2min`);
await redis.set(cbKey, "open", "EX", 30);
console.log(`[CIRCUIT-OPEN] ${provider} — success rate ${(successRate * 100).toFixed(0)}% < 30% → circuit open 30s`);

Copilot AI Apr 9, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circuit breaker open duration was reduced to 30s, but isCircuitOpen() promotes openhalf-open whenever TTL <= 30s. With EX 30, the TTL is always <= 30, so the circuit will be promoted to half-open immediately and effectively never block requests. Adjust the half-open promotion threshold (e.g., last few seconds) or keep the open TTL > 30s so the open state meaningfully gates traffic. Also update the stale comment that still says "Open circuit for 2 minutes".

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants