stdio.spec.ts 37 KB

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