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:
@@ -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
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
}
|
||||
@@ -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)))
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user