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:
@@ -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)
|
||||
|
||||
+12
-2
@@ -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+"=") {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user