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.

Mark the hand send before checking Harvey's queue; one Gmail draft per click

Ethancf79367921f2
diff --git a/harvey/dashboard.py b/harvey/dashboard.py
index 326b798..7b348f5 100644
--- a/harvey/dashboard.py
+++ b/harvey/dashboard.py
@@ -1044,7 +1044,7 @@ async def mark_prospect_sent(prospect_id: str, request: Request):
if sent:
if marked:
- return {"success": True, "sent": True}
+ return {"success": True, "sent": True, "already": True}
if current not in HAND_SENDABLE_STATUSES:
# A reply, opt-out or close is never touched; neither is
# someone Harvey already emailed.
@@ -1052,10 +1052,14 @@ async def mark_prospect_sent(prospect_id: str, request: Request):
# Allowed even when Harvey can send: recording "I already emailed
# them" can only ever stop Harvey sending. Not once Harvey has
# approved or sent mail to them, though: that may be going out now.
+ # The marker goes down FIRST: a drain that checks after this sees
+ # it and cancels; one that checked before only holds an approved
+ # row, which the check below then sees.
+ await state.set_setting(key, current)
if prospect_id in await _harvey_committed_ids():
+ await state.delete_setting(key)
return _refuse("Harvey has already approved or sent an email to this person. "
"Check the Outbox.")
- await state.set_setting(key, current)
await state.update_prospect_status(prospect_id, "contacted")
# Anything Harvey had queued for them must not go out as well.
changed = await state.cancel_pending_outbox_for_prospect(
diff --git a/harvey/web/app.js b/harvey/web/app.js
index abdfd3d..7581e98 100644
--- a/harvey/web/app.js
+++ b/harvey/web/app.js
@@ -1602,7 +1602,10 @@ function fillMergeVars(text, r) {
});
}
+let _gmailBusy = false; // one compose window per click, never two
+
async function openInGmail(ci, si, ri) {
+ if (_gmailBusy) return;
const c = _campaigns[ci];
const step = c && (c.sequence || [])[si];
const r = c && (c.recipients || [])[ri];
@@ -1627,7 +1630,10 @@ async function openInGmail(ci, si, ri) {
w.opener = null;
// Record the send BEFORE the user can press Send in Gmail, so Harvey can
// never email them as well — not even if they forget to tick the box.
- if (!r.sent_by_hand && !(await markSent(ci, ri, true))) { w.close(); return; }
+ _gmailBusy = true;
+ let claimed = r.sent_by_hand;
+ try { if (!claimed) claimed = await markSent(ci, ri, true); } finally { _gmailBusy = false; }
+ if (!claimed) { w.close(); return; }
w.location.href = url;
if (left) showToast('This draft still has a {{placeholder}} in it. Fix it in Gmail before sending.', 'error');
}
@@ -1648,8 +1654,9 @@ async function markSent(ci, ri, sent) {
if (!ok) showToast((res && res.message) || 'Could not save that. Try again.', 'error');
else if (!sent && res.sent) showToast('They already replied, so this stays marked as sent.');
loadCampaigns();
- // True only when this call changed it; a status in the reply means they replied or opted out.
- return ok && sent === !!res.sent && !res.status;
+ // True only when this call changed it: a status means they replied or
+ // opted out, 'already' means another click or tab got there first.
+ return ok && sent === !!res.sent && !res.status && !res.already;
}
async function loadCampaigns() {
diff --git a/tests/test_dashboard_manual_send.py b/tests/test_dashboard_manual_send.py
index 63d34d0..1b84230 100644
--- a/tests/test_dashboard_manual_send.py
+++ b/tests/test_dashboard_manual_send.py
@@ -296,3 +296,29 @@ def test_an_unreadable_config_means_harvey_might_send(monkeypatch):
raise ValueError("bad yaml")
monkeypatch.setattr(harvey.config, "load_config", boom)
assert dash._harvey_can_send() is True
+
+
+def test_marking_twice_says_it_was_already_marked(client, env):
+ # A double click must not open a second ready-to-send Gmail draft.
+ _prospect(env, "p1", "[email protected]", status="queued")
+ assert client.post("/api/prospects/p1/sent", json={"sent": True}).json() == {
+ "success": True, "sent": True}
+ assert client.post("/api/prospects/p1/sent", json={"sent": True}).json()["already"] is True
+
+
+def test_mail_approved_while_marking_is_refused_and_the_marker_removed(client, env, monkeypatch):
+ # The marker goes down first; if mail got approved meanwhile, the drain
+ # may already hold it, so the mark is refused and undone.
+ _prospect(env, "p1", "[email protected]", status="queued")
+ real = dash._harvey_committed_ids
+
+ async def approved_meanwhile():
+ _outbox(env, "o1", "p1", "approved")
+ with sqlite3.connect(env["db"]) as db:
+ assert db.execute("SELECT 1 FROM settings WHERE key = 'manual_send:p1'").fetchone()
+ return await real()
+ monkeypatch.setattr(dash, "_harvey_committed_ids", approved_meanwhile)
+ assert client.post("/api/prospects/p1/sent", json={"sent": True}).status_code == 409
+ with sqlite3.connect(env["db"]) as db:
+ assert db.execute("SELECT 1 FROM settings WHERE key = 'manual_send:p1'").fetchone() is None
+ assert _status(env, "prospects", "p1") == "queued"