Community编程与开发github.com

约束驱动开发

将项目的质量标准作为书面契约确立,并阻止 AI 智能体悄悄降低标准。通过访谈用户了解哪些维度重要,当用户没有具体数字时为提供合理的默认阈值,将所有内容记录在 CONSTRAINTS.md 中,并在差异中监控标准是否被削弱——如新的 @ts-ignore 或 eslint-disable 抑制、跳过或删除的测试、被剥离的断言、未实现的存根以及阈值被调低等。

约束驱动开发 是什么?

该技能名为“约束驱动开发”,旨在解决 AI 辅助开发中普遍存在的质量问题:AI 智能体可能产出大量代码,但人类开发者无力逐行审阅,导致标准无形中降低。其核心是一套结构化流程,首先通过简短访谈(四问四答,每题含合理默认值)收集用户最关心的质量维度(如测试覆盖率、性能预算、可访问性等),然后生成一个名为 `CONSTRAINTS.md` 的活文档,以表格形式明确列出每个维度的具体阈值、检查工具和执行时机。该技能定义了“底线”(如禁止新增 `@ts-ignore` 等)和可选的“维度”,并自动将对应的检测命令(如 `tsc --noEmit`、`biome check`、`gitleaks` 等)集成到项目的 npm 脚本中。最重要的设计是它通过 `git diff` 在代码审查时主动监控五种常见的“标准削弱”手段(阈值调低、测试变易、检查被静默、工作未完成、异常引入),确保标准只升不降。该技能适用于所有主流 AI 编码工具(Claude Code、Cursor、Codex 等),因为它仅依赖标准的 markdown 文件和工具链。

兼容平台✓Claude Code✓Codex CLI~Cursor
npx skills add https://github.com/alfathafp/portofolio-web-design/tree/HEAD/.agents/skills/constraint-driven-development

Installed? Explore more 编程与开发 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

约束驱动开发 是做什么的?

概述

本工具包中的其他技能描述了“什么是好的”。code-review-and-quality 为你提供了五个评估维度。test-driven-development 为你提供了一个开发循环。security-and-hardening 为你提供了一份威胁清单。所有这一切都存在于智能体可能遵循也可能不遵循的文本描述中,并且没有一个能在会话结束后幸存下来。

本技能产生了一些不同的东西:一份关于本项目标准的、带有具体数字的书面记录,它比对话本身更持久,并且可以被机械地检查。

这一点很重要。当你编写代码时,阅读代码就能告诉你它是否合格。一个智能体一下午写的代码比你一周能读的还多,因此判断权就从你的头脑中移出,转移到在开发循环中运行的检查中。这些检查需要存在,它们需要你真正选择的数字,并且它们需要足够接近工作发生的地方,以便智能体修正自己的输出。

规范驱动开发告诉你要构建什么。测试驱动开发证明它能工作。约束驱动开发定义了“好到可以发布”意味着什么,在任何人于 Pull Request 中争论之前。

何时使用

在以下情况应用本技能:

  • 开始一个新项目或重要特性,且尚未设定质量标准
  • 用户要求“设置约束”、“添加质量门禁”、“定义我们的标准”或“阻止智能体发布垃圾代码”
  • 智能体产出大量代码,没有人会逐行阅读
  • CI 中有检查,但没人能说清哪些会阻止合并,哪些只是装饰
  • 覆盖率、性能或可访问性的数字在每个 PR 中都被争论,而不是一次性决定
  • 你即将运行 /build auto 或任何自主开发循环,而此时唯一阻挡它和主分支之间的是智能体自己编写的测试套件

何时不使用:

  • 项目已有 CONSTRAINTS.md 且用户未修改——直接阅读并遵循它
  • 一次性脚本、技术原型、可丢弃的原型
  • 用户现在需要代码审查(code-review-and-quality)或构建 CI 流水线(ci-cd-and-automation)
  • 预计生命周期仅两周的、尚未找到产品市场契合的代码——下面的底线仍然值得设置,其余则不必

加载约束

