Skip to content

Add subagent identity to hook inputs#22882

Merged
abhinav-oai merged 14 commits into
mainfrom
abhinav/subagent-hook-context-stack
May 21, 2026
Merged

Add subagent identity to hook inputs#22882
abhinav-oai merged 14 commits into
mainfrom
abhinav/subagent-hook-context-stack

Conversation

@abhinav-oai
Copy link
Copy Markdown
Collaborator

@abhinav-oai abhinav-oai commented May 15, 2026

What

When a normal hook fires inside a thread-spawned subagent, Codex now includes these optional top-level fields in the hook input:

  • agent_id: the child thread id
  • agent_type: the subagent role

Root-agent hook inputs omit these fields. SubagentStart and SubagentStop keep their existing required agent_id and agent_type fields because those events are inherently subagent-scoped.

This does not change matcher behavior. Tool hooks still match on tool name, compact hooks still match on trigger, and UserPromptSubmit still ignores matchers. Only SubagentStart and SubagentStop match on agent_type.

This was referenced May 15, 2026
abhinav-oai added a commit that referenced this pull request May 19, 2026
# What

`SubagentStart` runs once when Codex creates a thread-spawned subagent,
before that child sends its first model request. Thread-spawned
subagents use `SubagentStart` instead of the normal root-agent
`SessionStart` hook.

Configured handlers match on the subagent `agent_type`, using the same
value passed to `spawn_agent`. When no agent type is specified, Codex
uses the default agent type.

Hook input includes the normal session-start fields plus:

- `agent_id`: the child thread id.
- `agent_type`: the resolved subagent type.

`SubagentStart` may return `hookSpecificOutput.additionalContext`. That
context is added to the child conversation before the first model
request.

# Lifecycle Scope

Only thread-spawned subagents run `SubagentStart`.

Internal/system subagents such as Review, Compact, MemoryConsolidation,
and Other do not run normal `SessionStart` hooks and do not run
`SubagentStart`. This avoids exposing synthetic matcher labels for
internal implementation paths.

Also the `SessionStart` hook no longer fires for subagents, this matches
behavior with other coding agents' implementation

# Stack

1. This PR: add `SubagentStart`.
2. #22873: add `SubagentStop`.
3. #22882: add subagent identity to normal hook inputs.
…o abhinav/subagent-hook-context-stack

# Conflicts:
#	codex-rs/core/src/hook_runtime.rs
#	codex-rs/core/tests/suite/subagent_notifications.rs
abhinav-oai added a commit that referenced this pull request May 20, 2026
# What

<img width="1792" height="1024" alt="image"
src="https://github.com/user-attachments/assets/8f81d232-5813-4994-a61d-e42a05a93a3e"
/>

`SubagentStop` runs when a thread-spawned subagent turn is about to
finish. Thread-spawned subagents use `SubagentStop` instead of the
normal root-agent `Stop` hook.

Configured handlers match on `agent_type`. Hook input includes the
normal stop fields plus:

- `agent_id`: the child thread id.
- `agent_type`: the resolved subagent type.
- `agent_transcript_path`: the child subagent transcript path.
- `transcript_path`: the parent thread transcript path.
- `last_assistant_message`: the final assistant message from the child
turn, when available.
- `stop_hook_active`: `true` when the child is already continuing
because an earlier stop-like hook blocked completion.

`SubagentStop` shares the same completion-control semantics as `Stop`,
scoped to the child turn:

- No decision allows the child turn to finish.
- `decision: "block"` with a non-empty `reason` records that reason as
hook feedback and continues the child with that prompt.
- `continue: false` stops the child turn. If `stopReason` is present,
Codex surfaces it as the stop reason.

# Lifecycle Scope

Only thread-spawned subagents run `SubagentStop`.

Internal/system subagents such as Review, Compact, MemoryConsolidation,
and Other do not run normal `Stop` hooks and do not run `SubagentStop`.
This avoids exposing synthetic matcher labels for internal
implementation paths.

# Stack

1. #22782: add `SubagentStart`.
2. This PR: add `SubagentStop`.
3. #22882: add subagent identity to normal hook inputs.
Base automatically changed from abhinav/subagent-stop-stack to main May 20, 2026 21:59
@abhinav-oai abhinav-oai changed the title [codex] Add subagent identity to hook inputs Add subagent identity to hook inputs May 20, 2026
…-context-stack

# Conflicts:
#	codex-rs/core/src/hook_runtime.rs
#	codex-rs/core/tests/suite/subagent_notifications.rs
@abhinav-oai abhinav-oai marked this pull request as ready for review May 20, 2026 22:44
@abhinav-oai abhinav-oai requested a review from a team as a code owner May 20, 2026 22:44
LEON-gittech pushed a commit to LEON-gittech/Open-Codex-CLI that referenced this pull request May 21, 2026
# What

`SubagentStart` runs once when Codex creates a thread-spawned subagent,
before that child sends its first model request. Thread-spawned
subagents use `SubagentStart` instead of the normal root-agent
`SessionStart` hook.

Configured handlers match on the subagent `agent_type`, using the same
value passed to `spawn_agent`. When no agent type is specified, Codex
uses the default agent type.

Hook input includes the normal session-start fields plus:

- `agent_id`: the child thread id.
- `agent_type`: the resolved subagent type.

`SubagentStart` may return `hookSpecificOutput.additionalContext`. That
context is added to the child conversation before the first model
request.

# Lifecycle Scope

Only thread-spawned subagents run `SubagentStart`.

Internal/system subagents such as Review, Compact, MemoryConsolidation,
and Other do not run normal `SessionStart` hooks and do not run
`SubagentStart`. This avoids exposing synthetic matcher labels for
internal implementation paths.

Also the `SessionStart` hook no longer fires for subagents, this matches
behavior with other coding agents' implementation

# Stack

1. This PR: add `SubagentStart`.
2. openai#22873: add `SubagentStop`.
3. openai#22882: add subagent identity to normal hook inputs.
@abhinav-oai abhinav-oai merged commit 16d85e2 into main May 21, 2026
31 checks passed
@abhinav-oai abhinav-oai deleted the abhinav/subagent-hook-context-stack branch May 21, 2026 21:54
@github-actions github-actions Bot locked and limited conversation to collaborators May 21, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants