fix(continuous-learning-v2): reconfigure stdout to UTF-8 on Windows#1328
fix(continuous-learning-v2): reconfigure stdout to UTF-8 on Windows#1328georgeradcj wants to merge 1 commit intoaffaan-m:mainfrom
Conversation
📝 WalkthroughWalkthroughThe change updates Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Greptile SummaryThis PR adds a Windows-only Confidence Score: 5/5Safe to merge — the change is a single, minimal, platform-guarded block that is a no-op on Linux/macOS and well-handled via try/except on Windows. The only changed code is a 10-line No files require special attention.
|
| Filename | Overview |
|---|---|
| skills/continuous-learning-v2/scripts/instinct-cli.py | Adds a sys.platform == "win32" guarded block to reconfigure stdout/stderr to UTF-8, wrapped in try/except Exception: pass to silently no-op in piped/test environments. Minimal, targeted, and safe. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[instinct-cli.py starts] --> B{sys.platform == 'win32'?}
B -- Yes --> C[try: reconfigure stdout + stderr to UTF-8]
C --> D{reconfigure succeeds?}
D -- Yes --> E[stdout/stderr: UTF-8 ✓]
D -- No / AttributeError / IOError --> F[silent pass – continue with default codec]
B -- No --> G[Linux/macOS: already UTF-8, no-op]
E --> H[status / evolve / etc. print █░ bars without UnicodeEncodeError]
F --> H
G --> H
Reviews (2): Last reviewed commit: "fix(continuous-learning-v2): reconfigure..." | Re-trigger Greptile
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (2)
skills/continuous-learning-v2/scripts/instinct-cli.py (2)
204-208:⚠️ Potential issue | 🟡 MinorInconsistent encoding handling in
cmd_importfor local files.Line 208 reads local files using
path.read_text()without specifying encoding, while the fix at line 107 explicitly usesencoding="utf-8". For consistency, local file imports should also use UTF-8.Suggested fix
content = path.read_text() + content = path.read_text(encoding="utf-8")🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@skills/continuous-learning-v2/scripts/instinct-cli.py` around lines 204 - 208, The local-file import in cmd_import reads the file with path.read_text() without an encoding; change it to read using UTF-8 (e.g., call path.read_text(encoding="utf-8") or open with encoding="utf-8") so it matches the explicit UTF-8 handling used elsewhere (see the earlier fix around line 107); update the reading of the variable content from path accordingly to ensure consistent UTF-8 decoding.
177-178:⚠️ Potential issue | 🟡 MinorMissing UTF-8 encoding for observations file.
Line 178 uses
open(OBSERVATIONS_FILE)without specifying encoding, which will use the system default on Windows (cp1250/cp1252) - the same issue being fixed elsewhere. This could fail on files with non-ASCII characters.Suggested fix
if OBSERVATIONS_FILE.exists(): - obs_count = sum(1 for _ in open(OBSERVATIONS_FILE)) + obs_count = sum(1 for _ in open(OBSERVATIONS_FILE, encoding="utf-8"))🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@skills/continuous-learning-v2/scripts/instinct-cli.py` around lines 177 - 178, The observation-counting code opens OBSERVATIONS_FILE without an explicit encoding; change it to open the file with UTF-8 to avoid platform-dependent decoding errors (use a context manager so the file is closed), e.g., replace the open(OBSERVATIONS_FILE) usage in the block that sets obs_count with a with open(OBSERVATIONS_FILE, encoding="utf-8") as f and count lines from f; reference OBSERVATIONS_FILE and the obs_count assignment to locate the change.
🧹 Nitpick comments (2)
skills/continuous-learning-v2/scripts/instinct-cli.py (2)
299-299: Consider specifying UTF-8 encoding for write operations.For consistency with the UTF-8 fixes applied to read operations,
write_text()calls should also explicitly specifyencoding="utf-8"to ensure correct behavior on Windows.Suggested fix
- output_file.write_text(output_content) + output_file.write_text(output_content, encoding="utf-8")Similarly for line 350:
- Path(args.output).write_text(output) + Path(args.output).write_text(output, encoding="utf-8")🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@skills/continuous-learning-v2/scripts/instinct-cli.py` at line 299, The write operations using Path.write_text currently omit an explicit encoding; update the calls (e.g., the occurrence invoking output_file.write_text(output_content) and the similar write_text call later) to pass encoding="utf-8" so files are written with UTF-8 on all platforms; locate the write_text invocations in instinct-cli.py and change them to include the encoding argument.
23-30: Consider logging the suppressed exception instead of silently passing.The broad
except Exception: passmakes debugging difficult ifreconfigurefails unexpectedly. While the fallback behavior is intentional, logging at DEBUG level would aid troubleshooting without disrupting normal operation.Suggested improvement
+import logging + +logger = logging.getLogger(__name__) + # Force UTF-8 stdout on Windows so ASCII-art confidence bars (█░) and # non-ASCII characters in instinct content don't crash under cp1250/cp1252. if sys.platform == "win32": try: sys.stdout.reconfigure(encoding="utf-8") sys.stderr.reconfigure(encoding="utf-8") - except Exception: - pass + except Exception as e: + logger.debug("Could not reconfigure stdout/stderr to UTF-8: %s", e)Note: Ruff flags this as S110 (
try-except-pass) and BLE001 (blind exception catch).🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@skills/continuous-learning-v2/scripts/instinct-cli.py` around lines 23 - 30, The try/except around sys.stdout.reconfigure and sys.stderr.reconfigure currently swallows all exceptions; instead catch Exception as e and log the failure at DEBUG so failures are visible but non-fatal. Import or use the existing logger and replace the bare except in the block that checks sys.platform == "win32" to log a debug message referencing sys.stdout.reconfigure/sys.stderr.reconfigure and the caught exception (e) before continuing.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@skills/continuous-learning-v2/scripts/instinct-cli.py`:
- Around line 204-208: The local-file import in cmd_import reads the file with
path.read_text() without an encoding; change it to read using UTF-8 (e.g., call
path.read_text(encoding="utf-8") or open with encoding="utf-8") so it matches
the explicit UTF-8 handling used elsewhere (see the earlier fix around line
107); update the reading of the variable content from path accordingly to ensure
consistent UTF-8 decoding.
- Around line 177-178: The observation-counting code opens OBSERVATIONS_FILE
without an explicit encoding; change it to open the file with UTF-8 to avoid
platform-dependent decoding errors (use a context manager so the file is
closed), e.g., replace the open(OBSERVATIONS_FILE) usage in the block that sets
obs_count with a with open(OBSERVATIONS_FILE, encoding="utf-8") as f and count
lines from f; reference OBSERVATIONS_FILE and the obs_count assignment to locate
the change.
---
Nitpick comments:
In `@skills/continuous-learning-v2/scripts/instinct-cli.py`:
- Line 299: The write operations using Path.write_text currently omit an
explicit encoding; update the calls (e.g., the occurrence invoking
output_file.write_text(output_content) and the similar write_text call later) to
pass encoding="utf-8" so files are written with UTF-8 on all platforms; locate
the write_text invocations in instinct-cli.py and change them to include the
encoding argument.
- Around line 23-30: The try/except around sys.stdout.reconfigure and
sys.stderr.reconfigure currently swallows all exceptions; instead catch
Exception as e and log the failure at DEBUG so failures are visible but
non-fatal. Import or use the existing logger and replace the bare except in the
block that checks sys.platform == "win32" to log a debug message referencing
sys.stdout.reconfigure/sys.stderr.reconfigure and the caught exception (e)
before continuing.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 9144c961-c9cf-4c1f-a13f-d7216d3a4024
📒 Files selected for processing (1)
skills/continuous-learning-v2/scripts/instinct-cli.py
`instinct-cli.py status` prints a confidence bar using the box-drawing
characters █ and ░. On Windows the default console codec is cp1250 /
cp1252, and Python's stdout inherits it, so the print statement crashes
with:
UnicodeEncodeError: 'charmap' codec can't encode characters in
position 4-13: character maps to <undefined>
This affects any Windows user whose console is not manually set to
`chcp 65001`, even after affaan-m#353 (file I/O UTF-8) and affaan-m#216 (.md glob) —
those fixes cover reading instinct files, but the crash happens on the
*output* side when rendering the status table.
Fix: reconfigure `sys.stdout` / `sys.stderr` to UTF-8 at startup, guarded
by `sys.platform == "win32"`. The reconfigure call is wrapped in
`try/except` because a caller may pipe into something that doesn't
support reconfigure (e.g., a captured buffer in tests).
No-op on Linux/macOS.
Verified locally on Windows 11 / Python 3.14 / cp1250 console:
`instinct-cli.py status` now prints all 155 instincts from
`~/.claude/homunculus/instincts/` without crashing.
54bb6be to
77ca23c
Compare
Summary
Narrowed after rebasing onto latest `main`: #216 (
.mdglob) and #353 (file I/O UTF-8) already landed, so this PR now only contains the remaining, orthogonal Windows-console fix.`instinct-cli.py status` renders a confidence bar using the box-drawing characters `█` / `░`. On Windows, `sys.stdout` inherits the console codec (cp1250 / cp1252 in non-UTF-8 locales), so the `print` call crashes with:
```
UnicodeEncodeError: 'charmap' codec can't encode characters in position 4-13: character maps to
```
The two earlier UTF-8 fixes cover reading instinct files — this one covers writing to the console.
Fix
Reconfigure `sys.stdout` / `sys.stderr` to UTF-8 at startup, guarded by `sys.platform == "win32"`. Wrapped in `try/except` because some captured streams (tests, certain wrappers) don't support `reconfigure`. No-op on Linux/macOS, where stdout is already UTF-8.
Repro (before this PR, on top of current `main`)
```
GLOBAL (apply to all projects)
AI-ASSISTANT (7)
Traceback (most recent call last):
...
File "C:\Program Files\Python314\Lib\encodings\cp1250.py", line 19, in encode
return codecs.charmap_encode(input, self.errors, encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 4-13: character maps to
```
After
```
GLOBAL (apply to all projects)
AI-ASSISTANT (7)
...
```
Test plan
Notes on bot reviews from the previous revision
The earlier revision of this PR duplicated changes that have since landed on `main` via #216 and #353. The reviewer comments about `cmd_import` / `cmd_export` / observations file encoding are already fixed on `main` — the new diff only touches stdout reconfigure.