stdio.spec.ts 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. import { Readable, Writable } from 'node:stream'
  2. import { describe, expect, it, vi } from 'vitest'
  3. import { Context } from 'cordis'
  4. import type { Agent, AgentStatus } from '@deepseek-ai/dsh-agent'
  5. import AgentRegistry from '@deepseek-ai/dsh-agent'
  6. import type { ContentBlock, StreamChunk } from '@deepseek-ai/dsh-llm'
  7. import { SessionId, type Session, type SessionEvent } from '@deepseek-ai/dsh-session'
  8. import UserInteractionService from '@deepseek-ai/dsh-user-interaction'
  9. import { createStdioChat, mountStdio, type Config, type StdioRuntime } from '../src/index.ts'
  10. /**
  11. * Unit tests for the stdio UI plugin. They drive the REAL plugin body
  12. * (`createStdioChat`) with an injected {@link StdioRuntime} so every render,
  13. * input, EOF, and disposal branch runs without touching the real `process`
  14. * streams — the I/O seam is what makes the per-file gate reachable. The
  15. * `agents` service is real (`@deepseek-ai/dsh-agent`); a minimal fake `Agent`
  16. * stands in for the loop, since the loop is the genuinely expensive collaborator
  17. * and we only need its `status` + `send`/`steer` surface here.
  18. */
  19. /** A controllable stdin: a Readable we push lines into and can end on demand. */
  20. function makeInput(): Readable & { feed(line: string): void; finish(): void } {
  21. const stream = new Readable({ read() {} }) as Readable & { feed(line: string): void; finish(): void }
  22. stream.feed = (line: string) => stream.push(`${line}\n`)
  23. stream.finish = () => stream.push(null)
  24. return stream
  25. }
  26. /** A stdout sink that accumulates everything written, for assertions. */
  27. function makeOutput(): { write: (s: string) => boolean; text: () => string } {
  28. let buf = ''
  29. return { write: (s: string) => { buf += s; return true }, text: () => buf }
  30. }
  31. function makeRuntime(over: Partial<StdioRuntime> = {}): {
  32. runtime: StdioRuntime
  33. input: ReturnType<typeof makeInput>
  34. out: ReturnType<typeof makeOutput>
  35. exit: ReturnType<typeof vi.fn>
  36. } {
  37. const input = makeInput()
  38. const out = makeOutput()
  39. const exit = vi.fn()
  40. return { runtime: { input, output: { write: out.write } as never, exit, ...over }, input, out, exit }
  41. }
  42. /** A minimal Agent fake exposing the surface the UI touches. */
  43. function makeAgent(id: string, status: AgentStatus = 'idle'): Agent & {
  44. status: AgentStatus
  45. sent: ContentBlock[][]
  46. steered: ContentBlock[][]
  47. } {
  48. const sent: ContentBlock[][] = []
  49. const steered: ContentBlock[][] = []
  50. return {
  51. id: id as Agent['id'],
  52. status,
  53. sent,
  54. steered,
  55. // A minimal session stub with the agent's shared durable identity.
  56. session: { id, header: { id } },
  57. send: (content: ContentBlock[]) => void sent.push(content),
  58. steer: (content: ContentBlock[]) => void steered.push(content),
  59. } as never
  60. }
  61. /** Register a fake configured agent and cross the supported startup-work boundary. */
  62. function registerReady(ctx: Context, agent: Agent, source: 'startup' | 'resume' = 'startup'): () => void {
  63. const dispose = ctx.agents.register(agent)
  64. ctx.emit('agent/session-start', agent, source)
  65. return dispose
  66. }
  67. /** A session stub whose `header.id` matches an agent's, for `session/event` emits. */
  68. function makeSession(id: string): Session {
  69. return { id, header: { id } } as Session
  70. }
  71. /** An `assistant/chunk` session event carrying one raw stream chunk. */
  72. function chunkEvent(chunk: StreamChunk): SessionEvent {
  73. return { type: 'assistant/chunk', seq: 0, time: 0, data: { turn: 1, step: 0, chunk } }
  74. }
  75. const CONFIG: Config = { welcome: 'hi there', sessionId: 'main' }
  76. function unrenderableFailure(): unknown {
  77. return { [Symbol.toPrimitive](): never { throw new Error('coercion escaped') } }
  78. }
  79. async function setup(config: Config = CONFIG, runtimeOver: Partial<StdioRuntime> = {}) {
  80. const ctx = new Context()
  81. await ctx.plugin(AgentRegistry)
  82. await ctx.plugin(UserInteractionService)
  83. const { runtime, input, out, exit } = makeRuntime(runtimeOver)
  84. const fiber = await ctx.plugin(Object.assign((inner: Context) => {
  85. createStdioChat(inner, config, runtime)
  86. }, { inject: ['agents', 'userInteraction'] }))
  87. return { ctx, fiber, input, out, exit }
  88. }
  89. /** Drive a fake idle timer past the 200ms flush delay. */
  90. function flushExit(): Promise<void> {
  91. return new Promise(resolve => setTimeout(resolve, 250))
  92. }
  93. describe('mountStdio readiness', () => {
  94. it('opens before the configured agent is created so startup input can queue', async () => {
  95. const ctx = new Context()
  96. await ctx.plugin(AgentRegistry)
  97. await ctx.plugin(UserInteractionService)
  98. const { runtime, out } = makeRuntime()
  99. const fiber = await ctx.plugin(Object.assign((inner: Context) => {
  100. mountStdio(inner, CONFIG, runtime)
  101. }, { inject: ['agents', 'userInteraction'] }))
  102. expect(out.text()).toBe('hi there\n> ')
  103. ctx.agents.register(makeAgent('other'))
  104. expect(out.text()).toBe('hi there\n> ')
  105. ctx.agents.register(makeAgent('main'))
  106. expect(out.text()).toBe('hi there\n> ')
  107. await fiber.dispose()
  108. })
  109. it('opens immediately when the configured agent already exists', async () => {
  110. const ctx = new Context()
  111. await ctx.plugin(AgentRegistry)
  112. await ctx.plugin(UserInteractionService)
  113. ctx.agents.register(makeAgent('main'))
  114. const { runtime, out } = makeRuntime()
  115. const fiber = await ctx.plugin(Object.assign((inner: Context) => {
  116. mountStdio(inner, CONFIG, runtime)
  117. }, { inject: ['agents', 'userInteraction'] }))
  118. expect(out.text()).toBe('hi there\n> ')
  119. await fiber.dispose()
  120. })
  121. it('opens for the default main identity when no target is configured', async () => {
  122. const ctx = new Context()
  123. await ctx.plugin(AgentRegistry)
  124. await ctx.plugin(UserInteractionService)
  125. const { runtime, out } = makeRuntime()
  126. const fiber = await ctx.plugin(Object.assign((inner: Context) => {
  127. mountStdio(inner, { welcome: 'ready' }, runtime)
  128. }, { inject: ['agents', 'userInteraction'] }))
  129. expect(out.text()).toBe('ready\n> ')
  130. ctx.agents.register(makeAgent('other'))
  131. expect(out.text()).toBe('ready\n> ')
  132. ctx.agents.register(makeAgent('main'))
  133. expect(out.text()).toBe('ready\n> ')
  134. await fiber.dispose()
  135. })
  136. })
  137. describe('createStdioChat rendering', () => {
  138. it('writes the welcome banner and prompt on start', async () => {
  139. const { out } = await setup()
  140. expect(out.text()).toBe('hi there\n> ')
  141. })
  142. it('falls back to the default welcome when called with empty config', async () => {
  143. // createStdioChat is exported and may be driven directly (bypassing the
  144. // Loader's schemastery validation), so it must default the welcome itself.
  145. const { out } = await setup({})
  146. expect(out.text()).toBe('ready.\n> ')
  147. })
  148. it('detects readline terminal mode from both stream TTY flags', async () => {
  149. for (const [inputTTY, outputTTY] of [[true, false], [true, true]] as const) {
  150. const ctx = new Context()
  151. await ctx.plugin(AgentRegistry)
  152. await ctx.plugin(UserInteractionService)
  153. let text = ''
  154. const output = new Writable({
  155. write(chunk, _encoding, callback) {
  156. text += String(chunk)
  157. callback()
  158. },
  159. }) as Writable & { isTTY?: boolean }
  160. const { runtime } = makeRuntime({ output })
  161. ;(runtime.input as Readable & { isTTY?: boolean }).isTTY = inputTTY
  162. output.isTTY = outputTTY
  163. const fiber = await ctx.plugin(Object.assign((inner: Context) => {
  164. createStdioChat(inner, CONFIG, runtime)
  165. }, { inject: ['agents', 'userInteraction'] }))
  166. expect(text).toContain('hi there')
  167. await fiber.dispose()
  168. }
  169. })
  170. it('renders text-delta chunks verbatim', async () => {
  171. const { ctx, out } = await setup()
  172. ctx.emit('session/event', makeSession('main'), chunkEvent({ type: 'text-delta', index: 0, text: 'hello' }))
  173. expect(out.text()).toContain('hello')
  174. })
  175. it('wraps reasoning-delta in the dim SGR and resets on the following text-delta', async () => {
  176. const { ctx, out } = await setup()
  177. const session = makeSession('main')
  178. ctx.emit('session/event', session, chunkEvent({ type: 'reasoning-delta', index: 0, text: 'think' }))
  179. ctx.emit('session/event', session, chunkEvent({ type: 'reasoning-delta', index: 0, text: 'more' }))
  180. ctx.emit('session/event', session, chunkEvent({ type: 'text-delta', index: 0, text: 'answer' }))
  181. expect(out.text()).toContain('\x1B[2mthinkmore\x1B[0m\nanswer')
  182. })
  183. it('ignores stream-chunk types it does not render', async () => {
  184. const { ctx, out } = await setup()
  185. const before = out.text()
  186. ctx.emit('session/event', makeSession('main'), chunkEvent({ type: 'block-start', index: 0, blockType: 'text' }))
  187. expect(out.text()).toBe(before)
  188. })
  189. it('renders turn/start and turn/end markers from the session feed', async () => {
  190. const { ctx, out } = await setup()
  191. const agent = makeAgent('main')
  192. ctx.agents.register(agent)
  193. const session = agent.session
  194. ctx.emit('session/event', session, {
  195. type: 'turn/start', seq: 1, time: 0, data: { turn: 3, trigger: { kind: 'message' } },
  196. } as SessionEvent)
  197. expect(out.text()).toContain('[main turn 3] ')
  198. ctx.emit('session/event', session, {
  199. type: 'turn/end', seq: 2, time: 0, data: { turn: 3, reason: { kind: 'completed' } },
  200. } as SessionEvent)
  201. expect(out.text()).toContain('\n> ')
  202. })
  203. it('renders failure turn/end reasons so a failed turn is not silent', async () => {
  204. const { ctx, out } = await setup()
  205. const session = makeSession('main')
  206. ctx.emit('session/event', session, {
  207. type: 'turn/end', seq: 1, time: 0,
  208. data: { turn: 1, reason: { kind: 'error', step: 1, message: 'fetch failed: connect ECONNREFUSED', code: 'NETWORK' } },
  209. } as SessionEvent)
  210. expect(out.text()).toContain('[turn failed NETWORK] fetch failed: connect ECONNREFUSED')
  211. ctx.emit('session/event', session, {
  212. type: 'turn/end', seq: 2, time: 0,
  213. data: { turn: 2, reason: { kind: 'error', step: 1, message: 'uncoded failure' } },
  214. } as SessionEvent)
  215. expect(out.text()).toContain('[turn failed] uncoded failure')
  216. ctx.emit('session/event', session, {
  217. type: 'turn/end', seq: 3, time: 0, data: { turn: 3, reason: { kind: 'aborted', reason: 'user cancelled' } },
  218. } as SessionEvent)
  219. expect(out.text()).toContain('[turn aborted] user cancelled')
  220. ctx.emit('session/event', session, {
  221. type: 'turn/end', seq: 4, time: 0, data: { turn: 4, reason: { kind: 'aborted' } },
  222. } as SessionEvent)
  223. expect(out.text()).toContain('[turn aborted]\n> ')
  224. ctx.emit('session/event', session, {
  225. type: 'turn/end', seq: 5, time: 0, data: { turn: 5, reason: { kind: 'rejected', reason: 'policy veto' } },
  226. } as SessionEvent)
  227. expect(out.text()).toContain('[turn rejected] policy veto')
  228. ctx.emit('session/event', session, {
  229. type: 'turn/end', seq: 6, time: 0, data: { turn: 6, reason: { kind: 'max-tokens' } },
  230. } as SessionEvent)
  231. expect(out.text()).toContain('[turn hit the output-token limit]')
  232. ctx.emit('session/event', session, {
  233. type: 'turn/end', seq: 7, time: 0, data: { turn: 7, reason: { kind: 'interrupted' } },
  234. } as SessionEvent)
  235. expect(out.text()).toContain('[turn interrupted by a previous process exit]')
  236. })
  237. it('uses the session id as the label for a non-target session', async () => {
  238. const { ctx, out } = await setup()
  239. // No target exists, so the event's durable identity is the label.
  240. ctx.emit('session/event', makeSession('orphan'), {
  241. type: 'turn/start', seq: 1, time: 0, data: { turn: 1, trigger: { kind: 'message' } },
  242. } as SessionEvent)
  243. expect(out.text()).toContain('[orphan turn 1] ')
  244. })
  245. it('uses an agent already registered before the UI installs as its target', async () => {
  246. // The pre-created `main` agent (and any agent surviving an HMR reload of just
  247. // this fiber) fired its `agent/created` before the UI's listener existed, so
  248. // the live listener alone would miss it. Seeding from `ctx.agents.list()` at
  249. // install time preserves the terminal's fixed `[main turn N]` label.
  250. const ctx = new Context()
  251. await ctx.plugin(AgentRegistry)
  252. await ctx.plugin(UserInteractionService)
  253. const agent = makeAgent('main')
  254. // Durable lineage does not imply runtime child ownership: the stdio app
  255. // may explicitly resume a persisted fork as its one configured agent.
  256. ;(agent.session.header as { parentSession?: string }).parentSession = 'persisted-parent'
  257. ctx.agents.register(agent) // registered BEFORE the UI plugin below
  258. const { runtime, out } = makeRuntime()
  259. await ctx.plugin(Object.assign((inner: Context) => {
  260. createStdioChat(inner, CONFIG, runtime)
  261. }, { inject: ['agents', 'userInteraction'] }))
  262. ctx.emit('session/event', agent.session, {
  263. type: 'turn/start', seq: 1, time: 0, data: { turn: 5, trigger: { kind: 'message' } },
  264. } as SessionEvent)
  265. expect(out.text()).toContain('[main turn 5] ')
  266. })
  267. it('buffers input for a lineage-bearing configured agent until its session starts', async () => {
  268. const { ctx, input } = await setup({ welcome: 'hi there', sessionId: 'resumed' })
  269. input.feed('continue')
  270. await new Promise(resolve => setImmediate(resolve))
  271. const unrelated = makeAgent('unrelated')
  272. ctx.agents.register(unrelated)
  273. ctx.emit('agent/session-start', unrelated, 'startup')
  274. const resumed = makeAgent('resumed')
  275. ;(resumed.session.header as { parentSession?: string }).parentSession = 'persisted-parent'
  276. ctx.agents.register(resumed)
  277. await new Promise(resolve => setImmediate(resolve))
  278. expect(resumed.sent).toEqual([])
  279. ctx.emit('agent/session-start', resumed, 'resume')
  280. await new Promise(resolve => setImmediate(resolve))
  281. expect(unrelated.sent).toEqual([])
  282. expect(resumed.sent).toEqual([[{ type: 'text', text: 'continue' }]])
  283. })
  284. it('resets dim styling at turn/end if a turn ends mid-reasoning', async () => {
  285. const { ctx, out } = await setup()
  286. const session = makeSession('main')
  287. ctx.emit('session/event', session, chunkEvent({ type: 'reasoning-delta', index: 0, text: 'mid' }))
  288. ctx.emit('session/event', session, {
  289. type: 'turn/end', seq: 1, time: 0, data: { turn: 1, reason: { kind: 'completed' } },
  290. } as SessionEvent)
  291. expect(out.text()).toContain('\x1B[2mmid\x1B[0m')
  292. })
  293. it('drops the target object on agent/disposed', async () => {
  294. const { ctx, out } = await setup()
  295. const agent = makeAgent('main')
  296. const dispose = ctx.agents.register(agent)
  297. dispose()
  298. // After disposal the event belongs to a non-target session, so its durable
  299. // identity is rendered directly.
  300. ctx.emit('session/event', agent.session, {
  301. type: 'turn/start', seq: 1, time: 0, data: { turn: 1, trigger: { kind: 'message' } },
  302. } as SessionEvent)
  303. expect(out.text()).toContain('[main turn 1] ')
  304. })
  305. it('keeps the target when a different agent is disposed', async () => {
  306. const { ctx, out } = await setup()
  307. const target = makeAgent('main')
  308. ctx.agents.register(target)
  309. ctx.emit('agent/disposed', makeAgent('other'))
  310. ctx.emit('session/event', target.session, {
  311. type: 'turn/start', seq: 1, time: 0, data: { turn: 1, trigger: { kind: 'message' } },
  312. } as SessionEvent)
  313. expect(out.text()).toContain('[main turn 1] ')
  314. })
  315. it('retargets only the exact identity after loop HMR recreation', async () => {
  316. const { ctx, input } = await setup({ welcome: 'hi there', sessionId: 'main-session-fixed' })
  317. const oldRoot = makeAgent('main-session-fixed')
  318. const prefixCollision = makeAgent('main-session-unrelated')
  319. const disposeOld = ctx.agents.register(oldRoot)
  320. ctx.agents.register(prefixCollision)
  321. disposeOld()
  322. const replacement = makeAgent('main-session-fixed')
  323. ctx.agents.register(replacement)
  324. input.feed('after hmr')
  325. await new Promise(resolve => setImmediate(resolve))
  326. expect(replacement.sent).toEqual([])
  327. ctx.emit('agent/session-start', replacement, 'resume')
  328. await new Promise(resolve => setImmediate(resolve))
  329. expect(prefixCollision.sent).toEqual([])
  330. expect(replacement.sent).toEqual([[{ type: 'text', text: 'after hmr' }]])
  331. })
  332. it('does not retarget stdin to an unrelated root after the configured agent is disposed', async () => {
  333. const { ctx, input } = await setup()
  334. const unrelated = makeAgent('unrelated')
  335. ctx.agents.register(unrelated)
  336. const configured = makeAgent('main')
  337. const disposeConfigured = registerReady(ctx, configured)
  338. const error = vi.spyOn(ctx.logger, 'error').mockImplementation(() => {})
  339. disposeConfigured()
  340. input.feed('must not leak')
  341. await new Promise(resolve => setImmediate(resolve))
  342. expect(unrelated.sent).toEqual([])
  343. expect(error).toHaveBeenCalledWith('ui-stdio: main agent is not running')
  344. })
  345. it('renders tool/call and tool/result session events', async () => {
  346. const { ctx, out } = await setup()
  347. const session = {} as Session
  348. const callEvent = {
  349. type: 'tool/call', seq: 1, time: 0,
  350. data: { turn: 1, step: 0, callId: 'c1', name: 'bash', arguments: '{"command":"ls"}' },
  351. } as SessionEvent
  352. ctx.emit('session/event', session, callEvent)
  353. expect(out.text()).toContain('[tool call] bash({"command":"ls"})')
  354. const resultEvent = {
  355. type: 'tool/result', seq: 2, time: 0,
  356. data: { turn: 1, step: 0, callId: 'c1', content: [{ type: 'text', text: 'file.txt' }], isError: false },
  357. } as SessionEvent
  358. ctx.emit('session/event', session, resultEvent)
  359. expect(out.text()).toContain('[tool result] file.txt')
  360. })
  361. it('renders a todo/write session event as a glyphed checklist', async () => {
  362. const { ctx, out } = await setup()
  363. const session = {} as Session
  364. ctx.emit('session/event', session, {
  365. type: 'todo/write', seq: 1, time: 0,
  366. data: { todos: [
  367. { content: 'read the code', status: 'completed' },
  368. { content: 'write the fix', status: 'in_progress' },
  369. { content: 'run the tests', status: 'pending' },
  370. ] },
  371. } as SessionEvent)
  372. const text = out.text()
  373. expect(text).toContain('[todos]')
  374. expect(text).toContain('[x] read the code')
  375. expect(text).toContain('[~] write the fix')
  376. expect(text).toContain('[ ] run the tests')
  377. })
  378. it('resets dim styling when a todo/write interrupts reasoning', async () => {
  379. const { ctx, out } = await setup()
  380. ctx.emit('session/event', {} as Session, chunkEvent({ type: 'reasoning-delta', index: 0, text: 'r' }))
  381. ctx.emit('session/event', {} as Session, {
  382. type: 'todo/write', seq: 1, time: 0,
  383. data: { todos: [{ content: 'a task', status: 'pending' }] },
  384. } as SessionEvent)
  385. expect(out.text()).toContain('\x1B[2mr\x1B[0m')
  386. })
  387. it('resets dim styling when a tool/call interrupts reasoning', async () => {
  388. const { ctx, out } = await setup()
  389. const session = {} as Session
  390. ctx.emit('session/event', session, chunkEvent({ type: 'reasoning-delta', index: 0, text: 'r' }))
  391. ctx.emit('session/event', session, {
  392. type: 'tool/call', seq: 1, time: 0,
  393. data: { turn: 1, step: 0, callId: 'c1', name: 'bash', arguments: '{}' },
  394. } as SessionEvent)
  395. expect(out.text()).toContain('\x1B[2mr\x1B[0m')
  396. })
  397. it('ignores session events it does not render', async () => {
  398. const { ctx, out } = await setup()
  399. const before = out.text()
  400. ctx.emit('session/event', {} as Session, {
  401. type: 'user/message', seq: 1, time: 0,
  402. data: { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } },
  403. } as SessionEvent)
  404. expect(out.text()).toBe(before)
  405. })
  406. })
  407. describe('createStdioChat input', () => {
  408. it('answers a pending user question instead of sending the line to the agent', async () => {
  409. const { ctx, input, out } = await setup()
  410. const agent = makeAgent('main', 'idle')
  411. ctx.agents.register(agent)
  412. const answer = ctx.userInteraction.ask({
  413. questions: [{
  414. id: 'confirm',
  415. header: 'Confirm',
  416. question: 'Proceed with the edit?',
  417. options: [{ label: 'Yes', description: 'Apply the edit now.' }],
  418. }],
  419. })
  420. await new Promise(r => setImmediate(r))
  421. input.feed('Use a smaller change')
  422. await expect(answer).resolves.toEqual({ answers: [{ id: 'confirm', selected: [], custom: 'Use a smaller change' }] })
  423. expect(agent.sent).toEqual([])
  424. expect(out.text()).toContain('[Confirm] Proceed with the edit?')
  425. expect(out.text()).toContain('1. Yes')
  426. expect(out.text()).toContain('Apply the edit now.')
  427. })
  428. it('answers a pending user question by numeric option selection', async () => {
  429. const { ctx, input } = await setup()
  430. const answer = ctx.userInteraction.ask({
  431. questions: [{
  432. id: 'mode',
  433. question: 'Which mode?',
  434. options: [
  435. { label: 'Safe' },
  436. { label: 'Fast' },
  437. ],
  438. }],
  439. })
  440. await new Promise(r => setImmediate(r))
  441. input.feed('2')
  442. await expect(answer).resolves.toEqual({
  443. answers: [{ id: 'mode', selected: ['Fast'] }],
  444. })
  445. })
  446. it('renders options in input order and selects by displayed number', async () => {
  447. const { ctx, input, out } = await setup()
  448. const answer = ctx.userInteraction.ask({
  449. questions: [{
  450. id: 'topic',
  451. question: 'Which topic?',
  452. options: [
  453. { label: 'Hobbies' },
  454. { label: 'Work', description: 'Questions about current projects.' },
  455. { label: 'Casual', description: 'Easy conversation.' },
  456. ],
  457. }],
  458. })
  459. await new Promise(r => setImmediate(r))
  460. expect(out.text()).toContain([
  461. 'Which topic?',
  462. ' 1. Hobbies',
  463. ' 2. Work',
  464. ' Questions about current projects.',
  465. ' 3. Casual',
  466. ' Easy conversation.',
  467. ].join('\n'))
  468. input.feed('3')
  469. await expect(answer).resolves.toEqual({
  470. answers: [{ id: 'topic', selected: ['Casual'] }],
  471. })
  472. })
  473. it('answers a multi-select question with multiple numeric selections', async () => {
  474. const { ctx, input } = await setup()
  475. const answer = ctx.userInteraction.ask({
  476. questions: [{
  477. id: 'targets',
  478. question: 'What should I update?',
  479. options: [{ label: 'Tests' }, { label: 'Docs' }, { label: 'Code' }],
  480. multiSelect: true,
  481. }],
  482. })
  483. await new Promise(r => setImmediate(r))
  484. input.feed('1 1, 3')
  485. await expect(answer).resolves.toEqual({
  486. answers: [{ id: 'targets', selected: ['Tests', 'Code'] }],
  487. })
  488. })
  489. it('accepts non-numeric multi-select input as a custom answer', async () => {
  490. const { ctx, input } = await setup()
  491. const answer = ctx.userInteraction.ask({
  492. questions: [{
  493. id: 'targets',
  494. question: 'What should I update?',
  495. options: [{ label: 'Tests' }, { label: 'Docs' }],
  496. multiSelect: true,
  497. }],
  498. })
  499. await new Promise(r => setImmediate(r))
  500. input.feed('the release notes')
  501. await expect(answer).resolves.toEqual({
  502. answers: [{ id: 'targets', selected: [], custom: 'the release notes' }],
  503. })
  504. })
  505. it('asks every question in a batch and returns answers by id', async () => {
  506. const { ctx, input, out } = await setup()
  507. const answer = ctx.userInteraction.ask({
  508. questions: [
  509. { id: 'language', question: 'Which language?', options: [{ label: 'Python' }, { label: 'TypeScript' }] },
  510. { id: 'note', question: 'Any note?' },
  511. ],
  512. })
  513. await new Promise(r => setImmediate(r))
  514. input.feed('2')
  515. await new Promise(r => setImmediate(r))
  516. expect(out.text()).toContain('\nAny note?\n')
  517. input.feed('ship today')
  518. await expect(answer).resolves.toEqual({
  519. answers: [
  520. { id: 'language', selected: ['TypeScript'] },
  521. { id: 'note', selected: [], custom: 'ship today' },
  522. ],
  523. })
  524. })
  525. it('re-prompts when option input is invalid', async () => {
  526. const { ctx, input, out } = await setup()
  527. const answer = ctx.userInteraction.ask({
  528. questions: [{
  529. id: 'mode',
  530. question: 'Which mode?',
  531. options: [{ label: 'Safe' }],
  532. multiSelect: true,
  533. }],
  534. })
  535. await new Promise(r => setImmediate(r))
  536. input.feed('2')
  537. await new Promise(r => setImmediate(r))
  538. expect(out.text()).toContain('Please enter one of the option numbers (comma or space separated) or a custom answer.')
  539. input.feed('1')
  540. await expect(answer).resolves.toEqual({
  541. answers: [{ id: 'mode', selected: ['Safe'] }],
  542. })
  543. })
  544. it('re-prompts when single-select option input is out of range', async () => {
  545. const { ctx, input, out } = await setup()
  546. const answer = ctx.userInteraction.ask({
  547. questions: [{
  548. id: 'mode',
  549. question: 'Which mode?',
  550. options: [{ label: 'Safe' }],
  551. }],
  552. })
  553. await new Promise(r => setImmediate(r))
  554. input.feed('2')
  555. await new Promise(r => setImmediate(r))
  556. expect(out.text()).toContain('Please enter one of the option numbers or a custom answer.')
  557. input.feed('1')
  558. await expect(answer).resolves.toEqual({
  559. answers: [{ id: 'mode', selected: ['Safe'] }],
  560. })
  561. })
  562. it('re-prompts when multi-select input contains no option numbers', async () => {
  563. const { ctx, input, out } = await setup()
  564. const answer = ctx.userInteraction.ask({
  565. questions: [{
  566. id: 'mode',
  567. question: 'Which mode?',
  568. options: [{ label: 'Safe' }],
  569. multiSelect: true,
  570. }],
  571. })
  572. await new Promise(r => setImmediate(r))
  573. input.feed(',')
  574. await new Promise(r => setImmediate(r))
  575. expect(out.text()).toContain('Please enter one of the option numbers (comma or space separated) or a custom answer.')
  576. input.feed('1')
  577. await expect(answer).resolves.toEqual({
  578. answers: [{ id: 'mode', selected: ['Safe'] }],
  579. })
  580. })
  581. it('re-prompts when an option question receives an empty answer', async () => {
  582. const { ctx, input, out } = await setup()
  583. const answer = ctx.userInteraction.ask({
  584. questions: [{
  585. id: 'mode',
  586. question: 'Which mode?',
  587. options: [{ label: 'Safe' }],
  588. }],
  589. })
  590. await new Promise(r => setImmediate(r))
  591. input.feed('')
  592. await new Promise(r => setImmediate(r))
  593. expect(out.text()).toContain('Please enter one of the option numbers or a custom answer.')
  594. input.feed('1')
  595. await expect(answer).resolves.toEqual({
  596. answers: [{ id: 'mode', selected: ['Safe'] }],
  597. })
  598. })
  599. it('re-prompts when a question receives an empty answer', async () => {
  600. const { ctx, input, out } = await setup()
  601. const answer = ctx.userInteraction.ask({ questions: [{ id: 'path', question: 'What should I use?' }] })
  602. await new Promise(r => setImmediate(r))
  603. input.feed('')
  604. await new Promise(r => setImmediate(r))
  605. expect(out.text()).toContain('Please enter an answer.')
  606. input.feed('Use defaults')
  607. await expect(answer).resolves.toEqual({ answers: [{ id: 'path', selected: [], custom: 'Use defaults' }] })
  608. })
  609. it('rejects an active question when its signal aborts', async () => {
  610. const { ctx } = await setup()
  611. const controller = new AbortController()
  612. const answer = ctx.userInteraction.ask({ questions: [{ id: 'continue', question: 'Continue?' }], signal: controller.signal })
  613. const rejected = expect(answer).rejects.toMatchObject({ code: 'ASK_ABORTED' })
  614. await new Promise(r => setImmediate(r))
  615. controller.abort()
  616. await rejected
  617. })
  618. it('continues to the next queued question when the active question aborts', async () => {
  619. const { ctx, input, out } = await setup()
  620. const controller = new AbortController()
  621. const first = ctx.userInteraction.ask({ questions: [{ id: 'first', question: 'First?' }], signal: controller.signal })
  622. const firstRejected = expect(first).rejects.toMatchObject({ code: 'ASK_ABORTED' })
  623. const second = ctx.userInteraction.ask({ questions: [{ id: 'second', question: 'Second?' }] })
  624. await new Promise(r => setImmediate(r))
  625. controller.abort()
  626. await firstRejected
  627. await new Promise(r => setImmediate(r))
  628. expect(out.text()).toContain('\nSecond?\n')
  629. input.feed('second answer')
  630. await expect(second).resolves.toEqual({ answers: [{ id: 'second', selected: [], custom: 'second answer' }] })
  631. })
  632. it('skips a queued question whose signal aborted before it became active', async () => {
  633. const { ctx, input, out } = await setup()
  634. const controller = new AbortController()
  635. const first = ctx.userInteraction.ask({ questions: [{ id: 'first', question: 'First?' }] })
  636. const second = ctx.userInteraction.ask({ questions: [{ id: 'second', question: 'Second?' }], signal: controller.signal })
  637. await new Promise(r => setImmediate(r))
  638. controller.abort()
  639. await expect(Promise.race([
  640. second.then(
  641. () => 'resolved',
  642. (error: unknown) => (error as { code?: string }).code,
  643. ),
  644. new Promise<string>((resolve) => { setImmediate(() => { resolve('pending') }) }),
  645. ])).resolves.toBe('ASK_ABORTED')
  646. expect(out.text()).not.toContain('\nSecond?\n')
  647. input.feed('first answer')
  648. await expect(first).resolves.toEqual({ answers: [{ id: 'first', selected: [], custom: 'first answer' }] })
  649. })
  650. it('removes an aborted queued question without promoting later queued work early', async () => {
  651. const { ctx, input, out } = await setup()
  652. const controller = new AbortController()
  653. const first = ctx.userInteraction.ask({ questions: [{ id: 'first', question: 'First?' }] })
  654. const second = ctx.userInteraction.ask({ questions: [{ id: 'second', question: 'Second?' }], signal: controller.signal })
  655. const third = ctx.userInteraction.ask({ questions: [{ id: 'third', question: 'Third?' }] })
  656. await new Promise(r => setImmediate(r))
  657. controller.abort()
  658. await expect(second).rejects.toMatchObject({ code: 'ASK_ABORTED' })
  659. expect(out.text()).toContain('\nFirst?\n')
  660. expect(out.text()).not.toContain('\nSecond?\n')
  661. expect(out.text()).not.toContain('\nThird?\n')
  662. input.feed('first answer')
  663. await new Promise(r => setImmediate(r))
  664. expect(out.text()).toContain('\nThird?\n')
  665. input.feed('third answer')
  666. await expect(first).resolves.toEqual({ answers: [{ id: 'first', selected: [], custom: 'first answer' }] })
  667. await expect(third).resolves.toEqual({ answers: [{ id: 'third', selected: [], custom: 'third answer' }] })
  668. })
  669. it('rejects active and queued questions when the UI is disposed', async () => {
  670. const { ctx, fiber } = await setup()
  671. const active = ctx.userInteraction.ask({ questions: [{ id: 'active', question: 'Active?' }] })
  672. const queued = ctx.userInteraction.ask({ questions: [{ id: 'queued', question: 'Queued?' }] })
  673. const activeRejected = expect(active).rejects.toMatchObject({ code: 'ASK_ABORTED' })
  674. const queuedRejected = expect(queued).rejects.toMatchObject({ code: 'ASK_ABORTED' })
  675. await new Promise(r => setImmediate(r))
  676. await fiber.dispose()
  677. await activeRejected
  678. await queuedRejected
  679. })
  680. it('rejects active and queued questions when stdin closes before the user answers', async () => {
  681. const { ctx, input, exit } = await setup()
  682. const active = ctx.userInteraction.ask({ questions: [{ id: 'active', question: 'Active?' }] })
  683. const queued = ctx.userInteraction.ask({ questions: [{ id: 'queued', question: 'Queued?' }] })
  684. const activeRejected = expect(active).rejects.toMatchObject({ code: 'ASK_ABORTED' })
  685. const queuedRejected = expect(queued).rejects.toMatchObject({ code: 'ASK_ABORTED' })
  686. await new Promise(r => setImmediate(r))
  687. input.finish()
  688. await new Promise(r => setImmediate(r))
  689. await activeRejected
  690. await queuedRejected
  691. expect(exit).not.toHaveBeenCalled()
  692. })
  693. it('rejects new questions immediately after stdin has closed', async () => {
  694. const { ctx, input, out } = await setup()
  695. input.finish()
  696. await new Promise(r => setImmediate(r))
  697. const before = out.text()
  698. const answer = ctx.userInteraction.ask({ questions: [{ id: 'late', question: 'Too late?' }] })
  699. await expect(answer).rejects.toMatchObject({ code: 'ASK_ABORTED' })
  700. expect(out.text()).toBe(before)
  701. })
  702. it('sends a typed line to an idle agent', async () => {
  703. const { ctx, input } = await setup()
  704. const agent = makeAgent('main', 'idle')
  705. registerReady(ctx, agent)
  706. input.feed('do a thing')
  707. await new Promise(r => setImmediate(r))
  708. expect(agent.sent).toEqual([[{ type: 'text', text: 'do a thing' }]])
  709. expect(agent.steered).toEqual([])
  710. })
  711. it('steers a typed line into a running agent', async () => {
  712. const { ctx, input } = await setup()
  713. const agent = makeAgent('main', 'running')
  714. registerReady(ctx, agent)
  715. input.feed('steer me')
  716. await new Promise(r => setImmediate(r))
  717. expect(agent.steered).toEqual([[{ type: 'text', text: 'steer me' }]])
  718. expect(agent.sent).toEqual([])
  719. })
  720. it('ignores blank lines', async () => {
  721. const { ctx, input } = await setup()
  722. const agent = makeAgent('main')
  723. ctx.agents.register(agent)
  724. input.feed(' ')
  725. await new Promise(r => setImmediate(r))
  726. expect(agent.sent).toEqual([])
  727. })
  728. it('buffers a line until the initial target session starts', async () => {
  729. const { ctx, input } = await setup()
  730. const spy = vi.spyOn(ctx.logger, 'error').mockImplementation(() => {})
  731. input.feed('nobody home')
  732. await new Promise(r => setImmediate(r))
  733. expect(spy).not.toHaveBeenCalled()
  734. const agent = makeAgent('main')
  735. ctx.agents.register(agent)
  736. await new Promise(r => setImmediate(r))
  737. expect(agent.sent).toEqual([])
  738. ctx.emit('agent/session-start', agent, 'startup')
  739. await new Promise(r => setImmediate(r))
  740. expect(agent.sent).toEqual([[{ type: 'text', text: 'nobody home' }]])
  741. })
  742. it('drops later input after the configured startup fails', async () => {
  743. const { ctx, input } = await setup()
  744. const error = vi.spyOn(ctx.logger, 'error').mockImplementation(() => {})
  745. const failure = unrenderableFailure()
  746. ctx.emit('agent-loop/config-start-failed', SessionId('main'), failure)
  747. input.feed('cannot run')
  748. await new Promise(r => setImmediate(r))
  749. expect(error).toHaveBeenCalledWith(
  750. 'ui-stdio: main agent failed to start; dropped queued stdin (1 line(s)): <unrenderable value>',
  751. )
  752. })
  753. it('ignores a stale config-start failure after the exact target is ready', async () => {
  754. const { ctx, input } = await setup()
  755. const agent = makeAgent('main')
  756. registerReady(ctx, agent)
  757. ctx.emit('agent-loop/config-start-failed', SessionId('main'), new Error('stale'))
  758. input.feed('still live')
  759. await new Promise(r => setImmediate(r))
  760. expect(agent.sent).toEqual([[{ type: 'text', text: 'still live' }]])
  761. })
  762. it('drives the exact app-configured resumed session', async () => {
  763. const { ctx, input } = await setup({ welcome: 'w', sessionId: 'worker' })
  764. const agent = makeAgent('worker')
  765. registerReady(ctx, agent, 'resume')
  766. input.feed('hi')
  767. await new Promise(r => setImmediate(r))
  768. expect(agent.sent).toHaveLength(1)
  769. })
  770. })
  771. describe('createStdioChat EOF exit', () => {
  772. it('exits immediately on EOF when no work was submitted', async () => {
  773. const { input, exit } = await setup()
  774. input.finish()
  775. await flushExit()
  776. expect(exit).toHaveBeenCalledWith(0)
  777. })
  778. it('waits for the agent to settle idle after running before exiting', async () => {
  779. const { ctx, input, exit } = await setup()
  780. const agent = makeAgent('main', 'idle')
  781. registerReady(ctx, agent)
  782. input.feed('work')
  783. await new Promise(r => setImmediate(r))
  784. input.finish()
  785. await new Promise(r => setImmediate(r))
  786. // Work submitted but no 'running' observed yet — must NOT exit.
  787. expect(exit).not.toHaveBeenCalled()
  788. // The turn starts, then settles.
  789. ctx.emit('agent/status', agent, 'running')
  790. ;(agent as { status: AgentStatus }).status = 'idle'
  791. ctx.emit('agent/status', agent, 'idle')
  792. await flushExit()
  793. expect(exit).toHaveBeenCalledWith(0)
  794. })
  795. it('keeps piped EOF pending until buffered startup input runs', async () => {
  796. const { ctx, input, exit } = await setup()
  797. input.feed('work')
  798. input.finish()
  799. await flushExit()
  800. expect(exit).not.toHaveBeenCalled()
  801. const agent = makeAgent('main', 'idle')
  802. ctx.agents.register(agent)
  803. await new Promise(r => setImmediate(r))
  804. expect(agent.sent).toEqual([])
  805. ctx.emit('agent/session-start', agent, 'startup')
  806. await new Promise(r => setImmediate(r))
  807. expect(agent.sent).toEqual([[{ type: 'text', text: 'work' }]])
  808. ctx.emit('agent/status', agent, 'running')
  809. ;(agent as { status: AgentStatus }).status = 'idle'
  810. ctx.emit('agent/status', agent, 'idle')
  811. await flushExit()
  812. expect(exit).toHaveBeenCalledWith(0)
  813. })
  814. it('drains buffered piped input and exits when configured startup fails', async () => {
  815. const { ctx, input, exit } = await setup()
  816. const error = vi.spyOn(ctx.logger, 'error').mockImplementation(() => {})
  817. input.feed('work')
  818. input.finish()
  819. await new Promise(r => setImmediate(r))
  820. ctx.emit('agent-loop/config-start-failed', SessionId('other'), new Error('unrelated'))
  821. await flushExit()
  822. expect(exit).not.toHaveBeenCalled()
  823. ctx.emit('agent-loop/config-start-failed', SessionId('main'), unrenderableFailure())
  824. await flushExit()
  825. expect(error).toHaveBeenCalledWith(
  826. 'ui-stdio: main agent failed to start; dropped queued stdin (1 line(s)): <unrenderable value>',
  827. )
  828. expect(exit).toHaveBeenCalledWith(0)
  829. })
  830. it('schedules the exit only once when idle fires repeatedly', async () => {
  831. const { ctx, input, exit } = await setup()
  832. const agent = makeAgent('main', 'running')
  833. registerReady(ctx, agent)
  834. input.feed('work')
  835. await new Promise(r => setImmediate(r))
  836. ctx.emit('agent/status', agent, 'running') // sawRunning = true
  837. input.finish()
  838. await new Promise(r => setImmediate(r)) // let readline 'close' set stdinClosed
  839. ;(agent as { status: AgentStatus }).status = 'idle'
  840. // Two idle signals while stdin is already closed: the first arms the timer,
  841. // the second must hit the already-scheduled guard, not arm a second.
  842. ctx.emit('agent/status', agent, 'idle')
  843. ctx.emit('agent/status', agent, 'idle')
  844. await flushExit()
  845. expect(exit).toHaveBeenCalledTimes(1)
  846. })
  847. it('does not exit on an idle transition for a different agent', async () => {
  848. const { ctx, input, exit } = await setup()
  849. const agent = makeAgent('main', 'idle')
  850. registerReady(ctx, agent)
  851. input.feed('work')
  852. await new Promise(r => setImmediate(r))
  853. input.finish()
  854. const other = makeAgent('other')
  855. ctx.emit('agent/status', other, 'running')
  856. ctx.emit('agent/status', other, 'idle')
  857. await flushExit()
  858. expect(exit).not.toHaveBeenCalled()
  859. })
  860. it('does not exit while a turn is still running at EOF', async () => {
  861. const { ctx, input, exit } = await setup()
  862. const agent = makeAgent('main', 'idle')
  863. registerReady(ctx, agent)
  864. input.feed('work')
  865. await new Promise(r => setImmediate(r))
  866. ctx.emit('agent/status', agent, 'running')
  867. ;(agent as { status: AgentStatus }).status = 'running'
  868. input.finish()
  869. // sawRunning is true, but the agent is still running — the idle gate holds.
  870. ctx.emit('agent/status', agent, 'idle') // a stale/duplicate signal while status stays 'running'
  871. await flushExit()
  872. expect(exit).not.toHaveBeenCalled()
  873. })
  874. })
  875. describe('createStdioChat disposal (HMR safety)', () => {
  876. it('never exits the process when EOF arrives after fiber dispose', async () => {
  877. const { fiber, input, exit } = await setup()
  878. await fiber.dispose()
  879. // A late EOF after disposal (reader.close() also fires 'close') must not exit.
  880. input.finish()
  881. await flushExit()
  882. expect(exit).not.toHaveBeenCalled()
  883. })
  884. it('cancels a scheduled exit if disposed within the flush window', async () => {
  885. const { fiber, input, exit } = await setup()
  886. // EOF with no work submitted schedules the 200ms flush-then-exit timer.
  887. input.finish()
  888. await new Promise(r => setImmediate(r))
  889. expect(exit).not.toHaveBeenCalled() // not yet — still inside the window
  890. // Dispose BEFORE the timer fires: the tracked handle must be cleared.
  891. await fiber.dispose()
  892. await flushExit()
  893. expect(exit).not.toHaveBeenCalled()
  894. })
  895. it('stops handling input after dispose', async () => {
  896. const { ctx, fiber, input } = await setup()
  897. const agent = makeAgent('main')
  898. ctx.agents.register(agent)
  899. await fiber.dispose()
  900. // The readline interface is closed on dispose; a late line reaches no handler.
  901. input.feed('too late')
  902. await new Promise(r => setImmediate(r))
  903. expect(agent.sent).toEqual([])
  904. })
  905. it('removes the agent/status listener on dispose', async () => {
  906. const { ctx, fiber, input, exit } = await setup()
  907. const agent = makeAgent('main', 'idle')
  908. registerReady(ctx, agent)
  909. input.feed('work')
  910. await new Promise(r => setImmediate(r))
  911. await fiber.dispose()
  912. // After dispose, status transitions must neither throw nor schedule an exit
  913. // (the listener and the EOF-exit path are both torn down).
  914. expect(() => {
  915. ctx.emit('agent/status', agent, 'running')
  916. ctx.emit('agent/status', agent, 'idle')
  917. }).not.toThrow()
  918. await flushExit()
  919. expect(exit).not.toHaveBeenCalled()
  920. })
  921. })