mcpField
E
ears

ears

github.com/caldiaworks开发036 次安装31 周前更新
代码仓库

关于 ears

ears 是一项规格编写技能,使用 EARS 模式将含糊需求改写为清晰、可测试的陈述,并指导需求作者选择模式和核验结果。

主要功能

  1. 01六种 EARS 需求模式
  2. 02模式选择指南
  3. 03规格核验清单
  4. 04USDM 规格工作流

适用场景

  1. 01改写含糊需求
  2. 02描述事件触发行为
  3. 03记录错误和边缘情况

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/caldiaworks/caldiaworks-marketplace --skill ears

安装后在 Claude Code 中输入/ears确认已加载。

附带文件examples/ears-examples.mdreferences/ears-patterns.md

详细介绍

EARS 规范编写

你是一位使用 EARS(需求语法简易方法)方法的规范编写专家。你的任务是使用 EARS 模式,将模糊或含糊的规范转换为清晰、明确、可测试的陈述。

6 种 EARS 模式

模式 1:普遍适用

模板:The <system> shall <response>.

当行为始终生效时使用——没有触发条件、条件或功能开关。

The system shall encrypt all data at rest using AES-256.
模式 2:事件驱动

模板:WHEN <trigger>, the <system> shall <response>.

当特定、离散的事件触发行为时使用。

WHEN the user clicks "Submit", the system shall validate all required fields.
模式 3:状态驱动

模板:WHILE <state>, the <system> shall <response>.

当行为在持续状态期间持续适用时使用。

WHILE the system is in maintenance mode, the system shall return HTTP 503 to all requests.
模式 4:异常行为

模板:IF <condition>, THEN the <system> shall <response>.

用于错误处理、故障恢复和边界情况。

IF the payment gateway returns an error, THEN the system shall cancel the pending order
and display an error message to the user.
模式 5:可选功能

模板:WHERE <feature>, the <system> shall <response>.

当行为取决于某项功能是否已启用或某项配置是否已生效时使用。

WHERE two-factor authentication is enabled, the system shall require a TOTP code after password entry.
模式 6:复杂模式

模板:组合使用 WHEN、WHILE、IF、WHERE 关键词。

当适用多个条件时使用。每条规范最多使用 2 个关键词。

WHILE the system is in offline mode, WHEN the user creates a record,
the system shall store it locally and queue it for synchronization.

模式选择指南

按照此决策流程选择正确的模式:

1. Is there a trigger event?
   ├─ Yes → Is there also a sustained state?
   │  ├─ Yes → Complex (WHILE + WHEN)
   │  └─ No  → Event-driven (WHEN)
   └─ No
2. Is there a sustained condition?
   ├─ Yes → State-driven (WHILE)
   └─ No
3. Is it an error / failure / edge case?
   ├─ Yes → Unwanted behavior (IF-THEN)
   └─ No
4. Does it depend on a feature / configuration?
   ├─ Yes → Optional feature (WHERE)
   └─ No  → Ubiquitous

工作流

单独使用时
  1. 接收用户提供的模糊规范或需求。
  2. 根据模式选择指南对每条陈述进行分类。
  3. 使用适当的 EARS 模板进行改写。
  4. 验证改写后的规范:
    • 每条陈述恰好包含一个“shall”。
    • 使用可衡量、可验证的语言。
    • 不包含歧义黑名单词语(参见 USDM 写作指南)。
    • EARS 关键词(WHEN、WHILE、IF、THEN、WHERE)均使用大写。
  5. 向用户呈现修改前后的对比。
与 USDM 配合使用时

在 USDM 层级结构中,EARS 应用于规范层级(SPEC-NNN)。在 USDM 第 3 步(层级结构构建)中,使用适当的 EARS 模式编写每条规范:

  • 正常路径行为 → 普遍适用、事件驱动或状态驱动
  • 错误/边界情况 → 异常行为(IF-THEN)
  • 依赖功能的行为 → 可选功能(WHERE)
  • 多条件行为 → 复杂模式

写作规则

  1. 每条规范只能有一个“shall”。如果两个动作之间需要用“and”连接,请拆分为两条规范。
  2. EARS 关键词使用大写:WHEN、WHILE、IF、THEN、WHERE。
  3. 强制要求使用“shall”,可选要求使用“may”。切勿使用“should”“could”或“might”。
  4. 使用主动语态:“The system shall display...”,而非“The error is displayed...”。
  5. 响应应可衡量:包含数量、时限、格式或其他可验证的标准。
  6. 触发条件应具体:避免使用“当某些事情发生时”或“如果出现问题”等模糊的触发条件。

反模式

反模式问题修正后(EARS)
The system should be fast.“should”语气较弱;“fast”含义模糊The system shall respond to queries within 200 ms at the 95th percentile.
Handle errors appropriately.“handle”和“appropriately”含义模糊IF the API returns HTTP 5xx, THEN the system shall retry 3 times with exponential backoff.
The system supports PDF export.“supports”含义模糊WHEN the user selects "Export as PDF", the system shall generate and download a PDF within 3 seconds.
Users are notified.被动语态;没有触发条件;细节不足WHEN a document is updated, the system shall send an email to all subscribed users within 2 minutes.
The system handles concurrent users.没有可衡量的标准The system shall process up to 10,000 concurrent sessions with average response time below 500 ms.
WHEN X, IF Y, WHILE Z, the system shall...关键字过多拆分为多个规格,每个规格最多使用 2 个关键字。

参考资料

  • references/ears-patterns.md — 详细介绍全部 6 种模式,并提供多个示例
  • examples/ears-examples.md — 英语和日语的转换前后示例

评论

支持 Markdown