Skip to content

fix: narrow bare excepts around json.loads in python_executor - #95

Open
harshadkhetpal wants to merge 1 commit into
wanxingai:mainfrom
harshadkhetpal:fix/narrow-bare-excepts
Open

fix: narrow bare excepts around json.loads in python_executor#95
harshadkhetpal wants to merge 1 commit into
wanxingai:mainfrom
harshadkhetpal:fix/narrow-bare-excepts

Conversation

@harshadkhetpal

Copy link
Copy Markdown

Summary

Two bare except: clauses in LightAgent/builtin_tools/python_executor.py wrap json.loads fallbacks (ruff E722). A bare except also swallows KeyboardInterrupt / SystemExit and can mask unrelated bugs in the recursive code-cleaning path. Narrowed both to the exceptions json.loads actually raises:

except (json.JSONDecodeError, TypeError):

Behavior for malformed JSON input is unchanged — only the accidental swallowing of process-control exceptions is removed.

Testing

python -m py_compile passes; ruff check --select E722 on the file goes from 2 errors to clean.

🤖 Generated with Claude Code

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: Harshad Khetpal <harshadkhetpal@users.noreply.github.com>

@wxai-space wxai-space left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Changes requested

The intent to narrow the bare except blocks is correct, but the second change introduces a compatibility regression.

A deeply nested JSON string that currently falls back to a normal code string now raises RecursionError from json.loads(), which can abort a tool call. I reproduced this with a 2,000-level nested JSON value: main returns a string, while this PR raises RecursionError.

Please isolate the json.loads() calls from the subsequent recursive cleanup and catch parsing failures appropriate for untrusted model output, including RecursionError where the existing fail-soft behavior is intended. Exceptions from unrelated cleanup logic should remain visible.

Please add regression tests for malformed JSON, deeply nested JSON, nested code fields, and propagation of KeyboardInterrupt/SystemExit. The full local suite passes, but it does not cover _clean_code_string().

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