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.
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: