Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2024-05-18 - Bypass Redundant Queries for Cached Methods

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Add the required run report

This tenant-auth performance implementation is a meaningful product-code slice, but the commit records it only in .jules/bolt.md and adds no report under docs/runs/ using the repository template. Add the required run report so the implementation rationale, validation performed, and remaining risk are captured in the repository's established execution record.

AGENTS.md reference: AGENTS.md:L39-L39

Useful? React with 👍 / 👎.

**Learning:** React's `cache()` is helpful but if it wraps a method doing a DB fetch, calling it when we *already* have the DB object fetched earlier in the execution path triggers a redundant query. We shouldn't modify the signature of the cached method to accept the object as it would fragment the cache key.
**Action:** Bypass the cached wrapper method and instead call the underlying synchronous compiler directly (e.g., `compileWorkspaceExperience`) from the application layer when the data is already available. Ensure generics are used properly (e.g., `<T extends BaseInterface>`) to prevent downcasting when mapping objects that need to be passed downstream.
4 changes: 2 additions & 2 deletions lib/application/auth/tenant-access-selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export interface MembershipSelectionOrganization {
slug: string;
}

export function selectMembershipOrganization(input: {
export function selectMembershipOrganization<T extends MembershipSelectionOrganization>(input: {
memberships: MembershipSelectionMember[];
organizationsById: Map<string, MembershipSelectionOrganization>;
organizationsById: Map<string, T>;
requestedSlug?: string | null;
}) {
const requestedSlug = input.requestedSlug?.trim() || null;
Expand Down
13 changes: 7 additions & 6 deletions lib/application/auth/tenant-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from "@/lib/application/auth/tenant-routing";
import { selectMembershipOrganization } from "@/lib/application/auth/tenant-access-selection";
import { getWorkspaceExperienceBySlug } from "@/lib/application/workspace/workspace-service";
import { compileWorkspaceExperience } from "@/lib/platform/workspace/compiler";
import { OrganizationMemberRepository } from "@/lib/infrastructure/supabase/organization-member-repository";
import { OrganizationRepository } from "@/lib/infrastructure/supabase/organization-repository";
import { resolveWorkspaceTemplateForEmailDomain } from "@/lib/platform/workspace/registry";
Expand Down Expand Up @@ -110,11 +111,11 @@ export async function resolveTenantAccess(
});

if (organization) {
const workspace = await getWorkspaceExperienceBySlug(organization.slug);
const compiled = compileWorkspaceExperience({ slug: organization.slug, organization });
return existingWorkspaceAccess({
slug: organization.slug,
name: organization.name,
workspace: workspace.workspace,
workspace: compiled.workspace,
accessMethod: "membership",
});
}
Expand All @@ -124,11 +125,11 @@ export async function resolveTenantAccess(
if (emailDomain) {
const workspaceOrganization = await organizations.findFirstByWorkspaceEmailDomain(emailDomain);
if (workspaceOrganization && (!requestedSlug || workspaceOrganization.slug === requestedSlug)) {
const workspace = await getWorkspaceExperienceBySlug(workspaceOrganization.slug);
const compiled = compileWorkspaceExperience({ slug: workspaceOrganization.slug, organization: workspaceOrganization });
return existingWorkspaceAccess({
slug: workspaceOrganization.slug,
name: workspaceOrganization.name,
workspace: workspace.workspace,
workspace: compiled.workspace,
accessMethod: "domain_template",
});
}
Expand All @@ -143,11 +144,11 @@ export async function resolveTenantAccess(
if (guessed.slug) {
const existingOrganization = await organizations.findBySlug(guessed.slug).catch(() => null);
if (existingOrganization) {
const workspace = await getWorkspaceExperienceBySlug(existingOrganization.slug);
const compiled = compileWorkspaceExperience({ slug: existingOrganization.slug, organization: existingOrganization });
return existingWorkspaceAccess({
slug: existingOrganization.slug,
name: existingOrganization.name,
workspace: workspace.workspace,
workspace: compiled.workspace,
accessMethod: "domain_template",
});
}
Expand Down
Loading