Private
Public Access
0
0

refactor(ai_client): migrate _llama_history call sites to provider_state.get_history('llama')

Phase 6 of code_path_audit_phase_3_provider_state_20260624. 16 sites across TWO llama functions migrated:
- _send_llama (8 sites): outer capture + 2 with history.lock blocks + 4 history.append/not/_history references + 2 kwargs (history_lock=history.lock, history=history)
- _send_llama_native (8 sites): outer capture + 2 with history.lock blocks + 4 history.append/not/messages.extend + 1 history.append(msg)

Both backend variants (OpenRouter + Ollama) share the same provider_state.get_history('llama') singleton.

Verified: 27 tests pass across test_provider_state_migration (14) + test_llama_provider (6) + test_llama_ollama_native (7).

Conventions: 1-space indentation, CRLF preserved, no comments added.
This commit is contained in:
2026-06-25 12:41:08 -04:00
parent 46d444206b
commit fd5661335f
+16 -14
View File
@@ -2901,19 +2901,20 @@ def _send_llama(md_content: str, user_message: str, base_dir: str,
return _send_llama_native(md_content, user_message, base_dir, file_items, discussion_history, stream, pre_tool_callback, qa_callback, stream_callback, patch_callback) return _send_llama_native(md_content, user_message, base_dir, file_items, discussion_history, stream, pre_tool_callback, qa_callback, stream_callback, patch_callback)
client = _ensure_llama_client() client = _ensure_llama_client()
tools: list[Metadata] | None = _get_deepseek_tools() or None tools: list[Metadata] | None = _get_deepseek_tools() or None
with _llama_history_lock: history = provider_state.get_history("llama")
with history.lock:
user_content = user_message user_content = user_message
if file_items: if file_items:
for fi in file_items: for fi in file_items:
if fi.get("is_image") and fi.get("base64_data"): if fi.get("is_image") and fi.get("base64_data"):
user_content = f"[IMAGE: {fi.get('path', 'attachment')}]\n{user_content}" user_content = f"[IMAGE: {fi.get('path', 'attachment')}]\n{user_content}"
if discussion_history and not _llama_history: if discussion_history and not history:
_llama_history.append({"role": "user", "content": f"[DISCUSSION HISTORY]\n\n{discussion_history}\n\n---\n\n{user_message}"}) history.append({"role": "user", "content": f"[DISCUSSION HISTORY]\n\n{discussion_history}\n\n---\n\n{user_message}"})
else: else:
_llama_history.append({"role": "user", "content": user_content}) history.append({"role": "user", "content": user_content})
def _build_llama_request(_round_idx: int) -> OpenAICompatibleRequest: def _build_llama_request(_round_idx: int) -> OpenAICompatibleRequest:
with _llama_history_lock: with history.lock:
history_msgs: list[ChatMessage] = [ChatMessage(role=m["role"], content=m["content"]) for m in _llama_history] history_msgs: list[ChatMessage] = [ChatMessage(role=m["role"], content=m["content"]) for m in history]
messages: list[ChatMessage] = [ChatMessage(role="system", content=f"{_get_combined_system_prompt()}\n\n<context>\n{md_content}\n</context>")] messages: list[ChatMessage] = [ChatMessage(role="system", content=f"{_get_combined_system_prompt()}\n\n<context>\n{md_content}\n</context>")]
messages.extend(history_msgs) messages.extend(history_msgs)
return OpenAICompatibleRequest( return OpenAICompatibleRequest(
@@ -2926,7 +2927,7 @@ def _send_llama(md_content: str, user_message: str, base_dir: str,
client, _build_llama_request, capabilities=caps, client, _build_llama_request, capabilities=caps,
pre_tool_callback=pre_tool_callback, qa_callback=qa_callback, stream_callback=stream_callback, pre_tool_callback=pre_tool_callback, qa_callback=qa_callback, stream_callback=stream_callback,
patch_callback=patch_callback, base_dir=base_dir, vendor_name="llama", patch_callback=patch_callback, base_dir=base_dir, vendor_name="llama",
history_lock=_llama_history_lock, history=_llama_history, history_lock=history.lock, history=history,
)) ))
except Exception as exc: except Exception as exc:
return Result(data="", errors=[_classify_openai_compatible_error(exc, source="ai_client.llama")]) return Result(data="", errors=[_classify_openai_compatible_error(exc, source="ai_client.llama")])
@@ -2995,13 +2996,14 @@ def _send_llama_native(md_content: str, user_message: str, base_dir: str,
""" """
try: try:
base_url = _llama_base_url.replace("/v1", "") base_url = _llama_base_url.replace("/v1", "")
with _llama_history_lock: history = provider_state.get_history("llama")
if discussion_history and not _llama_history: with history.lock:
_llama_history.append({"role": "user", "content": f"[DISCUSSION HISTORY]\n\n{discussion_history}\n\n---\n\n{user_message}"}) if discussion_history and not history:
history.append({"role": "user", "content": f"[DISCUSSION HISTORY]\n\n{discussion_history}\n\n---\n\n{user_message}"})
else: else:
_llama_history.append({"role": "user", "content": user_message}) history.append({"role": "user", "content": user_message})
messages: list[Metadata] = [{"role": "system", "content": f"{_get_combined_system_prompt()}\n\n<context>\n{md_content}\n</context>"}] messages: list[Metadata] = [{"role": "system", "content": f"{_get_combined_system_prompt()}\n\n<context>\n{md_content}\n</context>"}]
messages.extend(_llama_history) messages.extend(history)
images: list[str] = [] images: list[str] = []
if file_items: if file_items:
for fi in file_items: for fi in file_items:
@@ -3010,11 +3012,11 @@ def _send_llama_native(md_content: str, user_message: str, base_dir: str,
response = ollama_chat(_model, messages, images=images, base_url=base_url) response = ollama_chat(_model, messages, images=images, base_url=base_url)
text = response.get("message", {}).get("content", "") text = response.get("message", {}).get("content", "")
thinking = response.get("message", {}).get("thinking", "") thinking = response.get("message", {}).get("thinking", "")
with _llama_history_lock: with history.lock:
msg: Metadata = {"role": "assistant", "content": text or None} msg: Metadata = {"role": "assistant", "content": text or None}
if thinking: if thinking:
msg["thinking"] = thinking msg["thinking"] = thinking
_llama_history.append(msg) history.append(msg)
return Result(data=(f"<thinking>\n{thinking}\n</thinking>\n" if thinking else "") + text) return Result(data=(f"<thinking>\n{thinking}\n</thinking>\n" if thinking else "") + text)
except Exception as exc: except Exception as exc:
return Result(data="", errors=[ErrorInfo(kind=ErrorKind.INTERNAL, message=str(exc), source="ai_client.llama_native", original=exc)]) return Result(data="", errors=[ErrorInfo(kind=ErrorKind.INTERNAL, message=str(exc), source="ai_client.llama_native", original=exc)])