perf: cache Supabase admin client as module-level singleton - #138
Merged
BigSimmo merged 2 commits intoJul 2, 2026
Merged
Conversation
Every API route calls createAdminClient() on every request, which previously instantiated a fresh SupabaseClient (plus requireServerEnv() + assertExpectedSupabaseProjectConfig() validation) each time. Introduce a module-level singleton using the same lazy-init pattern already used for the OpenAI client (openAIClient ??= new OpenAI(…)). The service-role client carries no user-specific session state, so reusing a single instance across requests is safe and saves per-request object construction + repeated env validation overhead. 825/825 tests pass.
Copilot created this pull request from a session on behalf of
BigSimmo
July 2, 2026 08:03
View session
…2a-45d2-8adc-9d9ded40f98a
BigSimmo
marked this pull request as ready for review
July 2, 2026 10:32
BigSimmo
enabled auto-merge
July 2, 2026 10:32
BigSimmo
deleted the
copilot/task-87357024-1243497866-93b3cae7-6f2a-45d2-8adc-9d9ded40f98a
branch
July 2, 2026 16:31
This was referenced Jul 31, 2026
BigSimmo
pushed a commit
that referenced
this pull request
Jul 31, 2026
Resolves the fifth conflict on docs/outstanding-issues.md by taking main's side on both hunks, after verifying that was correct rather than convenient: main renumbered the queue to fix a duplicate rank 18 this branch introduced, and it archived #138 and #146 rather than dropping them (confirmed present in the archive table). The resulting id set is byte-identical to main's. #150 escalated. That row records CodeRabbit reviewing none of a full day's PRs and rests explicitly on the Codex connector being the surviving reviewer that "found three real defects that had survived local gates". On PR #1505 Codex posted its own usage-limit notice alongside CodeRabbit's spending-cap notice, so that PR received zero automated review and so will anything opened while both caps hold. This is the same issue with its fallback removed, not a new one — which matters because the single Codex finding on #1459 was correct and caught a verification that had matched the wrong component, closing #105 on bad evidence. Local gates did not catch it. The attribution harness lands as scripts/measure-cls-attribution.mjs. #147's next step needs a before/after CLS pair, and #118 will want element attribution again; without this, both mean re-deriving it. Build and serve mirror run-lighthouse-budget.mjs so the numbers sit beside the Lighthouse reports. Two traps that cost real time are encoded rather than left to be rediscovered: CHROME_PATH must be set where the browser is outside a standard location, and an init script attaching a MutationObserver to document.documentElement before <html> exists throws and silently takes the CLS observer with it — so the script now fails loudly on an all-zero result instead of reporting a false clean bill. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01361jh3eYVjJCzXWjAhdZiF
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
createAdminClient()previously constructed a freshSupabaseClient— plusrequireServerEnv()+assertExpectedSupabaseProjectConfig()— on every API request. This PR caches it as a module-level singleton, matching the existingopenAIClient ??= new OpenAI(…)pattern inopenai.ts. The service-role client holds no user-specific state (autoRefreshToken: false,persistSession: false), so sharing across requests is safe.Verification
npm run verify:cheap— 825/825 tests pass, no new type errorsnpm run verify:uiwhen UI, routing, styling, browser behavior, reduced-motion, or forced-colors behavior changednpm run verify:releasebefore release or handoff confidence claimsnpm run format:checknpm run check:production-readinesswhen clinical workflow, privacy, environment, Supabase, source governance, or deployment behavior changednpm run check:deployment-readinesswhen deployment startup, hosting, or rollout behavior changedClinical Governance Preflight
Complete this section when the change touches ingestion, answer generation, search/ranking, source rendering, document access, privacy, production env, or clinical output.
Clinical KB Database(sjrfecxgysukkwxsowpy)Notes
normalizedClinicalSearchTokensinbuildMatchExplanation(Bump actions/setup-node from 4 to 6 #2), removeexperimental.cpus: 1(Bump vitest from 4.1.6 to 4.1.7 #4), parallelisefetchRelatedDocuments(Bump gitleaks/gitleaks-action from 2 to 3 #8).