mirror of
https://github.com/multica-ai/multica.git
synced 2026-07-27 04:56:20 +02:00
* fix(agent/cursor): send prompt on stdin so CLI-like flags cannot be re-tokenised (MUL-4992) On Windows a Cursor task whose prompt contains CLI-like flags failed in ~2s with `error: unknown option '-X'` and no agent output (#5649). A pasted build log such as go build -ldflags "-X main.version=foo" -o bin/server ./cmd/server was enough to kill the run. buildCursorArgs put the whole prompt in argv as the positional after -p. The official Windows launcher chain ends in `& node.exe index.js $args` inside cursor-agent.ps1, where PowerShell re-serialises $args onto node's command line. Under Windows PowerShell 5.1 and pwsh <= 7.2 (Legacy native argument passing) an argument holding embedded double quotes is not re-escaped, so the quoted region closes early, node's argv parser re-splits at the interior spaces, and `-X` reaches commander.js as a standalone flag. #1709 removed the cmd.exe `%*` re-tokenisation but stopped at the Go -> PowerShell boundary, one hop before this. Its Windows tests only compare the argv slice Go builds and never execute a shim, which is why the gap stayed invisible. Fix: keep the prompt off every command line. cursor-agent's -p is a boolean print-mode switch and the prompt is positional; with no positional prompt and a non-TTY stdin the CLI reads stdin to EOF and uses that as the prompt. So drop the prompt from argv on all platforms and write it to stdin, leaving only fixed, content-free flags in argv. No shell or launcher on any platform can re-tokenise what is not on a command line. The write runs in its own goroutine: a prompt larger than the pipe buffer (~64 KiB) blocks mid-write until the child drains it, and the child cannot drain while nothing reads its stdout. Closing stdin signals end-of-prompt, so it is closed on both success and error paths, and on cancellation to release a blocked write. Write failures surface in the result diagnostic, ranked below explicit agent errors so an early child exit (bad auth, bad flag) is not masked by the resulting EPIPE. Tests: a prompt carrying the exact `-ldflags "-X ..."` shape must arrive byte-for-byte on stdin and appear nowhere in argv; a 512 KiB prompt must not deadlock; the prompt is written verbatim (the CLI trims it, we do not). Both new unix tests fail against the pre-fix code. A Windows-tagged test drives a real PowerShell host through the same .cmd -> -File rewrite. Closes #5649 Co-authored-by: multica-agent <github@multica.ai> * test(agent/cursor): prove the Windows launcher fix on both PowerShell hosts in CI The stdin fix has to hold on the host that actually exhibits the bug. powershell.exe (5.1) and pwsh <= 7.2 default to Legacy native argument passing; pwsh >= 7.3 defaults to Standard. A fix verified only on the newer host would not be a fix for the reporter. Run the shim probe against every PowerShell host on PATH rather than only the one defaultPowerShellLookup would select, and hook the windows-tagged launcher tests into the existing windows-execenv CI job. These tests are windows-tagged and the backend job runs on ubuntu, so until now they ran nowhere. Co-authored-by: multica-agent <github@multica.ai> * test(ci): run Windows launcher tests verbosely so skips are visible A skipped or unmatched test still reports "ok", which would make the Windows job look like coverage it is not providing. Co-authored-by: multica-agent <github@multica.ai> * test(agent/cursor): close both coverage gaps in the #5649 regression tests Two tests claimed guarantees they did not actually establish. Windows: the fake cursor-agent.ps1 called [Console]::In.ReadToEnd() and wrote the result itself, so it never launched a native child. The official shim ends in `& node.exe index.js $args`, and that last hop is precisely where the bug lives -- PowerShell re-serialises $args onto the child command line, and whether the child inherits stdin was left unproven. The fake ps1 now re-executes the test binary as a real native child (helper-process idiom), which records the argv it actually received and drains stdin. Both PowerShell hosts still run. Interlock: the large-prompt fake drained stdin before writing any stdout, so the child always unblocked the parent immediately and the test passed even against a synchronous write -- it could not fail for the reason it existed. The fake now floods stdout past pipe capacity *before* reading stdin, creating the real mutual block. Verified: with the writer made synchronous the test deadlocks to its 30s timeout, and passes only with the concurrent writer. Also adds the missing cancellation case: a child that never reads stdin leaves the writer blocked forever, so cancelling the context must close stdin, release the writer and settle the run as aborted. Co-authored-by: multica-agent <github@multica.ai> --------- Co-authored-by: Bohan-J <bohan@devv.ai> Co-authored-by: multica-agent <github@multica.ai>
188 lines
6.2 KiB
Go
188 lines
6.2 KiB
Go
//go:build windows
|
|
|
|
package agent
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// Env contract between the test and the helper process it re-executes as the
|
|
// native child of the PowerShell shim.
|
|
const (
|
|
shimHelperEnv = "MULTICA_CURSOR_SHIM_HELPER"
|
|
shimHelperArgvFile = "MULTICA_CURSOR_SHIM_ARGV_FILE"
|
|
shimHelperInFile = "MULTICA_CURSOR_SHIM_STDIN_FILE"
|
|
)
|
|
|
|
// TestCursorShimHelperProcess is not a test. Re-executed by the fake
|
|
// cursor-agent.ps1 below, it stands in for `node.exe index.js` as a real native
|
|
// child: it records the argv it was handed, drains stdin, and emits the
|
|
// terminal stream-json event. Inert unless the shim env var is set.
|
|
func TestCursorShimHelperProcess(t *testing.T) {
|
|
if os.Getenv(shimHelperEnv) != "1" {
|
|
t.Skip("helper process; only runs when re-executed by the shim")
|
|
}
|
|
|
|
// Everything after "--" is what the shim forwarded to us.
|
|
var forwarded []string
|
|
for i, a := range os.Args {
|
|
if a == "--" {
|
|
forwarded = os.Args[i+1:]
|
|
break
|
|
}
|
|
}
|
|
if err := os.WriteFile(os.Getenv(shimHelperArgvFile), []byte(strings.Join(forwarded, "\n")), 0o644); err != nil {
|
|
fmt.Fprintf(os.Stderr, "helper: write argv: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
stdin, err := io.ReadAll(os.Stdin)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "helper: read stdin: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
if err := os.WriteFile(os.Getenv(shimHelperInFile), stdin, 0o644); err != nil {
|
|
fmt.Fprintf(os.Stderr, "helper: write stdin: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
fmt.Println(`{"type":"result","subtype":"success","is_error":false,"result":"ok"}`)
|
|
// Exit before the test framework can print PASS/ok into the JSON stream.
|
|
os.Exit(0)
|
|
}
|
|
|
|
// TestCursorExecutePromptSurvivesPowerShellShim is the Windows half of the
|
|
// #5649 regression, and it exercises the full production launch chain:
|
|
//
|
|
// Go → powershell -File cursor-agent.ps1 → native child
|
|
//
|
|
// The last hop is the one that matters and the one a shim-only test cannot
|
|
// reach. The official cursor-agent.ps1 ends in `& node.exe index.js $args`, so
|
|
// the fake ps1 here likewise invokes a *native* executable with $args and lets
|
|
// that child record argv and read stdin. That proves both properties the fix
|
|
// depends on end to end:
|
|
//
|
|
// - the prompt is absent from the argv PowerShell re-serialises onto the
|
|
// child's command line, so there is nothing left to re-tokenise; and
|
|
// - stdin is still inherited by the native child, byte for byte.
|
|
//
|
|
// It runs against every PowerShell host present, not just the one
|
|
// defaultPowerShellLookup would pick, because the hosts differ on exactly the
|
|
// mechanism behind this bug: powershell.exe (5.1) and pwsh <= 7.2 default to
|
|
// Legacy native argument passing, pwsh >= 7.3 to Standard. A fix that only held
|
|
// on the newer host would be no fix at all for the reporter.
|
|
func TestCursorExecutePromptSurvivesPowerShellShim(t *testing.T) {
|
|
hosts := availablePowerShellHosts()
|
|
if len(hosts) == 0 {
|
|
t.Skip("no PowerShell host available")
|
|
}
|
|
for _, host := range hosts {
|
|
t.Run(filepath.Base(host), func(t *testing.T) {
|
|
stubPowerShell(t, host, true)
|
|
assertPromptSurvivesShim(t)
|
|
})
|
|
}
|
|
}
|
|
|
|
// availablePowerShellHosts resolves every PowerShell host on PATH so the
|
|
// regression can be proven on each independently.
|
|
func availablePowerShellHosts() []string {
|
|
var found []string
|
|
for _, name := range []string{"powershell.exe", "pwsh.exe"} {
|
|
if p, err := exec.LookPath(name); err == nil {
|
|
found = append(found, p)
|
|
}
|
|
}
|
|
return found
|
|
}
|
|
|
|
func assertPromptSurvivesShim(t *testing.T) {
|
|
t.Helper()
|
|
|
|
self, err := os.Executable()
|
|
if err != nil {
|
|
t.Fatalf("locate test binary to use as native child: %v", err)
|
|
}
|
|
|
|
dir := t.TempDir()
|
|
argvPath := filepath.Join(dir, "argv.txt")
|
|
stdinPath := filepath.Join(dir, "stdin.txt")
|
|
|
|
// The .cmd only has to exist and carry the right extension; the rewrite
|
|
// routes around it to the sibling .ps1, which is what actually runs.
|
|
cmdPath := filepath.Join(dir, "cursor-agent.cmd")
|
|
writeFile(t, cmdPath, "@echo off\r\npowershell -NoProfile -ExecutionPolicy Bypass -File \"%~dp0cursor-agent.ps1\" %*\r\n")
|
|
|
|
// Shaped like the official shim: set up, then hand $args to a native child.
|
|
ps1 := fmt.Sprintf(""+
|
|
"$env:%s = '1'\r\n"+
|
|
"$env:%s = '%s'\r\n"+
|
|
"$env:%s = '%s'\r\n"+
|
|
"& '%s' '-test.run=^TestCursorShimHelperProcess$' '--' $args\r\n"+
|
|
"exit $LASTEXITCODE\r\n",
|
|
shimHelperEnv,
|
|
shimHelperArgvFile, argvPath,
|
|
shimHelperInFile, stdinPath,
|
|
self)
|
|
writeFile(t, filepath.Join(dir, "cursor-agent.ps1"), ps1)
|
|
|
|
prompt := "Please fix the build.\n" +
|
|
`go build -ldflags "-X main.version=foo -X main.commit=bar" -o bin/server ./cmd/server` + "\n" +
|
|
"Thanks."
|
|
|
|
backend, err := New("cursor", Config{ExecutablePath: cmdPath, Logger: slog.Default()})
|
|
if err != nil {
|
|
t.Fatalf("New(cursor): %v", err)
|
|
}
|
|
session, err := backend.Execute(t.Context(), prompt, ExecOptions{Timeout: 60 * time.Second})
|
|
if err != nil {
|
|
t.Fatalf("Execute: %v", err)
|
|
}
|
|
go func() {
|
|
for range session.Messages {
|
|
}
|
|
}()
|
|
result := <-session.Result
|
|
|
|
argvRaw, err := os.ReadFile(argvPath)
|
|
if err != nil {
|
|
t.Fatalf("native child never recorded argv (did the shim reach it?): %v; result=%+v", err, result)
|
|
}
|
|
stdinRaw, err := os.ReadFile(stdinPath)
|
|
if err != nil {
|
|
t.Fatalf("native child never recorded stdin: %v; result=%+v", err, result)
|
|
}
|
|
|
|
// argv as the *native child* received it — past PowerShell's $args
|
|
// re-serialisation, which is where #5649 was introduced.
|
|
for _, a := range strings.Split(strings.TrimSuffix(string(argvRaw), "\n"), "\n") {
|
|
for _, needle := range []string{"-X", "ldflags", "main.version", "Please fix"} {
|
|
if strings.Contains(a, needle) {
|
|
t.Errorf("prompt fragment %q leaked into native child argv element %q", needle, a)
|
|
}
|
|
}
|
|
}
|
|
// The content-free flags must still survive the same hop.
|
|
gotArgv := string(argvRaw)
|
|
for _, want := range []string{"-p", "--output-format", "stream-json", "--yolo"} {
|
|
if !strings.Contains(gotArgv, want) {
|
|
t.Errorf("expected %q to reach the native child; argv was %q", want, gotArgv)
|
|
}
|
|
}
|
|
|
|
if string(stdinRaw) != prompt {
|
|
t.Errorf("prompt did not survive Go → PowerShell → native child:\n got %q\n want %q", string(stdinRaw), prompt)
|
|
}
|
|
|
|
if result.Status != "completed" {
|
|
t.Fatalf("status = %q, want completed; error=%q", result.Status, result.Error)
|
|
}
|
|
}
|