Skip to content

fix(core): don't mutate caller's tools list in CodeActAgent - #23136

Open
BlueX888 wants to merge 1 commit into
run-llama:mainfrom
BlueX888:fix/prep-codeact-mutates-caller-tools-list
Open

BlueX888 wants to merge 1 commit into
run-llama:mainfrom
BlueX888:fix/prep-codeact-mutates-caller-tools-list

Conversation

@BlueX888

Copy link
Copy Markdown

Description

CodeActAgent.__init__ modifies the tools list owned by the caller.

Summary

CodeActAgent appends its internal execute tool to the tools argument it is given. When that argument is a non-empty list, the list is aliased rather than copied, so the caller's list is modified in place.

Why (root cause)

llama-index-core/llama_index/core/agent/workflow/codeact_agent.py:98

tools = tools or []
tools.append(  # line 99
    FunctionTool.from_defaults(code_execute_fn, name=EXECUTE_TOOL_NAME)
)

tools or [] evaluates to the caller's list object whenever that list is non-empty — the [] fallback is only constructed when tools is None or empty. The append on the next line therefore writes into the caller's list.

Consequence: a tool list shared between agent instances gains one duplicate execute tool per construction:

tools = [my_tool]
CodeActAgent(code_execute_fn=fn, tools=tools)
CodeActAgent(code_execute_fn=fn, tools=tools)
# tools is now [my_tool, execute, execute]  -- caller's list was never touched by the caller

Changes

  • codeact_agent.py:98 — copy the incoming sequence into a new list before appending: tools = [*(tools or [])]. This uses the same copy idiom already present in this module (base_agent.py:277, multi_agent_workflow.py:253).
  • tests/agent/workflow/test_code_act_agent.py — add test_code_act_agent_does_not_mutate_caller_tools, asserting the caller's list still holds only the tool it was given and that the agent itself receives both the caller tool and execute.

Testing

Red — test added, codeact_agent.py reverted to origin/main:

$ .venv/bin/python -m pytest tests/agent/workflow/test_code_act_agent.py -k mutate
>       assert [tool.metadata.name for tool in tools] == ["add"]
E       AssertionError: assert ['add', 'execute'] == ['add']
E
E         Left contains one more item: 'execute'
tests/agent/workflow/test_code_act_agent.py:233: AssertionError
1 failed

Green — fix applied:

$ .venv/bin/python -m pytest tests/agent/workflow/test_code_act_agent.py
4 passed, 83 warnings in 0.09s

$ .venv/bin/python -m pytest tests/agent/
91 passed, 3 skipped

$ .venv/bin/python -m mypy llama_index      # as run by .github/workflows/core-typecheck.yml
Success: no issues found in 479 source files

$ python -m pre_commit run --files llama-index-core/llama_index/core/agent/workflow/codeact_agent.py llama-index-core/tests/agent/workflow/test_code_act_agent.py
ruff, ruff-format, mypy, codespell, prettier, trailing-whitespace, end-of-file-fixer — Passed

AI usage disclosure

Per CONTRIBUTING.md ("How to Use AI when Contributing" — Transparency), AI assistance was used to draft the one-line change and the regression test. Verification: I reproduced the failure against unmodified origin/main, confirmed the assertion passes with the change, and ran the full tests/agent/ suite, mypy llama_index, and the pre-commit hooks on the changed files locally. Both files are small and fully reviewed.

New Package?

  • No

Version Bump?

  • No (change is in llama-index-core)

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • I added new unit tests to cover this change

Suggested Checklist

  • I have performed a self-review of my own code
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I ran the linters (pre-commit: ruff, ruff-format, mypy, codespell) on the changed files

No linked issue — this is a self-contained bug fix found by reading the CodeActAgent constructor; I could not find a matching open issue or PR.

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.

1 participant