Repository navigation
fix(agent-server): reserve the run slot before a gateway turn saves its message - #5955
Conversation
…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
Coverage Report •
|
||||||||||||||||||||||||||||||
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
all-hands-bot
left a comment
There was a problem hiding this comment.
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_agentcallsreserve_run_slot()beforesend_message, and releases the handle in afinally. The nativePOST /api/conversations/{id}/eventspath (event_router.py) andsend_message's "Message saved…" rewrap are untouched, so the documentedF06.send-run-limitcontract is preserved. reserve_run_slot()correctly returnsNone(no fresh token) when the conversation already holds_run_session_slotor 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 suppliedrun_slotviaowner = 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 byRunSlot.acquire) rather than the native/runretry hint, matching the issue's expected detail. - The feature-map updates are internally consistent:
F30.chat-reuse-run-limit-429drops the# bugmarker, itsRequires:moves toF30.chat-reuse-header(which the recipe genuinely depends on for theQA_F30_SECONDturn), 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
/goalloop that holds a permit when the gateway checks and yields it beforerun()). 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, andready-for-dev; this PR addresses the gateway-only scope it defines.
No material correctness, security, or compatibility defect found on the current head.
✅ APPROVED
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:
POST /v1/chat/completionson a continued conversation) reserves the run slot before saving. Its 429 now saves nothing and saysConversation run limit reached. Retry the request later., the same as a new gateway conversation. The nativePOST /api/conversations/{id}/eventskeeps its documented contract (message saved, then 429 with a "retry/runwithout resending" hint, map bulletF06.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.F06.send-run-limitbullet, so it is left out here.Why
On a continued OpenAI-gateway conversation, a 429 (all
max_concurrent_runsslots 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 returnsNonewhen 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_messagetakes an optionalrun_slotand passes it torun()._run_agent(gateway) reserves beforesend_messageand releases its handle afterwards.F30.chat-reuse-run-limit-429nowRequires: F30.chat-reuse-header(it searches for that bullet'sQA_F30_SECONDturn, so--onlyfailed 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 newtest_gateway_turn_refused_before_message_is_savedfails on main (Message saved, but the conversation run limit was reached...) and passes here;test_reservation_skipped_while_conversation_holds_its_permitcovers the running case.test_init_router.py,test_api_authentication.py,test_acp_router.py(74 passed) and the live-server gateway tests intests/cross/test_remote_conversation_live_server.py -k "openai or gateway"(2 passed) also pass.control-agent-server map run --file F30 --only F30.chat-reuse-run-limit-429 --fresh --repeat 2:xfailtwice with--checkouton main (6e7a853),xpasstwice on this branch; after dropping the marker,map checkis clean and the bulletpasses.uv run pre-commit run --files <changed files>: all passed.Video/Screenshots
n/a
Type
Notes
/goalloop, which a user message stops) and gives it up beforerun(),run()acquires afresh and can still answer the old "Message saved" 429. That needs a full server at that exact moment._wait_for_reused_conversation_completionin 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
eclipse-temurin:17-jdkpython-node-runtimepython-node-runtimepython-node-runtimegolang:1.21-bookwormPull (multi-arch manifest)
# Each variant is a multi-arch manifest supporting both amd64 and arm64 docker pull ghcr.io/openhands/agent-server:5e4a57f-pythonRun
All tags pushed for this build
About Multi-Architecture Support
5e4a57f-python) is a multi-arch manifest supporting both amd64 and arm645e4a57f-python-amd64) are also available if needed