Prompt 测试应分两层:Core snapshot 验证模型可见角色、标记和顺序;HTTP/WebSocket 请求捕获验证最终 wire fields。只测某段文字“包含”不足以发现角色错误、重复注入、配对破坏或 Lite 方言映射错误。
调试入口复用真实构造链
build_prompt_input 创建 ephemeral Thread,捕获默认 Turn 与 StepContext,记录 Context updates 和用户 input,再调用 for_prompt() 与 build_prompt()。它没有进入完整 run_turn,却复用了 Prompt 关键准备路径。
async def capture_prompt_input(config, user_input):
thread = await start_ephemeral_thread(config)
turn = await thread.session.new_default_turn()
step = await thread.session.capture_step_context(turn)
await thread.session.record_context_updates(step)
await thread.session.record_user_input(user_input)
return thread.session.history.snapshot().for_prompt(turn.model.modalities)
Snapshot 适合固定大片结构:Developer/User message 数量、World State markers、AGENTS 替换文本和多 Agent 模式顺序。测试前应规范化临时路径、UUID 等非确定字段,但不能把 call_id、角色等语义字段一并抹掉。
Wire 请求捕获验证编译结果
测试服务器可挂载一次 SSE/Compact 响应并保存 request body。断言解析后的 JSON:instructions、input、tools、reasoning、text schema、cache key,以及连续 WebSocket 请求的 previous_response_id 和 delta。
async def assert_wire_request(session, fake_server):
fake_server.enqueue_sse_completed()
await session.run_turn("inspect prompt")
body = json.loads(fake_server.captured_request.body)
assert body["input"][0]["role"] == "developer"
assert_pairs_are_complete(body["input"])
assert body["prompt_cache_key"]
必测的故障样本
- 缺 output、孤儿 output 和不支持媒体的模型投影;
- World State 无变化不重复、变化替换、History replace 后完整重注入;
- 标准 Responses 与 Lite 的不同 wire shape;
- previous response 前缀匹配和任一字段变化后的完整回退;
- Local/Remote Compact 安装前后 History 与 baseline。
源码与测试锚点
codex-rs/core/src/prompt_debug.rs:真实构造链调试入口。codex-rs/core/src/session/tests.rs与 snapshots:上下文整体结构。codex-rs/core/src/client_tests.rs:Responses/Compact 请求捕获。codex-rs/core/src/context_manager/history_tests.rs:规范化细粒度测试。
评论
登录后即可评论