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.

Stop the Writer retrying a batch it can't write for, every cycle

Ethaneb19987420da
diff --git a/harvey/agents/writer.py b/harvey/agents/writer.py
index 18fda00..9597b18 100644
--- a/harvey/agents/writer.py
+++ b/harvey/agents/writer.py
@@ -23,6 +23,12 @@ from harvey.state import WRITABLE_EMAIL_STATUSES, StateManager
logger = logging.getLogger("harvey.writer")
+# How many times the model may fail to write a usable sequence for a
+# prospect before Harvey stops trying (status 'write_failed'). A provider
+# outage never counts; only a reply that came back unusable does.
+MAX_WRITE_ATTEMPTS = 3
+WRITE_ATTEMPTS_KEY = "write_attempts:"
+
# Merge variables that name a person; a company inbox has none of them.
PERSON_VAR = re.compile(r"\{\{\s*(first_?name|last_?name|title)\s*\}\}", re.IGNORECASE)
@@ -90,7 +96,14 @@ class Writer:
# Generate the email sequence
sequence = await self._write_sequence(prospects)
if not sequence:
+ paused = getattr(self.brain, "paused_reason", "")
+ if paused:
+ # The provider is down, not the draft bad: no attempt is
+ # counted, and the other batches would fail the same way.
+ logger.warning(f"Writer: {paused}; leaving '{batch_name}' for later.")
+ return
logger.warning(f"Writer: Failed to generate sequence for {batch_name}")
+ await self._count_failed_attempt(batch_name, prospects)
continue
# Create the campaign
@@ -107,6 +120,7 @@ class Writer:
# Mark prospects so they aren't picked up again
for p in prospects:
await self.state.update_prospect_status(p.id, "queued")
+ await self.state.delete_setting(WRITE_ATTEMPTS_KEY + p.id)
# Native providers: replace the shared template for email 1 with
# a per-prospect draft grounded in that company's actual facts.
@@ -128,6 +142,38 @@ class Writer:
f"{len(sequence)} emails for {len(prospects)} prospects."
)
+ async def _count_failed_attempt(self, batch_name: str, prospects: list):
+ """Count one unusable sequence against each prospect in the batch.
+
+ A prospect left 'new' makes the heartbeat choose write_campaign every
+ cycle, so after MAX_WRITE_ATTEMPTS it moves to 'write_failed': visible
+ in status and the dashboard, and out of the way of prospecting.
+ """
+ given_up = []
+ for p in prospects:
+ key = WRITE_ATTEMPTS_KEY + p.id
+ try:
+ attempts = int(await self.state.get_setting(key) or 0) + 1
+ except ValueError:
+ attempts = 1
+ if attempts >= MAX_WRITE_ATTEMPTS:
+ await self.state.update_prospect_status(p.id, "write_failed")
+ await self.state.delete_setting(key)
+ given_up.append(p.email)
+ else:
+ await self.state.set_setting(key, str(attempts))
+ if given_up:
+ await self.state.log_action(
+ action_type="write_failed",
+ agent="writer",
+ details={"campaign_name": batch_name, "prospects": given_up,
+ "attempts": MAX_WRITE_ATTEMPTS},
+ )
+ logger.warning(
+ f"Writer: Gave up on {len(given_up)} prospect(s) in '{batch_name}' after "
+ f"{MAX_WRITE_ATTEMPTS} unusable drafts; marked write_failed."
+ )
+
async def _personalize_first_emails(self, campaign_id: str, prospects: list):
"""Draft a grounded, per-prospect email 1 and stage it in the outbox.
diff --git a/harvey/state.py b/harvey/state.py
index d11b7b2..addc8e8 100644
--- a/harvey/state.py
+++ b/harvey/state.py
@@ -1410,6 +1410,11 @@ class StateManager:
row = await cursor.fetchone()
return row[0] if row else default
+ async def delete_setting(self, key: str):
+ async with self._connect() as db:
+ await db.execute("DELETE FROM settings WHERE key = ?", (key,))
+ await db.commit()
+
async def set_setting(self, key: str, value: str):
async with self._connect() as db:
await db.execute(
diff --git a/harvey/web/app.js b/harvey/web/app.js
index 591914a..817dee2 100644
--- a/harvey/web/app.js
+++ b/harvey/web/app.js
@@ -67,6 +67,7 @@ const STATUS = {
meeting: ['Meeting booked', 'note'],
not_interested: ['Not interested', 'bad'],
bounced: ['Bounced', 'bad'],
+ write_failed: ["Couldn't write", 'waiting'],
// campaigns
draft: ['Draft', 'waiting'],
active: ['Active', 'good'],
diff --git a/tests/test_writer_retry.py b/tests/test_writer_retry.py
new file mode 100644
index 0000000..9575924
--- /dev/null
+++ b/tests/test_writer_retry.py
@@ -0,0 +1,115 @@
+"""A batch the model can't write for must not loop forever.
+
+When _write_sequence comes back empty (unparseable output, or an inbox
+sequence that used a person's name), the batch's prospects stayed 'new', so
+every heartbeat chose write_campaign again, spent a Claude call, and Scout
+never ran. Now each prospect gets a few attempts; after that it is moved to
+'write_failed', which is visible and no longer blocks the pipeline. A
+provider outage is not the model's fault and never counts as an attempt.
+"""
+
+from types import SimpleNamespace
+
+import pytest
+import pytest_asyncio
+
+from harvey.agents.writer import MAX_WRITE_ATTEMPTS, Writer
+from harvey.main import decide_next_action
+from harvey.models.prospect import Prospect
+from harvey.state import StateManager
+
+GOOD = [{"step": 1, "subject": "quick one", "body": "Hi {{first_name}}, who answers at {{company}}?",
+ "delay_days": 0}]
+
+
+class FakeBrain:
+ def __init__(self, reply=None, paused=""):
+ self.reply = reply
+ self.paused_reason = paused
+ self.calls = 0
+
+ def load_skills_for_agent(self, name):
+ return ""
+
+ def load_prompt(self, name, **kw):
+ return "prompt"
+
+ async def think_json(self, prompt, **kw):
+ self.calls += 1
+ return None if self.paused_reason else self.reply
+
+
+CONFIG = SimpleNamespace(
+ channels=SimpleNamespace(email=SimpleNamespace(provider="instantly", require_approval=False)),
+ product=SimpleNamespace(name="P", description="D", key_benefits=[], pricing=""),
+ persona=SimpleNamespace(name="S", company="C", role="R", tone="t"),
+)
+
+
+@pytest_asyncio.fixture
+async def state(tmp_path):
+ state = StateManager(str(tmp_path / "w.db"))
+ await state.init_db()
+ return state
+
+
+async def add(state, n):
+ return await state.add_prospect(Prospect(
+ first_name=f"P{n}", last_name="Doe", title="Owner", company=f"Co{n}",
+ industry="Roofing", email=f"p{n}@co{n}.test", email_status="verified",
+ ))
+
+
+async def status(state, pid):
+ return (await state.get_prospect(pid)).status
+
+
+async def test_a_batch_that_keeps_failing_stops_after_a_few_attempts(state):
+ pid = await add(state, 1)
+ brain = FakeBrain(reply="not a sequence")
+ writer = Writer(brain, state, CONFIG, None)
+ for _ in range(MAX_WRITE_ATTEMPTS - 1):
+ await writer.run()
+ assert await status(state, pid) == "new"
+ await writer.run()
+ assert await status(state, pid) == "write_failed"
+ # The heartbeat moves on to prospecting instead of writing again.
+ summary = await state.get_state_summary()
+ assert await decide_next_action(None, state, CONFIG, summary=summary) == "prospect"
+ calls = brain.calls
+ await writer.run()
+ assert brain.calls == calls # no more model calls for it
+
+
+async def test_a_provider_outage_never_counts_as_an_attempt(state):
+ pid = await add(state, 1)
+ writer = Writer(FakeBrain(paused="Claude usage limit reached"), state, CONFIG, None)
+ for _ in range(MAX_WRITE_ATTEMPTS + 2):
+ await writer.run()
+ assert await status(state, pid) == "new"
+
+
+async def test_success_after_a_failure_writes_normally(state):
+ pid = await add(state, 1)
+ brain = FakeBrain(reply="not a sequence")
+ writer = Writer(brain, state, CONFIG, None)
+ await writer.run()
+ brain.reply = GOOD
+ await writer.run()
+ assert await status(state, pid) == "queued"
+
+
+async def test_attempts_are_counted_per_prospect(state):
+ # Someone who joins a failing batch late gets their own full set of tries.
+ early = await add(state, 1)
+ writer = Writer(FakeBrain(reply="not a sequence"), state, CONFIG, None)
+ for _ in range(MAX_WRITE_ATTEMPTS - 1):
+ await writer.run()
+ late = await add(state, 2)
+ await writer.run()
+ assert await status(state, early) == "write_failed"
+ assert await status(state, late) == "new"