Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(opencode): deny interactive questions in daemon mode (#2878)

YOMXXX34f16e2c7a56
diff --git a/server/pkg/agent/opencode.go b/server/pkg/agent/opencode.go
index eef8358612..8cff18e3b2 100644
--- a/server/pkg/agent/opencode.go
+++ b/server/pkg/agent/opencode.go
@@ -17,8 +17,9 @@ import (
// opencodeBlockedArgs are flags hardcoded by the daemon that must not be
// overridden by user-configured custom_args.
var opencodeBlockedArgs = map[string]blockedArgMode{
- "--format": blockedWithValue, // json output format for daemon communication
- "--dir": blockedWithValue, // task workdir anchor for skill / AGENTS.md discovery
+ "--format": blockedWithValue, // json output format for daemon communication
+ "--dir": blockedWithValue, // task workdir anchor for skill / AGENTS.md discovery
+ "--dangerously-skip-permissions": blockedStandalone, // daemon manages non-interactive permission prompts
}
// opencodeBackend implements Backend by spawning `opencode run --format json`
@@ -50,7 +51,7 @@ func (b *opencodeBackend) Execute(ctx context.Context, prompt string, opts ExecO
}
runCtx, cancel := context.WithTimeout(ctx, timeout)
- args := []string{"run", "--format", "json"}
+ args := []string{"run", "--format", "json", "--dangerously-skip-permissions"}
// Anchor OpenCode's project discovery (AGENTS.md walk-up + .opencode/skills/
// project config scan) at the task workdir. Without this, OpenCode falls
// back to PWD (inherited from the daemon process) or process.cwd(), which
@@ -87,8 +88,13 @@ func (b *opencodeBackend) Execute(ctx context.Context, prompt string, opts ExecO
}
env := buildEnv(b.cfg.Env)
- // Auto-approve all tool use in daemon mode.
- env = append(env, `OPENCODE_PERMISSION={"*":"allow"}`)
+ // Keep daemon-mode runs non-interactive without relying on
+ // OPENCODE_PERMISSION. OpenCode deep-merges that env override into user
+ // config while preserving existing key order, so a pre-existing
+ // permission.question key can be followed by a wildcard allow and bypass
+ // the intended question deny. Current OpenCode run sessions inject their
+ // own question/plan deny rules after agent config; this flag only answers
+ // prompts that survive those explicit denies.
// Override PWD so the child OpenCode process resolves its discovery root
// to the task workdir. cmd.Dir alone is not enough: OpenCode reads PWD
// (inherited from the parent daemon) before falling back to process.cwd()
diff --git a/server/pkg/agent/opencode_test.go b/server/pkg/agent/opencode_test.go
index 960ea56550..a7cca3ba38 100644
--- a/server/pkg/agent/opencode_test.go
+++ b/server/pkg/agent/opencode_test.go
@@ -835,9 +835,9 @@ func TestOpencodeWindowsPackageCandidatesAmd64(t *testing.T) {
// fakeOpencodeScript returns a POSIX-sh script that impersonates `opencode`
// for argv / env capture. It writes the argv (one per line) to
-// $OPENCODE_ARGS_FILE and the resolved PWD to $OPENCODE_PWD_FILE, emits a
-// minimal completed step on stdout so the daemon's event loop terminates,
-// then exits.
+// $OPENCODE_ARGS_FILE, the resolved PWD to $OPENCODE_PWD_FILE, and the
+// permission config to $OPENCODE_PERMISSION_FILE. It emits a minimal completed
+// step on stdout so the daemon's event loop terminates, then exits.
func fakeOpencodeScript() string {
return `#!/bin/sh
if [ -n "$OPENCODE_ARGS_FILE" ]; then
@@ -848,12 +848,30 @@ fi
if [ -n "$OPENCODE_PWD_FILE" ]; then
printf '%s\n' "$PWD" > "$OPENCODE_PWD_FILE"
fi
+if [ -n "$OPENCODE_PERMISSION_FILE" ]; then
+ printf '%s\n' "$OPENCODE_PERMISSION" > "$OPENCODE_PERMISSION_FILE"
+fi
+if [ -f "$PWD/opencode.json" ] && grep -Eq '"question"[[:space:]]*:[[:space:]]*"allow"' "$PWD/opencode.json"; then
+ if [ "$OPENCODE_PERMISSION" = '{"*":"allow","question":"deny"}' ]; then
+ printf '{"type":"error","timestamp":1,"sessionID":"ses_fake","error":{"name":"PermissionBypass","data":{"message":"question permission bypassed by env wildcard order"}}}\n'
+ exit 0
+ fi
+fi
printf '{"type":"step_start","timestamp":1,"sessionID":"ses_fake","part":{"type":"step-start"}}\n'
printf '{"type":"text","timestamp":2,"sessionID":"ses_fake","part":{"type":"text","text":"ok"}}\n'
printf '{"type":"step_finish","timestamp":3,"sessionID":"ses_fake","part":{"type":"step-finish"}}\n'
`
}
+func containsString(items []string, want string) bool {
+ for _, item := range items {
+ if item == want {
+ return true
+ }
+ }
+ return false
+}
+
// TestOpencodeBackendAnchorsDirAndPWD pins the discovery-root fix from
// MUL-2416: OpenCode resolves its AGENTS.md walk-up and .opencode/skills
// project config scan from `--dir` and PWD. cmd.Dir alone is not enough
@@ -936,6 +954,106 @@ func TestOpencodeBackendAnchorsDirAndPWD(t *testing.T) {
}
}
+func TestOpencodeBackendDoesNotUsePermissionEnvOverride(t *testing.T) {
+ t.Parallel()
+
+ tempDir := t.TempDir()
+ permissionFile := filepath.Join(tempDir, "permission.json")
+ fakePath := filepath.Join(tempDir, "opencode")
+ writeTestExecutable(t, fakePath, []byte(fakeOpencodeScript()))
+
+ backend, err := New("opencode", Config{
+ ExecutablePath: fakePath,
+ Logger: slog.Default(),
+ Env: map[string]string{
+ "OPENCODE_PERMISSION_FILE": permissionFile,
+ },
+ })
+ if err != nil {
+ t.Fatalf("new opencode backend: %v", err)
+ }
+
+ ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+ defer cancel()
+
+ session, err := backend.Execute(ctx, "prompt-ignored", ExecOptions{
+ Timeout: 5 * time.Second,
+ })
+ if err != nil {
+ t.Fatalf("execute: %v", err)
+ }
+ go func() {
+ for range session.Messages {
+ }
+ }()
+ <-session.Result
+
+ raw, err := os.ReadFile(permissionFile)
+ if err != nil {
+ t.Fatalf("read permission file: %v", err)
+ }
+ if got := strings.TrimSpace(string(raw)); got != "" {
+ t.Fatalf("OPENCODE_PERMISSION = %q, want empty env override", got)
+ }
+}
+
+func TestOpencodeBackendQuestionDenySurvivesUserConfig(t *testing.T) {
+ t.Parallel()
+
+ tempDir := t.TempDir()
+ argsFile := filepath.Join(tempDir, "argv.txt")
+ fakePath := filepath.Join(tempDir, "opencode")
+ writeTestExecutable(t, fakePath, []byte(fakeOpencodeScript()))
+
+ workDir := t.TempDir()
+ if err := os.WriteFile(
+ filepath.Join(workDir, "opencode.json"),
+ []byte(`{"permission":{"question":"allow"}}`),
+ 0o644,
+ ); err != nil {
+ t.Fatalf("write opencode config: %v", err)
+ }
+
+ backend, err := New("opencode", Config{
+ ExecutablePath: fakePath,
+ Logger: slog.Default(),
+ Env: map[string]string{
+ "OPENCODE_ARGS_FILE": argsFile,
+ },
+ })
+ if err != nil {
+ t.Fatalf("new opencode backend: %v", err)
+ }
+
+ ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+ defer cancel()
+
+ session, err := backend.Execute(ctx, "prompt-ignored", ExecOptions{
+ Cwd: workDir,
+ Timeout: 5 * time.Second,
+ })
+ if err != nil {
+ t.Fatalf("execute: %v", err)
+ }
+ go func() {
+ for range session.Messages {
+ }
+ }()
+ result := <-session.Result
+ if result.Status != "completed" {
+ t.Fatalf("result status = %q, error = %q; want completed", result.Status, result.Error)
+ }
+
+ raw, err := os.ReadFile(argsFile)
+ if err != nil {
+ t.Fatalf("read args file: %v", err)
+ }
+ args := strings.Split(strings.TrimSpace(string(raw)), "\n")
+ if !containsString(args, "--dangerously-skip-permissions") {
+ t.Fatalf("expected daemon-mode argv to include --dangerously-skip-permissions, got %q", args)
+ }
+}
+
// TestOpencodeBackendBlocksDirOverride ensures user-supplied custom args
// cannot replace the daemon-managed `--dir` anchor. Letting custom args
// override it would re-introduce the MUL-2416 regression.