-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.py
More file actions
281 lines (234 loc) · 10.4 KB
/
Copy pathrunner.py
File metadata and controls
281 lines (234 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
"""
AROS Runner — automated pipeline execution.
Scores, harvests, writes, and optionally sends emails for Tier A prospects.
"""
import csv
import os
import sys
import time
from datetime import datetime
import pandas as pd
from scorer import score_prospect
from harvester import harvest
from writer import write_email
from config import OWNER_EMAIL, SMTP_USER, SMTP_PASS
# MemCollab — cross-agent shared memory
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "memcollab"))
try:
from memcollab import record as mc_record, Trajectory, Outcome
MEMCOLLAB_AVAILABLE = True
except ImportError:
MEMCOLLAB_AVAILABLE = False
DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
SCORED_PATH = os.path.join(DATA_DIR, "scored_prospects.csv")
SENT_LOG_PATH = os.path.join(DATA_DIR, "sent_log.csv")
RUN_LOG_PATH = os.path.join(DATA_DIR, "run_log.csv")
def _ensure_data_dir():
os.makedirs(DATA_DIR, exist_ok=True)
def _already_sent(company: str) -> bool:
if not os.path.exists(SENT_LOG_PATH):
return False
df = pd.read_csv(SENT_LOG_PATH)
return company in df["Business Name"].values
def _log_sent(prospect: dict, email: dict):
row = {
"Business Name": prospect.get("Business Name", ""),
"First Name": prospect.get("First Name", ""),
"Last Name": prospect.get("Last Name", ""),
"Email": prospect.get("Email", ""),
"subject": email["subject"],
"reply_tier": email["predicted_reply_tier"],
"situation": prospect.get("situation", ""),
"timestamp": datetime.now().isoformat(),
}
write_header = not os.path.exists(SENT_LOG_PATH)
with open(SENT_LOG_PATH, "a", newline="") as f:
w = csv.DictWriter(f, fieldnames=row.keys())
if write_header:
w.writeheader()
w.writerow(row)
def _log_run(total_scored: int, tier_a: int, emails_generated: int, emails_sent: int):
row = {
"timestamp": datetime.now().isoformat(),
"total_scored": total_scored,
"tier_a": tier_a,
"emails_generated": emails_generated,
"emails_sent": emails_sent,
}
write_header = not os.path.exists(RUN_LOG_PATH)
with open(RUN_LOG_PATH, "a", newline="") as f:
w = csv.DictWriter(f, fieldnames=row.keys())
if write_header:
w.writeheader()
w.writerow(row)
def send_email_smtp(to_addr: str, subject: str, body: str) -> bool:
"""Send a plain-text email via Gmail SMTP. Returns True on success."""
if not SMTP_USER or not SMTP_PASS:
print(f" SMTP not configured — skipping send.")
return False
import smtplib
from email.mime.text import MIMEText
msg = MIMEText(body, "plain")
msg["Subject"] = subject
msg["From"] = SMTP_USER
msg["To"] = to_addr
try:
with smtplib.SMTP("smtp.gmail.com", 587, timeout=15) as server:
server.starttls()
server.login(SMTP_USER, SMTP_PASS)
server.sendmail(SMTP_USER, to_addr, msg.as_string())
return True
except Exception as e:
print(f" SMTP error: {e}")
return False
def run_pipeline(csv_path: str, max_emails: int = 5, dry_run: bool = True):
"""
Full pipeline:
1. Score all prospects from CSV
2. Take top N unsent Tier A
3. Harvest signals for each
4. Generate personalized email
5. Send (or dry-run log)
"""
_ensure_data_dir()
print("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
print(f"AROS RUNNER — {datetime.now().strftime('%Y-%m-%d %H:%M')}")
print(f"Mode: {'DRY RUN' if dry_run else 'LIVE SEND'}")
print("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
# Step 1: Score
print("\n[1/4] Scoring prospects...")
df = pd.read_csv(csv_path)
prospects = df.to_dict("records")
scored = [score_prospect(row) for row in prospects]
tier_a = [s for s in scored if s["icp_tier"] == "A"]
tier_b = [s for s in scored if s["icp_tier"] == "B"]
tier_c = [s for s in scored if s["icp_tier"] == "C"]
print(f" Total: {len(scored)} | A: {len(tier_a)} | B: {len(tier_b)} | C: {len(tier_c)}")
# Save scored
pd.DataFrame(scored).to_csv(SCORED_PATH, index=False)
# Step 2: Filter unsent Tier A
unsent = [s for s in tier_a if not _already_sent(s.get("Business Name", ""))]
targets = unsent[:max_emails]
print(f"\n[2/4] Targeting {len(targets)} unsent Tier A prospects (max {max_emails})")
if not targets:
print(" No new Tier A prospects to process.")
_log_run(len(scored), len(tier_a), 0, 0)
return
emails_generated = 0
emails_sent = 0
for i, prospect in enumerate(targets, 1):
company = prospect.get("Business Name", "—")
email_addr = prospect.get("Email", "")
name = f"{prospect.get('First Name', '')} {prospect.get('Last Name', '')}".strip()
print(f"\n[3/4] Prospect {i}/{len(targets)}: {name} | {company}")
# Harvest
print(f" Harvesting signals...")
prospect = harvest(prospect)
print(f" → {prospect['situation']} | {prospect['signal_evidence']}")
# Write email
print(f" Generating email...")
try:
email = write_email(prospect)
emails_generated += 1
print(f" → Subject: {email['subject']}")
print(f" → Reply tier: {email['predicted_reply_tier']}")
except Exception as e:
print(f" → Email generation failed: {e}")
continue
# Send or log
if dry_run:
print(f" → DRY RUN — logged, not sent.")
_log_sent(prospect, email)
emails_sent += 1
else:
if email_addr:
print(f" → Sending to {email_addr}...")
if send_email_smtp(email_addr, email["subject"], email["body"]):
_log_sent(prospect, email)
emails_sent += 1
print(f" → SENT.")
else:
print(f" → Send failed — logged anyway.")
_log_sent(prospect, email)
else:
print(f" → No email address — skipping send, logged.")
_log_sent(prospect, email)
# MemCollab: log trajectory for cross-agent learning
if MEMCOLLAB_AVAILABLE:
try:
defense = email.get("defense_profile") or {}
traj = Trajectory(
agent="AROS",
model_used="claude-haiku-4-5-20251001",
profile_text=f"{name} {company}",
defense_mode=defense.get("defense_mode", ""),
pkm_confidence=defense.get("awareness_score", 0) / 10.0,
awareness_score=defense.get("awareness_score", 0),
bypass_strategy=defense.get("bypass_strategy", ""),
channel="email",
message_text=email.get("body", ""),
message_word_count=len(email.get("body", "").split()),
outcome=Outcome.NO_REPLY,
vertical="home_care",
geography=f"{prospect.get('State', '')}",
icp_tier=prospect.get("icp_tier", ""),
)
mc_record(traj)
# Store trajectory_id for manual outcome updates
# Update via: python3 -c "from memcollab import update_outcome; update_outcome('TID', 'HOT')"
print(f" → MemCollab TID: {traj.trajectory_id}")
except Exception:
pass
# Rate limit between API calls
if i < len(targets):
time.sleep(1)
_log_run(len(scored), len(tier_a), emails_generated, emails_sent)
print(f"\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
print(f"RUN COMPLETE")
print(f" Scored: {len(scored)}")
print(f" Emails generated: {emails_generated}")
print(f" Emails sent/logged: {emails_sent}")
print(f"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
def cmd_test_smtp():
"""Send a test email to OWNER_EMAIL to verify SMTP works before any prospect sends."""
to_addr = OWNER_EMAIL or SMTP_USER
subject = f"AROS Test — SMTP Verified {datetime.now().strftime('%b %d %H:%M')}"
body = (
"This is a test email from AROS v1.\n\n"
"If you're reading this, SMTP sending is working correctly.\n"
f"From: {SMTP_USER}\n"
f"To: {to_addr}\n"
f"Timestamp: {datetime.now().isoformat()}\n\n"
"Your pipeline is ready to send prospect emails.\n"
"Run with --live to send real outreach.\n\n"
"— AROS v1"
)
print("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
print("AROS SMTP TEST")
print("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
print(f"From: {SMTP_USER}")
print(f"To: {to_addr}")
print(f"Subject: {subject}")
print(f"─────────────────────────────────")
print(body)
print(f"─────────────────────────────────")
print(f"Sending...")
if send_email_smtp(to_addr, subject, body):
print(f"SENT — check {to_addr} inbox.")
else:
print("FAILED — check SMTP_USER and SMTP_PASS in .env")
print("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="AROS Runner — automated pipeline")
parser.add_argument("--test", action="store_true", help="Send test email to OWNER_EMAIL to verify SMTP")
parser.add_argument("csv", nargs="?", help="Path to prospect CSV")
parser.add_argument("--max", type=int, default=5, help="Max emails to generate (default 5)")
parser.add_argument("--live", action="store_true", help="Actually send emails via SMTP (default: dry run)")
args = parser.parse_args()
if args.test:
cmd_test_smtp()
elif args.csv:
run_pipeline(args.csv, max_emails=args.max, dry_run=not args.live)
else:
parser.print_help()