host-runtime.spec.ts 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. import { existsSync, 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. it('ensures a missing project directory before minting the session', async () => {
  211. const { api } = await boot()
  212. const root = mkdtempSync(join(tmpdir(), 'dsh-host-create-cwd-'))
  213. const cwd = join(root, 'nested', 'workspace')
  214. expect(existsSync(cwd)).toBe(false)
  215. const { sessionId } = expectOk(await api.sessions.create(request({ cwd })))
  216. expect(existsSync(cwd)).toBe(true)
  217. const { items } = expectOk(await api.sessions.list(request({})))
  218. expect(items.find(item => item.sessionId === sessionId)?.cwd).toBe(cwd)
  219. })
  220. it('fails loud when the project directory cannot be created', async () => {
  221. const { api } = await boot()
  222. const root = mkdtempSync(join(tmpdir(), 'dsh-host-create-cwd-fail-'))
  223. const blocker = join(root, 'file-not-dir')
  224. writeFileSync(blocker, 'x')
  225. const response = await api.sessions.create(request({ cwd: join(blocker, 'child') }))
  226. expect(response.result.ok).toBe(false)
  227. if (response.result.ok) throw new Error('expected mkdir failure')
  228. expect(response.result.error.code).toBe('internal')
  229. expect(response.result.error.message).toMatch(/failed to ensure project directory/)
  230. })
  231. })
  232. describe('sessions.prompt / cancel', () => {
  233. it.each([
  234. { name: 'host default', config: true, target: '5 words', maxTokens: 64 },
  235. {
  236. name: 'configured policy',
  237. config: {
  238. targetWords: 3,
  239. targetCjkCharacters: 8,
  240. maxInputBytes: 2_048,
  241. maxOutputTokens: 24,
  242. timeoutMs: 2_000,
  243. },
  244. target: '3 words',
  245. maxTokens: 24,
  246. },
  247. ] satisfies {
  248. name: string
  249. config: true | SessionTitleLlmConfig
  250. target: string
  251. maxTokens: number
  252. }[])('replaces the fallback with a model-backed first-message title using the $name', async ({ config, target, maxTokens }) => {
  253. const modelTitle = 'Durable append-only session titles'
  254. const running = await boot([textResponse('pong')], undefined, config)
  255. const { api, ctx } = running
  256. const { sessionId } = expectOk(await api.sessions.create(request({})))
  257. const agent = ctx.agents.get(sessionId) as Agent
  258. const idle = waitForIdle(ctx, agent)
  259. expectOk(await api.sessions.prompt(request({
  260. sessionId,
  261. mode: 'queue' as const,
  262. content: [{ type: 'text' as const, text: 'Explain why append-only logs make session titles durable.' }],
  263. })))
  264. await idle
  265. await vi.waitFor(() => {
  266. expect(agent.session.events.filter(event => event.type === 'session/title').map(event => event.data))
  267. .toEqual([
  268. {
  269. title: 'Explain why append-only logs make',
  270. messageSeqs: [1],
  271. source: { kind: 'fallback' },
  272. },
  273. {
  274. title: modelTitle,
  275. messageSeqs: [1],
  276. source: {
  277. kind: 'provider',
  278. provider: 'session-title-first-message-llm',
  279. model: { provider: 'scripted', model: 'test-model' },
  280. },
  281. },
  282. ])
  283. })
  284. const titleRequest = agent.session.events.find(event => event.type === 'session/title-llm-request')
  285. expect(titleRequest?.data.system).toContain(target)
  286. expect(titleRequest?.data.maxTokens).toBe(maxTokens)
  287. })
  288. it.each([
  289. { name: 'host default', config: undefined, expected: 'Show the Web UI durable' },
  290. {
  291. name: 'configured limit',
  292. config: { fallbackMaxWords: 2, fallbackMaxBytes: 40, maxTitleBytes: 80 },
  293. expected: 'Show the',
  294. },
  295. ] satisfies { name: string; config: SessionTitleConfig | undefined; expected: string }[])(
  296. 'logs a durable fallback title with the $name',
  297. async ({ config, expected }) => {
  298. const running = await boot([textResponse('pong')], config)
  299. const { api, ctx } = running
  300. const { sessionId } = expectOk(await api.sessions.create(request({})))
  301. const agent = ctx.agents.get(sessionId) as Agent
  302. const idle = waitForIdle(ctx, agent)
  303. expectOk(await api.sessions.prompt(request({
  304. sessionId,
  305. mode: 'queue' as const,
  306. content: [{ type: 'text' as const, text: 'Show the Web UI durable session title' }],
  307. })))
  308. await idle
  309. const title = agent.session.events.find(event => event.type === 'session/title')
  310. expect(title?.data).toEqual({
  311. title: expected,
  312. messageSeqs: [1],
  313. source: { kind: 'fallback' },
  314. })
  315. },
  316. )
  317. it('queues a prompt whose rpcId rides into user/message, then the reply lands', async () => {
  318. const running = await boot([textResponse('pong')])
  319. const { api, ctx } = running
  320. const { sessionId } = expectOk(await api.sessions.create(request({})))
  321. const agent = ctx.agents.get(sessionId)
  322. expect(agent).toBeDefined()
  323. const idle = waitForIdle(ctx, agent as Agent)
  324. const promptRequest = request({ sessionId, mode: 'queue' as const, content: [{ type: 'text' as const, text: 'ping' }] })
  325. expectOk(await api.sessions.prompt(promptRequest))
  326. await idle
  327. const value = expectOk(await api.sessions.history(request({ sessionId })))
  328. const events = value.events.map(entry => entry.event)
  329. const userEvent = events.find(event => event.type === 'user/message') as
  330. | { data: { source?: { rpcId?: string } } } | undefined
  331. expect(userEvent?.data.source?.rpcId).toBe(promptRequest.rpcId)
  332. const reply = events.find(event => event.type === 'assistant/message')
  333. expect(reply).toBeDefined()
  334. })
  335. it('steer on an idle agent falls through to send', async () => {
  336. const running = await boot([textResponse('steered')])
  337. const { api, ctx } = running
  338. const { sessionId } = expectOk(await api.sessions.create(request({})))
  339. const idle = waitForIdle(ctx, ctx.agents.get(sessionId) as Agent)
  340. expectOk(await api.sessions.prompt(request({ sessionId, mode: 'steer' as const, content: [{ type: 'text' as const, text: 'now' }] })))
  341. await idle
  342. })
  343. it('errors session-not-found on a ghost session', async () => {
  344. const { api } = await boot()
  345. const response = await api.sessions.prompt(request({ sessionId: 'session-void' as SessionId, mode: 'queue' as const, content: [{ type: 'text' as const, text: 'x' }] }))
  346. expect(response.result.ok).toBe(false)
  347. if (!response.result.ok) expect(response.result.error.code).toBe('session-not-found')
  348. })
  349. it('maps a synchronous send throw to agent-busy', async () => {
  350. const { api } = await boot()
  351. const { sessionId } = expectOk(await api.sessions.create(request({})))
  352. const poisoned = [{ type: 'text', text: 'x', bad: () => 1 }] as never
  353. const response = await api.sessions.prompt(request({ sessionId, mode: 'queue' as const, content: poisoned }))
  354. expect(response.result.ok).toBe(false)
  355. if (!response.result.ok) expect(response.result.error.code).toBe('agent-busy')
  356. })
  357. it('cancels an attached agent and rejects an unattached one', async () => {
  358. const running = await boot(['hang'])
  359. const { api, ctx } = running
  360. const { sessionId } = expectOk(await api.sessions.create(request({})))
  361. const agent = ctx.agents.get(sessionId) as Agent
  362. agent.followup([{ type: 'text', text: 'run forever' }])
  363. expectOk(await api.sessions.cancel(request({ sessionId })))
  364. const missing = await api.sessions.cancel(request({ sessionId: 'session-none' as SessionId }))
  365. expect(missing.result.ok).toBe(false)
  366. if (!missing.result.ok) expect(missing.result.error.code).toBe('session-not-found')
  367. })
  368. })
  369. describe('sessions.history', () => {
  370. it('implicitly resumes a cold session, deduplicating concurrent calls to one attach', async () => {
  371. const persistenceRoot = mkdtempSync(join(tmpdir(), 'dsh-host-resume-'))
  372. const first = await startHost({
  373. boot: { persistenceRoot, workspaceContext: false, provider: 'scripted', model: 'test-model' },
  374. })
  375. first.ctx.llm.registerAdapter(['scripted'], new ScriptedAdapter([textResponse('persisted')]))
  376. const { sessionId } = expectOk(await first.api.sessions.create(request({})))
  377. const agent = first.ctx.agents.get(sessionId) as Agent
  378. const idle = waitForIdle(first.ctx, agent)
  379. agent.followup([{ type: 'text', text: 'save me' }])
  380. await idle
  381. const titleEvent = await appendTitle(first.ctx, agent, 'Persisted title')
  382. await first.dispose()
  383. host = await startHost({
  384. boot: { persistenceRoot, workspaceContext: false, provider: 'scripted', model: 'test-model' },
  385. })
  386. host.ctx.llm.registerAdapter(['scripted'], new ScriptedAdapter([]))
  387. expect(host.ctx.agents.get(sessionId)).toBeUndefined()
  388. const abort = new AbortController()
  389. const mux = host.api.events.mux(request({}), abort.signal)[Symbol.asyncIterator]()
  390. const [a, b] = await Promise.all([
  391. host.api.sessions.history(request({ sessionId })),
  392. host.api.sessions.history(request({ sessionId })),
  393. ])
  394. for (const response of [a, b]) {
  395. const value = expectOk(response)
  396. expect(value.events.some(entry => entry.event.type === 'assistant/message')).toBe(true)
  397. }
  398. expect(host.ctx.agents.get(sessionId)).toBeDefined()
  399. expect(host.ctx.agents.list()).toHaveLength(1)
  400. expect((await nextMux(mux)).payload).toMatchObject({ type: 'session/subscribed', sessionId })
  401. expect((await nextMux(mux)).payload).toEqual(expect.objectContaining({
  402. type: 'session/title', sessionId, title: 'Persisted title', eventSeq: titleEvent.seq,
  403. }))
  404. abort.abort()
  405. })
  406. it('errors session-not-found when resume fails, deduplicating concurrent resumes', async () => {
  407. const { api } = await boot()
  408. const ghost = 'session-ghost' as SessionId
  409. const [first, second] = await Promise.all([
  410. api.sessions.history(request({ sessionId: ghost })),
  411. api.sessions.history(request({ sessionId: ghost })),
  412. ])
  413. for (const response of [first, second]) {
  414. expect(response.result.ok).toBe(false)
  415. if (!response.result.ok) expect(response.result.error.code).toBe('session-not-found')
  416. }
  417. })
  418. it('paginates backwards on message boundaries with hasMore', async () => {
  419. const running = await boot([textResponse('a1'), textResponse('a2'), textResponse('a3')])
  420. const { api, ctx } = running
  421. const { sessionId } = expectOk(await api.sessions.create(request({})))
  422. const agent = ctx.agents.get(sessionId) as Agent
  423. for (const text of ['q1', 'q2', 'q3']) {
  424. const idle = waitForIdle(ctx, agent)
  425. agent.followup([{ type: 'text', text }])
  426. await idle
  427. }
  428. const all = expectOk(await api.sessions.history(request({ sessionId })))
  429. expect(all.hasMore).toBe(false)
  430. const messageCount = all.events.filter(entry => entry.event.type === 'user/message' || entry.event.type === 'assistant/message').length
  431. expect(messageCount).toBe(6)
  432. const lastPage = expectOk(await api.sessions.history(request({ sessionId, maxMessages: 1 })))
  433. expect(lastPage.hasMore).toBe(true)
  434. expect(lastPage.events.filter(entry => entry.event.type === 'assistant/message')).toHaveLength(1)
  435. expect(lastPage.events.filter(entry => entry.event.type === 'user/message')).toHaveLength(0)
  436. const firstSeq = lastPage.events[0]?.event.seq as number
  437. const olderPage = expectOk(await api.sessions.history(request({ sessionId, beforeSeq: firstSeq, maxMessages: 2 })))
  438. expect(olderPage.events.at(-1)?.event.seq).toBeLessThan(firstSeq)
  439. expect(olderPage.hasMore).toBe(true)
  440. expect(olderPage.events.filter(entry => entry.event.type === 'user/message' || entry.event.type === 'assistant/message').length).toBe(2)
  441. })
  442. })
  443. describe('events streams', () => {
  444. it('mux: a pending pull wakes when a frame arrives (waiter path)', async () => {
  445. const running = await boot()
  446. const { api } = running
  447. const ac = new AbortController()
  448. const stream = api.events.mux(request({}), ac.signal)[Symbol.asyncIterator]()
  449. // no sessions yet: next() must pend on the queue's waiter, not the buffer
  450. const pending = stream.next()
  451. const { sessionId } = expectOk(await api.sessions.create(request({})))
  452. const frame = (await pending).value as RpcRequest<MuxFrame>
  453. expect(frame.payload).toMatchObject({ type: 'session/subscribed', sessionId })
  454. ac.abort()
  455. expect((await stream.next()).done).toBe(true)
  456. })
  457. it('lists fork lineage and announces it on the host stream', async () => {
  458. const running = await boot()
  459. const { api, ctx } = running
  460. const { sessionId: parent } = expectOk(await api.sessions.create(request({})))
  461. const ac = new AbortController()
  462. const stream = api.events.host(request({}), ac.signal)[Symbol.asyncIterator]()
  463. const child = `session-child-${String(Date.now())}` as SessionId
  464. const handle = await ctx.agents.create({ sessionId: child, meta: { parentSession: parent }, agentOptions: { provider: 'scripted', model: 'test-model' } })
  465. expect(handle.agent.id).toBe(child)
  466. const added = (await stream.next()).value as RpcRequest<HostFrame>
  467. expect(added.payload).toMatchObject({ type: 'host/session-added', sessionId: child, parentSessionId: parent })
  468. const { items } = expectOk(await api.sessions.list(request({})))
  469. expect(items.find(item => item.sessionId === child)?.parentSessionId).toBe(parent)
  470. await handle.dispose()
  471. let frame: RpcRequest<HostFrame>
  472. do frame = (await stream.next()).value as RpcRequest<HostFrame>
  473. while (frame.payload.type !== 'host/session-removed')
  474. expect(frame.payload).toMatchObject({ type: 'host/session-removed', sessionId: child })
  475. ac.abort()
  476. })
  477. it('mux: emits subscribed baselines, live session events, and new-session subscriptions until abort', async () => {
  478. const running = await boot([textResponse('live')])
  479. const { api, ctx } = running
  480. const { sessionId } = expectOk(await api.sessions.create(request({})))
  481. const ac = new AbortController()
  482. const stream = api.events.mux(request({}), ac.signal)[Symbol.asyncIterator]()
  483. const baseline = await stream.next()
  484. expect((baseline.value as RpcRequest<MuxFrame>).payload).toMatchObject({ type: 'session/subscribed', sessionId })
  485. const agent = ctx.agents.get(sessionId) as Agent
  486. const idle = waitForIdle(ctx, agent)
  487. agent.followup([{ type: 'text', text: 'go' }])
  488. await idle
  489. const live = await stream.next()
  490. expect((live.value as RpcRequest<MuxFrame>).payload.type).toBe('session/event')
  491. const other = expectOk(await api.sessions.create(request({}))).sessionId
  492. let frame: RpcRequest<MuxFrame>
  493. do frame = (await stream.next()).value as RpcRequest<MuxFrame>
  494. while (!(frame.payload.type === 'session/subscribed' && frame.payload.sessionId === other))
  495. ac.abort()
  496. expect((await stream.next()).done).toBe(true)
  497. })
  498. it('mux: projects durable titles after open baselines and immediately after live raw events', async () => {
  499. const running = await boot()
  500. const { api, ctx } = running
  501. const { sessionId } = expectOk(await api.sessions.create(request({})))
  502. const agent = ctx.agents.get(sessionId) as Agent
  503. const initial = await appendTitle(ctx, agent, 'Initial title')
  504. const ac = new AbortController()
  505. const stream = api.events.mux(request({}), ac.signal)[Symbol.asyncIterator]()
  506. expect((await nextMux(stream)).payload).toMatchObject({ type: 'session/subscribed', sessionId })
  507. expect((await nextMux(stream)).payload).toEqual(expect.objectContaining({
  508. type: 'session/title', sessionId, title: 'Initial title', eventSeq: initial.seq, updatedAt: initial.time,
  509. }))
  510. const revised = await appendTitle(ctx, agent, 'Revised title')
  511. let raw: RpcRequest<MuxFrame>
  512. do raw = await nextMux(stream)
  513. while (!(raw.payload.type === 'session/event' && raw.payload.event.type === 'session/title'))
  514. expect(raw.payload).toMatchObject({ type: 'session/event', sessionId, event: { seq: revised.seq } })
  515. expect((await nextMux(stream)).payload).toEqual(expect.objectContaining({
  516. type: 'session/title', sessionId, title: 'Revised title', eventSeq: revised.seq, updatedAt: revised.time,
  517. }))
  518. ac.abort()
  519. })
  520. it('mux: emits no title control for untitled subscriptions', async () => {
  521. const { api } = await boot()
  522. const first = expectOk(await api.sessions.create(request({}))).sessionId
  523. const ac = new AbortController()
  524. const stream = api.events.mux(request({}), ac.signal)[Symbol.asyncIterator]()
  525. expect((await nextMux(stream)).payload).toMatchObject({ type: 'session/subscribed', sessionId: first })
  526. const second = expectOk(await api.sessions.create(request({}))).sessionId
  527. expect((await nextMux(stream)).payload).toMatchObject({ type: 'session/subscribed', sessionId: second })
  528. ac.abort()
  529. })
  530. it('host: session lifecycle, status flips (disposed suppressed), and agent errors', async () => {
  531. const running = await boot([textResponse('x')])
  532. const { api, ctx } = running
  533. const ac = new AbortController()
  534. const stream = api.events.host(request({}), ac.signal)[Symbol.asyncIterator]()
  535. const { sessionId } = expectOk(await api.sessions.create(request({})))
  536. const added = await stream.next()
  537. expect((added.value as RpcRequest<HostFrame>).payload).toMatchObject({ type: 'host/session-added', sessionId })
  538. const agent = ctx.agents.get(sessionId) as Agent
  539. const idle = waitForIdle(ctx, agent)
  540. agent.followup([{ type: 'text', text: 'run' }])
  541. await idle
  542. const runningFrame = await stream.next()
  543. expect((runningFrame.value as RpcRequest<HostFrame>).payload).toMatchObject({ type: 'host/session-status', running: true })
  544. const idleFrame = await stream.next()
  545. expect((idleFrame.value as RpcRequest<HostFrame>).payload).toMatchObject({ type: 'host/session-status', running: false })
  546. // Raw ctx.emit lacks the scope carrier the mounted invariants plugin now
  547. // enforces; dispatch the way the loop does.
  548. agentEvents(ctx, agent).emit('agent/error', 1, 1, new Error('boom'))
  549. const errorFrame = await stream.next()
  550. expect((errorFrame.value as RpcRequest<HostFrame>).payload).toMatchObject({ type: 'host/agent-error', message: 'Error: boom' })
  551. ac.abort()
  552. // Push-after-done: an event landing between abort and generator wind-down
  553. // must be dropped silently, not crash the queue.
  554. agentEvents(ctx, agent).emit('agent/error', 1, 1, new Error('late'))
  555. expect((await stream.next()).done).toBe(true)
  556. })
  557. })
  558. describe('question request / response', () => {
  559. const questions = [{
  560. id: 'mode', question: 'Choose a mode',
  561. options: [
  562. { label: 'Fast (Recommended)', description: 'Move quickly.' },
  563. { label: 'Careful', description: 'Review first.' },
  564. ],
  565. }]
  566. it('waits, replays the same rpcId on reconnect, validates, and resolves first-wins', async () => {
  567. const running = await boot()
  568. const { api, ctx } = running
  569. const { sessionId } = expectOk(await api.sessions.create(request({})))
  570. const agent = ctx.agents.get(sessionId) as Agent
  571. const ac = new AbortController()
  572. const stream = api.events.mux(request({}), ac.signal)[Symbol.asyncIterator]()
  573. await stream.next() // subscribed baseline starts the generator and installs the queue
  574. const answerPromise = ctx.userInteraction.ask({ questions, agent })
  575. const requested = (await stream.next()).value as RpcRequest<MuxFrame>
  576. expect(requested.payload).toMatchObject({ type: 'question/requested', sessionId, questions })
  577. const wrongSession = await api.respond({
  578. type: 'client-response', rpcId: requested.rpcId,
  579. result: {
  580. ok: true,
  581. value: { sessionId: 'session-other', answer: { answers: [{ id: 'mode', selected: ['Fast (Recommended)'] }] } },
  582. },
  583. })
  584. expect(wrongSession).toEqual({ accepted: false, reason: 'bad-response' })
  585. const badChoice = await api.respond({
  586. type: 'client-response', rpcId: requested.rpcId,
  587. result: {
  588. ok: true,
  589. value: { sessionId, answer: { answers: [{ id: 'mode', selected: ['Unknown'] }] } },
  590. },
  591. })
  592. expect(badChoice).toEqual({ accepted: false, reason: 'bad-response' })
  593. const invalidResults = [
  594. { ok: true as const, value: null },
  595. { ok: true as const, value: { sessionId, answer: { answers: [] } } },
  596. { ok: true as const, value: { sessionId, answer: { answers: [{ id: 'wrong', selected: ['Fast (Recommended)'] }] } } },
  597. { ok: true as const, value: { sessionId, answer: { answers: [{ id: 'mode', selected: ['Fast (Recommended)', 'Fast (Recommended)'] }] } } },
  598. { ok: true as const, value: { sessionId, answer: { answers: [{ id: 'mode', selected: ['Fast (Recommended)', 'Careful'] }] } } },
  599. { ok: true as const, value: { sessionId, answer: { answers: [{ id: 'mode', selected: [], custom: ' ' }] } } },
  600. { ok: true as const, value: { sessionId, answer: { answers: [{ id: 'mode', selected: ['Careful'], custom: 'Other' }] } } },
  601. { ok: false as const, error: { code: 'internal' as const, message: 'wrong error', details: {} } },
  602. ]
  603. for (const result of invalidResults) {
  604. expect(await api.respond({
  605. type: 'client-response', rpcId: requested.rpcId, result,
  606. })).toEqual({ accepted: false, reason: 'bad-response' })
  607. }
  608. const reconnectAbort = new AbortController()
  609. const replay = api.events.mux(request({}), reconnectAbort.signal)[Symbol.asyncIterator]()
  610. await replay.next()
  611. const replayed = (await replay.next()).value as RpcRequest<MuxFrame>
  612. expect(replayed.rpcId).toBe(requested.rpcId)
  613. expect(replayed.payload).toEqual(requested.payload)
  614. const response = {
  615. type: 'client-response' as const,
  616. rpcId: requested.rpcId,
  617. result: {
  618. ok: true as const,
  619. value: { sessionId, answer: { answers: [{ id: 'mode', selected: ['Fast (Recommended)'] }] } },
  620. },
  621. }
  622. const [first, duplicate] = await Promise.all([api.respond(response), api.respond(response)])
  623. expect([first, duplicate]).toContainEqual({ accepted: true })
  624. expect([first, duplicate]).toContainEqual({ accepted: false, reason: 'not-pending' })
  625. await expect(answerPromise).resolves.toEqual({
  626. answers: [{ id: 'mode', selected: ['Fast (Recommended)'] }],
  627. })
  628. const resolved = (await stream.next()).value as RpcRequest<MuxFrame>
  629. expect(resolved.payload).toMatchObject({
  630. type: 'question/resolved', sessionId, questionRpcId: requested.rpcId, outcome: 'answered',
  631. })
  632. expect(await api.respond(response)).toEqual({ accepted: false, reason: 'not-pending' })
  633. const customQuestions = [{ id: 'detail', question: 'What else?' }]
  634. const customAnswer = ctx.userInteraction.ask({ questions: customQuestions, agent })
  635. const customRequested = (await stream.next()).value as RpcRequest<MuxFrame>
  636. expect(await api.respond({
  637. type: 'client-response', rpcId: customRequested.rpcId,
  638. result: {
  639. ok: true,
  640. value: { sessionId, answer: { answers: [{ id: 'detail', selected: [], custom: 'Keep traces' }] } },
  641. },
  642. })).toEqual({ accepted: true })
  643. await expect(customAnswer).resolves.toEqual({
  644. answers: [{ id: 'detail', selected: [], custom: 'Keep traces' }],
  645. })
  646. expect(((await stream.next()).value as RpcRequest<MuxFrame>).payload).toMatchObject({
  647. type: 'question/resolved', questionRpcId: customRequested.rpcId, outcome: 'answered',
  648. })
  649. const blankAnswer = ctx.userInteraction.ask({ questions, agent })
  650. const blankRequested = (await stream.next()).value as RpcRequest<MuxFrame>
  651. expect(await api.respond({
  652. type: 'client-response', rpcId: blankRequested.rpcId,
  653. result: {
  654. ok: true,
  655. value: { sessionId, answer: { answers: [{ id: 'mode', selected: [] }] } },
  656. },
  657. })).toEqual({ accepted: true })
  658. await expect(blankAnswer).resolves.toEqual({
  659. answers: [{ id: 'mode', selected: [] }],
  660. })
  661. expect(((await stream.next()).value as RpcRequest<MuxFrame>).payload).toMatchObject({
  662. type: 'question/resolved', questionRpcId: blankRequested.rpcId, outcome: 'answered',
  663. })
  664. ac.abort()
  665. reconnectAbort.abort()
  666. })
  667. it('distinguishes user cancellation from owner abort and rejects late responses', async () => {
  668. const running = await boot()
  669. const { api, ctx } = running
  670. const { sessionId } = expectOk(await api.sessions.create(request({})))
  671. const agent = ctx.agents.get(sessionId) as Agent
  672. const streamAbort = new AbortController()
  673. const stream = api.events.mux(request({}), streamAbort.signal)[Symbol.asyncIterator]()
  674. await stream.next()
  675. const cancelled = ctx.userInteraction.ask({ questions, agent }).catch((error: unknown) => error)
  676. const requested = (await stream.next()).value as RpcRequest<MuxFrame>
  677. expect(await api.respond({
  678. type: 'client-response', rpcId: requested.rpcId,
  679. result: { ok: false, error: { code: 'cancelled', message: 'skip', details: {} } },
  680. })).toEqual({ accepted: true })
  681. await expect(cancelled).resolves.toMatchObject({ code: 'ASK_CANCELLED' })
  682. expect(((await stream.next()).value as RpcRequest<MuxFrame>).payload).toMatchObject({
  683. type: 'question/resolved', outcome: 'cancelled',
  684. })
  685. const ownerAbort = new AbortController()
  686. const aborted = ctx.userInteraction.ask({ questions, agent, signal: ownerAbort.signal })
  687. .catch((error: unknown) => error)
  688. const abortRequest = (await stream.next()).value as RpcRequest<MuxFrame>
  689. ownerAbort.abort()
  690. await expect(aborted).resolves.toMatchObject({ code: 'ASK_ABORTED' })
  691. expect(((await stream.next()).value as RpcRequest<MuxFrame>).payload).toMatchObject({
  692. type: 'question/resolved', questionRpcId: abortRequest.rpcId, outcome: 'cancelled',
  693. })
  694. expect(await api.respond({
  695. type: 'client-response', rpcId: abortRequest.rpcId,
  696. result: { ok: false, error: { code: 'cancelled', message: 'late', details: {} } },
  697. })).toEqual({ accepted: false, reason: 'not-pending' })
  698. streamAbort.abort()
  699. })
  700. it('rejects missing routing and pre-abort, then aborts outstanding waits on disposal', async () => {
  701. const running = await boot()
  702. const { ctx } = running
  703. await expect(ctx.userInteraction.ask({ questions })).rejects.toMatchObject({ code: 'ASK_MISSING_AGENT' })
  704. const { sessionId } = expectOk(await running.api.sessions.create(request({})))
  705. const agent = ctx.agents.get(sessionId) as Agent
  706. const alreadyAborted = new AbortController()
  707. alreadyAborted.abort()
  708. await expect(ctx.userInteraction.ask({ questions, agent, signal: alreadyAborted.signal }))
  709. .rejects.toMatchObject({ code: 'ASK_ABORTED' })
  710. const outstanding = ctx.userInteraction.ask({ questions, agent })
  711. const disposed = running.dispose()
  712. host = undefined
  713. await expect(outstanding).rejects.toMatchObject({ code: 'ASK_ABORTED' })
  714. await disposed
  715. })
  716. })