stdio.spec.ts 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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('uses the session id as the label for a non-target session', async () => {
  204. const { ctx, out } = await setup()
  205. // No target exists, so the event's durable identity is the label.
  206. ctx.emit('session/event', makeSession('orphan'), {
  207. type: 'turn/start', seq: 1, time: 0, data: { turn: 1, trigger: { kind: 'message' } },
  208. } as SessionEvent)
  209. expect(out.text()).toContain('[orphan turn 1] ')
  210. })
  211. it('uses an agent already registered before the UI installs as its target', async () => {
  212. // The pre-created `main` agent (and any agent surviving an HMR reload of just
  213. // this fiber) fired its `agent/created` before the UI's listener existed, so
  214. // the live listener alone would miss it. Seeding from `ctx.agents.list()` at
  215. // install time preserves the terminal's fixed `[main turn N]` label.
  216. const ctx = new Context()
  217. await ctx.plugin(AgentRegistry)
  218. await ctx.plugin(UserInteractionService)
  219. const agent = makeAgent('main')
  220. // Durable lineage does not imply runtime child ownership: the stdio app
  221. // may explicitly resume a persisted fork as its one configured agent.
  222. ;(agent.session.header as { parentSession?: string }).parentSession = 'persisted-parent'
  223. ctx.agents.register(agent) // registered BEFORE the UI plugin below
  224. const { runtime, out } = makeRuntime()
  225. await ctx.plugin(Object.assign((inner: Context) => {
  226. createStdioChat(inner, CONFIG, runtime)
  227. }, { inject: ['agents', 'userInteraction'] }))
  228. ctx.emit('session/event', agent.session, {
  229. type: 'turn/start', seq: 1, time: 0, data: { turn: 5, trigger: { kind: 'message' } },
  230. } as SessionEvent)
  231. expect(out.text()).toContain('[main turn 5] ')
  232. })
  233. it('buffers input for a lineage-bearing configured agent until its session starts', async () => {
  234. const { ctx, input } = await setup({ welcome: 'hi there', sessionId: 'resumed' })
  235. input.feed('continue')
  236. await new Promise(resolve => setImmediate(resolve))
  237. const unrelated = makeAgent('unrelated')
  238. ctx.agents.register(unrelated)
  239. ctx.emit('agent/session-start', unrelated, 'startup')
  240. const resumed = makeAgent('resumed')
  241. ;(resumed.session.header as { parentSession?: string }).parentSession = 'persisted-parent'
  242. ctx.agents.register(resumed)
  243. await new Promise(resolve => setImmediate(resolve))
  244. expect(resumed.sent).toEqual([])
  245. ctx.emit('agent/session-start', resumed, 'resume')
  246. await new Promise(resolve => setImmediate(resolve))
  247. expect(unrelated.sent).toEqual([])
  248. expect(resumed.sent).toEqual([[{ type: 'text', text: 'continue' }]])
  249. })
  250. it('resets dim styling at turn/end if a turn ends mid-reasoning', async () => {
  251. const { ctx, out } = await setup()
  252. const session = makeSession('main')
  253. ctx.emit('session/event', session, chunkEvent({ type: 'reasoning-delta', index: 0, text: 'mid' }))
  254. ctx.emit('session/event', session, {
  255. type: 'turn/end', seq: 1, time: 0, data: { turn: 1, reason: { kind: 'completed' } },
  256. } as SessionEvent)
  257. expect(out.text()).toContain('\x1B[2mmid\x1B[0m')
  258. })
  259. it('drops the target object on agent/disposed', async () => {
  260. const { ctx, out } = await setup()
  261. const agent = makeAgent('main')
  262. const dispose = ctx.agents.register(agent)
  263. dispose()
  264. // After disposal the event belongs to a non-target session, so its durable
  265. // identity is rendered directly.
  266. ctx.emit('session/event', agent.session, {
  267. type: 'turn/start', seq: 1, time: 0, data: { turn: 1, trigger: { kind: 'message' } },
  268. } as SessionEvent)
  269. expect(out.text()).toContain('[main turn 1] ')
  270. })
  271. it('keeps the target when a different agent is disposed', async () => {
  272. const { ctx, out } = await setup()
  273. const target = makeAgent('main')
  274. ctx.agents.register(target)
  275. ctx.emit('agent/disposed', makeAgent('other'))
  276. ctx.emit('session/event', target.session, {
  277. type: 'turn/start', seq: 1, time: 0, data: { turn: 1, trigger: { kind: 'message' } },
  278. } as SessionEvent)
  279. expect(out.text()).toContain('[main turn 1] ')
  280. })
  281. it('retargets only the exact identity after loop HMR recreation', async () => {
  282. const { ctx, input } = await setup({ welcome: 'hi there', sessionId: 'main-session-fixed' })
  283. const oldRoot = makeAgent('main-session-fixed')
  284. const prefixCollision = makeAgent('main-session-unrelated')
  285. const disposeOld = ctx.agents.register(oldRoot)
  286. ctx.agents.register(prefixCollision)
  287. disposeOld()
  288. const replacement = makeAgent('main-session-fixed')
  289. ctx.agents.register(replacement)
  290. input.feed('after hmr')
  291. await new Promise(resolve => setImmediate(resolve))
  292. expect(replacement.sent).toEqual([])
  293. ctx.emit('agent/session-start', replacement, 'resume')
  294. await new Promise(resolve => setImmediate(resolve))
  295. expect(prefixCollision.sent).toEqual([])
  296. expect(replacement.sent).toEqual([[{ type: 'text', text: 'after hmr' }]])
  297. })
  298. it('does not retarget stdin to an unrelated root after the configured agent is disposed', async () => {
  299. const { ctx, input } = await setup()
  300. const unrelated = makeAgent('unrelated')
  301. ctx.agents.register(unrelated)
  302. const configured = makeAgent('main')
  303. const disposeConfigured = registerReady(ctx, configured)
  304. const error = vi.spyOn(ctx.logger, 'error').mockImplementation(() => {})
  305. disposeConfigured()
  306. input.feed('must not leak')
  307. await new Promise(resolve => setImmediate(resolve))
  308. expect(unrelated.sent).toEqual([])
  309. expect(error).toHaveBeenCalledWith('ui-stdio: main agent is not running')
  310. })
  311. it('renders tool/call and tool/result session events', async () => {
  312. const { ctx, out } = await setup()
  313. const session = {} as Session
  314. const callEvent = {
  315. type: 'tool/call', seq: 1, time: 0,
  316. data: { turn: 1, step: 0, callId: 'c1', name: 'bash', arguments: '{"command":"ls"}' },
  317. } as SessionEvent
  318. ctx.emit('session/event', session, callEvent)
  319. expect(out.text()).toContain('[tool call] bash({"command":"ls"})')
  320. const resultEvent = {
  321. type: 'tool/result', seq: 2, time: 0,
  322. data: { turn: 1, step: 0, callId: 'c1', content: [{ type: 'text', text: 'file.txt' }], isError: false },
  323. } as SessionEvent
  324. ctx.emit('session/event', session, resultEvent)
  325. expect(out.text()).toContain('[tool result] file.txt')
  326. })
  327. it('renders a todo/write session event as a glyphed checklist', async () => {
  328. const { ctx, out } = await setup()
  329. const session = {} as Session
  330. ctx.emit('session/event', session, {
  331. type: 'todo/write', seq: 1, time: 0,
  332. data: { todos: [
  333. { content: 'read the code', status: 'completed' },
  334. { content: 'write the fix', status: 'in_progress' },
  335. { content: 'run the tests', status: 'pending' },
  336. ] },
  337. } as SessionEvent)
  338. const text = out.text()
  339. expect(text).toContain('[todos]')
  340. expect(text).toContain('[x] read the code')
  341. expect(text).toContain('[~] write the fix')
  342. expect(text).toContain('[ ] run the tests')
  343. })
  344. it('resets dim styling when a todo/write interrupts reasoning', async () => {
  345. const { ctx, out } = await setup()
  346. ctx.emit('session/event', {} as Session, chunkEvent({ type: 'reasoning-delta', index: 0, text: 'r' }))
  347. ctx.emit('session/event', {} as Session, {
  348. type: 'todo/write', seq: 1, time: 0,
  349. data: { todos: [{ content: 'a task', status: 'pending' }] },
  350. } as SessionEvent)
  351. expect(out.text()).toContain('\x1B[2mr\x1B[0m')
  352. })
  353. it('resets dim styling when a tool/call interrupts reasoning', async () => {
  354. const { ctx, out } = await setup()
  355. const session = {} as Session
  356. ctx.emit('session/event', session, chunkEvent({ type: 'reasoning-delta', index: 0, text: 'r' }))
  357. ctx.emit('session/event', session, {
  358. type: 'tool/call', seq: 1, time: 0,
  359. data: { turn: 1, step: 0, callId: 'c1', name: 'bash', arguments: '{}' },
  360. } as SessionEvent)
  361. expect(out.text()).toContain('\x1B[2mr\x1B[0m')
  362. })
  363. it('ignores session events it does not render', async () => {
  364. const { ctx, out } = await setup()
  365. const before = out.text()
  366. ctx.emit('session/event', {} as Session, {
  367. type: 'user/message', seq: 1, time: 0,
  368. data: { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } },
  369. } as SessionEvent)
  370. expect(out.text()).toBe(before)
  371. })
  372. })
  373. describe('createStdioChat input', () => {
  374. it('answers a pending user question instead of sending the line to the agent', async () => {
  375. const { ctx, input, out } = await setup()
  376. const agent = makeAgent('main', 'idle')
  377. ctx.agents.register(agent)
  378. const answer = ctx.userInteraction.ask({
  379. questions: [{
  380. id: 'confirm',
  381. header: 'Confirm',
  382. question: 'Proceed with the edit?',
  383. options: [{ label: 'Yes', description: 'Apply the edit now.' }],
  384. }],
  385. })
  386. await new Promise(r => setImmediate(r))
  387. input.feed('Use a smaller change')
  388. await expect(answer).resolves.toEqual({ answers: [{ id: 'confirm', selected: [], custom: 'Use a smaller change' }] })
  389. expect(agent.sent).toEqual([])
  390. expect(out.text()).toContain('[Confirm] Proceed with the edit?')
  391. expect(out.text()).toContain('1. Yes')
  392. expect(out.text()).toContain('Apply the edit now.')
  393. })
  394. it('answers a pending user question by numeric option selection', async () => {
  395. const { ctx, input } = await setup()
  396. const answer = ctx.userInteraction.ask({
  397. questions: [{
  398. id: 'mode',
  399. question: 'Which mode?',
  400. options: [
  401. { label: 'Safe' },
  402. { label: 'Fast' },
  403. ],
  404. }],
  405. })
  406. await new Promise(r => setImmediate(r))
  407. input.feed('2')
  408. await expect(answer).resolves.toEqual({
  409. answers: [{ id: 'mode', selected: ['Fast'] }],
  410. })
  411. })
  412. it('renders options in input order and selects by displayed number', async () => {
  413. const { ctx, input, out } = await setup()
  414. const answer = ctx.userInteraction.ask({
  415. questions: [{
  416. id: 'topic',
  417. question: 'Which topic?',
  418. options: [
  419. { label: 'Hobbies' },
  420. { label: 'Work', description: 'Questions about current projects.' },
  421. { label: 'Casual', description: 'Easy conversation.' },
  422. ],
  423. }],
  424. })
  425. await new Promise(r => setImmediate(r))
  426. expect(out.text()).toContain([
  427. 'Which topic?',
  428. ' 1. Hobbies',
  429. ' 2. Work',
  430. ' Questions about current projects.',
  431. ' 3. Casual',
  432. ' Easy conversation.',
  433. ].join('\n'))
  434. input.feed('3')
  435. await expect(answer).resolves.toEqual({
  436. answers: [{ id: 'topic', selected: ['Casual'] }],
  437. })
  438. })
  439. it('answers a multi-select question with multiple numeric selections', async () => {
  440. const { ctx, input } = await setup()
  441. const answer = ctx.userInteraction.ask({
  442. questions: [{
  443. id: 'targets',
  444. question: 'What should I update?',
  445. options: [{ label: 'Tests' }, { label: 'Docs' }, { label: 'Code' }],
  446. multiSelect: true,
  447. }],
  448. })
  449. await new Promise(r => setImmediate(r))
  450. input.feed('1 1, 3')
  451. await expect(answer).resolves.toEqual({
  452. answers: [{ id: 'targets', selected: ['Tests', 'Code'] }],
  453. })
  454. })
  455. it('accepts non-numeric multi-select input as a custom answer', async () => {
  456. const { ctx, input } = await setup()
  457. const answer = ctx.userInteraction.ask({
  458. questions: [{
  459. id: 'targets',
  460. question: 'What should I update?',
  461. options: [{ label: 'Tests' }, { label: 'Docs' }],
  462. multiSelect: true,
  463. }],
  464. })
  465. await new Promise(r => setImmediate(r))
  466. input.feed('the release notes')
  467. await expect(answer).resolves.toEqual({
  468. answers: [{ id: 'targets', selected: [], custom: 'the release notes' }],
  469. })
  470. })
  471. it('asks every question in a batch and returns answers by id', async () => {
  472. const { ctx, input, out } = await setup()
  473. const answer = ctx.userInteraction.ask({
  474. questions: [
  475. { id: 'language', question: 'Which language?', options: [{ label: 'Python' }, { label: 'TypeScript' }] },
  476. { id: 'note', question: 'Any note?' },
  477. ],
  478. })
  479. await new Promise(r => setImmediate(r))
  480. input.feed('2')
  481. await new Promise(r => setImmediate(r))
  482. expect(out.text()).toContain('\nAny note?\n')
  483. input.feed('ship today')
  484. await expect(answer).resolves.toEqual({
  485. answers: [
  486. { id: 'language', selected: ['TypeScript'] },
  487. { id: 'note', selected: [], custom: 'ship today' },
  488. ],
  489. })
  490. })
  491. it('re-prompts when option input is invalid', async () => {
  492. const { ctx, input, out } = await setup()
  493. const answer = ctx.userInteraction.ask({
  494. questions: [{
  495. id: 'mode',
  496. question: 'Which mode?',
  497. options: [{ label: 'Safe' }],
  498. multiSelect: true,
  499. }],
  500. })
  501. await new Promise(r => setImmediate(r))
  502. input.feed('2')
  503. await new Promise(r => setImmediate(r))
  504. expect(out.text()).toContain('Please enter one of the option numbers (comma or space separated) or a custom answer.')
  505. input.feed('1')
  506. await expect(answer).resolves.toEqual({
  507. answers: [{ id: 'mode', selected: ['Safe'] }],
  508. })
  509. })
  510. it('re-prompts when single-select option input is out of range', async () => {
  511. const { ctx, input, out } = await setup()
  512. const answer = ctx.userInteraction.ask({
  513. questions: [{
  514. id: 'mode',
  515. question: 'Which mode?',
  516. options: [{ label: 'Safe' }],
  517. }],
  518. })
  519. await new Promise(r => setImmediate(r))
  520. input.feed('2')
  521. await new Promise(r => setImmediate(r))
  522. expect(out.text()).toContain('Please enter one of the option numbers or a custom answer.')
  523. input.feed('1')
  524. await expect(answer).resolves.toEqual({
  525. answers: [{ id: 'mode', selected: ['Safe'] }],
  526. })
  527. })
  528. it('re-prompts when multi-select input contains no option numbers', async () => {
  529. const { ctx, input, out } = await setup()
  530. const answer = ctx.userInteraction.ask({
  531. questions: [{
  532. id: 'mode',
  533. question: 'Which mode?',
  534. options: [{ label: 'Safe' }],
  535. multiSelect: true,
  536. }],
  537. })
  538. await new Promise(r => setImmediate(r))
  539. input.feed(',')
  540. await new Promise(r => setImmediate(r))
  541. expect(out.text()).toContain('Please enter one of the option numbers (comma or space separated) or a custom answer.')
  542. input.feed('1')
  543. await expect(answer).resolves.toEqual({
  544. answers: [{ id: 'mode', selected: ['Safe'] }],
  545. })
  546. })
  547. it('re-prompts when an option question receives an empty answer', async () => {
  548. const { ctx, input, out } = await setup()
  549. const answer = ctx.userInteraction.ask({
  550. questions: [{
  551. id: 'mode',
  552. question: 'Which mode?',
  553. options: [{ label: 'Safe' }],
  554. }],
  555. })
  556. await new Promise(r => setImmediate(r))
  557. input.feed('')
  558. await new Promise(r => setImmediate(r))
  559. expect(out.text()).toContain('Please enter one of the option numbers or a custom answer.')
  560. input.feed('1')
  561. await expect(answer).resolves.toEqual({
  562. answers: [{ id: 'mode', selected: ['Safe'] }],
  563. })
  564. })
  565. it('re-prompts when a question receives an empty answer', async () => {
  566. const { ctx, input, out } = await setup()
  567. const answer = ctx.userInteraction.ask({ questions: [{ id: 'path', question: 'What should I use?' }] })
  568. await new Promise(r => setImmediate(r))
  569. input.feed('')
  570. await new Promise(r => setImmediate(r))
  571. expect(out.text()).toContain('Please enter an answer.')
  572. input.feed('Use defaults')
  573. await expect(answer).resolves.toEqual({ answers: [{ id: 'path', selected: [], custom: 'Use defaults' }] })
  574. })
  575. it('rejects an active question when its signal aborts', async () => {
  576. const { ctx } = await setup()
  577. const controller = new AbortController()
  578. const answer = ctx.userInteraction.ask({ questions: [{ id: 'continue', question: 'Continue?' }], signal: controller.signal })
  579. const rejected = expect(answer).rejects.toMatchObject({ code: 'ASK_ABORTED' })
  580. await new Promise(r => setImmediate(r))
  581. controller.abort()
  582. await rejected
  583. })
  584. it('continues to the next queued question when the active question aborts', async () => {
  585. const { ctx, input, out } = await setup()
  586. const controller = new AbortController()
  587. const first = ctx.userInteraction.ask({ questions: [{ id: 'first', question: 'First?' }], signal: controller.signal })
  588. const firstRejected = expect(first).rejects.toMatchObject({ code: 'ASK_ABORTED' })
  589. const second = ctx.userInteraction.ask({ questions: [{ id: 'second', question: 'Second?' }] })
  590. await new Promise(r => setImmediate(r))
  591. controller.abort()
  592. await firstRejected
  593. await new Promise(r => setImmediate(r))
  594. expect(out.text()).toContain('\nSecond?\n')
  595. input.feed('second answer')
  596. await expect(second).resolves.toEqual({ answers: [{ id: 'second', selected: [], custom: 'second answer' }] })
  597. })
  598. it('skips a queued question whose signal aborted before it became active', async () => {
  599. const { ctx, input, out } = await setup()
  600. const controller = new AbortController()
  601. const first = ctx.userInteraction.ask({ questions: [{ id: 'first', question: 'First?' }] })
  602. const second = ctx.userInteraction.ask({ questions: [{ id: 'second', question: 'Second?' }], signal: controller.signal })
  603. await new Promise(r => setImmediate(r))
  604. controller.abort()
  605. await expect(Promise.race([
  606. second.then(
  607. () => 'resolved',
  608. (error: unknown) => (error as { code?: string }).code,
  609. ),
  610. new Promise<string>((resolve) => { setImmediate(() => { resolve('pending') }) }),
  611. ])).resolves.toBe('ASK_ABORTED')
  612. expect(out.text()).not.toContain('\nSecond?\n')
  613. input.feed('first answer')
  614. await expect(first).resolves.toEqual({ answers: [{ id: 'first', selected: [], custom: 'first answer' }] })
  615. })
  616. it('removes an aborted queued question without promoting later queued work early', async () => {
  617. const { ctx, input, out } = await setup()
  618. const controller = new AbortController()
  619. const first = ctx.userInteraction.ask({ questions: [{ id: 'first', question: 'First?' }] })
  620. const second = ctx.userInteraction.ask({ questions: [{ id: 'second', question: 'Second?' }], signal: controller.signal })
  621. const third = ctx.userInteraction.ask({ questions: [{ id: 'third', question: 'Third?' }] })
  622. await new Promise(r => setImmediate(r))
  623. controller.abort()
  624. await expect(second).rejects.toMatchObject({ code: 'ASK_ABORTED' })
  625. expect(out.text()).toContain('\nFirst?\n')
  626. expect(out.text()).not.toContain('\nSecond?\n')
  627. expect(out.text()).not.toContain('\nThird?\n')
  628. input.feed('first answer')
  629. await new Promise(r => setImmediate(r))
  630. expect(out.text()).toContain('\nThird?\n')
  631. input.feed('third answer')
  632. await expect(first).resolves.toEqual({ answers: [{ id: 'first', selected: [], custom: 'first answer' }] })
  633. await expect(third).resolves.toEqual({ answers: [{ id: 'third', selected: [], custom: 'third answer' }] })
  634. })
  635. it('rejects active and queued questions when the UI is disposed', async () => {
  636. const { ctx, fiber } = await setup()
  637. const active = ctx.userInteraction.ask({ questions: [{ id: 'active', question: 'Active?' }] })
  638. const queued = ctx.userInteraction.ask({ questions: [{ id: 'queued', question: 'Queued?' }] })
  639. const activeRejected = expect(active).rejects.toMatchObject({ code: 'ASK_ABORTED' })
  640. const queuedRejected = expect(queued).rejects.toMatchObject({ code: 'ASK_ABORTED' })
  641. await new Promise(r => setImmediate(r))
  642. await fiber.dispose()
  643. await activeRejected
  644. await queuedRejected
  645. })
  646. it('rejects active and queued questions when stdin closes before the user answers', async () => {
  647. const { ctx, input, exit } = await setup()
  648. const active = ctx.userInteraction.ask({ questions: [{ id: 'active', question: 'Active?' }] })
  649. const queued = ctx.userInteraction.ask({ questions: [{ id: 'queued', question: 'Queued?' }] })
  650. const activeRejected = expect(active).rejects.toMatchObject({ code: 'ASK_ABORTED' })
  651. const queuedRejected = expect(queued).rejects.toMatchObject({ code: 'ASK_ABORTED' })
  652. await new Promise(r => setImmediate(r))
  653. input.finish()
  654. await new Promise(r => setImmediate(r))
  655. await activeRejected
  656. await queuedRejected
  657. expect(exit).not.toHaveBeenCalled()
  658. })
  659. it('rejects new questions immediately after stdin has closed', async () => {
  660. const { ctx, input, out } = await setup()
  661. input.finish()
  662. await new Promise(r => setImmediate(r))
  663. const before = out.text()
  664. const answer = ctx.userInteraction.ask({ questions: [{ id: 'late', question: 'Too late?' }] })
  665. await expect(answer).rejects.toMatchObject({ code: 'ASK_ABORTED' })
  666. expect(out.text()).toBe(before)
  667. })
  668. it('sends a typed line to an idle agent', async () => {
  669. const { ctx, input } = await setup()
  670. const agent = makeAgent('main', 'idle')
  671. registerReady(ctx, agent)
  672. input.feed('do a thing')
  673. await new Promise(r => setImmediate(r))
  674. expect(agent.sent).toEqual([[{ type: 'text', text: 'do a thing' }]])
  675. expect(agent.steered).toEqual([])
  676. })
  677. it('steers a typed line into a running agent', async () => {
  678. const { ctx, input } = await setup()
  679. const agent = makeAgent('main', 'running')
  680. registerReady(ctx, agent)
  681. input.feed('steer me')
  682. await new Promise(r => setImmediate(r))
  683. expect(agent.steered).toEqual([[{ type: 'text', text: 'steer me' }]])
  684. expect(agent.sent).toEqual([])
  685. })
  686. it('ignores blank lines', async () => {
  687. const { ctx, input } = await setup()
  688. const agent = makeAgent('main')
  689. ctx.agents.register(agent)
  690. input.feed(' ')
  691. await new Promise(r => setImmediate(r))
  692. expect(agent.sent).toEqual([])
  693. })
  694. it('buffers a line until the initial target session starts', async () => {
  695. const { ctx, input } = await setup()
  696. const spy = vi.spyOn(ctx.logger, 'error').mockImplementation(() => {})
  697. input.feed('nobody home')
  698. await new Promise(r => setImmediate(r))
  699. expect(spy).not.toHaveBeenCalled()
  700. const agent = makeAgent('main')
  701. ctx.agents.register(agent)
  702. await new Promise(r => setImmediate(r))
  703. expect(agent.sent).toEqual([])
  704. ctx.emit('agent/session-start', agent, 'startup')
  705. await new Promise(r => setImmediate(r))
  706. expect(agent.sent).toEqual([[{ type: 'text', text: 'nobody home' }]])
  707. })
  708. it('drops later input after the configured startup fails', async () => {
  709. const { ctx, input } = await setup()
  710. const error = vi.spyOn(ctx.logger, 'error').mockImplementation(() => {})
  711. const failure = unrenderableFailure()
  712. ctx.emit('agent-loop/config-start-failed', SessionId('main'), failure)
  713. input.feed('cannot run')
  714. await new Promise(r => setImmediate(r))
  715. expect(error).toHaveBeenCalledWith(
  716. 'ui-stdio: main agent failed to start; dropped queued stdin (1 line(s)): <unrenderable thrown value>',
  717. )
  718. })
  719. it('ignores a stale config-start failure after the exact target is ready', async () => {
  720. const { ctx, input } = await setup()
  721. const agent = makeAgent('main')
  722. registerReady(ctx, agent)
  723. ctx.emit('agent-loop/config-start-failed', SessionId('main'), new Error('stale'))
  724. input.feed('still live')
  725. await new Promise(r => setImmediate(r))
  726. expect(agent.sent).toEqual([[{ type: 'text', text: 'still live' }]])
  727. })
  728. it('drives the exact app-configured resumed session', async () => {
  729. const { ctx, input } = await setup({ welcome: 'w', sessionId: 'worker' })
  730. const agent = makeAgent('worker')
  731. registerReady(ctx, agent, 'resume')
  732. input.feed('hi')
  733. await new Promise(r => setImmediate(r))
  734. expect(agent.sent).toHaveLength(1)
  735. })
  736. })
  737. describe('createStdioChat EOF exit', () => {
  738. it('exits immediately on EOF when no work was submitted', async () => {
  739. const { input, exit } = await setup()
  740. input.finish()
  741. await flushExit()
  742. expect(exit).toHaveBeenCalledWith(0)
  743. })
  744. it('waits for the agent to settle idle after running before exiting', async () => {
  745. const { ctx, input, exit } = await setup()
  746. const agent = makeAgent('main', 'idle')
  747. registerReady(ctx, agent)
  748. input.feed('work')
  749. await new Promise(r => setImmediate(r))
  750. input.finish()
  751. await new Promise(r => setImmediate(r))
  752. // Work submitted but no 'running' observed yet — must NOT exit.
  753. expect(exit).not.toHaveBeenCalled()
  754. // The turn starts, then settles.
  755. ctx.emit('agent/status', agent, 'running')
  756. ;(agent as { status: AgentStatus }).status = 'idle'
  757. ctx.emit('agent/status', agent, 'idle')
  758. await flushExit()
  759. expect(exit).toHaveBeenCalledWith(0)
  760. })
  761. it('keeps piped EOF pending until buffered startup input runs', async () => {
  762. const { ctx, input, exit } = await setup()
  763. input.feed('work')
  764. input.finish()
  765. await flushExit()
  766. expect(exit).not.toHaveBeenCalled()
  767. const agent = makeAgent('main', 'idle')
  768. ctx.agents.register(agent)
  769. await new Promise(r => setImmediate(r))
  770. expect(agent.sent).toEqual([])
  771. ctx.emit('agent/session-start', agent, 'startup')
  772. await new Promise(r => setImmediate(r))
  773. expect(agent.sent).toEqual([[{ type: 'text', text: 'work' }]])
  774. ctx.emit('agent/status', agent, 'running')
  775. ;(agent as { status: AgentStatus }).status = 'idle'
  776. ctx.emit('agent/status', agent, 'idle')
  777. await flushExit()
  778. expect(exit).toHaveBeenCalledWith(0)
  779. })
  780. it('drains buffered piped input and exits when configured startup fails', async () => {
  781. const { ctx, input, exit } = await setup()
  782. const error = vi.spyOn(ctx.logger, 'error').mockImplementation(() => {})
  783. input.feed('work')
  784. input.finish()
  785. await new Promise(r => setImmediate(r))
  786. ctx.emit('agent-loop/config-start-failed', SessionId('other'), new Error('unrelated'))
  787. await flushExit()
  788. expect(exit).not.toHaveBeenCalled()
  789. ctx.emit('agent-loop/config-start-failed', SessionId('main'), unrenderableFailure())
  790. await flushExit()
  791. expect(error).toHaveBeenCalledWith(
  792. 'ui-stdio: main agent failed to start; dropped queued stdin (1 line(s)): <unrenderable thrown value>',
  793. )
  794. expect(exit).toHaveBeenCalledWith(0)
  795. })
  796. it('schedules the exit only once when idle fires repeatedly', async () => {
  797. const { ctx, input, exit } = await setup()
  798. const agent = makeAgent('main', 'running')
  799. registerReady(ctx, agent)
  800. input.feed('work')
  801. await new Promise(r => setImmediate(r))
  802. ctx.emit('agent/status', agent, 'running') // sawRunning = true
  803. input.finish()
  804. await new Promise(r => setImmediate(r)) // let readline 'close' set stdinClosed
  805. ;(agent as { status: AgentStatus }).status = 'idle'
  806. // Two idle signals while stdin is already closed: the first arms the timer,
  807. // the second must hit the already-scheduled guard, not arm a second.
  808. ctx.emit('agent/status', agent, 'idle')
  809. ctx.emit('agent/status', agent, 'idle')
  810. await flushExit()
  811. expect(exit).toHaveBeenCalledTimes(1)
  812. })
  813. it('does not exit on an idle transition for a different agent', async () => {
  814. const { ctx, input, exit } = await setup()
  815. const agent = makeAgent('main', 'idle')
  816. registerReady(ctx, agent)
  817. input.feed('work')
  818. await new Promise(r => setImmediate(r))
  819. input.finish()
  820. const other = makeAgent('other')
  821. ctx.emit('agent/status', other, 'running')
  822. ctx.emit('agent/status', other, 'idle')
  823. await flushExit()
  824. expect(exit).not.toHaveBeenCalled()
  825. })
  826. it('does not exit while a turn is still running at EOF', async () => {
  827. const { ctx, input, exit } = await setup()
  828. const agent = makeAgent('main', 'idle')
  829. registerReady(ctx, agent)
  830. input.feed('work')
  831. await new Promise(r => setImmediate(r))
  832. ctx.emit('agent/status', agent, 'running')
  833. ;(agent as { status: AgentStatus }).status = 'running'
  834. input.finish()
  835. // sawRunning is true, but the agent is still running — the idle gate holds.
  836. ctx.emit('agent/status', agent, 'idle') // a stale/duplicate signal while status stays 'running'
  837. await flushExit()
  838. expect(exit).not.toHaveBeenCalled()
  839. })
  840. })
  841. describe('createStdioChat disposal (HMR safety)', () => {
  842. it('never exits the process when EOF arrives after fiber dispose', async () => {
  843. const { fiber, input, exit } = await setup()
  844. await fiber.dispose()
  845. // A late EOF after disposal (reader.close() also fires 'close') must not exit.
  846. input.finish()
  847. await flushExit()
  848. expect(exit).not.toHaveBeenCalled()
  849. })
  850. it('cancels a scheduled exit if disposed within the flush window', async () => {
  851. const { fiber, input, exit } = await setup()
  852. // EOF with no work submitted schedules the 200ms flush-then-exit timer.
  853. input.finish()
  854. await new Promise(r => setImmediate(r))
  855. expect(exit).not.toHaveBeenCalled() // not yet — still inside the window
  856. // Dispose BEFORE the timer fires: the tracked handle must be cleared.
  857. await fiber.dispose()
  858. await flushExit()
  859. expect(exit).not.toHaveBeenCalled()
  860. })
  861. it('stops handling input after dispose', async () => {
  862. const { ctx, fiber, input } = await setup()
  863. const agent = makeAgent('main')
  864. ctx.agents.register(agent)
  865. await fiber.dispose()
  866. // The readline interface is closed on dispose; a late line reaches no handler.
  867. input.feed('too late')
  868. await new Promise(r => setImmediate(r))
  869. expect(agent.sent).toEqual([])
  870. })
  871. it('removes the agent/status listener on dispose', async () => {
  872. const { ctx, fiber, input, exit } = await setup()
  873. const agent = makeAgent('main', 'idle')
  874. registerReady(ctx, agent)
  875. input.feed('work')
  876. await new Promise(r => setImmediate(r))
  877. await fiber.dispose()
  878. // After dispose, status transitions must neither throw nor schedule an exit
  879. // (the listener and the EOF-exit path are both torn down).
  880. expect(() => {
  881. ctx.emit('agent/status', agent, 'running')
  882. ctx.emit('agent/status', agent, 'idle')
  883. }).not.toThrow()
  884. await flushExit()
  885. expect(exit).not.toHaveBeenCalled()
  886. })
  887. })