Skip to content

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

Go

fix(agent): route all Windows Pi runs through powershell -Command to preserve stdin (#7392)

Sanjay Ramaduguf48f24c40584
diff --git a/server/pkg/agent/pi_invocation.go b/server/pkg/agent/pi_invocation.go
index b3ff04c4a7..01f6c9c6aa 100644
--- a/server/pkg/agent/pi_invocation.go
+++ b/server/pkg/agent/pi_invocation.go
@@ -1,6 +1,18 @@
package agent
-import "log/slog"
+import (
+ "log/slog"
+ "strings"
+)
+
+// piPowerShellCommandArgs builds the argv for the -Command route, which is the
+// only PowerShell route that leaves stdin bytes untouched (#7355).
+func piPowerShellCommandArgs(ps1 string, args []string) []string {
+ quotedPS1 := strings.ReplaceAll(ps1, "'", "''")
+ full := make([]string, 0, 6+len(args))
+ full = append(full, "-NoProfile", "-ExecutionPolicy", "Bypass", "-Command", "& '"+quotedPS1+"' @args")
+ return append(full, args...)
+}
// choosePiInvocation selects the actual program (argv[0]) and the full
// argv to spawn a Pi run.
@@ -16,11 +28,13 @@ import "log/slog"
// containing newlines or other whitespace — for Pi, that's the
// multi-line positional prompt passed by buildPiArgs. Symptom: the
// Pi session JSONL records only the first line of the prompt
-// (#3306). To stay on the official launch path while avoiding that
-// re-tokenisation, we resolve pi.ps1 next to the .cmd and invoke
-// PowerShell with `-File <ps1>` directly. The task prompt now travels on
-// stdin (#6457), but the rewrite remains necessary for multi-line custom
-// option values and installations that still use the npm batch launcher.
+// (#3306). We resolve pi.ps1 next to the .cmd and invoke PowerShell
+// with `-Command & 'pi.ps1' @args`. The prompt moved to stdin in
+// #6457, and -File re-encodes stdin under the console ANSI codepage,
+// destroying every non-ASCII byte (#7355) — so every Pi invocation
+// takes the -Command route, which forwards stdin untouched. The
+// residual #3306 exposure is a multi-line custom option value, which
+// @args still forwards as one token.
//
// The Windows-specific behaviour is implemented in
// pi_invocation_windows.go; on other platforms we fall through to a
diff --git a/server/pkg/agent/pi_invocation_test.go b/server/pkg/agent/pi_invocation_test.go
index 3e123a0cf6..fa1ca44d7d 100644
--- a/server/pkg/agent/pi_invocation_test.go
+++ b/server/pkg/agent/pi_invocation_test.go
@@ -34,3 +34,47 @@ func TestChoosePiInvocation_PassthroughForNonLauncher(t *testing.T) {
t.Errorf("argv changed unexpectedly:\n got %#v\n want %#v", gotArgs, args)
}
}
+
+// TestPiPowerShellCommandArgs pins the argv shape of the -Command route,
+// which is the only PowerShell route that leaves stdin bytes untouched
+// (#7355). Runs on every platform: it tests the pure helper, not the
+// Windows-tagged rewrite.
+func TestPiPowerShellCommandArgs(t *testing.T) {
+ ps1 := `C:\Users\X\npm\pi.ps1`
+ args := []string{"-p", "--mode", "json"}
+
+ got := piPowerShellCommandArgs(ps1, args)
+ want := []string{
+ "-NoProfile",
+ "-ExecutionPolicy", "Bypass",
+ "-Command", "& '" + ps1 + "' @args",
+ "-p", "--mode", "json",
+ }
+ if !reflect.DeepEqual(got, want) {
+ t.Errorf("argv mismatch:\n got %#v\n want %#v", got, want)
+ }
+}
+
+func TestPiPowerShellCommandArgs_EscapesSingleQuotesInPath(t *testing.T) {
+ ps1 := `C:\Users\O'Brien\npm\pi.ps1`
+ got := piPowerShellCommandArgs(ps1, nil)
+ want := []string{
+ "-NoProfile",
+ "-ExecutionPolicy", "Bypass",
+ "-Command", "& 'C:\\Users\\O''Brien\\npm\\pi.ps1' @args",
+ }
+ if !reflect.DeepEqual(got, want) {
+ t.Errorf("argv mismatch:\n got %#v\n want %#v", got, want)
+ }
+}
+
+func TestPiPowerShellCommandArgs_ForwardsMultiLineArgVerbatim(t *testing.T) {
+ multiLine := "line one\n\nline two\n"
+ got := piPowerShellCommandArgs(`C:\pi.ps1`, []string{"-p", multiLine})
+ if len(got) != 7 {
+ t.Fatalf("len(got) = %d, want 7: %#v", len(got), got)
+ }
+ if got[len(got)-1] != multiLine {
+ t.Errorf("multi-line arg was not forwarded verbatim:\n got %q\n want %q", got[len(got)-1], multiLine)
+ }
+}
diff --git a/server/pkg/agent/pi_invocation_windows.go b/server/pkg/agent/pi_invocation_windows.go
index bccad85b8d..f23e580605 100644
--- a/server/pkg/agent/pi_invocation_windows.go
+++ b/server/pkg/agent/pi_invocation_windows.go
@@ -9,27 +9,13 @@ import (
"strings"
)
-// platformPiInvocation rewrites pi.cmd → PowerShell -File pi.ps1 on
-// Windows to avoid cmd.exe %* re-tokenisation (see #3306).
+// platformPiInvocation rewrites pi.cmd → PowerShell -Command pi.ps1 on
+// Windows. -Command with @args is the only route that preserves stdin bytes:
+// -File re-encodes stdin under the console ANSI codepage, destroying
+// non-ASCII input (#7355). It also avoids cmd.exe %* re-tokenisation (#3306).
// powerShellLookup and rewriteCmdToPS1 are defined in cursor_invocation_windows.go.
func platformPiInvocation(lookedUp string, args []string, logger *slog.Logger) (string, []string, bool) {
- if isPiRPCInvocation(args) {
- return rewriteCmdToPS1Command("pi", lookedUp, args, logger)
- }
- return rewriteCmdToPS1("pi", lookedUp, args, logger)
-}
-
-func isPiRPCInvocation(args []string) bool {
- for i := 0; i < len(args); i++ {
- arg := args[i]
- if arg == "--mode" && i+1 < len(args) && args[i+1] == "rpc" {
- return true
- }
- if arg == "--mode=rpc" {
- return true
- }
- }
- return false
+ return rewriteCmdToPS1Command("pi", lookedUp, args, logger)
}
func rewriteCmdToPS1Command(toolName, lookedUp string, args []string, logger *slog.Logger) (string, []string, bool) {
@@ -47,13 +33,10 @@ func rewriteCmdToPS1Command(toolName, lookedUp string, args []string, logger *sl
return "", nil, false
}
- quotedPS1 := strings.ReplaceAll(ps1, "'", "''")
- full := make([]string, 0, 6+len(args))
- full = append(full, "-NoProfile", "-ExecutionPolicy", "Bypass", "-Command", "& '"+quotedPS1+"' @args")
- full = append(full, args...)
+ full := piPowerShellCommandArgs(ps1, args)
if logger != nil {
- logger.Info(toolName+": routing RPC through powershell -Command to preserve stdin",
+ logger.Info(toolName+": routing through powershell -Command to preserve stdin",
"powershell", psExe,
"ps1", ps1,
"original", lookedUp,
diff --git a/server/pkg/agent/pi_invocation_windows_test.go b/server/pkg/agent/pi_invocation_windows_test.go
index e2396892f1..c89377cbb4 100644
--- a/server/pkg/agent/pi_invocation_windows_test.go
+++ b/server/pkg/agent/pi_invocation_windows_test.go
@@ -10,14 +10,15 @@ import (
"testing"
)
-// TestPlatformPiInvocation_RewritesCmdLauncherToPowerShellFile is the core
+// TestPlatformPiInvocation_RewritesCmdLauncherToPowerShellCommand is the core
// Windows test: when LookPath resolves pi to the npm-installed .cmd
-// launcher and a sibling pi.ps1 exists, we should invoke PowerShell with
-// -File <ps1> and forward every original arg unchanged — including a synthetic
-// multi-line value that cmd.exe %* would otherwise mangle. The task prompt now
-// travels on stdin (#6457), but this preserves the #3306 launcher guarantee for
-// custom option values and keeps the historical failure mode covered.
-func TestPlatformPiInvocation_RewritesCmdLauncherToPowerShellFile(t *testing.T) {
+// launcher and a sibling pi.ps1 exists, we invoke PowerShell with
+// -Command & 'pi.ps1' @args and forward every original arg unchanged —
+// including a multi-line value. The task prompt travels on stdin (#6457) and
+// -File re-encodes stdin under the console codepage (#7355), so every Pi
+// invocation takes the -Command route; @args also preserves the #3306
+// multi-line custom option value guarantee.
+func TestPlatformPiInvocation_RewritesCmdLauncherToPowerShellCommand(t *testing.T) {
dir := t.TempDir()
cmdPath := filepath.Join(dir, "pi.cmd")
ps1Path := filepath.Join(dir, "pi.ps1")
@@ -48,25 +49,28 @@ func TestPlatformPiInvocation_RewritesCmdLauncherToPowerShellFile(t *testing.T)
wantArgs := append([]string{
"-NoProfile",
"-ExecutionPolicy", "Bypass",
- "-File", ps1Path,
+ "-Command", "& '" + ps1Path + "' @args",
}, args...)
if !reflect.DeepEqual(gotArgs, wantArgs) {
t.Errorf("argv mismatch:\n got %#v\n want %#v", gotArgs, wantArgs)
}
// Explicit check: the last argv (the positional prompt) must still
- // contain every line of the original multi-line prompt. This is the
- // concrete property #3306 violates when cmd.exe re-tokenises %*.
+ // contain every line of the original multi-line prompt — forwarded
+ // verbatim through @args.
if gotArgs[len(gotArgs)-1] != multiLinePrompt {
t.Errorf("multi-line prompt was mangled:\n got %q\n want %q", gotArgs[len(gotArgs)-1], multiLinePrompt)
}
+ for _, arg := range gotArgs {
+ if arg == "-File" {
+ t.Fatalf("task runs must not use powershell -File (re-encodes stdin, #7355): %#v", gotArgs)
+ }
+ }
}
// TestPlatformPiInvocation_RPCRoutesThroughPowerShellCommand pins the model
-// discovery path. Pi RPC keeps stdin open for bidirectional JSON-RPC, while the
-// npm pi.ps1 -File wrapper can wait for EOF before forwarding stdin. Use
-// -Command with @args only for RPC so ordinary task execution keeps the
-// established -File launcher behaviour above.
+// discovery path: --mode rpc takes the same -Command route as every other Pi
+// invocation, since the route is now unconditional.
func TestPlatformPiInvocation_RPCRoutesThroughPowerShellCommand(t *testing.T) {
dir := t.TempDir()
cmdPath := filepath.Join(dir, "pi.cmd")