Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(profile-ui): keep a good release label across a failed refresh
Jose Ramirez09afaba98ba3
diff --git a/profile-ui/app/components/__tests__/ReleaseLabel.test.tsx b/profile-ui/app/components/__tests__/ReleaseLabel.test.tsxindex 3a41bf704..188eca96b 100644--- a/profile-ui/app/components/__tests__/ReleaseLabel.test.tsx+++ b/profile-ui/app/components/__tests__/ReleaseLabel.test.tsx@@ -72,6 +72,21 @@ describe('ReleaseLabel', () => {expect(screen.getByText('Release v0.6.0')).toBeInTheDocument()})+ it('keeps a mounted label when a later refresh fails', async () => {+ api.getDesktopRelease.mockResolvedValue({ releaseId: 'v0.7.0' })++ render(<ReleaseLabel />)+ expect(await screen.findByText('Release v0.7.0')).toBeInTheDocument()++ api.getDesktopRelease.mockRejectedValue(new Error('502 Bad Gateway'))+ await act(async () => {+ await refreshReleaseIdentity()+ })++ expect(screen.getByText('Release v0.7.0')).toBeInTheDocument()+ expect(screen.queryByText('Release unavailable')).not.toBeInTheDocument()+ })+it('publishes one read to every mounted label', async () => {api.getDesktopRelease.mockResolvedValue({ releaseId: 'v0.6.0' })diff --git a/profile-ui/lib/releaseIdentity.ts b/profile-ui/lib/releaseIdentity.tsindex ef85c0dc8..799651ffd 100644--- a/profile-ui/lib/releaseIdentity.ts+++ b/profile-ui/lib/releaseIdentity.ts@@ -14,14 +14,15 @@ export type ReleaseIdentityListener = (identity: ReleaseIdentity | null) => void// formatReleaseLabel, the sidebar brand through formatReleaseTitle.const RELEASE_PREFIX = 'Release'-// The running external-rest-api image bakes its release identity in, so the-// value cannot change while the tab is open. One resolved read is cached for-// the page session and shared by the always-mounted sidebar and the settings-// page. A failed read is deliberately NOT cached: the label is decoration, and-// pinning it to "unavailable" until the next full page load would be the wrong-// trade for one transient error.+// One resolved read is cached for the page session and shared by the+// always-mounted sidebar and the settings page. A failed first read is not+// cached, so the next mount retries. Refresh drops any in-flight read and+// re-asks; a failed re-read keeps the last good value rather than publishing+// "unavailable" over a label that was already correct. loadGeneration lets an+// abandoned in-flight promise finish without clobbering a newer read.let cachedIdentity: ReleaseIdentity | null = nulllet inFlight: Promise<ReleaseIdentity | null> | null = null+let loadGeneration = 0// Every mounted label subscribes, so a read resolved by one of them lands on// all of them. Without this, an instance that caught a transient failure would@@ -38,7 +39,14 @@ export function subscribeReleaseIdentity(listener: ReleaseIdentityListener): ()function publishReleaseIdentity(identity: ReleaseIdentity | null): void {// Iterate a copy: a listener may unsubscribe (unmount) while being notified.- for (const listener of [...listeners]) listener(identity)+ // Isolate each subscriber so a throw cannot rewind the cache or reject the read.+ for (const listener of [...listeners]) {+ try {+ listener(identity)+ } catch {+ // The label is decoration; a broken subscriber is not a failed read.+ }+ }}export function normalizeReleaseIdentity(payload: unknown): ReleaseIdentity | null {@@ -68,39 +76,59 @@ export function readCachedReleaseIdentity(): ReleaseIdentity | null {export function resetReleaseIdentityCache(): void {cachedIdentity = nullinFlight = null+ loadGeneration += 1}export async function loadReleaseIdentity(fetcher: ReleaseIdentityFetcher = getDesktopRelease): Promise<ReleaseIdentity | null> {- if (cachedIdentity) return cachedIdentity+ return readReleaseIdentity(fetcher, false)+}++// Drops any in-flight read so the next load re-asks. The settings Refresh+// button routes through here, which is what lets a label that caught a+// transient failure heal in place rather than waiting to be remounted. A+// failed re-read keeps the last good value; clearing the cache first would+// publish "unavailable" onto every mounted label for one 502.+export async function refreshReleaseIdentity(+ fetcher: ReleaseIdentityFetcher = getDesktopRelease+): Promise<ReleaseIdentity | null> {+ loadGeneration += 1+ inFlight = null+ return readReleaseIdentity(fetcher, true)+}++async function readReleaseIdentity(+ fetcher: ReleaseIdentityFetcher,+ force: boolean+): Promise<ReleaseIdentity | null> {+ if (!force && cachedIdentity) return cachedIdentityif (inFlight) return inFlight- inFlight = (async () => {+ const generation = loadGeneration+ const request = (async (): Promise<ReleaseIdentity | null> => {+ let identity: ReleaseIdentity | null = nulltry {- const identity = normalizeReleaseIdentity(await fetcher())- if (identity) cachedIdentity = identity- publishReleaseIdentity(identity)- return identity+ identity = normalizeReleaseIdentity(await fetcher())} catch {// Never surfaced to the page and never allowed to drive navigation: the// settings page's own getMe() call is what detects a dead session.- publishReleaseIdentity(null)- return null+ identity = null} finally {- inFlight = null+ if (generation === loadGeneration) inFlight = null}- })()- return inFlight-}+ if (generation !== loadGeneration) return cachedIdentity+ if (identity) {+ cachedIdentity = identity+ publishReleaseIdentity(identity)+ return identity+ }+ if (cachedIdentity) return cachedIdentity+ publishReleaseIdentity(null)+ return null+ })()-// Drops a resolved read so the next load re-asks. The settings Refresh button-// routes through here, which is what lets a label that caught a transient-// failure heal in place rather than waiting to be remounted.-export async function refreshReleaseIdentity(- fetcher: ReleaseIdentityFetcher = getDesktopRelease-): Promise<ReleaseIdentity | null> {- cachedIdentity = null- return loadReleaseIdentity(fetcher)+ inFlight = request+ return request}diff --git a/profile-ui/package-lock.json b/profile-ui/package-lock.jsonindex eed5f0c95..259d73165 100644--- a/profile-ui/package-lock.json+++ b/profile-ui/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-profile-ui",- "version": "0.1.125",+ "version": "0.1.126","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-profile-ui",- "version": "0.1.125",+ "version": "0.1.126","dependencies": {"@clerum/desktop-app-links": "file:../packages/desktop-app-links","next": "16.2.11",diff --git a/profile-ui/package.json b/profile-ui/package.jsonindex 71718b5c3..325ffaece 100644--- a/profile-ui/package.json+++ b/profile-ui/package.json@@ -1,6 +1,6 @@{"name": "clerum-profile-ui",- "version": "0.1.125",+ "version": "0.1.126","private": true,"scripts": {"dev": "next dev",diff --git a/profile-ui/test/releaseIdentity.test.ts b/profile-ui/test/releaseIdentity.test.tsindex 1ba713075..a8feffd28 100644--- a/profile-ui/test/releaseIdentity.test.ts+++ b/profile-ui/test/releaseIdentity.test.ts@@ -5,6 +5,7 @@ import {formatReleaseTitle,loadReleaseIdentity,normalizeReleaseIdentity,+ readCachedReleaseIdentity,refreshReleaseIdentity,resetReleaseIdentityCache,subscribeReleaseIdentity,@@ -164,3 +165,67 @@ test('refreshReleaseIdentity re-reads past a cached value and republishes', asynassert.equal(calls, 2)assert.deepEqual(seen, [{ releaseId: 'v0.6.0' }, { releaseId: 'v0.7.0' }])})++// Refresh used to null the cache *before* the new read resolved. A transient+// 502 then published null to every mounted label, turning a correct+// "Release v0.7.0" into "Release unavailable" until the next successful+// refresh or a full reload.+test('refreshReleaseIdentity keeps the last good identity when the re-read fails', async () => {+ resetReleaseIdentityCache()+ const seen: Array<{ releaseId: string } | null> = []+ const unsubscribe = subscribeReleaseIdentity(identity => seen.push(identity))++ await loadReleaseIdentity(async () => ({ releaseId: 'v0.7.0' }))+ const refreshed = await refreshReleaseIdentity(async () => {+ throw new Error('502 Bad Gateway')+ })+ unsubscribe()++ assert.deepEqual(refreshed, { releaseId: 'v0.7.0' })+ assert.deepEqual(readCachedReleaseIdentity(), { releaseId: 'v0.7.0' })+ assert.deepEqual(seen, [{ releaseId: 'v0.7.0' }])+})++// refreshReleaseIdentity used to leave inFlight alone, so a Refresh during+// the mount read returned that same promise and never issued a second+// request — the button existed to repair a stuck read and did nothing.+test('refreshReleaseIdentity starts a new read while one is already in flight', async () => {+ resetReleaseIdentityCache()+ let calls = 0+ let finishFirst: ((value: { releaseId: string }) => void) | undefined+ const firstPayload = new Promise<{ releaseId: string }>(resolve => {+ finishFirst = resolve+ })+ const fetcher = async () => {+ calls += 1+ if (calls === 1) return firstPayload+ return { releaseId: 'v0.8.0-NEW' }+ }++ const firstLoad = loadReleaseIdentity(fetcher)+ const refreshed = refreshReleaseIdentity(fetcher)+ assert.equal(calls, 2)++ const refreshedIdentity = await refreshed+ finishFirst!({ releaseId: 'v0.7.0-OLD' })+ await firstLoad++ assert.deepEqual(refreshedIdentity, { releaseId: 'v0.8.0-NEW' })+ assert.deepEqual(readCachedReleaseIdentity(), { releaseId: 'v0.8.0-NEW' })+})++// publishReleaseIdentity used to run inside the fetch try. A throwing+// subscriber was caught as if the read itself had failed, which published+// null over the good value and returned null from the load.+test('a throwing subscriber does not wipe a resolved release identity', async () => {+ resetReleaseIdentityCache()+ const unsubscribe = subscribeReleaseIdentity(() => {+ throw new Error('subscriber failed')+ })++ const identity = await loadReleaseIdentity(async () => ({ releaseId: 'v0.7.0' }))+ unsubscribe()++ assert.deepEqual(identity, { releaseId: 'v0.7.0' })+ assert.deepEqual(readCachedReleaseIdentity(), { releaseId: 'v0.7.0' })+})