mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-19 03:40:50 +00:00
* fix(kimi): discover configured models dynamically * fix(kimi): address dynamic model review findings Probe settlement (Major): parse `kimi provider list --json` on the child's 'close' event instead of 'exit', so stdio is fully drained first, and run the parse inside a guarded settle() helper. A parser throw previously escaped the promise from an async event callback and reached the runner's uncaughtException shutdown handler instead of listKimiModelsForCwd's catch. The helper also settles the promise exactly once across timeout, spawn error and close, names the terminating signal when there is no exit code, and tolerates a kill() that throws. Running-session discovery (Minor): add a session-scoped Kimi catalog. The shared RPC method `listKimiModels`, a CLI handler registered on the session connection with the probe cwd taken from the session metadata (never from RPC arguments), hub rpcGateway/syncEngine pass-throughs, and GET /api/sessions/:id/kimi-models gated on an active session of flavor `kimi`. SessionChat now reads the catalog from that endpoint, so an active session advertises its models even when no background runner holds the machine socket. New Session keeps the machine-scoped discovery unchanged, and model switching still goes through the existing ACP set_model path. * fix(kimi): register session model discovery before the local/remote loop The session catalog handler was registered inside the remote launcher, after ACP initialization. A session started with `hapi kimi` in local mode therefore answered the web picker's request with an unregistered-handler error, and the failed query was not retried or invalidated when remote control was taken over. The catalog is backend independent, so it is now registered in runKimi session setup alongside the other session-level handlers, using the session's working directory, and the late registration in the remote launcher is removed. This covers local, remote and resumed sessions on the same session connection. Adds cli/src/kimi/runKimi.test.ts asserting the handler is registered after bootstrap and before the loop, and that the probe cwd is the session's working directory. The launcher wiring assertion moves out of kimiRemoteLauncher.test.ts with the registration.
948 lines
37 KiB
TypeScript
948 lines
37 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { existsSync, mkdtempSync, rmSync, mkdirSync, realpathSync, symlinkSync, writeFileSync } from 'node:fs'
|
|
import { homedir, tmpdir } from 'node:os'
|
|
import { join } from 'node:path'
|
|
|
|
const ioMock = vi.hoisted(() => vi.fn())
|
|
const listOpencodeModelsForCwdMock = vi.hoisted(() => vi.fn())
|
|
const listOpencodeModelVariantsMock = vi.hoisted(() => vi.fn())
|
|
const listGrokModelsForCwdMock = vi.hoisted(() => vi.fn())
|
|
const listCopilotModelsForCwdMock = vi.hoisted(() => vi.fn())
|
|
const listKimiModelsForCwdMock = vi.hoisted(() => vi.fn())
|
|
const inspectCursorChatStoreMock = vi.hoisted(() => vi.fn())
|
|
|
|
vi.mock('socket.io-client', () => ({
|
|
io: ioMock
|
|
}))
|
|
|
|
vi.mock('@/api/auth', () => ({
|
|
getAuthToken: () => 'cli-token'
|
|
}))
|
|
|
|
vi.mock('../modules/common/opencodeModels', () => ({
|
|
listOpencodeModelsForCwd: listOpencodeModelsForCwdMock
|
|
}))
|
|
|
|
vi.mock('../modules/common/opencodeModelVariants', () => ({
|
|
listOpencodeModelVariants: listOpencodeModelVariantsMock
|
|
}))
|
|
|
|
vi.mock('../modules/common/grokModels', () => ({
|
|
listGrokModelsForCwd: listGrokModelsForCwdMock
|
|
}))
|
|
|
|
vi.mock('../modules/common/copilotModels', () => ({
|
|
listCopilotModelsForCwd: listCopilotModelsForCwdMock
|
|
}))
|
|
|
|
vi.mock('../modules/common/kimiModels', () => ({
|
|
listKimiModelsForCwd: listKimiModelsForCwdMock
|
|
}))
|
|
|
|
vi.mock('@/cursor/cursorChatStoreStatus', () => ({
|
|
inspectCursorChatStore: inspectCursorChatStoreMock
|
|
}))
|
|
|
|
import { ApiMachineClient, normalizeWindowsDriveRoot } from './apiMachine'
|
|
import type { Machine } from './types'
|
|
|
|
function makeMachine(id: string): Machine {
|
|
return {
|
|
id,
|
|
namespace: 'default',
|
|
seq: 1,
|
|
createdAt: 0,
|
|
updatedAt: 0,
|
|
active: true,
|
|
activeAt: 0,
|
|
metadata: null,
|
|
metadataVersion: 0,
|
|
runnerState: null,
|
|
runnerStateVersion: 0
|
|
}
|
|
}
|
|
|
|
describe('normalizeWindowsDriveRoot', () => {
|
|
it('restores the trailing separator when Windows realpath returns a bare drive', () => {
|
|
expect(normalizeWindowsDriveRoot('C:')).toBe('C:\\')
|
|
expect(normalizeWindowsDriveRoot('D:')).toBe('D:\\')
|
|
})
|
|
|
|
it('leaves non-drive-root paths unchanged', () => {
|
|
expect(normalizeWindowsDriveRoot('C:\\Users')).toBe('C:\\Users')
|
|
expect(normalizeWindowsDriveRoot('/tmp/workspace')).toBe('/tmp/workspace')
|
|
})
|
|
})
|
|
|
|
async function callListOpencodeModels(client: ApiMachineClient, machineId: string, cwd: string): Promise<unknown> {
|
|
// Reach into the private rpc handler manager to dispatch a request.
|
|
// Mirrors how the on-socket 'rpc-request' listener invokes handleRequest.
|
|
const manager = (client as unknown as { rpcHandlerManager: { handleRequest: (req: { method: string; params: string }) => Promise<string> } }).rpcHandlerManager
|
|
const raw = await manager.handleRequest({
|
|
method: `${machineId}:listOpencodeModelsForCwd`,
|
|
params: JSON.stringify({ cwd })
|
|
})
|
|
return JSON.parse(raw) as unknown
|
|
}
|
|
|
|
async function callListOpencodeModelVariants(client: ApiMachineClient, machineId: string, cwd: string): Promise<unknown> {
|
|
const manager = (client as unknown as { rpcHandlerManager: { handleRequest: (req: { method: string; params: string }) => Promise<string> } }).rpcHandlerManager
|
|
const raw = await manager.handleRequest({
|
|
method: `${machineId}:listOpencodeModelVariants`,
|
|
params: JSON.stringify({ cwd })
|
|
})
|
|
return JSON.parse(raw) as unknown
|
|
}
|
|
|
|
async function callListGrokModels(client: ApiMachineClient, machineId: string, cwd: string): Promise<unknown> {
|
|
const manager = (client as unknown as { rpcHandlerManager: { handleRequest: (req: { method: string; params: string }) => Promise<string> } }).rpcHandlerManager
|
|
const raw = await manager.handleRequest({
|
|
method: `${machineId}:listGrokModelsForCwd`,
|
|
params: JSON.stringify({ cwd })
|
|
})
|
|
return JSON.parse(raw) as unknown
|
|
}
|
|
|
|
async function callListCopilotModels(client: ApiMachineClient, machineId: string, cwd: string): Promise<unknown> {
|
|
const manager = (client as unknown as { rpcHandlerManager: { handleRequest: (req: { method: string; params: string }) => Promise<string> } }).rpcHandlerManager
|
|
const raw = await manager.handleRequest({
|
|
method: `${machineId}:listCopilotModelsForCwd`,
|
|
params: JSON.stringify({ cwd })
|
|
})
|
|
return JSON.parse(raw) as unknown
|
|
}
|
|
|
|
async function callListKimiModels(client: ApiMachineClient, machineId: string, cwd: string): Promise<unknown> {
|
|
const manager = (client as unknown as { rpcHandlerManager: { handleRequest: (req: { method: string; params: string }) => Promise<string> } }).rpcHandlerManager
|
|
const raw = await manager.handleRequest({
|
|
method: `${machineId}:listKimiModelsForCwd`,
|
|
params: JSON.stringify({ cwd })
|
|
})
|
|
return JSON.parse(raw) as unknown
|
|
}
|
|
|
|
async function callCursorChatStoreStatus(
|
|
client: ApiMachineClient,
|
|
machineId: string,
|
|
params: { workspacePath: string; cursorSessionId: string; homeDir?: string }
|
|
): Promise<unknown> {
|
|
const manager = (client as unknown as { rpcHandlerManager: { handleRequest: (req: { method: string; params: string }) => Promise<string> } }).rpcHandlerManager
|
|
const raw = await manager.handleRequest({
|
|
method: `${machineId}:cursor-chat-store-status`,
|
|
params: JSON.stringify(params)
|
|
})
|
|
return JSON.parse(raw) as unknown
|
|
}
|
|
|
|
async function callListCodexSessions(client: ApiMachineClient, machineId: string, params: { cwd?: string | null; sessionIds?: string[] }): Promise<unknown> {
|
|
const manager = (client as unknown as { rpcHandlerManager: { handleRequest: (req: { method: string; params: string }) => Promise<string> } }).rpcHandlerManager
|
|
const raw = await manager.handleRequest({
|
|
method: `${machineId}:listCodexSessions`,
|
|
params: JSON.stringify(params)
|
|
})
|
|
return JSON.parse(raw) as unknown
|
|
}
|
|
|
|
async function callListPiSessions(client: ApiMachineClient, machineId: string, params: { cwd?: string | null; sessionIds?: string[] }): Promise<unknown> {
|
|
const manager = (client as unknown as { rpcHandlerManager: { handleRequest: (req: { method: string; params: string }) => Promise<string> } }).rpcHandlerManager
|
|
const raw = await manager.handleRequest({
|
|
method: `${machineId}:listPiSessions`,
|
|
params: JSON.stringify(params)
|
|
})
|
|
return JSON.parse(raw) as unknown
|
|
}
|
|
|
|
async function callArchiveCodexSession(client: ApiMachineClient, machineId: string, sessionId: string): Promise<unknown> {
|
|
const manager = (client as unknown as { rpcHandlerManager: { handleRequest: (req: { method: string; params: string }) => Promise<string> } }).rpcHandlerManager
|
|
const raw = await manager.handleRequest({
|
|
method: `${machineId}:archiveCodexSession`,
|
|
params: JSON.stringify({ sessionId })
|
|
})
|
|
return JSON.parse(raw) as unknown
|
|
}
|
|
|
|
function writeCodexTranscript(codexHome: string, fileName: string, payload: Record<string, unknown>, userText: string): string {
|
|
const sessionDir = join(codexHome, 'sessions', '2026', '06', '29')
|
|
mkdirSync(sessionDir, { recursive: true })
|
|
const file = join(sessionDir, fileName)
|
|
writeFileSync(file, [
|
|
JSON.stringify({ type: 'session_meta', payload }),
|
|
JSON.stringify({ type: 'response_item', payload: { type: 'message', role: 'user', content: [{ type: 'input_text', text: userText }] } })
|
|
].join('\n'))
|
|
return file
|
|
}
|
|
|
|
function writePiTranscript(sessionsRoot: string, fileName: string, sessionId: string, cwd: string, userText: string): string {
|
|
const sessionDir = join(sessionsRoot, '--project--')
|
|
mkdirSync(sessionDir, { recursive: true })
|
|
const file = join(sessionDir, fileName)
|
|
writeFileSync(file, [
|
|
JSON.stringify({ type: 'session', version: 3, id: sessionId, cwd }),
|
|
JSON.stringify({ type: 'message', id: `${sessionId}-user`, parentId: null, message: { role: 'user', content: userText } })
|
|
].join('\n'))
|
|
return file
|
|
}
|
|
|
|
describe('ApiMachineClient cursor-chat-store-status handler', () => {
|
|
beforeEach(() => {
|
|
inspectCursorChatStoreMock.mockReset()
|
|
inspectCursorChatStoreMock.mockResolvedValue({ onDisk: false, store: null })
|
|
})
|
|
|
|
it('inspects stores under the recorded session owner home', async () => {
|
|
const machine = makeMachine('cursor-store-machine')
|
|
const client = new ApiMachineClient('cli-token', machine)
|
|
|
|
try {
|
|
await callCursorChatStoreStatus(client, machine.id, {
|
|
workspacePath: '/work/project',
|
|
cursorSessionId: 'cursor-session',
|
|
homeDir: ' /home/recorded-owner '
|
|
})
|
|
|
|
expect(inspectCursorChatStoreMock).toHaveBeenCalledWith({
|
|
home: '/home/recorded-owner',
|
|
workspacePath: '/work/project',
|
|
cursorSessionId: 'cursor-session'
|
|
})
|
|
} finally {
|
|
client.shutdown()
|
|
}
|
|
})
|
|
|
|
it('falls back to the CLI process home for old or whitespace-only homeDir metadata', async () => {
|
|
const machine = makeMachine('cursor-store-fallback-machine')
|
|
const client = new ApiMachineClient('cli-token', machine)
|
|
|
|
try {
|
|
await callCursorChatStoreStatus(client, machine.id, {
|
|
workspacePath: '/work/project',
|
|
cursorSessionId: 'cursor-session-old'
|
|
})
|
|
await callCursorChatStoreStatus(client, machine.id, {
|
|
workspacePath: '/work/project',
|
|
cursorSessionId: 'cursor-session-empty',
|
|
homeDir: ' '
|
|
})
|
|
|
|
expect(inspectCursorChatStoreMock).toHaveBeenNthCalledWith(1, {
|
|
home: homedir(),
|
|
workspacePath: '/work/project',
|
|
cursorSessionId: 'cursor-session-old'
|
|
})
|
|
expect(inspectCursorChatStoreMock).toHaveBeenNthCalledWith(2, {
|
|
home: homedir(),
|
|
workspacePath: '/work/project',
|
|
cursorSessionId: 'cursor-session-empty'
|
|
})
|
|
} finally {
|
|
client.shutdown()
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('ApiMachineClient listOpencodeModelsForCwd handler', () => {
|
|
let workspaceRoot: string
|
|
|
|
beforeEach(() => {
|
|
ioMock.mockReset()
|
|
listOpencodeModelsForCwdMock.mockReset()
|
|
listGrokModelsForCwdMock.mockReset()
|
|
workspaceRoot = mkdtempSync(join(tmpdir(), 'hapi-machine-ws-'))
|
|
})
|
|
|
|
afterEach(() => {
|
|
rmSync(workspaceRoot, { recursive: true, force: true })
|
|
})
|
|
|
|
it('rejects cwd outside the workspace root with the standard error shape', async () => {
|
|
const machine = makeMachine('machine-1')
|
|
const client = new ApiMachineClient('cli-token', machine, [workspaceRoot])
|
|
|
|
const outsideCwd = mkdtempSync(join(tmpdir(), 'hapi-outside-'))
|
|
try {
|
|
const result = await callListOpencodeModels(client, machine.id, outsideCwd)
|
|
expect(result).toEqual({ success: false, error: 'Path is outside workspace roots' })
|
|
expect(listOpencodeModelsForCwdMock).not.toHaveBeenCalled()
|
|
} finally {
|
|
rmSync(outsideCwd, { recursive: true, force: true })
|
|
client.shutdown()
|
|
}
|
|
})
|
|
|
|
it('rejects empty cwd with cwd-required error', async () => {
|
|
const machine = makeMachine('machine-2')
|
|
const client = new ApiMachineClient('cli-token', machine, [workspaceRoot])
|
|
|
|
try {
|
|
const result = await callListOpencodeModels(client, machine.id, '')
|
|
expect(result).toEqual({ success: false, error: 'cwd is required' })
|
|
expect(listOpencodeModelsForCwdMock).not.toHaveBeenCalled()
|
|
} finally {
|
|
client.shutdown()
|
|
}
|
|
})
|
|
|
|
it('forwards a workspace-internal cwd to listOpencodeModelsForCwd', async () => {
|
|
const machine = makeMachine('machine-3')
|
|
const client = new ApiMachineClient('cli-token', machine, [workspaceRoot])
|
|
|
|
const innerDir = join(workspaceRoot, 'inner-project')
|
|
mkdirSync(innerDir)
|
|
|
|
listOpencodeModelsForCwdMock.mockResolvedValueOnce({
|
|
success: true,
|
|
availableModels: [{ modelId: 'a/b' }],
|
|
currentModelId: 'a/b'
|
|
})
|
|
|
|
try {
|
|
const result = await callListOpencodeModels(client, machine.id, innerDir)
|
|
expect(result).toEqual({
|
|
success: true,
|
|
availableModels: [{ modelId: 'a/b' }],
|
|
currentModelId: 'a/b'
|
|
})
|
|
expect(listOpencodeModelsForCwdMock).toHaveBeenCalledTimes(1)
|
|
// The handler should pass the resolved (realpath'd) cwd to the lower layer.
|
|
expect(listOpencodeModelsForCwdMock).toHaveBeenCalledWith(expect.stringContaining('inner-project'))
|
|
} finally {
|
|
client.shutdown()
|
|
}
|
|
})
|
|
|
|
it('accepts cwd inside any configured workspace root', async () => {
|
|
const machine = makeMachine('machine-4')
|
|
const secondWorkspaceRoot = mkdtempSync(join(tmpdir(), 'hapi-machine-ws-2-'))
|
|
const client = new ApiMachineClient('cli-token', machine, [workspaceRoot, secondWorkspaceRoot])
|
|
|
|
listOpencodeModelsForCwdMock.mockResolvedValueOnce({
|
|
success: true,
|
|
availableModels: [{ modelId: 'x/y' }],
|
|
currentModelId: 'x/y'
|
|
})
|
|
|
|
try {
|
|
const result = await callListOpencodeModels(client, machine.id, secondWorkspaceRoot)
|
|
expect(result).toEqual({
|
|
success: true,
|
|
availableModels: [{ modelId: 'x/y' }],
|
|
currentModelId: 'x/y'
|
|
})
|
|
// The handler realpaths the cwd (security: prevents symlink escape),
|
|
// so on macOS /var/folders/... resolves to /private/var/folders/...
|
|
expect(listOpencodeModelsForCwdMock).toHaveBeenCalledWith(realpathSync.native(secondWorkspaceRoot))
|
|
} finally {
|
|
rmSync(secondWorkspaceRoot, { recursive: true, force: true })
|
|
client.shutdown()
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('ApiMachineClient listOpencodeModelVariants handler', () => {
|
|
let workspaceRoot: string
|
|
|
|
beforeEach(() => {
|
|
ioMock.mockReset()
|
|
listOpencodeModelVariantsMock.mockReset()
|
|
workspaceRoot = mkdtempSync(join(tmpdir(), 'hapi-opencode-variants-ws-'))
|
|
})
|
|
|
|
afterEach(() => {
|
|
rmSync(workspaceRoot, { recursive: true, force: true })
|
|
})
|
|
|
|
it('rejects cwd outside workspace roots before running the variant probe', async () => {
|
|
const machine = makeMachine('opencode-variants-machine-1')
|
|
const client = new ApiMachineClient('cli-token', machine, [workspaceRoot])
|
|
const outsideCwd = mkdtempSync(join(tmpdir(), 'hapi-opencode-variants-outside-'))
|
|
|
|
try {
|
|
expect(await callListOpencodeModelVariants(client, machine.id, outsideCwd)).toEqual({
|
|
success: false,
|
|
error: 'Path is outside workspace roots'
|
|
})
|
|
expect(listOpencodeModelVariantsMock).not.toHaveBeenCalled()
|
|
} finally {
|
|
rmSync(outsideCwd, { recursive: true, force: true })
|
|
client.shutdown()
|
|
}
|
|
})
|
|
|
|
it('rejects a workspace symlink that escapes the configured roots', async () => {
|
|
const machine = makeMachine('opencode-variants-machine-2')
|
|
const client = new ApiMachineClient('cli-token', machine, [workspaceRoot])
|
|
const outsideCwd = mkdtempSync(join(tmpdir(), 'hapi-opencode-variants-symlink-outside-'))
|
|
const escape = join(workspaceRoot, 'escape')
|
|
symlinkSync(outsideCwd, escape, 'dir')
|
|
|
|
try {
|
|
expect(await callListOpencodeModelVariants(client, machine.id, escape)).toEqual({
|
|
success: false,
|
|
error: 'Path is outside workspace roots'
|
|
})
|
|
expect(listOpencodeModelVariantsMock).not.toHaveBeenCalled()
|
|
} finally {
|
|
rmSync(outsideCwd, { recursive: true, force: true })
|
|
client.shutdown()
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('ApiMachineClient listCopilotModelsForCwd handler', () => {
|
|
let workspaceRoot: string
|
|
|
|
beforeEach(() => {
|
|
ioMock.mockReset()
|
|
listCopilotModelsForCwdMock.mockReset()
|
|
workspaceRoot = mkdtempSync(join(tmpdir(), 'hapi-copilot-machine-ws-'))
|
|
})
|
|
|
|
afterEach(() => {
|
|
rmSync(workspaceRoot, { recursive: true, force: true })
|
|
})
|
|
|
|
it('rejects cwd outside workspace roots before running the Copilot model probe', async () => {
|
|
const machine = makeMachine('copilot-machine-1')
|
|
const client = new ApiMachineClient('cli-token', machine, [workspaceRoot])
|
|
const outsideCwd = mkdtempSync(join(tmpdir(), 'hapi-copilot-outside-'))
|
|
|
|
try {
|
|
expect(await callListCopilotModels(client, machine.id, outsideCwd)).toEqual({
|
|
success: false,
|
|
error: 'Path is outside workspace roots'
|
|
})
|
|
expect(listCopilotModelsForCwdMock).not.toHaveBeenCalled()
|
|
} finally {
|
|
rmSync(outsideCwd, { recursive: true, force: true })
|
|
client.shutdown()
|
|
}
|
|
})
|
|
|
|
it('forwards a resolved workspace cwd to the Copilot model probe', async () => {
|
|
const machine = makeMachine('copilot-machine-2')
|
|
const client = new ApiMachineClient('cli-token', machine, [workspaceRoot])
|
|
listCopilotModelsForCwdMock.mockResolvedValueOnce({
|
|
success: true,
|
|
availableModels: [{ modelId: 'gpt-5.6' }],
|
|
currentModelId: 'gpt-5.6'
|
|
})
|
|
|
|
try {
|
|
expect(await callListCopilotModels(client, machine.id, workspaceRoot)).toEqual({
|
|
success: true,
|
|
availableModels: [{ modelId: 'gpt-5.6' }],
|
|
currentModelId: 'gpt-5.6'
|
|
})
|
|
expect(listCopilotModelsForCwdMock).toHaveBeenCalledWith(realpathSync.native(workspaceRoot))
|
|
} finally {
|
|
client.shutdown()
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('ApiMachineClient listGrokModelsForCwd handler', () => {
|
|
let workspaceRoot: string
|
|
|
|
beforeEach(() => {
|
|
ioMock.mockReset()
|
|
listGrokModelsForCwdMock.mockReset()
|
|
workspaceRoot = mkdtempSync(join(tmpdir(), 'hapi-grok-machine-ws-'))
|
|
})
|
|
|
|
afterEach(() => {
|
|
rmSync(workspaceRoot, { recursive: true, force: true })
|
|
})
|
|
|
|
it('rejects cwd outside workspace roots before running grok models', async () => {
|
|
const machine = makeMachine('grok-machine-1')
|
|
const client = new ApiMachineClient('cli-token', machine, [workspaceRoot])
|
|
const outsideCwd = mkdtempSync(join(tmpdir(), 'hapi-grok-outside-'))
|
|
|
|
try {
|
|
expect(await callListGrokModels(client, machine.id, outsideCwd)).toEqual({
|
|
success: false,
|
|
error: 'Path is outside workspace roots'
|
|
})
|
|
expect(listGrokModelsForCwdMock).not.toHaveBeenCalled()
|
|
} finally {
|
|
rmSync(outsideCwd, { recursive: true, force: true })
|
|
client.shutdown()
|
|
}
|
|
})
|
|
|
|
it('forwards a workspace cwd to the Grok model probe', async () => {
|
|
const machine = makeMachine('grok-machine-2')
|
|
const client = new ApiMachineClient('cli-token', machine, [workspaceRoot])
|
|
listGrokModelsForCwdMock.mockResolvedValueOnce({
|
|
success: true,
|
|
availableModels: [{ modelId: 'grok-4.5' }],
|
|
currentModelId: 'grok-4.5'
|
|
})
|
|
|
|
try {
|
|
expect(await callListGrokModels(client, machine.id, workspaceRoot)).toEqual({
|
|
success: true,
|
|
availableModels: [{ modelId: 'grok-4.5' }],
|
|
currentModelId: 'grok-4.5'
|
|
})
|
|
expect(listGrokModelsForCwdMock).toHaveBeenCalledWith(realpathSync.native(workspaceRoot))
|
|
} finally {
|
|
client.shutdown()
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('ApiMachineClient listKimiModelsForCwd handler', () => {
|
|
let workspaceRoot: string
|
|
|
|
beforeEach(() => {
|
|
ioMock.mockReset()
|
|
listKimiModelsForCwdMock.mockReset()
|
|
workspaceRoot = mkdtempSync(join(tmpdir(), 'hapi-kimi-machine-ws-'))
|
|
})
|
|
|
|
afterEach(() => {
|
|
rmSync(workspaceRoot, { recursive: true, force: true })
|
|
})
|
|
|
|
it('rejects cwd outside workspace roots before running the Kimi model probe', async () => {
|
|
const machine = makeMachine('kimi-machine-1')
|
|
const client = new ApiMachineClient('cli-token', machine, [workspaceRoot])
|
|
const outsideCwd = mkdtempSync(join(tmpdir(), 'hapi-kimi-outside-'))
|
|
|
|
try {
|
|
expect(await callListKimiModels(client, machine.id, outsideCwd)).toEqual({
|
|
success: false,
|
|
error: 'Path is outside workspace roots'
|
|
})
|
|
expect(listKimiModelsForCwdMock).not.toHaveBeenCalled()
|
|
} finally {
|
|
rmSync(outsideCwd, { recursive: true, force: true })
|
|
client.shutdown()
|
|
}
|
|
})
|
|
|
|
it('forwards a resolved workspace cwd to the Kimi model probe', async () => {
|
|
const machine = makeMachine('kimi-machine-2')
|
|
const client = new ApiMachineClient('cli-token', machine, [workspaceRoot])
|
|
listKimiModelsForCwdMock.mockResolvedValueOnce({
|
|
success: true,
|
|
availableModels: [
|
|
{ modelId: 'GLM-5.3-flash', name: 'thehive / GLM-5.3-flash', provider: 'thehive' },
|
|
{ modelId: 'hyper-glm-5.3-flash', name: 'charm-hyper / Hyper · GLM-5.3-Flash', provider: 'charm-hyper' }
|
|
],
|
|
currentModelId: 'GLM-5.3-flash'
|
|
})
|
|
|
|
try {
|
|
expect(await callListKimiModels(client, machine.id, workspaceRoot)).toEqual({
|
|
success: true,
|
|
availableModels: [
|
|
{ modelId: 'GLM-5.3-flash', name: 'thehive / GLM-5.3-flash', provider: 'thehive' },
|
|
{ modelId: 'hyper-glm-5.3-flash', name: 'charm-hyper / Hyper · GLM-5.3-Flash', provider: 'charm-hyper' }
|
|
],
|
|
currentModelId: 'GLM-5.3-flash'
|
|
})
|
|
expect(listKimiModelsForCwdMock).toHaveBeenCalledWith(realpathSync.native(workspaceRoot))
|
|
} finally {
|
|
client.shutdown()
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('ApiMachineClient Codex transcript handlers', () => {
|
|
const originalCodexHome = process.env.CODEX_HOME
|
|
let workspaceRoot: string
|
|
let outsideRoot: string
|
|
let codexHome: string
|
|
|
|
beforeEach(() => {
|
|
ioMock.mockReset()
|
|
listOpencodeModelsForCwdMock.mockReset()
|
|
workspaceRoot = mkdtempSync(join(tmpdir(), 'hapi-codex-allowed-'))
|
|
outsideRoot = mkdtempSync(join(tmpdir(), 'hapi-codex-outside-'))
|
|
codexHome = mkdtempSync(join(tmpdir(), 'hapi-codex-home-'))
|
|
process.env.CODEX_HOME = codexHome
|
|
})
|
|
|
|
afterEach(() => {
|
|
if (originalCodexHome === undefined) delete process.env.CODEX_HOME
|
|
else process.env.CODEX_HOME = originalCodexHome
|
|
rmSync(workspaceRoot, { recursive: true, force: true })
|
|
rmSync(outsideRoot, { recursive: true, force: true })
|
|
rmSync(codexHome, { recursive: true, force: true })
|
|
})
|
|
|
|
it('filters listed Codex sessions to workspace roots', async () => {
|
|
writeCodexTranscript(codexHome, 'allowed.jsonl', {
|
|
id: 'allowed-session-id',
|
|
cwd: workspaceRoot
|
|
}, 'allowed prompt')
|
|
writeCodexTranscript(codexHome, 'outside.jsonl', {
|
|
id: 'outside-session-id',
|
|
cwd: outsideRoot
|
|
}, 'outside prompt')
|
|
|
|
const machine = makeMachine('codex-machine-1')
|
|
const client = new ApiMachineClient('cli-token', machine, [workspaceRoot])
|
|
|
|
try {
|
|
const result = await callListCodexSessions(client, machine.id, {})
|
|
|
|
expect(result).toMatchObject({ success: true })
|
|
const sessions = (result as { sessions: Array<{ id: string }> }).sessions
|
|
expect(sessions.map((session) => session.id)).toEqual(['allowed-session-id'])
|
|
} finally {
|
|
client.shutdown()
|
|
}
|
|
})
|
|
|
|
it('filters import-by-sessionId Codex sessions to workspace roots before returning message bodies', async () => {
|
|
writeCodexTranscript(codexHome, 'allowed.jsonl', {
|
|
id: 'allowed-session-id',
|
|
cwd: workspaceRoot
|
|
}, 'allowed prompt')
|
|
writeCodexTranscript(codexHome, 'outside.jsonl', {
|
|
id: 'outside-session-id',
|
|
cwd: outsideRoot
|
|
}, 'outside prompt')
|
|
|
|
const machine = makeMachine('codex-machine-2')
|
|
const client = new ApiMachineClient('cli-token', machine, [workspaceRoot])
|
|
|
|
try {
|
|
const result = await callListCodexSessions(client, machine.id, {
|
|
sessionIds: ['allowed-session-id', 'outside-session-id']
|
|
})
|
|
|
|
expect(result).toMatchObject({ success: true })
|
|
const sessions = (result as { sessions: Array<{ id: string; messages?: unknown[] }> }).sessions
|
|
expect(sessions.map((session) => session.id)).toEqual(['allowed-session-id'])
|
|
expect(sessions[0]?.messages).toHaveLength(1)
|
|
} finally {
|
|
client.shutdown()
|
|
}
|
|
})
|
|
|
|
it('rejects archive for Codex sessions outside workspace roots', async () => {
|
|
const outsideFile = writeCodexTranscript(codexHome, 'outside.jsonl', {
|
|
id: 'outside-session-id',
|
|
cwd: outsideRoot
|
|
}, 'outside prompt')
|
|
|
|
const machine = makeMachine('codex-machine-3')
|
|
const client = new ApiMachineClient('cli-token', machine, [workspaceRoot])
|
|
|
|
try {
|
|
const result = await callArchiveCodexSession(client, machine.id, 'outside-session-id')
|
|
|
|
expect(result).toEqual({ success: false, error: 'Codex session is outside workspace roots' })
|
|
expect(existsSync(outsideFile)).toBe(true)
|
|
} finally {
|
|
client.shutdown()
|
|
}
|
|
})
|
|
|
|
})
|
|
|
|
describe('ApiMachineClient Pi transcript handlers', () => {
|
|
const originalPiSessions = process.env.PI_CODING_AGENT_SESSION_DIR
|
|
let workspaceRoot: string
|
|
let outsideRoot: string
|
|
let piSessions: string
|
|
|
|
beforeEach(() => {
|
|
ioMock.mockReset()
|
|
workspaceRoot = mkdtempSync(join(tmpdir(), 'hapi-pi-allowed-'))
|
|
outsideRoot = mkdtempSync(join(tmpdir(), 'hapi-pi-outside-'))
|
|
piSessions = mkdtempSync(join(tmpdir(), 'hapi-pi-sessions-'))
|
|
process.env.PI_CODING_AGENT_SESSION_DIR = piSessions
|
|
})
|
|
|
|
afterEach(() => {
|
|
if (originalPiSessions === undefined) delete process.env.PI_CODING_AGENT_SESSION_DIR
|
|
else process.env.PI_CODING_AGENT_SESSION_DIR = originalPiSessions
|
|
rmSync(workspaceRoot, { recursive: true, force: true })
|
|
rmSync(outsideRoot, { recursive: true, force: true })
|
|
rmSync(piSessions, { recursive: true, force: true })
|
|
})
|
|
|
|
it('filters Pi summaries and full transcripts to workspace roots', async () => {
|
|
writePiTranscript(piSessions, 'allowed.jsonl', 'allowed-pi-session', workspaceRoot, 'allowed')
|
|
writePiTranscript(piSessions, 'outside.jsonl', 'outside-pi-session', outsideRoot, 'outside')
|
|
const machine = makeMachine('pi-machine-1')
|
|
const client = new ApiMachineClient('cli-token', machine, [workspaceRoot])
|
|
|
|
try {
|
|
const summaries = await callListPiSessions(client, machine.id, {}) as { success: true; sessions: Array<{ id: string }> }
|
|
expect(summaries.sessions.map((session) => session.id)).toEqual(['allowed-pi-session'])
|
|
|
|
const full = await callListPiSessions(client, machine.id, {
|
|
sessionIds: ['allowed-pi-session', 'outside-pi-session']
|
|
}) as { success: true; sessions: Array<{ id: string; messages: unknown[] }> }
|
|
expect(full.sessions.map((session) => session.id)).toEqual(['allowed-pi-session'])
|
|
expect(full.sessions[0]?.messages).toHaveLength(1)
|
|
} finally {
|
|
client.shutdown()
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('ApiMachineClient SpawnHappySession handler', () => {
|
|
let workspaceRoot: string
|
|
|
|
beforeEach(() => {
|
|
ioMock.mockReset()
|
|
workspaceRoot = mkdtempSync(join(tmpdir(), 'hapi-machine-spawn-'))
|
|
})
|
|
|
|
afterEach(() => {
|
|
rmSync(workspaceRoot, { recursive: true, force: true })
|
|
})
|
|
|
|
async function callSpawnHappySession(
|
|
client: ApiMachineClient,
|
|
machineId: string,
|
|
params: Record<string, unknown>
|
|
): Promise<unknown> {
|
|
const manager = (client as unknown as {
|
|
rpcHandlerManager: { handleRequest: (req: { method: string; params: string }) => Promise<string> }
|
|
}).rpcHandlerManager
|
|
const raw = await manager.handleRequest({
|
|
method: `${machineId}:spawn-happy-session`,
|
|
params: JSON.stringify(params)
|
|
})
|
|
return JSON.parse(raw) as unknown
|
|
}
|
|
|
|
it('forwards collaborationMode and serviceTier to spawnSession', async () => {
|
|
const machine = makeMachine('machine-spawn-1')
|
|
const client = new ApiMachineClient('cli-token', machine, [workspaceRoot])
|
|
const spawnSession = vi.fn(async () => ({ type: 'success' as const, sessionId: 'session-1' }))
|
|
|
|
client.setRPCHandlers({
|
|
spawnSession,
|
|
stopSession: vi.fn(async () => 'stopped' as const),
|
|
requestShutdown: vi.fn()
|
|
})
|
|
|
|
try {
|
|
const result = await callSpawnHappySession(client, machine.id, {
|
|
directory: workspaceRoot,
|
|
agent: 'codex',
|
|
serviceTier: 'fast',
|
|
collaborationMode: 'plan'
|
|
})
|
|
|
|
expect(result).toEqual({ type: 'success', sessionId: 'session-1' })
|
|
expect(spawnSession).toHaveBeenCalledWith(expect.objectContaining({
|
|
directory: workspaceRoot,
|
|
agent: 'codex',
|
|
serviceTier: 'fast',
|
|
collaborationMode: 'plan'
|
|
}))
|
|
} finally {
|
|
client.shutdown()
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('ApiMachineClient keepAlive lifecycle', () => {
|
|
beforeEach(() => {
|
|
vi.useFakeTimers()
|
|
})
|
|
|
|
afterEach(() => {
|
|
vi.useRealTimers()
|
|
})
|
|
|
|
it('clears priming timeout on shutdown before first machine-alive emit', () => {
|
|
const machine = makeMachine('machine-keepalive')
|
|
const client = new ApiMachineClient('cli-token', machine)
|
|
const emit = vi.fn()
|
|
;(client as unknown as { socket: { emit: typeof emit; close: () => void } }).socket = {
|
|
emit,
|
|
close: vi.fn(),
|
|
} as never
|
|
|
|
const priv = client as unknown as {
|
|
startKeepAlive: () => void
|
|
keepAliveInterval: NodeJS.Timeout | null
|
|
keepAliveStartTimeout: ReturnType<typeof setTimeout> | null
|
|
}
|
|
|
|
priv.startKeepAlive()
|
|
client.shutdown()
|
|
vi.advanceTimersByTime(100)
|
|
|
|
expect(emit).not.toHaveBeenCalled()
|
|
expect(priv.keepAliveInterval).toBeNull()
|
|
expect(priv.keepAliveStartTimeout).toBeNull()
|
|
})
|
|
|
|
it('clears running keepAlive interval on shutdown', () => {
|
|
const machine = makeMachine('machine-keepalive-2')
|
|
const client = new ApiMachineClient('cli-token', machine)
|
|
const emit = vi.fn()
|
|
;(client as unknown as { socket: { emit: typeof emit; close: () => void } }).socket = {
|
|
emit,
|
|
close: vi.fn(),
|
|
} as never
|
|
|
|
const priv = client as unknown as {
|
|
startKeepAlive: () => void
|
|
keepAliveInterval: NodeJS.Timeout | null
|
|
}
|
|
|
|
priv.startKeepAlive()
|
|
vi.advanceTimersByTime(50)
|
|
expect(emit).toHaveBeenCalledTimes(1)
|
|
|
|
client.shutdown()
|
|
vi.advanceTimersByTime(20_000)
|
|
|
|
expect(emit).toHaveBeenCalledTimes(1)
|
|
expect(priv.keepAliveInterval).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('ApiMachineClient list-directory handler', () => {
|
|
let workspaceRoot: string
|
|
|
|
beforeEach(() => {
|
|
ioMock.mockReset()
|
|
workspaceRoot = mkdtempSync(join(tmpdir(), 'hapi-machine-ls-'))
|
|
mkdirSync(join(workspaceRoot, 'visible-dir'))
|
|
mkdirSync(join(workspaceRoot, '.hidden-dir'))
|
|
writeFileSync(join(workspaceRoot, 'plain.txt'), 'x')
|
|
writeFileSync(join(workspaceRoot, '.hidden-file'), 'x')
|
|
})
|
|
|
|
afterEach(() => {
|
|
rmSync(workspaceRoot, { recursive: true, force: true })
|
|
})
|
|
|
|
async function callListDirectory(client: ApiMachineClient, machineId: string, params: { path: string; includeHidden?: boolean }): Promise<unknown> {
|
|
const manager = (client as unknown as { rpcHandlerManager: { handleRequest: (req: { method: string; params: string }) => Promise<string> } }).rpcHandlerManager
|
|
const raw = await manager.handleRequest({
|
|
method: `${machineId}:list-directory`,
|
|
params: JSON.stringify(params)
|
|
})
|
|
return JSON.parse(raw) as unknown
|
|
}
|
|
|
|
function entryNames(result: unknown): string[] {
|
|
const entries = (result as { success: boolean; entries?: { name: string }[] }).entries ?? []
|
|
return entries.map((entry) => entry.name).sort()
|
|
}
|
|
|
|
it('browses outside the home directory when no workspace roots are configured', async () => {
|
|
const machine = makeMachine('machine-ls-unrestricted')
|
|
const client = new ApiMachineClient('cli-token', machine)
|
|
try {
|
|
const result = await callListDirectory(client, machine.id, { path: workspaceRoot })
|
|
expect((result as { success: boolean }).success).toBe(true)
|
|
expect(entryNames(result)).toEqual(['plain.txt', 'visible-dir'])
|
|
} finally {
|
|
client.shutdown()
|
|
}
|
|
})
|
|
|
|
it('lists in-root directory links and rejects links escaping explicit workspace roots', async () => {
|
|
const outside = mkdtempSync(join(tmpdir(), 'hapi-machine-ls-outside-'))
|
|
mkdirSync(join(workspaceRoot, 'visible-dir', '.git'))
|
|
symlinkSync(join(workspaceRoot, 'visible-dir'), join(workspaceRoot, 'linked-repo'), 'dir')
|
|
symlinkSync(outside, join(workspaceRoot, 'escape'), 'dir')
|
|
const machine = makeMachine('machine-ls-links')
|
|
const client = new ApiMachineClient('cli-token', machine, [workspaceRoot])
|
|
try {
|
|
const result = await callListDirectory(client, machine.id, { path: workspaceRoot })
|
|
expect(result).toMatchObject({
|
|
success: true,
|
|
entries: expect.arrayContaining([
|
|
expect.objectContaining({ name: 'linked-repo', type: 'directory', isGitRepo: true }),
|
|
]),
|
|
})
|
|
expect(entryNames(result)).not.toContain('escape')
|
|
expect(await callListDirectory(client, machine.id, { path: join(workspaceRoot, 'escape') }))
|
|
.toEqual({ success: false, error: 'Path is outside workspace roots' })
|
|
expect(await callListDirectory(client, machine.id, { path: join(workspaceRoot, '..') }))
|
|
.toEqual({ success: false, error: 'Path is outside workspace roots' })
|
|
expect(await callListDirectory(client, machine.id, { path: join(workspaceRoot, 'linked-repo') }))
|
|
.toMatchObject({ success: true })
|
|
} finally {
|
|
client.shutdown()
|
|
rmSync(outside, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('filters dot-prefixed entries by default', async () => {
|
|
const machine = makeMachine('machine-ls-1')
|
|
const client = new ApiMachineClient('cli-token', machine, [workspaceRoot])
|
|
|
|
try {
|
|
const result = await callListDirectory(client, machine.id, { path: workspaceRoot })
|
|
expect((result as { success: boolean }).success).toBe(true)
|
|
expect(entryNames(result)).toEqual(['plain.txt', 'visible-dir'])
|
|
} finally {
|
|
client.shutdown()
|
|
}
|
|
})
|
|
|
|
it('includes dot-prefixed entries when includeHidden is true', async () => {
|
|
const machine = makeMachine('machine-ls-2')
|
|
const client = new ApiMachineClient('cli-token', machine, [workspaceRoot])
|
|
|
|
try {
|
|
const result = await callListDirectory(client, machine.id, { path: workspaceRoot, includeHidden: true })
|
|
expect((result as { success: boolean }).success).toBe(true)
|
|
expect(entryNames(result)).toEqual(['.hidden-dir', '.hidden-file', 'plain.txt', 'visible-dir'])
|
|
} finally {
|
|
client.shutdown()
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('ApiMachineClient connect runner-state advertisement', () => {
|
|
beforeEach(() => {
|
|
ioMock.mockReset()
|
|
})
|
|
|
|
it('advertises piExistingSessionResume with the running state on connect', async () => {
|
|
const machine = makeMachine('capability-machine')
|
|
let connectHandler: () => void = () => {}
|
|
const emitWithAck = vi.fn().mockResolvedValue({
|
|
result: 'success',
|
|
version: 2,
|
|
runnerState: { status: 'running', capabilities: { piExistingSessionResume: true } }
|
|
})
|
|
const socket = {
|
|
on: vi.fn((event: string, handler: () => void) => {
|
|
if (event === 'connect') connectHandler = handler
|
|
}),
|
|
emit: vi.fn(),
|
|
emitWithAck,
|
|
close: vi.fn()
|
|
}
|
|
ioMock.mockReturnValue(socket)
|
|
|
|
const client = new ApiMachineClient('cli-token', machine)
|
|
try {
|
|
client.connect()
|
|
connectHandler()
|
|
await vi.waitFor(() => {
|
|
expect(emitWithAck).toHaveBeenCalled()
|
|
})
|
|
expect(emitWithAck).toHaveBeenCalledWith('machine-update-state', expect.objectContaining({
|
|
runnerState: expect.objectContaining({
|
|
status: 'running',
|
|
capabilities: expect.objectContaining({ piExistingSessionResume: true })
|
|
})
|
|
}))
|
|
} finally {
|
|
client.shutdown()
|
|
}
|
|
})
|
|
})
|