bug(stdlib): react() mutates the caller's tools list when model_options carries TOOLS
维护者通常 3 天内回复
还没有人认领这个 Issue。
评估
调研方向
该变异位于 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
toolslist silently gains entries it never declared. - Calling
react()repeatedly with the sametoolslist andmodel_optionsre-extends it each time (theReactInitiatortool dict dedupes by name, so the model prompt is unaffected, but the caller's list grows on every call). - A
toolslist 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
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
generative-computing/mellea 的其他 Issue
-
bug(cli): intrinsic_uploader builds path_in_repo with os.path.join, producing backslashes on Windows未关闭
难度 1/5 1 小时以内 新手友好度 84/100
generative-computing/mellea#1870 ·
维护者通常 3 天内回复
-
bug(backends): tool-call JSON nested under a non-first dict key is silently dropped by find_func未关闭
难度 2/5 1-3 小时 新手友好度 82/100
generative-computing/mellea#1867 ·
维护者通常 3 天内回复
-
bug: `mellea.plugins.builtin_debug.generation.log_generation_post_call` shows model as unknown可能已有人在做 @mvanhorn 今天认领。 未关闭bug
难度 2/5 1-3 小时 新手友好度 72/100
generative-computing/mellea#1864 · 2 条评论 ·
维护者通常 3 天内回复
-
bug: instruct() and react() replay tool-call turns without tool_calls可能已有人在做 @simonyang08 于 22 天前认领。 未关闭bug
难度 2/5 1-3 小时 新手友好度 85/100
generative-computing/mellea#1784 · 1 条评论 ·
维护者通常 3 天内回复
-
Silently mangled docstrings: lone backslash line-continuations in non-raw docstrings collapse command examples可能已有人在做 @madhusudhan1260 于 19 天前认领。 未关闭good first issue p2
难度 2/5 1-3 小时 新手友好度 82/100
generative-computing/mellea#1780 · 2 条评论 · 已指派 1 人 ·
维护者通常 3 天内回复
查看 generative-computing/mellea 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 72/100
ClickHouse/dbt-clickhouse#829 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
triage
难度 2/5 1-3 小时 新手友好度 75/100
维护者通常 3 天内回复
-
难度 2/5 1-3 小时 新手友好度 62/100
volcengine/OpenViking#8432 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复