|
| 1 | +# Spec-Kit Optimize Extension |
| 2 | + |
| 3 | +Audits and optimizes AI governance documents for context efficiency. Designed for long-term AI-driven development where constitutions grow organically and accumulate token debt. |
| 4 | + |
| 5 | +## Why This Extension Exists |
| 6 | + |
| 7 | +In AI-driven development, the constitution is loaded into every AI session's context window. Over time: |
| 8 | + |
| 9 | +- **Token bloat**: Rules, examples, and version history accumulate — costing tokens on every invocation |
| 10 | +- **Rule decay**: Incident-specific rules persist forever because AI has no institutional memory |
| 11 | +- **Non-deterministic governance**: Ambiguous rules cause different AI sessions to behave differently |
| 12 | +- **Governance echoes**: The same rule gets restated across multiple files, wasting tokens and risking contradictions |
| 13 | +- **No learning loop**: AI sessions make the same mistakes repeatedly with no mechanism to capture learnings |
| 14 | + |
| 15 | +This extension provides tooling to detect and fix these problems. |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +```bash |
| 20 | +# From the spec-kit catalog (when published) |
| 21 | +specify extension add optimize |
| 22 | + |
| 23 | +# From a direct URL |
| 24 | +specify extension add optimize --from https://github.com/sakitA/spec-kit-optimize/archive/refs/tags/v1.0.0.zip |
| 25 | + |
| 26 | +# For local development |
| 27 | +specify extension add --dev /path/to/spec-kit-optimize |
| 28 | +``` |
| 29 | + |
| 30 | +## Commands |
| 31 | + |
| 32 | +### `/speckit.optimize.run` — Constitution Audit |
| 33 | + |
| 34 | +Analyzes the constitution across 6 categories uniquely relevant to AI-driven development: |
| 35 | + |
| 36 | +1. **Token Budget Analysis** — measures per-section token cost and governance density |
| 37 | +2. **Rule Health Analysis** — detects stale, incident-specific, superseded, and graduated rules |
| 38 | +3. **AI Interpretability Analysis** — finds ambiguity, contradictions, unenforceable rules |
| 39 | +4. **Semantic Compression** — identifies collapsible rule clusters, redundant examples, inline-to-reference conversions |
| 40 | +5. **Constitution Coherence** — evaluates principle balance, rule scatter, cross-references, CLAUDE.md drift |
| 41 | +6. **Governance Echo Detection** — finds cross-file duplication and total governance token budget |
| 42 | + |
| 43 | +```bash |
| 44 | +# Full audit |
| 45 | +/speckit.optimize.run |
| 46 | + |
| 47 | +# Single category |
| 48 | +/speckit.optimize.run --category token_budget |
| 49 | + |
| 50 | +# Report only (no apply step) |
| 51 | +/speckit.optimize.run --report-only |
| 52 | +``` |
| 53 | + |
| 54 | +### `/speckit.optimize.tokens` — Token Usage Tracker |
| 55 | + |
| 56 | +Measures the token footprint of all governance files and extension commands. Tracks trends over time. |
| 57 | + |
| 58 | +```bash |
| 59 | +# Full token report |
| 60 | +/speckit.optimize.tokens |
| 61 | + |
| 62 | +# Compare against previous report |
| 63 | +/speckit.optimize.tokens --diff |
| 64 | + |
| 65 | +# Extensions only (skip governance files) |
| 66 | +/speckit.optimize.tokens --extensions-only |
| 67 | +``` |
| 68 | + |
| 69 | +### `/speckit.optimize.learn` — Session Learning |
| 70 | + |
| 71 | +End-of-session analysis: detects AI mistake patterns, repetitive corrections, and governance gaps. Suggests constitution rules or memory entries to prevent recurrence. |
| 72 | + |
| 73 | +```bash |
| 74 | +# Full session analysis |
| 75 | +/speckit.optimize.learn |
| 76 | + |
| 77 | +# Rules only (no memory suggestions) |
| 78 | +/speckit.optimize.learn --rules-only |
| 79 | + |
| 80 | +# Analyze from a specific commit |
| 81 | +/speckit.optimize.learn --since abc1234 |
| 82 | +``` |
| 83 | + |
| 84 | +## Design Philosophy |
| 85 | + |
| 86 | +**Suggest-only by default.** Every command produces a report first. Nothing is modified until the user explicitly approves. The flow is always: Analyze → Report → Propose → User Consent → Apply. |
| 87 | + |
| 88 | +**Spec-kit standard paths.** The extension works with any spec-kit project. It uses `.specify/memory/constitution.md` as the primary constitution path and follows redirects for project-specific layouts. It never hardcodes project-specific paths. |
| 89 | + |
| 90 | +**Semantic preservation.** Optimization removes redundancy in expression, not in intent. Every governance rule survives compression — only its token cost changes. |
| 91 | + |
| 92 | +## Configuration |
| 93 | + |
| 94 | +Copy `config-template.yml` to `optimize-config.yml` in the extension directory: |
| 95 | + |
| 96 | +```bash |
| 97 | +cp .specify/extensions/optimize/config-template.yml \ |
| 98 | + .specify/extensions/optimize/optimize-config.yml |
| 99 | +``` |
| 100 | + |
| 101 | +Key settings: |
| 102 | +- `categories.*` — toggle individual analysis categories on/off |
| 103 | +- `thresholds.max_constitution_tokens` — flag constitutions exceeding this token estimate |
| 104 | +- `thresholds.governance_budget_percent` — max % of context window for governance overhead |
| 105 | +- `target_context_window` — context window size for budget calculations (default: 200K) |
| 106 | +- `learn.min_corrections_to_flag` — minimum repeated corrections before flagging a pattern |
| 107 | + |
| 108 | +## Integration |
| 109 | + |
| 110 | +- **`/speckit.constitution`** — authoring tool. Optimize hands off to it for applying approved changes with proper version bumping. |
| 111 | +- **`/speckit.analyze`** — consistency checker. Run after optimization to verify cross-artifact alignment. |
| 112 | +- **`/speckit.optimize.tokens`** → **`/speckit.optimize.run`** — if token tracker reveals high governance overhead, run the full audit. |
| 113 | +- **`/speckit.optimize.learn`** → **`/speckit.constitution`** — approved rules from session learning are applied via the constitution skill. |
| 114 | + |
| 115 | +## Reports |
| 116 | + |
| 117 | +Reports are saved to `.specify/optimize/` (with user consent): |
| 118 | +- `token-report.md` — latest token usage snapshot (enables historical trends) |
| 119 | +- `learning-report-<date>.md` — per-session learning analysis |
| 120 | + |
| 121 | +## Requirements |
| 122 | + |
| 123 | +- Spec-Kit >= 0.1.0 |
| 124 | +- An existing populated constitution (not a raw template) |
| 125 | +- Git repository (for learn command's session analysis) |
| 126 | + |
| 127 | +## License |
| 128 | + |
| 129 | +MIT |
0 commit comments