Pipeline runs use the workspace's directory environment

Resolve the run workspace's environment with workenv (direnv, parent
.envrc, mise; same account rule as Claude chats) at run start and resume,
and apply it to Pi stage processes, a per-environment Pipelines-owned
OpenCode stage service (keyed by a hash of the change, reused across runs,
idle ones stopped after PIPELINES_OPENCODE_STAGE_IDLE_TIMEOUT, revived per
registration), delivery-gate checks and merges, plain-stage opencode CLI
calls and file-change capture git reads. Record the directory, .envrc
files, sources and variable names (never values) on the run detail; a
blocked .envrc becomes a run warning.
This commit is contained in:
2026-11-04 18:08:42 +00:00
parent e0d7b720f5
commit cedfc22e45
26 changed files with 1173 additions and 84 deletions
+1 -1
View File
@@ -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
}
}
+151
View File
@@ -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 "<unset>"
}
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") != "<unset>" {
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") != "<unset>" {
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)
}
}
}
+7 -1
View File
@@ -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
+219 -30
View File
@@ -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-<key>.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
+15 -3
View File
@@ -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