Skip to content

feat(presets): honour the automation timeout in conversation.run() - #567

Open
BorjaIP wants to merge 2 commits into
OpenHands:mainfrom
BorjaIP:feat/preset-run-timeout-env
Open

BorjaIP wants to merge 2 commits into
OpenHands:mainfrom
BorjaIP:feat/preset-run-timeout-env

Conversation

@BorjaIP

@BorjaIP BorjaIP commented Oct 9, 2026 •

Copy link
Copy Markdown

Why

The prompt and plugin presets call conversation.run() with no arguments, so every preset run is capped by the SDK default wait of 3600 seconds, whatever the automation's own timeout is.

  • RemoteConversation.run(blocking=True, poll_interval=1.0, timeout=3600.0) raises ConversationRunError("Run timed out after 3600.0 seconds...") once the wait exceeds timeout. The default is the same in the pinned SDK (1.53.0) and in 1.36.1.
  • The dispatcher already resolves the per-run timeout (dispatcher.py#L283) and uses it for the bash command timeout and the watchdog deadline, but the script running inside that command never learns it.

With AUTOMATION_MAX_RUN_DURATION=10800 and an automation timeout of 10800, the preset raises after about one hour. The run is reported as failed while the agent keeps working, because the presets create the conversation with delete_on_close=False.

What changed

The wait has to end before the bash command timeout kills the process. Otherwise the workspace context never reaches __exit__, and the failure completion callback is never sent. The bash timeout starts with the command, and main.py only starts after the tarball extraction and setup.sh, so the preset does not pass the raw value:

timeout = max(AUTOMATION_RUN_TIMEOUT - seconds since the script started - 60, 30)
  • SCRIPT_STARTED_AT = time.monotonic() is taken right after the stdlib imports, before the SDK imports, workspace setup and conversation creation, so all of that time is subtracted.
  • The 60 second margin leaves room for the failure callback and conversation.close(). It also has to absorb the setup.sh time, which runs before main.py and cannot be measured from it.
  • The 30 second floor keeps very short timeouts from failing instantly. In that case the bash timeout still bounds the run, as it does today.

Before and after

Before After
Automation timeout 10800, run takes 2 h Preset raises at ~3600 s, run FAILED, agent keeps working Preset waits until ~10740 s minus setup, run reports the real outcome
Run exceeds its timeout Bash kill, no completion callback, the watchdog marks the run Preset times out first and the workspace context sends the failure completion callback
AUTOMATION_RUN_TIMEOUT unset or invalid n/a Bare conversation.run(), identical to today

Compatibility and risk

  • Older dispatchers: if AUTOMATION_RUN_TIMEOUT is unset or is not a positive finite number (empty, abc, nan, inf, 0, negative), the presets call conversation.run() exactly as before.
  • Stored automations: they keep the main.py baked into their tarball (_replace_prompt_in_tarball copies every member except prompt.txt unchanged), so they only pick up the new behaviour once their tarball is regenerated. New automations get it immediately. An old main.py does not import run_timeout, so it is unaffected by the new file.
  • Custom entrypoints: unaffected. They receive one more environment variable they can choose to honour.
  • Risk: if setup.sh takes longer than the margin allows, the bash kill still happens first and the run falls back to today's watchdog path.

Tests

  • tests/test_dispatcher.py: AUTOMATION_RUN_TIMEOUT equals the automation timeout (and the timeout passed to execute_in_context), and falls back to default_run_duration. Both tests fail without the dispatcher change (KeyError: 'AUTOMATION_RUN_TIMEOUT').
  • tests/test_preset_router.py: the shared resolver covers the budget arithmetic, the wait ending before the command would be killed, the floor, and unset or invalid values. Further tests check that the resolver needs only the standard library, that both presets import it instead of redefining it, that both tarballs ship run_timeout.py, the conversation.run(timeout=...) wiring, and that the clock starts before the SDK imports.

Verification

uv run pre-commit run --all-files
# yamlfmt, ruff format, ruff lint, pycodestyle, pyright: Passed
CI=true uv run python -m pytest -q --cov=openhands/automation --cov-report=term --cov-fail-under=76 tests/
# 1907 passed, 60 skipped, total coverage 76.66% (gate 76%)
  • Docker: provided by colima, with DOCKER_HOST pointed at the colima socket.
  • Skipped locally: some tests/test_preset_router.py cases probe /var/run/docker.sock directly, so they are skipped here. CI runs them.
  • Not verified: the change has not yet been exercised end to end on a real multi-hour run.

Jev-Fast-Audit

⚡ Jev fast audit · estimates · 0.37s · commit e8be155
Strongest signal: No primary concern selected.
Evidence: No primary concern to locate.
Coverage: ⚠️ reduced context — partial coverage; 28/28 hunks, 8/8 files (context budget: 23).

All estimates and evidence
Estimate Likelihood / value Direct evidence
SQL injection 3.0% No direct hunk selected
Command injection 8.0% No direct hunk selected
Weakened authentication 4.0% No direct hunk selected
Weakened authorization 5.0% No direct hunk selected
Contract regression 10.0% No direct hunk selected
Data loss 4.0% No direct hunk selected
Sensitive data disclosure 5.0% No direct hunk selected
Unexpected data transfer 4.0% No direct hunk selected
Credential misuse 5.0% No direct hunk selected
Untrusted instruction authority 3.0% No direct hunk selected
Package source redirection 4.0% No direct hunk selected
Unverified remote execution 6.0% No direct hunk selected
Privileged environment access 3.0% No direct hunk selected
Security assessment bypass 4.0% No direct hunk selected
Prohibited workload 2.0% No direct hunk selected
Primary concern None selected; confidence 94.0% No primary concern to locate

@github-actions github-actions Bot added the type: feat A new feature label Oct 9, 2026
The prompt and plugin presets call conversation.run() without arguments, so the
SDK default of 3600 seconds applies to every run. With a longer automation
timeout the preset raises ConversationRunError after an hour and the run is
reported as failed while the agent keeps working.

The dispatcher now exports the run's resolved timeout as AUTOMATION_RUN_TIMEOUT,
the same value that bounds the bash command. The presets pass it to
conversation.run(timeout=...) minus the time already spent in the script and a
60 second safety margin, with a 30 second floor, so the wait ends before the
command is killed and the workspace context can still send the failure callback
with the conversation id.

When the variable is unset or not a positive finite number, the presets keep
calling conversation.run() unchanged, so tarballs run by an older dispatcher
behave as before.
@BorjaIP
BorjaIP force-pushed the feat/preset-run-timeout-env branch from 1d59874 to 33f32d3 Compare October 9, 2026 14:47
The prompt and plugin presets carried identical copies of the run timeout
helper and its constants, kept equal by an AST comparison test. The presets
already bundle presets/finish_tool_hook.py into every generated tarball for the
same reason, so the resolver now lives in presets/run_timeout.py and ships the
same way. Both main.py files import it, and the tests exercise it directly
instead of extracting source nodes.

Tarballs stored before this change keep their own main.py, which does not import
the new module, so they are unaffected.
@BorjaIP
BorjaIP marked this pull request as ready for review October 9, 2026 22:00
@neubig
neubig requested a review from all-hands-bot October 16, 2026 05:12

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This review was posted by an AI agent (OpenHands).

Scope: In-repo and correctly owned — dispatcher env injection and preset tarball packaging both live in OpenHands/automation. Proceeding with the technical review.

What I verified

  • Head e8be15558e0c460a86054f77326f809ef795cfd0 matches; PR is open with type: feat, no linked closing issues, and no prior reviews or comments.
  • RemoteConversation.run(blocking=True, poll_interval=1.0, timeout=3600.0) is identical in the pinned SDK 1.53.0 and in 1.36.1, and _wait_for_run_completion raises ConversationRunError once elapsed exceeds timeout. The sandbox SDK always matches the service's pinned version (fetched from /sdk-version), so the timeout kwarg is always present.
  • AUTOMATION_RUN_TIMEOUT is exported as exactly effective_timeout, the same value passed to execute_in_context(timeout=...) and used for the bash command and watchdog deadline. It is serialized through the existing env-file path, so preset main.py reads it the same way as the other AUTOMATION_* variables.
  • The new presets/run_timeout.py is stdlib-only and is bundled next to main.py in both prompt and plugin tarballs (including the variant path, which reuses _generate_plugin_tarball). _replace_prompt_in_tarball leaves the baked-in main.py untouched, so stored automations are unaffected exactly as described.
  • On timeout the ConversationRunError propagates out of the workspace context, so __exit__ sends the failure completion callback — the intended path.
  • Ran locally: tests/test_preset_router.py (128 passed, 45 skipped; 19/19 in TestPresetRunTimeout), tests/test_backends.py + tests/test_execution.py (79 passed). Changed files compile. tests/test_dispatcher.py requires Docker (unavailable here) and its new timeout assertions depend on the PostgreSQL fixture. On GitHub the ci / Run tests / Docker runs for this head are action_required (a fork PR awaiting approval), so CI has not yet validated this head.

Non-blocking observation

The safety margin is computed as AUTOMATION_RUN_TIMEOUT - (time since main.py start) - 60, but the bash command timeout starts before tarball extraction and setup.sh (a fresh-venv uv pip install of the SDK), which are not subtracted. When setup takes longer than the 60 s margin on a long run, the bash kill can still fire first and the run falls back to the watchdog path. This is acknowledged in the PR and is no worse than current behavior, so it does not block, but it bounds how often the new path actually delivers the completion callback.

No material correctness, security, compatibility, or acceptance-criterion defect found, and the change is in scope for this repository.

✅ APPROVED

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

type: feat A new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants