eca icon indicating copy to clipboard operation
eca copied to clipboard

promptStop should cancel already executing tool call futures

Open bombaywalla opened this issue 1 year ago • 45 comments

To upvote this issue, give it a thumbs up. See this list for the most upvoted issues.

When a chat/promptStop is executed while a tool call is executing in its own future, we currently get an "Invalid state transition" (by design). ECA then waits for the future to complete. It would be good if the future could be cancelled - especially if the tool call is long-lived. However, it may be challenging since the future thread has to cooperate with the cancellation process. And we don't always have control over the implementation.

Steps to reproduce the behavior (in eca-emacs):

  1. Send a prompt like Please run the bash tool with a sleep of 10 secs followed by a 'ls'..
  2. Click on accept to allow the tool.
  3. Click on stop, to stop the prompt, while the tool is still running.
  4. Check the <eca:stderr:1> buffer. You should see an "Invalid state transition" exception.

What we'd like in this issue is just to be able to cancel the future - ideally in a clean way, but okay if not clean. Secondarily, we want to allow the user to interrupt ECA from hanging on a long-lived call, but see #77 on background tasks and long-lived processes. Minimally, get rid of the invalid state transition exception.

bombaywalla avatar Sep 07 '25 18:09 bombaywalla

Are you okay with storing the futures in the tool call state?

bombaywalla avatar Sep 07 '25 22:09 bombaywalla

@bombaywalla I guess so, we should just:

  • make sure we clean it to avoid memory leak after the tool was executed
  • make sure we don't serialize it to a file, but I believe we already do this, check here

ericdallo avatar Sep 07 '25 22:09 ericdallo

I checked the place you pointed me at and yes, we are dissocing all the tool calls before serializing to a file.

Do you want me to remove the whole tool call state after the tool finishes executing or is stopped? Or just the future? Should the cleanup be done as soon at the tool call completes, or after the chat ends?

What are your thoughts about supporting "background" tool calls, as suggested in #77 ? If we do that, we may need to keep some state around to note that the tool is running? I'd prefer to come up with a solution to this issue that will be compatible with a "background" tool call if/when it is implemented.

Right now, I'm in the "hammock" phase :-) so just thinking about potential pros/cons of different approaches.

bombaywalla avatar Sep 07 '25 23:09 bombaywalla

Do you want me to remove the whole tool call state after the tool finishes executing or is stopped? Or just the future?

I believe it's a good idea clean the whole toolCall atom.

Should the cleanup be done as soon at the tool call completes, or after the chat ends?

Cleaning after toolCall reaches its final state sounds good to me, but whatever is easy to evolve/maintain, cleaning on finish-chat sounds good too.

What are your thoughts about supporting "background" tool calls,

That's something I want to add to ECA carefully, background agents/subagents is a feature I wanna support but in a way easy to evolve/maintain, I agree we should solve this issue in a way easy to support #77 later, but I haven't spent too much thought on that. Hammock is definitely a good idea for this one

ericdallo avatar Sep 07 '25 23:09 ericdallo

Is each ECA message (request or notification) run in its own thread asynchronously? Or does the ECA server loop take each message, run it to completion, and then run the next one?

In my investigations, I came across the pending-request function in the lsp4clj.server namespace. It has an interesting docstring. I was wondering whether we need to use something like what is described there?

bombaywalla avatar Sep 10 '25 00:09 bombaywalla

@bombaywalla yeah, lsp4clj should help with that, It's been years since I touched that code so I don't recall exactly, but not sure the thread that runs the request/notification is the issue here, but the future we spawn for tool calling here, WDYT?

ericdallo avatar Sep 10 '25 11:09 ericdallo

Thanks.

As of now, I see three main areas of concern:

  1. How best to save the future in tool call, so that we have a handle on it. For example, we can always do it directly in code, but it would be more consistent if we made it an action in the state machine framework, since currently all the changes to the tool call state are managed through actions.
  2. How best to clean up the future in the calling thread. And where. We have touched on this before.
  3. How best to clean up the underlying tool call inside the future, when there is an interrupt. This is what I am currently investigating.

bombaywalla avatar Sep 10 '25 13:09 bombaywalla

Sounds good @bombaywalla , I agree all three are valid concerns

ericdallo avatar Sep 10 '25 14:09 ericdallo

From a protocol and UX perspective, what do you think about a protocol message, toolCallCancel and a corresponding toolCallCancelled or toolCallStopped? That would allow some flexibility in the UX. For example, if a tool call takes more than (say) 20 secs, the editor client could ask the user "This call is taking too long. Would you like to abort the call?" and if the users says "yes" it could send a toolCallCancel message rather than a promptStop message. The former would just interrupt the one call, whereas the latter would kill the whole prompt, which may have multiple tool calls.

bombaywalla avatar Sep 10 '25 22:09 bombaywalla

Interesting indeed, maybe only a client request toolCall/cancel would be enough as we could use toolCallRejected with a reason mentioning why, WDYT?

ericdallo avatar Sep 11 '25 00:09 ericdallo

That could work. I'd prefer a different message since toolCallRejected is primarily sent when the call hasn't yet started (as one might assume from the name). If there is another message that is returned after the tool has started, it might be better to use that (with a reason). Overall, I'd prefer a separate message to make the semantics clear. But I could also go for any of these.

In any case, just thinking at this point and wanted to see what your thinking was. So that I could architect the solution in a way that could also enable this new mesage.

bombaywalla avatar Sep 11 '25 01:09 bombaywalla

Add another message means change all cleints, and we already have too many message for tool calls, so I think we can add a new one if we think is really needed, let's start with a single one to cancel the toolCall and see how it behaves the whole package. sounds good, go ahead

ericdallo avatar Sep 11 '25 11:09 ericdallo

