Skip to content

Commit 92ec93e

Browse files
MichielDeanLobsterdog
andauthored
fix: opencode run works — 3 critical fixes (#433)
## Summary Fixes three critical bugs that prevented Cistern from spawning opencode agents. 1. **PromptPositional**: opencode takes the prompt as a positional argument after `run`, not via a flag. The old code used `PromptFlag: ""` with `NonInteractive.PromptFlag: "-p"`, but `-p` is `--password` in opencode, not a prompt flag. Added `PromptPositional: true` to the opencode preset. When set, the short prompt is appended as a positional argument after all flags, and the full prompt is written to `AGENTS.md` via `PromptFileTemplate`. 2. **`--dangerously-skip-permissions`**: Added to the opencode preset `Args`. Cistern agents must run without permission prompts to function autonomously. 3. **Unset `OPENCODE_SERVER_*` env vars**: CodeNomad sets `OPENCODE_SERVER_USERNAME` and `OPENCODE_SERVER_PASSWORD` in the environment. When these are set, `opencode run` tries to connect to an authenticated server instead of starting a fresh session, causing "Session not found" errors. The fix explicitly unsets `OPENCODE_SERVER_USERNAME`, `OPENCODE_SERVER_PASSWORD`, `OPENCODE_PID`, and `OPENCODE` in every spawned tmux session. See: anomalyco/opencode#8502 Co-authored-by: Lobsterdog <[email protected]>
1 parent 9f1571e commit 92ec93e

3 files changed

Lines changed: 49 additions & 16 deletions

File tree

‎internal/cataractae/session.go‎

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,17 @@ func (s *Session) collectEnvArgs() []string {
198198
args = append(args, "-e", "GH_TOKEN="+tok)
199199
}
200200

201+
// Always-unset: env vars that interfere with non-interactive agent execution.
202+
// OPENCODE_SERVER_* causes "session not found" errors when opencode run
203+
// tries to connect to an authenticated server instead of starting fresh.
204+
// See: https://github.com/anomalyco/opencode/issues/8502
205+
args = append(args,
206+
"-e", "OPENCODE_SERVER_USERNAME=",
207+
"-e", "OPENCODE_SERVER_PASSWORD=",
208+
"-e", "OPENCODE_PID=",
209+
"-e", "OPENCODE=",
210+
)
211+
201212
return args
202213
}
203214