访谈需要一个活跃的用户。不要在非交互式上下文中运行它(CI 环境、/loop、自主运行)。如果缺少约束且你处于这些上下文中,则应用下面的“底线”,记录你的操作,并将其余部分标记给人工处理。

流程

步骤 1:在询问之前先检测

永远不要问你能读到的东西。在提出第一个问题之前,先收集以下信息:

内容查看位置
语言和栈package.json, pyproject.toml, go.mod, Cargo.toml
测试运行器开发依赖,test 脚本,现有测试文件
现有的代码检查工具eslint.config.*, biome.json, .ruff.toml
当前覆盖率coverage/ 输出,或运行一次测试套件
CI 配置.github/workflows/, .gitlab-ci.yml
智能体配置.claude/, .codex/, AGENTS.md

用两行报告你发现的内容,然后只询问剩余的部分。

步骤 2:四个问题,每个都带默认值

遵循 interview-me 中的“一次一问”纪律,但有一个变化:这里的每个问题都有一个默认值,因此“我不知道”也是一个完整的回答,仍然可以产生一个可行的配置。

Q1:除了底线,以下哪些你想要强制执行?
    (a) 新代码的测试覆盖率
    (b) 安全扫描
    (c) 性能预算
    (d) 可访问性
    (e) 架构边界
猜测: (a) 和 (b)——你已经有一个测试运行器并且正在处理用户输入。
如果不确定则使用默认值: (a) 和 (b)。
说明每个选择带来的成本: (c) 和 (d) 需要一个正在运行的 URL,(e) 需要编写规则文件。
Q2:当智能体在任务中途时检查失败,应该阻止还是发出警告?
猜测:阻止。你在无人看管的情况下运行智能体,而没人阅读的警告就等于没有警告。
如果不确定则使用默认值:前两周,对底线阻止,对其他一切发出警告。
Q3:你心中有目标数字,还是应该测量当前状态并保持该标准?
猜测:测量。大多数团队没有具体数字,而凭空捏造的数字会被忽略。
如果不确定则使用默认值:测量并保持。参见下面的“棘轮”。
Q4:在智能体交回工作之前,你能容忍的最慢检查是多久?
猜测:大约 90 秒。再长的话,你就不想运行它了。
如果不确定则使用默认值:任务结束时 90 秒,CI 中无限制。

只问四个。十二个问题的访谈会产生一个没人能理解的配置和一个后悔开始的用户。

步骤 3:编写 CONSTRAINTS.md

在仓库根目录创建一个文件。任何智能体在任何框架下都能读取它,对其的修改也会出现在审查中,这正是它应该存在的地方。

# 约束 (Constraints)

最后审查:2026-08-08 由 @addy

## 底线(始终强制执行,无需设置)

- 无新的抑制注释:`@ts-ignore`, `eslint-disable`, `# noqa`, `# type: ignore`
- 无未实现的存根:`throw new Error("Not implemented")`, 空的 `catch {}`
- 无跳过或删除的测试(除非在提交信息中说明了原因)
- 源代码中无密钥
- 此文件绝不会为了通过某个变更而被削弱

## 强制执行(带数字)

| 维度 | 规则 | 检查工具 | 执行时机 |
|-----------|------|-----------|---------|
| 类型 | 零类型错误 | `tsc --noEmit` | 每次编辑后 |
| 代码检查 | 根据我们的配置,零错误 | `biome check` | 每次编辑后 |
| 密钥 | 源代码中无密钥 | `gitleaks detect --redact` | 每次编辑后 |
| 覆盖率 | 变更行 ≥ 80% 覆盖率 | `vitest run --coverage` + git diff | 任务结束时, CI |
| 安全:代码 | 无高风险发现 | `semgrep scan --config p/default` | CI |
| 安全:依赖 | 无高或以上风险 | `osv-scanner scan source -r .` | CI |
| 可访问性 | 零严重或重要问题 | `axe $PREVIEW_URL --tags wcag2a,wcag2aa,wcag21aa` | 预览部署时 |
| 性能 | LCP ≤ 2500ms, CLS ≤ 0.1 | `lighthouse $PREVIEW_URL --output=json` | 预览部署时 |

