Skip to content

The agent that grows with you

fix(desktop): prevent queue livelock and detect commands after palette refs

StanleyStetson6acd6e91efe0
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-queue.ts b/apps/desktop/src/app/chat/composer/hooks/use-composer-queue.ts
index dd4376c344d..4d99c6b6f42 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-queue.ts
+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-queue.ts
@@ -3,6 +3,7 @@ import { type RefObject, useCallback, useEffect, useRef, useState } from 'react'
import { useI18n } from '@/i18n'
import { triggerHaptic } from '@/lib/haptics'
+import { isSlashCommandText } from '@/lib/chat-runtime'
import { useSessionSlice } from '@/lib/use-session-slice'
import { type ComposerAttachment } from '@/store/composer'
import { resetBrowseState } from '@/store/composer-input-history'
@@ -170,6 +171,19 @@ export function useComposerQueue({
return false
}
+ // Editing a queued entry into a slash-command + attachment combo would
+ // produce an undrainable entry (submitText rejects it on every attempt).
+ // Refuse the save so the queue can never hold an entry that livelocks.
+ if (isSlashCommandText(text) && next.length) {
+ notify({
+ kind: 'warning',
+ title: t.desktop.slashCommandIgnoredTitle,
+ message: t.desktop.slashCommandIgnoredBody
+ })
+
+ return false
+ }
+
const saved = updateQueuedPrompt(queueEdit.sessionKey, queueEdit.entryId, { attachments: next, text })
triggerHaptic(saved ? 'success' : 'selection')
} else {
@@ -190,6 +204,19 @@ export function useComposerQueue({
return false
}
+ // Slash commands cannot ride alongside attachments — the drain path would
+ // reject the entry on every attempt (submitText warns + returns false) and
+ // the queue would livelock. Refuse to enqueue it in the first place.
+ if (isSlashCommandText(text) && attachments.length) {
+ notify({
+ kind: 'warning',
+ title: t.desktop.slashCommandIgnoredTitle,
+ message: t.desktop.slashCommandIgnoredBody
+ })
+
+ return false
+ }
+
if (!enqueueQueuedPrompt(activeQueueSessionKey, { text, attachments })) {
return false
}
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.ts b/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.ts
index 8f7500b231b..f5d7fc1dc12 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.ts
+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.ts
@@ -4,6 +4,7 @@ import { type RefObject, useLayoutEffect, useRef } from 'react'
import { usePaneVisible } from '@/components/pane-shell/pane-visibility'
import { isSideTaskSlashCommand } from '@/lib/desktop-slash-commands'
import { useI18n } from '@/i18n'
+import { isSlashCommandText, SLASH_COMMAND_RE } from '@/lib/chat-runtime'
import { triggerHaptic } from '@/lib/haptics'
import { hasClarifyRequest, skipClarifyRequest } from '@/store/clarify'
import { clearSessionDraft, type ComposerAttachment, isFreshDraftScope } from '@/store/composer'
@@ -290,7 +291,7 @@ export function useComposerSubmit({
// busy guard for commands that genuinely need an idle session (skill
// /send directives). Queuing them would make every slash command wait
// for the current turn to finish, which is how the TUI never behaves.
- if (SLASH_COMMAND_RE.test(text.trim())) {
+ if (isSlashCommandText(text)) {
if (attachments.length) {
// Slash commands cannot ride alongside attachments — warn the user
// instead of silently queuing the payload (which would then reach the
diff --git a/apps/desktop/src/app/chat/session-tile-actions.ts b/apps/desktop/src/app/chat/session-tile-actions.ts
index 257030a483f..ac7b01c42e6 100644
--- a/apps/desktop/src/app/chat/session-tile-actions.ts
+++ b/apps/desktop/src/app/chat/session-tile-actions.ts
@@ -16,6 +16,7 @@ import type { ClientSessionState } from '@/app/types'
import type { WorkspaceMode } from '@/contrib/types'
import { useI18n } from '@/i18n'
import { textPart } from '@/lib/chat-messages'
+import { isSlashCommandText } from '@/lib/chat-runtime'
import { triggerHaptic } from '@/lib/haptics'
import { clearClarifyRequest } from '@/store/clarify'
import type { ComposerAttachment } from '@/store/composer'
@@ -325,7 +326,7 @@ export function useSessionTileActions({ requestGateway, runtimeId, scope, stored
listTileSession(visibleText)
- if (SLASH_COMMAND_RE.test(visibleText)) {
+ if (isSlashCommandText(visibleText)) {
if (attachments.length) {
notify({
kind: 'warning',
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts
index bd272d65ad0..9d3753a6aaa 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts
@@ -9,7 +9,7 @@ import { type ResolvedOwner, transcribeAudio } from '@/hermes'
import { useI18n } from '@/i18n'
import { attachmentPathNeedsUpload } from '@/lib/attachment-upload-policy'
import { type ChatMessage, textPart } from '@/lib/chat-messages'
-import { pathLabel } from '@/lib/chat-runtime'
+import { isSlashCommandText, pathLabel } from '@/lib/chat-runtime'
import { sanitizeComposerInput } from '@/lib/composer-input-sanitize'
import { triggerHaptic } from '@/lib/haptics'
import { setMutableRef } from '@/lib/mutable-ref'
@@ -632,7 +632,7 @@ export function usePromptActions({
const visibleText = sanitizeComposerInput(rawText).trim()
const attachments = options?.attachments ?? $composerAttachments.get()
- if (SLASH_COMMAND_RE.test(visibleText)) {
+ if (isSlashCommandText(visibleText)) {
if (attachments.length) {
notify({
kind: 'warning',
diff --git a/apps/desktop/src/lib/chat-runtime.test.ts b/apps/desktop/src/lib/chat-runtime.test.ts
index 9671918278e..fe1fc9db6ec 100644
--- a/apps/desktop/src/lib/chat-runtime.test.ts
+++ b/apps/desktop/src/lib/chat-runtime.test.ts
@@ -10,9 +10,13 @@ import {
coerceThinkingText,
createClientSessionState,
createToolMergeCache,
+ isSlashCommandText,
messageCreatedAt,
optimisticAttachmentRef,
+ parseCommandDispatch,
+ parseSlashCommand,
personalityNamesFromConfig,
+ stripAttachmentRefs,
toRuntimeMessage
} from './chat-runtime'
@@ -300,3 +304,61 @@ describe('personalityNamesFromConfig', () => {
expect(names).toEqual(['catgirl', 'spaced'])
})
})
+
+describe('stripAttachmentRefs', () => {
+ it('strips a single leading image ref line', () => {
+ expect(stripAttachmentRefs('@image:/tmp/screenshot.png\n\n/moa what is this?')).toBe('\n/moa what is this?')
+ })
+
+ it('strips multiple ref lines joined by a single newline (producer format)', () => {
+ expect(stripAttachmentRefs('@image:a.png\n@file:b.pdf\n\n/moa hi')).toBe('\n/moa hi')
+ })
+
+ it('strips backtick-quoted ref values (formatRefValue output for spaced paths)', () => {
+ expect(stripAttachmentRefs('@image:`C:\\Users\\John Doe\\photo.png`\n\n/moa hi')).toBe('\n/moa hi')
+ })
+
+ it('strips folder/terminal/line refs from the inline palette', () => {
+ expect(stripAttachmentRefs('@folder:`apps/desktop/`\n\n/moa hi')).toBe('\n/moa hi')
+ expect(stripAttachmentRefs('@terminal:main\n\n/status')).toBe('\n/status')
+ expect(stripAttachmentRefs('@line:src/a.ts:12\n\n/moa')).toBe('\n/moa')
+ })
+
+ it('does not strip a ref-looking token in the middle of the text', () => {
+ expect(stripAttachmentRefs('look at @image:x /moa hi')).toBe('look at @image:x /moa hi')
+ })
+
+ it('leaves plain text untouched', () => {
+ expect(stripAttachmentRefs('hello world')).toBe('hello world')
+ })
+
+ it('handles an empty string', () => {
+ expect(stripAttachmentRefs('')).toBe('')
+ })
+})
+
+describe('isSlashCommandText', () => {
+ it('detects a plain slash command', () => {
+ expect(isSlashCommandText('/new')).toBe(true)
+ })
+
+ it('detects a command after an image ref (composer wire format)', () => {
+ expect(isSlashCommandText('@image:/tmp/foo.png\n\n/moa something')).toBe(true)
+ })
+
+ it('detects a command after a folder ref from the inline palette', () => {
+ expect(isSlashCommandText('@folder:`apps/desktop/`\n\n/moa hi')).toBe(true)
+ })
+
+ it('detects a command after multiple refs with one newline each', () => {
+ expect(isSlashCommandText('@image:a.png\n@file:b.pdf\n\n/compress')).toBe(true)
+ })
+
+ it('rejects plain text that merely contains a slash later', () => {
+ expect(isSlashCommandText('what is this?')).toBe(false)
+ })
+
+ it('rejects a ref-only message (no command)', () => {
+ expect(isSlashCommandText('@image:/tmp/foo.png')).toBe(false)
+ })
+})
diff --git a/apps/desktop/src/lib/chat-runtime.ts b/apps/desktop/src/lib/chat-runtime.ts
index 7f8380c9d58..cfd115e43c1 100644
--- a/apps/desktop/src/lib/chat-runtime.ts
+++ b/apps/desktop/src/lib/chat-runtime.ts
@@ -316,6 +316,78 @@ export function normalizePersonalityValue(value: string): string {
return foldPersonalityName(value)
}
+// Desktop prepends attachment ref tags (@image:, @file:, @url:, @folder:,
+// @terminal:, @line:, @session:, @tool:, ...) to the submitted wire text. A
+// slash command typed after those refs must still be detected — strip leading
+// ref lines before testing the text for a command. Mirrors the gateway's
+// _ATTACHMENT_REF_RE, but covers every ref kind the composer can emit.
+const ATTACHMENT_REF_LINE_RE = /^@[a-z][a-z0-9-]*:[^\n]*\n?/i
+
+export function stripAttachmentRefs(text: string): string {
+ let current = text ?? ''
+
+ // eslint-disable-next-line no-constant-condition
+ while (true) {
+ const next = current.replace(ATTACHMENT_REF_LINE_RE, '')
+ if (next === current) {
+ break
+ }
+ current = next
+ }
+
+ return current
+}
+
+export function isSlashCommandText(text: string): boolean {
+ return SLASH_COMMAND_RE.test(stripAttachmentRefs(text).trimStart())
+}
+
+export function parseSlashCommand(command: string) {
+ // `[\s\S]*` (not `.*`): the arg may span newlines — `/goal <multi-line text>`
+ // or a skill command with a long pasted context. The old `.*$` regex failed
+ // the whole match on any newline, so every multiline slash command parsed as
+ // an empty name and got swallowed (#41323, #55510). The backend and CLI both
+ // split on any whitespace (`split(maxsplit=1)`), so this is the parity fix.
+ const match = command.replace(/^\/+/, '').match(/^(\S+)([\s\S]*)$/)
+
+ return match ? { name: match[1], arg: match[2].trim() } : { name: '', arg: '' }
+}
+
+export function parseCommandDispatch(raw: unknown): CommandDispatchResponse | null {
+ if (!raw || typeof raw !== 'object') {
+ return null
+ }
+
+ const row = raw as Record<string, unknown>
+ const str = (value: unknown) => (typeof value === 'string' ? value : undefined)
+
+ switch (row.type) {
+ case 'exec':
+
+ case 'plugin':
+ return { type: row.type, output: str(row.output) }
+
+ case 'alias':
+ return typeof row.target === 'string' ? { type: 'alias', target: row.target } : null
+
+ case 'skill':
+ return typeof row.name === 'string'
+ ? { type: 'skill', name: row.name, message: str(row.message), display: str(row.display) }
+ : null
+
+ case 'send':
+ return typeof row.message === 'string'
+ ? { type: 'send', message: row.message, notice: str(row.notice), display: str(row.display) }
+ : null
+
+ case 'prefill':
+ return typeof row.message === 'string' ? { type: 'prefill', message: row.message, notice: str(row.notice) } : null
+
+ default:
+ return null
+ }
+}
+
export function quickModelOptions(
data: ModelOptionsResult | undefined,
currentProvider: string,