cedfc22e45
Resolve the run workspace's environment with workenv (direnv, parent .envrc, mise; same account rule as Claude chats) at run start and resume, and apply it to Pi stage processes, a per-environment Pipelines-owned OpenCode stage service (keyed by a hash of the change, reused across runs, idle ones stopped after PIPELINES_OPENCODE_STAGE_IDLE_TIMEOUT, revived per registration), delivery-gate checks and merges, plain-stage opencode CLI calls and file-change capture git reads. Record the directory, .envrc files, sources and variable names (never values) on the run detail; a blocked .envrc becomes a run warning.
154 lines
4.8 KiB
Go
154 lines
4.8 KiB
Go
package workenv
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"os"
|
|
"slices"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
// AccountEnvKeys select which Claude account a process signs in with. When a
|
|
// directory's own environment sets or unsets any of them, that directory owns
|
|
// the account, exactly as a terminal opened there would behave.
|
|
var AccountEnvKeys = []string{
|
|
"CLAUDE_CONFIG_DIR", "CLAUDE_CODE_OAUTH_TOKEN", "ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN",
|
|
"CLAUDE_SECURESTORAGE_CONFIG_DIR", "CLAUDE_CODE_USE_BEDROCK", "CLAUDE_CODE_USE_VERTEX",
|
|
}
|
|
|
|
// CredentialEnvKeys are inherited server credentials a directory account must
|
|
// not be overridden by. An inherited CLAUDE_CONFIG_DIR is kept.
|
|
var CredentialEnvKeys = []string{"CLAUDE_CODE_OAUTH_TOKEN", "ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN", "CLAUDE_SECURESTORAGE_CONFIG_DIR"}
|
|
|
|
// OwnsAccount reports whether the directory chooses the Claude account.
|
|
func (r Result) OwnsAccount() bool {
|
|
for _, key := range AccountEnvKeys {
|
|
if r.Touches(key) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// WithoutInheritedCredentials returns base without the inherited credentials
|
|
// the directory does not set itself, and the names it removed.
|
|
func (r Result) WithoutInheritedCredentials(base []string) ([]string, []string) {
|
|
kept := make([]string, 0, len(base))
|
|
var dropped []string
|
|
for _, item := range base {
|
|
key, _, _ := strings.Cut(item, "=")
|
|
if slices.Contains(CredentialEnvKeys, key) && !r.Touches(key) {
|
|
if !slices.Contains(dropped, key) {
|
|
dropped = append(dropped, key)
|
|
}
|
|
continue
|
|
}
|
|
kept = append(kept, item)
|
|
}
|
|
sort.Strings(dropped)
|
|
return kept, dropped
|
|
}
|
|
|
|
// Environment is a directory's environment as applied to every process the
|
|
// server starts for work there (pipeline stages, their services and checks).
|
|
// It records variable names and files only; values live in Env alone.
|
|
type Environment struct {
|
|
Directory string
|
|
// Env is the complete environment: the server's, the directory's changes
|
|
// on top, and inherited credentials dropped when the directory owns the
|
|
// Claude account. Nil means the server's own environment, unchanged.
|
|
Env []string
|
|
// Key identifies the change (a hash of names, values and dropped names);
|
|
// empty when the directory changes nothing.
|
|
Key string
|
|
Files []string
|
|
Sources []string
|
|
Set []string
|
|
Unset []string
|
|
Dropped []string
|
|
Warnings []string
|
|
}
|
|
|
|
// Environment resolves directory against base (normally os.Environ()).
|
|
func (r *Resolver) Environment(ctx context.Context, directory string, base []string) Environment {
|
|
dir := r.Resolve(ctx, directory, base)
|
|
e := Environment{Directory: directory, Files: dir.Files, Sources: dir.Sources, Warnings: dir.Warnings}
|
|
if len(dir.Changes) == 0 {
|
|
return e
|
|
}
|
|
kept := base
|
|
if dir.OwnsAccount() {
|
|
kept, e.Dropped = dir.WithoutInheritedCredentials(base)
|
|
}
|
|
e.Env = dir.Apply(kept)
|
|
keys := make([]string, 0, len(dir.Changes))
|
|
for key := range dir.Changes {
|
|
keys = append(keys, key)
|
|
}
|
|
sort.Strings(keys)
|
|
hash := sha256.New()
|
|
for _, key := range keys {
|
|
hash.Write([]byte(key))
|
|
if value := dir.Changes[key]; value != nil {
|
|
e.Set = append(e.Set, key)
|
|
hash.Write([]byte{0})
|
|
hash.Write([]byte(*value))
|
|
} else {
|
|
e.Unset = append(e.Unset, key)
|
|
hash.Write([]byte{1})
|
|
}
|
|
hash.Write([]byte{'\n'})
|
|
}
|
|
for _, key := range e.Dropped {
|
|
hash.Write([]byte("-" + key + "\n"))
|
|
}
|
|
e.Key = hex.EncodeToString(hash.Sum(nil))[:16]
|
|
return e
|
|
}
|
|
|
|
// Applies reports whether the directory changes the server's environment.
|
|
func (e Environment) Applies() bool { return e.Env != nil }
|
|
|
|
// Command returns the environment for a command run in directory: nil (inherit
|
|
// the server's) when nothing applies, else Env with PWD set to directory.
|
|
func (e Environment) Command(directory string) []string {
|
|
if e.Env == nil {
|
|
return nil
|
|
}
|
|
return WithPWD(e.Env, directory)
|
|
}
|
|
|
|
// Base returns Env, or the server's environment when nothing applies.
|
|
func (e Environment) Base() []string {
|
|
if e.Env == nil {
|
|
return os.Environ()
|
|
}
|
|
return append([]string{}, e.Env...)
|
|
}
|
|
|
|
type environmentKey struct{}
|
|
|
|
// WithEnvironment carries a run's resolved environment to the stage code and
|
|
// server commands that start processes for it.
|
|
func WithEnvironment(ctx context.Context, e Environment) context.Context {
|
|
return context.WithValue(ctx, environmentKey{}, e)
|
|
}
|
|
|
|
// FromContext returns the environment WithEnvironment attached, if any.
|
|
func FromContext(ctx context.Context) (Environment, bool) {
|
|
if ctx == nil {
|
|
return Environment{}, false
|
|
}
|
|
e, ok := ctx.Value(environmentKey{}).(Environment)
|
|
return e, ok
|
|
}
|
|
|
|
// CommandEnv is the environment for a command run in directory under ctx: nil
|
|
// (inherit the server's) when no directory environment applies.
|
|
func CommandEnv(ctx context.Context, directory string) []string {
|
|
e, _ := FromContext(ctx)
|
|
return e.Command(directory)
|
|
}
|