Skill 不是一个 prompt 模板,这两件事看着一样、实则差别巨大。模板只描述"这样说话";Skill 描述"在某个场景下,我承诺我会有什么行为"。差别让它从一次性 evolves 成可被信任的复用单元。本章拆出 7 条设计原则。

21.1 原则 1:Trigger 必须可机器判别

Skill 写得再好,靠"模型自己看着办" 何时启用,迟早翻车。

# ❌ 模糊
Trigger: 用户在创造性工作时启用

# ✅ 显式
Trigger:
  - starts_with_any: ["我要加", "我需要实现", "开一个新", "新增功能"]
  - or_intent: [create_feature, modify_behavior, add_capability]
  - not_when: [reading, explaining, debugging]

判别三种实现量化(参考 superpowers 的 trigger 语法):

  1. 关键字模糊匹配:含 “实现” / “重构” / “新增”
  2. 意图分类:模型先做一次轻量分类决定 intent
  3. 元 hook:harness 在 before_user_msg / before_response 等时机注入

OpenCode 偏重 1+3 组合:BM25 关键字加权 + 用户显式 slash command。

21.2 原则 2:Skill 是契约不是文档

模板文档常常被跳读,Skill 不行——它会被作为 system prompt 注入。这意味着:SKILL.md 的每一行都被模型在自己的 context 里读。所以:

  • 用命令式而不是说明性语言
  • 用 “You MUST” 而不是 “建议”
  • 用"Avoid X. Use Y"而不是"考虑 X 和 Y"
# ❌ 文档口吻
本节描述最佳实践:思考时要 brainstorm,写代码要 TDD...

# ✅ 契约口吻
You MUST brainstorm before code.
You MUST run `pytest tests/` before claiming done.
DO NOT ship with `as any` in diff.

差别是"如果模型违反,你能识别这是违规"。可识别 = 可校验。

21.3 原则 3:分层 references,从抽象到具体

主 SKILL.md 应该短——500-2000 tokens 内描述行为契约。细节、示例、antipatterns 放 references/

my-review-skill/
├── SKILL.md              ← 主 prompt-契约
├── references/
│   ├── patterns.md       ← "好的 review 该这样写"
│   ├── antipatterns.md   ← "避免这种 review 形态"
│   └── severity.md       ← "🟥/🟧/🟨 的判定标准"
└── skills/
    └── auto-review.py    ← 可选的派生 sub-skill

主 SKILL.md 里这样 anchor references:

When reviewing, follow the rules in `references/patterns.md`.
For severity classification, see `references/severity.md`.

OpenCode 等会把 references 路径让模型主动 read——节约 context 同时让"细节按需读"。

21.4 原则 4:明确 DONE 条件

Skill 不应只描述"如何做",还要描述"何时算完"。

When you have produced:
  - 一个 spec.md 包含目标、输入、输出、约束(4 节齐全)
  - 用户对 spec 答 yes / go
then brainstorming is DONE. Proceed to implementation.

DONE 条件让"主 Agent 在 Skill 流程中"知道跳出 Skill 当下流程节点——而不是把 Skill 当成长 process 一直挂着。

21.5 原则 5:失败模式列出来

Skill 的实战可靠性来自显式列失败。Skill 不仅描述"该做",也要列"会怎么失败,再做什么":

# Failure modes
- If user says "skip brainstorm, just code":
  - Stop and write code; mark this session as `--no-brainstorm`.
  - DO NOT silently skip and continue the brainstorm anyway.

- If user provides conflicting constraints:
  - Ask one short yes/no question to disambiguate.
  - DO NOT pick one and proceed.

把"模型在该 Skill 里出错的三种历史"提前列出来——它就不会反复碰撞。

21.6 原则 6:Skill Output 是可观察的

让用户能事后知道这个 Skill 是否被执行过

# Output prefix
每次你在 brainstorm 时,回复开头写:
  `[brainstorming] ...`
让用户在 transcript 里能 grep `[brainstorming]` 判断是否触发.

在 OpenCode 会话里能 rg "\[(brainstorming|tdd|review)\]" .opencode/log/ 看本周哪些 Skill 出场多少次——让你做"哪个 Skill 沉淀得好、哪个被忽视"的复盘。

21.7 原则 7:Skill 的"配套自我反思"

