Files
a99d541b45 fix(db): actors outlived their accounts — the last table in the family (#655)
profiles (#637) and cat_conversations (#651) both cascade from the account
now. Each sweep stopped one table short of done, and `actors.user_id` is
what was left: it references nothing at all. Deleting an account still
leaves the actor standing — the row that owns every product, service,
project, cause, event, loan and asset the person ever published.

15 orphaned actors were live in production. They were `e2e-reset-*`,
`cat-wallet-eval-*` and `cascade-proof-*` rows: the verification of the
earlier fixes leaked rows into the table the next fix would be about.
Purged, behind a guard that proved they owned 0 rows across all 25 child
foreign keys before deleting anything.

Unlike its siblings this one cannot be closed with a constraint, which is
why this adds a check rather than a migration:

  - ON DELETE CASCADE would delete the actor, which cascades on into
    contracts, investments and bookings — destroying the COUNTERPARTY's
    record of an agreement because the other side closed their account.
  - ON DELETE SET NULL is rejected outright by actor_type_check, which
    requires actor_type='user' to have a non-null user_id.

The honest fix is a tombstone — keep the actor, sever the identity — which
needs a deleted/anonymous actor_type. That is a schema decision, not a
drive-by, so it is stated here and left for one.

Until it lands:

  - `actors.orphaned` in the nightly invariant gate. This is the part that
    covers call sites nobody thought to patch, including future ones.
  - the two known leak sites delete the actor explicitly:
    refresh-e2e-reset-tokens.mjs (every CI run) and write-test-supabase.js,
    whose comment claimed "cascade removes profile" — true, and precisely
    the trap: it names the fixed thing while missing the one still broken.

Mutation-tested both directions with --gate: one injected orphan makes it
exit 1, removing it exits 0.

Co-authored-by: Georgy Butaev <41178744+g-but@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-07 19:39:49 +00:00
..