fix(api): insert-only occurrence writes revive superseded rows (stash 409) #887

Merged
chris merged 2 commits from hotfix/occurrence-insert-revives-superseded into main 2026-09-26 01:23:55 +02:00
Owner

Root cause

Since #876 (spec 045 insert-only stash decisions), insertOccurrenceNoSteal relies on the unique index class_instance_occurrences (domain_key, owner_id, occurrence_type, occurrence_key) + ON CONFLICT DO NOTHING. A row in state='superseded' still occupies that index (supersede keeps the row; the face was released, e.g. a face auto-assigned into a stash that was later disbanded/archived and released by V124). The insert therefore returns 0, requireOccurrenceWrites fails and acceptPendingCandidatesIntoExistingInstance returns null:

  • every "Stash away" on a suggestion for such a face 409s ("Candidate changed while accepting; decision was not applied")
  • shadow auto-accepts take the lost-insert-race branch and reject the candidate + write lost_insert_race review-decision spam
  • human "accept into existing person" was unaffected (allowMove=true takes the unconditional UPDATE arm)

Prod evidence: ~8,166 superseded rows, all on non-active instances, sitting on live unique keys.

Fix

Both insert-only arms now revive a superseded row in place before inserting:

  • reviveSupersededOccurrence: targeted UPDATE ... WHERE domain_key/owner_id/occurrence_type/occurrence_key AND state='superseded' sets class_instance_id=target, candidate_id, media_item_id, occurrence_ref, state='accepted', assignment_basis=<path basis>, provenance, accepted_at=now, superseded_at=NULL, updated_at=now and counts as a written occurrence (same row id preserved, no duplicate).
  • Rows in state='accepted' remain no-steal conflicts (ON CONFLICT DO NOTHING); removed rows stay untouched.
  • H2 arm: MERGE KEY (blind replace) replaced with MERGE ... USING whose matched update is restricted to state='superseded', so H2 no longer steals live rows either.
  • No disband/dissolve/merge/split changes, no schema changes, skew guard (allFaceRefsAlreadyAcceptedOnInstance) still counts only accepted, pendingCandidate/snapshot parity untouched.

Tests

  • Spec045StashTentativePostgresTest: stash decision revives the zombie row in place (id preserved, re-pointed to the shadow, user_confirmed); shadow auto-accept revives instead of lost_insert_race (auto_assigned); negative live-accepted no-steal test unchanged and green.
  • ClassInstanceDiscoveryServiceTest (H2): revive parity + live-accepted no-steal parity (previously H2 MERGE replaced live rows).
  • TentativeCandidatePostgresTest.pgZeroWriteAutoAcceptLeavesLockedOverlapUntouched now seeds a 'removed' conflict (still blocks, not revived, not filtered) so it keeps covering the zero-write overlap-untouched branch.
  • ./gradlew :apps:api:test: 1217 tests, 0 failures, 9 skipped.
