refactor(services): extract agent-ownership predicate to shared helper (closes #4377)#4388
Merged
Merged
Conversation
closes #4377) PR #4250's review flagged the agent-ownership JOIN as a drift surface — duplicated inline in two places with subtly different semantics: - registry-api.ts:4825 `resolveAgentOwnerOrg(userId, agentUrl)` — "any org the user is a member of that owns the agent" - member-tools.ts:3588 (inline) — "the resolved member-context org IS the owning org" (tighter predicate that adds the org_id constraint) Both queries join `member_profiles.agents` against `organization_ memberships` — same canonical relation, different selectivity. This PR extracts both to `server/src/services/agent-ownership.ts`: - `findOwnerOrgForUser(userId, agentUrl): Promise<string | null>` — registry-api.ts's "discover ownership" use case - `isOrgOwnerOfAgent(orgId, userId, agentUrl): Promise<boolean>` — member-tools.ts's "confirm specific org" use case Both call sites updated to use the helpers. `resolveAgentOwnerOrg` is retained as a closure-scoped alias inside the registry-api factory so existing call sites don't need to thread the import. Note on active-membership filtering: `organization_memberships` has no status column in this schema — removed members get their row deleted, not status-flipped. Row existence is the membership signal. Documented in the helper file's module doc. Tests (7 passing): - findOwnerOrgForUser returns org_id, null on no match, null on throw - isOrgOwnerOfAgent returns true/false correctly - semantic distinction pinned: findOwnerOrgForUser uses 2 params (no org filter); isOrgOwnerOfAgent uses 3 params (org_id constraint) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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
Closes #4377. Extracts the agent-ownership JOIN predicate into `server/src/services/agent-ownership.ts` so the two distinct semantic uses share one source.
The drift surface
PR #4250's review flagged two inline copies of the same JOIN with subtly different semantics:
Both query `member_profiles.agents @> [...] JOIN organization_memberships` but with different selectivity. Three call sites adding more inline copies is when rot sets in.
What changed
Note on active-membership filtering
`organization_memberships` has no status column in this schema — removed members get their row deleted, not status-flipped. Row existence is the membership signal. Documented in the new module's doc comment so future readers don't add a redundant `AND status = 'active'` filter.
Test plan
🤖 Generated with Claude Code