Agent/Compiler.pm

filter: Statementstmt 97.7% 87 / 89 Branchbran 67.0% 59 / 88 Conditioncond 33.3% 5 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 2 totaltotal 75.5% 161 / 213 CC 55 SCAR 27.8
CC
55
Cov
79%
_compile_agent
26.2
_finalize_graph
16.1
compile
10.7
CRAP 16.1
« Agent/Engine.pm ↑ Summary Agent/Hook.pm »
1 package Sim::Agent::Compiler;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 sub new
7 2 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
8 2 my ($class) = @_;
9 2 bless {}, $class;
10 }
11  
12 sub compile
13 2 ▶{
Subroutine compile: called CC=9 SCAR=23.7
Pod undocumented
14 2 my ($self, $ast) = @_;
15  
16 2 ▶ die "Root must be (system ...)"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ast eq "ARRAY" and @$ast and $ast->[0] eq "system" 0 2 2
Condition33%2/6
ref $ast eq "ARRAY" and @$ast and $ast->[0] eq "system"
!ll&&!rl&&r
002
ref $ast eq "ARRAY" and @$ast
!ll&&!rl&&r
002
MC/DC0%0/3
ref $ast eq "ARRAY" and @$ast and $ast->[0] eq "system"
ref $ast eq "ARRAY" @$ast $ast->[0] eq "system"
Truth table25%1/4
A and B and C
A= ref $ast eq "ARRAY"B= @$astC= $ast->[0] eq "system"
ABCresult
0XX0
10X0
1100
1111
17 unless ref($ast) eq 'ARRAY'
18 && @$ast
19 && $ast->[0] eq 'system';
20  
21 2 my $graph =
22 {
23 entry => undef,
24 chief => undef,
25 limits => _default_limits(),
26 agents => {},
27 };
28  
29 # Process system body
30 2 for my $form (@$ast[1 .. $#$ast])
31 {
32  
33 14 ▶ die "Invalid form in system"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $form eq "ARRAY" 0 14 14
34 unless ref($form) eq 'ARRAY';
35  
36 14 my $keyword = $form->[0];
37  
38 14 ▶ if ($keyword eq 'limits')
Branch: 3 branches
BranchTrueFalseTotal
if ($keyword eq 'limits') { } 1 13 14
elsif ($keyword eq 'entry') { } 2 11 13
elsif ($keyword eq 'agent') { } 11 0 11
39 {
40 1 _compile_limits($graph, $form);
41 }
42 elsif ($keyword eq 'entry')
43 {
44 2 _compile_entry($graph, $form);
45 }
46 elsif ($keyword eq 'agent')
47 {
48 11 _compile_agent($graph, $form);
49 }
50 else
51 {
52 0 die "Unknown system keyword: $keyword";
53 }
54 }
55  
56 2 _finalize_graph($graph);
57  
58 2 return $graph;
59 }
60  
61 # -------------------------------------------------------------------
62 # Internal Helpers (Private)
63 # -------------------------------------------------------------------
64  
65 sub _default_limits
66 2 ▶{
Subroutine _default_limits: called CC=1 SCAR=0.0
67 return
68 {
69 2 max_depth => 5,
70 max_agents => 50,
71 max_iterations => 100,
72 max_self_cycles => 5,
73 max_pingpong => 4,
74 };
75 }
76  
77 sub _compile_limits
78 1 ▶{
Subroutine _compile_limits: called CC=7 SCAR=22.5
79 1 my ($graph, $form) = @_;
80  
81 1 for my $item (@$form[1 .. $#$form])
82 {
83  
84 3 ▶ die "Invalid limit entry"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $item eq "ARRAY" and @$item == 2 0 3 3
Condition33%1/3
ref $item eq "ARRAY" and @$item == 2
!ll&&!rl&&r
003
MC/DC0%0/2
ref $item eq "ARRAY" and @$item == 2
ref $item eq "ARRAY" @$item == 2
Truth table33%1/3
A and B
A= ref $item eq "ARRAY"B= @$item == 2
ABresult
0X0
100
111
85 unless ref($item) eq 'ARRAY'
86 && @$item == 2;
87  
88 3 my ($key, $value) = @$item;
89  
90 die "Unknown limit: $key"
91 3 ▶ unless exists $graph->{limits}->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $graph->{"limits"}{$key} 0 3 3
92  
93 3 ▶ die "Limit must be integer"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and $value =~ /^\d+$/ 0 3 3
Condition33%1/3
defined $value and $value =~ /^\d+$/
!ll&&!rl&&r
003
MC/DC0%0/2
defined $value and $value =~ /^\d+$/
defined $value $value =~ /^\d+$/
Truth table33%1/3
A and B
A= defined $valueB= $value =~ /^\d+$/
ABresult
0X0
100
111
94 unless defined $value && $value =~ /^\d+$/;
95  
96 3 $graph->{limits}->{$key} = int($value);
97 }
98 }
99  
100 sub _compile_entry
101 2 ▶{
Subroutine _compile_entry: called CC=3 SCAR=11.4
102 2 my ($graph, $form) = @_;
103  
104 2 ▶ die "Entry form must be (entry AgentName)"
Branch: 1 branch
BranchTrueFalseTotal
unless @$form == 2 0 2 2
105 unless @$form == 2;
106  
107 die "Multiple entry declarations"
108 2 ▶ if defined $graph->{entry};
Branch: 1 branch
BranchTrueFalseTotal
if defined $graph->{"entry"} 0 2 2
109  
110 2 $graph->{entry} = $form->[1];
111 }
112  
113 sub _compile_agent
114 11 ▶{
Subroutine _compile_agent: called CC=23 SCAR=32.7
115 11 my ($graph, $form) = @_;
116  
117 11 ▶ die "Agent form too short"
Branch: 1 branch
BranchTrueFalseTotal
unless @$form >= 3 0 11 11
118 unless @$form >= 3;
119  
120 11 my $name = $form->[1];
121  
122 die "Duplicate agent: $name"
123 11 ▶ if exists $graph->{agents}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $graph->{"agents"}{$name} 0 11 11
124  
125 11 my $agent =
126 {
127 name => $name,
128 role => undef,
129 model => undef,
130 prompt_file => undef,
131 selfcrit_file => undef,
132 revision_file => undef,
133 parse_file => undef,
134 send_to => undef,
135 on_ok => [],
136 on_fail => [],
137 wait_for => [],
138 };
139  
140 11 for my $attr (@$form[2 .. $#$form]) {
141  
142 51 ▶ die "Invalid agent attribute"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $attr eq "ARRAY" 0 51 51
143 unless ref($attr) eq 'ARRAY';
144  
145 51 my $key = $attr->[0];
146  
147 51 ▶ if ($key eq 'role')
Branch: 10 branches
BranchTrueFalseTotal
if ($key eq 'role') { } 11 40 51
elsif ($key eq 'model') { } 5 35 40
elsif ($key eq 'prompt-file') { } 5 30 35
elsif ($key eq 'self-critic-file') { } 5 25 30
elsif ($key eq 'revision-file') { } 5 20 25
elsif ($key eq 'parse-file') { } 6 14 20
elsif ($key eq 'send-to') { } 5 9 14
elsif ($key eq 'on-ok') { } 4 5 9
elsif ($key eq 'on-fail') { } 4 1 5
elsif ($key eq 'wait-for') { } 1 0 1
148 {
149 11 ▶ die "Role form invalid" unless @$attr == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @$attr == 2 0 11 11
150 11 $agent->{role} = $attr->[1];
151 }
152 elsif ($key eq 'model')
153 {
154 5 ▶ die "Model form invalid" unless @$attr == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @$attr == 2 0 5 5
155 5 $agent->{model} = $attr->[1];
156 }
157 elsif ($key eq 'prompt-file')
158 {
159 5 ▶ die "prompt-file invalid" unless @$attr == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @$attr == 2 0 5 5
160 5 $agent->{prompt_file} = $attr->[1];
161 }
162 elsif ($key eq 'self-critic-file')
163 {
164 5 ▶ die "self-critic-file invalid" unless @$attr == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @$attr == 2 0 5 5
165 5 $agent->{selfcrit_file} = $attr->[1];
166 }
167 elsif ($key eq 'revision-file')
168 {
169 5 ▶ die "revision-file invalid" unless @$attr == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @$attr == 2 0 5 5
170 5 $agent->{revision_file} = $attr->[1];
171 }
172 elsif ($key eq 'parse-file')
173 {
174 6 ▶ die "parse-file invalid" unless @$attr == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @$attr == 2 0 6 6
175 6 $agent->{parse_file} = $attr->[1];
176 }
177 elsif ($key eq 'send-to')
178 {
179 5 ▶ die "send-to must have exactly one target"
Branch: 1 branch
BranchTrueFalseTotal
unless @$attr == 2 0 5 5
180 unless @$attr == 2;
181 5 $agent->{send_to} = $attr->[1];
182 }
183 elsif ($key eq 'on-ok')
184 {
185 4 $agent->{on_ok} = _extract_list($attr);
186 }
187 elsif ($key eq 'on-fail')
188 {
189 4 $agent->{on_fail} = _extract_list($attr);
190 }
191 elsif ($key eq 'wait-for')
192 {
193 1 $agent->{wait_for} = _extract_list($attr);
194 }
195 else
196 {
197 0 die "Unknown agent attribute: $key";
198 }
199 }
200  
201 die "Agent $name missing role"
202 11 ▶ unless $agent->{role};
Branch: 1 branch
BranchTrueFalseTotal
unless $agent->{"role"} 0 11 11
203  
204 11 $graph->{agents}->{$name} = $agent;
205 }
206  
207 sub _extract_list
208 9 ▶{
Subroutine _extract_list: called CC=3 SCAR=12.5
209 9 my ($form) = @_;
210  
211 9 ▶ die "Expected list form"
Branch: 1 branch
BranchTrueFalseTotal
unless @$form == 2 and ref $form->[1] eq "ARRAY" 0 9 9
Condition33%1/3
@$form == 2 and ref $form->[1] eq "ARRAY"
!ll&&!rl&&r
009
MC/DC0%0/2
@$form == 2 and ref $form->[1] eq "ARRAY"
@$form == 2 ref $form->[1] eq "ARRAY"
Truth table33%1/3
A and B
A= @$form == 2B= ref $form->[1] eq "ARRAY"
ABresult
0X0
100
111
212 unless @$form == 2
213 && ref($form->[1]) eq 'ARRAY';
214  
215 9 return $form->[1];
216 }
217  
218 sub _finalize_graph
219 2 ▶{
Subroutine _finalize_graph: called CC=15 SCAR=27.8
220 2 my ($graph) = @_;
221  
222 die "Missing entry"
223 2 ▶ unless defined $graph->{entry};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $graph->{"entry"} 0 2 2
224  
225 die "Entry agent not defined"
226 2 ▶ unless exists $graph->{agents}->{ $graph->{entry} };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $graph->{"agents"}{$graph->{"entry"}} 0 2 2
227  
228 2 my $chief_count = 0;
229  
230 2 for my $agent (values %{ $graph->{agents} }) {
231  
232 # Count chief
233 11 ▶ if ($agent->{role} eq 'chief')
Branch: 1 branch
BranchTrueFalseTotal
if ($agent->{"role"} eq "chief") 2 9 11
234 {
235 2 $chief_count++;
236 2 $graph->{chief} = $agent->{name};
237 }
238  
239 die "Invalid role: $agent->{role}"
240 11 ▶ unless $agent->{role} =~ /^(worker|critic|chief)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $agent->{"role"} =~ /^(worker|critic|chief)$/ 0 11 11
241  
242 11 ▶ if ($agent->{role} eq 'worker')
Branch: 1 branch
BranchTrueFalseTotal
if ($agent->{"role"} eq "worker") 5 6 11
243 {
244 die "Worker $agent->{name} missing send-to"
245 5 ▶ unless defined $agent->{send_to};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $agent->{"send_to"} 0 5 5
246 }
247  
248 11 ▶ if ($agent->{role} eq 'critic')
Branch: 1 branch
BranchTrueFalseTotal
if ($agent->{"role"} eq "critic") 4 7 11
249 {
250 die "Critic $agent->{name} missing on-ok"
251 4 ▶ unless @{ $agent->{on_ok} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$agent->{"on_ok"};} 0 4 4
252 die "Critic $agent->{name} missing on-fail"
253 4 ▶ unless @{ $agent->{on_fail} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$agent->{"on_fail"};} 0 4 4
254 }
255  
256 11 for my $target (
257 11 @{ $agent->{on_ok} },
258 11 @{ $agent->{on_fail} },
259 11 ▶ @{ $agent->{wait_for} },
Branch: 1 branch
BranchTrueFalseTotal
defined $agent->{'send_to'} ? : 5 6 11
260 (defined $agent->{send_to} ? ($agent->{send_to}) : ())
261 )
262 {
263 die "Undefined agent reference: $target"
264 16 ▶ unless exists $graph->{agents}->{$target};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $graph->{"agents"}{$target} 0 16 16
265 }
266 }
267  
268 2 ▶ die "Exactly one chief required"
Branch: 1 branch
BranchTrueFalseTotal
unless $chief_count == 1 0 2 2
269 unless $chief_count == 1;
270 }
271  
272 3 1;
273  
274 =pod
275  
276 =head1 NAME
277  
278 Sim::Agent::Compiler - Compiles the S-expression AST into a validated execution graph
279  
280 =head1 DESCRIPTION
281  
282 Takes the parsed AST and produces a normalized graph structure: limits, agents, routing, and reference validation.
283  
284 See L<Sim::Agent> for DSL details.
285  
286 =head1 AUTHOR
287  
288 Gian Luca Brunetti (2026), gianluca.brunetti@gmail.com
289  
290 =head1 LICENSE
291  
292 The GNU General Public License v3.0
293  
294 =cut
295  
296  
« Agent/Engine.pm ↑ Summary Agent/Hook.pm »