home / skills / 0xbigboss / claude-code / specalign

specalign skill

/.claude/skills/specalign

This skill helps you align spec files with implementations, surfacing drift and guiding you to update spec or code.

npx playbooks add skill 0xbigboss/claude-code --skill specalign

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
3.5 KB
---
name: specalign
description: Align spec files with implementation. Detects drift between spec and code, surfaces discrepancies, user decides whether to update spec or code. Use when both a spec file and its implementation are in context.
---

# Spec Alignment

## Principles (Always Active)

These apply whenever a spec file and its corresponding implementation are both in context:

### Spec and Code Must Agree

- A spec describes intended behavior; code implements it. When they disagree, one is wrong.
- Never silently tolerate drift - surface it immediately when noticed.
- The user decides which is the source of truth for each discrepancy. Do not assume.

### Drift Categories

- **Type drift**: spec defines fields/types that don't match the implementation
- **Behavior drift**: spec describes logic the code doesn't follow
- **Missing implementation**: spec defines something with no corresponding code
- **Missing spec**: code implements behavior not described in the spec
- **Constraint drift**: spec states invariants the code doesn't enforce
- **Error handling drift**: spec defines error cases the code doesn't handle (or vice versa)

### Mutation Policy

- Do not edit spec files unless the user explicitly chooses "update spec" for a discrepancy.
- Do not change implementation logic unless the user explicitly chooses "update code".
- When updating code, run lint/typecheck after changes.
- When updating spec, preserve formatting and structure of unrelated sections.

### Bidirectional Awareness

When reading code, check if a spec exists and note divergences.
When reading a spec, check if the implementation matches and note divergences.
This awareness should be passive - flag drift in your responses without interrupting the user's primary task, unless the drift is directly relevant.

## Workflow (When Explicitly Aligning)

### Step 1: Locate the Spec

A spec file is required. Search for:
- `*.spec.md`, `*-spec.md`, `SPEC.md`
- `spec/*.md`, `docs/*.spec.md`

If multiple specs exist, ask which to align. If none exist, stop - this workflow requires an existing spec.

Read the spec file completely.

### Step 2: Map Spec to Code

For each spec section, identify the corresponding implementation:

| Spec Section | Source File(s) | Status |
|---|---|---|
| `## Types` | `src/types.ts:10-40` | aligned / drifted / missing-impl / missing-spec |

Read each mapped source file before assessing.

### Step 3: Present Discrepancies

For each discrepancy:

```
### DRIFT-01: <short description>

**Spec says** (spec-file.md:L42):
> <quoted spec text>

**Code does** (src/module.ts:L87):
> <summarized code behavior>

**Impact**: <what breaks or is inconsistent>
```

Number with stable IDs (`DRIFT-NN`). Batch related discrepancies that share a root cause.

### Step 4: User Decision

For each discrepancy, ask:

- **Update spec** - the code is correct, update the spec to match
- **Update code** - the spec is correct, update the code to match
- **Skip** - defer this discrepancy

### Step 5: Apply Changes

**Spec updates:**
- Edit the spec file with corrected text
- Preserve formatting and structure of unrelated sections

**Code updates:**
- Fix the implementation to match the spec
- Run lint/typecheck after changes
- If non-trivial, outline the change and confirm before editing
- If unit tests exist for the affected code, run them
- If unit tests don't exist and the spec defines testable behavior, flag it

### Step 6: Summary

```
## Spec Alignment: <file>

**Discrepancies found**: N
**Resolved**: X (spec: A, code: B, skipped: C)

### Remaining
- DRIFT-04: <description> (skipped)
```

Overview

This skill aligns specification files with their TypeScript implementations to detect and manage drift. It surfaces concrete discrepancies, categorizes them, and guides the user through choosing whether to update the spec or the code. Changes are only applied with explicit user approval and follow linting and typecheck policies for code edits.

How this skill works

When both a spec file and the corresponding implementation are in context, the skill scans the spec and maps each section to implementation files. It categorizes mismatches (type, behavior, missing implementation, missing spec, constraint, error handling) and produces numbered discrepancy reports with referenced locations and impact summaries. For each discrepancy the user chooses update spec, update code, or skip; edits are made only after confirmation and are followed by lint/typecheck for code changes.

When to use it

  • You have a spec file (e.g., *.spec.md, SPEC.md, docs/*.spec.md) and its implementation in context.
  • You suspect recent commits introduced drift between design and code.
  • Before releasing a feature to ensure spec and behavior match.
  • When onboarding or reviewing a module with separate spec documentation.
  • When adding tests driven by spec-defined behavior.

Best practices

  • Keep spec files discoverable with consistent naming and central directories.
  • Map every spec section to specific source file ranges and read the mapped source before assessing.
  • Do not assume which side is correct — always present a clear choice to the user.
  • Preserve unrelated spec formatting when applying spec edits; run lint/typecheck after code edits.
  • Batch related discrepancies with a shared root cause and use stable IDs (DRIFT-01, DRIFT-02, ...).

Example use cases

  • Detect a type mismatch where the spec declares a nullable field but the TypeScript type is non-nullable.
  • Find behavior drift when the spec requires retry logic but the implementation never retries.
  • Discover missing implementation when the spec documents an endpoint that has no handler in code.
  • Uncover constraint drift where the spec requires input validation that the code omits.
  • Align error handling when the spec lists specific error cases not represented in thrown errors.

FAQ

What input is required to run alignment?

A spec file must be present in context (e.g., *.spec.md, SPEC.md). The corresponding implementation files should also be available so the tool can map spec sections to source ranges.

Will the skill change files automatically?

No. The skill only edits spec or code after you explicitly choose update spec or update code for each discrepancy. Code edits are followed by lint/typecheck.