Skip to content

Commit 9a0d6f9

Browse files
fix(opencode): use OPENCODE_CONFIG + workaround DB migration marker
Two related fixes to clown's opencode invocation: 1. Switch from XDG_CONFIG_HOME=<tmpdir> to OPENCODE_CONFIG=<tmpfile>. Hijacking XDG_CONFIG_HOME was a heavier intervention than needed — OPENCODE_CONFIG is the documented mechanism for pointing opencode at a specific config file without affecting the rest of the XDG environment. 2. Idempotently symlink ~/.local/share/opencode/opencode.db -> opencode-stable.db before launch as a workaround for upstream bug anomalyco/opencode#16885. opencode's JSON->SQLite migration gate checks for opencode.db (unsuffixed) but the stable channel uses opencode-stable.db, so the migration banner reruns on every launch. The symlink satisfies the gate without disturbing the real DB. Best-effort: any error path silently leaves the marker alone. 🤡 Generated with [Clown](https://github.com/amarbel-llc/clown)
1 parent c59a80a commit 9a0d6f9

2 files changed

Lines changed: 53 additions & 17 deletions

File tree

‎cmd/clown/opencode.go‎

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,12 @@ func promptOpencodeLocalConfig(path string) (opencodeLocalConfig, error) {
159159
return opencodeLocalConfig{URL: url, Token: token}, nil
160160
}
161161

162-
func writeOpencodeConfig(configDir, url, token, model string) error {
162+
// writeOpencodeConfigFile writes the synthesized provider config to the
163+
// given file path. Clown points opencode at it via OPENCODE_CONFIG rather
164+
// than hijacking XDG_CONFIG_HOME — XDG_CONFIG_HOME also shadows opencode's
165+
// data-dir derivation, which makes opencode believe each launch is a fresh
166+
// install and re-run its one-time database migration.
167+
func writeOpencodeConfigFile(path, url, token, model string) error {
163168
if model == "" {
164169
model = "gpt-4o"
165170
}
@@ -208,15 +213,14 @@ func writeOpencodeConfig(configDir, url, token, model string) error {
208213
Model: "custom/" + model,
209214
}
210215

211-
dir := filepath.Join(configDir, "opencode")
212-
if err := os.MkdirAll(dir, 0o700); err != nil {
216+
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
213217
return fmt.Errorf("mkdir: %w", err)
214218
}
215219
data, err := json.Marshal(cfg)
216220
if err != nil {
217221
return fmt.Errorf("marshal config: %w", err)
218222
}
219-
return os.WriteFile(filepath.Join(dir, "opencode.json"), data, 0o600)
223+
return os.WriteFile(path, data, 0o600)
220224
}
221225

222226
// readCircusPortfile reads the bare port number circus writes to
@@ -248,6 +252,32 @@ func readCircusPortfile() (string, error) {
248252
return "127.0.0.1:" + val, nil
249253
}
250254

255+
// ensureOpencodeMigrationMarker works around anomalyco/opencode#16885:
256+
// opencode's startup migration gate checks for ~/.local/share/opencode
257+
// /opencode.db, but the stable channel uses opencode-stable.db. Without a
258+
// matching marker the JSON->SQLite migration banner reruns on every launch.
259+
// We create an idempotent symlink to the channel-specific DB. Best-effort:
260+
// if anything fails (missing data dir, marker already exists as a regular
261+
// file the user owns, etc.) we leave it alone — the worst case is the
262+
// upstream bug stays visible, not a launch failure.
263+
func ensureOpencodeMigrationMarker() {
264+
home, err := os.UserHomeDir()
265+
if err != nil {
266+
return
267+
}
268+
dataDir := filepath.Join(home, ".local", "share", "opencode")
269+
target := filepath.Join(dataDir, "opencode-stable.db")
270+
marker := filepath.Join(dataDir, "opencode.db")
271+
272+
if _, err := os.Stat(target); err != nil {
273+
return
274+
}
275+
if _, err := os.Lstat(marker); err == nil {
276+
return
277+
}
278+
_ = os.Symlink(target, marker)
279+
}
280+
251281
func runOpencode(opencodePath string, args []string, prof *profile.Profile) int {
252282
if opencodePath == "" {
253283
fmt.Fprintln(os.Stderr, "clown: opencode binary path not configured (build misconfiguration)")
@@ -303,16 +333,19 @@ func runOpencode(opencodePath string, args []string, prof *profile.Profile) int
303333
}
304334
defer os.RemoveAll(tmpDir)
305335

306-
if err := writeOpencodeConfig(tmpDir, url, token, model); err != nil {
336+
cfgPath := filepath.Join(tmpDir, "opencode.json")
337+
if err := writeOpencodeConfigFile(cfgPath, url, token, model); err != nil {
307338
fmt.Fprintf(os.Stderr, "clown: write opencode config: %v\n", err)
308339
return 1
309340
}
310341

342+
ensureOpencodeMigrationMarker()
343+
311344
cmd := exec.Command(opencodePath, args...)
312345
cmd.Stdin = os.Stdin
313346
cmd.Stdout = os.Stdout
314347
cmd.Stderr = os.Stderr
315-
cmd.Env = append(os.Environ(), "XDG_CONFIG_HOME="+tmpDir)
348+
cmd.Env = append(os.Environ(), "OPENCODE_CONFIG="+cfgPath)
316349

317350
if err := cmd.Run(); err != nil {
318351
if exitErr, ok := err.(*exec.ExitError); ok {

‎cmd/clown/opencode_test.go‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,14 @@ func TestWriteOpencodeLocalConfigFile_QuotesAreEscaped(t *testing.T) {
126126
}
127127
}
128128

129-
func TestWriteOpencodeConfig_CreatesFile(t *testing.T) {
129+
func TestWriteOpencodeConfigFile_CreatesFile(t *testing.T) {
130130
dir := t.TempDir()
131-
err := writeOpencodeConfig(dir, "https://example.com/v1", "test-token", "")
131+
path := filepath.Join(dir, "opencode.json")
132+
err := writeOpencodeConfigFile(path, "https://example.com/v1", "test-token", "")
132133
if err != nil {
133134
t.Fatalf("unexpected error: %v", err)
134135
}
135-
data, err := os.ReadFile(filepath.Join(dir, "opencode", "opencode.json"))
136+
data, err := os.ReadFile(path)
136137
if err != nil {
137138
t.Fatalf("config file not created: %v", err)
138139
}
@@ -148,13 +149,14 @@ func TestWriteOpencodeConfig_CreatesFile(t *testing.T) {
148149
}
149150
}
150151

151-
func TestWriteOpencodeConfig_WithProfile(t *testing.T) {
152+
func TestWriteOpencodeConfigFile_WithProfile(t *testing.T) {
152153
dir := t.TempDir()
153-
err := writeOpencodeConfig(dir, "https://gw.example.com/v1", "tok-xyz", "gpt-4o")
154+
path := filepath.Join(dir, "opencode.json")
155+
err := writeOpencodeConfigFile(path, "https://gw.example.com/v1", "tok-xyz", "gpt-4o")
154156
if err != nil {
155-
t.Fatalf("writeOpencodeConfig: %v", err)
157+
t.Fatalf("writeOpencodeConfigFile: %v", err)
156158
}
157-
data, err := os.ReadFile(filepath.Join(dir, "opencode", "opencode.json"))
159+
data, err := os.ReadFile(path)
158160
if err != nil {
159161
t.Fatalf("read config: %v", err)
160162
}
@@ -167,13 +169,14 @@ func TestWriteOpencodeConfig_WithProfile(t *testing.T) {
167169
}
168170
}
169171

170-
func TestWriteOpencodeConfig_ModelOverride(t *testing.T) {
172+
func TestWriteOpencodeConfigFile_ModelOverride(t *testing.T) {
171173
dir := t.TempDir()
172-
err := writeOpencodeConfig(dir, "https://gw.example.com/v1", "tok-xyz", "my-custom-model")
174+
path := filepath.Join(dir, "opencode.json")
175+
err := writeOpencodeConfigFile(path, "https://gw.example.com/v1", "tok-xyz", "my-custom-model")
173176
if err != nil {
174-
t.Fatalf("writeOpencodeConfig: %v", err)
177+
t.Fatalf("writeOpencodeConfigFile: %v", err)
175178
}
176-
data, err := os.ReadFile(filepath.Join(dir, "opencode", "opencode.json"))
179+
data, err := os.ReadFile(path)
177180
if err != nil {
178181
t.Fatalf("read config: %v", err)
179182
}

0 commit comments

Comments
 (0)