Skip to content

fix(terminus): judge Ctrl-C input drain by queue state and confirm delivery - #5951

Draft
ananthsub wants to merge 3 commits into
ananthsub/terminus-interrupt-racefrom
ananthsub/terminus-drain-queue-state
Draft

ananthsub wants to merge 3 commits into
ananthsub/terminus-interrupt-racefrom
ananthsub/terminus-drain-queue-state

Conversation

@ananthsub

@ananthsub ananthsub commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Note

Stacked on #5943 (ananthsub/terminus-interrupt-race), because both change test_terminal.py. The base stays on that branch, and this PR stays a draft, until #5943 merges. The new tests reuse #5943's eventually helper.

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:

  1. False quiet: Ctrl-C was lost. Starting timeout and cat and opening the tty counted against the 0.2 s window. Under CPU contention cat could 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, and send_keys returned as if the interrupt had worked.
  2. Fixed five-second budget. Under load, tmux pushes a large paste through the 4 KB queue more slowly than 25 windows allow. The drain gave up with "Pending terminal input did not become quiet within 5 seconds" while input was still flowing, and that surfaced as a generic RuntimeError.

TerminusTmuxSession._interrupt replaces _drain_pending_input and the separate send-keys C-c. It runs one script in the sandbox:

  • One reader for the whole call. A single cat reads 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.
  • Quiet comes from queue state. The input is quiet only when the reader has made no progress for five polls and the tty has nothing to read (read -t 0 on the open descriptor). Start-up delays no longer look like quiet: pending input stays readable until the reader actually takes it.
  • It gives up when input stops draining, not after a short fixed time. The drain continues for as long as input arrives. It fails if input stays readable but the reader makes no progress for 100 polls. A 90-second deadline for the whole script bounds a paste that never ends and keeps the script inside its command timeout.
  • Delivery is confirmed. Before Ctrl-C, the script records each foreground process that SIGINT would end: default disposition, not blocked, ignored or caught. After send-keys C-c it 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 /proc there 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_session turns that into ShellExitedError. 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 PATH and a stopped tmux server rather than load. These fail on main and pass with this change:

  • Slow reader start-up. A cat that takes 2 s to start must not make the drain report quiet while about 1.3 MB of paste is pending. On main, Ctrl-C stays behind the paste and sleep keeps running.
  • Slow reader. A reader that consumes about 5 KB/s must not exhaust a budget while input is still draining. On main this fails with "did not become quiet within 5 seconds".
  • Stalled tmux. The tmux server is stopped for 3 s just before the drain starts reading. The kernel queue empties while tmux still holds most of the paste. Ctrl-C must still end sleep. On main, the drain reports quiet and Ctrl-C stays queued behind the paste.
  • Lost Ctrl-C. A tmux shim drops the Ctrl-C key. The call must fail with "Ctrl-C did not reach the foreground job", as a terminal failure and not as ShellExitedError, while sleep is still running and no queued text has run. On main the call returns normally.

The next tests pin the parts of the script that the confirmation after Ctrl-C would otherwise hide:

  • A job that catches SIGINT. The job is a shell that traps SIGINT and runs sleep in 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 the read -t 0 quiet check with an unconditional stop fails the late-reader case, and dropping the five-poll quiet period fails the brief-stall case.
  • The reader stops after Ctrl-C. A shim delivers Ctrl-C during a poll and stops the reader there, after the script has seen sleep running. 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 or ShellExitedError.
  • The script's process group is killed mid-drain, with SIGKILL and with SIGTERM, as the local provider does on a command timeout. No process may be left holding the terminal, a later interrupt and command must reach the shell, and after SIGTERM the temporary file must be gone. With the previous revision's timeout 900 cat, the SIGKILL case fails: timeout moves into its own process group, so its reader survives.
  • The deadline. With the deadline lowered to 2 seconds and a slow reader, the interrupt must fail with "still arriving after 2 seconds", leave no process holding the terminal and no temporary file, and let a later interrupt and command reach the shell. With the deadline disabled, the call does not fail.

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", because head reading 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.md

Under load

Host: 24 cores. Each run starts 24 busy loops, and other workloads were running too (load average 30 to 150).

  • Reproduction from the test(terminus): wait for the foreground process before sending Ctrl-C #5943 review. 48 copies of the interrupt flow per round, under pytest -n 24, for 4 rounds. Each copy waits for sleep, pastes shell text, sends Ctrl-C, and expects the set -e shell to exit.
    • About 1.3 MB of paste, on 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.
    • About 1.3 MB of paste, at the head of this PR: 0 of 192 runs failed. The median interrupt took 3.3 s, the longest 7.7 s.
    • About 95 KB of paste, at the head of this PR: 0 of 192 runs failed. The median interrupt took 1.1 s, the longest 1.9 s.
  • The whole test_terminal.py, 8 rounds under pytest -n 16 at 0df3a1253f and 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, while sleep had SIGINT pending but had not yet exited. The tests that check that sleep has ended now poll for it.
  • No leftovers. No tmux, sleep 120 or 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 main and on this change alike (4 of 6 runs each), because it sends Ctrl-C before sleep has started. #5943 fixes that race.

Rollout evidence

Real model, at 0df3a1253f (the head after it changes only a test): nvidia/openai/eccn-gpt-oss-20b through openai_model, with gym eval run <task folders> --agent terminus_2_sandboxed_agent --sandbox docker. The sandbox image was ghcr.io/astral-sh/uv:python3.13-bookworm-slim with tmux 3.3a and bash 5.2.15. There were two Harbor task folders, 3 repeats each:

  • ctrl-c-sleep: run sleep 600, send C-c, then write interrupted to /app/done.txt.
  • ctrl-c-queued-input: the same, but type echo one/two/three lines while sleep runs, before C-c.
  • Verifier: reward 1 only if done.txt holds interrupted and no sleep 600 process remains.

None of the 6 rollouts raised an interrupt error or was masked.

  • Rewards. 5 rollouts sent C-c as a key and scored 1.0. In each of them sleep 600 ended and the next command ran. Three of them sent C-c with a trailing newline, which the agent corrected to the key.
  • One reward of 0. In one ctrl-c-queued-input rollout the model put every step, C-c included, into a single keystroke string. That sends C-c as text, so no interrupt was requested.
  • Direct container check (earlier revision). Interrupt runs of the script in the container with 3, 1,000 and 20,000 pasted lines behind sleep each 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 emitted mcp_call output 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.

@copy-pr-bot

copy-pr-bot Bot commented Nov 10, 2026

Copy link
Copy Markdown

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
ananthsub force-pushed the ananthsub/terminus-drain-queue-state branch from 2128d10 to fcdc8fa Compare November 10, 2026 17:52
@ananthsub
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]>
@svcnvidia-nemo-ci svcnvidia-nemo-ci added bug Something isn't working area:agent Agent harnesses and Responses API agent behavior labels Nov 16, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:agent Agent harnesses and Responses API agent behavior bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants