|
| 1 | +package project |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/fs" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "sort" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/LAA-Software-Engineering/agentic-control-plane/internal/spec" |
| 12 | +) |
| 13 | + |
| 14 | +// YAML file suffixes loaded from directories (recursive) and explicit import paths. |
| 15 | +const yamlExt = ".yaml" |
| 16 | +const ymlExt = ".yml" |
| 17 | + |
| 18 | +// LoadProject loads root/project.yaml (or project.yml), expands spec.imports, parses |
| 19 | +// every YAML document with [internal/spec], and merges resources into a ProjectGraph. |
| 20 | +// Duplicate kind/metadata.name pairs are rejected (§9.1). Only the root project file |
| 21 | +// may define kind Project. |
| 22 | +func LoadProject(root string) (*spec.ProjectGraph, error) { |
| 23 | + rootAbs, err := filepath.Abs(filepath.Clean(root)) |
| 24 | + if err != nil { |
| 25 | + return nil, fmt.Errorf("project root: %w", err) |
| 26 | + } |
| 27 | + |
| 28 | + projPath, err := findProjectFile(rootAbs) |
| 29 | + if err != nil { |
| 30 | + return nil, err |
| 31 | + } |
| 32 | + |
| 33 | + dec, err := spec.LoadResourceFile(projPath) |
| 34 | + if err != nil { |
| 35 | + return nil, err |
| 36 | + } |
| 37 | + pr, ok := dec.Resource.(*spec.ProjectResource) |
| 38 | + if !ok || dec.Kind() != spec.KindProject { |
| 39 | + return nil, fmt.Errorf("%s: expected kind Project, got %q", projPath, dec.Kind()) |
| 40 | + } |
| 41 | + |
| 42 | + g := &spec.ProjectGraph{ |
| 43 | + Meta: pr.Metadata, |
| 44 | + Spec: pr.Spec, |
| 45 | + Agents: make(map[string]*spec.AgentResource), |
| 46 | + Tools: make(map[string]*spec.ToolResource), |
| 47 | + Workflows: make(map[string]*spec.WorkflowResource), |
| 48 | + Policies: make(map[string]*spec.PolicyResource), |
| 49 | + Environments: make(map[string]*spec.EnvironmentResource), |
| 50 | + } |
| 51 | + |
| 52 | + seen := map[resourceKey]string{ |
| 53 | + {kind: spec.KindProject, name: strings.TrimSpace(pr.Metadata.Name)}: projPath, |
| 54 | + } |
| 55 | + |
| 56 | + files, err := expandImports(rootAbs, projPath, g.Spec.Imports) |
| 57 | + if err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + |
| 61 | + for _, path := range files { |
| 62 | + if path == projPath { |
| 63 | + continue |
| 64 | + } |
| 65 | + d, err := spec.LoadResourceFile(path) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + if err := mergeDecoded(g, d, path, seen); err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return g, nil |
| 75 | +} |
| 76 | + |
| 77 | +type resourceKey struct { |
| 78 | + kind string |
| 79 | + name string |
| 80 | +} |
| 81 | + |
| 82 | +func findProjectFile(dir string) (string, error) { |
| 83 | + for _, name := range []string{"project.yaml", "project.yml"} { |
| 84 | + p := filepath.Join(dir, name) |
| 85 | + if _, err := os.Stat(p); err == nil { |
| 86 | + return p, nil |
| 87 | + } |
| 88 | + } |
| 89 | + return "", fmt.Errorf("no project.yaml or project.yml in %q", dir) |
| 90 | +} |
| 91 | + |
| 92 | +func expandImports(rootAbs, projPath string, imports []string) ([]string, error) { |
| 93 | + seen := map[string]struct{}{} |
| 94 | + var out []string |
| 95 | + |
| 96 | + add := func(p string) { |
| 97 | + p = filepath.Clean(p) |
| 98 | + if _, ok := seen[p]; ok { |
| 99 | + return |
| 100 | + } |
| 101 | + seen[p] = struct{}{} |
| 102 | + out = append(out, p) |
| 103 | + } |
| 104 | + |
| 105 | + add(projPath) |
| 106 | + |
| 107 | + for _, imp := range imports { |
| 108 | + imp = strings.TrimSpace(imp) |
| 109 | + if imp == "" { |
| 110 | + continue |
| 111 | + } |
| 112 | + if filepath.IsAbs(imp) { |
| 113 | + return nil, fmt.Errorf("import %q: absolute paths are not allowed", imp) |
| 114 | + } |
| 115 | + full := filepath.Join(rootAbs, filepath.FromSlash(imp)) |
| 116 | + full = filepath.Clean(full) |
| 117 | + if !isUnderRoot(rootAbs, full) { |
| 118 | + return nil, fmt.Errorf("import %q resolves outside project root", imp) |
| 119 | + } |
| 120 | + |
| 121 | + fi, err := os.Stat(full) |
| 122 | + if err != nil { |
| 123 | + return nil, fmt.Errorf("import %q: %w", imp, err) |
| 124 | + } |
| 125 | + |
| 126 | + if fi.IsDir() { |
| 127 | + list, err := walkYAMLFiles(full) |
| 128 | + if err != nil { |
| 129 | + return nil, fmt.Errorf("import %q: %w", imp, err) |
| 130 | + } |
| 131 | + for _, f := range list { |
| 132 | + add(f) |
| 133 | + } |
| 134 | + } else { |
| 135 | + add(full) |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + sort.Strings(out) |
| 140 | + return out, nil |
| 141 | +} |
| 142 | + |
| 143 | +func walkYAMLFiles(dir string) ([]string, error) { |
| 144 | + var files []string |
| 145 | + err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { |
| 146 | + if err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + if d.IsDir() { |
| 150 | + return nil |
| 151 | + } |
| 152 | + ext := strings.ToLower(filepath.Ext(path)) |
| 153 | + if ext == yamlExt || ext == ymlExt { |
| 154 | + files = append(files, path) |
| 155 | + } |
| 156 | + return nil |
| 157 | + }) |
| 158 | + if err != nil { |
| 159 | + return nil, err |
| 160 | + } |
| 161 | + sort.Strings(files) |
| 162 | + return files, nil |
| 163 | +} |
| 164 | + |
| 165 | +func isUnderRoot(root, p string) bool { |
| 166 | + root = filepath.Clean(root) |
| 167 | + p = filepath.Clean(p) |
| 168 | + rel, err := filepath.Rel(root, p) |
| 169 | + if err != nil { |
| 170 | + return false |
| 171 | + } |
| 172 | + if rel == "." { |
| 173 | + return true |
| 174 | + } |
| 175 | + return rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) |
| 176 | +} |
| 177 | + |
| 178 | +func mergeDecoded(g *spec.ProjectGraph, d *spec.Decoded, path string, seen map[resourceKey]string) error { |
| 179 | + kind := d.Kind() |
| 180 | + if kind == spec.KindProject { |
| 181 | + return fmt.Errorf("%s: kind Project must only be defined in the root project.yaml", path) |
| 182 | + } |
| 183 | + |
| 184 | + id := d.ResourceID() |
| 185 | + name := strings.TrimSpace(id.Name) |
| 186 | + if name == "" { |
| 187 | + return fmt.Errorf("%s: resource has empty metadata.name", path) |
| 188 | + } |
| 189 | + |
| 190 | + key := resourceKey{kind: kind, name: name} |
| 191 | + if prev, ok := seen[key]; ok { |
| 192 | + return &DuplicateResourceError{Kind: kind, Name: name, Paths: []string{prev, path}} |
| 193 | + } |
| 194 | + seen[key] = path |
| 195 | + |
| 196 | + switch kind { |
| 197 | + case spec.KindAgent: |
| 198 | + ar, ok := d.Resource.(*spec.AgentResource) |
| 199 | + if !ok { |
| 200 | + return fmt.Errorf("%s: internal error: wrong type for Agent", path) |
| 201 | + } |
| 202 | + g.Agents[name] = ar |
| 203 | + case spec.KindTool: |
| 204 | + tr, ok := d.Resource.(*spec.ToolResource) |
| 205 | + if !ok { |
| 206 | + return fmt.Errorf("%s: internal error: wrong type for Tool", path) |
| 207 | + } |
| 208 | + g.Tools[name] = tr |
| 209 | + case spec.KindWorkflow: |
| 210 | + wr, ok := d.Resource.(*spec.WorkflowResource) |
| 211 | + if !ok { |
| 212 | + return fmt.Errorf("%s: internal error: wrong type for Workflow", path) |
| 213 | + } |
| 214 | + g.Workflows[name] = wr |
| 215 | + case spec.KindPolicy: |
| 216 | + pr, ok := d.Resource.(*spec.PolicyResource) |
| 217 | + if !ok { |
| 218 | + return fmt.Errorf("%s: internal error: wrong type for Policy", path) |
| 219 | + } |
| 220 | + g.Policies[name] = pr |
| 221 | + case spec.KindEnvironment: |
| 222 | + er, ok := d.Resource.(*spec.EnvironmentResource) |
| 223 | + if !ok { |
| 224 | + return fmt.Errorf("%s: internal error: wrong type for Environment", path) |
| 225 | + } |
| 226 | + g.Environments[name] = er |
| 227 | + default: |
| 228 | + return fmt.Errorf("%s: unsupported kind %q", path, kind) |
| 229 | + } |
| 230 | + return nil |
| 231 | +} |
0 commit comments