feat(rls): Phase 1 #8 — HeartbeatContext RLS + callsite fixes#136
Merged
Conversation
Migration 20260602000000: TENANT_DIRECT RLS on HeartbeatContext. Hot path: processContextForRun() batches prune+get+buildPrompt into one withOrgContext transaction (1 SET instead of 3) for the BullMQ worker. Worker uses explicit organizationId from job.data (ALS empty in workers). Individual functions updated with organizationId param for route callsites.
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.
Summary
20260602000000_rls_phase1_heartbeatcontext: TENANT_DIRECT RLS onHeartbeatContext(SELECT/INSERT/UPDATE/DELETE policies + compositeorganizationId, idindex)context-manager.ts): newprocessContextForRun(agentId, organizationId)combines prune-expired + read-active + build-prompt into onewithOrgContexttransaction — 1 SET call instead of 3. Required because this table has the highest write frequency of any Phase 1 table (one upsert per heartbeat cycle per agent)heartbeat-worker.ts): replaces the three separatepruneContext/getContext/buildContextPromptcalls withprocessContextForRun(agentId, organizationId)using explicitorganizationIdfromjob.data— ALS is empty in BullMQ workersgetContext,setContext,deleteContext,pruneContext,buildContextPrompt): all gainorganizationIdparam and wrap their queries inwithOrgContext— used by route callsites/api/agents/[agentId]/heartbeat/context): GET and DELETE fetchorganizationIdvia agent lookup; POST is unchanged (setContexthandles it internally via body'sorganizationId)ALS context per callsite
context/route.tsGETgetContextcontext/route.tsPOSTorganizationId)setContextwraps internallycontext/route.tsDELETEwithOrgContextdirectheartbeat-worker.tsjob.data.organizationIdprocessContextForRun(batch)Test plan
pnpm precheck— 4/4 pass (TypeScript, vitest 4119 tests, lucide mocks, ESLint) ✅GET /api/agents/:id/heartbeat/contextRLS state after this PR
Phase 1: 8/14 live — OrganizationMember, Invitation, CompanyMission, Department, Goal, AgentPermissionGrant, HeartbeatConfig, HeartbeatContext
🤖 Generated with Claude Code