蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

提交一个有范围和验收的提示词,再执行四种测试

PYTHONWindows、Python 3.12.14、codex-cli 0.167.0-alpha.4.1。cli_prompting_lab.py
cli_prompting_lab.py68 行
"""Verify a structured task prompt and run its acceptance cases without a model call."""
from hashlib import sha256
import json
import os
from pathlib import Path
import shutil
import subprocess
import sys
from tempfile import TemporaryDirectory


PROMPT = """Goal: return a sorted copy of the supplied task numbers.
Context: use Python and preserve duplicates.
Scope: implement in queue_view.py only; read existing tests.
Verify: test empty, already sorted, reversed, and duplicate input.
Report: list the changed paths and exact verification output."""


def main():
    codex = shutil.which("codex.exe") or shutil.which("codex")
    if not codex:
        raise SystemExit("Install Codex CLI on PATH.")
    cases = []

    def check(name, inputs, operation, expected, actual):
        assert expected == actual, (name,expected,actual)
        cases.append(dict(name="cli_prompt_"+name,input=inputs,operation=operation,expected=expected,actual=actual))

    with TemporaryDirectory(prefix="mbsky-cli-prompting-") as temporary:
        root=Path(temporary)
        home=root/"profile"
        repo=root/"workspace"
        home.mkdir()
        repo.mkdir()
        (repo/"AGENTS.md").write_bytes(b"MBSKY_PROMPT_REPO. Leave public filenames stable.\n")
        env={k:v for k,v in os.environ.items() if not k.startswith("CODEX_")}
        env["CODEX_HOME"]=str(home)

        def run(*args):
            result=subprocess.run([codex,*args],cwd=repo,env=env,capture_output=True,encoding="utf8",check=True,timeout=18)
            return result.stdout

        messages=json.loads(run("debug","prompt-input","--",PROMPT))
        actual=[part["text"] for row in messages if row.get("role")=="user"
                for part in row["content"] if part.get("type")=="input_text"]
        check("assembled",PROMPT,"Run native CLI input diagnostic and compare submitted text byte-for-byte",PROMPT,actual[-1])
        check("scope","AGENTS.md","Inspect the context included in the local request",
              True,any("MBSKY_PROMPT_REPO" in s for s in actual))
        resume=run("resume","--help")
        check("resume","resume --help","Read current resume options without touching real conversations",
              [True,True],["SESSION_ID" in resume,"--last" in resume])
        version=run("--version").strip()

        source=root/"queue_view.py"
        source.write_bytes(b"import json, sys\nvalues = json.loads(sys.argv[1])\nprint(json.dumps([sorted(values), values]))\n")
        for name,values,expected in (("empty",[],[]),("ordered",[1,4],[1,4]),("reverse",[8,3,1],[1,3,8]),
                                     ("duplicates",[9,1,9],[1,9,9])):
            result=subprocess.run([sys.executable,"-I","-B",str(source),json.dumps(values)],cwd=root,
                                  env=env,capture_output=True,encoding="utf8",check=True,timeout=10)
            check(name,values,"Run own Python implementation as a subprocess and verify input stability",
                  [expected,values],json.loads(result.stdout))
    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。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows、Python 3.12.14、codex-cli 0.167.0-alpha.4.1。

入口文件:projects/codex-cli_prompting/cli_prompting_lab.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 cli_prompting_lab.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows、Python 3.12.14、codex-cli 0.167.0-alpha.4.1。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Codex 教程

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a97.zip

下载课程关联工程包:Codex 教程

课程关联工程包(登记关联40章) · 含当前章节或实例 · mbskycom202612a90.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习