Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(conversations): correct thread run mode handling for queued messages

Steven Enamakelffc9fcb52466
diff --git a/app/src/features/conversations/aui/queueAdapter.test.tsx b/app/src/features/conversations/aui/queueAdapter.test.tsx
new file mode 100644
index 0000000000..8acdf29477
--- /dev/null
+++ b/app/src/features/conversations/aui/queueAdapter.test.tsx
@@ -0,0 +1,150 @@
+import type { AppendMessage } from '@assistant-ui/react';
+import { combineReducers, configureStore } from '@reduxjs/toolkit';
+import { act, renderHook, waitFor } from '@testing-library/react';
+import type { ReactNode } from 'react';
+import { Provider } from 'react-redux';
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { chatRemoveQueueItem } from '../../../services/chatService';
+import chatRuntimeReducer from '../../../store/chatRuntimeSlice';
+import queueReducer, { pendingFollowupAdded, queueItemQueued } from '../../../store/queueSlice';
+import { buildOpenHumanQueueAdapter, useOpenHumanQueueAdapter } from './queueAdapter';
+
+vi.mock('../../../services/chatService', () => ({ chatRemoveQueueItem: vi.fn() }));
+
+const append = (text: string): AppendMessage =>
+ ({ role: 'user', content: [{ type: 'text', text }] }) as unknown as AppendMessage;
+
+function buildStore() {
+ return configureStore({
+ reducer: combineReducers({ chatRuntime: chatRuntimeReducer, queue: queueReducer }),
+ });
+}
+
+describe('buildOpenHumanQueueAdapter', () => {
+ it('projects core items onto assistant-ui queue items', () => {
+ const adapter = buildOpenHumanQueueAdapter({
+ items: [{ id: 'q1', lane: null, textPreview: 'and the pricing?' }],
+ send: vi.fn(),
+ remove: vi.fn(),
+ });
+
+ expect(adapter.items).toEqual([
+ {
+ id: 'q1',
+ prompt: 'and the pricing?',
+ parts: [{ type: 'text', text: 'and the pricing?' }],
+ },
+ ]);
+ expect(adapter.steerItems).toEqual([]);
+ });
+
+ it('sends both lanes through the host send path, which owns queue_mode', () => {
+ const send = vi.fn().mockResolvedValue(undefined);
+ const adapter = buildOpenHumanQueueAdapter({ items: [], send, remove: vi.fn() });
+
+ adapter.enqueue(append('idle send'));
+ adapter.steer(append('send while running'));
+
+ expect(send.mock.calls.map(([m]) => (m as AppendMessage).content)).toEqual([
+ [{ type: 'text', text: 'idle send' }],
+ [{ type: 'text', text: 'send while running' }],
+ ]);
+ });
+
+ it('swallows a rejected send (the host surfaces it) instead of an unhandled rejection', async () => {
+ const send = vi.fn().mockRejectedValue(new Error('no surface'));
+ const adapter = buildOpenHumanQueueAdapter({ items: [], send, remove: vi.fn() });
+
+ expect(() => adapter.enqueue(append('x'))).not.toThrow();
+ await Promise.resolve();
+ expect(send).toHaveBeenCalledTimes(1);
+ });
+
+ it('forwards removal and ignores move/edit, which the core queue cannot do', () => {
+ const remove = vi.fn();
+ const send = vi.fn();
+ const adapter = buildOpenHumanQueueAdapter({ items: [], send, remove });
+
+ adapter.remove('q1');
+ adapter.move('q1', { lane: 'steer' });
+ adapter.edit('q1', append('edited'));
+
+ expect(remove).toHaveBeenCalledWith('q1');
+ expect(send).not.toHaveBeenCalled();
+ });
+});
+
+describe('useOpenHumanQueueAdapter', () => {
+ beforeEach(() => vi.mocked(chatRemoveQueueItem).mockReset());
+
+ const wrapperFor =
+ (store: ReturnType<typeof buildStore>) =>
+ ({ children }: { children: ReactNode }) => <Provider store={store}>{children}</Provider>;
+
+ it('reads the thread queue from the store and keeps items referentially stable', () => {
+ const store = buildStore();
+ store.dispatch(queueItemQueued({ threadId: 't1', item: { id: 'q1', text_preview: 'hi' } }));
+ const { result, rerender } = renderHook(() => useOpenHumanQueueAdapter('t1', vi.fn()), {
+ wrapper: wrapperFor(store),
+ });
+
+ const first = result.current.items;
+ rerender();
+ expect(result.current.items).toBe(first);
+ expect(first.map(i => i.id)).toEqual(['q1']);
+ });
+
+ it('is empty without a thread', () => {
+ const { result } = renderHook(() => useOpenHumanQueueAdapter(null, vi.fn()), {
+ wrapper: wrapperFor(buildStore()),
+ });
+ expect(result.current.items).toEqual([]);
+ });
+
+ it('removes an item (and its pending follow-up) once the core confirms', async () => {
+ vi.mocked(chatRemoveQueueItem).mockResolvedValue(true);
+ const store = buildStore();
+ store.dispatch(
+ pendingFollowupAdded({
+ threadId: 't1',
+ text: 'drop me',
+ message: {
+ id: 'm1',
+ content: 'drop me',
+ type: 'text',
+ extraMetadata: {},
+ sender: 'user',
+ createdAt: '2026-01-01T00:00:00.000Z',
+ },
+ })
+ );
+ store.dispatch(queueItemQueued({ threadId: 't1', item: { id: 'q1', text_preview: 'drop me' } }));
+ const { result } = renderHook(() => useOpenHumanQueueAdapter('t1', vi.fn()), {
+ wrapper: wrapperFor(store),
+ });
+
+ act(() => result.current.remove('q1'));
+
+ await waitFor(() => expect(store.getState().queue.itemsByThread.t1).toBeUndefined());
+ expect(chatRemoveQueueItem).toHaveBeenCalledWith('t1', 'q1');
+ expect(store.getState().queue.pendingFollowupsByThread.t1).toBeUndefined();
+ });
+
+ it('keeps the item when the core does not confirm the removal', async () => {
+ vi.mocked(chatRemoveQueueItem).mockResolvedValue(false);
+ const store = buildStore();
+ store.dispatch(queueItemQueued({ threadId: 't1', item: { id: 'q1', text_preview: 'stay' } }));
+ const { result } = renderHook(() => useOpenHumanQueueAdapter('t1', vi.fn()), {
+ wrapper: wrapperFor(store),
+ });
+
+ await act(async () => {
+ result.current.remove('q1');
+ await Promise.resolve();
+ });
+
+ expect(chatRemoveQueueItem).toHaveBeenCalledWith('t1', 'q1');
+ expect(store.getState().queue.itemsByThread.t1).toHaveLength(1);
+ });
+});
diff --git a/app/src/features/conversations/aui/queueAdapter.ts b/app/src/features/conversations/aui/queueAdapter.ts
new file mode 100644
index 0000000000..8dbfa60da2
--- /dev/null
+++ b/app/src/features/conversations/aui/queueAdapter.ts
@@ -0,0 +1,19 @@
+import type { AppendMessage, ExternalThreadQueueAdapter } from '@assistant-ui/react';
+
+import type { RunQueueItem } from '../../../store/queueSlice';
+
+export function buildOpenHumanQueueAdapter(_args: {
+ items: readonly RunQueueItem[];
+ send: (message: AppendMessage) => Promise<void>;
+ remove: (itemId: string) => void;
+}): ExternalThreadQueueAdapter {
+ const noop = () => {};
+ return { items: [], steerItems: [], enqueue: noop, steer: noop, move: noop, edit: noop, remove: noop };
+}
+
+export function useOpenHumanQueueAdapter(
+ _threadId: string | null,
+ _send: (message: AppendMessage) => Promise<void>
+): ExternalThreadQueueAdapter {
+ return buildOpenHumanQueueAdapter({ items: [], send: _send, remove: () => {} });
+}
diff --git a/app/src/features/conversations/aui/useRunMode.ts b/app/src/features/conversations/aui/useRunMode.ts
index d18bd8d4dc..9bdf885ea1 100644
--- a/app/src/features/conversations/aui/useRunMode.ts
+++ b/app/src/features/conversations/aui/useRunMode.ts
@@ -16,6 +16,7 @@ import debug from 'debug';
import { useCallback, useEffect, useRef } from 'react';
import { callCoreRpc } from '../../../services/coreRpcClient';
+import { store } from '../../../store';
import { useAppDispatch, useAppSelector } from '../../../store/hooks';
import { type RunMode, setRunMode } from '../../../store/runModeSlice';
@@ -38,6 +39,9 @@ export function useRunMode(threadId: string | null): UseRunModeResult {
useEffect(() => {
if (!threadId || loadedFor.current === threadId) return;
loadedFor.current = threadId;
+ // Only fetch when the slice has no live entry yet — a value already set
+ // (e.g. by a `run_mode_changed` event that arrived first) wins.
+ if (store.getState().runMode.byThread[threadId] !== undefined) return;
let cancelled = false;
void (async () => {
try {
diff --git a/crates/openhuman-core/src/threads/schemas/handlers.rs b/crates/openhuman-core/src/threads/schemas/handlers.rs
index c8dd9b00b5..31e53b4662 100644
--- a/crates/openhuman-core/src/threads/schemas/handlers.rs
+++ b/crates/openhuman-core/src/threads/schemas/handlers.rs
@@ -133,6 +133,20 @@ pub(super) fn handle_transcript_get(params: Map<String, Value>) -> ControllerFut
})
}
+pub(super) fn handle_goal_get(params: Map<String, Value>) -> ControllerFuture {
+ Box::pin(async move {
+ let p = parse::<ops::ThreadLiveStateRequest>(params)?;
+ to_json(ops::goal_get(p).await?)
+ })
+}
+
+pub(super) fn handle_todos_get(params: Map<String, Value>) -> ControllerFuture {
+ Box::pin(async move {
+ let p = parse::<ops::ThreadLiveStateRequest>(params)?;
+ to_json(ops::todos_get(p).await?)
+ })
+}
+
// ── Helpers ──────────────────────────────────────────────────────────
pub(super) fn parse<T: DeserializeOwned>(params: Map<String, Value>) -> Result<T, String> {