From cedfc22e454cafc27ffdd8e7f63ec39a040c35ef Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Wed, 4 Nov 2026 18:08:42 +0000 Subject: [PATCH] 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. --- cmd/pipelines-server/main.go | 3 + docs/harnesses.md | 35 +++ internal/claude/auth_env.go | 39 +-- internal/filediff/snapshot.go | 21 +- internal/opencode/connection.go | 2 +- internal/opencode/stage_env_test.go | 151 +++++++++++ internal/opencode/stage_limits.go | 8 +- internal/opencode/stage_owned.go | 249 ++++++++++++++++--- internal/opencode/stage_owned_test.go | 18 +- internal/pipeline/runs.go | 15 +- internal/pipeline/runs_mutate.go | 13 + internal/pipeline/runs_schema.go | 3 + internal/protocol/runs.go | 22 ++ internal/server/delivery_gate.go | 5 + internal/server/run_environment.go | 53 ++++ internal/server/run_environment_live_test.go | 61 +++++ internal/server/run_environment_test.go | 116 +++++++++ internal/server/runner.go | 7 + internal/stage/opencode.go | 8 +- internal/stage/pi.go | 14 +- internal/stage/pi_env_test.go | 47 ++++ internal/stage/pi_test.go | 3 + internal/workenv/environment.go | 153 ++++++++++++ internal/workenv/environment_test.go | 115 +++++++++ internal/workenv/workenv.go | 25 +- internal/workenv/workenvtest/fake.go | 71 ++++++ 26 files changed, 1173 insertions(+), 84 deletions(-) create mode 100644 internal/opencode/stage_env_test.go create mode 100644 internal/server/run_environment.go create mode 100644 internal/server/run_environment_live_test.go create mode 100644 internal/server/run_environment_test.go create mode 100644 internal/stage/pi_env_test.go create mode 100644 internal/workenv/environment.go create mode 100644 internal/workenv/environment_test.go create mode 100644 internal/workenv/workenvtest/fake.go diff --git a/cmd/pipelines-server/main.go b/cmd/pipelines-server/main.go index e2a676a1..1632e826 100644 --- a/cmd/pipelines-server/main.go +++ b/cmd/pipelines-server/main.go @@ -164,6 +164,9 @@ func main() { // service is stopped only when no stage session is still active on it. defer opencode.StopIdleStageService() defer server.WaitRuns(srv.Handler) + // Per-environment stage services (a workspace with its own .envrc) stop + // once idle for PIPELINES_OPENCODE_STAGE_IDLE_TIMEOUT. + go opencode.RunStageServiceReaper(ctx) go func() { select { case <-signals.Done(): diff --git a/docs/harnesses.md b/docs/harnesses.md index 92f1c81c..7983f38e 100644 --- a/docs/harnesses.md +++ b/docs/harnesses.md @@ -287,6 +287,41 @@ Codex and OpenCode chats share one App Server or `opencode serve` process across directories, so only the working directory applies to them; their account is the one that process uses. +**Pipeline runs.** A run resolves its workspace's environment the same way +(direnv in the workspace, so a parent `.envrc` applies to a worktree below it; +mise; the same account rule) when it starts, and again when it resumes after a +decision or a restart. Everything the server starts for the run gets it: + +- Pi stage processes (`PWD` is the workspace). +- OpenCode stages: the server runs one Pipelines-owned stage service per + distinct environment (keyed by a hash of the change, registration + `opencode-stage-service-env-.json` beside the original + `opencode-stage-service.json`), started with that environment, so the + agent's shell commands, `gcloud`, and any `claude` it launches use the + directory's accounts. Runs with the same environment share the service; a + workspace without its own environment keeps the original service. An idle + per-environment service is stopped after + `PIPELINES_OPENCODE_STAGE_IDLE_TIMEOUT` (default `30m`, `off` keeps it until + shutdown); its registration stays, so a retained session revives on the same + endpoint with the directory's current environment. With + `PIPELINES_OPENCODE_STAGE_SERVICE=shared` stages run on the CLI-managed + service and the directory environment does not reach their tools. +- Delivery-gate checks (`sh -c`) and the gate's merge commands, the + `opencode` CLI calls of plain model stages, and the git reads behind + file-change captures. + +When the directory chooses the Claude account (for example its `.envrc` sets +`CLAUDE_CONFIG_DIR`), inherited `CLAUDE_CODE_OAUTH_TOKEN`, `ANTHROPIC_API_KEY`, +`ANTHROPIC_AUTH_TOKEN` and `CLAUDE_SECURESTORAGE_CONFIG_DIR` are removed for all +of these, as for chats. + +The run detail (`pipelines inspect --json`, field `environment`) records +what applied: the directory, the `.envrc` files, the sources, the names of the +variables set and unset, the dropped credential names, and the service key, +never a value. A blocked `.envrc` or untrusted mise config is listed in the +run's `warnings` (and printed by `pipelines inspect`); the run continues with +the server's environment. + ## Adopting existing sessions ```sh diff --git a/internal/claude/auth_env.go b/internal/claude/auth_env.go index 5a7a63f7..af845294 100644 --- a/internal/claude/auth_env.go +++ b/internal/claude/auth_env.go @@ -6,7 +6,6 @@ import ( "log" "os" "os/exec" - "slices" "strings" "sync" "time" @@ -93,28 +92,11 @@ func SignedIn(ctx context.Context, binary string, env []string) (bool, string) { return false, "" } -// accountEnvKeys select which account Claude Code signs in with. When a chat -// directory's own environment (direnv, mise) sets or unsets any of them, that -// directory owns the account: the server's fallbacks (service token, login -// file) are not added and inherited credentials are dropped, exactly as a -// terminal opened in that directory would behave. -var accountEnvKeys = []string{ - "CLAUDE_CONFIG_DIR", "CLAUDE_CODE_OAUTH_TOKEN", "ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN", - secureStorageEnvKey, "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", secureStorageEnvKey} - -func directoryOwnsAccount(dir workenv.Result) bool { - for _, key := range accountEnvKeys { - if dir.Touches(key) { - return true - } - } - return false -} +// A chat directory whose own environment (direnv, mise) sets or unsets any of +// workenv.AccountEnvKeys owns the account: the server's fallbacks (service +// token, login file) are not added and inherited credentials are dropped, +// exactly as a terminal opened in that directory would behave. Pipeline stages +// apply the same rule through workenv.Environment. // processEnv is the environment for a CLI process started in directory: the // server's environment, then the directory's own environment, then the @@ -133,15 +115,8 @@ func (c *Chat) processEnvFrom(ctx context.Context, directory string, base []stri log.Printf("claude chat directory %s: %s", directory, warning) } var env []string - if directoryOwnsAccount(dir) { - kept := make([]string, 0, len(base)) - for _, item := range base { - key, _, _ := strings.Cut(item, "=") - if slices.Contains(credentialEnvKeys, key) && !dir.Touches(key) { - continue - } - kept = append(kept, item) - } + if dir.OwnsAccount() { + kept, _ := dir.WithoutInheritedCredentials(base) env = dir.Apply(kept) } else { env = c.auth.resolve(c.ctx, c.binary, withServiceToken(dir.Apply(base))) diff --git a/internal/filediff/snapshot.go b/internal/filediff/snapshot.go index 836be5fc..2cb7c261 100644 --- a/internal/filediff/snapshot.go +++ b/internal/filediff/snapshot.go @@ -21,6 +21,7 @@ import ( "unicode/utf8" "git.a-wan.net/alan/pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/workenv" "github.com/pmezard/go-difflib/difflib" ) @@ -109,10 +110,18 @@ type Snapshot struct { missing map[string]string } +// gitCommand runs git for directory with the run's directory environment +// (workenv.WithEnvironment) when ctx carries one, as the stage's own git sees. +func gitCommand(ctx context.Context, directory string, args ...string) *exec.Cmd { + cmd := exec.CommandContext(ctx, "git", args...) + cmd.Env = workenv.CommandEnv(ctx, directory) + return cmd +} + // readGitIndex returns the stage-0 index object id of every tracked path in // the run directory (paths relative to it, as ls-files prints them). func readGitIndex(ctx context.Context, directory string) (map[string]string, bool) { - cmd := exec.CommandContext(ctx, "git", "-c", "core.fsmonitor=false", "-C", directory, "ls-files", "--stage", "-z", "--", ".") + cmd := gitCommand(ctx, directory, "-c", "core.fsmonitor=false", "-C", directory, "ls-files", "--stage", "-z", "--", ".") out := &boundedOutput{limit: 8 << 20} cmd.Stdout = out if err := cmd.Run(); err != nil { @@ -137,7 +146,7 @@ func readBlobs(ctx context.Context, directory string, ids []string) map[string][ if len(ids) == 0 { return result } - cmd := exec.CommandContext(ctx, "git", "-c", "core.fsmonitor=false", "-C", directory, "cat-file", "--batch") + cmd := gitCommand(ctx, directory, "-c", "core.fsmonitor=false", "-C", directory, "cat-file", "--batch") cmd.Stdin = strings.NewReader(strings.Join(ids, "\n") + "\n") pipe, err := cmd.StdoutPipe() if err != nil { @@ -181,12 +190,12 @@ func readBlobs(ctx context.Context, directory string, ids []string) map[string][ // returns its paths relative to that directory. It only reads: optional locks // are disabled so the index is never refreshed or rewritten. func readGitStatus(ctx context.Context, directory string) (map[string]string, bool) { - prefixOut, err := exec.CommandContext(ctx, "git", "-c", "core.fsmonitor=false", "-C", directory, "rev-parse", "--show-prefix").Output() + prefixOut, err := gitCommand(ctx, directory, "-c", "core.fsmonitor=false", "-C", directory, "rev-parse", "--show-prefix").Output() if err != nil { return nil, false } prefix := strings.TrimSpace(string(prefixOut)) - cmd := exec.CommandContext(ctx, "git", "--no-optional-locks", "-c", "core.fsmonitor=false", "-C", directory, "status", "--porcelain=v1", "-z", "--no-renames", "--untracked-files=all", "--", ".") + cmd := gitCommand(ctx, directory, "--no-optional-locks", "-c", "core.fsmonitor=false", "-C", directory, "status", "--porcelain=v1", "-z", "--no-renames", "--untracked-files=all", "--", ".") out := &boundedOutput{limit: 4 << 20} cmd.Stdout = out if err := cmd.Run(); err != nil { @@ -255,9 +264,9 @@ func (b *boundedOutput) Write(p []byte) (int, error) { // inventory lists the files to snapshot. Git is only read, never altered; // the baseline is still stage-start content (see Capture and recoverBaselines). func inventory(ctx context.Context, directory string, root *os.Root) ([]string, bool, error) { - probe := exec.CommandContext(ctx, "git", "-c", "core.fsmonitor=false", "-C", directory, "rev-parse", "--is-inside-work-tree") + probe := gitCommand(ctx, directory, "-c", "core.fsmonitor=false", "-C", directory, "rev-parse", "--is-inside-work-tree") if data, err := probe.Output(); err == nil && strings.TrimSpace(string(data)) == "true" { - cmd := exec.CommandContext(ctx, "git", "-c", "core.fsmonitor=false", "-C", directory, "ls-files", "--cached", "--others", "--exclude-standard", "--deduplicate", "-z", "--", ".") + cmd := gitCommand(ctx, directory, "-c", "core.fsmonitor=false", "-C", directory, "ls-files", "--cached", "--others", "--exclude-standard", "--deduplicate", "-z", "--", ".") out := &boundedOutput{limit: 2 << 20} cmd.Stdout = out if err := cmd.Run(); err != nil { diff --git a/internal/opencode/connection.go b/internal/opencode/connection.go index 7745f046..b6b71930 100644 --- a/internal/opencode/connection.go +++ b/internal/opencode/connection.go @@ -159,7 +159,7 @@ func FromReference(path string) (*Connection, error) { // A retained reference to the owned stage service revives it on its // recorded URL and password if it exited; it never selects another one. if isOwnedStageRegistration(reference.CredentialFile) { - if err := reviveOwnedStageService(reference.Directory); err != nil { + if err := reviveOwnedStageService(reference.CredentialFile, reference.Directory); err != nil { return nil, err } } diff --git a/internal/opencode/stage_env_test.go b/internal/opencode/stage_env_test.go new file mode 100644 index 00000000..62a326a2 --- /dev/null +++ b/internal/opencode/stage_env_test.go @@ -0,0 +1,151 @@ +package opencode + +import ( + "context" + "encoding/json" + "os" + "path/filepath" + "syscall" + "testing" + "time" + + "git.a-wan.net/alan/pipelines/internal/workenv" + "git.a-wan.net/alan/pipelines/internal/workenv/workenvtest" +) + +// fakeServiceEnv reads the variables named in FAKE_ENV_KEYS from the fake +// serve process; a nil value is unset there. +func fakeServiceEnv(t *testing.T, c *Connection) map[string]*string { + t.Helper() + data, err := c.nativeRequest(context.Background(), "get", "/api/env", nil, "") + if err != nil { + t.Fatal(err) + } + values := map[string]*string{} + if err := json.Unmarshal(data, &values); err != nil { + t.Fatal(err) + } + return values +} + +func value(values map[string]*string, key string) string { + if v := values[key]; v != nil { + return *v + } + return "" +} + +func TestOwnedStageServicePerDirectoryEnvironment(t *testing.T) { + executable := fakeOwnedStageEnv(t) + t.Setenv("PIPELINES_OPENCODE", executable) + workenvtest.InstallDirenv(t) + t.Setenv("FAKE_ENV_KEYS", "CLAUDE_CONFIG_DIR,SCRATCH_VAR,ANTHROPIC_API_KEY") + t.Setenv("ANTHROPIC_API_KEY", "server-key") + resolver := &workenv.Resolver{Timeout: 10 * time.Second} + + projectA := t.TempDir() + workspaceA := filepath.Join(projectA, "repos", "worktrees", "a") + if err := os.MkdirAll(workspaceA, 0o755); err != nil { + t.Fatal(err) + } + profile := filepath.Join(projectA, ".claude-profile") + workenvtest.WriteEnvrc(t, projectA, true, map[string]*string{"CLAUDE_CONFIG_DIR": workenvtest.Str(profile), "SCRATCH_VAR": workenvtest.Str("a")}) + projectB := t.TempDir() + workenvtest.WriteEnvrc(t, projectB, true, map[string]*string{"SCRATCH_VAR": workenvtest.Str("b")}) + + open := func(directory string) *Connection { + t.Helper() + env := resolver.Environment(context.Background(), directory, os.Environ()) + c, err := OpenStage(workenv.WithEnvironment(context.Background(), env), executable, directory, "v2") + if err != nil { + t.Fatal(err) + } + return c + } + + // A workspace under the project gets a service started with the parent + // .envrc, without the server's inherited credentials. + a := open(workspaceA) + defaultPath, _ := StageServiceRegistrationPath() + if a.credentialFile == defaultPath || !isOwnedStageRegistration(a.credentialFile) { + t.Fatalf("environment service registration = %s", a.credentialFile) + } + envA := fakeServiceEnv(t, a) + if value(envA, "CLAUDE_CONFIG_DIR") != profile || value(envA, "SCRATCH_VAR") != "a" || value(envA, "ANTHROPIC_API_KEY") != "" { + t.Fatalf("service environment = %v %v %v", value(envA, "CLAUDE_CONFIG_DIR"), value(envA, "SCRATCH_VAR"), value(envA, "ANTHROPIC_API_KEY")) + } + pidA := ownedPID(t, a) + + // The same environment from another subdirectory reuses the service. + if again := open(filepath.Join(projectA, "repos")); again.URL != a.URL || ownedPID(t, again) != pidA { + t.Fatal("same environment did not reuse its service") + } + + // A different environment gets its own service; no directory environment + // keeps the original one. + b := open(projectB) + if b.URL == a.URL || b.credentialFile == a.credentialFile { + t.Fatal("different environments share a service") + } + if envB := fakeServiceEnv(t, b); value(envB, "SCRATCH_VAR") != "b" || value(envB, "ANTHROPIC_API_KEY") != "server-key" { + t.Fatal("second environment not applied") + } + plain := open(t.TempDir()) + if plain.credentialFile != defaultPath || value(fakeServiceEnv(t, plain), "SCRATCH_VAR") != "" { + t.Fatal("a directory without its own environment left the original service") + } + + // A retained reference to a per-environment service revives it on the same + // endpoint with the directory's environment. + reference, err := a.Reference() + if err != nil { + t.Fatal(err) + } + _ = syscall.Kill(pidA, syscall.SIGKILL) + waitGone(t, pidA) + revived, err := FromReference(reference) + if err != nil { + t.Fatal(err) + } + if revived.URL != a.URL || value(fakeServiceEnv(t, revived), "SCRATCH_VAR") != "a" { + t.Fatal("per-environment service not revived with its environment") + } + pidA = ownedPID(t, revived) + + // Idle per-environment services stop after the timeout; a busy one and the + // original service keep running. + pidB, pidPlain := ownedPID(t, b), ownedPID(t, plain) + if err := os.WriteFile(os.Getenv("FAKE_BUSY_FILE"), nil, 0600); err != nil { + t.Fatal(err) + } + time.Sleep(20 * time.Millisecond) + if ReapIdleStageServices(10*time.Millisecond) != 0 { + t.Fatal("a busy service was reaped") + } + _ = os.Remove(os.Getenv("FAKE_BUSY_FILE")) + if ReapIdleStageServices(time.Hour) != 0 { + t.Fatal("a recently used service was reaped") + } + time.Sleep(20 * time.Millisecond) // busy counted as use; let the idle clock pass + if n := ReapIdleStageServices(10 * time.Millisecond); n != 2 { + t.Fatalf("reaped %d services, want 2", n) + } + waitGone(t, pidA) + waitGone(t, pidB) + if ownedPID(t, plain) != pidPlain { + t.Fatal("the original stage service was reaped") + } + // The registration stays, so a reaped service's reference revives. + if _, err := FromReference(reference); err != nil { + t.Fatalf("reaped service reference: %v", err) + } +} + +func TestStageIdleTimeoutSetting(t *testing.T) { + for raw, want := range map[string]time.Duration{"": defaultStageIdleTimeout, "off": 0, "0": 0, "5m": 5 * time.Minute, "nonsense": defaultStageIdleTimeout} { + t.Setenv(StageIdleTimeoutEnvKey, raw) + if got := StageIdleTimeout(); got != want { + t.Fatalf("%q: %s, want %s", raw, got, want) + } + } +} diff --git a/internal/opencode/stage_limits.go b/internal/opencode/stage_limits.go index c3f0bf63..dd8f1dcc 100644 --- a/internal/opencode/stage_limits.go +++ b/internal/opencode/stage_limits.go @@ -147,8 +147,14 @@ func mergeConfig(dst, src map[string]any) { // serviceEnvironment is the inherited environment without the variables the // owned serve process must receive from Pipelines only. func serviceEnvironment(content string) []string { + return serviceEnvironmentFrom(os.Environ(), content) +} + +// serviceEnvironmentFrom is serviceEnvironment over base instead of the +// server's own environment (a run directory's resolved environment). +func serviceEnvironmentFrom(base []string, content string) []string { var env []string - for _, entry := range os.Environ() { + for _, entry := range base { if strings.HasPrefix(entry, "OPENCODE_SERVER_PASSWORD=") || strings.HasPrefix(entry, "OPENCODE_SERVER_USERNAME=") || (content != "" && strings.HasPrefix(entry, "OPENCODE_CONFIG_CONTENT=")) { continue diff --git a/internal/opencode/stage_owned.go b/internal/opencode/stage_owned.go index a8dbc7b5..ac0aa4a4 100644 --- a/internal/opencode/stage_owned.go +++ b/internal/opencode/stage_owned.go @@ -18,6 +18,8 @@ import ( "sync" "syscall" "time" + + "git.a-wan.net/alan/pipelines/internal/workenv" ) // The owned stage service is one Pipelines-owned V2 `opencode serve` process, @@ -34,6 +36,16 @@ import ( // session export reach the same URL. A dead service is revived on the same port // and password before use. The server stops it on shutdown only when no // session is active on it. +// +// A run whose workspace has its own environment (direnv/mise, see workenv) +// gets its own owned stage service started with that environment, so the +// agent's shell commands, CLIs and any `claude` it starts behave as in a +// terminal opened there. Each distinct environment has its own registration +// (opencode-stage-service-env-.json, key = hash of the environment +// change); the limits stay part of each service's identity as before. Runs +// with the same environment share a service, and an idle per-environment +// service is stopped after PIPELINES_OPENCODE_STAGE_IDLE_TIMEOUT. A run whose +// directory changes nothing uses the original opencode-stage-service.json. // ownedStageStartTimeout bounds one start attempt; attempts retry a few times // because a concurrent OpenCode migration can briefly lock the database. @@ -43,7 +55,40 @@ const ownedStageStartAttempts = 3 // ownedStageProbeTimeout bounds the health check of an existing service. const ownedStageProbeTimeout = 3 * time.Second -var ownedStageMu sync.Mutex +// ownedStageLocks serializes start/stop per registration file within this +// process (the file lock covers other processes); services for different +// environments start independently. +var ( + ownedStageLocksMu sync.Mutex + ownedStageLocks = map[string]*sync.Mutex{} + ownedStageUsed = map[string]time.Time{} +) + +func ownedStageLock(path string) *sync.Mutex { + ownedStageLocksMu.Lock() + defer ownedStageLocksMu.Unlock() + lock := ownedStageLocks[path] + if lock == nil { + lock = &sync.Mutex{} + ownedStageLocks[path] = lock + } + return lock +} + +func markOwnedStageUsed(path string) { + ownedStageLocksMu.Lock() + ownedStageUsed[path] = time.Now() + ownedStageLocksMu.Unlock() +} + +// StageIdleTimeoutEnvKey bounds how long an idle per-environment stage service +// keeps running ("off" or 0 keeps them until shutdown). Default 30 minutes. +const StageIdleTimeoutEnvKey = "PIPELINES_OPENCODE_STAGE_IDLE_TIMEOUT" + +const defaultStageIdleTimeout = 30 * time.Minute + +const stageServiceFile = "opencode-stage-service" +const stageEnvServicePrefix = stageServiceFile + "-env-" type ownedStageState struct { URL string `json:"url"` @@ -62,10 +107,41 @@ type ownedService struct { } var ( - stageOwnedService = ownedService{name: "stage", file: "opencode-stage-service"} + stageOwnedService = ownedService{name: "stage", file: stageServiceFile} chatOwnedService = ownedService{name: "chat", file: "opencode-chat-service"} ) +// stageServiceFor names the owned stage service for an environment key; the +// empty key (no directory environment) is the original stage service. +func stageServiceFor(key string) ownedService { + if key == "" { + return stageOwnedService + } + return ownedService{name: "stage", file: stageEnvServicePrefix + key} +} + +// stageServiceAt is the owned stage service a registration path names. +func stageServiceAt(path string) ownedService { + return ownedService{name: "stage", file: strings.TrimSuffix(filepath.Base(path), ".json")} +} + +// StageServiceRegistrationPathFor is the registration of the owned stage +// service for an environment key (see workenv.Environment.Key). +func StageServiceRegistrationPathFor(key string) (string, error) { + return stageServiceFor(key).registrationPath() +} + +// stageServiceRegistrations lists every owned stage service registration. +func stageServiceRegistrations() []string { + path, err := StageServiceRegistrationPath() + if err != nil { + return nil + } + paths := []string{path} + matches, _ := filepath.Glob(filepath.Join(filepath.Dir(path), stageEnvServicePrefix+"*.json")) + return append(paths, matches...) +} + func (s ownedService) registrationPath() (string, error) { cache, err := os.UserCacheDir() if err != nil { @@ -103,18 +179,32 @@ func EnsureOwnedChatService(ctx context.Context, executable, directory string) ( return nil, err } } - return ensureOwnedService(ctx, chatOwnedService, content, executable, directory) + return ensureOwnedService(ctx, chatOwnedService, content, executable, directory, nil, "") } func isOwnedStageRegistration(path string) bool { owned, err := StageServiceRegistrationPath() - return err == nil && filepath.Clean(path) == filepath.Clean(owned) + if err != nil { + return false + } + path = filepath.Clean(path) + if path == filepath.Clean(owned) { + return true + } + name := filepath.Base(path) + return filepath.Dir(path) == filepath.Dir(filepath.Clean(owned)) && strings.HasPrefix(name, stageEnvServicePrefix) && strings.HasSuffix(name, ".json") } // ensureOwnedStageService returns a connection to the healthy owned stage -// service, starting or reviving it when needed. It fails with a clear error and -// never falls back to another service: the stage limits are a guarantee. +// service for the run environment ctx carries (workenv.WithEnvironment), +// starting or reviving it when needed. It fails with a clear error and never +// falls back to another service: the stage limits are a guarantee. func ensureOwnedStageService(ctx context.Context, executable, directory string) (*Connection, error) { + env, _ := workenv.FromContext(ctx) + return ensureOwnedStageAt(ctx, stageServiceFor(env.Key), env.Env, env.Key, executable, directory) +} + +func ensureOwnedStageAt(ctx context.Context, service ownedService, env []string, key, executable, directory string) (*Connection, error) { overrides, err := StageConfigOverrides() if err != nil { return nil, err @@ -123,10 +213,12 @@ func ensureOwnedStageService(ctx context.Context, executable, directory string) if err != nil { return nil, err } - return ensureOwnedService(ctx, stageOwnedService, content, executable, directory) + return ensureOwnedService(ctx, service, content, executable, directory, env, key) } -func ensureOwnedService(ctx context.Context, service ownedService, content, executable, directory string) (*Connection, error) { +// ensureOwnedService starts or reuses a service. env is the process +// environment (nil: the server's own) and envKey identifies it. +func ensureOwnedService(ctx context.Context, service ownedService, content, executable, directory string, env []string, envKey string) (*Connection, error) { path, err := service.registrationPath() if err != nil { return nil, err @@ -134,15 +226,20 @@ func ensureOwnedService(ctx context.Context, service ownedService, content, exec if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { return nil, err } - ownedStageMu.Lock() - defer ownedStageMu.Unlock() + lock := ownedStageLock(path) + lock.Lock() + defer lock.Unlock() unlock, err := lockFile(path + ".lock") if err != nil { return nil, fmt.Errorf("OpenCode %s service lock: %w", service.name, err) } defer unlock() + markOwnedStageUsed(path) identity := ownedStageIdentity(executable, content) + if envKey != "" { + identity = ownedStageIdentity(executable, content+"\x00env:"+envKey) + } state, readErr := readOwnedStageState(path) port := 0 if readErr == nil { @@ -181,7 +278,7 @@ func ensureOwnedService(ctx context.Context, service ownedService, content, exec case <-time.After(time.Duration(attempt) * time.Second): } } - started, err := startOwnedStage(ctx, service, executable, port, state.Password, content) + started, err := startOwnedStage(ctx, service, executable, port, state.Password, content, env) if err == nil { started.Identity = identity if err := writeOwnedStageState(path, started); err != nil { @@ -214,18 +311,26 @@ func ownedStageConnection(path, executable, directory string) (*Connection, erro // reviveOwnedStageService makes a retained reference to the owned service // usable again after the service exited, on the recorded URL and password. // A healthy service is used as is, so recovery never depends on the current -// limit settings being valid. -func reviveOwnedStageService(directory string) error { +// limit settings being valid. A per-environment service is revived with the +// stage directory's current environment (values are never stored on disk). +func reviveOwnedStageService(path, directory string) error { ctx, cancel := context.WithTimeout(context.Background(), ownedStageStartTimeout*ownedStageStartAttempts) defer cancel() - if path, err := StageServiceRegistrationPath(); err == nil { - if state, err := readOwnedStageState(path); err == nil { - if _, healthy := (&Connection{Backend: "v2", URL: state.URL, password: state.Password}).ownedInfo(ctx); healthy { - return nil - } + markOwnedStageUsed(path) + if state, err := readOwnedStageState(path); err == nil { + if _, healthy := (&Connection{Backend: "v2", URL: state.URL, password: state.Password}).ownedInfo(ctx); healthy { + return nil } } - _, err := ensureOwnedStageService(ctx, Executable(), directory) + service := stageServiceAt(path) + var env workenv.Environment + if service.file != stageServiceFile { + env = workenv.Default.Environment(ctx, directory, os.Environ()) + if stageServiceFor(env.Key) != service { + log.Printf("OpenCode stage service %s: the environment of %s changed since the stage started; reviving it with the current environment", service.file, directory) + } + } + _, err := ensureOwnedStageAt(ctx, service, env.Env, strings.TrimPrefix(service.file, stageEnvServicePrefix), Executable(), directory) return err } @@ -243,7 +348,7 @@ func ownedStageIdentity(executable, content string) string { // startOwnedStage starts one detached serve process and waits until it answers // the authenticated V2 info contract. -func startOwnedStage(ctx context.Context, service ownedService, executable string, port int, password, content string) (ownedStageState, error) { +func startOwnedStage(ctx context.Context, service ownedService, executable string, port int, password, content string, env []string) (ownedStageState, error) { if port == 0 { free, err := freePort() if err != nil { @@ -256,7 +361,10 @@ func startOwnedStage(ctx context.Context, service ownedService, executable strin if home, err := os.UserHomeDir(); err == nil { cmd.Dir = home } - cmd.Env = append(serviceEnvironment(content), "OPENCODE_SERVER_PASSWORD="+password, "OPENCODE_SERVER_USERNAME=opencode") + if env == nil { + env = os.Environ() + } + cmd.Env = append(serviceEnvironmentFrom(env, content), "OPENCODE_SERVER_PASSWORD="+password, "OPENCODE_SERVER_USERNAME=opencode") // Its own session: a stopping Pipelines service (and its process group) // does not take active stage work with it. cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true} @@ -338,28 +446,109 @@ func (c *Connection) ownedIdle(ctx context.Context) bool { // later start reuses the same endpoint and retained references stay valid. A // busy service keeps running so restart recovery can interrupt it properly. func StopIdleStageService() { - path, err := StageServiceRegistrationPath() - if err != nil { - return + for _, path := range stageServiceRegistrations() { + stopIdleOwnedStage(path, 0) + } +} + +// stopIdleOwnedStage stops the service at path when it has no active session +// and was not used by this process within unusedFor. It reports whether it +// stopped one. +func stopIdleOwnedStage(path string, unusedFor time.Duration) bool { + lock := ownedStageLock(path) + lock.Lock() + defer lock.Unlock() + if unusedFor > 0 { + ownedStageLocksMu.Lock() + used, seen := ownedStageUsed[path] + if !seen { + // A service this process has not used yet (for example one that + // survived a restart) starts its idle clock now. + ownedStageUsed[path] = time.Now() + } + ownedStageLocksMu.Unlock() + if !seen || time.Since(used) < unusedFor { + return false + } } - ownedStageMu.Lock() - defer ownedStageMu.Unlock() state, err := readOwnedStageState(path) if err != nil { - return + return false } ctx, cancel := context.WithTimeout(context.Background(), 2*ownedStageProbeTimeout) defer cancel() conn := &Connection{Backend: "v2", URL: state.URL, password: state.Password} pid, healthy := conn.ownedInfo(ctx) if !healthy { - return + return false } if !conn.ownedIdle(ctx) { - log.Printf("OpenCode stage service has active sessions; leaving it running") - return + // Busy counts as use: the idle clock starts when its work ends. + markOwnedStageUsed(path) + if unusedFor == 0 { + log.Printf("OpenCode stage service %s has active sessions; leaving it running", filepath.Base(path)) + } + return false } stopOwnedPID(pid) + return true +} + +// StageIdleTimeout is the configured idle timeout for per-environment stage +// services; zero disables stopping them before shutdown. +func StageIdleTimeout() time.Duration { + raw := strings.TrimSpace(os.Getenv(StageIdleTimeoutEnvKey)) + switch strings.ToLower(raw) { + case "": + return defaultStageIdleTimeout + case "off", "0": + return 0 + } + value, err := time.ParseDuration(raw) + if err != nil || value < 0 { + log.Printf("%s must be a duration such as 30m or off, not %q; using %s", StageIdleTimeoutEnvKey, raw, defaultStageIdleTimeout) + return defaultStageIdleTimeout + } + return value +} + +// ReapIdleStageServices stops every per-environment stage service that has no +// active session and was not used within timeout. The original stage service +// (no directory environment) keeps its shutdown-only lifecycle. Registrations +// are kept, so a retained reference revives on the same endpoint. +func ReapIdleStageServices(timeout time.Duration) int { + if timeout <= 0 { + return 0 + } + stopped := 0 + for _, path := range stageServiceRegistrations() { + if strings.HasPrefix(filepath.Base(path), stageEnvServicePrefix) && stopIdleOwnedStage(path, timeout) { + log.Printf("OpenCode stage service %s was idle for %s; stopped it", filepath.Base(path), timeout) + stopped++ + } + } + return stopped +} + +// RunStageServiceReaper calls ReapIdleStageServices periodically until ctx +// ends. +func RunStageServiceReaper(ctx context.Context) { + timeout := StageIdleTimeout() + if timeout <= 0 { + return + } + interval := min(timeout/4, time.Minute) + interval = max(interval, time.Second) + ticker := time.NewTicker(interval) + defer ticker.Stop() + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + ReapIdleStageServices(timeout) + } + } } // stopOwnedPID terminates a process identified through the authenticated info diff --git a/internal/opencode/stage_owned_test.go b/internal/opencode/stage_owned_test.go index 8e46cfcc..e3e368ce 100644 --- a/internal/opencode/stage_owned_test.go +++ b/internal/opencode/stage_owned_test.go @@ -63,6 +63,17 @@ func runFakeOpenCodeServe() { } _, _ = w.Write([]byte(`{"data":{}}`)) })) + mux.HandleFunc("/api/env", auth(func(w http.ResponseWriter) { + values := map[string]*string{} + for _, key := range strings.Split(os.Getenv("FAKE_ENV_KEYS"), ",") { + if value, ok := os.LookupEnv(key); ok { + values[key] = &value + } else { + values[key] = nil + } + } + _ = json.NewEncoder(w).Encode(values) + })) mux.HandleFunc("/api/config", auth(func(w http.ResponseWriter) { _ = json.NewEncoder(w).Encode(map[string]string{"content": os.Getenv("OPENCODE_CONFIG_CONTENT")}) })) @@ -85,9 +96,10 @@ func fakeOwnedStageEnv(t *testing.T) string { } t.Cleanup(StopIdleStageService) t.Cleanup(func() { - path, _ := StageServiceRegistrationPath() - if state, err := readOwnedStageState(path); err == nil { - _ = syscall.Kill(state.PID, syscall.SIGKILL) + for _, path := range stageServiceRegistrations() { + if state, err := readOwnedStageState(path); err == nil { + _ = syscall.Kill(state.PID, syscall.SIGKILL) + } } }) return executable diff --git a/internal/pipeline/runs.go b/internal/pipeline/runs.go index f631c5c0..87a08e20 100644 --- a/internal/pipeline/runs.go +++ b/internal/pipeline/runs.go @@ -160,7 +160,7 @@ func (s *Store) GetRun(ctx context.Context, id int64) (*protocol.RunDetail, erro } return nil, err } - var input, output, paths string + var input, output, paths, environment string var graphJSON string if err := tx.QueryRowContext(ctx, "SELECT graph_json FROM pipeline_versions WHERE pipeline_id=? AND version=?", r.PipelineID, r.Version).Scan(&graphJSON); err != nil { return nil, err @@ -168,9 +168,15 @@ func (s *Store) GetRun(ctx context.Context, id int64) (*protocol.RunDetail, erro if err := json.Unmarshal([]byte(graphJSON), &r.Graph); err != nil { return nil, err } - if err := tx.QueryRowContext(ctx, "SELECT workspace,allow_tools,write_paths,input_json,output_json,COALESCE((SELECT original_run_id FROM run_retries WHERE run_id=pipeline_runs.id),0),COALESCE((SELECT mode FROM retry_checkpoints WHERE run_id=pipeline_runs.id),CASE WHEN EXISTS(SELECT 1 FROM run_retries WHERE run_id=pipeline_runs.id) THEN 'restart' ELSE '' END),COALESCE((SELECT source_run_id FROM retry_checkpoints WHERE run_id=pipeline_runs.id),0),COALESCE((SELECT source_version FROM retry_checkpoints WHERE run_id=pipeline_runs.id),0),COALESCE((SELECT source_hash FROM retry_checkpoints WHERE run_id=pipeline_runs.id),''),title_source,title_model FROM pipeline_runs WHERE id=?", id).Scan(&r.Workspace, &r.AllowTools, &paths, &input, &output, &r.RetryOf, &r.RetryMode, &r.RetrySource, &r.RetrySourceVersion, &r.RetrySourceHash, &r.TitleSource, &r.TitleModel); err != nil { + if err := tx.QueryRowContext(ctx, "SELECT workspace,allow_tools,write_paths,input_json,output_json,COALESCE((SELECT original_run_id FROM run_retries WHERE run_id=pipeline_runs.id),0),COALESCE((SELECT mode FROM retry_checkpoints WHERE run_id=pipeline_runs.id),CASE WHEN EXISTS(SELECT 1 FROM run_retries WHERE run_id=pipeline_runs.id) THEN 'restart' ELSE '' END),COALESCE((SELECT source_run_id FROM retry_checkpoints WHERE run_id=pipeline_runs.id),0),COALESCE((SELECT source_version FROM retry_checkpoints WHERE run_id=pipeline_runs.id),0),COALESCE((SELECT source_hash FROM retry_checkpoints WHERE run_id=pipeline_runs.id),''),title_source,title_model,environment_json FROM pipeline_runs WHERE id=?", id).Scan(&r.Workspace, &r.AllowTools, &paths, &input, &output, &r.RetryOf, &r.RetryMode, &r.RetrySource, &r.RetrySourceVersion, &r.RetrySourceHash, &r.TitleSource, &r.TitleModel, &environment); err != nil { return nil, err } + if environment != "" { + r.Environment = &protocol.RunEnvironment{} + if err := json.Unmarshal([]byte(environment), r.Environment); err != nil { + return nil, err + } + } if err := decodeValues(input, &r.Input); err != nil { return nil, err } @@ -304,6 +310,11 @@ strftime('%Y-%m-%dT%H:%M:%SZ',started_at),COALESCE(strftime('%Y-%m-%dT%H:%M:%SZ' // query and no transcript parse. r.Usage = protocol.TotalUsage(r.Steps) r.Warnings = protocol.RunWarnings(r.Steps) + if r.Environment != nil { + for _, warning := range r.Environment.Warnings { + r.Warnings = append(r.Warnings, "Workspace environment: "+warning) + } + } alerts, err := tx.QueryContext(ctx, `SELECT sequence,event,channel,status,attempts,diagnostic,message_id FROM run_notifications WHERE run_id=? ORDER BY sequence,event,channel`, id) if err != nil { return nil, err diff --git a/internal/pipeline/runs_mutate.go b/internal/pipeline/runs_mutate.go index e3176632..80b8fb62 100644 --- a/internal/pipeline/runs_mutate.go +++ b/internal/pipeline/runs_mutate.go @@ -404,6 +404,19 @@ AND NOT EXISTS(SELECT 1 FROM run_steps WHERE run_id=pipeline_runs.id AND agent_a return tx.Commit() } +// SetRunEnvironment records the directory environment a run resolved (names +// and files only). A resumed run records its latest resolution. +func (s *Store) SetRunEnvironment(ctx context.Context, id int64, environment *protocol.RunEnvironment) error { + data := []byte{} + if environment != nil { + var err error + if data, err = json.Marshal(environment); err != nil { + return err + } + } + return affected(s.db.ExecContext(ctx, "UPDATE pipeline_runs SET environment_json=? WHERE id=?", string(data), id)) +} + func (s *Store) MarkCleanupUnconfirmed(ctx context.Context, id int64) error { return affected(s.db.ExecContext(ctx, `UPDATE pipeline_runs SET cleanup_unconfirmed=1, diagnostic='Agent cleanup was not confirmed; workspace remains reserved until successful restart recovery' diff --git a/internal/pipeline/runs_schema.go b/internal/pipeline/runs_schema.go index bb5dbe5a..de0ff3b7 100644 --- a/internal/pipeline/runs_schema.go +++ b/internal/pipeline/runs_schema.go @@ -76,6 +76,9 @@ CREATE TABLE IF NOT EXISTS retry_checkpoints ( if err := s.addColumnIfMissing("pipeline_runs", "notify_mode", "TEXT NOT NULL DEFAULT 'auto'"); err != nil { return err } + if err := s.addColumnIfMissing("pipeline_runs", "environment_json", "TEXT NOT NULL DEFAULT ''"); err != nil { + return err + } if err := s.dropResourceSchema(); err != nil { return err } diff --git a/internal/protocol/runs.go b/internal/protocol/runs.go index 78317c1e..1879f225 100644 --- a/internal/protocol/runs.go +++ b/internal/protocol/runs.go @@ -519,6 +519,28 @@ type RunDetail struct { // two apart and see which model was used. TitleSource string `json:"title_source,omitempty"` TitleModel string `json:"title_model,omitempty"` + // Environment records the workspace's own environment (direnv/mise) the + // run's stages, stage services and checks ran with: files and variable + // names only, never values. Absent for runs that predate it. + Environment *RunEnvironment `json:"environment,omitempty"` +} + +// RunEnvironment is the directory environment a run resolved for its +// workspace. It never carries a variable's value. +type RunEnvironment struct { + Directory string `json:"directory"` + Files []string `json:"files,omitempty"` + Sources []string `json:"sources,omitempty"` + Set []string `json:"set,omitempty"` + Unset []string `json:"unset,omitempty"` + // Dropped are inherited server credentials removed because the directory + // chooses the Claude account (for example it sets CLAUDE_CONFIG_DIR). + Dropped []string `json:"dropped,omitempty"` + Warnings []string `json:"warnings,omitempty"` + // ServiceKey names the OpenCode stage service environment; empty when the + // directory changes nothing. + ServiceKey string `json:"service_key,omitempty"` + ResolvedAt string `json:"resolved_at,omitempty"` } // RunNotificationReceipt records publisher attempts, not device reception. diff --git a/internal/server/delivery_gate.go b/internal/server/delivery_gate.go index 44c5b6b8..e3837a76 100644 --- a/internal/server/delivery_gate.go +++ b/internal/server/delivery_gate.go @@ -14,6 +14,7 @@ import ( "time" "git.a-wan.net/alan/pipelines/internal/stage" + "git.a-wan.net/alan/pipelines/internal/workenv" ) // verifyDelivery proves that the stage landed a complete, committed tree before @@ -204,6 +205,7 @@ func cleanupDeliveryCandidate(workspace, candidate string) { func boundedCommandOutput(ctx context.Context, directory string, args ...string) ([]byte, error) { cmd := exec.CommandContext(ctx, "git", append([]string{"-C", directory}, args...)...) + cmd.Env = workenv.CommandEnv(ctx, directory) var output boundedDeliveryOutput cmd.Stdout = &output cmd.Stderr = &output @@ -214,6 +216,9 @@ func boundedCommandOutput(ctx context.Context, directory string, args ...string) func runDeliveryCheck(ctx context.Context, directory, check string) error { cmd := exec.CommandContext(ctx, "sh", "-c", check) cmd.Dir = directory + // The run workspace's own environment (direnv/mise), as a terminal there + // would have it; nil inherits the server's. + cmd.Env = workenv.CommandEnv(ctx, directory) var output boundedDeliveryOutput cmd.Stdout = &output cmd.Stderr = &output diff --git a/internal/server/run_environment.go b/internal/server/run_environment.go new file mode 100644 index 00000000..02065638 --- /dev/null +++ b/internal/server/run_environment.go @@ -0,0 +1,53 @@ +package server + +import ( + "context" + "log" + "os" + "time" + + "git.a-wan.net/alan/pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/workenv" +) + +// withRunEnvironment resolves the workspace's own environment (direnv/mise, +// with the same rules as Claude chats, a parent .envrc included), records its +// files and variable names on the run, and returns ctx carrying it so stage +// processes, the OpenCode stage service, delivery checks and file captures +// start with it. A blocked .envrc becomes a run warning; nothing from it is +// applied. A fresh run, a resumed decision and a reattached run all resolve +// again, so an edited .envrc is seen at the next start. +func (r *runner) withRunEnvironment(ctx context.Context, id int64, workspace string) context.Context { + resolver := r.dirEnv + if resolver == nil { + resolver = workenv.Default + } + env := resolver.Environment(ctx, workspace, os.Environ()) + for _, warning := range env.Warnings { + log.Printf("run %d workspace %s: %s", id, workspace, warning) + } + if env.Applies() { + log.Printf("run %d workspace %s: applying its environment from %v (%d set, %d unset, %d inherited credentials dropped)", id, workspace, env.Sources, len(env.Set), len(env.Unset), len(env.Dropped)) + } + save, cancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second) + defer cancel() + if err := r.store.SetRunEnvironment(save, id, runEnvironmentRecord(env)); err != nil { + log.Printf("run %d: record workspace environment: %v", id, err) + } + return workenv.WithEnvironment(ctx, env) +} + +// runEnvironmentRecord keeps names and files only, never values. +func runEnvironmentRecord(env workenv.Environment) *protocol.RunEnvironment { + return &protocol.RunEnvironment{ + Directory: env.Directory, + Files: env.Files, + Sources: env.Sources, + Set: env.Set, + Unset: env.Unset, + Dropped: env.Dropped, + Warnings: env.Warnings, + ServiceKey: env.Key, + ResolvedAt: time.Now().UTC().Format(time.RFC3339), + } +} diff --git a/internal/server/run_environment_live_test.go b/internal/server/run_environment_live_test.go new file mode 100644 index 00000000..718f2811 --- /dev/null +++ b/internal/server/run_environment_live_test.go @@ -0,0 +1,61 @@ +package server + +import ( + "context" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + + "git.a-wan.net/alan/pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/stage" + "git.a-wan.net/alan/pipelines/internal/workenv" +) + +// TestLiveDirenvReachesStageShell is opt-in: point PIPELINES_LIVE_DIRENV_DIR at +// a scratch directory whose allowed .envrc exports SCRATCH_VAR. It uses the +// installed direnv; a stage shell in a subdirectory and a delivery check must +// both see SCRATCH_VAR. +func TestLiveDirenvReachesStageShell(t *testing.T) { + scratch := os.Getenv("PIPELINES_LIVE_DIRENV_DIR") + if scratch == "" { + t.Skip("set PIPELINES_LIVE_DIRENV_DIR to a scratch directory with an allowed .envrc exporting SCRATCH_VAR") + } + workspace := filepath.Join(scratch, "sub", "worktree") + if err := os.MkdirAll(workspace, 0o755); err != nil { + t.Fatal(err) + } + ctx, r, id := concurrentStore(t) + r.dirEnv = workenv.New() + var stageOutput string + r.execute = func(ctx context.Context, _ stage.Definition, _ stage.Values) (stage.Result, error) { + cmd := exec.CommandContext(ctx, "sh", "-c", "env | grep SCRATCH_VAR") + cmd.Dir = workspace + cmd.Env = workenv.CommandEnv(ctx, workspace) + out, err := cmd.Output() + stageOutput = string(out) + return stage.Result{Outcome: "success"}, err + } + run, err := r.start(protocol.Message{PipelineID: id, Version: 1, RequestKey: "live-direnv", Workspace: workspace, Input: map[string]any{"prompt": "task"}}) + if err != nil { + t.Fatal(err) + } + r.wg.Wait() + detail, err := r.store.GetRun(ctx, run.ID) + if err != nil { + t.Fatal(err) + } + t.Logf("stage shell: %s", strings.TrimSpace(stageOutput)) + t.Logf("recorded: files=%v sources=%v set=%v warnings=%v", detail.Environment.Files, detail.Environment.Sources, detail.Environment.Set, detail.Environment.Warnings) + if !strings.Contains(stageOutput, "SCRATCH_VAR=") || detail.Status != "succeeded" { + t.Fatalf("stage shell did not see SCRATCH_VAR (status %s, warnings %v)", detail.Status, detail.Warnings) + } + env := workenv.New().Environment(context.Background(), workspace, os.Environ()) + out := filepath.Join(t.TempDir(), "check.txt") + if err := runDeliveryCheck(workenv.WithEnvironment(context.Background(), env), t.TempDir(), "env | grep SCRATCH_VAR > "+out); err != nil { + t.Fatal(err) + } + data, _ := os.ReadFile(out) + t.Logf("delivery check: %s", strings.TrimSpace(string(data))) +} diff --git a/internal/server/run_environment_test.go b/internal/server/run_environment_test.go new file mode 100644 index 00000000..f2983462 --- /dev/null +++ b/internal/server/run_environment_test.go @@ -0,0 +1,116 @@ +package server + +import ( + "context" + "encoding/json" + "os" + "path/filepath" + "slices" + "strings" + "testing" + "time" + + "git.a-wan.net/alan/pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/stage" + "git.a-wan.net/alan/pipelines/internal/workenv" + "git.a-wan.net/alan/pipelines/internal/workenv/workenvtest" +) + +func envValue(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 +} + +// runInWorkspace runs the one-stage template in workspace and returns what the +// stage saw in its context and the stored run. +func runInWorkspace(t *testing.T, workspace string) (workenv.Environment, *protocol.RunDetail) { + t.Helper() + ctx, r, id := concurrentStore(t) + r.dirEnv = &workenv.Resolver{Timeout: 10 * time.Second} + var seen workenv.Environment + r.execute = func(ctx context.Context, def stage.Definition, _ stage.Values) (stage.Result, error) { + seen, _ = workenv.FromContext(ctx) + return stage.Result{Outcome: "success"}, nil + } + run, err := r.start(protocol.Message{PipelineID: id, Version: 1, RequestKey: "env-" + filepath.Base(workspace), Workspace: workspace, Input: map[string]any{"prompt": "task"}}) + if err != nil { + t.Fatal(err) + } + r.wg.Wait() + detail, err := r.store.GetRun(ctx, run.ID) + if err != nil { + t.Fatal(err) + } + return seen, detail +} + +func TestRunAppliesAndRecordsWorkspaceEnvironment(t *testing.T) { + workenvtest.InstallDirenv(t) + t.Setenv("ANTHROPIC_API_KEY", "server-key") + project, _ := filepath.EvalSymlinks(t.TempDir()) + workspace := filepath.Join(project, "repos", "worktrees", "item") + if err := os.MkdirAll(workspace, 0o755); err != nil { + t.Fatal(err) + } + secret := "value-that-must-not-be-recorded" + workenvtest.WriteEnvrc(t, project, true, map[string]*string{"CLAUDE_CONFIG_DIR": workenvtest.Str(filepath.Join(project, ".claude-profile")), "SCRATCH_VAR": workenvtest.Str(secret)}) + + seen, detail := runInWorkspace(t, workspace) + if v, _ := envValue(seen.Env, "SCRATCH_VAR"); v != secret { + t.Fatal("stage context did not carry the workspace environment") + } + if _, ok := envValue(seen.Env, "ANTHROPIC_API_KEY"); ok { + t.Fatal("inherited credential kept although the directory chooses the account") + } + recorded := detail.Environment + if recorded == nil || recorded.Directory != workspace || recorded.ServiceKey == "" || !slices.Equal(recorded.Set, []string{"CLAUDE_CONFIG_DIR", "SCRATCH_VAR"}) || + !slices.Contains(recorded.Dropped, "ANTHROPIC_API_KEY") || !slices.Equal(recorded.Files, []string{filepath.Join(project, ".envrc")}) || !slices.Equal(recorded.Sources, []string{"direnv"}) { + t.Fatalf("recorded environment = %+v", recorded) + } + data, _ := json.Marshal(detail) + if strings.Contains(string(data), secret) || strings.Contains(string(data), "server-key") { + t.Fatal("run detail carries an environment value") + } +} + +func TestRunWarnsAboutBlockedWorkspaceEnvrc(t *testing.T) { + workenvtest.InstallDirenv(t) + project, _ := filepath.EvalSymlinks(t.TempDir()) + workspace := filepath.Join(project, "item") + os.Mkdir(workspace, 0o755) + workenvtest.WriteEnvrc(t, project, false, map[string]*string{"SCRATCH_VAR": workenvtest.Str("x")}) + + seen, detail := runInWorkspace(t, workspace) + if seen.Applies() { + t.Fatal("blocked .envrc applied") + } + if detail.Status != "succeeded" { + t.Fatalf("status %s", detail.Status) + } + found := false + for _, warning := range detail.Warnings { + found = found || (strings.Contains(warning, "Workspace environment") && strings.Contains(warning, filepath.Join(project, ".envrc")) && strings.Contains(warning, "direnv allow")) + } + if !found || detail.Environment == nil || len(detail.Environment.Warnings) != 1 { + t.Fatalf("no blocked .envrc warning: %v", detail.Warnings) + } +} + +func TestDeliveryCheckSeesRunEnvironment(t *testing.T) { + workenvtest.InstallDirenv(t) + project := t.TempDir() + workenvtest.WriteEnvrc(t, project, true, map[string]*string{"SCRATCH_VAR": workenvtest.Str("from-envrc")}) + env := (&workenv.Resolver{Timeout: 10 * time.Second}).Environment(context.Background(), project, os.Environ()) + ctx := workenv.WithEnvironment(context.Background(), env) + candidate := t.TempDir() + if err := runDeliveryCheck(ctx, candidate, `test "$SCRATCH_VAR" = from-envrc && test "$PWD" = "`+candidate+`"`); err != nil { + t.Fatal(err) + } + if err := runDeliveryCheck(context.Background(), candidate, `test -z "$SCRATCH_VAR"`); err != nil { + t.Fatal("a check without a run environment saw it:", err) + } +} diff --git a/internal/server/runner.go b/internal/server/runner.go index 20464db2..d38c32b8 100644 --- a/internal/server/runner.go +++ b/internal/server/runner.go @@ -21,6 +21,7 @@ import ( "git.a-wan.net/alan/pipelines/internal/stage" "git.a-wan.net/alan/pipelines/internal/summarise" "git.a-wan.net/alan/pipelines/internal/turn" + "git.a-wan.net/alan/pipelines/internal/workenv" ) // activeRun records an in-flight or cleanup-unconfirmed run's cancellation seam @@ -39,6 +40,9 @@ type runner struct { mu sync.Mutex active map[int64]activeRun execute func(context.Context, stage.Definition, stage.Values) (stage.Result, error) + // dirEnv resolves the run workspace's own environment (direnv/mise) at + // run start; nil uses workenv.Default. + dirEnv *workenv.Resolver exportRepairMu sync.Mutex @@ -1573,6 +1577,9 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na finish("failed", "error", "stage returned an undeclared outcome") return } + // Every process started for this run (stages, the stage service, checks, + // captures) gets the workspace's own environment, as a terminal there would. + ctx = r.withRunEnvironment(ctx, id, workspace) // Resolve the effective provider/model for every model stage once, before the // loop, so applying an override is an in-memory O(1) map lookup per visit and // never re-parses the graph or re-resolves the whole definition. A run with no diff --git a/internal/stage/opencode.go b/internal/stage/opencode.go index 3aa964d0..7540c17e 100644 --- a/internal/stage/opencode.go +++ b/internal/stage/opencode.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "git.a-wan.net/alan/pipelines/internal/opencode" + "git.a-wan.net/alan/pipelines/internal/workenv" "io" "os" "os/exec" @@ -132,8 +133,13 @@ func (c OpenCodeClient) call(ctx context.Context, method, path string, body any) } cmd := opencode.CommandContext(ctx, opencode.Executable(), args...) cmd.Dir = directory + // The run's directory environment (workenv.WithEnvironment), if any. + cmd.Env = workenv.CommandEnv(ctx, directory) if c.ArtifactsDir != "" { - cmd.Env = append(os.Environ(), ArtifactsEnv+"="+c.ArtifactsDir) + if cmd.Env == nil { + cmd.Env = os.Environ() + } + cmd.Env = append(cmd.Env, ArtifactsEnv+"="+c.ArtifactsDir) } if c.connection != nil { c.connection.PinCLI(cmd) diff --git a/internal/stage/pi.go b/internal/stage/pi.go index c13b4353..039bfe84 100644 --- a/internal/stage/pi.go +++ b/internal/stage/pi.go @@ -16,6 +16,8 @@ import ( "strings" "syscall" "time" + + "git.a-wan.net/alan/pipelines/internal/workenv" ) //go:embed pi_policy.mjs @@ -165,7 +167,7 @@ func (c *PiClient) Generate(ctx context.Context, config ModelConfig, prompt stri c.cmd = exec.Command(c.Executable, args...) c.cmd.Dir = c.Directory c.cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} - c.cmd.Env = piEnvironment(c.Reference, c.ArtifactsDir) + c.cmd.Env = piEnvironmentFrom(workenv.CommandEnv(ctx, c.Directory), c.Reference, c.ArtifactsDir) stderr, err := os.OpenFile(filepath.Join(c.Reference, "stderr.log"), os.O_CREATE|os.O_WRONLY, 0600) if err != nil { return "", err @@ -382,7 +384,15 @@ func (c *PiClient) settleTurn(ctx context.Context) (string, error) { // never silently replaced by an ambient one (for example Pipelines' own // PIPELINES_ARTIFACTS_DIR when a stage runs inside another stage). func piEnvironment(reference, artifactsDir string) []string { - inherited := os.Environ() + return piEnvironmentFrom(nil, reference, artifactsDir) +} + +// piEnvironmentFrom builds the Pi environment over inherited (the run directory's +// environment), or over the server's own when inherited is nil. +func piEnvironmentFrom(inherited []string, reference, artifactsDir string) []string { + if inherited == nil { + inherited = os.Environ() + } env := make([]string, 0, len(inherited)+4) for _, entry := range inherited { if strings.HasPrefix(entry, ArtifactsEnv+"=") { diff --git a/internal/stage/pi_env_test.go b/internal/stage/pi_env_test.go new file mode 100644 index 00000000..b5de0ee3 --- /dev/null +++ b/internal/stage/pi_env_test.go @@ -0,0 +1,47 @@ +package stage + +import ( + "context" + "os" + "path/filepath" + "strings" + "testing" + "time" + + "git.a-wan.net/alan/pipelines/internal/workenv" +) + +// A Pi stage process starts with the run workspace's environment that the +// runner attached to the context, with PWD naming the workspace. +func TestPiStageProcessUsesRunEnvironment(t *testing.T) { + root := t.TempDir() + t.Setenv("XDG_CACHE_HOME", root) + t.Setenv("HOME", root) + t.Setenv("PIPELINES_PI_FIXTURE", "1") + t.Setenv("PI_FIXTURE_CASE", "success") + out := filepath.Join(root, "env.txt") + t.Setenv("PI_FIXTURE_ENV_OUT", out) + executable, err := os.Executable() + if err != nil { + t.Fatal(err) + } + wrapper := filepath.Join(root, "pi") + if err := os.WriteFile(wrapper, []byte("#!/bin/sh\nexec '"+executable+"' -test.run=^TestPiRPCFixture$\n"), 0700); err != nil { + t.Fatal(err) + } + workspace := t.TempDir() + env := workenv.Environment{Directory: workspace, Env: append(os.Environ(), "SCRATCH_VAR=from-envrc"), Key: "k"} + ctx, cancel := context.WithTimeout(workenv.WithEnvironment(context.Background(), env), 5*time.Second) + defer cancel() + client := &PiClient{Executable: wrapper, Directory: workspace, Tools: []string{}} + if _, err := client.Generate(ctx, ModelConfig{Provider: "fixture", Model: "model", Budget: NewBudget(0, 100)}, "task"); err != nil { + t.Fatal(err) + } + data, err := os.ReadFile(out) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(string(data), "SCRATCH_VAR=from-envrc\n") || !strings.Contains(string(data), "PWD="+workspace+"\n") { + t.Fatalf("Pi environment: %s", data) + } +} diff --git a/internal/stage/pi_test.go b/internal/stage/pi_test.go index c4a9ae3d..fd4a4b33 100644 --- a/internal/stage/pi_test.go +++ b/internal/stage/pi_test.go @@ -20,6 +20,9 @@ func TestPiRPCFixture(t *testing.T) { root := filepath.Dir(os.Getenv("PIPELINES_PI_POLICY")) sessionID := filepath.Base(root) _ = os.WriteFile(filepath.Join(root, "ready"), []byte("ready"), 0600) + if out := os.Getenv("PI_FIXTURE_ENV_OUT"); out != "" { + _ = os.WriteFile(out, []byte("SCRATCH_VAR="+os.Getenv("SCRATCH_VAR")+"\nPWD="+os.Getenv("PWD")+"\n"), 0600) + } message := map[string]any{"role": "assistant", "stopReason": "stop", "timestamp": 1, "content": []map[string]string{{"type": "text", "text": `# outcome success diff --git a/internal/workenv/environment.go b/internal/workenv/environment.go new file mode 100644 index 00000000..e4996c5b --- /dev/null +++ b/internal/workenv/environment.go @@ -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) +} diff --git a/internal/workenv/environment_test.go b/internal/workenv/environment_test.go new file mode 100644 index 00000000..28b057e7 --- /dev/null +++ b/internal/workenv/environment_test.go @@ -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) + } +} diff --git a/internal/workenv/workenv.go b/internal/workenv/workenv.go index fa5d1bf5..ba3ce752 100644 --- a/internal/workenv/workenv.go +++ b/internal/workenv/workenv.go @@ -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) { diff --git a/internal/workenv/workenvtest/fake.go b/internal/workenv/workenvtest/fake.go new file mode 100644 index 00000000..f06a57ae --- /dev/null +++ b/internal/workenv/workenvtest/fake.go @@ -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 }