host-runtime.spec.ts 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. import { mkdirSync, mkdtempSync, writeFileSync } from 'node:fs'
  2. import { tmpdir } from 'node:os'
  3. import { join } from 'node:path'
  4. import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
  5. import type { Context } from 'cordis'
  6. import type { Agent } from '@deepseek-ai/dsh-agent'
  7. import { agentEvents } from '@deepseek-ai/dsh-agent'
  8. import type { GenerateOptions, StreamChunk } from '@deepseek-ai/dsh-llm'
  9. import { LlmAdapter } from '@deepseek-ai/dsh-llm'
  10. import type { SessionId } from '@deepseek-ai/dsh-session'
  11. import type { Config as SessionTitleConfig } from '@deepseek-ai/dsh-session-title'
  12. import type { Config as SessionTitleLlmConfig } from '@deepseek-ai/dsh-session-title-first-message-llm'
  13. import type { HostFrame, MuxFrame } from '@deepseek-ai/dsh-host-apiproxy/api'
  14. import type { RpcRequest, RpcResponse } from '@deepseek-ai/dsh-host-apiproxy/api/rpc'
  15. import { RpcId } from '@deepseek-ai/dsh-host-apiproxy/api/rpc'
  16. import { bootHost, startHost, type HostHandle, type RunningHost } from '../src/index.ts'
  17. /** Scripted adapter: each model call consumes the next chunk list; 'hang' streams then waits for abort. */
  18. class ScriptedAdapter extends LlmAdapter {
  19. readonly requests: GenerateOptions[] = []
  20. constructor(private script: (StreamChunk[] | 'hang')[]) {
  21. super()
  22. }
  23. async * stream(options: GenerateOptions): AsyncIterable<StreamChunk> {
  24. if ((options.tools?.length ?? 0) === 0) {
  25. yield * textResponse('Durable append-only session titles')
  26. return
  27. }
  28. this.requests.push(options)
  29. const entry = this.script.shift()
  30. if (!entry) throw new Error('ScriptedAdapter: script exhausted')
  31. if (entry === 'hang') {
  32. yield { type: 'block-start', index: 0, blockType: 'text' }
  33. await new Promise<void>((_resolve, reject) => {
  34. options.signal?.addEventListener('abort', () => { reject(new Error('aborted')) }, { once: true })
  35. })
  36. return
  37. }
  38. yield * entry
  39. }
  40. }
  41. function textResponse(text: string): StreamChunk[] {
  42. return [
  43. { type: 'block-start', index: 0, blockType: 'text' },
  44. { type: 'text-delta', index: 0, text },
  45. { type: 'block-end', index: 0, block: { type: 'text', text } },
  46. { type: 'usage', usage: { inputTokens: 10, outputTokens: text.length } },
  47. { type: 'finish', reason: { kind: 'stop' } },
  48. ]
  49. }
  50. function request<P>(payload: P): RpcRequest<P> {
  51. return { rpcId: RpcId(`req-${String(nextRpc++)}`), payload }
  52. }
  53. let nextRpc = 1
  54. function waitForIdle(ctx: Context, agent: Agent): Promise<void> {
  55. return new Promise((resolve) => {
  56. const dispose = ctx.on('agent/status', (subject: Agent, status: string) => {
  57. if (subject === agent && status === 'idle') {
  58. dispose()
  59. resolve()
  60. }
  61. })
  62. })
  63. }
  64. function expectOk<T>(response: RpcResponse<T>): T {
  65. expect(response.result.ok).toBe(true)
  66. if (!response.result.ok) throw new Error('unreachable')
  67. return response.result.value
  68. }
  69. async function nextMux(iterator: AsyncIterator<RpcRequest<MuxFrame>>): Promise<RpcRequest<MuxFrame>> {
  70. const next = await iterator.next()
  71. if (next.done === true) throw new Error('mux ended before the expected frame')
  72. return next.value
  73. }
  74. /** Durably append a title event without mounting title-generation policy. */
  75. function appendTitle(ctx: Context, agent: Agent, title: string) {
  76. return ctx.sessions.appendOutOfBand(agent.session, 'session/title', {
  77. title,
  78. messageSeqs: [1],
  79. source: { kind: 'fallback' },
  80. }, { kind: 'session-title' })
  81. }
  82. let host: RunningHost | undefined
  83. beforeEach(() => {
  84. vi.stubEnv('DEEPSEEK_API_KEY', 'spec-placeholder-key')
  85. })
  86. afterEach(async () => {
  87. await host?.dispose()
  88. host = undefined
  89. vi.unstubAllEnvs()
  90. })
  91. async function boot(
  92. script: (StreamChunk[] | 'hang')[] = [],
  93. sessionTitle?: SessionTitleConfig,
  94. sessionTitleLlm?: true | SessionTitleLlmConfig,
  95. ): Promise<RunningHost> {
  96. host = await startHost({
  97. boot: {
  98. persistenceRoot: mkdtempSync(join(tmpdir(), 'dsh-host-runtime-')),
  99. workspaceContext: false,
  100. provider: 'scripted',
  101. model: 'test-model',
  102. ...(sessionTitle === undefined ? {} : { sessionTitle }),
  103. ...(sessionTitleLlm === undefined ? {} : { sessionTitleLlm }),
  104. },
  105. })
  106. host.ctx.llm.registerAdapter(['scripted'], new ScriptedAdapter(script))
  107. return host
  108. }
  109. describe('bootHost / startHost', () => {
  110. it('falls back to the deepseek defaults and disposes idempotently', async () => {
  111. const handle: HostHandle = await bootHost({
  112. persistenceRoot: mkdtempSync(join(tmpdir(), 'dsh-boot-')),
  113. workspaceContext: false,
  114. })
  115. expect(handle.defaults).toMatchObject({ provider: 'deepseek', model: 'deepseek-v4-flash' })
  116. expect(typeof handle.defaults.cwd).toBe('string')
  117. await handle.dispose()
  118. })
  119. it('uses the JSONL backend compressed default', async () => {
  120. const handle: HostHandle = await bootHost({
  121. persistenceRoot: mkdtempSync(join(tmpdir(), 'dsh-boot-zstd-')),
  122. workspaceContext: false,
  123. })
  124. const session = handle.ctx.sessions.create()
  125. expect(handle.ctx.sessionPersistence.locate(session.header)?.path).toMatch(/\.jsonl\.zstd$/)
  126. await handle.dispose()
  127. })
  128. it('startHost assembles api + handler over the same defaults and dedupes dispose', async () => {
  129. const running = await boot()
  130. expect(running.defaults).toMatchObject({ provider: 'scripted', model: 'test-model' })
  131. const body = JSON.stringify({ type: 'client-request', rpcId: 'r-h', method: 'host.describe', payload: {} })
  132. const response = await running.handler.fetch(new Request('http://x/api/host.describe', { method: 'POST', body }))
  133. const parsed = await response.json() as { result: { ok: boolean; value: { provider: string } } }
  134. expect(parsed.result.value.provider).toBe('scripted')
  135. const first = running.dispose()
  136. expect(running.dispose()).toBe(first)
  137. await first
  138. host = undefined
  139. })
  140. it('routes workspace instructions through the assembled agent request prefix', async () => {
  141. const workspace = mkdtempSync(join(tmpdir(), 'dsh-host-workspace-'))
  142. mkdirSync(join(workspace, '.git'))
  143. writeFileSync(join(workspace, 'AGENTS.md'), 'host-workspace-context-probe\n')
  144. const adapter = new ScriptedAdapter([textResponse('done')])
  145. host = await startHost({
  146. boot: {
  147. persistenceRoot: mkdtempSync(join(tmpdir(), 'dsh-host-workspace-sessions-')),
  148. workspaceContext: { dshHome: join(workspace, '.dsh'), maxBytes: 65_536 },
  149. provider: 'scripted',
  150. model: 'test-model',
  151. cwd: workspace,
  152. },
  153. })
  154. host.ctx.llm.registerAdapter(['scripted'], adapter)
  155. const { sessionId } = expectOk(await host.api.sessions.create(request({})))
  156. const agent = host.ctx.agents.get(sessionId) as Agent
  157. const idle = waitForIdle(host.ctx, agent)
  158. expectOk(await host.api.sessions.prompt(request({
  159. sessionId,
  160. mode: 'queue' as const,
  161. content: [{ type: 'text' as const, text: 'go' }],
  162. })))
  163. await idle
  164. const requestText = adapter.requests[0]?.messages
  165. .flatMap(message => message.content)
  166. .filter(block => block.type === 'text')
  167. .map(block => block.text)
  168. .join('\n') ?? ''
  169. expect(requestText).toContain('Instructions from: AGENTS.md')
  170. expect(requestText).toContain('host-workspace-context-probe')
  171. })
  172. it('keeps model title generation disabled when sessionTitleLlm is omitted', async () => {
  173. const running = await boot([textResponse('pong')])
  174. const { api, ctx } = running
  175. const { sessionId } = expectOk(await api.sessions.create(request({})))
  176. const agent = ctx.agents.get(sessionId) as Agent
  177. const idle = waitForIdle(ctx, agent)
  178. expectOk(await api.sessions.prompt(request({
  179. sessionId,
  180. mode: 'queue' as const,
  181. content: [{ type: 'text' as const, text: 'Explain durable session titles.' }],
  182. })))
  183. await idle
  184. expect((await ctx.sessionTitle.refresh(agent.session))?.source).toEqual({ kind: 'fallback' })
  185. expect(agent.session.events.some(event => event.type === 'session/title-llm-request')).toBe(false)
  186. })
  187. })
  188. describe('host.describe', () => {
  189. it('reports version, cwd, defaults, and the attached count', async () => {
  190. const { api } = await boot()
  191. const value = expectOk(await api.host.describe(request({})))
  192. expect(value).toMatchObject({ version: '0.0.1', cwd: process.cwd(), provider: 'scripted', model: 'test-model', attachedSessions: 0 })
  193. })
  194. })
  195. describe('sessions.create / list', () => {
  196. it('creates a session (echoing the request rpcId) and lists it newest-first', async () => {
  197. const { api } = await boot()
  198. const created = await api.sessions.create(request({ cwd: '/tmp' }))
  199. const { sessionId } = expectOk(created)
  200. expect(created.rpcId).toMatch(/^req-/)
  201. const second = expectOk(await api.sessions.create(request({}))).sessionId
  202. const { items } = expectOk(await api.sessions.list(request({})))
  203. expect(items.map(item => item.sessionId)).toContain(sessionId)
  204. expect(items.map(item => item.sessionId)).toContain(second)
  205. const first = items.find(item => item.sessionId === sessionId)
  206. expect(first?.cwd).toBe('/tmp')
  207. expect(first?.running).toBe(false)
  208. expect(first?.parentSessionId).toBeUndefined()
  209. })
  210. })
  211. describe('sessions.prompt / cancel', () => {
  212. it.each([
  213. { name: 'host default', config: true, target: '5 words', maxTokens: 64 },
  214. {
  215. name: 'configured policy',
  216. config: {
  217. targetWords: 3,
  218. targetCjkCharacters: 8,
  219. maxInputBytes: 2_048,
  220. maxOutputTokens: 24,
  221. timeoutMs: 2_000,
  222. },
  223. target: '3 words',
  224. maxTokens: 24,
  225. },
  226. ] satisfies {
  227. name: string
  228. config: true | SessionTitleLlmConfig
  229. target: string
  230. maxTokens: number
  231. }[])('replaces the fallback with a model-backed first-message title using the $name', async ({ config, target, maxTokens }) => {
  232. const modelTitle = 'Durable append-only session titles'
  233. const running = await boot([textResponse('pong')], undefined, config)
  234. const { api, ctx } = running
  235. const { sessionId } = expectOk(await api.sessions.create(request({})))
  236. const agent = ctx.agents.get(sessionId) as Agent
  237. const idle = waitForIdle(ctx, agent)
  238. expectOk(await api.sessions.prompt(request({
  239. sessionId,
  240. mode: 'queue' as const,
  241. content: [{ type: 'text' as const, text: 'Explain why append-only logs make session titles durable.' }],
  242. })))
  243. await idle
  244. await vi.waitFor(() => {
  245. expect(agent.session.events.filter(event => event.type === 'session/title').map(event => event.data))
  246. .toEqual([
  247. {
  248. title: 'Explain why append-only logs make',
  249. messageSeqs: [1],
  250. source: { kind: 'fallback' },
  251. },
  252. {
  253. title: modelTitle,
  254. messageSeqs: [1],
  255. source: {
  256. kind: 'provider',
  257. provider: 'session-title-first-message-llm',
  258. model: { provider: 'scripted', model: 'test-model' },
  259. },
  260. },
  261. ])
  262. })
  263. const titleRequest = agent.session.events.find(event => event.type === 'session/title-llm-request')
  264. expect(titleRequest?.data.system).toContain(target)
  265. expect(titleRequest?.data.maxTokens).toBe(maxTokens)
  266. })
  267. it.each([
  268. { name: 'host default', config: undefined, expected: 'Show the Web UI durable' },
  269. {
  270. name: 'configured limit',
  271. config: { fallbackMaxWords: 2, fallbackMaxBytes: 40, maxTitleBytes: 80 },
  272. expected: 'Show the',
  273. },
  274. ] satisfies { name: string; config: SessionTitleConfig | undefined; expected: string }[])(
  275. 'logs a durable fallback title with the $name',
  276. async ({ config, expected }) => {
  277. const running = await boot([textResponse('pong')], config)
  278. const { api, ctx } = running
  279. const { sessionId } = expectOk(await api.sessions.create(request({})))
  280. const agent = ctx.agents.get(sessionId) as Agent
  281. const idle = waitForIdle(ctx, agent)
  282. expectOk(await api.sessions.prompt(request({
  283. sessionId,
  284. mode: 'queue' as const,
  285. content: [{ type: 'text' as const, text: 'Show the Web UI durable session title' }],
  286. })))
  287. await idle
  288. const title = agent.session.events.find(event => event.type === 'session/title')
  289. expect(title?.data).toEqual({
  290. title: expected,
  291. messageSeqs: [1],
  292. source: { kind: 'fallback' },
  293. })
  294. },
  295. )
  296. it('queues a prompt whose rpcId rides into user/message, then the reply lands', async () => {
  297. const running = await boot([textResponse('pong')])
  298. const { api, ctx } = running
  299. const { sessionId } = expectOk(await api.sessions.create(request({})))
  300. const agent = ctx.agents.get(sessionId)
  301. expect(agent).toBeDefined()
  302. const idle = waitForIdle(ctx, agent as Agent)
  303. const promptRequest = request({ sessionId, mode: 'queue' as const, content: [{ type: 'text' as const, text: 'ping' }] })
  304. expectOk(await api.sessions.prompt(promptRequest))
  305. await idle
  306. const value = expectOk(await api.sessions.history(request({ sessionId })))
  307. const events = value.events.map(entry => entry.event)
  308. const userEvent = events.find(event => event.type === 'user/message') as
  309. | { data: { source?: { rpcId?: string } } } | undefined
  310. expect(userEvent?.data.source?.rpcId).toBe(promptRequest.rpcId)
  311. const reply = events.find(event => event.type === 'assistant/message')
  312. expect(reply).toBeDefined()
  313. })
  314. it('steer on an idle agent falls through to send', async () => {
  315. const running = await boot([textResponse('steered')])
  316. const { api, ctx } = running
  317. const { sessionId } = expectOk(await api.sessions.create(request({})))
  318. const idle = waitForIdle(ctx, ctx.agents.get(sessionId) as Agent)
  319. expectOk(await api.sessions.prompt(request({ sessionId, mode: 'steer' as const, content: [{ type: 'text' as const, text: 'now' }] })))
  320. await idle
  321. })
  322. it('errors session-not-found on a ghost session', async () => {
  323. const { api } = await boot()
  324. const response = await api.sessions.prompt(request({ sessionId: 'session-void' as SessionId, mode: 'queue' as const, content: [{ type: 'text' as const, text: 'x' }] }))
  325. expect(response.result.ok).toBe(false)
  326. if (!response.result.ok) expect(response.result.error.code).toBe('session-not-found')
  327. })
  328. it('maps a synchronous send throw to agent-busy', async () => {
  329. const { api } = await boot()
  330. const { sessionId } = expectOk(await api.sessions.create(request({})))
  331. const poisoned = [{ type: 'text', text: 'x', bad: () => 1 }] as never
  332. const response = await api.sessions.prompt(request({ sessionId, mode: 'queue' as const, content: poisoned }))
  333. expect(response.result.ok).toBe(false)
  334. if (!response.result.ok) expect(response.result.error.code).toBe('agent-busy')
  335. })
  336. it('cancels an attached agent and rejects an unattached one', async () => {
  337. const running = await boot(['hang'])
  338. const { api, ctx } = running
  339. const { sessionId } = expectOk(await api.sessions.create(request({})))
  340. const agent = ctx.agents.get(sessionId) as Agent
  341. agent.send([{ type: 'text', text: 'run forever' }])
  342. expectOk(await api.sessions.cancel(request({ sessionId })))
  343. const missing = await api.sessions.cancel(request({ sessionId: 'session-none' as SessionId }))
  344. expect(missing.result.ok).toBe(false)
  345. if (!missing.result.ok) expect(missing.result.error.code).toBe('session-not-found')
  346. })
  347. })
  348. describe('sessions.history', () => {
  349. it('implicitly resumes a cold session, deduplicating concurrent calls to one attach', async () => {
  350. const persistenceRoot = mkdtempSync(join(tmpdir(), 'dsh-host-resume-'))
  351. const first = await startHost({
  352. boot: { persistenceRoot, workspaceContext: false, provider: 'scripted', model: 'test-model' },
  353. })
  354. first.ctx.llm.registerAdapter(['scripted'], new ScriptedAdapter([textResponse('persisted')]))
  355. const { sessionId } = expectOk(await first.api.sessions.create(request({})))
  356. const agent = first.ctx.agents.get(sessionId) as Agent
  357. const idle = waitForIdle(first.ctx, agent)
  358. agent.send([{ type: 'text', text: 'save me' }])
  359. await idle
  360. const titleEvent = await appendTitle(first.ctx, agent, 'Persisted title')
  361. await first.dispose()
  362. host = await startHost({
  363. boot: { persistenceRoot, workspaceContext: false, provider: 'scripted', model: 'test-model' },
  364. })
  365. host.ctx.llm.registerAdapter(['scripted'], new ScriptedAdapter([]))
  366. expect(host.ctx.agents.get(sessionId)).toBeUndefined()
  367. const abort = new AbortController()
  368. const mux = host.api.events.mux(request({}), abort.signal)[Symbol.asyncIterator]()
  369. const [a, b] = await Promise.all([
  370. host.api.sessions.history(request({ sessionId })),
  371. host.api.sessions.history(request({ sessionId })),
  372. ])
  373. for (const response of [a, b]) {
  374. const value = expectOk(response)
  375. expect(value.events.some(entry => entry.event.type === 'assistant/message')).toBe(true)
  376. }
  377. expect(host.ctx.agents.get(sessionId)).toBeDefined()
  378. expect(host.ctx.agents.list()).toHaveLength(1)
  379. expect((await nextMux(mux)).payload).toMatchObject({ type: 'session/subscribed', sessionId })
  380. expect((await nextMux(mux)).payload).toEqual(expect.objectContaining({
  381. type: 'session/title', sessionId, title: 'Persisted title', eventSeq: titleEvent.seq,
  382. }))
  383. abort.abort()
  384. })
  385. it('errors session-not-found when resume fails, deduplicating concurrent resumes', async () => {
  386. const { api } = await boot()
  387. const ghost = 'session-ghost' as SessionId
  388. const [first, second] = await Promise.all([
  389. api.sessions.history(request({ sessionId: ghost })),
  390. api.sessions.history(request({ sessionId: ghost })),
  391. ])
  392. for (const response of [first, second]) {
  393. expect(response.result.ok).toBe(false)
  394. if (!response.result.ok) expect(response.result.error.code).toBe('session-not-found')
  395. }
  396. })
  397. it('paginates backwards on message boundaries with hasMore', async () => {
  398. const running = await boot([textResponse('a1'), textResponse('a2'), textResponse('a3')])
  399. const { api, ctx } = running
  400. const { sessionId } = expectOk(await api.sessions.create(request({})))
  401. const agent = ctx.agents.get(sessionId) as Agent
  402. for (const text of ['q1', 'q2', 'q3']) {
  403. const idle = waitForIdle(ctx, agent)
  404. agent.send([{ type: 'text', text }])
  405. await idle
  406. }
  407. const all = expectOk(await api.sessions.history(request({ sessionId })))
  408. expect(all.hasMore).toBe(false)
  409. const messageCount = all.events.filter(entry => entry.event.type === 'user/message' || entry.event.type === 'assistant/message').length
  410. expect(messageCount).toBe(6)
  411. const lastPage = expectOk(await api.sessions.history(request({ sessionId, maxMessages: 1 })))
  412. expect(lastPage.hasMore).toBe(true)
  413. expect(lastPage.events.filter(entry => entry.event.type === 'assistant/message')).toHaveLength(1)
  414. expect(lastPage.events.filter(entry => entry.event.type === 'user/message')).toHaveLength(0)
  415. const firstSeq = lastPage.events[0]?.event.seq as number
  416. const olderPage = expectOk(await api.sessions.history(request({ sessionId, beforeSeq: firstSeq, maxMessages: 2 })))
  417. expect(olderPage.events.at(-1)?.event.seq).toBeLessThan(firstSeq)
  418. expect(olderPage.hasMore).toBe(true)
  419. expect(olderPage.events.filter(entry => entry.event.type === 'user/message' || entry.event.type === 'assistant/message').length).toBe(2)
  420. })
  421. })
  422. describe('events streams', () => {
  423. it('mux: a pending pull wakes when a frame arrives (waiter path)', async () => {
  424. const running = await boot()
  425. const { api } = running
  426. const ac = new AbortController()
  427. const stream = api.events.mux(request({}), ac.signal)[Symbol.asyncIterator]()
  428. // no sessions yet: next() must pend on the queue's waiter, not the buffer
  429. const pending = stream.next()
  430. const { sessionId } = expectOk(await api.sessions.create(request({})))
  431. const frame = (await pending).value as RpcRequest<MuxFrame>
  432. expect(frame.payload).toMatchObject({ type: 'session/subscribed', sessionId })
  433. ac.abort()
  434. expect((await stream.next()).done).toBe(true)
  435. })
  436. it('lists fork lineage and announces it on the host stream', async () => {
  437. const running = await boot()
  438. const { api, ctx } = running
  439. const { sessionId: parent } = expectOk(await api.sessions.create(request({})))
  440. const ac = new AbortController()
  441. const stream = api.events.host(request({}), ac.signal)[Symbol.asyncIterator]()
  442. const child = `session-child-${String(Date.now())}` as SessionId
  443. const handle = await ctx.agents.create({ sessionId: child, meta: { parentSession: parent }, agentOptions: { provider: 'scripted', model: 'test-model' } })
  444. expect(handle.agent.id).toBe(child)
  445. const added = (await stream.next()).value as RpcRequest<HostFrame>
  446. expect(added.payload).toMatchObject({ type: 'host/session-added', sessionId: child, parentSessionId: parent })
  447. const { items } = expectOk(await api.sessions.list(request({})))
  448. expect(items.find(item => item.sessionId === child)?.parentSessionId).toBe(parent)
  449. await handle.dispose()
  450. let frame: RpcRequest<HostFrame>
  451. do frame = (await stream.next()).value as RpcRequest<HostFrame>
  452. while (frame.payload.type !== 'host/session-removed')
  453. expect(frame.payload).toMatchObject({ type: 'host/session-removed', sessionId: child })
  454. ac.abort()
  455. })
  456. it('mux: emits subscribed baselines, live session events, and new-session subscriptions until abort', async () => {
  457. const running = await boot([textResponse('live')])
  458. const { api, ctx } = running
  459. const { sessionId } = expectOk(await api.sessions.create(request({})))
  460. const ac = new AbortController()
  461. const stream = api.events.mux(request({}), ac.signal)[Symbol.asyncIterator]()
  462. const baseline = await stream.next()
  463. expect((baseline.value as RpcRequest<MuxFrame>).payload).toMatchObject({ type: 'session/subscribed', sessionId })
  464. const agent = ctx.agents.get(sessionId) as Agent
  465. const idle = waitForIdle(ctx, agent)
  466. agent.send([{ type: 'text', text: 'go' }])
  467. await idle
  468. const live = await stream.next()
  469. expect((live.value as RpcRequest<MuxFrame>).payload.type).toBe('session/event')
  470. const other = expectOk(await api.sessions.create(request({}))).sessionId
  471. let frame: RpcRequest<MuxFrame>
  472. do frame = (await stream.next()).value as RpcRequest<MuxFrame>
  473. while (!(frame.payload.type === 'session/subscribed' && frame.payload.sessionId === other))
  474. ac.abort()
  475. expect((await stream.next()).done).toBe(true)
  476. })
  477. it('mux: projects durable titles after open baselines and immediately after live raw events', async () => {
  478. const running = await boot()
  479. const { api, ctx } = running
  480. const { sessionId } = expectOk(await api.sessions.create(request({})))
  481. const agent = ctx.agents.get(sessionId) as Agent
  482. const initial = await appendTitle(ctx, agent, 'Initial title')
  483. const ac = new AbortController()
  484. const stream = api.events.mux(request({}), ac.signal)[Symbol.asyncIterator]()
  485. expect((await nextMux(stream)).payload).toMatchObject({ type: 'session/subscribed', sessionId })
  486. expect((await nextMux(stream)).payload).toEqual(expect.objectContaining({
  487. type: 'session/title', sessionId, title: 'Initial title', eventSeq: initial.seq, updatedAt: initial.time,
  488. }))
  489. const revised = await appendTitle(ctx, agent, 'Revised title')
  490. let raw: RpcRequest<MuxFrame>
  491. do raw = await nextMux(stream)
  492. while (!(raw.payload.type === 'session/event' && raw.payload.event.type === 'session/title'))
  493. expect(raw.payload).toMatchObject({ type: 'session/event', sessionId, event: { seq: revised.seq } })
  494. expect((await nextMux(stream)).payload).toEqual(expect.objectContaining({
  495. type: 'session/title', sessionId, title: 'Revised title', eventSeq: revised.seq, updatedAt: revised.time,
  496. }))
  497. ac.abort()
  498. })
  499. it('mux: emits no title control for untitled subscriptions', async () => {
  500. const { api } = await boot()
  501. const first = expectOk(await api.sessions.create(request({}))).sessionId
  502. const ac = new AbortController()
  503. const stream = api.events.mux(request({}), ac.signal)[Symbol.asyncIterator]()
  504. expect((await nextMux(stream)).payload).toMatchObject({ type: 'session/subscribed', sessionId: first })
  505. const second = expectOk(await api.sessions.create(request({}))).sessionId
  506. expect((await nextMux(stream)).payload).toMatchObject({ type: 'session/subscribed', sessionId: second })
  507. ac.abort()
  508. })
  509. it('host: session lifecycle, status flips (disposed suppressed), and agent errors', async () => {
  510. const running = await boot([textResponse('x')])
  511. const { api, ctx } = running
  512. const ac = new AbortController()
  513. const stream = api.events.host(request({}), ac.signal)[Symbol.asyncIterator]()
  514. const { sessionId } = expectOk(await api.sessions.create(request({})))
  515. const added = await stream.next()
  516. expect((added.value as RpcRequest<HostFrame>).payload).toMatchObject({ type: 'host/session-added', sessionId })
  517. const agent = ctx.agents.get(sessionId) as Agent
  518. const idle = waitForIdle(ctx, agent)
  519. agent.send([{ type: 'text', text: 'run' }])
  520. await idle
  521. const runningFrame = await stream.next()
  522. expect((runningFrame.value as RpcRequest<HostFrame>).payload).toMatchObject({ type: 'host/session-status', running: true })
  523. const idleFrame = await stream.next()
  524. expect((idleFrame.value as RpcRequest<HostFrame>).payload).toMatchObject({ type: 'host/session-status', running: false })
  525. // Raw ctx.emit lacks the scope carrier the mounted invariants plugin now
  526. // enforces; dispatch the way the loop does.
  527. agentEvents(ctx, agent).emit('agent/error', 1, 1, new Error('boom'))
  528. const errorFrame = await stream.next()
  529. expect((errorFrame.value as RpcRequest<HostFrame>).payload).toMatchObject({ type: 'host/agent-error', message: 'Error: boom' })
  530. ac.abort()
  531. // Push-after-done: an event landing between abort and generator wind-down
  532. // must be dropped silently, not crash the queue.
  533. agentEvents(ctx, agent).emit('agent/error', 1, 1, new Error('late'))
  534. expect((await stream.next()).done).toBe(true)
  535. })
  536. })
  537. describe('question request / response', () => {
  538. const questions = [{
  539. id: 'mode', question: 'Choose a mode',
  540. options: [
  541. { label: 'Fast (Recommended)', description: 'Move quickly.' },
  542. { label: 'Careful', description: 'Review first.' },
  543. ],
  544. }]
  545. it('waits, replays the same rpcId on reconnect, validates, and resolves first-wins', async () => {
  546. const running = await boot()
  547. const { api, ctx } = running
  548. const { sessionId } = expectOk(await api.sessions.create(request({})))
  549. const agent = ctx.agents.get(sessionId) as Agent
  550. const ac = new AbortController()
  551. const stream = api.events.mux(request({}), ac.signal)[Symbol.asyncIterator]()
  552. await stream.next() // subscribed baseline starts the generator and installs the queue
  553. const answerPromise = ctx.userInteraction.ask({ questions, agent })
  554. const requested = (await stream.next()).value as RpcRequest<MuxFrame>
  555. expect(requested.payload).toMatchObject({ type: 'question/requested', sessionId, questions })
  556. const wrongSession = await api.respond({
  557. type: 'client-response', rpcId: requested.rpcId,
  558. result: {
  559. ok: true,
  560. value: { sessionId: 'session-other', answer: { answers: [{ id: 'mode', selected: ['Fast (Recommended)'] }] } },
  561. },
  562. })
  563. expect(wrongSession).toEqual({ accepted: false, reason: 'bad-response' })
  564. const badChoice = await api.respond({
  565. type: 'client-response', rpcId: requested.rpcId,
  566. result: {
  567. ok: true,
  568. value: { sessionId, answer: { answers: [{ id: 'mode', selected: ['Unknown'] }] } },
  569. },
  570. })
  571. expect(badChoice).toEqual({ accepted: false, reason: 'bad-response' })
  572. const invalidResults = [
  573. { ok: true as const, value: null },
  574. { ok: true as const, value: { sessionId, answer: { answers: [] } } },
  575. { ok: true as const, value: { sessionId, answer: { answers: [{ id: 'wrong', selected: ['Fast (Recommended)'] }] } } },
  576. { ok: true as const, value: { sessionId, answer: { answers: [{ id: 'mode', selected: ['Fast (Recommended)', 'Fast (Recommended)'] }] } } },
  577. { ok: true as const, value: { sessionId, answer: { answers: [{ id: 'mode', selected: ['Fast (Recommended)', 'Careful'] }] } } },
  578. { ok: true as const, value: { sessionId, answer: { answers: [{ id: 'mode', selected: [], custom: ' ' }] } } },
  579. { ok: true as const, value: { sessionId, answer: { answers: [{ id: 'mode', selected: ['Careful'], custom: 'Other' }] } } },
  580. { ok: false as const, error: { code: 'internal' as const, message: 'wrong error', details: {} } },
  581. ]
  582. for (const result of invalidResults) {
  583. expect(await api.respond({
  584. type: 'client-response', rpcId: requested.rpcId, result,
  585. })).toEqual({ accepted: false, reason: 'bad-response' })
  586. }
  587. const reconnectAbort = new AbortController()
  588. const replay = api.events.mux(request({}), reconnectAbort.signal)[Symbol.asyncIterator]()
  589. await replay.next()
  590. const replayed = (await replay.next()).value as RpcRequest<MuxFrame>
  591. expect(replayed.rpcId).toBe(requested.rpcId)
  592. expect(replayed.payload).toEqual(requested.payload)
  593. const response = {
  594. type: 'client-response' as const,
  595. rpcId: requested.rpcId,
  596. result: {
  597. ok: true as const,
  598. value: { sessionId, answer: { answers: [{ id: 'mode', selected: ['Fast (Recommended)'] }] } },
  599. },
  600. }
  601. const [first, duplicate] = await Promise.all([api.respond(response), api.respond(response)])
  602. expect([first, duplicate]).toContainEqual({ accepted: true })
  603. expect([first, duplicate]).toContainEqual({ accepted: false, reason: 'not-pending' })
  604. await expect(answerPromise).resolves.toEqual({
  605. answers: [{ id: 'mode', selected: ['Fast (Recommended)'] }],
  606. })
  607. const resolved = (await stream.next()).value as RpcRequest<MuxFrame>
  608. expect(resolved.payload).toMatchObject({
  609. type: 'question/resolved', sessionId, questionRpcId: requested.rpcId, outcome: 'answered',
  610. })
  611. expect(await api.respond(response)).toEqual({ accepted: false, reason: 'not-pending' })
  612. const customQuestions = [{ id: 'detail', question: 'What else?' }]
  613. const customAnswer = ctx.userInteraction.ask({ questions: customQuestions, agent })
  614. const customRequested = (await stream.next()).value as RpcRequest<MuxFrame>
  615. expect(await api.respond({
  616. type: 'client-response', rpcId: customRequested.rpcId,
  617. result: {
  618. ok: true,
  619. value: { sessionId, answer: { answers: [{ id: 'detail', selected: [], custom: 'Keep traces' }] } },
  620. },
  621. })).toEqual({ accepted: true })
  622. await expect(customAnswer).resolves.toEqual({
  623. answers: [{ id: 'detail', selected: [], custom: 'Keep traces' }],
  624. })
  625. expect(((await stream.next()).value as RpcRequest<MuxFrame>).payload).toMatchObject({
  626. type: 'question/resolved', questionRpcId: customRequested.rpcId, outcome: 'answered',
  627. })
  628. const blankAnswer = ctx.userInteraction.ask({ questions, agent })
  629. const blankRequested = (await stream.next()).value as RpcRequest<MuxFrame>
  630. expect(await api.respond({
  631. type: 'client-response', rpcId: blankRequested.rpcId,
  632. result: {
  633. ok: true,
  634. value: { sessionId, answer: { answers: [{ id: 'mode', selected: [] }] } },
  635. },
  636. })).toEqual({ accepted: true })
  637. await expect(blankAnswer).resolves.toEqual({
  638. answers: [{ id: 'mode', selected: [] }],
  639. })
  640. expect(((await stream.next()).value as RpcRequest<MuxFrame>).payload).toMatchObject({
  641. type: 'question/resolved', questionRpcId: blankRequested.rpcId, outcome: 'answered',
  642. })
  643. ac.abort()
  644. reconnectAbort.abort()
  645. })
  646. it('distinguishes user cancellation from owner abort and rejects late responses', async () => {
  647. const running = await boot()
  648. const { api, ctx } = running
  649. const { sessionId } = expectOk(await api.sessions.create(request({})))
  650. const agent = ctx.agents.get(sessionId) as Agent
  651. const streamAbort = new AbortController()
  652. const stream = api.events.mux(request({}), streamAbort.signal)[Symbol.asyncIterator]()
  653. await stream.next()
  654. const cancelled = ctx.userInteraction.ask({ questions, agent }).catch((error: unknown) => error)
  655. const requested = (await stream.next()).value as RpcRequest<MuxFrame>
  656. expect(await api.respond({
  657. type: 'client-response', rpcId: requested.rpcId,
  658. result: { ok: false, error: { code: 'cancelled', message: 'skip', details: {} } },
  659. })).toEqual({ accepted: true })
  660. await expect(cancelled).resolves.toMatchObject({ code: 'ASK_CANCELLED' })
  661. expect(((await stream.next()).value as RpcRequest<MuxFrame>).payload).toMatchObject({
  662. type: 'question/resolved', outcome: 'cancelled',
  663. })
  664. const ownerAbort = new AbortController()
  665. const aborted = ctx.userInteraction.ask({ questions, agent, signal: ownerAbort.signal })
  666. .catch((error: unknown) => error)
  667. const abortRequest = (await stream.next()).value as RpcRequest<MuxFrame>
  668. ownerAbort.abort()
  669. await expect(aborted).resolves.toMatchObject({ code: 'ASK_ABORTED' })
  670. expect(((await stream.next()).value as RpcRequest<MuxFrame>).payload).toMatchObject({
  671. type: 'question/resolved', questionRpcId: abortRequest.rpcId, outcome: 'cancelled',
  672. })
  673. expect(await api.respond({
  674. type: 'client-response', rpcId: abortRequest.rpcId,
  675. result: { ok: false, error: { code: 'cancelled', message: 'late', details: {} } },
  676. })).toEqual({ accepted: false, reason: 'not-pending' })
  677. streamAbort.abort()
  678. })
  679. it('rejects missing routing and pre-abort, then aborts outstanding waits on disposal', async () => {
  680. const running = await boot()
  681. const { ctx } = running
  682. await expect(ctx.userInteraction.ask({ questions })).rejects.toMatchObject({ code: 'ASK_MISSING_AGENT' })
  683. const { sessionId } = expectOk(await running.api.sessions.create(request({})))
  684. const agent = ctx.agents.get(sessionId) as Agent
  685. const alreadyAborted = new AbortController()
  686. alreadyAborted.abort()
  687. await expect(ctx.userInteraction.ask({ questions, agent, signal: alreadyAborted.signal }))
  688. .rejects.toMatchObject({ code: 'ASK_ABORTED' })
  689. const outstanding = ctx.userInteraction.ask({ questions, agent })
  690. const disposed = running.dispose()
  691. host = undefined
  692. await expect(outstanding).rejects.toMatchObject({ code: 'ASK_ABORTED' })
  693. await disposed
  694. })
  695. })