Documentation
¶
Overview ¶
Package util contains the file-only part of Gofer binary upgrades. Worker handover and managed server lifecycle remain with their own owners.
Package util holds gofer's small, dependency-free helpers: the allocation capacity hints (CapSum/CapMul) and the environment layering used before a child process is spawned (Environ/MergeEnv/EnvWith).
It is a leaf package: it imports nothing from gofer and only the standard library, so any layer may use it without risking an import cycle (G022). Keep it that way — anything with a real domain, a dependency, or more than a handful of lines belongs in its own package, not here.
Index ¶
- Constants
- func CapMul(n, k int) int
- func CapSum(parts ...int) int
- func EnvWith(base, overrides map[string]string) map[string]string
- func Environ(extra map[string]string) []string
- func EnvironKeyDenied(key string, deny, allow []string) bool
- func EnvironKeysPresent(names []string) []string
- func EnvironWithout(deny, allow []string, extra map[string]string) []string
- func MergeEnv(base, extra map[string]string) map[string]string
- func ParseVersionOutput(raw string) (string, error)
- func RealPath(p string) string
- func StageUpgradeFile(source, target, upgradeID string, size int64, wantSHA256 string) (string, error)
- func SwitchBinary(target, candidate string) (rollback func() error, err error)
- func VerifyUpgradeFile(path string, size int64, wantSHA256 string) error
Constants ¶
const MaxHint = 1 << 20
MaxHint is the largest capacity hint any caller can receive. It is deliberately far below any int limit so that the arithmetic in CapSum/CapMul can never wrap, and high enough (1M elements) that no realistic collection in gofer is clamped: these hints size env blocks, argv blocks and log lines, not bulk payloads.
Variables ¶
This section is empty.
Functions ¶
func CapMul ¶
CapMul returns a capacity hint for a collection holding n groups of k elements each: CapMul(len(m), 2) for "two entries per map key". Non-positive factors yield 0. The result is always in [0, MaxHint].
func CapSum ¶
CapSum returns a capacity hint for a collection that will hold the given element counts added together: CapSum(len(a), len(b)) for "a followed by b". Non-positive parts contribute nothing (a nil map/slice has no elements). The result is always in [0, MaxHint], so the hint can never overflow and can never be rejected by make.
func EnvWith ¶
EnvWith returns a fresh copy of base with overrides layered on top. Unlike MergeEnv it always copies, and overrides always win: it is the seam for gofer-owned keys (job metadata, worktree identity, ...) that a caller-supplied env key must never shadow, and the result is safe for the caller to keep mutating.
func Environ ¶
Environ returns os.Environ() with extra layered on top, ready for exec.Cmd.Env or a pty spec. extra wins on key collision: os/exec (and therefore both the local and the pty backend) keeps the LAST occurrence of a duplicated key, so appending extra after the inherited environment overrides it. An empty extra returns the inherited environment unchanged.
func EnvironKeyDenied ¶
EnvironKeyDenied reports whether key is denied by the deny list and not re-admitted by the allow list. Exported because the job pipeline needs the same decision to report WHICH allowances actually took effect (job.env_allowed) — a second spelling of the rule would let the event disagree with the environment.
func EnvironKeysPresent ¶
EnvironKeysPresent returns the subset of names that the CURRENT process environment actually carries a value for, preserving the input order. It is the "which of these keys does the allow list really hand to a child" question the SEC-01 job.env_allowed event answers, without copying values around.
func EnvironWithout ¶
EnvironWithout is Environ with a DENY list applied to the inherited environment (SEC-01): every os.Environ() entry whose key is named in deny is dropped before extra is layered on, unless the same key is named in allow.
The filter exists because gofer's own credentials live in the process environment — `server.token_env: GOFER_TOKEN` for a serve started from the deployment's .env, GOFER_SERVER_TOKEN / GOFER_WORKER_TOKEN for the CLI and the worker — and a job process that inherits them is handed the operator's bearer token. That is not theoretical: a v0.57 leader job without gofer MCP fell back to the inherited server token and reviewed work as the human (design §背景).
Only the INHERITED environment is filtered. `extra` is the caller's own, written-down decision (agent/role/job config, gofer metadata) and passes through verbatim: those keys are configured deliberately, not inherited by accident.
Key matching is case-insensitive, because Windows environment variable names are (and os.Environ preserves whatever case it was set with).
func MergeEnv ¶
MergeEnv returns base with extra layered on top (extra wins on key collision) as a fresh map; neither input is modified. An empty extra returns base itself — there is nothing to add, so no copy is needed.
func ParseVersionOutput ¶
ParseVersionOutput removes terminal color and returns the version token printed by Gofer's --version. Worker and managed server upgrades share it.
func RealPath ¶
RealPath resolves symlinks on the existing prefix of p and re-appends the non-existent tail. When nothing resolves it falls back to the lexical clean, so pure-logical (non-existent) paths map to themselves.
func StageUpgradeFile ¶
func StageUpgradeFile(source, target, upgradeID string, size int64, wantSHA256 string) (string, error)
Stage copies a verified candidate into the executable's directory, so the final rename stays on one volume. The destination is unique to upgradeID.
func SwitchBinary ¶
Switch parks the old image beside target, installs a same-volume candidate, and returns a rollback closure. The old image remains until caller cleanup.
Types ¶
This section is empty.