[FEATURE REQUEST] Add agent context fields to hook inputs
Summary
When hooks fire, there's no way to determine whether the hook is running in a main agent context or a subagent context. This limits the ability to implement context-aware hook behaviors for orchestrator-worker architectures.
Use Cases
1. Post-Compaction Recovery (SessionStart:compact)
Inject different recovery instructions based on context:
- Main agent (orchestrator): "Use Task tool to delegate, don't edit files directly"
- Subagent (worker): "Use selective git staging, follow Angular commit format"
2. Context-Aware Tool Validation (PreToolUse)
Apply different validation rules:
- Main agent: Block direct file edits, enforce delegation
- Subagent: Allow file edits, validate against task scope
3. Evidence Collection (PostToolUse)
Collect evidence with proper attribution:
- Main agent: Track orchestration decisions
- Subagent: Track implementation actions with task ID
4. Permission Enforcement (PreToolUse)
Implement role-based permissions:
- Main agent: Restrict to coordination tools (Task, Read, Grep)
- Subagent: Allow implementation tools (Edit, Write, Bash)
5. Logging & Debugging
Separate logs by context for easier debugging:
- Know which agent made which tool call
- Track subagent lifecycle with proper parent-child correlation
6. Resource Management
Apply different resource limits or timeouts:
- Main agent: Longer timeout for orchestration
- Subagent: Shorter timeout, scoped to single task
Current Limitation
All hooks receive the same session_id regardless of context:
// Same input for both orchestrator and subagent
{
"session_id": "abc-123",
"hook_event_name": "PreToolUse",
"tool_name": "Edit"
// No way to know: am I the orchestrator or a subagent?
}
Proposed Solution
Add context fields to ALL hook inputs:
{
"session_id": "abc-123",
"hook_event_name": "PreToolUse",
"tool_name": "Edit",
// New fields (all hooks)
"agent_context": "subagent", // "main" | "subagent"
"agent_id": "agent-def456", // unique ID if subagent, null if main
"parent_agent_id": null // for nested subagents (future)
}
For main agent context:
{
"agent_context": "main",
"agent_id": null,
"parent_agent_id": null
}
Additional Consideration: Teammate Integration
I noticed Anthropic is experimenting with the "Teammate" feature for persistent, named AI collaborators. If this feature moves forward, it would be valuable to include teammate identity in hook inputs as well:
{
"agent_context": "subagent",
"agent_id": "agent-def456",
// Teammate fields (if applicable)
"teammate_name": "backend-dev", // configured teammate name
"teammate_id": "teammate-abc123" // unique teammate identifier
}
This would enable:
- Teammate-specific configurations: Different hooks/rules per teammate
- Persistent memory integration: Associate tool calls with specific teammates
- Team coordination: Hooks that understand multi-teammate workflows
- Audit trails: Track which teammate performed which actions
Having a unified agent identity system across subagents and teammates would make the hook system more powerful and future-proof.
Workaround Attempted
Tried file-based tracking (write marker on SubagentStart, check on other hooks), but this fails because:
- When main agent's hook fires while subagent is running → marker exists → wrong context detected
- Can't distinguish "I am the subagent" from "a subagent is currently active"
Impact
This would enable plugins to:
- Implement proper orchestrator-worker patterns
- Apply role-based permissions and validations
- Inject context-appropriate instructions after compaction
- Collect properly attributed evidence and logs
- Build more sophisticated multi-agent workflows
- Prepare for teammate feature integration
Environment
- Claude Code version: 2.1.9+
- Platform: All (macOS, Linux, Windows)
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