OpenSpec Applied to an AI-Assisted Order-Cancellation Refactor
An engineering report describes a team adopting OpenSpec during an order-cancellation state-machine refactor after AI-generated code failed tests in two consecutive rounds. The workflow separates proposed changes from established specifications, describes externally observable behavior through Given/When/Then scenarios, and explicitly addresses concurrency, idempotency, permissions and input boundaries. In the team’s one-week comparison of four requirements in one order module, average rework per requirement reportedly fell from three rounds to zero, while first-review pass rate rose from about 40% to 85%. These figures are self-reported by the team.
The report presents specifications as a structured input contract for AI-assisted coding, showing how explicit behavioral and boundary requirements can supplement an implementation request. The comparison is limited to one team, four requirements and one week.
订单状态机是那种"改一行、崩一片"的模块。我们团队上个月重写取消订单链路时,AI 生成的代码连续两轮被测试打回,第三次我干脆停手,把 OpenSpec 引入进来重新做了一遍。
这篇不是 OpenSpec 的入门介绍,而是一次完整落地的工程记录:目录怎么组织、spec 怎么写才不变成伪代码、并发与幂等这些 AI 天生不会主动考虑的点怎么约束、以及最后拿到的实际数据。
为什么"描述需求给 AI"这条路走不通
我们原来的协作方式是:我在 IDE 里选中一段代码,用自然语言说"给取消订单加个原因字段,注意别影响退款流程"。AI 改出来的代码在 80% 的情况下是对的,剩下 20% 是灾难。
问题不在模型能力,在于自然语言没有契约性。"别影响退款流程"这句话,AI 的理解是"不要修改退款相关文件",而我的意思是"退款流程的行为必须保持不变"。这两者差别很大:前者可以通过不碰文件来满足,后者要求它在改状态机时必须验证所有下游分支。