@every-env/compound-plugin 0.9.0 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/.claude-plugin/marketplace.json +3 -3
  2. package/AGENTS.md +5 -1
  3. package/CHANGELOG.md +42 -0
  4. package/CLAUDE.md +3 -3
  5. package/README.md +49 -15
  6. package/docs/plans/2026-02-14-feat-auto-detect-install-and-gemini-sync-plan.md +360 -0
  7. package/docs/plans/2026-02-25-feat-windsurf-global-scope-support-plan.md +627 -0
  8. package/docs/plans/2026-03-01-feat-ce-command-aliases-backwards-compatible-deprecation-plan.md +261 -0
  9. package/docs/plans/feature_opencode-commands-as-md-and-config-merge.md +574 -0
  10. package/docs/solutions/adding-converter-target-providers.md +692 -0
  11. package/docs/solutions/plugin-versioning-requirements.md +3 -3
  12. package/docs/specs/windsurf.md +477 -0
  13. package/package.json +1 -1
  14. package/plans/landing-page-launchkit-refresh.md +2 -2
  15. package/plugins/compound-engineering/.claude-plugin/plugin.json +2 -2
  16. package/plugins/compound-engineering/CHANGELOG.md +72 -1
  17. package/plugins/compound-engineering/CLAUDE.md +9 -7
  18. package/plugins/compound-engineering/README.md +10 -7
  19. package/plugins/compound-engineering/agents/research/git-history-analyzer.md +1 -1
  20. package/plugins/compound-engineering/agents/research/learnings-researcher.md +1 -1
  21. package/plugins/compound-engineering/agents/review/code-simplicity-reviewer.md +1 -1
  22. package/plugins/compound-engineering/commands/ce/brainstorm.md +145 -0
  23. package/plugins/compound-engineering/commands/ce/compound.md +240 -0
  24. package/plugins/compound-engineering/commands/ce/plan.md +636 -0
  25. package/plugins/compound-engineering/commands/ce/review.md +525 -0
  26. package/plugins/compound-engineering/commands/ce/work.md +470 -0
  27. package/plugins/compound-engineering/commands/create-agent-skill.md +1 -1
  28. package/plugins/compound-engineering/commands/deepen-plan.md +6 -6
  29. package/plugins/compound-engineering/commands/deploy-docs.md +1 -1
  30. package/plugins/compound-engineering/commands/feature-video.md +15 -6
  31. package/plugins/compound-engineering/commands/heal-skill.md +1 -1
  32. package/plugins/compound-engineering/commands/lfg.md +3 -3
  33. package/plugins/compound-engineering/commands/slfg.md +3 -3
  34. package/plugins/compound-engineering/commands/test-xcode.md +2 -2
  35. package/plugins/compound-engineering/commands/workflows/brainstorm.md +4 -123
  36. package/plugins/compound-engineering/commands/workflows/compound.md +4 -234
  37. package/plugins/compound-engineering/commands/workflows/plan.md +4 -562
  38. package/plugins/compound-engineering/commands/workflows/review.md +4 -522
  39. package/plugins/compound-engineering/commands/workflows/work.md +4 -448
  40. package/plugins/compound-engineering/skills/brainstorming/SKILL.md +3 -3
  41. package/plugins/compound-engineering/skills/document-review/SKILL.md +1 -1
  42. package/plugins/compound-engineering/skills/file-todos/SKILL.md +1 -1
  43. package/plugins/compound-engineering/skills/git-worktree/SKILL.md +5 -5
  44. package/plugins/compound-engineering/skills/proof/SKILL.md +185 -0
  45. package/plugins/compound-engineering/skills/resolve-pr-parallel/SKILL.md +1 -1
  46. package/plugins/compound-engineering/skills/setup/SKILL.md +2 -2
  47. package/src/commands/convert.ts +101 -24
  48. package/src/commands/install.ts +102 -45
  49. package/src/commands/sync.ts +58 -38
  50. package/src/converters/claude-to-openclaw.ts +240 -0
  51. package/src/converters/claude-to-opencode.ts +12 -10
  52. package/src/converters/claude-to-qwen.ts +238 -0
  53. package/src/converters/claude-to-windsurf.ts +205 -0
  54. package/src/sync/gemini.ts +76 -0
  55. package/src/targets/index.ts +60 -1
  56. package/src/targets/openclaw.ts +96 -0
  57. package/src/targets/opencode.ts +76 -10
  58. package/src/targets/qwen.ts +64 -0
  59. package/src/targets/windsurf.ts +104 -0
  60. package/src/types/openclaw.ts +52 -0
  61. package/src/types/opencode.ts +7 -8
  62. package/src/types/qwen.ts +48 -0
  63. package/src/types/windsurf.ts +34 -0
  64. package/src/utils/detect-tools.ts +46 -0
  65. package/src/utils/files.ts +7 -0
  66. package/src/utils/resolve-output.ts +50 -0
  67. package/src/utils/secrets.ts +24 -0
  68. package/tests/cli.test.ts +78 -0
  69. package/tests/converter.test.ts +43 -10
  70. package/tests/detect-tools.test.ts +96 -0
  71. package/tests/openclaw-converter.test.ts +200 -0
  72. package/tests/opencode-writer.test.ts +142 -5
  73. package/tests/qwen-converter.test.ts +238 -0
  74. package/tests/resolve-output.test.ts +131 -0
  75. package/tests/sync-gemini.test.ts +106 -0
  76. package/tests/windsurf-converter.test.ts +573 -0
  77. package/tests/windsurf-writer.test.ts +359 -0
  78. package/docs/css/docs.css +0 -675
  79. package/docs/css/style.css +0 -2886
  80. package/docs/index.html +0 -1046
  81. package/docs/js/main.js +0 -225
  82. package/docs/pages/agents.html +0 -649
  83. package/docs/pages/changelog.html +0 -534
  84. package/docs/pages/commands.html +0 -523
  85. package/docs/pages/getting-started.html +0 -582
  86. package/docs/pages/mcp-servers.html +0 -409
  87. package/docs/pages/skills.html +0 -611
