Skip to content

fix(reasoning): use flexible regex to detect READY state to prevent agent deadlocks - #6625

Open
Kikingodoy wants to merge 2 commits into
crewAIInc:mainfrom
Kikingodoy:fix/reasoning-loop-regex-deadlock
Open

fix(reasoning): use flexible regex to detect READY state to prevent agent deadlocks#6625
Kikingodoy wants to merge 2 commits into
crewAIInc:mainfrom
Kikingodoy:fix/reasoning-loop-regex-deadlock

Conversation

@Kikingodoy

Copy link
Copy Markdown

This PR fixes a bug in the AgentReasoning loop where the readiness check was hardcoded to expect the exact string "READY: I am ready to execute the task.".

Newer local/open-source models, or models under slightly altered prompts, frequently output just "READY" or some other variation, causing the planning executor to fail to detect readiness and trap the agent in an infinite planning loop.

This replaces the hardcoded exact string matches with a flexible regular expression (?i)(?<!not )\bready\b that safely identifies when the model is ready, gracefully ignoring "not ready".

@coderabbitai

coderabbitai Bot commented Jul 23, 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: 9ae59bbe-a0a6-4f74-92e3-51c42c70e7d1

📥 Commits

Reviewing files that changed from the base of the PR and between 8373661 and d2c0b59.

📒 Files selected for processing (2)
  • lib/crewai/src/crewai/utilities/reasoning_handler.py
  • lib/crewai/tests/utilities/test_reasoning_handler.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • lib/crewai/src/crewai/utilities/reasoning_handler.py

📝 Walkthrough

Walkthrough

Readiness detection in reasoning_handler.py now uses a case-insensitive regex that recognizes standalone “ready” while excluding “not ready” across function-calling and text-parsing paths, with parametrized tests covering the new behavior.

Changes

Readiness Detection

Layer / File(s) Summary
Regex-based readiness checks
lib/crewai/src/crewai/utilities/reasoning_handler.py
Adds a shared regex-based readiness helper and applies it to function-calling fallbacks and planning-response parsing.
Readiness detection validation
lib/crewai/tests/utilities/test_reasoning_handler.py
Adds parametrized tests for ready, not-ready, case-insensitive, prefixed, and invalid response patterns.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.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
Title check ✅ Passed The title accurately describes the main change: making READY detection more flexible to avoid deadlocks.
Description check ✅ Passed The description matches the code changes and explains the regex-based readiness fix and why it was needed.
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.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@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/src/crewai/utilities/reasoning_handler.py`:
- Line 413: Update the readiness detection in the relevant paths of the
reasoning handler to reject any case-insensitive `not` followed by one or more
whitespace characters before `ready`, including tabs, repeated spaces, and
newlines. Centralize this logic in a shared helper and replace the checks at the
`response_str` readiness sites, then add tests covering all three paths and
whitespace variants.
🪄 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: 1a144e3e-806a-4d37-9d3c-2cf9dd69ce82

📥 Commits

Reviewing files that changed from the base of the PR and between b14d36b and 8373661.

📒 Files selected for processing (1)
  • lib/crewai/src/crewai/utilities/reasoning_handler.py

Comment thread lib/crewai/src/crewai/utilities/reasoning_handler.py Outdated
@Kikingodoy

Copy link
Copy Markdown
Author

I've pushed an update addressing the feedback!

I've extracted the readiness check into a centralized _is_ready helper method. It uses the \s+ regex token to robustly handle all whitespace variations of "not ready" (including tabs, newlines, and double spaces), ensuring we don't prematurely evaluate the readiness state.

I've also added a comprehensive test suite in tests/utilities/test_reasoning_handler.py to explicitly guarantee all these edge cases (and various casing formats) are covered moving forward!

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

Thanks for broadening support beyond the legacy exact sentence. I reproduced a remaining semantic false-positive in the centralized helper: common unready/future-readiness prose now ends planning early. I included a protocol-based parsing direction and focused validation inline.

"""Check if the response indicates the agent is ready."""
if re.search(r"(?i)\bnot\s+ready\b", response):
return False
return bool(re.search(r"(?i)\bready\b", response))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could we parse an explicit readiness marker instead of treating any remaining ready token as permission to execute? On this exact head, both AgentReasoning._parse_planning_response("Plan draft.\nNOT YET READY: missing token")[1] and the same call with "I need more context before I am ready." return True; never ready, hardly ready, will be ready, and NOT-READY do too. That prematurely exits the refinement loop even though the model is explicitly unready. The prompt already defines a terminal READY / NOT READY protocol, so a safer approach is to match line-anchored markers (optionally Status: / I am) and use the last marker, rather than infer intent from unrestricted prose. I validated that approach against the 16 existing cases plus six semantic-negation cases: 22 focused tests passed, along with Ruff and mypy.

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.

2 participants