bug(stdlib): react() mutates the caller's tools list when model_options carries TOOLS

未关闭 适合新手
#1,869 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1 小时以内
新手友好度
82/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
ai

调研方向

该变异位于 mellea/stdlib/frameworks/react.py 的第 87-94 行附近,当 tools 非空时,tools.extend(mo_tools) 会写入调用方的列表。运行 issue 中的复现脚本,确认调用方的列表多出了 'f2'。在合并之前复制该列表(tools = list(tools or [])),然后重新运行。当 react() 返回后(包括它抛出异常的情况),调用方的列表保持不变,即视为完成。

由索引模型根据 Issue 内容生成。

描述

What happened?

react() merges model_options[ModelOption.TOOLS] into the tools argument with an in-place extend on the caller's own list:

# mellea/stdlib/frameworks/react.py:87-94
tools = tools or []
mo_tools: list[AbstractMelleaTool] | None = (
    model_options.get(ModelOption.TOOLS, None)
    if model_options is not None
    else None
)
if mo_tools is not None:
    tools.extend(mo_tools)   # mutates the caller's list when `tools` was truthy

tools or [] only creates a new list when tools is falsy; a non-empty list is used by reference, so react() appends the model-options tools into whatever list the caller passed. The list stays mutated after react() returns — including when react() raises before completing.

Consequences:

  • The caller's tools list silently gains entries it never declared.
  • Calling react() repeatedly with the same tools list and model_options re-extends it each time (the ReactInitiator tool dict dedupes by name, so the model prompt is unaffected, but the caller's list grows on every call).
  • A tools list shared across react calls or reused by other code is corrupted.
What did you expect to happen?

react() should not mutate caller-owned inputs — merge into a fresh list (e.g. tools = list(tools or [])).

How can we reproduce it?
import asyncio
from unittest.mock import Mock
from mellea import ModelOption
from mellea.stdlib.context.chat import ChatContext
from mellea.stdlib.frameworks.react import react
from mellea.backends.tools import MelleaTool

def f1(x: int) -> int: return x
def f2(x: int) -> int: return x

my_tools = [MelleaTool.from_callable(f1, "f1")]
mo = {ModelOption.TOOLS: [MelleaTool.from_callable(f2, "f2")]}

try:
    asyncio.run(
        react(goal="g", context=ChatContext(), backend=Mock(),
              tools=my_tools, model_options=mo, loop_budget=0)
    )
except RuntimeError:
    pass  # loop budget exhausted; mutation already happened

print([t.name for t in my_tools])
# ['f1', 'f2']  <-- expected ['f1']; react() appended to the caller's list
Mellea version
0.10.0.dev0
Any environmental details we need to know?

No backend or model needed to reproduce — the mutation runs before the first model call. Repro exits via RuntimeError (loop budget), but the same extend fires on any run where model_options[ModelOption.TOOLS] is set.

Additional context

The fix is a one-liner — copy before merging:

tools = list(tools or [])

(tools = tools or [] is only needed to satisfy the None case; the subsequent extend must not write into the caller's object.)

主要语言
Python
星标
1.8k
派生
161
平均合并
4 天 4 小时
30 天内合并 PR
47

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

generative-computing/mellea 的其他 Issue

查看 generative-computing/mellea 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。