← 研究笔记
在让 AI 代理实现功能之前,先锁定规格说明:试用 Spec Kit

在让 AI 代理实现功能之前,先锁定规格说明:试用 Spec Kit

记录安装和初始化 GitHub 官方的规格驱动开发 CLI——Spec Kit,以及让 Codex 代理编写规格说明的过程。

本文涉及的仓库

github/spec-kit

💫 Toolkit to help you get started with Spec-Driven Development

121.6k Python MIT License

你可以把实现工作交给 AI 代理,只用一句需求就让它生成代码。代理很快就能产出结果,但后续评审可能会发现其中的假设存在遗漏。实现之前,先写清楚要构建什么、为什么要这样构建,以及范围延伸到哪里。实现、规划和任务拆分都可以建立在同一份基础之上。GitHub 官方的 Spec Kit 将这一步“先写规格说明”封装成 CLI 和一组模板。我安装了它,并验证了项目初始化流程。

Spec Kit 是一套规格驱动开发工具。它会为 Claude Code、Copilot、Cursor 等 AI 编程代理安装规格说明、实现计划和任务列表所需的模板与技能。该项目以 MIT License 发布源代码(*1)。

1. 安装

安装固定版本。

FROM python:3.12.13-slim
RUN pip install --no-cache-dir --quiet 'specify-cli==0.12.15'
WORKDIR /work
podman build -t specify-sandbox .

Podman 在几秒内就构建好了镜像,依赖项也很少。

2. 查看版本

podman run --rm --network none --memory 2g --cpus 2 --pids-limit 512 \
  -v "$(pwd)":/work -w /work specify-sandbox specify --version

命令返回了 specify 0.12.15。

3. 初始化项目

specify init 会根据软件包内置的模板生成文件,因此不需要网络连接。运行时将网络禁用。

podman run --rm --network none --memory 2g --cpus 2 --pids-limit 512 \
  -v "$(pwd)":/work -w /work specify-sandbox \
  specify init demo-project --ignore-agent-tools --integration claude --script sh

-v 选项让容器可以读写指定目录。请在一个专门为本次实验准备的空目录中运行这条命令。不要把私钥、凭据和未发布的源代码放进该目录。

命令执行成功,输出逐步显示了每个进度步骤。

运行 specify init 的结果
运行 specify init 的结果

命令生成了 27 个文件。

.claude/skills/speckit-analyze/SKILL.md
.claude/skills/speckit-checklist/SKILL.md
.claude/skills/speckit-clarify/SKILL.md
.claude/skills/speckit-constitution/SKILL.md
.claude/skills/speckit-converge/SKILL.md
.claude/skills/speckit-implement/SKILL.md
.claude/skills/speckit-plan/SKILL.md
.claude/skills/speckit-specify/SKILL.md
.claude/skills/speckit-tasks/SKILL.md
.claude/skills/speckit-taskstoissues/SKILL.md
.specify/init-options.json
.specify/integration.json
.specify/integrations/claude.manifest.json
.specify/integrations/speckit.manifest.json
.specify/memory/constitution.md
.specify/scripts/bash/check-prerequisites.sh
.specify/scripts/bash/common.sh
.specify/scripts/bash/create-new-feature.sh
.specify/scripts/bash/setup-plan.sh
.specify/scripts/bash/setup-tasks.sh
.specify/templates/checklist-template.md
.specify/templates/constitution-template.md
.specify/templates/plan-template.md
.specify/templates/spec-template.md
.specify/templates/tasks-template.md
.specify/workflows/speckit/workflow.yml
.specify/workflows/workflow-registry.json

Claude Code 可以将 .claude/skills/ 下的 10 个技能定义作为命令调用,例如 /speckit-specify、/speckit-plan、/speckit-tasks 和 /speckit-implement。.specify/templates/ 目录包含规格说明、实现计划和任务列表的模板。例如,constitution-template.md 在 ## Core Principles 下列出了 [PRINCIPLE_1_NAME] 这样的占位符,为编写项目原则提供模板。

