Skip to content
Closed
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
27 changes: 27 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 @@ -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 {
Expand Down
15 changes: 13 additions & 2 deletions internal/runtime/provider_agentcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package runtime
import (
"crypto/rand"
"encoding/hex"
"io"
"os"
"path/filepath"
"slices"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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.
Expand Down