Skip to content

The agent that grows with you

fix(quick-entry): prove the stored-session ack names the requested session

LeonardoLGDS6261dc479b59
diff --git a/apps/desktop/src/app/chat/session-tile-actions.test.ts b/apps/desktop/src/app/chat/session-tile-actions.test.ts
index 19854fb5613..81c0cbf8ab1 100644
--- a/apps/desktop/src/app/chat/session-tile-actions.test.ts
+++ b/apps/desktop/src/app/chat/session-tile-actions.test.ts
@@ -93,7 +93,10 @@ describe('useSessionTileActions sleep/wake session recovery', () => {
executeSlash: vi.fn(async () => undefined),
interruptSession: vi.fn(async () => undefined),
resumeTile: vi.fn(async () => RUNTIME_SESSION_ID),
- submitToSession: vi.fn(async () => RUNTIME_SESSION_ID),
+ submitToSession: vi.fn(async () => ({
+ runtimeSessionId: RUNTIME_SESSION_ID,
+ storedSessionId: null
+ })),
updateSession: vi.fn((_runtimeId, updater) =>
updater({
attachedImages: [],
diff --git a/apps/desktop/src/app/chat/session-tile-attachments.test.tsx b/apps/desktop/src/app/chat/session-tile-attachments.test.tsx
index 90018b77fc1..d8a790aafaf 100644
--- a/apps/desktop/src/app/chat/session-tile-attachments.test.tsx
+++ b/apps/desktop/src/app/chat/session-tile-attachments.test.tsx
@@ -89,7 +89,10 @@ function installDelegate(): void {
executeSlash: vi.fn(async () => undefined),
interruptSession: vi.fn(async () => undefined),
resumeTile: vi.fn(async () => RUNTIME_ID),
- submitToSession: vi.fn(async () => RUNTIME_ID),
+ submitToSession: vi.fn(async () => ({
+ runtimeSessionId: RUNTIME_ID,
+ storedSessionId: null
+ })),
updateSession
})
}
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 5b695ca89ef..734a1cf56aa 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
@@ -191,7 +191,7 @@ describe('useQuickEntryBridge', () => {
container.remove()
})
- it('acknowledges an accepted selected-session submit with exact identity', async () => {
+ it('acknowledges with the proven identity when the accepted stored id matches', async () => {
const correlationId = 'selected-submit-correlation'
const ackSubmit = vi.fn()
ackSubmit.mockImplementationOnce(() => {
@@ -205,7 +205,10 @@ describe('useQuickEntryBridge', () => {
} as unknown as typeof window.hermesDesktop
const resumeTile = vi.fn(async () => 'runtime-session-1')
- const submitToSession = vi.fn(async () => 'runtime-session-1')
+ const submitToSession = vi.fn(async () => ({
+ runtimeSessionId: 'runtime-session-1',
+ storedSessionId: 'stored-session-1'
+ }))
vi.mocked(sessionTileDelegate).mockReturnValue({
resumeTile,
submitToSession
@@ -246,7 +249,10 @@ describe('useQuickEntryBridge', () => {
} as unknown as typeof window.hermesDesktop
const resumeTile = vi.fn(async () => 'runtime-session-before-recovery')
- const submitToSession = vi.fn(async () => 'runtime-session-recovered')
+ const submitToSession = vi.fn(async () => ({
+ runtimeSessionId: 'runtime-session-recovered',
+ storedSessionId: 'stored-session-1'
+ }))
vi.mocked(sessionTileDelegate).mockReturnValue({
resumeTile,
submitToSession
@@ -268,6 +274,80 @@ describe('useQuickEntryBridge', () => {
container.remove()
})
+ it('refuses to acknowledge success when the accepted stored id differs from the requested target', async () => {
+ const correlationId = 'selected-submit-identity-mismatch'
+ const ackSubmit = vi.fn()
+ window.hermesDesktop = {
+ quickEntry: {
+ ackSubmit,
+ pushState: vi.fn()
+ }
+ } as unknown as typeof window.hermesDesktop
+
+ const resumeTile = vi.fn(async () => 'runtime-session-1')
+ const submitToSession = vi.fn(async () => ({
+ runtimeSessionId: 'runtime-session-1',
+ storedSessionId: 'other-session'
+ }))
+ vi.mocked(sessionTileDelegate).mockReturnValue({
+ resumeTile,
+ submitToSession
+ } as unknown as ReturnType<typeof sessionTileDelegate>)
+
+ const { container, submit } = await renderBridge()
+
+ await act(async () => {
+ await submit({ correlationId, target: 'stored-session-1', text: 'Wrong target' })
+ })
+
+ expect(ackSubmit).toHaveBeenCalledTimes(1)
+ expect(ackSubmit).toHaveBeenCalledWith(correlationId, {
+ code: 'submit-identity-mismatch',
+ message: 'The prompt was accepted by a different session than the one requested.',
+ ok: false,
+ retryable: false
+ })
+
+ container.remove()
+ })
+
+ it('refuses to acknowledge success when the accepted stored id is unknown', async () => {
+ const correlationId = 'selected-submit-unknown-identity'
+ const ackSubmit = vi.fn()
+ window.hermesDesktop = {
+ quickEntry: {
+ ackSubmit,
+ pushState: vi.fn()
+ }
+ } as unknown as typeof window.hermesDesktop
+
+ const resumeTile = vi.fn(async () => 'runtime-session-1')
+ const submitToSession = vi.fn(async () => ({
+ runtimeSessionId: 'runtime-session-1',
+ storedSessionId: null
+ }))
+ vi.mocked(sessionTileDelegate).mockReturnValue({
+ resumeTile,
+ submitToSession
+ } as unknown as ReturnType<typeof sessionTileDelegate>)
+
+ const { container, submit } = await renderBridge()
+
+ await act(async () => {
+ await submit({ correlationId, target: 'stored-session-1', text: 'Unknown target' })
+ })
+
+ expect(ackSubmit).toHaveBeenCalledTimes(1)
+ expect(ackSubmit).toHaveBeenCalledWith(correlationId, {
+ code: 'submit-identity-mismatch',
+ message: 'The prompt was accepted by a different session than the one requested.',
+ ok: false,
+ retryable: false
+ })
+
+ container.remove()
+ })
+
it('keeps a post-dispatch failure non-retryable', async () => {
const correlationId = 'selected-submit-post-dispatch-failure'
const ackSubmit = vi.fn()
@@ -317,7 +397,10 @@ describe('useQuickEntryBridge', () => {
const resumeTile = vi.fn(async () => {
throw new Error('resume failed')
})
- const submitToSession = vi.fn(async () => 'runtime-session-1')
+ const submitToSession = vi.fn(async () => ({
+ runtimeSessionId: 'runtime-session-1',
+ storedSessionId: 'stored-session-1'
+ }))
vi.mocked(sessionTileDelegate).mockReturnValue({
resumeTile,
submitToSession
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 de5bcefb027..6a10e6c502e 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
@@ -136,9 +136,24 @@ export function useQuickEntryBridge({
try {
const runtimeId = await delegate.resumeTile(target)
promptDispatched = true
- const acceptedRuntimeId = await delegate.submitToSession(runtimeId, text)
+ const accepted = await delegate.submitToSession(runtimeId, text)
- ack({ ok: true, runtimeSessionId: acceptedRuntimeId, sessionId: target })
+ if (accepted.storedSessionId !== target) {
+ // The backend accepted the prompt, but the accepted runtime is not
+ // bound to the requested stored session (or the binding is
+ // unprovable). Never report success for a session this request
+ // did not prove it used.
+ ack({
+ code: 'submit-identity-mismatch',
+ message: 'The prompt was accepted by a different session than the one requested.',
+ ok: false,
+ retryable: false
+ })
+
+ return
+ }
+
+ ack({ ok: true, runtimeSessionId: accepted.runtimeSessionId, sessionId: accepted.storedSessionId })
} catch (error) {
if (promptDispatched) {
ack({
diff --git a/apps/desktop/src/app/contrib/hooks/use-session-tile-delegate.test.ts b/apps/desktop/src/app/contrib/hooks/use-session-tile-delegate.test.ts
index 9254fdc365c..56093f551ee 100644
--- a/apps/desktop/src/app/contrib/hooks/use-session-tile-delegate.test.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-session-tile-delegate.test.ts
@@ -768,7 +768,7 @@ describe('useSessionTileDelegate submitToSession', () => {
setSessions([])
})
- it('returns the accepting runtime id on both the happy and recovered paths', async () => {
+ it('returns the accepted runtime and its stored binding', async () => {
setSessions([row({ id: 'stored-submit', profile: 'default' })])
const state = { busy: false, messages: [{ id: 'm1' }], storedSessionId: 'stored-submit' }
@@ -795,11 +795,17 @@ describe('useSessionTileDelegate submitToSession', () => {
renderTile(requestGateway, { runtimeIdByStoredSessionIdRef, sessionStateByRuntimeIdRef })
const delegate = sessionTileDelegate()!
- const recoveredId = await delegate.submitToSession('runtime-dead', 'Send from Quick Entry')
- expect(recoveredId).toBe('runtime-recovered')
+ const recovered = await delegate.submitToSession('runtime-dead', 'Send from Quick Entry')
+ expect(recovered).toEqual({
+ runtimeSessionId: 'runtime-recovered',
+ storedSessionId: 'stored-submit'
+ })
- const acceptedId = await delegate.submitToSession('runtime-recovered', 'Send again')
- expect(acceptedId).toBe('runtime-recovered')
+ const accepted = await delegate.submitToSession('runtime-recovered', 'Send again')
+ expect(accepted).toEqual({
+ runtimeSessionId: 'runtime-recovered',
+ storedSessionId: 'stored-submit'
+ })
expect(promptAttempts).toBe(1)
expect(runtimeIdByStoredSessionIdRef.current.get('stored-submit')).toBe('runtime-recovered')
expect(requestGateway).toHaveBeenNthCalledWith(
diff --git a/apps/desktop/src/app/contrib/hooks/use-session-tile-delegate.ts b/apps/desktop/src/app/contrib/hooks/use-session-tile-delegate.ts
index 268e925d816..79764742e92 100644
--- a/apps/desktop/src/app/contrib/hooks/use-session-tile-delegate.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-session-tile-delegate.ts
@@ -520,7 +520,12 @@ export function useSessionTileDelegate({
}
)
- return acceptedRuntimeId
+ return {
+ runtimeSessionId: acceptedRuntimeId,
+ // The durable binding for the accepted runtime — the only proof the
+ // requested stored session is what actually took the prompt.
+ storedSessionId: storedSessionIdForRuntime(acceptedRuntimeId) ?? null
+ }
},
updateSession: (runtimeId, updater) => updateSessionState(runtimeId, updater)
})
diff --git a/apps/desktop/src/store/session-states.ts b/apps/desktop/src/store/session-states.ts
index dd89a30a350..2f11712a310 100644
--- a/apps/desktop/src/store/session-states.ts
+++ b/apps/desktop/src/store/session-states.ts
@@ -2306,16 +2306,24 @@ export interface SessionTileDelegate {
* caller writes the mirror itself. */
updateHeldSession?(runtimeId: string, updater: (state: ClientSessionState) => ClientSessionState): boolean
/**
- * Resolves with the live runtime session id that ACCEPTED the prompt. A
- * session-not-found recovery can rebind the runtime, so the accepted id may
- * differ from the input id; callers that report delivery must use this value.
+ * Resolves with the EXACT identity that ACCEPTED the prompt. A
+ * session-not-found recovery can rebind the runtime, so the accepted runtime
+ * id may differ from the input id; `storedSessionId` is the durable session
+ * the accepted runtime is bound to, or null when that binding is unknown. A
+ * caller that reports delivery must prove the requested target from this.
*/
- submitToSession(runtimeId: string, text: string): Promise<string>
+ submitToSession(runtimeId: string, text: string): Promise<AcceptedSessionIdentity>
/** THE session-state write path — routes through the wiring cache so the
* cache, the primary view (when active), and every tile mirror agree. */
updateSession(runtimeId: string, updater: (state: ClientSessionState) => ClientSessionState): ClientSessionState
}
+/** Exact identity a prompt was accepted into: live runtime id + durable stored id. */
+export interface AcceptedSessionIdentity {
+ runtimeSessionId: string
+ storedSessionId: null | string
+}
+
let delegate: SessionTileDelegate | null = null
export const $sessionTileDelegateRevision = atom(0)