fix(chat-instance): self-bind org context in startInstance for boot + webhooks#522
Merged
Conversation
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
#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.
20cd496 to
df5d21e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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):
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
Closes
Fixes the regression described in #506's review (the org-context boot bug that landed unaddressed).