Skip to content

Remove StateProxy from flow state access#6327

Merged
vinibrsl merged 1 commit into
mainfrom
nuke-state-proxy
Jun 24, 2026
Merged

Remove StateProxy from flow state access#6327
vinibrsl merged 1 commit into
mainfrom
nuke-state-proxy

Conversation

@vinibrsl

@vinibrsl vinibrsl commented Jun 24, 2026

Copy link
Copy Markdown
Member

StateProxy looked like a thread-safety boundary, but it only protected a small slice of state operations. Some examples of operations that were not covered:

  • self.state.counter += 1, self.state["counter"] += 1 (increments)
  • self.state.user.profile.score += 1 (nested object mutations)
  • self.state.config["limits"]["max"] = 10 (mutation through model fields)
  • self.state.items[0].status = "done" (list/container mutations)

This commit decided to remove it completely for simplicity and performance:

  • Simpler runtime code
  • attr read: 24x faster, attr write: 27x faster, list append: 19x faster (local benchmark)
  • Clearer concurrency contract (lifecycle locks remain, but arbitrary shared state mutation is not presented as thread-safe)

Note

Medium Risk
Parallel flow listeners can race on shared flow.state mutations where proxies previously gave partial protection; callers must not assume thread-safe arbitrary state updates.

Overview
Removes the StateProxy / LockedDictProxy / LockedListProxy layer and Flow._state_lock. flow.state now returns the underlying state object directly instead of a thread-safe wrapper.

Public API: Those proxy types are dropped from crewai.flow.flow exports. AgentExecutor no longer exposes a custom state property over StateProxy.

Tests no longer assert atomic shared-state updates under parallel branches; parallel join behavior is checked via a locked execution-order recorder. Persistence and definition parity tests expect plain state objects (e.g. isinstance(flow.state, State) instead of _unwrap()).

Reviewed by Cursor Bugbot for commit f0dd16a. Bugbot is set up for automated code reviews on this repo. Configure here.

Summary by CodeRabbit

  • Bug Fixes
    • Improved flow state handling by removing the state proxy layer, so flow.state now exposes the underlying state object directly.
    • Enhanced parallel branching behavior so joined steps wait for all parallel branches to complete.
    • Updated tests to align with the new state behavior and concurrency expectations.
  • Refactor
    • Reduced the exported runtime surface area by no longer exposing thread-safety state proxy types via the legacy flow import path.

@corridor-security corridor-security 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.

Summary: This PR removes thread-safe flow state proxy exports/access and exposes the underlying flow state directly.

Risk: Low risk. No exploitable security vulnerabilities were identified because the change affects internal flow state concurrency behavior and does not introduce public endpoints, authentication/authorization changes, SQL/file/network handling, or new trust boundaries.

@coderabbitai

coderabbitai Bot commented Jun 24, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: d78cb578-eb0a-494f-b32d-f90acde48138

📥 Commits

Reviewing files that changed from the base of the PR and between f724c73 and f0dd16a.

📒 Files selected for processing (8)
  • lib/crewai/src/crewai/experimental/agent_executor.py
  • lib/crewai/src/crewai/flow/flow.py
  • lib/crewai/src/crewai/flow/runtime/__init__.py
  • lib/crewai/tests/agents/test_agent_executor.py
  • lib/crewai/tests/test_flow.py
  • lib/crewai/tests/test_flow_conversation.py
  • lib/crewai/tests/test_flow_from_definition.py
  • lib/crewai/tests/test_flow_persistence.py
💤 Files with no reviewable changes (2)
  • lib/crewai/src/crewai/flow/flow.py
  • lib/crewai/tests/test_flow_conversation.py
✅ Files skipped from review due to trivial changes (1)
  • lib/crewai/tests/agents/test_agent_executor.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • lib/crewai/src/crewai/experimental/agent_executor.py
  • lib/crewai/src/crewai/flow/runtime/init.py

📝 Walkthrough

Walkthrough

The PR removes state proxy wrappers from the Flow runtime, changes Flow.state to return the underlying state directly, and updates AgentExecutor, public exports, and tests to match.

Changes

State proxy removal and direct state access

Layer / File(s) Summary
Runtime state access
lib/crewai/src/crewai/flow/runtime/__init__.py
The runtime module removes the proxy classes, changes Flow.state to return _state directly, and updates the private listener lock field.
Compatibility exports and AgentExecutor
lib/crewai/src/crewai/flow/flow.py, lib/crewai/src/crewai/experimental/agent_executor.py, lib/crewai/tests/agents/test_agent_executor.py
The compatibility module stops exporting the proxy types, AgentExecutor drops its StateProxy import and state override, and the executor test helper no longer initializes _state_lock.
Flow test updates
lib/crewai/tests/test_flow.py, lib/crewai/tests/test_flow_conversation.py, lib/crewai/tests/test_flow_from_definition.py, lib/crewai/tests/test_flow_persistence.py
The flow tests update parallel-branch assertions to execution order, adjust the conversational-state comment, and handle direct flow.state values in definition and persistence tests.

Suggested reviewers

  • greysonlalonde
  • lorenzejay
  • akaKuruma
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% 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
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: removing StateProxy from flow state access.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 nuke-state-proxy

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.

@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 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 f724c73. Configure here.

Comment thread lib/crewai/src/crewai/flow/runtime/__init__.py

@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.

Actionable comments posted: 1

🤖 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.

Inline comments:
In `@lib/crewai/tests/test_flow.py`:
- Around line 1513-1518: The test for parallel branch completion is too weak
because it only checks final state and can still pass if the joined listener
runs too early. Tighten the assertions in
test_parallel_branches_complete_before_join and the related verify_state checks
so they explicitly record and compare event order, then assert the joined
listener executes only after both branch updates are present. Use the existing
listener/branch state assertions in this test to add ordering/index checks
rather than relying on membership alone.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 534b1898-3542-41c1-9650-79e98d487e68

📥 Commits

Reviewing files that changed from the base of the PR and between 7738a1d and f724c73.

📒 Files selected for processing (6)
  • lib/crewai/src/crewai/experimental/agent_executor.py
  • lib/crewai/src/crewai/flow/flow.py
  • lib/crewai/src/crewai/flow/runtime/__init__.py
  • lib/crewai/tests/agents/test_agent_executor.py
  • lib/crewai/tests/test_flow.py
  • lib/crewai/tests/test_flow_conversation.py
💤 Files with no reviewable changes (1)
  • lib/crewai/src/crewai/flow/flow.py

Comment thread lib/crewai/tests/test_flow.py Outdated
`StateProxy` looked like a thread-safety boundary, but it only protected
a small slice of state operations. Some examples of operations that were
not covered:

- `self.state.counter += 1`, `self.state["counter"] += 1` (increments)
- `self.state.user.profile.score += 1` (nested object mutations)
- `self.state.config["limits"]["max"] = 10` (mutation through model fields)
- `self.state.items[0].status = "done"` (list/container mutations)

This commit decided to remove it completely for simplicity and
performance:

- Simpler runtime code
- attr read: 24x faster, attr write: 27x faster, list append: 19x faster (local benchmark)
- Clearer concurrency contract (lifecycle locks remain, but arbitrary
  shared state mutation is not presented as thread-safe)
@vinibrsl vinibrsl merged commit 340d23a into main Jun 24, 2026
57 checks passed
@vinibrsl vinibrsl deleted the nuke-state-proxy branch June 24, 2026 23:37
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