Skip to content

Auto-fallback to stdin when prompt exceeds ARG_MAX #306

Closed

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.

Activity

  1. poe-code-agent commented on May 13, 2026

    @poe-code-agent
    Contributor

    Implemented this in PR #307.

    The change automatically switches large prompts (>64 KiB UTF-8) to stdin for CLI agents that declare stdinMode, and applies the same decision in both buildSpawnArgs and spawn.

    Validation passed: targeted agent-spawn tests, typecheck, agent-spawn build, eslint, and the full pre-push npm test run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions