Documentation
¶
Overview ¶
Package process starts the subprocesses Aviary owns, such as CLI providers and the ngrok tunnel, and makes sure none of them outlives Aviary.
Index ¶
- Constants
- func CleanupTimedOut(err error) bool
- func CombinedOutput(cmd *exec.Cmd) ([]byte, error)
- func Command(ctx context.Context, binary string, args ...string) *exec.Cmd
- func ExitAfterReaping(code int)
- func Kill(cmd *exec.Cmd) error
- func Output(cmd *exec.Cmd) ([]byte, error)
- func Pause(cmd *exec.Cmd) error
- func ReapAll()
- func Resume(cmd *exec.Cmd) error
- func Run(cmd *exec.Cmd) error
- func ShimArgv(binary string, args []string) (string, []string, error)
- func ShutdownContext(prepare func()) (context.Context, func())
- func Start(cmd *exec.Cmd) error
- func Stop(cmd *exec.Cmd) error
- func Wait(cmd *exec.Cmd) error
- type OutputDrain
Constants ¶
const CleanupTimedOutNote = "process cleanup timed out: something the agent started kept its output open after it exited, and was killed"
CleanupTimedOutNote is what a run that finished, but whose process cleanup timed out, notes instead of failing.
Variables ¶
This section is empty.
Functions ¶
func CleanupTimedOut ¶ added in v0.7.25
CleanupTimedOut reports whether err is Wait's report that the child exited successfully but its output pipes stayed open past cmd.WaitDelay. The child itself finished; only the processes it left behind did not.
func CombinedOutput ¶
CombinedOutput runs a tracked child and returns its standard output and standard error interleaved, the tracked equivalent of exec.Cmd.CombinedOutput.
func Command ¶
Command is exec.CommandContext for a CLI provider such as claude or codex, started through ShimArgv. When the provider cannot be started without losing an argument, Start reports why, so the caller fails loudly instead of the CLI running on truncated arguments.
func ExitAfterReaping ¶
func ExitAfterReaping(code int)
ExitAfterReaping terminates every owned subprocess before exiting, because os.Exit runs no deferred cleanup.
func Kill ¶
Kill ends a tracked child and everything it spawned at once, paused or not. It only signals and never waits, so it is safe as an exec.Cmd Cancel function while another goroutine is in Wait. Killing just the child would leave its descendants holding its output pipes, and a paused descendant never lets go of them, so Wait would never return.
func Output ¶
Output runs a tracked child and returns what it wrote to standard output, the tracked equivalent of exec.Cmd.Output.
func Pause ¶
Pause suspends a tracked child and everything it spawned, so nothing in the tree runs until Resume or a kill. The shutdown checkpoint uses it to commit an agent's checkout without catching a half-written file.
func ReapAll ¶
func ReapAll()
ReapAll terminates every owned subprocess. It is safe to call more than once, so shutdown paths can call it defensively.
func ShimArgv ¶
ShimArgv returns the program and arguments to start a CLI provider with.
On Windows a .cmd or .bat file can only run through cmd.exe, and cmd.exe ends the command line at the first newline, so a multi-line argument handed to an npm shim such as claude.cmd reached the CLI cut short, along with every argument after it: a chat's multi-line system prompt dropped the approval flags that followed it (#553). The shim is therefore replaced by the program it launches, which receives every argument intact. A batch file aviary cannot read through is still run as is, unless an argument has a line break it would lose: that is refused with an error naming models.cli_definitions.<name>.binary as the way to point aviary at the real executable.
func ShutdownContext ¶
ShutdownContext returns a context that is canceled by the first shutdown signal, giving aviary a chance to stop its subprocesses in order. The first signal runs prepare, when given, before the context is canceled, so work that must happen while subprocesses are still alive (the shutdown checkpoint, issue #741) finishes first. A second signal means "now": every owned subprocess is killed and aviary exits immediately, even mid-prepare, so an impatient second Ctrl-C cannot leave an orphan behind.
func Start ¶
Start starts cmd as an owned subprocess: it runs in its own process group so the processes it spawns are terminated with it, it is tracked so aviary kills the whole tree before exiting, and the kernel is asked to tear it down should aviary die without running any cleanup at all.
On platforms where the kernel guard can only be installed on a process that already exists, the child is created in a state where it has not run yet and adoptOrphanedProcess releases it; a child that cannot be released is killed rather than handed back to the caller suspended forever.
func Stop ¶
Stop terminates a tracked child's process tree and reaps it, asking politely first so the child can shut itself down cleanly.
func Wait ¶
Wait waits for a tracked child and stops tracking it.
A child that exits while something it started still holds its output pipes leaves Wait waiting on them until cmd.WaitDelay expires, which it reports as exec.ErrWaitDelay. Those leftovers, typically a background shell the agent launched, would otherwise keep running unowned, so the rest of the child's process tree is killed then and the reaped PIDs are logged (#1122).
Types ¶
type OutputDrain ¶ added in v0.7.75
type OutputDrain struct {
// contains filtered or unexported fields
}
OutputDrain is a child's stdout/stderr writer that never makes the child's output wait on whatever consumes it. Writes are queued and handed to the underlying writer by a goroutine of its own, so the pipe exec.Cmd copies from is always read as fast as the child writes it.
Without it, a consumer that is slow for a moment -- a UI refresh that reads CLI quotas, say -- stalls exec.Cmd's copy goroutine. A child that exits during that stall leaves its last output, such as an agent's final result event, in the pipe, and cmd.WaitDelay expires and discards it before it is read. The run is then reported as not exiting cleanly even though the child finished (#1420).
func NewOutputDrain ¶ added in v0.7.75
func NewOutputDrain(out io.Writer) *OutputDrain
NewOutputDrain starts draining into out. Close it once the child has been waited on to deliver everything it wrote.
func (*OutputDrain) Close ¶ added in v0.7.75
func (d *OutputDrain) Close() error
Close waits until every queued write has reached the underlying writer and returns the first error that writer reported.