runtime.queue.test.ts 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. import { describe, expect, test } from "bun:test"
  2. import { runPromptQueue as runPromptQueueBase, type QueueInput } from "../../src/mini/runtime.queue"
  3. import type { RunPrompt } from "../../src/mini/types"
  4. import { createFooterApiFixture } from "./fixture/footer-api"
  5. function runPromptQueue(input: Omit<QueueInput, "admit" | "settle"> & Partial<Pick<QueueInput, "admit" | "settle">>) {
  6. return runPromptQueueBase({
  7. admit: async () => {},
  8. settle: async () => {},
  9. ...input,
  10. })
  11. }
  12. describe("run runtime queue", () => {
  13. test("ignores empty prompts", async () => {
  14. const ui = createFooterApiFixture()
  15. let calls = 0
  16. const task = runPromptQueue({
  17. footer: ui.api,
  18. run: async () => {
  19. calls += 1
  20. },
  21. })
  22. ui.submit(" ")
  23. ui.api.close()
  24. await task
  25. expect(calls).toBe(0)
  26. })
  27. test("treats /exit as a close command", async () => {
  28. const ui = createFooterApiFixture()
  29. let calls = 0
  30. const task = runPromptQueue({
  31. footer: ui.api,
  32. run: async () => {
  33. calls += 1
  34. },
  35. })
  36. ui.submit("/exit")
  37. await task
  38. expect(calls).toBe(0)
  39. })
  40. test("treats /new as a local session command", async () => {
  41. const ui = createFooterApiFixture()
  42. const seen: string[] = []
  43. let created = 0
  44. const task = runPromptQueue({
  45. footer: ui.api,
  46. onNewSession: async () => {
  47. created += 1
  48. },
  49. run: async (input) => {
  50. seen.push(input.text)
  51. ui.api.close()
  52. },
  53. })
  54. ui.submit("/new")
  55. ui.submit("hello")
  56. await task
  57. expect(created).toBe(1)
  58. expect(seen).toEqual(["hello"])
  59. expect(ui.commits).toEqual([
  60. {
  61. kind: "user",
  62. text: "hello",
  63. phase: "start",
  64. source: "system",
  65. messageID: expect.any(String),
  66. },
  67. ])
  68. })
  69. test("treats /compact as a local compaction command", async () => {
  70. const ui = createFooterApiFixture()
  71. const seen: string[] = []
  72. let compacted = 0
  73. const task = runPromptQueue({
  74. footer: ui.api,
  75. onCompact: async () => {
  76. compacted += 1
  77. },
  78. run: async (input) => {
  79. seen.push(input.text)
  80. ui.api.close()
  81. },
  82. })
  83. ui.submit("/compact")
  84. ui.submit("hello")
  85. await task
  86. expect(compacted).toBe(1)
  87. expect(seen).toEqual(["hello"])
  88. expect(ui.commits.map((item) => item.text)).toEqual(["hello"])
  89. })
  90. test("keeps prompts submitted after an in-flight /compact behind the compaction barrier", async () => {
  91. const ui = createFooterApiFixture()
  92. const active = Promise.withResolvers<void>()
  93. const order: string[] = []
  94. const task = runPromptQueue({
  95. footer: ui.api,
  96. onCompact: async () => {
  97. order.push("compact")
  98. },
  99. admit: async (prompt) => {
  100. order.push(`admit:${prompt.text}`)
  101. },
  102. run: async (prompt) => {
  103. order.push(`run:${prompt.text}`)
  104. if (prompt.text === "first") await active.promise
  105. if (prompt.text === "later") ui.api.close()
  106. },
  107. })
  108. ui.submit("first")
  109. await Promise.resolve()
  110. ui.submit("/compact")
  111. ui.submit("later")
  112. await Promise.resolve()
  113. expect(order).toEqual(["run:first"])
  114. active.resolve()
  115. await task
  116. expect(order).toEqual(["run:first", "compact", "run:later"])
  117. })
  118. test("shell mode submits /exit as a shell command", async () => {
  119. const ui = createFooterApiFixture()
  120. const seen: RunPrompt[] = []
  121. const task = runPromptQueue({
  122. footer: ui.api,
  123. run: async (input) => {
  124. seen.push(input)
  125. ui.api.close()
  126. },
  127. })
  128. ui.submit("/exit", "shell")
  129. await task
  130. expect(seen).toEqual([{ text: "/exit", parts: [], mode: "shell" }])
  131. expect(ui.commits).toEqual([])
  132. })
  133. test("shell mode submits /new instead of creating a session", async () => {
  134. const ui = createFooterApiFixture()
  135. const seen: RunPrompt[] = []
  136. let created = 0
  137. const task = runPromptQueue({
  138. footer: ui.api,
  139. onNewSession: async () => {
  140. created += 1
  141. },
  142. run: async (input) => {
  143. seen.push(input)
  144. ui.api.close()
  145. },
  146. })
  147. ui.submit("/new", "shell")
  148. await task
  149. expect(created).toBe(0)
  150. expect(seen).toEqual([{ text: "/new", parts: [], mode: "shell" }])
  151. expect(ui.commits).toEqual([])
  152. })
  153. test("shell mode does not append a synthetic user row", async () => {
  154. const ui = createFooterApiFixture()
  155. const task = runPromptQueue({
  156. footer: ui.api,
  157. run: async () => {
  158. expect(ui.commits).toEqual([])
  159. ui.api.close()
  160. },
  161. })
  162. ui.submit("ls", "shell")
  163. await task
  164. })
  165. test("shell mode does not emit a turn duration summary", async () => {
  166. const ui = createFooterApiFixture()
  167. const task = runPromptQueue({
  168. footer: ui.api,
  169. run: async () => {
  170. ui.api.close()
  171. },
  172. })
  173. ui.submit("ls", "shell")
  174. await task
  175. expect(ui.events.some((event) => event.type === "turn.duration")).toBe(false)
  176. })
  177. test("preserves whitespace for initial input", async () => {
  178. const ui = createFooterApiFixture()
  179. const seen: string[] = []
  180. await runPromptQueue({
  181. footer: ui.api,
  182. initialInput: " hello ",
  183. run: async (input) => {
  184. seen.push(input.text)
  185. ui.api.close()
  186. },
  187. })
  188. expect(seen).toEqual([" hello "])
  189. expect(ui.commits).toEqual([
  190. {
  191. kind: "user",
  192. text: " hello ",
  193. phase: "start",
  194. source: "system",
  195. messageID: expect.any(String),
  196. },
  197. ])
  198. })
  199. test("durably admits in-flight follow-ups in submission order", async () => {
  200. const ui = createFooterApiFixture()
  201. const admitted: string[] = []
  202. const gate = Promise.withResolvers<void>()
  203. const task = runPromptQueue({
  204. footer: ui.api,
  205. run: async (input, _signal, onAdmitted) => {
  206. admitted.push(`${input.text}:steer`)
  207. onAdmitted()
  208. await gate.promise
  209. },
  210. admit: async (input) => {
  211. admitted.push(`${input.text}:queue`)
  212. },
  213. settle: async () => ui.api.close(),
  214. })
  215. ui.submit("one")
  216. ui.submit("two")
  217. ui.submit("three")
  218. while (admitted.length < 3) await Bun.sleep(0)
  219. expect(admitted).toEqual(["one:steer", "two:queue", "three:queue"])
  220. expect(ui.commits.map((item) => item.text)).toEqual(["one"])
  221. gate.resolve()
  222. await task
  223. })
  224. test("preserves explicit steer and queue delivery for in-flight prompts", async () => {
  225. const ui = createFooterApiFixture()
  226. const admitted: string[] = []
  227. const gate = Promise.withResolvers<void>()
  228. const task = runPromptQueue({
  229. footer: ui.api,
  230. run: async (_input, _signal, onAdmitted) => {
  231. onAdmitted()
  232. await gate.promise
  233. },
  234. admit: async (input, delivery) => {
  235. admitted.push(`${input.text}:${delivery}`)
  236. },
  237. settle: async () => ui.api.close(),
  238. })
  239. ui.submit("one")
  240. ui.submit("two", undefined, "steer")
  241. ui.submit("three", undefined, "queue")
  242. while (admitted.length < 2) await Bun.sleep(0)
  243. expect(admitted).toEqual(["two:steer", "three:queue"])
  244. gate.resolve()
  245. await task
  246. })
  247. test("continues durable admission after one fails", async () => {
  248. const ui = createFooterApiFixture()
  249. const admitted: string[] = []
  250. const errors: string[] = []
  251. const gate = Promise.withResolvers<void>()
  252. const task = runPromptQueue({
  253. footer: ui.api,
  254. run: async (_input, _signal, admitted) => {
  255. admitted()
  256. await gate.promise
  257. },
  258. admit: async (input) => {
  259. if (input.text === "two") throw new Error("admission failed")
  260. admitted.push(input.text)
  261. },
  262. onAdmissionError: (_prompt, error) => {
  263. errors.push(error instanceof Error ? error.message : String(error))
  264. },
  265. settle: async () => ui.api.close(),
  266. })
  267. ui.submit("one")
  268. ui.submit("two")
  269. ui.submit("three")
  270. while (admitted.length === 0) await Bun.sleep(0)
  271. gate.resolve()
  272. await task
  273. expect(errors).toEqual(["admission failed"])
  274. expect(admitted).toEqual(["three"])
  275. })
  276. test("close aborts an in-flight durable admission", async () => {
  277. const ui = createFooterApiFixture()
  278. let admissionHit = false
  279. const admissionStarted = Promise.withResolvers<void>()
  280. const task = runPromptQueue({
  281. footer: ui.api,
  282. run: async (_input, signal, admitted) => {
  283. admitted()
  284. await new Promise<void>((resolve) => signal.addEventListener("abort", () => resolve(), { once: true }))
  285. },
  286. admit: async (_prompt, _delivery, signal) => {
  287. admissionStarted.resolve()
  288. await new Promise<void>((resolve) => {
  289. if (signal.aborted) {
  290. admissionHit = true
  291. resolve()
  292. return
  293. }
  294. signal.addEventListener(
  295. "abort",
  296. () => {
  297. admissionHit = true
  298. resolve()
  299. },
  300. { once: true },
  301. )
  302. })
  303. },
  304. })
  305. ui.submit("one")
  306. await Promise.resolve()
  307. ui.submit("two")
  308. await admissionStarted.promise
  309. ui.api.close()
  310. await task
  311. expect(admissionHit).toBe(true)
  312. })
  313. test.each([
  314. ["session", undefined, false],
  315. ["shell", "shell", true],
  316. ] as const)("close handles an active %s turn", async (_name, mode, aborted) => {
  317. const ui = createFooterApiFixture()
  318. const started = Promise.withResolvers<AbortSignal>()
  319. const active = Promise.withResolvers<void>()
  320. const task = runPromptQueue({
  321. footer: ui.api,
  322. run: async (_input, signal) => {
  323. started.resolve(signal)
  324. await active.promise
  325. },
  326. })
  327. ui.submit("one", mode)
  328. const signal = await started.promise
  329. ui.api.close()
  330. await task
  331. expect(signal.aborted).toBe(aborted)
  332. active.resolve()
  333. })
  334. test("propagates run errors", async () => {
  335. const ui = createFooterApiFixture()
  336. const task = runPromptQueue({
  337. footer: ui.api,
  338. run: async () => {
  339. throw new Error("boom")
  340. },
  341. })
  342. ui.submit("one")
  343. await expect(task).rejects.toThrow("boom")
  344. })
  345. })