Skip to content

feat(post_turn_hooks): a hook can read the agent's per-turn trajectories - #852

Closed
kaihackney-scale wants to merge 3 commits into
mainfrom
kai/post-turn-hook-trajectories
Closed

kaihackney-scale wants to merge 3 commits into
mainfrom
kai/post-turn-hook-trajectories

Conversation

@kaihackney-scale

@kaihackney-scale kaihackney-scale commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

What and why

A post-turn hook can read the envs and the agent's reply after each turn, but not what the agent did to get there:
its tool calls and their results. A hook that checks a milestone against the agent's work, the way a
trajectory-reading verifier grades it, needs that.

prompt_agent already stores each turn's native trajectory before the hooks run. This hands those to the hook:

  • turn.trajectory_urls: one entry per turn so far, oldest first; None for a turn whose trajectory the agent
    returned none of or that couldn't be fetched; empty when the agent's card offers no trajectories.
  • await turn.read_trajectory(n): turn n's trajectory as text (1 is the first turn), or None when that turn has
    none. A turn that hasn't happened raises IndexError.

Hooks that don't use them are unaffected; run_post_turn_hooks takes trajectory_urls as an optional argument.

How it was tested

  • New unit tests in test_prompt_agent_post_turn_hooks.py: over three turns, where the agent returns no trajectory
    for one of them, the hook sees one URL per turn so far, None for the missing turn, reads each stored trajectory's
    text, and is refused a turn ahead; an agent whose card offers no trajectories hands the hook an empty tuple. The first
    test fails without the one-line prompt_agent change.
  • pytest tst/unit/task_step -k "prompt_agent or post_turn or hook or user_sim or always_on": 1120 passed. ruff check on the changed files: passed.
  • End to end: a local agent-env task run of a two-turn prompt_agent task on a deployed claude-code agent, with a
    hook that records trajectory_urls and reads each trajectory back after every turn. After turn 1 it had one URL and
    read that turn's trajectory (with the agent's one tool call); after turn 2 it had two and read both; reading a turn
    ahead was refused both times.

🤖 Generated with Claude Code

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; no actionable issues remain.

Summary

Hooks can now read the agent’s stored trajectories after each turn.

  • Post-turn hooks can read the agent’s completed-turn trajectories.

Reviews (3) · Last reviewed commit: "Merge main into kai/post-turn-hook-traje..." · Reviewed by Greptile

A post-turn hook could read the envs and the agent's reply, but not what the agent did to get there: the tool calls
and their results. prompt_agent already stores each turn's native trajectory before the hooks run. Turn now carries
trajectory_urls, one per turn so far (None for a turn without one, empty when the agent's card offers none), and
read_trajectory(n) returns turn n's as text. A hook can then check a milestone against the work itself, as a
trajectory-reading verifier does, without its own copy of the env state.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kaihackney-scale
kaihackney-scale requested a review from a team as a code owner November 20, 2026 22:31
kaihackney-scale and others added 2 commits November 20, 2026 17:34
…offers no trajectories

With no trajectories, trajectory_urls is empty, so reading a turn that happened raised IndexError instead of
returning None. The turn number is now checked against the conversation's turn, and a turn with no entry is None.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Both sides extend the hook Turn: keeps read_trajectory and main's ask_user(note),
and run_post_turn_hooks takes both note-capable ask_user and trajectory_urls.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kaihackney-scale

Copy link
Copy Markdown
Collaborator Author

Combined into #912, which carries this change together with the milestone-gates hook and the user-sim presets, so they're reviewed and merged as one.

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