Repository navigation
feat(crew-api): let the owner cancel a running turn by its id - #45463
Conversation
POST /v1/responses/{response_id}/cancel stops a turn POST /v1/responses
runs, for the machine owner alone, and answers the turn's Response.
- A running turn is stopped with the stop a caller that left sends.
The answer waits until the request that runs the turn has kept it:
cancelled when the stop cut it short, or the state the turn reached
when it ended by itself first.
- A turn that had ended answers its Response unchanged, so a repeated
call answers as the first did. Idempotency-Key and the body are not
read.
- A turn that has not ended within the stop limit answers in_progress:
the stop was sent, and the caller reads the turn again.
- An id that names no kept or running turn is 404 not_found.
- A tool approval the turn waits on is refused in the step that starts
the stop and reads expired with reason turn_cancelled. Neither the
dashboard nor the decision route can allow it afterwards. A turn in
a conversation writes approval.decided for it ahead of its
turn.completed. An approval the dashboard's Stop ends still reads
denied.
- The approval's record is written after the refusal, no later than
when the stop returns. A process that ends before that write reads
the approval gateway_restart.
Not built: serving it to an app credential, an id for a turn answered
as JSON before it ends.
Closes #45368
UX Review (Opus 5.5) — ⏭️ skippedRevision |
Design Review (Opus 5.5) — ✅ PASSDesign-level review of Design-Verdict: PASS Nothing blocks: the change only adds things, and the new cancel route reuses the stop that Notes
[DESIGN-REVIEWED] f1d530b Description read[DESCRIPTION-READ] 27dbb80f9d7aa3166ea46af4871f2ad589dec3fa7ec925512131061567b566ae That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
First Principles Review (Opus 5.5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Nothing blocks. The approval refusal and Notes
What this change shipsInventory (7 items) — 6 justifiedIntent: let the machine owner stop a running Crew API turn by its id, also from another process. ADDITION.
[FIRST-PRINCIPLES-REVIEWED] f1d530b Description read[DESCRIPTION-READ] f5ffed35a86f588bdf31b5f21ce090d1a5a2bd5c66634eaae73d7a60998848ea That sha256 names the PR description this verdict read. Recompute it with |
Opus 5.5 Review — ✅ no blocking findingsReviewed Verdict parsed from the review's SHA-scoped output markers for commit False positive or not applicable? A repository writer can comment: |
GPT 6.1 Review — ✅ no blocking findingsGPT 6.1 completed its review of This comment is updated in place on each push. Review detailsNo findings. False positive or not applicable? A repository writer can comment: |
Code change approver deep review: A person should decideThis review does not approve this head. It confirmed one documentation item, and a maintainer decides whether it holds the merge. The item is not a code error, so this is not a change request. The confirmed item
Smallest fix: rewrite the sentence at 1457-1458 to say that a turn cancelled through What the review verified on this head
Done well: the route answers through the lookup of the read route, so redaction, the 404 for a turn that is not kept and the audit row are shared and not written a second time. One more point for the maintainer: the description says six sentences in Optional:
Tier: 2, held for a person: 21 files, more than 20. A deep review by the code change approver, not by a person · commit |
bolichen97
left a comment
There was a problem hiding this comment.
Approved on head f1d530b.
POST /v1/responses/{response_id}/cancel goes through the owner gate (owner_route) and then _host_owner_refusal, so a Shared Crew teammate, an app credential or an internal caller gets 403 owner_only before anything is read or stopped. A running turn is stopped through the same stop_slot_turn a leaving caller uses (escalate=False). In that same event-loop step, stop_turn_refusing_its_approvals refuses the pending native prompts for that session, so no later answer can allow one of them. A subagent's background request is left alone, as Stop leaves it. The wait for the turn's record is bounded by agent.soft_stop_budget_secs. An unknown or unkept id is 404, and a repeat call returns the same Response. The real-machine run shows main answering 404 and this head passing all 28 probe checks against a no-model gateway on 127.0.0.1.
AI-assisted, sent by Bolin Review bot
Problem / Motivation
Goal: The machine owner can stop a turn that
POST /v1/responsesruns, by the turn's id, through the Crew API:POST /v1/responses/{response_id}/cancelstops it and answers the turn'sResponse.The owner can run a turn and read it back. A client that holds the turn's id (from the first event of a streamed answer, or from
turn.startedon the conversation's event stream) had no call that stops the turn while it runs. The only way to stop it through the API was to close the connection of the request that runs it.Why it matters
A client with no dashboard needs to stop a turn it started, also from another process than the one that holds the turn's request open. The contract's cancel route is the call for that, and nothing served it.
Not a goal
What changed (motivation → approach → change)
Goal → the cancel route finds the running turn by its id, stops its session with the stop the turn route already sends when its caller leaves, and answers the turn as a read does → one new module,
src/kiro_crew/dashboard/crew_api_response_cancel.py, one new function insrc/kiro_crew/dashboard/crew_api_approvals.py, and three fields the turn route keeps for a running turn insrc/kiro_crew/dashboard/crew_api_responses.py.POST /v1/responses/{response_id}/cancelby the machine ownerResponsewithstatus: cancelledcompletedorfailed)status: in_progress: the stop was sent, and the caller reads the turn againResponseunchanged; a second call answers as the first didnot_foundowner_only, before anything is read or stoppedstop_slot_turn(source="crew_api", escalate=False), the call the turn route makes for a caller that left. The cancel route then waits until the request that runs the turn has kept it, for at most the engine's cancel-acknowledgement budget (agent.soft_stop_budget_secs, 10 seconds unless set), and answers through the read route's own lookup, so the answer is redacted and audited as a read is.deny, through the decision registry the approval routes use) and recordedexpiredwith reasonturn_cancelled. The scan for waiting prompts, the refusal, the record and the start of the stop are one step on the event loop. So neither the dashboard nor the decision route can allow an approval recorded that way, and the turn cannot ask again before the stop reaches it. An approval answered before the cancel keeps its answer.approval.decidedwithexpiredandturn_cancelled, ahead ofturn.completedwithcancelled. A prompt the turn had not announced yet gets itsapproval.requestedfirst, after its record is stored.expiredwith reasongateway_restartafter the restart. A turn whose stop did not take and that endscompletedorfailedkeeps the approval'sturn_cancelledreading: the cancel refused the tool. A background request that names the session (a subagent's) is left waiting. When the approval records cannot be read, nothing is refused there and the stop ends the turn's prompts itself.Idempotency-Keyand the request body.crew-public.yamlmarks the operationavailabletoowner,preview.crew-owner.yamland the Owner fixture addturn_cancelledto an approval'sexpired_reason. The reference pages, the RFC status line, three module specs and the feature map follow.Six sentences are proposed amendments, waiting for a maintainer's sign-off, each marked in the operation's description in
crew-public.yaml.The config signal is the
x-crew-available-tomark in the OpenAPI file.Backwards compatibility
POST /v1/responses/{response_id}/cancelanswered 404 and now answers the owner. An app credential gets 403owner_only.approval.decidedevent, can now carryexpired_reason: turn_cancelled, a value the Public contract already names. It is written only by this route.POST /v1/responsesand the two read routes answer as before. The approval events of a turn that is not cancelled are written as before. The dashboard's Stop is unchanged, and an approval it ends still readsdenied.turn_cancelledrecord readable: the record file takes any text as a reason.Files this change shares with open PRs: #43525 changes one line of
crew_api_responses.pyand adds lines toroute_access.pyand the feature map. It deletes or rewrites no line this change touches.Tests
test/test_crew_api_response_cancel.py(new, 31 tests)stop_slot_turn; a streamed turn's stream ending and itsturn.completedwithcancelled; two calls at once; the stop sent assource="crew_api", escalate=False; a turn that is not kept answering 404 and not being stopped; every caller but the owner refused before a read or a stop; the audit row; the spec's marks, with a key sent twice and a body changing nothing. The approval:turn_cancelledbeside thedeniedof a Stop in one test; an answer of allow made from inside the engine's stop (two dashboard entries and the decision route) refused; the reading kept when the turn then endscompletedorfailed; an approval answered before the cancel; the approval refused before the stop while its record on disk still readspending, what a next process reads of that file, andturn_cancelledonce written; the records unreadable. The event stream:approval.requested,approval.decidedwithturn_cancelledandturn.completed, in that order, for a prompt the turn had announced and for one it had not.test/test_crew_api_scopes.pytest/e2e/crew_api_contract_cases/post_v1_responses_response_id_cancel.py(new)The contract suite does not hold a turn that is cancelled while it runs. The one turn its cases can hold is a turn that waits on a tool approval, and the stand-in engine does not hear a stop during that wait. That case is
test_a_running_turn_is_stopped_and_answers_cancelledandtest_a_streamed_turn_is_stopped_and_its_stream_ends, and step 1 of the real-machine run below. The turn that has not ended within the limit istest_a_turn_not_ended_within_the_limit_answers_in_progress, and step 4 below.On this head (f1d530b), on a development host: the new test file and the 15 other test files that import or name a changed module or route passed, with mypy, black, isort, flake8, docs-lint, the flake ratchet and the repository's semgrep rules. The full suites are CI's and were not run locally.
On a 64 vCPU Linux EC2, on this head's tree: the contract suite (
KIROCREW_E2E=1,test/e2e/test_crew_api_contract.py) passed, 142 cases, and the four test files nearest the change passed (392 tests).scripts/local-gate.py --tree-scansran there on an earlier commit of this PR (f0d759ea6c8d), with a new, empty home directory. Since that commit, 5 files changed, 99 insertions and 16 deletions: how the event stream is told of a refused prompt, and its test. On that commit the frontend set passed (563 files), the second tree-scan batch passed (22914), and two tests failed,test_route_access_labels.py::test_every_registered_route_is_labelled_or_baselinedandtest_dashboard_server_composition_contract_reach.py::test_the_dashboard_boot_keeps_its_chain_hooks_and_order. Both fail the same way on the base commit (16a28a1) in the same environment, each over eight/app-windows/...routes this change does not touch. They are not changed here.Two local reviews (GPT and Opus) read this patch in five rounds. The first found that the feature map's row named the route in its text and not in its lists of modules and endpoints. The second found nothing that blocks. Main then gained the approval events, and the commit was rebased onto it: one conflict, in the RFC's status line, was resolved by hand, keeping main's text and this change's sentence. The third and fourth rounds each found a window in which a prompt the cancel refused was missing from the conversation's event stream; the stream is now told in the step that refuses the prompt. The fifth round, on this commit, found nothing.
Determinism: the new test file passed once in order and once with its cases in reverse order at
-n0on this head.Manual verification
N/A — the real-machine run below covers the manual path.
Real-machine verification
Machine: a Linux development host, Amazon Linux 2023, x86_64, 32 cores; Before and After ran there, one after the other, with the same script
Before (main 16a28a1, the commit this head sits on):
run_cancel_probe.sh <tree> <work dir> before --expect base->POST /v1/responses/{response_id}/cancelis 404After (head f1d530b):
run_cancel_probe.sh <tree> <work dir> after --expect head-> ALL PASS (28 checks)Cleanup: the gateway was stopped by the script after each run, 0 sockets left on its port
The script builds the tree into a scratch venv and runs it as a gateway with no dashboard, on 127.0.0.1, with its own data home. The engine is the repository's stand-in behind the contract suite's wrapper (
test/e2e/crew_api_contract_gated_engine.py). No model is called and no command is run. On the head:turn.startedon the conversation's event stream, and it readsin_progress. The cancel answers 200cancelledin 1.1 s, the turn's own stream ends, a read answers the sameResponse, and the event stream ends onturn.completedwithcancelled.Response.Responseunchanged.expiredwith reasonturn_cancelled, and allowing it afterwards is 409approval_already_resolved. This turn had not ended within the limit, and the cancel answered 200in_progress.denied.owner_only; no credential is 401; each alike for a turn that exists and one that does not.Not shown on a real machine: the approval events of a cancelled turn, a turn that ends by itself as the stop arrives, two cancels at once, an approval answered from the dashboard between the cancel and the stop, a gateway killed between the refusal and the record's write, the approval records unreadable (unit tests cover each), and a credential through the entry port. Not run: any of this on Windows.
Related Issues
Closes #45368
Refs #39451
Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...); PR Hygiene does not count commits