Repository navigation
Conversation
Contributor
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="tests/unit/test_shared_preferences.py" line_range="284" />
<code_context>
+
+ store = SharedPreferences(str(path))
+
+ assert store.get("greeting") == "こんにちは \u2603 snowman"
+
+
</code_context>
<issue_to_address>
**New tests pass paths as strings**
Both added tests convert `tmp_path` children to strings before passing them to `SharedPreferences`; its filesystem operations accept `Path` objects, so these new path-handling call sites do not follow the required `pathlib.Path` convention.
Pass `path` directly to `SharedPreferences` in both tests.
Also at `tests/unit/test_shared_preferences.py:296`.
</issue_to_address>Sourcery assessment
Needs a human reviewer. If an existing preferences file contains bytes that are not valid UTF-8, loading it now catches the decoding error and deletes the file, losing its saved preferences. Reverting stops future deletions but cannot restore files already removed.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The v3 shared-preferences file is written with raw non-ASCII (
ensure_ascii=False) but opened without an explicit encoding, so its charset silently depends on the machine locale:astrbot/core/db/migration/shared_preferences_v3.py:20-29Two consequences on Windows, where the default codec is the ANSI codepage (GBK/cp936 on Chinese Windows, cp1252 on Western):
shared_preferences.jsonauthored as UTF-8 (created on Linux/macOS, or edited by the user) that contains a character the local codepage cannot decode raisesUnicodeDecodeError._load_preferencesonly catchesjson.JSONDecodeError, and the module instantiatessp = SharedPreferences()at import time, whichmigra_3_to_4.py:14imports — so the error escapes during the v3→v4 migration instead of being treated as a corrupt file.open(self.path, "w")truncates before any encoding happens, so storing a value the codepage cannot encode raises mid-write and leaves the previously saved preferences gone or half-written.Reproduced here on Chinese Windows (Python 3.12,
locale.getpreferredencoding()=cp936):On a machine whose locale already is UTF-8 the two tests pass even before the fix; they are correctness assertions rather than locale-sensitive probes, and the failure above is what Windows users hit.
Fix
Pin the charset for both directions and treat an undecodable file the same way a malformed one is already treated:
This is the same convention the rest of the codebase uses for JSON text I/O (e.g.
core/agent/runners/tool_loop_agent_runner.py:397,core/skills/neo_skill_sync.py:295,dashboard/services/skills_service.py:587all passencoding="utf-8"), and it matches AGENTS.md item 10 on cross-platform compatibility.Tests
Two cases added to
tests/unit/test_shared_preferences.py, next to the existing preference tests:☃must return the exact value;put()of a value the OS codepage cannot encode must leave a valid file holding both the previous and the new key (the truncation case above).After the change the whole file passes, including the pre-existing cases:
ruff format/ruff checkare clean on both files. The fix touches only the v3 migration helper; the current DB-backedastrbot/core/utils/shared_preferences.pyis unchanged.Prepared with an AI coding agent.
Summary by Sourcery
Make legacy shared-preferences persistence reliably UTF-8 across platforms and protect existing data during non-ASCII writes.
Bug Fixes:
Tests: