Skip to content

fix: skip code regex without delimiters#6012

Open
he-yufeng wants to merge 1 commit into
google:mainfrom
he-yufeng:fix/code-block-no-delimiter-fast-path
Open

fix: skip code regex without delimiters#6012
he-yufeng wants to merge 1 commit into
google:mainfrom
he-yufeng:fix/code-block-no-delimiter-fast-path

Conversation

@he-yufeng
Copy link
Copy Markdown
Contributor

Summary

  • skip the expensive code-block regex when the response has no opening code delimiter
  • keep the existing extraction path unchanged once a delimiter is present
  • add a regression test that verifies delimiter-free text does not compile or run the regex

Fixes #5992

To verify

  • $env:PYTHONPATH='src'; python -m pytest tests\unittests\flows\llm_flows\test_code_execution.py -q
  • $env:PYTHONPATH='src'; python -m ruff check src\google\adk\code_executors\code_execution_utils.py tests\unittests\flows\llm_flows\test_code_execution.py
  • python -m py_compile src\google\adk\code_executors\code_execution_utils.py tests\unittests\flows\llm_flows\test_code_execution.py
  • git diff --check

@adk-bot adk-bot added the tools [Component] This issue is related to tools label Jun 8, 2026
@adk-bot
Copy link
Copy Markdown
Collaborator

adk-bot commented Jun 8, 2026

I have completed a thorough, read-only analysis of Pull Request #6012 and its linked issue #5992.

I have created a premium, structured analysis report artifact:
👉 Google ADK PR #6012 Analysis Report


Summary of Analysis & Recommendations

  1. CLA Compliance Gate: Passed
    • The mandatory Google CLA status check (cla/google) is verified with status SUCCESS. It is fully safe to review, triage, and merge this change.
  2. Issue Resolved: Catastrophic Regular Expression Backtracking (Issue #5992) ⚡
    • On large delimiter-free payloads (e.g., 100KB+ JSON responses from agents/callbacks), the wildcard post-processing regex in CodeExecutionUtils.extract_code_and_truncate_content gets stuck on an $O(n^2)$ path exploring possibilities, freezing the agent execution thread.
  3. The Fix: Inline Fast Path Substring Search 🚀
    • Adding a quick substring precheck in code_execution_utils.py via any(d[0] in response_text for d in code_block_delimiters) completely bypasses the regex matcher when no delimiter is present. This is highly optimized, $O(n)$ time complexity, and preserves current logic 100% when a delimiter is found.
  4. Testing: Highly Rigorous 🧪
    • The new unit test in test_code_execution.py mocks re.compile to assert that the regular expression compiler is never invoked for delimiter-free payloads—safeguarding against future regressions.

Final Recommendation: Approve (No changes or nits required) 🌟

The PR is exceptionally localized, highly elegant, structurally safe, and fixes a severe reliability issue.

Please take a look at the full report here and let me know if you would like me to assist with taking over, merging, or further actions on this PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tools [Component] This issue is related to tools

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Code executor: catastrophic O(n^2) regex backtracking in extract_code_and_truncate_content hangs on large delimiter-free responses

2 participants