Skip to content

The agent that grows with you

fix: preserve active turn ownership during canonical queue admission

Tekniuma131bf4aca37
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
index d51eb3b1375..07d2efd208e 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
@@ -429,6 +429,23 @@ describe('submit timeout admission fences', () => {
describe('terminal receipt settlement', () => {
afterEach(cleanup)
+ it('admits a native queued input without taking ownership of the running turn', async () => {
+ let handle: HarnessHandle | null = null
+ const requestGateway = vi.fn(async (method: string, params?: Record<string, unknown>) => {
+ if (method !== 'prompt.submit') { return {} as never }
+ expect(handle!.state()).toMatchObject({ busy: true, awaitingResponse: true, streamId: 'owner-stream', turnLive: true })
+ return { admission_id: 'server-queue', submission_id: params?.submission_id, session_id: RUNTIME_SESSION_ID, status: 'queued' } as never
+ })
+ await actRender(<Harness onReady={h => (handle = h)} rawAdmissionReceipts refreshSessions={async () => undefined} requestGateway={requestGateway} />)
+ Object.assign(handle!.state(), { busy: true, awaitingResponse: true, streamId: 'owner-stream', turnLive: true })
+ $connection.set({ wsUrl: 'ws://localhost/api/ws?native_dial=unminted', mode: 'local' } as never)
+ try {
+ expect(await handle!.submitText('next input', { fromQueue: true })).toBe(true)
+ expect(handle!.state()).toMatchObject({ busy: true, awaitingResponse: true, streamId: 'owner-stream', turnLive: true })
+ expect(JSON.parse(window.localStorage.getItem('hermes.desktop.preparedSubmissions.v1')!)).toEqual({})
+ } finally { $connection.set(null) }
+ })
+
it('settles a retained terminal retry without waiting for another lifecycle event', async () => {
let terminal = false
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts
index e52af0a55f2..932ddbea2ea 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts
@@ -18,6 +18,7 @@ import {
mainComposerScope,
terminalContextBlocksFromDraft
} from '@/store/composer'
+import { serverOwnsComposerQueue } from '@/store/composer-queue'
import { $hudMode } from '@/store/hud'
import { clearNotifications, notify, notifyError } from '@/store/notifications'
import { consumePendingCredentialWarning, requestDesktopOnboarding } from '@/store/onboarding'
@@ -178,10 +179,12 @@ export function useSubmitPrompt(deps: SubmitPromptDeps) {
const hasSendable = Boolean(visibleText || terminalContextBlocks || attachments.length || hasImage)
const guardSessionId = options?.sessionId ?? activeSessionIdRef.current
+ const serverQueue = serverOwnsComposerQueue(options?.storedSessionId ?? guardSessionId)
+ const queueAdmission = serverQueue && Boolean(options?.fromQueue || isTargetSessionBusy($sessionStates.get(), guardSessionId, busyRef.current))
if (
!hasSendable ||
- (!options?.fromQueue && isTargetSessionBusy($sessionStates.get(), guardSessionId, busyRef.current))
+ (!serverQueue && !options?.fromQueue && isTargetSessionBusy($sessionStates.get(), guardSessionId, busyRef.current))
) {
return false
}
@@ -418,7 +421,7 @@ export function useSubmitPrompt(deps: SubmitPromptDeps) {
const releaseBusy = () => {
releaseSubmitLock()
- if (targetIsCurrentView()) {
+ if (!queueAdmission && targetIsCurrentView()) {
setMutableRef(busyRef, false)
scope.setBusy(false)
scope.setAwaitingResponse(false)
@@ -428,6 +431,7 @@ export function useSubmitPrompt(deps: SubmitPromptDeps) {
// Idempotent optimistic insert — re-running with the resolved sessionId
// after createBackendSessionForSend just overwrites with the same id.
const seedOptimistic = (sid: string) => {
+ if (queueAdmission) { return }
// Recents jump on send — not stream start, not turn resolve.
const activity = bubbleText.trim() ? { preview: bubbleText.trim() } : undefined
touchSessionActivity(sid, activity)
@@ -489,6 +493,7 @@ export function useSubmitPrompt(deps: SubmitPromptDeps) {
)
const dropOptimistic = (sid: null | string) => {
+ if (queueAdmission) { return }
if (!sid) {
if (targetIsCurrentView()) {
scope.setMessages(current => current.filter(m => m.id !== optimisticId))
@@ -523,7 +528,7 @@ export function useSubmitPrompt(deps: SubmitPromptDeps) {
// Foreground-only state: a background queue drain must never write the
// selected view's busy/awaiting flags or clear its notifications.
- if (targetIsCurrentView()) {
+ if (!queueAdmission && targetIsCurrentView()) {
setMutableRef(busyRef, true)
scope.setBusy(true)
scope.setAwaitingResponse(true)
@@ -573,7 +578,7 @@ export function useSubmitPrompt(deps: SubmitPromptDeps) {
if (sessionId) {
seedOptimistic(sessionId)
- } else if (targetIsCurrentView()) {
+ } else if (!queueAdmission && targetIsCurrentView()) {
scope.setMessages(current => [...current, buildUserMessage()])
}
@@ -818,7 +823,7 @@ export function useSubmitPrompt(deps: SubmitPromptDeps) {
// the next turn untouched — without it, losing the settle race
// (client saw idle, server still unwinding) redirects or interrupts
// the live turn with text the user explicitly queued.
- ...(options?.fromQueue && { queued: true })
+ ...((options?.fromQueue || queueAdmission) && { queued: true })
})
// A fresh draft had no session owner at entry. Adopt its published
@@ -997,6 +1002,11 @@ export function useSubmitPrompt(deps: SubmitPromptDeps) {
return false
}
+ if (queueAdmission) {
+ notifyError(err, copy.promptFailed)
+ return false
+ }
+
const message = inlineErrorMessage(err, copy.promptFailed)
const occurredAt = Date.now() / 1000