Skip to content

fix(state-pg): setIfNotExists TTL expiry - #636

Merged
bensabic merged 2 commits into
vercel:mainfrom
Zygimantass:codex/state-pg-set-if-not-exists-ttl
Jul 15, 2026
Merged

bensabic merged 2 commits into
vercel:mainfrom
Zygimantass:codex/state-pg-set-if-not-exists-ttl

Conversation

@Zygimantass

Copy link
Copy Markdown
Contributor

Summary

Fixes the Postgres state adapter so setIfNotExists() can claim a cache key whose existing row has expired.

Previously the method used ON CONFLICT DO NOTHING, so an expired row in chat_state_cache still blocked acquisition until a separate cleanup deleted it. That diverged from the memory adapter behavior and from the expected lease semantics for cache-backed coordination.

The new query keeps active rows protected, but replaces rows whose expires_at is in the past.

Validation

  • pnpm --filter @chat-adapter/state-pg test
  • pnpm --filter @chat-adapter/state-pg typecheck
  • pnpm exec ultracite check packages/state-pg/src/index.ts packages/state-pg/src/index.test.ts

@vercel

vercel Bot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

@Zygimantass is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

@bensabic
bensabic force-pushed the codex/state-pg-set-if-not-exists-ttl branch from d973328 to 0870b6d Compare July 15, 2026 03:05
@bensabic
bensabic marked this pull request as ready for review July 15, 2026 03:06
@bensabic
bensabic requested a review from a team as a code owner July 15, 2026 03:06
@bensabic bensabic changed the title [codex] fix state-pg setIfNotExists TTL expiry fix(state-pg): setIfNotExists TTL expiry Jul 15, 2026
@bensabic
bensabic merged commit d88789c into vercel:main Jul 15, 2026
11 of 13 checks passed
patrick-chinchill added a commit to Chinchill-AI/chat-sdk-python that referenced this pull request Sep 30, 2026
…ion-owned schemas (#240)

PostgresStateAdapter.set_if_not_exists now uses upstream's conditional upsert (ON CONFLICT DO UPDATE ... WHERE expires_at <= now() RETURNING), so expired dedupe/lease rows are reclaimed atomically; live and permanent rows are never overwritten.
Ports upstream d88789c9 (vercel/chat#636, chat@4.35.0) and ea025af7 (vercel/chat#913, chat@4.41.0).
New opt-in auto_create_schema=False (keyword-only; default True) skips DDL and runs a read-only table/privilege/sequence probe, raising chat_sdk.StateSchemaError on a missing table or grant. New exports: POSTGRES_SCHEMA_STATEMENTS, StateSchemaError; README documents the migration SQL.
Divergences (docs/UPSTREAM_SYNC.md): StateSchemaError(ChatError) instead of plain Error; connect() serialized on an asyncio.Lock (queued callers retry); adapter-owned asyncpg pool closed on a failed connect() to avoid leaking 10 connections per retry.
Consumer impact: Postgres-backed dedupe and leases recover after expiry without cleanup; no schema change; defaults unchanged.
Tests: mock pool no longer reclaims under DO NOTHING; injectable mock clock; opt-in live suite via POSTGRES_TEST_URL.
Closes #240
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants