Skip to content

fix(agent): add encoding="utf-8" to os.fdopen() calls for cross-platform safety - #125

Open
hashbender wants to merge 1 commit into
mainfrom
mirror/pr-56033
Open

fix(agent): add encoding="utf-8" to os.fdopen() calls for cross-platform safety#125
hashbender wants to merge 1 commit into
mainfrom
mirror/pr-56033

Conversation

@hashbender

Copy link
Copy Markdown
Owner

Problem

os.fdopen(fd, "w") without an explicit encoding parameter uses the platform default encoding. On Windows this is cp1252 (or mbcs), which silently corrupts non-ASCII characters — emoji, CJK, accented characters — when written to JSON files. The corrupted data is then read back with open(..., encoding="utf-8"), producing garbled output or UnicodeDecodeError.

This is the same class of cross-platform corruption bug as the write_text() without encoding pattern (PR NousResearch#54240).

Affected locations

  1. agent/nous_rate_guard.py:120 — rate-limit state persistence via json.dump(state, f). The state dict can contain model names, provider slugs, or error messages with non-ASCII characters.

  2. agent/shell_hooks.py:645 — shell hook metadata writes via json.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 both os.fdopen() calls, matching the pattern already used in hermes_cli/config.py (which passes **write_kw with encoding="utf-8").

Testing

  • python3 -m py_compile agent/nous_rate_guard.py — OK
  • python3 -m py_compile agent/shell_hooks.py — OK
  • Both changes are single-line os.fdopen(fd, "w")os.fdopen(fd, "w", encoding="utf-8")

Mirror-of: NousResearch#56033
NousResearch#56033

@tenki-reviewer

tenki-reviewer Bot commented Jul 1, 2026

Copy link
Copy Markdown

Review Complete
No issues found!

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)
agent/nous_rate_guard.py
agent/shell_hooks.py

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.

1 participant