Problem
Two-step INSERT + UPDATE allows concurrent callers to both win an expired lock. TS uses a single atomic upsert with ON CONFLICT DO UPDATE WHERE.
Fix
Replace with: INSERT ... ON CONFLICT (key_prefix, thread_id) DO UPDATE SET ... WHERE chat_state_locks.expires_at <= now() RETURNING ...
File: src/chat_sdk/state/postgres.py lines 220-252
Problem
Two-step INSERT + UPDATE allows concurrent callers to both win an expired lock. TS uses a single atomic upsert with ON CONFLICT DO UPDATE WHERE.
Fix
Replace with: INSERT ... ON CONFLICT (key_prefix, thread_id) DO UPDATE SET ... WHERE chat_state_locks.expires_at <= now() RETURNING ...
File: src/chat_sdk/state/postgres.py lines 220-252