Claude tasks: stop listing finished subagent/foreground calls as running

The host counted any tool_result containing "running in background with ID"
as a launch acknowledgement, so a subagent's foreground `sed` of
fake-claude.py (or translate.go) became a background task that never ends.
A subagent's background command whose completion notification preceded its
launch result was also revived. Both stayed "Running" in the chat for days.

- claudehost: only a Bash/Agent/Task call's own first-line acknowledgement
  starts a task; finished ids are never revived by late acks/progress;
  background_tasks_changed (the CLI's running set) ends unlisted tasks;
  tool output no longer counts as a task notification.
- claude: same strict acknowledgement; background_tasks_changed settles
  tracked tasks; on reattach, host tasks without a native id are checked
  against the main and subagent transcripts, so phantoms from hosts still
  running the old build disappear.
- Fake CLI "subagent" turn and regression tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 00:27:50 +00:00
parent accb9970b6
commit 64e51ee751
7 changed files with 449 additions and 20 deletions
+58
View File
@@ -76,6 +76,13 @@ def user_message(text):
if "gate" in text:
gated(message_id)
return
if "subagent" in text:
subagent(message_id)
return
if "agent-done" in text:
send({"type": "system", "subtype": "task_notification", "task_id": "afake",
"tool_use_id": "toolu_agent_fake", "status": "completed", "summary": "Agent finished"})
send({"type": "system", "subtype": "background_tasks_changed", "tasks": []})
if "background" in text:
background(message_id)
return
@@ -191,6 +198,57 @@ def background(message_id):
finish(message_id)
def subagent(message_id):
"""Launches a background subagent whose own tool calls stream as a sidechain
(parent_tool_use_id), as Claude Code 2.1 sends them: a foreground Bash whose
output quotes a background acknowledgement mid-text, and a background Bash
the CLI reports complete before the tool_result acknowledging its launch.
Neither is the operator's background task; only the agent is, until a later
"agent-done" turn ends it."""
agent = "toolu_agent_fake"
send({"type": "assistant", "message": {"id": message_id, "role": "assistant", "model": "claude-test", "content": [
{"type": "tool_use", "id": agent, "name": "Agent",
"input": {"description": "Investigate", "prompt": "look", "run_in_background": True}}]}})
send({"type": "system", "subtype": "task_started", "task_id": "afake", "tool_use_id": agent,
"description": "Investigate", "task_type": "local_agent", "is_backgrounded": True})
send({"type": "user", "message": {"role": "user", "content": [
{"type": "tool_result", "tool_use_id": agent, "content": "Async agent launched successfully.\nagentId: afake"}]}})
send({"type": "system", "subtype": "background_tasks_changed",
"tasks": [{"task_id": "afake", "task_type": "local_agent"}]})
sidechain = []
sed = "toolu_sub_sed"
sidechain.append({"type": "assistant", "message": {"id": "msg_sub_1", "role": "assistant", "model": "claude-test", "content": [
{"type": "tool_use", "id": sed, "name": "Bash", "input": {"command": "sed -n 1,230p internal/claude/testdata/fake-claude.py"}}]}})
sidechain.append({"type": "user", "message": {"role": "user", "content": [
{"type": "tool_result", "tool_use_id": sed, "content":
"#!/usr/bin/env python3\n block = {\"content\": \"Command running in background with ID: b1. Output is being written to: /tmp/b1.output\"}\n"}]}})
xvfb = "toolu_sub_xvfb"
sidechain.append({"type": "assistant", "message": {"id": "msg_sub_2", "role": "assistant", "model": "claude-test", "content": [
{"type": "tool_use", "id": xvfb, "name": "Bash", "input": {"command": "Xvfb :99 &", "run_in_background": True}}]}})
for entry in sidechain:
send(dict(entry, parent_tool_use_id=agent))
send({"type": "system", "subtype": "task_started", "task_id": "bz", "tool_use_id": xvfb,
"owned_by_subagent": True, "is_backgrounded": True, "task_type": "local_bash"})
send({"type": "system", "subtype": "background_tasks_changed",
"tasks": [{"task_id": "afake", "task_type": "local_agent"}, {"task_id": "bz", "task_type": "local_bash"}]})
send({"type": "system", "subtype": "task_notification", "task_id": "bz", "tool_use_id": xvfb,
"owned_by_subagent": True, "status": "completed", "summary": "Background command completed"})
send({"type": "system", "subtype": "background_tasks_changed",
"tasks": [{"task_id": "afake", "task_type": "local_agent"}]})
ack = {"type": "user", "message": {"role": "user", "content": [
{"type": "tool_result", "tool_use_id": xvfb, "content": "Command running in background with ID: bz. Output is being written to: /tmp/bz.output"}]}}
sidechain.append(ack)
send(dict(ack, parent_tool_use_id=agent))
send({"type": "system", "subtype": "task_progress", "task_id": "bz", "tool_use_id": xvfb})
# The CLI keeps each subagent's transcript beside the session's.
directory = os.path.join(transcript_path()[:-len(".jsonl")], "subagents")
os.makedirs(directory, exist_ok=True)
with open(os.path.join(directory, "agent-afake.jsonl"), "a", encoding="utf-8") as file:
for entry in sidechain:
file.write(json.dumps(dict(entry, isSidechain=True, agentId="afake", timestamp=stamp())) + "\n")
finish(message_id)
def refused(message_id):
"""A turn refused for a usage limit, as Claude Code 2.1 ends one: a rejected
rate_limit_event, a synthetic assistant API-error message and an is_error