Skip to content

fix(agent-server): reserve the run slot before a gateway turn saves its message - #5955

Merged
enyst merged 1 commit into
mainfrom
claude/umbrella-bug-fixes-tgz9ai-chat-run-limit-429
Oct 16, 2026
Merged

enyst merged 1 commit into
mainfrom
claude/umbrella-bug-fixes-tgz9ai-chat-run-limit-429

Conversation

@enyst

@enyst enyst commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

HUMAN:
This PR proposes a fix for a bug uncovered during the first full agentic run of verify-agent-server.


AGENT:

I'm an AI agent (Claude Code) helping Engel Nyst (@enyst) with project maintenance.

Requested by Engel · project thread

Decision needed

A maintainer should confirm the scope of the contract change:

  • Implemented: only the OpenAI gateway (POST /v1/chat/completions on a continued conversation) reserves the run slot before saving. Its 429 now saves nothing and says Conversation run limit reached. Retry the request later., the same as a new gateway conversation. The native POST /api/conversations/{id}/events keeps its documented contract (message saved, then 429 with a "retry /run without resending" hint, map bullet F06.send-run-limit). Reason: OpenAI clients retry by resending the same request and cannot call /run, while native clients already rely on the saved-message contract.
  • Alternative: make the native endpoint refuse before saving too (one rule for both). That changes a documented REST contract and the F06.send-run-limit bullet, so it is left out here.

Why

On a continued OpenAI-gateway conversation, a 429 (all max_concurrent_runs slots taken) still appended the user message. OpenAI SDKs retry a 429 automatically, so each refused try added another copy of the same user turn, and the retry that finally succeeded added one more.