每一行都指明了产生判决结果的命令。如果一个维度有数字但在本列中没有命令,那它只是一个愿望,而不是一个约束。

## 已测量,尚未强制执行

| 指标 | 今天 | 方向 |
|--------|-------|-----------|
| 项目覆盖率 | 62.4% | 不得下降 |
| 打包大小 (main) | 184 kB | 不得增长 |

## 例外

| ID | 规则 | 路径 | 原因 | 负责人 | 过期时间 |
|----|------|------|--------|-------|---------|
| W1 | `no-explicit-any` | `src/legacy/**` | 重写工作由 ENG-441 跟踪 | @addy | 2026-11-01 |

然后在 AGENTS.md 和 CLAUDE.md 中添加一行:Read CONSTRAINTS.md before writing code. Do not weaken it to make a change pass.

步骤 4:为每个维度安装所需工具

选择一个维度意味着安装一些东西。不要只给用户一个数字而没有实现机制,也不要凭空发明自己的检查器,当业界已经有事实上的标准工具时——列出这些工具是因为它们的规则格式和阈值是生态系统中其他所有工具的目标,因此团队现有的配置可以继续使用。

维度工具安装运行门禁条件
类型 (TS)tsc已存在tsc --noEmit任何错误
类型 (Python)mypypip install mypymypy .任何错误
代码检查你现有的配置已存在eslint . / biome check / ruff check任何错误
覆盖率 (JS)你的测试运行器已存在vitest run --coverage (或 jest --coverage)变更行的覆盖率
覆盖率 (Python)pytest-covpip install pytest-covpytest --cov --cov-report=lcov同上
安全:代码Semgreppipx install semgrepsemgrep scan --config p/default --config p/owasp-top-ten任何高风险发现
安全:密钥gitleaksbrew install gitleaksgitleaks detect --redact --no-banner任何发现
安全:依赖osv-scannerbrew install osv-scannerosv-scanner scan source -r .高或以上风险
性能:页面Lighthousenpm i -D lighthouselighthouse $URL --output=json --quietLCP, CLS, 性能分数
性能:打包size-limitnpm i -D size-limitsize-limit --json每个入口的字节预算
可访问性axe-corenpm i -D @axe-core/cliaxe $URL --tags wcag2a,wcag2aa,wcag21aa零严重或重要问题
架构dependency-cruisernpm i -D dependency-cruiserdepcruise --validate src任何违规
断言质量Strykernpm i -D @stryker-mutator/corestryker run --mutate <changed files>突变测试分数

有五件事如果跳过会给你带来麻烦:

  1. gitleaks 的 --redact 不是可选的。 如果没有它,匹配到的密钥会出现在智能体的记录中,这就是泄露的密钥出现在日志、摘要或提交信息中的方式。报告规则和位置,永远不要报告值。
  2. Lighthouse 和 axe 需要一个 URL。 它们只对正在运行的应用程序有效,因此它们属于运行时阶段,针对预览部署或你首先启动的本地服务器。如果项目没有可以访问的 URL——如 CLI 工具、库、桌面应用——请说明情况并放弃该维度,而不是凭空创造无法运行的检查。
  3. 将代价高昂的检查范围限制在差异上。 对整个仓库运行 stryker run --mutate 需要数小时并且最终会被关闭;只对变更影响的文件运行则不到一分钟。Semgrep 也是如此,它接受路径列表。
  4. 覆盖率无需第二次运行测试。 读取你的套件已经生成的 lcov 报告,并用 git diff 取交集。为了得到一个数字而运行两次测试套件是让人们讨厌这个做法的最快途径。
  5. Semgrep 的注册表规则可以免费运行;但在重新分发之前请检查许可证。 如果你的法务团队在意,opengrep 是一个即插即用的分支,具有相同的规则格式和 JSON 输出。

将每个工具添加到项目自身的脚本中,以便在没有智能体的情况下也可重现:

{
  "scripts": {
    "check:fast": "tsc --noEmit && eslint . && gitleaks detect --redact --no-banner",
    "check:task": "npm run check:fast && vitest run --coverage",
    "check:full": "npm run check:task && semgrep scan --config p/default && osv-scanner scan source -r ."
  }
}

