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), } }