@@ -317,20 +328,17 @@ func (s *Session) buildPresetCmd(preset provider.ProviderPreset, skillsDir strin
317328

318329
prompt := s.buildPrompt()
319330

320-
// Determine the effective prompt flag: use the preset's PromptFlag if set,
321-
// otherwise fall back to the NonInteractive PromptFlag (e.g. opencode uses
322-
// "" for PromptFlag but "-p" in NonInteractive).
331+
// Determine how the prompt is delivered to the agent:
332+
// 1. PromptFlag set → use flag + value (e.g. claude uses -p)
333+
// 2. PromptPositional + PromptFileTemplate → write AGENTS.md, append short prompt as positional arg
334+
// 3. PromptFileTemplate only → write AGENTS.md, no prompt arg (agent reads it natively)
335+
// 4. None → build the prompt flag from NonInteractive config as fallback
323336
promptFlag := preset.PromptFlag
324-
if promptFlag == "" && preset.NonInteractive.PromptFlag != "" {
337+
if promptFlag == "" && preset.NonInteractive.PromptFlag != "" && !preset.PromptPositional {
325338
promptFlag = preset.NonInteractive.PromptFlag
326339
}
327340

328341
if promptFlag != "" {
329-
// For providers that read their instructions file natively
330-
// (e.g. opencode reads AGENTS.md), a PromptFileTemplate can
331-
// replace the full prompt with a short reference. When set, the
332-
// prompt content is written to the file named by PromptFileTemplate
333-
// and a short prompt referencing CONTEXT.md is used instead.
334342
if preset.PromptFileTemplate != "" {
335343
identityPath := s.resolveIdentityPath()
336344
promptFile := strings.ReplaceAll(preset.PromptFileTemplate, "{identity}", identityPath)
@@ -357,10 +365,29 @@ func (s *Session) buildPresetCmd(preset provider.ProviderPreset, skillsDir strin
357365
} else {
358366
parts = append(parts, promptFlag, "'"+strings.ReplaceAll(prompt, "'", `'\''`)+"'")
359367
}
368+
} else if preset.PromptPositional && preset.PromptFileTemplate != "" {
369+
// Providers like opencode take the prompt as a positional argument
370+
// rather than a flag. Write the full prompt to the instructions file
371+
// and append a short positional prompt after all flags.
372+
identityPath := s.resolveIdentityPath()
373+
promptFile := strings.ReplaceAll(preset.PromptFileTemplate, "{identity}", identityPath)
374+
promptFilePath := filepath.Join(s.WorkDir, promptFile)
375+
if err := os.WriteFile(promptFilePath, []byte(prompt), 0o644); err != nil {
376+
slog.Default().Error("spawn: failed to write prompt file",
377+
"session", s.ID,
378+
"path", promptFilePath,
379+
"error", err)
380+
} else {
381+
slog.Default().Info("spawn: wrote prompt file",
382+
"session", s.ID,
383+
"path", promptFilePath,
384+
"bytes", len(prompt))
385+
}
386+
shortPrompt := "Read CONTEXT.md for your task and begin work. Follow the instructions in " + promptFile + "."
387+
parts = append(parts, shellQuote(shortPrompt))
360388
} else if preset.PromptFileTemplate != "" {
361-
// No prompt flag available, but the provider reads an instructions
362-
// file natively (e.g. AGENTS.md). Write the full prompt to it so
363-
// the agent picks it up automatically.
389+
// No prompt flag and no positional mode, but the provider reads an
390+
// instructions file natively. Write the full prompt to it.
364391
identityPath := s.resolveIdentityPath()
365392
promptFile := strings.ReplaceAll(preset.PromptFileTemplate, "{identity}", identityPath)
366393
promptFilePath := filepath.Join(s.WorkDir, promptFile)

‎internal/provider/preset.go‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ type ProviderPreset struct {
6060
// When empty, no prompt flag is appended and the prompt must be delivered via
6161
// an alternative mechanism (stdin, instructions file, etc.).
6262
PromptFlag string `json:"prompt_flag,omitempty"`
63+
// PromptPositional indicates that the prompt should be appended as a
64+
// positional argument rather than via a flag. When true and PromptFileTemplate
65+
// is set, the short prompt is appended as a positional argument after the
66+
// subcommand and flags. This is for CLIs like opencode where the prompt
67+
// is a positional argument, not a flag value.
68+
PromptPositional bool `json:"prompt_positional,omitempty"`
6369
// PermissionsFlag is the CLI flag used to grant additional permissions.
6470
PermissionsFlag string `json:"permissions_flag,omitempty"`
6571
// InstructionsFile is the filename the agent reads for task instructions (e.g. "CLAUDE.md").
@@ -165,13 +171,13 @@ var builtins = []ProviderPreset{
165171
Name: "opencode",
166172
Command: "opencode",
167173
Subcommand: "run",
174+
Args: []string{"--dangerously-skip-permissions"},
168175
ModelFlag: "--model",
169176
DefaultModel: "ollama/glm-5.1:cloud",
170-
PromptFlag: "",
171-
ContinueFlag: "--continue",
177+
PromptPositional: true,
172178
InstructionsFile: "AGENTS.md",
173179
PromptFileTemplate: "AGENTS.md",
174-
NonInteractive: NonInteractiveConfig{Subcommand: "run", PromptFlag: "-p"},
180+
NonInteractive: NonInteractiveConfig{Subcommand: "run"},
175181
},
176182
}
177183

‎internal/provider/preset_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func TestBuiltins_NonInteractiveConfig(t *testing.T) {
9494
{"codex", "exec", "", "-p", ""},
9595
{"gemini", "", "", "-p", ""},
9696
{"copilot", "", "", "-p", ""},
97-
{"opencode", "run", "", "-p", ""},
97+
{"opencode", "run", "", "", ""},
9898
}
9999
for _, tt := range tests {
100100
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)