fix(chat): classify framework error guard leaks

这个提交存在于:
YokiiroBW 2026-08-01 02:36:25 +08:00
父节点 bbbe0c9048
当前提交 3679b95fe8
共有 3 个文件被更改,包括 164 次插入 和 43 次删除

32
main.py
查看文件

@ -3933,25 +3933,6 @@ class PrivateCompanionPlugin(
return
if not bool(getattr(self, "enable_framework_error_leak_guard", True)):
return
error_markers = (
"error occurred while processing agent request",
"all chat models failed",
"badrequesterror",
"provider api error",
"unable to submit request",
"invalid_request",
"functiondeclaration",
"function declaration",
"schema didn't specify",
"tool schema",
"aisearch",
"sqlite3.operationalerror",
"database is locked",
"sqlalche.me/e/20/e3q8",
"model do not support image input",
"image_url",
"invalidparameter",
)
tool_loop_markers = (
"trying to send messages",
"sent 20",
@ -3969,25 +3950,22 @@ class PrivateCompanionPlugin(
"发了差不多20条",
"还没收到回复",
)
marker_kind = "framework_error" if any(marker in compact for marker in error_markers) else ""
meta_leak_checker = getattr(self, "_framework_agent_meta_summary_leak", None)
if not marker_kind and callable(meta_leak_checker) and meta_leak_checker(text):
marker_kind = "tool_loop_summary"
error_kind_checker = getattr(self, "_framework_error_leak_kind", None)
marker_kind = error_kind_checker(text) if callable(error_kind_checker) else ""
if not marker_kind and any(marker in compact for marker in tool_loop_markers):
marker_kind = "tool_loop_summary"
marker_kind = "tool_loop"
if not marker_kind:
return
logger.warning(
"[PrivateCompanion] 已拦截框架异常文本外发: kind=%s session=%s preview=%s",
"[PrivateCompanion] 已拦截框架异常文本外发: kind=%s session=%s",
marker_kind,
_single_line(getattr(event, "unified_msg_origin", ""), 120) or "unknown",
_single_line(text, 180),
)
self._record_passive_no_reply(
event,
source="发送前拦截",
reason=f"框架异常文本外发被拦截:{marker_kind}",
reply_preview=text,
reply_preview="",
level="warn",
)
empty_result = self._build_result_from_chain([])

查看文件

@ -925,17 +925,10 @@ class ProactiveMessageMixin:
"unable to submit request",
"invalid_request",
"invalid request error",
"functiondeclaration",
"function declaration",
"schema didn't specify",
"tool schema",
"tool has no return value",
"status disabled",
"主动消息专用模式下",
"普通被动回复不可使用 private companion 工具",
"主动渲染阶段不可使用 private companion 工具",
"has sent the result directly to the user",
"traceback",
"error code: 400",
"error code 400",
"400 bad request",
@ -963,13 +956,7 @@ class ProactiveMessageMixin:
"providerapierror",
"unabletosubmitrequest",
"invalid_request",
"invalidrequest",
"invalidrequesterror",
"functiondeclaration",
"schemadidntspecify",
"toolschema",
"toolhasnoreturnvalue",
"statusdisabled",
"主动消息专用模式下",
"普通被动回复不可使用privatecompanion工具",
"主动渲染阶段不可使用privatecompanion工具",
@ -987,6 +974,21 @@ class ProactiveMessageMixin:
return True
if any(marker in compact for marker in compact_markers):
return True
provider_error_context = any(
token in compact
for token in (
"providerapierror",
"errorcode",
"statuscode",
"badrequest",
"invalidrequest",
"requestfailed",
"请求失败",
"调用失败",
"模型调用失败",
"工具调用失败",
)
)
if "errorcode" in compact and any(
token in compact
for token in (
@ -998,10 +1000,12 @@ class ProactiveMessageMixin:
)
):
return True
if "functiondeclaration" in compact and any(
if "functiondeclaration" in compact and provider_error_context and any(
token in compact for token in ("schema", "properties", "parameters", "tool", "tools", "badrequest", "invalidrequest")
):
return True
if any(token in compact for token in ("schemadidntspecify", "toolschema", "image_url", "invalidparameter")) and provider_error_context:
return True
if "aisearch" in cleaned and any(
marker in cleaned
for marker in (
@ -2829,8 +2833,6 @@ class ProactiveMessageMixin:
cleaned = _single_line(text, 500).lower()
if not cleaned:
return False
if self._looks_like_internal_provider_error_text(text):
return True
normalized = re.sub(r"[^a-z0-9\u4e00-\u9fff_]+", " ", cleaned).strip()
compact = re.sub(r"[^a-z0-9\u4e00-\u9fff_]+", "", cleaned)
if self._is_proactive_delivery_receipt_text(text):
@ -14686,6 +14688,49 @@ Output:
return self._onebot_forward_action_result_ok(result)
return False
@staticmethod
def _looks_like_python_traceback_text(text: Any) -> bool:
"""Only treat a complete Python stack trace as an outbound error leak.
Technical conversations commonly mention ``traceback`` or explain a
schema. Those words alone are not an error. A real Python traceback
has the header, at least one frame, and a terminating exception line.
"""
raw = str(text or "").replace("\r\n", "\n").replace("\r", "\n").strip()
if not raw:
return False
has_header = bool(re.search(r"(?im)^\s*traceback \(most recent call last\):\s*$", raw))
has_frame = bool(re.search(r"(?im)^\s*file \"[^\n\"]+\", line \d+(?:, in .+)?\s*$", raw))
has_exception = bool(
re.search(
r"(?im)^\s*(?:[a-z_][\w.]*(?:error|exception|warning)|assertionerror|keyboardinterrupt|systemexit)(?::|$)",
raw,
)
)
return has_header and has_frame and has_exception
def _framework_error_leak_kind(self, text: Any) -> str:
"""Return a safe classifier code for a real framework leak, if any."""
if self._looks_like_python_traceback_text(text):
return "python_traceback"
if self._looks_like_internal_provider_error_text(text):
return "provider_error"
cleaned = _single_line(text, 1000).lower()
if any(
marker in cleaned
for marker in (
"error occurred while processing agent request",
"sqlite3.operationalerror",
"database is locked",
"sqlalche.me/e/20/e3q8",
"model do not support image input",
)
):
return "framework_error"
if self._framework_agent_meta_summary_leak(str(text or "")):
return "tool_loop"
return ""
async def _send_segmented_forward_message(
self,
*,

查看文件

@ -0,0 +1,98 @@
from __future__ import annotations
import ast
import copy
from pathlib import Path
import re
from typing import Any
import unittest
ROOT = Path(__file__).resolve().parents[1]
def _single_line(value: Any, limit: int = 1000) -> str:
return re.sub(r"\s+", " ", str(value or "")).strip()[:limit]
def _guard_host_type():
"""Load only the pure guard helpers, without importing AstrBot."""
source = (ROOT / "proactive_message.py").read_text(encoding="utf-8")
tree = ast.parse(source)
mixin = next(node for node in tree.body if isinstance(node, ast.ClassDef) and node.name == "ProactiveMessageMixin")
names = {
"_looks_like_internal_provider_error_text",
"_looks_like_python_traceback_text",
"_framework_agent_meta_summary_leak",
"_framework_error_leak_kind",
}
namespace: dict[str, Any] = {"Any": Any, "re": re, "_single_line": _single_line}
for node in mixin.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 / "proactive_message.py"), "exec"), namespace)
class GuardHost:
def _is_proactive_delivery_receipt_text(self, _text: str) -> bool:
return False
def _is_proactive_instruction_leak_text(self, _text: str) -> bool:
return False
for name in names:
setattr(GuardHost, name, namespace[name])
return GuardHost
GuardHost = _guard_host_type()
class FrameworkErrorGuardTests(unittest.TestCase):
def setUp(self) -> None:
self.host = GuardHost()
def test_technical_guidance_with_traceback_schema_and_image_url_is_allowed(self) -> None:
text = "请把完整 traceback、调用栈、相关 tool schema 和页面报错截图贴出来,再检查 image_url 字段。"
self.assertFalse(self.host._looks_like_internal_provider_error_text(text))
self.assertEqual("", self.host._framework_error_leak_kind(text))
def test_complete_python_traceback_has_its_own_reason_code(self) -> None:
text = """Traceback (most recent call last):
File \"main.py\", line 8, in <module>
run()
ValueError: bad input"""
self.assertTrue(self.host._looks_like_python_traceback_text(text))
self.assertEqual("python_traceback", self.host._framework_error_leak_kind(text))
def test_provider_request_error_is_still_blocked(self) -> None:
text = "Provider API Error: Error code: 400 - Invalid request: tool schema is invalid"
self.assertTrue(self.host._looks_like_internal_provider_error_text(text))
self.assertEqual("provider_error", self.host._framework_error_leak_kind(text))
def test_tool_loop_summary_is_still_blocked(self) -> None:
text = "Agent reached max steps; forcing a final response after trying to send messages."
self.assertEqual("tool_loop", self.host._framework_error_leak_kind(text))
def test_send_guard_uses_reason_code_without_recording_the_body(self) -> None:
source = (ROOT / "main.py").read_text(encoding="utf-8")
tree = ast.parse(source)
hook = next(
node
for node in ast.walk(tree)
if isinstance(node, ast.AsyncFunctionDef) and node.name == "suppress_framework_error_leak_before_send"
)
hook_source = ast.get_source_segment(source, hook) or ""
self.assertIn('getattr(self, "_framework_error_leak_kind", None)', hook_source)
self.assertIn('marker_kind = "tool_loop"', hook_source)
self.assertIn('reply_preview=""', hook_source)
self.assertNotIn("tool_loop_summary", hook_source)
if __name__ == "__main__":
unittest.main()