Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

fix(assistant): pass oversized chat prompts on stdin instead of argv (#996)

yowucai57066840d449
diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go
index 38b55e67..d7d04ead 100644
--- a/internal/httpapi/assistant.go
+++ b/internal/httpapi/assistant.go
@@ -1744,6 +1744,9 @@ func prepareAssistantCommand(opts assistantCommandOpts) (runtime.CommandSpec, er
}
command.CredentialBindings = []runtime.CredentialBinding{runtime.BindCodeBuddy}
command.Telemetry = nil
+ // A fresh session embeds the whole conversation in the prompt; past
+ // MAX_ARG_STRLEN it would fail with E2BIG before the CLI starts.
+ command = runtime.PromptOnStdin(command, opts.Prompt)
return command.WithMCPStartupWait(agentToolSessionMCPStartupWait), nil
}
diff --git a/internal/httpapi/assistant_engine_cli.go b/internal/httpapi/assistant_engine_cli.go
index f8235ce4..8397951b 100644
--- a/internal/httpapi/assistant_engine_cli.go
+++ b/internal/httpapi/assistant_engine_cli.go
@@ -213,6 +213,7 @@ func prepareAssistantClaudeInvocation(o assistantEngineInvocationOpts) (assistan
// --disallowedTools is variadic: end option parsing before the prompt.
argv = append(argv, "--", o.Prompt)
command := runtime.CommandSpec{Argv: argv, Provider: "claude-code", CredentialBindings: []runtime.CredentialBinding{runtime.BindClaudeCode}}
+ command = runtime.PromptOnStdin(command, o.Prompt)
return assistantCLIInvocation{
command: command.WithClaudeBypassAccepted(),
credentials: runtime.RunCredentials{ClaudeCode: o.User},
diff --git a/internal/httpapi/assistant_prompt_stdin_test.go b/internal/httpapi/assistant_prompt_stdin_test.go
new file mode 100644
index 00000000..743ea8e2
--- /dev/null
+++ b/internal/httpapi/assistant_prompt_stdin_test.go
@@ -0,0 +1,94 @@
+package httpapi
+
+import (
+ "io"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "agentflow/internal/runtime"
+)
+
+// Linux rejects any single argv string above MAX_ARG_STRLEN (128KiB).
+const testMaxArgStrlen = 128 << 10
+
+// A fresh chat session embeds the whole conversation in the prompt. Once it
+// passes MAX_ARG_STRLEN the CLI failed with "fork/exec ...: argument list too
+// long" before starting, and every retry failed the same way.
+func oversizedAssistantPrompt() string {
+ return strings.Repeat(`{"role":"user","content":"很长的对话历史"}`, 200<<10/48)
+}
+
+func assertPromptOnStdin(t *testing.T, name string, command runtime.CommandSpec, prompt string) {
+ t.Helper()
+ for _, arg := range command.Argv {
+ if len(arg) >= testMaxArgStrlen {
+ t.Fatalf("%s: argv still carries a %d-byte argument", name, len(arg))
+ }
+ }
+ if command.Argv[len(command.Argv)-1] != "--" {
+ t.Fatalf("%s: argv should end with the -- terminator, got %q", name, command.Argv[len(command.Argv)-1])
+ }
+ if command.Stdin == nil {
+ t.Fatalf("%s: oversized prompt was not moved to stdin", name)
+ }
+ data, err := io.ReadAll(command.Stdin)
+ if err != nil || string(data) != prompt {
+ t.Fatalf("%s: stdin carries %d bytes (err=%v), want %d", name, len(data), err, len(prompt))
+ }
+}
+
+func TestAssistantCodeBuddyOversizedPromptUsesStdin(t *testing.T) {
+ prompt := oversizedAssistantPrompt()
+ cases := map[string]assistantCommandOpts{
+ "platform": {Bin: "codebuddy", Config: "/tmp/mcp.json", Prompt: prompt, SessionID: "s1", Native: true},
+ "workspace": {Bin: "codebuddy", Config: "/tmp/mcp.json", Prompt: prompt, SessionID: "s1", Native: true, Workspace: true},
+ "resume": {Bin: "codebuddy", Config: "/tmp/mcp.json", Prompt: prompt, SessionID: "s1", Native: true, Resume: true},
+ "report": {Bin: "codebuddy", Config: "/tmp/mcp.json", Prompt: prompt, ReportOnly: true},
+ "review": {Bin: "codebuddy", Config: "/tmp/mcp.json", Prompt: prompt, ReviewOnly: true},
+ }
+ for name, opts := range cases {
+ command, err := prepareAssistantCommand(opts)
+ if err != nil {
+ t.Fatalf("%s: %v", name, err)
+ }
+ assertPromptOnStdin(t, name, command, prompt)
+ if !runtime.ConversationCommandFor(command, runtime.RunCredentials{}).Interactive {
+ t.Fatalf("%s: remote conversation must forward stdin", name)
+ }
+ }
+}
+
+func TestAssistantCodeBuddyShortPromptStaysOnArgv(t *testing.T) {
+ command, err := prepareAssistantCommand(assistantCommandOpts{Bin: "codebuddy", Config: "/tmp/mcp.json", Prompt: "你好", SessionID: "s1", Native: true})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if command.Argv[len(command.Argv)-1] != "你好" || command.Stdin != nil {
+ t.Fatalf("short prompt should stay positional: %q", command.Argv[len(command.Argv)-2:])
+ }
+}
+
+func TestAssistantClaudeOversizedPromptUsesStdin(t *testing.T) {
+ root := t.TempDir()
+ config := filepath.Join(root, "mcp.json")
+ if err := os.WriteFile(config, []byte(`{"mcpServers":{}}`), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ prompt := oversizedAssistantPrompt()
+ inv, err := prepareAssistantClaudeInvocation(assistantEngineInvocationOpts{Bin: "claude", Root: root, Config: config, Prompt: prompt, Model: "m", SessionID: "s"})
+ if err != nil {
+ t.Fatal(err)
+ }
+ assertPromptOnStdin(t, "claude", inv.command, prompt)
+}
+
+func TestClarifyOversizedPromptUsesStdin(t *testing.T) {
+ prompt := oversizedAssistantPrompt()
+ command, err := prepareClarifyCommandWith(clarifyCommandOpts{Bin: "codebuddy", Prompt: prompt, SessionID: "s", Native: true, MCPConfig: "/tmp/mcp.json", SubmitOnly: true})
+ if err != nil {
+ t.Fatal(err)
+ }
+ assertPromptOnStdin(t, "clarify", command, prompt)
+}
diff --git a/internal/httpapi/clarify_session.go b/internal/httpapi/clarify_session.go
index 67bc7ced..04241a7d 100644
--- a/internal/httpapi/clarify_session.go
+++ b/internal/httpapi/clarify_session.go
@@ -240,7 +240,7 @@ func prepareClarifyCommandWith(opts clarifyCommandOpts) (runtime.CommandSpec, er
if strings.TrimSpace(opts.MCPConfig) != "" {
command = command.WithMCPStartupWait(agentToolSessionMCPStartupWait)
}
- return command, nil
+ return runtime.PromptOnStdin(command, opts.Prompt), nil
}
func appendDisallowedTools(argv []string, extra string) []string {
diff --git a/internal/runtime/process.go b/internal/runtime/process.go
index a5c53652..f4e14983 100644
--- a/internal/runtime/process.go
+++ b/internal/runtime/process.go
@@ -43,6 +43,11 @@ func runCommand(cmd *exec.Cmd, sink EventSink) (string, error) {
sink(domain.TelemetryEvent{Category: "process", Name: "process.start_failed", Timestamp: time.Now().UTC(), Phase: "start", Level: "error", Message: err.Error()})
}
if err != nil && cmd.Process == nil {
+ if errors.Is(err, syscall.E2BIG) {
+ // Retrying with the same input fails again; name the limit so the
+ // diagnostic points at the oversized argument, not the CLI.
+ err = fmt.Errorf("%w (a single argument exceeds the ~128KiB kernel limit; pass long prompts on stdin or in a file)", err)
+ }
err = fmt.Errorf("%w: %w", errProcessNotStarted, err)
}
if cmd.Process != nil && cmd.SysProcAttr != nil && cmd.SysProcAttr.Setpgid {
diff --git a/internal/runtime/prompt_stdin_test.go b/internal/runtime/prompt_stdin_test.go
new file mode 100644
index 00000000..ca1cd2c9
--- /dev/null
+++ b/internal/runtime/prompt_stdin_test.go
@@ -0,0 +1,75 @@
+package runtime
+
+import (
+ "context"
+ "io"
+ goruntime "runtime"
+ "strconv"
+ "strings"
+ "testing"
+)
+
+func TestPromptOnStdinKeepsShortPromptOnArgv(t *testing.T) {
+ command := CommandSpec{Argv: []string{"codebuddy", "-p", "--", "hello"}}
+ got := PromptOnStdin(command, "hello")
+ if strings.Join(got.Argv, " ") != "codebuddy -p -- hello" || got.Stdin != nil {
+ t.Fatalf("short prompt should stay on argv: %q stdin=%v", got.Argv, got.Stdin != nil)
+ }
+}
+
+func TestPromptOnStdinMovesOversizedPrompt(t *testing.T) {
+ prompt := strings.Repeat("对话历史", maxArgvPromptBytes/12+100)
+ argv := []string{"codebuddy", "-p", "--output-format", "stream-json", "--", prompt}
+ got := PromptOnStdin(CommandSpec{Argv: argv, Provider: "codebuddy"}, prompt)
+ if strings.Join(got.Argv, " ") != "codebuddy -p --output-format stream-json --" {
+ t.Fatalf("oversized prompt must leave argv and keep the -- terminator: %d args, last=%.20q", len(got.Argv), got.Argv[len(got.Argv)-1])
+ }
+ if got.Stdin == nil {
+ t.Fatal("oversized prompt must be sent on stdin")
+ }
+ data, err := io.ReadAll(got.Stdin)
+ if err != nil || string(data) != prompt {
+ t.Fatalf("stdin prompt mismatch: %d bytes, err=%v", len(data), err)
+ }
+ if argv[len(argv)-1] != prompt {
+ t.Fatal("the caller's argv must not be modified")
+ }
+}
+
+func TestPromptOnStdinLeavesOtherShapesAlone(t *testing.T) {
+ prompt := strings.Repeat("x", maxArgvPromptBytes+1)
+ cases := map[string]CommandSpec{
+ "no terminator": {Argv: []string{"cli", "-p", prompt}},
+ "different prompt": {Argv: []string{"cli", "-p", "--", "summary"}},
+ "stdin in use": {Argv: []string{"cli", "-p", "--", prompt}, Stdin: strings.NewReader("protocol")},
+ }
+ for name, command := range cases {
+ got := PromptOnStdin(command, prompt)
+ if len(got.Argv) != len(command.Argv) || got.Stdin != command.Stdin {
+ t.Fatalf("%s: command should be unchanged", name)
+ }
+ }
+}
+
+// Linux rejects a single argv string above MAX_ARG_STRLEN (128KiB) before the
+// process starts. A chat prompt that embeds a long conversation must still
+// reach the CLI, so it travels on stdin instead.
+func TestOversizedChatPromptReachesCLIOnStdin(t *testing.T) {
+ if goruntime.GOOS != "linux" {
+ t.Skip("MAX_ARG_STRLEN is a Linux limit")
+ }
+ prompt := strings.Repeat("长对话", 200<<10/9) // ~200KiB, above MAX_ARG_STRLEN
+ command := CommandSpec{Argv: []string{"sh", "-c", "wc -c", "--", prompt}}
+
+ if _, err := ExecuteLocalCommand(context.Background(), t.TempDir(), command, RunCredentials{}, nil); err == nil || !strings.Contains(err.Error(), "argument list too long") || !strings.Contains(err.Error(), "128KiB") {
+ t.Fatalf("argv prompt should fail with an explained E2BIG, got %v", err)
+ }
+
+ out, err := ExecuteLocalCommand(context.Background(), t.TempDir(), PromptOnStdin(command, prompt), RunCredentials{}, nil)
+ if err != nil {
+ t.Fatalf("stdin prompt should start the CLI: %v", err)
+ }
+ if n, convErr := strconv.Atoi(strings.TrimSpace(out)); convErr != nil || n != len(prompt) {
+ t.Fatalf("CLI received %q bytes on stdin, want %d", strings.TrimSpace(out), len(prompt))
+ }
+}
diff --git a/internal/runtime/provider.go b/internal/runtime/provider.go
index e22cf4c0..38df8dfd 100644
--- a/internal/runtime/provider.go
+++ b/internal/runtime/provider.go
@@ -105,6 +105,28 @@ func positionalPrompt(in providerInvocation) string {
return in.Prompt
}
+// PromptOnStdin moves an oversized trailing positional prompt to stdin.
+//
+// Linux rejects any single argv string above MAX_ARG_STRLEN (128KiB) with
+// E2BIG, so the CLI never starts ("fork/exec ...: argument list too long").
+// Chat prompts embed the whole conversation on a fresh native session and
+// have no prompt file to point at. CodeBuddy and Claude Code in print mode
+// read the prompt from stdin when no positional prompt is given; stdin is
+// forwarded by local, Docker and remote conversation execution.
+//
+// Only a command ending in "--", prompt is rewritten. The "--" terminator
+// stays so flags appended later by callers remain before it. Short prompts
+// keep the existing argv form.
+func PromptOnStdin(command CommandSpec, prompt string) CommandSpec {
+ n := len(command.Argv)
+ if len(prompt) <= maxArgvPromptBytes || command.Stdin != nil || n < 3 || command.Argv[n-1] != prompt || command.Argv[n-2] != "--" {
+ return command
+ }
+ command.Argv = append([]string(nil), command.Argv[:n-1]...)
+ command.Stdin = strings.NewReader(prompt)
+ return command
+}
+
func (p baseProvider) Protocol(ex domain.ExecutorDefinition, sink EventSink) *protocolAdapter {
return makeProtocolAdapter(ex, sink, p.parser, p.adapter)
}