|
| 1 | +import { runStreamCase, StreamEvent } from "../lib/stream-harness" |
| 2 | + |
| 3 | +const START_PROMPT = |
| 4 | + 'Run exactly this command and do not summarize until it finishes: sleep 12 && echo "done". After it finishes, reply with exactly "done".' |
| 5 | +const FOLLOWUP_PROMPT = 'After cancellation, reply with only "RACE-OK".' |
| 6 | + |
| 7 | +async function main() { |
| 8 | + const startRequestId = `start-${Date.now()}` |
| 9 | + const cancelRequestId = `cancel-${Date.now()}` |
| 10 | + const followupRequestId = `message-${Date.now()}` |
| 11 | + const shutdownRequestId = `shutdown-${Date.now()}` |
| 12 | + |
| 13 | + let initSeen = false |
| 14 | + let sentCancelAndFollowup = false |
| 15 | + let sentShutdown = false |
| 16 | + let cancelDoneCode: string | undefined |
| 17 | + let followupDoneCode: string | undefined |
| 18 | + let followupResult = "" |
| 19 | + let sawFollowupUserTurn = false |
| 20 | + let sawMisroutedToolResult = false |
| 21 | + let sawMessageControlError = false |
| 22 | + |
| 23 | + await runStreamCase({ |
| 24 | + onEvent(event: StreamEvent, context) { |
| 25 | + if (event.type === "system" && event.subtype === "init" && !initSeen) { |
| 26 | + initSeen = true |
| 27 | + context.sendCommand({ |
| 28 | + command: "start", |
| 29 | + requestId: startRequestId, |
| 30 | + prompt: START_PROMPT, |
| 31 | + }) |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + if (event.type === "control" && event.subtype === "error") { |
| 36 | + if (event.requestId === followupRequestId) { |
| 37 | + sawMessageControlError = true |
| 38 | + } |
| 39 | + throw new Error( |
| 40 | + `received control error for requestId=${event.requestId ?? "unknown"} command=${event.command ?? "unknown"} code=${event.code ?? "unknown"} content=${event.content ?? ""}`, |
| 41 | + ) |
| 42 | + } |
| 43 | + |
| 44 | + if ( |
| 45 | + !sentCancelAndFollowup && |
| 46 | + event.type === "tool_use" && |
| 47 | + event.requestId === startRequestId && |
| 48 | + event.subtype === "command" |
| 49 | + ) { |
| 50 | + context.sendCommand({ |
| 51 | + command: "cancel", |
| 52 | + requestId: cancelRequestId, |
| 53 | + }) |
| 54 | + context.sendCommand({ |
| 55 | + command: "message", |
| 56 | + requestId: followupRequestId, |
| 57 | + prompt: FOLLOWUP_PROMPT, |
| 58 | + }) |
| 59 | + sentCancelAndFollowup = true |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + if ( |
| 64 | + event.type === "control" && |
| 65 | + event.command === "cancel" && |
| 66 | + event.subtype === "done" && |
| 67 | + event.requestId === cancelRequestId |
| 68 | + ) { |
| 69 | + cancelDoneCode = event.code |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + if ( |
| 74 | + event.type === "control" && |
| 75 | + event.command === "message" && |
| 76 | + event.subtype === "done" && |
| 77 | + event.requestId === followupRequestId |
| 78 | + ) { |
| 79 | + followupDoneCode = event.code |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + if ( |
| 84 | + event.type === "tool_result" && |
| 85 | + event.requestId === followupRequestId && |
| 86 | + typeof event.content === "string" && |
| 87 | + event.content.includes("<user_message>") |
| 88 | + ) { |
| 89 | + sawMisroutedToolResult = true |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + if (event.type === "user" && event.requestId === followupRequestId) { |
| 94 | + sawFollowupUserTurn = typeof event.content === "string" && event.content.includes("RACE-OK") |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + if (event.type !== "result" || event.done !== true || event.requestId !== followupRequestId) { |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + followupResult = event.content ?? "" |
| 103 | + |
| 104 | + if (followupResult.trim().length === 0) { |
| 105 | + throw new Error("follow-up after cancel produced an empty result") |
| 106 | + } |
| 107 | + if (cancelDoneCode !== "cancel_requested") { |
| 108 | + throw new Error( |
| 109 | + `cancel done code mismatch; expected cancel_requested, got "${cancelDoneCode ?? "none"}"`, |
| 110 | + ) |
| 111 | + } |
| 112 | + if (followupDoneCode !== "responded" && followupDoneCode !== "queued") { |
| 113 | + throw new Error( |
| 114 | + `unexpected follow-up done code after cancel race; expected responded|queued, got "${followupDoneCode ?? "none"}"`, |
| 115 | + ) |
| 116 | + } |
| 117 | + if (sawMessageControlError) { |
| 118 | + throw new Error("follow-up message emitted control error in cancel recovery race") |
| 119 | + } |
| 120 | + if (sawMisroutedToolResult) { |
| 121 | + throw new Error( |
| 122 | + "follow-up message was misrouted into tool_result (<user_message>) in cancel recovery race", |
| 123 | + ) |
| 124 | + } |
| 125 | + if (!sawFollowupUserTurn) { |
| 126 | + throw new Error("follow-up after cancel did not appear as a normal user turn") |
| 127 | + } |
| 128 | + |
| 129 | + console.log(`[PASS] cancel done code: "${cancelDoneCode}"`) |
| 130 | + console.log(`[PASS] follow-up done code: "${followupDoneCode}"`) |
| 131 | + console.log(`[PASS] follow-up user turn observed: ${sawFollowupUserTurn}`) |
| 132 | + console.log(`[PASS] follow-up result: "${followupResult}"`) |
| 133 | + |
| 134 | + if (!sentShutdown) { |
| 135 | + context.sendCommand({ |
| 136 | + command: "shutdown", |
| 137 | + requestId: shutdownRequestId, |
| 138 | + }) |
| 139 | + sentShutdown = true |
| 140 | + } |
| 141 | + }, |
| 142 | + onTimeoutMessage() { |
| 143 | + return [ |
| 144 | + "timed out waiting for cancel-message-recovery-race validation", |
| 145 | + `initSeen=${initSeen}`, |
| 146 | + `sentCancelAndFollowup=${sentCancelAndFollowup}`, |
| 147 | + `cancelDoneCode=${cancelDoneCode ?? "none"}`, |
| 148 | + `followupDoneCode=${followupDoneCode ?? "none"}`, |
| 149 | + `sawFollowupUserTurn=${sawFollowupUserTurn}`, |
| 150 | + `sawMisroutedToolResult=${sawMisroutedToolResult}`, |
| 151 | + `sawMessageControlError=${sawMessageControlError}`, |
| 152 | + `haveFollowupResult=${Boolean(followupResult)}`, |
| 153 | + ].join(" ") |
| 154 | + }, |
| 155 | + }) |
| 156 | +} |
| 157 | + |
| 158 | +main().catch((error) => { |
| 159 | + console.error(`[FAIL] ${error instanceof Error ? error.message : String(error)}`) |
| 160 | + process.exit(1) |
| 161 | +}) |
0 commit comments