Repository navigation
fix(realtime): bind the socket to the organization the client is viewing - #17
Conversation
… stated organization
|
Playwright pass rate over five consecutive full-suite runs (
|
📝 WalkthroughWalkthroughThe change centralizes active-membership selection, adds organization-aware realtime authentication and close codes, propagates organization IDs through realtime clients and web providers, and adds unit, integration, reconnect, and end-to-end coverage. ChangesActive organization and realtime scoping
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant WebApp
participant RealtimeClient
participant RealtimeServer
participant MembershipAuth
WebApp->>RealtimeClient: Create client with organizationId
RealtimeClient->>RealtimeServer: Connect with token and organizationId
RealtimeServer->>MembershipAuth: Authenticate connection and resolve membership
MembershipAuth-->>RealtimeServer: Principal or rejection reason
RealtimeServer-->>RealtimeClient: Ready response or close code
RealtimeClient-->>WebApp: Scoped realtime state or disposed connection
Possibly related PRs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 Checkov (3.3.8)apps/realtime/package.jsonTraceback (most recent call last): Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
apps/web/e2e/second-workspace-realtime.spec.ts (1)
4-5: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winValidate E2E environment configuration with Zod.
ORBIT_E2E_BASE_URLandORBIT_E2E_SHOTSare external inputs and are used without validation. Parse them once with ane2eConfigSchemabefore deriving these constants.Proposed change
+import { z } from 'zod'; + +const e2eConfigSchema = z.object({ + ORBIT_E2E_BASE_URL: z.url().optional(), + ORBIT_E2E_SHOTS: z.string().min(1).optional(), +}); + +const e2eConfig = e2eConfigSchema.parse(process.env); -const BASE = process.env['ORBIT_E2E_BASE_URL'] ?? 'http://localhost:3011'; -const SHOTS = process.env['ORBIT_E2E_SHOTS'] ?? 'test-results'; +const BASE = e2eConfig.ORBIT_E2E_BASE_URL ?? 'http://localhost:3011'; +const SHOTS = e2eConfig.ORBIT_E2E_SHOTS ?? 'test-results';As per coding guidelines, “Validate every external input with Zod.”
🤖 Prompt for 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. In `@apps/web/e2e/second-workspace-realtime.spec.ts` around lines 4 - 5, Define an e2eConfigSchema with Zod for ORBIT_E2E_BASE_URL and ORBIT_E2E_SHOTS, parse the relevant process.env values once, and derive BASE and SHOTS from the validated result. Preserve the existing defaults while ensuring both external inputs are validated before use.Source: Coding guidelines
🤖 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 `@apps/realtime/src/server.ts`:
- Around line 75-79: Update the request parsing flow around
connectionOrganizationIdSchema.safeParse to distinguish an absent organizationId
from a present invalid value: preserve null only when the parameter is absent,
and reject or close the request when parsing fails. Ensure malformed empty and
overlong organizationId values cannot fall back to the active organization, and
add coverage for both cases.
In `@apps/web/e2e/second-workspace-realtime.spec.ts`:
- Around line 29-32: Update the membership timestamp mutation in the realtime
workspace test so the first membership’s createdAt is changed to a distinct
value rather than set to membership.createdAt unchanged. Preserve the existing
update targeting schema.member.id and ensure the resulting timestamps produce
the intended active-membership precedence after the second workspace is created.
---
Nitpick comments:
In `@apps/web/e2e/second-workspace-realtime.spec.ts`:
- Around line 4-5: Define an e2eConfigSchema with Zod for ORBIT_E2E_BASE_URL and
ORBIT_E2E_SHOTS, parse the relevant process.env values once, and derive BASE and
SHOTS from the validated result. Preserve the existing defaults while ensuring
both external inputs are validated before use.
🪄 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: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 61523d23-94f6-40bb-b798-0c8087f789d8
⛔ Files ignored due to path filters (1)
pnpm-lock.yamlis excluded by!**/pnpm-lock.yaml
📒 Files selected for processing (19)
apps/mcp/src/create-key.tsapps/realtime/package.jsonapps/realtime/src/active-organization.test.tsapps/realtime/src/auth.tsapps/realtime/src/client-reconnect.test.tsapps/realtime/src/server.tsapps/realtime/src/test-helpers.tsapps/web/e2e/second-workspace-realtime.spec.tsapps/web/src/app/(app)/inbox/page.tsxapps/web/src/features/inbox/inbox-realtime.tsxapps/web/src/lib/auth/principal.tsapps/web/src/lib/realtime/provider.tsxpackages/core/src/index.tspackages/core/src/org/active-organization.test.tspackages/core/src/org/active-organization.tspackages/realtime-client/src/index.tspackages/realtime-client/src/react.test.tsxpackages/realtime-client/src/react.tsxpackages/shared/src/events/index.ts
| const stated = connectionOrganizationIdSchema.safeParse(url.searchParams.get('organizationId')); | ||
| return { | ||
| token: url.searchParams.get('token') ?? '', | ||
| organizationId: stated.success ? stated.data : null, | ||
| }; |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Reject malformed stated organization IDs instead of falling back.
At Line 75, a present-but-invalid organizationId becomes null, so authentication selects the active organization. Distinguish an absent parameter from a parse failure and close malformed requests; add coverage for organizationId= and overlong values.
As per coding guidelines, “Validate every external input with Zod.”
🤖 Prompt for 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.
In `@apps/realtime/src/server.ts` around lines 75 - 79, Update the request parsing
flow around connectionOrganizationIdSchema.safeParse to distinguish an absent
organizationId from a present invalid value: preserve null only when the
parameter is absent, and reject or close the request when parsing fails. Ensure
malformed empty and overlong organizationId values cannot fall back to the
active organization, and add coverage for both cases.
Source: Coding guidelines
| await db | ||
| .update(schema.member) | ||
| .set({ createdAt: membership.createdAt }) | ||
| .where(eq(schema.member.id, membership.id)); |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Actually update the first membership timestamp.
This writes createdAt back unchanged, so it cannot alter active-membership precedence after creating the second workspace. The test can therefore pass without exercising the intended differing active-organization state.
Proposed change
await db
.update(schema.member)
- .set({ createdAt: membership.createdAt })
+ .set({ createdAt: new Date() })
.where(eq(schema.member.id, membership.id));📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| await db | |
| .update(schema.member) | |
| .set({ createdAt: membership.createdAt }) | |
| .where(eq(schema.member.id, membership.id)); | |
| await db | |
| .update(schema.member) | |
| .set({ createdAt: new Date() }) | |
| .where(eq(schema.member.id, membership.id)); |
🤖 Prompt for 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.
In `@apps/web/e2e/second-workspace-realtime.spec.ts` around lines 29 - 32, Update
the membership timestamp mutation in the realtime workspace test so the first
membership’s createdAt is changed to a distinct value rather than set to
membership.createdAt unchanged. Preserve the existing update targeting
schema.member.id and ensure the resulting timestamps produce the intended
active-membership precedence after the second workspace is created.
Resolve the active organization through one shared helper and let the client state which workspace its socket is for, so the page and the realtime stream can never disagree.