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

Aperta Adatta ai principianti
#1,869 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 3 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
Meno di un'ora
Idoneità per principianti
82/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
ai

Direzione di ricerca

La mutazione si trova in mellea/stdlib/frameworks/react.py, intorno alle righe 87-94, dove tools.extend(mo_tools) scrive nella lista del chiamante quando tools non è vuoto. Esegui lo script di riproduzione dalla issue e verifica che la lista del chiamante contenga 'f2'. Copia la lista prima dell'unione (tools = list(tools or [])), quindi eseguilo di nuovo. Il lavoro è completato quando la lista del chiamante resta invariata dopo che react() restituisce il controllo, anche quando solleva un'eccezione.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
Python
Stelle
1.8k
Fork
161
Merge medio
4g 4h
PR unite (30g)
47

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di generative-computing/mellea

Tutte le issue di generative-computing/mellea

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.