Skip to content

feat(hooks): add PreTurn and PostTurn lifecycle hook events (roadmap 7.28)#185

Merged
emal-avala merged 1 commit intomainfrom
feat/turn-lifecycle-hooks
Apr 23, 2026
Merged

feat(hooks): add PreTurn and PostTurn lifecycle hook events (roadmap 7.28)#185
emal-avala merged 1 commit intomainfrom
feat/turn-lifecycle-hooks

Conversation

@emal-avala
Copy link
Copy Markdown
Member

Summary

Closes roadmap item 7.28. Adds two new HookEvent variants:

  • PreTurn — fires after user input is in history, before the LLM is called. Context payload: turn number + user-input preview (first 200 chars).
  • PostTurn — fires on successful turn completion (no tool_use in the assistant's response). Not fired on cancel or error — hooks shouldn't have to guard against 'did the turn actually succeed?'.

Configure via [[hooks]] in settings.toml with event = "pre_turn" or event = "post_turn". Shell and HTTP action types both work out of the box.

Why

PreToolUse / PostToolUse hooks exist, but there's no way to run something once per agent turn. Common asks:

  • snapshot /proc/meminfo per turn (perf regression bisection)
  • log turn start/end to a prometheus gateway
  • run a project-specific linter after the agent finishes a turn

Turn-level hooks close that gap.

Behavior

  • PreTurn fires after push_message(user_msg), before enable_caching / API call. Fires once per run_turn_with_sink invocation regardless of how many internal tool iterations happen.
  • PostTurn fires only at the happy-path exit (no tool_use). The max-turns / cancel / error / overflow paths are deliberately not hooked — those aren't 'a turn completed'.

Context JSON carries turn + user_input_preview for PreTurn, turn + total_turns for PostTurn. Shell/HTTP actions don't consume context today — wiring is in place for when they do.

Test plan

  • cargo fmt --all clean
  • cargo check passes
  • cargo clippy --all-targets -- -D warnings clean
  • cargo test -p agent-code-lib --lib config::schema::tests::hook_event — 7 variants round-trip (5 existing + 2 new)
  • Manual: [[hooks]] event = "post_turn" action = { type = "shell", command = "echo done >> /tmp/turns.log" } → log line per successful turn
  • Manual: cancelled turn (Ctrl-C) does NOT append to the log

…7.28)

Adds two new HookEvent variants:
- PreTurn: fires after user input is in history, before the LLM is
  called. Context payload includes turn number and user-input preview.
- PostTurn: fires on successful turn completion (no tool_use in the
  assistant's response). NOT fired on cancel or error paths — hooks
  shouldn't have to guard against 'did the turn actually succeed?'.

Both events integrate with the existing HookRegistry::run_hooks
machinery — configured via `[[hooks]]` in settings.toml with
`event = "pre_turn"` or `event = "post_turn"`. Shell and HTTP
actions both work.

Context JSON is passed through HookRegistry::run_hooks's third
parameter — the existing shell/http hook actions don't consume it
today, but the wiring is in place for when they do (e.g. writing it
to stdin or a query-string).
@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@emal-avala emal-avala merged commit c080ff8 into main Apr 23, 2026
13 of 14 checks passed
@emal-avala emal-avala deleted the feat/turn-lifecycle-hooks branch April 23, 2026 02:19
@emal-avala emal-avala mentioned this pull request Apr 23, 2026
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant