Skip to content

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

TypeScript

fix(desktop-ui): r6-m1 define logical switch feedback ownership

Josué046f2cc8ae8e
diff --git a/desktop-app/ui/src/lib/__tests__/chatSwitchRequest.test.ts b/desktop-app/ui/src/lib/__tests__/chatSwitchRequest.test.ts
new file mode 100644
index 000000000..8a1a47f48
--- /dev/null
+++ b/desktop-app/ui/src/lib/__tests__/chatSwitchRequest.test.ts
@@ -0,0 +1,51 @@
+import { describe, expect, it, vi } from 'vitest'
+import fc from 'fast-check'
+import { runChatSwitchRequest } from '../chatSwitchRequest'
+
+describe('logical switch request invariants', () => {
+ it('composes duplicate awaiters without duplicate execution or reports', async () => {
+ await fc.assert(
+ fc.asyncProperty(
+ fc.boolean(),
+ fc.boolean(),
+ fc.integer({ min: 1, max: 30 }),
+ async (current, succeeds, observerCount) => {
+ const error = new Error('request failure')
+ const execute = vi.fn(() => (succeeds ? Promise.resolve() : Promise.reject(error)))
+ const report = vi.fn()
+ const request = runChatSwitchRequest(execute, () => current, report)
+ const results = await Promise.allSettled(
+ Array.from({ length: observerCount }, () => request)
+ )
+ expect(execute).toHaveBeenCalledTimes(1)
+ expect(report).toHaveBeenCalledTimes(!succeeds && current ? 1 : 0)
+ expect(
+ results.every(result =>
+ succeeds
+ ? result.status === 'fulfilled'
+ : result.status === 'rejected' && result.reason === error
+ )
+ ).toBe(true)
+ }
+ ),
+ { numRuns: 500 }
+ )
+ })
+
+ it('never lets a settled request own a later independent failure', async () => {
+ await fc.assert(
+ fc.asyncProperty(fc.array(fc.boolean(), { minLength: 2, maxLength: 30 }), async outcomes => {
+ const report = vi.fn()
+ for (const succeeds of outcomes) {
+ await runChatSwitchRequest(
+ () => (succeeds ? Promise.resolve() : Promise.reject(new Error('independent failure'))),
+ () => true,
+ report
+ ).catch(() => undefined)
+ }
+ expect(report).toHaveBeenCalledTimes(outcomes.filter(succeeds => !succeeds).length)
+ }),
+ { numRuns: 500 }
+ )
+ })
+})
diff --git a/desktop-app/ui/src/lib/chatSwitchRequest.ts b/desktop-app/ui/src/lib/chatSwitchRequest.ts
new file mode 100644
index 000000000..5ed0f9ec1
--- /dev/null
+++ b/desktop-app/ui/src/lib/chatSwitchRequest.ts
@@ -0,0 +1,11 @@
+/** One execution owns feedback; any number of continuations may await its promise. */
+export function runChatSwitchRequest(
+ execute: () => Promise<void>,
+ isCurrent: () => boolean,
+ onCurrentError: (error: unknown) => void
+): Promise<void> {
+ return execute().catch(error => {
+ if (isCurrent()) onCurrentError(error)
+ throw error
+ })
+}