Skip to content

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

TypeScript

fix(desktop): start no producer work for an already-aborted signal (R2-L3)

M92da7b21ae1a
diff --git a/desktop-app/src/__tests__/gfsCancellableInvoke.test.ts b/desktop-app/src/__tests__/gfsCancellableInvoke.test.ts
new file mode 100644
index 000000000..159417eed
--- /dev/null
+++ b/desktop-app/src/__tests__/gfsCancellableInvoke.test.ts
@@ -0,0 +1,65 @@
+/**
+ * R2-L3 — an already-aborted signal must start NO producer work: the invoke is
+ * never sent and no gfs:abort event fires. Also pins the mid-flight contract:
+ * requestId on the invoke, exactly one gfs:abort on abort, listener cleanup on
+ * settle.
+ */
+import { describe, expect, it, vi } from 'vitest'
+import { cancellableInvoke } from '../gfs/cancellableInvoke.js'
+
+function bridge() {
+ return {
+ invoke: vi.fn(() => new Promise(() => undefined)),
+ send: vi.fn(),
+ }
+}
+
+describe('cancellableInvoke (R2-L3)', () => {
+ it('starts no IPC or producer work for an already-aborted signal', async () => {
+ const mockBridge = bridge()
+ const controller = new AbortController()
+ controller.abort()
+
+ await expect(
+ cancellableInvoke(mockBridge, 'gfs:listChildren', { resourceId: 'r' }, controller.signal)
+ ).rejects.toMatchObject({ name: 'AbortError' })
+
+ expect(mockBridge.invoke).not.toHaveBeenCalled()
+ expect(mockBridge.send).not.toHaveBeenCalled()
+ })
+
+ it('sends the requestId, fires gfs:abort exactly once on mid-flight abort, and detaches on settle', async () => {
+ const mockBridge = bridge()
+ const controller = new AbortController()
+
+ const pending = cancellableInvoke(
+ mockBridge,
+ 'gfs:download',
+ { uri: 'gfs://main/x', maxBytes: 10 },
+ controller.signal
+ )
+ expect(mockBridge.invoke).toHaveBeenCalledTimes(1)
+ const payload = mockBridge.invoke.mock.calls[0]?.[1] as { requestId?: string }
+ expect(typeof payload.requestId).toBe('string')
+
+ controller.abort()
+ expect(mockBridge.send).toHaveBeenCalledTimes(1)
+ expect(mockBridge.send).toHaveBeenCalledWith('gfs:abort', { requestId: payload.requestId })
+ // A second abort event (late listener) must not re-send after cleanup.
+ controller.signal.dispatchEvent(new Event('abort'))
+ pending.catch(() => undefined)
+ await Promise.resolve()
+ expect(mockBridge.send).toHaveBeenCalledTimes(1)
+ })
+
+ it('invokes without a signal exactly as a plain pass-through', async () => {
+ const mockBridge = bridge()
+ mockBridge.invoke.mockReturnValue(Promise.resolve({ ok: true }))
+ const result = await cancellableInvoke<{ ok: boolean }>(mockBridge, 'gfs:resolve', {
+ uri: 'gfs://main/x',
+ })
+ expect(result).toEqual({ ok: true })
+ expect(mockBridge.invoke).toHaveBeenCalledWith('gfs:resolve', { uri: 'gfs://main/x' })
+ expect(mockBridge.send).not.toHaveBeenCalled()
+ })
+})
diff --git a/desktop-app/src/gfs/cancellableInvoke.ts b/desktop-app/src/gfs/cancellableInvoke.ts
new file mode 100644
index 000000000..dc7db438c
--- /dev/null
+++ b/desktop-app/src/gfs/cancellableInvoke.ts
@@ -0,0 +1,33 @@
+/**
+ * Preload-side wiring for cancellable invokes (folder-zip Stop, R1-M1/R2-L3).
+ * Pure with an injected ipcRenderer-shaped bridge so the abort ordering is
+ * unit-testable without Electron:
+ *
+ * - an ALREADY-ABORTED signal starts NO producer work: the invoke is never
+ * sent and no abort event is fired (R2-L3) — the caller gets an AbortError
+ * immediately;
+ * - a signal that aborts mid-flight fires `gfs:abort` with the invoke's
+ * requestId exactly once, and the listener detaches when the invoke settles.
+ */
+export interface CancellableIpcBridge {
+ invoke(channel: string, payload: unknown): Promise<unknown>
+ send(channel: string, payload: unknown): void
+}
+
+export function cancellableInvoke<T>(
+ bridge: CancellableIpcBridge,
+ channel: string,
+ payload: Record<string, unknown>,
+ signal?: AbortSignal
+): Promise<T> {
+ if (!signal) return bridge.invoke(channel, payload) as Promise<T>
+ if (signal.aborted) {
+ return Promise.reject(new DOMException('Request was stopped before it started.', 'AbortError'))
+ }
+ const requestId = crypto.randomUUID()
+ const onAbort = () => bridge.send('gfs:abort', { requestId })
+ signal.addEventListener('abort', onAbort, { once: true })
+ return bridge
+ .invoke(channel, { ...payload, requestId })
+ .finally(() => signal.removeEventListener('abort', onAbort)) as Promise<T>
+}
diff --git a/desktop-app/src/preload.ts b/desktop-app/src/preload.ts
index 09bbf8651..784501baf 100644
--- a/desktop-app/src/preload.ts
+++ b/desktop-app/src/preload.ts
@@ -1,5 +1,6 @@
import { contextBridge, ipcRenderer, webUtils } from 'electron'
import type { DesktopCommandId, DesktopCommandSource } from './desktopCommands.js'
+import { cancellableInvoke } from './gfs/cancellableInvoke.js'
import type { PluginConsentRequest } from './pluginSdkProtocol.js'
import type {
EntityChangeStreamEvent,
@@ -46,23 +47,22 @@ const DESKTOP_COMMAND_IDS = new Set<DesktopCommandId>([
])
/**
- * Producer-side cancellation wiring (folder-zip Stop, R1-M1): a cancellable
- * invoke carries a requestId; if the caller's signal aborts, tell main to abort
- * the in-flight request. The cleanup detaches the listener once the invoke
- * settles either way.
+ * Producer-side cancellation wiring (folder-zip Stop, R1-M1): see
+ * gfs/cancellableInvoke.ts — a cancellable invoke carries a requestId; if the
+ * caller's signal aborts, main aborts the in-flight request. An already-
+ * aborted signal never starts producer work at all (R2-L3).
*/
-function attachRequestAbort(signal?: AbortSignal): {
- requestId?: string
- cleanup: () => void
-} {
- if (!signal) return { cleanup: () => undefined }
- const requestId = crypto.randomUUID()
- const onAbort = () => ipcRenderer.send('gfs:abort', { requestId })
- signal.addEventListener('abort', onAbort, { once: true })
- return {
- requestId,
- cleanup: () => signal.removeEventListener('abort', onAbort),
- }
+function cancellableGfsInvoke<T>(
+ channel: string,
+ payload: Record<string, unknown>,
+ signal?: AbortSignal
+): Promise<T> {
+ return cancellableInvoke<T>(
+ { invoke: (c, p) => ipcRenderer.invoke(c, p), send: (c, p) => ipcRenderer.send(c, p) },
+ channel,
+ payload,
+ signal
+ )
}
function isDesktopCommandId(value: unknown): value is DesktopCommandId {
@@ -163,16 +163,8 @@ const clerum = Object.freeze({
},
gfs: {
resolve: (uri: string) => ipcRenderer.invoke('gfs:resolve', { uri }),
- download: (uri: string, options?: { maxBytes?: number; signal?: AbortSignal }) => {
- const abort = attachRequestAbort(options?.signal)
- return ipcRenderer
- .invoke('gfs:download', {
- uri,
- maxBytes: options?.maxBytes,
- requestId: abort.requestId,
- })
- .finally(abort.cleanup)
- },
+ download: (uri: string, options?: { maxBytes?: number; signal?: AbortSignal }) =>
+ cancellableGfsInvoke('gfs:download', { uri, maxBytes: options?.maxBytes }, options?.signal),
downloadPreview: (uri: string, maxBytes: number) =>
ipcRenderer.invoke('gfs:downloadPreview', { uri, maxBytes }),
listAccessible: (drive?: string, cursor?: string) =>
@@ -182,12 +174,7 @@ const clerum = Object.freeze({
drive?: string,
cursor?: string,
options?: { signal?: AbortSignal }
- ) => {
- const abort = attachRequestAbort(options?.signal)
- return ipcRenderer
- .invoke('gfs:listChildren', { resourceId, drive, cursor, requestId: abort.requestId })
- .finally(abort.cleanup)
- },
+ ) => cancellableGfsInvoke('gfs:listChildren', { resourceId, drive, cursor }, options?.signal),
affordances: (resourceId: string, drive?: string) =>
ipcRenderer.invoke('gfs:affordances', { resourceId, drive }),
createFolder: (parentResourceId: string, name: string, drive?: string) =>