Skip to content

Fix Entity/Observation lock-order inversion between indexing and accepted writes #1199

Closed

Description

@phernandez

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:

  1. File indexing through EntityService.update_entity_and_observations deletes/replaces Observation rows, then applies and flushes Entity fields and checksum: Observation -> Entity.
  2. Accepted note replacement prepares and flushes the Entity first, then persist_accepted_note_snapshot / _replace_accepted_note_graph calls ObservationRepository.replace_accepted_observations: Entity -> Observation.
  3. PostgreSQL detects the cycle and aborts one transaction.
  4. 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

  1. added
    bug
    production
    on Aug 6, 2026
  2. claude commented on Aug 6, 2026

    @claude
    Contributor

    Claude finished @phernandez's task in 1m 46s —— View job


    Triage complete

    • Read issue body and repo CLAUDE.md guidance
    • Classify type, priority, component, complexity
    • Check for duplicate issues
    • Apply labels
    • Post triage summary

    Classification

    • Type: Bug — a real correctness/reliability defect (transaction lock-order inversion), not a feature request.
    • Priority: Critical — production evidence (6 Logfire fingerprints across releases #2256–#2348) shows worker retry exhaustion and customer-visible 503s on note edits; this is data-path reliability, not cosmetic.
    • Component: Database/sync (Core persistence layer) — the conflict is between EntityService.update_entity_and_observations (indexing path, in src/basic_memory/services/entity_service.py) and persist_accepted_note_snapshot / _replace_accepted_note_graph → ObservationRepository.replace_accepted_observations (accepted-write path, in src/basic_memory/indexing/accepted_note_write_runner.py / accepted_note_mutation_runner.py and src/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.
    • Complexity: Complex — this needs one explicit, globally-proven Entity → Observation lock order applied consistently across indexing, accepted writes, and reconciliation, plus a real Postgres concurrency regression test, while preserving accepted-note CAS/atomicity and the existing Entity → NoteContent ordering from Prevent Entity-NoteContent lock-order deadlocks during materialization #1187. Cross-cutting, not a local fix.

    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 bug and production, which correctly capture type and severity signal. I attempted to add finer-grained critical / database / complexity labels per the triage taxonomy, but none of those labels currently exist in this repo's label set (confirmed via edit-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-clarification status applies.

    No implementation, file edits, or branch changes were made, per triage scope.

  3. added this to the v0.23 milestone on Aug 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugproduction

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions