|
1 | 1 | package project |
2 | 2 |
|
3 | | -import ( |
4 | | - "fmt" |
5 | | - "strings" |
| 3 | +import "github.com/LAA-Software-Engineering/agentic-control-plane/internal/spec" |
6 | 4 |
|
7 | | - "github.com/LAA-Software-Engineering/agentic-control-plane/internal/spec" |
8 | | -) |
9 | | - |
10 | | -// ResolveReferences checks that symbolic references in the merged graph resolve to |
11 | | -// existing resources (§9.1) and applies simple sequential workflow rules (§9.4). |
| 5 | +// ResolveReferences checks symbolic references and workflow step rules (§9.1, §9.4). |
12 | 6 | func ResolveReferences(g *spec.ProjectGraph) error { |
13 | | - if g == nil { |
14 | | - return nil |
15 | | - } |
16 | | - ix := BuildRefIndex(g) |
17 | | - |
18 | | - for agentName, tools := range ix.AgentTools { |
19 | | - for _, tn := range tools { |
20 | | - if _, ok := g.Tools[tn]; !ok { |
21 | | - return &MissingRefError{ |
22 | | - Referrer: spec.ResourceID{Kind: spec.KindAgent, Name: agentName}, |
23 | | - Missing: spec.ResourceID{Kind: spec.KindTool, Name: tn}, |
24 | | - } |
25 | | - } |
26 | | - } |
27 | | - } |
28 | | - for agentName, pol := range ix.AgentPolicies { |
29 | | - if _, ok := g.Policies[pol]; !ok { |
30 | | - return &MissingRefError{ |
31 | | - Referrer: spec.ResourceID{Kind: spec.KindAgent, Name: agentName}, |
32 | | - Missing: spec.ResourceID{Kind: spec.KindPolicy, Name: pol}, |
33 | | - } |
34 | | - } |
35 | | - } |
36 | | - |
37 | | - for wfName, wr := range g.Workflows { |
38 | | - if wr == nil { |
39 | | - continue |
40 | | - } |
41 | | - if err := validateWorkflowSteps(wfName, &wr.Spec); err != nil { |
42 | | - return err |
43 | | - } |
44 | | - for _, an := range ix.WorkflowAgents[wfName] { |
45 | | - if _, ok := g.Agents[an]; !ok { |
46 | | - return &MissingRefError{ |
47 | | - Referrer: spec.ResourceID{Kind: spec.KindWorkflow, Name: wfName}, |
48 | | - Missing: spec.ResourceID{Kind: spec.KindAgent, Name: an}, |
49 | | - } |
50 | | - } |
51 | | - } |
52 | | - for _, tn := range ix.WorkflowTools[wfName] { |
53 | | - if _, ok := g.Tools[tn]; !ok { |
54 | | - return &MissingRefError{ |
55 | | - Referrer: spec.ResourceID{Kind: spec.KindWorkflow, Name: wfName}, |
56 | | - Missing: spec.ResourceID{Kind: spec.KindTool, Name: tn}, |
57 | | - } |
58 | | - } |
59 | | - } |
60 | | - if pol := ix.WorkflowPolicies[wfName]; pol != "" { |
61 | | - if _, ok := g.Policies[pol]; !ok { |
62 | | - return &MissingRefError{ |
63 | | - Referrer: spec.ResourceID{Kind: spec.KindWorkflow, Name: wfName}, |
64 | | - Missing: spec.ResourceID{Kind: spec.KindPolicy, Name: pol}, |
65 | | - } |
66 | | - } |
67 | | - } |
68 | | - if err := validateWorkflowStepOrder(wfName, &wr.Spec); err != nil { |
69 | | - return err |
70 | | - } |
71 | | - } |
72 | | - return nil |
73 | | -} |
74 | | - |
75 | | -func validateWorkflowSteps(wfName string, w *spec.WorkflowSpec) error { |
76 | | - seenID := make(map[string]struct{}) |
77 | | - for _, st := range w.Steps { |
78 | | - sid := strings.TrimSpace(st.ID) |
79 | | - if sid != "" { |
80 | | - if _, dup := seenID[sid]; dup { |
81 | | - return fmt.Errorf("workflow %s: duplicate step id %q", wfName, sid) |
82 | | - } |
83 | | - seenID[sid] = struct{}{} |
84 | | - } |
85 | | - hasA := strings.TrimSpace(st.Agent) != "" |
86 | | - hasU := strings.TrimSpace(st.Uses) != "" |
87 | | - if hasA && hasU { |
88 | | - return fmt.Errorf("workflow %s step %q: cannot set both agent and uses", wfName, sid) |
89 | | - } |
90 | | - if !hasA && !hasU { |
91 | | - return fmt.Errorf("workflow %s step %q: must set exactly one of agent or uses", wfName, sid) |
92 | | - } |
93 | | - if hasU { |
94 | | - u := strings.TrimSpace(st.Uses) |
95 | | - if _, ok := spec.ParseToolUses(u); !ok { |
96 | | - return fmt.Errorf("workflow %s step %q: unsupported uses %q (expected tool.<name>...)", wfName, sid, u) |
97 | | - } |
98 | | - } |
99 | | - } |
100 | | - return nil |
101 | | -} |
102 | | - |
103 | | -func validateWorkflowStepOrder(wfName string, w *spec.WorkflowSpec) error { |
104 | | - idToIdx := make(map[string]int) |
105 | | - for i, st := range w.Steps { |
106 | | - id := strings.TrimSpace(st.ID) |
107 | | - if id == "" { |
108 | | - continue |
109 | | - } |
110 | | - idToIdx[id] = i |
111 | | - } |
112 | | - for i, st := range w.Steps { |
113 | | - sid := strings.TrimSpace(st.ID) |
114 | | - for _, sval := range spec.CollectWithStringValues(st.With) { |
115 | | - for _, dep := range spec.InterpolationStepRefs(sval) { |
116 | | - j, ok := idToIdx[dep] |
117 | | - if !ok { |
118 | | - return fmt.Errorf("workflow %s step %q: interpolation references unknown step %q", wfName, sid, dep) |
119 | | - } |
120 | | - if j >= i { |
121 | | - return fmt.Errorf("workflow %s step %q: forward reference to steps.%s (§9.4)", wfName, sid, dep) |
122 | | - } |
123 | | - } |
124 | | - } |
125 | | - } |
126 | | - return nil |
| 7 | + return spec.ResolveReferences(g) |
127 | 8 | } |
0 commit comments