diff --git a/internal/runtime/agent_cli_test.go b/internal/runtime/agent_cli_test.go index 8dacb432..d3e8c78e 100644 --- a/internal/runtime/agent_cli_test.go +++ b/internal/runtime/agent_cli_test.go @@ -2,6 +2,7 @@ package runtime import ( "encoding/json" + "io" "os" "path/filepath" "reflect" @@ -71,6 +72,32 @@ func TestAgentCLICommandChatRestrictions(t *testing.T) { } } +func TestPiOversizedPromptGoesToStdin(t *testing.T) { + // Linux rejects any single argv string above MAX_ARG_STRLEN (128KiB), so a + // chat prompt that embeds a long conversation must reach pi on stdin. + prompt := strings.Repeat("长对话", maxArgvPromptBytes/9+100) + command := AgentCLICommand(AgentCLIPi, AgentCLICommandOptions{ + Prompt: prompt, SystemPromptFile: "/s.md", Chat: true, Workspace: true, + }) + for _, arg := range command.Argv { + if !PromptFitsArgv(len(arg)) { + t.Fatalf("argv still carries an oversized argument (%d bytes)", len(arg)) + } + } + if command.Stdin == nil { + t.Fatal("oversized prompt must be sent on stdin") + } + data, err := io.ReadAll(command.Stdin) + if err != nil || string(data) != prompt { + t.Fatalf("stdin prompt mismatch: %d bytes, err=%v", len(data), err) + } + // Short turns keep the existing positional form. + short := AgentCLICommand(AgentCLIPi, AgentCLICommandOptions{Prompt: "hello"}) + if short.Stdin != nil || short.Argv[len(short.Argv)-1] != "hello" { + t.Fatalf("short pi turn changed: argv=%q stdin=%v", short.Argv, short.Stdin != nil) + } +} + func TestKimiThinkingConfiguration(t *testing.T) { home := t.TempDir() for _, tc := range []struct { diff --git a/internal/runtime/provider_agentcli.go b/internal/runtime/provider_agentcli.go index 600d4a64..e9cc90ea 100644 --- a/internal/runtime/provider_agentcli.go +++ b/internal/runtime/provider_agentcli.go @@ -3,6 +3,7 @@ package runtime import ( "crypto/rand" "encoding/hex" + "io" "os" "path/filepath" "slices" @@ -121,6 +122,7 @@ func AgentCLICommand(kind string, o AgentCLICommandOptions) CommandSpec { } extra := append([]string{}, o.Extra...) var argv []string + var stdin io.Reader readOnly := o.Chat && !o.Workspace options := AgentCLIOptions{Model: model, Effort: effort, ReadOnly: readOnly} switch kind { @@ -207,7 +209,16 @@ func AgentCLICommand(kind string, o AgentCLICommandOptions) CommandSpec { if readOnly { argv = append(argv, "--exclude-tools", "bash,edit,write") } - argv = append(append(argv, extra...), prompt) + argv = append(argv, extra...) + if PromptFitsArgv(len(prompt)) { + argv = append(argv, prompt) + } else { + // Pi reads the prompt from stdin when the positional argument is + // omitted. A chat prompt embeds the whole conversation and can + // exceed Linux's MAX_ARG_STRLEN, which fails the fork with E2BIG + // ("argument list too long") before the CLI starts. + stdin = strings.NewReader(prompt) + } case AgentCLIDeepSeek: mode := "danger-full-access" if readOnly { @@ -229,7 +240,7 @@ func AgentCLICommand(kind string, o AgentCLICommandOptions) CommandSpec { } argv = append(argv, extra...) } - return CommandSpec{Argv: argv, Provider: kind, AgentCLI: options} + return CommandSpec{Argv: argv, Provider: kind, AgentCLI: options, Stdin: stdin} } // AgyModelEfforts returns the separate effort choices for Gemini API models.