eca icon indicating copy to clipboard operation
eca copied to clipboard

Long-running processes blocks ECA

Open zikajk opened this issue 1 year ago • 28 comments

DESCRIPTION (This bug has been fixed and discussion here switched mainly to discussion about background processes)

  1. The model decides to start the long-running process using the bash tool.
  2. This creates a long-running tool that blocks the ECA
  3. ~~When the user clicks on stop, it crashes the ECA~~ (fixed)

PROPOSED SOLUTION how to handle long-running (background) processes: We should support background processes that will be used for cases like this. We should be able to manage them. Something like this - the first half describes the problem and possible solution perfectly: https://github.com/ruvnet/claude-flow/wiki/background-commands

zikajk avatar Aug 24 '25 10:08 zikajk

I didn't get why the problem happens exactly, but we should fix indeed

ericdallo avatar Aug 24 '25 14:08 ericdallo

I forgot which provider it was, I believe it was deepseek with anthropic API. Will check again and attach more log.

zikajk avatar Aug 24 '25 14:08 zikajk

Would be nice to confirm if that happens with other providers

ericdallo avatar Aug 24 '25 14:08 ericdallo

  1. It happens with DeepSeek via Anthropic
[ANTHROPIC] [7622] message_delta {:type "message_delta", :delta {:stop_reason "tool_use", :stop_sequence nil}, :usage {:output_tokens 71}}
[TOOLS] Calling tool 'eca_shell_command' with args '{"command" "cd /Users/user/Code/VIBE/crossroad && npm run dev", "working_directory" "/Users/user/Code/VIBE/crossroad"}'
[TOOLS-SHELL] Running command: cd /Users/user/Code/VIBE/crossroad && npm run dev

  1. It happens with OpenRouter + Gemini-2.5-pro
[TOOLS] Calling tool 'eca_shell_command' with args '{:command "cd /Users/user/Code/VIBE/crossroad && npm run dev"}'
[TOOLS-SHELL] Running command: cd /Users/user/Code/VIBE/crossroad && npm run dev

When I click on stop, both eca sessions are stopped and this appears in log Process eca stderr<2> finished

zikajk avatar Aug 24 '25 14:08 zikajk

I believe that there is no good way how to fix this without improving the UX and doing something simillar to CC.

zikajk avatar Aug 24 '25 14:08 zikajk

@zikajk I can't repro that with openai responses with a prompt like: run a bash call of sleep for 30seconds, I'm wondering why

ericdallo avatar Aug 24 '25 15:08 ericdallo

@ericdallo Try smth like "create simple web app that needs node server running and then run the node server"

Btw. it doesn't always crash. But the main issue is that you must click on stop and you can't control the background process.

zikajk avatar Aug 24 '25 15:08 zikajk

@zikajk this seems a little bit related to #65, FYI @bombaywalla we may want to cancel the tool call future somehow although not sure java will manage that, but this should have a separated issue I guess for that.

ericdallo avatar Aug 24 '25 17:08 ericdallo

Yeah, I agree that the tool call future needs to be interruptible. Also agree to make a separate issue.

bombaywalla avatar Aug 24 '25 18:08 bombaywalla

Yeah, we will need to have a special tool or a new parameter in the bash tool to handle background tasks and come up with a good ux.

zikajk avatar Aug 24 '25 18:08 zikajk

Folks, we had some improvements lately:

  • ECA now has a state machine for toolCalls events (thanks @bombaywalla ), so stop now properly send the reject events for toolCalls, we still don't cancel the future tho, but not sure it's a big deal/even possible.
  • ECA now send a toolCallRunning to state when we are really running and wait for the toolCall to finish (toolCalled).

Given that, what issues regarding this issue are still true/we should fix? @zikajk

ericdallo avatar Sep 05 '25 18:09 ericdallo

My opinion is that for a good UX we must, eventually, support the ability to interrupt long-lived tool calls / processes.

I am aware that Clojure/Java requires the process to cooperate and check periodically whether an interrupt has been requested, if we want the interrupt to be clean. I think the user should have the option to interrupt the process even, if the exit is not clean.

