Skip to content

Conversation

@Soulter
Copy link
Member

@Soulter Soulter commented Jan 15, 2026

fixes: #4331

Modifications / 改动点

  • This is NOT a breaking change. / 这不是一个破坏性变更。

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


Checklist / 检查清单

  • 😊 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。/ If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
  • 👀 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。/ My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
  • 🤓 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到了 requirements.txtpyproject.toml 文件相应位置。/ I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
  • 😮 我的更改没有引入恶意代码。/ My changes do not introduce malicious code.

Summary by Sourcery

通过从底层会话对象派生消息来源和会话 ID,修复会话身份处理问题。

Bug Fixes:

  • 确保用于群组消息的唯一会话 ID 应用到底层会话对象上,从而使唯一会话按预期工作。

Enhancements:

  • unified_msg_originsession_id 暴露为属性,并与内部的 MessageSession 对象保持同步。
Original summary in English

Summary by Sourcery

Fix session identity handling by deriving message origin and session IDs from the underlying session object.

Bug Fixes:

  • Ensure unique session IDs for group messages are applied to the underlying session so unique sessions work as intended.

Enhancements:

  • Expose unified_msg_origin and session_id as properties that stay in sync with the internal MessageSession object.

@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. labels Jan 15, 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 - 我发现了 3 个问题,并给出了一些总体反馈:

  • waking_check/stage.py 中,建议使用新的 event.session_id 属性,而不是 event.session.session_id,以便将会话处理封装在 AstrMessageEvent 中。
  • unified_msg_origin 的 setter 会在每个 : 上进行分割;如果任意片段中包含冒号,就会解析错误,因此建议将分割限制为两次(split(":", 2)),或者以更健壮的方式校验/设计该格式。
给 AI 代理的提示
Please address the comments from this code review:

## Overall Comments
- In `waking_check/stage.py`, consider using the new `event.session_id` property rather than `event.session.session_id` to keep session handling encapsulated in `AstrMessageEvent`.
- The `unified_msg_origin` setter splits on every `:`; if any segment ever contains a colon this will misparse, so consider limiting the split to two (`split(":", 2)`) or otherwise validating/structuring the format more robustly.

## Individual Comments

### Comment 1
<location> `astrbot/core/platform/astr_message_event.py:74-76` </location>
<code_context>
         self.platform = platform_meta

+    @property
+    def unified_msg_origin(self) -> str:
+        """统一的消息来源字符串。格式为 platform_name:message_type:session_id"""
+        return str(self.session)
+
+    @unified_msg_origin.setter
</code_context>

<issue_to_address>
**suggestion (bug_risk):** 避免依赖 `__str__` 作为 `unified_msg_origin` 的序列化格式。

setter 强制要求使用 `platform_name:message_type:session_id` 这种格式,但 getter 使用的是 `str(self.session)`,这就把该协议与 `session.__str__` 耦合在一起。如果 `__str__` 发生变化(例如为了日志输出),序列化值也会随之改变,从而可能导致下游消费者出错。建议直接通过 `self.session.platform_name``self.session.message_type.value`(或类似字段)以及 `self.session.session_id` 来拼接字符串,以保持格式的稳定和明确。

Suggested implementation:

```python
    @property
    def unified_msg_origin(self) -> str:
        """统一的消息来源字符串。格式为 platform_name:message_type:session_id"""
        return f"{self.session.platform_name}:{self.session.message_type.value}:{self.session.session_id}"

    @unified_msg_origin.setter
    def unified_msg_origin(self, value: str):
        """设置统一的消息来源字符串。格式为 platform_name:message_type:session_id"""
        parts = value.split(":")
        if len(parts) != 3:
            raise ValueError(
                "unified_msg_origin 格式错误,应该为 platform_name:message_type:session_id",
            )
        platform_name, message_type_str, session_id = parts
        self.session.platform_name = platform_name
        self.session.message_type = MessageType(message_type_str)
        self.session.session_id = session_id

```

