用codex修复了windows环境问题,供佬们参考:
Windows 修复方法
1. 修正用户目录解析
原代码在 Windows 使用 Path.home(),测试设置的 HOME 不生效。新增:
def resolve_home() → Path:
configured = (
os.environ.get(“CLAUDE_KEYSMITH_HOME”)
or os.environ.get(“HOME”)
)
if configured:
return Path(configured).expanduser().resolve()
return Path.home().resolve()
然后将 user scope 中的:
Path.home()
替换为:
resolve_home()
2. 自动识别 PowerShell
def runtime_shell_kind() → str:
configured = os.environ.get(
“CLAUDE_KEYSMITH_SHELL”, “”
).strip().lower()
if configured:
return configured
return "powershell" if os.name == "nt" else "zsh"
3. 定位 PowerShell Profile
def powershell_profile_path(home: Path) → Path:
configured = os.environ.get(“CLAUDE_KEYSMITH_SHELL_RC”)
if configured:
return Path(configured).expanduser().resolve()
module_path = os.environ.get("PSModulePath", "")
profile_dir = (
"WindowsPowerShell"
if "WindowsPowerShell" in module_path
else "PowerShell"
)
return (
home
/ "Documents"
/ profile_dir
/ "Microsoft.PowerShell_profile.ps1"
)
4. 定位 Windows Claude CLI
def find_claude_binary(home: Path, shell_kind: str) → Path:
configured = os.environ.get(“CLAUDE_KEYSMITH_CLAUDE_BIN”)
if configured:
return Path(configured).expanduser().resolve()
if shell_kind == "powershell":
found = (
shutil.which("claude.cmd")
or shutil.which("claude.exe")
or shutil.which("claude")
)
if found:
return Path(found).resolve()
appdata = Path(
os.environ.get(
"APPDATA",
str(home / "AppData" / "Roaming"),
)
)
return appdata / "npm" / "claude.cmd"
found = shutil.which("claude")
return (
Path(found).resolve()
if found
else home / ".local" / "bin" / "claude"
)
5. 生成 PowerShell Wrapper
def powershell_quote(value: Path) → str:
return “'” + str(value).replace(“'”, “‘’”) + “'”
def render_powershell_wrapper(
claude_bin: Path,
system_prompt: Path,
append_prompt: Path,
) → str:
return “\n”.join([
“# >>> claude-keysmith runtime >>>”,
“# Managed by claude-keysmith. Do not edit by hand.”,
“function global:claude {”,
f" & {powershell_quote(claude_bin)} `“,
f” --system-prompt-file {powershell_quote(system_prompt)} `“,
f” --append-system-prompt-file {powershell_quote(append_prompt)} `“,
" @args”,
“}”,
“# <<< claude-keysmith runtime <<<”,
“”,
])
6. 统一运行时路径
将原来的固定字段:
“zshrc”: home / “.zshrc”,
“claude_bin”: home / “.local” / “bin” / “claude”,
改成:
“shell_kind”: shell_kind,
“shell_rc”: shell_rc,
“claude_bin”: find_claude_binary(home, shell_kind),
安装、状态检查、卸载和 doctor 中统一使用:
rt[“shell_rc”]
rt[“shell_kind”]
而不是固定使用:
rt[“zshrc”]
7. 测试
python -m py_compile .\claude-instruct.py
python -m pytest -q
当前修复结果:
25 passed
8. Windows 安装验证
python .\claude-instruct.py install --scope user --runtime
python .\claude-instruct.py install --scope user --runtime --yes
python .\claude-instruct.py status --scope user --runtime --json
. $PROFILE
claude --version