Allow Bedrock Runtime selection during setup and login (#62222)

## Why

The app server's Bedrock setup and login flows always selected Mantle.
Clients using Bedrock Runtime had to configure that provider separately.

## What changed

Accept an optional `providerId` in `account/bedrock/setup` for AWS profiles
and environment credentials, and in `account/login/start` for Bedrock API
keys and AWS access keys. Pass `"amazon-bedrock-runtime"` to select
Runtime. Omit the field or pass `null` to keep the Mantle default.

Save configuration for the selected provider and check that provider for
an existing `aws.credential_export` before changing credential sources.

## Testing

Extend app-server tests to cover Runtime setup with profiles and
environment credentials, Runtime login with both key types, and Mantle
fallback. Verify that environment setup selects the Runtime default
model for a new thread and that a configured Runtime credential exporter
blocks profile setup.

GitOrigin-RevId: 6396b638208ab9b12f50f13f13bd517a919d0848
This commit is contained in:
alexsong-oai
2026-11-18 07:43:45 +00:00
committed by copyberry
parent bcff988bd1
commit 24e236514b
20 changed files with 267 additions and 44 deletions
+20 -10
View File
@@ -1305,11 +1305,14 @@ async fn login_account_api_key_succeeds_and_notifies() -> Result<()> {
Ok(())
}
#[test_case("amazonBedrock"; "api_key")]
#[test_case("amazonBedrockAccessKeys"; "access_keys")]
#[test_case("amazonBedrock", "amazon-bedrock"; "legacy_api_key")]
#[test_case("amazonBedrockAccessKeys", "amazon-bedrock"; "legacy_access_keys")]
#[test_case("amazonBedrock", "amazon-bedrock-runtime"; "runtime_api_key")]
#[test_case("amazonBedrockAccessKeys", "amazon-bedrock-runtime"; "runtime_access_keys")]
#[tokio::test]
async fn login_amazon_bedrock_replaces_primary_auth_and_persists_provider(
credential_type: &str,
provider_id: &str,
) -> Result<()> {
let managed_access_keys = credential_type == "amazonBedrockAccessKeys";
let codex_home = TempDir::new()?;
@@ -1319,9 +1322,9 @@ async fn login_amazon_bedrock_replaces_primary_auth_and_persists_provider(
std::fs::write(
&config_path,
format!(
"{original_config}\n[model_providers.amazon-bedrock]\n\
"{original_config}\n[model_providers.{provider_id}]\n\
http_headers = {{ X-Existing = \"preserved\" }}\n\
[model_providers.amazon-bedrock.aws]\n\
[model_providers.{provider_id}.aws]\n\
profile = \"stale-profile\"\n\
region = \"us-east-1\"\n\
auth_refresh = {{ command = \"aws\" }}\n"
@@ -1336,7 +1339,11 @@ async fn login_amazon_bedrock_replaces_primary_auth_and_persists_provider(
let mut mcp = TestAppServer::builder()
.with_codex_home(codex_home.path())
.without_auto_env()
.with_env_overrides(&[("OPENAI_API_KEY", None)])
.with_env_overrides(&[
("OPENAI_API_KEY", None),
("OPENAI_FEDERATION_RULE_ID", None),
("OPENAI_IDENTITY_TOKEN_FILE", None),
])
.build_initialized_with_timeout(DEFAULT_READ_TIMEOUT)
.await?;
let mut expected_config = read_config_toml(codex_home.path())?;
@@ -1345,17 +1352,17 @@ async fn login_amazon_bedrock_replaces_primary_auth_and_persists_provider(
.expect("config should be a table")
.insert(
"model_provider".to_string(),
toml::Value::String("amazon-bedrock".to_string()),
toml::Value::String(provider_id.to_string()),
);
expected_config["model_providers"]["amazon-bedrock"]["aws"]
expected_config["model_providers"][provider_id]["aws"]
.as_table_mut()
.expect("AWS configuration should be a table")
.remove("profile");
if managed_access_keys {
expected_config["model_providers"]["amazon-bedrock"]["aws"]["region"] =
expected_config["model_providers"][provider_id]["aws"]["region"] =
toml::Value::String("us-west-2".to_string());
}
let params = if managed_access_keys {
let mut params = if managed_access_keys {
json!({
"type": credential_type,
"accessKeyId": " test-id ",
@@ -1370,6 +1377,9 @@ async fn login_amazon_bedrock_replaces_primary_auth_and_persists_provider(
"region": " us-west-2 ",
})
};
if provider_id == "amazon-bedrock-runtime" {
params["providerId"] = json!(provider_id);
}
let request_id = mcp.send_login_account_request(params).await?;
let response: JSONRPCResponse = timeout(
DEFAULT_READ_TIMEOUT,
@@ -1450,7 +1460,7 @@ async fn login_amazon_bedrock_replaces_primary_auth_and_persists_provider(
.expect("config should be a table");
expected_logout_config_root.remove("model_provider");
expected_logout_config_root.remove("model");
expected_logout_config["model_providers"]["amazon-bedrock"]
expected_logout_config["model_providers"][provider_id]
.as_table_mut()
.expect("Bedrock provider config should be a table")
.remove("aws");
@@ -70,6 +70,7 @@ async fn checks_current_provider_after_login_without_restart() -> Result<()> {
params: LoginAccountParams::AmazonBedrock {
api_key: "test-key".to_string(),
region: "us-west-2".to_string(),
provider_id: None,
},
})
.await?;
@@ -2,6 +2,7 @@ use anyhow::Result;
use app_test_support::TestAppServer;
use codex_app_server_protocol::BedrockDiscoverParams;
use codex_app_server_protocol::BedrockDiscoverResponse;
use codex_app_server_protocol::BedrockProviderId;
use codex_app_server_protocol::BedrockSetupParams;
use codex_app_server_protocol::BedrockSetupResponse;
use codex_app_server_protocol::ClientRequest;
@@ -39,6 +40,9 @@ async fn bedrock_app_server(
TestAppServer::builder()
.with_codex_home(codex_home)
.with_env_overrides(&[
("OPENAI_API_KEY", None),
("OPENAI_FEDERATION_RULE_ID", None),
("OPENAI_IDENTITY_TOKEN_FILE", None),
("AWS_CONFIG_FILE", Some(aws_config_path.as_ref())),
(
"AWS_SHARED_CREDENTIALS_FILE",
@@ -99,6 +103,7 @@ async fn discover_bedrock_profiles_and_environment_credentials() -> Result<()> {
request_id,
params: BedrockSetupParams::Environment {
region: " us-east-2 ".to_string(),
provider_id: Some(BedrockProviderId::AmazonBedrockRuntime),
},
})
.await?;
@@ -110,8 +115,8 @@ async fn discover_bedrock_profiles_and_environment_credentials() -> Result<()> {
assert_eq!(
config,
toml::toml! {
model_provider = "amazon-bedrock"
[model_providers.amazon-bedrock.aws]
model_provider = "amazon-bedrock-runtime"
[model_providers.amazon-bedrock-runtime.aws]
region = "us-east-2"
}
.into()
@@ -126,11 +131,11 @@ async fn discover_bedrock_profiles_and_environment_credentials() -> Result<()> {
})
.await?;
let default_model = models.data.iter().find(|model| model.is_default).unwrap();
assert!(default_model.model.starts_with("openai."));
assert!(default_model.model.starts_with("global.openai."));
let thread = app_server
.start_thread(ThreadStartParams::default())
.await?;
assert_eq!(thread.model_provider, "amazon-bedrock");
assert_eq!(thread.model_provider, "amazon-bedrock-runtime");
assert_eq!(thread.model, default_model.model);
Ok(())
@@ -158,15 +163,13 @@ async fn setup_bedrock_profile_and_environment() -> Result<()> {
)
.await?;
let response: BedrockSetupResponse = app_server
.request(|request_id| ClientRequest::BedrockSetup {
request_id,
params: BedrockSetupParams::Profile {
profile: " engineering ".to_string(),
region: " us-west-2 ".to_string(),
},
})
let request_id = app_server
.send_raw_request(
"account/bedrock/setup",
Some(json!({"type":"profile", "profile":" engineering ", "region":" us-west-2 "})),
)
.await?;
let response: BedrockSetupResponse = app_server.read_response(request_id).await?;
assert_eq!(response, BedrockSetupResponse {});
let config: toml::Value = toml::from_str(&std::fs::read_to_string(&config_path)?)?;
assert_eq!(
@@ -185,6 +188,7 @@ async fn setup_bedrock_profile_and_environment() -> Result<()> {
request_id,
params: BedrockSetupParams::Environment {
region: "us-east-1".to_string(),
provider_id: None,
},
})
.await?;
@@ -215,6 +219,30 @@ async fn setup_bedrock_profile_and_environment() -> Result<()> {
.into()
);
let _: BedrockSetupResponse = app_server
.request(|request_id| ClientRequest::BedrockSetup {
request_id,
params: BedrockSetupParams::Profile {
profile: "engineering".to_string(),
region: "us-west-2".to_string(),
provider_id: Some(BedrockProviderId::AmazonBedrockRuntime),
},
})
.await?;
let saved: toml::Value = toml::from_str(&std::fs::read_to_string(&config_path)?)?;
let mut expected = config;
expected["model_provider"] = "amazon-bedrock-runtime".into();
expected["model_providers"].as_table_mut().unwrap().insert(
"amazon-bedrock-runtime".to_string(),
toml::toml! {
[aws]
profile = "engineering"
region = "us-west-2"
}
.into(),
);
assert_eq!(saved, expected);
let managed_home = TempDir::new()?;
let managed_config_path = managed_home.path().join("config.toml");
std::fs::write(&managed_config_path, "model_provider = 'openai'")?;
@@ -255,6 +283,7 @@ async fn setup_bedrock_profile_and_environment() -> Result<()> {
params: BedrockSetupParams::Profile {
profile: "engineering".to_string(),
region: "us-west-2".to_string(),
provider_id: None,
},
})
.await?;
@@ -358,7 +387,9 @@ async fn setup_bedrock_rejects_invalid_or_conflicting_credentials() -> Result<()
let source_path = layered_home_path.join(source_file);
let active_path = layered_home_path.join("selected.config.toml");
let exporter_config = "[model_providers.amazon-bedrock.aws.credential_export]\n\
command = \"secret-command\"\nargs = [\"secret-argument\"]\n";
command = \"secret-command\"\nargs = [\"secret-argument\"]\n\
[model_providers.amazon-bedrock-runtime.aws.credential_export]\n\
command = \"runtime-command\"\n";
std::fs::write(&config_path, "")?;
std::fs::write(&active_path, "")?;
std::fs::write(&source_path, exporter_config)?;
@@ -384,6 +415,10 @@ async fn setup_bedrock_rejects_invalid_or_conflicting_credentials() -> Result<()
"account/bedrock/setup",
json!({"type": "environment", "region": "us-west-2"}),
),
(
"account/bedrock/setup",
json!({"type": "profile", "profile": "engineering", "region": "us-west-2", "providerId": "amazon-bedrock-runtime"}),
),
(
"account/login/start",
json!({"type": "amazonBedrock", "apiKey": "new-key", "region": "us-west-2"}),