Skip to content

The agent that grows with you

fix(quick-entry): keep the draft and correlation through an unresolved submit

LeonardoLGDS0fc30d63ab11
diff --git a/apps/desktop/src/app/quick-entry/quick-entry-app.tsx b/apps/desktop/src/app/quick-entry/quick-entry-app.tsx
index d1ec7901d43..dc563558b02 100644
--- a/apps/desktop/src/app/quick-entry/quick-entry-app.tsx
+++ b/apps/desktop/src/app/quick-entry/quick-entry-app.tsx
@@ -7,7 +7,8 @@ import {
QUICK_TARGET_NEW,
type QuickComposerEvent,
quickComposerReducer,
- type QuickComposerState
+ type QuickComposerState,
+ quickEntryResultEvent
} from '@/store/quick-entry'
// Native select popups do not reliably inherit the closed control's colors.
@@ -48,15 +49,7 @@ export function QuickEntryApp() {
if (send) {
const submitId = submitIdRef.current
void api?.submit(send).then(result => {
- dispatch(
- result.ok
- ? { submitId, type: 'submit-ok' }
- : {
- message: result.message || 'Quick Entry could not deliver the prompt.',
- submitId,
- type: 'submit-error'
- }
- )
+ dispatch(quickEntryResultEvent(result, submitId))
if (!result.ok) {
requestAnimationFrame(() => inputRef.current?.focus())
}
diff --git a/apps/desktop/src/store/quick-entry.test.ts b/apps/desktop/src/store/quick-entry.test.ts
index 15b36231681..a50fbc55ea9 100644
--- a/apps/desktop/src/store/quick-entry.test.ts
+++ b/apps/desktop/src/store/quick-entry.test.ts
@@ -7,7 +7,9 @@ import {
type QuickComposerEvent,
quickComposerReducer,
type QuickComposerState,
- type QuickEntrySubmitPayload
+ type QuickEntrySubmitPayload,
+ quickEntryResultEvent,
+ type QuickEntrySubmitResult
} from './quick-entry'
// Drive the reducer like the window does, collecting every send it asked for.
@@ -44,20 +46,19 @@ describe('quickComposerReducer', () => {
connected: false,
draft: '',
error: null,
+ lastSubmitText: '',
+ orphanedFailure: null,
pendingSubmitId: null,
sessions: [],
submitting: false,
target: QUICK_TARGET_CURRENT,
+ unknownSubmitId: null,
visible: true
})
})
it('submit sends the trimmed draft but waits for acknowledgement to clear and hide', () => {
- const { sent, state } = run([
- connect,
- { draft: ' ship it ', type: 'edit' },
- { submitId: 1, type: 'submit' }
- ])
+ const { sent, state } = run([connect, { draft: ' ship it ', type: 'edit' }, { submitId: 1, type: 'submit' }])
expect(sent).toEqual([{ target: QUICK_TARGET_CURRENT, text: 'ship it' }])
expect(state.draft).toBe(' ship it ')
@@ -86,13 +87,20 @@ describe('quickComposerReducer', () => {
})
})
- it('ignores submit acknowledgements with stale ids', () => {
+ it('keeps the current owner for stale acknowledgements and records an unmatched late failure', () => {
const { state } = run([connect, { draft: 'keep', type: 'edit' }, { submitId: 7, type: 'submit' }])
expect(quickComposerReducer(state, { submitId: 8, type: 'submit-ok' }).state).toEqual(state)
- expect(
- quickComposerReducer(state, { message: 'stale', submitId: 8, type: 'submit-error' }).state
- ).toEqual(state)
+ const failed = quickComposerReducer(state, { message: 'stale', submitId: 8, type: 'submit-error' }).state
+
+ expect(failed).toMatchObject({
+ draft: 'keep',
+ error: 'stale',
+ lastSubmitText: '',
+ orphanedFailure: { message: 'stale', text: 'keep' },
+ pendingSubmitId: 7,
+ submitting: true
+ })
})
it('keeps resume and current-target submits on their existing routes', () => {
@@ -212,22 +220,22 @@ describe('quickComposerReducer', () => {
expect(state.draft).toBe('')
})
- it('the blur that follows a submit does not re-send or resurrect the draft', () => {
+ it('the blur that follows a submit keeps that submit correlated and delivered', () => {
const { sent, state } = run([connect, { draft: 'go', type: 'edit' }, { type: 'submit' }, { type: 'blur' }])
expect(sent).toEqual([{ target: QUICK_TARGET_CURRENT, text: 'go' }])
- expect(state.draft).toBe('')
- expect(state.submitting).toBe(false)
+ expect(state.draft).toBe('go')
+ expect(state.submitting).toBe(true)
expect(state.visible).toBe(false)
})
- it('being re-summoned resets the capture surface but KEEPS the pushed gateway truth', () => {
+ it('being re-summoned reconnects to an in-flight submit and KEEPS the pushed gateway truth', () => {
const afterSubmit = run([connect, { draft: 'first', type: 'edit' }, { type: 'submit' }]).state
const { sent, state } = run([{ type: 'shown' }], afterSubmit)
expect(sent).toEqual([])
- expect(state.draft).toBe('')
- expect(state.target).toBe(QUICK_TARGET_CURRENT)
+ expect(state.draft).toBe('first')
+ expect(state.submitting).toBe(true)
expect(state.visible).toBe(true)
// The gateway did not disconnect just because the window was re-opened.
expect(state.connected).toBe(true)
@@ -241,4 +249,160 @@ describe('quickComposerReducer', () => {
expect(first.sent).toEqual([{ target: QUICK_TARGET_CURRENT, text: 'one' }])
expect(second.sent).toEqual([{ target: QUICK_TARGET_CURRENT, text: 'two' }])
})
+
+ it('keeps the correlation and draft when dismissed mid-submit', () => {
+ const { state } = run([
+ connect,
+ { draft: 'pending prompt', type: 'edit' },
+ { submitId: 7, type: 'submit' },
+ { type: 'dismiss' }
+ ])
+
+ expect(state.draft).toBe('pending prompt')
+ expect(state.pendingSubmitId).toBe(7)
+ expect(state.submitting).toBe(true)
+ expect(state.visible).toBe(false)
+ })
+
+ it('reconnects to the same generation when re-shown mid-submit', () => {
+ const dismissed = run([
+ connect,
+ { draft: 'pending prompt', type: 'edit' },
+ { submitId: 7, type: 'submit' },
+ { type: 'dismiss' }
+ ]).state
+ const state = quickComposerReducer(dismissed, { type: 'shown' }).state
+
+ expect(state.draft).toBe('pending prompt')
+ expect(state.error).toBeNull()
+ expect(state.pendingSubmitId).toBe(7)
+ expect(state.submitting).toBe(true)
+ expect(state.visible).toBe(true)
+ })
+
+ it('a late success for the owning generation clears the surface', () => {
+ const dismissed = run([
+ connect,
+ { draft: 'pending prompt', type: 'edit' },
+ { submitId: 7, type: 'submit' },
+ { type: 'dismiss' }
+ ]).state
+ const shown = quickComposerReducer(dismissed, { type: 'shown' }).state
+ const state = quickComposerReducer(shown, { submitId: 7, type: 'submit-ok' }).state
+
+ expect(state.draft).toBe('')
+ expect(state.pendingSubmitId).toBeNull()
+ expect(state.visible).toBe(false)
+ })
+
+ it('a late failure for the owning generation keeps the text and shows the error', () => {
+ const { state } = run([
+ connect,
+ { draft: 'pending prompt', type: 'edit' },
+ { submitId: 7, type: 'submit' },
+ { type: 'dismiss' },
+ { message: 'gateway down', submitId: 7, type: 'submit-error' }
+ ])
+
+ expect(state.draft).toBe('pending prompt')
+ expect(state.error).toBe('gateway down')
+ expect(state.pendingSubmitId).toBeNull()
+ expect(state.visible).toBe(true)
+ })
+
+ it('a timeout is unknown, keeps the correlation, and is not retryable', () => {
+ const { state } = run([
+ connect,
+ { draft: 'pending prompt', type: 'edit' },
+ { submitId: 7, type: 'submit' },
+ { message: 'may still be delivered', submitId: 7, type: 'submit-unknown' }
+ ])
+ const retried = run(
+ [
+ { draft: 'try again', type: 'edit' },
+ { submitId: 8, type: 'submit' }
+ ],
+ state
+ )
+
+ expect(state.draft).toBe('pending prompt')
+ expect(state.error).toBe('may still be delivered')
+ expect(state.pendingSubmitId).toBeNull()
+ expect(state.submitting).toBe(false)
+ expect(state.unknownSubmitId).toBe(7)
+ expect(state.visible).toBe(true)
+ expect(retried.sent).toEqual([])
+ expect(retried.state.draft).toBe('try again')
+ expect(retried.state.unknownSubmitId).toBe(7)
+ })
+
+ it('a late success reconciles the unknown outcome', () => {
+ const unknown = run([
+ connect,
+ { draft: 'pending prompt', type: 'edit' },
+ { submitId: 7, type: 'submit' },
+ { message: 'may still be delivered', submitId: 7, type: 'submit-unknown' }
+ ]).state
+ const state = quickComposerReducer(unknown, { submitId: 7, type: 'submit-ok' }).state
+
+ expect(state.draft).toBe('')
+ expect(state.unknownSubmitId).toBeNull()
+ expect(state.visible).toBe(false)
+ })
+
+ it('a late failure clears the unknown correlation so a retry is allowed', () => {
+ const unknown = run([
+ connect,
+ { draft: 'pending prompt', type: 'edit' },
+ { submitId: 7, type: 'submit' },
+ { message: 'may still be delivered', submitId: 7, type: 'submit-unknown' }
+ ]).state
+ const state = quickComposerReducer(unknown, { message: 'rejected', submitId: 7, type: 'submit-error' }).state
+
+ expect(state.draft).toBe('pending prompt')
+ expect(state.error).toBe('rejected')
+ expect(state.unknownSubmitId).toBeNull()
+ })
+
+ it('quickEntryResultEvent maps timeout to submit-unknown, ok to submit-ok, else submit-error', () => {
+ const timeout: QuickEntrySubmitResult = { code: 'timeout', ok: false }
+ const ok: QuickEntrySubmitResult = { ok: true }
+ const failed: QuickEntrySubmitResult = { ok: false }
+ const timeoutEvent = quickEntryResultEvent(timeout, 7)
+
+ expect(timeoutEvent).toEqual({
+ message: 'Hermes has not confirmed the prompt yet — it may still be delivered.',
+ submitId: 7,
+ type: 'submit-unknown'
+ })
+ expect(timeoutEvent.type).not.toBe('submit-error')
+ expect(quickEntryResultEvent(ok, 7)).toEqual({ submitId: 7, type: 'submit-ok' })
+ expect(quickEntryResultEvent(failed, 7)).toEqual({
+ message: 'Quick Entry could not deliver the prompt.',
+ submitId: 7,
+ type: 'submit-error'
+ })
+ })
+
+ it('a failure for a superseded generation is handed back on the next summon', () => {
+ const { sent, state } = run([
+ connect,
+ { draft: 'first prompt', type: 'edit' },
+ { submitId: 7, type: 'submit' },
+ { type: 'dismiss' },
+ { type: 'shown' },
+ { draft: 'second prompt', type: 'edit' },
+ { submitId: 8, type: 'submit' },
+ { message: 'first failed', submitId: 7, type: 'submit-error' },
+ { submitId: 8, type: 'submit-ok' },
+ { type: 'shown' }
+ ])
+
+ expect(sent).toEqual([
+ { target: QUICK_TARGET_CURRENT, text: 'first prompt' },
+ { target: QUICK_TARGET_CURRENT, text: 'second prompt' }
+ ])
+ expect(state.draft).toBe('first prompt')
+ expect(state.error).toBe('first failed')
+ })
})
diff --git a/apps/desktop/src/store/quick-entry.ts b/apps/desktop/src/store/quick-entry.ts
index 65b49850941..c9ebf0d05bb 100644
--- a/apps/desktop/src/store/quick-entry.ts
+++ b/apps/desktop/src/store/quick-entry.ts
@@ -157,6 +157,15 @@ export interface QuickComposerState {
error: null | string
/** Local correlation for ignoring an acknowledgement from an older submit. */
pendingSubmitId: number | null
+ /** Text of the in-flight submit, kept so a late failure can restore it. */
+ lastSubmitText: string
+ /** A failure whose generation no longer owns the window. The next summon
+ * restores this text instead of silently dropping the prompt. */
+ orphanedFailure: null | { message: string; text: string }
+ /** A submit whose outcome is UNKNOWN (relay timeout): the backend may still
+ * have accepted it. Kept so a late acknowledgement can reconcile, and never
+ * presented as retryable until non-acceptance is proven. */
+ unknownSubmitId: number | null
/** Where a submit lands: current / new / a stored session id. */
target: string
/** Whether the window should be visible. False asks the shell to hide. */
@@ -171,9 +180,35 @@ export type QuickComposerEvent =
| { type: 'state'; connected: boolean; sessions: QuickEntrySessionOption[] }
| { type: 'submit'; submitId?: number }
| { type: 'submit-error'; message: string; submitId: number }
+ | { message: string; submitId: number; type: 'submit-unknown' }
| { type: 'submit-ok'; submitId: number }
| { type: 'target'; target: string }
+/**
+ * Map a relay result to the composer event that reconciles it. A timeout is an
+ * UNKNOWN outcome — the prompt may already be accepted — so it must never be
+ * mapped to a retryable failure.
+ */
+export function quickEntryResultEvent(result: QuickEntrySubmitResult, submitId: number): QuickComposerEvent {
+ if (result.ok) {
+ return { submitId, type: 'submit-ok' }
+ }
+
+ if (result.code === 'timeout') {
+ return {
+ message: result.message || 'Hermes has not confirmed the prompt yet — it may still be delivered.',
+ submitId,
+ type: 'submit-unknown'
+ }
+ }
+
+ return {
+ message: result.message || 'Quick Entry could not deliver the prompt.',
+ submitId,
+ type: 'submit-error'
+ }
+}
+
export interface QuickComposerTransition {
/** Payload to send through the real prompt-submit path, or null for none. */
send: null | QuickEntrySubmitPayload
@@ -186,10 +221,13 @@ export const initialQuickComposerState: QuickComposerState = {
connected: false,
draft: '',
error: null,
+ lastSubmitText: '',
+ orphanedFailure: null,
pendingSubmitId: null,
sessions: [],
submitting: false,
target: QUICK_TARGET_CURRENT,
+ unknownSubmitId: null,
visible: true
}
@@ -197,19 +235,27 @@ export function quickComposerReducer(state: QuickComposerState, event: QuickComp
switch (event.type) {
case 'blur':
case 'dismiss': {
- // Escape / focus loss discards without sending. A dismiss mid-submit still
- // hides — the send already left for the main process.
+ // Escape / focus loss discards a surface with nothing unresolved. A submit
+ // already handed to main keeps its correlation and draft: the promise
+ // still resolves, and a late failure must be able to restore the text.
+ const unresolved = state.submitting || state.unknownSubmitId !== null
+
return {
send: null,
- state: {
- ...state,
- draft: '',
- error: null,
- pendingSubmitId: null,
- submitting: false,
- target: QUICK_TARGET_CURRENT,
- visible: false
- }
+ state: unresolved
+ ? { ...state, error: null, visible: false }
+ : {
+ ...state,
+ draft: '',
+ error: null,
+ lastSubmitText: '',
+ orphanedFailure: null,
+ pendingSubmitId: null,
+ submitting: false,
+ target: QUICK_TARGET_CURRENT,
+ unknownSubmitId: null,
+ visible: false
+ }
}
}
@@ -218,17 +264,26 @@ export function quickComposerReducer(state: QuickComposerState, event: QuickComp
}
case 'shown': {
- // Re-summoned: a fresh capture surface every time — never a stale draft or
- // a leftover target — but the pushed gateway truth carries over.
+ // Re-summoned. With a submit unresolved the window reconnects to the SAME
+ // generation and shows the text still being delivered. Otherwise the
+ // surface is fresh — except that a failure whose generation lost the
+ // window hands its text back rather than dropping it.
+ if (state.submitting || state.unknownSubmitId !== null) {
+ return { send: null, state: { ...state, error: null, visible: true } }
+ }
+
return {
send: null,
state: {
...state,
- draft: '',
- error: null,
+ draft: state.orphanedFailure?.text ?? '',
+ error: state.orphanedFailure?.message ?? null,
+ lastSubmitText: '',
+ orphanedFailure: null,
pendingSubmitId: null,
submitting: false,
target: QUICK_TARGET_CURRENT,
+ unknownSubmitId: null,
visible: true
}
}
@@ -258,28 +313,116 @@ export function quickComposerReducer(state: QuickComposerState, event: QuickComp
const text = state.draft.trim()
// Nothing to send — or nowhere to send it (gateway down): stay open and
- // keep the draft so a stray Enter can't make the text vanish.
- if (!text || state.submitting || !state.connected) {
+ // keep the draft so a stray Enter can't make the text vanish. Re-entering
+ // changed text supersedes the pending generation; an unknown outcome never
+ // invites a second delivery attempt.
+ if (
+ !text ||
+ !state.connected ||
+ state.unknownSubmitId !== null ||
+ (state.submitting && text === state.lastSubmitText)
+ ) {
return { send: null, state }
}
return {
send: { target: state.target, text },
// Wait for main's result before clearing or hiding the capture window (#85590).
- state: { ...state, error: null, pendingSubmitId: event.submitId ?? null, submitting: true }
+ state: {
+ ...state,
+ error: null,
+ // If changed text supersedes an unresolved generation, retain the
+ // older prompt: its late failure must hand that text back later.
+ lastSubmitText: state.submitting ? state.lastSubmitText : text,
+ pendingSubmitId: event.submitId ?? null,
+ submitting: true
+ }
}
}
case 'submit-ok': {
- return event.submitId > 0 && state.pendingSubmitId === event.submitId
- ? { send: null, state: { ...state, draft: '', error: null, pendingSubmitId: null, submitting: false, visible: false } }
+ // The owning generation — or a submit whose outcome was unknown — clears
+ // the surface. A late success for a superseded generation is still good
+ // news, but it must not wipe the draft the window now owns.
+ const owner = state.pendingSubmitId === event.submitId || state.unknownSubmitId === event.submitId
+
+ return event.submitId > 0 && owner
+ ? {
+ send: null,
+ state: {
+ ...state,
+ draft: '',
+ error: null,
+ lastSubmitText: '',
+ // A successful newer generation does not erase an older
+ // generation's already-recorded late failure.
+ orphanedFailure: state.orphanedFailure,
+ pendingSubmitId: null,
+ submitting: false,
+ unknownSubmitId: null,
+ visible: false
+ }
+ }
: { send: null, state }
}
+ case 'submit-unknown': {
+ if (event.submitId <= 0 || state.pendingSubmitId !== event.submitId) {
+ return { send: null, state }
+ }
+
+ // Delivery is UNCONFIRMED, not failed: keep the draft, keep the
+ // correlation, and never present this as retryable.
+ return {
+ send: null,
+ state: {
+ ...state,
+ error: event.message,
+ pendingSubmitId: null,
+ submitting: false,
+ unknownSubmitId: event.submitId,
+ visible: true
+ }
+ }
+ }
+
case 'submit-error': {
- return event.submitId > 0 && state.pendingSubmitId === event.submitId
- ? { send: null, state: { ...state, error: event.message, pendingSubmitId: null, submitting: false, visible: true } }
- : { send: null, state }
+ if (event.submitId > 0 && state.pendingSubmitId === event.submitId) {
+ return {
+ send: null,
+ state: {
+ ...state,
+ error: event.message,
+ lastSubmitText: '',
+ pendingSubmitId: null,
+ submitting: false,
+ visible: true
+ }
+ }
+ }
+
+ if (event.submitId > 0 && state.unknownSubmitId === event.submitId) {
+ // Non-acceptance is now proven: drop the unknown correlation so a
+ // retry is legitimate, and keep the text.
+ return {
+ send: null,
+ state: { ...state, error: event.message, lastSubmitText: '', unknownSubmitId: null }
+ }
+ }
+
+ // Late failure whose generation no longer owns the window: keep the text
+ // for the next summon and surface the message now if nothing else has.
+ return {
+ send: null,
+ state: {
+ ...state,
+ error: state.error ?? event.message,
+ orphanedFailure: state.lastSubmitText
+ ? { message: event.message, text: state.lastSubmitText }
+ : state.orphanedFailure,
+ lastSubmitText: ''
+ }
+ }
}
case 'target': {