Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(agent/opencode): bypass npm .cmd shim on Windows to preserve multi-line prompts (#1718)
Truffle8b340fcf212b
diff --git a/server/pkg/agent/exec_fixture_windows_test.go b/server/pkg/agent/exec_fixture_windows_test.gonew file mode 100644index 0000000000..37e3d6d2a5--- /dev/null+++ b/server/pkg/agent/exec_fixture_windows_test.go@@ -0,0 +1,24 @@+//go:build windows++package agent++import (+ "os"+ "testing"+)++// writeTestExecutable is the Windows counterpart to the //go:build unix+// implementation in exec_fixture_unix_test.go. ETXTBSY is a Linux/Unix+// fork-exec race; Windows doesn't have that pathology, so a plain+// os.WriteFile is sufficient.+//+// The helper is referenced by claude_test.go / codex_test.go /+// kimi_test.go, so the absence of a Windows impl made+// `go test ./pkg/agent` fail to build on Windows. Lifted from #1719+// (Codex) with attribution.+func writeTestExecutable(tb testing.TB, path string, content []byte) {+ tb.Helper()+ if err := os.WriteFile(path, content, 0o755); err != nil {+ tb.Fatalf("write test executable %s: %v", path, err)+ }+}diff --git a/server/pkg/agent/opencode.go b/server/pkg/agent/opencode.goindex e8887d6f57..e6b27502b4 100644--- a/server/pkg/agent/opencode.go+++ b/server/pkg/agent/opencode.go@@ -6,7 +6,10 @@ import ("encoding/json""fmt""io"+ "os""os/exec"+ "path/filepath"+ "runtime""strings""time")@@ -28,9 +31,17 @@ func (b *opencodeBackend) Execute(ctx context.Context, prompt string, opts ExecOif execPath == "" {execPath = "opencode"}- if _, err := exec.LookPath(execPath); err != nil {+ resolved, err := exec.LookPath(execPath)+ if err != nil {return nil, fmt.Errorf("opencode executable not found at %q: %w", execPath, err)}+ if runtime.GOOS == "windows" {+ if native := resolveOpenCodeNativeFromShim(resolved, os.Stat); native != "" {+ b.cfg.Logger.Info("opencode resolved to native binary to avoid .cmd shim argv truncation", "shim", resolved, "native", native)+ resolved = native+ }+ }+ execPath = resolvedtimeout := opts.Timeoutif timeout == 0 {@@ -275,6 +286,59 @@ func (b *opencodeBackend) handleErrorEvent(event opencodeEvent, ch chan<- Messag*finalError = errMsg}+// resolveOpenCodeNativeFromShim returns the path to the native OpenCode+// executable bundled inside the npm package, given the path to the npm+// `opencode.cmd` shim that PATH lookup found on Windows. Returns "" if shim+// doesn't end in `.cmd` or no candidate npm platform package has a bundled+// native binary present.+//+// Windows batch argument forwarding via `%*` does not preserve newlines, so+// multi-line positional argv is truncated at the first newline before the+// shim hands off to the JS entrypoint. Daemon prompts can include literal+// newlines (system prompt + user message), which makes the agent see only+// the first line. Native binary spawn skips the cmd.exe layer entirely.+//+// Layout when installed via `npm install -g opencode-ai`:+//+// <prefix>\opencode.cmd (shim)+// <prefix>\node_modules\opencode-ai\node_modules\opencode-windows-{x64,x64-baseline,arm64}\bin\opencode.exe (native)+//+// `opencode-windows-x64-baseline` ships for older CPUs without AVX2;+// `opencode-windows-arm64` ships for Surface / Copilot+ PC hosts.+// Candidates are tried in GOARCH-preferred order so the most likely match+// for the current host comes first.+//+// statFn is injected so this is testable on non-Windows hosts.+func resolveOpenCodeNativeFromShim(shimPath string, statFn func(string) (os.FileInfo, error)) string {+ if !strings.EqualFold(filepath.Ext(shimPath), ".cmd") {+ return ""+ }+ prefix := filepath.Dir(shimPath)+ for _, pkg := range opencodeWindowsPackageCandidates(runtime.GOARCH) {+ candidate := filepath.Join(prefix, "node_modules", "opencode-ai", "node_modules", pkg, "bin", "opencode.exe")+ if _, err := statFn(candidate); err == nil {+ return candidate+ }+ }+ return ""+}++// opencodeWindowsPackageCandidates returns the npm platform package names+// that may host the bundled `opencode.exe` on Windows, ordered so the most+// likely match for the given GOARCH comes first. ARM64 hosts try the arm64+// build first; everything else tries x64, then the baseline x64 build for+// older CPUs without AVX2, then arm64 as a final fallback. Cost is one+// extra statFn call per miss when the GOARCH-preferred package isn't+// installed.+func opencodeWindowsPackageCandidates(goarch string) []string {+ switch goarch {+ case "arm64":+ return []string{"opencode-windows-arm64", "opencode-windows-x64", "opencode-windows-x64-baseline"}+ default:+ return []string{"opencode-windows-x64", "opencode-windows-x64-baseline", "opencode-windows-arm64"}+ }+}+// extractToolOutput converts the tool state output (which may be a string or// structured object) into a string.func extractToolOutput(output any) string {@@ -328,8 +392,8 @@ type opencodeEventPart struct {// opencodeTokens represents token usage in a step_finish event.type opencodeTokens struct {- Input int64 `json:"input"`- Output int64 `json:"output"`+ Input int64 `json:"input"`+ Output int64 `json:"output"`Cache *opencodeCacheTokens `json:"cache,omitempty"`}diff --git a/server/pkg/agent/opencode_test.go b/server/pkg/agent/opencode_test.goindex 73c6d491cf..8ecb101066 100644--- a/server/pkg/agent/opencode_test.go+++ b/server/pkg/agent/opencode_test.go@@ -2,8 +2,11 @@ package agentimport ("encoding/json"+ "errors""fmt""log/slog"+ "os"+ "path/filepath""strings""testing")@@ -709,3 +712,133 @@ func TestOpencodeProcessEventsErrorDoesNotRevertToCompleted(t *testing.T) {close(ch)}++// ── Windows native-binary resolution tests ──++// fakeStat returns a statFn that reports any path in `present` as existing+// and every other path as not-found. The returned os.FileInfo is a stub+// because resolveOpenCodeNativeFromShim only inspects the error.+func fakeStat(present ...string) func(string) (os.FileInfo, error) {+ set := make(map[string]struct{}, len(present))+ for _, p := range present {+ set[p] = struct{}{}+ }+ return func(path string) (os.FileInfo, error) {+ if _, ok := set[path]; ok {+ return nil, nil+ }+ return nil, errors.New("not found")+ }+}++func TestResolveOpenCodeNativeFromShimResolvesNpmShim(t *testing.T) {+ t.Parallel()++ // Reporter's exact layout from multica#1717.+ shim := filepath.Join("C:\\nvm4w", "nodejs", "opencode.cmd")+ native := filepath.Join("C:\\nvm4w", "nodejs", "node_modules", "opencode-ai", "node_modules", "opencode-windows-x64", "bin", "opencode.exe")++ got := resolveOpenCodeNativeFromShim(shim, fakeStat(native))+ if got != native {+ t.Errorf("got %q, want %q", got, native)+ }+}++func TestResolveOpenCodeNativeFromShimReturnsEmptyWhenNativeMissing(t *testing.T) {+ t.Parallel()++ // Shim ends in .cmd but the bundled native binary isn't present (e.g.+ // platform package didn't install or layout changed). Caller must keep+ // the original shim path so PATH lookup still wins.+ shim := filepath.Join("C:\\nvm4w", "nodejs", "opencode.cmd")++ got := resolveOpenCodeNativeFromShim(shim, fakeStat())+ if got != "" {+ t.Errorf("got %q, want empty (missing native binary)", got)+ }+}++func TestResolveOpenCodeNativeFromShimSkipsNonCmdPath(t *testing.T) {+ t.Parallel()++ // On macOS/Linux the path returned by exec.LookPath is the native+ // binary itself, with no .cmd extension. Helper should signal "no+ // rewrite needed" by returning empty.+ cases := []string{+ "/usr/local/bin/opencode",+ "C:\\nvm4w\\nodejs\\opencode.exe",+ "",+ }+ for _, p := range cases {+ if got := resolveOpenCodeNativeFromShim(p, fakeStat("anything")); got != "" {+ t.Errorf("path %q: got %q, want empty", p, got)+ }+ }+}++func TestResolveOpenCodeNativeFromShimAcceptsUppercaseExtension(t *testing.T) {+ t.Parallel()++ // Windows is case-insensitive on filesystem extensions. PATHEXT tokens+ // are commonly uppercase, and exec.LookPath can return either case.+ shim := filepath.Join("C:\\nvm4w", "nodejs", "opencode.CMD")+ native := filepath.Join("C:\\nvm4w", "nodejs", "node_modules", "opencode-ai", "node_modules", "opencode-windows-x64", "bin", "opencode.exe")++ got := resolveOpenCodeNativeFromShim(shim, fakeStat(native))+ if got != native {+ t.Errorf("got %q, want %q", got, native)+ }+}++func TestResolveOpenCodeNativeFromShimFallsBackToBaseline(t *testing.T) {+ t.Parallel()++ // Older CPUs without AVX2 get `opencode-windows-x64-baseline` instead of+ // the default x64 build. Resolver should fall through and find it when+ // the primary x64 package isn't installed.+ shim := filepath.Join("C:\\nvm4w", "nodejs", "opencode.cmd")+ baseline := filepath.Join("C:\\nvm4w", "nodejs", "node_modules", "opencode-ai", "node_modules", "opencode-windows-x64-baseline", "bin", "opencode.exe")++ got := resolveOpenCodeNativeFromShim(shim, fakeStat(baseline))+ if got != baseline {+ t.Errorf("got %q, want %q", got, baseline)+ }+}++func TestOpencodeWindowsPackageCandidatesArm64(t *testing.T) {+ t.Parallel()++ // ARM64 hosts (Surface, Copilot+ PC) should try arm64 first so the+ // resolver doesn't accidentally pick up a leftover x64 install when+ // the matching arm64 package is present.+ got := opencodeWindowsPackageCandidates("arm64")+ want := []string{"opencode-windows-arm64", "opencode-windows-x64", "opencode-windows-x64-baseline"}+ if !equalStringSlice(got, want) {+ t.Errorf("got %v, want %v", got, want)+ }+}++func TestOpencodeWindowsPackageCandidatesAmd64(t *testing.T) {+ t.Parallel()++ // amd64 (and any non-arm64) hosts try x64 → baseline → arm64. The arm64+ // fallback at the end covers the unusual case where only the arm64+ // package is installed; resolution still succeeds.+ got := opencodeWindowsPackageCandidates("amd64")+ want := []string{"opencode-windows-x64", "opencode-windows-x64-baseline", "opencode-windows-arm64"}+ if !equalStringSlice(got, want) {+ t.Errorf("got %v, want %v", got, want)+ }+}++func equalStringSlice(a, b []string) bool {+ if len(a) != len(b) {+ return false+ }+ for i := range a {+ if a[i] != b[i] {+ return false+ }+ }+ return true+}