使用 --network none 时,Podman 会阻止容器内的进程与外部系统通信。但它不会阻止对挂载目录进行读写,也不会限制主机上的资源消耗。执行目录以及 --memory/--cpus/--pids-limit 设置都必须由你自行管理。

4. 在无网络连接的情况下观察行为

podman run --rm --network none --memory 2g --cpus 2 --pids-limit 512 \
  -v "$(pwd)":/work -w /work specify-sandbox \
  specify self check

输出包含以下两行。

Installed: 0.12.15
Could not check latest release: offline or timeout

这不是错误。self check 会联系 GitHub,检查最新发布版本。无法连接 GitHub 的 self check 运行会打印这行内容,然后无错误退出。没有网络连接的开发机或封闭的 CI 环境运行 specify 时,也应显示相同的输出。不需要采取任何措施,继续下一个任务即可。

5. 让 Codex 代理编写规格说明

到这里为止,我只生成了模板,还没有和 AI 代理一起尝试完整工作流。Spec Kit 正式支持 Codex CLI,因此我让一个代理编写规格说明。

初始化项目,并安装 Codex 技能。

specify init --here --integration codex --integration-options="--skills"

该命令会在 .agents/skills/ 下安装一组技能定义。在 Codex CLI 中,使用 $ 前缀调用技能,例如 $speckit-specify;Claude Code 使用 /speckit-specify 这种形式。

我只给了代理一句话:

codex exec --sandbox workspace-write --skip-git-repo-check \
  'Use the $speckit-specify skill to create a baseline specification for a small CLI tool: a text-reversal utility that reads stdin and writes the reversed string to stdout. Keep it simple.'

Codex 按照 $speckit-specify 技能的步骤执行,整理出对问题的回答,并生成了 specs/001-reverse-text/spec.md。生成的文件包含以下摘录:

### User Story 1 - Reverse Piped Text (Priority: P1)

As a command-line user, I want to pipe text into the utility and receive
the same text in reverse order so that I can reverse a string in scripts
or interactive shell workflows.

**Acceptance Scenarios**:

1. **Given** standard input contains `stressed`, **When** the utility runs,
   **Then** standard output is exactly `desserts`.
2. **Given** standard input is empty, **When** the utility runs, **Then**
   standard output is empty and the run completes successfully.

## Requirements *(mandatory)*

### Functional Requirements

- **FR-001**: The utility MUST read the complete text value from standard input.
- **FR-004**: Successful output MUST contain no prompts, labels, diagnostics,
  or extra characters beyond the reversed input.
- **FR-008**: If input cannot be read or output cannot be written, the utility
  MUST indicate failure and MUST keep any diagnostic message separate from
  standard output.

在写下第一行代码之前,代理就完成了用户故事和验收标准。它覆盖了边界情况。文件中包含功能需求和成功标准。接下来可以继续执行实现计划($speckit-plan)。由于生成后续文件需要时间,我就停在了这里。

这一步是在 Podman 的 --network none 沙箱之外、主机上执行的。Codex 需要与 OpenAI 的推理端点通信,因此无法在网络禁用的容器内运行。Codex 自身的沙箱机制 --sandbox workspace-write 会将写入范围限制在执行目录内。该工具会根据第三方编写的指令生成技能文件。在代理读取这些文件之前,请先检查它们。

养成先写规格说明的习惯

运行一次 specify init,就能得到规格说明、计划和任务的模板,以及一组将它们连接到 AI 代理的技能。我只给了 Codex 一句话,它就用 prose 补充了用户故事、功能需求和成功标准。工作流从确定规格说明并将实现交给代理开始。模板阶段让下一步变得清晰。

来源

*1 Spec Kit (GitHub), https://github.com/github/spec-kit, MIT License