fix(api): insert-only occurrence writes revive superseded rows (stash 409) #887
Loading…
Reference in a new issue
No description provided.
Delete branch "hotfix/occurrence-insert-revives-superseded"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Root cause
Since #876 (spec 045 insert-only stash decisions),
insertOccurrenceNoStealrelies on the unique indexclass_instance_occurrences (domain_key, owner_id, occurrence_type, occurrence_key)+ON CONFLICT DO NOTHING. A row instate='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,requireOccurrenceWritesfails andacceptPendingCandidatesIntoExistingInstancereturns null:lost_insert_racereview-decision spamProd 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: targetedUPDATE ... WHERE domain_key/owner_id/occurrence_type/occurrence_key AND state='superseded'setsclass_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=nowand counts as a written occurrence (same row id preserved, no duplicate).state='accepted'remain no-steal conflicts (ON CONFLICT DO NOTHING);removedrows stay untouched.MERGE KEY(blind replace) replaced withMERGE ... USINGwhose matched update is restricted tostate='superseded', so H2 no longer steals live rows either.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 oflost_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.pgZeroWriteAutoAcceptLeavesLockedOverlapUntouchednow 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.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.bd8c1e66f39339960a989339960a983b7a2a995b3b7a2a995b206995d20e