Skip to content

fix: skip code regex without delimiters#6012

Open
he-yufeng wants to merge 2 commits 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 2 commits 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

adk-bot commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator

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!

@he-yufeng

Copy link
Copy Markdown
Contributor Author

Pushed ef85534e after rebasing onto the latest branch head.

This addresses the mypy-diff failure by returning None explicitly on the new no-delimiter fast path, and keeps the test formatted the way pyink wanted.

Validation:

  • python -m py_compile src\google\adk\code_executors\code_execution_utils.py tests\unittests\flows\llm_flows\test_code_execution.py
  • PYTHONPATH=src python -m pytest tests\unittests\flows\llm_flows\test_code_execution.py::test_extract_code_skips_regex_when_no_code_delimiter -q (1 passed)
  • python -m pre_commit run isort --files src\google\adk\code_executors\code_execution_utils.py tests\unittests\flows\llm_flows\test_code_execution.py
  • python -m pre_commit run pyink --files src\google\adk\code_executors\code_execution_utils.py tests\unittests\flows\llm_flows\test_code_execution.py
  • git diff --check

@rohityan rohityan requested a review from GWeale June 11, 2026 23:44
@rohityan rohityan added the needs review [Status] The PR/issue is awaiting review from the maintainer label Jun 11, 2026
@rohityan

Copy link
Copy Markdown
Collaborator

Hi @he-yufeng , Thank you for your contribution! We appreciate you taking the time to submit this pull request. Your PR has been received by the team and is currently under review. We will provide feedback as soon as we have an update to share.

@rohityan

Copy link
Copy Markdown
Collaborator

Hi @GWeale , can you please review this.

@he-yufeng he-yufeng force-pushed the fix/code-block-no-delimiter-fast-path branch 2 times, most recently from 34b1582 to 5a71e46 Compare June 12, 2026 04:45
@he-yufeng he-yufeng force-pushed the fix/code-block-no-delimiter-fast-path branch from 5a71e46 to 4e8602f Compare June 12, 2026 13:28
@he-yufeng

Copy link
Copy Markdown
Contributor Author

Rebased this on current main and re-ran the focused local checks:

  • PYTHONPATH=src python -m pytest tests\unittests\flows\llm_flows\test_code_execution.py -q --basetemp .tmp\pytest-6012 -p no:cacheprovider -> 4 passed
  • python -m py_compile on the touched files
  • python -m ruff check on the touched files
  • git diff --check upstream/main..HEAD

All passed locally.

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

Labels

needs review [Status] The PR/issue is awaiting review from the maintainer 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

3 participants