Skip to content

fix(agent-server): re-run input stranded in a finished run's cleanup tail - #3364

Merged
VascoSch92 merged 2 commits into
mainfrom
fix/run-tail-strand-rearm
May 24, 2026
Merged

VascoSch92 merged 2 commits into
mainfrom
fix/run-tail-strand-rearm

Conversation

@VascoSch92

@VascoSch92 VascoSch92 commented May 22, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes #3363.

A user message delivered over the events WebSocket while the previous run is still in its post-finish cleanup tail (EventService._run_and_publish awaiting wait_for_pending) was appended to the conversation but never triggered an agent run — the user sees no response and has to resend. send_message(run=True) suppresses run()'s conversation_already_running while _run_task is wrapping up, and once conversation.run() has returned nothing re-runs the message.

Fix

Re-arm a run at the end of _run_and_publish when the conversation is left IDLE. That state only arises when send_message reset a terminal FINISHED/STUCK status during the tail — i.e. there is pending input with no run scheduled. Otherwise the run ends FINISHED and the re-arm is a no-op (zero overhead on the normal path).

Safe because:

  • _run_task is already None and run() re-checks under _run_lock, so a concurrent message's own run() and the re-arm can't both start a run.
  • The re-armed run ends FINISHED → the next check is false, so it terminates (it only chains while real input keeps landing in each tail).
  • PAUSED / WAITING_FOR_CONFIRMATION / ERROR are not reset to IDLE by send_message, so they're never auto-re-armed.

This is distinct from the in-flight case, which LocalConversation.run() already handles by deliberately not breaking on FINISHED.

Test

test_message_in_run_cleanup_tail_is_not_stranded drives a real conversation, parks the first run in its wait_for_pending tail, sends a second message into that window, releases the tail, and asserts the second message gets processed. It fails on main (call_count=1, stuck IDLE) and passes with this change.

tests/agent_server/test_event_service.py: 81 passed

Agent Server images for this PR

• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server

Variants & Base Images

Variant Architectures Base Image Docs / Tags
java amd64, arm64 eclipse-temurin:17-jdk Link
python amd64, arm64 nikolaik/python-nodejs:python3.13-nodejs22-slim Link
golang amd64, arm64 golang:1.21-bookworm Link

Pull (multi-arch manifest)

# Each variant is a multi-arch manifest supporting both amd64 and arm64
docker pull ghcr.io/openhands/agent-server:c51e488-python

Run

docker run -it --rm \
  -p 8000:8000 \
  --name agent-server-c51e488-python \
  ghcr.io/openhands/agent-server:c51e488-python

All tags pushed for this build

ghcr.io/openhands/agent-server:c51e488-golang-amd64
ghcr.io/openhands/agent-server:c51e48829d5f065100102485e56b528132f07a6b-golang-amd64
ghcr.io/openhands/agent-server:fix-run-tail-strand-rearm-golang-amd64
ghcr.io/openhands/agent-server:c51e488-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:c51e488-golang-arm64
ghcr.io/openhands/agent-server:c51e48829d5f065100102485e56b528132f07a6b-golang-arm64
ghcr.io/openhands/agent-server:fix-run-tail-strand-rearm-golang-arm64
ghcr.io/openhands/agent-server:c51e488-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:c51e488-java-amd64
ghcr.io/openhands/agent-server:c51e48829d5f065100102485e56b528132f07a6b-java-amd64
ghcr.io/openhands/agent-server:fix-run-tail-strand-rearm-java-amd64
ghcr.io/openhands/agent-server:c51e488-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:c51e488-java-arm64
ghcr.io/openhands/agent-server:c51e48829d5f065100102485e56b528132f07a6b-java-arm64
ghcr.io/openhands/agent-server:fix-run-tail-strand-rearm-java-arm64
ghcr.io/openhands/agent-server:c51e488-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:c51e488-python-amd64
ghcr.io/openhands/agent-server:c51e48829d5f065100102485e56b528132f07a6b-python-amd64
ghcr.io/openhands/agent-server:fix-run-tail-strand-rearm-python-amd64
ghcr.io/openhands/agent-server:c51e488-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-amd64
ghcr.io/openhands/agent-server:c51e488-python-arm64
ghcr.io/openhands/agent-server:c51e48829d5f065100102485e56b528132f07a6b-python-arm64
ghcr.io/openhands/agent-server:fix-run-tail-strand-rearm-python-arm64
ghcr.io/openhands/agent-server:c51e488-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-arm64
ghcr.io/openhands/agent-server:c51e488-golang
ghcr.io/openhands/agent-server:c51e48829d5f065100102485e56b528132f07a6b-golang
ghcr.io/openhands/agent-server:fix-run-tail-strand-rearm-golang
ghcr.io/openhands/agent-server:c51e488-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:c51e488-java
ghcr.io/openhands/agent-server:c51e48829d5f065100102485e56b528132f07a6b-java
ghcr.io/openhands/agent-server:fix-run-tail-strand-rearm-java
ghcr.io/openhands/agent-server:c51e488-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:c51e488-python
ghcr.io/openhands/agent-server:c51e48829d5f065100102485e56b528132f07a6b-python
ghcr.io/openhands/agent-server:fix-run-tail-strand-rearm-python
ghcr.io/openhands/agent-server:c51e488-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim

