Pipeline runs use the workspace's directory environment

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.
This commit is contained in:
2026-11-04 18:08:42 +00:00
parent e0d7b720f5
commit cedfc22e45
26 changed files with 1173 additions and 84 deletions
+153
View File
@@ -0,0 +1,153 @@
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)
}
+115
View File
@@ -0,0 +1,115 @@
package workenv_test
import (
"context"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/workenv"
"git.a-wan.net/alan/pipelines/internal/workenv/workenvtest"
)
func lookup(env []string, key string) (string, bool) {
for i := len(env) - 1; i >= 0; i-- {
if k, v, ok := strings.Cut(env[i], "="); ok && k == key {
return v, true
}
}
return "", false
}
func TestEnvironmentSubdirectoryInheritsAndDropsCredentials(t *testing.T) {
workenvtest.InstallDirenv(t)
project := t.TempDir()
workspace := filepath.Join(project, "repos", "worktrees", "item")
if err := os.MkdirAll(workspace, 0o755); err != nil {
t.Fatal(err)
}
workenvtest.WriteEnvrc(t, project, true, map[string]*string{
"CLAUDE_CONFIG_DIR": workenvtest.Str(filepath.Join(project, ".claude-profile")),
"CLOUDSDK_CONFIG": workenvtest.Str(filepath.Join(project, ".gcloud-profile")),
"GONE": nil,
})
base := []string{"PATH=" + os.Getenv("PATH"), "HOME=" + t.TempDir(), "ANTHROPIC_API_KEY=server-key", "CLAUDE_CODE_OAUTH_TOKEN=server-token", "GONE=1", "KEEP=1"}
r := &workenv.Resolver{Timeout: 10 * time.Second}
got := r.Environment(context.Background(), workspace, base)
if !got.Applies() || got.Key == "" || len(got.Warnings) != 0 {
t.Fatalf("key %q warnings %v", got.Key, got.Warnings)
}
if v, _ := lookup(got.Env, "CLAUDE_CONFIG_DIR"); v != filepath.Join(project, ".claude-profile") {
t.Fatalf("parent .envrc not applied to the subdirectory: %q", v)
}
for _, key := range []string{"ANTHROPIC_API_KEY", "CLAUDE_CODE_OAUTH_TOKEN", "GONE"} {
if _, ok := lookup(got.Env, key); ok {
t.Fatalf("%s kept", key)
}
}
if v, _ := lookup(got.Env, "KEEP"); v != "1" {
t.Fatal("unrelated variable lost")
}
if !slices.Equal(got.Set, []string{"CLAUDE_CONFIG_DIR", "CLOUDSDK_CONFIG"}) || !slices.Equal(got.Unset, []string{"GONE"}) ||
!slices.Equal(got.Dropped, []string{"ANTHROPIC_API_KEY", "CLAUDE_CODE_OAUTH_TOKEN"}) {
t.Fatalf("names: set %v unset %v dropped %v", got.Set, got.Unset, got.Dropped)
}
if !slices.Equal(got.Files, []string{filepath.Join(project, ".envrc")}) {
t.Fatalf("files: %v", got.Files)
}
if v, _ := lookup(got.Command(workspace), "PWD"); v != workspace {
t.Fatalf("PWD = %q", v)
}
// The same environment from another subdirectory has the same key; a
// different value has another.
other := filepath.Join(project, "repos")
if again := r.Environment(context.Background(), other, base); again.Key != got.Key {
t.Fatal("same environment, different key")
}
second := t.TempDir()
workenvtest.WriteEnvrc(t, second, true, map[string]*string{"CLAUDE_CONFIG_DIR": workenvtest.Str("/elsewhere")})
if r.Environment(context.Background(), second, base).Key == got.Key {
t.Fatal("different environments share a key")
}
}
func TestEnvironmentWithoutAccountKeepsCredentialsAndNoneAppliesWithoutChanges(t *testing.T) {
workenvtest.InstallDirenv(t)
dir := t.TempDir()
workenvtest.WriteEnvrc(t, dir, true, map[string]*string{"SCRATCH_VAR": workenvtest.Str("1")})
base := []string{"PATH=" + os.Getenv("PATH"), "HOME=" + t.TempDir(), "ANTHROPIC_API_KEY=server-key"}
r := &workenv.Resolver{Timeout: 10 * time.Second}
got := r.Environment(context.Background(), dir, base)
if v, _ := lookup(got.Env, "ANTHROPIC_API_KEY"); v != "server-key" || len(got.Dropped) != 0 {
t.Fatal("credentials dropped without a directory account")
}
none := r.Environment(context.Background(), t.TempDir(), base)
if none.Applies() || none.Key != "" || none.Command("/x") != nil {
t.Fatalf("key %q", none.Key)
}
}
func TestEnvironmentBlockedEnvrcWarns(t *testing.T) {
workenvtest.InstallDirenv(t)
project := t.TempDir()
workspace := filepath.Join(project, "sub")
os.Mkdir(workspace, 0o755)
workenvtest.WriteEnvrc(t, project, false, map[string]*string{"CLAUDE_CONFIG_DIR": workenvtest.Str("/p")})
got := (&workenv.Resolver{Timeout: 10 * time.Second}).Environment(context.Background(), workspace, []string{"PATH=" + os.Getenv("PATH"), "HOME=" + t.TempDir()})
if got.Applies() || len(got.Warnings) != 1 || !strings.Contains(got.Warnings[0], "not allowed") || !strings.Contains(got.Warnings[0], filepath.Join(project, ".envrc")) {
t.Fatalf("key %q warnings %v", got.Key, got.Warnings)
}
}
func TestEnvironmentContext(t *testing.T) {
if workenv.CommandEnv(context.Background(), "/x") != nil {
t.Fatal("no environment should inherit")
}
ctx := workenv.WithEnvironment(context.Background(), workenv.Environment{Env: []string{"A=1"}, Key: "k"})
env := workenv.CommandEnv(ctx, "/x")
if !slices.Equal(env, []string{"A=1", "PWD=/x"}) {
t.Fatal(env)
}
}
+19 -6
View File
@@ -21,6 +21,7 @@ import (
"os/exec"
"path/filepath"
"regexp"
"slices"
"sort"
"strings"
"sync"
@@ -37,6 +38,8 @@ type Result struct {
Changes map[string]*string
// Sources names what contributed, for example "direnv" or "mise".
Sources []string
// Files are the .envrc (or dotenv) files direnv loaded, nearest first.
Files []string
// Warnings are operator-facing notices: a blocked .envrc, an untrusted
// mise config, or a tool that failed. Nothing from a warned source is applied.
Warnings []string
@@ -154,7 +157,7 @@ func (r *Resolver) Resolve(ctx context.Context, directory string, base []string)
}
}
if direnv != "" {
changes, watched, warning := r.runDirenv(ctx, direnv, directory, clean)
changes, watched, files, warning := r.runDirenv(ctx, direnv, directory, clean)
watches = watched
if warning != "" {
result.Warnings = append(result.Warnings, warning)
@@ -163,6 +166,7 @@ func (r *Resolver) Resolve(ctx context.Context, directory string, base []string)
result.Changes[key] = value
}
result.Sources = append(result.Sources, "direnv")
result.Files = files
}
}
// A cancelled lookup is not cached; the next start tries again.
@@ -233,30 +237,39 @@ func (r *Resolver) run(ctx context.Context, binary, directory string, env []stri
var blockedRC = regexp.MustCompile(`(\S+) is blocked`)
func (r *Resolver) runDirenv(ctx context.Context, binary, directory string, env []string) (map[string]*string, []string, string) {
func (r *Resolver) runDirenv(ctx context.Context, binary, directory string, env []string) (map[string]*string, []string, []string, string) {
stdout, stderr, err := r.run(ctx, binary, directory, env, "export", "json")
if match := blockedRC.FindStringSubmatch(stderr); match != nil {
return nil, nil, fmt.Sprintf("direnv: %s is not allowed, so its environment was not applied. Run `direnv allow` in %s to use it.", match[1], filepath.Dir(match[1]))
return nil, nil, nil, fmt.Sprintf("direnv: %s is not allowed, so its environment was not applied. Run `direnv allow` in %s to use it.", match[1], filepath.Dir(match[1]))
}
if err != nil {
return nil, nil, "direnv export failed in " + directory + ", so its environment was not applied: " + firstLine(stderr, err)
return nil, nil, nil, "direnv export failed in " + directory + ", so its environment was not applied: " + firstLine(stderr, err)
}
changes := map[string]*string{}
if text := bytes.TrimSpace(stdout); len(text) > 0 {
if err := json.Unmarshal(text, &changes); err != nil {
return nil, nil, "direnv export returned invalid output in " + directory + "; its environment was not applied"
return nil, nil, nil, "direnv export returned invalid output in " + directory + "; its environment was not applied"
}
}
var watches []string
if encoded := changes["DIRENV_WATCHES"]; encoded != nil {
watches = decodeWatches(*encoded)
}
var files []string
if file := changes["DIRENV_FILE"]; file != nil && *file != "" {
files = append(files, *file)
}
for _, path := range watches {
if name := filepath.Base(path); (name == ".envrc" || name == ".env") && !slices.Contains(files, path) {
files = append(files, path)
}
}
for key := range changes {
if strings.HasPrefix(key, "DIRENV_") {
delete(changes, key)
}
}
return changes, watches, ""
return changes, watches, files, ""
}
func (r *Resolver) runMise(ctx context.Context, binary, directory string, env []string) (map[string]*string, string) {
+71
View File
@@ -0,0 +1,71 @@
// Package workenvtest provides a fake direnv for tests of code that applies a
// directory's environment. It never touches the operator's real direnv state.
package workenvtest
import (
"encoding/json"
"os"
"path/filepath"
"testing"
)
// fakeDirenv behaves like `direnv export json` for the nearest .envrc from
// $PWD up: an .envrc next to an .envrc.allowed marker exports .envrc.json; any
// other .envrc is reported blocked, with direnv's wording.
const fakeDirenv = `#!/bin/sh
[ "$1 $2" = "export json" ] || exit 2
d="$PWD"
while :; do
if [ -f "$d/.envrc" ]; then
if [ ! -f "$d/.envrc.allowed" ]; then
echo "direnv: error $d/.envrc is blocked. Run ` + "`direnv allow`" + ` to approve its content" >&2
exit 1
fi
cat "$d/.envrc.json"
exit 0
fi
[ "$d" = "/" ] && exit 0
d=$(dirname "$d")
done
`
// InstallDirenv writes the fake direnv into a new directory, puts it first on
// the test's PATH and returns that directory.
func InstallDirenv(t testing.TB) string {
t.Helper()
bin := t.TempDir()
if err := os.WriteFile(filepath.Join(bin, "direnv"), []byte(fakeDirenv), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
return bin
}
// WriteEnvrc creates dir/.envrc exporting vars (a nil value unsets), allowed or
// blocked.
func WriteEnvrc(t testing.TB, dir string, allowed bool, vars map[string]*string) {
t.Helper()
file := filepath.Join(dir, ".envrc")
if err := os.WriteFile(file, []byte("# fake\n"), 0o644); err != nil {
t.Fatal(err)
}
out := map[string]*string{"DIRENV_FILE": &file}
for key, value := range vars {
out[key] = value
}
data, _ := json.Marshal(out)
if err := os.WriteFile(filepath.Join(dir, ".envrc.json"), data, 0o644); err != nil {
t.Fatal(err)
}
marker := filepath.Join(dir, ".envrc.allowed")
if allowed {
if err := os.WriteFile(marker, nil, 0o644); err != nil {
t.Fatal(err)
}
} else {
_ = os.Remove(marker)
}
}
// Str returns a pointer to value, for WriteEnvrc.
func Str(value string) *string { return &value }