MoonshotAI / MoonshotAI/kimi-code

feat(hooks): expose agent identity (agent_name / agent_id) in hook payloads, especially PreToolUse

Open
#3,742 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
7.5k
Forks
1.2k
Avg merge
11h 53m
Merged PRs (30d)
350

Description

Summary

Hook payloads carry no agent identity: a PreToolUse hook cannot tell whether a tool call comes from the main agent or a subagent — let alone which subagent. This makes per-agent guard/audit policies impossible to implement.

Evidence (measured on kimi-code 0.42.0)

I dumped the full stdin payload of a PreToolUse hook while (a) the main agent wrote a file and (b) a subagent dispatched via the Agent tool wrote a file. The two payloads are structurally identical:

hook_event_name, session_id, session_title, client_type, cwd, tool_name, tool_input, tool_call_id
  • session_id is the same for main agent and subagent calls
  • No field distinguishes the caller; no KIMI_* environment variable helps either

SubagentStart/SubagentStop do carry agent_name, but there is no shared correlation id (e.g. turn_id) linking them to PreToolUse calls, so with parallel subagents even a time-window heuristic is unreliable.

Use case

I run a workspace write-guard hook that checks every Write/Edit/Bash against a path registry. I want rules like "subagent finding-news may only write under results/; the main agent is unrestricted" — same path, different identity, different permission. Today only path-based rules are possible.

Prior art showing this capability is valuable:

  • opencode: the plugin API exposes session.agent (the actual agent name) + parentID inside tool.execute.before — per-agent rules work there today
  • Cursor: subagent preToolUse calls carry a distinct conversation_id and transcript_path: null — main/sub are distinguishable (though the subagent is unnamed)

Kimi Code currently offers no signal at all in PreToolUse.

Proposal

Add agent identity to the base hook payload so every event benefits:

field meaning
agent_name "main" for the main agent; the subagent name (explore, coder, or a custom agent name) otherwise
agent_id stable per-instance id, to disambiguate parallel instances of the same subagent type
parent_agent_id null for the main agent; the dispatching agent's id otherwise

Most valuable on: PreToolUse (blocking decisions), PostToolUse / PostToolUseFailure (audit), PermissionRequest / PermissionResult (approval UX), Stop.

Alternatively (or additionally): include turn_id in tool-event payloads so they can be joined with TurnStarted, which already has origin_kind / origin_name.

Related: #916 (per-event payload fields undocumented), #2122 (expose permission mode in payloads) — same theme: hooks need more caller context to be useful.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how the base hook payload is assembled for PreToolUse, PostToolUse, PermissionRequest, and Stop, then compare the existing SubagentStart/SubagentStop fields. Determine how agent_name, agent_id, parent_agent_id, or turn_id can be propagated across main and parallel subagents. Done means hook consumers can reliably identify the caller and correlate tool events without time-window heuristics.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, cli
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.