Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions internal/runtime/agent_cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package runtime

import (
"encoding/json"
"io"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -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)
}
}
17 changes: 15 additions & 2 deletions internal/runtime/provider_agentcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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.
Expand Down