跪拜 Guibai
← All articles
Backend · AI Programming

OpenSpec in Practice: Write the Spec First, Then Let AI Write the Code

By 周末程序猿 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Multi-day AI coding sessions lose requirements to chat history; OpenSpec makes alignment a commit you can review, diff, and archive. Teams that let agents run unattended get a deterministic read/write contract instead of hoping the model remembers the last ten turns.

Summary

OpenSpec is an open-source tool (MIT) that formalizes spec-driven development for AI coding assistants. Instead of letting requirements live in dozens of chat turns, it puts "what to build" into Markdown files under `openspec/specs/` (current state) and `openspec/changes/` (in-flight changes). A CLI scaffolds changes, validates deltas, and archives them back into the main specs. The core design: each change holds only a delta (ADDED/MODIFIED/REMOVED requirements) plus proposal, design, and tasks files, so parallel changes don't clash.

The CLI's `instructions` command generates tagged directives that tell the agent exactly which file to write, which sections are mandatory, and what project constraints are non-negotiable. Real output from SimpleBase shows `<project_context>` injected from `config.yaml`, `<dependencies>` forcing re-reads of completed artifacts, and `<instruction>` banning implementation details in specs. `openspec instructions apply --json` returns `contextFiles`, a task list with progress counts, so agents never guess the next step.

SimpleBase, a Go + Vue serverless database, uses OpenSpec in production: 12 capability baselines split by bounded context, 7 archived changes, 7 in-flight. A six-step loop covers open -> generate -> validate -> implement -> archive, with code-side gates (architecture tests, build, test) catching behavior errors that spec validation can't. `config.yaml` injects constraints that can't be inferred from code, while `skip_specs: true` lets pure refactors skip deltas without faking requirements.

Takeaways
— OpenSpec stores current specs in `openspec/specs/` and in-flight changes in `openspec/changes/`, each change holding proposal, specs delta, design, tasks, and `.openspec.yaml`.
— Changes write only delta requirements (ADDED/MODIFIED/REMOVED/RENAMED); archive merges deltas back into the main spec and moves the directory to `changes/archive/YYYY-MM-DD-<id>/`.
— The CLI's `instructions` command generates machine-readable directives with `<project_context>`, `<dependencies>`, and `<instruction>` blocks that tell the agent exactly what to write and which constraints are non-negotiable.
— Requirement sections must follow `### Requirement:` and include at least one `#### Scenario` with WHEN/THEN; chapter names must be exact delta headings, or they're silently ignored.
— Running `validate --strict` only checks structure; behavior correctness is enforced by separate code gates like `TestArchitectureGate`, `go test`, and frontend typecheck.
— Pure refactors set `skip_specs: true` in `.openspec.yaml`, so validation gives an INFO message instead of forcing fabricated requirements.
— Telemetry collection is opt-out via `openspec config set telemetry.enabled false` or environment variables, and only captures command names and versions.
— SimpleBase as of 2026-11-27 has 12 capability baselines, 7 archived changes, and 7 in-flight changes, with tasks like `add-ops-maturity` at 1/15 and `close-legacy-backlog` at 6/54.
Conclusions

The delta-and-archive mechanism is what separates OpenSpec from a design-doc prompt: parallel changes never overwrite baselines, and archived specs accumulate as a readable history.

Injecting `context` from `config.yaml` into every instruction is a stronger alignment tool than relying on `AGENTS.md`, because it reaches the agent at the moment of artifact generation rather than as a general preamble.

OpenSpec's explicit limitation (structure-only validation) forces teams to keep human review and code-side gates, which is probably why SimpleBase can run 7 concurrent changes without the spec library rotting.

The tool's success depends on a team actually reading specs; for solo exploration or one-off scripts, the overhead of writing a proposal outweighs the benefit.

Concepts & terms
Spec-driven development (SDD)
A workflow where behavior is specified as machine-readable Markdown contracts (requirements and scenarios) before implementation, so both human and AI agree on observable behavior rather than implementation details.
Capability baseline
A `specs/<capability>/spec.md` file in OpenSpec that represents the current, authoritative description of a system capability's observable behavior, split by bounded context.
Delta
In OpenSpec, a change's specs directory contains only the differences from the current baseline (ADDED/MODIFIED/REMOVED/RENAMED requirements). On archive, these deltas merge into the main spec files.
skip_specs
A flag in `.openspec.yaml` that tells the validator a change has no spec-level behavior changes (e.g., pure refactor or docs), so no delta is required and archive succeeds with an INFO message.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