fix(consolidation): guard _execute_update_action against FK violation on 0-row UPDATE - #3094
Open
ai-ag2026 wants to merge 1 commit into
Open
fix(consolidation): guard _execute_update_action against FK violation on 0-row UPDATE#3094ai-ag2026 wants to merge 1 commit into
ai-ag2026 wants to merge 1 commit into
Conversation
… on 0-row UPDATE If the observation row is concurrently deleted or invalidated, the UPDATE in _execute_update_action matches 0 rows — and the observation_history INSERT that follows carries an observation_id FK onto memory_units, so appending history for the now-missing row raises ForeignKeyViolationError. Because integrity violations are (correctly) not retried, the consolidation operation is marked failed for a row that simply no longer exists. Capture the UPDATE status in the SQL branch and bail out before writing history when 0 rows matched, logging at debug. The store branch is an upsert and cannot hit the 0-row case. Tests: mock-level coverage in test_integrity_violation_not_retried.py — 2 tests go red without the guard and green with it. (The three embedded-postgres tests in that file are environment-bound and behave identically on pristine main.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
If the observation row is concurrently deleted or invalidated, the
UPDATEin
_execute_update_actionmatches 0 rows — and theobservation_historyINSERT that follows carries an
observation_idFK ontomemory_units, soappending history for the now-missing row raises
ForeignKeyViolationError.Because integrity violations are (correctly) not retried, the consolidation
operation is marked failed for a row that simply no longer exists.
This captures the
UPDATEstatus in the SQL branch and bails out beforewriting history when 0 rows matched (debug log). The store branch is an
upsert and cannot hit the 0-row case, so it needs no guard.
Why
Hit in production under concurrent curation: consolidation raced an
invalidation, and the resulting FK violation marked the whole operation
failed. Running with this guard since 2026-07-16.
Validation
Mock-level tests in
tests/test_integrity_violation_not_retried.py.Verified both directions: without the guard two tests go red
(
2 failed, 2 passed); with it4 passed. The file's threeembedded-postgres tests could not run in our sandbox (embedded PG cannot
create TCP sockets there) — they error identically on pristine
main, sothat is an environment bound, not an effect of this change.