Repository navigation
Description
Summary
Production file indexing and accepted DB-first note writes acquire the same Entity and Observation rows in opposite orders. Under concurrent work on one note this produces PostgreSQL deadlocks, exhausts worker retries, and can return a customer-visible 503 from note edits.
This needs one explicit Core-owned lock order. Retrying harder or suppressing the Logfire fingerprints would leave the correctness defect intact.
Production evidence
Observed release: Cloud release 837b8c258ddb9ca78d0d9d897cec9202f7650c28, pinning Core revision de4589d24155332002709f5529c9bdd4937861a4.
Logfire fingerprints:
- #2348 —
2ed3c94b-9645-4df1-b1f5-c63b6026954d - #2339 —
5c68fa3f-75a3-4d21-be74-a9abdb509525 - #2328 —
8c14efe1-626f-4a46-bf83-3b227739885a - #2323 —
c12ec416-9c97-40f3-95dd-cc404db09753 - #2310 —
eb1787eb-5875-4d7f-a53d-8cfe0d41fb18 - #2256 —
5286205d-fadf-4760-b3b0-38b255212eb9
Representative traces:
The trigger is concurrent indexing and an accepted edit for the same entity. Production correlation confirmed that both paths touched the same entity; identifying tenant and note values are intentionally omitted.
Causal chain
The two transactions invert their shared row order:
- File indexing through
EntityService.update_entity_and_observationsdeletes/replaces Observation rows, then applies and flushes Entity fields and checksum: Observation -> Entity. - Accepted note replacement prepares and flushes the Entity first, then
persist_accepted_note_snapshot/_replace_accepted_note_graphcallsObservationRepository.replace_accepted_observations: Entity -> Observation. - PostgreSQL detects the cycle and aborts one transaction.
- Worker victims may exhaust five retries; request victims surface as 5xx.
Core issue #1187 / PR #1193 established Entity -> NoteContent ordering. That fix is present in the observed Core revision, but it does not cover Observation rows.
Required outcome
Establish and document one transaction lock order for Entity and Observation mutations across accepted writes, file indexing, and reconciliation. The likely direction is to acquire/flush or explicitly lock the Entity before replacing observations in the indexing path, while preserving accepted-note compare-and-swap and atomic graph replacement.
The solution belongs in Core so local and hosted runtimes share the same invariant.
Non-solutions
- Increasing deadlock retries
- Lowering exception severity or suppressing the fingerprints
- Broadly serializing all tenant work
- Weakening accepted-note checksum/CAS behavior
Acceptance criteria
- A real PostgreSQL concurrency regression runs an accepted note edit and file index against the same entity and both transactions terminate without deadlock.
- Every Core path that mutates both resources follows one explicit Entity -> Observation order, or an equivalent globally proven order.
- The accepted database content is not reverted by a stale file/index operation.
- Observation replacement remains atomic with the owning entity update.
- The request path does not return 5xx and the worker path does not reach terminal failure under the regression.
- Operations on different entities can still proceed concurrently; no project- or tenant-wide mutex is introduced.
- Existing Entity -> NoteContent ordering and accepted-note CAS tests remain green.
Activity
claude commentedon Aug 6, 2026
Claude finished @phernandez's task in 1m 46s —— View job
Triage complete
Classification
EntityService.update_entity_and_observations(indexing path, insrc/basic_memory/services/entity_service.py) andpersist_accepted_note_snapshot/_replace_accepted_note_graph→ObservationRepository.replace_accepted_observations(accepted-write path, insrc/basic_memory/indexing/accepted_note_write_runner.py/accepted_note_mutation_runner.pyandsrc/basic_memory/repository/observation_repository.py). All referenced files/functions exist in the current tree, so the causal chain in the report is verifiable against the code, not speculative.Duplicate check: Not a duplicate. Closest related issue is #1187 ("Prevent Entity-NoteContent lock-order deadlocks during materialization", closed via #1193), which fixed Entity ↔ NoteContent ordering but — as this report correctly notes — did not cover Entity ↔ Observation ordering. This issue is the natural follow-on, not a repeat.
Labels: This issue already carries
bugandproduction, which correctly capture type and severity signal. I attempted to add finer-grainedcritical/database/complexitylabels per the triage taxonomy, but none of those labels currently exist in this repo's label set (confirmed viaedit-issue-labels.sh, which silently drops unknown labels — verified against #1187 and #1199 label data). I did not create new labels, since label-taxonomy creation is outside triage scope; leaving that decision to a maintainer.Reproduction/clarification: Not needed. The report already includes production trace links, Logfire fingerprints, the exact code paths involved, explicit non-solutions, and concrete acceptance criteria — it's implementation-ready. No
needs-reproduction/needs-clarificationstatus applies.No implementation, file edits, or branch changes were made, per triage scope.