Skip to content

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

TypeScript

fix(control-api): bound the registry connection cache with a TTL

Jose Ramirezd72a79288875
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index 18198fe98..33b3b25bf 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.376",
+ "version": "0.1.377",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.376",
+ "version": "0.1.377",
"dependencies": {
"@clerum/image-policy": "file:../packages/image-policy",
"@clerum/llm-providers": "file:../packages/llm-providers",
diff --git a/control-api/package.json b/control-api/package.json
index 643e068a3..07f496b31 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.376",
+ "version": "0.1.377",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --git a/control-api/src/services/registryConnectionDb.ts b/control-api/src/services/registryConnectionDb.ts
index 4fa4a30be..1826b500a 100644
--- a/control-api/src/services/registryConnectionDb.ts
+++ b/control-api/src/services/registryConnectionDb.ts
@@ -49,15 +49,19 @@ interface RawRow {
registry_url: string | null
}
-// Process-local cache: a control-api serves one deployment for its lifetime, so
-// the decrypted row is stable. Evicted by any write here + the test reset.
-// Caveat: eviction is process-local only — a post-connection DELETE/reconnect
-// clears this cache on THIS replica alone, so a reconnect-in-place on a
-// multi-replica deployment requires a rollout restart to evict peer replicas.
+// Process-local cache with a bounded TTL: a control-api serves one deployment
+// for its lifetime, so the decrypted row is stable — but the DERIVED auth state
+// (isRegistryAuthActive) is an access-control input, and an unbounded cache
+// would let a peer replica keep reporting auth-active forever after another
+// replica disconnected. The TTL caps that window; writes here still evict
+// immediately on the local process.
+const CONNECTION_CACHE_TTL_MS = 15_000
let cached: RegistryConnectionRow | null | undefined
+let cachedAt = 0
export function __resetRegistryConnectionCacheForTests(): void {
cached = undefined
+ cachedAt = 0
}
function encKey(): Buffer {
@@ -67,7 +71,7 @@ function encKey(): Buffer {
export async function getRegistryConnection(
db: DbClient = pool
): Promise<RegistryConnectionRow | null> {
- if (cached !== undefined) return cached
+ if (cached !== undefined && Date.now() - cachedAt < CONNECTION_CACHE_TTL_MS) return cached
const res = await db.query(
`SELECT deployment_id, key_id, public_key_pem, private_key_encrypted, client_id,
client_secret_encrypted, org_name, requested_org_name, contact_email, status, registry_url
@@ -78,6 +82,7 @@ export async function getRegistryConnection(
const raw = res.rows[0] as RawRow | undefined
if (!raw) {
cached = null
+ cachedAt = Date.now()
return null
}
const key = encKey()
@@ -96,6 +101,7 @@ export async function getRegistryConnection(
status: raw.status,
registryUrl: raw.registry_url,
}
+ cachedAt = Date.now()
return cached
}
diff --git a/control-api/test/services.registryConnectionDb.test.ts b/control-api/test/services.registryConnectionDb.test.ts
index 05db471bc..5d5aa7a6a 100644
--- a/control-api/test/services.registryConnectionDb.test.ts
+++ b/control-api/test/services.registryConnectionDb.test.ts
@@ -408,3 +408,40 @@ describe('markConnected — scoped write', () => {
expect(ok).toBe(false)
})
})
+
+describe('getRegistryConnection — cache TTL', () => {
+ it('serves a cached ROW inside the window, re-queries after it', { retry: 0 }, async () => {
+ vi.useFakeTimers()
+ try {
+ cfg.oauthEncryptionKey = randomBytes(32).toString('hex')
+ dbQuery.mockResolvedValue({ rows: [makeRawRow(cfg.oauthEncryptionKey)], rowCount: 1 })
+ await getRegistryConnection()
+ const afterFirst = dbQuery.mock.calls.length
+ await getRegistryConnection()
+ expect(dbQuery.mock.calls.length).toBe(afterFirst) // cache hit
+ vi.advanceTimersByTime(15_001)
+ await getRegistryConnection()
+ expect(dbQuery.mock.calls.length).toBe(afterFirst + 1) // re-queried
+ } finally {
+ vi.useRealTimers()
+ }
+ })
+
+ // The easiest way to implement the TTL wrong: stamp cachedAt only on the
+ // row-found branch. Then `cached` is null (so `!== undefined` passes) but
+ // cachedAt stays 0, and EVERY call re-queries — the most common self-hosted
+ // state, hit on every mintToken and every admin-route auth check.
+ it('serves a cached NULL inside the window without re-querying', { retry: 0 }, async () => {
+ vi.useFakeTimers()
+ try {
+ dbQuery.mockResolvedValue({ rows: [], rowCount: 0 })
+ await getRegistryConnection()
+ const afterFirst = dbQuery.mock.calls.length
+ await getRegistryConnection()
+ await getRegistryConnection()
+ expect(dbQuery.mock.calls.length).toBe(afterFirst)
+ } finally {
+ vi.useRealTimers()
+ }
+ })
+})