feat(api_server): add POST /v1/runs/{run_id}/stop (salvage of #15656) - #15842
Merged
Conversation
Add ability to interrupt a running agent via the runs API. Previously /v1/runs could start a run and subscribe to events, but there was no way to cancel it. The new endpoint stores agent and task references during execution, calls agent.interrupt() to stop LLM calls, then cancels the asyncio task. Includes 15 tests covering start, events, and stop scenarios.
task.cancel() can't preempt the run_in_executor thread running run_conversation(), so we rely on agent.interrupt() to wake the loop. Without a timeout, a slow/unresponsive interrupt blocks the HTTP response indefinitely. Wrap the await in wait_for(shield(task), 5.0) and log a warning on timeout. Also tidy one extra space in the module docstring's /stop entry.
2 tasks
9 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Salvages PR #15656 by @EKKOLearnAI onto current main with a bounded-wait fix.
Summary
Adds
POST /v1/runs/{run_id}/stopto interrupt agents started viaPOST /v1/runs. Callsagent.interrupt(), cancels the asyncio task, and cleans up refs — but caps the await at 5s so the handler can't hang when the agent is slow to respond to the interrupt.Changes
gateway/platforms/api_server.py: contributor's stop endpoint + agent/task ref dicts + cleanup wiring (unchanged from feat(api_server): add POST /v1/runs/{run_id}/stop endpoint #15656)gateway/platforms/api_server.py: follow-up — wrapawait taskinasyncio.wait_for(shield(task), 5.0)with TimeoutError warning, so an unresponsiveagent.interrupt()can't block the handler indefinitely.run_conversation()runs in the default executor whichtask.cancel()cannot preempt.tests/gateway/test_api_server_runs.py: 15 new tests (start/events/stop, auth, 404, interrupt, cleanup, exception paths) — unchanged from feat(api_server): add POST /v1/runs/{run_id}/stop endpoint #15656scripts/release.py: AUTHOR_MAP entry for fqsy1416@gmail.com → EKKOLearnAIValidation
scripts/run_tests.sh tests/gateway/test_api_server_runs.py— 15/15 pass.Closes #15656 (with credit preserved via cherry-pick authorship).