1. 确保 `self.session` 具有 `platform_name``message_type``session_id` 这些属性,并且 `message_type` 是一个枚举,其 `value` 为协议中使用的字符串。
2. 如果 `session_id` 在创建后应该保持不可变,请从 setter 中移除 `self.session.session_id = session_id`,并相应调整解析逻辑(例如改为只校验而不是重新赋值)。
</issue_to_address>

### Comment 2
<location> `astrbot/core/platform/astr_message_event.py:81-89` </location>
<code_context>
+    @unified_msg_origin.setter
+    def unified_msg_origin(self, value: str):
+        """设置统一的消息来源字符串。格式为 platform_name:message_type:session_id"""
+        parts = value.split(":")
+        if len(parts) != 3:
+            raise ValueError(
+                "unified_msg_origin 格式错误,应该为 platform_name:message_type:session_id",
+            )
+        platform_name, message_type_str, session_id = parts
+        self.session.platform_name = platform_name
+        self.session.message_type = MessageType(message_type_str)
+        self.session.session_id = session_id
+
+    @property
</code_context>

<issue_to_address>
**issue:** 如果任意组件包含冒号,`unified_msg_origin` 的解析会非常脆弱。

当 `session_id`(或其他组件)可能包含 `:` 时,`value.split(":")` 会因为产生超过三个部分而导致 `ValueError`。可以考虑使用 `split(":", 2)` 将分割限制在前两个冒号,或者在协议层面强制/编码出不包含冒号的 `session_id`。同时也建议在 API 边界处对这些输入约束进行校验和文档说明。
</issue_to_address>

### Comment 3
<location> `astrbot/core/pipeline/waking_check/stage.py:84` </location>
<code_context>
         if self.unique_session and event.message_obj.type == MessageType.GROUP_MESSAGE:
             sid = build_unique_session_id(event)
             if sid:
-                event.session_id = sid
+                event.session.session_id = sid

         # ignore bot self message
</code_context>

<issue_to_address>
**suggestion:** 建议优先使用新的 `session_id` 属性,而不是直接访问 `event.session`。

直接修改 `event.session.session_id` 会破坏 `AstrBotMessageEvent.session_id` 提供的抽象,并且可能会让未来关于会话表示方式的变更变得更加复杂。通过 `event.session_id` 进行写入能够更好地保持这一抽象边界。
</issue_to_address>

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

Hey - I've found 3 issues, and left some high level feedback:

  • In waking_check/stage.py, consider using the new event.session_id property rather than event.session.session_id to keep session handling encapsulated in AstrMessageEvent.
  • The unified_msg_origin setter splits on every :; if any segment ever contains a colon this will misparse, so consider limiting the split to two (split(":", 2)) or otherwise validating/structuring the format more robustly.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `waking_check/stage.py`, consider using the new `event.session_id` property rather than `event.session.session_id` to keep session handling encapsulated in `AstrMessageEvent`.
- The `unified_msg_origin` setter splits on every `:`; if any segment ever contains a colon this will misparse, so consider limiting the split to two (`split(":", 2)`) or otherwise validating/structuring the format more robustly.

## Individual Comments

### Comment 1
<location> `astrbot/core/platform/astr_message_event.py:74-76` </location>
<code_context>
         self.platform = platform_meta

+    @property
+    def unified_msg_origin(self) -> str:
+        """统一的消息来源字符串。格式为 platform_name:message_type:session_id"""
+        return str(self.session)
+
+    @unified_msg_origin.setter
</code_context>

<issue_to_address>
**suggestion (bug_risk):** Avoid relying on `__str__` for the serialization format of `unified_msg_origin`.

The setter enforces a `platform_name:message_type:session_id` format, but the getter uses `str(self.session)`, coupling this protocol to `session.__str__`. If `__str__` changes (e.g., for logging), the serialized value changes and may break consumers. Consider building the string directly from `self.session.platform_name`, `self.session.message_type.value` (or similar), and `self.session.session_id` to keep the format stable and explicit.

