Skip to content

fix: propagate AbortSignal to executeAsReasoningAgent for task cancellation#262811

Merged
flash1293 merged 3 commits intoelastic:mainfrom
flash1293:fix/reasoning-agent-abort-signal-262800
Apr 14, 2026
Merged

fix: propagate AbortSignal to executeAsReasoningAgent for task cancellation#262811
flash1293 merged 3 commits intoelastic:mainfrom
flash1293:fix/reasoning-agent-abort-signal-262800

Conversation

@flash1293
Copy link
Copy Markdown
Contributor

Summary

Fixes #262800

Tasks that use executeAsReasoningAgent from @kbn/inference-prompt-utils could not properly respond to Task Manager cancellation because the reasoning agent did not accept an AbortSignal. This PR adds cancellation support throughout the chain.

Changes

1. executeAsReasoningAgent now accepts an optional abortSignal (kbn-inference-prompt-utils)

  • Added abortSignal?: AbortSignal to ReasoningPromptOptions
  • The signal is checked at the start of each reasoning step — if aborted, the agent throws 'Request was aborted'
  • The signal is checked in callTools before invoking tool callbacks, returning an error result if aborted
  • The signal flows through the existing options spread to inferenceClient.prompt(), so in-progress HTTP requests are also cancelled

2. cancellableTask now aborts the signal before resolving the cancellation promise (cancellable_task.ts)

  • Previously, runContext.abortController.abort() was called only in the .finally() block — after the Promise.race was already decided
  • Now the signal is aborted immediately when TaskStatus.BeingCanceled is detected in the polling loop, before resolving the promise
  • The .finally() block still handles the case where run() exits early on its own (scenario 2 from the original comment), calling abort() only if the signal hasn't already been aborted

3. Affected tasks now pass abortSignal: runContext.abortController.signal

  • memory_generation — passes signal to executeAsReasoningAgent and checks signal.aborted at per-stream loop boundary
  • memory_consolidation — passes signal to executeAsReasoningAgent
  • conversation_scraper — passes signal to executeAsReasoningAgent
  • memory_updatediscovery_completed_trigger — signal propagated via new abortSignal field on MemoryUpdateContext, with loop boundary check in the trigger

4. Tests

  • Added 4 new unit tests for executeAsReasoningAgent abort signal behavior

How cancellation now works

Before this fix:

  1. Task Manager sets task to BeingCanceled
  2. cancellableTask polling detects it, resolves cancellation promise
  3. Signal is only aborted in .finally() — too late for the running task to act on it
  4. Tasks using executeAsReasoningAgent continue running all reasoning steps

After this fix:

  1. Task Manager sets task to BeingCanceled
  2. cancellableTask polling detects it, aborts the signal immediately, then resolves cancellation promise
  3. The running task's executeAsReasoningAgent detects signal.aborted at the next reasoning step and throws
  4. In-progress inferenceClient.prompt() calls also receive the signal and can abort HTTP requests
  5. The catch blocks in each task detect 'Request was aborted' and return getDeleteTaskRunResult()

How to test

  1. Start a long-running task that uses executeAsReasoningAgent (e.g., memory consolidation, conversation scraper)
  2. Cancel the task via Task Manager
  3. Verify the task stops promptly instead of running to completion
  4. Unit tests: node scripts/jest x-pack/platform/packages/shared/kbn-inference-prompt-utils/src/flows/reasoning/execute_as_reasoning_agent.test.ts

…lation

- Add optional `abortSignal` to `ReasoningPromptOptions` so callers can
  pass a cancellation signal to the reasoning agent loop
- Check `abortSignal.aborted` at the start of each reasoning step and
  in `callTools`, throwing 'Request was aborted' when cancelled
- Forward `abortSignal` to `inferenceClient.prompt()` calls via the
  existing options spread (signal flows through to HTTP requests)
- Fix `cancellableTask` to abort the signal BEFORE resolving the
  cancellation promise, so running tasks can detect it immediately
  rather than only after the Promise.race completes
- Pass `runContext.abortController.signal` as `abortSignal` in
  memory_generation, memory_consolidation, conversation_scraper, and
  via MemoryUpdateContext for memory_update/discovery_completed_trigger
