fix(emails): record the account on rejected sends (chat#1889 row 27)
Matrix row 27 in chat#1889.
Why
sendEmailHandler.ts L59 set attempt = { status: "rejected" } with no account, so every account-scoped email_send_log audit under-reported.
Concretely, in the 2026-07-27 scheduled-run incident: 8 POST /api/emails attempts, 2 sent and 6 rejected. An account-scoped query returned 2 of 8 — the six rejections were findable only by filtering raw_body on the recipient. The recoup-internal-task-email-audit skill is account-scoped, so it under-reports the same way. This is why the incident went unnoticed.
What changed
validateSendEmailBody's error branch now carries accountId wherever one can be resolved, and the handler records it:
| Rejection path | Account recorded |
|---|---|
| Schema / body invalid (400) | — (nothing resolved yet) |
| Auth failed (401) | the account the body claimed (account_id, already schema-validated as a UUID) |
| No recipient on file (400) | the authenticated account |
| Recipient restriction (403) | the authenticated account |
The auth-failure row is the one that matters: the six rejections in the incident were validateAuthContext failures after the sandbox credential expired ~1h from kickoff, so an expired credential is precisely how a legitimate account's sends vanish from the audit.
Deliberate trade-off, worth a reviewer's eye
On the auth-failed path the recorded id is the account the request claimed, not one we verified. That is the only thing knowable there, and attribution is the point of the log — but it does mean a caller could write rows referencing an account they don't hold. I judged that acceptable because the row is an internal audit record, its status is rejected (so it is never evidence of a successful action), and raw_body is stored alongside. It is documented on ValidateSendEmailResult as "the best-known account, not proof of authorization".
If you'd rather not record unverified ids, the alternative is to carry only the authenticated cases — but then the incident's six rejections still would not be attributable, which defeats the row. Happy to switch on your call.
Verification
TDD, red → green:
# RED
FAIL lib/emails/__tests__/sendEmailHandler.test.ts (2 failed)
> records the account on a rejected attempt when validation resolved one
> still logs a rejected attempt when no account could be resolved
# GREEN — full api suite
pnpm exec vitest run
Test Files 784 passed (784)
Tests 4250 passed (4250)
Three tests: the handler forwards a resolved account, the handler still logs when none was resolved (so the fix can't regress into dropping rejections entirely), and validateSendEmailBody surfaces the authenticated account on the 403 path.
pnpm exec tsc --noEmit → 236, identical to main's baseline, none in the touched files. eslint clean.
No docs change: POST /api/emails's request/response contract is unchanged — this only affects what we write to email_send_log.
Preview verification to follow on this PR.
Tracked in chat#1889 (matrix row 27).
Summary by cubic
Record the best-known account on rejected email sends so account-scoped email_send_log audits include failures, addressing chat#1889 (row 26). This restores visibility for rejections, including those caused by expired credentials.
- Bug Fixes
validateSendEmailBodynow returnsaccountIdon error:- 401 auth failed: uses body
account_id(schema-validated UUID). - 400 no recipient / 403 recipient restriction: uses authenticated account.
- 400 schema/body invalid: no account.
- 401 auth failed: uses body
sendEmailHandlerrecordsaccountIdforrejectedattempts.- Added tests for both resolved and unresolved account cases.
- Note: On 401, the recorded account is the claimed one; for attribution only, not proof of authorization.
Summary by CodeRabbit
- Bug Fixes
- Email rejection records now retain the associated account information.
- Account details are preserved across authentication, address, and recipient validation failures, improving auditability and troubleshooting.