package executor import ( "context" "errors" "net" "net/http" "strings" "syscall" "time" "github.com/google/uuid" kiroauth "github.com/router-for-me/CLIProxyAPI/v6/internal/auth/kiro" "github.com/router-for-me/CLIProxyAPI/v6/internal/config" kiroclaude "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/kiro/claude" kiroopenai "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/kiro/openai" "github.com/router-for-me/CLIProxyAPI/v6/internal/util" cliproxyauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth" sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" log "github.com/sirupsen/logrus" ) // retryConfig holds configuration for socket retry logic. // Based on kiro2Api Python implementation patterns. type retryConfig struct { MaxRetries int // Maximum number of retry attempts BaseDelay time.Duration // Base delay between retries (exponential backoff) MaxDelay time.Duration // Maximum delay cap RetryableErrors []string // List of retryable error patterns RetryableStatus map[int]bool // HTTP status codes to retry FirstTokenTmout time.Duration // Timeout for first token in streaming StreamReadTmout time.Duration // Timeout between stream chunks } // defaultRetryConfig returns the default retry configuration for Kiro socket operations. func defaultRetryConfig() retryConfig { return retryConfig{ MaxRetries: kiroSocketMaxRetries, BaseDelay: kiroSocketBaseRetryDelay, MaxDelay: kiroSocketMaxRetryDelay, RetryableStatus: retryableHTTPStatusCodes, RetryableErrors: []string{ "connection reset", "connection refused", "broken pipe", "EOF", "timeout", "temporary failure", "no such host", "network is unreachable", "i/o timeout", }, FirstTokenTmout: kiroFirstTokenTimeout, StreamReadTmout: kiroStreamingReadTimeout, } } // isRetryableError checks if an error is retryable based on error type and message. // Returns true for network timeouts, connection resets, and temporary failures. // Based on kiro2Api's retry logic patterns. func isRetryableError(err error) bool { if err == nil { return false } // Check for context cancellation - not retryable if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { return false } // Check for EventStreamError FIRST - fatal and malformed errors are never retryable // This must come before string pattern matching to avoid false positives var streamErr *EventStreamError if errors.As(err, &streamErr) { return false } // Check for net.Error (timeout, temporary) var netErr net.Error if errors.As(err, &netErr) { if netErr.Timeout() { log.Debugf("kiro: isRetryableError: network timeout detected") return true } // Note: Temporary() is deprecated but still useful for some error types } // Check for specific syscall errors (connection reset, broken pipe, etc.) var syscallErr syscall.Errno if errors.As(err, &syscallErr) { switch syscallErr { case syscall.ECONNRESET: // Connection reset by peer log.Debugf("kiro: isRetryableError: ECONNRESET detected") return true case syscall.ECONNREFUSED: // Connection refused log.Debugf("kiro: isRetryableError: ECONNREFUSED detected") return true case syscall.EPIPE: // Broken pipe log.Debugf("kiro: isRetryableError: EPIPE (broken pipe) detected") return true case syscall.ETIMEDOUT: // Connection timed out log.Debugf("kiro: isRetryableError: ETIMEDOUT detected") return true case syscall.ENETUNREACH: // Network is unreachable log.Debugf("kiro: isRetryableError: ENETUNREACH detected") return true case syscall.EHOSTUNREACH: // No route to host log.Debugf("kiro: isRetryableError: EHOSTUNREACH detected") return true } } // Check for net.OpError wrapping other errors var opErr *net.OpError if errors.As(err, &opErr) { log.Debugf("kiro: isRetryableError: net.OpError detected, op=%s", opErr.Op) // Recursively check the wrapped error if opErr.Err != nil { return isRetryableError(opErr.Err) } return true } // Check error message for retryable patterns errMsg := strings.ToLower(err.Error()) cfg := defaultRetryConfig() for _, pattern := range cfg.RetryableErrors { if strings.Contains(errMsg, pattern) { log.Debugf("kiro: isRetryableError: pattern '%s' matched in error: %s", pattern, errMsg) return true } } return false } // isRetryableHTTPStatus checks if an HTTP status code is retryable. // Based on kiro2Api: 502, 503, 504 are retryable server errors. func isRetryableHTTPStatus(statusCode int) bool { return retryableHTTPStatusCodes[statusCode] } // calculateRetryDelay calculates the delay for the next retry attempt using exponential backoff. // delay = min(baseDelay * 2^attempt, maxDelay) // Adds ±30% jitter to prevent thundering herd. func calculateRetryDelay(attempt int, cfg retryConfig) time.Duration { return kiroauth.ExponentialBackoffWithJitter(attempt, cfg.BaseDelay, cfg.MaxDelay) } // logRetryAttempt logs a retry attempt with relevant context. func logRetryAttempt(attempt, maxRetries int, reason string, delay time.Duration, endpoint string) { log.Warnf("kiro: retry attempt %d/%d for %s, waiting %v before next attempt (endpoint: %s)", attempt+1, maxRetries, reason, delay, endpoint) } // kiroHTTPClientPool provides a shared HTTP client with connection pooling for Kiro API. // This reduces connection overhead and improves performance for concurrent requests. // Based on kiro2Api's connection pooling pattern. // getKiroPooledHTTPClient returns a shared HTTP client with optimized connection pooling. // The client is lazily initialized on first use and reused across requests. // This is especially beneficial for: // - Reducing TCP handshake overhead // - Enabling HTTP/2 multiplexing // - Better handling of keep-alive connections func getKiroPooledHTTPClient() *http.Client { kiroHTTPClientPoolOnce.Do(func() { transport := &http.Transport{ // Connection pool settings MaxIdleConns: 100, // Max idle connections across all hosts MaxIdleConnsPerHost: 20, // Max idle connections per host MaxConnsPerHost: 50, // Max total connections per host IdleConnTimeout: 90 * time.Second, // How long idle connections stay in pool // Timeouts for connection establishment DialContext: (&net.Dialer{ Timeout: 30 * time.Second, // TCP connection timeout KeepAlive: 30 * time.Second, // TCP keep-alive interval }).DialContext, // TLS handshake timeout TLSHandshakeTimeout: 10 * time.Second, // Response header timeout ResponseHeaderTimeout: 30 * time.Second, // Expect 100-continue timeout ExpectContinueTimeout: 1 * time.Second, // Enable HTTP/2 when available ForceAttemptHTTP2: true, } kiroHTTPClientPool = &http.Client{ Transport: transport, // No global timeout - let individual requests set their own timeouts via context } log.Debugf("kiro: initialized pooled HTTP client (MaxIdleConns=%d, MaxIdleConnsPerHost=%d, MaxConnsPerHost=%d)", transport.MaxIdleConns, transport.MaxIdleConnsPerHost, transport.MaxConnsPerHost) }) return kiroHTTPClientPool } // newKiroHTTPClientWithPooling creates an HTTP client that uses connection pooling when appropriate. // It respects proxy configuration from auth or config, falling back to the pooled client. // This provides the best of both worlds: custom proxy support + connection reuse. func newKiroHTTPClientWithPooling(ctx context.Context, cfg *config.Config, auth *cliproxyauth.Auth, timeout time.Duration) *http.Client { // Check if a proxy is configured - if so, we need a custom client var proxyURL string if auth != nil { proxyURL = strings.TrimSpace(auth.ProxyURL) } if proxyURL == "" && cfg != nil { proxyURL = strings.TrimSpace(cfg.ProxyURL) } // If proxy is configured, use the existing proxy-aware client (doesn't pool) if proxyURL != "" { log.Debugf("kiro: using proxy-aware HTTP client (proxy=%s)", proxyURL) return newProxyAwareHTTPClient(ctx, cfg, auth, timeout) } // No proxy - use pooled client for better performance pooledClient := getKiroPooledHTTPClient() // If timeout is specified, we need to wrap the pooled transport with timeout if timeout > 0 { return &http.Client{ Transport: pooledClient.Transport, Timeout: timeout, } } return pooledClient } // applyDynamicFingerprint applies token-specific fingerprint headers to the request // For IDC auth, uses dynamic fingerprint-based User-Agent // For other auth types, uses static Amazon Q CLI style headers func applyDynamicFingerprint(req *http.Request, auth *cliproxyauth.Auth) { if isIDCAuth(auth) { // Get token-specific fingerprint for dynamic UA generation tokenKey := getTokenKey(auth) fp := getGlobalFingerprintManager().GetFingerprint(tokenKey) // Use fingerprint-generated dynamic User-Agent req.Header.Set("User-Agent", fp.BuildUserAgent()) req.Header.Set("X-Amz-User-Agent", fp.BuildAmzUserAgent()) req.Header.Set("x-amzn-kiro-agent-mode", kiroIDEAgentModeSpec) // Safely truncate tokenKey for logging (avoid panic if shorter than 8 chars) tokenKeyPreview := tokenKey if len(tokenKey) > 8 { tokenKeyPreview = tokenKey[:8] + "..." } else if len(tokenKey) > 0 { tokenKeyPreview = tokenKey[:min(len(tokenKey), 4)] + "..." } log.Debugf("kiro: using dynamic fingerprint for token %s (SDK:%s, OS:%s/%s, Kiro:%s)", tokenKeyPreview, fp.SDKVersion, fp.OSType, fp.OSVersion, fp.KiroVersion) } else { // Use static Amazon Q CLI style headers for non-IDC auth req.Header.Set("User-Agent", kiroUserAgent) req.Header.Set("X-Amz-User-Agent", kiroFullUserAgent) } } // PrepareRequest prepares the HTTP request before execution. func (e *KiroExecutor) PrepareRequest(req *http.Request, auth *cliproxyauth.Auth) error { if req == nil { return nil } accessToken, _ := kiroCredentials(auth) if strings.TrimSpace(accessToken) == "" { return statusErr{code: http.StatusUnauthorized, msg: "missing access token"} } // Apply dynamic fingerprint-based headers applyDynamicFingerprint(req, auth) req.Header.Set("Amz-Sdk-Request", "attempt=1; max=3") req.Header.Set("Amz-Sdk-Invocation-Id", uuid.New().String()) req.Header.Set("Authorization", "Bearer "+accessToken) var attrs map[string]string if auth != nil { attrs = auth.Attributes } util.ApplyCustomHeadersFromAttrs(req, attrs) return nil } // HttpRequest injects Kiro credentials into the request and executes it. func (e *KiroExecutor) HttpRequest(ctx context.Context, auth *cliproxyauth.Auth, req *http.Request) (*http.Response, error) { if req == nil { return nil, errors.New("kiro executor: request is nil") } if ctx == nil { ctx = req.Context() } httpReq := req.WithContext(ctx) if errPrepare := e.PrepareRequest(httpReq, auth); errPrepare != nil { return nil, errPrepare } httpClient := newKiroHTTPClientWithPooling(ctx, e.cfg, auth, 0) return httpClient.Do(httpReq) } // buildKiroPayloadForFormat builds the Kiro API payload based on the source format. // This is critical because OpenAI and Claude formats have different tool structures: // - OpenAI: tools[].function.name, tools[].function.description // - Claude: tools[].name, tools[].description // headers parameter allows checking Anthropic-Beta header for thinking mode detection. // Returns the serialized JSON payload and a boolean indicating whether thinking mode was injected. func buildKiroPayloadForFormat(body []byte, modelID, profileArn, origin string, isAgentic, isChatOnly bool, sourceFormat sdktranslator.Format, headers http.Header) ([]byte, bool) { switch sourceFormat.String() { case "openai": log.Debugf("kiro: using OpenAI payload builder for source format: %s", sourceFormat.String()) return kiroopenai.BuildKiroPayloadFromOpenAI(body, modelID, profileArn, origin, isAgentic, isChatOnly, headers, nil) default: // Default to Claude format (also handles "claude", "kiro", etc.) log.Debugf("kiro: using Claude payload builder for source format: %s", sourceFormat.String()) return kiroclaude.BuildKiroPayload(body, modelID, profileArn, origin, isAgentic, isChatOnly, headers, nil) } } // determineAgenticMode determines if the model is an agentic or chat-only variant. // Returns (isAgentic, isChatOnly) based on model name suffixes. func determineAgenticMode(model string) (isAgentic, isChatOnly bool) { isAgentic = strings.HasSuffix(model, "-agentic") isChatOnly = strings.HasSuffix(model, "-chat") return isAgentic, isChatOnly } // getEffectiveProfileArn determines if profileArn should be included based on auth method. // profileArn is only needed for social auth (Google OAuth), not for builder-id (AWS SSO). func getEffectiveProfileArn(auth *cliproxyauth.Auth, profileArn string) string { if auth != nil && auth.Metadata != nil { if authMethod, ok := auth.Metadata["auth_method"].(string); ok && authMethod == "builder-id" { return "" // Don't include profileArn for builder-id auth } } return profileArn } // getEffectiveProfileArnWithWarning determines if profileArn should be included based on auth method, // and logs a warning if profileArn is missing for non-builder-id auth. // This consolidates the auth_method check that was previously done separately. func getEffectiveProfileArnWithWarning(auth *cliproxyauth.Auth, profileArn string) string { if auth != nil && auth.Metadata != nil { if authMethod, ok := auth.Metadata["auth_method"].(string); ok && (authMethod == "builder-id" || authMethod == "idc") { // builder-id and idc auth don't need profileArn return "" } } // For non-builder-id/idc auth (social auth), profileArn is required if profileArn == "" { log.Warnf("kiro: profile ARN not found in auth, API calls may fail") } return profileArn } // getKiroEndpointConfigs returns the list of Kiro API endpoint configurations to try in order. // Supports reordering based on "preferred_endpoint" in auth metadata/attributes. // For IDC auth method, automatically uses CodeWhisperer endpoint with CLI origin. func getKiroEndpointConfigs(auth *cliproxyauth.Auth) []kiroEndpointConfig { if auth == nil { return kiroEndpointConfigs } // For IDC auth, use CodeWhisperer endpoint with AI_EDITOR origin (same as Social auth) // Based on kiro2api analysis: IDC tokens work with CodeWhisperer endpoint using Bearer auth // The difference is only in how tokens are refreshed (OIDC with clientId/clientSecret for IDC) // NOT in how API calls are made - both Social and IDC use the same endpoint/origin if auth.Metadata != nil { authMethod, _ := auth.Metadata["auth_method"].(string) if authMethod == "idc" { log.Debugf("kiro: IDC auth, using CodeWhisperer endpoint") return kiroEndpointConfigs } } // Check for preference var preference string if auth.Metadata != nil { if p, ok := auth.Metadata["preferred_endpoint"].(string); ok { preference = p } } // Check attributes as fallback (e.g. from HTTP headers) if preference == "" && auth.Attributes != nil { preference = auth.Attributes["preferred_endpoint"] } if preference == "" { return kiroEndpointConfigs } preference = strings.ToLower(strings.TrimSpace(preference)) // Create new slice to avoid modifying global state var sorted []kiroEndpointConfig var remaining []kiroEndpointConfig for _, cfg := range kiroEndpointConfigs { name := strings.ToLower(cfg.Name) // Check for matches // CodeWhisperer aliases: codewhisperer, ide isMatch := false if (preference == "codewhisperer" || preference == "ide") && name == "codewhisperer" { isMatch = true } if isMatch { sorted = append(sorted, cfg) } else { remaining = append(remaining, cfg) } } // If preference didn't match anything, return default if len(sorted) == 0 { return kiroEndpointConfigs } // Combine: preferred first, then others return append(sorted, remaining...) }