idempotency_key is UNIQUE across the whole ledger_entries table, not
scoped to (tenant_id, idempotency_key). apply_entry_checked's replay
lookup returned the matched entry as Applied regardless of whether its
tenant_id matched the caller's requested tenant_id, pairing the OTHER
tenant's entry (reference/memo/amount) with THIS tenant's own
unaffected balance — reporting success for a write that never actually
happened, with no error anywhere.
Every real caller today derives idempotency_key from a CSPRNG/UUIDv4
id (idgen.New, Uuid::new_v4), so a genuine collision across tenants is
cryptographically negligible in practice — but the lookup itself never
enforced that, which matters for a platform whose hackathon track is
explicitly multi-tenant isolation. Add LedgerError::IdempotencyKeyTenantMismatch
and fail loudly instead of silently mixing tenants.
Verified: cargo build --release clean, cargo check --release --tests
clean (new test type-checks), cargo fmt --check clean on touched
files, cargo clippy --release exit 0, and a full cargo test --release
--test idempotency run (including the new
replaying_another_tenants_idempotency_key_is_rejected_not_silently_applied
regression test) completed with exit code 0 on this box despite heavy
concurrent sibling-agent load intermittently hanging repeat attempts
at process exec (unrelated amfid/syspolicyd contention, not a code
issue — confirmed via `sample` showing the test binary stuck at
dyld_start, and the same stall reproducing on the already-built,
previously-working main ledger-core binary too).
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>