Repository navigation
Conversation
|
Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually. Contributors can view more details about this message here. |
…livery The drain before Ctrl-C used 0.2 s `timeout cat` windows within a fixed five-second budget. Process start-up counted against each window, so a loaded host could report a full input queue as quiet and leave Ctrl-C queued in tmux behind the paste, while send_keys returned normally. A large paste could also outlast the budget while still draining. Run one script that keeps a single reader on the tty for the whole call. Input is quiet only when the reader has stopped making progress and the tty has nothing to read. The drain continues while input arrives and fails if it stops draining. After Ctrl-C, the script waits until each foreground process that SIGINT would end has exited or has SIGINT pending, and fails if input stops arriving first. Signed-off-by: Ananth Subramaniam <[email protected]>
ananthsub
force-pushed
the
ananthsub/terminus-drain-queue-state
branch
from
November 10, 2026 17:52
2128d10 to
fcdc8fa
Compare
ananthsub
changed the base branch from
main
to
ananthsub/terminus-interrupt-race
November 10, 2026 17:52
…script's group A provider that times out the interrupt script could leave its terminal reader running, and that reader would then consume the agent's later keystrokes. GNU timeout moves into its own process group, so a group kill missed it, and a provider that kills only its client left the script running. The reader is now a plain cat in the script's process group, the script fails after 90 seconds, and the command carries an explicit 120-second timeout. The confirmation after Ctrl-C now samples the reader before the foreground processes, so a reader that the exiting shell hung up is accepted only once the job has ended. Both waits share one bound on polls without progress, and a failing sleep ends the script. New tests cover a job that catches SIGINT, a reader that stops before or after Ctrl-C takes effect, and a killed process group; each fails when the part of the script it covers is removed. Signed-off-by: Ananth Subramaniam <[email protected]>
With the deadline lowered to two seconds and a slow reader, the interrupt must fail with the deadline message, leave no reader on the terminal, and let a later interrupt and command reach the shell. The test fails with the deadline disabled. Signed-off-by: Ananth Subramaniam <[email protected]>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note
Stacked on #5943 (
ananthsub/terminus-interrupt-race), because both changetest_terminal.py. The base stays on that branch, and this PR stays a draft, until #5943 merges. The new tests reuse #5943'seventuallyhelper.What changed and why
When the terminus agent sends Ctrl-C, it first drains the pane's pending input. A paste queued behind a job that never reads, such as
sleep, fills the kernel input queue, and tmux then holds even Ctrl-C behind the rest of the paste. The drain decided that the input was gone with timed windows (timeout 0.2 cat "$tty" | wc -c, up to 25 times). On a loaded host this gave the wrong answer in two ways:timeoutandcatand opening the tty counted against the 0.2 s window. Under CPU contentioncatcould read nothing before it was killed, and one 0-byte window counted as proof that the input was gone. In one reproduction the drain returned after 0.37 s while the tty's input queue (FIONREAD) held 4094 bytes both before and after. Ctrl-C then waited in tmux behind the rest of the paste, the foreground job kept running, andsend_keysreturned as if the interrupt had worked.RuntimeError.TerminusTmuxSession._interruptreplaces_drain_pending_inputand the separatesend-keys C-c. It runs one script in the sandbox:catreads the tty into a temporary file from start to finish, so it keeps consuming input while Ctrl-C is sent too. The reader stays in the script's process group, and the script's exit trap stops it. A reader left running would consume the agent's later keystrokes.read -t 0on the open descriptor). Start-up delays no longer look like quiet: pending input stays readable until the reader actually takes it.send-keys C-cit keeps draining until each one has exited or has SIGINT pending. This covers a tmux that is starved after the kernel queue empties. If such a process is still running after 100 polls without progress (the same bound as the drain), the call fails with "Ctrl-C did not reach the foreground job", instead of reporting success. The reader also stops when Ctrl-C ends a shell and the terminal hangs up; the script checks the reader before the processes, so that is a failure only while one of them still runs. A shell at its prompt and programs that handle SIGINT themselves have no such processes, so the script does not wait for them. Without/procthere is nothing to confirm, and the script returns after sending Ctrl-C.The script runs with an explicit 120-second command timeout. The error contract is unchanged. A script failure raises
RuntimeError. When the shell has exited,_send_keys_to_sessionturns that intoShellExitedError. Provider failures from the exec layer pass through unchanged. Raw applications (-isig) still receive Ctrl-C as a byte, without a drain. The README paragraph about the five-second limit now describes the new behavior.Issue: none filed. This PR describes the bug in full above. It came out of the review of #5943, which shrinks that test's paste from about 1.3 MB to about 95 KB so the test does not depend on this bug. This PR fixes the product code.
Tests
Each new real-tmux test reproduces one failure mode deterministically, with shims on
PATHand a stopped tmux server rather than load. These fail onmainand pass with this change:catthat takes 2 s to start must not make the drain report quiet while about 1.3 MB of paste is pending. Onmain, Ctrl-C stays behind the paste andsleepkeeps running.mainthis fails with "did not become quiet within 5 seconds".sleep. Onmain, the drain reports quiet and Ctrl-C stays queued behind the paste.tmuxshim drops the Ctrl-C key. The call must fail with "Ctrl-C did not reach the foreground job", as a terminal failure and not asShellExitedError, whilesleepis still running and no queued text has run. Onmainthe call returns normally.The next tests pin the parts of the script that the confirmation after Ctrl-C would otherwise hide:
sleepin the background, so the script has no process to confirm and the drain alone must have emptied the queue. It runs behind about 1.3 MB of paste with a reader that starts 2 s late, behind a slow reader, and behind a tmux stopped for 0.3 s once the kernel queue has drained. The handler must run and no queued line may run. Replacing theread -t 0quiet check with an unconditional stop fails the late-reader case, and dropping the five-poll quiet period fails the brief-stall case.sleeprunning. The call must succeed, as it does when an exiting shell hangs up the terminal. If the reader stops before Ctrl-C takes effect, the call must fail with "reader stopped before Ctrl-C took effect", not with a lost-Ctrl-C error orShellExitedError.timeout 900 cat, the SIGKILL case fails:timeoutmoves into its own process group, so its reader survives.The successful cases also check that no queued shell text ran, and that the terminal accepts a new command afterwards. The raw-mode path (
-isig), which sends Ctrl-C as a byte without a drain, was not covered by any test for the old drain. With this change, the raw-mode step of the full-PTY Ctrl-C test does cover it: when that path is turned into a no-op, the step fails with "Ctrl-C did not reach the foreground job", becauseheadreading in raw mode never gets SIGINT. The unit tests that faked the old command sequence now check that Ctrl-C is sent only by the interrupt script, that a failed interrupt raises with the script's error output and skips the rest of the command batch, and that command order is kept.Commands (the server's venv from
uv sync --frozen --all-extras):cd responses_api_agents/terminus_2_sandboxed_agent .venv/bin/python -m pytest -q tests/test_terminal.py pre-commit run --files responses_api_agents/terminus_2_sandboxed_agent/terminal.py responses_api_agents/terminus_2_sandboxed_agent/tests/test_terminal.py responses_api_agents/terminus_2_sandboxed_agent/README.mdUnder load
Host: 24 cores. Each run starts 24 busy loops, and other workloads were running too (load average 30 to 150).
pytest -n 24, for 4 rounds. Each copy waits forsleep, pastes shell text, sends Ctrl-C, and expects theset -eshell to exit.main: 74 of 192 runs failed. 73 failed with "did not become quiet within 5 seconds" and one with a lost Ctrl-C. The median drain took 5.6 s.test_terminal.py, 8 rounds underpytest -n 16at0df3a1253fand 4 more after the deadline test was added, all passed. An earlier set of 8 rounds had one failure in the killed-group test: it read the pane right after the interrupt returned, whilesleephad SIGINT pending but had not yet exited. The tests that check thatsleephas ended now poll for it.tmux,sleep 120or reader processes, and no temporary files from these runs, were left behind afterwards.Without #5943's test changes, the interrupt case of the existing exited-shell recovery test fails under load on
mainand on this change alike (4 of 6 runs each), because it sends Ctrl-C beforesleephas started. #5943 fixes that race.Rollout evidence
Real model, at
0df3a1253f(the head after it changes only a test):nvidia/openai/eccn-gpt-oss-20bthroughopenai_model, withgym eval run <task folders> --agent terminus_2_sandboxed_agent --sandbox docker. The sandbox image wasghcr.io/astral-sh/uv:python3.13-bookworm-slimwith tmux 3.3a and bash 5.2.15. There were two Harbor task folders, 3 repeats each:ctrl-c-sleep: runsleep 600, sendC-c, then writeinterruptedto/app/done.txt.ctrl-c-queued-input: the same, but typeecho one/two/threelines whilesleepruns, beforeC-c.done.txtholdsinterruptedand nosleep 600process remains.None of the 6 rollouts raised an interrupt error or was masked.
C-cas a key and scored 1.0. In each of themsleep 600ended and the next command ran. Three of them sentC-cwith a trailing newline, which the agent corrected to the key.ctrl-c-queued-inputrollout the model put every step,C-cincluded, into a single keystroke string. That sendsC-cas text, so no interrupt was requested.sleepeach took about 0.65 s. No queued line ran, and the next command ran.An earlier smoke run, on the first revision of this PR, had the same result for the interrupt. Two of its rollouts were masked with
harbor:trajectory_export_failed, because gpt-oss-20b emittedmcp_calloutput items that the harbor environment server's trajectory export does not accept. That is unrelated to this change.Compatibility
There are no API or configuration changes. A Ctrl-C now waits until the input has been quiet for about 0.5 s; the old window was 0.2 s. With a foreground job that SIGINT ends, the call also waits for that job to take the signal. A Ctrl-C that does not reach such a job now fails instead of reporting success. The model sees that failure as a terminal error, and the shell recovery path still applies when the shell itself exited.
The interrupt command now passes an explicit 120-second timeout to the sandbox exec. Before, it passed none, so the provider's default applied (180 s for docker, local and enroot). The script itself fails after 90 seconds, so it ends, and stops its reader, before that timeout. A provider that kills only its client on timeout, as docker does, therefore cannot leave the reader consuming later keystrokes for long, and a provider that kills the process group also kills the reader. The old drain gave up after about 5 seconds, so a paste that keeps arriving can now hold a Ctrl-C for up to 90 seconds before it fails.