Skip to content

feat(crew-api): let the owner cancel a running turn by its id - #45463

Merged
bolichen97 merged 1 commit into
mainfrom
crew/headless-crew/v1-cancel
Dec 1, 2026
Merged

bolichen97 merged 1 commit into
mainfrom
crew/headless-crew/v1-cancel

Conversation

@buluoray

@buluoray buluoray commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: The machine owner can stop a turn that POST /v1/responses runs, by the turn's id, through the Crew API: POST /v1/responses/{response_id}/cancel stops it and answers the turn's Response.

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.started on 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

  • Serving the route to an app credential. It answers the machine owner only.
  • Giving a turn answered as JSON its id before it ends.
  • Cancelling a turn that is not kept (a request or a reply too large to keep): it cannot be named, so the cancel is 404.
  • Any change to what the dashboard's Stop does or to how an approval it ends reads.

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 in src/kiro_crew/dashboard/crew_api_approvals.py, and three fields the turn route keeps for a running turn in src/kiro_crew/dashboard/crew_api_responses.py.

POST /v1/responses/{response_id}/cancel by the machine owner Answer
The turn runs, and the stop cuts it short 200, the turn's Response with status: cancelled
The turn runs, and ends by itself while the stop is on its way 200, the state it reached (completed or failed)
The turn runs, and has not ended within the stop limit 200, status: in_progress: the stop was sent, and the caller reads the turn again
The turn had ended 200, its Response unchanged; a second call answers as the first did
An id that names no kept or running turn 404 not_found
Any of these from an app credential 403 owner_only, before anything is read or stopped
  • The stop. stop_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.
  • The running turn. The turn route already keeps each running turn by its id. It now also keeps the turn's session, a future it sets once it has kept the turn, and the turn's conversation stream.
  • A tool approval the turn waits on is refused (deny, through the decision registry the approval routes use) and recorded expired with reason turn_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.
  • The event stream. A turn that runs in a conversation is told of each refusal in that same step. Its stream then holds approval.decided with expired and turn_cancelled, ahead of turn.completed with cancelled. A prompt the turn had not announced yet gets its approval.requested first, after its record is stored.
  • Stated limits. The approval's record is written to disk after the refusal, no later than when the stop returns; a gateway process that ends before that write reads the approval expired with reason gateway_restart after the restart. A turn whose stop did not take and that ends completed or failed keeps the approval's turn_cancelled reading: 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.
  • Not read. Idempotency-Key and the request body.
  • Spec and docs. crew-public.yaml marks the operation available to owner, preview. crew-owner.yaml and the Owner fixture add turn_cancelled to an approval's expired_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-to mark in the OpenAPI file.

Backwards compatibility

  • POST /v1/responses/{response_id}/cancel answered 404 and now answers the owner. An app credential gets 403 owner_only.
  • An Owner approval, and an approval.decided event, can now carry expired_reason: turn_cancelled, a value the Public contract already names. It is written only by this route.
  • POST /v1/responses and 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 reads denied.
  • Rolling back to an earlier build leaves any turn_cancelled record 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.py and adds lines to route_access.py and the feature map. It deletes or rewrites no line this change touches.

Tests

File What is pinned
test/test_crew_api_response_cancel.py (new, 31 tests) The answers of the table above, with the real stop_slot_turn; a streamed turn's stream ending and its turn.completed with cancelled; two calls at once; the stop sent as source="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_cancelled beside the denied of 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 ends completed or failed; an approval answered before the cancel; the approval refused before the stop while its record on disk still reads pending, what a next process reads of that file, and turn_cancelled once written; the records unreadable. The event stream: approval.requested, approval.decided with turn_cancelled and turn.completed, in that order, for a prompt the turn had announced and for one it had not.
test/test_crew_api_scopes.py The route is owner-only
test/e2e/crew_api_contract_cases/post_v1_responses_response_id_cancel.py (new) On a real gateway, against the spec: an ended turn answered unchanged, the 404, the app's 403, and the two cases every operation gets for a missing and a foreign credential

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_cancelled and test_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 is test_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-scans ran 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_baselined and test_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 -n0 on 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}/cancel is 404
After (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:

  1. A streamed turn is held. Its id is read from turn.started on the conversation's event stream, and it reads in_progress. The cancel answers 200 cancelled in 1.1 s, the turn's own stream ends, a read answers the same Response, and the event stream ends on turn.completed with cancelled.
  2. A second cancel answers the same Response.
  3. A turn that had completed answers its Response unchanged.
  4. A turn that waits on a tool approval is cancelled: the approval reads expired with reason turn_cancelled, and allowing it afterwards is 409 approval_already_resolved. This turn had not ended within the limit, and the cancel answered 200 in_progress.
  5. Beside it, the same wait ended by the dashboard's Stop reads denied.
  6. The approval's record is on disk. After a gateway restart the cancelled turn, the conversation's history and both approvals read as before, and the turn that had not ended is 404.
  7. An id nobody ran is 404; an app credential is 403 owner_only; no credential is 401; each alike for a turn that exists and one that does not.
  8. The gateway listens on loopback only, and the command the engine asked approval for was not run.

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

  • A Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...); PR Hygiene does not count commits
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

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
@buluoray
buluoray requested review from a team and KiroCrewCaptain as code owners December 1, 2026 12:14
@buluoray
buluoray requested a review from iamwhatever December 1, 2026 12:14
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Dec 1, 2026
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

Revision f1d530b32bf058330ce87a563f276b397b681f23 touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Does not block merge.

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

Design-level review of f1d530b32bf058330ce87a563f276b397b681f23 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

Design-Verdict: PASS

Nothing blocks: the change only adds things, and the new cancel route reuses the stop that POST /v1/responses already sends when its caller leaves, plus the read route's own lookup.

Notes

  • Six spec sentences are marked as amendments still waiting for a maintainer's sign-off in crew-public.yaml. That sign-off has to land before the route leaves preview. Otherwise the contract will promise behaviour that nobody approved.

[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 .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of f1d530b32bf058330ce87a563f276b397b681f23 — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

First-Principles-Verdict: PASS

Nothing blocks. The approval refusal and turn_cancelled come from the RFC's own approval-states row (rfc-crew-api.md:499).

Notes

  • Item 2: one consumer, generalized. stop_turn_refusing_its_approvals(..., stop: Callable[[], Awaitable[Any]], refused: ... | None = None) has 1 caller (grep for stop_turn_refusing_its_approvals: 1 hit in crew_api_response_cancel.py). That caller always passes the same stop_slot_turn partial, and refused is None only when the turn has no stream. Subtraction: drop the stop callable. Call stop_slot_turn(state, slot, source="crew_api", escalate=False) directly, pass the turn's stream (or None) instead of the generic refused hook, and delete the functools.partial in the route.

What this change ships

Inventory (7 items) — 6 justified

Intent: let the machine owner stop a running Crew API turn by its id, also from another process. ADDITION.

  1. POST /v1/responses/{id}/cancel stops a running turn and returns its Response — justified
  2. A tool approval the cancelled turn waits on reads expired/turn_cancelled and can no longer be allowed — one consumer, generalized
  3. The conversation stream gets approval.requested (for prompts not yet announced) and approval.decided before turn.completed — justified
  4. A turn not ended within soft_stop_budget_secs returns 200 in_progress — justified
  5. A turn that already ended returns unchanged; 404 for an unkept id; 403 for app credentials — justified
  6. New audit operation crew_api.responses.cancel and a route-access row (preview) — justified
  7. OpenAPI marks the operation available/owner/preview and adds turn_cancelled to the Owner enum — justified

[FIRST-PRINCIPLES-REVIEWED] f1d530b

Description read

[DESCRIPTION-READ] f5ffed35a86f588bdf31b5f21ce090d1a5a2bd5c66634eaae73d7a60998848ea

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed f1d530b32bf058330ce87a563f276b397b681f23 — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] f1d530b

Verdict parsed from the review's SHA-scoped output markers for commit f1d530b32bf058330ce87a563f276b397b681f23.

False positive or not applicable? A repository writer can comment:
/ai-review override fable f1d530b32bf058330ce87a563f276b397b681f23 false-positive: <reason, 20 to 500 characters>

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of f1d530b32bf058330ce87a563f276b397b681f23 and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] f1d530b

False positive or not applicable? A repository writer can comment:
/ai-review override gpt f1d530b32bf058330ce87a563f276b397b681f23 false-positive: <reason, 20 to 500 characters>

@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running labels Dec 1, 2026
@KiroCrewCaptain

KiroCrewCaptain commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Code change approver deep review: A person should decide

This 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

docs/request-for-change/assets/crew-api/crew-public.yaml:1457-1458 describes GET /v1/crew/events and says: "a turn cancelled while it waited reads expired with timeout, turn_cancelled is never sent". This change sends that value.

  • crew_api_responses.py:968-970 copies the record's expired_reason into approval.decided.
  • test/test_crew_api_response_cancel.py:538-542 asserts expired_reason: turn_cancelled on the event stream.
  • The same contract file at lines 502-503, added here, says the event carries turn_cancelled. The file now gives two opposite answers.
  • docs/reference/crew-api/public-events.md:23 repeats the old sentence.

Smallest fix: rewrite the sentence at 1457-1458 to say that a turn cancelled through POST /v1/responses/{response_id}/cancel reads expired with turn_cancelled. Then regenerate docs/reference/crew-api/public-events.md. No code change is needed. The event schema already lists turn_cancelled, so only a client written from the prose is misled.

What the review verified on this head

  • The review read the whole diff (21 files) and traced the cancel route through the approval refusal, the stop and the event stream.
  • The owner check is the first statement of the route. Nothing is read or stopped before it. The route only denies an approval, so it widens no gate.
  • The refusal, its record and the start of the stop run with no await between them. approval.decided is written ahead of turn.completed.
  • PR Readiness is green on this head, and no AI lane reports a blocking finding. This review did not run the tests.

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 crew-public.yaml are proposed amendments that wait for a maintainer's sign-off. The thread holds no sign-off yet.

Optional:

  • crew-public.yaml:496-497 says "the stop was sent" for the in_progress answer. A session that is compacting declines the stop (turn_stop.py:873-893), and the route still answers in_progress after the full wait. Reword it to "the stop was asked for; a session that is compacting declines it".
  • stop_turn_refusing_its_approvals (crew_api_approvals.py:650) takes a stop callable and has one caller. You can call stop_slot_turn inside it and delete the functools.partial in the route.
  • No test fails by name if running["ended"].set_result(None) (crew_api_responses.py:1287-1289) is deleted. The tests that expect cancelled then pass about 10 seconds slower. One test with a very large stop limit and a bounded request pins it.

Tier: 2, held for a person: 21 files, more than 20.

A deep review by the code change approver, not by a person · commit f1d530b32b · 934 changed lines. If it is wrong, reply here: replies are how the approver is evaluated.

@bolichen97 bolichen97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

@bolichen97
bolichen97 enabled auto-merge (squash) December 1, 2026 14:26
@bolichen97
bolichen97 merged commit 033d379 into main Dec 1, 2026
163 of 164 checks passed
@bolichen97
bolichen97 deleted the crew/headless-crew/v1-cancel branch December 1, 2026 14:26
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Dec 1, 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.

Crew API: cancel a running turn for the machine owner (POST /v1/responses/{response_id}/cancel)

3 participants