Skip to content

fix: preserve user content in dict-type config items - #9958

Merged
Soulter merged 1 commit into
AstrBotDevs:masterfrom
Shxiao101:fix/9512-preserve-dict-config
Sep 6, 2026
Merged

fix: preserve user content in dict-type config items#9958
Soulter merged 1 commit into
AstrBotDevs:masterfrom
Shxiao101:fix/9512-preserve-dict-config

Conversation

@Shxiao101

@Shxiao101 Shxiao101 commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Fixes #9512.

Plugin schema items declared as "type": "dict" are free-form mappings edited from the WebUI. check_config_integrity() recursed into them like structured dicts, so user-added keys were treated as stale entries and removed on plugin reload/restart, and the wiped value was written back to disk.

Modifications / 改动点

  • astrbot/core/config/astrbot_config.py: check_config_integrity() now receives the schema of the current level (the items mapping for object nodes). Entries declared as "type": "dict" keep the user's mapping as-is instead of being recursed and cleaned; "type": "object" entries — including empty items — and the schema-less global config keep the existing stale-key cleanup.

  • tests/unit/test_config.py: add regression tests covering dict items with empty and non-empty defaults on reload, dict items nested in objects, object entries with empty items still dropping unknown keys, stale-key cleanup for structured dicts, and non-dict values resetting to default.

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

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

Regression tests for the fix are in tests/unit/test_config.py::TestDictTypeConfigIntegrity. Full test logs (pre-existing third-party DeprecationWarnings filtered via -W ignore::DeprecationWarning):

$ uv run pytest tests/unit/test_config.py -q -W ignore::DeprecationWarning
.....................................................                    [100%]
53 passed in 3.10s

$ uv run pytest tests/unit/test_astrbot_config_manager.py tests/test_plugin_manager.py -q -W ignore::DeprecationWarning
..................................................................       [100%]
66 passed in 8.58s

$ uv run pytest tests/unit/test_agent_runner_config.py tests/test_fastapi_v1_dashboard.py -q -W ignore::DeprecationWarning
........................................................................ [ 63%]
..........................................                               [100%]
114 passed in 28.34s

$ uv run ruff check astrbot/core/config/astrbot_config.py tests/unit/test_config.py
All checks passed!

$ uv run ruff format --check astrbot/core/config/astrbot_config.py tests/unit/test_config.py
2 files already formatted

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.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Preserve user content in free-form dictionary configuration items without weakening structured configuration cleanup.

Bug Fixes:

  • Preserve user-defined entries in plugin schema items declared as free-form dictionaries across reloads and restarts.
  • Reset invalid non-dictionary values to the configured default while retaining existing cleanup for structured objects and dictionaries.

Enhancements:

  • Propagate schema context during configuration integrity checks so free-form and structured mappings are handled according to their declared types.

Tests:

  • Add regression coverage for top-level and nested free-form dictionaries, non-empty defaults, invalid values, and continued stale-key cleanup for structured objects.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Needs a human reviewer. The change preserves user-supplied keys in schema-declared free-form mappings, so an incorrect implementation could retain unexpected configuration that affects later runtime behavior and survives until the config is normalized or edited. The persisted values are bounded and can be removed or corrected, with no apparent irreversible external action in this diff.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@Soulter
Soulter merged commit 610eab0 into AstrBotDevs:master Sep 6, 2026
21 checks passed
@Shxiao101
Shxiao101 deleted the fix/9512-preserve-dict-config branch September 6, 2026 10:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] check_config_integrity 会清空插件配置中 dict 类型键的用户内容

2 participants