Documentation
¶
Overview ¶
Package runner owns agent CLI subprocess lifecycle: spawning, streaming stdout line by line, capturing a bounded stderr tail, enforcing idle and absolute deadlines, and making sure nothing outlives the request.
Adapters do not touch os/exec directly; they only build argv and parse lines.
Index ¶
Constants ¶
const ( DefaultMaxLineBytes = 16 << 20 // 16 MiB DefaultStderrTailBytes = 8 << 10 // 8 KiB )
Defaults for the size limits. They are generous on purpose: a single stream-json line can carry a whole assistant message.
Variables ¶
This section is empty.
Functions ¶
func Environ ¶
Environ builds the child environment: the base allowlist, plus any variable matching one of allowPrefixes (adapters use this to let their own vendor variables through, e.g. "ANTHROPIC_"), plus the variables named in passthrough, plus explicit overrides from extra.
passthrough is for what a prefix cannot anticipate: a CLI configured to read its key from a variable of the user's choosing, such as a codex model_providers entry with env_key = "PROVIDER_API_KEY". Naming it exactly keeps the rest of the gateway's environment out, and keeps the key itself out of the config file. A name that is not set is skipped, not set empty: the CLI's own "missing variable" message is clearer than an empty credential.
The result is sorted, so argv/env is reproducible across runs.
func ExpandPath ¶
ExpandPath resolves a leading "~" against the user's home directory.
func ScratchDir ¶
ScratchDir creates an empty per-request working directory under root (or the OS temp directory when root is empty) and returns it with a cleanup func.
Running the CLI here is defense in depth: even if a tool-disable flag is renamed or ignored by a future CLI version, the process starts somewhere with nothing in it rather than in the gateway's own directory.
Types ¶
type Result ¶
type Result struct {
ExitCode int
// Stderr holds the last StderrTailBytes bytes the child wrote to stderr.
Stderr string
}
Result reports how the child ended. It is filled in even when Run returns an error, so callers can inspect the CLI's own complaint.
func Run ¶
Run starts the process and calls onLine for every line the child writes to stdout, in order, from a single goroutine. Run returns once the child has exited and all output has been consumed.
If onLine returns an error, the child is killed and that error is returned. Infrastructure failures (missing binary, timeout, non-zero exit) come back as *ir.Error so frontends can map them to a status without re-classifying.
type Spec ¶
type Spec struct {
// Binary is a command name resolved through PATH, or an absolute path.
Binary string
Args []string
// Adapter is the adapter id this run belongs to, used to name the config
// keys in a timeout message. Empty is fine for calls that are not a
// request, like probing an install.
Adapter string
// Dir is the working directory. Callers should point this at an empty
// scratch directory so the CLI has nothing real within reach.
Dir string
// Env is the complete environment for the child; nothing is inherited
// implicitly. Build it with [Environ].
Env []string
// Stdin is written to the child's stdin, which is then closed. Passing the
// prompt this way avoids OS argv length limits on long conversations.
Stdin string
// RequestTimeout is the absolute cap on the run. Zero means no cap.
RequestTimeout time.Duration
// IdleTimeout kills the child when it has produced no output line for this
// long. Zero disables the watchdog.
IdleTimeout time.Duration
MaxLineBytes int
StderrTailBytes int
}
Spec describes one subprocess run.