feat(emails): thread trigger_run_id from chat runs into the email send log (write path)

SW
4mo ago
+162-4

Implements row 4 of recoupable/chat#1958 — the write path that links an email to the scheduled run that sent it.

The carrier, scoped (the row's research flag, resolved)

The sandbox agent sends email by curling POST /api/emails itself (recoup-api skill + ephemeral key) — so the run id must reach the sandbox environment:

POST /api/chat/runs { trigger_run_id }            (sent by tasks#156)
  → chatRunBodySchema → ChatRunRequest.triggerRunId
  → buildRunAgentInput → AgentContext.triggerRunId (durable workflow input)
  → buildRecoupExecEnv → RECOUP_TRIGGER_RUN_ID in every sandboxed exec
  → recoupApiSkillPrompt: when set, ALWAYS include trigger_run_id in POST /api/emails bodies
  → validateSendEmailBody accepts it → sendEmailHandler → logEmailAttempt
  → email_send_log.subject + .trigger_run_id   (columns from database#57)

Honest softness, by design: the final hop rides prompt adherence (the agent composes the curl). A miss degrades to the schedule-title fallback (api#764), never a broken run — and the send log's raw_body still captures everything for debugging.

subject is now persisted on every logged attempt (interactive sends included), which also benefits admin email debugging.

Tests

TDD red → green across 5 suites: buildRecoupExecEnv injects/omits the env var; chat-run schema passes trigger_run_id through; validateSendEmailBody accepts it; sendEmailHandler forwards subject + run id to the log; logEmailAttempt persists them (nulls when absent). 320 tests green across the touched suites; tsc delta vs baseline: 0 (202 = 202); eslint clean.

Sequencing

After docs#268 + database#57 (needs the columns). tasks#156 supplies the id; api#764 reads it back.

🤖 Generated with Claude Code


Summary by cubic

Threads a scheduled task’s Trigger.dev run id from chat runs into the sandbox as RECOUP_TRIGGER_RUN_ID and persists it (with subject) to email_send_log. This links each sent email to the run that produced it so the Tasks UI can name and trace runs by email subject; previously neither trigger_run_id nor subject were consistently logged.

  • Accepts trigger_run_id on POST /api/chat/runs, passes it to the workflow input and AgentContext, and injects RECOUP_TRIGGER_RUN_ID via buildRecoupExecEnv.
  • Updates recoupApiSkillPrompt to instruct including "trigger_run_id" in POST /api/emails when the env var is set; validateSendEmailBody accepts it; sendEmailHandler forwards; logEmailAttempt stores subject and trigger_run_id (null when absent).
  • Behavior change: subject is now logged for every attempt (interactive and headless). The new trigger_run_id is optional; callers of POST /api/emails are not required to send it. Missing ids fall back to the schedule-title naming.

Rollout

  • Run the migration adding subject and trigger_run_id to email_send_log before deploying.
  • Ensure scheduled tasks supply trigger_run_id to POST /api/chat/runs. No other client changes required.

Summary by CodeRabbit

  • New Features
    • Added support for associating scheduled runs with chat-initiated email sends.
    • Email requests can now include an optional run identifier.
    • Email activity logs capture the associated run ID and subject for both successful and failed sends.