b24fc31761
New internal/agentenv drops every PIPELINES_* variable naming a token, secret, password, auth file or key (except *_PUBLIC_KEY) from the environment of processes started for agents: Claude stages and chats (direct and hosted), Pi stages, OpenCode stage CLI calls and serve processes, check commands, delivery-gate checks, `!` shell lines, the run's workspace environment and turn-diff git snapshots. MCP servers are children of the harness and inherit its scrubbed environment. The interactive terminal is unchanged. An agent's `pipelines` CLI call falls back to the default server-token location. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
56 lines
2.1 KiB
Go
56 lines
2.1 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/agentenv"
|
|
"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
|
|
}
|
|
// Every process started from the run's environment (stages, checks, the
|
|
// OpenCode stage service) builds on the server's minus Pipelines' secrets.
|
|
env := resolver.Environment(ctx, workspace, agentenv.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),
|
|
}
|
|
}
|