Skip to content

fix(chat-instance): self-bind org context in startInstance for boot + webhooks#522

Merged
buremba merged 2 commits into
mainfrom
fix/chat-instance-boot-org-context
May 4, 2026
Merged

fix(chat-instance): self-bind org context in startInstance for boot + webhooks#522
buremba merged 2 commits into
mainfrom
fix/chat-instance-boot-org-context

Conversation

@buremba
Copy link
Copy Markdown
Member

@buremba buremba commented May 4, 2026

Why

#516 made `PostgresSecretStore` route reads/writes per-org via AsyncLocalStorage. #506 (the connection-storage refactor) wires connection secrets through that store. Together: any caller of `startInstance()` that lacks org context will fail to resolve per-org `secret://` refs and silently mark the connection `status='error'`.

Affected entry points (none of which run inside HTTP middleware):

  1. Gateway boot — `ChatInstanceManager.initialize()` loads all connections.
  2. `/slack/events` public webhook — calls `handleSlackAppWebhook → ensureConnectionRunning → restartConnection → startInstance`.
  3. Slack OAuth completion — same code path.

Without this fix, every existing chat connection breaks on the first restart after #506+#516 land.

What

Move org-context derivation INSIDE `startInstance` itself so every caller benefits. If `tryGetOrgId()` already returns an id (HTTP-driven calls), pass through. Otherwise, derive from `connection.templateAgentId` via `getAgentOrganizationId()` and wrap the inner work in `orgContext.run({organizationId})`. Pre-org rows (no template agent or deleted agent) keep the GLOBAL bucket fallback.

Single source of truth — boot loop, Slack webhook, and OAuth all reuse the same self-binding without callers having to remember.

Test plan

  • `bun run typecheck` clean
  • `bun run format:check` clean
  • `bun test packages/server/src/gateway` → 502 pass / 0 fail

Closes

Fixes the regression described in #506's review (the org-context boot bug that landed unaddressed).

@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@codecov-commenter
Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

buremba added 2 commits May 4, 2026 22:12
#506's chat-instance-manager.ts:initialize loads connections at gateway
boot and calls startInstance() → resolveConfigForRuntime() →
secretStore.get(). Since #516 made PostgresSecretStore resolve secrets
per-org via AsyncLocalStorage, boot-time reads have no org context, fall
back to the GLOBAL bucket, and find nothing — every connection written
under an org's context is then marked status='error' on first restart.

Resolve the agent's organization_id via getAgentOrganizationId() and
wrap the startInstance call in orgContext.run({organizationId}). Pre-org
rows (no template agent or no org) keep the GLOBAL bucket fallback.

Verified: bun test packages/server/src/gateway → 502 pass / 0 fail.
@buremba buremba force-pushed the fix/chat-instance-boot-org-context branch from 20cd496 to df5d21e Compare May 4, 2026 21:13
@buremba buremba merged commit 5257162 into main May 4, 2026
16 checks passed
@buremba buremba deleted the fix/chat-instance-boot-org-context branch May 4, 2026 21:25
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