About Multi-Architecture Support

  • Each variant tag (e.g., c51e488-python) is a multi-arch manifest supporting both amd64 and arm64
  • Docker automatically pulls the correct architecture for your platform
  • Individual architecture tags (e.g., c51e488-python-amd64) are also available if needed

…tail

A user message delivered over the events WebSocket while the previous run is
still in its _run_and_publish wait_for_pending tail had its run() rejected as
conversation_already_running and suppressed, and nothing re-ran it once the
tail cleared — so the message sat unprocessed until the next send (no agent
response; resend appears to work). The in-flight case is already handled by
LocalConversation.run(); this closes the post-run executor-tail gap.

Re-arm a run at the end of _run_and_publish when the conversation is left
IDLE, which only happens when send_message reset a terminal FINISHED/STUCK
status during the tail (pending input, no run scheduled). Zero overhead on
the normal path, where status is FINISHED.

Refs #3363
@github-actions

github-actions Bot commented May 22, 2026 •

Copy link
Copy Markdown
Contributor

Python API breakage checks — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented May 22, 2026 •

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented May 22, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-agent-server/openhands/agent_server
   event_service.py4679779%87–88, 118, 121–122, 126–127, 134, 138, 144, 154–158, 161–164, 223, 244–245, 316, 367, 377, 401–402, 406, 414, 417, 475, 477, 481–483, 487, 496–497, 499, 503, 509, 511, 558, 588, 591, 642, 646, 838, 840–841, 845, 859–861, 863, 884, 889–892, 896–899, 907–910, 916–919, 965–966, 968–975, 977–978, 987–988, 990–991, 998–999, 1001–1002, 1022, 1028, 1034, 1043–1044
TOTAL281601217556% 

all-hands-bot

This comment was marked as duplicate.

all-hands-bot

This comment was marked as outdated.

Using IDLE as the proxy for a suppressed send_message(run=True) was too
broad: a deliberate run=False append (a real HTTP path via event_router) or
an IDLE reached another way (e.g. confirmation rejection) would wrongly
trigger a run. Track request intent instead — set _rerun_requested only when
a run=True is rejected as conversation_already_running, and consume it in
_run_and_publish (still gated on IDLE so an already-absorbed message doesn't
re-run). Add a run=False-during-tail regression test.

Refs #3363
@VascoSch92
VascoSch92 requested a review from all-hands-bot May 22, 2026 20:27

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🟢 Taste Rating: Good taste — targeted fix, and the explicit _rerun_requested flag addresses the prior run=False concern.

I didn’t find any code issues that need to be addressed. Targeted verification passed: uv run pytest tests/agent_server/test_event_service.py -k 'message_in_run_cleanup_tail_is_not_stranded or run_false_message_in_cleanup_tail_is_not_run' (2 passed).

[RISK ASSESSMENT]

  • [Overall PR] ⚠️ Risk Assessment: 🟡 MEDIUM — this touches agent-server run scheduling / conversation loop behavior. Per repo policy, I’m leaving a COMMENT rather than APPROVE and deferring merge/eval-risk sign-off to a human maintainer.

VERDICT: ✅ Worth merging from code review; needs human maintainer sign-off for the run-scheduling/eval-risk policy.

KEY INSIGHT: Preserving explicit run=True intent is the right data flow; IDLE alone is no longer used as a proxy.

This review was created by an AI agent (OpenHands) on behalf of the user.


Was this automated review useful? React with 👍 or 👎 to this review to help us measure review quality.
Workflow run: https://github.com/OpenHands/software-agent-sdk/actions/runs/26310353309

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Code Review

The re-arm approach is well-structured. The key improvement over the previous iteration is using an explicit _rerun_requested flag rather than inferring intent from IDLE status — this directly addresses the concern raised in the prior review and correctly preserves the run=False API contract. The negative-case test (test_run_false_message_in_cleanup_tail_is_not_run) is a particularly good addition: it guards the explicit-intent contract and would have caught the earlier IDLE-proxy design.

Two minor issues are worth noting inline.


This review was generated by an AI agent (OpenHands) on behalf of the user via OpenHands Automation.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

✅ QA Report: PASS

Verified the run-tail race fix with a real ConversationService/EventService flow: the pre-fix behavior strands the second message, while this PR processes it after cleanup-tail completion and preserves run=False append behavior.

Does this PR achieve its stated goal?

Yes. The PR set out to fix a message sent with run=True while the previous run is still in wait_for_pending() cleanup being appended but never processed. I reproduced that exact race with the base event_service.py (call_count stayed 1, status ended idle), then reran the same script on the PR branch and observed the second message trigger a follow-up agent turn (call_count=2, status finished). I also exercised the related run=False behavior and confirmed it did not auto-run (call_count stayed 1).

Phase Result
Environment Setup ✅ uv sync --dev completed and the QA script ran via uv run python
CI Status ⏳ At check time: 32 success, 4 skipped, 1 in progress (qa-changes)
Functional Verification ✅ Before/after race reproduced and fixed; run=False side effect checked
Functional Verification

Test 1: cleanup-tail run=True message is no longer stranded

Step 1 — Reproduce baseline without the fix:
Temporarily restored openhands-agent-server/openhands/agent_server/event_service.py from origin/main, then ran:

OPENHANDS_SUPPRESS_BANNER=1 uv run python -u /tmp/qa_tail_race.py

Observed:

after first: call_count=1, status=finished, task_alive=True
during tail after second send: run=True, call_count=1, same_task=True, status=idle
final: call_count=1, status=idle, run_task=None
RESULT=UNEXPECTED
script_exit=1

This confirms the bug: the second user message reset the conversation to idle during the first run's cleanup tail, but no second agent turn ran after the tail cleared.

Step 2 — Apply the PR's changes:
Restored the PR branch version of event_service.py and reran the same real service flow.

Step 3 — Re-run with the fix in place:

OPENHANDS_SUPPRESS_BANNER=1 uv run python -u /tmp/qa_tail_race.py

Observed:

after first: call_count=1, status=finished, task_alive=True
during tail after second send: run=True, call_count=1, same_task=True, status=idle
final: call_count=2, status=finished, run_task=None
RESULT=EXPECTED
script_exit=0

This confirms the fix: the second message remained pending during cleanup, then the PR re-armed the run once the cleanup tail finished.

Test 2: cleanup-tail run=False append does not auto-run

Ran the same setup on the PR branch, but appended the second message with run=False:

OPENHANDS_SUPPRESS_BANNER=1 uv run python -u /tmp/qa_tail_race.py run-false

Observed:

after first: call_count=1, status=finished, task_alive=True
during tail after second send: run=False, call_count=1, same_task=True, status=idle
final: call_count=1, status=idle, run_task=None
RESULT=EXPECTED
script_exit=0

This shows the re-arm is tied to explicit run intent and does not unexpectedly process a deliberate append-only message.

Issues Found

None.

This QA review was created by an AI agent (OpenHands) on behalf of the user.

@VascoSch92
VascoSch92 merged commit 41169ac into main May 24, 2026
43 of 44 checks passed
@VascoSch92
VascoSch92 deleted the fix/run-tail-strand-rearm branch May 24, 2026 13:46
StressTestor pushed a commit to StressTestor/software-agent-sdk that referenced this pull request Jun 1, 2026
shanemort1982 added a commit to shanemort1982/software-agent-sdk that referenced this pull request Jul 16, 2026
…asks

Fixes OpenHands#3842.

Two defects compounded into permanently wedged conversations:

1. WebhookSubscriber._post_events retried on a fixed delay with no backoff,
   ignored Retry-After, ran overlapping volleys, and after exhausting retries
   only tried again when a NEW event arrived - so against a rate-limited
   downstream (the OpenHands app server rate-limits /api/v1/webhooks/events/*
   like any other route) delivery stalled forever and the queue trim dropped
   events permanently.

2. EventService.run() raised conversation_already_running whenever _run_task
   was alive, even with execution_status reporting non-RUNNING; a run task
   that never completes therefore blocked the conversation forever - the
   self-heal from OpenHands#3364 never fires because the task never reaches its
   cleanup tail.

Webhook poster: serialise flushes; exponential backoff honouring Retry-After;
exponentially growing cooldown (1s floor, 60s cap) instead of hammering;
re-arm the flush timer on failure so delivery resumes without new events;
prepend re-queued events to preserve delivery order; enforce the queue bound
on enqueue as well as on failed-flush trim.

EventService.run(): when a live _run_task coexists with non-RUNNING status,
start a staleness clock; reap the same task if still lingering past
STALE_RUN_TASK_REAP_SECONDS (120s, well beyond the 30s-bounded
wait_for_pending tail) and start the requested run. The task finally block
now only clears its own task reference so a reaped task cannot clobber its
replacement.

Rebased onto main and reconciled with the webhook_subscriber refactor.
pengzhang-create pushed a commit to Pyromind-Dynamics/software-agent-sdk that referenced this pull request Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

agent-server: message arriving during a finished run's wait_for_pending tail is never processed (stranded until next send)

3 participants