Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(agent-core): classify JSON-wrapped throttling by message prefix (#3064)

Call Whitec52545d49a13
diff --git a/core/agent_core/ai/errors.py b/core/agent_core/ai/errors.py
index 50edb2850..cbc25db7d 100644
--- a/core/agent_core/ai/errors.py
+++ b/core/agent_core/ai/errors.py
@@ -168,9 +168,11 @@ def classify_error(
status=status,
partial=partial,
)
+ # Anchored overflow exclusions belong to the extracted message, not its JSON envelope. Keep the body afterward
+ # for other evidence, such as ThrottlingException type markers, without accepting incidental later prefixes.
kind = _classify_kind(
status=status,
- message=f"{body}\n{message}",
+ message=f"{message.strip()}\n{body}",
exc=exc,
code=effective_code,
)
diff --git a/tests/agent_core/agent/test_compaction.py b/tests/agent_core/agent/test_compaction.py
index 6d9817a6c..b14457336 100644
--- a/tests/agent_core/agent/test_compaction.py
+++ b/tests/agent_core/agent/test_compaction.py
@@ -25,6 +25,7 @@ from core.agent_core.agent.events import (
AssistantTextDelta,
CompactionFailed,
CompactionFinished,
+ CompactionStarted,
ContextExhausted,
MessageCommitted,
RunEnded,
@@ -951,6 +952,40 @@ async def test_two_ineffective_checkpoints_stop_compaction_for_the_run():
assert events[-1].reason == "completed"
+ ("prefix", "ending", "attempts"),
+ [("Throttling error", "error", 3), ("Service unavailable", "completed", 4)],
+)
+async def test_json_non_overflow_errors_use_ordinary_handling_without_context_relief(prefix, ending, attempts):
+ # Enough context for the overflow ladder to checkpoint, but below the proactive threshold. Feed the real
+ # classifier's outcome into the loop, not a hand-written ProviderError that assumes classification is correct.
+ refusal = classify_error(
+ status=400,
+ body=json.dumps(anthropic_error(f"{prefix}: Too many tokens, please wait before trying again.")),
+ protocol="anthropic",
+ )
+ model = Model(
+ [call("read", "r0", path="f"), call("read", "r1", path="f"), [refusal], [Done(assistant("done"))]],
+ [[Done(assistant(CHECKPOINT))]],
+ )
+ agent = make_agent(model, tools=[reader(tokens(4_000))], retry=RetryPolicy(initial_delay_s=0))
+
+ events = await run(agent)
+
+ assert not model.checkpoint_requests
+ assert not any(
+ isinstance(event, (CompactionStarted, CompactionFinished, CompactionFailed, ContextExhausted)) for event in events
+ )
+ rows = await agent.store.load("session")
+ assert not any(row.kind in {"compaction", "context_edit"} for row in rows)
+ assert len(model.conversation_requests) == attempts
+ assert events[-1].reason == ending
+ if ending == "completed":
+ assert model.conversation_requests[-1] == model.conversation_requests[-2]
+ else:
+ assert [event.kind for event in events if isinstance(event, AgentError)] == ["invalid_request"]
+
+
OVERFLOW = ProviderError("overflow", "prompt is too long: 40000 tokens > 32000 maximum", False, status=400)
diff --git a/tests/agent_core/ai/test_transform_sse_errors.py b/tests/agent_core/ai/test_transform_sse_errors.py
index 91d7482f4..1d46f6e86 100644
--- a/tests/agent_core/ai/test_transform_sse_errors.py
+++ b/tests/agent_core/ai/test_transform_sse_errors.py
@@ -357,22 +357,85 @@ def test_overflow_classifies_cerebras_bodyless_status() -> None:
assert classify_error(status=400, body="400 status code (no body)").kind == "overflow"
[email protected]("status", [400, 429])
[email protected]("representation", ["plain", "json", "json-string"])
@pytest.mark.parametrize(
- "body",
+ ("message", "kind", "retryable"),
[
- 'ThrottlingException: too many tokens',
- '{"error":{"type":"ThrottlingException","message":"too many tokens"}}',
- '{"error":{"message":"ThrottlingException: too many tokens"}}',
+ ("Throttling error: Too many tokens, please wait before trying again.", "invalid_request", False),
+ ("Service unavailable: Too many tokens, please wait before trying again.", "server", True),
+ (" \n\ttHrOtTlInG eRrOr: too many tokens\r\n", "invalid_request", False),
+ (" \n\tsErViCe UnAvAiLaBlE: too many tokens\r\n", "server", True),
+ ("ThrottlingException: too many tokens", "invalid_request", False),
+ ("com.amazonaws.bedrock.ThrottlingException: too many tokens", "invalid_request", False),
],
)
-def test_http_400_throttling_is_excluded_from_overflow_without_becoming_rate_limit(body: str) -> None:
- error = classify_error(status=400, body=body)
- assert error.kind == "invalid_request"
- assert error.kind != "overflow"
+def test_non_overflow_prefixes_survive_message_extraction(
+ status: int, representation: str, message: str, kind: str, retryable: bool,
+) -> None:
+ body = {
+ "plain": message,
+ "json": json.dumps({"type": "error", "error": {"type": "invalid_request_error", "message": message}}),
+ "json-string": json.dumps(message),
+ }[representation]
+ if status == 429:
+ kind, retryable = "rate_limit", True
+
+ error = classify_error(status=status, body=body)
+
+ assert error.kind == kind
+ assert error.retryable is retryable
+ assert error.continuable is retryable
+ assert error.message == (message.strip() if representation == "plain" else message)
+
[email protected]("as_json", [False, True], ids=["plain", "json"])
+ "message",
+ [
+ "too many tokens",
+ "'Throttling error: too many tokens'",
+ "Request failed: Service unavailable: too many tokens",
+ "too many tokens\nThrottling error: previous attempt",
+ "too many tokens\nService unavailable: previous attempt",
+ "Throttling errorish: too many tokens",
+ "Service unavailableish: too many tokens",
+ "Throttling error without a colon: too many tokens",
+ "Service unavailable without a colon: too many tokens",
+ ],
+)
+def test_non_overflow_prefix_lookalikes_do_not_hide_true_overflow(as_json: bool, message: str) -> None:
+ body = json.dumps({"error": {"message": message}}) if as_json else message
+ assert classify_error(status=400, body=body).kind == "overflow"
-def test_generic_http_400_too_many_tokens_remains_overflow() -> None:
- assert classify_error(status=400, body="too many tokens").kind == "overflow"
+
[email protected]("prefix", ["Throttling error", "Service unavailable"])
+def test_non_message_json_fields_cannot_supply_a_non_overflow_prefix(prefix: str) -> None:
+ body = json.dumps({"diagnostic": f"{prefix}: previous attempt", "error": {"message": "too many tokens"}}, indent=2)
+ assert classify_error(status=400, body=body).kind == "overflow"
+
+
+ ("status", "code", "message", "kind"),
+ [
+ (400, "ThrottlingException", "too many tokens", "invalid_request"),
+ (400, "context_length_exceeded", "Throttling error: too many tokens", "overflow"),
+ (400, "request_too_large", "Service unavailable: too many tokens", "overflow"),
+ (400, "rate_limit_error", "Throttling error: too many tokens", "rate_limit"),
+ (400, "authentication_error", "Service unavailable: too many tokens", "auth"),
+ (413, "rate_limit_error", "Throttling error: too many tokens", "overflow"),
+ (401, "context_length_exceeded", "Throttling error: too many tokens", "auth"),
+ (503, "context_length_exceeded", "Throttling error: too many tokens", "server"),
+ (429, "context_length_exceeded", "Throttling error: too many tokens", "rate_limit"),
+ (429, "invalid_request_error", "Service unavailable: too many tokens", "rate_limit"),
+ (429, "ThrottlingException", "ThrottlingException: too many tokens", "rate_limit"),
+ ],
+)
+def test_non_overflow_prefixes_preserve_status_and_machine_code_priority(
+ status: int, code: str, message: str, kind: str,
+) -> None:
+ body = json.dumps({"error": {"type": code, "message": message}})
+ assert classify_error(status=status, body=body).kind == kind
@pytest.mark.parametrize("status", [401, 403])