## Root cause Since #876 (spec 045 insert-only stash decisions), `insertOccurrenceNoSteal` relies on the unique index `class_instance_occurrences (domain_key, owner_id, occurrence_type, occurrence_key)` + `ON CONFLICT DO NOTHING`. A row in `state='superseded'` still occupies that index (supersede keeps the row; the face was released, e.g. a face auto-assigned into a stash that was later disbanded/archived and released by V124). The insert therefore returns 0, `requireOccurrenceWrites` fails and `acceptPendingCandidatesIntoExistingInstance` returns null: - every "Stash away" on a suggestion for such a face 409s ("Candidate changed while accepting; decision was not applied") - shadow auto-accepts take the lost-insert-race branch and reject the candidate + write `lost_insert_race` review-decision spam - human "accept into existing person" was unaffected (allowMove=true takes the unconditional UPDATE arm) Prod evidence: ~8,166 superseded rows, all on non-active instances, sitting on live unique keys. ## Fix Both insert-only arms now revive a superseded row in place before inserting: - `reviveSupersededOccurrence`: targeted `UPDATE ... WHERE domain_key/owner_id/occurrence_type/occurrence_key AND state='superseded'` sets `class_instance_id=target, candidate_id, media_item_id, occurrence_ref, state='accepted', assignment_basis=<path basis>, provenance, accepted_at=now, superseded_at=NULL, updated_at=now` and counts as a written occurrence (same row id preserved, no duplicate). - Rows in `state='accepted'` remain no-steal conflicts (`ON CONFLICT DO NOTHING`); `removed` rows stay untouched. - H2 arm: `MERGE KEY` (blind replace) replaced with `MERGE ... USING` whose matched update is restricted to `state='superseded'`, so H2 no longer steals live rows either. - No disband/dissolve/merge/split changes, no schema changes, skew guard (`allFaceRefsAlreadyAcceptedOnInstance`) still counts only accepted, pendingCandidate/snapshot parity untouched. ## Tests - `Spec045StashTentativePostgresTest`: stash decision revives the zombie row in place (id preserved, re-pointed to the shadow, `user_confirmed`); shadow auto-accept revives instead of `lost_insert_race` (`auto_assigned`); negative live-accepted no-steal test unchanged and green. - `ClassInstanceDiscoveryServiceTest` (H2): revive parity + live-accepted no-steal parity (previously H2 MERGE replaced live rows). - `TentativeCandidatePostgresTest.pgZeroWriteAutoAcceptLeavesLockedOverlapUntouched` now seeds a `'removed'` conflict (still blocks, not revived, not filtered) so it keeps covering the zero-write overlap-untouched branch. - `./gradlew :apps:api:test`: 1217 tests, 0 failures, 9 skipped.
fix(api): revive superseded occurrence rows on insert-only writes (stash 409)
All checks were successful
Wiki Sync / Sync wiki from docs (push) Has been skipped
Build and Deploy / Detect changed services and test scopes (push) Successful in 4s
Build and Deploy / Android unit tests and debug APK (push) Has been skipped
Build and Deploy / Smoke API and media worker (push) Has been skipped
Build and Deploy / Smoke web (push) Has been skipped
Build and Deploy / Smoke ML worker (push) Has been skipped
Build and Deploy / Smoke people worker (push) Has been skipped
Build and Deploy / Build postgres image (push) Has been skipped
Build and Deploy / Build plants worker image (push) Has been skipped
Build and Deploy / Build converter worker image (push) Has been skipped
Build and Deploy / Deploy postgres (push) Has been skipped
Build and Deploy / Build API image (push) Has been skipped
Build and Deploy / Build media worker image (push) Has been skipped
Build and Deploy / Build ML worker image (push) Has been skipped
Build and Deploy / Build people worker image (push) Has been skipped
Build and Deploy / Build web image (push) Has been skipped
Build and Deploy / Deploy API (push) Has been skipped
Build and Deploy / Deploy web (push) Has been skipped
Build and Deploy / Deploy media worker (push) Has been skipped
Build and Deploy / Deploy ML worker (push) Has been skipped
Build and Deploy / Deploy plants worker (push) Has been skipped
Build and Deploy / Deploy people worker (push) Has been skipped
Build and Deploy / Deploy converter worker (push) Has been skipped
Build and Deploy / Clean Docker build artifacts (push) Has been skipped
Build and Deploy / Run scoped PR tests (push) Successful in 9m48s
230245c32c
Insert-only occurrence writes (Stash away decisions, shadow auto-accept) treated any row occupying the unique occurrence key as a lost insert race. A state='superseded' zombie row — a face released by V124 supersede while the old row stayed on an archived/dissolved instance — therefore made every "Stash away" on such a face 409 ("Candidate changed while accepting") and routed shadow auto-accepts into lost_insert_race rejection spam; ~8,166 zombie rows existed in prod.

Revive a superseded row in place before the insert: same id, re-pointed to the target instance, state='accepted', accepted_at=now, this candidate's provenance and the path's assignment_basis (user_confirmed for owner decisions, auto_assigned for the stash shadow auto-accept). The revive counts as a written occurrence and publishes the occurrence event, no extra review-decision rows. Rows in state='accepted' stay no-steal conflicts (ON CONFLICT DO NOTHING); 'removed' rows stay untouched.

