fix(postgres): absorb idle pool client errors without full reconnect - #1619
Conversation
pg.Pool idle disconnects are self-healing; re-emitting error with no listeners crashed Bun, and tearing down the pool killed healthy clients. Co-authored-by: Cursor <cursoragent@cursor.com>
|
The latest Agentuity deployment details.
View deployment logs with the Agentuity CLI: |
|
Warning Review limit reachedYou’ve reached a temporary PR review limit under our Fair Usage Limits Policy. Next review available in: 53 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughChangesThe PostgreSQL pool now absorbs idle-client errors without marking the pool disconnected or closing it, conditionally forwards error events, updates callback documentation, and tests continued query usability. ChangesPostgreSQL idle error resilience
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
Comment |
📦 Canary Packages Publishedversion: PackagesInstallAdd to your {
"dependencies": {
"@agentuity/client": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-client-3.1.9-907c351.tgz",
"@agentuity/vite": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-vite-3.1.9-907c351.tgz",
"@agentuity/postgres": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-postgres-3.1.9-907c351.tgz",
"@agentuity/server": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-server-3.1.9-907c351.tgz",
"@agentuity/sandbox": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-sandbox-3.1.9-907c351.tgz",
"@agentuity/migrate": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-migrate-3.1.9-907c351.tgz",
"@agentuity/genesis": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-genesis-3.1.9-907c351.tgz",
"@agentuity/vector": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-vector-3.1.9-907c351.tgz",
"@agentuity/runtime": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-runtime-3.1.9-907c351.tgz",
"@agentuity/core": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-core-3.1.9-907c351.tgz",
"@agentuity/stream": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-stream-3.1.9-907c351.tgz",
"@agentuity/storage": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-storage-3.1.9-907c351.tgz",
"@agentuity/coder": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-coder-3.1.9-907c351.tgz",
"@agentuity/db": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-db-3.1.9-907c351.tgz",
"@agentuity/claude-code": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-claude-code-3.1.9-907c351.tgz",
"@agentuity/analytics": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-analytics-3.1.9-907c351.tgz",
"@agentuity/telemetry": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-telemetry-3.1.9-907c351.tgz",
"@agentuity/drizzle": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-drizzle-3.1.9-907c351.tgz",
"create-agentuity": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/create-agentuity-3.1.9-907c351.tgz",
"@agentuity/keyvalue": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-keyvalue-3.1.9-907c351.tgz",
"@agentuity/webhook": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-webhook-3.1.9-907c351.tgz",
"@agentuity/aigateway": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-aigateway-3.1.9-907c351.tgz",
"@agentuity/cli": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-cli-3.1.9-907c351.tgz",
"@agentuity/email": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-email-3.1.9-907c351.tgz",
"@agentuity/pi": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-pi-3.1.9-907c351.tgz",
"@agentuity/config": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-config-3.1.9-907c351.tgz",
"@agentuity/coder-tui": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-coder-tui-3.1.9-907c351.tgz",
"@agentuity/skills": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-skills-3.1.9-907c351.tgz",
"@agentuity/opencode": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-opencode-3.1.9-907c351.tgz",
"@agentuity/task": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-task-3.1.9-907c351.tgz",
"@agentuity/adapter": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-adapter-3.1.9-907c351.tgz",
"@agentuity/queue": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-queue-3.1.9-907c351.tgz",
"@agentuity/schema": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-schema-3.1.9-907c351.tgz",
"@agentuity/schedule": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-schedule-3.1.9-907c351.tgz",
"@agentuity/api": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-api-3.1.9-907c351.tgz",
"@agentuity/hono": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-hono-3.1.9-907c351.tgz"
}
}Or install directly: bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-client-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-vite-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-postgres-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-server-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-sandbox-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-migrate-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-genesis-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-vector-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-runtime-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-core-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-stream-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-storage-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-coder-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-db-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-claude-code-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-analytics-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-telemetry-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-drizzle-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/create-agentuity-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-keyvalue-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-webhook-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-aigateway-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-cli-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-email-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-pi-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-config-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-coder-tui-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-skills-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-opencode-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-task-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-adapter-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-queue-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-schema-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-schedule-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-api-3.1.9-907c351.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/3.1.9-907c351/agentuity-hono-3.1.9-907c351.tgz |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 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 `@packages/postgres/src/pool.ts`:
- Around line 514-532: Remove the unconditional this._stats.lastDisconnectedAt
update from _handleIdleClientError, since idle-client errors must not alter
pool-level disconnect statistics while the pool remains connected. If diagnostic
tracking is required by the existing stats design, use a dedicated idle-client
error timestamp instead of lastDisconnectedAt; otherwise leave the handler
limited to conditional error emission.
🪄 Autofix (Beta)
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: CHILL
Plan: Pro
Run ID: 05db1ca7-0fc2-46cf-8651-44b03ee2595b
📒 Files selected for processing (3)
packages/postgres/src/pool.tspackages/postgres/src/types.tspackages/postgres/test/pool-compat.test.ts
📜 Review details
⏰ Context from checks skipped due to timeout. (14)
- GitHub Check: Package Installation & Usage Test (node)
- GitHub Check: Agentuity - docs-docs
- GitHub Check: Queue CLI Tests (bun)
- GitHub Check: Queue CLI Tests (node)
- GitHub Check: Package Installation & Usage Test (bun)
- GitHub Check: Framework Demo Tests
- GitHub Check: Postgres SSL Integration Test
- GitHub Check: Testing Apps (local tests)
- GitHub Check: Service Client Smoke Tests
- GitHub Check: Migrate Chain (v1 → v2 → v3)
- GitHub Check: Build
- GitHub Check: Pack & Upload
- GitHub Check: OSV Scanner PR
- GitHub Check: actionlint
🧰 Additional context used
📓 Path-based instructions (7)
packages/postgres/**/*.{ts,tsx,js,jsx}
📄 CodeRabbit inference engine (packages/postgres/AGENTS.md)
packages/postgres/**/*.{ts,tsx,js,jsx}: Use tagged template literals as the primary query interface when using the postgres client
Use StructuredError from@agentuity/corefor error handling
Handle ConnectionClosedError and retryable errors using error instanceof checks and isRetryableError utility
Set preconnect: true in config to establish TCP connection immediately, otherwise connections are established lazily on first query
Use shutdownAll, getClientCount, and hasActiveClients utilities from the global registry for coordinated client lifecycle management
Files:
packages/postgres/test/pool-compat.test.tspackages/postgres/src/types.tspackages/postgres/src/pool.ts
packages/postgres/**/*.{ts,tsx}
📄 CodeRabbit inference engine (packages/postgres/AGENTS.md)
Maintain full TypeScript support with type safety throughout the postgres client
Files:
packages/postgres/test/pool-compat.test.tspackages/postgres/src/types.tspackages/postgres/src/pool.ts
packages/postgres/**/*.test.{ts,tsx,js,jsx}
📄 CodeRabbit inference engine (packages/postgres/AGENTS.md)
Tests require a running PostgreSQL instance and should use
@agentuity/test-utilsfor mocking
Files:
packages/postgres/test/pool-compat.test.ts
**/*.{ts,tsx,js,jsx}
📄 CodeRabbit inference engine (AGENTS.md)
Run
bun run formatusing Biome with tabs (width 3), single quotes, semicolons, lineWidth 100, and trailingCommas es5
Files:
packages/postgres/test/pool-compat.test.tspackages/postgres/src/types.tspackages/postgres/src/pool.ts
**/*.{ts,tsx}
📄 CodeRabbit inference engine (AGENTS.md)
**/*.{ts,tsx}: Use TypeScript in strict mode with ESNext target and bundler moduleResolution
UseStructuredErrorfrom@agentuity/corefor error handling
Files:
packages/postgres/test/pool-compat.test.tspackages/postgres/src/types.tspackages/postgres/src/pool.ts
**/packages/*/test/**/*.{ts,tsx}
📄 CodeRabbit inference engine (AGENTS.md)
**/packages/*/test/**/*.{ts,tsx}: Place tests intest/folder parallel tosrc/directory, never insidesrc/or under__tests__/
Import from../src/in test files
Use@agentuity/test-utilsfor shared mocks in tests
Files:
packages/postgres/test/pool-compat.test.ts
packages/postgres/src/**/*.{ts,tsx}
📄 CodeRabbit inference engine (packages/postgres/AGENTS.md)
Implement exponential backoff with jitter for connection reconnection attempts
Files:
packages/postgres/src/types.tspackages/postgres/src/pool.ts
🧠 Learnings (2)
📚 Learning: 2025-12-21T00:31:41.858Z
Learnt from: jhaynie
Repo: agentuity/sdk PR: 274
File: packages/cli/src/cmd/build/vite/server-bundler.ts:12-41
Timestamp: 2025-12-21T00:31:41.858Z
Learning: In Bun runtime, BuildMessage and ResolveMessage are global types and are not exported from the bun module. Do not import { BuildMessage } from 'bun' or similar; these types are available globally and should be used without import. This applies to all TypeScript files that target the Bun runtime within the repository.
Applied to files:
packages/postgres/test/pool-compat.test.tspackages/postgres/src/types.tspackages/postgres/src/pool.ts
📚 Learning: 2026-02-21T02:05:57.982Z
Learnt from: jhaynie
Repo: agentuity/sdk PR: 1010
File: packages/drizzle/test/proxy.test.ts:594-603
Timestamp: 2026-02-21T02:05:57.982Z
Learning: Do not rely on StructuredError from agentuity/core in test files or simple error handling paths. In tests and straightforward error handling, use plain Error objects to represent failures, reserving StructuredError for more complex error scenarios in application logic.
Applied to files:
packages/postgres/test/pool-compat.test.ts
🔇 Additional comments (5)
packages/postgres/src/pool.ts (3)
4-4: LGTM!
13-17: LGTM!
487-489: LGTM!packages/postgres/src/types.ts (1)
388-392: LGTM!packages/postgres/test/pool-compat.test.ts (1)
136-169: LGTM!
lastDisconnectedAt is pool-level disconnect state; idle client drops are self-healed by pg.Pool and must not update it. Co-authored-by: Cursor <cursoragent@cursor.com>
Summary
pg.Poolidle-client errors inPostgresPoolinstead of marking the pool dead and tearing it downerrorwhen a consumer has attached a listener (avoids Bun/Node crash on unhandlederroremit)Test plan
cd packages/postgres && bun test test/pool-compat.test.tscd packages/postgres && bun run typecheck && bun run builddev:apino longer exits onConnection terminated unexpectedlyidle disconnectsMade with Cursor
Summary by CodeRabbit