Skip to content

The agent that grows with you

fix(desktop): fail closed on selected quick-entry submits

LeonardoLGDSd3e3f9d8a769
diff --git a/apps/desktop/src/app/contrib/hooks/use-quick-entry-bridge.test.ts b/apps/desktop/src/app/contrib/hooks/use-quick-entry-bridge.test.ts
index e7dc992d08e..f7eb29a3a3c 100644
--- a/apps/desktop/src/app/contrib/hooks/use-quick-entry-bridge.test.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-quick-entry-bridge.test.ts
@@ -1,5 +1,28 @@
-import { describe, expect, it } from 'vitest'
-import { quickEntrySubmitAck } from './use-quick-entry-bridge'
+import { act, createElement } from 'react'
+import { createRoot, type Root } from 'react-dom/client'
+import { afterEach, describe, expect, it, vi } from 'vitest'
+import { useQuickEntryBridge, quickEntrySubmitAck } from './use-quick-entry-bridge'
+import { sessionTileDelegate } from '@/store/session-states'
+
+const registeredSubmitHandler = vi.hoisted(() => ({
+ current: null as ((payload: { correlationId: string; target: string; text: string }) => void) | null
+}))
+
+vi.mock('@/store/quick-entry', async importOriginal => {
+ const actual = await importOriginal<typeof import('@/store/quick-entry')>()
+
+ return {
+ ...actual,
+ setQuickEntrySubmitHandler(fn: Parameters<typeof actual.setQuickEntrySubmitHandler>[0]) {
+ registeredSubmitHandler.current = fn
+ actual.setQuickEntrySubmitHandler(fn)
+ }
+ }
+})
+
+vi.mock('@/store/session-states', () => ({
+ sessionTileDelegate: vi.fn()
+}))
describe('quickEntrySubmitAck', () => {
it('reports a rejected prompt as failure instead of acknowledging success', () => {
@@ -15,3 +38,77 @@ describe('quickEntrySubmitAck', () => {
expect(quickEntrySubmitAck(true)).toEqual({ ok: true })
})
})
+
+describe('useQuickEntryBridge', () => {
+ const originalHermesDesktop = window.hermesDesktop
+ let root: Root | null = null
+
+ afterEach(() => {
+ root?.unmount()
+ root = null
+ window.hermesDesktop = originalHermesDesktop
+ vi.clearAllMocks()
+ })
+
+ it('does not acknowledge a selected-session void submit as success', async () => {
+ const correlationId = 'selected-submit-correlation'
+ const ackSubmit = vi.fn()
+ ackSubmit.mockImplementationOnce(() => {
+ throw new Error('ack channel failed')
+ })
+ window.hermesDesktop = {
+ quickEntry: {
+ ackSubmit,
+ pushState: vi.fn()
+ }
+ } as typeof window.hermesDesktop
+
+ const resumeTile = vi.fn(async () => 'runtime-session-1')
+ const submitToSession = vi.fn(async () => {})
+ vi.mocked(sessionTileDelegate).mockReturnValue({
+ resumeTile,
+ submitToSession
+ } as ReturnType<typeof sessionTileDelegate>)
+
+ function Harness() {
+ useQuickEntryBridge({
+ submitText: () => true,
+ submitTextToNewSession: async () => ({
+ runtimeSessionId: 'runtime-new',
+ sessionId: 'stored-new'
+ })
+ })
+
+ return null
+ }
+
+ const container = document.createElement('div')
+ document.body.appendChild(container)
+ root = createRoot(container)
+ await act(async () => {
+ root?.render(createElement(Harness))
+ })
+
+ expect(registeredSubmitHandler.current).toBeTypeOf('function')
+
+ await act(async () => {
+ await registeredSubmitHandler.current?.({
+ correlationId,
+ target: 'stored-session-1',
+ text: 'Send from Quick Entry'
+ })
+ })
+
+ expect(resumeTile).toHaveBeenCalledWith('stored-session-1')
+ expect(submitToSession).toHaveBeenCalledWith('runtime-session-1', 'Send from Quick Entry')
+ expect(ackSubmit).toHaveBeenCalledTimes(1)
+ expect(ackSubmit).toHaveBeenCalledWith(correlationId, {
+ code: 'submit-failed',
+ message: 'The selected session prompt was dispatched, but backend acceptance is unknown.',
+ ok: false,
+ retryable: false
+ })
+
+ container.remove()
+ })
+})
diff --git a/apps/desktop/src/app/contrib/hooks/use-quick-entry-bridge.ts b/apps/desktop/src/app/contrib/hooks/use-quick-entry-bridge.ts
index 62c5878a8f9..95937028da7 100644
--- a/apps/desktop/src/app/contrib/hooks/use-quick-entry-bridge.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-quick-entry-bridge.ts
@@ -82,8 +82,16 @@ export function useQuickEntryBridge({
}
setQuickEntrySubmitHandler(async ({ correlationId, target, text }) => {
- const ack = (result: QuickEntrySubmitResult) =>
+ let acknowledged = false
+
+ const ack = (result: QuickEntrySubmitResult) => {
+ if (acknowledged) {
+ return
+ }
+
+ acknowledged = true
window.hermesDesktop?.quickEntry.ackSubmit(correlationId, result)
+ }
if (target === QUICK_TARGET_NEW) {
// Create and submit as one route-neutral operation so drift cannot
@@ -104,11 +112,31 @@ export function useQuickEntryBridge({
const delegate = sessionTileDelegate()
if (delegate) {
+ let promptDispatched = false
+
try {
const runtimeId = await delegate.resumeTile(target)
+ promptDispatched = true
await delegate.submitToSession(runtimeId, text)
- ack({ ok: true })
+
+ ack({
+ code: 'submit-failed',
+ message: 'The selected session prompt was dispatched, but backend acceptance is unknown.',
+ ok: false,
+ retryable: false
+ })
} catch (error) {
+ if (promptDispatched) {
+ ack({
+ code: 'submit-failed',
+ message: 'The selected session prompt was dispatched, but backend acceptance is unknown.',
+ ok: false,
+ retryable: false
+ })
+
+ return
+ }
+
ack({ code: 'submit-failed', message: error instanceof Error ? error.message : String(error), ok: false, retryable: true })
}