Skip to content

Auto-fallback to stdin when prompt exceeds ARG_MAX #306

@kamilio

Description

@kamilio

Problem

spawn(...) passes the prompt as an argv element by default (useStdin: false). For agents whose spawn config declares promptFlag (e.g. codex exec <prompt>), large prompts cause the kernel to reject the spawn with E2BIG ("Argument list too long") before the agent starts.

On Linux the per-arg limit is typically ~128KB; total argv+envp is ~2MB. We hit this in an automations workflow that passes a rendered PR diff + reviewer profile (~150KB) into codex via spawn("codex", { prompt, ... }).

Current workaround

Callers explicitly opt in: spawn("codex", { prompt, useStdin: true }). This works because codexService.supportsStdinPrompt === true and codexSpawnConfig.stdinMode = { omitPrompt: true, extraArgs: ["-"] } are already wired through in packages/agent-spawn/src/spawn.ts.

Suggestion

Automatically switch to stdin when both:

  1. spawnConfig.stdinMode is defined (i.e. the adapter supports it), and
  2. Buffer.byteLength(options.prompt, "utf8") exceeds a safe threshold (~64KB leaves headroom for env, mcp args, mode flags).

Sketch:

```ts
const PROMPT_STDIN_FALLBACK_BYTES = 64 * 1024;

function resolveStdinMode(spawnConfig, options) {
if (!spawnConfig.stdinMode) return undefined;
if (options.useStdin) return spawnConfig.stdinMode;
if (Buffer.byteLength(options.prompt, "utf8") > PROMPT_STDIN_FALLBACK_BYTES) {
return spawnConfig.stdinMode;
}
return undefined;
}
```

Used in both buildSpawnArgs and spawn in packages/agent-spawn/src/spawn.ts. Callers without useStdin: true would silently get the stdin path for large prompts instead of a kernel rejection.

Alternative

Make the failure mode obvious: detect at spawn time, throw a clear error pointing callers at useStdin: true. Less magic, but still better than a bare E2BIG.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions