Skip to content

feat: wire execution-boundary interception points#6517

Merged
lucasgomide merged 4 commits into
mainfrom
luzk/hooks-exec-boundaries
Jul 14, 2026
Merged

feat: wire execution-boundary interception points#6517
lucasgomide merged 4 commits into
mainfrom
luzk/hooks-exec-boundaries

Conversation

@lucasgomide

@lucasgomide lucasgomide commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

With the dispatcher in place (#6516, now merged), the execution lifecycle still had no interception seams. This introduces the typed interception contexts and wires execution_start, input, output, and execution_end for both crews and flows: prepare_kickoff and Flow.kickoff_async let a hook rewrite resolved inputs before a run, while Crew._create_crew_output and the flow tail let the final result be observed or replaced. It closes the eight critical-path points the policy engine needs without touching the legacy hooks.

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Execution-level interception points and typed contexts are added for crew and flow lifecycle inputs and outputs. Crew kickoff, flow kickoff, flow resume, and completion paths now dispatch these events and apply hook-modified payloads. Conformance tests cover firing, rewriting, propagation, and abortion behavior.

Changes

Execution interception

Layer / File(s) Summary
Interception contracts
lib/crewai/src/crewai/hooks/dispatch.py, lib/crewai/src/crewai/hooks/contexts.py
Adds execution lifecycle interception points and typed contexts carrying mutable payloads, inputs, outputs, and framework references.
Crew execution boundaries
lib/crewai/src/crewai/crews/utils.py, lib/crewai/src/crewai/crew.py
Routes crew kickoff inputs and final outputs through execution-start, input, output, and execution-end dispatches.
Flow execution boundaries
lib/crewai/src/crewai/flow/runtime/__init__.py
Adds interception dispatches to flow kickoff and resume paths, applies rewritten inputs and outputs, republishes updated flow input baggage, and detaches it during cleanup.
Interception conformance validation
lib/crewai/tests/hooks/test_interception_conformance.py
Tests boundary firing counts, input payload propagation, output rewriting, and HookAborted handling.

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant CrewOrFlow
  participant Dispatch
  participant Execution
  Caller->>CrewOrFlow: start execution with inputs
  CrewOrFlow->>Dispatch: dispatch EXECUTION_START
  CrewOrFlow->>Dispatch: dispatch INPUT
  Dispatch-->>CrewOrFlow: return modified inputs
  CrewOrFlow->>Execution: execute with resolved inputs
  Execution-->>CrewOrFlow: produce output
  CrewOrFlow->>Dispatch: dispatch OUTPUT
  CrewOrFlow->>Dispatch: dispatch EXECUTION_END
  Dispatch-->>CrewOrFlow: return modified output
  CrewOrFlow-->>Caller: return final output
Loading

Suggested reviewers: vinibrsl

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 12.50% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly summarizes the main change: adding execution-boundary interception points.
Description check ✅ Passed The description is directly aligned with the PR and accurately describes the new interception hooks and wiring.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch luzk/hooks-exec-boundaries

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Comment thread lib/crewai/src/crewai/crew.py
Comment thread lib/crewai/src/crewai/flow/runtime/__init__.py
Comment thread lib/crewai/src/crewai/crews/utils.py
Comment thread lib/crewai/src/crewai/flow/runtime/__init__.py
Comment thread lib/crewai/src/crewai/flow/runtime/__init__.py Outdated
@lucasgomide lucasgomide force-pushed the luzk/hooks-exec-boundaries branch from 790bde5 to ea20c1a Compare July 11, 2026 17:16
@lucasgomide lucasgomide force-pushed the luzk/hooks-dispatcher branch from 8bc4fa2 to a85e100 Compare July 11, 2026 17:16
@lucasgomide lucasgomide marked this pull request as draft July 11, 2026 17:22
@lucasgomide lucasgomide force-pushed the luzk/hooks-exec-boundaries branch from ea20c1a to 73bdfaa Compare July 11, 2026 22:26
@github-actions github-actions Bot added size/L and removed size/M labels Jul 11, 2026
@lucasgomide lucasgomide force-pushed the luzk/hooks-exec-boundaries branch 4 times, most recently from 852a3ec to cdd79da Compare July 13, 2026 19:18
@lucasgomide lucasgomide marked this pull request as ready for review July 14, 2026 05:45
Comment thread lib/crewai/src/crewai/crew.py
vinibrsl
vinibrsl previously approved these changes Jul 14, 2026
@lucasgomide lucasgomide force-pushed the luzk/hooks-dispatcher branch from 0471ad0 to 8a24330 Compare July 14, 2026 13:53
@lucasgomide lucasgomide force-pushed the luzk/hooks-exec-boundaries branch from f5d67bd to a867c45 Compare July 14, 2026 13:53

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit a867c45. Configure here.

Comment thread lib/crewai/src/crewai/flow/runtime/__init__.py
Base automatically changed from luzk/hooks-dispatcher to main July 14, 2026 14:27
@lucasgomide lucasgomide dismissed vinibrsl’s stale review July 14, 2026 14:27

The base branch was changed.

Adds the typed interception contexts (`crewai/hooks/contexts.py`) and wires
the `execution_start`, `input`, `output`, and `execution_end` points for both
crews and flows through the dispatcher. `prepare_kickoff` and
`Flow.kickoff_async` fire `execution_start`/`input` so a hook can rewrite
resolved inputs before the run, while `Crew._create_crew_output` and the flow
tail fire `output`/`execution_end` so the final result can be observed or
replaced. Closes the eight critical-path points without touching the legacy
hooks.
Reworks the crew and flow boundary seams flagged in review. `OUTPUT` and
`EXECUTION_END` now run before the completion event (`CrewKickoffCompletedEvent`
and `FlowFinishedEvent`) so a `HookAborted` no longer leaves a spurious
completed signal and a returned payload replacement is honored on the emitted
and returned result. Boundary contexts alias `inputs` to the same object as
`payload` instead of a fresh dict from `or`, so in-place edits survive
read-back. Flows re-publish the resolved inputs into `flow_inputs` baggage
after the `INPUT` hook so trigger-payload injection observes hook rewrites, and
a resumed flow now dispatches `OUTPUT`/`EXECUTION_END` on its completion path.
Removes two inline comments narrating the OUTPUT/EXECUTION_END dispatch
ordering in `crew.py` and the flow runtime, plus a stray sentence about
enterprise adapters in the conformance-suite docstring. Comment-only
cleanup, no behavior change.
`_create_crew_output` reassigned `crew_output` from the hook contexts'
`payload`, which is typed `Any`, so mypy flagged `no-any-return` at the
function's return. Cast the payload back to `CrewOutput` after each
dispatch and split the `ExecutionEndContext` construction to satisfy
`ruff format`'s line-length limit.
@lucasgomide lucasgomide force-pushed the luzk/hooks-exec-boundaries branch from a867c45 to fa0ed74 Compare July 14, 2026 14:34

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
lib/crewai/src/crewai/crew.py (1)

1936-1944: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Emit crew_output in the completion event. OUTPUT and EXECUTION_END can mutate the payload, but CrewKickoffCompletedEvent still sends the pre-interception final_task_output, so listeners see a different value than the one returned. CrewKickoffCompletedEvent.output already accepts Any, so no schema change is needed.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@lib/crewai/src/crewai/crew.py` around lines 1936 - 1944, Update the
CrewKickoffCompletedEvent construction in the crew kickoff completion flow to
emit the post-interception crew_output rather than the pre-interception
final_task_output. Preserve the existing event metadata and return behavior,
using the already-available crew_output value for the event’s output field.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@lib/crewai/src/crewai/crew.py`:
- Around line 1936-1944: Update the CrewKickoffCompletedEvent construction in
the crew kickoff completion flow to emit the post-interception crew_output
rather than the pre-interception final_task_output. Preserve the existing event
metadata and return behavior, using the already-available crew_output value for
the event’s output field.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: c4a5d8ba-18b8-40bd-8706-e572c4deb6b3

📥 Commits

Reviewing files that changed from the base of the PR and between 7d21283 and fa0ed74.

📒 Files selected for processing (6)
  • lib/crewai/src/crewai/crew.py
  • lib/crewai/src/crewai/crews/utils.py
  • lib/crewai/src/crewai/flow/runtime/__init__.py
  • lib/crewai/src/crewai/hooks/contexts.py
  • lib/crewai/src/crewai/hooks/dispatch.py
  • lib/crewai/tests/hooks/test_interception_conformance.py

@lucasgomide lucasgomide merged commit a194f38 into main Jul 14, 2026
55 of 56 checks passed
@lucasgomide lucasgomide deleted the luzk/hooks-exec-boundaries branch July 14, 2026 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants