Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

perf(control-api): dedup grant-existence CR reads per mcpServerName

Diego Zacariasd4ebb95c2e48
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index faefb55b7..b2ce7fe8d 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.502",
+ "version": "0.1.503",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.502",
+ "version": "0.1.503",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/image-policy": "file:../packages/image-policy",
diff --git a/control-api/package.json b/control-api/package.json
index 072449e94..c60946ab6 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.502",
+ "version": "0.1.503",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --git a/control-api/src/routes/mcpOauth.ts b/control-api/src/routes/mcpOauth.ts
index 9cb8806e6..c5ee97884 100644
--- a/control-api/src/routes/mcpOauth.ts
+++ b/control-api/src/routes/mcpOauth.ts
@@ -185,6 +185,52 @@ export async function resolveBatchGrantExistence(
): Promise<GrantExistsResult[]> {
const log = deps.log ?? rootLogger
const results: GrantExistsResult[] = []
+
+ // Memoize the CR resolution by `mcpServerName` for the lifetime of THIS batch.
+ // The sweep fans a per-user partition out to one entry per user, so a shared
+ // server with N live users produces N IDENTICAL apiserver reads of the same CR
+ // (mini-spec 13 Should-fix #1). The apiserver read + `resolveServerOAuth`
+ // depend ONLY on the server name (not on userId), so they are resolved once
+ // per distinct name; the grant SELECT still runs per entry (it keys by userId).
+ //
+ // A `null` entry is the memoized fail-open verdict for a server whose CR read
+ // threw (deleted CR / apiserver blip) OR did not resolve as OAuth — both yield
+ // `exists:true` per entry exactly as before, and neither is retried N times.
+ // `.has()` distinguishes "not yet read" from "read → null", so a failed read is
+ // cached, never re-attempted within the batch.
+ const serverOAuthCache = new Map<string, ReturnType<typeof resolveServerOAuth>>()
+ const resolveServerOAuthCached = async (
+ name: string
+ ): Promise<ReturnType<typeof resolveServerOAuth>> => {
+ if (serverOAuthCache.has(name)) return serverOAuthCache.get(name) ?? null
+ let resolved: ReturnType<typeof resolveServerOAuth>
+ try {
+ const server = (await deps.gateway.getResource(
+ 'mcpservers',
+ name,
+ deps.mcpServersNamespace
+ )) as McpServerResource
+ resolved = resolveServerOAuth(server)
+ if (!resolved) {
+ // Not an OAuth server / no usable oauth id → ambiguous, fail-open.
+ log.warn(
+ { mcpServerName: name },
+ 'grant-existence: server not resolvable as oauth; fail-open'
+ )
+ }
+ } catch (err) {
+ // Server not found (deleted CR) or apiserver blip — transient/ambiguous.
+ // Fail-open: never evict a live partition on a hiccup.
+ log.warn(
+ { err, mcpServerName: name },
+ 'grant-existence: server read failed; fail-open (exists:true)'
+ )
+ resolved = null
+ }
+ serverOAuthCache.set(name, resolved)
+ return resolved
+ }
+
for (const raw of queries) {
const entry = (raw && typeof raw === 'object' ? raw : {}) as {
mcpServerName?: unknown
@@ -211,38 +257,34 @@ export async function resolveBatchGrantExistence(
continue
}
+ // Resolve the server's OAuth spec ONCE per distinct name (memoized above).
+ // A null verdict (read failed OR not an OAuth server) is fail-open per entry.
+ const resolved = await resolveServerOAuthCached(mcpServerName)
+ if (!resolved) {
+ results.push(echo(true))
+ continue
+ }
+ const key = buildMcpServerGrantKey(resolved, {
+ mcpServerName,
+ mcpServersNamespace: deps.mcpServersNamespace,
+ userId,
+ })
+ if (!key) {
+ // Missing the coordinate the flavor needs (no userId for a user server,
+ // no contextRef for a context server) → ambiguous, fail-open. Depends on
+ // the per-entry userId, so it is decided per entry, not memoized.
+ log.warn(
+ { mcpServerName, grantScope: resolved.grantScope },
+ 'grant-existence: missing grant coordinate; fail-open'
+ )
+ results.push(echo(true))
+ continue
+ }
try {
- const server = (await deps.gateway.getResource(
- 'mcpservers',
- mcpServerName,
- deps.mcpServersNamespace
- )) as McpServerResource
- const resolved = resolveServerOAuth(server)
- if (!resolved) {
- // Not an OAuth server / no usable oauth id → ambiguous, fail-open.
- log.warn({ mcpServerName }, 'grant-existence: server not resolvable as oauth; fail-open')
- results.push(echo(true))
- continue
- }
- const key = buildMcpServerGrantKey(resolved, {
- mcpServerName,
- mcpServersNamespace: deps.mcpServersNamespace,
- userId,
- })
- if (!key) {
- // Missing the coordinate the flavor needs (no userId for a user server,
- // no contextRef for a context server) → ambiguous, fail-open.
- log.warn(
- { mcpServerName, grantScope: resolved.grantScope },
- 'grant-existence: missing grant coordinate; fail-open'
- )
- results.push(echo(true))
- continue
- }
+ // The grant SELECT keys by the per-entry coordinate, so it always runs per
+ // entry. A DB error here is transient/ambiguous → fail-open for THIS entry.
results.push(echo(await oauthGrantExists(deps.db, key)))
} catch (err) {
- // Server not found (deleted CR), apiserver blip, or DB error — all
- // transient/ambiguous. Fail-open: never evict a live partition on a hiccup.
log.warn({ err, mcpServerName }, 'grant-existence check failed; fail-open (exists:true)')
results.push(echo(true))
}
diff --git a/control-api/test/routes.mcpOauth.test.ts b/control-api/test/routes.mcpOauth.test.ts
index 44b6bfe1e..c6c4ba9f6 100644
--- a/control-api/test/routes.mcpOauth.test.ts
+++ b/control-api/test/routes.mcpOauth.test.ts
@@ -521,6 +521,45 @@ describe('routes/mcp-oauth — POST /mcp-oauth/grants/exists (mini-spec 13)', ()
])
})
+ // Should-fix #1 (efficiency, mini-spec 13): within ONE batch the CR of a
+ // shared server is read from the apiserver ONCE, not once per entry. N per-user
+ // partitions of the SAME server → a single getResource('mcpservers', 'gdrive'),
+ // yet still N grant SELECTs (one per userId). The per-entry response is
+ // IDENTICAL to the un-deduped semantics — only the apiserver fan-out drops.
+ it('200: dedups the getResource read across N entries of the same server', async () => {
+ seedOauthServer(gateway, { name: 'gdrive', grantScope: 'user' })
+ const getResourceSpy = vi.spyOn(gateway, 'getResource')
+ // alice has a grant (first SELECT 1 finds a row); bob and carol do not.
+ mockPoolQuery.mockResolvedValueOnce({ rows: [{ exists: 1 }], rowCount: 1 })
+ const res = await post(controlToken(), {
+ queries: [
+ { mcpServerName: 'gdrive', userId: 'alice' },
+ { mcpServerName: 'gdrive', userId: 'bob' },
+ { mcpServerName: 'gdrive', userId: 'carol' },
+ ],
+ })
+ expect(res.status).toBe(200)
+ // Per-entry booleans + order unchanged vs. the one-read-per-entry version.
+ expect(res.body.results).toEqual([
+ { mcpServerName: 'gdrive', userId: 'alice', exists: true },
+ { mcpServerName: 'gdrive', userId: 'bob', exists: false },
+ { mcpServerName: 'gdrive', userId: 'carol', exists: false },
+ ])
+ // The shared server CR is read from the apiserver EXACTLY ONCE for the batch.
+ const gdriveReads = getResourceSpy.mock.calls.filter(
+ ([plural, name]) => plural === 'mcpservers' && name === 'gdrive'
+ )
+ expect(gdriveReads).toHaveLength(1)
+ // Yet every entry still ran its own SELECT 1 (one grant lookup per userId).
+ const grantSelects = mockPoolQuery.mock.calls.filter(
+ ([sql]) => typeof sql === 'string' && sql.includes('FROM oauth_grants')
+ )
+ expect(grantSelects).toHaveLength(3)
+ // …and each SELECT carries its own entry's userId, in order (userId is the
+ // 4th bind param: [ownerKind, ns, name, userId, clientId]).
+ expect(grantSelects.map(call => (call[1] as unknown[])[3])).toEqual(['alice', 'bob', 'carol'])
+ })
+
// Context flavor: keyed by the server's AUTHORITATIVE spec.contextRef, NEVER
// the body contextId (no body-trust). The SELECT 1 coordinate proves it; the
// lying body contextId is only echoed back, never used to key.