From b66c1cc4908c1294f6099d598b883fe26bcf8d08 Mon Sep 17 00:00:00 2001 From: REI-330 <173389910+REI-330@users.noreply.github.com> Date: Sat, 21 Nov 2026 13:25:16 +0800 Subject: [PATCH] fix(runtime): send oversized pi prompts on stdin The pi provider appended the whole prompt to argv. A chat prompt embeds the conversation history and can grow past Linux MAX_ARG_STRLEN, so fork/exec failed with E2BIG ("argument list too long") before the CLI started. Pi reads the prompt from stdin when the positional argument is omitted, so route an oversized prompt through CommandSpec.Stdin and keep short turns on argv. --- internal/runtime/agent_cli_test.go | 27 +++++++++++++++++++++++++++ internal/runtime/provider_agentcli.go | 15 +++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/internal/runtime/agent_cli_test.go b/internal/runtime/agent_cli_test.go index 8dacb432a..d3e8c78e9 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 600d4a649..e9cc90ea8 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.