@@ -502,9 +502,8 @@ impl AnthropicClient {
502502 // Best-effort refinement using the Anthropic count_tokens endpoint.
503503 // On any failure (network, parse, auth), fall back to the local
504504 // byte-estimate result which already passed above.
505- let counted_input_tokens = match self . count_tokens ( request) . await {
506- Ok ( count) => count,
507- Err ( _) => return Ok ( ( ) ) ,
505+ let Ok ( counted_input_tokens) = self . count_tokens ( request) . await else {
506+ return Ok ( ( ) ) ;
508507 } ;
509508 let estimated_total_tokens = counted_input_tokens. saturating_add ( request. max_tokens ) ;
510509 if estimated_total_tokens > limit. context_window_tokens {
@@ -631,21 +630,7 @@ impl AuthSource {
631630 if let Some ( bearer_token) = read_env_non_empty ( "ANTHROPIC_AUTH_TOKEN" ) ? {
632631 return Ok ( Self :: BearerToken ( bearer_token) ) ;
633632 }
634- match load_saved_oauth_token ( ) {
635- Ok ( Some ( token_set) ) if oauth_token_is_expired ( & token_set) => {
636- if token_set. refresh_token . is_some ( ) {
637- Err ( ApiError :: Auth (
638- "saved OAuth token is expired; load runtime OAuth config to refresh it"
639- . to_string ( ) ,
640- ) )
641- } else {
642- Err ( ApiError :: ExpiredOAuthToken )
643- }
644- }
645- Ok ( Some ( token_set) ) => Ok ( Self :: BearerToken ( token_set. access_token ) ) ,
646- Ok ( None ) => Err ( anthropic_missing_credentials ( ) ) ,
647- Err ( error) => Err ( error) ,
648- }
633+ Err ( anthropic_missing_credentials ( ) )
649634 }
650635}
651636
@@ -665,14 +650,14 @@ pub fn resolve_saved_oauth_token(config: &OAuthConfig) -> Result<Option<OAuthTok
665650
666651pub fn has_auth_from_env_or_saved ( ) -> Result < bool , ApiError > {
667652 Ok ( read_env_non_empty ( "ANTHROPIC_API_KEY" ) ?. is_some ( )
668- || read_env_non_empty ( "ANTHROPIC_AUTH_TOKEN" ) ?. is_some ( )
669- || load_saved_oauth_token ( ) ?. is_some ( ) )
653+ || read_env_non_empty ( "ANTHROPIC_AUTH_TOKEN" ) ?. is_some ( ) )
670654}
671655
672656pub fn resolve_startup_auth_source < F > ( load_oauth_config : F ) -> Result < AuthSource , ApiError >
673657where
674658 F : FnOnce ( ) -> Result < Option < OAuthConfig > , ApiError > ,
675659{
660+ let _ = load_oauth_config;
676661 if let Some ( api_key) = read_env_non_empty ( "ANTHROPIC_API_KEY" ) ? {
677662 return match read_env_non_empty ( "ANTHROPIC_AUTH_TOKEN" ) ? {
678663 Some ( bearer_token) => Ok ( AuthSource :: ApiKeyAndBearer {
@@ -685,25 +670,7 @@ where
685670 if let Some ( bearer_token) = read_env_non_empty ( "ANTHROPIC_AUTH_TOKEN" ) ? {
686671 return Ok ( AuthSource :: BearerToken ( bearer_token) ) ;
687672 }
688-
689- let Some ( token_set) = load_saved_oauth_token ( ) ? else {
690- return Err ( anthropic_missing_credentials ( ) ) ;
691- } ;
692- if !oauth_token_is_expired ( & token_set) {
693- return Ok ( AuthSource :: BearerToken ( token_set. access_token ) ) ;
694- }
695- if token_set. refresh_token . is_none ( ) {
696- return Err ( ApiError :: ExpiredOAuthToken ) ;
697- }
698-
699- let Some ( config) = load_oauth_config ( ) ? else {
700- return Err ( ApiError :: Auth (
701- "saved OAuth token is expired; runtime OAuth config is missing" . to_string ( ) ,
702- ) ) ;
703- } ;
704- Ok ( AuthSource :: from ( resolve_saved_oauth_token_set (
705- & config, token_set,
706- ) ?) )
673+ Err ( anthropic_missing_credentials ( ) )
707674}
708675
709676fn resolve_saved_oauth_token_set (
@@ -1016,7 +983,7 @@ fn strip_unsupported_beta_body_fields(body: &mut Value) {
1016983 object. remove ( "presence_penalty" ) ;
1017984 // Anthropic uses "stop_sequences" not "stop". Convert if present.
1018985 if let Some ( stop_val) = object. remove ( "stop" ) {
1019- if stop_val. as_array ( ) . map_or ( false , |a| !a. is_empty ( ) ) {
986+ if stop_val. as_array ( ) . is_some_and ( |a| !a. is_empty ( ) ) {
1020987 object. insert ( "stop_sequences" . to_string ( ) , stop_val) ;
1021988 }
1022989 }
@@ -1180,7 +1147,7 @@ mod tests {
11801147 }
11811148
11821149 #[ test]
1183- fn auth_source_from_saved_oauth_when_env_absent ( ) {
1150+ fn auth_source_from_env_or_saved_ignores_saved_oauth_when_env_absent ( ) {
11841151 let _guard = env_lock ( ) ;
11851152 let config_home = temp_config_home ( ) ;
11861153 std:: env:: set_var ( "CLAW_CONFIG_HOME" , & config_home) ;
@@ -1194,8 +1161,8 @@ mod tests {
11941161 } )
11951162 . expect ( "save oauth credentials" ) ;
11961163
1197- let auth = AuthSource :: from_env_or_saved ( ) . expect ( "saved auth " ) ;
1198- assert_eq ! ( auth . bearer_token ( ) , Some ( "saved-access-token ") ) ;
1164+ let error = AuthSource :: from_env_or_saved ( ) . expect_err ( "saved oauth should be ignored " ) ;
1165+ assert ! ( error . to_string ( ) . contains ( "ANTHROPIC_API_KEY ") ) ;
11991166
12001167 clear_oauth_credentials ( ) . expect ( "clear credentials" ) ;
12011168 std:: env:: remove_var ( "CLAW_CONFIG_HOME" ) ;
@@ -1251,7 +1218,7 @@ mod tests {
12511218 }
12521219
12531220 #[ test]
1254- fn resolve_startup_auth_source_uses_saved_oauth_without_loading_config ( ) {
1221+ fn resolve_startup_auth_source_ignores_saved_oauth_without_loading_config ( ) {
12551222 let _guard = env_lock ( ) ;
12561223 let config_home = temp_config_home ( ) ;
12571224 std:: env:: set_var ( "CLAW_CONFIG_HOME" , & config_home) ;
@@ -1265,41 +1232,9 @@ mod tests {
12651232 } )
12661233 . expect ( "save oauth credentials" ) ;
12671234
1268- let auth = resolve_startup_auth_source ( || panic ! ( "config should not be loaded" ) )
1269- . expect ( "startup auth" ) ;
1270- assert_eq ! ( auth. bearer_token( ) , Some ( "saved-access-token" ) ) ;
1271-
1272- clear_oauth_credentials ( ) . expect ( "clear credentials" ) ;
1273- std:: env:: remove_var ( "CLAW_CONFIG_HOME" ) ;
1274- cleanup_temp_config_home ( & config_home) ;
1275- }
1276-
1277- #[ test]
1278- fn resolve_startup_auth_source_errors_when_refreshable_token_lacks_config ( ) {
1279- let _guard = env_lock ( ) ;
1280- let config_home = temp_config_home ( ) ;
1281- std:: env:: set_var ( "CLAW_CONFIG_HOME" , & config_home) ;
1282- std:: env:: remove_var ( "ANTHROPIC_AUTH_TOKEN" ) ;
1283- std:: env:: remove_var ( "ANTHROPIC_API_KEY" ) ;
1284- save_oauth_credentials ( & runtime:: OAuthTokenSet {
1285- access_token : "expired-access-token" . to_string ( ) ,
1286- refresh_token : Some ( "refresh-token" . to_string ( ) ) ,
1287- expires_at : Some ( 1 ) ,
1288- scopes : vec ! [ "scope:a" . to_string( ) ] ,
1289- } )
1290- . expect ( "save expired oauth credentials" ) ;
1291-
1292- let error =
1293- resolve_startup_auth_source ( || Ok ( None ) ) . expect_err ( "missing config should error" ) ;
1294- assert ! (
1295- matches!( error, crate :: error:: ApiError :: Auth ( message) if message. contains( "runtime OAuth config is missing" ) )
1296- ) ;
1297-
1298- let stored = runtime:: load_oauth_credentials ( )
1299- . expect ( "load stored credentials" )
1300- . expect ( "stored token set" ) ;
1301- assert_eq ! ( stored. access_token, "expired-access-token" ) ;
1302- assert_eq ! ( stored. refresh_token. as_deref( ) , Some ( "refresh-token" ) ) ;
1235+ let error = resolve_startup_auth_source ( || panic ! ( "config should not be loaded" ) )
1236+ . expect_err ( "saved oauth should be ignored" ) ;
1237+ assert ! ( error. to_string( ) . contains( "ANTHROPIC_API_KEY" ) ) ;
13031238
13041239 clear_oauth_credentials ( ) . expect ( "clear credentials" ) ;
13051240 std:: env:: remove_var ( "CLAW_CONFIG_HOME" ) ;
0 commit comments