这个映射关系比工具本身更重要。check:fast 在每次编辑后运行,check:task 在智能体认为完成时运行,check:full 在 CI 中运行。

这些命令现在存在于两个地方——CONSTRAINTS.md 中的“检查工具”列和这些脚本中。CONSTRAINTS.md 是权威来源:它承载了每个命令旁边的理由,并且在审查中可见。脚本是方便使用的包装器,必须镜像它,而不是第二个真相来源;如果它们出现偏差,以文件为准。

步骤 5:将其接入开发生命周期

最大的错误是在所有地方运行所有检查。一个会拖慢智能体的检查最终会被关闭,而一个被关闭的门禁比没有门禁更糟糕,因为质量标准看起来仍然存在。

阶段命令运行内容预算
构建/build类型、代码检查、密钥、底线5 秒内,仅变更的文件
验证/test相关测试,变更行的覆盖率90 秒内
审查/review所有内容,加上下面的守卫分钟级
发布/ship方向检查,无回归CI

两条规则让其保持可容忍:

  1. 范围限制在差异上。 检查此变更触碰的行,而不是整个仓库。变更行的覆盖率是智能体可以影响的数字;项目覆盖率是它继承的。
  2. 成本决定位置。 任何超过几秒的检查都应移出编辑循环。对整个仓库进行突变测试需要数小时;对变更影响的文件进行则不到一分钟,这决定了检查是人们会运行还是不会运行。

步骤 6:守护标准本身

会有人指出,如果智能体既写代码又写检查,那么检查就证明不了什么。说对了一半,值得为此进行工程化预防。

智能体不会精心设计巧妙的漏洞。它们遇到一个红色检查,然后走最便宜的路让它变绿。在审查时,通过差异(diff)留意以下五种行为:

  1. 阈值移动了。 预算降低、严重性下调、检查从快速阶段移除。将 CONSTRAINTS.md 与其在分支点时的状态进行比较。
  2. 测试变容易了。 添加了 .skip、删除了测试文件、从仍然存在的测试中剥离了断言。
  3. 检查器被静默了。 新增的 @ts-ignore 或 eslint-disable。四个抑制方式值得特别关注,因为它们关闭了你在依赖的检查:istanbul ignore 将代码从覆盖率中排除而不是测试它,Stryker disable 隐藏了一个幸存的突变,nosemgrep 和 gitleaks:allow 则针对安全发现。
  4. 工作未完成。 抛出异常的存根,将失败转化为静默的空 catch,替换实现的 TODO。
  5. 出现了例外。 没有人讨论过的“例外”表中的新行。

所有这些都不需要除 git diff 之外的工具。收紧标准应该是无声的;放松标准应该是响亮的。

与有编号的维度不同,底线没有自己的事实标准工具,因此被要求强制实施底线的智能体倾向于从头编写一个检查器,而两个智能体会写出两个不同的检查器。这些五项检查的参考实现随本技能一起提供,位于 references/floor-guard.md(差异范围,退出码 0/1/2,模式可根据生态系统调整)。调整该实现而不是重新发明轮子,原因与每个维度都命名一个事实标准工具相同:以便跨运行和跨栈的机制保持一致。

并非所有检查都是同等循环的。 通过一个问题对它们进行排名:智能体能否通过编写不起作用的代码来通过这些检查?

  • 外部——axe-core 编码了 WCAG,osv-scanner 读取漏洞数据库,Lighthouse 在真实浏览器中测量。智能体无法与这些争论。
  • 项目——你的代码检查规则,你的层边界。文件由人拥有。
  • 套件——你自己的测试。最有用,也是唯一真正循环的。

一个质量标准

…

[截断以供翻译]

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

alfathafp/portofolio-web-design

You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).

alfathafp/portofolio-web-design

Tests in real browsers via Chrome DevTools MCP. Use when building or debugging anything that runs in a browser. Use when you need to inspect the DOM, capture console errors, analyze network requests, profile performance, or verify visual output with real runtime data. Requires the chrome-devtools MCP server to be configured.

alfathafp/portofolio-web-design

