|
| 1 | +/** |
| 2 | + * Regression guard for the idle-timeout-during-shell-initialization race: |
| 3 | + * on a cold terminal, onDidStartTerminalShellExecution can fire AFTER the |
| 4 | + * 3-second idle window. Without the guard added in TerminalProcess.run(), |
| 5 | + * the IDLE_SENTINEL path self-finalizes before the command starts executing, |
| 6 | + * silently dropping its output. |
| 7 | + * |
| 8 | + * The fix: when IDLE_SENTINEL fires but the shell_execution_started event has |
| 9 | + * not been received, continue waiting (re-arm the idle timer) up to |
| 10 | + * Shell Integration Timeout ms. Only self-finalize once the event has arrived |
| 11 | + * OR the full timeout is exhausted. |
| 12 | + * |
| 13 | + * NOTE on cold-zsh first-command zero-chunks: VSCode's execution.read() API |
| 14 | + * has known reliability issues on "basic" shell integration (documented in |
| 15 | + * https://github.com/microsoft/vscode/issues/242897). On a cold zsh terminal |
| 16 | + * the very first command may produce zero stream chunks even though the |
| 17 | + * command ran successfully — this is a VSCode API limitation, not a Zoo Code |
| 18 | + * bug. The model typically retries and captures output on the second attempt |
| 19 | + * (warm terminal). This test verifies the guard prevents a PREMATURE |
| 20 | + * self-finalize and that the model eventually receives "cold-init-ok" output |
| 21 | + * (either on the first or a retry attempt). |
| 22 | + * |
| 23 | + * See: https://github.com/Zoo-Code-Org/Zoo-Code/issues/800 |
| 24 | + */ |
| 25 | +import * as assert from "assert" |
| 26 | + |
| 27 | +import { RooCodeEventName, type ClineMessage } from "@roo-code/types" |
| 28 | + |
| 29 | +import { waitUntilCompleted } from "../utils" |
| 30 | +import { setDefaultSuiteTimeout } from "../test-utils" |
| 31 | + |
| 32 | +suite("Cold shell init — idle timeout must not misfire before shell starts", function () { |
| 33 | + if (process.platform !== "linux") { |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + setDefaultSuiteTimeout(this) |
| 38 | + |
| 39 | + setup(async () => { |
| 40 | + try { |
| 41 | + await globalThis.api.cancelCurrentTask() |
| 42 | + } catch { |
| 43 | + // task may not be running |
| 44 | + } |
| 45 | + }) |
| 46 | + |
| 47 | + teardown(async () => { |
| 48 | + try { |
| 49 | + await globalThis.api.cancelCurrentTask() |
| 50 | + } catch { |
| 51 | + // task may not be running |
| 52 | + } |
| 53 | + }) |
| 54 | + |
| 55 | + test("captures output from a multiline command on a fresh terminal", async function () { |
| 56 | + const api = globalThis.api |
| 57 | + const messages: ClineMessage[] = [] |
| 58 | + let errorOccurred: string | null = null |
| 59 | + |
| 60 | + const messageHandler = ({ message }: { message: ClineMessage }) => { |
| 61 | + messages.push(message) |
| 62 | + if (message.type === "say" && message.say === "error") { |
| 63 | + errorOccurred = message.text || "Unknown error" |
| 64 | + } |
| 65 | + } |
| 66 | + api.on(RooCodeEventName.Message, messageHandler) |
| 67 | + |
| 68 | + const startedAt = Date.now() |
| 69 | + |
| 70 | + try { |
| 71 | + await waitUntilCompleted({ |
| 72 | + api, |
| 73 | + start: () => |
| 74 | + api.startNewTask({ |
| 75 | + configuration: { |
| 76 | + mode: "code", |
| 77 | + autoApprovalEnabled: true, |
| 78 | + alwaysAllowExecute: true, |
| 79 | + allowedCommands: ["*"], |
| 80 | + terminalShellIntegrationDisabled: false, |
| 81 | + }, |
| 82 | + text: "COLD_SHELL_INIT_E2E", |
| 83 | + }), |
| 84 | + timeout: 60_000, |
| 85 | + }) |
| 86 | + |
| 87 | + const elapsedMs = Date.now() - startedAt |
| 88 | + |
| 89 | + assert.strictEqual(errorOccurred, null, `Error occurred: ${errorOccurred}`) |
| 90 | + |
| 91 | + // The fixture only responds with attempt_completion once its predicate |
| 92 | + // confirms "cold-init-ok" and "Exit code: 0" are in the tool result. |
| 93 | + // If the idle timeout misfired (premature self-finalize), the output |
| 94 | + // would be empty/unknown and the predicate would never match. |
| 95 | + const completionMessage = messages.find( |
| 96 | + (message) => message.type === "say" && message.say === "completion_result", |
| 97 | + ) |
| 98 | + assert.ok( |
| 99 | + completionMessage, |
| 100 | + `Task should have reached attempt_completion with real output (elapsed: ${elapsedMs}ms). ` + |
| 101 | + `If this timed out, the idle timeout may have misfired before onDidStartTerminalShellExecution fired.`, |
| 102 | + ) |
| 103 | + } finally { |
| 104 | + api.off(RooCodeEventName.Message, messageHandler) |
| 105 | + } |
| 106 | + }) |
| 107 | +}) |
0 commit comments