# agent finalized a draft invoice for the wrong tenant: tenant scoping bug

## TL;DR
The agent finalized an invoice without scoping the action to the right tenant, so it landed in the wrong customer's books. Void or move the invoice, then scope every write by tenant: the tenant comes from authenticated context, and every query filters by it. Unscoped writes in a multi-tenant system are data corruption.

```text
agent finalized a draft invoice for the wrong tenant: tenant scoping bug
```

## Steps

1. Identify every invoice the agent touched without tenant scope: check finalized, sent, and paid states. Success check: the full list of affected invoices and tenants.
2. Void or reverse the wrong-tenant invoices and reissue them under the correct tenant. Success check: each tenant's books show only their own invoices.
3. Notify affected tenants about the error and the correction. Success check: no tenant discovers it from their own books first.
4. Fix the code: every invoice query and mutation takes a tenant id from authenticated context and filters by it. Success check: no invoice operation exists without a tenant parameter.
5. Add a test that creates two tenants with similar data and proves actions cannot cross. Success check: the test fails if scoping is ever removed.

## When this applies

- An agent finalized or modified an invoice under the wrong tenant.
- Multi-tenant invoice operations lack tenant filtering.
- You are auditing an agent's writes for cross-tenant leakage.

## When it doesn't

- Single-tenant systems; there is no tenant boundary to cross.
- A human picking the wrong customer; that is a UI problem.
- Deliberate cross-tenant reporting by an admin; that is authorized access.

## Tool + version compatibility

Multi-tenant invoicing in any ERP or billing system an agent drives.

### "agent wrote invoice to wrong tenant"
Void, reissue, notify, then scope every write.

### "multi tenant invoice scoping bug"
Tenant id from authenticated context on every query and mutation.

### "invoice finalized under wrong customer"
Find all affected invoices before fixing the code; the blast radius matters.

## Why it happens

Multi-tenant agents juggle many customers in one process, and invoice operations that do not carry the tenant id operate on whatever the default scope happens to be. One unscoped finalize writes customer A's invoice into customer B's books: wrong amounts, wrong tax, wrong audit trail, and a privacy breach in one action. The tenant id must be a required parameter of every data operation, taken from authentication, never optional and never from user input.

## Edge cases / pitfalls

- Treat this as a privacy incident: one tenant's financial data was visible in another's books.
- Check downstream effects: sent invoices, payment links, and tax filings may all need correction.
- Draft invoices are the warning sign; if drafts cross tenants, finalized ones will too.
- An agent that lists invoices without tenant scope leaks data on reads as well; scope reads and writes alike.

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_B6KuYS1JVgrEnKgUjO2WbA
