claude-code icon indicating copy to clipboard operation
claude-code copied to clipboard

Feature request: non-blocking tasks / notification to main agent when a background bash session finishes

Open german-swan opened this issue 1 year ago • 4 comments

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

german-swan avatar Aug 30 '25 03:08 german-swan

Hey, this is a fantastic and well-thought-out feature request. I've run into this exact limitation myself, especially for longer-running tasks like builds or extensive test suites. Your analysis is spot-on: the Task tool is blocking, and background bash sessions are "fire and forget" without a built-in notification mechanism.

Your proposed implementation using something like process.on('exit') to trigger a follow-up action is exactly how I'd envision this working natively. It would make orchestrating parallel subagents so much more powerful.

While we wait for an official implementation, I was digging through the docs and had an idea for a potential (though admittedly complex) workaround using the Hooks system. It's not as clean as a dedicated event, but it might be possible to build something that approximates the behavior you want.

Potential Workaround using Stop Hooks

The core idea is to use a Stop hook as a "session watchdog" that checks on background processes before letting the main agent's turn truly end.

Here’s the high-level concept:

  1. Create a Custom Slash Command: Instead of running the background command directly, you'd use a custom slash command, let's say /run-bg <your-command>. You can create this in .claude/commands/run-bg.md.

  2. Track Background Processes: The /run-bg command would execute the bash command in the background (<your-command> &) and, crucially, it would also write the process ID (PID) to a temporary file (e.g., ~/.claude/running_pids.txt).

  3. Implement a Stop Hook: You'd configure a Stop hook in your settings.json. According to the hooks documentation, this hook runs whenever the main agent has finished responding.

  4. The Hook's Logic: The script for your Stop hook would:

    • Read the PIDs from ~/.claude/running_pids.txt.
    • Check if any of those processes are still running (e.g., using ps -p <pid>).
    • If a process has finished, the script would output a specific JSON message to Claude. The docs mention you can use {"decision": "block", "reason": "Background task bash_XYZ has finished. Please check its output and summarize the results."}. This would prevent the agent from stopping and instead prompt it to process the results.
    • If processes are still running, it could either do nothing (letting the session stop until your next prompt) or use the same "decision": "block" mechanism to ask Claude to wait a moment before checking again.

Example settings.json hook configuration:

{
  "hooks": {
    "Stop": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "python3 /path/to/your/check_bg_processes.py"
          }
        ]
      }
    ]
  }
}

Limitations of this approach:

  • It's definitely a hack and much more complex than a native BackgroundTaskEnd hook would be.
  • You have to manage the PID file carefully to avoid stale entries.
  • It might still lead to some token burn if you design the hook to keep the agent in an active waiting loop.

Still, it might be a path to achieving a more autonomous workflow for background tasks with the current toolset.

Great write-up, and I hope the team considers adding a dedicated hook for this. It would unlock a whole new level of agent orchestration. Upvoted!

Cheers.

coygeek avatar Aug 30 '25 03:08 coygeek

This issue has been inactive for 30 days. If the issue is still occurring, please comment to let us know. Otherwise, this issue will be automatically closed in 30 days for housekeeping purposes.

github-actions[bot] avatar Dec 05 '25 10:12 github-actions[bot]

This issue has been automatically closed due to 60 days of inactivity. If you're still experiencing this issue, please open a new issue with updated information.

github-actions[bot] avatar Jan 05 '26 10:01 github-actions[bot]

This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.

github-actions[bot] avatar Jan 12 '26 14:01 github-actions[bot]