diff --git a/internal/runtime/agent_cli_test.go b/internal/runtime/agent_cli_test.go index c518421d..d9321f67 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" @@ -516,3 +517,33 @@ func TestOpenAgentCLIHomeTrustsBase(t *testing.T) { } } } + +// A chat turn embeds the whole conversation in the prompt. Pi reads it from +// stdin when no positional prompt is given, so an oversized prompt must leave +// argv instead of making fork/exec fail with E2BIG before the CLI starts. +func TestAgentCLIPiOversizedChatPromptUsesStdin(t *testing.T) { + prompt := strings.Repeat("长对话", maxArgvPromptBytes/9+100) + command := AgentCLICommand(AgentCLIPi, AgentCLICommandOptions{Chat: true, Prompt: prompt}) + for _, arg := range command.Argv { + if len(arg) >= len(prompt) { + t.Fatalf("argv still carries the %d-byte prompt: %q", len(arg), arg[:40]) + } + } + if command.Stdin == nil { + t.Fatal("oversized chat prompt was not moved to stdin") + } + data, err := io.ReadAll(command.Stdin) + if err != nil || string(data) != prompt { + t.Fatalf("stdin carries %d bytes (err=%v), want %d", len(data), err, len(prompt)) + } +} + +// SOP invocations point at the task prompt file when the text is too long, so +// their argv shape must not change. +func TestAgentCLIPiSOPPromptStaysOnArgv(t *testing.T) { + prompt := strings.Repeat("任务", maxArgvPromptBytes/6+100) + command := AgentCLICommand(AgentCLIPi, AgentCLICommandOptions{Prompt: prompt}) + if command.Stdin != nil || command.Argv[len(command.Argv)-1] != prompt { + t.Fatalf("SOP prompt should stay positional: stdin=%v", command.Stdin != nil) + } +} diff --git a/internal/runtime/provider_agentcli.go b/internal/runtime/provider_agentcli.go index 600d4a64..5e1c625a 100644 --- a/internal/runtime/provider_agentcli.go +++ b/internal/runtime/provider_agentcli.go @@ -121,6 +121,7 @@ func AgentCLICommand(kind string, o AgentCLICommandOptions) CommandSpec { } extra := append([]string{}, o.Extra...) var argv []string + promptOnStdin := false readOnly := o.Chat && !o.Workspace options := AgentCLIOptions{Model: model, Effort: effort, ReadOnly: readOnly} switch kind { @@ -207,7 +208,15 @@ func AgentCLICommand(kind string, o AgentCLICommandOptions) CommandSpec { if readOnly { argv = append(argv, "--exclude-tools", "bash,edit,write") } - argv = append(append(argv, extra...), prompt) + // A chat turn embeds the whole conversation. Pi reads the prompt from + // stdin when no positional prompt is given; keeping an oversized one + // on argv makes fork/exec fail with E2BIG before the CLI starts. + if o.Chat && !PromptFitsArgv(len(prompt)) { + promptOnStdin = true + argv = append(argv, extra...) + } else { + argv = append(append(argv, extra...), prompt) + } case AgentCLIDeepSeek: mode := "danger-full-access" if readOnly { @@ -229,7 +238,11 @@ func AgentCLICommand(kind string, o AgentCLICommandOptions) CommandSpec { } argv = append(argv, extra...) } - return CommandSpec{Argv: argv, Provider: kind, AgentCLI: options} + spec := CommandSpec{Argv: argv, Provider: kind, AgentCLI: options} + if promptOnStdin { + spec.Stdin = strings.NewReader(prompt) + } + return spec } // AgyModelEfforts returns the separate effort choices for Gemini API models.