start-while-busy.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { runStreamCase, StreamEvent } from "../lib/stream-harness"
  2. const LONG_PROMPT =
  3. 'Run exactly this command and do not summarize until it finishes: sleep 8 && echo "done". After it finishes, reply with exactly "done".'
  4. async function main() {
  5. const firstStartRequestId = `start-a-${Date.now()}`
  6. const secondStartRequestId = `start-b-${Date.now()}`
  7. const shutdownRequestId = `shutdown-${Date.now()}`
  8. let initSeen = false
  9. let firstStartAccepted = false
  10. let secondStartSent = false
  11. let sawTaskBusyError = false
  12. let sentShutdown = false
  13. await runStreamCase({
  14. onEvent(event: StreamEvent, context) {
  15. if (event.type === "system" && event.subtype === "init" && !initSeen) {
  16. initSeen = true
  17. context.sendCommand({
  18. command: "start",
  19. requestId: firstStartRequestId,
  20. prompt: LONG_PROMPT,
  21. })
  22. return
  23. }
  24. if (
  25. event.type === "control" &&
  26. event.subtype === "ack" &&
  27. event.command === "start" &&
  28. event.requestId === firstStartRequestId &&
  29. !firstStartAccepted
  30. ) {
  31. firstStartAccepted = true
  32. context.sendCommand({
  33. command: "start",
  34. requestId: secondStartRequestId,
  35. prompt: "What is 1+1? Reply with only 2.",
  36. })
  37. secondStartSent = true
  38. return
  39. }
  40. if (
  41. event.type === "control" &&
  42. event.subtype === "error" &&
  43. event.command === "start" &&
  44. event.requestId === secondStartRequestId &&
  45. event.code === "task_busy"
  46. ) {
  47. sawTaskBusyError = true
  48. if (!sentShutdown) {
  49. context.sendCommand({
  50. command: "shutdown",
  51. requestId: shutdownRequestId,
  52. })
  53. sentShutdown = true
  54. }
  55. return
  56. }
  57. },
  58. onTimeoutMessage() {
  59. return `timed out waiting for task_busy error (initSeen=${initSeen}, firstStartAccepted=${firstStartAccepted}, secondStartSent=${secondStartSent}, sawTaskBusyError=${sawTaskBusyError})`
  60. },
  61. })
  62. if (!sawTaskBusyError) {
  63. throw new Error("expected task_busy error for second start command was not observed")
  64. }
  65. }
  66. main().catch((error) => {
  67. console.error(`[FAIL] ${error instanceof Error ? error.message : String(error)}`)
  68. process.exit(1)
  69. })