fix: 完善插件卸载时的清理逻辑,新增KV数据清理,更新了多语言文案以说明会清理数据库KV数据#8291
Conversation
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- Since
_cleanup_plugin_optional_artifactsis now async, consider renaming it (e.g.,_cleanup_plugin_optional_artifacts_async) or adding a short docstring to make it clear it must be awaited, which helps prevent future callers from accidentally using it synchronously. - In the KV cleanup warning log (
清除插件 KV 数据失败), consider including theplugin_idas well asplugin_labelto make troubleshooting failed cleanup cases easier.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Since `_cleanup_plugin_optional_artifacts` is now async, consider renaming it (e.g., `_cleanup_plugin_optional_artifacts_async`) or adding a short docstring to make it clear it must be awaited, which helps prevent future callers from accidentally using it synchronously.
- In the KV cleanup warning log (`清除插件 KV 数据失败`), consider including the `plugin_id` as well as `plugin_label` to make troubleshooting failed cleanup cases easier.
## Individual Comments
### Comment 1
<location path="tests/test_plugin_manager.py" line_range="1344-1356" />
<code_context>
+ lambda p: None,
+ )
+
+ async def mock_cleanup(*, root_dir_name, plugin_label, plugin_id, delete_config, delete_data):
+ cleanup_calls.append(
+ {"root_dir_name": root_dir_name, "plugin_label": plugin_label, "plugin_id": plugin_id}
+ )
+
+ monkeypatch.setattr(plugin_manager_pm, "_cleanup_plugin_optional_artifacts", mock_cleanup)
+
+ await plugin_manager_pm.uninstall_plugin(
+ TEST_PLUGIN_NAME, delete_config=False, delete_data=True
+ )
+
+ assert len(cleanup_calls) == 1
+ assert cleanup_calls[0]["plugin_id"] == "mock_author/mock_name"
+
+
</code_context>
<issue_to_address>
**suggestion (testing):** Assert that `delete_config`/`delete_data` flags are correctly forwarded to `_cleanup_plugin_optional_artifacts`.
Since `mock_cleanup` already receives `delete_config` and `delete_data`, please also assert that these flags are forwarded with the expected values (e.g. `False` and `True`) to better validate the wiring between `uninstall_plugin` and `_cleanup_plugin_optional_artifacts`.
```suggestion
async def mock_cleanup(*, root_dir_name, plugin_label, plugin_id, delete_config, delete_data):
cleanup_calls.append(
{
"root_dir_name": root_dir_name,
"plugin_label": plugin_label,
"plugin_id": plugin_id,
"delete_config": delete_config,
"delete_data": delete_data,
}
)
monkeypatch.setattr(plugin_manager_pm, "_cleanup_plugin_optional_artifacts", mock_cleanup)
await plugin_manager_pm.uninstall_plugin(
TEST_PLUGIN_NAME, delete_config=False, delete_data=True
)
assert len(cleanup_calls) == 1
assert cleanup_calls[0]["plugin_id"] == "mock_author/mock_name"
assert cleanup_calls[0]["delete_config"] is False
assert cleanup_calls[0]["delete_data"] is True
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Code Review
This pull request implements the deletion of plugin-specific KV preference data from the database during the uninstallation process. Key changes include updating _cleanup_plugin_optional_artifacts to be asynchronous, integrating database cleanup logic, and updating the dashboard's localization files to inform users of this new behavior. Comprehensive unit tests were also added to verify the cleanup logic. Feedback from the reviewer suggests improving the robustness of plugin ID retrieval by using plugin.star_cls_type as a fallback when a plugin is disabled and refactoring this extraction logic into a shared helper function to reduce duplication across different uninstallation scenarios.
将插件ID生成逻辑抽离到StarMetadata类中,移除重复的代码实现, 同时在__post_init__中自动补全plugin_id字段。
已修改 |
a4c4a7d to
9bd38ca
Compare
Dt8333
left a comment
There was a problem hiding this comment.
[09:07:07.325] [Core] [INFO] [star.star_manager:927]: Loading plugin builtin_commands ...
[09:07:07.361] [Core] [INFO] [star.star_manager:1015]: Plugin builtin_commands (0.0.1) by Soulter: AstrBot's internal plugin, providing all built-in commands such as /reset.
[09:07:07.361] [Core] [ERRO] [v4.25.1] [star.star_manager:1214]: ----- 插件 builtin_commands 载入失败 -----
[09:07:07.361] [Core] [ERRO] [v4.25.1] [star.star_manager:1217]: | Traceback (most recent call last):
[09:07:07.361] [Core] [ERRO] [v4.25.1] [star.star_manager:1217]: | File "AstrBot\astrbot\core\star\star_manager.py", line 1021, in load
[09:07:07.361] [Core] [ERRO] [v4.25.1] [star.star_manager:1217]: | p_author, p_name = plugin_id.split("/")
[09:07:07.363] [Core] [ERRO] [v4.25.1] [star.star_manager:1217]: | ^^^^^^^^^^^^^^^
[09:07:07.363] [Core] [ERRO] [v4.25.1] [star.star_manager:1217]: | AttributeError: 'NoneType' object has no attribute 'split'
[09:07:07.363] [Core] [ERRO] [v4.25.1] [star.star_manager:1217]: |
[09:07:07.363] [Core] [ERRO] [v4.25.1] [star.star_manager:1218]: ----------------------------------
[09:07:07.363] [Core] [INFO] [star.star_manager:1231]: 失败插件依旧在插件列表中,正在清理...
看起来在实例化Star类前就有对plugin_id的使用。这时候postInit还未被调用。
个人觉得这个工具函数放到StarManager下改动会比较小。
如果有其他方案也可直接进行修复。
确实存在问题,上次修复采用Mock的方式测试,没有加载正确的环境 这次的修复根据改动小的原则,把plugin_id改为@Property,动态计算,这样能够提供最大的兼容性 本次修改我在本地docker环境下做了测试,没有出现加载问题: |
|
个人感觉每次计算pluginId可能性能开销相对来说有点大? 以上为个人意见,仅供参考。 |
理解你的顾虑,一方面该plugin_id的计算量并不大,而且调用也不频繁,只在插件加载、卸载、日志打印时使用 其实个人感觉,更好的解决方案是将plugin_id固定到插件的metadata文件,这样的plugin_id可以在插件市场确保唯一,避免数据存储混乱,但修改内容过多,且对现有插件不兼容 |
Modifications / 改动点
Fixes #8290
Screenshots or Test Results / 运行截图或测试结果
============================= test session starts =============================
platform win32 -- Python 3.12.13, pytest-9.0.3, pluggy-1.6.0 -- C:\Users\leaf\code\AstrBot.venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\leaf\code\AstrBot
configfile: pyproject.toml
plugins: anyio-4.13.0, asyncio-1.3.0, cov-7.1.0
asyncio: mode=Mode.STRICT, debug=False, asyncio_default_fixture_loop_scope=None, asyncio_default_test_loop_scope=function
collecting ... collected 40 items / 36 deselected / 4 selected
tests/test_plugin_manager.py::test_cleanup_plugin_optional_artifacts_clears_kv_when_plugin_id_present PASSED [ 25%]
tests/test_plugin_manager.py::test_cleanup_plugin_optional_artifacts_skips_kv_when_plugin_id_none PASSED [ 50%]
tests/test_plugin_manager.py::test_uninstall_plugin_reads_plugin_id_from_star_cls PASSED [ 75%]
tests/test_plugin_manager.py::test_uninstall_failed_plugin_passes_none_plugin_id PASSED [100%]
====================== 4 passed, 36 deselected in 5.83s =======================
Checklist / 检查清单
😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
/ 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
/ 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。
🤓 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.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。
Summary by Sourcery
Ensure plugin uninstallation also clears associated KV data and reflects this behavior in UI text and tests.
Bug Fixes:
Enhancements:
Documentation:
Tests: