| 1 | """Provider-side failures (auth, rate limit, overload) are infrastructure, not task failures."""
|
| 2 |
|
| 3 | from __future__ import annotations
|
| 4 |
|
| 5 | import json
|
| 6 | import sys
|
| 7 | import unittest
|
| 8 | from pathlib import Path
|
| 9 |
|
| 10 | sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))
|
| 11 |
|
| 12 | import agent_workflow # noqa: E402
|
| 13 |
|
| 14 | CLAUDE = "claude_print_stream_json"
|
| 15 |
|
| 16 |
|
| 17 | def stream(*events: dict) -> str:
|
| 18 | return "\n".join(json.dumps(event) for event in events) + "\n"
|
| 19 |
|
| 20 |
|
| 21 | class ProviderApiErrorTest(unittest.TestCase):
|
| 22 | def test_auth_failure_is_a_provider_error(self) -> None:
|
| 23 | # Observed from claude 2.1.321 with an invalid key (cloud runner, 2026-11-09).
|
| 24 | text = stream({"type": "system", "subtype": "init"},
|
| 25 | {"type": "result", "subtype": "success", "is_error": True, "api_error_status": 401,
|
| 26 | "terminal_reason": "api_error", "result": "Invalid API key"})
|
| 27 | self.assertEqual(agent_workflow.provider_api_error(CLAUDE, text), "provider_api_error:401")
|
| 28 |
|
| 29 | def test_overload_without_status_is_still_a_provider_error(self) -> None:
|
| 30 | text = stream({"type": "result", "is_error": True, "terminal_reason": "api_error"})
|
| 31 | self.assertEqual(agent_workflow.provider_api_error(CLAUDE, text), "provider_api_error:unknown")
|
| 32 |
|
| 33 | def test_normal_completion_is_not(self) -> None:
|
| 34 | text = stream({"type": "result", "subtype": "success", "is_error": False, "api_error_status": None,
|
| 35 | "terminal_reason": "completed"})
|
| 36 | self.assertIsNone(agent_workflow.provider_api_error(CLAUDE, text))
|
| 37 |
|
| 38 | def test_agent_task_error_is_not_a_provider_error(self) -> None:
|
| 39 | # is_error without a provider status: the agent itself gave up; that is a task outcome.
|
| 40 | text = stream({"type": "result", "subtype": "error_max_turns", "is_error": True})
|
| 41 | self.assertIsNone(agent_workflow.provider_api_error(CLAUDE, text))
|
| 42 |
|
| 43 | def test_other_adapters_are_unmeasured(self) -> None:
|
| 44 | text = stream({"type": "result", "api_error_status": 429})
|
| 45 | self.assertIsNone(agent_workflow.provider_api_error("codex_exec_json", text))
|
| 46 |
|
| 47 | def test_truncated_or_garbage_stream(self) -> None:
|
| 48 | self.assertIsNone(agent_workflow.provider_api_error(CLAUDE, '{"type": "result", "api_err'))
|
| 49 | self.assertIsNone(agent_workflow.provider_api_error(CLAUDE, ""))
|
| 50 |
|
| 51 |
|
| 52 | if __name__ == "__main__":
|
| 53 | unittest.main()
|