Cancel a suspended background job only once when a streamed continuation request fails
When a streamed request that continues a suspended response fails during the continuation (for example, the continuation request_stream raises ModelAPIError), the suspended job was cancelled twice. The streamed continuation composite cancelled it in its error branch. It then still reported the response as a resumable 'suspended' detach (the graph's teardown calls aclose()), so _commit_interrupted_response in the graph cancelled the same job again. For OpenAI background mode, each cancel is a POST /responses/{id}/cancel. The duplicate is an extra request, plus an extra activity, step or task under Temporal, DBOS or Prefect. Non-streamed runs were not affected.
This PR adds a _job_cancelled flag on the composite. Once the composite has cancelled the job, get() stops reporting it as a resumable detach, so the graph records 'interrupted' (as before) without cancelling again.
Tightening the existing streamed test_cost_limit_mid_continuation_cancels_job to require exactly one cancel exposed a second issue. When check_usage tripped, the composite cancelled the previous segment's response, because response = merged ran after the check. The graph's duplicate cancel had been the one hitting the newest job. The composite now advances to the merged response before checking usage. A limit trip therefore cancels the newest suspended job, and cancels nothing once the merge is 'complete'. This matches the non-streamed loop (test_nonstream_usage_limit_on_completed_merge_does_not_cancel).
Out of scope: under FallbackModel, a failed pinned continuation still leads to two cancels, both streamed and non-streamed. FallbackModel cancels the abandoned job before rewinding to the chain. If the chain then also fails, the outer continuation loop cancels the same suspended response again. That has a separate cause and needs its own fix.
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
- PR title is fit for the release changelog.
š¤ Generated with Claude Code