Windows brain script portability

From CarlosCaPe/octorato · View interactive version

Windows brain script portability is an AI agent skill from the GitHub repository CarlosCaPe/octorato. Scripts in ~/.claude/scripts/ that run on a Windows brain: use the python3.cmd shim and not bare python3, and reconfigure stdout to UTF-8 if you print symbols. Use it when writing a brain script or triaging a Windows-only crash.

How to use this skill

Skill file

Windows Brain Script Portability

When this fires

Rule 1 — `python3` is a Microsoft Store stub on Windows

python.org's Windows installer ships only python.exe. The name python3 is claimed by an App-Execution-Alias stub at %LOCALAPPDATA%\Microsoft\WindowsApps\python3.exe that prints "Python was not found" and exits non-zero.

Consequences for the brain:

The fix: scripts/install-runners.py drops ~/.local/bin/python3.cmd (forwards %* to py -3) as a thunk that wins PATH order against the stub. Re-run install-runners.py on any new Windows brain. This is documented in docs/architecture/python3-stub-windows-fix.md.

What you, the author, must do: Keep using python3 in scripts and docs — the shim makes it canonical. Do NOT switch to python or py -3 (breaks POSIX brains).

Rule 2 — UTF-8 reconfigure is mandatory for any script that prints Unicode

Windows consoles default to cp1252. Python's print() encodes through whatever the stdout stream says — and the stream defaults to the console encoding. Print a ✓ and you get UnicodeEncodeError: 'charmap' codec can't encode character '\u2713'. The crash happens AFTER the script did its actual work but BEFORE reporting success — pure mind-game.

The canonical block (must be at module top, after imports, with sys in scope):

# Force UTF-8 on stdout/stderr so the ✓ / ✗ / em-dash glyphs in reports
# survive on Windows shells defaulting to cp1252. Without this, a script
# can do its work correctly and still crash with UnicodeEncodeError when
# printing success.
for _stream in (sys.stdout, sys.stderr):
    try:
        _stream.reconfigure(encoding="utf-8", errors="replace")
    except (AttributeError, ValueError):
        pass

The try/except keeps it portable to non-stream stdio (some embedded contexts) and older Python (reconfigure is 3.7+).

errors="replace" over errors="strict" so a truly-unrepresentable byte degrades to ? instead of crashing — fail-soft on output beats fail-closed on debug noise.

Detection

# Audit: which brain scripts print Unicode AND lack the guard?
Get-ChildItem ~/.claude/scripts -Filter "*.py" | Where-Object {
  $c = Get-Content $_.FullName -Raw -Encoding UTF8
  ($c -match '[\u2190-\u2BFF\u2010-\u206F]') -and
  ($c -notmatch 'reconfigure\(encoding="utf-8"')
}

Expected output as of 2026-06-05: empty. If you see entries, apply Rule 2 to each.

Anti-patterns

Provenance

The pattern lived implicitly in e5aa67b (Windows-portability of pre-push lineage check) for one script. It became canonical on 2026-06-05 after a session sprawled across the same class of problem in install-runners.py and canon-render.py — when the operator noted "llevas todo el día con ese tema de python, mal gastando tokens en círculo" (you've been on that python topic all day, wasting tokens going in circles), the lesson got promoted from per-script reactive patch to repo-wide preventive sweep. Applied to 54 scripts in one commit, plus this skill written so the next session that touches a .py file resolves the question by activation instead of re-discovery.

The meta-lesson — "a fix without a skill is a pisada; a fix with a skill becomes piso" — sits next to harmonization-over-accretion and session-learn-extractor in the brain's self-improvement loop.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.