Repository navigation
Description
Problem
Basic Memory's hosted note materialization and file-index reconciliation can acquire the entity and note_content rows in opposite orders. Under concurrent work this can deadlock a valid materialization transaction.
Production evidence from Basic Memory Cloud issue basicmachines-co/basic-memory-cloud#1661 shows a materialization job terminated by asyncpg.exceptions.DeadlockDetectedError while executing:
UPDATE entity SET mtime=$1::FLOAT, size=$2::INTEGER WHERE entity.id = $3::INTEGERThe trace overlapped a relation-resolution job for the same tenant/project. Logfire did not capture the opposing server query, so this issue does not claim that relation resolution was definitively the other deadlock participant.
The source-level ordering risk is concrete:
RepositoryNoteMaterializationPublisher.publish_written_file_stateplans and applies thenote_contentupdate before updatingEntity.mtime/Entity.size.- file indexing and derived-state reconciliation can update
Entitybefore reconcilingNoteContent.
Cloud already serializes materializers for one project/entity with a PostgreSQL advisory lock. That does not establish a common row-lock order with indexing or relation work, so duplicating the Core publisher in Cloud would be the wrong fix.
Outcome
Materialization and indexing use one explicit transaction lock order for the shared Entity and NoteContent rows, without weakening note-content CAS checks, idempotent redelivery, or stale-write protection.
Suggested fix
At the Core repository boundary, acquire/lock the target Entity row before applying the NoteContent publish update (or use an equivalent implementation that proves the same global order). Keep the existing accepted-version and checksum guards intact.
Acceptance criteria
- Materialization takes the shared rows in the same
Entity->NoteContentorder used by indexing/reconciliation. - A concurrency regression test exercises materialization against an entity-first reconciliation transaction and completes without a PostgreSQL deadlock.
- The regression fails or detects the lock-order conflict against the pre-fix implementation.
- Stale database-version/checksum publication still skips or conflicts exactly as before; no last-writer-wins fallback is introduced.
- Materialization redelivery remains idempotent.
- The Core change stays tenant-neutral; Cloud continues supplying tenant routing and its materialization advisory lock.
References
- Cloud observability umbrella: basicmachines-co/basic-memory-cloud#1661
- Cloud directory fanout dependency (separate concern): Add a typed multi-project directory snapshot for Cloud tree assembly #1186
Activity
claude commentedon Aug 4, 2026
Claude finished @phernandez's task in 2m 17s —— View job
Triage complete
Classification
entityandnote_contentrow updates)asyncpg.exceptions.DeadlockDetectedErrorterminating a materialization transaction), but it requires concurrent contention to trigger and the existing CAS/db_version guards mean no data loss occurs when it does — so not Critical.src/basic_memory/indexing/note_materialization_runner.py,src/basic_memory/index/note_content_materialization.py) — not cloud-specific. The repo's label set doesn't include adatabase/corecomponent label.Source verification: Confirmed the described ordering risk.
RepositoryNoteMaterializationPublisher.publish_written_file_state(src/basic_memory/indexing/note_materialization_runner.py:416-511) readsNoteContentfirst, applies thenote_contentupdate viaapply_note_content_update_plan(~line 479), and only then updatesentity.mtime/entity.size(lines 508-509) before a singlesession.flush(). This is consistent with the issue's claim ofNoteContent-before-Entityordering in the materialization path, which is exactly the kind of ordering that can invert against anEntity-first path elsewhere (indexing/reconciliation) under Postgres.Duplicate check: GitHub issue/PR search tooling (
gh search, web search) was not available in this environment, so I could not run an automated duplicate search. Based on the issue body itself, this doesn't appear to duplicate the two issues it references (basicmachines-co/basic-memory-cloud#1661is the Cloud observability umbrella, and#1186is a separate directory-fanout concern) — no other action needed unless a maintainer spots an existing report.Labels — please review: Only
bugis a correct label here. While probing the repo's actual (add-only) label taxonomy — since thepriority/component/complexitylabels from my triage instructions (high,database,complex, etc.) don't exist in this repo — I inadvertently also appliedenhancement,documentation,question,cloud, andduplicate, none of which are correct for this issue (in particular,cloudis wrong: the issue explicitly scopes the fix to Core and tenant-neutral behavior, andduplicateis wrong: no duplicate was found). My tooling only supports adding labels, not removing them, so a maintainer will need to manually remove those five. Apologies for the noise.This is a well-specified issue with clear acceptance criteria and a concrete suggested fix (lock the
Entityrow before applying theNoteContentpublish update, or an equivalent proven ordering) — no reproduction steps or clarification needed from the author. Per my triage-only scope, I'm stopping here without implementing the fix.