/** * Regression: web UI input stuck in running state after turn completes. * * isLoading in SessionDetail is driven by session_status events; when the * turn's result event does not flip the session to idle, every new message * lands in the queue instead of being sent. Covers both worker write paths: * - v1 session-ingress (HybridTransport POST → ingestBridgeMessage) * - v2 CCR worker events (POST /v1/code/sessions/:id/worker/events) * Both must end the turn with a derived session_status event, status=idle. * * Also covers the inversion variant: the worker's bridge write paths are not * strictly ordered, so the turn-end result can arrive BEFORE the final * mirrored assistant message. A late assistant must NOT flip the session * back to running (turn-signal guard, EventBus.turnActive) — that re-engaged * the web UI input and left every subsequent message stuck in the queue. */ import { describe, test, expect, beforeEach, mock } from 'bun:test' import { mockConfigModule } from './helpers/mock-config' mock.module('../config', () => mockConfigModule({ pollTimeout: 1 })) import { Hono } from 'hono' import { storeReset } from '../store' import { getAllEventBuses, removeEventBus, getEventBus, type SessionEvent, } from '../transport/event-bus' import v1SessionIngress from '../routes/v1/session-ingress' import v2WorkerEvents from '../routes/v2/worker-events' import webSessions from '../routes/web/sessions' import webControl from '../routes/web/control' import { issueToken } from '../auth/token' import { getSession } from '../services/session' function createApp() { const app = new Hono() app.route('/v2/session_ingress', v1SessionIngress) app.route('/v1/code/sessions', v2WorkerEvents) app.route('/web', webSessions) app.route('/web', webControl) return app } const AUTH_HEADERS = { Authorization: 'Bearer test-api-key', 'X-Username': 'testuser', } function auth(user: string): string { return `Bearer ${issueToken(user).token}` } function statusEvents(events: SessionEvent[]): string[] { return events .filter(e => e.type === 'session_status') .map(e => (e.payload as Record).status as string) } describe('turn end emits session_status idle (stuck-running regression)', () => { let app: Hono let sessionId: string beforeEach(async () => { storeReset() for (const [key] of getAllEventBuses()) { removeEventBus(key) } app = createApp() const res = await app.request('/web/sessions', { method: 'POST', headers: { Authorization: auth('user-1'), 'Content-Type': 'application/json', }, body: JSON.stringify({}), }) const body = (await res.json()) as { id: string } sessionId = body.id }) test('v1 session-ingress: user message → assistant → result ends idle', async () => { // 1. Web UI sends a user message → derived status running const sendRes = await app.request(`/web/sessions/${sessionId}/events`, { method: 'POST', headers: { Authorization: auth('user-1'), 'Content-Type': 'application/json', }, body: JSON.stringify({ type: 'user', uuid: 'web-uuid-1', content: 'hello', message: { content: 'hello' }, }), }) expect(sendRes.status).toBe(200) expect(getSession(sessionId)?.status).toBe('running') // 2. Child posts assistant + result via session ingress (HybridTransport) const ingestRes = await app.request( `/v2/session_ingress/session/${sessionId}/events`, { method: 'POST', headers: { ...AUTH_HEADERS, 'Content-Type': 'application/json' }, body: JSON.stringify({ events: [ { type: 'assistant', uuid: 'asst-uuid-1', message: { role: 'assistant', content: [{ type: 'text', text: 'final answer' }], }, }, { type: 'result_success', subtype: 'success', uuid: 'result-uuid-1', result: 'final answer', usage: { input_tokens: 1, output_tokens: 2 }, total_cost_usd: 0, }, ], }), }, ) expect(ingestRes.status).toBe(200) const events = getEventBus(sessionId).getEventsSince(0) const statuses = statusEvents(events) // The turn must end with an idle status so the web UI releases the queue expect(statuses[statuses.length - 1]).toBe('idle') expect(getSession(sessionId)?.status).toBe('idle') }) test('v2 worker events: user → assistant → result derives running → idle', async () => { // User message from the web UI starts the turn (turn-signal) const res0 = await app.request(`/web/sessions/${sessionId}/events`, { method: 'POST', headers: { Authorization: auth('user-1'), 'Content-Type': 'application/json', }, body: JSON.stringify({ type: 'user', uuid: 'v2-web-user-1', content: 'hello', message: { content: 'hello' }, }), }) expect(res0.status).toBe(200) // Assistant event marks the turn running const res1 = await app.request( `/v1/code/sessions/${sessionId}/worker/events`, { method: 'POST', headers: { ...AUTH_HEADERS, 'Content-Type': 'application/json' }, body: JSON.stringify([ { type: 'assistant', uuid: 'v2-asst-1', message: { role: 'assistant', content: [{ type: 'text', text: 'working on it' }], }, }, ]), }, ) expect(res1.status).toBe(200) expect(getSession(sessionId)?.status).toBe('running') // Result event ends the turn — status must flip to idle and a // session_status event must be broadcast to live web viewers const res2 = await app.request( `/v1/code/sessions/${sessionId}/worker/events`, { method: 'POST', headers: { ...AUTH_HEADERS, 'Content-Type': 'application/json' }, body: JSON.stringify([ { type: 'result_success', subtype: 'success', uuid: 'v2-result-1', result: 'done', usage: { input_tokens: 1, output_tokens: 2 }, }, ]), }, ) expect(res2.status).toBe(200) expect(getSession(sessionId)?.status).toBe('idle') const statuses = statusEvents(getEventBus(sessionId).getEventsSince(0)) expect(statuses).toContain('running') expect(statuses[statuses.length - 1]).toBe('idle') }) test('v1 session-ingress: REPL-initiated turn (mirrored user) derives running', async () => { // A turn typed in the terminal REPL reaches the server via the // transcript mirror: [user, assistant] in one writeBatch POST. The // mirrored user message must arm the turn signal so the assistant // event derives running. const res = await app.request( `/v2/session_ingress/session/${sessionId}/events`, { method: 'POST', headers: { ...AUTH_HEADERS, 'Content-Type': 'application/json' }, body: JSON.stringify({ events: [ { type: 'user', uuid: 'repl-user-1', message: { role: 'user', content: 'typed in terminal' }, }, { type: 'assistant', uuid: 'repl-asst-1', message: { role: 'assistant', content: [{ type: 'text', text: 'terminal reply' }], }, }, ], }), }, ) expect(res.status).toBe(200) expect(getSession(sessionId)?.status).toBe('running') }) test('v1 session-ingress: late assistant after result does not resurrect running', async () => { // Inversion regression: the REPL bridge POSTs the synthetic turn-end // result BEFORE the final mirrored assistant message (transcript-mirror // render race). The late assistant must not flip the session back to // running — that left the web UI input engaged and queued every new // message indefinitely. await app.request(`/web/sessions/${sessionId}/events`, { method: 'POST', headers: { Authorization: auth('user-1'), 'Content-Type': 'application/json', }, body: JSON.stringify({ type: 'user', uuid: 'inv-user-1', content: 'hi', message: { content: 'hi' }, }), }) // Worker delivers result first, then the final assistant message const res = await app.request( `/v2/session_ingress/session/${sessionId}/events`, { method: 'POST', headers: { ...AUTH_HEADERS, 'Content-Type': 'application/json' }, body: JSON.stringify({ events: [ { type: 'result_success', subtype: 'success', uuid: 'inv-result-1', result: '', total_cost_usd: 0, usage: {}, }, ], }), }, ) expect(res.status).toBe(200) expect(getSession(sessionId)?.status).toBe('idle') const lateRes = await app.request( `/v2/session_ingress/session/${sessionId}/events`, { method: 'POST', headers: { ...AUTH_HEADERS, 'Content-Type': 'application/json' }, body: JSON.stringify({ events: [ { type: 'assistant', uuid: 'inv-asst-1', message: { role: 'assistant', content: [{ type: 'text', text: 'Hello again!' }], }, }, ], }), }, ) expect(lateRes.status).toBe(200) expect(getSession(sessionId)?.status).toBe('idle') const statuses = statusEvents(getEventBus(sessionId).getEventsSince(0)) expect(statuses[statuses.length - 1]).toBe('idle') }) test('v2 worker events: late assistant after result does not resurrect running', async () => { await app.request(`/web/sessions/${sessionId}/events`, { method: 'POST', headers: { Authorization: auth('user-1'), 'Content-Type': 'application/json', }, body: JSON.stringify({ type: 'user', uuid: 'inv2-user-1', content: 'hi', message: { content: 'hi' }, }), }) await app.request(`/v1/code/sessions/${sessionId}/worker/events`, { method: 'POST', headers: { ...AUTH_HEADERS, 'Content-Type': 'application/json' }, body: JSON.stringify([ { type: 'result_success', subtype: 'success', uuid: 'inv2-result-1', result: '', }, ]), }) expect(getSession(sessionId)?.status).toBe('idle') await app.request(`/v1/code/sessions/${sessionId}/worker/events`, { method: 'POST', headers: { ...AUTH_HEADERS, 'Content-Type': 'application/json' }, body: JSON.stringify([ { type: 'assistant', uuid: 'inv2-asst-1', message: { role: 'assistant', content: [{ type: 'text', text: 'late mirror' }], }, }, ]), }) expect(getSession(sessionId)?.status).toBe('idle') const statuses = statusEvents(getEventBus(sessionId).getEventsSince(0)) expect(statuses[statuses.length - 1]).toBe('idle') }) test('interrupt ends the turn: late assistant does not resurrect running', async () => { await app.request(`/web/sessions/${sessionId}/events`, { method: 'POST', headers: { Authorization: auth('user-1'), 'Content-Type': 'application/json', }, body: JSON.stringify({ type: 'user', uuid: 'intr-user-1', content: 'work', message: { content: 'work' }, }), }) expect(getSession(sessionId)?.status).toBe('running') await app.request(`/web/sessions/${sessionId}/interrupt`, { method: 'POST', headers: { Authorization: auth('user-1') }, }) expect(getSession(sessionId)?.status).toBe('idle') // A mirrored assistant message that was still in flight when the user // hit Stop arrives after the interrupt — must not re-engage running. await app.request(`/v2/session_ingress/session/${sessionId}/events`, { method: 'POST', headers: { ...AUTH_HEADERS, 'Content-Type': 'application/json' }, body: JSON.stringify({ events: [ { type: 'assistant', uuid: 'intr-asst-1', message: { role: 'assistant', content: [{ type: 'text', text: 'partial reply' }], }, }, ], }), }) expect(getSession(sessionId)?.status).toBe('idle') }) test('v2 worker events: control_request derives requires_action', async () => { const res = await app.request( `/v1/code/sessions/${sessionId}/worker/events`, { method: 'POST', headers: { ...AUTH_HEADERS, 'Content-Type': 'application/json' }, body: JSON.stringify([ { type: 'control_request', request_id: 'req-1', request: { subtype: 'can_use_tool', tool_name: 'Bash', input: {}, tool_use_id: 't1', }, }, ]), }, ) expect(res.status).toBe(200) expect(getSession(sessionId)?.status).toBe('requires_action') }) })