Skip to content

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

TypeScript

fix(control-ui): r6-h12 route stream expiry through auth handler

Josué04fb22c72d59
diff --git a/control-ui/components/GfsBrowser.tsx b/control-ui/components/GfsBrowser.tsx
index 528019652..5f4799473 100644
--- a/control-ui/components/GfsBrowser.tsx
+++ b/control-ui/components/GfsBrowser.tsx
@@ -33,7 +33,14 @@ import {
IconX,
} from '@components/icons'
import { Button, Field, TextInput } from '@components/ui'
-import { apiGet, apiSend, getGfsResourceByPath, gfsDownload, isSilentApiError } from '@lib/api'
+import {
+ apiGet,
+ apiSend,
+ getGfsResourceByPath,
+ gfsDownload,
+ handleControlUIUnauthorized,
+ isSilentApiError,
+} from '@lib/api'
import { parseEntityChangeFrame } from '@lib/entityChangeStream'
import { isGfsDocumentFile } from '@lib/gfsDocumentFile'
import {
@@ -1036,6 +1043,11 @@ export function GfsBrowser(): React.JSX.Element {
headers: { accept: 'application/x-ndjson' },
signal: controller.signal,
})
+ if (response.status === 401) {
+ active = false
+ handleControlUIUnauthorized()
+ return
+ }
if (!response.ok || !response.body) {
throw new Error(`Entity-change stream returned ${response.status}`)
}
@@ -1064,6 +1076,11 @@ export function GfsBrowser(): React.JSX.Element {
invalidateVisibleState(frame.cursor)
} else if (frame.type === 'stream.closing') {
await reader.cancel()
+ if (frame.reason === 'session_expired') {
+ active = false
+ handleControlUIUnauthorized()
+ return
+ }
break
}
newline = pending.indexOf('\n')
diff --git a/control-ui/components/__tests__/GfsBrowser.test.tsx b/control-ui/components/__tests__/GfsBrowser.test.tsx
index 8168aed49..b7efab08e 100644
--- a/control-ui/components/__tests__/GfsBrowser.test.tsx
+++ b/control-ui/components/__tests__/GfsBrowser.test.tsx
@@ -17,6 +17,7 @@ import {
getRecipes,
gfsDownload,
gfsFetchFileBlob,
+ handleControlUIUnauthorized,
putGfsGrant,
} from '@lib/api'
import {
@@ -37,6 +38,7 @@ import { ToastProvider } from '../Toast'
vi.mock('@lib/api', () => ({
apiGet: vi.fn(),
apiSend: vi.fn(),
+ handleControlUIUnauthorized: vi.fn(),
GFS_UPLOAD_TIMEOUT_MS: 300000,
getAdminTeams: vi.fn(),
getAdminUsers: vi.fn(),
@@ -101,6 +103,7 @@ vi.mock('../../../control-api/src/observability/metrics.js', () => controlApiPro
const mockApiGet = apiGet as unknown as ReturnType<typeof vi.fn>
const mockApiSend = apiSend as unknown as ReturnType<typeof vi.fn>
+const mockHandleControlUIUnauthorized = vi.mocked(handleControlUIUnauthorized)
const mockGetAdminUsers = vi.mocked(getAdminUsers)
const mockGetAdminTeams = vi.mocked(getAdminTeams)
const mockGetGfsGrants = vi.mocked(getGfsGrants)
@@ -181,6 +184,35 @@ async function controlApiProducerFrame(
return frame
}
+async function controlApiSessionExpiredProducerFrame(): Promise<string> {
+ const cursor = 'd119f895-1ef8-4e73-8f08-f9754919682a'
+ controlApiProducer.readEntityChangeCheckpoint.mockResolvedValue({
+ resyncRequired: false,
+ cursor,
+ scopes: ['gfs'],
+ })
+ let authorizationChecks = 0
+ const req = new ControlApiProducerRequest()
+ const res = new ControlApiProducerResponse()
+ streamEntityChanges(
+ req as unknown as Parameters<typeof streamEntityChanges>[0],
+ res as unknown as Parameters<typeof streamEntityChanges>[1],
+ null,
+ async () => ++authorizationChecks === 1,
+ 'operator',
+ 'operator-session-expiry-test'
+ )
+ await vi.waitFor(() => expect(res.writableEnded).toBe(true))
+ const frame = res.frames
+ .map(value => value.trim())
+ .find(value => {
+ const parsed = JSON.parse(value) as { type?: string; reason?: string }
+ return parsed.type === 'stream.closing' && parsed.reason === 'session_expired'
+ })
+ if (!frame) throw new Error('Control API producer did not emit session_expired')
+ return frame
+}
+
function renderBrowser() {
return render(
<ToastProvider>
@@ -263,6 +295,7 @@ describe('GfsBrowser', () => {
controlApiProducer.subscribeEntityChangeFeedWake.mockReset().mockReturnValue(vi.fn())
mockApiGet.mockReset()
mockApiSend.mockReset()
+ mockHandleControlUIUnauthorized.mockReset()
mockGetAdminUsers.mockReset()
mockGetAdminTeams.mockReset()
mockGetGfsGrants.mockReset()
@@ -624,6 +657,35 @@ describe('GfsBrowser', () => {
expect(JSON.stringify(mockApiSend.mock.calls)).toContain('remote-folder')
})
+ it('routes producer-issued session expiry through the existing auth handler and stops the stream', async () => {
+ mockApiGet.mockResolvedValue({ items: [], nextCursor: null })
+ const frame = await controlApiSessionExpiredProducerFrame()
+ const fetchMock = vi.fn(
+ async () =>
+ new Response(`${frame}\n`, {
+ status: 200,
+ headers: { 'content-type': 'application/x-ndjson' },
+ })
+ )
+ vi.stubGlobal('fetch', fetchMock)
+
+ renderBrowser()
+
+ await waitFor(() => expect(mockHandleControlUIUnauthorized).toHaveBeenCalledOnce())
+ expect(fetchMock).toHaveBeenCalledOnce()
+ })
+
+ it('routes an unauthenticated stream 401 through the existing auth handler without retrying', async () => {
+ mockApiGet.mockResolvedValue({ items: [], nextCursor: null })
+ const fetchMock = vi.fn(async () => new Response(null, { status: 401 }))
+ vi.stubGlobal('fetch', fetchMock)
+
+ renderBrowser()
+
+ await waitFor(() => expect(mockHandleControlUIUnauthorized).toHaveBeenCalledOnce())
+ expect(fetchMock).toHaveBeenCalledOnce()
+ })
+
it('keeps the loaded list and Rename dialog visible during a soft stream refresh', async () => {
const rootId = '11111111-1111-1111-1111-111111111111'
const report = child('report.md', 'file', 1, 4)
diff --git a/control-ui/lib/__tests__/apiErrors.test.ts b/control-ui/lib/__tests__/apiErrors.test.ts
index 8ac3d0ac8..c090a9f6c 100644
--- a/control-ui/lib/__tests__/apiErrors.test.ts
+++ b/control-ui/lib/__tests__/apiErrors.test.ts
@@ -1,5 +1,18 @@
-import { describe, expect, it } from 'vitest'
-import { formatApiError } from '../api'
+import { describe, expect, it, vi } from 'vitest'
+import { formatApiError, handleControlUIUnauthorized, setGlobalAuthErrorHandler } from '../api'
+
+describe('handleControlUIUnauthorized', () => {
+ it('clears the legacy browser token and invokes the registered session-expiry handler', () => {
+ const handler = vi.fn()
+ window.localStorage.setItem('controlUiAdminToken', 'synthetic-test-token')
+ setGlobalAuthErrorHandler(handler)
+
+ handleControlUIUnauthorized()
+
+ expect(window.localStorage.getItem('controlUiAdminToken')).toBeNull()
+ expect(handler).toHaveBeenCalledOnce()
+ })
+})
describe('formatApiError', () => {
it('preserves a nested structured error without rendering object Object', () => {
diff --git a/control-ui/lib/api.ts b/control-ui/lib/api.ts
index 12da16e04..5484f0b77 100644
--- a/control-ui/lib/api.ts
+++ b/control-ui/lib/api.ts
@@ -89,10 +89,14 @@ export function isSilentApiError(error: unknown): boolean {
return Boolean(error && typeof error === 'object' && (error as { silent?: unknown }).silent)
}
-function handleUnauthorized(): never {
+/** Route non-JSON authenticated transports through the same session-expiry handler as API calls. */
+export function handleControlUIUnauthorized(): void {
clearAdminAuthToken()
- const handler = getGlobalAuthErrorHandler()
- handler?.()
+ getGlobalAuthErrorHandler()?.()
+}
+
+function handleUnauthorized(): never {
+ handleControlUIUnauthorized()
throw new AuthExpiredError()
}
diff --git a/control-ui/package-lock.json b/control-ui/package-lock.json
index 9f394d2af..299d2d216 100644
--- a/control-ui/package-lock.json
+++ b/control-ui/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-ui",
- "version": "0.1.666",
+ "version": "0.1.667",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-ui",
- "version": "0.1.666",
+ "version": "0.1.667",
"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 d782553c7..0e8b91f31 100644
--- a/control-ui/package.json
+++ b/control-ui/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-ui",
- "version": "0.1.666",
+ "version": "0.1.667",
"private": true,
"scripts": {
"dev": "NEXT_IGNORE_INCORRECT_LOCKFILE=1 next dev",