|
| 1 | +import { runStreamCase, StreamEvent } from "../lib/stream-harness" |
| 2 | + |
| 3 | +const START_PROMPT = 'Answer this question and finish: What is 1+1? Reply with only "2", then complete the task.' |
| 4 | +const FOLLOWUP_PROMPT = 'Different question now: what is 3+3? Reply with only "6".' |
| 5 | + |
| 6 | +async function main() { |
| 7 | + const startRequestId = `start-${Date.now()}` |
| 8 | + const followupRequestId = `message-${Date.now()}` |
| 9 | + const shutdownRequestId = `shutdown-${Date.now()}` |
| 10 | + |
| 11 | + let initSeen = false |
| 12 | + let sentFollowup = false |
| 13 | + let sentShutdown = false |
| 14 | + let startAckCount = 0 |
| 15 | + let sawStartControlAfterFollowup = false |
| 16 | + let followupDoneCode: string | undefined |
| 17 | + let sawFollowupUserTurn = false |
| 18 | + let sawMisroutedToolResult = false |
| 19 | + let followupResult = "" |
| 20 | + |
| 21 | + await runStreamCase({ |
| 22 | + onEvent(event: StreamEvent, context) { |
| 23 | + if (event.type === "system" && event.subtype === "init" && !initSeen) { |
| 24 | + initSeen = true |
| 25 | + context.sendCommand({ |
| 26 | + command: "start", |
| 27 | + requestId: startRequestId, |
| 28 | + prompt: START_PROMPT, |
| 29 | + }) |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + if (event.type === "control" && event.subtype === "error") { |
| 34 | + throw new Error( |
| 35 | + `received control error for requestId=${event.requestId ?? "unknown"} command=${event.command ?? "unknown"} code=${event.code ?? "unknown"} content=${event.content ?? ""}`, |
| 36 | + ) |
| 37 | + } |
| 38 | + |
| 39 | + if (event.type === "control" && event.command === "start" && event.subtype === "ack") { |
| 40 | + startAckCount += 1 |
| 41 | + if (sentFollowup) { |
| 42 | + sawStartControlAfterFollowup = true |
| 43 | + } |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + if ( |
| 48 | + event.type === "control" && |
| 49 | + event.command === "message" && |
| 50 | + event.subtype === "done" && |
| 51 | + event.requestId === followupRequestId |
| 52 | + ) { |
| 53 | + followupDoneCode = event.code |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + if ( |
| 58 | + event.type === "tool_result" && |
| 59 | + event.requestId === followupRequestId && |
| 60 | + typeof event.content === "string" && |
| 61 | + event.content.includes("<user_message>") |
| 62 | + ) { |
| 63 | + sawMisroutedToolResult = true |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + if (event.type === "user" && event.requestId === followupRequestId) { |
| 68 | + sawFollowupUserTurn = typeof event.content === "string" && event.content.includes("3+3") |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + if (event.type === "result" && event.done === true && event.requestId === startRequestId && !sentFollowup) { |
| 73 | + context.sendCommand({ |
| 74 | + command: "message", |
| 75 | + requestId: followupRequestId, |
| 76 | + prompt: FOLLOWUP_PROMPT, |
| 77 | + }) |
| 78 | + sentFollowup = true |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + if (event.type !== "result" || event.done !== true || event.requestId !== followupRequestId) { |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + followupResult = event.content ?? "" |
| 87 | + if (followupResult.trim().length === 0) { |
| 88 | + throw new Error("follow-up produced an empty result") |
| 89 | + } |
| 90 | + |
| 91 | + if (followupDoneCode !== "responded") { |
| 92 | + throw new Error( |
| 93 | + `follow-up message was not routed as ask response; code="${followupDoneCode ?? "none"}"`, |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | + if (sawMisroutedToolResult) { |
| 98 | + throw new Error("follow-up message was misrouted into tool_result (<user_message>), old bug reproduced") |
| 99 | + } |
| 100 | + |
| 101 | + if (!sawFollowupUserTurn) { |
| 102 | + throw new Error("follow-up did not appear as a normal user turn in stream output") |
| 103 | + } |
| 104 | + |
| 105 | + if (sawStartControlAfterFollowup) { |
| 106 | + throw new Error("unexpected start control event after follow-up; message should not trigger a new task") |
| 107 | + } |
| 108 | + |
| 109 | + if (startAckCount !== 1) { |
| 110 | + throw new Error(`expected exactly one start ack event, saw ${startAckCount}`) |
| 111 | + } |
| 112 | + |
| 113 | + console.log(`[PASS] follow-up control code: "${followupDoneCode}"`) |
| 114 | + console.log(`[PASS] follow-up user turn observed: ${sawFollowupUserTurn}`) |
| 115 | + console.log(`[PASS] follow-up result: "${followupResult}"`) |
| 116 | + |
| 117 | + if (!sentShutdown) { |
| 118 | + context.sendCommand({ |
| 119 | + command: "shutdown", |
| 120 | + requestId: shutdownRequestId, |
| 121 | + }) |
| 122 | + sentShutdown = true |
| 123 | + } |
| 124 | + }, |
| 125 | + onTimeoutMessage() { |
| 126 | + return [ |
| 127 | + "timed out waiting for completion ask-response follow-up validation", |
| 128 | + `initSeen=${initSeen}`, |
| 129 | + `sentFollowup=${sentFollowup}`, |
| 130 | + `startAckCount=${startAckCount}`, |
| 131 | + `followupDoneCode=${followupDoneCode ?? "none"}`, |
| 132 | + `sawFollowupUserTurn=${sawFollowupUserTurn}`, |
| 133 | + `sawMisroutedToolResult=${sawMisroutedToolResult}`, |
| 134 | + `haveFollowupResult=${Boolean(followupResult)}`, |
| 135 | + ].join(" ") |
| 136 | + }, |
| 137 | + }) |
| 138 | +} |
| 139 | + |
| 140 | +main().catch((error) => { |
| 141 | + console.error(`[FAIL] ${error instanceof Error ? error.message : String(error)}`) |
| 142 | + process.exit(1) |
| 143 | +}) |
0 commit comments