I think the issue of long-lived calls is related to, but slightly different than this issue of background calls. Correct me if I am wrong, but it seems to me that the reason ECA hangs in the case of a long lived call is that there is code in chat.clj which is like (run! deref calls) towards the end of the :on-tools-called callback. That run! call waits for all the calls to complete, thus blocking that ECA thread. There is a similar issue near the start of the future in :on-tools-called where it waits on a deref of the approval promise. In both these cases, I think that the derefs should have a configurable timeout on them. In my opinion, this is essential for robustness and for a convenient UX - eventually. I am happy to work on it if deemed useful.

bombaywalla avatar Sep 05 '25 19:09 bombaywalla

That sounds correct @bombaywalla, we didn't have these issues before we introduce parallel toolCall (adding the derefs to calls), I believe we should improve that like you mentioned indeed, when user stop a prompt we should somehow try at least to cancel the running futures. Not sure I'm being naive here, but a a simple future reference in an atom may help fix that, were we can cancel the future, not sure a timeout would help for this case.

ericdallo avatar Sep 05 '25 19:09 ericdallo

If there is a way for the user to interrupt, then yes, a timeout is strictly not necessary.

If no one else is already working on this, would you like for me to take a look?

bombaywalla avatar Sep 05 '25 19:09 bombaywalla

@bombaywalla there is a easy way, user can just stop the prompt, like other editors/tools have, a cancel button, which will send the chat/stopPrompt request.

Yes, go ahead, no one working on that especifically.

ericdallo avatar Sep 05 '25 19:09 ericdallo

@ericdallo Let's say we need to run bb run dev and keep it running in the background. This is how Claude handles it, and I think we should follow a similar approach.

  1. The eca_shell_command would need to have run_in_background parameter. Image

  2. We need to access the output from a running or completed task. The second highlighted block in the image above shows this. This will likely be the most challenging part. It also shows that CC can read the output and filter it.

  3. There must be a way to cancel the running eca_shell_command. Claude Code does this with a separate tool: Image

Here you can see how it is being used in CC:

  1. Select the "background-task" choice
  2. Select a specific task.
  3. See Status, Stdout and Stderr
  4. Go back or kill the task.
  5. Return to the prompt

https://github.com/user-attachments/assets/9495db7e-6672-4891-a4b9-0793ee86f112

zikajk avatar Oct 03 '25 16:10 zikajk

Hummm, interesting, now I get this feature. yeah, the way I see this is:

  • we would need a eca_kill_shell_command indeed
  • the work @bombaywalla is doing will help a lot here, since we will have control over tool call futures/resources, so we could stop or leave it running, but we will need some tweak for this.

I wonder how common it is to have infinite commands running while coding with AI tho.

ericdallo avatar Oct 03 '25 18:10 ericdallo

Personally, I'd mostly use it for js / cljs compilation, where I don't use the Clojure-MCP / REPL. I believe the Clojure-MCP makes it a bit less useful for us than for people using other languages. Here are some reactions to this:

https://apidog.com/blog/claude-code-background-tasks/

https://www.threads.com/@boris_cherny/post/DNGzVZCziB1/video-claude-code-can-now-handle-long-running-tasks-in-the-backgroundstart-your-dev-se

https://x.com/jarredsumner/status/1954422523963068839

In my opinion, at this moment, it's a nice thing that we can live without, but the more the LLM does, the more useful it is, and it saves lot of copy / paste.

E.g. when vibe-coding some game - it will run the server, write code, use browser and debug any problem as it appears.

zikajk avatar Oct 04 '25 04:10 zikajk

Thanks for the links. From reading those links, the background tasks as implemented by Claude Code, involves quite a bit more than just running shell commands in the background. It seems that they are running the tasks in a container and maintaining state across session invocation with a persistence layer. There also appears to be an orchestration layer that I don't quite understand yet.

bombaywalla avatar Oct 04 '25 14:10 bombaywalla

yeah, I believe all of that is useful for subagents/background agents, but I'm not sure we need all of that for this issue especifically

ericdallo avatar Oct 05 '25 21:10 ericdallo