最深刻的 Skill 形态——在使用结束时让模型自报:“我刚才做到了 Skill 的什么程度”:

在 brainstorm 结束时, 用一行回包:
  `[brainstorming complete] spec.md at <path>, user agree: yes/no`

如果你跳过了, 回报:
  `[brainstorming skipped] reason: <短理由>`

有自报才有改进——下次你看 Trace 就能发现"哦,brainstorm Skill 被跳过 5 次是因为用户根本是在问解释性问题",则去找改 trigger。

21.8 Skill 设计的反模式清单

反模式             | 现象                                  | 修正
─────────────────────────────────────────────────────────
过宽 trigger     | "凡是涉及代码" → 触发 99%               | 收窄到具体场景
过深细节          | 主 SKILL.md 5000 行                    | 拆 references/
无 DONE 条件     | Skill 流程跑过、没出口                 | 加 OK 判据
无失败模式     | 用户跳过时模型仍执行 → strong 武断       | 加显式 skip 路径
无自报           | 事后无法判断是否真到位                  | 要求开头 / 结尾标记
模糊命令     | "考虑" "可以" "建议"                   | "you MUST" / "DO NOT"
过深嵌套       | Skill 内嵌 Skill 内嵌 Skill → 维护噩梦   | 至多 2 层;交付用 dispatch

21.9 五个 skill 模板

21.9.1 Process Skill (brainstorming 风格)

# brainstorming/SKILL.md

<description>
MUST use before any creative work - creating features, building components,
adding functionality, or modifying behavior. Explores user intent,
requirements and design before implementation.
</description>

<trigger>
starts_with_any: ["我要加", "新增", "重构", "实现 a feature", "新增功能"]
or_intent:       [create_feature, modify_behavior]
NOT_when:        [explaining, debugging, reading]
</trigger>

<flow>
1. Interview the user (max 5 questions):
   - Target users
   - Inputs / outputs
   - Constraints
   - Acceptance criteria
   - Known failure modes
2. Write `spec.md` capturing the answers
3. Wait for user to say "go" before implementing
</flow>

<done>
spec.md exists and user explicitly says "go" / "implement"
</done>

<failure>
- User says "skip": write code, log `[brainstorming skipped: user-skip]`
- User provides conflict: ask 1 yes/no question to disambiguate
</failure>

<references>
- patterns.md
- antipatterns.md
</references>

21.9.2 Reference Skill (security checklist)

# pre-commit-safety/SKILL.md
Use before claiming work is complete.

<trigger>
intent: commit_and_pr
keywords: ["commit", "提 PR", "推上"]
</trigger>

<flow>
1. Run `detect-secrets scan`
2. Run `bandit -r src/` or `semgrep scan`
3. Run `pytest -x`
4. Any failure → stop, report, do not proceed
</flow>

21.9.3 Tool Skill (parallel dispatch)

# multi-explore/SKILL.md
MUST use when facing 2+ independent discovery tasks.

<trigger>
intent: parallel_search
</trigger>

<flow>
For each task, dispatch `explore` subagent:
  task(subagent_type="explore", prompt="...")
Await `asyncio.gather`. Merge results to single report.
</flow>

21.10 写 Skill 的协作机制

Skill 不只你自己写:

  • 团队 review Skill 时,把 SKILL.md 当 source code review,两条 reviewer 你要请他们评:契约清晰否、失败模式齐否
  • 改写已有 Skill 时记录 diff(git follow .opencode/skills/),加 commit 标签 skill-update
  • 每月一次 “skill audit”:grep log 看 Skill 实际被触发率、被跳过率 → 调 trigger

21.11 小结

  • Skill = 契约不是文档;用 You MUST / DO NOT / 不用"建议 / 考虑"
  • 7 原则:可判 trigger / 契约口吻 / 分层 references / 明确 DONE / 列失败 / Output 可观察 / 自反思
  • Skill 模板三种:Process / Reference / Tool
  • 团队协作当作 source code review,定期 audit

下一篇:《22 编写第一个 Skill》——实战起来,从 0 到 1 写一个能在团队 install 的 Skill。

Summary: Skill 设计 7 原则:可判 trigger / 契约口吻 / 分层 references / 明确 DONE / 列失败 / 可观察 Output / 自反思。