Wait for terminal
terminal_waitBlock until a terminal command finishes, output matches a regex, or output stays quiet, avoiding sleeps and polling.
Instructions
Block until the terminal reaches a state, instead of guessing with sleeps or polling terminal_read. Use it after terminal_type for anything that outlives the settle window: builds, installs, test runs, servers starting up.
DEFAULT (no pattern): waits for the running command to finish — i.e. for the shell to take the terminal's foreground back and its prompt to go quiet. This asks the kernel who owns the terminal, so it works with any prompt and stays correct while a command is silent (sleep 60) or chatty. Returns immediately if the shell is already at its prompt. On Windows this cannot be detected in a shell session: the call waits for a stretch of silence instead and says so — prefer pattern there. In a session created with command, it waits for that command to exit and reports its exit status. Background jobs (cmd &) do not count as running, and inside a nested program (ssh, a REPL, a TUI) the outer shell never regains the foreground until that program exits — use pattern there.
until: "quiet": waits until nothing has been printed for quietMs (default 1000), whatever owns the terminal. This is the same "settled" rule terminal_type uses, without its 10s cap — for nested programs where completion cannot be detected and no pattern fits.
WITH pattern: waits until the regex matches the text on screen (the viewport plus the 200 rows above it). Use this for servers that never exit ("Listening on"), REPL prompts, or TUI states. Text that is already on screen counts, so pick a pattern that only the new output can satisfy.
Returns the screen when the condition is met, or when timeoutMs runs out (not an error — the response says the command is still running). In a shell session this does not report an exit status; run echo $? afterwards if you need it.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| until | No | "command" (default): the running command has finished and the shell is back at its prompt. "quiet": output has been silent for `quietMs`. Ignored when `pattern` is given. | |
| pattern | No | JavaScript regular expression matched against the screen text, lines joined with "\n" (e.g. "Listening on .*:3000", "^>>>\\s*$", "(?i)error"). `^` and `$` match at the start and end of each line; trailing spaces are trimmed from screen lines, so do not match a space after a prompt. Omit to wait for command completion. | |
| quietMs | No | How long the output must stay silent. With until="command" (default 300) it applies once the shell is back in the foreground, so the prompt has finished drawing; with until="quiet" (default 1000) it is the whole condition. | |
| sessionId | Yes | Numeric id of the session to target. Get one by calling terminal_create. Use terminal_list to enumerate currently-known ids. | |
| timeoutMs | No | Give up after this long. Default 30000, max 600000. |