Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ members = ["crates/*"]
resolver = "2"

[workspace.package]
version = "0.1.200"
version = "0.1.201"
edition = "2024"
rust-version = "1.88"
license = "Apache-2.0"
Expand Down
70 changes: 65 additions & 5 deletions crates/csa-executor/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use std::time::Duration;

use crate::executor::Executor;
use crate::transport_gemini_retry::{
gemini_inject_api_key_fallback, gemini_max_attempts, gemini_rate_limit_backoff,
gemini_retry_model, gemini_should_use_api_key, is_gemini_rate_limited_error,
is_gemini_rate_limited_result,
gemini_auth_mode, gemini_inject_api_key_fallback, gemini_max_attempts,
gemini_rate_limit_backoff, gemini_retry_model, gemini_should_use_api_key,
is_gemini_rate_limited_error, is_gemini_rate_limited_result,
};
use anyhow::{Result, anyhow};
use async_trait::async_trait;
Expand Down Expand Up @@ -238,13 +238,35 @@ impl LegacyTransport {
idle_timeout_seconds: u64,
) -> Result<TransportResult> {
// 3-phase fallback: OAuth(original) → APIKey(original) → APIKey(flash)
let has_fallback_key = extra_env
.is_some_and(|env| env.contains_key(csa_core::gemini::API_KEY_FALLBACK_ENV_KEY));
let auth_mode = gemini_auth_mode(extra_env).unwrap_or("unknown");
let max_attempts = gemini_max_attempts(extra_env);
tracing::debug!(
max_attempts,
has_fallback_key,
auth_mode,
"gemini-cli legacy retry chain initialized"
);

let mut attempt = 1u8;
loop {
let executor = self.executor_for_attempt(attempt);

// Phase 2+: inject API key auth if available, otherwise keep original env.
let api_key_env = if gemini_should_use_api_key(attempt) {
gemini_inject_api_key_fallback(extra_env)
let injected = gemini_inject_api_key_fallback(extra_env);
if injected.is_none() {
tracing::warn!(
attempt,
auth_mode,
has_fallback_key,
"gemini-cli legacy: API key fallback unavailable for retry \
(auth_mode must be 'oauth' and _CSA_API_KEY_FALLBACK must be set); \
retrying with original auth"
);
}
injected
} else {
None
};
Expand Down Expand Up @@ -576,17 +598,39 @@ impl Transport for AcpTransport {

// Gemini-cli: 3-phase fallback: OAuth(original) → APIKey(original) → APIKey(flash)
let max_attempts = gemini_max_attempts(extra_env);
let has_fallback_key = extra_env
.is_some_and(|env| env.contains_key(csa_core::gemini::API_KEY_FALLBACK_ENV_KEY));
let auth_mode = gemini_auth_mode(extra_env).unwrap_or("unknown");
tracing::debug!(
max_attempts,
has_fallback_key,
auth_mode,
"gemini-cli ACP retry chain initialized"
);

let mut attempt = 1u8;
loop {
// Build ACP args for this attempt, injecting model override in phase 3.
let mut args = self.acp_args.clone();
if let Some(model) = gemini_retry_model(attempt) {
tracing::info!(attempt, model, "gemini-cli ACP: overriding model for retry");
args.extend(["-m".into(), model.into()]);
}

// Phase 2+: inject API key auth if available, otherwise keep original env.
let api_key_env = if gemini_should_use_api_key(attempt) {
gemini_inject_api_key_fallback(extra_env)
let injected = gemini_inject_api_key_fallback(extra_env);
if injected.is_none() {
tracing::warn!(
attempt,
auth_mode,
has_fallback_key,
"gemini-cli ACP: API key fallback unavailable for retry \
(auth_mode must be 'oauth' and _CSA_API_KEY_FALLBACK must be set); \
retrying with original auth"
);
}
injected
} else {
None
};
Comment on lines 600 to 636
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

There's significant code duplication here for initializing the retry chain and injecting the API key. This logic is nearly identical to the implementation in LegacyTransport::execute_in (lines 241-272).

Additionally, the LegacyTransport::execute method (lines 317-359 in the full file) which also contains retry logic, has not been updated with this new tracing and fallback checking, leading to inconsistent behavior.

To improve maintainability and ensure consistency, consider refactoring this duplicated logic into a shared helper function or a dedicated struct (e.g., GeminiRetryHandler). This would centralize the retry logic and make it easier to apply to all relevant transport methods.

Expand All @@ -603,6 +647,13 @@ impl Transport for AcpTransport {
tracing::debug!(%session_id, "resuming ACP session from tool state");
}

tracing::debug!(
attempt,
max_attempts,
has_api_key_override = api_key_env.is_some(),
"gemini-cli ACP: executing attempt"
);

let result = self
.execute_acp_attempt(
prompt,
Expand Down Expand Up @@ -635,6 +686,14 @@ impl Transport for AcpTransport {
continue;
}

if should_retry {
tracing::warn!(
attempt,
max_attempts,
"gemini-cli ACP: all retry phases exhausted, returning last result"
);
}

return result;
}
}
Expand Down Expand Up @@ -725,6 +784,7 @@ mod tests {
use crate::transport_gemini_retry::*;

include!("transport_tests_tail.rs");
include!("transport_tests_gemini_fallback.rs");
include!("transport_tests_extra.rs");
}

Expand Down
Loading
Loading