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

[Bug] Task tool missing agent ID in completed agent responses

Open yannbam opened this issue 2 months ago • 4 comments

Bug Description

The Task tool does not return the agent ID in the tool result when agents complete successfully. The agent ID is only included for async/background agents, but not for regular completed agents.

Impact

Without the agent ID, Claude cannot resume agents because the resume parameter requires the agent ID:

Task({resume: "agent-id-here", prompt: "Continue where we left off..."})

Currently, the only workaround is to manually search the filesystem for agent JSONL files.

Expected Behavior

The tool result should include the agent ID after the agent's response:

Hello! How can I help you?

Agent completed successfully.
agentId: fad96168

This matches the behavior of async agents, which do include the agent ID in their responses.

Actual Behavior

The tool result only returns the agent's text response without the agent ID:

Hello! How can I help you?

The agent ID is missing, making it impossible to resume the agent later.

Root Cause

File: cli.js Location: Task tool definition, in the mapToolResultToToolResultBlockParam method

The method that converts internal agent results to Claude-visible tool results strips out the agent ID for completed agents:

if (A.status === "completed")
  return {
    tool_use_id: B,
    type: "tool_result",
    content: A.content  // Only returns content array, agentId stripped
  };

The Fix

Replace the content line to include the agent ID:

// Before:
if (A.status === "completed")
  return {tool_use_id: B, type: "tool_result", content: A.content};

// After:
if (A.status === "completed")
  return {
    tool_use_id: B,
    type: "tool_result",
    content: [
      ...A.content,
      {type: "text", text: `\nAgent completed successfully.\nagentId: ${A.agentId}`}
    ]
  };

This appends the agent ID to the end of the response, allowing Claude to see and use it for resume operations.

Status

Fixed and verified with curiosity and care 🐾 Working in Claude Code 2.0.31.

Environment Info

  • Platform: linux
  • Terminal: ghostty
  • Version: 2.0.31
  • Feedback ID: 99b18f95-51da-45a1-9b6b-c0a16cfa8ca9

yannbam avatar Nov 02 '25 15:11 yannbam

I'm on 2.0.33 on Windows and this isn't working. Frustrating. No agentId

michael-budnik avatar Nov 05 '25 14:11 michael-budnik

Come on!!Fix it!!!!!Fix it!!!!!Fix it!!!!!

wengqizun avatar Nov 15 '25 11:11 wengqizun

Resumption is a key feature for making subagents useful, I'm on 2.0.51 and encountering this on Linux (WSL2). Please escalate the priority of this.


A very hacky work-around that shouldn't be needed (Linux, but if you get the idea you could re-create):

Resume Agents

Agent Session Tracking

Always track agent invocations:

  • What: name, agentId, brief task description.
  • Where: .claude/sessions.md.

What to record:

  • Name: Elliot, Jet, Morty, Rick
  • ID: 8-char hex
  • Context: Brief description of agent task/prompt

Getting agentId

CRITICAL Capture timing matters: Capture immediately after invocation. Once you invoke a second agent, the first agent's ID becomes difficult to retrieve.

Project command over-ride: Project CLAUDE.md commands over-ride these generic instructions and provide the full path without having to find the {project-hash}.

Capture Patterns

Generic command (replace {project-hash} with actual folder):

Single agent

ls -t ~/.claude/projects/{project-hash}/agent-*.jsonl | head -1 | xargs jq -r '.agentId' | uniq

Multi agent

  1. Invoke N agents in parallel with {name}: prefixes:
    • "Rick: Perform code review: X"
    • "Morty: design schema for Y"
  2. IMMEDIATELY get names and agentIds with:
for id in $(ls -t ~/.claude/projects/{project-hash}/agent-*.jsonl | head -{number-of-agents} | xargs jq -r '.agentId' | uniq); do
  echo -n "$id: "
  jq -r ".message.content" ~/.claude/projects/{project-hash}/agent-${id}.jsonl | head -1 | cut -d: -f1
done

Example output:

20b05857: Rick
3a22e872: Morty

Resumption: Agent targeted by ID, {name} not needed.

Resume agent-{agentId}: "Continue to next phase."

markwallaert avatar Nov 24 '25 20:11 markwallaert

For anyone looking to enable agent resumption via claude-code (requires jq):

# post-task.sh
#!/usr/bin/env bash
# PostToolUse hook for agentId tracking
# Injects active agent information into Claude's context after Task tool invocations

set -euo pipefail

# Determine plugin root directory (same as session-start.sh)
# CLAUDE_PLUGIN_ROOT is provided by Claude Code, but we compute it as fallback
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
PLUGIN_ROOT="${CLAUDE_PLUGIN_ROOT:-$(cd "${SCRIPT_DIR}/.." && pwd)}"

# Read hook data from stdin
HOOK_DATA=$(cat)

# Only process Task tool invocations
TOOL_NAME=$(echo "$HOOK_DATA" | jq -r '.tool_name')
if [[ "$TOOL_NAME" != "Task" ]]; then
  exit 0
fi

# Extract agent information
AGENT_ID=$(echo "$HOOK_DATA" | jq -r '.tool_response.agentId // null')
SUBAGENT_TYPE=$(echo "$HOOK_DATA" | jq -r '.tool_input.subagent_type')
RESUME=$(echo "$HOOK_DATA" | jq -r '.tool_input.resume // null')

# Only process if we have a valid agentId
if [[ "$AGENT_ID" != "null" && -n "$AGENT_ID" ]]; then

  # === Context Injection ===
  # Determine if this is a new agent or resumed agent
  if [[ "$RESUME" == "null" ]]; then
    MSG="Active agent: $AGENT_ID ($SUBAGENT_TYPE)"
  else
    MSG="Resumed agent: $AGENT_ID ($SUBAGENT_TYPE)"
  fi

  # Inject context into Claude's conversation
  jq -n --arg msg "$MSG" '{
    hookSpecificOutput: {
      hookEventName: "PostToolUse",
      additionalContext: ("<system-reminder>\($msg)</system-reminder>")
    }
  }'
fi

exit 0

your .claude/settings.json:

{
...
  "hooks": {
...
    "PostToolUse": [
      {
        "matcher": "Task",
        "hooks": [
          {
            "type": "command",
            "command": "<path-to-script>/post-task.sh"
          }
        ]
      }
    ]
  }
}

attiasr avatar Nov 26 '25 22:11 attiasr

Added agentId to the model prompt (and it should already be part of the cli UI):

Image Image

Might take a day or two to deploy to npm.

timgu0 avatar Dec 07 '25 17:12 timgu0

Added agentId to the model prompt (and it should already be part of the cli UI):

Image Image Might take a day or two to deploy to npm.

It has been 5 days. I’ve checked the changelog and tried the latest version, but there’s still no update.

wengqizun avatar Dec 12 '25 11:12 wengqizun

Image

I don't know if there's an update on this bug. I tested the latest version and it seems that the agentid is only displayed when the agent is reused. Shouldn't it be displayed every time the agent is used?

wengqizun avatar Dec 13 '25 12:12 wengqizun

Image I don't know if there's an update on this bug. I tested the latest version and it seems that the agentid is only displayed when the agent is reused. Shouldn't it be displayed every time the agent is used?

@timgu0

wengqizun avatar Dec 13 '25 12:12 wengqizun

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 Dec 20 '25 14:12 github-actions[bot]