Skip to content

Autonomous AI sales agent powered by Claude Code or Codex. Finds prospects, writes cold emails, sends campaigns, handles replies, books meetings. No API keys needed — runs on your Claude or Codex subscriptions.

Treat a Claude CLI error payload as no reply, not model text

Ethanb98e6b7b71b2
diff --git a/harvey/brain.py b/harvey/brain.py
index 75aebf6..57a4e85 100644
--- a/harvey/brain.py
+++ b/harvey/brain.py
@@ -373,6 +373,15 @@ class Brain:
except Exception as e:
logger.warning(f"Failed to record usage event: {e}")
+ if payload and payload.get("is_error"):
+ # The CLI reported an API error (overloaded, rate limited)
+ # inside a successful exit. That text is not the model's
+ # answer, so no caller may use it as one: retry like any
+ # other transient failure.
+ logger.error(f"Claude reported an error: {response[:300]}")
+ last_error = response
+ continue
+
logger.debug(f"Brain response: {response[:200]}...")
return response
diff --git a/tests/test_writer_retry.py b/tests/test_writer_retry.py
index e9ac5ed..7bcaf5b 100644
--- a/tests/test_writer_retry.py
+++ b/tests/test_writer_retry.py
@@ -156,3 +156,44 @@ async def test_given_up_prospects_can_be_retried(state, capsys):
assert await state.retry_write_failed() == 1
assert await status(state, pid) == "new"
assert await state.retry_write_failed() == 0
+
+
+class _ClaudeProcess:
+ """A Claude CLI that exits 0 with the given JSON result payload."""
+
+ returncode = 0
+
+ def __init__(self, payload):
+ self.payload = payload
+
+ async def communicate(self, data=None):
+ import json
+ return json.dumps(self.payload).encode(), b""
+
+
+async def test_a_cli_reported_api_error_is_no_reply_not_model_text(state, monkeypatch):
+ # The CLI can report an API error (overloaded, rate limited) inside a
+ # successful exit. That text is not the model's answer: no caller should
+ # treat it as a draft, and the Writer must not count it as a bad one.
+ import harvey.brain as brain_mod
+ from harvey.brain import Brain
+
+ payloads = []
+
+ async def fake_exec(*cmd, **kwargs):
+ return _ClaudeProcess(payloads.pop(0))
+
+ async def no_sleep(*a, **kw):
+ return None
+
+ monkeypatch.setattr(brain_mod.asyncio, "create_subprocess_exec", fake_exec)
+ monkeypatch.setattr(brain_mod.asyncio, "sleep", no_sleep)
+ brain = Brain(state)
+
+ payloads[:] = [{"type": "result", "is_error": True, "result": "API Error: 529 Overloaded"}] * 5
+ assert await brain.think("hi", max_retries=1) == ""
+ assert await brain.think_json("hi") is None and brain.last_reply_empty is True
+
+ payloads[:] = [{"type": "result", "is_error": False, "result": '{"ok": true}'}]
+ assert await brain.think_json("hi") == {"ok": True} and brain.last_reply_empty is False