← 研究筆記
請 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 授權條款發布原始碼 (*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,確認最新發行版本。如果執行 self check 時無法連上 GitHub,就會印出這一行並正常結束。沒有網路存取權的開發機器或封閉的 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 一句話,它便以文字填寫了使用者故事、功能需求和成功標準。工作流程從確定規格並將實作交給代理開始。範本階段讓下一步變得清楚。

來源

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