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.

【架构优化】合并 serveRunnerBinary/serveGateMCPBinary 中重复的 os/arch 参数解析与校验为 binaryDownloadPlatform (#679) (#680)

fx-kurumi8830005314af
diff --git a/internal/httpapi/binary_download_platform_test.go b/internal/httpapi/binary_download_platform_test.go
new file mode 100644
index 00000000..71bd290d
--- /dev/null
+++ b/internal/httpapi/binary_download_platform_test.go
@@ -0,0 +1,80 @@
+package httpapi
+
+import (
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/gin-gonic/gin"
+)
+
+func TestBinaryDownloadPlatform(t *testing.T) {
+ cases := []struct {
+ name string
+ query string
+ wantOS string
+ wantArch string
+ wantOK bool
+ wantErr string
+ }{
+ {name: "defaults", query: "", wantOS: "darwin", wantArch: "amd64", wantOK: true},
+ {name: "normalized", query: "?os=%20Linux%20&arch=ARM64", wantOS: "linux", wantArch: "arm64", wantOK: true},
+ {name: "invalid chars fall back to defaults", query: "?os=lin-ux&arch=amd_64", wantOS: "darwin", wantArch: "amd64", wantOK: true},
+ {name: "unsupported os", query: "?os=windows&arch=amd64", wantErr: `unsupported os "windows" (darwin, linux)`},
+ {name: "unsupported arch", query: "?os=linux&arch=386", wantErr: `unsupported arch "386" (amd64, arm64)`},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ rec := httptest.NewRecorder()
+ c, _ := gin.CreateTestContext(rec)
+ c.Request = httptest.NewRequest(http.MethodGet, "/binary"+tc.query, nil)
+ gotOS, gotArch, ok := binaryDownloadPlatform(c)
+ if ok != tc.wantOK || gotOS != tc.wantOS || gotArch != tc.wantArch {
+ t.Fatalf("got (%q, %q, %v), want (%q, %q, %v)", gotOS, gotArch, ok, tc.wantOS, tc.wantArch, tc.wantOK)
+ }
+ if tc.wantOK {
+ if rec.Body.Len() != 0 {
+ t.Fatalf("unexpected response body %q", rec.Body.String())
+ }
+ return
+ }
+ if rec.Code != http.StatusBadRequest {
+ t.Fatalf("status=%d want 400", rec.Code)
+ }
+ var body map[string]string
+ if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil || body["error"] != tc.wantErr {
+ t.Fatalf("body=%q err=%v want error %q", rec.Body.String(), err, tc.wantErr)
+ }
+ })
+ }
+}
+
+// Both binary download handlers must keep rejecting unsupported platforms
+// before touching any prebuilt artifact or source build.
+func TestBinaryDownloadHandlersRejectUnsupportedPlatform(t *testing.T) {
+ dir := t.TempDir()
+ for _, name := range []string{"agentflow-runner-windows-amd64", "agentflow-gate-mcp-linux-386"} {
+ if err := os.WriteFile(filepath.Join(dir, name), []byte("bin"), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ }
+ t.Setenv("AGENTFLOW_RUNNER_BINARY", filepath.Join(dir, "agentflow-runner-{os}-{arch}"))
+ t.Setenv("AGENTFLOW_GATE_MCP_BINARY", filepath.Join(dir, "agentflow-gate-mcp-{os}-{arch}"))
+ g := gin.New()
+ g.GET("/runner", serveRunnerBinary)
+ g.GET("/gate", serveGateMCPBinary)
+ for _, tc := range []struct{ path, wantErr string }{
+ {"/runner?os=windows&arch=amd64", `unsupported os "windows" (darwin, linux)`},
+ {"/gate?os=linux&arch=386", `unsupported arch "386" (amd64, arm64)`},
+ } {
+ rec := httptest.NewRecorder()
+ g.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, tc.path, nil))
+ var body map[string]string
+ if rec.Code != http.StatusBadRequest || json.Unmarshal(rec.Body.Bytes(), &body) != nil || body["error"] != tc.wantErr {
+ t.Fatalf("%s: status=%d body=%q want 400 %q", tc.path, rec.Code, rec.Body.String(), tc.wantErr)
+ }
+ }
+}
diff --git a/internal/httpapi/gate_mcp_binary.go b/internal/httpapi/gate_mcp_binary.go
index 6bbc94ee..2cd040a0 100644
--- a/internal/httpapi/gate_mcp_binary.go
+++ b/internal/httpapi/gate_mcp_binary.go
@@ -19,20 +19,8 @@ var gateMCPBinaryMu sync.Mutex
// submit terminal node decisions. It mirrors runner binary distribution so
// installed execution machines need neither Go nor Python.
func serveGateMCPBinary(c *gin.Context) {
- targetOS := sanitizePlatformParam(c.Query("os"))
- targetArch := sanitizePlatformParam(c.Query("arch"))
- if targetOS == "" {
- targetOS = "darwin"
- }
- if targetArch == "" {
- targetArch = "amd64"
- }
- if targetOS != "darwin" && targetOS != "linux" {
- c.JSON(http.StatusBadRequest, map[string]string{"error": fmt.Sprintf("unsupported os %q (darwin, linux)", targetOS)})
- return
- }
- if targetArch != "amd64" && targetArch != "arm64" {
- c.JSON(http.StatusBadRequest, map[string]string{"error": fmt.Sprintf("unsupported arch %q (amd64, arm64)", targetArch)})
+ targetOS, targetArch, ok := binaryDownloadPlatform(c)
+ if !ok {
return
}
if pattern := strings.TrimSpace(os.Getenv("AGENTFLOW_GATE_MCP_BINARY")); pattern != "" {
diff --git a/internal/httpapi/runner_binary.go b/internal/httpapi/runner_binary.go
index d190f01a..4673a4c1 100644
--- a/internal/httpapi/runner_binary.go
+++ b/internal/httpapi/runner_binary.go
@@ -32,24 +32,8 @@ var runnerBinaryMu sync.Mutex
//
// g.GET("/runners/agentflow-runner", serveRunnerBinary)
func serveRunnerBinary(c *gin.Context) {
- targetOS := sanitizePlatformParam(c.Query("os"))
- targetArch := sanitizePlatformParam(c.Query("arch"))
- if targetOS == "" {
- targetOS = "darwin"
- }
- if targetArch == "" {
- targetArch = "amd64"
- }
- switch targetOS {
- case "darwin", "linux":
- default:
- c.JSON(http.StatusBadRequest, map[string]string{"error": fmt.Sprintf("unsupported os %q (darwin, linux)", targetOS)})
- return
- }
- switch targetArch {
- case "amd64", "arm64":
- default:
- c.JSON(http.StatusBadRequest, map[string]string{"error": fmt.Sprintf("unsupported arch %q (amd64, arm64)", targetArch)})
+ targetOS, targetArch, ok := binaryDownloadPlatform(c)
+ if !ok {
return
}
@@ -106,6 +90,34 @@ func serveRunnerArtifact(c *gin.Context, path string) {
serveBinaryArtifact(c, path, "agentflow-runner")
}
+// binaryDownloadPlatform reads the os/arch query parameters shared by the
+// runner and gate MCP binary downloads. Empty values default to darwin/amd64.
+// Unsupported platforms are answered with 400 and ok=false, so callers simply
+// return.
+func binaryDownloadPlatform(c *gin.Context) (targetOS, targetArch string, ok bool) {
+ targetOS = sanitizePlatformParam(c.Query("os"))
+ targetArch = sanitizePlatformParam(c.Query("arch"))
+ if targetOS == "" {
+ targetOS = "darwin"
+ }
+ if targetArch == "" {
+ targetArch = "amd64"
+ }
+ switch targetOS {
+ case "darwin", "linux":
+ default:
+ c.JSON(http.StatusBadRequest, map[string]string{"error": fmt.Sprintf("unsupported os %q (darwin, linux)", targetOS)})
+ return "", "", false
+ }
+ switch targetArch {
+ case "amd64", "arm64":
+ default:
+ c.JSON(http.StatusBadRequest, map[string]string{"error": fmt.Sprintf("unsupported arch %q (amd64, arm64)", targetArch)})
+ return "", "", false
+ }
+ return targetOS, targetArch, true
+}
+
func sanitizePlatformParam(v string) string {
v = strings.ToLower(strings.TrimSpace(v))
if len(v) > 16 {