"""Check instruction discovery and a structured prompt using the installed offline diagnostic."""
from hashlib import sha256
import json
import os
from pathlib import Path
import shutil
import subprocess
import sys
from tempfile import TemporaryDirectory
PROMPT = """Goal: Define catalog login error responses.
Context: Work in api/ and use the existing JSON envelope.
Constraints: Preserve success behavior and avoid logging passwords.
Acceptance: Missing email returns 400 and the error body contains only the public message."""
def main():
codex = shutil.which("codex.exe") or shutil.which("codex")
git = shutil.which("git")
if not codex or not git:
raise SystemExit("Codex CLI and Git must be installed.")
cases = []
def check(name, data, operation, expected, actual):
assert expected == actual, (name, expected, actual)
cases.append(dict(name="prompt_" + name, input=data, operation=operation, expected=expected, actual=actual))
with TemporaryDirectory(prefix="mbsky-codex-prompt-instructions-") as temporary:
root = Path(temporary)
repo = root / "repo"
child = repo / "api"
home = root / "codex-home"
home.mkdir()
child.mkdir(parents=True)
files = {
"codex-home/AGENTS.md": "MBSKY_GLOBAL_RULE",
"repo/AGENTS.md": "MBSKY_REPOSITORY_RULE",
"repo/api/AGENTS.md": "MBSKY_CHILD_BASE",
"repo/api/AGENTS.override.md": "MBSKY_CHILD_OVERRIDE",
}
for name, value in files.items():
(root / name).write_bytes((value + "\n").encode())
env = {key:value for key,value in os.environ.items() if not key.startswith(("GIT_", "CODEX_THREAD"))}
env.update(CODEX_HOME=str(home), GIT_CONFIG_NOSYSTEM="1", GIT_CONFIG_GLOBAL=os.devnull)
subprocess.run([git,"init","--quiet","--template=",str(repo)],check=True,env=env,capture_output=True)
def cli(cwd, *args):
return subprocess.run([codex,*args],cwd=cwd,env=env,check=True,capture_output=True,
encoding="utf8",timeout=15).stdout
def summarize(cwd):
messages = json.loads(cli(cwd,"debug","prompt-input","--",PROMPT))
items = [(row.get("role"), item.get("text","")) for row in messages for item in row.get("content",[])]
text = "\n".join(value for _,value in items)
markers = [(text.find(value), value) for value in files.values() if value in text]
return [value for _,value in sorted(markers)], items[-1]
baseline = ["MBSKY_GLOBAL_RULE","MBSKY_REPOSITORY_RULE"]
repo_markers, _ = summarize(repo)
check("root",dict(cwd="repo",files=files),"Render local input from repository root and compare instruction order",
baseline,repo_markers)
child_markers, prompt = summarize(child)
check("child",dict(cwd="repo/api",files=files),"Render local input from the subdirectory with an override file",
baseline+["MBSKY_CHILD_OVERRIDE"],child_markers)
check("message",PROMPT,"Verify the exact four-field input stays in the final user message",
["user",PROMPT],list(prompt))
# Compare an existing empty override with a missing override.
(child / "AGENTS.override.md").write_bytes(b"")
empty_markers, _ = summarize(child)
check("empty_override",dict(cwd="repo/api",empty_override=True,files=files),
"Repeat input rendering with an empty override and a nonempty base instructions file",
baseline,empty_markers)
(child / "AGENTS.override.md").unlink()
missing_markers, _ = summarize(child)
check("missing_override",dict(cwd="repo/api",override_present=False,files=files),
"Repeat input rendering after removing only the fixture override",
baseline+["MBSKY_CHILD_BASE"],missing_markers)
version = cli(repo,"--version").strip()
print(json.dumps(dict(version={"python":sys.version.split()[0], "codex":version},
codex_sha256=sha256(Path(codex).read_bytes()).hexdigest(),cases=cases)))
if __name__=="__main__":
main()
本例需在 Windows、Python 3.12.14、codex-cli 0.167.0-alpha.4.1、本机 Git。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows、Python 3.12.14、codex-cli 0.167.0-alpha.4.1、本机 Git。
入口文件:projects/codex-prompting/prompting_lab.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
prompting_lab.py,或下载右侧课程工程包。 - 确认本地已准备 Windows、Python 3.12.14、codex-cli 0.167.0-alpha.4.1、本机 Git。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联40章) · 含当前章节或实例 · mbskycom202612a90.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a116.zip