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

オープン 初心者向け
#1,869 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 3 日以内に返信

まだ誰も着手していません。

評価

難易度
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時間
マージ済み PR(30日)
47

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

generative-computing/mellea のほかの issue

generative-computing/mellea の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。