Skip to content

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

TypeScript

fix(control-ui): bound invalidation coalescing wait (r8-m8)

Josuéa2dae8823de4
diff --git a/control-ui/lib/__tests__/coalescedRevalidation.test.ts b/control-ui/lib/__tests__/coalescedRevalidation.test.ts
new file mode 100644
index 000000000..9059f8376
--- /dev/null
+++ b/control-ui/lib/__tests__/coalescedRevalidation.test.ts
@@ -0,0 +1,21 @@
+import { afterEach, describe, expect, it, vi } from 'vitest'
+import { createCoalescedRevalidation } from '../coalescedRevalidation'
+
+describe('coalesced GFS revalidation', () => {
+ afterEach(() => vi.useRealTimers())
+
+ it('runs a latest-state pass within the maximum wait during a continuous burst', async () => {
+ vi.useFakeTimers()
+ const run = vi.fn(async (_latest: string | undefined) => undefined)
+ const revalidation = createCoalescedRevalidation(run, 100)
+
+ for (let index = 0; index < 20; index += 1) {
+ revalidation.request(`cursor-${index}`)
+ await vi.advanceTimersByTimeAsync(50)
+ }
+
+ expect(run).toHaveBeenCalledTimes(1)
+ expect(run).toHaveBeenCalledWith('cursor-19')
+ revalidation.dispose()
+ })
+})
diff --git a/control-ui/lib/coalescedRevalidation.ts b/control-ui/lib/coalescedRevalidation.ts
index 474a5d2d8..cbc0f4c70 100644
--- a/control-ui/lib/coalescedRevalidation.ts
+++ b/control-ui/lib/coalescedRevalidation.ts
@@ -6,30 +6,38 @@ export interface CoalescedRevalidation<T> {
/** Serialize latest-state refreshes, coalescing bursts and preserving one trailing pass. */
export function createCoalescedRevalidation<T>(
run: (latest: T | undefined) => Promise<void>,
- delayMs = 100
+ delayMs = 100,
+ maxWaitMs = 1000
): CoalescedRevalidation<T> {
let timer: ReturnType<typeof setTimeout> | null = null
+ let maxTimer: ReturnType<typeof setTimeout> | null = null
let running = false
let pending = false
let disposed = false
let latest: T | undefined
+ const flush = () => {
+ if (timer) clearTimeout(timer)
+ if (maxTimer) clearTimeout(maxTimer)
+ timer = null
+ maxTimer = null
+ if (disposed || running || !pending) return
+ const value = latest
+ pending = false
+ latest = undefined
+ running = true
+ void run(value)
+ .catch(() => undefined)
+ .finally(() => {
+ running = false
+ schedule()
+ })
+ }
+
const schedule = () => {
- if (disposed || timer || running || !pending) return
- timer = setTimeout(() => {
- timer = null
- if (disposed || running || !pending) return
- const value = latest
- pending = false
- latest = undefined
- running = true
- void run(value)
- .catch(() => undefined)
- .finally(() => {
- running = false
- schedule()
- })
- }, delayMs)
+ if (disposed || running || !pending) return
+ if (!timer) timer = setTimeout(flush, delayMs)
+ if (!maxTimer) maxTimer = setTimeout(flush, maxWaitMs)
}
return {
@@ -47,6 +55,8 @@ export function createCoalescedRevalidation<T>(
latest = undefined
if (timer) clearTimeout(timer)
timer = null
+ if (maxTimer) clearTimeout(maxTimer)
+ maxTimer = null
},
}
}
diff --git a/control-ui/package-lock.json b/control-ui/package-lock.json
index 1cc1a37ed..b494122f7 100644
--- a/control-ui/package-lock.json
+++ b/control-ui/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-ui",
- "version": "0.1.693",
+ "version": "0.1.694",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-ui",
- "version": "0.1.693",
+ "version": "0.1.694",
"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.json
index 18d73d4fd..153cdfffe 100644
--- a/control-ui/package.json
+++ b/control-ui/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-ui",
- "version": "0.1.693",
+ "version": "0.1.694",
"private": true,
"scripts": {
"dev": "NEXT_IGNORE_INCORRECT_LOCKFILE=1 next dev",