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.
JavaScript agent-orchestrationagentic-workflowai-agentsai-codingclaude-codecode-reviewcodebuddycodexcoding-agentscursordeveloper-toolshuman-on-the-loopllmmcpmulti-agentsdlcself-hostedsopworkflow-automationworkflow-engine
【架构优化】从 prepareSandboxEnvironment 中拆出环境变量覆盖层组装 environmentOverlay (#631)
fx-kurumi15e188054c8d
diff --git a/internal/runtime/sandbox_environment.go b/internal/runtime/sandbox_environment.goindex a5c91e8d..bc960443 100644--- a/internal/runtime/sandbox_environment.go+++ b/internal/runtime/sandbox_environment.go@@ -80,6 +80,17 @@ func prepareSandboxEnvironment(workspace string, profile domain.SandboxProfile,}root = discovered}+ overlay, err := environmentOverlay(workspace, root, envID, profile, catalog)+ if err != nil {+ return activatedEnvironment{}, err+ }+ return activatedEnvironment{Overlay: overlay, Root: root, InstallRel: installRel, InstallScript: catalog.InstallScript}, nil+}++// environmentOverlay builds the variables layered over a process sandbox for+// an activated environment rooted at root, writing login-shell PATH hooks into+// the workspace when the environment contributes PATH directories.+func environmentOverlay(workspace, root, envID string, profile domain.SandboxProfile, catalog domain.EnvironmentDefinition) (map[string]string, error) {id := envIDif id == "" {id = workspaceEnvironmentID@@ -106,7 +117,7 @@ func prepareSandboxEnvironment(workspace string, profile domain.SandboxProfile,overlay["PATH"] = strings.Join(append(append([]string{}, pathDirs...), basePath), string(os.PathListSeparator))zdot, bashEnv, err := writeEnvironmentShellHooks(workspace, root, pathDirs)if err != nil {- return activatedEnvironment{}, err+ return nil, err}// Codex/Claude tool calls often use `zsh -lc` / `bash -lc`. Login shells// rebuild PATH (macOS path_helper, ~/.zshrc) and would otherwise hide@@ -116,7 +127,7 @@ func prepareSandboxEnvironment(workspace string, profile domain.SandboxProfile,overlay["BASH_ENV"] = bashEnvoverlay["ENV"] = bashEnv}- return activatedEnvironment{Overlay: overlay, Root: root, InstallRel: installRel, InstallScript: catalog.InstallScript}, nil+ return overlay, nil}func catalogDirName(id string) (string, error) {diff --git a/internal/runtime/sandbox_environment_overlay_test.go b/internal/runtime/sandbox_environment_overlay_test.gonew file mode 100644index 00000000..05ff639e--- /dev/null+++ b/internal/runtime/sandbox_environment_overlay_test.go@@ -0,0 +1,116 @@+package runtime++import (+ "os"+ "path/filepath"+ "strings"+ "testing"++ "agentflow/internal/domain"+)++func TestEnvironmentOverlayDefaultsIDAndPrefersProfilePath(t *testing.T) {+ workspace := t.TempDir()+ root := mkEnvironmentRoot(t, workspace, "agentflow-environment")+ profile := domain.SandboxProfile{Driver: "bwrap", Environment: map[string]string{"PATH": "/profile/bin"}}++ overlay, err := environmentOverlay(workspace, root, "", profile, domain.EnvironmentDefinition{})+ if err != nil {+ t.Fatal(err)+ }+ if overlay[environmentIDVar] != workspaceEnvironmentID {+ t.Fatalf("empty env id should fall back to %q, got %+v", workspaceEnvironmentID, overlay)+ }+ if overlay[environmentRootVar] != root || overlay[environmentPrefixVar] != root {+ t.Fatalf("root identity = %+v", overlay)+ }+ want := filepath.Join(root, "bin") + string(os.PathListSeparator) + "/profile/bin"+ if overlay["PATH"] != want {+ t.Fatalf("PATH = %q, want %q", overlay["PATH"], want)+ }+ zdot := filepath.Join(root, environmentZdotDir)+ if overlay["ZDOTDIR"] != zdot || overlay["BASH_ENV"] != filepath.Join(zdot, environmentBashEnvName) || overlay["ENV"] != overlay["BASH_ENV"] {+ t.Fatalf("shell hooks = %+v", overlay)+ }+ raw, err := os.ReadFile(overlay["BASH_ENV"])+ if err != nil {+ t.Fatal(err)+ }+ if !strings.Contains(string(raw), filepath.Join(root, "bin")) {+ t.Fatalf("bash env hook did not pin env bin: %s", raw)+ }+}++func TestEnvironmentOverlayFallsBackToProcessPath(t *testing.T) {+ workspace := t.TempDir()+ root := mkEnvironmentRoot(t, workspace, "env")+ t.Setenv("PATH", "/process/bin")++ overlay, err := environmentOverlay(workspace, root, "custom", domain.SandboxProfile{Driver: "bwrap"}, domain.EnvironmentDefinition{})+ if err != nil {+ t.Fatal(err)+ }+ if overlay[environmentIDVar] != "custom" {+ t.Fatalf("explicit env id lost: %+v", overlay)+ }+ if !strings.HasSuffix(overlay["PATH"], string(os.PathListSeparator)+"/process/bin") {+ t.Fatalf("PATH should end with process PATH, got %q", overlay["PATH"])+ }+}++func TestEnvironmentOverlayCatalogVariablesRequireCatalogID(t *testing.T) {+ workspace := t.TempDir()+ root := mkEnvironmentRoot(t, workspace, "env")+ vars := map[string]string{"COMPOSER_HOME": "${PREFIX}/composer"}++ without, err := environmentOverlay(workspace, root, "php", domain.SandboxProfile{}, domain.EnvironmentDefinition{Variables: vars})+ if err != nil {+ t.Fatal(err)+ }+ if _, ok := without["COMPOSER_HOME"]; ok {+ t.Fatalf("catalog variables without catalog id should be ignored: %+v", without)+ }++ with, err := environmentOverlay(workspace, root, "php", domain.SandboxProfile{}, domain.EnvironmentDefinition{ID: "php", Variables: vars, PathEntries: []string{"sbin", "/abs/ignored", "sbin"}})+ if err != nil {+ t.Fatal(err)+ }+ if with["COMPOSER_HOME"] != filepath.Join(root, "composer") {+ t.Fatalf("PREFIX placeholder was not expanded: %+v", with)+ }+ prefix := filepath.Join(root, "bin") + string(os.PathListSeparator) + filepath.Join(root, "sbin") + string(os.PathListSeparator)+ if !strings.HasPrefix(with["PATH"], prefix) {+ t.Fatalf("PATH = %q, want prefix %q", with["PATH"], prefix)+ }+}++func TestEnvironmentOverlayPropagatesShellHookError(t *testing.T) {+ workspace := t.TempDir()+ root := mkEnvironmentRoot(t, workspace, "env")+ // A regular file where the zdot hook directory belongs makes+ // writeEnvironmentShellHooks fail; the overlay must surface that error+ // instead of returning a PATH without the login-shell hooks.+ if err := os.WriteFile(filepath.Join(root, environmentZdotDir), []byte("not a dir"), 0o600); err != nil {+ t.Fatal(err)+ }++ overlay, err := environmentOverlay(workspace, root, "env", domain.SandboxProfile{}, domain.EnvironmentDefinition{})+ if err == nil {+ t.Fatalf("expected shell hook error, got overlay %+v", overlay)+ }+ if !strings.Contains(err.Error(), "not a real directory") {+ t.Fatalf("unexpected error: %v", err)+ }+ if overlay != nil {+ t.Fatalf("overlay should be nil on error, got %+v", overlay)+ }+}++func mkEnvironmentRoot(t *testing.T, workspace, name string) string {+ t.Helper()+ root := filepath.Join(workspace, name)+ if err := os.MkdirAll(root, 0o755); err != nil {+ t.Fatal(err)+ }+ return root+}diff --git a/internal/runtime/sandbox_environment_prepare_overlay_test.go b/internal/runtime/sandbox_environment_prepare_overlay_test.gonew file mode 100644index 00000000..91de3cd4--- /dev/null+++ b/internal/runtime/sandbox_environment_prepare_overlay_test.go@@ -0,0 +1,56 @@+package runtime++import (+ "os"+ "path/filepath"+ "strings"+ "testing"++ "agentflow/internal/domain"+)++// blockEnvironmentZdot puts a regular file where the login-shell hook+// directory goes, so writing the PATH hooks fails.+func blockEnvironmentZdot(t *testing.T, root string) {+ t.Helper()+ if err := os.WriteFile(filepath.Join(root, environmentZdotDir), []byte("not a dir"), 0o600); err != nil {+ t.Fatal(err)+ }+}++// prepareSandboxEnvironment must surface the overlay error instead of+// activating the environment with a partial or missing overlay.+func TestPrepareSandboxEnvironmentPropagatesOverlayError(t *testing.T) {+ workspace := t.TempDir()+ root := mkEnvironmentRoot(t, workspace, workspaceEnvironmentDir)+ blockEnvironmentZdot(t, root)++ activated, err := prepareSandboxEnvironment(workspace, domain.SandboxProfile{Driver: "bwrap"}, domain.EnvironmentDefinition{})+ if err == nil || !strings.Contains(err.Error(), "not a real directory") {+ t.Fatalf("err = %v, want shell hook directory error", err)+ }+ if activated.Overlay != nil || activated.Root != "" || activated.InstallRel != "" {+ t.Fatalf("activated on error = %+v, want zero value", activated)+ }+}++// The drop-in environment discovered in the workspace is activated with the+// overlay environmentOverlay built for that root.+func TestPrepareSandboxEnvironmentActivatesDiscoveredDropIn(t *testing.T) {+ workspace := t.TempDir()+ root := mkEnvironmentRoot(t, workspace, workspaceEnvironmentDir)++ activated, err := prepareSandboxEnvironment(workspace, domain.SandboxProfile{Driver: "bwrap", Environment: map[string]string{"PATH": "/profile/bin"}}, domain.EnvironmentDefinition{})+ if err != nil {+ t.Fatal(err)+ }+ if activated.Root != root || activated.InstallRel != "" {+ t.Fatalf("activated = %+v, want root %q", activated, root)+ }+ if activated.Overlay[environmentIDVar] != workspaceEnvironmentID || activated.Overlay[environmentRootVar] != root {+ t.Fatalf("overlay identity = %+v", activated.Overlay)+ }+ if want := filepath.Join(root, "bin") + string(os.PathListSeparator) + "/profile/bin"; activated.Overlay["PATH"] != want {+ t.Fatalf("PATH = %q, want %q", activated.Overlay["PATH"], want)+ }+}