| |
| import os as _os |
| _np = _os.environ.get("no_proxy", _os.environ.get("NO_PROXY", "")) |
| for _h in ("127.0.0.1", "localhost", "28.7.184.198"): |
| if _h not in _np: |
| _np = (_np + "," + _h).lstrip(",") |
| _os.environ["no_proxy"] = _np |
| _os.environ["NO_PROXY"] = _np |
| |
|
|
| """ |
| Reward Script: AM Renewal Outreach — Northwind Traders renewal check-in |
| Task ID: am_renewal_outreach_002 |
| Domain: mock_websites (salesforce_mock + gmail_mock + slack_mock) |
| |
| Scoring (1.0 total), all programmatic / deterministic — verifies only the |
| NEW artifacts the agent must create (identified by id-set diff vs initial_state, |
| so pre-existing distractor records — incl. the old sent email to Karen Walsh and |
| the old customer-success message — never earn points): |
| |
| SALESFORCE task (0.34): |
| - 0.18 NEW 'task' activity related to the Northwind renewal (opp-1 / account-1), |
| with renewal/check-in intent and assigned to Jordan (user-1 when present) |
| - 0.16 that task has dueDate 2026-07-01 AND status 'Open' (not completed) |
| GMAIL email (0.33): |
| - 0.13 NEW email in 'sent' folder addressed to karen.walsh@northwind-traders.example.com |
| - 0.10 its subject matches the explicitly required subject, allowing case/spacing/punctuation variants |
| - 0.10 its body proposes a renewal call before the renewal timing/date |
| SLACK message (0.33): |
| - 0.16 NEW message posted by Jordan (user_1) in the 'customer-success' channel |
| - 0.17 that message says Northwind/Karen renewal outreach is underway/sent |
| """ |
| import html |
| import os |
| import re |
| import sys |
| from datetime import date |
|
|
| import requests |
|
|
| |
| |
| |
| try: |
| with open('/tmp/task_web_sid') as f: |
| sid = f.read().strip() |
| if not sid: |
| raise ValueError('sid is empty') |
| except Exception as e: |
| print(f'CRITICAL: Cannot read sid from /tmp/task_web_sid: {e}') |
| print('REWARD: 0.0') |
| sys.exit(0) |
|
|
| MOCKS = { |
| 'salesforce': 'http://28.7.184.198:8175', |
| 'gmail': 'http://28.7.184.198:8138', |
| 'slack': 'http://28.7.184.198:8178', |
| } |
|
|
| |
| |
| |
| PROXY_CANDIDATES = [ |
| os.environ.get('HTTPS_PROXY') or os.environ.get('https_proxy'), |
| None, |
| ] |
|
|
|
|
| def resolve_proxy(probe_url): |
| try: |
| requests.get(probe_url, timeout=8) |
| return None |
| except Exception: |
| pass |
| for candidate in PROXY_CANDIDATES: |
| if not candidate: |
| continue |
| try: |
| requests.get(probe_url, timeout=12, |
| proxies={'http': candidate, 'https': candidate}) |
| return candidate |
| except Exception: |
| continue |
| return None |
|
|
|
|
| PROXY = resolve_proxy(f"{MOCKS['salesforce']}/go?sid=conn-probe") |
| PROXIES = {'http': PROXY, 'https': PROXY} if PROXY else None |
|
|
|
|
| def fetch(name): |
| url = MOCKS[name] |
| data = requests.get(f'{url}/go?sid={sid}', timeout=20, proxies=PROXIES).json() |
| return data.get('initial_state') or {}, data.get('current_state') or {} |
|
|
|
|
| |
| |
| |
| try: |
| sf_init, sf_cur = fetch('salesforce') |
| gm_init, gm_cur = fetch('gmail') |
| sl_init, sl_cur = fetch('slack') |
| except Exception as e: |
| print(f'CRITICAL: Cannot fetch mock state: {e}') |
| print('REWARD: 0.0') |
| sys.exit(0) |
|
|
|
|
| def strip_html(s): |
| return re.sub(r'<[^>]+>', ' ', str(s or '')) |
|
|
|
|
| TARGET_EMAIL_SUBJECT = 'Northwind Traders renewal call before July 15' |
| EXPECTED_TASK_DUE_DATE = date(2026, 7, 1) |
| EXPECTED_RENEWAL_DATE = date(2026, 7, 15) |
|
|
|
|
| def norm(s): |
| text = html.unescape(strip_html(s)) |
| text = re.sub(r'[\u2010-\u2015]', '-', text) |
| return re.sub(r'\s+', ' ', text).strip().casefold() |
|
|
|
|
| def loose(s): |
| """Normalize user-entered prose for case/whitespace/punctuation-tolerant matching.""" |
| return re.sub(r'\s+', ' ', re.sub(r'[^a-z0-9]+', ' ', norm(s))).strip() |
|
|
|
|
| def contains_any(text, phrases): |
| haystack = f" {loose(text)} " |
| return any(f" {loose(phrase)} " in haystack for phrase in phrases) |
|
|
|
|
| def text_blob(*values): |
| return ' '.join(str(v or '') for v in values) |
|
|
|
|
| def canonical_subject(s): |
| return loose(s) |
|
|
|
|
| def subject_matches_target(subject): |
| return canonical_subject(subject) == canonical_subject(TARGET_EMAIL_SUBJECT) |
|
|
|
|
| def norm_channel_name(s): |
| return norm(s).lstrip('#').strip() |
|
|
|
|
| MONTH_NAMES = { |
| 1: ('january', 'jan'), 2: ('february', 'feb'), 3: ('march', 'mar'), |
| 4: ('april', 'apr'), 5: ('may', 'may'), 6: ('june', 'jun'), |
| 7: ('july', 'jul'), 8: ('august', 'aug'), 9: ('september', 'sep'), |
| 10: ('october', 'oct'), 11: ('november', 'nov'), 12: ('december', 'dec'), |
| } |
|
|
|
|
| def ordinal(n): |
| if 10 <= n % 100 <= 20: |
| suffix = 'th' |
| else: |
| suffix = {1: 'st', 2: 'nd', 3: 'rd'}.get(n % 10, 'th') |
| return f'{n}{suffix}' |
|
|
|
|
| def mentions_date(text, expected): |
| t = norm(text) |
| month_full, month_short = MONTH_NAMES[expected.month] |
| day = expected.day |
| year = expected.year |
| variants = { |
| f'{month_full} {day}', f'{month_full} {ordinal(day)}', |
| f'{month_full} {day}, {year}', f'{month_full} {ordinal(day)}, {year}', |
| f'{month_full} {day} {year}', f'{month_full} {ordinal(day)} {year}', |
| f'{month_short} {day}', f'{month_short} {ordinal(day)}', |
| f'{month_short} {day}, {year}', f'{month_short} {ordinal(day)}, {year}', |
| f'{month_short} {day} {year}', f'{month_short} {ordinal(day)} {year}', |
| f'{day} {month_full}', f'{ordinal(day)} {month_full}', |
| f'{day} {month_short}', f'{ordinal(day)} {month_short}', |
| f'{day} {month_full} {year}', f'{ordinal(day)} {month_full} {year}', |
| f'{day} {month_short} {year}', f'{ordinal(day)} {month_short} {year}', |
| expected.isoformat(), |
| } |
| if any(v in t for v in variants): |
| return True |
|
|
| m = expected.month |
| numeric_patterns = [ |
| rf'\b0?{m}[/.-]0?{day}(?:[/.-]{year})?\b', |
| rf'\b{year}[/.-]0?{m}[/.-]0?{day}\b', |
| rf'\b0?{day}[/.-]0?{m}(?:[/.-]{year})?\b', |
| ] |
| return any(re.search(p, t) for p in numeric_patterns) |
|
|
|
|
| def mentions_renewal_timing(text): |
| return mentions_date(text, EXPECTED_RENEWAL_DATE) or contains_any(text, [ |
| 'renewal date', 'renewal deadline', 'renewal close date', 'close date', |
| 'before renewal', 'before the renewal', 'before your renewal', |
| 'ahead of renewal', 'ahead of the renewal', 'prior to renewal', |
| 'prior to the renewal', 'before it renews', 'before the contract renews', |
| ]) |
|
|
|
|
| def proposes_call(text): |
| call_intent = contains_any(text, [ |
| 'call', 'meeting', 'meet', 'chat', 'connect', 'sync', 'discussion', |
| 'discuss', 'conversation', 'touch base', 'talk', 'speak', |
| 'schedule time', 'set up time', |
| ]) |
| renewal_context = contains_any(text, ['renewal', 'renew', 'account']) \ |
| or mentions_renewal_timing(text) |
| return call_intent and renewal_context |
|
|
|
|
| def date_field_matches(value, expected): |
| if not value: |
| return False |
| text = norm(value) |
| if text[:10] == expected.isoformat(): |
| return True |
| return mentions_date(text, expected) |
|
|
|
|
| total_score = 0.0 |
|
|
| |
| |
| |
| try: |
| init_ids = {a.get('activityId') for a in sf_init.get('activities', [])} |
| new_acts = [a for a in sf_cur.get('activities', []) |
| if a.get('activityId') not in init_ids] |
|
|
| |
| |
| |
| |
| sf_task = None |
| for a in new_acts: |
| if norm(a.get('type')) != 'task': |
| continue |
| task_text = text_blob(a.get('subject'), a.get('description'), a.get('comments'), a.get('notes')) |
| related_type = norm(a.get('relatedToType')) |
| related_ok = (related_type == 'opportunity' and a.get('relatedToId') == 'opp-1') \ |
| or (related_type == 'account' and a.get('relatedToId') == 'account-1') |
| assigned_ok = not a.get('assignedToId') or a.get('assignedToId') == 'user-1' |
| intent_ok = contains_any(task_text, [ |
| 'renewal', 'renew', 'check in', 'check-in', 'follow up', |
| 'follow-up', 'outreach', 'call', 'meeting', 'touch base', |
| 'karen', 'walsh', |
| ]) |
| if related_ok and assigned_ok and intent_ok: |
| sf_task = a |
| break |
|
|
| |
| if sf_task is not None: |
| print(f"PASS: SF new renewal task on Northwind (id={sf_task.get('activityId')}, " |
| f"subject={sf_task.get('subject')!r}, related={sf_task.get('relatedToId')}) (0.18)") |
| total_score += 0.18 |
|
|
| |
| due_ok = date_field_matches(sf_task.get('dueDate'), EXPECTED_TASK_DUE_DATE) |
| status_ok = norm(sf_task.get('status')) in ('open', 'not started', 'in progress', 'pending') |
| if due_ok and status_ok: |
| print(f"PASS: SF task dueDate=2026-07-01 and status Open " |
| f"(status={sf_task.get('status')!r}) (0.16)") |
| total_score += 0.16 |
| else: |
| print(f"FAIL: SF task date/status — dueDate={sf_task.get('dueDate')!r} " |
| f"(want 2026-07-01), status={sf_task.get('status')!r} (want Open)") |
| else: |
| print(f"FAIL: SF — no NEW task related to opp-1/account-1 with " |
| f"renewal/check-in intent assigned to user-1 when present " |
| f"(new activities: {[a.get('activityId') for a in new_acts]})") |
| except Exception as e: |
| print(f'ERROR: Salesforce component — {e}') |
|
|
| |
| |
| |
| KAREN = 'karen.walsh@northwind-traders.example.com' |
| try: |
| init_ids = {e.get('id') for e in gm_init.get('emails', [])} |
| new_emails = [e for e in gm_cur.get('emails', []) |
| if e.get('id') not in init_ids] |
|
|
| |
| |
| |
| sent_to_karen = None |
| sent_to_karen_emails = [] |
| exact_subject_email = None |
| for e in new_emails: |
| if norm(e.get('folder')) != 'sent': |
| continue |
| recips = [norm(t.get('email')) for t in e.get('to', [])] |
| if KAREN in recips: |
| sent_to_karen_emails.append(e) |
| if sent_to_karen is None: |
| sent_to_karen = e |
| if subject_matches_target(e.get('subject')): |
| exact_subject_email = e |
|
|
| |
| if sent_to_karen is not None: |
| print(f"PASS: Gmail NEW sent email to Karen Walsh " |
| f"(id={sent_to_karen.get('id')}, subject={sent_to_karen.get('subject')!r}) (0.13)") |
| total_score += 0.13 |
|
|
| |
| if exact_subject_email is not None: |
| print(f"PASS: Gmail subject matches required subject {TARGET_EMAIL_SUBJECT!r} (0.10)") |
| total_score += 0.10 |
| else: |
| print(f"FAIL: Gmail subject — expected required subject {TARGET_EMAIL_SUBJECT!r}; " |
| f"got {[e.get('subject') for e in sent_to_karen_emails]}") |
|
|
| |
| |
| |
| def email_body_ok(e): |
| body = strip_html(e.get('body')) |
| timing_ok = mentions_renewal_timing(body) or subject_matches_target(e.get('subject')) |
| return proposes_call(body) and timing_ok |
|
|
| body_email = next((e for e in ([exact_subject_email] if exact_subject_email else []) |
| if e and email_body_ok(e)), None) |
| if body_email is None: |
| body_email = next((e for e in sent_to_karen_emails if email_body_ok(e)), None) |
|
|
| if body_email is not None: |
| print("PASS: Gmail body proposes a renewal call before the renewal timing/date (0.10)") |
| total_score += 0.10 |
| else: |
| print("FAIL: Gmail body — need a sent-to-Karen email body proposing a renewal " |
| "call/meeting before the renewal timing/date") |
| else: |
| print(f"FAIL: Gmail — no NEW sent email addressed to {KAREN} " |
| f"(new emails: {[e.get('id') for e in new_emails]})") |
| except Exception as e: |
| print(f'ERROR: Gmail component — {e}') |
|
|
| |
| |
| |
| try: |
| |
| |
| def customer_success_keys(state): |
| keys = set() |
| for c in state.get('channels', []): |
| name = c.get('name') |
| channel_id = c.get('channelId') |
| if norm_channel_name(name) == 'customer-success' \ |
| or norm_channel_name(channel_id) == 'customer-success': |
| for value in (channel_id, name, norm_channel_name(name), |
| f"#{norm_channel_name(name)}"): |
| if value: |
| keys.add(value) |
| return keys |
|
|
| cs_keys = customer_success_keys(sl_cur) or customer_success_keys(sl_init) |
|
|
| if not cs_keys: |
| print("FAIL: Slack — 'customer-success' channel not found") |
| else: |
| def messages_for_channel(state, keys): |
| allowed = {norm_channel_name(k) for k in keys if k} |
| messages = [] |
| seen = set() |
| for key, vals in (state.get('messages') or {}).items(): |
| if key in keys or norm_channel_name(key) in allowed: |
| for m in vals or []: |
| msg_id = m.get('messageId') or id(m) |
| if msg_id not in seen: |
| seen.add(msg_id) |
| messages.append(m) |
| return messages |
|
|
| init_msgs = messages_for_channel(sl_init, cs_keys) |
| cur_msgs = messages_for_channel(sl_cur, cs_keys) |
| init_msg_ids = {m.get('messageId') for m in init_msgs} |
| new_msgs = [m for m in cur_msgs if m.get('messageId') not in init_msg_ids] |
|
|
| |
| my_new = [m for m in new_msgs if m.get('senderId') == 'user_1'] |
| if my_new: |
| print(f"PASS: Slack NEW message by user_1 in #customer-success " |
| f"({len(my_new)} new) (0.16)") |
| total_score += 0.16 |
|
|
| |
| |
| |
| def content_ok(m): |
| t = m.get('content') |
| customer = contains_any(t, ['northwind', 'karen walsh', 'karen', 'walsh']) |
| renewal = contains_any(t, ['renewal', 'renew', 'renewing', 'outreach', |
| 'check in', 'check-in', 'call', 'meeting']) |
| underway = contains_any(t, [ |
| 'underway', 'kicked off', 'kicked-off', 'reached out', 'reach out', |
| 'emailed', 'email sent', 'sent email', 'contacted', 'check-in', |
| 'check in', 'sent', 'outreach', 'started', 'initiated', |
| 'in progress', 'following up', 'followed up', 'handled', 'done', |
| ]) |
| return customer and renewal and underway |
|
|
| good = next((m for m in my_new if content_ok(m)), None) |
| if good is not None: |
| print("PASS: Slack message reports Northwind/Karen renewal outreach underway (0.17)") |
| total_score += 0.17 |
| else: |
| print(f"FAIL: Slack message content — need Northwind/Karen + renewal/check-in " |
| f"+ outreach-underway/sent. Contents: {[norm(m.get('content'))[:80] for m in my_new]}") |
| else: |
| print(f"FAIL: Slack — no NEW message by user_1 in #customer-success " |
| f"(new msgs: {[m.get('messageId') for m in new_msgs]})") |
| except Exception as e: |
| print(f'ERROR: Slack component — {e}') |
|
|
| |
| final_score = round(min(total_score, 1.0), 4) |
| print(f'\nScore: {total_score}/1.0') |
| print(f'REWARD: {final_score}') |
|
|