Skip to content

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

TypeScript

refactor(control-ui): r7-m8 centralize load arbitration

Codexdfde3cab4b3a
diff --git a/control-ui/components/GfsBrowser.tsx b/control-ui/components/GfsBrowser.tsx
index 413ed3111..72f23acf1 100644
--- a/control-ui/components/GfsBrowser.tsx
+++ b/control-ui/components/GfsBrowser.tsx
@@ -54,7 +54,7 @@ import {
uploadGfsFileLegacy,
} from '@lib/gfsFileUpload'
import { gfsImagePreviewMimeType } from '@lib/gfsImagePreview'
-import { isCurrentGfsLoad } from '@lib/gfsLoadArbitration'
+import { GfsLoadArbiter } from '@lib/gfsLoadArbitration'
import { isGfsMarkdownPreviewFile } from '@lib/gfsMarkdownPreview'
import { isGfsVideoFile } from '@lib/gfsVideoFile'
import { gfsVideoPreviewMimeType } from '@lib/gfsVideoPreview'
@@ -337,8 +337,7 @@ export function GfsBrowser(): React.JSX.Element {
const [loading, setLoading] = useState(true)
const [loadingMore, setLoadingMore] = useState(false)
const [error, setError] = useState('')
- const loadSeqRef = useRef(0)
- const backgroundLoadSeqRef = useRef(0)
+ const loadArbiterRef = useRef(new GfsLoadArbiter())
const entityChangeRefetchControllerRef = useRef<AbortController | null>(null)
const loadedPageCountRef = useRef(1)
const loadedLocationRef = useRef<string | null | undefined>(undefined)
@@ -472,24 +471,9 @@ export function GfsBrowser(): React.JSX.Element {
): Promise<void> => {
const appending = Boolean(cursor)
const background = Boolean(options?.background)
- // Background revalidation has its own sequence: it can be superseded by
- // user navigation, but never invalidates that navigation's request.
- const navigationSeq = background ? loadSeqRef.current : ++loadSeqRef.current
- const backgroundSeq = background ? ++backgroundLoadSeqRef.current : undefined
- const loadToken = background
- ? {
- kind: 'background' as const,
- navigationSequence: navigationSeq,
- backgroundSequence: backgroundSeq!,
- }
- : {
- kind: 'foreground' as const,
- navigationSequence: navigationSeq,
- backgroundSequenceAtStart: backgroundLoadSeqRef.current,
- }
- const isCurrent = () =>
- !options?.signal?.aborted &&
- isCurrentGfsLoad(loadToken, loadSeqRef.current, backgroundLoadSeqRef.current)
+ const loadArbiter = loadArbiterRef.current
+ const loadToken = background ? loadArbiter.beginBackground() : loadArbiter.beginForeground()
+ const isCurrent = () => !options?.signal?.aborted && loadArbiter.isCurrent(loadToken)
if (!background && !cursor) loadedPageCountRef.current = 1
if (appending) {
setLoadingMore(true)
@@ -810,7 +794,7 @@ export function GfsBrowser(): React.JSX.Element {
performVisibleStateRevalidation = async (cursor?: string) => {
// Retire an older same-folder focus/cache refresh before publishing the
// newer stream-authoritative listing. Its late response must not win.
- backgroundLoadSeqRef.current += 1
+ loadArbiterRef.current.beginStreamRevalidation()
// A committed remote change supersedes any local move/retry ancestry
// reconstruction still in flight. Its older response must not replace
// the hierarchy we are about to refetch from the authoritative API.
diff --git a/control-ui/lib/__tests__/gfsLoadArbitration.test.ts b/control-ui/lib/__tests__/gfsLoadArbitration.test.ts
index 4315a5754..ed8829a5d 100644
--- a/control-ui/lib/__tests__/gfsLoadArbitration.test.ts
+++ b/control-ui/lib/__tests__/gfsLoadArbitration.test.ts
@@ -1,7 +1,22 @@
import { describe, expect, it } from 'vitest'
-import { isCurrentGfsLoad } from '../gfsLoadArbitration'
+import { GfsLoadArbiter, isCurrentGfsLoad } from '../gfsLoadArbitration'
describe('GFS list-load arbitration', () => {
+ it('retires stale background reads without superseding foreground work', () => {
+ const arbiter = new GfsLoadArbiter()
+ const background = arbiter.beginBackground()
+ const foreground = arbiter.beginForeground()
+
+ expect(arbiter.isCurrent(background)).toBe(false)
+ expect(arbiter.isCurrent(foreground)).toBe(true)
+
+ const newerBackground = arbiter.beginBackground()
+ arbiter.beginStreamRevalidation()
+
+ expect(arbiter.isCurrent(newerBackground)).toBe(false)
+ expect(arbiter.isCurrent(foreground)).toBe(true)
+ })
+
it('does not let a later background revalidation invalidate a foreground load', () => {
const foreground = {
kind: 'foreground' as const,
diff --git a/control-ui/lib/gfsLoadArbitration.ts b/control-ui/lib/gfsLoadArbitration.ts
index 357caa01e..367fe7999 100644
--- a/control-ui/lib/gfsLoadArbitration.ts
+++ b/control-ui/lib/gfsLoadArbitration.ts
@@ -2,6 +2,39 @@ export type GfsLoadToken =
| { kind: 'foreground'; navigationSequence: number; backgroundSequenceAtStart: number }
| { kind: 'background'; navigationSequence: number; backgroundSequence: number }
+/** Own the sequence rules for foreground navigation and soft stream refreshes. */
+export class GfsLoadArbiter {
+ private navigationSequence = 0
+ private backgroundSequence = 0
+
+ beginForeground(): GfsLoadToken {
+ this.navigationSequence += 1
+ return {
+ kind: 'foreground',
+ navigationSequence: this.navigationSequence,
+ backgroundSequenceAtStart: this.backgroundSequence,
+ }
+ }
+
+ beginBackground(): GfsLoadToken {
+ this.backgroundSequence += 1
+ return {
+ kind: 'background',
+ navigationSequence: this.navigationSequence,
+ backgroundSequence: this.backgroundSequence,
+ }
+ }
+
+ /** A committed stream state supersedes older background reads, not user work. */
+ beginStreamRevalidation(): void {
+ this.backgroundSequence += 1
+ }
+
+ isCurrent(token: GfsLoadToken): boolean {
+ return isCurrentGfsLoad(token, this.navigationSequence, this.backgroundSequence)
+ }
+}
+
/** Decide whether a list response still owns the visible folder state. */
export function isCurrentGfsLoad(
token: GfsLoadToken,
diff --git a/control-ui/package-lock.json b/control-ui/package-lock.json
index d2f07540f..f7d39b925 100644
--- a/control-ui/package-lock.json
+++ b/control-ui/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-ui",
- "version": "0.1.673",
+ "version": "0.1.674",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-ui",
- "version": "0.1.673",
+ "version": "0.1.674",
"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 d55750d71..62048b43c 100644
--- a/control-ui/package.json
+++ b/control-ui/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-ui",
- "version": "0.1.673",
+ "version": "0.1.674",
"private": true,
"scripts": {
"dev": "NEXT_IGNORE_INCORRECT_LOCKFILE=1 next dev",