Skip to content

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

TypeScript

fix(control-api): r7-m12 bound auth retry on stream wakes

Josué54aba9531394
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index 7bc4125fe..f0f315c32 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.687",
+ "version": "0.1.688",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.687",
+ "version": "0.1.688",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/grok-provider-attempt-contract": "file:../packages/grok-provider-attempt-contract",
diff --git a/control-api/package.json b/control-api/package.json
index d2e83ca3a..bf10b20e9 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.687",
+ "version": "0.1.688",
"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/entityChangeStream.ts b/control-api/src/routes/entityChangeStream.ts
index 99382c5bc..67bae1b0b 100644
--- a/control-api/src/routes/entityChangeStream.ts
+++ b/control-api/src/routes/entityChangeStream.ts
@@ -79,6 +79,7 @@ export function streamEntityChanges(
let cursor = principalKind === 'user' ? ZERO_CURSOR : initialCursor
let userInitialResyncPending = principalKind === 'user'
let lastAuthorizationCheck = 0
+ let authorizationRetryAfter = 0
let lastHeartbeat = Date.now()
let pollTimer: NodeJS.Timeout | null = null
let heartbeatTimer: NodeJS.Timeout | null = null
@@ -201,11 +202,24 @@ export function streamEntityChanges(
const checkpoint = await readEntityChangeCheckpoint(cursor)
const hasChange = checkpoint.resyncRequired || checkpoint.scopes.length > 0
if (hasChange || Date.now() - lastAuthorizationCheck >= AUTHORIZATION_RECHECK_MS) {
- if (!(await isAuthorized())) {
- await closeForFailure('session_expired', checkpoint.cursor)
+ if (Date.now() < authorizationRetryAfter) return
+ try {
+ if (!(await isAuthorized())) {
+ await closeForFailure('session_expired', checkpoint.cursor)
+ return
+ }
+ lastAuthorizationCheck = Date.now()
+ authorizationRetryAfter = 0
+ } catch (err) {
+ // Feed wakes may arrive for every committed change while the auth
+ // service is unavailable. Keep the stream fail-closed and retry
+ // authorization at a bounded cadence instead of amplifying load.
+ authorizationRetryAfter = Date.now() + config.entityChangeStreamPollMs
+ throw err
+ }
+ if (closed) {
return
}
- lastAuthorizationCheck = Date.now()
}
if (checkpoint.resyncRequired) {
entityChangeStreamResyncRequiredTotal.inc({ principal_kind: principalKind })
diff --git a/control-api/test/routes.entityChangeStream.test.ts b/control-api/test/routes.entityChangeStream.test.ts
index e85f99068..a70808241 100644
--- a/control-api/test/routes.entityChangeStream.test.ts
+++ b/control-api/test/routes.entityChangeStream.test.ts
@@ -311,6 +311,11 @@ describe('routes/entityChangeStream', () => {
})
const req = new FakeRequest()
const res = new FakeResponse()
+ let wakeFeed: (() => void) | undefined
+ serviceMock.subscribeEntityChangeFeedWake.mockImplementation((wake: () => void) => {
+ wakeFeed = wake
+ return vi.fn()
+ })
const isAuthorized = vi
.fn()
.mockResolvedValueOnce(true)
@@ -327,7 +332,15 @@ describe('routes/entityChangeStream', () => {
await vi.advanceTimersByTimeAsync(0)
expect(res.frames).toEqual([])
+ for (let attempt = 0; attempt < 3; attempt += 1) {
+ wakeFeed?.()
+ await vi.advanceTimersByTimeAsync(0)
+ }
+ expect(isAuthorized).toHaveBeenCalledTimes(2)
+ expect(res.frames).toEqual([])
+
await vi.advanceTimersByTimeAsync(configMock.entityChangeStreamPollMs)
+ expect(isAuthorized).toHaveBeenCalledTimes(3)
expect(res.frames.map(frame => JSON.parse(frame))).toContainEqual({
schemaVersion: 1,
type: 'scope.invalidated',