Skip to content

fix(drains): a manual reply or voice drain reads only the caller's tenant - #644

Open
privatejoel wants to merge 1 commit into
mainfrom
claude/scope-manual-drains
Open

privatejoel wants to merge 1 commit into
mainfrom
claude/scope-manual-drains

Conversation

@privatejoel

Copy link
Copy Markdown
Collaborator

Summary

This PR fixes a cross-tenant defect in two manual drain endpoints. The SO agent infrastructure audit plan lists it as D-1 (docs/SO_AGENT_INFRA_AUDIT_PLAN.md, section 8.1).

  • POST /api/agents/handle_replies and POST /api/voice/process_actions run on the service role. The service role bypasses RLS.
  • Before this PR, the manual path called drainOnce(svc) with no tenant filter. An approver in one tenant processed the inbound_emails rows and the voice_call_actions rows of every tenant.
  • Now drainOnce takes an optional tenant id. The manual path passes ctx.tenantId. The cron path (Bearer CRON_SECRET, from src/api/cron/tick.js) passes no tenant id. It still drains all tenants.
  • process_actions.js passes the tenant id to drainQueue, which already supports it. handle_replies.js adds .eq("tenant_id", tenantId) to its own read when a tenant id is set.
  • This follows the pattern in src/api/inbound/process_messages.js and src/api/inbound/email/draft_orders.js.

Deploy notes

  • No migration.
  • No environment change.
  • No change to the cron path.

Tests

  • src/v3-app/api-agents-handle-replies-tenant-scope.test.js (new, 2 tests).
  • src/v3-app/api-voice-process-actions-tenant-scope.test.js (new, 2 tests).
  • Each file seeds one row for each of two tenants. A manual drain by a tenant A approver handles only the tenant A row, and its read filters on tenant A. A cron drain handles both rows, and its read has no tenant filter.
  • On the old code, the two manual tests fail and the two cron tests pass.
  • npm run check passes.

Not covered

  • Six more manual drains call an unscoped drain after requirePermission. This PR does not change them:
    • src/api/quotes/expire.js:75
    • src/api/inbound/email/persist_attachments.js:128
    • src/api/inbound/email/parse.js:148
    • src/api/cron/tally-reconcile.js:90
    • src/api/cron/drift-meter.js:156 (requires admin)
    • src/api/cron/drift-report.js:270
  • The write paths inside each drain (row updates by id, audit inserts with the row's own tenant_id) did not change. They were correct before, because they act only on rows the read returned.
  • I did not run the endpoints against a live database.

🤖 Generated with Claude Code

…nant

The manual path of /api/agents/handle_replies and /api/voice/process_actions
called drainOnce(svc) on the service role with no tenant filter. An approver
in one tenant processed the inbound_emails and voice_call_actions rows of
every tenant. drainOnce now takes an optional tenant id. The manual path
passes ctx.tenantId. The cron path passes none and still drains all tenants.
This follows inbound/process_messages.js and inbound/email/draft_orders.js.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vercel

vercel Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
anvil Ready Ready Preview Oct 27, 2026 2:37am UTC

This branch was successfully deployed

1 active deployment
Preview — a1e7769a Deployed Oct 27, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant