Derive PostgreSQL presence from connection leases - #21
Conversation
Summary by CodeRabbit
WalkthroughPostgreSQL schema v7 adds instance-generation fencing to connection leases. Lease lifecycle operations now emit exact presence transitions, reject stale ownership, and support bounded crash sweeping with typing cleanup before departures. Migration tests cover generation backfills. ChangesGeneration-Fenced Presence
Estimated code review effort: 4 (Complex) | ~60 minutes Sequence Diagram(s)sequenceDiagram
participant Client
participant PostgresConnectionRegistry
participant PostgreSQL
participant TypingState
participant PresenceObserver
Client->>PostgresConnectionRegistry: acquire or release connection
PostgresConnectionRegistry->>PostgreSQL: validate instance generation
PostgreSQL-->>PostgresConnectionRegistry: lease and active count
PostgresConnectionRegistry->>PresenceObserver: emit presence transition
PostgresConnectionRegistry->>PostgreSQL: reap bounded stale leases
PostgreSQL->>TypingState: stop typing
PostgreSQL->>PresenceObserver: emit departure transition
Possibly related PRs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 4
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@CHANGELOG.md`:
- Line 33: Update the changelog entry for PYSEC-2026-3552 to describe the
complete affected range as cryptography>=44.0.0,<50.0.0, while retaining that
the asymmetric-authentication, test, and development dependency groups now
require cryptography>=50,<51.
In `@samsarix_chat_engine/postgres_connections.py`:
- Around line 134-136: Define the module-level _ADMISSION_SWEEP_LIMIT constant
near the other constants and replace the limit=1_000 argument in try_acquire’s
_delete_expired call with this bound. Leave the background reap_expired flow
responsible for deleting any remaining expired leases.
In `@samsarix_chat_engine/postgres.py`:
- Around line 494-501: Update the migration sequence around the connection lease
index creation to first drop public.samsarix_connection_leases_instance, then
create samsarix_connection_leases_instance_generation. Preserve the existing IF
NOT EXISTS behavior for the replacement index.
In `@tests/test_postgres_presence.py`:
- Around line 164-179: Update the reap_expired test to capture the transitions
returned by a batched call using limit=10, then assert their connection_id
values are in sorted order. Keep the existing event-type and active-connection
assertions unchanged.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: e3e4d442-add8-44bc-b146-179505c562af
📒 Files selected for processing (10)
CHANGELOG.mdROADMAP.mddocs/MULTI_INSTANCE_ARCHITECTURE.mdpyproject.tomlsamsarix_chat_engine/postgres.pysamsarix_chat_engine/postgres_connections.pysamsarix_chat_engine/postgres_typing.pytests/test_postgres_presence.pytests/test_postgres_store.pytests/test_postgres_typing.py
What changed
presence.joinedandpresence.leftevents from committed connection lease changescryptography50.x afterPYSEC-2026-3552identifiedcryptography>=44,<50as vulnerableWhy
Stable instance names can be reused after a process lease expires. Instance-ID checks alone could make old socket rows appear live again, and deleting those rows during registration lost the information needed to converge presence after a crash. A per-registration UUID generation makes old rows immediately non-live while allowing a bounded sweeper to produce the missing transitions transactionally.
The first CI attempt also surfaced a newly published vulnerability in the resolved cryptography release; upstream 50.0.0 is fixed and supports the full project Python matrix.
Impact
This is internal PostgreSQL topology groundwork for v0.13. Public presence and typing fan-out remain gated until sender exclusion and application wiring are proven. Existing schema-v6 rows are backfilled transactionally with the matching owner generation, and their superseded index is removed.
Validation
cryptography 50.0.04cb9454All current CI jobs pass at head
4cb945401ab2dd66c608552902c1db6f1eefc87c.