|
| 1 | +# Add Slash Command Support for Coding Agents |
| 2 | + |
| 3 | +## Summary |
| 4 | +- Enable OpenSpec to generate and update custom slash commands for supported coding agents (Claude Code and Cursor). |
| 5 | +- Provide three slash commands aligned with OpenSpec's workflow: proposal (start a change proposal), apply (implement), and archive. |
| 6 | +- Share slash command templating between agents to make future extensions simple. |
| 7 | + |
| 8 | +## Motivation |
| 9 | +Developers use different coding agents and editors. Having consistent slash commands across tools for the OpenSpec workflow reduces friction and ensures a standard way to trigger the workflow. Supporting both Claude Code and Cursor now lays a foundation for future agents that introduce slash command features. |
| 10 | + |
| 11 | +## Proposal |
| 12 | +1. During `openspec init`, when a user selects a supported tool, generate slash command configuration for three OpenSpec workflow stages: |
| 13 | + - Claude (namespaced): `/openspec/proposal`, `/openspec/apply`, `/openspec/archive`. |
| 14 | + - Cursor (flat, prefixed): `/openspec-proposal`, `/openspec-apply`, `/openspec-archive`. |
| 15 | + - Semantics: |
| 16 | + - Create – scaffold a change (ID, `proposal.md`, `tasks.md`, delta specs); validate strictly. |
| 17 | + - Apply – implement an approved change; complete tasks; validate strictly. |
| 18 | + - Archive – archive after deployment; update specs if needed. |
| 19 | + - Each command file MUST embed concise, step-by-step instructions sourced from `openspec/README.md` (see Template Content section). |
| 20 | +2. Store slash command files per tool: |
| 21 | + - Claude Code: `.claude/commands/openspec/{proposal,apply,archive}.md` |
| 22 | + - Cursor: `.cursor/commands/{openspec-proposal,openspec-apply,openspec-archive}.md` |
| 23 | + - Ensure nested directories are created. |
| 24 | +3. Command file format and metadata: |
| 25 | + - Use Markdown with optional YAML frontmatter for tool metadata (name/title, description, category/tags) when supported by the tool. |
| 26 | + - Place OpenSpec markers around the body only, never inside frontmatter. |
| 27 | + - Keep the visible slash name, file name, and any frontmatter `name`/`id` consistently aligned (e.g., `proposal`, `openspec-proposal`). |
| 28 | + - Namespacing: categorize these under “OpenSpec” and prefer unique IDs (e.g., `openspec-proposal`) to avoid collisions. |
| 29 | +4. Centralize templates: define command bodies once and reuse across tools; apply minimal per-tool wrappers (frontmatter, categories, filenames). |
| 30 | +5. During `openspec update`, refresh only existing slash command files (per-file basis) within markers; do not create missing files or new tools. |
| 31 | + |
| 32 | +## Design Ideas |
| 33 | +- Introduce `SlashCommandConfigurator` to manage multiple files per tool. |
| 34 | + - Expose targets rather than a single `configFileName` (e.g., `getTargets(): Array<{ path: string; kind: 'slash'; id: string }>`). |
| 35 | + - Provide `generateAll(projectPath, openspecDir)` for init and `updateExisting(projectPath, openspecDir)` for update. |
| 36 | +- Per-tool adapters add only frontmatter and pathing; bodies come from shared templates. |
| 37 | +- Templates live in `TemplateManager` with helpers that extract concise, authoritative snippets from `openspec/README.md`. |
| 38 | +- Update flow logs per-file results so users see exactly which slash files were refreshed. |
| 39 | + |
| 40 | +### Marker Placement |
| 41 | +- Markers MUST wrap only the Markdown body contents: |
| 42 | + - Frontmatter (if present) goes first. |
| 43 | + - Then `<!-- OPENSPEC:START -->` … body … `<!-- OPENSPEC:END -->`. |
| 44 | + - Avoid inserting markers into the YAML block to prevent parse errors. |
| 45 | + |
| 46 | +### Idempotency and Creation Rules |
| 47 | +- `init`: create all three files for the chosen tool(s) once; subsequent `init` runs are no-ops for existing files. |
| 48 | +- `update`: refresh only files that exist; skip missing ones without creating new files. |
| 49 | +- Directory creation for `.claude/commands/openspec/` and `.cursor/commands/` is the configurator’s responsibility. |
| 50 | + |
| 51 | +### Command Naming & UX |
| 52 | +- Claude Code: use namespacing in the slash itself for readability and grouping: `/openspec/proposal`, `/openspec/apply`, `/openspec/archive`. |
| 53 | +- Cursor: use flat names with an `openspec-` prefix: `/openspec-proposal`, `/openspec-apply`, `/openspec-archive`. Group via `category: OpenSpec` when supported. |
| 54 | +- Consistency: align file names, visible slash names, and any frontmatter `id` (e.g., `id: openspec-apply`). |
| 55 | +- Migration: do not rename existing commands during `update`; apply new naming only on `init` (or via an explicit migrate step). |
| 56 | + |
| 57 | +## Open Questions |
| 58 | +- Validate exact metadata/frontmatter supported by each tool version; if unsupported, omit frontmatter and ship Markdown body only. |
| 59 | +- Confirm the final Cursor command file location for the targeted versions; fall back to Markdown-only if Cursor does not parse frontmatter. |
| 60 | +- Evaluate additional commands beyond the initial three (e.g., `/show-change`, `/validate-all`) based on user demand. |
| 61 | + |
| 62 | +## Alternatives |
| 63 | +- Hard-code slash command text per tool (rejected: duplicates content; increases maintenance). |
| 64 | +- Delay Cursor support until its config stabilizes (partial accept): gate Cursor behind a feature flag until verified in real environments. |
| 65 | + |
| 66 | +## Risks |
| 67 | +- Tool configuration formats may change, requiring updates to wrappers/frontmatter. |
| 68 | +- Incorrect paths or categories can hide commands; add path existence checks and clear logging. |
| 69 | +- Marker misuse (inside frontmatter) can break parsing; enforce placement rules in tests. |
| 70 | + |
| 71 | +## Future Work |
| 72 | +- Support additional editors/agents that expose slash command APIs. |
| 73 | +- Allow users to customize command names and categories during `openspec init`. |
| 74 | +- Provide a dedicated command to regenerate slash commands without running full `update`. |
| 75 | + |
| 76 | +## File Format Examples |
| 77 | +The following examples illustrate expected structure. If a tool does not support frontmatter, omit the YAML block and keep only the markers + body. |
| 78 | + |
| 79 | +### Claude Code: `.claude/commands/openspec/proposal.md` |
| 80 | +```markdown |
| 81 | +--- |
| 82 | +name: OpenSpec: Proposal |
| 83 | +description: Scaffold a new OpenSpec change and validate strictly. |
| 84 | +category: OpenSpec |
| 85 | +tags: [openspec, change] |
| 86 | +--- |
| 87 | +<!-- OPENSPEC:START --> |
| 88 | +...command body from shared template... |
| 89 | +<!-- OPENSPEC:END --> |
| 90 | +``` |
| 91 | + |
| 92 | +Slash invocation: `/openspec/proposal` (namespaced) |
| 93 | + |
| 94 | +### Cursor: `.cursor/commands/openspec-proposal.md` |
| 95 | +```markdown |
| 96 | +--- |
| 97 | +name: /openspec-proposal |
| 98 | +id: openspec-proposal |
| 99 | +category: OpenSpec |
| 100 | +description: Scaffold a new OpenSpec change and validate strictly. |
| 101 | +--- |
| 102 | +<!-- OPENSPEC:START --> |
| 103 | +...command body from shared template... |
| 104 | +<!-- OPENSPEC:END --> |
| 105 | +``` |
| 106 | + |
| 107 | +Slash invocation: `/openspec-proposal` (flat, prefixed) |
| 108 | + |
| 109 | +## Template Content |
| 110 | +Templates should be brief, actionable, and sourced from `openspec/README.md` to avoid duplication. Each command body includes: |
| 111 | +- Guardrails: ask 1–2 clarifying questions if needed; follow minimal-complexity rules; use `pnpm` for Node projects. |
| 112 | +- Step list tailored to the workflow stage (proposal, apply, archive), including strict validation commands. |
| 113 | +- Pointers to `openspec show`, `openspec list`, and troubleshooting tips when validation fails. |
| 114 | + |
| 115 | +## Testing Strategy |
| 116 | +- Golden snapshots for generated files per tool (frontmatter + markers + body). |
| 117 | +- Partial presence tests: if 1–2 files exist, `update` only refreshes those and does not create missing ones. |
| 118 | +- Marker placement tests: ensure markers never appear inside frontmatter; cover missing/duplicated marker recovery behavior. |
| 119 | +- Logging tests: `update` reports per-file updates for slash commands. |
0 commit comments