Summary

  • EventService.reserve_run_slot() claims run capacity up front (or returns None when the conversation already holds its own run permit or still has a run task wrapping up, so turns on a running conversation are not refused). send_message takes an optional run_slot and passes it to run().
  • _run_agent (gateway) reserves before send_message and releases its handle afterwards.
  • Map: F30.chat-reuse-run-limit-429 now Requires: F30.chat-reuse-header (it searches for that bullet's QA_F30_SECOND turn, so --only failed on a missing prerequisite), and its known-bug marker, prose and Gotchas line are updated.

Issue Number

Fixes #5934

How to Test

  • uv run pytest tests/agent_server/test_run_admission.py tests/agent_server/test_event_service.py tests/agent_server/test_goal_loop.py tests/agent_server/test_openai_gateway_launch.py: 160 passed. The new test_gateway_turn_refused_before_message_is_saved fails on main (Message saved, but the conversation run limit was reached...) and passes here; test_reservation_skipped_while_conversation_holds_its_permit covers the running case. test_init_router.py, test_api_authentication.py, test_acp_router.py (74 passed) and the live-server gateway tests in tests/cross/test_remote_conversation_live_server.py -k "openai or gateway" (2 passed) also pass.
  • Map proof, control-agent-server map run --file F30 --only F30.chat-reuse-run-limit-429 --fresh --repeat 2: xfail twice with --checkout on main (6e7a853), xpass twice on this branch; after dropping the marker, map check is clean and the bullet passes.
  • uv run pre-commit run --files <changed files>: all passed.

Video/Screenshots

n/a

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes

  • Remaining narrow window: if the conversation holds a permit when the gateway checks (e.g. an active /goal loop, which a user message stops) and gives it up before run(), run() acquires afresh and can still answer the old "Message saved" 429. That needs a full server at that exact moment.
  • Other workers are changing _wait_for_reused_conversation_completion in the same module (F30.chat-reuse-concurrent / chat-reuse-confirmation); this PR only touches the send step in _run_agent.

🤖 Generated with Claude Code

https://claude.ai/code/session_014MKndVj7XWvRQmvW9w2bDg


Generated by Claude Code


🐳 Agent Server images for this PR — GHCR package, pull/run commands, and all pushed tags (click to expand)

• 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-slim amd64, arm64 python-node-runtime Link
python-minimal amd64, arm64 python-node-runtime Link
python amd64, arm64 python-node-runtime 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:5e4a57f-python

Run

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

All tags pushed for this build

ghcr.io/openhands/agent-server:5e4a57f-golang-amd64
ghcr.io/openhands/agent-server:5e4a57fd0d5c41a4598def933c83d6539da5628d-golang-amd64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-chat-run-limit-429-golang-amd64
ghcr.io/openhands/agent-server:5e4a57f-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:5e4a57f-golang-arm64
ghcr.io/openhands/agent-server:5e4a57fd0d5c41a4598def933c83d6539da5628d-golang-arm64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-chat-run-limit-429-golang-arm64
ghcr.io/openhands/agent-server:5e4a57f-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:5e4a57f-java-amd64
ghcr.io/openhands/agent-server:5e4a57fd0d5c41a4598def933c83d6539da5628d-java-amd64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-chat-run-limit-429-java-amd64
ghcr.io/openhands/agent-server:5e4a57f-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:5e4a57f-java-arm64
ghcr.io/openhands/agent-server:5e4a57fd0d5c41a4598def933c83d6539da5628d-java-arm64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-chat-run-limit-429-java-arm64
ghcr.io/openhands/agent-server:5e4a57f-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:5e4a57f-python-amd64
ghcr.io/openhands/agent-server:5e4a57fd0d5c41a4598def933c83d6539da5628d-python-amd64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-chat-run-limit-429-python-amd64
ghcr.io/openhands/agent-server:5e4a57f-python-node-runtime-amd64
ghcr.io/openhands/agent-server:5e4a57f-python-arm64
ghcr.io/openhands/agent-server:5e4a57fd0d5c41a4598def933c83d6539da5628d-python-arm64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-chat-run-limit-429-python-arm64
ghcr.io/openhands/agent-server:5e4a57f-python-node-runtime-arm64
ghcr.io/openhands/agent-server:5e4a57f-python-minimal-amd64
ghcr.io/openhands/agent-server:5e4a57fd0d5c41a4598def933c83d6539da5628d-python-minimal-amd64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-chat-run-limit-429-python-minimal-amd64
ghcr.io/openhands/agent-server:5e4a57f-python-node-runtime-minimal-amd64
ghcr.io/openhands/agent-server:5e4a57f-python-minimal-arm64
ghcr.io/openhands/agent-server:5e4a57fd0d5c41a4598def933c83d6539da5628d-python-minimal-arm64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-chat-run-limit-429-python-minimal-arm64
ghcr.io/openhands/agent-server:5e4a57f-python-node-runtime-minimal-arm64
ghcr.io/openhands/agent-server:5e4a57f-python-slim-amd64
ghcr.io/openhands/agent-server:5e4a57fd0d5c41a4598def933c83d6539da5628d-python-slim-amd64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-chat-run-limit-429-python-slim-amd64
ghcr.io/openhands/agent-server:5e4a57f-python-node-runtime-slim-amd64
ghcr.io/openhands/agent-server:5e4a57f-python-slim-arm64
ghcr.io/openhands/agent-server:5e4a57fd0d5c41a4598def933c83d6539da5628d-python-slim-arm64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-chat-run-limit-429-python-slim-arm64
ghcr.io/openhands/agent-server:5e4a57f-python-node-runtime-slim-arm64
ghcr.io/openhands/agent-server:5e4a57f-golang
ghcr.io/openhands/agent-server:5e4a57fd0d5c41a4598def933c83d6539da5628d-golang
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-chat-run-limit-429-golang
ghcr.io/openhands/agent-server:5e4a57f-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:5e4a57f-java
ghcr.io/openhands/agent-server:5e4a57fd0d5c41a4598def933c83d6539da5628d-java
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-chat-run-limit-429-java
ghcr.io/openhands/agent-server:5e4a57f-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:5e4a57f-python-minimal
ghcr.io/openhands/agent-server:5e4a57fd0d5c41a4598def933c83d6539da5628d-python-minimal
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-chat-run-limit-429-python-minimal
ghcr.io/openhands/agent-server:5e4a57f-python-node-runtime-minimal
ghcr.io/openhands/agent-server:5e4a57f-python-slim
ghcr.io/openhands/agent-server:5e4a57fd0d5c41a4598def933c83d6539da5628d-python-slim
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-chat-run-limit-429-python-slim
ghcr.io/openhands/agent-server:5e4a57f-python-node-runtime-slim
ghcr.io/openhands/agent-server:5e4a57f-python
ghcr.io/openhands/agent-server:5e4a57fd0d5c41a4598def933c83d6539da5628d-python
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-chat-run-limit-429-python
ghcr.io/openhands/agent-server:5e4a57f-python-node-runtime

About Multi-Architecture Support

  • Each variant tag (e.g., 5e4a57f-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., 5e4a57f-python-amd64) are also available if needed

…ts message

A continued OpenAI-gateway conversation answered 429 when the server's run
slots were full, but only after EventService.send_message had appended the
user message, so every client retry of the 429 added a duplicate user turn.

The gateway now reserves run capacity (EventService.reserve_run_slot) before
saving the message and hands the slot to run(). A full server refuses the
turn with "Conversation run limit reached. Retry the request later." and
leaves the conversation unchanged. A conversation that already holds its
own run permit needs no reservation, so turns on it behave as before. The
native POST /api/conversations/{id}/events contract is unchanged.

The F30.chat-reuse-run-limit-429 map bullet now requires
F30.chat-reuse-header (it searches for that bullet's turn) and its known-bug
marker is removed.

Fixes #5934

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014MKndVj7XWvRQmvW9w2bDg
@enyst enyst self-assigned this Oct 14, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-agent-server/openhands/agent_server
   event_service.py103612188%154, 160–161, 280–281, 404, 408, 413, 440, 447, 481, 484–485, 489–490, 501, 507, 536–538, 541–542, 631–632, 753, 773, 800, 824–825, 829, 837, 840, 877, 912, 916–917, 952, 959, 965, 1025, 1028, 1043–1044, 1132, 1162, 1165, 1243, 1261, 1275, 1433–1434, 1477, 1514, 1654–1657, 1661, 1704, 1708, 1715, 1729, 1743, 1767, 1814–1816, 1900, 1925, 1931, 1933, 1943, 1945, 1952, 1960, 1962–1963, 1967, 2015, 2021, 2030–2031, 2037–2040, 2048–2051, 2100–2102, 2162–2163, 2165–2172, 2174–2175, 2184–2185, 2187–2188, 2195–2196, 2198–2199, 2210, 2235, 2241, 2247, 2256–2257, 2279, 2289
openhands-agent-server/openhands/agent_server/openai
   service.py2845481%91, 105–106, 110–111, 119, 123–133, 135, 139–141, 145, 160, 189–191, 195–197, 231, 243, 248, 283–284, 358, 363, 375, 383, 388, 397, 415–418, 515–516, 571–572, 578, 588–589, 620, 631, 698
TOTAL497471981860% 

@github-actions

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@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.

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

Review of #5955 — reserve the run slot before a gateway turn saves its message

Scope: In-repo and correctly owned. The change stays inside openhands-agent-server (the canonical Agent Server) and reuses the existing RunSlot capacity mechanism rather than adding a parallel one, which matches the linked issue's stated non-goals and the ownership guidance in AGENTS.md / .agents/skills/custom-codereview-guide.md. No product/architecture decision is needed.

What I verified on head 5e4a57f:

  • The diff only adds an up-front reservation on the OpenAI gateway's continued-conversation path. _run_agent calls reserve_run_slot() before send_message, and releases the handle in a finally. The native POST /api/conversations/{id}/events path (event_router.py) and send_message's "Message saved…" rewrap are untouched, so the documented F06.send-run-limit contract is preserved.
  • reserve_run_slot() correctly returns None (no fresh token) when the conversation already holds _run_session_slot or still has a live _run_task, so a turn on an already-running conversation is not spuriously refused — the linked issue's second acceptance criterion.
  • run() consumes a supplied run_slot via owner = run_slot.share() on the first run of a chain, and explicitly releases a redundant reservation when a session is already held. No token leak on any of the paths I traced.
  • The 429 now surfaces with the plain Conversation run limit reached. Retry the request later. detail (raised by RunSlot.acquire) rather than the native /run retry hint, matching the issue's expected detail.
  • The feature-map updates are internally consistent: F30.chat-reuse-run-limit-429 drops the # bug marker, its Requires: moves to F30.chat-reuse-header (which the recipe genuinely depends on for the QA_F30_SECOND turn), and the Gotchas line now states the 429 precedes the save.

Checks: Current-head CI is fully green (including agent-server-tests, cross-tests, REST API (OpenAPI), pre-commit, security, and Validate PR description). I ran the focused suites locally on this head: tests/agent_server/test_run_admission.py (17 passed, incl. the new test_gateway_turn_refused_before_message_is_saved and test_reservation_skipped_while_conversation_holds_its_permit), test_event_service.py/test_goal_loop.py/test_openai_gateway_launch.py (143 passed), and test_init_router.py/test_api_authentication.py/test_acp_router.py (74 passed).

Notes (not blocking):

  • The author already documents a remaining narrow window (a /goal loop that holds a permit when the gateway checks and yields it before run()). This is a pre-existing, timing-dependent gap, not a regression introduced here.
  • The new test asserts spare._run_task is None, a private attribute; this is acceptable alongside the behavioral assertions (event count unchanged, last_user_message_id is None, tracker counts) that already pin the observable outcome.
  • The linked issue #5934 is open with bug, priority:medium, and ready-for-dev; this PR addresses the gateway-only scope it defines.

No material correctness, security, or compatibility defect found on the current head.

✅ APPROVED

@all-hands-bot
all-hands-bot requested a review from neubig October 14, 2026 03:30
@enyst
enyst merged commit 4a5f5a0 into main Oct 16, 2026
60 checks passed
@enyst
enyst deleted the claude/umbrella-bug-fixes-tgz9ai-chat-run-limit-429 branch October 16, 2026 22:03
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.

[Bug]: A 429 (run limit) on a continued OpenAI-gateway conversation still saves the user message, so each retry appends a duplicate

4 participants