Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

refactor(runtime): share sandbox env overlay and driver construction (#374)

fx-kurumic5c6d4a69d3f
diff --git a/internal/runtime/remote_sandbox.go b/internal/runtime/remote_sandbox.go
index 3d8e2a7a..4911e1c5 100644
--- a/internal/runtime/remote_sandbox.go
+++ b/internal/runtime/remote_sandbox.go
@@ -28,26 +28,8 @@ func OpenRemoteSandbox(ctx context.Context, id, workspace string, profile domain
if err != nil {
return nil, err
}
- if len(activated.Overlay) > 0 {
- path := activated.Overlay["PATH"]
- profile.Environment = mergeEnvironmentMaps(activated.Overlay, profile.Environment)
- if path != "" {
- profile.Environment["PATH"] = path
- }
- }
- var sandbox Sandbox
- switch strings.TrimSpace(profile.Driver) {
- case "", "local":
- sandbox = &localSandbox{id: id, workspace: workspace, environment: profile.Environment}
- case "seatbelt":
- sandbox, err = createSeatbelt(id, workspace, profile, requirements)
- case "bwrap":
- sandbox, err = createBwrap(id, workspace, profile, requirements)
- case "docker":
- sandbox, err = createDocker(ctx, id, workspace, profile, requirements, resources.Dockerfile)
- default:
- return nil, fmt.Errorf("unsupported runner sandbox driver %q", profile.Driver)
- }
+ profile.Environment = overlayEnvironment(profile.Environment, activated.Overlay)
+ sandbox, err := createDriverSandbox(ctx, "runner sandbox", id, workspace, profile, requirements, resources.Dockerfile)
if err != nil {
return nil, err
}
diff --git a/internal/runtime/sandbox_driver_ctor_test.go b/internal/runtime/sandbox_driver_ctor_test.go
new file mode 100644
index 00000000..4535d761
--- /dev/null
+++ b/internal/runtime/sandbox_driver_ctor_test.go
@@ -0,0 +1,74 @@
+package runtime
+
+import (
+ "context"
+ "reflect"
+ "strings"
+ "testing"
+
+ "agentflow/internal/domain"
+)
+
+func TestOverlayEnvironmentEmptyOverlayKeepsEnv(t *testing.T) {
+ env := map[string]string{"A": "1"}
+ got := overlayEnvironment(env, nil)
+ if !reflect.DeepEqual(got, env) {
+ t.Fatalf("overlayEnvironment(env, nil) = %v, want %v", got, env)
+ }
+ if got := overlayEnvironment(nil, map[string]string{}); got != nil {
+ t.Fatalf("empty overlay must keep nil env, got %v", got)
+ }
+}
+
+func TestOverlayEnvironmentProfileWinsExceptPath(t *testing.T) {
+ env := map[string]string{"PATH": "/profile/bin", "LANG": "profile", "ONLY_PROFILE": "p"}
+ overlay := map[string]string{"PATH": "/overlay/bin:/usr/bin", "LANG": "overlay", "ONLY_OVERLAY": "o"}
+ got := overlayEnvironment(env, overlay)
+ want := map[string]string{"PATH": "/overlay/bin:/usr/bin", "LANG": "profile", "ONLY_PROFILE": "p", "ONLY_OVERLAY": "o"}
+ if !reflect.DeepEqual(got, want) {
+ t.Fatalf("overlayEnvironment = %v, want %v", got, want)
+ }
+ if env["PATH"] != "/profile/bin" {
+ t.Fatalf("input env must not be mutated, got %v", env)
+ }
+}
+
+func TestOverlayEnvironmentEmptyOverlayPathKeepsProfilePath(t *testing.T) {
+ got := overlayEnvironment(map[string]string{"PATH": "/profile/bin"}, map[string]string{"PATH": "", "X": "1"})
+ if got["PATH"] != "/profile/bin" || got["X"] != "1" {
+ t.Fatalf("overlayEnvironment = %v", got)
+ }
+}
+
+func TestCreateDriverSandboxLocal(t *testing.T) {
+ workspace := t.TempDir()
+ for _, driver := range []string{"", "local", " local "} {
+ sb, err := createDriverSandbox(context.Background(), "sandbox", "id", workspace, domain.SandboxProfile{Driver: driver, Environment: map[string]string{"K": "V"}}, SandboxRequirements{}, domain.DockerfileDefinition{})
+ if err != nil {
+ t.Fatalf("driver %q: %v", driver, err)
+ }
+ local, ok := sb.(*localSandbox)
+ if !ok || local.id != "id" || local.workspace != workspace || local.environment["K"] != "V" {
+ t.Fatalf("driver %q: got %#v", driver, sb)
+ }
+ }
+}
+
+func TestCreateDriverSandboxUnknownDriverNamesCaller(t *testing.T) {
+ for kind, want := range map[string]string{
+ "sandbox": `unsupported sandbox driver "typo"`,
+ "runner sandbox": `unsupported runner sandbox driver "typo"`,
+ } {
+ sb, err := createDriverSandbox(context.Background(), kind, "id", t.TempDir(), domain.SandboxProfile{Driver: "typo"}, SandboxRequirements{}, domain.DockerfileDefinition{})
+ if sb != nil || err == nil || err.Error() != want {
+ t.Fatalf("kind %q: got %v, %v; want error %q", kind, sb, err, want)
+ }
+ }
+}
+
+func TestOpenRemoteSandboxRejectsUnknownDriver(t *testing.T) {
+ _, err := OpenRemoteSandbox(t.Context(), "typo", t.TempDir(), domain.SandboxProfile{ID: "t", Driver: "typo-driver"}, SandboxRequirements{}, SandboxResources{})
+ if err == nil || !strings.Contains(err.Error(), `unsupported runner sandbox driver "typo-driver"`) {
+ t.Fatalf("unknown driver should fail closed, got %v", err)
+ }
+}
diff --git a/internal/runtime/sandbox_manager.go b/internal/runtime/sandbox_manager.go
index 7fca4fb3..d65a236d 100644
--- a/internal/runtime/sandbox_manager.go
+++ b/internal/runtime/sandbox_manager.go
@@ -95,27 +95,9 @@ func (m *Manager) open(ctx context.Context, id, dir string, profile domain.Sandb
if err != nil {
return fail(err)
}
- if len(activated.Overlay) > 0 {
- path := activated.Overlay["PATH"]
- profile.Environment = mergeEnvironmentMaps(activated.Overlay, profile.Environment)
- if path != "" {
- profile.Environment["PATH"] = path
- }
- }
+ profile.Environment = overlayEnvironment(profile.Environment, activated.Overlay)
profile, resources.Dockerfile = isolateLocalDriver(profile, resources.Dockerfile)
- var sandbox Sandbox
- switch strings.TrimSpace(profile.Driver) {
- case "", "local":
- sandbox = &localSandbox{id: id, workspace: workspace, environment: profile.Environment}
- case "seatbelt":
- sandbox, err = createSeatbelt(id, workspace, profile, requirements)
- case "bwrap":
- sandbox, err = createBwrap(id, workspace, profile, requirements)
- case "docker":
- sandbox, err = createDocker(ctx, id, workspace, profile, requirements, resources.Dockerfile)
- default:
- err = fmt.Errorf("unsupported sandbox driver %q", profile.Driver)
- }
+ sandbox, err := createDriverSandbox(ctx, "sandbox", id, workspace, profile, requirements, resources.Dockerfile)
if err != nil {
return fail(err)
}
@@ -440,3 +422,34 @@ func (s *managedSandbox) Exec(ctx context.Context, command CommandSpec, sink Eve
defer cleanup()
return s.Sandbox.Exec(ctx, bound, sink)
}
+
+// overlayEnvironment layers an activated environment's overlay under env,
+// except PATH: the overlay's PATH wins so its tools stay first on the path.
+// An empty overlay returns env unchanged.
+func overlayEnvironment(env, overlay map[string]string) map[string]string {
+ if len(overlay) == 0 {
+ return env
+ }
+ merged := mergeEnvironmentMaps(overlay, env)
+ if path := overlay["PATH"]; path != "" {
+ merged["PATH"] = path
+ }
+ return merged
+}
+
+// createDriverSandbox builds the sandbox for profile.Driver. kind only names
+// the caller in the unsupported-driver error.
+func createDriverSandbox(ctx context.Context, kind, id, workspace string, profile domain.SandboxProfile, requirements SandboxRequirements, dockerfile domain.DockerfileDefinition) (Sandbox, error) {
+ switch strings.TrimSpace(profile.Driver) {
+ case "", "local":
+ return &localSandbox{id: id, workspace: workspace, environment: profile.Environment}, nil
+ case "seatbelt":
+ return createSeatbelt(id, workspace, profile, requirements)
+ case "bwrap":
+ return createBwrap(id, workspace, profile, requirements)
+ case "docker":
+ return createDocker(ctx, id, workspace, profile, requirements, dockerfile)
+ default:
+ return nil, fmt.Errorf("unsupported %s driver %q", kind, profile.Driver)
+ }
+}