-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix: unique session not working #4490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this 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>帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进后续的评审。
Original comment in English
Hey - I've found 3 issues, and left some high level feedback:
- In
waking_check/stage.py, consider using the newevent.session_idproperty rather thanevent.session.session_idto keep session handling encapsulated inAstrMessageEvent. - The
unified_msg_originsetter 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>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 |
There was a problem hiding this comment.
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.
fixes: #4331
Modifications / 改动点
Screenshots or Test Results / 运行截图或测试结果
Checklist / 检查清单
requirements.txt和pyproject.toml文件相应位置。/ I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations inrequirements.txtandpyproject.toml.Summary by Sourcery
通过从底层会话对象派生消息来源和会话 ID,修复会话身份处理问题。
Bug Fixes:
Enhancements:
unified_msg_origin和session_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:
Enhancements: