fix(sleep): read and write state.json as UTF-8 - #302
Open
PerryLink (PerryLink) wants to merge 1 commit into
Open
PerryLink (PerryLink) wants to merge 1 commit into
PerryLink (PerryLink) wants to merge 1 commit into
Conversation
Contributor
|
PerryLink (@PerryLink) please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
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.
skillopt_sleep/state.pyreads and writesstate.jsonwith no explicit encoding, whilesave()deliberately passesensure_ascii=False. The file therefore lands in the platform's locale charset, and two things follow -- the second is the serious one.1. On a locale that cannot represent the text,
save()raises. On a GBK machine:2. Even when it does not raise, the bytes are not UTF-8, and
load()swallows the failure.load()wraps its decode in a bareexcept Exception: pass, so any reader whose locale codec is UTF-8 -- a container, CI, WSL,PYTHONUTF8=1, or simply another machine -- gets a decode error, discards it, and returns a fresh state:Night counter, harvest cursors, history and the cross-night memory all disappear without a warning. The file is readable only by the machine that wrote it.
This is the defect
#124fixed inskillopt/config.py-- "read YAML config files as UTF-8", merged 2026-07-12, one line:state.pywas simply missed by that sweep. The change here is the same two lines:Tests
Two tests in
tests/test_sleep_state.py, and the second exists because the first is not sufficient:test_state_file_round_trips_non_ascii_as_utf8-- saves a CJK lesson and a CJK project path, asserts the bytes on disk decode as UTF-8, and asserts the round-trip preserves both.test_state_io_does_not_fall_back_to_the_locale_codec-- repeats the round-trip in a subprocess under-X warn_default_encoding -W error::EncodingWarning.The behavioural test cannot fail on a UTF-8 CI runner, because there the locale codec is UTF-8. On its own it would ship a regression test that never goes red. The warning-to-error guard makes CPython's own
EncodingWarninga failure on every platform, which is what actually pins the fix.Before (source fix reverted, tests kept):
After: