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.

Claim only due mail, compare claim times as dates, label mail sending now

Ethan20001eeedee0
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0bf60dd..bc14812 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,7 +14,7 @@ minor versions can still change behaviour.
through its queue now stops it, instead of the Sender sending its earlier
copy and recording it as sent. Two Harvey processes running at once can no
longer send the same email twice. An email cut off mid-send (Harvey stopped,
- or the provider raised an error) is marked failed and never retried, since
+ or the mail library crashed mid-call) is marked failed and never retried, since
it may have gone out; it shows under "Didn't send" with a note to check your
Sent folder, and the dashboard won't offer to email that person by hand.
Rejecting an email that is already sending or sent is refused instead of
diff --git a/harvey/state.py b/harvey/state.py
index aad4439..44448fd 100644
--- a/harvey/state.py
+++ b/harvey/state.py
@@ -941,15 +941,17 @@ class StateManager:
async def claim_outbox_item(self, item_id: str) -> bool:
"""Take an approved item for sending: approved → sending, atomically.
- False when it is no longer approved (cancelled by a reply, bounce or
- hand send, rejected, or taken by another sender), so it must not go
- out. Cancels never touch a claimed item: it is already on its way.
+ False when it is no longer approved and due (cancelled by a reply,
+ bounce or hand send, rejected, rescheduled, or taken by another
+ sender), so it must not go out now. Cancels never touch a claimed item: it is already on its way.
"""
+ now = _utcnow().isoformat()
async with self._connect() as db:
cursor = await db.execute(
"UPDATE outbox SET status = 'sending', updated_at = ? "
- "WHERE id = ? AND status = 'approved'",
- (_utcnow().isoformat(), item_id),
+ "WHERE id = ? AND status = 'approved' "
+ "AND (send_at IS NULL OR send_at <= ?)",
+ (now, item_id, now),
)
await db.commit()
return cursor.rowcount == 1
@@ -966,7 +968,7 @@ class StateManager:
async with self._connect() as db:
cursor = await db.execute(
"UPDATE outbox SET status = 'failed', error = ?, updated_at = ? "
- "WHERE status = 'sending' AND updated_at < ?",
+ "WHERE status = 'sending' AND datetime(updated_at) < datetime(?)",
(STALE_SENDING_ERROR, _utcnow().isoformat(), cutoff),
)
await db.commit()
diff --git a/harvey/web/app.js b/harvey/web/app.js
index 7581e98..b97d9c2 100644
--- a/harvey/web/app.js
+++ b/harvey/web/app.js
@@ -82,6 +82,7 @@ const STATUS = {
// outbox
pending_review: ['Waiting on you', 'waiting'],
approved: ['Approved', 'good'],
+ sending: ['Sending now', 'active'],
scheduled: ['Scheduled', 'active'],
sent: ['Sent', 'good'],
failed: ['Failed', 'bad'],
@@ -1297,7 +1298,7 @@ async function loadOutbox() {
list.innerHTML =
table('Approved &amp; scheduled', data.approved, [
['To', r => r.to_email], ['Step', r => r.step], ['Subject', r => r.subject],
- ['Sends', r => formatDate(r.send_at), true],
+ ['Sends', r => r.status === 'sending' ? badge(r.status) : escHtml(formatDate(r.send_at)), true, true],
]) +
table('Recently sent', data.sent, [
['To', r => r.to_email], ['Step', r => r.step], ['Subject', r => r.subject],
diff --git a/tests/test_outbox_claim.py b/tests/test_outbox_claim.py
index c97064a..cb98c1f 100644
--- a/tests/test_outbox_claim.py
+++ b/tests/test_outbox_claim.py
@@ -247,3 +247,27 @@ async def test_reject_never_touches_mail_already_sending_or_sent(state):
assert (await state.get_outbox_item(sending))["status"] == "sending"
assert (await state.get_outbox_item(sent))["status"] == "sent"
assert (await state.get_outbox_item(pending))["status"] == "rejected"
+
+
+async def test_claim_refuses_an_email_not_due_yet(state):
+ # Rescheduled since the queue was read: it waits for its new time.
+ pid = await seed_prospect(state, status="contacted")
+ item_id = await _approved(state, pid)
+ later = (datetime.now(timezone.utc).replace(tzinfo=None) + timedelta(days=1)).isoformat()
+ await state.update_outbox_item(item_id, send_at=later)
+ assert await state.claim_outbox_item(item_id) is False
+ assert (await state.get_outbox_item(item_id))["status"] == "approved"
+
+
+async def test_a_fresh_claim_is_never_swept_whatever_the_timestamp_format(state):
+ pid = await seed_prospect(state, status="contacted")
+ item_id = await _approved(state, pid)
+ await state.claim_outbox_item(item_id)
+ async with state._connect() as db: # SQLite's own format: space, not 'T'
+ await db.execute(
+ "UPDATE outbox SET updated_at = CURRENT_TIMESTAMP WHERE id = ?", (item_id,))
+ await db.commit()
+ assert await state.fail_stale_sending_outbox() == 0
+ assert (await state.get_outbox_item(item_id))["status"] == "sending"