refactor(core): use explicit sessions in repositories#1002
Conversation
Signed-off-by: phernandez <paul@basicmachines.co>
Signed-off-by: phernandez <paul@basicmachines.co>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: bb65be817b
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| rel.target, | ||
| strict=True, | ||
| load_relations=False, | ||
| session=active_session, |
There was a problem hiding this comment.
Don't share one AsyncSession across relation tasks
When a note has multiple relations, the surrounding asyncio.gather launches one resolve_link task per relation but now passes the same active_session into every task. AsyncSession is not safe for concurrent operations (asyncpg/Postgres rejects overlapping commands), and because this gather uses return_exceptions=True those failures are then treated as unresolved links, so MCP/API writes can persist forward references even when targets exist. Resolve these lookups sequentially on the transaction session or give each concurrent task its own session.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in a6ad20a: relation target resolution now runs sequentially inside the active transaction session, so one AsyncSession is no longer shared across concurrent tasks. I also narrowed exception handling so cancellation still propagates. Verified with the focused upsert tests, typecheck, and the broad testmon run aside from the local OpenAI quota-only semantic case.
| scan_coro = self._scan_directory_modified_since( | ||
| directory, project.last_scan_timestamp | ||
| ) | ||
| async with self._session_scope() as session: |
There was a problem hiding this comment.
Narrow the scan database session scope
This session now wraps the project lookup, _quick_count_files, the full/incremental filesystem scan, and per-file stat/checksum work before it exits. On large projects those non-DB operations can take seconds or minutes; after the first repository query the connection and transaction stay open for the whole scan, so concurrent watch/API syncs can exhaust the pool or leave Postgres idle-in-transaction. Scope sessions only around the project/entity repository calls and do filesystem work outside them.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in a6ad20a: scan now snapshots project/entity/checksum/file-path state through short repository sessions, then releases the DB session before file counting, filesystem scans, stat checks, and checksum work. Verified with sync incremental/scan/watch tests in the broad testmon run.
Signed-off-by: phernandez <paul@basicmachines.co>
Signed-off-by: phernandez <paul@basicmachines.co>
Signed-off-by: phernandez <paul@basicmachines.co>
Signed-off-by: phernandez <paul@basicmachines.co>
Summary
Closes #750
Verification