Distinguish forked sessions in session-start hooks (#44349)

## Why

Forked threads reported `startup`, causing startup hooks to run again even when their context was inherited from the parent. Resuming with supplied history also reported `startup` instead of `resume`.

## What changed

- Add `fork` as a `SessionStart` source and expose it in the hook input schema.
- Report `fork` for histories with a fork parent and `resume` for supplied histories without one.
- Continue dispatching `SubagentStart` for spawned subagents with either fresh or forked context.

## Testing

Add regression coverage for fork hook matching without duplicate startup context, resume hooks with supplied history, and `SubagentStart` context injection for fresh and forked subagents.

GitOrigin-RevId: 5ac1ae4ab8adcbbbceb8cb0ffb9b32b6402c585b
This commit is contained in:
Abhinav
2026-09-10 00:26:30 +00:00
committed by copyberry
parent c6a59ef923
commit e444aa99d7
7 changed files with 197 additions and 16 deletions
+3 -3
View File
@@ -127,14 +127,14 @@ pub(crate) async fn run_pending_session_start_hooks(
turn_context: &Arc<TurnContext>,
) -> bool {
while let Some(session_start_source) = sess.take_pending_session_start_source().await {
// Pending session-start hooks are reused to dispatch thread-spawn subagent
// starts. Other subagent sessions are internal/system work and do not run
// start hooks.
// Spawned subagents can start fresh or fork their parent's history, so both
// sources dispatch SubagentStart. Internal/system subagents skip start hooks.
let target = match &turn_context.session_source {
SessionSource::SubAgent(SubAgentSource::ThreadSpawn { agent_role, .. })
if matches!(
session_start_source,
codex_hooks::SessionStartSource::Startup
| codex_hooks::SessionStartSource::Fork
) =>
{
let context = subagent_hook_context(sess, agent_role);