Skip to content

Conversation

@ocetars
Copy link
Member

@ocetars ocetars commented Jan 25, 2026

Motivation / 动机

添加调试功能:在 DEBUG 日志级别下,打印发送给 LLM 的完整 messages 列表,便于开发者排查 LLM 请求相关问题。

Modifications / 改动点

  • 修改 astrbot/core/agent/runners/tool_loop_agent_runner.py

    • 新增 import logging 导入
    • 在调用 LLM 前添加 DEBUG 级别的日志输出,打印完整的 messages 列表(包括每条消息的索引、角色和内容)
  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

ba0aa2a369be802945d06f71fe3964f9 ---

Checklist / 检查清单

  • 😊 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
  • 👀 我的更改经过了良好的测试,并已在上方提供了"验证步骤"和"运行截图"
  • 🤓 我确保没有引入新依赖库。(logging 是 Python 标准库,无需额外安装)
  • 😮 我的更改没有引入恶意代码。

@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jan 25, 2026
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - 我发现了 1 个问题,并给出了一些高层次的反馈:

  • 建议增加保护或脱敏逻辑,这样在 DEBUG 级别记录完整 messages 时,不会在日志中意外泄露敏感或用户相关数据,尤其是在共享或生产环境中。
  • 目前的调试输出会记录每条消息的完整内容,可能会非常大;建议对过长内容进行截断,或限制日志中消息的数量,以避免产生过多日志量并带来潜在的性能影响。
给 AI 代理的提示词
Please address the comments from this code review:

## Overall Comments
- Consider adding a guard or masking logic so that DEBUG logging of full `messages` cannot inadvertently leak sensitive or user-specific data into logs, especially in shared or production environments.
- The current debug dump logs the full content of every message, which could be very large; consider truncating long contents or limiting the number of messages logged to avoid excessive log volume and potential performance impact.

## Individual Comments

### Comment 1
<location> `astrbot/core/agent/runners/tool_loop_agent_runner.py:119-126` </location>
<code_context>

+        # ========== DEBUG: dump final messages sent to LLM ==========
+        # 打印最终发给 LLM 的完整 messages 列表
+        if logger.isEnabledFor(logging.DEBUG):
+            logger.debug("===== [LLM Request Messages] =====")
+            for idx, msg in enumerate(messages):
+                role = msg.role if hasattr(msg, "role") else msg.get("role", "?")
+                content = (
+                    msg.content if hasattr(msg, "content") else msg.get("content", "")
+                )
+                logger.debug(f"  [{idx}] {role}: {content}")
+            logger.debug("===== [End LLM Request Messages] =====")
+        # =============================================================
</code_context>

<issue_to_address>
**🚨 issue (security):** Dumping full LLM messages to logs can leak sensitive user data and credentials.

Even at DEBUG level, logging full prompts and tool messages can expose user data, secrets, or identifiers, especially in centralized logging systems. Please either redact sensitive fields, restrict logs to metadata (e.g., role, length, tool name), or guard this behind an explicit, clearly unsafe flag (e.g., `unsafe_debug`/`log_prompts`) that is disabled in non-local environments.
</issue_to_address>

Sourcery 对开源项目免费——如果你觉得我们的评审有帮助,欢迎分享 ✨
帮我变得更有用!请对每条评论点 👍 或 👎,我会根据你的反馈改进后续的评审质量。
Original comment in English

Hey - I've found 1 issue, and left some high level feedback:

  • Consider adding a guard or masking logic so that DEBUG logging of full messages cannot inadvertently leak sensitive or user-specific data into logs, especially in shared or production environments.
  • The current debug dump logs the full content of every message, which could be very large; consider truncating long contents or limiting the number of messages logged to avoid excessive log volume and potential performance impact.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider adding a guard or masking logic so that DEBUG logging of full `messages` cannot inadvertently leak sensitive or user-specific data into logs, especially in shared or production environments.
- The current debug dump logs the full content of every message, which could be very large; consider truncating long contents or limiting the number of messages logged to avoid excessive log volume and potential performance impact.

## Individual Comments

### Comment 1
<location> `astrbot/core/agent/runners/tool_loop_agent_runner.py:119-126` </location>
<code_context>

+        # ========== DEBUG: dump final messages sent to LLM ==========
+        # 打印最终发给 LLM 的完整 messages 列表
+        if logger.isEnabledFor(logging.DEBUG):
+            logger.debug("===== [LLM Request Messages] =====")
+            for idx, msg in enumerate(messages):
+                role = msg.role if hasattr(msg, "role") else msg.get("role", "?")
+                content = (
+                    msg.content if hasattr(msg, "content") else msg.get("content", "")
+                )
+                logger.debug(f"  [{idx}] {role}: {content}")
+            logger.debug("===== [End LLM Request Messages] =====")
+        # =============================================================
</code_context>

<issue_to_address>
**🚨 issue (security):** Dumping full LLM messages to logs can leak sensitive user data and credentials.

Even at DEBUG level, logging full prompts and tool messages can expose user data, secrets, or identifiers, especially in centralized logging systems. Please either redact sensitive fields, restrict logs to metadata (e.g., role, length, tool name), or guard this behind an explicit, clearly unsafe flag (e.g., `unsafe_debug`/`log_prompts`) that is disabled in non-local environments.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +119 to +126
if logger.isEnabledFor(logging.DEBUG):
logger.debug("===== [LLM Request Messages] =====")
for idx, msg in enumerate(messages):
role = msg.role if hasattr(msg, "role") else msg.get("role", "?")
content = (
msg.content if hasattr(msg, "content") else msg.get("content", "")
)
logger.debug(f" [{idx}] {role}: {content}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 issue (security): 将完整的 LLM 消息写入日志可能会泄露敏感用户数据和凭据。

即使是在 DEBUG 级别,记录完整的提示词和工具消息也可能暴露用户数据、密钥或标识符,尤其是在集中式日志系统中。请对敏感字段进行脱敏处理,只记录元数据(例如角色、长度、工具名),或者将这类日志放在一个明确标记为不安全的开关(例如 unsafe_debug/log_prompts)后面,并确保在非本地环境中默认关闭。

Original comment in English

🚨 issue (security): Dumping full LLM messages to logs can leak sensitive user data and credentials.

Even at DEBUG level, logging full prompts and tool messages can expose user data, secrets, or identifiers, especially in centralized logging systems. Please either redact sensitive fields, restrict logs to metadata (e.g., role, length, tool name), or guard this behind an explicit, clearly unsafe flag (e.g., unsafe_debug/log_prompts) that is disabled in non-local environments.

@dosubot dosubot bot added the area:core The bug / feature is about astrbot's core, backend label Jan 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant