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.

【架构优化】从 readWorkspaceTree 中拆出空目录剔除 pruneEmptyWorkspaceDirs (#714)

fx-kurumic300bcfe78e3
diff --git a/internal/httpapi/run_workspace.go b/internal/httpapi/run_workspace.go
index 357b432b..a961bc1d 100644
--- a/internal/httpapi/run_workspace.go
+++ b/internal/httpapi/run_workspace.go
@@ -519,6 +519,14 @@ func readWorkspaceTree(root, raw string) ([]workspaceEntry, bool, error) {
entries = append(entries, workspaceEntry{Path: rel, Name: d.Name(), Type: kind, Size: info.Size(), Modified: info.ModTime()})
return nil
})
+ entries = pruneEmptyWorkspaceDirs(entries)
+ sort.Slice(entries, func(i, j int) bool { return entries[i].Path < entries[j].Path })
+ return entries, truncated, err
+}
+
+// pruneEmptyWorkspaceDirs drops directory entries that have no visible file
+// beneath them, filtering entries in place and keeping the original order.
+func pruneEmptyWorkspaceDirs(entries []workspaceEntry) []workspaceEntry {
visibleParents := map[string]bool{}
for _, entry := range entries {
if entry.Type != "file" {
@@ -535,9 +543,7 @@ func readWorkspaceTree(root, raw string) ([]workspaceEntry, bool, error) {
}
visible = append(visible, entry)
}
- entries = visible
- sort.Slice(entries, func(i, j int) bool { return entries[i].Path < entries[j].Path })
- return entries, truncated, err
+ return visible
}
func readWorkspaceFile(root, raw string) (string, int64, error) {
diff --git a/internal/httpapi/run_workspace_prune_test.go b/internal/httpapi/run_workspace_prune_test.go
new file mode 100644
index 00000000..0f796120
--- /dev/null
+++ b/internal/httpapi/run_workspace_prune_test.go
@@ -0,0 +1,34 @@
+package httpapi
+
+import (
+ "reflect"
+ "testing"
+)
+
+func TestPruneEmptyWorkspaceDirs(t *testing.T) {
+ dir := func(path string) workspaceEntry { return workspaceEntry{Path: path, Type: "directory"} }
+ file := func(path string, size int64) workspaceEntry {
+ return workspaceEntry{Path: path, Type: "file", Size: size}
+ }
+ for _, tc := range []struct {
+ name string
+ in []workspaceEntry
+ want []workspaceEntry
+ }{
+ {name: "empty input", in: nil, want: nil},
+ {name: "files only", in: []workspaceEntry{file("b.txt", 2), file("a.txt", 1)}, want: []workspaceEntry{file("b.txt", 2), file("a.txt", 1)}},
+ {name: "empty directories dropped", in: []workspaceEntry{dir("empty"), dir("empty/nested"), file("a.txt", 1)}, want: []workspaceEntry{file("a.txt", 1)}},
+ {
+ name: "ancestors of nested file kept in order",
+ in: []workspaceEntry{dir("src"), dir("src/pkg"), dir("src/pkg/empty"), file("src/pkg/a.go", 3), dir("docs"), file("README.md", 4)},
+ want: []workspaceEntry{dir("src"), dir("src/pkg"), file("src/pkg/a.go", 3), file("README.md", 4)},
+ },
+ {name: "directory prefix is not an ancestor", in: []workspaceEntry{dir("src"), file("src2/a.go", 1)}, want: []workspaceEntry{file("src2/a.go", 1)}},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ if got := pruneEmptyWorkspaceDirs(tc.in); !reflect.DeepEqual(got, tc.want) {
+ t.Fatalf("pruneEmptyWorkspaceDirs() = %+v, want %+v", got, tc.want)
+ }
+ })
+ }
+}