- Add signal.aborted check at loop boundaries outside the reasoning
  agent (e.g., per-stream loop in memory_generation)
- Add unit tests for abort signal behavior in executeAsReasoningAgent

Fixes elastic#262800
@flash1293 flash1293 force-pushed the fix/reasoning-agent-abort-signal-262800 branch from 067c599 to d6dd3b8 Compare April 13, 2026 15:46
@flash1293 flash1293 added release_note:skip Skip the PR/issue when compiling release notes backport:skip This PR does not require backporting Team:obs-onboarding Observability Onboarding Team Feature:Streams This is the label for the Streams Project v9.5.0 labels Apr 13, 2026
@flash1293 flash1293 marked this pull request as ready for review April 14, 2026 07:49
@flash1293 flash1293 requested review from a team as code owners April 14, 2026 07:49
@elasticmachine
Copy link
Copy Markdown
Contributor

Pinging @elastic/obs-onboarding-team (Team:obs-onboarding)

@elasticmachine
Copy link
Copy Markdown
Contributor

💛 Build succeeded, but was flaky

Failed CI Steps

Metrics [docs]

✅ unchanged

History

Copy link
Copy Markdown
Contributor

@tonyghiani tonyghiani left a comment

Choose a reason for hiding this comment

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

LGTM 👌

@flash1293 flash1293 merged commit ecedf7a into elastic:main Apr 14, 2026
23 checks passed
mbondyra added a commit to mbondyra/kibana that referenced this pull request Apr 14, 2026
* commit '11ed3645c5ededae2a6e29f2a79b31f52208b441': (157 commits)
  remove sync register uiAction methods (elastic#254590)
  [performance] Apply minimal auth to the search route (elastic#257497)
  [ES|QL] Reports correctly the controls server side errors (elastic#263020)
  [SecuritySolution][Navigation] Enable classic nav updates (elastic#262358)
  [Inference] Use pretty name and logo on feature settings page (elastic#262531)
  [Security Solution] fix AT-AB cypress test (elastic#262991)
  [SigEvents] Seed sigevents env script (elastic#261172)
  Adjust conditions for validating no refetch for expanded row (elastic#262978)
  [Agent Builder] update copy for the announcement modal (elastic#263034)
  [Search] Hide index management links for users without privileges (elastic#262627)
  Simplify OAS schema for GET `/api/spaces/space` query params (elastic#260831)
  Fix fleet output OAS regressions: SSL type explosion and Kafka union wrappers (elastic#260842)
  [Dashboards in chat] fix agent confusing the axes in a horizontal chat (elastic#263064)
  [One Workflow] Add alert state checkbox UI for workflow connector (elastic#259770)
  [One Workflow] Deprecate legacy Cases step types in workflow authoring (elastic#262070)
  skip failing test suite (elastic#248090)
  fix flaky test: MonitorDetails filter apply button not enabled (elastic#260788)
  fix: propagate AbortSignal to executeAsReasoningAgent for task cancellation (elastic#262811)
  [Security Solution][Alert KPI] Fix white space bug in alert KPIs (elastic#260803)
  [Streams] Move helpers and format_size_unit to utils folder (elastic#262550)
  ...

# Conflicts:
#	x-pack/platform/plugins/shared/dashboard_agent/public/attachment_types/canvas_integration/dashboard_canvas_content.test.tsx
#	x-pack/platform/plugins/shared/dashboard_agent/public/attachment_types/canvas_integration/dashboard_canvas_content.tsx
#	x-pack/platform/plugins/shared/dashboard_agent/public/attachment_types/canvas_integration/use_register_canvas_action_buttons.ts
#	x-pack/platform/plugins/shared/dashboard_agent/public/attachment_types/index.test.tsx
#	x-pack/platform/plugins/shared/dashboard_agent/public/attachment_types/index.tsx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport:skip This PR does not require backporting Feature:Streams This is the label for the Streams Project release_note:skip Skip the PR/issue when compiling release notes Team:obs-onboarding Observability Onboarding Team v9.5.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Streams] Task cancellation doesn't work for tasks using executeAsReasoningAgent

5 participants