skills/writing-skills/SKILL.md
将 TDD 方法论应用于流程文档:先用压力场景测试基线行为(RED)→写技能文档使代理合规(GREEN)→收紧漏洞(REFACTOR)。触发:创建/编辑 SKILL.md、部署前验证技能有效性。不适用:一次性解决方案、广泛文档化的标准实践、项目特定约定(放 AGENTS.md)、可用正则/验证强制执行的机械约束。铁律:没有先写失败测试就没有技能文档。
npx skillsauth add Chikage0o0/opencode writing-skillsInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
编写技能是将测试驱动开发(TDD)应用于流程文档。
项目技能存放在 .opencode/skills/ 中,任何共享库都应通过当前 OpenCode 环境指令暴露。
你编写测试用例(使用子代理的压力场景),观察它们失败(基线行为),编写技能(文档),观察测试通过(代理合规),然后重构(关闭漏洞)。
**核心原则:**如果你没有观察过代理在没有技能的情况下失败,你就不知道这个技能是否教会了正确的东西。
**必需背景:**在使用此技能之前,你必须理解 test-driven-development。该技能定义了基本的 RED-GREEN-REFACTOR 循环。此技能将 TDD 适配到文档中。
**官方指南:**有关 Anthropic 的官方技能编写最佳实践,请参阅 anthropic-best-practices.md。本文档提供了额外的模式和指南,以补充此技能中以 TDD 为重点的方法。
技能是针对已验证技术、模式或工具的参考指南。技能帮助未来的 OpenCode 会话找到并应用有效的方法。
**技能是:**可重用的技术、模式、工具、参考指南
**技能不是:**关于你如何一次性解决某个问题的叙述
| TDD 概念 | 技能创建 | |---------|---------| | 测试用例 | 使用子代理的压力场景 | | 生产代码 | 技能文档(SKILL.md) | | 测试失败(RED) | 代理在没有技能的情况下违反规则(基线) | | 测试通过(GREEN) | 代理在有技能时合规 | | 重构 | 在保持合规的同时关闭漏洞 | | 先写测试 | 在编写技能之前运行基线场景 | | 观察失败 | 记录代理使用的准确辩解 | | 最小化代码 | 编写解决这些特定违规的技能 | | 观察通过 | 验证代理现在合规 | | 重构循环 | 发现新的辩解 → 堵塞 → 重新验证 |
整个技能创建过程遵循 RED-GREEN-REFACTOR。
创建时机:
不要为以下情况创建:
AGENTS.md)具有可遵循步骤的具体方法(condition-based-waiting、root-cause-tracing)
思考问题的方式(flatten-with-flags、test-invariants)
API 文档、语法指南、工具文档(office 文档)
skills/
skill-name/
SKILL.md # Main reference (required)
supporting-file.* # Only if needed
扁平命名空间 - 所有技能都在一个可搜索的命名空间中
单独文件用于:
保持内联:
Frontmatter(YAML):
name 和 description(有关所有支持的字段,请参阅 agentskills.io/specification)name:仅使用字母、数字和连字符(无括号、特殊字符)description:第三人称,仅描述何时使用(不是做什么的)
---
name: Skill-Name-With-Hyphens
description: Use when [specific triggering conditions and symptoms]
---
# Skill Name
## Overview
What is this? Core principle in 1-2 sentences.
## When to Use
[Small inline flowchart IF decision non-obvious]
Bullet list with SYMPTOMS and use cases
When NOT to use
## Core Pattern (for techniques/patterns)
Before/after code comparison
## Quick Reference
Table or bullets for scanning common operations
## Implementation
Inline code for simple patterns
Link to file for heavy reference or reusable tools
## Common Mistakes
What goes wrong + fixes
## Real-World Impact (optional)
Concrete results
**发现的关键:**未来的 OpenCode 会话需要找到你的技能
**目的:**OpenCode 读取描述元数据以决定为给定任务加载哪些技能。让它回答:"我现在应该阅读这个技能吗?"
**格式:**以 "Use when..." 开头,聚焦于触发条件
关键:描述 = 何时使用,不是技能做什么
描述应该仅描述触发条件。不要在描述中总结技能的过程或工作流。
**为什么这很重要:**测试揭示,当描述总结技能的工作流时,代理可能会遵循描述而不是阅读完整的技能内容。一个说"任务之间的代码审查"的描述导致代理只做一次审查,尽管技能的流程图清楚地显示了两个审查(规范合规然后代码质量)。
当描述更改为只是 "Use when executing implementation plans with independent tasks"(无工作流总结)时,代理正确阅读了流程图并遵循了两阶段审查过程。
**陷阱:**总结工作流的描述会创建一个代理会采取的捷径。技能主体变成了它跳过的文档。
# ❌ BAD: Summarizes workflow - the agent may follow this instead of reading skill
description: Use when executing plans - dispatches subagent per task with code review between tasks
# ❌ BAD: Too much process detail
description: Use for TDD - write test first, watch it fail, write minimal code, refactor
# ✅ GOOD: Just triggering conditions, no workflow summary
description: Use when executing implementation plans with independent tasks in the current session
# ✅ GOOD: Triggering conditions only
description: Use when implementing any feature or bugfix, before writing implementation code
内容:
# ❌ BAD: Too abstract, vague, doesn't include when to use
description: For async testing
# ❌ BAD: First person
description: I can help you with async tests when they're flaky
# ❌ BAD: Mentions technology but skill isn't specific to it
description: Use when tests use setTimeout/sleep and are flaky
# ✅ GOOD: Starts with "Use when", describes problem, no workflow
description: Use when tests have race conditions, timing dependencies, or pass/fail inconsistently
# ✅ GOOD: Technology-specific skill with explicit trigger
description: Use when using React Router and handling authentication redirects
使用 OpenCode 会搜索的词:
使用主动语态,动词优先:
creating-skills 而不是 skill-creationcondition-based-waiting 而不是 async-test-helpers**问题:**getting-started 和频繁引用的技能会加载到每个对话中。每个 token 都很重要。
目标字数:
技巧:
将详细信息移至工具帮助:
# ❌ BAD: Document all flags in SKILL.md
search-conversations supports --text, --both, --after DATE, --before DATE, --limit N
# ✅ GOOD: Reference --help
search-conversations supports multiple modes and filters. Run --help for details.
使用交叉引用:
# ❌ BAD: Repeat workflow details
When searching, dispatch subagent with template...
[20 lines of repeated instructions]
# ✅ GOOD: Reference other skill
Always use subagents (50-100x context savings). REQUIRED: Use [other-skill-name] for workflow.
压缩示例:
# ❌ BAD: Verbose example (42 words)
your human partner: "How did we handle authentication errors in React Router before?"
You: I'll search past conversations for React Router authentication patterns.
[Dispatch subagent with search query: "React Router authentication error handling 401"]
# ✅ GOOD: Minimal example (20 words)
Partner: "How did we handle auth errors in React Router?"
You: Searching...
[Dispatch subagent → synthesis]
消除冗余:
验证:
wc -w skills/path/SKILL.md
# getting-started workflows: aim for <150 each
# Other frequently-loaded: aim for <200 total
根据你做什么或核心洞察来命名:
condition-based-waiting > async-test-helpersusing-skills 而不是 skill-usageflatten-with-flags > data-structure-refactoringroot-cause-tracing > debugging-techniques动名词(-ing)适用于过程:
creating-skills、testing-skills、debugging-with-logs编写引用其他技能的文档时:
仅使用技能名称,并带有明确的要求标记:
**REQUIRED SUB-SKILL:** Use test-driven-development**REQUIRED BACKGROUND:** You MUST understand systematic-debuggingSee skills/testing/test-driven-development(不清楚是否必需)@skills/testing/test-driven-development/SKILL.md(强制加载,消耗上下文)为什么不用 @ 链接:@ 语法会立即强制加载文件,在你需要它们之前消耗 200k+ 上下文。
flowchart TD
A{需要展示信息?} -->|是| B{可能出错的决策?}
B -->|是| C[小型内联流程图]
B -->|否| D[使用 Markdown]
仅对以下内容使用流程图:
永远不要对以下内容使用流程图:
Mermaid 流程图规范:
flowchart TD(自上而下)或 flowchart LR(从左到右){},处理节点用 [],终态节点用 (())style nodeId fill:颜色-->|标签| 语法为你的合作伙伴可视化: Mermaid 图表可在 GitHub/GitLab 中直接渲染,也可使用 mmdc 命令行工具导出为 SVG:
mmdc -i input.mmd -o output.svg
一个优秀的示例胜过许多平庸的示例
选择最相关的语言:
好的示例:
不要:
你很擅长移植——一个优秀的示例就足够了。
defense-in-depth/
SKILL.md # Everything inline
何时:所有内容都适合,不需要大量参考
condition-based-waiting/
SKILL.md # Overview + patterns
example.ts # Working helpers to adapt
何时:工具是可重用代码,不仅仅是叙述
pptx/
SKILL.md # Overview + workflows
pptxgenjs.md # 600 lines API reference
ooxml.md # 500 lines XML structure
scripts/ # Executable tools
何时:参考材料太大,无法内联
NO SKILL WITHOUT A FAILING TEST FIRST
这适用于新技能和现有技能的编辑。
在测试之前编写技能?删除它。重新开始。 未经测试就编辑技能?同样的违规。
没有例外:
必需背景:test-driven-development 技能解释了为什么这很重要。相同的原则适用于文档。
不同类型的技能需要不同的测试方法:
**示例:**TDD、verification-before-completion、designing-before-coding
使用以下方式测试:
**成功标准:**代理在最大压力下遵循规则
**示例:**condition-based-waiting、root-cause-tracing、defensive-programming
使用以下方式测试:
**成功标准:**代理成功将技术应用于新场景
**示例:**reducing-complexity、information-hiding 概念
使用以下方式测试:
**成功标准:**代理正确识别何时/如何应用模式
**示例:**API 文档、命令参考、库指南
使用以下方式测试:
**成功标准:**代理找到并正确应用参考信息
| 辩解 | 现实 | |--------|---------| | "Skill is obviously clear" | 对你清楚 ≠ 对其他代理清楚。测试它。 | | "It's just a reference" | 参考可能有缺口、不清晰的章节。测试检索。 | | "Testing is overkill" | 未测试的技能总是有问题。15 分钟测试节省数小时。 | | "I'll test if problems emerge" | 问题 = 代理无法使用技能。在部署之前测试。 | | "Too tedious to test" | 测试比在生产中调试坏技能不那么乏味。 | | "I'm confident it's good" | 过度自信保证会有问题。还是要测试。 | | "Academic review is enough" | 阅读 ≠ 使用。测试应用场景。 | | "No time to test" | 部署未测试的技能会浪费更多时间以后修复。 |
所有这些都意味着:部署之前测试。没有例外。
执行纪律的技能(如 TDD)需要抵抗合理化。代理很聪明,在压力下会找到漏洞。
**心理学注释:**理解为什么说服技巧有效有助于你系统地应用它们。请参阅 persuasion-principles.md 了解关于权威、承诺、稀缺、社会认同和团结原则的研究基础(Cialdini,2021;Meincke 等,2025)。
不要只陈述规则——禁止特定的变通方法:
<Bad> ```markdown Write code before test? Delete it. ``` </Bad> <Good> ```markdown Write code before test? Delete it. Start over.No exceptions:
</Good>
### 处理"精神 vs 字面"的争论
尽早添加基础原则:
```markdown
**Violating the letter of the rules is violating the spirit of the rules.**
这切断了一整类"我遵循的是精神"的辩解。
从基线测试中捕获辩解(见下面的测试部分)。代理提出的每个借口都放入表中:
| Excuse | Reality |
|--------|---------|
| "Too simple to test" | Simple code breaks. Test takes 30 seconds. |
| "I'll test after" | Tests passing immediately prove nothing. |
| "Tests after achieve same goals" | Tests-after = "what does this do?" Tests-first = "what should this do?" |
让代理在合理化时容易自我检查:
## Red Flags - STOP and Start Over
- Code before test
- "I already manually tested it"
- "Tests after achieve the same purpose"
- "It's about spirit not ritual"
- "This is different because..."
**All of these mean: Delete code. Start over with TDD.**
添加到描述:当你即将违反规则时的症状:
description: use when implementing any feature or bugfix, before writing implementation code
遵循 TDD 循环:
在没有技能的情况下使用子代理运行压力场景。记录准确的行为:
这就是"观察测试失败"——在编写技能之前,你必须看到代理自然会做什么。
编写解决这些特定辩解的技能。不要为假设情况添加额外内容。
在有技能的情况下运行相同场景。代理现在应该合规。
代理发现了新的辩解?添加明确的反驳。重新测试直到无懈可击。
**测试方法论:**请参阅 @testing-skills-with-subagents.md 了解完整的测试方法论:
"In session 2025-10-03, we found empty projectDir caused..." **为什么不好:**太具体,不可重用
example-js.js, example-py.py, example-go.go **为什么不好:**质量平庸,维护负担
flowchart LR
A[import fs] --> B[read file]
**为什么不好:**无法复制粘贴,难以阅读
helper1, helper2, step3, pattern4 **为什么不好:**标签应具有语义意义
编写任何技能后,你必须停止并完成部署过程。
不要:
下面的部署清单对每个技能都是强制性的。
部署未测试的技能 = 部署未测试的代码。这是违反质量标准的。
重要:使用 TodoWrite 为下面的每个清单项创建待办事项。
RED 阶段 - 编写失败测试:
GREEN 阶段 - 编写最小化技能:
name 和 description 字段(最多 1024 个字符;请参阅 spec)REFACTOR 阶段 - 关闭漏洞:
质量检查:
部署:
未来的 OpenCode 会话如何找到你的技能:
为此流程优化 - 尽早并频繁地放置可搜索的术语。
创建技能就是流程文档的 TDD。
相同的铁律:没有先失败的测试就没有技能。 相同的循环:RED(基线)→ GREEN(编写技能)→ REFACTOR(关闭漏洞)。 相同的好处:更好的质量、更少的意外、无懈可击的结果。
如果你遵循代码的 TDD,也请为技能遵循它。这是应用于文档的相同纪律。
data-ai
Sync delta specs from a change to main specs. Use when the user wants to update main specs with changes from a delta spec, without archiving the change.
development
Create new agent skills with proper structure, progressive disclosure, and bundled resources. Use when user wants to create, write, or build a new skill.
development
Test-driven development with red-green-refactor loop. Use when user wants to build features or fix bugs using TDD, mentions "red-green-refactor", wants integration tests, or asks for test-first development.
development
Simplifies code for clarity without changing behavior. Use for readability, maintainability, and complexity reduction after behavior is understood.