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.

Don't let unusable prospects stop Harvey from prospecting

Ethanfcc3b80210ab
diff --git a/harvey/agents/writer.py b/harvey/agents/writer.py
index 233c470..18fda00 100644
--- a/harvey/agents/writer.py
+++ b/harvey/agents/writer.py
@@ -19,7 +19,7 @@ from harvey.config import HarveyConfig
from harvey.humanize import humanize, humanizer_enabled
from harvey.integrations.mail_provider import NATIVE_PROVIDERS
from harvey.models.campaign import Campaign, EmailStep
-from harvey.state import StateManager
+from harvey.state import WRITABLE_EMAIL_STATUSES, StateManager
logger = logging.getLogger("harvey.writer")
@@ -65,10 +65,9 @@ class Writer:
# Only write for prospects we can actually deliver to. Guessed and
# invalid addresses are skipped so we don't spend Claude calls (or
# sending reputation) on mail that will bounce.
- deliverable = {"verified", "risky"}
prospects_with_email = [
p for p in new_prospects
- if p.email and (p.email_status or "guess") in deliverable
+ if p.email and (p.email_status or "guess") in WRITABLE_EMAIL_STATUSES
]
if not prospects_with_email:
logger.info(
diff --git a/harvey/main.py b/harvey/main.py
index 0d16069..762e946 100644
--- a/harvey/main.py
+++ b/harvey/main.py
@@ -93,6 +93,9 @@ async def decide_next_action(
prospects = summary.get("prospects") or {}
new_prospects = prospects.get("new", 0) if isinstance(prospects, dict) else 0
+ # Only prospects the Writer can draft for count: one with a guessed email
+ # stays 'new' for good, and counting it starved Scout forever.
+ new_prospects = summary.get("writable_prospects", new_prospects) or 0
draft_campaigns = summary.get("draft_campaigns", 0) or 0
open_conversations = summary.get("open_conversations", 0) or 0
diff --git a/harvey/state.py b/harvey/state.py
index 8158211..128c782 100644
--- a/harvey/state.py
+++ b/harvey/state.py
@@ -29,6 +29,10 @@ DB_PATH = PROJECT_ROOT / "data" / "harvey.db"
# How long (seconds) a connection waits on a locked database before failing.
BUSY_TIMEOUT_SECONDS = 30.0
+# Email statuses the Writer drafts for. Shared with the heartbeat so it only
+# chooses write_campaign when there is something the Writer will act on.
+WRITABLE_EMAIL_STATUSES = ("verified", "risky")
+
def _new_id() -> str:
return uuid.uuid4().hex[:12]
@@ -1456,6 +1460,18 @@ class StateManager:
return True
return False
+ async def count_writable_prospects(self) -> int:
+ """New prospects the Writer can draft for (a deliverable email)."""
+ marks = ",".join("?" for _ in WRITABLE_EMAIL_STATUSES)
+ async with self._connect() as db:
+ async with db.execute(
+ f"""SELECT COUNT(*) FROM prospects
+ WHERE status = 'new' AND email != ''
+ AND email_status IN ({marks})""",
+ WRITABLE_EMAIL_STATUSES,
+ ) as cursor:
+ return (await cursor.fetchone())[0]
+
async def count_prospects_by_status(self) -> dict[str, int]:
async with self._connect() as db:
async with db.execute(
@@ -1880,6 +1896,10 @@ class StateManager:
return {
"prospects": prospect_counts,
+ # The 'new' prospects the Writer can act on. A guessed or invalid
+ # address stays 'new' forever, so deciding from the raw count
+ # would keep choosing write_campaign with nothing to write.
+ "writable_prospects": await self.count_writable_prospects(),
"draft_campaigns": draft_campaigns,
"active_campaigns": active_campaigns,
"open_conversations": open_conversations,
diff --git a/tests/test_prospecting_stall.py b/tests/test_prospecting_stall.py
new file mode 100644
index 0000000..f7d38f1
--- /dev/null
+++ b/tests/test_prospecting_stall.py
@@ -0,0 +1,77 @@
+"""Prospects the Writer can't use must not stop Harvey from prospecting.
+
+The Writer only drafts for verified or risky addresses; a guessed, invalid or
+missing email leaves the prospect 'new'. The heartbeat used to count every
+'new' prospect, so a single guessed address made it choose write_campaign
+every cycle, the Writer had nothing to do, and Scout never ran again.
+"""
+
+from types import SimpleNamespace
+
+import pytest
+import pytest_asyncio
+
+from harvey.main import decide_next_action
+from harvey.models.prospect import Prospect
+from harvey.state import StateManager
+
+CONFIG = SimpleNamespace(channels=SimpleNamespace(email=SimpleNamespace(
+ provider="gmail", require_approval=True)))
+
+
+@pytest_asyncio.fixture
+async def state(tmp_path):
+ state = StateManager(str(tmp_path / "stall.db"))
+ await state.init_db()
+ return state
+
+
+async def add(state, n, email, email_status):
+ await state.add_prospect(Prospect(
+ first_name=f"P{n}", last_name="Doe", title="Owner", company=f"Co{n}",
+ email=email, email_status=email_status,
+ ))
+
+
+async def test_summary_counts_only_prospects_the_writer_can_use(state):
+ await add(state, 1, "[email protected]", "verified")
+ await add(state, 2, "[email protected]", "risky")
+ await add(state, 3, "[email protected]", "guess")
+ await add(state, 4, "[email protected]", "invalid")
+ await add(state, 5, "", "")
+ summary = await state.get_state_summary()
+ assert summary["prospects"]["new"] == 5
+ assert summary["writable_prospects"] == 2
+
+
+async def test_guessed_emails_alone_lead_back_to_prospecting(state):
+ for n in range(3):
+ await add(state, n, f"p{n}@co{n}.test", "guess")
+ summary = await state.get_state_summary()
+ assert await decide_next_action(None, state, CONFIG, summary=summary) == "prospect"
+
+
+async def test_many_guessed_emails_still_lead_back_to_prospecting(state):
+ # 20+ unusable prospects must not tip Harvey into idle either.
+ for n in range(25):
+ await add(state, n, f"p{n}@co{n}.test", "guess")
+ summary = await state.get_state_summary()
+ assert await decide_next_action(None, state, CONFIG, summary=summary) == "prospect"
+
+
+async def test_a_deliverable_prospect_is_still_written_for(state):
+ await add(state, 1, "[email protected]", "guess")
+ await add(state, 2, "[email protected]", "verified")
+ summary = await state.get_state_summary()
+ assert await decide_next_action(None, state, CONFIG, summary=summary) == "write_campaign"
+
+
+async def test_a_summary_without_the_new_count_keeps_the_old_rule():
+ # Callers that build their own summary (older tests, scripts) still work.
+ summary = {"prospects": {"new": 3}, "draft_campaigns": 0, "open_conversations": 0}
+ assert await decide_next_action(None, None, CONFIG, summary=summary) == "write_campaign"