Repository navigation
Description
Summary
Basic Memory's repositories still own their own SQLAlchemy sessions for most high-level methods. That makes it awkward to orchestrate one logical write transaction across multiple repositories.
This is showing up clearly in Basic Memory Cloud's accepted-write path for note materialization, where the service needs one shared tenant DB transaction to:
- resolve
Project - load or create
Entity - load or upsert
NoteContent - flush related ORM changes together
- commit once
Because the current repository methods open their own sessions, cloud has to keep several transaction-scoped query helpers local in the service layer instead of reusing the repository classes.
Current pain
Examples:
ProjectRepository.get_by_external_id()opens its own sessionEntityRepository.get_by_external_id()opens its own sessionEntityRepository.get_by_file_path()opens its own sessionNoteContentRepository.get_by_entity_id()opens its own sessionNoteContentRepository.upsert()opens its own session
That makes it hard to compose these operations safely inside an existing caller-owned transaction.
Proposed direction
Add transaction-friendly repository methods that accept an existing AsyncSession.
For example:
ProjectRepository.get_by_external_id(session, external_id)EntityRepository.get_by_external_id(session, external_id, *, load_relations=True)EntityRepository.get_by_file_path(session, file_path, *, load_relations=True)NoteContentRepository.get_by_entity_id(session, entity_id)NoteContentRepository.upsert(..., session=...)or a dedicatedupsert_accepted_state(session, ...)
The service layer should own transaction boundaries. Repositories should provide query/persistence helpers that can either:
- work with a caller-owned session, or
- provide a convenience wrapper that opens a session when the caller does not care.
Why this matters
This lets orchestration-heavy flows stay simple and correct:
- accepted note writes in cloud
- directory deletes / other multi-step content mutations
- any future flow that needs multiple repositories to participate in one commit
It also reduces duplicated query helpers in cloud code.
Scope
Start small. We do not need to redesign every repository at once.
Suggested first pass:
ProjectRepository.get_by_external_id(session, ...)EntityRepository.get_by_external_id(session, ...)EntityRepository.get_by_file_path(session, ...)NoteContentRepository.get_by_entity_id(session, ...)- one note-content upsert/update method that can participate in an existing session
Then update callers to use the shared-session variants where they are already orchestrating a transaction.
Out of scope
- changing user-facing API behavior
- changing note materialization semantics
- broad repository cleanup unrelated to transaction ownership
Acceptance criteria
- caller-owned transaction flows can use repository methods without opening nested sessions
- existing convenience behavior can remain for simple call sites if useful
- cloud can delete its local transaction-scoped lookup/upsert helpers once the new repository API is available
Activity
claude commentedon Apr 16, 2026
Claude finished @phernandez's task in 1m 40s —— View job
Triage Analysis
enhancement,cloud)Issue Triage: #750 — Refactor repositories to support caller-owned AsyncSession flows
Type: Enhancement (architectural improvement)
Priority: Medium-High — not blocking existing local functionality, but actively impeding cloud service layer correctness
Component: Database/Repository layer + Cloud
Complexity: Complex — multi-file refactor with test coverage required
Code Analysis
I reviewed the current repository implementations to confirm the pain described in the issue.
Current session ownership patterns:
ProjectRepository.get_by_external_id()find_one()→execute_query()EntityRepository.get_by_external_id()_find_one_by_query()→execute_query()EntityRepository.get_by_file_path()NoteContentRepository.get_by_entity_id()find_by_id()which callsscoped_sessionNoteContentRepository.upsert()async with db.scoped_session(...))The pattern already exists for low-level methods:
select_by_id(session, ...),select_by_ids(session, ...), andget_by_file_paths(session, ...)already accept a caller-providedAsyncSession. The gap is that the higher-level lookup methods (by external_id, by file_path, etc.) don't offer this variant.Assessment
No duplicates found. This is a new issue.
Why this is medium-high priority:
The cloud note materialization path needs a shared transaction across
ProjectRepository,EntityRepository, andNoteContentRepository. Without caller-owned session support, the cloud service must duplicate query helpers locally — a code smell that will diverge from the tested repository code over time.Why this is complex:
session: AsyncSession | None = None) or dedicated_with_session(session, ...)variantsSuggested API design (from the issue):
The second pattern (optional
sessionkwarg) is less disruptive to existing call sites.Recommended approach:
sessionkeyword argument — keeps backward compatibility and avoids combinatorial overloadsLabels applied:
enhancement,cloud