The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { $composerAttachments, addComposerAttachment, type ComposerAttachment, mainComposerScope } from './composer'
import {
$parkedQueueSessions,
$queuedPromptsBySession,
clearQueuedPrompts,
dequeueQueuedPrompt,
enqueueQueuedPrompt,
getFrozenQueuedTransport,
getQueuedPrompts,
isQueueParked,
migrateQueuedPrompts,
parkQueuedPrompts,
promoteQueuedPrompt,
queuedSubmissionId,
removeQueuedPrompt,
resetFrozenQueuedTransportsForTests,
resolveQueuedPromptTransport,
shouldAutoDrain,
simulateComposerQueueReloadForTests,
unparkQueuedPrompts,
updateQueuedPrompt,
updateQueuedPromptText
} from './composer-queue'
const SESSION_KEY = 'session-abc'
const QUEUE_STORAGE_KEY = 'hermes.desktop.composerQueue.v1'
function attachment(id: string, kind: ComposerAttachment['kind'] = 'file'): ComposerAttachment {
return {
id,
kind,
label: id,
refText: `@file:${id}`
}
}
function stubRevokeObjectURL() {
const revokeObjectURL = vi.fn()
vi.stubGlobal('URL', { ...URL, revokeObjectURL })
return revokeObjectURL
}
describe('composer queue store', () => {
beforeEach(() => {
window.localStorage.removeItem(QUEUE_STORAGE_KEY)
$queuedPromptsBySession.set({})
$composerAttachments.set([])
vi.unstubAllGlobals()
vi.restoreAllMocks()
})
it('queued-prompt handoff keeps blob previews across composer clear, then revokes when the entry is discarded', () => {
// Mirrors use-composer-queue: enqueue → clear({ retainPreviewUrls }).
const revokeObjectURL = stubRevokeObjectURL()
const blobUrl = 'blob:hermes-queued-1'
const image = {
id: 'image:drop',
kind: 'image' as const,
label: 'Lattice.png',
previewUrl: blobUrl
}
addComposerAttachment(image)
const queued = enqueueQueuedPrompt(SESSION_KEY, {
text: 'look at this',
attachments: $composerAttachments.get()
})
mainComposerScope.clear({ retainPreviewUrls: true })
expect(queued).not.toBeNull()
expect($composerAttachments.get()).toEqual([])
expect(revokeObjectURL).not.toHaveBeenCalled()
expect(getQueuedPrompts(SESSION_KEY)[0]?.attachments[0]?.previewUrl).toBe(blobUrl)
expect(removeQueuedPrompt(SESSION_KEY, queued!.id)).toBe(true)
expect(revokeObjectURL).toHaveBeenCalledWith(blobUrl)
})
it('drain handoff retains blob previews when the queued entry is removed after submit owns them', () => {
const revokeObjectURL = stubRevokeObjectURL()
const blobUrl = 'blob:hermes-drain-1'
const queued = enqueueQueuedPrompt(SESSION_KEY, {
text: 'drain me',
attachments: [{ id: 'image:drop', kind: 'image', label: 'shot.png', previewUrl: blobUrl }]
})
expect(queued).not.toBeNull()
expect(removeQueuedPrompt(SESSION_KEY, queued!.id, { retainPreviewUrls: true })).toBe(true)
expect(revokeObjectURL).not.toHaveBeenCalled()
})
it('revokes replaced blob previews when a queued entry attachment snapshot changes', () => {
const revokeObjectURL = stubRevokeObjectURL()
const oldUrl = 'blob:hermes-old'
const newUrl = 'blob:hermes-new'
const queued = enqueueQueuedPrompt(SESSION_KEY, {
text: 'edit me',
attachments: [{ id: 'image:old', kind: 'image', label: 'old.png', previewUrl: oldUrl }]
})
expect(queued).not.toBeNull()
expect(
updateQueuedPrompt(SESSION_KEY, queued!.id, {
text: 'edit me',
attachments: [{ id: 'image:new', kind: 'image', label: 'new.png', previewUrl: newUrl }]
})
).toBe(true)
expect(revokeObjectURL).toHaveBeenCalledWith(oldUrl)
expect(revokeObjectURL).not.toHaveBeenCalledWith(newUrl)
})
it('queues prompts in FIFO order', () => {
enqueueQueuedPrompt(SESSION_KEY, { attachments: [], text: 'first' })
enqueueQueuedPrompt(SESSION_KEY, { attachments: [], text: 'second' })
expect(dequeueQueuedPrompt(SESSION_KEY)?.text).toBe('first')
expect(dequeueQueuedPrompt(SESSION_KEY)?.text).toBe('second')
expect(dequeueQueuedPrompt(SESSION_KEY)).toBeNull()
})
it('clones attachments when queueing', () => {
const source = [attachment('a-1')]
const queued = enqueueQueuedPrompt(SESSION_KEY, { attachments: source, text: 'check clones' })
expect(queued).not.toBeNull()
expect(getQueuedPrompts(SESSION_KEY)[0]?.attachments[0]).toEqual(source[0])
expect(getQueuedPrompts(SESSION_KEY)[0]?.attachments[0]).not.toBe(source[0])
})
it('updates and removes queued entries by id', () => {
const first = enqueueQueuedPrompt(SESSION_KEY, { attachments: [], text: 'draft one' })
const second = enqueueQueuedPrompt(SESSION_KEY, { attachments: [], text: 'draft two' })
expect(first).not.toBeNull()
expect(second).not.toBeNull()
expect(updateQueuedPromptText(SESSION_KEY, first!.id, 'draft one edited')).toBe(true)
expect(getQueuedPrompts(SESSION_KEY).map(entry => entry.text)).toEqual(['draft one edited', 'draft two'])
expect(removeQueuedPrompt(SESSION_KEY, first!.id)).toBe(true)
expect(getQueuedPrompts(SESSION_KEY).map(entry => entry.text)).toEqual(['draft two'])
})
it('promotes a queued entry to the front', () => {
const first = enqueueQueuedPrompt(SESSION_KEY, { attachments: [], text: 'first' })
const second = enqueueQueuedPrompt(SESSION_KEY, { attachments: [], text: 'second' })
const third = enqueueQueuedPrompt(SESSION_KEY, { attachments: [], text: 'third' })
expect(first).not.toBeNull()
expect(second).not.toBeNull()
expect(third).not.toBeNull()
expect(promoteQueuedPrompt(SESSION_KEY, third!.id)).toBe(true)
expect(getQueuedPrompts(SESSION_KEY).map(entry => entry.text)).toEqual(['third', 'first', 'second'])
expect(promoteQueuedPrompt(SESSION_KEY, third!.id)).toBe(false)
})
it('updates queued text and attachment snapshot', () => {
const first = enqueueQueuedPrompt(SESSION_KEY, { attachments: [attachment('f-1')], text: 'draft one' })
const editedAttachments = [attachment('f-2'), attachment('f-3', 'image')]
expect(first).not.toBeNull()
expect(
updateQueuedPrompt(SESSION_KEY, first!.id, {
attachments: editedAttachments,
text: 'edited text'
})
).toBe(true)
const queue = getQueuedPrompts(SESSION_KEY)
expect(queue[0]?.text).toBe('edited text')
expect(queue[0]?.attachments).toEqual(editedAttachments)
expect(queue[0]?.attachments[0]).not.toBe(editedAttachments[0])
})
it('clears queue state for a session', () => {
enqueueQueuedPrompt(SESSION_KEY, { attachments: [attachment('img-1', 'image')], text: 'queued' })
clearQueuedPrompts(SESSION_KEY)
expect(getQueuedPrompts(SESSION_KEY)).toEqual([])
expect($queuedPromptsBySession.get()[SESSION_KEY]).toBeUndefined()
expect(window.localStorage.getItem(QUEUE_STORAGE_KEY)).toBeNull()
})
it('persists queue entries into local storage', () => {
enqueueQueuedPrompt(SESSION_KEY, { attachments: [], text: 'persist me' })
const raw = window.localStorage.getItem(QUEUE_STORAGE_KEY)
expect(raw).toBeTruthy()
const parsed = JSON.parse(String(raw)) as Record<string, { text: string }[]>
expect(parsed[SESSION_KEY]?.[0]?.text).toBe('persist me')
})
})
describe('migrateQueuedPrompts', () => {
beforeEach(() => {
window.localStorage.removeItem(QUEUE_STORAGE_KEY)
$queuedPromptsBySession.set({})
resetFrozenQueuedTransportsForTests()
})
it('moves entries from a dead runtime key onto the live one', () => {
enqueueQueuedPrompt('rt-old', { attachments: [], text: 'stranded' })
expect(migrateQueuedPrompts('rt-old', 'rt-new')).toBe(true)
expect(getQueuedPrompts('rt-old')).toEqual([])
expect(getQueuedPrompts('rt-new').map(e => e.text)).toEqual(['stranded'])
// The dead key is dropped from the store entirely.
expect($queuedPromptsBySession.get()['rt-old']).toBeUndefined()
})
it('appends after existing target entries (FIFO preserved)', () => {
enqueueQueuedPrompt('rt-new', { attachments: [], text: 'already here' })
enqueueQueuedPrompt('rt-old', { attachments: [], text: 'migrated' })
migrateQueuedPrompts('rt-old', 'rt-new')
expect(getQueuedPrompts('rt-new').map(e => e.text)).toEqual(['already here', 'migrated'])
})
it('is a no-op when source is empty or keys match', () => {
expect(migrateQueuedPrompts('rt-old', 'rt-new')).toBe(false)
expect(migrateQueuedPrompts('rt-x', 'rt-x')).toBe(false)
})
it('migrating the same submission twice keeps one entry (#138934)', () => {
enqueueQueuedPrompt('rt-old', { attachments: [], id: 'sub-1', text: 'once' })
migrateQueuedPrompts('rt-old', 'rt-new')
enqueueQueuedPrompt('rt-old', { attachments: [], id: 'sub-1', text: 'once' })
migrateQueuedPrompts('rt-old', 'rt-new')
expect(getQueuedPrompts('rt-new').map(e => e.id)).toEqual(['sub-1'])
expect(getQueuedPrompts('rt-old')).toEqual([])
})
})
describe('shouldAutoDrain', () => {
it('drains whenever idle with a non-empty queue', () => {
expect(shouldAutoDrain({ isBusy: false, queueLength: 1 })).toBe(true)
})
it('does not drain mid-turn', () => {
expect(shouldAutoDrain({ isBusy: true, queueLength: 1 })).toBe(false)
})
it('does not drain an empty queue', () => {
expect(shouldAutoDrain({ isBusy: false, queueLength: 0 })).toBe(false)
})
it('does not drain a parked queue, even when idle', () => {
// The Stop/Esc settle edge: busy just flipped false but the user asked to
// HALT — the park must hold the head back until they resume.
expect(shouldAutoDrain({ isBusy: false, parked: true, queueLength: 1 })).toBe(false)
})
})
describe('parked queue sessions', () => {
beforeEach(() => {
window.localStorage.removeItem(QUEUE_STORAGE_KEY)
$queuedPromptsBySession.set({})
$parkedQueueSessions.set({})
resetFrozenQueuedTransportsForTests()
})
it('parks only sessions with queued entries', () => {
expect(parkQueuedPrompts(SESSION_KEY)).toBe(false)
expect(isQueueParked(SESSION_KEY)).toBe(false)
enqueueQueuedPrompt(SESSION_KEY, { attachments: [], text: 'held back' })
expect(parkQueuedPrompts(SESSION_KEY)).toBe(true)
expect(isQueueParked(SESSION_KEY)).toBe(true)
})
it('unparks explicitly', () => {
enqueueQueuedPrompt(SESSION_KEY, { attachments: [], text: 'held back' })
parkQueuedPrompts(SESSION_KEY)
unparkQueuedPrompts(SESSION_KEY)
expect(isQueueParked(SESSION_KEY)).toBe(false)
})
it('queueing a fresh prompt lifts the park', () => {
enqueueQueuedPrompt(SESSION_KEY, { attachments: [], text: 'held back' })
parkQueuedPrompts(SESSION_KEY)
enqueueQueuedPrompt(SESSION_KEY, { attachments: [], text: 'new intent' })
expect(isQueueParked(SESSION_KEY)).toBe(false)
})
it('emptying the queue drops the park', () => {
const entry = enqueueQueuedPrompt(SESSION_KEY, { attachments: [], text: 'held back' })
parkQueuedPrompts(SESSION_KEY)
removeQueuedPrompt(SESSION_KEY, entry!.id)
expect(isQueueParked(SESSION_KEY)).toBe(false)
})
it('a park travels with migrated entries', () => {
// A backend bounce right after Stop re-keys the queue; shedding the park
// there would auto-send the exact prompts the user just halted.
enqueueQueuedPrompt('rt-old', { attachments: [], text: 'held back' })
parkQueuedPrompts('rt-old')
migrateQueuedPrompts('rt-old', 'rt-new')
expect(isQueueParked('rt-old')).toBe(false)
expect(isQueueParked('rt-new')).toBe(true)
})
it('migration without a park does not invent one', () => {
enqueueQueuedPrompt('rt-old', { attachments: [], text: 'flowing' })
migrateQueuedPrompts('rt-old', 'rt-new')
expect(isQueueParked('rt-new')).toBe(false)
})
})
describe('hidden entries', () => {
beforeEach(() => {
clearQueuedPrompts('hidden-session')
})
it('keeps the hidden kind on a queued note and leaves visible entries without one', () => {
enqueueQueuedPrompt('hidden-session', { text: '[setup] links opened', attachments: [], displayKind: 'hidden' })
enqueueQueuedPrompt('hidden-session', { text: 'Start without connections.', attachments: [] })
expect(getQueuedPrompts('hidden-session').map(({ text, displayKind }) => ({ text, displayKind }))).toEqual([
{ text: '[setup] links opened', displayKind: 'hidden' },
{ text: 'Start without connections.', displayKind: undefined }
])
})
})
describe('cross-window sync (#46732)', () => {
beforeEach(() => {
window.localStorage.removeItem(QUEUE_STORAGE_KEY)
$queuedPromptsBySession.set({})
})
const storedEntry = (id: string, text: string) => ({ id, text, attachments: [], queuedAt: 1 })
const dispatchStorage = (key: null | string, newValue: null | string) => {
window.dispatchEvent(new StorageEvent('storage', { key, newValue }))
}
it("adopts another window's write from the storage event", () => {
window.localStorage.setItem(
QUEUE_STORAGE_KEY,
JSON.stringify({ 'session-other': [storedEntry('q1', 'from other window')] })
)
dispatchStorage(QUEUE_STORAGE_KEY, window.localStorage.getItem(QUEUE_STORAGE_KEY))
expect(getQueuedPrompts('session-other').map(entry => entry.text)).toEqual(['from other window'])
})
it("does not clobber another window's entries when saving its own (same-frame race)", () => {
enqueueQueuedPrompt(SESSION_KEY, { attachments: [], text: 'mine first' })
// Another window queues into its own session directly in storage, faster
// than any storage event could reach us.
window.localStorage.setItem(
QUEUE_STORAGE_KEY,
JSON.stringify({
...JSON.parse(window.localStorage.getItem(QUEUE_STORAGE_KEY)!),
'session-other': [storedEntry('q2', 'theirs')]
})
)
enqueueQueuedPrompt(SESSION_KEY, { attachments: [], text: 'mine second' })
expect(getQueuedPrompts(SESSION_KEY).map(entry => entry.text)).toEqual(['mine first', 'mine second'])
expect(getQueuedPrompts('session-other').map(entry => entry.text)).toEqual(['theirs'])
})
it('drops entries locally once another window drains them', () => {
enqueueQueuedPrompt(SESSION_KEY, { attachments: [], text: 'drained elsewhere' })
window.localStorage.setItem(QUEUE_STORAGE_KEY, JSON.stringify({}))
dispatchStorage(QUEUE_STORAGE_KEY, '{}')
expect(getQueuedPrompts(SESSION_KEY)).toEqual([])
})
it('resyncs on a full storage clear (event.key === null)', () => {
enqueueQueuedPrompt(SESSION_KEY, { attachments: [], text: 'wiped' })
window.localStorage.removeItem(QUEUE_STORAGE_KEY)
dispatchStorage(null, null)
expect(getQueuedPrompts(SESSION_KEY)).toEqual([])
})
it("keeps both windows' entries when they queue into the same session before either syncs", async () => {
// One fresh module instance per window, both booted from the same storage.
vi.resetModules()
const windowA = await import('./composer-queue')
vi.resetModules()
const windowB = await import('./composer-queue')
windowA.enqueueQueuedPrompt(SESSION_KEY, { attachments: [], text: 'x from A' })
// B's storage event for A's write has not arrived yet.
windowB.enqueueQueuedPrompt(SESSION_KEY, { attachments: [], text: 'y from B' })
const stored = JSON.parse(window.localStorage.getItem(QUEUE_STORAGE_KEY)!)[SESSION_KEY]
expect(stored.map((entry: { text: string }) => entry.text)).toEqual(['x from A', 'y from B'])
})
it('ignores storage events for unrelated keys', () => {
enqueueQueuedPrompt(SESSION_KEY, { attachments: [], text: 'kept' })
dispatchStorage('unrelated.key', '{}')
expect(getQueuedPrompts(SESSION_KEY).map(entry => entry.text)).toEqual(['kept'])
})
})
const TERMINAL_CHIP_DRAFT = 'look at @terminal:`zsh:23-58`'
const SELECTION_A = 'selection A from pane one'
const SELECTION_A_TRANSPORT = '```terminal\nselection A from pane one\n```\n\nlook at'
describe('composer queue terminal payload persistence', () => {
beforeEach(() => {
window.localStorage.removeItem(QUEUE_STORAGE_KEY)
$queuedPromptsBySession.set({})
resetFrozenQueuedTransportsForTests()
})
it('does not persist frozen terminal CONTENTS into hermes.desktop.composerQueue.v1', () => {
const entry = enqueueQueuedPrompt(SESSION_KEY, {
attachments: [],
text: TERMINAL_CHIP_DRAFT,
displayText: TERMINAL_CHIP_DRAFT,
frozenTransport: SELECTION_A_TRANSPORT
})
expect(entry).not.toBeNull()
expect(getFrozenQueuedTransport(entry!.id)).toBe(SELECTION_A_TRANSPORT)
const raw = String(window.localStorage.getItem(QUEUE_STORAGE_KEY))
expect(raw).toContain(TERMINAL_CHIP_DRAFT)
expect(raw).not.toContain(SELECTION_A)
expect(raw).not.toContain('```terminal')
expect(JSON.parse(raw)[SESSION_KEY][0].text).toBe(TERMINAL_CHIP_DRAFT)
})
it('fails closed after reload when chips remain but the runtime payload is gone', () => {
const entry = enqueueQueuedPrompt(SESSION_KEY, {
attachments: [],
text: TERMINAL_CHIP_DRAFT,
displayText: TERMINAL_CHIP_DRAFT,
frozenTransport: SELECTION_A_TRANSPORT
})
expect(resolveQueuedPromptTransport(entry!).ok).toBe(true)
simulateComposerQueueReloadForTests()
const restored = getQueuedPrompts(SESSION_KEY)[0]
expect(restored).toBeDefined()
expect(restored!.id).toBe(entry!.id)
expect(restored!.text).toBe(TERMINAL_CHIP_DRAFT)
expect(getFrozenQueuedTransport(restored!.id)).toBeUndefined()
const resolved = resolveQueuedPromptTransport(restored!)
expect(resolved).toEqual({ ok: false, reason: 'missing-terminal-payload' })
})
it('lets an ordinary text queue survive reload and send normally', () => {
const entry = enqueueQueuedPrompt(SESSION_KEY, { attachments: [], text: 'plain follow-up' })
simulateComposerQueueReloadForTests()
const restored = getQueuedPrompts(SESSION_KEY)[0]
expect(restored!.id).toBe(entry!.id)
expect(resolveQueuedPromptTransport(restored!)).toEqual({
ok: true,
transportText: 'plain follow-up'
})
})
it('drains an entry whose terminal selection was replaced under a new submission id', () => {
const entry = enqueueQueuedPrompt(SESSION_KEY, {
attachments: [],
text: TERMINAL_CHIP_DRAFT,
displayText: TERMINAL_CHIP_DRAFT,
frozenTransport: SELECTION_A_TRANSPORT
})
// Same chip text, a different selection behind it: the payload sent changed.
updateQueuedPrompt(SESSION_KEY, entry!.id, {
text: TERMINAL_CHIP_DRAFT,
displayText: TERMINAL_CHIP_DRAFT,
frozenTransport: '```terminal\nselection B\n```\n\nlook at'
})
expect(queuedSubmissionId(getQueuedPrompts(SESSION_KEY)[0]!)).not.toBe(entry!.id)
})
it('cleans the ephemeral payload on remove and clear', () => {
const first = enqueueQueuedPrompt(SESSION_KEY, {
attachments: [],
text: TERMINAL_CHIP_DRAFT,
displayText: TERMINAL_CHIP_DRAFT,
frozenTransport: SELECTION_A_TRANSPORT
})
const second = enqueueQueuedPrompt(SESSION_KEY, {
attachments: [],
text: 'second @terminal:`bash:1-2`',
displayText: 'second @terminal:`bash:1-2`',
frozenTransport: '```terminal\nother\n```\n\nsecond'
})
expect(getFrozenQueuedTransport(first!.id)).toBeDefined()
expect(removeQueuedPrompt(SESSION_KEY, first!.id)).toBe(true)
expect(getFrozenQueuedTransport(first!.id)).toBeUndefined()
expect(getFrozenQueuedTransport(second!.id)).toBeDefined()
const dequeued = dequeueQueuedPrompt(SESSION_KEY)
expect(dequeued?.id).toBe(second!.id)
expect(getFrozenQueuedTransport(second!.id)).toBeUndefined()
const third = enqueueQueuedPrompt(SESSION_KEY, {
attachments: [],
text: TERMINAL_CHIP_DRAFT,
displayText: TERMINAL_CHIP_DRAFT,
frozenTransport: SELECTION_A_TRANSPORT
})
clearQueuedPrompts(SESSION_KEY)
expect(getFrozenQueuedTransport(third!.id)).toBeUndefined()
expect(getQueuedPrompts(SESSION_KEY)).toEqual([])
})
it('preserves the ephemeral payload by id across migrate, promote, and replacement edit', () => {
const entry = enqueueQueuedPrompt('rt-old', {
attachments: [],
text: TERMINAL_CHIP_DRAFT,
displayText: TERMINAL_CHIP_DRAFT,
frozenTransport: SELECTION_A_TRANSPORT
})
enqueueQueuedPrompt('rt-old', { attachments: [], text: 'later' })
expect(migrateQueuedPrompts('rt-old', 'rt-new')).toBe(true)
expect(entry!.id).toBe(getQueuedPrompts('rt-new')[0]?.id)
expect(getFrozenQueuedTransport(entry!.id)).toBe(SELECTION_A_TRANSPORT)
const later = getQueuedPrompts('rt-new')[1]!
expect(promoteQueuedPrompt('rt-new', later.id)).toBe(true)
expect(getFrozenQueuedTransport(entry!.id)).toBe(SELECTION_A_TRANSPORT)
const replacement = '```terminal\nselection B after edit\n```\n\nlook at'
expect(
updateQueuedPrompt('rt-new', entry!.id, {
text: TERMINAL_CHIP_DRAFT,
displayText: TERMINAL_CHIP_DRAFT,
frozenTransport: replacement
})
).toBe(true)
expect(getFrozenQueuedTransport(entry!.id)).toBe(replacement)
expect(String(window.localStorage.getItem(QUEUE_STORAGE_KEY))).not.toContain('selection B after edit')
expect(
updateQueuedPrompt('rt-new', entry!.id, {
text: 'no longer a terminal chip',
frozenTransport: null
})
).toBe(true)
expect(getFrozenQueuedTransport(entry!.id)).toBeUndefined()
})
})