Skip to content

fix(agent-service): abort the in-flight run when an agent is deleted - #9864

Open
sshiv012 wants to merge 2 commits into
apache:mainfrom
sshiv012:sshiv012/fix-agent-delete-abort
Open

sshiv012 wants to merge 2 commits into
apache:mainfrom
sshiv012:sshiv012/fix-agent-delete-abort

Conversation

@sshiv012

@sshiv012 sshiv012 commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

What changes were proposed in this PR?

Deleting an agent mid-run now ends its run. TexeraAgent.destroy() calls stop() first, so the run's AbortController aborts. It also drops the step callback, so steps recorded while the run winds down reach no subscriber.

Before:  DELETE /agents/:id -> destroy() -> run keeps calling the model and executing operators
After:   DELETE /agents/:id -> destroy() -> stop() aborts the run -> loop ends at the next await
In flight at delete Effect
Model call Aborted; no further model call is made
Operator execution request Aborted through the same signal the Stop path already uses
Debounced workflow auto-persist Already cancelled by destroy() (unchanged)

Stale sockets get nothing: broadcastToAgentClients returns early once the agent is gone from the store, and destroy() clears the client set.

Out of scope, possible follow-up: telling sockets still open on a deleted agent that it was deleted (for example a final event and close). Today they receive no further events.

Any related issues, documentation, discussions?

Closes #9863. Part of #9576 ("closing or deleting an agent ends its work"). Discussion: #9568.

How was this PR tested?

New tests in agent-service/src/agent/texera-agent.spec.ts, each red before the fix:

  • destroy() mid-run: the model call's abort signal fires, only one model call is made, the run ends as stopped, and no step after the destroy reaches the step callback.
  • destroy() while an operator executes: the execution request, which hangs until its signal aborts, is cancelled and the run ends as stopped.
  • destroy() on an idle agent leaves it available.
cd agent-service
bun test               # 421 pass, 0 fail
bun run typecheck
bun run format:check

Manually on the local dev stack: started a multi-step prompt over the agent WebSocket, then deleted the agent mid-run. With the fix the agent-service log shows agent run complete with steps=0 about 2 s after the prompt. Before it, the run went on for 10 to 20 s after the delete.

Was this PR authored or co-authored using generative AI tooling?

Generated-by: Claude Code (Claude Opus 5.5)

🤖 Generated with Claude Code

https://claude.ai/code/session_01SRmgtAUqHYqXFnu7gX8qXE

@bobbai00
bobbai00 self-requested a review December 3, 2026 20:15

@bobbai00 bobbai00 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.

LGTM overall, left a few minor comments.

Comment thread agent-service/src/agent/texera-agent.ts Resolved
Comment thread agent-service/src/agent/texera-agent.ts Resolved
Comment thread agent-service/src/agent/texera-agent.ts Resolved
sshiv012 added a commit to sshiv012/texera that referenced this pull request Dec 4, 2026
Addresses review on apache#9864:
- Pass the run's abort signal to the first turn's workflow refresh and
  re-check it after the refresh, so a stop or delete during the refresh
  ends the turn before any model call.
- Forward the abort signal to compileWorkflowAsync.
- A destroyed flag guards post-destroy writes: addStep, recordResult and
  the refresh write nothing, and tool calls from the step that raced the
  destroy are skipped.
- destroy() also clears the cached operator results.
- Add a WebSocket test that deletes an agent mid-run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sshiv012

sshiv012 commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

@bobbai00 thanks for the review. All three comments are addressed in 6688554 (replies are in each thread). Could you take another look?

bobbai00 pushed a commit to sshiv012/texera that referenced this pull request Dec 4, 2026
Addresses review on apache#9864:
- Pass the run's abort signal to the first turn's workflow refresh and
  re-check it after the refresh, so a stop or delete during the refresh
  ends the turn before any model call.
- Forward the abort signal to compileWorkflowAsync.
- A destroyed flag guards post-destroy writes: addStep, recordResult and
  the refresh write nothing, and tool calls from the step that raced the
  destroy are skipped.
- destroy() also clears the cached operator results.
- Add a WebSocket test that deletes an agent mid-run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bobbai00
bobbai00 force-pushed the sshiv012/fix-agent-delete-abort branch from 9dd7614 to 8df2410 Compare December 4, 2026 18:35
@bobbai00

bobbai00 commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

@mengw15 @xuang7 can you decide whether to backport this change ?

@mengw15 mengw15 added the release/v1.3 back porting to release/v1.3 label Dec 4, 2026

@mengw15 mengw15 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.

Under the current definition in CONTRIBUTING.md, this may not qualify for v1.3, since deleting an agent mid-run has never stopped its run. #9862 may move us to defining a fix by intended behavior. As the v1.3 release manager, I've decided to backport it.

@sshiv012 once this merges into main, CI will open a backport PR to release/v1.3. The patch conflicts on that branch, so that PR will open as a draft and need some changes.

@xuang7

xuang7 commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

I'll add the release/v1.2 label and let the GitHub Action open the backport PR. This is not urgent for v1.2, as it does not affect execution results. After an agent is deleted, the run continues until it finishes or reaches its step limit. Users can also stop the run before deleting the agent.

Since the patch does not apply cleanly to the v1.2 branch, the backport PR will require some manual work. I think a minimal fix would be sufficient: the change in destroy(), the abort signal on the two API helpers, and a test to verify that no further model calls are made after deletion. The destroyed guards can be limited to the fields available in v1.2.

@sshiv012 , it would be great if you could help resolve the backport conflicts when you have time. Thanks!

@xuang7 xuang7 added the release/v1.2 back porting to release/v1.2 label Dec 4, 2026
@xuang7
xuang7 enabled auto-merge December 4, 2026 20:14
@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Automated Reviewer Suggestions

Based on the git blame history of the changed files, we recommend the following reviewers:

  • Contributors with relevant context: @bobbai00
    You can notify them by mentioning @bobbai00 in a comment.

sshiv012 and others added 2 commits December 4, 2026 14:08
TexeraAgent.destroy() now calls stop(), so deleting an agent mid-run
aborts the run's AbortController: the ReAct loop makes no further model
calls and an in-flight operator execution request is cancelled. It also
drops the step callback, so steps recorded while the run winds down
reach no one.

Closes apache#9863

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SRmgtAUqHYqXFnu7gX8qXE
Addresses review on apache#9864:
- Pass the run's abort signal to the first turn's workflow refresh and
  re-check it after the refresh, so a stop or delete during the refresh
  ends the turn before any model call.
- Forward the abort signal to compileWorkflowAsync.
- A destroyed flag guards post-destroy writes: addStep, recordResult and
  the refresh write nothing, and tool calls from the step that raced the
  destroy are skipped.
- destroy() also clears the cached operator results.
- Add a WebSocket test that deletes an agent mid-run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bobbai00
bobbai00 force-pushed the sshiv012/fix-agent-delete-abort branch from 8df2410 to 769b53c Compare December 4, 2026 22:08

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-service release/v1.2 back porting to release/v1.2 release/v1.3 back porting to release/v1.3

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deleting an agent mid-run does not stop its run

4 participants