stdio.spec.ts 39 KB

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