fix(agent): deepcopy agent.tools before llama.cpp grammar-retry sanitization - #34595
fix(agent): deepcopy agent.tools before llama.cpp grammar-retry sanitization#34595AhmetArif0 wants to merge 1 commit into
Conversation
…ization strip_pattern_and_format() mutates its input in place (documented in schema_sanitizer.py: "Callers that need to preserve the original should deep-copy first"). The llama.cpp grammar-retry path called it directly on agent.tools, permanently stripping pattern/format keywords from the shared tool-registry dicts — not just for this retry, but for every subsequent call in the session (auxiliary tasks routed to a cloud provider, model switches). PR NousResearch#34416 applied the same deepcopy fix to chat_completion_helpers.py and auxiliary_client.py but this call site was missed. Fix: deepcopy agent.tools into _tools_copy, strip from the copy, then assign back. The registry dicts are preserved; future tool rebuilds from the registry return full schemas. The session-level permanent strip for llama.cpp (so subsequent llama.cpp calls don't re-trigger the retry) is retained via the explicit agent.tools = _tools_copy assignment.
|
Thanks for isolating the remaining in-place sanitizer call. Current main still invokes Problems
Suggested changes
The code change itself matches the prior deepcopy pattern in |
|
Flagging for visibility: we're pulling #67349 into our fork, which touches this same |
Root Cause
strip_pattern_and_format()is documented to mutate its input in place:The llama.cpp grammar-retry path in
conversation_loop.pycalled it directlyon
agent.toolswithout a deep-copy, permanently strippingpattern/formatkeywords from the shared tool-registry dicts — not only for the immediate
retry, but for every subsequent API call in the same session.
Affected sessions: any that mix a llama.cpp primary model with cloud-provider
auxiliary tasks (Anthropic, OpenAI, OpenRouter, Gemini), or that switch models
mid-session after a grammar error. Those follow-on calls lose the schema hints
that cloud providers rely on for date/phone/email parameters.
Missed sibling of #34416
PR #34416 applied the identical deep-copy fix to
chat_completion_helpers.py(line 623) andauxiliary_client.py(line 708)after identifying that the xAI sanitizer paths suffered the same permanent
mutation. This call site in
conversation_loop.pywas not included.Fix
/reload-mcpor tool rebuild returnsfull schemas.
re-trigger the retry) is retained via the explicit
agent.tools = _tools_copyassignment — behaviour is unchanged for pure llama.cpp sessions.
strip_pattern_and_formatno longer leaves
agent.toolsin a partially-mutated state.Test plan
pattern/formatin tool schemas (HTTP 400 grammar error).in the same session — verify the tool schemas sent to the cloud provider
still contain
pattern/formatkeywords.test_run_agent_codex_responses.pyand schema-sanitizer unittests pass unchanged.