Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(control-ui): let the automatic retry wait for a running family recovery
Alfredo Lopez5f612d06bf0f
diff --git a/control-ui/lib/__tests__/readRequestCache.test.ts b/control-ui/lib/__tests__/readRequestCache.test.tsindex 47253ae10..412c977f6 100644--- a/control-ui/lib/__tests__/readRequestCache.test.ts+++ b/control-ui/lib/__tests__/readRequestCache.test.ts@@ -617,6 +617,56 @@ describe('bounded metadata read reuse', () => {expect(retry).toHaveBeenCalledOnce()})+ it('waits for the family recovery already rereading before it re-runs the consumer', async () => {+ const path = '/api/v1/admin/connections'+ const connections = { metadataRead: 'subscription-connections' } as const+ // The recovery's reread stays in flight until released, and rejects on+ // abort the way a browser fetch does.+ let releaseReread!: () => void+ let rereadAborted = false+ fetchMock.mockResolvedValueOnce(throttled(12)).mockImplementationOnce(+ (_input: RequestInfo | URL, init?: RequestInit) =>+ new Promise<Response>((resolve, reject) => {+ releaseReread = () => resolve(success({ connections: ['recovered'] }))+ init?.signal?.addEventListener('abort', () => {+ rereadAborted = true+ reject(new DOMException('The read was cancelled', 'AbortError'))+ })+ })+ )+ // The consumer's effect: its cleanup aborts the signal registered as the+ // recovery's interest, then the effect reads again.+ const effect = new AbortController()+ let denial: unknown+ await apiGet(path, {}, { ...connections, signal: effect.signal }).catch(error => {+ denial = error+ })+ expect(denial).toMatchObject({ status: 429 })+ const rerun = vi.fn(() => {+ effect.abort()+ return apiGet(path, {}, connections)+ })+ // Scheduled after the recovery, as the consumer does, so at the shared+ // deadline the recovery's timer fires first and its reread is in flight+ // when this one fires.+ scheduleReadRequestRetry(denial as Parameters<typeof scheduleReadRequestRetry>[0], rerun)++ await vi.advanceTimersByTimeAsync(12_000)+ // Witness: the recovery is rereading at the deadline.+ expect(fetchMock).toHaveBeenCalledTimes(2)+ expect(rerun).not.toHaveBeenCalled()+ expect(rereadAborted).toBe(false)++ releaseReread()+ await vi.advanceTimersByTimeAsync(0)+ // The consumer re-runs once the recovery settled, and the recovered entry+ // serves its read.+ expect(rerun).toHaveBeenCalledOnce()+ await expect(rerun.mock.results[0]?.value).resolves.toEqual({ connections: ['recovered'] })+ expect(rereadAborted).toBe(false)+ expect(fetchMock).toHaveBeenCalledTimes(2)+ })+it('changes the session identity only when the confirmed principal changes', () => {const start = getReadRequestSessionIdentity()setReadRequestCacheEntry('entry', { value: 1 }, 30_000)diff --git a/control-ui/lib/readRequestCache.ts b/control-ui/lib/readRequestCache.tsindex 6f1284605..772de7734 100644--- a/control-ui/lib/readRequestCache.ts+++ b/control-ui/lib/readRequestCache.ts@@ -43,6 +43,9 @@ const READ_REQUEST_INVALIDATION_CHANNEL = 'control-ui-read-metadata-invalidationconst entries = new Map<string, CacheEntry>()const cooldowns = new Map<string, CooldownEntry>()const recoveries = new Map<string, RecoveryEntry>()+// The family each denial belongs to, so a consumer's automatic retry can find+// the recovery scheduled for that denial without knowing the family key.+const denialFamilies = new WeakMap<ApiRequestError, string>()let principal: PrincipalContext | null = nulllet invalidationChannel: BroadcastChannel | null = nulllet invalidationHandler: ((remote?: boolean) => void) | null = null@@ -217,6 +220,7 @@ export function setReadRequestCooldown(const retryAtMs = Math.max(nowMs + delayMs, current?.retryAtMs ?? 0)const issued = current?.issued ?? new Set<ApiRequestError>()issued.add(error)+ denialFamilies.set(error, key)for (const denied of issued) denied.retryAtMs = retryAtMswhile (cooldowns.size >= METADATA_READ_CACHE_MAX_ENTRIES && !cooldowns.has(key)) {const oldest = cooldowns.keys().next().value@@ -391,23 +395,39 @@ export function completeReadRequestRecovery(key: string, memberKey: string): voi* Run `retry` once a denial's retry time has passed. A later denial in the same* family can extend that time on the error itself (setReadRequestCooldown), so* the timer re-arms until the extended time is reached instead of firing into- * the cooldown. Returns the cancel function.+ * the cooldown. When the denial's family recovery is due or already rereading,+ * `retry` runs only once it settles: re-running the consumer earlier would+ * release the recovery's registered interest, cancel its reread and spend a+ * read of its own. Returns the cancel function.*/export function scheduleReadRequestRetry(error: ApiRequestError, retry: () => void): () => void {const untimedRetryAtMs = Date.now() + METADATA_READ_UNTIMED_COOLDOWN_MSlet timer: ReturnType<typeof setTimeout>+ let cancelled = false+ const runRetry = () => {+ if (!cancelled) retry()+ }const arm = () => {const delayMs = (error.retryAtMs ?? untimedRetryAtMs) - Date.now()timer = setTimeout(() => {- if (Date.now() < (error.retryAtMs ?? untimedRetryAtMs)) arm()- else retry()+ if (Date.now() < (error.retryAtMs ?? untimedRetryAtMs)) {+ arm()+ return+ }+ const family = denialFamilies.get(error)+ const recovery = family ? getReadRequestRecovery(family) : undefined+ if (recovery) recovery.then(runRetry, runRetry)+ else runRetry()},Math.min(2_147_483_647, Math.max(0, delayMs)))}arm()- return () => clearTimeout(timer)+ return () => {+ cancelled = true+ clearTimeout(timer)+ }}export function __resetReadRequestCacheForTests(): void {diff --git a/control-ui/package-lock.json b/control-ui/package-lock.jsonindex d9687a302..52d8e326e 100644--- a/control-ui/package-lock.json+++ b/control-ui/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-ui",- "version": "0.1.719",+ "version": "0.1.720","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-ui",- "version": "0.1.719",+ "version": "0.1.720","dependencies": {"@clerum/display-field": "file:../packages/display-field","@clerum/frontend-components": "file:../packages/frontend-components",diff --git a/control-ui/package.json b/control-ui/package.jsonindex 7b0d7da53..d302541cf 100644--- a/control-ui/package.json+++ b/control-ui/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-ui",- "version": "0.1.719",+ "version": "0.1.720","private": true,"scripts": {"dev": "NEXT_IGNORE_INCORRECT_LOCKFILE=1 next dev",