-
Notifications
You must be signed in to change notification settings - Fork 7.8k
feat: wire execution-boundary interception points #6517
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d114a0f
feat: wire execution-boundary interception points
lucasgomide 7d3e83d
fix: correct execution-boundary hook ordering and input aliasing
lucasgomide 2781b71
chore: drop redundant seam comments from execution-boundary wiring
lucasgomide fa0ed74
fix: keep crew output typed across boundary hook dispatch
lucasgomide File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| """Typed contexts for the interception points wired in phases 2-5. | ||
|
|
||
| Each context is a dataclass whose fields are nullable and defaulted, so a field | ||
| that is not meaningful for a given runtime (e.g. ``agent_role`` inside a flow) | ||
| is simply ``None`` rather than an error. Every context exposes a ``payload`` | ||
| field: the interceptable value a hook may mutate in place or replace by | ||
| returning a new value. | ||
|
|
||
| The legacy ``pre/post_model_call`` and ``pre/post_tool_call`` points keep using | ||
| :class:`~crewai.hooks.llm_hooks.LLMCallHookContext` and | ||
| :class:`~crewai.hooks.tool_hooks.ToolCallHookContext` for backwards | ||
| compatibility; they are intentionally not redefined here. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass, field | ||
| from typing import Any | ||
|
|
||
|
|
||
| @dataclass | ||
| class InterceptionContext: | ||
| """Base context shared by the framework-native interception points.""" | ||
|
|
||
| payload: Any = None | ||
| agent: Any = None | ||
| agent_role: str | None = None | ||
| task: Any = None | ||
| crew: Any = None | ||
| flow: Any = None | ||
|
|
||
|
|
||
| @dataclass | ||
| class ExecutionStartContext(InterceptionContext): | ||
| """``execution_start``: a crew or flow is about to begin. ``payload`` = inputs.""" | ||
|
|
||
| inputs: dict[str, Any] = field(default_factory=dict) | ||
|
|
||
|
|
||
| @dataclass | ||
| class InputContext(InterceptionContext): | ||
| """``input``: resolved inputs for an execution. ``payload`` = inputs.""" | ||
|
|
||
| inputs: dict[str, Any] = field(default_factory=dict) | ||
|
|
||
|
|
||
| @dataclass | ||
| class OutputContext(InterceptionContext): | ||
| """``output``: final result of a crew or flow. ``payload`` = the output object.""" | ||
|
|
||
| output: Any = None | ||
|
|
||
|
|
||
| @dataclass | ||
| class ExecutionEndContext(InterceptionContext): | ||
| """``execution_end``: a crew or flow has finished. ``payload`` = the output object.""" | ||
|
|
||
| output: Any = None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| """Conformance suite for the framework-native interception points. | ||
|
|
||
| For each wired point this suite asserts the shared contract: the probe hook | ||
| sees a well-shaped payload, an in-place/returned modification is honored, and a | ||
| :class:`HookAborted` interrupts the step. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from crewai.flow.flow import Flow, listen, start | ||
| from crewai.hooks.dispatch import ( | ||
| HookAborted, | ||
| InterceptionPoint, | ||
| clear_all, | ||
| on, | ||
| ) | ||
| import pytest | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def clear_dispatch_registry(): | ||
| clear_all() | ||
| yield | ||
| clear_all() | ||
|
|
||
|
|
||
| class _SimpleFlow(Flow): | ||
| @start() | ||
| def begin(self): | ||
| return "begin" | ||
|
|
||
| @listen(begin) | ||
| def finish(self, _): | ||
| return "flow-result" | ||
|
|
||
|
|
||
| class TestFlowExecutionBoundaries: | ||
| """execution_start / input / output / execution_end on a flow.""" | ||
|
|
||
| def test_all_boundary_points_fire_once(self): | ||
| fired: list[str] = [] | ||
|
|
||
| for point in ( | ||
| InterceptionPoint.EXECUTION_START, | ||
| InterceptionPoint.INPUT, | ||
| InterceptionPoint.OUTPUT, | ||
| InterceptionPoint.EXECUTION_END, | ||
| ): | ||
|
|
||
| @on(point) | ||
| def _probe(ctx, _point=point): | ||
| fired.append(_point.value) | ||
|
|
||
| _SimpleFlow().kickoff(inputs={"seed": 1}) | ||
|
|
||
| assert fired == [ | ||
| "execution_start", | ||
| "input", | ||
| "output", | ||
| "execution_end", | ||
| ] | ||
|
|
||
| def test_output_modification_is_honored(self): | ||
| @on(InterceptionPoint.OUTPUT) | ||
| def rewrite(ctx): | ||
| return "intercepted" | ||
|
|
||
| result = _SimpleFlow().kickoff() | ||
| assert result == "intercepted" | ||
|
|
||
| def test_input_payload_carries_inputs(self): | ||
| seen: dict = {} | ||
|
|
||
| @on(InterceptionPoint.INPUT) | ||
| def capture(ctx): | ||
| seen.update(ctx.payload or {}) | ||
|
|
||
| _SimpleFlow().kickoff(inputs={"seed": 42}) | ||
| assert seen == {"seed": 42} | ||
|
|
||
| def test_abort_at_execution_start_interrupts(self): | ||
| @on(InterceptionPoint.EXECUTION_START) | ||
| def block(ctx): | ||
| raise HookAborted(reason="not allowed", source="policy") | ||
|
|
||
| with pytest.raises(HookAborted) as exc: | ||
| _SimpleFlow().kickoff() | ||
| assert exc.value.reason == "not allowed" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.