H2 parity: the KEY-form MERGE (which blindly replaced live rows) is replaced by MERGE ... USING with a matched-update restricted to state='superseded'.

Tests: PG stash revive + shadow auto-accept revive (Spec045StashTentativePostgresTest), H2 revive + H2 no-steal parity (ClassInstanceDiscoveryServiceTest); the zero-write overlap regression now uses a 'removed' conflict so it stays on the lost-insert-race branch.
docs(known-issues): reference PR#887 for ISSUE-072
All checks were successful
Wiki Sync / Sync wiki from docs (push) Has been skipped
Build and Deploy / Detect changed services and test scopes (push) Successful in 5s
Build and Deploy / Android unit tests and debug APK (push) Has been skipped
Build and Deploy / Smoke API and media worker (push) Has been skipped
Build and Deploy / Smoke web (push) Has been skipped
Build and Deploy / Smoke ML worker (push) Has been skipped
Build and Deploy / Smoke people worker (push) Has been skipped
Build and Deploy / Build postgres image (push) Has been skipped
Build and Deploy / Build plants worker image (push) Has been skipped
Build and Deploy / Build converter worker image (push) Has been skipped
Build and Deploy / Deploy postgres (push) Has been skipped
Build and Deploy / Build API image (push) Has been skipped
Build and Deploy / Build media worker image (push) Has been skipped
Build and Deploy / Build ML worker image (push) Has been skipped
Build and Deploy / Build people worker image (push) Has been skipped
Build and Deploy / Build web image (push) Has been skipped
Build and Deploy / Deploy API (push) Has been skipped
Build and Deploy / Deploy web (push) Has been skipped
Build and Deploy / Deploy media worker (push) Has been skipped
Build and Deploy / Deploy ML worker (push) Has been skipped
Build and Deploy / Deploy plants worker (push) Has been skipped
Build and Deploy / Deploy people worker (push) Has been skipped
Build and Deploy / Deploy converter worker (push) Has been skipped
Build and Deploy / Clean Docker build artifacts (push) Has been skipped
Build and Deploy / Run scoped PR tests (push) Successful in 10m45s
bd8c1e66f3
chris force-pushed hotfix/occurrence-insert-revives-superseded from bd8c1e66f3
All checks were successful
Wiki Sync / Sync wiki from docs (push) Has been skipped
Build and Deploy / Detect changed services and test scopes (push) Successful in 5s
Build and Deploy / Android unit tests and debug APK (push) Has been skipped
Build and Deploy / Smoke API and media worker (push) Has been skipped
Build and Deploy / Smoke web (push) Has been skipped
Build and Deploy / Smoke ML worker (push) Has been skipped
Build and Deploy / Smoke people worker (push) Has been skipped
Build and Deploy / Build postgres image (push) Has been skipped
Build and Deploy / Build plants worker image (push) Has been skipped
Build and Deploy / Build converter worker image (push) Has been skipped
Build and Deploy / Deploy postgres (push) Has been skipped
Build and Deploy / Build API image (push) Has been skipped
Build and Deploy / Build media worker image (push) Has been skipped
Build and Deploy / Build ML worker image (push) Has been skipped
Build and Deploy / Build people worker image (push) Has been skipped
Build and Deploy / Build web image (push) Has been skipped
Build and Deploy / Deploy API (push) Has been skipped
Build and Deploy / Deploy web (push) Has been skipped
Build and Deploy / Deploy media worker (push) Has been skipped
Build and Deploy / Deploy ML worker (push) Has been skipped
Build and Deploy / Deploy plants worker (push) Has been skipped
Build and Deploy / Deploy people worker (push) Has been skipped
Build and Deploy / Deploy converter worker (push) Has been skipped
Build and Deploy / Clean Docker build artifacts (push) Has been skipped
Build and Deploy / Run scoped PR tests (push) Successful in 10m45s
to 9339960a98
All checks were successful
Wiki Sync / Sync wiki from docs (push) Has been skipped
Build and Deploy / Detect changed services and test scopes (push) Successful in 3s
Build and Deploy / Android unit tests and debug APK (push) Has been skipped
Build and Deploy / Smoke API and media worker (push) Has been skipped
Build and Deploy / Smoke web (push) Has been skipped
Build and Deploy / Smoke ML worker (push) Has been skipped
Build and Deploy / Smoke people worker (push) Has been skipped
Build and Deploy / Build postgres image (push) Has been skipped
Build and Deploy / Build plants worker image (push) Has been skipped
Build and Deploy / Build converter worker image (push) Has been skipped
Build and Deploy / Deploy postgres (push) Has been skipped
Build and Deploy / Build API image (push) Has been skipped
Build and Deploy / Build media worker image (push) Has been skipped
Build and Deploy / Build ML worker image (push) Has been skipped
Build and Deploy / Build people worker image (push) Has been skipped
Build and Deploy / Build web image (push) Has been skipped
Build and Deploy / Deploy API (push) Has been skipped
Build and Deploy / Deploy web (push) Has been skipped
Build and Deploy / Deploy media worker (push) Has been skipped
Build and Deploy / Deploy ML worker (push) Has been skipped
Build and Deploy / Deploy plants worker (push) Has been skipped
Build and Deploy / Deploy people worker (push) Has been skipped
Build and Deploy / Deploy converter worker (push) Has been skipped
Build and Deploy / Clean Docker build artifacts (push) Has been skipped
Build and Deploy / Run scoped PR tests (push) Successful in 7m24s
2026-09-26 00:18:03 +02:00
Compare
chris force-pushed hotfix/occurrence-insert-revives-superseded from 9339960a98
All checks were successful
Wiki Sync / Sync wiki from docs (push) Has been skipped
Build and Deploy / Detect changed services and test scopes (push) Successful in 3s
Build and Deploy / Android unit tests and debug APK (push) Has been skipped
Build and Deploy / Smoke API and media worker (push) Has been skipped
Build and Deploy / Smoke web (push) Has been skipped
Build and Deploy / Smoke ML worker (push) Has been skipped
Build and Deploy / Smoke people worker (push) Has been skipped
Build and Deploy / Build postgres image (push) Has been skipped
Build and Deploy / Build plants worker image (push) Has been skipped
Build and Deploy / Build converter worker image (push) Has been skipped
Build and Deploy / Deploy postgres (push) Has been skipped
Build and Deploy / Build API image (push) Has been skipped
Build and Deploy / Build media worker image (push) Has been skipped
Build and Deploy / Build ML worker image (push) Has been skipped
Build and Deploy / Build people worker image (push) Has been skipped
Build and Deploy / Build web image (push) Has been skipped
Build and Deploy / Deploy API (push) Has been skipped
Build and Deploy / Deploy web (push) Has been skipped
Build and Deploy / Deploy media worker (push) Has been skipped
Build and Deploy / Deploy ML worker (push) Has been skipped
Build and Deploy / Deploy plants worker (push) Has been skipped
Build and Deploy / Deploy people worker (push) Has been skipped
Build and Deploy / Deploy converter worker (push) Has been skipped
Build and Deploy / Clean Docker build artifacts (push) Has been skipped
Build and Deploy / Run scoped PR tests (push) Successful in 7m24s
to 3b7a2a995b
All checks were successful
Wiki Sync / Sync wiki from docs (push) Has been skipped
Build and Deploy / Detect changed services and test scopes (push) Successful in 3s
Build and Deploy / Android unit tests and debug APK (push) Has been skipped
Build and Deploy / Smoke API and media worker (push) Has been skipped
Build and Deploy / Smoke web (push) Has been skipped
Build and Deploy / Smoke ML worker (push) Has been skipped
Build and Deploy / Smoke people worker (push) Has been skipped
Build and Deploy / Build postgres image (push) Has been skipped
Build and Deploy / Build plants worker image (push) Has been skipped
Build and Deploy / Build converter worker image (push) Has been skipped
Build and Deploy / Deploy postgres (push) Has been skipped
Build and Deploy / Build API image (push) Has been skipped
Build and Deploy / Build media worker image (push) Has been skipped
Build and Deploy / Build ML worker image (push) Has been skipped
Build and Deploy / Build people worker image (push) Has been skipped
Build and Deploy / Build web image (push) Has been skipped
Build and Deploy / Deploy API (push) Has been skipped
Build and Deploy / Deploy web (push) Has been skipped
Build and Deploy / Deploy media worker (push) Has been skipped
Build and Deploy / Deploy ML worker (push) Has been skipped
Build and Deploy / Deploy plants worker (push) Has been skipped
Build and Deploy / Deploy people worker (push) Has been skipped
Build and Deploy / Deploy converter worker (push) Has been skipped
Build and Deploy / Clean Docker build artifacts (push) Has been skipped
Build and Deploy / Run scoped PR tests (push) Successful in 10m4s
2026-09-26 00:52:37 +02:00
Compare
chris force-pushed hotfix/occurrence-insert-revives-superseded from 3b7a2a995b
All checks were successful
Wiki Sync / Sync wiki from docs (push) Has been skipped
Build and Deploy / Detect changed services and test scopes (push) Successful in 3s
Build and Deploy / Android unit tests and debug APK (push) Has been skipped
Build and Deploy / Smoke API and media worker (push) Has been skipped
Build and Deploy / Smoke web (push) Has been skipped
Build and Deploy / Smoke ML worker (push) Has been skipped
Build and Deploy / Smoke people worker (push) Has been skipped
Build and Deploy / Build postgres image (push) Has been skipped
Build and Deploy / Build plants worker image (push) Has been skipped
Build and Deploy / Build converter worker image (push) Has been skipped
Build and Deploy / Deploy postgres (push) Has been skipped
Build and Deploy / Build API image (push) Has been skipped
Build and Deploy / Build media worker image (push) Has been skipped
Build and Deploy / Build ML worker image (push) Has been skipped
Build and Deploy / Build people worker image (push) Has been skipped
Build and Deploy / Build web image (push) Has been skipped
Build and Deploy / Deploy API (push) Has been skipped
Build and Deploy / Deploy web (push) Has been skipped
Build and Deploy / Deploy media worker (push) Has been skipped
Build and Deploy / Deploy ML worker (push) Has been skipped
Build and Deploy / Deploy plants worker (push) Has been skipped
Build and Deploy / Deploy people worker (push) Has been skipped
Build and Deploy / Deploy converter worker (push) Has been skipped
Build and Deploy / Clean Docker build artifacts (push) Has been skipped
Build and Deploy / Run scoped PR tests (push) Successful in 10m4s
to 206995d20e
All checks were successful
Wiki Sync / Sync wiki from docs (push) Has been skipped
Build and Deploy / Detect changed services and test scopes (push) Successful in 3s
Build and Deploy / Android unit tests and debug APK (push) Has been skipped
Build and Deploy / Smoke API and media worker (push) Has been skipped
Build and Deploy / Smoke web (push) Has been skipped
Build and Deploy / Smoke ML worker (push) Has been skipped
Build and Deploy / Smoke people worker (push) Has been skipped
Build and Deploy / Build postgres image (push) Has been skipped
Build and Deploy / Build plants worker image (push) Has been skipped
Build and Deploy / Build converter worker image (push) Has been skipped
Build and Deploy / Deploy postgres (push) Has been skipped
Build and Deploy / Build API image (push) Has been skipped
Build and Deploy / Build media worker image (push) Has been skipped
Build and Deploy / Build ML worker image (push) Has been skipped
Build and Deploy / Build people worker image (push) Has been skipped
Build and Deploy / Build web image (push) Has been skipped
Build and Deploy / Deploy API (push) Has been skipped
Build and Deploy / Deploy web (push) Has been skipped
Build and Deploy / Deploy media worker (push) Has been skipped
Build and Deploy / Deploy ML worker (push) Has been skipped
Build and Deploy / Deploy plants worker (push) Has been skipped
Build and Deploy / Deploy people worker (push) Has been skipped
Build and Deploy / Deploy converter worker (push) Has been skipped
Build and Deploy / Clean Docker build artifacts (push) Has been skipped
Build and Deploy / Run scoped PR tests (push) Successful in 7m15s
2026-09-26 01:12:44 +02:00
Compare
chris merged commit 542239d67d into main 2026-09-26 01:23:55 +02:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
chris/memories!887
No description provided.