fix(hooks): UTF-8 stdin, atomic multi-DB save, and unique backup dirs in update-db.py - #17
Open
otsune wants to merge 1 commit into
Open
fix(hooks): UTF-8 stdin, atomic multi-DB save, and unique backup dirs in update-db.py#17otsune wants to merge 1 commit into
otsune wants to merge 1 commit into
Conversation
… in update-db.py Three related data-integrity fixes for update-db.py, found when a real session update crashed mid-save on Windows/Git Bash: - force_utf8_io() now reconfigures stdin too. Under an ASCII/C locale, a piped payload containing CJK text was decoded with surrogateescape and only blew up later at re-encode time, after some databases were already written (double-counting stats on retry). - Replace per-file save_json() with a two-phase save_all(): stage all six databases to .tmp files first, then swap them in. Serialization/ encoding errors now exit 2 before any real database is touched, so the six files can no longer end up mutually inconsistent. - backup_all() never reuses an existing backup dir. Retrying the same session_id previously overwrote the only pre-corruption backup; it now creates pre-update-<id>-2, -3, ... siblings. Also: test suite file reads now pass encoding="utf-8" (the non-Latin milestone test failed on default cp932), plus four new regression tests covering CJK payloads, forced ASCII locale, backup preservation, and save-failure atomicity. Docs updated to match. Co-Authored-By: Claude <noreply@anthropic.com>
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.
Summary
Three related data-integrity fixes for
update-db.py, found when a real session update crashed mid-save on Windows/Git Bash and left the six databases mutually inconsistent (stats double-counted after retry).1.
force_utf8_io()now reconfigures stdin (fluent_paths.py)Under an ASCII/C locale (Git Bash on Windows), a piped session payload containing CJK text was decoded with
surrogateescape. The lone surrogates survived JSON parsing and only blew up at re-encode time — i.e. during the save loop, afterlearner-profile.jsonandprogress-db.jsonhad already been written. Retrying then double-counted sessions/exercises/minutes.2. Two-phase commit across all six databases (
update-db.py)Replaced the per-file
save_json()loop withsave_all(): every database is staged to a.tmpfile (withfsync) first, and only when all six serialize successfully are they swapped in viarename. A serialization/encoding error now exits2before any real database is touched, so the documented "no files were modified" guarantee indocs/DB_SCRIPTS.mdactually holds. Stale.tmpfiles are cleaned up on failure.3. Pre-update backups are never overwritten (
update-db.py)backup_all()previously reusedpre-update-<session_id>/, so retrying the same session id overwrote the only backup taken before anything went wrong. It now creates numbered siblings (pre-update-<id>-2,-3, …), preserving the earliest (pre-corruption) backup.Also
encoding="utf-8"—test_milestone_non_latin_distinct_nonempty_idsfailed on Windows default locales (cp932) before this.LC_ALL=Crun, backup preservation on retry, and save-failure atomicity (all six DBs byte-identical after a forced staging failure).docs/DB_SCRIPTS.mdside-effects section updated to match; CHANGELOG entry added under Unreleased (version bump left to the maintainer).Test plan
python3 tests/test_update_db.py— 16 tests pass on Windows 11 / Git Bash / Python 3.10 (12 existing + 4 new; the new CJK tests fail before the fix, pass after)PYTHONUTF8=1Co-Authored-By: Claude noreply@anthropic.com