Preserve cancellation across AI, MCP and telemetry integrations
Cancellation could be mistaken for an ordinary failure when AI callbacks, MCP transports or telemetry hooks threw during cleanup. That could trigger retries, send error responses or continue recording after the operation had been canceled.
Preserve cancellation at the existing integration boundaries:
- Keep AI failure observers and conversation persistence from replacing cancellation; preserve child-task ownership and completed-turn fallback behavior.
- Stop MCP handshake retries and server error responses when cancellation is wrapped by cleanup, while still closing owned transports.
- Propagate cancellation from OpenTelemetry, Sentry and Telescope callbacks without hiding genuine ordinary failures or skipping required cleanup.
Regression coverage checks direct and wrapped cancellation, callback failures, transport cleanup and ordinary-error behavior. Targeted source analysis and affected tests pass. The full suite and service matrices run in CI.
[!NOTE]
Preserve wrapped coroutine cancellation across AI, MCP, Sentry, Telescope, and OpenTelemetry
Many catch blocks only checked for a direct Swoole CanceledException, so a cancellation wrapped in another exception was swallowed or converted into an error response, purged value, or empty result. This PR replaces those checks with Coroutine::causedByCancellation() and Coroutine::preferCancellation() so cancellation propagates through the whole exception chain.
- Replaces direct
CanceledException catches with chain-aware checks in OpenTelemetry instrumentations (CacheInstrumentation, RedisInstrumentation, HttpClientInstrumentation.applyUrlTemplate, ExceptionInstrumentation.record, UserContextResolver.resolve), Sentry (EventHandler, Tracing/EventHandler, Hub.addBreadcrumb and samplers, AiIntegration, CacheIntegration), Telescope (Telescope.startRecording/record, JsonNormalizer, watchers, DatabaseEntriesRepository), and MCP (Protocol, ClientManager, transports, Server, InteractsWithResponses, CompletionComplete).
- Reworks AI failure-reporting paths so the original exception is rethrown and failure callbacks run in a
finally block. If a callback throws, its exception keeps the original failure (including cancellation) as its previous exception; see TextGenerationLoop.php, RememberConversation.php, StreamableAgentResponse.php, and Provider.php.
TextGenerationLoop.concurrentToolResults now wraps cancellation-origin tool failures in ChildCancellationException with the original chained, and PendingConversationTitle records canceled title generation the same way. RememberConversation.rememberTurn catches ChildCancellationException during title generation so a completed turn is still persisted.
- Adds or parameterizes tests across all affected suites to cover both direct and wrapped cancellation.
- Behavioral Change: exception paths that previously swallowed wrapped cancellation now rethrow it. Callers of the changed resolvers, handlers, and event dispatchers (e.g.
Server.sendException, Hub.startTransaction, JsonNormalizer.normalize) may now observe exceptions that were previously returned as defaults or error responses. An oversized UntrustedUrl.fetch transfer that is canceled now rethrows instead of raising the size-limit InvalidArgumentException.
Macroscope summarized 9f7e35e.
Summary by CodeRabbit
- Bug Fixes
- Coroutine cancellations are now consistently propagated across AI, MCP, OpenTelemetry, Sentry, and Telescope operations, including when cancellation is wrapped in another error.
- When failure reporting or cleanup also fails, the original operation error is preserved in the exception chain.
- Failed conversation turns are retained more reliably when cancellation occurs during generation or persistence.