diff --git a/main.py b/main.py index 7215855..6f9b333 100644 --- a/main.py +++ b/main.py @@ -153,6 +153,7 @@ from .busy_reply_gate import BusyReplyGateMixin from .memory_companion_adapter import MemoryCompanionAdapterMixin from .p5_attestation import P5AttestationError, P5AttestationRegistry, REASON_CODES as P5_ATTESTATION_REASON_CODES from .p5_source_observer import evaluate_source +from .tool_history_sanitizer import sanitize_openai_tool_history from .forward_message import ForwardMessageMixin from .private_image import PrivateImageMixin from .prompt_surface import PromptSurface @@ -9188,6 +9189,15 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge ) ) + def _llm_request_uses_deepseek_openai_compatible_provider( + self, + event: AstrMessageEvent | None, + req: ProviderRequest | None, + ) -> bool: + """Limit history cleanup to DeepSeek's OpenAI-compatible endpoint.""" + identity = " ".join(self._llm_request_provider_identity_parts(event, req)).lower() + return "deepseek" in identity + @staticmethod def _tool_set_has_named_tool(tool_set: Any, tool_name: str) -> bool: get_tool = getattr(tool_set, "get_tool", None) @@ -9414,6 +9424,29 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge if removed: await self._append_sensitive_screen_tool_guard_to_request(event, req, removed) + @filter.on_llm_request(priority=-20500) + async def sanitize_deepseek_tool_call_history(self, event: AstrMessageEvent, req: ProviderRequest, *args, **kwargs): + """Drop only malformed tool-call groups before a DeepSeek provider request.""" + if self is None or req is None: + return + if not self._llm_request_uses_deepseek_openai_compatible_provider(event, req): + return + contexts = getattr(req, "contexts", None) + cleaned, stats = sanitize_openai_tool_history(contexts) + if not stats.get("changed"): + return + try: + req.contexts = cleaned + except Exception: + return + logger.info( + "[PrivateCompanion] Cleaned malformed DeepSeek tool history: groups=%s assistants=%s tool_results=%s orphans=%s", + stats.get("removed_groups", 0), + stats.get("removed_assistants", 0), + stats.get("removed_tool_results", 0), + stats.get("removed_orphans", 0), + ) + @filter.on_llm_request(priority=-20000) async def sanitize_incompatible_web_search_tools(self, event: AstrMessageEvent, req: ProviderRequest, *args, **kwargs): """移除 Gemini/OpenAI 兼容层会拒绝的 Baidu AI Search MCP 工具声明。""" diff --git a/tests/test_req021_q2_deepseek_tool_history.py b/tests/test_req021_q2_deepseek_tool_history.py new file mode 100644 index 0000000..7e48d3d --- /dev/null +++ b/tests/test_req021_q2_deepseek_tool_history.py @@ -0,0 +1,162 @@ +from __future__ import annotations + +import ast +import copy +from pathlib import Path +from types import SimpleNamespace +from typing import Any +import unittest + +from tool_history_sanitizer import sanitize_openai_tool_history + + +ROOT = Path(__file__).resolve().parents[1] + + +class OpenAIToolHistorySanitizerTests(unittest.TestCase): + def test_valid_single_and_multi_tool_groups_keep_original_objects(self) -> None: + assistant_single = {"role": "assistant", "tool_calls": [{"id": "call-a"}]} + result_single = {"role": "tool", "tool_call_id": "call-a", "content": "ok"} + assistant_multi = {"role": "assistant", "tool_calls": [{"id": "call-b"}, {"id": "call-c"}]} + result_b = {"role": "tool", "tool_call_id": "call-b", "content": "b"} + result_c = {"role": "tool", "tool_call_id": "call-c", "content": "c"} + contexts = [{"role": "user", "content": "hello"}, assistant_single, result_single, assistant_multi, result_b, result_c] + + cleaned, stats = sanitize_openai_tool_history(contexts) + + self.assertIs(contexts, cleaned) + self.assertEqual(0, stats["changed"]) + self.assertIs(assistant_single, cleaned[1]) + self.assertIs(result_c, cleaned[-1]) + + def test_missing_result_removes_the_entire_assistant_group(self) -> None: + tail = {"role": "user", "content": "next"} + cleaned, stats = sanitize_openai_tool_history( + [{"role": "assistant", "tool_calls": [{"id": "call-a"}]}, tail] + ) + + self.assertEqual([tail], cleaned) + self.assertEqual(1, stats["removed_groups"]) + self.assertEqual(1, stats["removed_assistants"]) + + def test_orphan_and_duplicate_results_are_removed(self) -> None: + orphan = {"role": "tool", "tool_call_id": "orphan", "content": "x"} + assistant = {"role": "assistant", "tool_calls": [{"id": "call-a"}]} + first = {"role": "tool", "tool_call_id": "call-a", "content": "one"} + duplicate = {"role": "tool", "tool_call_id": "call-a", "content": "two"} + + cleaned, stats = sanitize_openai_tool_history([orphan, assistant, first, duplicate]) + + self.assertEqual([], cleaned) + self.assertEqual(1, stats["removed_orphans"]) + self.assertEqual(1, stats["removed_groups"]) + self.assertEqual(3, stats["removed_tool_results"]) + + def test_duplicate_call_id_mismatched_id_and_malformed_call_remove_groups(self) -> None: + malformed = {"role": "assistant", "tool_calls": "call-a"} + duplicate_call = {"role": "assistant", "tool_calls": [{"id": "call-b"}, {"id": "call-b"}]} + duplicate_result = {"role": "tool", "tool_call_id": "call-b"} + mismatch_call = {"role": "assistant", "tool_calls": [{"id": "call-c"}]} + mismatch_result = {"role": "tool", "tool_call_id": "call-other"} + normal = {"role": "assistant", "content": "ordinary"} + + cleaned, stats = sanitize_openai_tool_history( + [malformed, duplicate_call, duplicate_result, mismatch_call, mismatch_result, normal] + ) + + self.assertEqual([normal], cleaned) + self.assertEqual(3, stats["removed_groups"]) + self.assertEqual(3, stats["removed_assistants"]) + self.assertEqual(2, stats["removed_tool_results"]) + + def test_empty_or_absent_tool_calls_and_non_list_history_are_unchanged(self) -> None: + empty = {"role": "assistant", "tool_calls": []} + ordinary = {"role": "assistant", "content": "plain"} + contexts = [empty, ordinary] + + cleaned, stats = sanitize_openai_tool_history(contexts) + + self.assertIs(contexts, cleaned) + self.assertEqual(0, stats["changed"]) + sentinel = object() + self.assertIs(sentinel, sanitize_openai_tool_history(sentinel)[0]) + + +def _load_main_methods(names: set[str]) -> dict[str, Any]: + source = (ROOT / "main.py").read_text(encoding="utf-8") + tree = ast.parse(source) + owner = next(node for node in tree.body if isinstance(node, ast.ClassDef) and node.name == "PrivateCompanionPlugin") + namespace: dict[str, Any] = { + "Any": Any, + "AstrMessageEvent": Any, + "ProviderRequest": Any, + "filter": SimpleNamespace(on_llm_request=lambda **_kwargs: lambda target: target), + "sanitize_openai_tool_history": sanitize_openai_tool_history, + "logger": SimpleNamespace(info=lambda *_args, **_kwargs: None), + } + for node in owner.body: + if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and node.name in names: + module = ast.Module(body=[copy.deepcopy(node)], type_ignores=[]) + ast.fix_missing_locations(module) + exec(compile(module, str(ROOT / "main.py"), "exec"), namespace) + return {name: namespace[name] for name in names} + + +MAIN_METHODS = _load_main_methods( + {"_llm_request_uses_deepseek_openai_compatible_provider", "sanitize_deepseek_tool_call_history"} +) + + +class _HookHost: + def __init__(self, is_deepseek: bool) -> None: + self.is_deepseek = is_deepseek + + def _llm_request_uses_deepseek_openai_compatible_provider(self, _event: object, _req: object) -> bool: + return self.is_deepseek + + +_HookHost.sanitize_deepseek_tool_call_history = MAIN_METHODS["sanitize_deepseek_tool_call_history"] + + +class DeepSeekHookTests(unittest.IsolatedAsyncioTestCase): + async def test_non_deepseek_request_keeps_contexts_and_p3_p5_attributes_identical(self) -> None: + contexts = [{"role": "tool", "tool_call_id": "orphan"}] + request = SimpleNamespace( + contexts=contexts, + private_companion_p3_state=object(), + private_companion_p5_attestation=object(), + ) + before_p3 = request.private_companion_p3_state + before_p5 = request.private_companion_p5_attestation + + await _HookHost(False).sanitize_deepseek_tool_call_history(SimpleNamespace(), request) + + self.assertIs(contexts, request.contexts) + self.assertIs(before_p3, request.private_companion_p3_state) + self.assertIs(before_p5, request.private_companion_p5_attestation) + + async def test_deepseek_request_changes_only_damaged_context_history(self) -> None: + request = SimpleNamespace( + contexts=[{"role": "assistant", "tool_calls": [{"id": "call-a"}]}], + private_companion_p3_state=object(), + private_companion_p5_attestation=object(), + ) + before_p3 = request.private_companion_p3_state + before_p5 = request.private_companion_p5_attestation + + await _HookHost(True).sanitize_deepseek_tool_call_history(SimpleNamespace(), request) + + self.assertEqual([], request.contexts) + self.assertIs(before_p3, request.private_companion_p3_state) + self.assertIs(before_p5, request.private_companion_p5_attestation) + + def test_provider_matcher_is_deepseek_specific(self) -> None: + host = SimpleNamespace(_llm_request_provider_identity_parts=lambda _event, _req: ["openai", "deepseek-chat"]) + matcher = MAIN_METHODS["_llm_request_uses_deepseek_openai_compatible_provider"] + self.assertTrue(matcher(host, None, None)) + host._llm_request_provider_identity_parts = lambda _event, _req: ["openai", "gpt-5"] + self.assertFalse(matcher(host, None, None)) + + +if __name__ == "__main__": + unittest.main() diff --git a/tool_history_sanitizer.py b/tool_history_sanitizer.py new file mode 100644 index 0000000..dade971 --- /dev/null +++ b/tool_history_sanitizer.py @@ -0,0 +1,105 @@ +from __future__ import annotations + +from typing import Any + + +def _role(item: Any) -> str: + if not isinstance(item, dict): + return "" + return str(item.get("role") or "").strip().lower() + + +def _is_tool_result(item: Any) -> bool: + return _role(item) == "tool" + + +def _tool_call_ids(tool_calls: Any) -> list[str] | None: + """Return strict OpenAI-style tool-call IDs, or None for malformed calls.""" + if not isinstance(tool_calls, list) or not tool_calls: + return None + identifiers: list[str] = [] + for call in tool_calls: + if not isinstance(call, dict): + return None + call_id = str(call.get("id") or "").strip() + if not call_id or call_id in identifiers: + return None + identifiers.append(call_id) + return identifiers + + +def _tool_result_ids(items: list[Any]) -> list[str] | None: + identifiers: list[str] = [] + for item in items: + if not isinstance(item, dict): + return None + call_id = str(item.get("tool_call_id") or "").strip() + if not call_id: + return None + identifiers.append(call_id) + return identifiers + + +def sanitize_openai_tool_history(contexts: Any) -> tuple[Any, dict[str, int]]: + """Remove only malformed OpenAI tool-call groups from a generic history list. + + The function does not inspect content or mutate retained entries. A no-op + returns the original list object so ordinary history remains untouched. + """ + stats = { + "changed": 0, + "removed_groups": 0, + "removed_assistants": 0, + "removed_tool_results": 0, + "removed_orphans": 0, + } + if not isinstance(contexts, list) or not contexts: + return contexts, stats + + kept: list[Any] = [] + index = 0 + while index < len(contexts): + item = contexts[index] + role = _role(item) + if role == "tool": + stats["changed"] = 1 + stats["removed_tool_results"] += 1 + stats["removed_orphans"] += 1 + index += 1 + continue + + raw_calls = item.get("tool_calls") if isinstance(item, dict) and role == "assistant" else None + has_effective_calls = raw_calls not in (None, []) + if not has_effective_calls: + kept.append(item) + index += 1 + continue + + next_index = index + 1 + result_items: list[Any] = [] + while next_index < len(contexts) and _is_tool_result(contexts[next_index]): + result_items.append(contexts[next_index]) + next_index += 1 + + call_ids = _tool_call_ids(raw_calls) + result_ids = _tool_result_ids(result_items) + valid = bool( + call_ids + and result_ids is not None + and len(result_ids) == len(call_ids) + and len(set(result_ids)) == len(result_ids) + and set(result_ids) == set(call_ids) + ) + if valid: + kept.append(item) + kept.extend(result_items) + else: + stats["changed"] = 1 + stats["removed_groups"] += 1 + stats["removed_assistants"] += 1 + stats["removed_tool_results"] += len(result_items) + index = next_index + + if not stats["changed"]: + return contexts, stats + return kept, stats