The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
fix(opencode): resolve the user plugin directory through XDG_CONFIG_HOME (#928)
Rudy Mizrahi Celekli93f1c8da9be3
diff --git a/cli/beacon/internal/endpoint/harness/harness.go b/cli/beacon/internal/endpoint/harness/harness.goindex 45af903e..126e5ea5 100644--- a/cli/beacon/internal/endpoint/harness/harness.go+++ b/cli/beacon/internal/endpoint/harness/harness.go@@ -186,9 +186,13 @@ func DiscoverOpenCode() Harness {h := Harness{Name: "opencode", DisplayName: "opencode", Capability: "plugin"}detectExecutable(&h, "opencode")home, _ := os.UserHomeDir()- pluginPath := filepath.Join(home, ".config", "opencode", "plugins", "beacon.ts")+ configDir, dirErr := hooks.OpenCodeConfigDir(home)+ pluginPath := filepath.Join(configDir, "plugins", "beacon.ts")+ if dirErr != nil {+ pluginPath = filepath.Join(".config", "opencode", "plugins", "beacon.ts")+ }h.ConfigPath = pluginPath- if !h.Detected && dirExists(filepath.Join(home, ".config", "opencode")) {+ if !h.Detected && dirErr == nil && dirExists(configDir) {h.Detected = true}if fileExists(pluginPath) {diff --git a/cli/beacon/internal/endpoint/harness/opencode_test.go b/cli/beacon/internal/endpoint/harness/opencode_test.gonew file mode 100644index 00000000..53794d29--- /dev/null+++ b/cli/beacon/internal/endpoint/harness/opencode_test.go@@ -0,0 +1,42 @@+package harness++import (+ "os"+ "path/filepath"+ "testing"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/testenv"+)++// With XDG_CONFIG_HOME set, OpenCode's config directory is $XDG_CONFIG_HOME/opencode, so that is the+// directory whose presence counts as evidence of the runtime and whose plugin discovery reports.+func TestDiscoverOpenCodeHonorsXDGConfigHome(t *testing.T) {+ home := t.TempDir()+ testenv.SetHome(t, home)+ t.Setenv("PATH", t.TempDir())+ xdg := t.TempDir()+ t.Setenv("XDG_CONFIG_HOME", xdg)+ pluginPath := filepath.Join(xdg, "opencode", "plugins", "beacon.ts")+ if err := os.MkdirAll(filepath.Dir(pluginPath), 0755); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(pluginPath, []byte("// beacon-managed-opencode-plugin:v1"), 0600); err != nil {+ t.Fatal(err)+ }++ h := DiscoverOpenCode()+ if !h.Detected || h.ConfigPath != pluginPath || h.TelemetryStatus != TelemetryEnabled {+ t.Fatalf("DiscoverOpenCode = %#v, want detected, enabled at %q", h, pluginPath)+ }++ // A stale ~/.config/opencode is not evidence once XDG_CONFIG_HOME points elsewhere.+ if err := os.RemoveAll(filepath.Join(xdg, "opencode")); err != nil {+ t.Fatal(err)+ }+ if err := os.MkdirAll(filepath.Join(home, ".config", "opencode"), 0755); err != nil {+ t.Fatal(err)+ }+ if h := DiscoverOpenCode(); h.Detected {+ t.Fatalf("DiscoverOpenCode detected opencode from ~/.config/opencode with XDG_CONFIG_HOME set elsewhere: %#v", h)+ }+}diff --git a/cli/beacon/internal/endpoint/hooks/opencode.go b/cli/beacon/internal/endpoint/hooks/opencode.goindex d8fb9a13..30afcc46 100644--- a/cli/beacon/internal/endpoint/hooks/opencode.go+++ b/cli/beacon/internal/endpoint/hooks/opencode.go@@ -137,14 +137,42 @@ func opencodePluginPath(level Level) (string, error) {return filepath.Join(dir, opencodePluginFileName), nil}+// OpenCodeConfigDir returns OpenCode's global config directory for the given home directory.+//+// OpenCode roots it at `$XDG_CONFIG_HOME/opencode` and falls back to `~/.config/opencode`+// (packages/util/src/global-roots.ts: `process.env.XDG_CONFIG_HOME || path.join(home, ".config")`).+// An empty value falls back and nothing is trimmed; a relative value is resolved against the+// working directory, so it is made absolute here. The home directory is only needed for the+// default, so an XDG_CONFIG_HOME override resolves without one.+//+// Exported because discovery and the inventory scan report on the same plugin the installer+// writes and must not rebuild the path themselves.+func OpenCodeConfigDir(home string) (string, error) {+ if base := os.Getenv("XDG_CONFIG_HOME"); base != "" {+ abs, err := filepath.Abs(base)+ if err != nil {+ return "", err+ }+ return filepath.Join(abs, "opencode"), nil+ }+ if home == "" {+ return "", fmt.Errorf("home directory is required to resolve opencode's config directory")+ }+ return filepath.Join(home, ".config", "opencode"), nil+}+func opencodePluginDir(level Level) (string, error) {switch level {case "", LevelUser:home, err := os.UserHomeDir()+ if err != nil && os.Getenv("XDG_CONFIG_HOME") == "" {+ return "", err+ }+ dir, err := OpenCodeConfigDir(home)if err != nil {return "", err}- return filepath.Join(home, ".config", "opencode", "plugins"), nil+ return filepath.Join(dir, "plugins"), nilcase LevelProject:cwd, err := os.Getwd()if err != nil {diff --git a/cli/beacon/internal/endpoint/hooks/opencode_test.go b/cli/beacon/internal/endpoint/hooks/opencode_test.goindex 6f0df372..c4c7880a 100644--- a/cli/beacon/internal/endpoint/hooks/opencode_test.go+++ b/cli/beacon/internal/endpoint/hooks/opencode_test.go@@ -149,6 +149,7 @@ func TestOpenCodeInstalledUsesManagedMarker(t *testing.T) {func TestOpenCodePluginPathLevels(t *testing.T) {home := t.TempDir()testenv.SetHome(t, home)+ t.Setenv("XDG_CONFIG_HOME", "")if got, want := mustOpenCodePluginPath(t, LevelUser), filepath.Join(home, ".config", "opencode", "plugins", "beacon.ts"); got != want {t.Fatalf("user plugin path = %q, want %q", got, want)}@@ -168,3 +169,52 @@ func mustOpenCodePluginPath(t *testing.T, level Level) string {}return path}++// OpenCode roots its config directory at $XDG_CONFIG_HOME/opencode when that is set+// (packages/util/src/global-roots.ts). An install that wrote ~/.config/opencode/plugins would report+// success while OpenCode loaded plugins from the moved directory.+func TestOpenCodePluginPathHonorsXDGConfigHome(t *testing.T) {+ home := t.TempDir()+ testenv.SetHome(t, home)+ project := t.TempDir()+ t.Chdir(project)+ cwd, err := os.Getwd()+ if err != nil {+ t.Fatal(err)+ }++ xdg := filepath.Join(t.TempDir(), "xdg")+ for name, tc := range map[string]struct{ value, want string }{+ "absolute": {xdg, filepath.Join(xdg, "opencode", "plugins", "beacon.ts")},+ "relative": {"rel-xdg", filepath.Join(cwd, "rel-xdg", "opencode", "plugins", "beacon.ts")},+ "empty": {"", filepath.Join(home, ".config", "opencode", "plugins", "beacon.ts")},+ } {+ t.Run(name, func(t *testing.T) {+ t.Setenv("XDG_CONFIG_HOME", tc.value)+ if got := mustOpenCodePluginPath(t, LevelUser); got != tc.want {+ t.Fatalf("user plugin path = %q, want %q", got, tc.want)+ }+ // The project plugin never moves.+ if got, want := mustOpenCodePluginPath(t, LevelProject), filepath.Join(cwd, ".opencode", "plugins", "beacon.ts"); got != want {+ t.Fatalf("project plugin path = %q, want %q", got, want)+ }+ })+ }++ // The status check goes through the same resolver as the installer.+ t.Setenv("XDG_CONFIG_HOME", xdg)+ want := filepath.Join(xdg, "opencode", "plugins", "beacon.ts")+ if err := os.MkdirAll(filepath.Dir(want), 0755); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(want, []byte("// "+opencodeManagedPluginMarker), 0600); err != nil {+ t.Fatal(err)+ }+ opts := OpenCodeOptions{Level: LevelUser, UserMode: true}+ if status := OpenCodeHookStatus(opts); status.PluginPath != want {+ t.Fatalf("OpenCodeHookStatus plugin path = %q, want %q", status.PluginPath, want)+ }+ if !IsOpenCodeInstalled(opts) {+ t.Fatalf("IsOpenCodeInstalled = false for a managed plugin at %q", want)+ }+}diff --git a/cli/beacon/internal/endpoint/inventory/inventory.go b/cli/beacon/internal/endpoint/inventory/inventory.goindex d994b518..8b98a308 100644--- a/cli/beacon/internal/endpoint/inventory/inventory.go+++ b/cli/beacon/internal/endpoint/inventory/inventory.go@@ -342,11 +342,13 @@ func copilotCandidates(home string) []candidate {return []candidate{{runtime: "copilot_cli", path: shellProfilePath(home), scope: ScopeUser, format: formatMetadataOnly, kind: KindProfile}}}+// opencodeCandidates resolves the user plugin through XDG_CONFIG_HOME, as OpenCode does.func opencodeCandidates(home, wd string) []candidate {- return []candidate{- {runtime: "opencode", path: filepath.Join(home, ".config", "opencode", "plugins", "beacon.ts"), scope: ScopeUser, format: formatMetadataOnly, kind: KindPlugin},- {runtime: "opencode", path: filepath.Join(wd, ".opencode", "plugins", "beacon.ts"), scope: ScopeProject, format: formatMetadataOnly, kind: KindPlugin},+ items := []candidate{}+ if dir, err := hooks.OpenCodeConfigDir(home); err == nil {+ items = append(items, candidate{runtime: "opencode", path: filepath.Join(dir, "plugins", "beacon.ts"), scope: ScopeUser, format: formatMetadataOnly, kind: KindPlugin})}+ return append(items, candidate{runtime: "opencode", path: filepath.Join(wd, ".opencode", "plugins", "beacon.ts"), scope: ScopeProject, format: formatMetadataOnly, kind: KindPlugin})}// clineCandidates resolves the user plugin through CLINE_DIR, as Cline does. Project scope isdiff --git a/cli/beacon/internal/endpoint/inventory/inventory_test.go b/cli/beacon/internal/endpoint/inventory/inventory_test.goindex fa0cffb2..895fab48 100644--- a/cli/beacon/internal/endpoint/inventory/inventory_test.go+++ b/cli/beacon/internal/endpoint/inventory/inventory_test.go@@ -456,6 +456,22 @@ func TestScanReadsClinePluginFromClineDir(t *testing.T) {}}+// With XDG_CONFIG_HOME set, the OpenCode plugin inventory reports is the one OpenCode loads.+func TestScanReadsOpenCodePluginFromXDGConfigHome(t *testing.T) {+ home := t.TempDir()+ xdg := t.TempDir()+ t.Setenv("XDG_CONFIG_HOME", xdg)+ writeFile(t, filepath.Join(xdg, "opencode", "plugins", "beacon.ts"), `// beacon-managed-opencode-plugin:v1`)++ result := Scan(Options{HomeDir: home, WorkingDir: t.TempDir(), Now: fixedNow})+ if got := findConfig(result.Configs, "opencode", filepath.Join(xdg, "opencode", "plugins", "beacon.ts")); got == nil || !got.BeaconManaged {+ t.Fatalf("opencode plugin under XDG_CONFIG_HOME not reported as managed: %#v", got)+ }+ if got := findConfig(result.Configs, "opencode", filepath.Join(home, ".config", "opencode", "plugins", "beacon.ts")); got != nil {+ t.Fatalf("inventory still scanned ~/.config/opencode with XDG_CONFIG_HOME set: %#v", got)+ }+}+// With GEMINI_CLI_HOME set, the Gemini CLI settings inventory reads are the ones Gemini reads.func TestScanReadsGeminiSettingsFromGeminiCLIHome(t *testing.T) {home := t.TempDir()@@ -509,6 +525,7 @@ func TestScanReadsClaudeUserFilesFromClaudeConfigDir(t *testing.T) {}func TestScanIncludesAllSupportedCurrentUserAndProjectConfigs(t *testing.T) {+ t.Setenv("XDG_CONFIG_HOME", "")home := t.TempDir()work := t.TempDir()t.Setenv("SHELL", "/bin/bash")@@ -657,6 +674,7 @@ func TestScanYAMLAndMetadataOnlyConfigs(t *testing.T) {work := t.TempDir()t.Setenv("SHELL", "/bin/zsh")t.Setenv("CLINE_DIR", "")+ t.Setenv("XDG_CONFIG_HOME", "")writeFile(t, filepath.Join(hermesDefaultRoot(t, home), "config.yaml"), `mcpServers:memory:diff --git a/cli/beacon/internal/mcpconnect/targets.go b/cli/beacon/internal/mcpconnect/targets.goindex 4c82d35e..eab9867b 100644--- a/cli/beacon/internal/mcpconnect/targets.go+++ b/cli/beacon/internal/mcpconnect/targets.go@@ -347,11 +347,10 @@ func vscodeMCPPath(string) (string, error) {// existing opencode.json, else an existing opencode.jsonc, else a new opencode.jsonc. (Checked// against opencode 1.18: with both files present it writes opencode.json.)func opencodeConfigPath(home string) (string, error) {- base := strings.TrimSpace(os.Getenv("XDG_CONFIG_HOME"))- if base == "" {- base = filepath.Join(home, ".config")+ dir, err := hooks.OpenCodeConfigDir(home)+ if err != nil {+ return "", err}- dir := filepath.Join(base, "opencode")for _, name := range []string{"opencode.json", "opencode.jsonc"} {if info, err := os.Stat(filepath.Join(dir, name)); err == nil && !info.IsDir() {return filepath.Join(dir, name), nil