Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

bedrock: fix credential_process profiles, sigv4 signing, empty streams and claude 5 caching (#3750)

kt100e7053374b
diff --git a/crates/pa-ai/src/providers/bedrock/auth.rs b/crates/pa-ai/src/providers/bedrock/auth.rs
index e4f036d8fb..cb27328abf 100644
--- a/crates/pa-ai/src/providers/bedrock/auth.rs
+++ b/crates/pa-ai/src/providers/bedrock/auth.rs
@@ -3,6 +3,7 @@
//! endpoint / credential resolution rules from the TS provider.
use std::fmt::Write as _;
+use std::path::Path;
use hmac::{Hmac, Mac};
use sha2::{Digest, Sha256};
@@ -13,7 +14,7 @@ use crate::types::Model;
type HmacSha256 = Hmac<Sha256>;
/// Resolved AWS credentials (env vars, profile config, or SigV4-skip dummy).
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AwsCredentials {
pub access_key_id: String,
pub secret_access_key: String,
@@ -24,6 +25,13 @@ fn env(name: &str) -> Option<String> {
std::env::var(name).ok().filter(|value| !value.is_empty())
}
+/// AWS credential files use `[profile name]` (config) or `[name]`.
+pub(crate) fn profile_section_matches(name: &str, profile: &str) -> bool {
+ name == profile
+ || name == format!("profile {profile}")
+ || name.split_whitespace().last() == Some(profile)
+}
+
/// Parse an AWS credentials-style ini file: `[section-name]` headers with `key = value` pairs (only
/// the two credential lines are needed).
fn parse_ini_credentials(text: &str, section: &str) -> Option<AwsCredentials> {
@@ -35,10 +43,7 @@ fn parse_ini_credentials(text: &str, section: &str) -> Option<AwsCredentials> {
let trimmed = line.trim();
if trimmed.starts_with('[') && trimmed.ends_with(']') {
let name = trimmed[1..trimmed.len() - 1].trim();
- // AWS credential files use `[profile name]` (config) or `[name]`.
- in_section = name == section
- || name == format!("profile {section}")
- || name.split_whitespace().last() == Some(section);
+ in_section = profile_section_matches(name, section);
continue;
}
if !in_section {
@@ -62,9 +67,33 @@ fn parse_ini_credentials(text: &str, section: &str) -> Option<AwsCredentials> {
})
}
+/// Where the shared credentials and config files live.
+#[derive(Debug, PartialEq)]
+struct AwsFilePaths {
+ credentials: Option<std::path::PathBuf>,
+ config: Option<std::path::PathBuf>,
+}
+
+/// `AWS_SHARED_CREDENTIALS_FILE` / `AWS_CONFIG_FILE` from `lookup` when set, else the defaults under
+/// `home`; an override needs no home directory.
+fn aws_file_paths(lookup: impl Fn(&str) -> Option<String>, home: Option<&Path>) -> AwsFilePaths {
+ let path = |var: &str, default: &str| {
+ lookup(var)
+ .map(std::path::PathBuf::from)
+ .or_else(|| home.map(|home| home.join(default)))
+ };
+ AwsFilePaths {
+ credentials: path("AWS_SHARED_CREDENTIALS_FILE", ".aws/credentials"),
+ config: path("AWS_CONFIG_FILE", ".aws/config"),
+ }
+}
+
/// The SDK credential chain used by the TS provider: SigV4-skip dummy keys, static env credentials,
-/// then the shared credentials file (default or `AWS_PROFILE` / `options.profile`).
-pub fn resolve_credentials(profile: Option<&str>) -> Option<AwsCredentials> {
+/// the shared credentials file (`AWS_SHARED_CREDENTIALS_FILE` or `~/.aws/credentials`; default or
+/// `AWS_PROFILE` / `options.profile`), then the profile's `credential_process` in the AWS config
+/// file (`AWS_CONFIG_FILE` or `~/.aws/config`). May run the credential process; dropping the future
+/// kills it.
+pub async fn resolve_credentials(profile: Option<&str>) -> Option<AwsCredentials> {
if std::env::var("AWS_BEDROCK_SKIP_AUTH").as_deref() == Ok("1") {
return Some(AwsCredentials {
access_key_id: "dummy-access-key".to_string(),
@@ -85,10 +114,18 @@ pub fn resolve_credentials(profile: Option<&str>) -> Option<AwsCredentials> {
.map(std::string::ToString::to_string)
.or_else(|| env("AWS_PROFILE"))
.unwrap_or_else(|| "default".to_string());
- let home = pa_types::platform::home_dir()?;
- let path = home.join(".aws/credentials");
- let text = std::fs::read_to_string(path).ok()?;
- parse_ini_credentials(&text, &profile)
+ let paths = aws_file_paths(env, pa_types::platform::home_dir().as_deref());
+ if let Some(credentials_path) = &paths.credentials {
+ let shared = tokio::fs::read_to_string(credentials_path)
+ .await
+ .ok()
+ .and_then(|text| parse_ini_credentials(&text, &profile));
+ if shared.is_some() {
+ return shared;
+ }
+ }
+ let config = tokio::fs::read_to_string(paths.config?).await.ok()?;
+ crate::providers::bedrock::credential_process::credentials_from_process(&profile, &config).await
}
fn sha256_hex(bytes: &[u8]) -> String {
@@ -103,10 +140,7 @@ fn hmac_sha256(key: &[u8], data: &[u8]) -> Vec<u8> {
/// UTC timestamp in `SigV4` formats: `x-amz-date` (20250101T000000Z) and date stamp (20250101).
/// Uses std time to avoid a chrono dependency.
-fn now_utc_parts() -> (String, String) {
- let seconds = std::time::SystemTime::now()
- .duration_since(std::time::UNIX_EPOCH)
- .map_or(0, |duration| duration.as_secs());
+fn utc_parts(seconds: u64) -> (String, String) {
let (year, month, day, hour, minute, second) = civil_from_unix(seconds);
(
format!("{year:04}{month:02}{day:02}T{hour:02}{minute:02}{second:02}Z"),
@@ -144,8 +178,8 @@ fn civil_from_unix(secs: u64) -> (i64, u32, u32, u32, u32, u32) {
/// x-amz-security-token)`.
pub struct SigV4Params<'a> {
pub method: &'a str,
- /// Path with query string, starting with `/`.
- pub path_and_query: &'a str,
+ /// Request path, starting with `/`; signed requests carry no query string.
+ pub path: &'a str,
pub host: &'a str,
pub region: &'a str,
pub service: &'a str,
@@ -154,13 +188,57 @@ pub struct SigV4Params<'a> {
pub extra_signed_headers: &'a [(String, String)],
}
+/// Percent-encode every byte outside the `SigV4` unreserved set (`/` kept as the separator).
+fn uri_encode_path(path: &str) -> String {
+ let mut encoded = String::new();
+ for byte in path.bytes() {
+ let c = byte as char;
+ if c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.' | '~' | '/') {
+ encoded.push(c);
+ } else {
+ let _ = write!(encoded, "%{byte:02X}");
+ }
+ }
+ encoded
+}
+
+/// The `SigV4` canonical request: method, canonical URI, an empty canonical query string (signed
+/// requests carry none), the canonical headers, a blank line, the signed header names and the body
+/// hash.
+/// Every service except S3 encodes the already-escaped request path a second time, so a model id
+/// sent as `...v1%3A0` is signed as `...v1%253A0`.
+fn canonical_request(
+ method: &str,
+ path: &str,
+ canonical_headers_text: &str,
+ signed_headers: &str,
+ payload_hash: &str,
+) -> String {
+ let canonical_uri = uri_encode_path(path);
+ format!(
+ "{method}\n{canonical_uri}\n\n{canonical_headers_text}\n{signed_headers}\n{payload_hash}"
+ )
+}
+
/// Produce the `SigV4` auth headers for a request. Returns `(x-amz-date, authorization,
/// x-amz-security-token)`.
pub fn sigv4_headers(
params: &SigV4Params<'_>,
credentials: &AwsCredentials,
) -> (String, String, Option<String>) {
- let (amz_date, date_stamp) = now_utc_parts();
+ let now = std::time::SystemTime::now()
+ .duration_since(std::time::UNIX_EPOCH)
+ .map_or(0, |duration| duration.as_secs());
+ sigv4_headers_at(params, credentials, now)
+}
+
+/// [`sigv4_headers`] for a fixed signing time (Unix seconds).
+fn sigv4_headers_at(
+ params: &SigV4Params<'_>,
+ credentials: &AwsCredentials,
+ unix_seconds: u64,
+) -> (String, String, Option<String>) {
+ let (amz_date, date_stamp) = utc_parts(unix_seconds);
let payload_hash = sha256_hex(params.body);
let mut canonical_headers: Vec<(String, String)> = vec![
@@ -169,7 +247,9 @@ pub fn sigv4_headers(
("x-amz-date".to_string(), amz_date.clone()),
];
for (name, value) in params.extra_signed_headers {
- canonical_headers.push((name.to_lowercase(), value.clone()));
+ // Canonical header values are trimmed with inner whitespace runs collapsed.
+ let value = value.split_whitespace().collect::<Vec<_>>().join(" ");
+ canonical_headers.push((name.to_lowercase(), value));
}
if let Some(token) = &credentials.session_token {
canonical_headers.push(("x-amz-security-token".to_string(), token.clone()));
@@ -189,9 +269,12 @@ pub fn sigv4_headers(
text
});
- let canonical_request = format!(
- "{}\n{}\n{canonical_headers_text}\n{signed_headers}\n{payload_hash}",
- params.method, params.path_and_query
+ let canonical_request = canonical_request(
+ params.method,
+ params.path,
+ &canonical_headers_text,
+ &signed_headers,
+ &payload_hash,
);
let credential_scope = format!(
@@ -282,13 +365,13 @@ pub(crate) fn resolve_endpoint(model: &Model, options: &BedrockOptions) -> (Stri
&model.base_url,
)
})
- .or_else(env_region_or_profile)
+ .or_else(|| env_region_or_profile(env, pa_types::platform::home_dir().as_deref()))
.unwrap_or_else(|| "us-east-1".to_string());
return (model.base_url.trim_end_matches('/').to_string(), region);
}
let region = configured_region
- .or_else(env_region_or_profile)
+ .or_else(|| env_region_or_profile(env, pa_types::platform::home_dir().as_deref()))
.unwrap_or_else(|| "us-east-1".to_string());
(
format!("https://bedrock-runtime.{region}.amazonaws.com"),
@@ -296,22 +379,17 @@ pub(crate) fn resolve_endpoint(model: &Model, options: &BedrockOptions) -> (Stri
)
}
-/// AWS_PROFILE-resolved region from `~/.aws/config`, else None.
-fn env_region_or_profile() -> Option<String> {
- if let Some(region) = std::env::var("AWS_REGION").ok().filter(|v| !v.is_empty()) {
- return Some(region);
- }
- if let Some(region) = std::env::var("AWS_DEFAULT_REGION")
- .ok()
- .filter(|v| !v.is_empty())
- {
+/// `AWS_REGION` / `AWS_DEFAULT_REGION` from `lookup`, else the `AWS_PROFILE` region from the AWS
+/// config file (`AWS_CONFIG_FILE` or `~/.aws/config`), else None.
+fn env_region_or_profile(
+ lookup: impl Fn(&str) -> Option<String>,
+ home: Option<&Path>,
+) -> Option<String> {
+ if let Some(region) = lookup("AWS_REGION").or_else(|| lookup("AWS_DEFAULT_REGION")) {
return Some(region);
}
- let profile = std::env::var("AWS_PROFILE")
- .ok()
- .filter(|v| !v.is_empty())?;
- let home = pa_types::platform::home_dir()?;
- let text = std::fs::read_to_string(home.join(".aws/config")).ok()?;
+ let profile = lookup("AWS_PROFILE")?;
+ let text = std::fs::read_to_string(aws_file_paths(&lookup, home).config?).ok()?;
let mut in_section = false;
for line in text.lines() {
let trimmed = line.trim();
@@ -331,3 +409,156 @@ fn env_region_or_profile() -> Option<String> {
}
None
}
+
+#[cfg(test)]
+mod sigv4_tests {
+ use super::*;
+
+ const PATH: &str = "/model/anthropic.claude-3-5-haiku-20241022-v1%3A0/converse-stream";
+ /// 2015-08-30T12:36:00Z.
+ const SIGNED_AT: u64 = 1_440_938_160;
+
+ fn example_credentials(session_token: Option<&str>) -> AwsCredentials {
+ AwsCredentials {
+ access_key_id: "AKIDEXAMPLE".into(),
+ secret_access_key: "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY".into(),
+ session_token: session_token.map(str::to_string),
+ }
+ }
+
+ fn sign(
+ extra_signed_headers: &[(String, String)],
+ session_token: Option<&str>,
+ ) -> (String, String, Option<String>) {
+ sigv4_headers_at(
+ &SigV4Params {
+ method: "POST",
+ path: PATH,
+ host: "bedrock-runtime.us-east-1.amazonaws.com",
+ region: "us-east-1",
+ service: "bedrock",
+ body: br#"{"messages":[]}"#,
+ extra_signed_headers,
+ },
+ &example_credentials(session_token),
+ SIGNED_AT,
+ )
+ }
+
+ /// Expected values come from botocore's `SigV4Auth` signing the same request at the same time.
+ #[test]
+ fn signature_matches_the_aws_sdk_for_a_converse_stream_request() {
+ assert_eq!(
+ sign(&[], None),
+ (
+ "20150830T123600Z".to_string(),
+ "AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/bedrock/aws4_request, SignedHeaders=content-type;host;x-amz-date, Signature=2b962e4a0a2957cde2764ba8f78dae35436939649a081a25a45c3bd64dfe16d2".to_string(),
+ None,
+ )
+ );
+ }
+
+ /// Same request with a session token and the request-metadata header (whose value botocore
+ /// canonicalizes by collapsing inner whitespace runs).
+ #[test]
+ fn signature_matches_the_aws_sdk_with_a_session_token_and_metadata_header() {
+ let metadata = vec![(
+ "x-amzn-bedrock-request-metadata".to_string(),
+ r#"{"team": "a"}"#.to_string(),
+ )];
+ assert_eq!(
+ sign(&metadata, Some("session-token")),
+ (
+ "20150830T123600Z".to_string(),
+ "AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/bedrock/aws4_request, SignedHeaders=content-type;host;x-amz-date;x-amz-security-token;x-amzn-bedrock-request-metadata, Signature=607ee2b6778ba42dc6f50e8aa52b419c3aa6eaeb0d4ef441787fcb6a3b2cdb15".to_string(),
+ Some("session-token".to_string()),
+ )
+ );
+ }
+
+ #[test]
+ fn canonical_request_has_an_empty_query_line_and_double_encodes_the_path() {
+ assert_eq!(
+ canonical_request("POST", PATH, "host:example\n", "host", "abc"),
+ "POST\n/model/anthropic.claude-3-5-haiku-20241022-v1%253A0/converse-stream\n\nhost:example\n\nhost\nabc"
+ );
+ }
+}
+
+#[cfg(test)]
+mod file_path_tests {
+ use super::*;
+
+ #[test]
+ fn file_overrides_apply_with_or_without_a_home_directory() {
+ let overrides = |var: &str| match var {
+ "AWS_SHARED_CREDENTIALS_FILE" => Some("/etc/aws/credentials".to_string()),
+ "AWS_CONFIG_FILE" => Some("/etc/aws/config".to_string()),
+ _ => None,
+ };
+ let unset = |_: &str| None;
+ let home = Path::new("/home/user");
+ let overridden = AwsFilePaths {
+ credentials: Some("/etc/aws/credentials".into()),
+ config: Some("/etc/aws/config".into()),
+ };
+
+ assert_eq!(
+ [
+ aws_file_paths(overrides, None),
+ aws_file_paths(overrides, Some(home)),
+ aws_file_paths(unset, Some(home)),
+ aws_file_paths(unset, None),
+ ],
+ [
+ overridden,
+ AwsFilePaths {
+ credentials: Some("/etc/aws/credentials".into()),
+ config: Some("/etc/aws/config".into()),
+ },
+ AwsFilePaths {
+ credentials: Some("/home/user/.aws/credentials".into()),
+ config: Some("/home/user/.aws/config".into()),
+ },
+ AwsFilePaths {
+ credentials: None,
+ config: None,
+ },
+ ]
+ );
+ }
+}
+
+#[cfg(test)]
+mod region_tests {
+ use super::*;
+
+ #[test]
+ fn the_profile_region_comes_from_the_config_file_override_without_a_home_directory() {
+ let dir = tempfile::tempdir().expect("tempdir");
+ let config = dir.path().join("config");
+ std::fs::write(
+ &config,
+ "[default]\nregion = us-east-1\n\n[profile work]\nregion = eu-central-1\n",
+ )
+ .expect("write config");
+ let config = config.display().to_string();
+ let lookup = |var: &str| match var {
+ "AWS_PROFILE" => Some("work".to_string()),
+ "AWS_CONFIG_FILE" => Some(config.clone()),
+ _ => None,
+ };
+ let empty_home = dir.path().join("home");
+
+ assert_eq!(
+ [
+ env_region_or_profile(lookup, None),
+ env_region_or_profile(lookup, Some(&empty_home)),
+ ],
+ [
+ Some("eu-central-1".to_string()),
+ Some("eu-central-1".to_string())
+ ]
+ );
+ }
+}
diff --git a/crates/pa-ai/src/providers/bedrock/convert.rs b/crates/pa-ai/src/providers/bedrock/convert.rs
index 51d95d0a72..69f6ec5593 100644
--- a/crates/pa-ai/src/providers/bedrock/convert.rs
+++ b/crates/pa-ai/src/providers/bedrock/convert.rs
@@ -81,10 +81,29 @@ pub fn supports_prompt_caching(model: &Model) -> bool {
return std::env::var("AWS_BEDROCK_FORCE_CACHE").as_deref() == Ok("1");
}
candidates.iter().any(|s| {
- s.contains("-4-") || s.contains("claude-3-7-sonnet") || s.contains("claude-3-5-haiku")
+ s.contains("-4-")
+ || s.contains("claude-3-7-sonnet")
+ || s.contains("claude-3-5-haiku")
+ || claude_generation(s).is_some_and(|generation| generation >= 5)
})
}
+/// The generation number in a `claude-<family>-<generation>` id (`claude-opus-5-5` is 5), so Claude 5
+/// and later models are recognised without listing each release.
+fn claude_generation(candidate: &str) -> Option<u32> {
+ ["fable", "mythos", "opus", "sonnet", "haiku"]
+ .iter()
+ .find_map(|family| {
+ let marker = format!("claude-{family}-");
+ let start = candidate.find(&marker)? + marker.len();
+ let digits: String = candidate[start..]
+ .chars()
+ .take_while(char::is_ascii_digit)
+ .collect();
+ digits.parse().ok()
+ })
+}
+
pub fn supports_thinking_signature(model: &Model) -> bool {
is_anthropic_claude_model(model)
}
@@ -446,3 +465,56 @@ mod supports_always_on_adaptive_thinking_tests {
));
}
}
+
+#[cfg(test)]
+mod prompt_caching_gate_tests {
+ use super::*;
+
+ fn model_with_id(id: &str) -> Model {
+ Model {
+ id: id.into(),
+ name: id.into(),
+ api: crate::providers::bedrock::API_BEDROCK_CONVERSE_STREAM.to_string(),
+ provider: "amazon-bedrock".into(),
+ base_url: "https://bedrock-runtime.us-west-2.amazonaws.com".into(),
+ reasoning: true,
+ thinking_level_map: None,
+ input: vec![crate::types::ModelInput::Text],
+ cost: crate::types::zero_model_cost(),
+ context_window: 200_000,
+ max_tokens: 8192,
+ max_tokens_explicit: false,
+ featured: None,
+ headers: None,
+ compat: None,
+ }
+ }
+
+ #[test]
+ fn claude_5_and_later_get_cache_points_like_the_4_family() {
+ for id in [
+ "global.anthropic.claude-sonnet-5-5",
+ "global.anthropic.claude-opus-5-5",
+ "global.anthropic.claude-opus-5",
+ "us.anthropic.claude-sonnet-5",
+ "global.anthropic.claude-fable-5-1",
+ "us.anthropic.claude-opus-5-5-v1:0",
+ "us.anthropic.claude-opus-4-6-v1",
+ "global.anthropic.claude-haiku-4-5-20251001-v1:0",
+ "global.anthropic.claude-opus-10-0",
+ ] {
+ assert!(supports_prompt_caching(&model_with_id(id)), "{id}");
+ }
+ }
+
+ #[test]
+ fn older_claude_ids_still_do_not() {
+ for id in [
+ "anthropic.claude-3-haiku-20240307-v1:0",
+ "anthropic.claude-v2:1",
+ "anthropic.claude-3-5-sonnet-20240620-v1:0",
+ ] {
+ assert!(!supports_prompt_caching(&model_with_id(id)), "{id}");
+ }
+ }
+}
diff --git a/crates/pa-ai/src/providers/bedrock/credential_process.rs b/crates/pa-ai/src/providers/bedrock/credential_process.rs
new file mode 100644
index 0000000000..7310033f18
--- /dev/null
+++ b/crates/pa-ai/src/providers/bedrock/credential_process.rs
@@ -0,0 +1,428 @@
+//! AWS `credential_process` support: a profile in the AWS config file names a command that prints
+//! version 1 credential JSON (`AccessKeyId`, `SecretAccessKey`, `SessionToken`, `Expiration`). The
+//! result is cached per profile and command until shortly before it expires.
+
+use std::collections::HashMap;
+use std::sync::{LazyLock, Mutex};
+
+use serde_json::Value;
+
+use crate::providers::bedrock::auth::{profile_section_matches, AwsCredentials};
+
+/// Refresh this long before the credentials expire so a request never signs with a dead token.
+const REFRESH_MARGIN_SECS: u64 = 300;
+
+struct CachedCredentials {
+ credentials: AwsCredentials,
+ /// `None` when the process reported no `Expiration` (non-expiring credentials).
+ expires_at: Option<u64>,
+}
+
+/// Keyed by profile name and command, so an edited `credential_process` never serves the old
+/// command's credentials.
+static CACHE: LazyLock<Mutex<HashMap<String, CachedCredentials>>> =
+ LazyLock::new(|| Mutex::new(HashMap::new()));
+
+/// When the process output's `Expiration` says the credentials lapse.
+enum Expiry {
+ Never,
+ At(u64),
+ /// Present but not a timestamp we can read: use the credentials once, never cache them.
+ Unknown,
+}
+
+/// The `credential_process` command of `profile` in an AWS config file.
+fn find_command(config: &str, profile: &str) -> Option<String> {
+ let mut in_section = false;
+ for line in config.lines() {
+ let trimmed = line.trim();
+ if trimmed.starts_with('[') && trimmed.ends_with(']') {
+ in_section = profile_section_matches(trimmed[1..trimmed.len() - 1].trim(), profile);
+ continue;
+ }
+ if !in_section {
+ continue;
+ }
+ if let Some((key, value)) = trimmed.split_once('=') {
+ if key.trim() == "credential_process" && !value.trim().is_empty() {
+ return Some(value.trim().to_string());
+ }
+ }
+ }
+ None
+}
+
+/// Seconds since the Unix epoch for an RFC 3339 timestamp (`Z` or a `±HH:MM` offset); fractional
+/// seconds are ignored.
+fn parse_rfc3339(text: &str) -> Option<u64> {
+ let (date, rest) = text.trim().split_once(['T', 't'])?;
+ let (time, offset_secs) = if let Some(time) = rest.strip_suffix(['Z', 'z']) {
+ (time, 0)
+ } else {
+ let split = rest.rfind(['+', '-'])?;
+ let (time, offset) = rest.split_at(split);
+ let sign = if offset.starts_with('-') { -1 } else { 1 };
+ let (hours, minutes) = offset[1..].split_once(':')?;
+ let hours: i64 = hours.parse().ok()?;
+ let minutes: i64 = minutes.parse().ok()?;
+ (time, sign * (hours * 3600 + minutes * 60))
+ };
+ let mut date_parts = date.split('-');
+ let year: i64 = date_parts.next()?.parse().ok()?;
+ let month: i64 = date_parts.next()?.parse().ok()?;
+ let day: i64 = date_parts.next()?.parse().ok()?;
+ let mut time_parts = time.split('.').next()?.split(':');
+ let hour: i64 = time_parts.next()?.parse().ok()?;
+ let minute: i64 = time_parts.next()?.parse().ok()?;
+ let second: i64 = time_parts.next()?.parse().ok()?;
+ if date_parts.next().is_some() || time_parts.next().is_some() || !(1..=12).contains(&month) {
+ return None;
+ }
+ // Howard Hinnant's days_from_civil, the inverse of `auth::civil_from_unix`.
+ let y = if month <= 2 { year - 1 } else { year };
+ let era = y.div_euclid(400);
+ let yoe = y.rem_euclid(400);
+ let mp = (month + 9) % 12;
+ let doy = (153 * mp + 2) / 5 + day - 1;
+ let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
+ let days = era * 146_097 + doe - 719_468;
+ u64::try_from(days * 86_400 + hour * 3600 + minute * 60 + second - offset_secs).ok()
+}
+
+/// Parse what a `credential_process` prints (the AWS version 1 JSON document).
+fn parse_output(stdout: &[u8]) -> Option<(AwsCredentials, Expiry)> {
+ let value: Value = serde_json::from_slice(stdout).ok()?;
+ let field = |name: &str| {
+ value
+ .get(name)
+ .and_then(Value::as_str)
+ .filter(|text| !text.is_empty())
+ .map(str::to_string)
+ };
+ let credentials = AwsCredentials {
+ access_key_id: field("AccessKeyId")?,
+ secret_access_key: field("SecretAccessKey")?,
+ session_token: field("SessionToken"),
+ };
+ let expiry = match field("Expiration") {
+ None => Expiry::Never,
+ Some(text) => parse_rfc3339(&text).map_or(Expiry::Unknown, Expiry::At),
+ };
+ Some((credentials, expiry))
+}
+
+/// Dropping the returned future kills the spawned shell (and the command, when the shell runs it
+/// in place), so a caller that stops waiting (cancellation, timeout) is not tied to a stalled
+/// `credential_process`.
+async fn run(command: &str) -> Option<Vec<u8>> {
+ // Like Node's `exec` (the TS SDK's runner): `cmd /d /s /c "<command>"` passed verbatim, since
+ // cmd.exe does not understand the MSVC quoting `arg` applies, which breaks quoted paths.
+ #[cfg(windows)]
+ let mut process = {
+ use std::os::windows::process::CommandExt as _;
+ let mut process = tokio::process::Command::new("cmd");
+ process
+ .as_std_mut()
+ .raw_arg(format!("/d /s /c \"{command}\""));
+ process
+ };
+ #[cfg(not(windows))]
+ let mut process = {
+ let mut process = tokio::process::Command::new("sh");
+ process.args(["-c", command]);
+ process
+ };
+ // `output()` closes stdin and captures stderr, so neither reaches the TUI.
+ let output = process.kill_on_drop(true).output().await.ok()?;
+ if !output.status.success() {
+ tracing::warn!(
+ status = %output.status,
+ stderr = %String::from_utf8_lossy(&output.stderr).trim(),
+ "AWS credential_process exited unsuccessfully"
+ );
+ return None;
+ }
+ Some(output.stdout)
+}
+
+fn unix_now() -> u64 {
+ std::time::SystemTime::now()
+ .duration_since(std::time::UNIX_EPOCH)
+ .map_or(0, |duration| duration.as_secs())
+}
+
+/// Credentials from `profile`'s `credential_process` in the AWS config file text, served from the
+/// cache while they stay valid for more than `REFRESH_MARGIN_SECS`.
+pub(crate) async fn credentials_from_process(
+ profile: &str,
+ config: &str,
+) -> Option<AwsCredentials> {
+ let now = unix_now();
+ let command = find_command(config, profile)?;
+ let cache_key = format!("{profile}\0{command}");
+ if let Ok(cache) = CACHE.lock() {
+ if let Some(entry) = cache.get(&cache_key) {
+ if entry
+ .expires_at
+ .is_none_or(|expires_at| expires_at > now + REFRESH_MARGIN_SECS)
+ {
+ return Some(entry.credentials.clone());
+ }
+ }
+ }
+ let Some((credentials, expiry)) = run(&command).await.as_deref().and_then(parse_output) else {
+ tracing::warn!("AWS credential_process printed no usable credentials");
+ return None;
+ };
+ let expires_at = match expiry {
+ Expiry::Never => None,
+ Expiry::At(expires_at) => Some(expires_at),
+ Expiry::Unknown => Some(now),
+ };
+ if let Ok(mut cache) = CACHE.lock() {
+ cache.insert(
+ cache_key,
+ CachedCredentials {
+ credentials: credentials.clone(),
+ expires_at,
+ },
+ );
+ }
+ Some(credentials)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn finds_the_command_for_the_named_profile_only() {
+ let config = "[profile a]\ncredential_process = echo a\n\n[profile b]\nregion = x\ncredential_process=/bin/b --flag\n\n[default]\ncredential_process = echo d\n";
+ assert_eq!(find_command(config, "a").as_deref(), Some("echo a"));
+ assert_eq!(find_command(config, "b").as_deref(), Some("/bin/b --flag"));
+ assert_eq!(find_command(config, "default").as_deref(), Some("echo d"));
+ assert_eq!(find_command(config, "missing"), None);
+ }
+
+ #[test]
+ fn parses_rfc3339_timestamps() {
+ assert_eq!(parse_rfc3339("1970-01-01T00:00:00Z"), Some(0));
+ assert_eq!(parse_rfc3339("2000-03-01T00:00:00.123Z"), Some(951_868_800));
+ assert_eq!(
+ parse_rfc3339("2026-10-12T23:20:44+00:00"),
+ Some(1_791_847_244)
+ );
+ assert_eq!(
+ parse_rfc3339("2026-10-13T01:20:44+02:00"),
+ Some(1_791_847_244)
+ );
+ assert_eq!(
+ parse_rfc3339("2026-10-12T22:20:44-01:00"),
+ Some(1_791_847_244)
+ );
+ assert_eq!(parse_rfc3339("tomorrow"), None);
+ }
+
+ #[cfg(unix)]
+ fn stub_script(dir: &std::path::Path, body: &str) -> std::path::PathBuf {
+ use std::os::unix::fs::PermissionsExt as _;
+ let path = dir.join("credential-process.sh");
+ std::fs::write(&path, format!("#!/bin/sh\n{body}\n")).expect("write stub");
+ std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755))
+ .expect("chmod stub");
+ path
+ }
+
+ /// A profile whose only credential source is a `credential_process` stub script resolves to
+ /// the script's credentials, and a second request is served from the cache.
+ #[cfg(unix)]
+ #[tokio::test]
+ async fn a_credential_process_profile_runs_its_stub_script_once() {
+ let dir = tempfile::tempdir().expect("tempdir");
+ let runs = dir.path().join("runs");
+ let script = stub_script(
+ dir.path(),
+ &format!(
+ "echo run >> '{}'\nprintf '%s' '{{\"Version\":1,\"AccessKeyId\":\"AKIDSTUB\",\"SecretAccessKey\":\"stub-secret\",\"SessionToken\":\"stub-token\",\"Expiration\":\"2999-01-01T00:00:00Z\"}}'",
+ runs.display()
+ ),
+ );
+ let config = format!(
+ "[default]\nregion = us-east-1\n\n[profile cp-stub-cached]\nregion = us-west-2\ncredential_process = {}\n",
+ script.display()
+ );
+
+ let first = credentials_from_process("cp-stub-cached", &config)
+ .await
+ .expect("process runs");
+ let second = credentials_from_process("cp-stub-cached", &config)
+ .await
+ .expect("cached");
+
+ let expected = AwsCredentials {
+ access_key_id: "AKIDSTUB".into(),
+ secret_access_key: "stub-secret".into(),
+ session_token: Some("stub-token".into()),
+ };
+ assert_eq!((first, second), (expected.clone(), expected));
+ let runs = std::fs::read_to_string(runs).expect("runs log");
+ assert_eq!(runs.lines().count(), 1);
+ }
+
+ /// Credentials inside the refresh margin are fetched again rather than reused.
+ #[cfg(unix)]
+ #[tokio::test]
+ async fn expiring_process_credentials_are_refreshed() {
+ let dir = tempfile::tempdir().expect("tempdir");
+ let runs = dir.path().join("runs");
+ let script = stub_script(
+ dir.path(),
+ &format!(
+ "echo run >> '{}'\nprintf '%s' '{{\"Version\":1,\"AccessKeyId\":\"AKIDSOON\",\"SecretAccessKey\":\"s\",\"Expiration\":\"2000-01-01T00:00:00Z\"}}'",
+ runs.display()
+ ),
+ );
+ let config = format!(
+ "[profile cp-stub-expiring]\ncredential_process = {}\n",
+ script.display()
+ );
+
+ assert!(credentials_from_process("cp-stub-expiring", &config)
+ .await
+ .is_some());
+ assert!(credentials_from_process("cp-stub-expiring", &config)
+ .await
+ .is_some());
+ let runs = std::fs::read_to_string(runs).expect("runs log");
+ assert_eq!(runs.lines().count(), 2);
+ }
+
+ /// Editing a profile's `credential_process` runs the new command instead of serving the old
+ /// command's cached credentials.
+ #[cfg(unix)]
+ #[tokio::test]
+ async fn a_changed_command_does_not_reuse_the_old_commands_credentials() {
+ let credential_json = |key: &str| {
+ format!(
+ "printf '%s' '{{\"Version\":1,\"AccessKeyId\":\"{key}\",\"SecretAccessKey\":\"s\"}}'"
+ )
+ };
+ let (old_dir, new_dir) = (
+ tempfile::tempdir().expect("tempdir"),
+ tempfile::tempdir().expect("tempdir"),
+ );
+ let old = stub_script(old_dir.path(), &credential_json("AKIDOLD"));
+ let new = stub_script(new_dir.path(), &credential_json("AKIDNEW"));
+ let config_for = |script: &std::path::Path| {
+ format!(
+ "[profile cp-stub-edited]\ncredential_process = {}\n",
+ script.display()
+ )
+ };
+
+ let before = credentials_from_process("cp-stub-edited", &config_for(&old))
+ .await
+ .expect("old command runs");
+ let after = credentials_from_process("cp-stub-edited", &config_for(&new))
+ .await
+ .expect("new command runs");
+
+ assert_eq!(
+ (before.access_key_id, after.access_key_id),
+ ("AKIDOLD".to_string(), "AKIDNEW".to_string())
+ );
+ }
+
+ /// A caller that stops waiting on a stalled process (cancellation or timeout drops the future)
+ /// kills it rather than leaving it running.
+ #[cfg(unix)]
+ #[tokio::test]
+ async fn dropping_a_stalled_resolution_kills_the_process() {
+ let dir = tempfile::tempdir().expect("tempdir");
+ let pid_file = dir.path().join("pid");
+ // The shell records its own pid, then becomes the stalled command in place.
+ let config = format!(
+ "[profile cp-stub-stalled]\ncredential_process = echo $$ > '{}'; exec sleep 600\n",
+ pid_file.display()
+ );
+
+ let resolution = credentials_from_process("cp-stub-stalled", &config);
+ let pid = tokio::select! {
+ credentials = resolution => panic!("a stalled process resolved: {credentials:?}"),
+ pid = async {
+ loop {
+ if let Some(pid) = std::fs::read_to_string(&pid_file)
+ .ok()
+ .and_then(|text| text.trim().parse::<u32>().ok())
+ {
+ break pid;
+ }
+ tokio::time::sleep(std::time::Duration::from_millis(10)).await;
+ }
+ } => pid,
+ };
+
+ // The resolution future is dropped once the select returns; its process must exit.
+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
+ loop {
+ let state = std::process::Command::new("ps")
+ .args(["-o", "stat=", "-p", &pid.to_string()])
+ .output()
+ .expect("ps runs");
+ let state = String::from_utf8_lossy(&state.stdout);
+ if state.trim().is_empty() || state.trim_start().starts_with('Z') {
+ break;
+ }
+ assert!(
+ std::time::Instant::now() < deadline,
+ "credential_process {pid} still running: {state}"
+ );
+ tokio::time::sleep(std::time::Duration::from_millis(10)).await;
+ }
+ }
+
+ /// The AWS docs' Windows form: a quoted script path plus a quoted argument with spaces reaches
+ /// the script intact.
+ #[cfg(windows)]
+ #[tokio::test]
+ async fn a_quoted_windows_command_path_and_argument_run() {
+ let dir = tempfile::tempdir().expect("tempdir");
+ let script_dir = dir.path().join("dir with spaces");
+ std::fs::create_dir(&script_dir).expect("script dir");
+ let script = script_dir.join("credentials.cmd");
+ std::fs::write(
+ &script,
+ "@if not \"%~2\"==\"parameter with spaces\" exit /b 1\r\n@echo {\"Version\":1,\"AccessKeyId\":\"AKIDWIN\",\"SecretAccessKey\":\"s\"}\r\n",
+ )
+ .expect("write script");
+ let config = format!(
+ "[profile cp-stub-windows]\ncredential_process = \"{}\" parameterWithoutSpaces \"parameter with spaces\"\n",
+ script.display()
+ );
+
+ let credentials = credentials_from_process("cp-stub-windows", &config)
+ .await
+ .expect("quoted command runs");
+
+ assert_eq!(credentials.access_key_id, "AKIDWIN");
+ }
+
+ #[cfg(unix)]
+ #[tokio::test]
+ async fn a_failing_or_malformed_process_yields_no_credentials() {
+ let dir = tempfile::tempdir().expect("tempdir");
+ let failing = stub_script(dir.path(), "exit 1");
+ let config = format!(
+ "[profile cp-stub-failing]\ncredential_process = {}\n[profile cp-stub-malformed]\ncredential_process = echo '{{\"AccessKeyId\":\"x\"}}'\n",
+ failing.display()
+ );
+ assert_eq!(
+ credentials_from_process("cp-stub-failing", &config).await,
+ None
+ );
+ assert_eq!(
+ credentials_from_process("cp-stub-malformed", &config).await,
+ None
+ );
+ }
+}
diff --git a/crates/pa-ai/src/providers/bedrock/events.rs b/crates/pa-ai/src/providers/bedrock/events.rs
index 0062045245..ef9d0eb967 100644
--- a/crates/pa-ai/src/providers/bedrock/events.rs
+++ b/crates/pa-ai/src/providers/bedrock/events.rs
@@ -60,6 +60,20 @@ impl BedrockStreamState {
}
}
+/// On the wire the payload is the bare event body and `:event-type` names it; the handlers read the
+/// SDK's wrapped form `{"<eventType>": {...}}`, so wrap it as the SDK does (exceptions carry their
+/// own header and stay bare).
+fn wrap_event_payload(event_type: Option<&str>, is_exception: bool, parsed: Value) -> Value {
+ match event_type {
+ Some(event_type) if !is_exception => {
+ let mut wrapped = Map::new();
+ wrapped.insert(event_type.to_string(), parsed);
+ Value::Object(wrapped)
+ }
+ _ => parsed,
+ }
+}
+
pub(crate) fn handle_event(
message: &crate::providers::bedrock::eventstream::EventStreamMessage,
model: &Model,
@@ -70,6 +84,11 @@ pub(crate) fn handle_event(
) -> Result<(), ProviderError> {
let payload = String::from_utf8_lossy(&message.payload);
let parsed: Value = serde_json::from_str(payload.trim()).unwrap_or(Value::Null);
+ let parsed = wrap_event_payload(
+ message.event_type.as_deref(),
+ message.exception_type.is_some(),
+ parsed,
+ );
// Exception events carry :exception-type and a JSON payload with message. TS rethrows the
// modeled SDK exception, so `formatBedrockError` composes `{prefix}: {message}` and the
@@ -482,4 +501,123 @@ mod tests {
);
assert_eq!(output.usage.total_tokens, 15);
}
+
+ fn wire_model() -> Model {
+ Model {
+ id: "us.anthropic.claude-opus-4-6-v1".into(),
+ name: "Claude Opus 4.6".into(),
+ api: crate::providers::bedrock::API_BEDROCK_CONVERSE_STREAM.to_string(),
+ provider: "amazon-bedrock".into(),
+ base_url: "https://bedrock-runtime.us-east-1.amazonaws.com".into(),
+ reasoning: true,
+ thinking_level_map: None,
+ input: vec![ModelInput::Text],
+ cost: zero_model_cost(),
+ context_window: 200_000,
+ max_tokens: 8192,
+ max_tokens_explicit: false,
+ featured: None,
+ headers: None,
+ compat: None,
+ }
+ }
+
+ /// Feed decoded frames the way Bedrock sends them: the event name in `:event-type` and the bare
+ /// event body as the payload.
+ fn run_wire_events(frames: &[(&str, Value)]) -> AssistantMessage {
+ let model = wire_model();
+ let (writer, _stream) = crate::event_stream::create_assistant_message_event_stream();
+ let mut state = BedrockStreamState::new();
+ let mut output = crate::event_stream::initial_assistant_message(
+ crate::providers::bedrock::API_BEDROCK_CONVERSE_STREAM,
+ "amazon-bedrock",
+ &model.id,
+ );
+ for (event_type, body) in frames {
+ let message = crate::providers::bedrock::eventstream::EventStreamMessage {
+ message_type: Some("event".into()),
+ event_type: Some((*event_type).to_string()),
+ exception_type: None,
+ payload: body.to_string().into_bytes(),
+ };
+ handle_event(&message, &model, &mut output, &writer, &mut state, None)
+ .expect("event handled");
+ }
+ output
+ }
+
+ #[test]
+ fn bare_wire_payloads_produce_text_usage_and_stop_reason() {
+ let output = run_wire_events(&[
+ ("messageStart", json!({"role": "assistant", "p": "abc"})),
+ (
+ "contentBlockDelta",
+ json!({"contentBlockIndex": 0, "delta": {"text": "2 + 2 is "}, "p": "abcdefghijklmnopqrstuvwxyzABC"}),
+ ),
+ (
+ "contentBlockDelta",
+ json!({"contentBlockIndex": 0, "delta": {"text": "4."}, "p": "ab"}),
+ ),
+ (
+ "contentBlockStop",
+ json!({"contentBlockIndex": 0, "p": "ab"}),
+ ),
+ ("messageStop", json!({"stopReason": "end_turn", "p": "ab"})),
+ (
+ "metadata",
+ json!({"usage": {"inputTokens": 12, "outputTokens": 7, "totalTokens": 19}, "metrics": {"latencyMs": 300}, "p": "ab"}),
+ ),
+ ]);
+ assert_eq!(
+ output.content,
+ vec![AssistantContent::Text(TextContent {
+ text: "2 + 2 is 4.".into(),
+ text_signature: None,
+ rest: Map::default(),
+ })]
+ );
+ assert_eq!(
+ output.usage,
+ Usage {
+ input: 12,
+ output: 7,
+ total_tokens: 19,
+ ..Usage::default()
+ }
+ );
+ assert_eq!(output.stop_reason, StopReason::Stop);
+ }
+
+ #[test]
+ fn a_metadata_only_wire_stream_still_records_usage() {
+ let output = run_wire_events(&[(
+ "metadata",
+ json!({"usage": {"inputTokens": 3, "outputTokens": 0, "totalTokens": 3, "cacheReadInputTokens": 40}}),
+ )]);
+ assert_eq!(output.content, Vec::new());
+ assert_eq!(
+ output.usage,
+ Usage {
+ input: 3,
+ cache_read: 40,
+ total_tokens: 3,
+ ..Usage::default()
+ }
+ );
+ }
+
+ #[test]
+ fn event_payloads_are_wrapped_and_exception_and_headerless_payloads_pass_through() {
+ let bare = json!({"contentBlockIndex": 0, "delta": {"text": "hi"}});
+ let wrapped = json!({"contentBlockDelta": bare});
+ assert_eq!(
+ wrap_event_payload(Some("contentBlockDelta"), false, bare.clone()),
+ wrapped
+ );
+ assert_eq!(
+ wrap_event_payload(Some("throttlingException"), true, bare.clone()),
+ bare
+ );
+ assert_eq!(wrap_event_payload(None, false, bare.clone()), bare);
+ }
}
diff --git a/crates/pa-ai/src/providers/bedrock/mod.rs b/crates/pa-ai/src/providers/bedrock/mod.rs
index 6bb4eda778..9152ce5158 100644
--- a/crates/pa-ai/src/providers/bedrock/mod.rs
+++ b/crates/pa-ai/src/providers/bedrock/mod.rs
@@ -10,6 +10,7 @@ use serde_json::{json, Value};
mod auth;
mod convert;
+mod credential_process;
mod events;
mod eventstream;
mod goaway;
@@ -496,12 +497,12 @@ async fn run_stream(
}
let (endpoint, region) = resolve_endpoint(model, &options);
- let path_and_query = format!("/model/{}/converse-stream", encode_model_id(&model.id));
+ let path = format!("/model/{}/converse-stream", encode_model_id(&model.id));
let host = url::Url::parse(&endpoint)
.ok()
.and_then(|url| url.host_str().map(str::to_string))
.ok_or_else(|| ProviderError::Message(format!("Invalid Bedrock endpoint: {endpoint}")))?;
- let url = format!("{endpoint}{path_and_query}");
+ let url = format!("{endpoint}{path}");
let mut headers: Vec<(String, String)> = vec![
("content-type".into(), "application/json".into()),
@@ -538,13 +539,25 @@ async fn run_stream(
headers.push((name.clone(), value.clone()));
}
} else {
- let credentials = resolve_credentials(options.profile.as_deref()).ok_or_else(|| {
+ // Resolution may run the profile's credential_process, so it honours the request's signal;
+ // leaving the select drops the resolution, which kills a stalled process.
+ let cancelled = async {
+ match &options.base.signal {
+ Some(signal) => signal.cancelled().await,
+ None => std::future::pending().await,
+ }
+ };
+ let credentials = tokio::select! {
+ () = cancelled => return Err(ProviderError::Aborted),
+ credentials = resolve_credentials(options.profile.as_deref()) => credentials,
+ }
+ .ok_or_else(|| {
ProviderError::Message("No AWS credentials available for Bedrock".to_string())
})?;
let (amz_date, authorization, security_token) = sigv4_headers(
&crate::providers::bedrock::auth::SigV4Params {
method: "POST",
- path_and_query: &path_and_query,
+ path: &path,
host: &host,
region: &region,
service: "bedrock",