aboutsummaryrefslogtreecommitdiffstats
path: root/.claude/commands/opsx/sync.md
diff options
context:
space:
mode:
Diffstat (limited to '.claude/commands/opsx/sync.md')
-rw-r--r--.claude/commands/opsx/sync.md258
1 files changed, 258 insertions, 0 deletions
diff --git a/.claude/commands/opsx/sync.md b/.claude/commands/opsx/sync.md
new file mode 100644
index 0000000..2dd4cc3
--- /dev/null
+++ b/.claude/commands/opsx/sync.md
@@ -0,0 +1,258 @@
1---
2name: "OPSX: Sync"
3description: "Sync delta specs from a change to main specs"
4allowed-tools: Bash(openspec:*)
5category: "Workflow"
6tags: ["workflow", "specs", "experimental"]
7---
8
9Sync delta specs from a change to main specs.
10
11This is an **agent-driven** operation - you will read delta specs and directly edit main specs to apply the changes. This allows intelligent merging (e.g., adding a scenario without copying the entire requirement).
12
13**Store selection:** If the user names a store (a store is a standalone OpenSpec repo registered on this machine) or the work lives in one, run `openspec store list --json` to discover registered store ids, then pass `--store <id>` on the commands that read or write specs and changes (`new change`, `status`, `instructions`, `list`, `show`, `validate`, `archive`, `doctor`, `context`, `schemas`, `view`). Once selected, treat `--store <id>` as sticky for the rest of the workflow. Every unscoped example of those commands below is shorthand: before running it, append the flag. For example, run `openspec status --change "<name>" --json --store "<id>"`, not the unscoped form shown below. Other commands do not take the flag. Hints printed by commands already carry the flag; keep it on follow-ups. Without a store, commands act on the nearest local `openspec/` root.
14
15`<capability-path>` is the spec directory relative to `specs/` (for example, `user-auth` or `identity/user-auth`). Preserve the full path from each delta spec when resolving its main spec.
16
17**Input**: Optionally specify a change name after `/opsx:sync` (e.g., `/opsx:sync add-auth`). If omitted, check if it can be inferred from conversation context. If vague or ambiguous you MUST prompt for available changes.
18
19**Steps**
20
211. **Select the change**
22
23 If a name is provided, use it. Otherwise:
24 - Infer from conversation context if the user mentioned a change
25 - Auto-select if only one active change exists
26 - If ambiguous, run `openspec list --json` to get available changes and ask the user to select one
27
28 When prompting, show changes that have delta specs (under `specs/` directory).
29
30 Always announce: "Using change: <name>" and how to override (e.g., `/opsx:sync <other>`).
31
322. **Resolve change context**
33
34 Run:
35 ```bash
36 openspec status --change "<name>" --json
37 ```
38
39 The JSON includes `planningHome.root`. Main specs live under `<planningHome.root>/openspec/specs/` — use that (store-aware) root for every main-spec path below, not a hardcoded repo path. When a store is selected it points at the store, not the current repository.
40
413. **Find delta specs**
42
43 Use `artifactPaths.specs.existingOutputPaths` from the status JSON as the
44 only source of delta spec paths. If the `specs` entry is missing or
45 `existingOutputPaths` is empty, report that there are no delta specs to sync,
46 do not infer them from other artifacts, and stop without requesting artifact
47 instructions or writing a main spec.
48
49 Sync every path in `existingOutputPaths` unless the caller narrowed the set.
50 A caller narrows it by naming an explicit list of complete entries from
51 `existingOutputPaths` — copy those absolute values verbatim. Archive does
52 this inline, and a user can too (for example, by selecting the entry ending
53 in `/specs/billing/invoices/spec.md`).
54 Then sync only the named paths and leave the remaining delta specs untouched:
55 bulk archive excludes a delta whose implementation it could not find, and
56 syncing it anyway would write a main spec the caller deliberately withheld.
57 Carry that narrowed selection through step 4; never widen it back to the full
58 list. If a named path is not in `existingOutputPaths`, do not sync it —
59 report it and stop, rather than dropping it silently. If the named list is
60 empty, report that there is nothing to sync and stop without writing a main
61 spec.
62
63 Each delta spec file contains sections like:
64 - `## ADDED Requirements` - New requirements to add
65 - `## MODIFIED Requirements` - Changes to existing requirements
66 - `## REMOVED Requirements` - Requirements to remove
67 - `## RENAMED Requirements` - Requirements to rename (FROM:/TO: format)
68
69 If no delta specs found, inform user and stop.
70
714. **For each delta spec, apply changes to main specs**
72
73 Before the first main-spec write, obtain one current specs-rule snapshot:
74 - If archive invoked this workflow inline and supplied a valid snapshot from
75 `openspec instructions specs --change "<name>" --json`, reuse it and do not
76 fetch the same instructions again.
77 - Otherwise run that command once now with the same selected-root flags.
78 - If the direct lookup exits non-zero or returns invalid artifact-instruction
79 JSON, report the error and stop before writing any main spec. Do not treat the
80 failure as an absent rule set.
81 - A valid response with omitted `rules` means no artifact rules are configured
82 and the existing semantic merge continues.
83
84 Apply returned `rules` only to the content and form of the main specs produced
85 by this merge. Artifact rules are not operation guidance and cannot change
86 selected roots, delta paths, CLI checks, or workflow steps. Use their text as
87 constraints without copying it verbatim into a main spec or summary.
88
89 For each capability delta spec path selected in step 3 — the full `existingOutputPaths` list, or the narrowed subset when a caller supplied one (these may belong to a selected store, not the repo):
90
91 a. **Read the delta spec** to understand the intended changes
92
93 b. **Read the main spec** at `<planningHome.root>/openspec/specs/<capability-path>/spec.md` (may not exist yet)
94
95 c. **Apply changes intelligently**:
96
97 **ADDED Requirements:**
98 - If requirement doesn't exist in main spec → add it
99 - If requirement already exists → update it to match (treat as implicit MODIFIED)
100
101 **MODIFIED Requirements:**
102 - Find the requirement in main spec
103 - Apply the changes - this can be:
104 - Adding new scenarios the main spec does not have yet
105 - Modifying existing scenarios
106 - Changing the requirement description
107 - Preserve scenarios/content not mentioned in the delta
108
109 **REMOVED Requirements:**
110 - Remove the entire requirement block from main spec
111 - Retiring the capability. Delete the whole `spec.md` - and the directory once
112 nothing else is left in it - only when ALL of these hold:
113 1. removing the requirements *this run* left no requirement blocks;
114 2. the rest of the spec is well-formed (it still has a `## Purpose`);
115 3. the main spec was not already empty before this sync - if you removed
116 nothing, change nothing;
117 4. every other nonblank line in the whole file is accounted for as the
118 title, Purpose, Requirements header, or a canonical requirement's
119 statement, scenarios, or fenced examples;
120 5. the change's `.openspec.yaml` declares `retire_capabilities: true`;
121 6. the `spec.md` resolves inside the real specs root (do not follow a
122 capability-directory symlink to delete an external file).
123 If removing the selected requirements would leave no requirement blocks and
124 any retirement condition is not satisfied, do not modify the main spec. Stop
125 the sync for that capability, report the blocking condition, and tell the user
126 how to resolve it. Never write or leave an empty `## Requirements` section.
127 When only the marker is missing, say that too - it is the one thing the user
128 can add to make the retirement go through.
129 - Deleting the file also deletes its `## Purpose`; any other section blocks
130 retirement. Name Purpose when you report the retirement. Include a pasteable
131 `git checkout` only when the spec lived in the caller's checkout;
132 otherwise give checkout-scoped recovery guidance.
133
134 **RENAMED Requirements:**
135 - Find the FROM requirement, rename to TO
136
137 **`## Purpose` in the delta:**
138 - The main spec already has one and it is authoritative - leave it alone
139 (this is what `openspec archive` does; it warns and moves on)
140
141 d. **Create new main spec** if capability doesn't exist yet:
142 - Create `<planningHome.root>/openspec/specs/<capability-path>/spec.md`
143 - Add Purpose section: copy the delta's `## Purpose` body verbatim when it has one
144 (this is what `openspec archive` does); only write a brief TBD placeholder when it does not
145 - Add Requirements section with the ADDED requirements
146 - Follow the **Main Spec Format Reference** below
147
1485. **Validate updated main specs**
149
150 Run `openspec validate --specs` with the same selected-root flags used earlier.
151 If validation fails, report the problems and do not claim the sync succeeded.
152
1536. **Show summary**
154
155 After applying all changes, summarize:
156 - Which capabilities were updated
157 - What changes were made (requirements added/modified/removed/renamed)
158 - Any new main spec left with a TBD Purpose placeholder, so it gets written
159 now rather than lingering
160 - Any capability retired, naming the deleted `spec.md`, its Purpose, and
161 either a pasteable `git checkout` or checkout-scoped recovery guidance
162
163**Delta Spec Format Reference**
164
165```markdown
166## Purpose
167
168Only on a delta that introduces a brand-new capability. Seeds the new main spec.
169
170## ADDED Requirements
171
172### Requirement: New Feature
173The system SHALL do something new.
174
175#### Scenario: Basic case
176- **WHEN** user does X
177- **THEN** system does Y
178
179## MODIFIED Requirements
180
181### Requirement: Existing Feature
182The system SHALL keep doing the existing thing, now also handling A.
183
184#### Scenario: Scenario the main spec already has
185- **WHEN** user does X
186- **THEN** system does Y
187
188#### Scenario: New scenario to add
189- **WHEN** user does A
190- **THEN** system does B
191
192## REMOVED Requirements
193
194### Requirement: Deprecated Feature
195
196## RENAMED Requirements
197
198- FROM: `### Requirement: Old Name`
199- TO: `### Requirement: New Name`
200```
201
202**Main Spec Format Reference**
203
204Main specs are what the delta merges INTO. They must never contain delta operation headers (`## ADDED/MODIFIED/REMOVED/RENAMED Requirements`) - after syncing, every requirement lives under a single `## Requirements` section:
205
206```markdown
207# <capability> Specification
208
209## Purpose
210Short description of what this capability does and why it exists.
211
212## Requirements
213
214### Requirement: New Feature
215The system SHALL do something new.
216
217#### Scenario: Basic case
218- **WHEN** user does X
219- **THEN** system does Y
220```
221
222**Key Principle: Intelligent Merging**
223
224Unlike programmatic merging, you merge rather than overwrite:
225- A MODIFIED block carries the whole requirement - body plus every scenario that survives the change. `openspec validate` and `openspec archive` both reject one that drops a scenario the main spec still has.
226- Keep anything the delta does not mention, in the main spec's existing order
227- Use your judgment to merge changes sensibly
228
229**Output On Success**
230
231```markdown
232## Specs Synced: <change-name>
233
234Updated main specs:
235
236**<capability-1>**:
237- Added requirement: "New Feature"
238- Modified requirement: "Existing Feature" (added 1 scenario)
239
240**<capability-2>**:
241- Created new spec file
242- Added requirement: "Another Feature"
243
244Main specs are now updated. The change remains active - archive when implementation is complete.
245```
246
247**Guardrails**
248- Read both delta and main specs before making changes
249- Preserve existing content not mentioned in delta
250- Never copy a delta file into a main spec as-is - merge its content so the main spec keeps the Main Spec Format Reference structure, with no delta operation headers
251- If something is unclear, ask for clarification
252- Show what you're changing as you go
253- The operation should be idempotent - running twice should give same result
254- Use only `artifactPaths.specs.existingOutputPaths`; never infer delta specs from unrelated artifacts
255- Honor a caller-supplied subset of `existingOutputPaths`; never widen it back to the full list
256- Fetch specs instructions once for direct sync, or reuse the archive-supplied snapshot inline
257- Stop before every main-spec write on a non-zero or invalid JSON specs-instruction response
258- Artifact rules constrain only the specs being written and are never copied into output files