Skip to content

Commit d469d4d

Browse files
committed
fix(init): resolve the OpenCode global path properly and guard the block
Verifying --global against how the clients actually behave turned up two defects in it. OpenCode resolves its global config through XDG_CONFIG_HOME when that is set. It is supported but missing from the rules docs (anomalyco/opencode#6669), so hardcoding ~/.config wrote the block where nothing would read it and --global reported success having done nothing. The directory is ~/.config/opencode on every platform including Windows; OpenCode does not use %APPDATA%. A global block also reaches projects that never wired GrayMatter, where it was telling the model to call tools that are not in its toolbelt. It now guards on the tools existing, phrased as a capability check rather than a judgement call, since a judgement-shaped hedge is what made the old block ignorable in the first place. --global now applies to the wizard too, through one shared helper so the two paths cannot drift.
1 parent 6fa40f1 commit d469d4d

4 files changed

Lines changed: 113 additions & 22 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ Versioning: [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
1818
- New `knownAgents` table (`cmd_init_interactive.go`) as a single source of truth for agent → {writer, instructionFile} mapping.
1919

2020
**`graymatter init --global` (issue #17)**
21-
- Writes the managed memory block into `~/.claude/CLAUDE.md` and `~/.config/opencode/AGENTS.md`, the home-scoped files agents read no matter which project they are working in. Global installs no longer need `init` run in every repo just to get the instructions.
21+
- Writes the managed memory block into `~/.claude/CLAUDE.md` and `~/.config/opencode/AGENTS.md`, the home-scoped files agents read no matter which project they are working in. Global installs no longer need `init` run in every repo just to get the instructions. Works with `-i` too.
22+
- `XDG_CONFIG_HOME` is honoured for the OpenCode path. OpenCode resolves its global config through it, so hardcoding `~/.config` would have written where nothing reads. The directory is `~/.config/opencode` on every platform, Windows included; OpenCode does not use `%APPDATA%`.
2223
- Same marker-based upsert as the project files, so it stays idempotent and leaves your own global instructions untouched.
24+
- The block guards on the tools actually being present, since a global install reaches projects that never wired GrayMatter. It is a capability check, not a judgement call, so it cannot become the hedge that made the old block ignorable.
25+
- Deliberately not a `SKILL.md`. OpenCode loads skills lazily, advertising only name and description and leaving the body to the model's discretion, which puts the same "the model decides whether to bother" failure one level up. Instructions that must run before the first reply belong in a file that is always loaded.
2326

2427
### Fixed
2528

‎cmd/graymatter/cmd_init.go‎

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,17 @@ just the ones we auto-wire; any MCP-compatible client works over stdio
3636
(` + "`graymatter mcp serve`" + `) or HTTP (` + "`graymatter mcp serve --http :8080`" + `).`,
3737
RunE: func(cmd *cobra.Command, args []string) error {
3838
if interactive {
39-
return runInteractiveWizard(dataDir, ".", quiet)
39+
if err := runInteractiveWizard(dataDir, ".", quiet); err != nil {
40+
return err
41+
}
42+
// --global is orthogonal to which clients the wizard wired, so
43+
// it applies to both paths rather than only the flag-driven one.
44+
if global {
45+
for _, w := range installGlobalInstructions(quiet) {
46+
fmt.Fprintf(os.Stderr, "\n%s\n", w)
47+
}
48+
}
49+
return nil
4050
}
4151
dir := dataDir
4252
if err := os.MkdirAll(dir, 0o755); err != nil {
@@ -150,22 +160,7 @@ just the ones we auto-wire; any MCP-compatible client works over stdio
150160
// files, so agents pick up memory in projects that never ran init
151161
// (issue #17).
152162
if global {
153-
if !quiet {
154-
fmt.Println("\nGlobal agent instructions (apply in every project):")
155-
}
156-
for _, res := range writeGlobalInstructionFiles() {
157-
if res.warn != "" {
158-
warnings = append(warnings, res.warn)
159-
continue
160-
}
161-
if !quiet {
162-
glyph, note := "✓", ""
163-
if !res.changed {
164-
glyph, note = "·", " (already present)"
165-
}
166-
fmt.Printf(" %s %s%s\n", glyph, res.path, note)
167-
}
168-
}
163+
warnings = append(warnings, installGlobalInstructions(quiet)...)
169164
}
170165

171166
if !quiet {

‎cmd/graymatter/cmd_init_instructions.go‎

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,15 @@ const (
3939
const blockTmpl = `
4040
## Memory (GrayMatter)
4141
42-
You have persistent memory in this project through the ~graymatter~ MCP tools.
43-
Wiring the MCP server only makes those tools available; nothing calls them for
44-
you. That is your job, every session.
42+
You have persistent memory through the ~graymatter~ MCP tools. Wiring the MCP
43+
server only makes them available; nothing calls them for you. That is your job,
44+
every session.
45+
46+
This block can be installed globally, so it may reach a project that has no
47+
GrayMatter wired. If ~memory_search~ is not in your toolbelt for this session,
48+
skip the rest of this section. That is a check on what tools exist, not a
49+
judgement call about whether memory seems useful: if the tools are there,
50+
everything below applies.
4551
4652
### Your identity
4753
@@ -175,12 +181,33 @@ func globalInstructionPaths() ([]string, error) {
175181
if err != nil {
176182
return nil, err
177183
}
184+
opencodeDir, err := opencodeConfigDir()
185+
if err != nil {
186+
return nil, err
187+
}
178188
return []string{
179189
filepath.Join(home, ".claude", "CLAUDE.md"),
180-
filepath.Join(home, ".config", "opencode", "AGENTS.md"),
190+
filepath.Join(opencodeDir, "AGENTS.md"),
181191
}, nil
182192
}
183193

194+
// opencodeConfigDir resolves OpenCode's global config directory.
195+
//
196+
// It is ~/.config/opencode on every platform, Windows included: OpenCode does
197+
// not use %APPDATA%. XDG_CONFIG_HOME takes precedence when set, which OpenCode
198+
// supports but does not document on the rules page (anomalyco/opencode#6669) —
199+
// hardcoding ~/.config would silently write where nothing reads.
200+
func opencodeConfigDir() (string, error) {
201+
if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" {
202+
return filepath.Join(xdg, "opencode"), nil
203+
}
204+
home, err := resolveHome()
205+
if err != nil {
206+
return "", err
207+
}
208+
return filepath.Join(home, ".config", "opencode"), nil
209+
}
210+
184211
// writeGlobalInstructionFiles upserts the memory block into every path returned
185212
// by globalInstructionPaths. Returns one result per file, in a stable order.
186213
func writeGlobalInstructionFiles() []writeResult {
@@ -199,6 +226,30 @@ func writeGlobalInstructionFiles() []writeResult {
199226
return out
200227
}
201228

229+
// installGlobalInstructions writes the block into the home-scoped files and
230+
// prints what happened, returning any warnings for the caller to surface.
231+
// Shared by the plain --global path and the wizard so the two cannot drift.
232+
func installGlobalInstructions(quiet bool) []string {
233+
var warnings []string
234+
if !quiet {
235+
fmt.Println("\nGlobal agent instructions (apply in every project):")
236+
}
237+
for _, res := range writeGlobalInstructionFiles() {
238+
if res.warn != "" {
239+
warnings = append(warnings, res.warn)
240+
continue
241+
}
242+
if !quiet {
243+
glyph, note := "✓", ""
244+
if !res.changed {
245+
glyph, note = "·", " (already present)"
246+
}
247+
fmt.Printf(" %s %s%s\n", glyph, res.path, note)
248+
}
249+
}
250+
return warnings
251+
}
252+
202253
// hasInstructionsBlock reports whether path contains the managed block (or at
203254
// least mentions graymatter, for users who wrote their own briefing).
204255
// Used by `graymatter doctor`.

‎cmd/graymatter/cmd_init_instructions_test.go‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ func TestInstructionsBlock_IsProcedural(t *testing.T) {
131131
"root directory", // agent_id is derivable, not invented
132132
"checkpoint_resume",
133133
"checkpoint_save",
134+
// --global puts this block in projects that may not have GrayMatter
135+
// wired, so it has to guard on the tools existing. The guard is a
136+
// capability check on purpose: phrased as judgement it would recreate
137+
// the very hedge that made the old block ignorable.
138+
"not in your toolbelt",
139+
"not a\njudgement call",
134140
} {
135141
if !strings.Contains(block, want) {
136142
t.Errorf("block no longer contains %q", want)
@@ -162,6 +168,42 @@ func TestInstructionsBlock_IsProcedural(t *testing.T) {
162168
}
163169
}
164170

171+
// TestGlobalInstructionPaths_HonoursXDG pins where --global writes. OpenCode
172+
// resolves its global config through XDG_CONFIG_HOME when that is set, so
173+
// hardcoding ~/.config would put the block somewhere nothing reads and make
174+
// --global look like it worked while doing nothing.
175+
func TestGlobalInstructionPaths_HonoursXDG(t *testing.T) {
176+
home := t.TempDir()
177+
testHomeOverride = home
178+
t.Cleanup(func() { testHomeOverride = "" })
179+
180+
// Unset: ~/.config/opencode on every platform, Windows included.
181+
t.Setenv("XDG_CONFIG_HOME", "")
182+
paths, err := globalInstructionPaths()
183+
if err != nil {
184+
t.Fatal(err)
185+
}
186+
if want := filepath.Join(home, ".config", "opencode", "AGENTS.md"); paths[1] != want {
187+
t.Errorf("default opencode path = %q, want %q", paths[1], want)
188+
}
189+
190+
// Set: it wins.
191+
xdg := t.TempDir()
192+
t.Setenv("XDG_CONFIG_HOME", xdg)
193+
paths, err = globalInstructionPaths()
194+
if err != nil {
195+
t.Fatal(err)
196+
}
197+
if want := filepath.Join(xdg, "opencode", "AGENTS.md"); paths[1] != want {
198+
t.Errorf("XDG opencode path = %q, want %q", paths[1], want)
199+
}
200+
201+
// Claude Code's own path is home-based and unaffected by XDG.
202+
if want := filepath.Join(home, ".claude", "CLAUDE.md"); paths[0] != want {
203+
t.Errorf("claude path = %q, want %q", paths[0], want)
204+
}
205+
}
206+
165207
func TestWriteGlobalInstructionFiles(t *testing.T) {
166208
home := t.TempDir()
167209
testHomeOverride = home

0 commit comments

Comments
 (0)