# Jaka Agent v2 — Simplified Design

**Date:** 2026-02-25
**Status:** Approved
**Problem:** v1 agent had compound bugs (overlapping processes, uncapped AI loops, member reply chains) that caused 464 API calls, 7M tokens, and duplicate emails in a single hour. The multi-turn AI agent loop for member replies is too complex and fragile.

## Goal

Rewrite the orchestrator (`jaka_agent.pl`) as a clean ~400-500 line file that eliminates all member-facing reply logic. Members never receive a reply from the agent. Resumes/photos/profile data are processed silently.

## What Stays

| Feature | How |
|---------|-----|
| Proactive outreach | Jaka::Outreach sends profile-completion emails to members (A/B variants, cooldowns, daily report) |
| Boss-forwarded client emails | AI agent loop replies to clients, CC boss (CASE 2) |
| Boss direct messages | AI agent replies to boss — progress reports, doc learning (CASE 2b/2c) |
| Staff questions | AI agent drafts reply (CASE 3) |
| Circuit breaker / safety | All sanity checks, lock file, stop file |
| Email gateway | Jaka::Email with DB-backed rate limiting |

## What Changes

### Member replies (CASE 4) — the dangerous path

**Old:** 3 fast paths + AI agent conversation loop + compose reply + send reply
**New:** One-pass silent processing, zero replies

1. Classify attachments (photos vs resumes)
2. Save photos → update member profile photo (direct function call)
3. Save resumes → single Claude API call to extract structured data → update profile
4. Parse body text for profile info (college, employer mentioned)
5. Update `jaka_member_outreach` status to `replied`/`completed`
6. Delete email from POP3
7. **No reply sent. Ever.**

### Fallback routing (unknown external senders)

**Old:** 3-step lookup (jaka_emails → registry_data → forward to boss), could route into AI agent loop
**New:** Forward to boss. Period.

### Resume parsing — single-shot, no agent loop

Instead of running the full 25-iteration agent loop with tool calls:

```
Single Claude API call:
  "Extract from this resume text: job_title, current_employer, college, major,
   skills, work_history[]. Return JSON only."
→ Parse JSON
→ Call update_professional_data() + add_work_history() directly
```

One API call per resume. No tool-use loop, no send_email tool available.

## Eliminated Complexity

- 3 fast paths (A/B/C template replies)
- AI agent conversation loop for member replies
- `api_retry` mechanism (one-shot: works or skip)
- Per-thread reply dedup
- Fallback member routing logic
- `send_email` tool availability during member processing
- Multi-turn context window management for member conversations

## Architecture

```
jaka_agent_v2.pl (new, ~400-500 lines)
  ├── process_emails()
  │     ├── POP3 fetch + dedup (existing Inbox.pm)
  │     ├── Route by sender:
  │     │     ├── Boss forward → AI agent (existing AI.pm) → reply to client
  │     │     ├── Boss direct → AI agent → reply to boss
  │     │     ├── Staff → AI agent → draft only
  │     │     ├── Known member → silent_process_member_reply() [NEW]
  │     │     └── Unknown → forward to boss
  │     └── Delete from POP3
  ├── silent_process_member_reply()  [NEW ~100 lines]
  │     ├── Save photos (direct call to handle_save_member_photo)
  │     ├── Save + parse resumes (single API call, no agent loop)
  │     ├── Update profile fields
  │     └── Update outreach status
  └── run_proactive_tasks() (delegates to existing Outreach.pm)

Existing modules (unchanged):
  Jaka::Config, Jaka::DB, Jaka::Email, Jaka::Inbox,
  Jaka::Safety, Jaka::Outreach, Jaka::AI, Jaka::Tools
```

## Files

| File | Action |
|------|--------|
| `jaka_agent.pl` | Rename to `jaka_agent_v1.pl` (backup) |
| `jaka_agent.pl` | New clean orchestrator (~400-500 lines) |
| Modules in `lib/Jaka/` | No changes needed |
| `tests/run_tests.pl` | Update for new routing (remove member-reply tests, add silent-processing tests) |