@@ -0,0 +1,574 @@
1
+ # Feature: OpenCode Commands as .md Files, Config Merge, and Permissions Default Fix
2
+
3
+ **Type:** feature + bug fix (consolidated)
4
+ **Date:** 2026-02-20
5
+ **Starting point:** Branch `main` at commit `174cd4c`
6
+ **Create feature branch:** `feature/opencode-commands-md-merge-permissions`
7
+ **Baseline tests:** 180 pass, 0 fail (run `bun test` to confirm before starting)
8
+
9
+ ---
10
+
11
+ ## Context
12
+
13
+ ### User-Facing Goal
14
+
15
+ When running `bunx @every-env/compound-plugin install compound-engineering --to opencode`, three problems exist:
16
+
17
+ 1. **Commands overwrite `opencode.json`**: Plugin commands are written into the `command` key of `opencode.json`, which replaces the user's existing configuration file (the writer does `writeJson(configPath, bundle.config)` — a full overwrite). The user loses their personal settings (model, theme, provider keys, MCP servers they previously configured).
18
+
19
+ 2. **Commands should be `.md` files, not JSON**: OpenCode supports defining commands as individual `.md` files in `~/.config/opencode/commands/`. This is additive and non-destructive — one file per command, never touches `opencode.json`.
20
+
21
+ 3. **`--permissions broad` is the default and pollutes global config**: The `--permissions` flag defaults to `"broad"`, which writes 14 `permission: allow` entries and 14 `tools: true` entries into `opencode.json` on every install. These are global settings that affect ALL OpenCode sessions, not just plugin commands. Even `--permissions from-commands` is semantically wrong — it unions per-command `allowedTools` restrictions into a single global block, which inverts restriction semantics (a command allowing only `Read` gets merged with one allowing `Bash`, producing global `bash: allow`).
22
+
23
+ ### Expected Behavior After This Plan
24
+
25
+ - Commands are written as `~/.config/opencode/commands/<name>.md` with YAML frontmatter (`description`, `model`). The `command` key is never written to `opencode.json`.
26
+ - `opencode.json` is deep-merged (not overwritten): existing user keys survive, plugin's MCP servers are added. User values win on conflict.
27
+ - `--permissions` defaults to `"none"` — no `permission` or `tools` entries are written to `opencode.json` unless the user explicitly passes `--permissions broad` or `--permissions from-commands`.
28
+
29
+ ### Relevant File Paths
30
+
31
+ | File | Current State on `main` | What Changes |
32
+ |---|---|---|
33
+ | `src/types/opencode.ts` | `OpenCodeBundle` has no `commandFiles` field. Has `OpenCodeCommandConfig` type and `command` field on `OpenCodeConfig`. | Add `OpenCodeCommandFile` type. Add `commandFiles` to `OpenCodeBundle`. Remove `OpenCodeCommandConfig` type and `command` field from `OpenCodeConfig`. |
34
+ | `src/converters/claude-to-opencode.ts` | `convertCommands()` returns `Record<string, OpenCodeCommandConfig>`. Result set on `config.command`. `applyPermissions()` writes `config.permission` and `config.tools`. | `convertCommands()` returns `OpenCodeCommandFile[]`. `config.command` is never set. No changes to `applyPermissions()` itself. |
35
+ | `src/targets/opencode.ts` | `writeOpenCodeBundle()` does `writeJson(configPath, bundle.config)` — full overwrite. No `commandsDir`. No merge logic. | Add `commandsDir` to path resolver. Write command `.md` files with backup. Replace overwrite with `mergeOpenCodeConfig()` — read existing, deep-merge, write back. |
36
+ | `src/commands/install.ts` | `--permissions` default is `"broad"` (line 51). | Change default to `"none"`. Update description string. |
37
+ | `src/utils/files.ts` | Has `readJson()`, `pathExists()`, `backupFile()` already. | No changes needed — utilities already exist. |
38
+ | `tests/converter.test.ts` | Tests reference `bundle.config.command` (lines 19, 74, 202-214, 243). Test `"maps commands, permissions, and agents"` tests `from-commands` mode. | Update all to use `bundle.commandFiles`. Rename permission-related test to clarify opt-in nature. |
39
+ | `tests/opencode-writer.test.ts` | 4 tests, none have `commandFiles` in bundles. `"backs up existing opencode.json before overwriting"` test expects full overwrite. | Add `commandFiles: []` to all existing bundles. Rewrite backup test to test merge behavior. Add new tests for command file writing and merge. |
40
+ | `tests/cli.test.ts` | 10 tests. None check for commands directory. | Add test for `--permissions none` default. Add test for command `.md` file existence. |
41
+ | `AGENTS.md` | Line 10: "Keep OpenCode output at `opencode.json` and `.opencode/{agents,skills,plugins}`." | Update to document commands go to `commands/<name>.md`, `opencode.json` is deep-merged. |
42
+ | `README.md` | Line 54: "OpenCode output is written to `~/.config/opencode` by default, with `opencode.json` at the root..." | Update to document `.md` command files, merge behavior, `--permissions` default. |
43
+
44
+ ### Prior Context (Pre-Investigation)
45
+
46
+ - **No `docs/decisions/` directory on `main`**: ADRs will be created fresh during this plan.
47
+ - **No prior plans touch the same area**: The `2026-02-08-feat-convert-local-md-settings-for-opencode-codex-plan.md` discusses path rewriting in command bodies but does not touch command output format or permissions.
48
+ - **OpenCode docs (confirmed via context7 MCP, library `/sst/opencode`):**
49
+ - Command `.md` frontmatter supports: `description`, `agent`, `model`. Does NOT support `permission` or `tools`. Placed in `~/.config/opencode/commands/` (global) or `.opencode/commands/` (project).
50
+ - Agent `.md` frontmatter supports: `description`, `mode`, `model`, `temperature`, `tools`, `permission`. Placed in `~/.config/opencode/agents/` or `.opencode/agents/`.
51
+ - `opencode.json` is the only place for: `mcp`, global `permission`, global `tools`, `model`, `provider`, `theme`, `server`, `compaction`, `watcher`, `share`.
52
+
53
+ ### Rejected Approaches
54
+
55
+ **1. Map `allowedTools` to per-agent `.md` frontmatter permissions.**
56
+ Rejected: Claude commands are not agents. There is no per-command-to-per-agent mapping. Commands don't specify which agent to run with. Even if they did, the union of multiple commands' restrictions onto a single agent's permissions loses the per-command scoping. Agent `.md` files DO support `permission` in frontmatter, but this would require creating synthetic agents just to hold permissions — misleading and fragile.
57
+
58
+ **2. Write permissions into command `.md` file frontmatter.**
59
+ Rejected: OpenCode command `.md` files only support `description`, `agent`, `model` in frontmatter. There is no `permission` or `tools` key. Confirmed via context7 docs. Anything else is silently ignored.
60
+
61
+ **3. Keep `from-commands` as the default but fix the flattening logic.**
62
+ Rejected: There is no correct way to flatten per-command tool restrictions into a single global permission block. Any flattening loses information and inverts semantics.
63
+
64
+ **4. Remove the `--permissions` flag entirely.**
65
+ Rejected: Some users may want to write permissions to `opencode.json` as a convenience. Keeping the flag with a changed default preserves optionality.
66
+
67
+ **5. Write commands as both `.md` files AND in `opencode.json` `command` block.**
68
+ Rejected: Redundant and defeats the purpose of avoiding `opencode.json` pollution. `.md` files are the sole output format.
69
+
70
+ ---
71
+
72
+ ## Decision Record
73
+
74
+ ### Decision 1: Commands emitted as individual `.md` files, never in `opencode.json`
75
+
76
+ - **Decision:** `convertCommands()` returns `OpenCodeCommandFile[]` (one `.md` file per command with YAML frontmatter). The `command` key is never set on `OpenCodeConfig`. The writer creates `<commandsDir>/<name>.md` for each file.
77
+ - **Context:** OpenCode supports two equivalent formats for commands — JSON in config and `.md` files. The `.md` format is additive (new files) rather than destructive (rewriting JSON). This is consistent with how agents and skills are already handled as `.md` files.
78
+ - **Alternatives rejected:** JSON-only (destructive), both formats (redundant). See Rejected Approaches above.
79
+ - **Assumptions:** OpenCode resolves commands from the `commands/` directory at runtime. Confirmed via docs.
80
+ - **Reversal trigger:** If OpenCode deprecates `.md` command files or the format changes incompatibly.
81
+
82
+ ### Decision 2: `opencode.json` deep-merged, not overwritten
83
+
84
+ - **Decision:** `writeOpenCodeBundle()` reads the existing `opencode.json` (if present), deep-merges plugin-provided keys (MCP servers, and optionally permission/tools if `--permissions` is not `none`) without overwriting user-set values, and writes the merged result. User keys always win on conflict.
85
+ - **Context:** Users have personal configuration in `opencode.json` (API keys, model preferences, themes, existing MCP servers). The current full-overwrite destroys all of this.
86
+ - **Alternatives rejected:** Skip writing `opencode.json` entirely — rejected because MCP servers must be written there (no `.md` alternative exists for MCP).
87
+ - **Assumptions:** `readJson()` and `pathExists()` already exist in `src/utils/files.ts`. Malformed JSON in existing file should warn and fall back to plugin-only config (do not crash, do not destroy).
88
+ - **Reversal trigger:** If OpenCode adds a separate mechanism for plugin MCP server registration that doesn't involve `opencode.json`.
89
+
90
+ ### Decision 3: `--permissions` default changed from `"broad"` to `"none"`
91
+
92
+ - **Decision:** The `--permissions` CLI flag default changes from `"broad"` to `"none"`. No `permission` or `tools` keys are written to `opencode.json` unless the user explicitly opts in.
93
+ - **Context:** `"broad"` silently writes 14 global tool permissions. `"from-commands"` has a semantic inversion bug (unions per-command restrictions into global allows). Both are destructive to user config. `applyPermissions()` already short-circuits on `"none"` (line 299: `if (mode === "none") return`), so no changes to that function are needed.
94
+ - **Alternatives rejected:** Fix `from-commands` flattening — impossible to do correctly with global-only target. Remove flag entirely — too restrictive for power users.
95
+ - **Assumptions:** The `applyPermissions()` function with mode `"none"` leaves `config.permission` and `config.tools` as `undefined`.
96
+ - **Reversal trigger:** If OpenCode adds per-command permission scoping, `from-commands` could become meaningful again.
97
+
98
+ ---
99
+
100
+ ## ADRs To Create
101
+
102
+ Create `docs/decisions/` directory (does not exist on `main`). ADRs follow `AGENTS.md` numbering convention: `0001-short-title.md`.
103
+
104
+ ### ADR 0001: OpenCode commands written as `.md` files, not in `opencode.json`
105
+
106
+ - **Context:** OpenCode supports two equivalent formats for custom commands. Writing to `opencode.json` requires overwriting or merging the user's config file. Writing `.md` files is additive and non-destructive.
107
+ - **Decision:** The OpenCode target always emits commands as individual `.md` files in the `commands/` subdirectory. The `command` key is never written to `opencode.json` by this tool.
108
+ - **Consequences:**
109
+ - Positive: Installs are non-destructive. Commands are visible as individual files, easy to inspect. Consistent with agents/skills handling.
110
+ - Negative: Users inspecting `opencode.json` won't see plugin commands; they must look in `commands/`.
111
+ - Neutral: Requires OpenCode >= the version with command file support (confirmed stable).
112
+
113
+ ### ADR 0002: Plugin merges into existing `opencode.json` rather than replacing it
114
+
115
+ - **Context:** Users have existing `opencode.json` files with personal configuration. The install command previously backed up and replaced this file entirely, destroying user settings.
116
+ - **Decision:** `writeOpenCodeBundle` reads existing `opencode.json` (if present), deep-merges plugin-provided keys without overwriting user-set values, and writes the merged result. User keys always win on conflict.
117
+ - **Consequences:**
118
+ - Positive: User config preserved across installs. Re-installs are idempotent for user-set values.
119
+ - Negative: Plugin cannot remove or update an MCP server entry if the user already has one with the same name.
120
+ - Neutral: Backup of pre-merge file is still created for safety.
121
+
122
+ ### ADR 0003: Global permissions not written to `opencode.json` by default
123
+
124
+ - **Context:** Claude commands carry `allowedTools` as per-command restrictions. OpenCode has no per-command permission mechanism. Writing per-command restrictions as global permissions is semantically incorrect and pollutes the user's global config.
125
+ - **Decision:** `--permissions` defaults to `"none"`. The plugin never writes `permission` or `tools` to `opencode.json` unless the user explicitly passes `--permissions broad` or `--permissions from-commands`.
126
+ - **Consequences:**
127
+ - Positive: User's global OpenCode permissions are never silently modified.
128
+ - Negative: Users who relied on auto-set permissions must now pass the flag explicitly.
129
+ - Neutral: The `"broad"` and `"from-commands"` modes still work as documented for opt-in use.
130
+
131
+ ---
132
+
133
+ ## Assumptions & Invalidation Triggers
134
+
135
+ - **Assumption:** OpenCode command `.md` frontmatter supports `description`, `agent`, `model` and does NOT support `permission` or `tools`.
136
+ - **If this changes:** The converter could emit per-command permissions in command frontmatter, making `from-commands` mode semantically correct. Phase 2 would need a new code path.
137
+
138
+ - **Assumption:** `readJson()` and `pathExists()` exist in `src/utils/files.ts` and work as expected.
139
+ - **If this changes:** Phase 4's merge logic needs alternative I/O utilities.
140
+
141
+ - **Assumption:** `applyPermissions()` with mode `"none"` returns early at line 299 and does not set `config.permission` or `config.tools`.
142
+ - **If this changes:** The merge logic in Phase 4 might still merge stale data. Verify before implementing.
143
+
144
+ - **Assumption:** 180 tests pass on `main` at commit `174cd4c` with `bun test`.
145
+ - **If this changes:** Do not proceed until the discrepancy is understood.
146
+
147
+ - **Assumption:** `formatFrontmatter()` in `src/utils/frontmatter.ts` handles `Record<string, unknown>` data and string body, producing valid YAML frontmatter. It filters out `undefined` values (line 35). It already supports nested objects/arrays via `formatYamlLine()`.
148
+ - **If this changes:** Phase 2's command file content generation would produce malformed output.
149
+
150
+ - **Assumption:** The `backupFile()` function in `src/utils/files.ts` returns `null` if the file does not exist, and returns the backup path if it does. It does NOT throw on missing files.
151
+ - **If this changes:** Phase 4's backup-before-write for command files would need error handling.
152
+
153
+ ---
154
+
155
+ ## Phases
156
+
157
+ ### Phase 1: Add `OpenCodeCommandFile` type and update `OpenCodeBundle`
158
+
159
+ **What:** In `src/types/opencode.ts`:
160
+ - Add a new type `OpenCodeCommandFile` with `name: string` (command name, used as filename stem) and `content: string` (full file content: YAML frontmatter + body).
161
+ - Add `commandFiles: OpenCodeCommandFile[]` field to `OpenCodeBundle`.
162
+ - Remove `command?: Record<string, OpenCodeCommandConfig>` from `OpenCodeConfig`.
163
+ - Remove the `OpenCodeCommandConfig` type entirely (lines 23-28).
164
+
165
+ **Why:** This is the foundational type change that all subsequent phases depend on. Commands move from the config object to individual file entries in the bundle.
166
+
167
+ **Test first:**
168
+
169
+ File: `tests/converter.test.ts`
170
+
171
+ Before making any type changes, update the test file to reflect the new shape. The existing tests will fail because they reference `bundle.config.command` and `OpenCodeBundle` doesn't have `commandFiles` yet.
172
+
173
+ Tests to modify (they will fail after type changes, then pass after Phase 2):
174
+ - `"maps commands, permissions, and agents"` (line 11): Change `bundle.config.command?.["workflows:review"]` to `bundle.commandFiles.find(f => f.name === "workflows:review")`. Change `bundle.config.command?.["plan_review"]` to `bundle.commandFiles.find(f => f.name === "plan_review")`.
175
+ - `"normalizes models and infers temperature"` (line 60): Change `bundle.config.command?.["workflows:work"]` to check `bundle.commandFiles.find(f => f.name === "workflows:work")` and parse its frontmatter for model.
176
+ - `"excludes commands with disable-model-invocation from command map"` (line 202): Change `bundle.config.command?.["deploy-docs"]` to `bundle.commandFiles.find(f => f.name === "deploy-docs")`.
177
+ - `"rewrites .claude/ paths to .opencode/ in command bodies"` (line 217): Change `bundle.config.command?.["review"]?.template` to access `bundle.commandFiles.find(f => f.name === "review")?.content`.
178
+
179
+ Also update `tests/opencode-writer.test.ts`:
180
+ - Add `commandFiles: []` to every `OpenCodeBundle` literal in all 4 existing tests (lines 20, 43, 67, 98). These bundles currently only have `config`, `agents`, `plugins`, `skillDirs`.
181
+
182
+ **Implementation:**
183
+
184
+ In `src/types/opencode.ts`:
185
+ 1. Remove lines 23-28 (`OpenCodeCommandConfig` type).
186
+ 2. Remove line 10 (`command?: Record<string, OpenCodeCommandConfig>`) from `OpenCodeConfig`.
187
+ 3. Add after line 47:
188
+ ```typescript
189
+ export type OpenCodeCommandFile = {
190
+ name: string // command name, used as the filename stem: <name>.md
191
+ content: string // full file content: YAML frontmatter + body
192
+ }
193
+ ```
194
+ 4. Add `commandFiles: OpenCodeCommandFile[]` to `OpenCodeBundle` (between `agents` and `plugins`).
195
+
196
+ In `src/converters/claude-to-opencode.ts`:
197
+ - Update the import on line 11: Remove `OpenCodeCommandConfig` from the import. Add `OpenCodeCommandFile`.
198
+
199
+ **Code comments required:**
200
+ - Above the `commandFiles` field in `OpenCodeBundle`: `// Commands are written as individual .md files, not in opencode.json. See ADR-001.`
201
+
202
+ **Verification:** `bun test` will show failures in converter tests (they reference the old command format). This is expected — Phase 2 fixes them.
203
+
204
+ ---
205
+
206
+ ### Phase 2: Convert `convertCommands()` to emit `.md` command files
207
+
208
+ **What:** In `src/converters/claude-to-opencode.ts`:
209
+ - Rewrite `convertCommands()` (line 114) to return `OpenCodeCommandFile[]` instead of `Record<string, OpenCodeCommandConfig>`.
210
+ - Each command becomes a `.md` file with YAML frontmatter (`description`, optionally `model`) and body (the template text with Claude path rewriting applied).
211
+ - In `convertClaudeToOpenCode()` (line 64): replace `commandMap` with `commandFiles`. Remove `config.command` assignment. Add `commandFiles` to returned bundle.
212
+
213
+ **Why:** This is the core conversion logic change that implements ADR-001.
214
+
215
+ **Test first:**
216
+
217
+ File: `tests/converter.test.ts`
218
+
219
+ The tests were already updated in Phase 1 to reference `bundle.commandFiles`. Now they need to pass. Specific assertions:
220
+
221
+ 1. Rename `"maps commands, permissions, and agents"` to `"from-commands mode: maps allowedTools to global permission block"` — to clarify this tests an opt-in mode, not the default.
222
+ - Assert `bundle.config.command` is `undefined` (it no longer exists on the type, but accessing it returns `undefined`).
223
+ - Assert `bundle.commandFiles.find(f => f.name === "workflows:review")` is defined.
224
+ - Assert `bundle.commandFiles.find(f => f.name === "plan_review")` is defined.
225
+ - Permission assertions remain unchanged (they test `from-commands` mode explicitly).
226
+
227
+ 2. `"normalizes models and infers temperature"`:
228
+ - Find `workflows:work` in `bundle.commandFiles`, parse its frontmatter with `parseFrontmatter()`, assert `data.model === "openai/gpt-4o"`.
229
+
230
+ 3. `"excludes commands with disable-model-invocation from command map"` — rename to `"excludes commands with disable-model-invocation from commandFiles"`:
231
+ - Assert `bundle.commandFiles.find(f => f.name === "deploy-docs")` is `undefined`.
232
+ - Assert `bundle.commandFiles.find(f => f.name === "workflows:review")` is defined.
233
+
234
+ 4. `"rewrites .claude/ paths to .opencode/ in command bodies"`:
235
+ - Find `review` in `bundle.commandFiles`, assert `content` contains `"compound-engineering.local.md"`.
236
+
237
+ 5. Add NEW test: `"command .md files include description in frontmatter"`:
238
+ - Create a minimal `ClaudePlugin` with one command (`name: "test-cmd"`, `description: "Test description"`, `body: "Do the thing"`).
239
+ - Convert with `permissions: "none"`.
240
+ - Find the command file, parse frontmatter, assert `data.description === "Test description"`.
241
+ - Assert the body (after frontmatter) contains `"Do the thing"`.
242
+
243
+ **Implementation:**
244
+
245
+ In `src/converters/claude-to-opencode.ts`:
246
+
247
+ Replace lines 114-128 (`convertCommands` function):
248
+ ```typescript
249
+ // Commands are written as individual .md files rather than entries in opencode.json.
250
+ // Chosen over JSON map because opencode resolves commands by filename at runtime (ADR-001).
251
+ function convertCommands(commands: ClaudeCommand[]): OpenCodeCommandFile[] {
252
+ const files: OpenCodeCommandFile[] = []
253
+ for (const command of commands) {
254
+ if (command.disableModelInvocation) continue
255
+ const frontmatter: Record<string, unknown> = {
256
+ description: command.description,
257
+ }
258
+ if (command.model && command.model !== "inherit") {
259
+ frontmatter.model = normalizeModel(command.model)
260
+ }
261
+ const content = formatFrontmatter(frontmatter, rewriteClaudePaths(command.body))
262
+ files.push({ name: command.name, content })
263
+ }
264
+ return files
265
+ }
266
+ ```
267
+
268
+ Replace lines 64-87 (`convertClaudeToOpenCode` function body):
269
+ - Change line 69: `const commandFiles = convertCommands(plugin.commands)`
270
+ - Change lines 73-77 (config construction): Remove the `command: ...` line. Config should only have `$schema` and `mcp`.
271
+ - Change line 81-86 (return): Replace `plugins` in the return with `commandFiles, plugins` (add `commandFiles` field to returned bundle).
272
+
273
+ **Code comments required:**
274
+ - Above `convertCommands()`: `// Commands are written as individual .md files rather than entries in opencode.json.` and `// Chosen over JSON map because opencode resolves commands by filename at runtime (ADR-001).`
275
+
276
+ **Verification:** Run `bun test tests/converter.test.ts`. All converter tests must pass. Then run `bun test` — writer tests should still fail (they expect the old bundle shape; fixed in Phase 1's test updates) but converter tests pass.
277
+
278
+ ---
279
+
280
+ ### Phase 3: Add `commandsDir` to path resolver and write command files
281
+
282
+ **What:** In `src/targets/opencode.ts`:
283
+ - Add `commandsDir` to the return value of `resolveOpenCodePaths()` for both branches (global and custom output dir).
284
+ - In `writeOpenCodeBundle()`, iterate `bundle.commandFiles` and write each as `<commandsDir>/<name>.md` with backup-before-overwrite.
285
+
286
+ **Why:** This creates the file output mechanism for command `.md` files. Separated from Phase 4 (merge logic) for testability.
287
+
288
+ **Test first:**
289
+
290
+ File: `tests/opencode-writer.test.ts`
291
+
292
+ Add these new tests:
293
+
294
+ 1. `"writes command files as .md in commands/ directory"`:
295
+ - Create a bundle with one `commandFiles` entry: `{ name: "my-cmd", content: "---\ndescription: Test\n---\n\nDo something." }`.
296
+ - Use an output root of `path.join(tempRoot, ".config", "opencode")` (global-style).
297
+ - Assert `exists(path.join(outputRoot, "commands", "my-cmd.md"))` is true.
298
+ - Read the file, assert content matches (with trailing newline: `content + "\n"`).
299
+
300
+ 2. `"backs up existing command .md file before overwriting"`:
301
+ - Pre-create `commands/my-cmd.md` with old content.
302
+ - Write a bundle with a `commandFiles` entry for `my-cmd`.
303
+ - Assert a `.bak.` file exists in `commands/` directory.
304
+ - Assert new content is written.
305
+
306
+ **Implementation:**
307
+
308
+ In `resolveOpenCodePaths()`:
309
+ - In the global branch (line 39-46): Add `commandsDir: path.join(outputRoot, "commands")` with comment: `// .md command files; alternative to the command key in opencode.json`
310
+ - In the custom branch (line 49-56): Add `commandsDir: path.join(outputRoot, ".opencode", "commands")` with same comment.
311
+
312
+ In `writeOpenCodeBundle()`:
313
+ - After the agents loop (line 18), add:
314
+ ```typescript
315
+ const commandsDir = paths.commandsDir
316
+ for (const commandFile of bundle.commandFiles) {
317
+ const dest = path.join(commandsDir, `${commandFile.name}.md`)
318
+ const cmdBackupPath = await backupFile(dest)
319
+ if (cmdBackupPath) {
320
+ console.log(`Backed up existing command file to ${cmdBackupPath}`)
321
+ }
322
+ await writeText(dest, commandFile.content + "\n")
323
+ }
324
+ ```
325
+
326
+ **Code comments required:**
327
+ - Inline comment on `commandsDir` in both `resolveOpenCodePaths` branches: `// .md command files; alternative to the command key in opencode.json`
328
+
329
+ **Verification:** Run `bun test tests/opencode-writer.test.ts`. The two new command file tests must pass. Existing tests must still pass (they have `commandFiles: []` from Phase 1 updates).
330
+
331
+ ---
332
+
333
+ ### Phase 4: Replace config overwrite with deep-merge
334
+
335
+ **What:** In `src/targets/opencode.ts`:
336
+ - Replace `writeJson(paths.configPath, bundle.config)` (line 13) with a call to a new `mergeOpenCodeConfig()` function.
337
+ - `mergeOpenCodeConfig()` reads the existing `opencode.json` (if present), merges plugin-provided keys using user-wins-on-conflict strategy, and returns the merged config.
338
+ - Import `pathExists` and `readJson` from `../utils/files` (add to existing import on line 2).
339
+
340
+ **Why:** This implements ADR-002 — the user's existing config is preserved across installs.
341
+
342
+ **Test first:**
343
+
344
+ File: `tests/opencode-writer.test.ts`
345
+
346
+ Modify existing test and add new tests:
347
+
348
+ 1. Rename `"backs up existing opencode.json before overwriting"` (line 88) to `"merges plugin config into existing opencode.json without destroying user keys"`:
349
+ - Pre-create `opencode.json` with `{ $schema: "https://opencode.ai/config.json", custom: "value" }`.
350
+ - Write a bundle with `config: { $schema: "...", mcp: { "plugin-server": { type: "local", command: "uvx", args: ["plugin-srv"] } } }`.
351
+ - Assert merged config has BOTH `custom: "value"` (user key) AND `mcp["plugin-server"]` (plugin key).
352
+ - Assert backup file exists with original content.
353
+
354
+ 2. NEW: `"merges mcp servers without overwriting user entries"`:
355
+ - Pre-create `opencode.json` with `{ mcp: { "user-server": { type: "local", command: "uvx", args: ["user-srv"] } } }`.
356
+ - Write a bundle with `config.mcp` containing both `"plugin-server"` (new) and `"user-server"` (conflict — different args).
357
+ - Assert both servers exist in merged output.
358
+ - Assert `user-server` keeps user's original args (user wins on conflict).
359
+ - Assert `plugin-server` is present with plugin's args.
360
+
361
+ 3. NEW: `"preserves unrelated user keys when merging opencode.json"`:
362
+ - Pre-create `opencode.json` with `{ model: "my-model", theme: "dark", mcp: {} }`.
363
+ - Write a bundle with `config: { $schema: "...", mcp: { "plugin-server": ... }, permission: { "bash": "allow" } }`.
364
+ - Assert `model` and `theme` are preserved.
365
+ - Assert plugin additions are present.
366
+
367
+ **Implementation:**
368
+
369
+ Add to imports in `src/targets/opencode.ts` line 2:
370
+ ```typescript
371
+ import { backupFile, copyDir, ensureDir, pathExists, readJson, writeJson, writeText } from "../utils/files"
372
+ import type { OpenCodeBundle, OpenCodeConfig } from "../types/opencode"
373
+ ```
374
+
375
+ Add `mergeOpenCodeConfig()` function:
376
+ ```typescript
377
+ async function mergeOpenCodeConfig(
378
+ configPath: string,
379
+ incoming: OpenCodeConfig,
380
+ ): Promise<OpenCodeConfig> {
381
+ // If no existing config, write plugin config as-is
382
+ if (!(await pathExists(configPath))) return incoming
383
+
384
+ let existing: OpenCodeConfig
385
+ try {
386
+ existing = await readJson<OpenCodeConfig>(configPath)
387
+ } catch {
388
+ // Safety first per AGENTS.md -- do not destroy user data even if their config is malformed.
389
+ // Warn and fall back to plugin-only config rather than crashing.
390
+ console.warn(
391
+ `Warning: existing ${configPath} is not valid JSON. Writing plugin config without merging.`
392
+ )
393
+ return incoming
394
+ }
395
+
396
+ // User config wins on conflict -- see ADR-002
397
+ // MCP servers: add plugin entries, skip keys already in user config.
398
+ const mergedMcp = {
399
+ ...(incoming.mcp ?? {}),
400
+ ...(existing.mcp ?? {}), // existing takes precedence (overwrites same-named plugin entries)
401
+ }
402
+
403
+ // Permission: add plugin entries, skip keys already in user config.
404
+ const mergedPermission = incoming.permission
405
+ ? {
406
+ ...(incoming.permission),
407
+ ...(existing.permission ?? {}), // existing takes precedence
408
+ }
409
+ : existing.permission
410
+
411
+ // Tools: same pattern
412
+ const mergedTools = incoming.tools
413
+ ? {
414
+ ...(incoming.tools),
415
+ ...(existing.tools ?? {}),
416
+ }
417
+ : existing.tools
418
+
419
+ return {
420
+ ...existing, // all user keys preserved
421
+ $schema: incoming.$schema ?? existing.$schema,
422
+ mcp: Object.keys(mergedMcp).length > 0 ? mergedMcp : undefined,
423
+ permission: mergedPermission,
424
+ tools: mergedTools,
425
+ }
426
+ }
427
+ ```
428
+
429
+ In `writeOpenCodeBundle()`, replace line 13 (`await writeJson(paths.configPath, bundle.config)`) with:
430
+ ```typescript
431
+ const merged = await mergeOpenCodeConfig(paths.configPath, bundle.config)
432
+ await writeJson(paths.configPath, merged)
433
+ ```
434
+
435
+ **Code comments required:**
436
+ - Above `mergeOpenCodeConfig()`: `// Merges plugin config into existing opencode.json. User keys win on conflict. See ADR-002.`
437
+ - On the `...(existing.mcp ?? {})` line: `// existing takes precedence (overwrites same-named plugin entries)`
438
+ - On malformed JSON catch: `// Safety first per AGENTS.md -- do not destroy user data even if their config is malformed.`
439
+
440
+ **Verification:** Run `bun test tests/opencode-writer.test.ts`. All tests must pass including the renamed test and the 2 new merge tests.
441
+
442
+ ---
443
+
444
+ ### Phase 5: Change `--permissions` default to `"none"`
445
+
446
+ **What:** In `src/commands/install.ts`, change line 51 `default: "broad"` to `default: "none"`. Update the description string.
447
+
448
+ **Why:** This implements ADR-003 — stops polluting user's global config with permissions by default.
449
+
450
+ **Test first:**
451
+
452
+ File: `tests/cli.test.ts`
453
+
454
+ Add these tests:
455
+
456
+ 1. `"install --to opencode uses permissions:none by default"`:
457
+ - Run install with no `--permissions` flag against the fixture plugin.
458
+ - Read the written `opencode.json`.
459
+ - Assert it does NOT contain a `permission` key.
460
+ - Assert it does NOT contain a `tools` key.
461
+
462
+ 2. `"install --to opencode --permissions broad writes permission block"`:
463
+ - Run install with `--permissions broad` against the fixture plugin.
464
+ - Read the written `opencode.json`.
465
+ - Assert it DOES contain a `permission` key with values.
466
+
467
+ **Implementation:**
468
+
469
+ In `src/commands/install.ts`:
470
+ - Line 51: Change `default: "broad"` to `default: "none"`.
471
+ - Line 52: Change description to `"Permission mapping written to opencode.json: none (default) | broad | from-commands"`.
472
+
473
+ **Code comments required:**
474
+ - On the `default: "none"` line: `// Default is "none" -- writing global permissions to opencode.json pollutes user config. See ADR-003.`
475
+
476
+ **Verification:** Run `bun test tests/cli.test.ts`. All CLI tests must pass including the 2 new permission tests. Then run `bun test` — all tests (180 original + new ones) must pass.
477
+
478
+ ---
479
+
480
+ ### Phase 6: Update `AGENTS.md` and `README.md`
481
+
482
+ **What:** Update documentation to reflect all three changes.
483
+
484
+ **Why:** Keeps docs accurate for future contributors and users.
485
+
486
+ **Test first:** No tests required for documentation changes.
487
+
488
+ **Implementation:**
489
+
490
+ In `AGENTS.md` line 10, replace:
491
+ ```
492
+ - **Output Paths:** Keep OpenCode output at `opencode.json` and `.opencode/{agents,skills,plugins}`.
493
+ ```
494
+ with:
495
+ ```
496
+ - **Output Paths:** Keep OpenCode output at `opencode.json` and `.opencode/{agents,skills,plugins}`. For OpenCode, commands go to `~/.config/opencode/commands/<name>.md`; `opencode.json` is deep-merged (never overwritten wholesale).
497
+ ```
498
+
499
+ In `README.md` line 54, replace:
500
+ ```
501
+ OpenCode output is written to `~/.config/opencode` by default, with `opencode.json` at the root and `agents/`, `skills/`, and `plugins/` alongside it.
502
+ ```
503
+ with:
504
+ ```
505
+ OpenCode output is written to `~/.config/opencode` by default. Commands are written as individual `.md` files to `~/.config/opencode/commands/<name>.md`. Agents, skills, and plugins are written to the corresponding subdirectories alongside. `opencode.json` (MCP servers) is deep-merged into any existing file -- user keys such as `model`, `theme`, and `provider` are preserved, and user values win on conflicts. Command files are backed up before being overwritten.
506
+ ```
507
+
508
+ Also update `AGENTS.md` to add a Repository Docs Conventions section if not present:
509
+ ```
510
+ ## Repository Docs Conventions
511
+
512
+ - **ADRs** live in `docs/decisions/` and are numbered with 4-digit zero-padding: `0001-short-title.md`, `0002-short-title.md`, etc.
513
+ - **Orchestrator run reports** live in `docs/reports/`.
514
+
515
+ When recording a significant decision (new provider, output format change, merge strategy), create an ADR in `docs/decisions/` following the numbering sequence.
516
+ ```
517
+
518
+ **Code comments required:** None.
519
+
520
+ **Verification:** Read the updated files and confirm accuracy. Run `bun test` to confirm no regressions.
521
+
522
+ ---
523
+
524
+ ## TDD Enforcement
525
+
526
+ The executing agent MUST follow this sequence for every phase that touches source code:
527
+
528
+ 1. Write the test(s) first in the test file.
529
+ 2. Run `bun test <test-file>` and confirm the new/modified tests FAIL (red).
530
+ 3. Implement the code change.
531
+ 4. Run `bun test <test-file>` and confirm the new/modified tests PASS (green).
532
+ 5. Run `bun test` (all tests) and confirm no regressions.
533
+
534
+ **Exception:** Phase 6 is documentation only. Run `bun test` after to confirm no regressions but no red/green cycle needed.
535
+
536
+ **Note on Phase 1:** Type changes alone will cause test failures. Phase 1 and Phase 2 are tightly coupled — the tests updated in Phase 1 will not pass until Phase 2's implementation is complete. The executing agent should:
537
+ 1. Update tests in Phase 1 (expect them to fail — both due to type errors and logic changes).
538
+ 2. Implement type changes in Phase 1.
539
+ 3. Implement converter changes in Phase 2.
540
+ 4. Confirm all converter tests pass after Phase 2.
541
+
542
+ ---
543
+
544
+ ## Constraints
545
+
546
+ **Do not modify:**
547
+ - `src/converters/claude-to-opencode.ts` lines 294-417 (`applyPermissions()`, `normalizeTool()`, `parseToolSpec()`, `normalizePattern()`) — these functions are correct for `"broad"` and `"from-commands"` modes. Only the default that triggers them is changing.
548
+ - Any files under `tests/fixtures/` — these are data files, not test logic.
549
+ - `src/types/claude.ts` — no changes to source types.
550
+ - `src/parsers/claude.ts` — no changes to parser logic.
551
+ - `src/utils/files.ts` — all needed utilities already exist. Do not add new utility functions.
552
+ - `src/utils/frontmatter.ts` — already handles the needed formatting.
553
+
554
+ **Dependencies not to add:** None. No new npm/bun packages.
555
+
556
+ **Patterns to follow:**
557
+ - Existing writer tests in `tests/opencode-writer.test.ts` use `fs.mkdtemp()` for temp directories and the local `exists()` helper function.
558
+ - Existing CLI tests in `tests/cli.test.ts` use `Bun.spawn()` to invoke the CLI.
559
+ - Existing converter tests in `tests/converter.test.ts` use `loadClaudePlugin(fixtureRoot)` for real fixtures and inline `ClaudePlugin` objects for isolated tests.
560
+ - ADR format: Follow `AGENTS.md` numbering convention `0001-short-title.md` with sections: Status, Date, Context, Decision, Consequences, Plan Reference.
561
+ - Commits: Use conventional commit format. Reference ADRs in commit bodies.
562
+ - Branch: Create `feature/opencode-commands-md-merge-permissions` from `main`.
563
+
564
+ ## Final Checklist
565
+
566
+ After all phases complete:
567
+ - [ ] `bun test` passes all tests (180 original + new ones, 0 fail)
568
+ - [ ] `docs/decisions/0001-opencode-command-output-format.md` exists
569
+ - [ ] `docs/decisions/0002-opencode-json-merge-strategy.md` exists
570
+ - [ ] `docs/decisions/0003-opencode-permissions-default-none.md` exists
571
+ - [ ] `opencode.json` is never fully overwritten — merge logic confirmed by test
572
+ - [ ] Commands are written as `.md` files — confirmed by test
573
+ - [ ] `--permissions` defaults to `"none"` — confirmed by CLI test
574
+ - [ ] `AGENTS.md` and `README.md` updated to reflect new behavior