Files
hermes-ui/tests/server/run-chat-abort-goal.test.ts
T
toor 8c1ddf415f Phase 003: remove the coding-agent and ekko-agent runtimes
Every chat session now runs through the Hermes bridge, so the three chat
chains become feature-equivalent instead of three partially-maintained
dispatch paths. Keeping the coding-agent and in-house agent runtimes alive
meant every bridge change had to be mirrored into two more runtimes that
had already drifted.

Historical rows are read, not migrated: sessions stored with the retired
source normalize to cli on load, workflow graphs naming the retired agents
keep loading and run on the bridge, and the visible-source check plus
SOURCE_LABELS still recognize the old value so those sessions stay listed.

The public surface follows the code: the OpenAPI generator and the MCP
chat-run tool schema drop the six retired entry fields so external callers
are not told about runtimes that no longer exist.

harness-check gains a RETIRED_RUNTIME scan so the residual check is a
mechanical gate instead of a manual grep, with the two surviving read-side
compat sites recorded with reasons.

Refs: docs/phases/phase_003.md
2026-10-16 13:27:50 +08:00

211 lines
7.5 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest'
const updateSessionStatsMock = vi.fn()
const updateSessionMock = vi.fn()
const flushBridgePendingToDbMock = vi.fn()
const replaceStateMock = vi.fn()
const calcAndUpdateUsageMock = vi.fn()
vi.mock('../../packages/server/src/db/hermes/session-store', () => ({
updateSession: updateSessionMock,
updateSessionStats: updateSessionStatsMock,
}))
vi.mock('../../packages/server/src/services/logger', () => ({
logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
}))
vi.mock('../../packages/server/src/services/hermes/run-chat/bridge-message', () => ({
flushBridgePendingToDb: flushBridgePendingToDbMock,
}))
vi.mock('../../packages/server/src/services/hermes/run-chat/compression', () => ({
replaceState: replaceStateMock,
}))
vi.mock('../../packages/server/src/services/hermes/run-chat/usage', () => ({
calcAndUpdateUsage: calcAndUpdateUsageMock,
}))
function makeHarness() {
const emit = vi.fn()
const nsp = {
adapter: { rooms: new Map([['session:session-1', new Set(['socket-1'])]]) },
to: vi.fn(() => ({ emit })),
}
const socket = {
connected: true,
emit: vi.fn(),
}
return { emit, nsp, socket }
}
describe('run chat abort goal handling', () => {
beforeEach(() => {
vi.clearAllMocks()
calcAndUpdateUsageMock.mockResolvedValue({ inputTokens: 0, outputTokens: 0 })
})
it('pauses an active goal and clears hidden goal continuations when aborting a CLI run', async () => {
const { handleAbort } = await import('../../packages/server/src/services/hermes/run-chat/abort')
const { emit, nsp, socket } = makeHarness()
const state = {
messages: [],
isWorking: true,
isAborting: false,
events: [],
queue: [
{ queue_id: 'goal-1', input: 'continue goal', profile: 'default', goalContinuation: true },
{ queue_id: 'user-1', input: 'normal follow-up', profile: 'default', source: 'cli' },
],
runId: 'run-1',
profile: 'default',
source: 'cli',
} as any
const sessionMap = new Map([['session-1', state]])
const bridge = {
interrupt: vi.fn().mockResolvedValue({ ok: true }),
goalPause: vi.fn().mockResolvedValue({ handled: true, status: 'paused', reason: 'user-interrupted' }),
}
const runQueuedItem = vi.fn()
await handleAbort(nsp as any, socket as any, 'session-1', sessionMap, bridge, runQueuedItem)
expect(bridge.interrupt).toHaveBeenCalledWith('session-1', 'Aborted by user', 'default')
expect(bridge.goalPause).toHaveBeenCalledWith('session-1', 'user-interrupted', 'default')
expect(runQueuedItem).toHaveBeenCalledWith(socket, 'session-1', expect.objectContaining({
queue_id: 'user-1',
}), 'default')
expect(state.queue).toEqual([])
expect(emit).toHaveBeenCalledWith('abort.completed', expect.objectContaining({
session_id: 'session-1',
synced: true,
}))
expect(updateSessionMock).not.toHaveBeenCalled()
})
it('releases local working state when a CLI interrupt does not sync before timeout', async () => {
const { handleAbort } = await import('../../packages/server/src/services/hermes/run-chat/abort')
const { emit, nsp, socket } = makeHarness()
const state = {
messages: [],
isWorking: true,
isAborting: false,
events: [],
queue: [
{ queue_id: 'goal-1', input: 'continue goal', profile: 'default', goalContinuation: true },
],
runId: 'run-1',
profile: 'default',
source: 'cli',
} as any
const sessionMap = new Map([['session-1', state]])
const bridge = {
interrupt: vi.fn().mockResolvedValue({ ok: true, synced: false }),
goalPause: vi.fn().mockResolvedValue({ handled: true, status: 'paused', reason: 'user-interrupted' }),
destroy: vi.fn().mockResolvedValue({ destroyed: true }),
}
const runQueuedItem = vi.fn()
await handleAbort(nsp as any, socket as any, 'session-1', sessionMap, bridge, runQueuedItem)
expect(runQueuedItem).not.toHaveBeenCalled()
expect(bridge.destroy).toHaveBeenCalledWith('session-1', 'default')
expect(calcAndUpdateUsageMock).toHaveBeenCalled()
expect(state.isWorking).toBe(false)
expect(state.isAborting).toBe(false)
expect(state.runId).toBeUndefined()
expect(state.activeRunMarker).toBeUndefined()
expect(state.queue).toEqual([])
expect(emit).toHaveBeenCalledWith('abort.timeout', expect.objectContaining({
session_id: 'session-1',
run_id: 'run-1',
synced: false,
}))
expect(emit).toHaveBeenCalledWith('abort.completed', expect.objectContaining({
session_id: 'session-1',
run_id: 'run-1',
synced: false,
}))
expect(updateSessionMock).toHaveBeenCalledWith('session-1', expect.objectContaining({
ended_at: expect.any(Number),
end_reason: 'abort',
}))
})
it('aborts a workflow-scoped run through the bridge', async () => {
const { handleAbort } = await import('../../packages/server/src/services/hermes/run-chat/abort')
const { emit, nsp, socket } = makeHarness()
const state = {
messages: [],
isWorking: true,
isAborting: false,
events: [],
queue: [],
runId: 'workflow-run-1',
profile: 'default',
source: 'workflow',
} as any
const sessionMap = new Map([['session-1', state]])
const bridge = {
interrupt: vi.fn().mockResolvedValue({ ok: true, synced: true }),
goalPause: vi.fn().mockResolvedValue({ handled: true }),
}
const runQueuedItem = vi.fn()
await handleAbort(nsp as any, socket as any, 'session-1', sessionMap, bridge, runQueuedItem)
expect(bridge.interrupt).toHaveBeenCalledWith('session-1', 'Aborted by user', 'default')
expect(bridge.goalPause).toHaveBeenCalled()
expect(updateSessionMock).toHaveBeenCalledWith('session-1', expect.objectContaining({
ended_at: expect.any(Number),
end_reason: 'abort',
}))
expect(flushBridgePendingToDbMock).toHaveBeenCalledWith(expect.objectContaining({
source: 'workflow',
}), 'session-1')
expect(state.isWorking).toBe(false)
expect(state.isAborting).toBe(false)
expect(emit).toHaveBeenCalledWith('abort.completed', expect.objectContaining({
session_id: 'session-1',
run_id: 'workflow-run-1',
synced: true,
}))
})
it('aborts a session stored with a retired source through the bridge', async () => {
const { handleAbort } = await import('../../packages/server/src/services/hermes/run-chat/abort')
const { emit, nsp, socket } = makeHarness()
const state = {
messages: [],
isWorking: true,
isAborting: false,
events: [],
queue: [],
runId: 'legacy-run-1',
profile: 'default',
source: 'retired_source',
} as any
const sessionMap = new Map([['session-1', state]])
const bridge = {
interrupt: vi.fn().mockResolvedValue({ ok: true, synced: true }),
goalPause: vi.fn().mockResolvedValue({ handled: true }),
}
const runQueuedItem = vi.fn()
await handleAbort(nsp as any, socket as any, 'session-1', sessionMap, bridge, runQueuedItem)
expect(bridge.interrupt).toHaveBeenCalledWith('session-1', 'Aborted by user', 'default')
expect(updateSessionMock).toHaveBeenCalledWith('session-1', expect.objectContaining({
ended_at: expect.any(Number),
end_reason: 'abort',
}))
expect(emit).toHaveBeenCalledWith('abort.completed', expect.objectContaining({
session_id: 'session-1',
run_id: 'legacy-run-1',
synced: true,
}))
})
})