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

Đang mở Phù hợp với người mới
#1,869 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 3 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
Dưới một giờ
Mức phù hợp với người mới
82/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
ai

Hướng nghiên cứu

Đột biến nằm trong mellea/stdlib/frameworks/react.py, quanh các dòng 87-94, nơi tools.extend(mo_tools) ghi vào danh sách của bên gọi khi tools không rỗng. Chạy script tái hiện từ issue và xác nhận danh sách của bên gọi có thêm 'f2'. Sao chép danh sách trước khi gộp (tools = list(tools or [])), rồi chạy lại. Hoàn thành khi danh sách của bên gọi không thay đổi sau khi react() trả về, kể cả khi nó ném ngoại lệ.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.)

Ngôn ngữ chính
Python
Star
1.8k
Fork
161
Merge trung bình
4 ngày 4 giờ
Pull request đã merge (30 ngày)
47

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của generative-computing/mellea

Tất cả issue của generative-computing/mellea

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.