Feature request: non-blocking tasks / notification to main agent when a background bash session finishes
The problem
When main agent is instructed to execute commands in background, it does not get notified of the finished terminal sessions. It only checks the outputs after the next prompt. Also, sometimes (or when instructed to do so) it will check the outputs in a loop until a (possibly long-running) command finishes, thus burning tokens unnecessarily.
Origin problem
There are already Tasks and bash sessions with background mode. Task allows only sequential execution, so main agent blocks until a task executes. Bash sessions do run in background, but results are not passed back without actively waiting for outputs. What I wanted to achieve is running another agent in background, while still chatting with main agent and scheduling more tasks to run in parallel. What I expected is basically non-blocking Tasks.
Possible implementation
This might be a low hanging fruit (hard to tell without sources): add a listener to the bash session child process, (e.g. something like process.on('exit') in nodejs), which would add a Todo item (and trigger generation if not generating already) to read the outputs of the finished background process. Or just append to current chat history a small message stating bash session 'bash_id' exited with code $code and let agent decide when or whether to read it. As the task can be a Subagent, this would allow us to execute a subagent in background (non-blocking and with automatic injection of needed context) and inject into main context only the summary of the results.
Other solutions
I've researched other solutions, which allow automatically running and orchestrating multiple agents, but there is lack of control and visibility and those require complex additional setups. The simple but powerful native solution would work much better imo and eliminate all those third-party hacks and/or the need to manage a complex workflow with shared context files manually.
Of course there is always capability to start another terminal and copy-paste (or otherwise provide) some context into it, but that requires more manual work and disrupts the flow.
Use case
I setup a live debugging session for UI app, where my backend is run by claude code in background bash session, I test it and claude investigates issues by adding logs to frontend and reading them from the backend logs in background (so great feature already!). In this debug session I add small messages on minor UI tweaks faster than single agent can resolve them. When the fixes are small enough a single agent works great and I see the fixes on UI in real-time, but when a problem arises the flow just breaks until all Tasks (possibly long-running) are complete.
For these cases it would be great to run a Subagent in background, which would also do additional quality-checks and only report back with concise results. When the Subagent finishes, the main agent would continue all pending tasks (blocked by execution of subagents) until they are done, which would require less monitoring and presence from user. Now, in this case, it would hang until user returns and tells to read the outputs from bashes.
Chat example
> run all checks in parallel in background @.claude\commands\test.md
⎿ Read .claude\commands\test.md (37 lines)
● I'll run all quality checks and tests in parallel in the background as specified in the test command file.
● Bash(uv run python run-checks.py)
⎿ Running in the background (down arrow to manage)
● Bash(cd client && npm run check)
⎿ Running in the background (down arrow to manage)
● Bash(uv run pytest server/tests/)
⎿ Running in the background (down arrow to manage)
● Bash(cd client && npm run test)
⎿ Running in the background (down arrow to manage)
● All checks and tests are now running in parallel in the background:
- Backend quality checks (bash_2)
- Frontend quality checks (bash_3)
- Backend tests (bash_4)
- End-to-end tests (bash_5)
I'll monitor the output and report results as they complete.
> you did not monitor them, they are all finished long ago
● You're right, let me check the output from all the completed background processes.
...
Env
Claude Code v1.0.98
In vscode on windows 11
This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