Automates CI/CD pipeline setup. Use when setting up or modifying build and deployment pipelines. Use when you need to automate quality gates, configure test runners in CI, or establish deployment strategies.

alfathafp/portofolio-web-design

Conducts multi-axis code review. Use before merging any change. Use when reviewing code written by yourself, another agent, or a human. Use when you need to assess code quality across multiple dimensions before it enters the main branch. Use when asked to review a diff or a pull request, even when the diff is pasted inline.

alfathafp/portofolio-web-design

Simplifies code for clarity. Use when refactoring code for clarity without changing behavior. Use when code works but is harder to read, maintain, or extend than it should be. Use when reviewing code that has accumulated unnecessary complexity.

alfathafp/portofolio-web-design

Optimizes agent context setup. Use when starting a new session, when agent output quality degrades, when switching between tasks, or when you need to configure rules files and context for a project.

alfathafp/portofolio-web-design

Guides systematic root-cause debugging. Use when tests fail, builds break, something that worked yesterday broke, behavior doesn't match expectations, or you encounter any unexpected error. Use when you need to figure out what broke and why — a systematic approach to finding and fixing the root cause rather than guessing.

alfathafp/portofolio-web-design

Deploy applications and websites to Vercel. Use when the user requests deployment actions like "deploy my app", "deploy and give me the link", "push this live", or "create a preview deployment".

alfathafp/portofolio-web-design

Manages deprecation and migration. Use when removing old systems, APIs, or features. Use when migrating users from one implementation to another. Use when migrating a database schema in production, such as renaming or dropping a column without downtime (expand/contract). Use when deciding whether to maintain or sunset existing code.

alfathafp/portofolio-web-design

Use when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.

alfathafp/portofolio-web-design

Use when facing 2+ independent tasks that can be worked on without shared state or sequential dependencies

alfathafp/portofolio-web-design

Records decisions and documentation. Use when you need to document an architecture decision (ADR) or the reasoning behind a design choice, when changing public APIs, shipping features, or when you need to record context that future engineers and agents will need to understand the codebase.

alfathafp/portofolio-web-design

Subjects every non-trivial decision to a fresh-context adversarial review before it stands. Use when you want every assumption cross-examined before proceeding, when stress-testing a plan for hidden failure modes, when correctness matters more than speed, when working in unfamiliar code, when stakes are high (production auth, security-sensitive logic, a high-stakes migration, irreversible operations), or any time a confident output would be cheaper to verify now than to debug later.

alfathafp/portofolio-web-design

Use when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available

alfathafp/portofolio-web-design

Use when implementation is complete, all tests pass, and you need to decide how to integrate the work

alfathafp/portofolio-web-design

Builds production-quality, accessible, responsive user-facing UIs. Use when building or modifying interfaces and pages, creating components, implementing layouts, meeting WCAG accessibility requirements, managing state, or when the output needs to look and feel production-quality rather than AI-generated.

alfathafp/portofolio-web-design

Structures git workflow practices. Use when making any code change. Use when committing, branching, resolving conflicts, splitting uncommitted work in a messy working tree into clean atomic commits, opening or reviewing a pull request (PR), pushing to a remote, or when you need to organize work across multiple parallel streams. Use when cutting a release, choosing a semantic version bump, tagging, or writing a changelog.

alfathafp/portofolio-web-design

Refines raw ideas into sharp, actionable concepts through structured divergent and convergent thinking. Use when an idea is still vague, when you need to stress-test assumptions before committing to a plan, or when you want to expand options before converging on one. Triggers on "ideate", "refine this idea", or "stress-test my plan".

alfathafp/portofolio-web-design

Delivers changes incrementally in thin, verifiable slices. Use when implementing any feature or change that touches more than one file, or when picking up the next task from a plan. Use when rolling a change out behind a feature flag, when you're about to write a large amount of code at once, or when a task feels too big to land in one step.

alfathafp/portofolio-web-design

Guides stable API and interface design. Use when designing APIs, module boundaries, or any public interface. Use when creating REST or GraphQL endpoints, defining type contracts between modules, or establishing boundaries between frontend and backend.

相关技能