Codex 提示词:给出上下文、约束与验收结果
把功能、故障或审查任务写成能复现的输入,再检查发送前实际使用的项目说明。
学完这一章,你能够
- 把功能、故障或审查任务写成能复现的输入,再检查发送前实际使用的项目说明。
把目标、背景与验收条件写在一起
一个任务描述可包括功能目标、已有模块、允许变化和具体验收。给登录 API 写需求时,注明路由、邮件与密码字段、兼容的响应格式,以及缺失字段、错误凭据和成功登录的不同结果。密钥、密码和完整令牌应留在受保护的环境。
FastAPI、PostgreSQL、bcrypt 与 JWT 等实现条件按项目已有依赖说明。复用认证工具时检查令牌签发、过期、验证及撤销策略。若要记录请求日志,只保留排错所需字段。发布前用不同状态的本地测试确认请求和响应,而不是仅检查能否返回令牌。
把认证任务分成数据、注册、登录、鉴权集成、登出、回归测试与文档。每一步都写可观察的输出和依赖,验证后再继续。Plan Mode 可用来梳理步骤和待决问题。
检查指令和发送的实际范围
首次处理模块时给出实际技术版本、目录、相关函数和已有项目约定。引用接口契约、现有测试或一份清楚的响应样例。性能要求提供基线、数据规模、并发条件及目标指标。
本站用临时 Git 仓库核对项目说明。根工作目录读取全局及项目指令。进入 api 后,同目录的 AGENTS.override.md 优先于 AGENTS.md。本机版本遇到空覆盖文件时也跳过同目录基础文件。移除覆盖文件后才读到基础文件。图中给出本机对比记录。

本地诊断也核对最终用户消息的四行内容与角色。此步骤可以发现项目范围和输入丢失;进入模型后的理解质量要靠后续操作和验证。
让错误和图像都能指向原始条件
给出最短复现命令、异常、相关调用栈、测试数据、观察行为和预期。以登录测试为例,把实际 500 与应有 400 的输入保存下来,再定位数据验证或错误处理。
错误截图用于界面或日志位置,设计稿用于布局意图,架构图用于组件与数据流,数据图表需要解释尺度与时间范围。附图时保留清晰尺寸和所在版本,清除敏感信息。CLI 支持 -i 或 --image,重复可传多张。桌面与 IDE 按附件能力使用粘贴、拖放或选文件,格式及大小以当前客户端为准。
迭代中保留已有契约
以列表 API 为例,先保证分页,再增加排序和搜索,每次比较现有响应。遇到慢查询时从测量与执行计划判断索引作用。诸如让它更快的要求应说明代码位置、负载和目标;批量删除则应明确输入、权限、部分失败及重试行为。
功能任务可以写目标、参考实现、约束、边界和验收。故障任务可以写复现、栈、期望和应继续通过的测试。审查任务先选实际差异、提交或比较基线,再写安全、性能等重点。旧 /review 路径及范围写法依本机命令检查。
若结果偏离预期,补充可复现上下文、当前代码示例与接受标准。持久的团队约定放在适用目录的 AGENTS.md,回合级任务继续明确新增条件。
提示词基本要素
| 要素 | 组织方式 |
|---|---|
| 任务描述 | 指出要实现、排查或解释的业务行为。 |
| 上下文 | 引用已有模块或契约,并确认工作目录。 |
| 约束条件 | 指定应保留的接口、允许的范围与日志处理。 |
| 期望结果 | 明确状态码、错误字段和通过条件。 |
按需提供上下文
| 类型 | 对应的材料 |
|---|---|
| 技术栈 | 列出相关运行时、框架、数据库和版本。 |
| 项目结构 | 说明入口、API 层和相关项目说明。 |
| 现有代码 | 列出已确认的文件、符号或相关测试。 |
| 约束条件 | 写清适用目录、接口兼容和性能要求。 |
附图的教学用途
| 类型 | 可验证目标 |
|---|---|
| 错误截图 | 定位错误内容,结合复现输入。 |
| 设计稿 | 确认组件层次、样式和交互行为。 |
| 架构图 | 说明模块与数据流,并对照源码。 |
| 数据图表 | 核对量纲、数据范围和统计定义。 |
在发送前核对项目说明与提示词
"""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。
当前源码 SHA256:eaafe78214da82efa8ecb9d2fe8e77d19fd20ff4da204ec8b9c7523a02142a56
提示词输入练习 ZIP
含 Python 检查脚本、运行说明和浏览器观测页。
所需环境:Windows、Python 3.12.14、codex-cli 0.167.0-alpha.4.1、本机 Git。
ZIP SHA256:36312d59627a6bb58a4415011fd36cf55e28beb6c7931963cd03cf61ee8d5575
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
把临时子目录的覆盖文件改成另一句明确要求,再观察最终输入。
展开参考解答
根目录保持两组标记,子目录显示新的覆盖文本。