I'm thinking that since addressing this issue has the potential to incur major changes, it might be best to use multiple smaller PRs to implement this issue. My thought is to make smaller PRs that set the stage for the main PR, and each of the smaller PRs are ideally something that would be useful anyway and would not disrupt (and possibly enhance) the current flow of operations.

In that vein, I was thinking of creating a PR that would do step 1 (save the future so as to keep a handle on it).

So, I started looking at the existing code and was looking at how best to handle it. That led me to the question of whether the current location and use of the future is appropriate. Right now, the future is created just before the approved?* promise is derefed. I'm wondering whether the future should instead be created in f.tools/call-tool! and returned by it, since the future is presumably created in order to run the actual tool call in another thread. The operational difference that I can see between the two approaches is that if the approved?* promise has not been delivered because of user inaction, then the current thread (the one handling the :on-tools-called callback) will block in the new proposed location of the future, whereas currently that thread does not block. My guess was that if the user has not clicked on allow/reject then it doesn't matter whether this thread blocks or not. But I am not familiar enough with the rest of the code to be definitive in the answer.

What do you think?

bombaywalla avatar Sep 11 '25 23:09 bombaywalla

My thought is to make smaller PRs that set the stage for the main PR, and each of the smaller PRs are ideally something that would be useful anyway and would not disrupt (and possibly enhance) the current flow of operations.

That sounds great to me!

Yes, you are right, the thread flows right now is:

  • we receive chat/prompt, we request LLM using hato which is async, has its own thread for that, so the current thread just respond chat/prompt and it's done
  • the hato thread request, parse LLM response and may call a tool, it doesn't matter if this thread blocks or spawn another one, we are already in a async flow

So I'm ok with changing the future place if we keep everything working of course

Does that makes sense?

ericdallo avatar Sep 12 '25 02:09 ericdallo

Ah. I did not realize that the hato thread was already async. In that case, it makes even more sense to have the future in the actual call. I will spend some time thinking about it to see if that might have any adverse consequences.

bombaywalla avatar Sep 12 '25 18:09 bombaywalla

I was thinking more about the clean up of the future.

delete-chat removes the whole chat (as we discussed earlier) and that is probably enough for now. It also makes sense to delete the future when the chat is deleted since the tool call lifecycle is related to the chat lifecycle. Moreover, when we want to support long-lived background tool calls, we may need to have the future around for longer.

My current thought is to leave things as they are. What do you think?

In the future, we can potentially have a background task that goes around cleaning things up (including completed futures). Or we can have an explicit cleanup / compact user-initiated command that could potentially clean up completed futures, among other things.

bombaywalla avatar Sep 14 '25 19:09 bombaywalla

I agree with you, sounds extensible for background indeed, background agents is something we will start to investigate soon c/c @zikajk

ericdallo avatar Sep 15 '25 15:09 ericdallo

@bombaywalla just pasting here a weird behavior which fixing this issue should resolve it:

I stopped the prompt, while the command was running (sleeping), but the call finised (sent a toolCalled) and sent the LLM text after doing it which is quite wrong.

Image

ericdallo avatar Sep 15 '25 16:09 ericdallo

That is correct. Currently, this is intended behavior. When this issue is completed, the call should be interrupted and stopped.

bombaywalla avatar Sep 15 '25 17:09 bombaywalla

Just an update on the progress and some questions.

I've been looking at the code to better understand where and how to modify it. Looking at MCP and how they handle things. Attempting to come up with an architecture that supports future expansion of capabilities (e.g., progress updates, background tasks). I'm currently working on a spike to better understand the implementation issues and tradeoffs.

Here are my observations/thoughts so far, in no particular order:

  • There seem to be three types of tool calls (from the POV of cancellation):
    • ECA tools that spawn a process.
    • ECA tools that run in the same process.
    • MCP tools.
  • Currently considering an enhanced state machine using future-cancel with InterruptedException as the primary method of cancellation and also have a volatile flag for a requested interrupt that cooperating tools can utilize if needed.
  • Currently thinking of implementing via an incremental approach tool by tool. First the shell tool, then other spawned process tools, then MCP tools and then in-process tools. With smallish PRs at each step.
  • According to the MCP spec, there is a fire-and-forget tool cancellation capability that does not wait for the cancellation to complete and gets no acknowledgement that the call has been cancelled. Additionally, the spec allows the server to ignore the cancellation request. The current version of the Java SDK, used by clojure-mcp does not implement the cancellation request and just ignores it.

I'd be interested in any thoughts / comments / feedback on the above.

Now, the questions, in no particular order, that I'd like your feedback on:

  1. Is there any plan to eventually support progress updates from tools as they are running? Similar to MCP. If so, we might want to consider an architecture that involves spawning a tool call and communicating with the spawned tool via a core.async channel. This would also handily support a background tool call.
  2. I'm thinking that we should add a cancel-call multi-method for each tool, similar to the tool-call-details-after-invocation multi-methods. Are you okay with that?
  3. Do we need a toolCallStopping ECA protocol notification to be sent from the server to client?
  4. Related to the previous question is whether we should wait for a cancelled tool call to complete as it currently does? Alternatively, we can adopt a fire-and-forget approach like MCP, just send the cancellation to the tool, and not wait on that tool anymore. Note that we would wait for a non-cancelled tool call to complete. Either approach would be okay by me. Looking for your thoughts on this.
  5. I am writing some specs for tool call state - mostly for documentation. Where would you like to put any specs? Would you prefer them in a separate namespace (my preference)? Or would you prefer them to be in the same namespace as the code?
  6. Not particularly related to this PR, but would you please add a line to .gitignore to avoid including .eca/?

bombaywalla avatar Sep 19 '25 16:09 bombaywalla