-
Notifications
You must be signed in to change notification settings - Fork 1
fix(drizzle): use resilient SQL proxy to survive reconnection #901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Drizzle was initialized with a static reference to client.raw (the Bun.SQL instance). When the connection dropped and PostgresClient reconnected, it created a new Bun.SQL instance, but Drizzle still held the old dead reference, causing ERR_POSTGRES_CONNECTION_CLOSED on all subsequent queries. Fix: Replace the static client.raw reference with a dynamic Proxy that always resolves client.raw at call time. The proxy also wraps unsafe() calls through executeWithRetry for automatic retry on transient connection errors. Closes #899
📝 WalkthroughWalkthroughThis PR introduces a resilient SQL proxy in Drizzle that dynamically delegates to the current raw Postgres connection, enabling proper reconnection handling. It also exposes a public executeWithRetry method on PostgresClient, allowing external retry logic with automatic reconnection-aware execution. Changes
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. No actionable comments were generated in the recent review. 🎉 🧹 Recent nitpick comments
📜 Recent review detailsConfiguration used: Organization UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (4)
🧰 Additional context used📓 Path-based instructions (8)packages/drizzle/**/*.{ts,tsx}📄 CodeRabbit inference engine (packages/drizzle/AGENTS.md)
Files:
packages/drizzle/**/*.{test,spec}.{ts,tsx}📄 CodeRabbit inference engine (packages/drizzle/AGENTS.md)
Files:
**/*.{ts,tsx,js,jsx,json,md}📄 CodeRabbit inference engine (AGENTS.md)
Files:
**/*.{ts,tsx}📄 CodeRabbit inference engine (AGENTS.md)
Files:
packages/**/*.{ts,tsx}📄 CodeRabbit inference engine (AGENTS.md)
Files:
packages/*/test/**/*.{ts,tsx}📄 CodeRabbit inference engine (AGENTS.md)
Files:
packages/postgres/**/*.{ts,tsx}📄 CodeRabbit inference engine (packages/postgres/AGENTS.md)
Files:
packages/postgres/**/*.{test,spec}.{ts,tsx}📄 CodeRabbit inference engine (packages/postgres/AGENTS.md)
Files:
🧠 Learnings (1)📚 Learning: 2025-12-21T00:31:41.858ZApplied to files:
🧬 Code graph analysis (1)packages/drizzle/test/proxy.test.ts (2)
🔇 Additional comments (9)
✏️ Tip: You can disable this entire section by setting Comment |
📦 Canary Packages Publishedversion: PackagesInstallAdd to your {
"dependencies": {
"@agentuity/server": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-server-1.0.4-2cc91ef.tgz",
"@agentuity/drizzle": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-drizzle-1.0.4-2cc91ef.tgz",
"@agentuity/frontend": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-frontend-1.0.4-2cc91ef.tgz",
"@agentuity/workbench": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-workbench-1.0.4-2cc91ef.tgz",
"@agentuity/cli": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-cli-1.0.4-2cc91ef.tgz",
"@agentuity/opencode": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-opencode-1.0.4-2cc91ef.tgz",
"@agentuity/claude-code": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-claude-code-1.0.4-2cc91ef.tgz",
"@agentuity/evals": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-evals-1.0.4-2cc91ef.tgz",
"@agentuity/auth": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-auth-1.0.4-2cc91ef.tgz",
"@agentuity/runtime": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-runtime-1.0.4-2cc91ef.tgz",
"@agentuity/react": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-react-1.0.4-2cc91ef.tgz",
"@agentuity/core": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-core-1.0.4-2cc91ef.tgz",
"@agentuity/postgres": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-postgres-1.0.4-2cc91ef.tgz",
"@agentuity/schema": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-schema-1.0.4-2cc91ef.tgz"
}
}Or install directly: bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-server-1.0.4-2cc91ef.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-drizzle-1.0.4-2cc91ef.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-frontend-1.0.4-2cc91ef.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-workbench-1.0.4-2cc91ef.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-cli-1.0.4-2cc91ef.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-opencode-1.0.4-2cc91ef.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-claude-code-1.0.4-2cc91ef.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-evals-1.0.4-2cc91ef.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-auth-1.0.4-2cc91ef.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-runtime-1.0.4-2cc91ef.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-react-1.0.4-2cc91ef.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-core-1.0.4-2cc91ef.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-postgres-1.0.4-2cc91ef.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.4-2cc91ef/agentuity-schema-1.0.4-2cc91ef.tgz |
Summary
Fixes #899 —
ERR_POSTGRES_CONNECTION_CLOSEDstill happening even with the custom@agentuity/drizzlepackage.Root Cause
createPostgresDrizzle()passedclient.raw(a static reference to the Bun.SQL instance) to Drizzle at creation time:When the connection dropped and
PostgresClient._reconnectLoop()succeeded,_reinitializeSql()created a newthis._sqlBun.SQL instance — but Drizzle's internalBunSQLSessionstill held the old, dead reference. All subsequent Drizzle queries used the dead connection →ERR_POSTGRES_CONNECTION_CLOSED.Fix
packages/drizzle/src/postgres.ts): Replace the staticclient.rawwith aProxythat always resolvesclient.rawat call time. After reconnection, Drizzle automatically uses the new connection.unsafe(): The proxy interceptsunsafe()calls and routes them throughclient.executeWithRetry(), providing automatic retry with exponential backoff on transient connection errors. Supports.values()chaining that Drizzle uses internally.executeWithRetry()(packages/postgres/src/client.ts): Exposes the existing private retry logic as a public method for use by integration layers like the Drizzle proxy.Changes
packages/drizzle/src/postgres.tscreateResilientSQLProxy(), replacedclient.rawwith proxypackages/postgres/src/client.tsexecuteWithRetry()method, bound increateCallableClient()packages/drizzle/test/proxy.test.ts.values()chainingpackages/postgres/test/client.test.tsexecuteWithRetrybehavior, retry logic, error handlingVerification
bun run format— cleanbun run lint— 0 errorsbun run typecheck— 0 errorsbun run build— cleanbun run test— 212 pass, 0 fail (postgres + drizzle packages)Summary by CodeRabbit
New Features
Tests