Repository navigation
fix: solve #5852 — chat pane FK violation inserting chat_sessions.v2_workspace_id for local-first workspaces - #5853
Draft
github-actions[bot] wants to merge 1 commit into
Draft
github-actions[bot] wants to merge 1 commit into
github-actions[bot] wants to merge 1 commit into
Conversation
chat.createSession inserted chat_sessions.v2_workspace_id straight from the client-supplied id. Post local-first migration, v2 workspaces live in host.db and the electric-synced cloud v2_workspaces table can be empty for the org, so the FK (chat_sessions_v2_workspace_id_v2_workspaces_id_fk) can never be satisfied — every message failed with a raw DrizzleQueryError in the chat pane. Now the workspace id is verified against cloud v2_workspaces inside the transaction and only pinned when it exists; otherwise NULL is persisted, mirroring the REST route's retry-without-workspaceId fallback. Checking up front (rather than catch-and-retry) is required because a failed insert aborts the surrounding transaction.
This branch has not been deployed
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.
Root cause
chat.createSession(packages/trpc/src/router/chat/chat.ts) insertedchat_sessions.v2_workspace_iddirectly from the client-suppliedinput.v2WorkspaceId. That column has an FK to cloudv2_workspaces(chat_sessions_v2_workspace_id_v2_workspaces_id_fk).Post local-first migration, v2 workspaces are authored in
host.dband the electric-synced cloudv2_workspacestable can be empty for the org (0 rows) while the desktop runs several local workspaces. The FK can therefore never be satisfied: the insert throws and the rawDrizzleQueryErrorsurfaces in the chat pane on every message. The older REST route (apps/api/src/app/api/chat/[sessionId]/route.ts) already has a retry-without-workspaceId fallback for this exact failure class; the tRPC path had none.The fix
Before inserting, verify the workspace exists cloud-side and only pin
v2_workspace_idwhen it does; otherwise persistNULL— mirroring the REST route'sshouldRetryWithoutWorkspaceIdbehavior (option (a) from the issue).The check is done up front inside the transaction rather than as catch-and-retry, because a failed
INSERTaborts the surrounding Postgres transaction (current transaction is aborted…), which would make an in-transaction retry fail regardless. ASELECTreturning zero rows leaves the transaction usable.Tests
Added
packages/trpc/src/router/chat/chat.test.ts:v2_workspacesset empty (local-first), the mock insert faithfully models the FK and throws the reported error. Before the fixcreateSessionrejected with the raw SQL; after the fix it falls back tov2_workspace_id = NULLand returns the session id.Both pass;
packages/trpctypechecks and lints clean.Closes #5852
Summary by cubic
Prevent FK violations when creating chat sessions by verifying
v2_workspace_idagainst cloudv2_workspacesand insertingNULLwhen missing. Fixes chat pane errors for local-first workspaces and aligns tRPC with the REST fallback (closes #5852).packages/trpc/src/router/chat/chat.ts, check for the workspace inv2Workspacesinside the transaction; only setv2_workspace_idwhen it exists, else persistNULL.SELECTinstead.packages/trpc/src/router/chat/chat.test.tscovering local-first fallback and cloud-present cases.Written for commit 9a60af4. Summary will update on new commits.