fix(agent): add encoding="utf-8" to os.fdopen() calls for cross-platform safety - #125
Open
hashbender wants to merge 1 commit into
Open
fix(agent): add encoding="utf-8" to os.fdopen() calls for cross-platform safety#125hashbender wants to merge 1 commit into
hashbender wants to merge 1 commit into
Conversation
|
Review Complete Risk: 🟢 Low (5/100) — no findings · 4 LOC across 2 files Clean, minimal fix: adds explicit encoding='utf-8' to two os.fdopen() calls to prevent locale-dependent encoding issues when writing JSON state files. Files Reviewed (2 files) |
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
os.fdopen(fd, "w")without an explicitencodingparameter uses the platform default encoding. On Windows this iscp1252(ormbcs), which silently corrupts non-ASCII characters — emoji, CJK, accented characters — when written to JSON files. The corrupted data is then read back withopen(..., encoding="utf-8"), producing garbled output orUnicodeDecodeError.This is the same class of cross-platform corruption bug as the
write_text()without encoding pattern (PR NousResearch#54240).Affected locations
agent/nous_rate_guard.py:120— rate-limit state persistence viajson.dump(state, f). The state dict can contain model names, provider slugs, or error messages with non-ASCII characters.agent/shell_hooks.py:645— shell hook metadata writes viajson.dumps(data, ...). The data dict can contain user-provided hook names, command outputs, or environment variable values with Unicode.Fix
Add
encoding="utf-8"to bothos.fdopen()calls, matching the pattern already used inhermes_cli/config.py(which passes**write_kwwithencoding="utf-8").Testing
python3 -m py_compile agent/nous_rate_guard.py— OKpython3 -m py_compile agent/shell_hooks.py— OKos.fdopen(fd, "w")→os.fdopen(fd, "w", encoding="utf-8")Mirror-of: NousResearch#56033
NousResearch#56033