Help us shape the future of Go! Take the 2026 Go Developer Survey

procx

package
v0.7.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package procx 子进程托管纪律单源:进程组执行(Setpgid 建组 → 超时/取消 TERM 整组 → 宽限 SIGKILL)、限容采集(stdout/单行双上限)、stdout 按行拆分、 子进程环境白名单(绝不继承宿主全量环境)。 acpx 各 CLI agent 适配器与 mcp/workcopy 等自行 spawn 外部命令的包共用本包, 纪律只有一份。

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTimeout 运行超过 Timeout 被终止。
	ErrTimeout = errors.New("执行超时")
	// ErrCanceled 运行被调用方 context 取消。
	ErrCanceled = errors.New("执行被取消")
)

sentinel 错误:调用方可 errors.Is 分类(超时 / 被取消)。

Functions

func ChildEnv

func ChildEnv(extra []string) []string

ChildEnv 构造子进程最小环境(导出:mcp 等自行 spawn 外部子进程的包共用同一 纪律;经 procx.Run 执行时无需调用——Run 内部已应用): 白名单 + 显式透传项 + 禁交互。 extra 两种形态:纯名(如 "HF_TOKEN")从当前进程按名透传(不存在则丢弃); 含 "="(如 "HF_TOKEN=xxx")按 KEY=VALUE 字面注入,且同名父进程值不透传 (每 key 唯一,避免 environ 同名两项时子进程取值依实现而异)。 尾部固定追加 GIT_TERMINAL_PROMPT=0(git 弹终端提问会挂到超时)与 CI=1—— 对非 git 子进程无害。

func Run

func Run(ctx context.Context, req RunRequest) (stdout, stderr string, exitCode int, err error)

Run 以进程组纪律执行外部命令:Setpgid 建组 → 超时/取消 TERM 整组 → killGrace 宽限后 SIGKILL。超时/取消可经 errors.Is(err, ErrTimeout/ErrCanceled) 程序化区分;非零退出返回 error(含 stderr 尾巴),exitCode 一并返回。 环境走白名单——待执行目录里的命令可执行任意代码,全量环境等于泄漏宿主凭证。

Types

type RunRequest

type RunRequest struct {
	// Argv 可执行 + 参数(argv 直传,无 shell、无注入面)。
	Argv []string
	// Dir 工作目录(空 = 继承当前进程)。
	Dir string
	// Env 子进程环境白名单(按名从当前进程透传;含 "=" 按 KEY=VALUE 字面),
	// 叠加在 baseEnvAllow 基础集之上——绝不全量继承。
	Env []string
	// Timeout 单次执行超时(0 = 10 分钟缺省)。
	Timeout time.Duration
	// OnLine 可选 stdout 按行回调(nil = 只缓冲;回调在读取 goroutine 中执行,
	// 不得阻塞/panic;单行超 maxLineLen 会被丢弃)。
	OnLine func(string)
	// MaxStdout stdout 采集上限(0 = 8MB 缺省)。
	MaxStdout int
	// Stdin 可选:启动后写入子进程 stdin 并关闭(hookx 等交互式协议用)。
	// 子进程不读 stdin 提前退出时写入失败静默容忍(printf 类命令无害)。
	Stdin []byte
}

RunRequest 独立进程执行请求:只要进程组托管纪律的调用方使用 (acpx 各 CLI agent 适配器经 acpx.runCLI 复用同一纪律)。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL