OpenSpec 通过规格驱动 AI 编码流程,解决沟通成本高、上下文丢失等痛点。它利用 Markdown 文件持久化需求和设计,将 AI 对话转化为可追溯的项目资产。本文涵盖安装配置、核心理念、命令详解及七大实战场景,帮助开发者在 TypeScript 和 Node.js 环境下高效管理 AI 辅助开发工作流,实现从需求到验收的闭环控制。
星落10 浏览
OpenSpec 完全使用指南
用规格驱动 AI 编码 —— 让 AI 真正理解你要什么
如果你正在用 AI 写代码,却总觉得'沟通成本'比'写代码'还高——OpenSpec 可能是你一直缺的那块拼图。
## Purpose
用户认证模块,管理登录、登出和会话维护。
### Requirement: Login Authentication
系统 MUST 在用户提供有效凭证时签发 JWT token。
系统 MUST 在凭证无效时返回 401 错误,且不泄露是用户名还是密码错误。
系统 SHOULD 在连续 5 次失败后触发临时锁定。
系统 MAY 支持'记住我'功能以延长 token 有效期。
关键字
含义
MUST
必须实现,不实现就是 Bug
SHOULD
强烈建议,除非有充分理由可以不做
MAY
可选的增强功能
场景 - Given/When/Then 格式
#### Scenario: Successful Login
Given 用户名 "alice" 存在且密码正确
When 用户提交登录请求
Then 系统返回 200 和有效的 JWT token
And token 有效期为 24 小时
#### Scenario: Invalid Password
Given 用户名 "alice" 存在但密码错误
When 用户提交登录请求
Then 系统返回 401
And 错误信息不区分'用户不存在'和'密码错误'
You: /opsx:explore 我们应该用 WebSocket 还是 SSE 来实现实时通知?请分析当前的架构,评估两种方案
AI: Let me investigate your codebase...
Comparison:
• WebSocket: bidirectional, but requires nginx upgrade config
• SSE: simpler, works through existing HTTP stack
Recommendation: SSE for your use case.
explore 完成后可以自然衔接到 propose 或 new。
/opsx:apply — 按清单执行实现
工件准备好了,让 AI 按 tasks.md 里的清单逐条干活:
You: /opsx:apply add-github-oauth
AI: Reading tasks.md... Found 8 tasks, 0 completed.
Working on task 1: Install passport-github2 dependency
✓ Added to package.json, ran npm install
Working on task 2: Create OAuth strategy configuration
✓ Created src/auth/github-strategy.ts
... All 8 tasks complete!
You: 发现 localStorage 写入逻辑缺失,请补充
AI: Fixing the issue...
✓ Added localStorage.setItem('theme', selectedTheme) in ThemeContext
✓ Added loadSavedTheme() on app initialization
Fix complete!
方式 B - 需要多个步骤,追加任务:
You: /opsx:continue add-dark-mode
AI: All planning artifacts exist. You can:
- Edit tasks.md manually to add new tasks
- Or describe what needs to be added
You: 在 tasks.md 里追加两个任务:
1. 添加 localStorage 读取逻辑
2. 添加主题切换后的持久化写入
AI: ✓ Updated tasks.md with 2 new tasks
Ready for /opsx:apply
再次,执行修复
You: /opsx:apply add-dark-mode
AI: Reading tasks.md... Found 2 new tasks, 0 completed.
Working on task 1: Add localStorage read logic
✓ Added loadSavedTheme() in App.tsx
Working on task 2: Add persistence on theme change
✓ Added useEffect hook in ThemeContext
All tasks complete!
最后,再次 verify 确认修复
You: /opsx:verify add-dark-mode
AI: ✅ Completeness: 8/8 tasks done
✅ Correctness: Theme persists correctly after refresh
✅ Coherence: No issues found
Ready for archive!
You: /opsx:apply fix-login-bug
AI: Reading tasks.md... Found 3 tasks pending.
Working on task 3: Add rate limiting to login endpoint
✓ Added express-rate-limit middleware
... All 3 tasks complete!
情况 B - 规划中途(工件还没生成完):
You: /opsx:continue fix-login-bug
AI: Last completed artifact: specs
Next artifact: design.md
Creating design.md...
✓ design.md created
Next artifact: tasks
情况 C - 需要先看规格再决定:
You: 帮我读一下 openspec/changes/fix-login-bug/specs/auth.md
AI: Reading specs...
Current delta specs for auth module:
- ADDED: Account lockout after 5 failed attempts
- MODIFIED: Login error response (no distinction between "user not found" and "wrong password")
Based on these specs, the implementation should...
第四步:必要时同步规格
You: /opsx:sync fix-login-bug
AI: Merging delta specs into main specs...
✓ openspec/specs/auth.md updated
Change remains active (not archived).
You: /opsx:verify fix-login-bug
AI: Verifying implementation against specs...
⚠️ Completeness: WARNING — Missing scenario coverage
The following scenario is not tested:
- Scenario: Account Unlock After Timeout
Recommendation: Add this scenario to specs and implement corresponding logic.
## ADDED Requirements### Requirement: Account Lockout (MODIFIED)
系统 MUST 在连续 5 次登录失败后锁定账户。
系统 MUST 在锁定 30 分钟后自动解锁。← 新增
#### Scenario: Account Lock After Failed Attempts
Given 用户连续 5 次输入错误密码
When 第 5 次登录请求提交
Then 系统返回 401
And 账户被锁定 30 分钟
#### Scenario: Account Unlock After Timeout ← 新增场景
Given 账户因多次失败被锁定
When 30 分钟过去
Then 账户自动解锁
And 用户可以重新尝试登录
第三步:同步规格到主规格(可选)
You: /opsx:sync fix-login-bug
AI: Merging delta specs into main specs...
✓ openspec/specs/auth.md updated
Change remains active.
You: 在 tasks.md 里追加两个任务:
1. 添加 30 分钟后自动解锁逻辑
2. 添加解锁场景的单元测试
AI: ✓ Updated tasks.md with 2 new tasks
第五步:执行新增任务
You: /opsx:apply fix-login-bug
AI: Reading tasks.md... Found 2 new tasks.
Working on task 4: Add automatic unlock logic
✓ Added scheduled job to clear lockouts
Working on task 5: Add unit tests
✓ Created auth-unlock.test.ts with 3 test cases
All tasks complete!
第六步:最终 verify
You: /opsx:verify fix-login-bug
AI: ✅ Completeness: 5/5 tasks done, all scenarios covered
✅ Correctness: Implementation matches updated specs
✅ Coherence: No issues found
Ready for archive!
💡 关键要点:
Specs 是活的文档,发现遗漏随时可以改
改了 specs 后,记得同步更新 tasks.md
用 /opsx:verify 验证场景覆盖是否完整
如果遗漏影响很大,可能需要 /opsx:explore 重新评估方案
场景七:有明确的需求文档,怎么开始
问题: 手里已经有 PRD/需求文档,如何用 OpenSpec 开始工作。
两种路径:
路径 A:让 AI 从需求文档生成工件(推荐)
Step 1:创建变更目录
You: /opsx:new user-registration-module
AI: Created openspec/changes/user-registration-module/
Ready to create: proposal
Step 2:提供需求文档,生成 proposal
You: 这是我们的 PRD 文档,请帮我生成 proposal.md:
[粘贴 PRD 内容或上传文件]
AI: Analyzing requirements...
✓ Created proposal.md with:
- Problem statement: User registration friction
- Scope: Email registration, verification, welcome flow
- Out of scope: Social login, SSO (future phases)
- Expected impact: 30% reduction in signup drop-off
Step 3:基于 proposal 生成 specs
You: /opsx:continue user-registration-module
AI: Creating specs/...
✓ specs/registration.md created (delta)
Generated 8 requirements and 12 scenarios based on the proposal. Please review for accuracy.