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
【架构优化】合并 run_workspace.go 中五处重复的工作区不可用响应构造为 workspaceUnavailablePayload (#728)
fx-kurumicdce425e8058
diff --git a/internal/httpapi/run_workspace.go b/internal/httpapi/run_workspace.goindex a961bc1d..02066a96 100644--- a/internal/httpapi/run_workspace.go+++ b/internal/httpapi/run_workspace.go@@ -70,6 +70,16 @@ func workspaceRepositoryDetails(run domain.Run) []workspaceRepositoryDetail {return details}+// workspaceUnavailablePayload builds the shared 「工作区不可用」 view: the panel+// renders a read-only notice with the reason instead of a change list, so the+// fields stay in one place instead of being copied per call site.+func workspaceUnavailablePayload(run domain.Run, reason string) gin.H {+ return gin.H{+ "available": false, "readOnly": true, "reason": reason,+ "runStatus": run.Status, "repositoryDetails": workspaceRepositoryDetails(run),+ }+}+// cloneURLWithoutCredentials keeps only what the panel needs to build a 工蜂// link: a remote configured as https://user:token@host/... must not hand the// token to the browser.@@ -131,7 +141,7 @@ func (h *runsHandlers) runWorkspace(c *gin.Context) {if !run.WorkspaceCleanedAt.IsZero() {reason := "工作区已回收;有保存的快照时可按需恢复,空闲 1 小时后再次回收"if c.FullPath() == "/api/runs/:id/workspace" {- payload := gin.H{"available": false, "readOnly": true, "reason": reason, "runStatus": run.Status, "repositoryDetails": workspaceRepositoryDetails(run)}+ payload := workspaceUnavailablePayload(run, reason)h.workspaceLifecycleInfo(c, run, payload)c.JSON(http.StatusOK, payload)return@@ -143,7 +153,7 @@ func (h *runsHandlers) runWorkspace(c *gin.Context) {if _, exists := h.repo.Runner(runnerID); !exists {reason := "执行机器 Daemon 已卸载,工作区已随之删除;重新连接执行机器也无法恢复此工作区"if c.FullPath() == "/api/runs/:id/workspace" {- payload := gin.H{"available": false, "readOnly": true, "reason": reason, "runStatus": run.Status, "repositoryDetails": workspaceRepositoryDetails(run)}+ payload := workspaceUnavailablePayload(run, reason)h.workspaceLifecycleInfo(c, run, payload)c.JSON(http.StatusOK, payload)return@@ -162,7 +172,7 @@ func (h *runsHandlers) runWorkspace(c *gin.Context) {}if reason != "" {if c.FullPath() == "/api/runs/:id/workspace" {- payload := gin.H{"available": false, "readOnly": true, "reason": reason, "runStatus": run.Status, "repositoryDetails": workspaceRepositoryDetails(run)}+ payload := workspaceUnavailablePayload(run, reason)h.workspaceLifecycleInfo(c, run, payload)c.JSON(http.StatusOK, payload)return@@ -311,7 +321,7 @@ func (h *runsHandlers) proxyRunnerWorkspace(c *gin.Context, run domain.Run, runnreason = "远程工作区读取超时"}if operation == domain.RunnerWorkspaceOperationInfo {- payload := gin.H{"available": false, "readOnly": true, "reason": reason, "runStatus": run.Status, "repositoryDetails": workspaceRepositoryDetails(run)}+ payload := workspaceUnavailablePayload(run, reason)h.workspaceLifecycleInfo(c, run, payload)c.JSON(http.StatusOK, payload)return@@ -325,7 +335,7 @@ func (h *runsHandlers) proxyRunnerWorkspace(c *gin.Context, run domain.Run, runnstatus = http.StatusBadRequest}if operation == domain.RunnerWorkspaceOperationInfo && status == http.StatusNotFound {- c.JSON(http.StatusOK, gin.H{"available": false, "readOnly": true, "reason": response.Error, "runStatus": run.Status, "repositoryDetails": workspaceRepositoryDetails(run)})+ c.JSON(http.StatusOK, workspaceUnavailablePayload(run, response.Error))return}c.JSON(status, map[string]string{"error": response.Error})diff --git a/internal/httpapi/run_workspace_unavailable_sites_test.go b/internal/httpapi/run_workspace_unavailable_sites_test.gonew file mode 100644index 00000000..f19940aa--- /dev/null+++ b/internal/httpapi/run_workspace_unavailable_sites_test.go@@ -0,0 +1,127 @@+package httpapi++import (+ "encoding/json"+ "net/http"+ "net/http/httptest"+ "path/filepath"+ "testing"++ "agentflow/internal/domain"+ "agentflow/internal/engine"+ "agentflow/internal/repo"+ "agentflow/internal/runtime"+ "agentflow/internal/tracker"++ "github.com/gin-gonic/gin"+)++// workspaceUnavailableView is what the panel decodes from the 「工作区不可用」+// response; every call site of workspaceUnavailablePayload must fill it with+// its own reason.+type workspaceUnavailableView struct {+ Available *bool `json:"available"`+ ReadOnly bool `json:"readOnly"`+ Reason string `json:"reason"`+ RunStatus string `json:"runStatus"`+ RepositoryDetails []workspaceRepositoryDetail `json:"repositoryDetails"`+}++func assertWorkspaceUnavailableView(t *testing.T, got workspaceUnavailableView, reason, status string) {+ t.Helper()+ if got.Available == nil || *got.Available || !got.ReadOnly {+ t.Fatalf("available/readOnly = %v/%v, want false/true", got.Available, got.ReadOnly)+ }+ if got.Reason != reason {+ t.Fatalf("reason = %q, want %q", got.Reason, reason)+ }+ if got.RunStatus != status {+ t.Fatalf("runStatus = %q, want %q", got.RunStatus, status)+ }+ if len(got.RepositoryDetails) != 1 || got.RepositoryDetails[0].Name != "app" || got.RepositoryDetails[0].CloneURL != "https://example.com/app.git" {+ t.Fatalf("repositoryDetails = %+v", got.RepositoryDetails)+ }+}++// A local run whose workspace directory was never created reports the local+// reason through the shared payload, not an empty or generic one.+func TestRunWorkspaceMissingLocalWorkspaceReportsReason(t *testing.T) {+ store, err := repo.OpenTest()+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { store.Close() })+ run := domain.Run{+ ID: "run-local-missing", Status: "failed", Nodes: map[string]*domain.NodeRun{},+ Repositories: []domain.RepositoryContext{{Name: "app", CloneURL: "https://user:[email protected]/app.git", WorkBranch: "feat/x"}},+ }+ if err := store.PutRun(run); err != nil {+ t.Fatal(err)+ }+ handler := New(store, engine.New(store, runtime.NewManager(filepath.Join(t.TempDir(), "workspaces"))), tracker.NewService(), "", "")+ info := requestEmptyJSON[workspaceUnavailableView](t, handler, http.MethodGet, "/api/runs/run-local-missing/workspace", http.StatusOK)+ assertWorkspaceUnavailableView(t, info, "工作区尚未创建或已被清理", "failed")+}++// When the runner answers the info request with 404, the panel must show the+// runner's own error as the reason (it explains why the directory is gone).+func TestRunWorkspaceRemoteNotFoundReportsRunnerError(t *testing.T) {+ gin.SetMode(gin.TestMode)+ f := cvBNewHTTPFixture(t)+ run := domain.Run{+ ID: "run-remote-404", Status: "succeeded", Nodes: map[string]*domain.NodeRun{},+ Repositories: []domain.RepositoryContext{{Name: "app", CloneURL: "https://user:[email protected]/app.git"}},+ ExecutionContext: domain.TelemetryContext{ExecutionMode: domain.ExecutionModeRunner, ExecutionRunnerID: "runner-404"},+ }+ if err := f.s.PutRun(run); err != nil {+ t.Fatal(err)+ }+ if err := f.s.PutRunner(domain.Runner{ID: "runner-404", Status: domain.RunnerStatusOnline}); err != nil {+ t.Fatal(err)+ }+ hub := newRunnerControlHub()+ conn := &runnerControlConn{runnerID: "runner-404", send: make(chan runnerControlMessage, 4), done: make(chan struct{})}+ hub.conns[conn.runnerID] = conn+ done := make(chan struct{})+ go func() {+ defer close(done)+ for message := range conn.send {+ hub.resolveWorkspace(message.RequestID, domain.RunnerWorkspaceResponse{Status: http.StatusNotFound, Error: "执行机器上的运行目录不存在"})+ }+ }()+ t.Cleanup(func() {+ close(conn.send)+ <-done+ })+ h := &runsHandlers{deps: &deps{repo: f.s, engine: f.e}, runnerControls: hub}++ router := gin.New()+ router.GET("/api/runs/:id/workspace", h.runWorkspace)+ router.GET("/api/runs/:id/workspace/tree", h.runWorkspace)++ rec := httptest.NewRecorder()+ router.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/api/runs/run-remote-404/workspace", nil))+ if rec.Code != http.StatusOK {+ t.Fatalf("info status = %d: %s", rec.Code, rec.Body.String())+ }+ var info workspaceUnavailableView+ if err := json.Unmarshal(rec.Body.Bytes(), &info); err != nil {+ t.Fatal(err)+ }+ assertWorkspaceUnavailableView(t, info, "执行机器上的运行目录不存在", "succeeded")++ // Only the info view degrades to the unavailable payload; other+ // operations keep the runner's 404 so the panel shows an error.+ rec = httptest.NewRecorder()+ router.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/api/runs/run-remote-404/workspace/tree", nil))+ if rec.Code != http.StatusNotFound {+ t.Fatalf("tree status = %d: %s", rec.Code, rec.Body.String())+ }+ var tree map[string]any+ if err := json.Unmarshal(rec.Body.Bytes(), &tree); err != nil {+ t.Fatal(err)+ }+ if tree["error"] != "执行机器上的运行目录不存在" || tree["available"] != nil {+ t.Fatalf("tree body = %v", tree)+ }+}diff --git a/internal/httpapi/run_workspace_unavailable_test.go b/internal/httpapi/run_workspace_unavailable_test.gonew file mode 100644index 00000000..75519041--- /dev/null+++ b/internal/httpapi/run_workspace_unavailable_test.go@@ -0,0 +1,50 @@+package httpapi++import (+ "testing"++ "agentflow/internal/domain"+)++// 面板靠这四个字段判断「工作区不可用」并展示只读提示;它们之前由 5 处+// 内联 gin.H 各自复制,抽成 helper 后必须保持完全一致的取值。+func TestWorkspaceUnavailablePayloadKeepsShape(t *testing.T) {+ run := domain.Run{+ ID: "run-1",+ Status: "succeeded",+ Repositories: []domain.RepositoryContext{{+ Name: "loopforge",+ CloneURL: "https://user:[email protected]/loopforge.git",+ BaseBranch: "main",+ WorkBranch: "feat/x",+ }},+ }+ payload := workspaceUnavailablePayload(run, "工作区尚未创建或已被清理")++ if payload["available"] != false {+ t.Fatalf("available = %v, want false", payload["available"])+ }+ if payload["readOnly"] != true {+ t.Fatalf("readOnly = %v, want true", payload["readOnly"])+ }+ if payload["reason"] != "工作区尚未创建或已被清理" {+ t.Fatalf("reason = %v", payload["reason"])+ }+ if payload["runStatus"] != "succeeded" {+ t.Fatalf("runStatus = %v, want succeeded", payload["runStatus"])+ }+ details, ok := payload["repositoryDetails"].([]workspaceRepositoryDetail)+ if !ok || len(details) != 1 {+ t.Fatalf("repositoryDetails = %#v", payload["repositoryDetails"])+ }+ if details[0].Name != "loopforge" || details[0].WorkBranch != "feat/x" || details[0].BaseBranch != "main" {+ t.Fatalf("repository detail = %#v", details[0])+ }+ // 与 workspaceRepositoryDetails 一致:凭据不能出现在面板响应里。+ if details[0].CloneURL != "https://example.com/loopforge.git" {+ t.Fatalf("cloneUrl leaked credentials: %q", details[0].CloneURL)+ }+ if len(payload) != 5 {+ t.Fatalf("unexpected field count %d: %#v", len(payload), payload)+ }+}