Skip to content

test: pin the order of existing hooks and events (#241 PR 0) - #242

Merged
yuanhao merged 2 commits into
mainfrom
feat/extension-hook-order
Oct 6, 2026
Merged

yuanhao merged 2 commits into
mainfrom
feat/extension-hook-order

Conversation

@yuanhao

@yuanhao yuanhao commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator

PR 0 of #241. It adds tests/hook_order_test.rs and changes no behaviour.

The Extension refactor (#241 PR 1) re-routes the trait hooks through one dispatch path and leaves the closures where they are. This test must pass unchanged across that refactor: any difference is a behaviour change for existing users, such as yoyo's on_before_turn checkpoint mode.

What it records:

  • Every hook, installed on one Agent: input filters (sync and async), ToolSource, on_before_turn, TurnHook, the provider call, ToolMiddleware, tool execute, on_error, on_after_turn.
  • Every event, interleaved with the hooks. Each hook records how many events had been sent when it fired (the channel stays undrained until the run ends, so its length is exact). The timeline is therefore deterministic, not a race with a consumer. The test passed 30 times in a row locally.
  • AgentEnd's final messages.

Paths: a plain answer, a tool call then an answer, a refusal carrying a tool call, a provider error, a cancel while streaming, a cancel while tools run, a turn limit, and a rejected input.

Three things today's behaviour does, pinned as is and marked in the test. They're worth a separate issue rather than a change here:

  1. Turn limit: the limit is checked after TurnStart, so that TurnStart never gets a TurnEnd.
  2. Cancel while streaming: the aborted assistant message ends up in the final history, but no MessageStart or MessageEnd is sent for it, so a transcript built from events misses it.
  3. Cancel before tools run: if the run is cancelled after the response arrives but before its tools start, the tools still execute. The cancel marker only lands at the next turn.

Checks: cargo clippy --all-targets with --all-features and with --no-default-features; cargo fmt --check.

🤖 Generated with Claude Code

https://claude.ai/code/session_01T7iq5hpndSiHQcnAsywKuG

yuanhao and others added 2 commits October 6, 2026 18:44
 PR 0)

Records each hook with the number of events sent when it fired, so the
timeline is exact. Covers a plain answer, a tool call, a refusal, a
provider error, a cancel while streaming, a cancel while tools run, a turn
limit, and a rejected input. No behaviour change.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01T7iq5hpndSiHQcnAsywKuG
… notes, continue_loop and errors before output

Adds the cases the review found missing: the turn hook runs once per
provider attempt; filters and middleware run in installation order (a
Modify feeds the next, a Deny stops the chain, a Warn passes); a sourced
tool goes through middleware; a turn hook's note lands on the latest user
message; continue_loop runs no input filters; a provider error or cancel
before any output leaves a message with no MessageStart/MessageEnd; a
cancel after streaming started ends the message as Aborted. Renames
cancelled_while_tools_run to cancelled_before_tools_start and corrects the
notes on current behaviour. The original eight timelines are unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01T7iq5hpndSiHQcnAsywKuG
@yuanhao
yuanhao merged commit f956bd4 into main Oct 6, 2026
14 checks passed
yuanhao added a commit that referenced this pull request Oct 6, 2026
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01T7iq5hpndSiHQcnAsywKuG

# Conflicts:
#	tests/hook_order_test.rs
@yuanhao
yuanhao deleted the feat/extension-hook-order branch October 6, 2026 20:22
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