Suggested implementation:

```python
    @property
    def unified_msg_origin(self) -> str:
        """统一的消息来源字符串。格式为 platform_name:message_type:session_id"""
        return f"{self.session.platform_name}:{self.session.message_type.value}:{self.session.session_id}"

    @unified_msg_origin.setter
    def unified_msg_origin(self, value: str):
        """设置统一的消息来源字符串。格式为 platform_name:message_type:session_id"""
        parts = value.split(":")
        if len(parts) != 3:
            raise ValueError(
                "unified_msg_origin 格式错误,应该为 platform_name:message_type:session_id",
            )
        platform_name, message_type_str, session_id = parts
        self.session.platform_name = platform_name
        self.session.message_type = MessageType(message_type_str)
        self.session.session_id = session_id

```

1. Ensure that `self.session` has the attributes `platform_name`, `message_type`, and `session_id`, and that `message_type` is an enum whose `value` is the string used in the protocol.
2. If `session_id` is intended to be immutable after creation, remove `self.session.session_id = session_id` from the setter and adjust the parsing logic accordingly (e.g., validate it instead of setting it).
</issue_to_address>

### Comment 2
<location> `astrbot/core/platform/astr_message_event.py:81-89` </location>
<code_context>
+    @unified_msg_origin.setter
+    def unified_msg_origin(self, value: str):
+        """设置统一的消息来源字符串。格式为 platform_name:message_type:session_id"""
+        parts = value.split(":")
+        if len(parts) != 3:
+            raise ValueError(
+                "unified_msg_origin 格式错误,应该为 platform_name:message_type:session_id",
+            )
+        platform_name, message_type_str, session_id = parts
+        self.session.platform_name = platform_name
+        self.session.message_type = MessageType(message_type_str)
+        self.session.session_id = session_id
+
+    @property
</code_context>

<issue_to_address>
**issue:** The `unified_msg_origin` parsing is fragile if any component contains a colon.

`value.split(":")` will raise `ValueError` if `session_id` (or other components) can contain `:` because it produces more than three parts. Consider `split(":", 2)` to limit splitting to the first two colons, or enforce/encode colon-free `session_id`. Also consider validating and documenting these input constraints at the API boundary.
</issue_to_address>

### Comment 3
<location> `astrbot/core/pipeline/waking_check/stage.py:84` </location>
<code_context>
         if self.unique_session and event.message_obj.type == MessageType.GROUP_MESSAGE:
             sid = build_unique_session_id(event)
             if sid:
-                event.session_id = sid
+                event.session.session_id = sid

         # ignore bot self message
</code_context>

<issue_to_address>
**suggestion:** Prefer using the new `session_id` property instead of reaching into `event.session` directly.

Directly mutating `event.session.session_id` breaks the abstraction that `AstrBotMessageEvent.session_id` provides and can complicate future changes to how sessions are represented. Keeping writes going through `event.session_id` preserves that boundary.
</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.

if self.unique_session and event.message_obj.type == MessageType.GROUP_MESSAGE:
sid = build_unique_session_id(event)
if sid:
event.session_id = sid
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: 建议优先使用新的 session_id 属性,而不是直接访问 event.session

直接修改 event.session.session_id 会破坏 AstrBotMessageEvent.session_id 提供的抽象,并且可能会让未来关于会话表示方式的变更变得更加复杂。通过 event.session_id 进行写入能够更好地保持这一抽象边界。

Original comment in English

suggestion: Prefer using the new session_id property instead of reaching into event.session directly.

Directly mutating event.session.session_id breaks the abstraction that AstrBotMessageEvent.session_id provides and can complicate future changes to how sessions are represented. Keeping writes going through event.session_id preserves that boundary.

@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Jan 15, 2026
@Soulter Soulter merged commit dbdb4f5 into master Jan 15, 2026
4 of 5 checks passed
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 area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 在QQ中隔离会话没有生效。

2 participants