"""Verify temporary Windows settings and native shell behavior without signing in."""
from hashlib import sha256
import json
import os
from pathlib import Path
import shutil
import subprocess
import sys
from tempfile import TemporaryDirectory
def main():
if os.name != "nt":
raise SystemExit("Run this lesson on a Windows host.")
codex = shutil.which("codex.exe") or shutil.which("codex")
powershell = shutil.which("pwsh") or shutil.which("powershell.exe")
cmd = shutil.which("cmd.exe")
if not all((codex, powershell, cmd)):
raise SystemExit("Codex CLI, PowerShell and Command Prompt must be installed.")
cases = []
def check(name, inputs, action, expected, actual):
assert expected == actual, (name, expected, actual)
cases.append(dict(name="windows_" + name, input=inputs, operation=action, expected=expected, actual=actual))
with TemporaryDirectory(prefix="mbsky-codex-windows-") as temporary:
root = Path(temporary)
workspace = root / "图书 练习"
profile = root / "fixture-codex"
workspace.mkdir()
profile.mkdir()
(workspace / "AGENTS.md").write_bytes(b"LAB_WINDOWS_WORKSPACE\n")
(profile / "AGENTS.md").write_bytes(b"LAB_WINDOWS_PROFILE\n")
env = {key:value for key,value in os.environ.items() if not key.startswith(("CODEX_", "GIT_"))}
env.update(CODEX_HOME=str(profile), MBSKY_LAB_BINARY=codex,
PATH=str(Path(codex).parent) + os.pathsep + env["PATH"])
def run(program, *args):
return subprocess.run([program, *args], cwd=workspace, env=env, capture_output=True, encoding="utf8",
timeout=18)
config = profile / "config.toml"
diagnostic = ["debug", "prompt-input", "--", "Count the library entries."]
for implementation in ("mxc", "elevated", "unelevated", "invalid"):
config.write_bytes(f'[windows]\nsandbox = "{implementation}"\n'.encode())
loaded = run(codex, *diagnostic)
if implementation == "invalid":
actual = [loaded.returncode, "unknown variant" in loaded.stderr and "windows.sandbox" in loaded.stderr]
check("invalid", implementation, "Read a TOML file with an unknown selector", [1, True], actual)
else:
diagnostic_output = json.loads(loaded.stdout)
text = "\n".join(part.get("text","") for item in diagnostic_output for part in item.get("content", ()))
check(implementation, implementation, "Read configuration through offline request assembly",
[0, True, True], [loaded.returncode, "LAB_WINDOWS_PROFILE" in text, "LAB_WINDOWS_WORKSPACE" in text])
config.unlink()
version = run(codex, "--version")
version.check_returncode()
ps = run(powershell, "-NoProfile", "-NonInteractive", "-Command",
"& $env:MBSKY_LAB_BINARY --version; if ($LASTEXITCODE) { exit $LASTEXITCODE }")
batch = run(cmd, "/d", "/c", "codex.exe --version")
for name, result in (("powershell", ps), ("cmd", batch)):
check(name, ["codex", "--version"], "Invoke the same native binary from the named host shell",
[0, version.stdout.strip()], [result.returncode, result.stdout.strip()])
os_build = sys.getwindowsversion()
print(json.dumps(dict(version={"python": sys.version.split()[0], "codex": version.stdout.strip(),
"windows_build": os_build.build}, codex_sha256=sha256(Path(codex).read_bytes()).hexdigest(), cases=cases)))
if __name__ == "__main__":
main()
本例需在 Windows build 26100、Python 3.12.14、codex-cli 0.167.0-alpha.4.1。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows build 26100、Python 3.12.14、codex-cli 0.167.0-alpha.4.1。
入口文件:projects/codex-windows/windows_lab.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
windows_lab.py,或下载右侧课程工程包。 - 确认本地已准备 Windows build 26100、Python 3.12.14、codex-cli 0.167.0-alpha.4.1。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联40章) · 含当前章节或实例 · mbskycom202612a90.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a123.zip