Skip to content

Commit 8af45d4

Browse files
Ambient Code Botclaude
andcommitted
fix(runner): preserve original HTTP status code in credential retry error message
The _retry_with_fresh_bot_token helper was hardcoding HTTP 401 in the PermissionError message regardless of the actual response code. This broke test_raises_permission_error_on_403_without_caller_token which expects "authentication failed with HTTP 403". Now the original status code is passed through and the retry error message reflects the actual HTTP code from both the initial and retry attempts. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent bf6be85 commit 8af45d4

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

  • components/runners/ambient-runner/ambient_runner/platform

components/runners/ambient-runner/ambient_runner/platform/auth.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def _do_req():
176176
return resp.read().decode("utf-8", errors="replace")
177177
except _urllib_request.HTTPError as fallback_err:
178178
if fallback_err.code in (401, 403):
179-
return _retry_with_fresh_bot_token()
179+
return _retry_with_fresh_bot_token(fallback_err.code)
180180
logger.warning(
181181
f"{credential_type} BOT_TOKEN fallback also failed: {fallback_err}"
182182
)
@@ -194,23 +194,23 @@ def _do_req():
194194
) from fallback_err
195195
if e.code in (401, 403):
196196
# BOT_TOKEN may have expired — refresh from CP endpoint and retry once.
197-
return _retry_with_fresh_bot_token()
197+
return _retry_with_fresh_bot_token(e.code)
198198
logger.warning(f"{credential_type} credential fetch failed: {e}")
199199
return ""
200200
except Exception as e:
201201
logger.warning(f"{credential_type} credential fetch failed: {e}")
202202
return ""
203203

204-
def _retry_with_fresh_bot_token():
204+
def _retry_with_fresh_bot_token(original_code: int):
205205
logger.info(
206-
f"{credential_type} got 401 with cached BOT_TOKEN — refreshing from CP endpoint and retrying"
206+
f"{credential_type} got {original_code} with cached BOT_TOKEN — refreshing from CP endpoint and retrying"
207207
)
208208
try:
209209
fresh_bot = refresh_bot_token()
210210
except Exception as refresh_err:
211211
logger.warning(f"{credential_type} CP token refresh failed: {refresh_err}")
212212
raise PermissionError(
213-
f"{credential_type} authentication failed with HTTP 401"
213+
f"{credential_type} authentication failed with HTTP {original_code}"
214214
) from refresh_err
215215
retry_req = _urllib_request.Request(url, method="GET")
216216
if fresh_bot:
@@ -221,10 +221,15 @@ def _retry_with_fresh_bot_token():
221221
with _urllib_request.urlopen(retry_req, timeout=10) as resp:
222222
logger.info(f"{credential_type} retry with fresh BOT_TOKEN succeeded")
223223
return resp.read().decode("utf-8", errors="replace")
224+
except _urllib_request.HTTPError as retry_err:
225+
logger.warning(f"{credential_type} retry with fresh BOT_TOKEN failed: {retry_err}")
226+
raise PermissionError(
227+
f"{credential_type} authentication failed with HTTP {retry_err.code}"
228+
) from retry_err
224229
except Exception as retry_err:
225230
logger.warning(f"{credential_type} retry with fresh BOT_TOKEN failed: {retry_err}")
226231
raise PermissionError(
227-
f"{credential_type} authentication failed with HTTP 401"
232+
f"{credential_type} authentication failed with HTTP {original_code}"
228233
) from retry_err
229234

230235
resp_text = await loop.run_in_executor(None, _do_req)

0 commit comments

Comments
 (0)