Refactor.pm

filter: Statementstmt 39.8% 73 / 183 Branchbran 10.3% 6 / 58 Conditioncond 15.0% 9 / 60 MC/DCmcdc 5.5% 2 / 36 Subroutinesub 94.4% 17 / 18 Podpod 100% 3 / 3 totaltotal 30.7% 110 / 358 CC 57 SCAR 53.5
CC
57
Cov
29%
_fix_issues
420.0
refactor_until_clean
330.8
refactor_issues
83.5
CRAP 211.1
↑ Summary Refactor/Options.pm »
1 package Claude::Agent::Code::Refactor;
2  
3 4 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 our @EXPORT_OK = qw(refactor refactor_issues refactor_until_clean);
9  
10 4 ▶use Claude::Agent qw(query);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Claude::Agent::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Claude::Agent::Code::Review qw(review_files);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Claude::Agent::Code::Refactor::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Claude::Agent::Code::Refactor::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 4 our $VERSION = '0.03';
20  
21 2 ▶async sub refactor {
Subroutine refactor: called CC=6 SCAR=21.0
Pod documented
22 2 my (%args) = @_;
23  
24 2 ▶ my $target = $args{target} // die "refactor() requires 'target' argument";
Condition100%2/2
$args{"target"} // die("refactor() requires 'target' argument")
l!l
11
MC/DC100%1/1
$args{"target"} // die("refactor() requires 'target' argument")
$args{"target"}
Truth table100%2/2
A
A= $args{"target"}
Aresult
11
00
25 1 ▶ my $options = $args{options} // Claude::Agent::Code::Refactor::Options->new();
Condition33%1/3
$args{"options"} // "Claude::Agent::Code::Refactor::Options"->new
l!l&&r!l&&!r
010
MC/DC0%0/2
$args{"options"} // "Claude::Agent::Code::Refactor::Options"->new
$args{"options"} "Claude::Agent::Code::Refactor::Options"->new
Truth table33%1/3
A // B
A= $args{"options"}B= "Claude::Agent::Code::Refactor::Options"->new
ABresult
1X1
011
000
26 1 ▶ my $loop = $args{loop} // IO::Async::Loop->new;
Condition33%1/3
$args{"loop"} // "IO::Async::Loop"->new
l!l&&r!l&&!r
010
MC/DC0%0/2
$args{"loop"} // "IO::Async::Loop"->new
$args{"loop"} "IO::Async::Loop"->new
Truth table33%1/3
A // B
A= $args{"loop"}B= "IO::Async::Loop"->new
ABresult
1X1
011
000
27  
28 1 my @paths;
29 1 ▶ if (-d $target || -f $target) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $target or -f $target) { } 0 1 1
Condition33%1/3
-d $target or -f $target
l!l&&r!l&&!r
001
MC/DC0%0/2
-d $target or -f $target
-d $target -f $target
Truth table33%1/3
A or B
A= -d $targetB= -f $target
ABresult
1X1
011
000
30 0 @paths = ($target);
31 }
32 else {
33 1 die "Unknown target type: $target (must be file or directory)";
34 }
35  
36 0 return await refactor_until_clean(
37 paths => \@paths,
38 options => $options,
39 loop => $loop,
40 );
41 4 }
42  
43 1 ▶async sub refactor_until_clean {
Subroutine refactor_until_clean: called CC=19 SCAR=58.0
Pod documented
44 1 my (%args) = @_;
45  
46 1 ▶ my $paths = $args{paths} // die "refactor_until_clean() requires 'paths' argument";
Condition50%1/2
$args{"paths"} // die("refactor_until_clean() requires 'paths' argument")
l!l
01
MC/DC0%0/1
$args{"paths"} // die("refactor_until_clean() requires 'paths' argument")
$args{"paths"}
Truth table50%1/2
A
A= $args{"paths"}
Aresult
11
00
47 0 ▶ my $options = $args{options} // Claude::Agent::Code::Refactor::Options->new();
Condition0%0/3
$args{"options"} // "Claude::Agent::Code::Refactor::Options"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"options"} // "Claude::Agent::Code::Refactor::Options"->new
$args{"options"} "Claude::Agent::Code::Refactor::Options"->new
Truth table0%0/3
A // B
A= $args{"options"}B= "Claude::Agent::Code::Refactor::Options"->new
ABresult
1X1
011
000
48 0 ▶ my $loop = $args{loop} // IO::Async::Loop->new;
Condition0%0/3
$args{"loop"} // "IO::Async::Loop"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"loop"} // "IO::Async::Loop"->new
$args{"loop"} "IO::Async::Loop"->new
Truth table0%0/3
A // B
A= $args{"loop"}B= "IO::Async::Loop"->new
ABresult
1X1
011
000
49  
50 0 my $start_time = time();
51  
52 0 my $result = Claude::Agent::Code::Refactor::Result->new();
53  
54 0 my $review_options = $options->to_review_options;
55  
56 0 for my $iteration (1 .. $options->max_iterations) {
57 0 my $report = await review_files(
58 paths => $paths,
59 options => $review_options,
60 loop => $loop,
61 );
62  
63 0 ▶ if (!defined $report) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $report) 0 0 0
64 0 $result->error('Review failed to return a report');
65 0 last;
66 }
67 0 ▶ my @issues = @{$report->issues // []};
Condition0%0/2
$report->issues // []
l!l
00
MC/DC0%0/1
$report->issues // []
$report->issues
Truth table0%0/2
A
A= $report->issues
Aresult
11
00
68 0 my $issues_found = scalar @issues;
69  
70 0 ▶ if ($iteration == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iteration == 1) 0 0 0
71 0 $result->initial_issues($issues_found);
72 }
73  
74 0 ▶ if ($issues_found == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($issues_found == 0) 0 0 0
75 0 $result->success(1);
76 0 $result->final_issues(0);
77 0 $result->final_report($report);
78 0 $result->add_iteration(
79 issues_found => 0,
80 issues_fixed => 0,
81 files_modified => [],
82 );
83 0 last;
84 }
85  
86 0 my $fix_result;
87 0 ▶ if ($options->dry_run) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->dry_run) { } 0 0 0
88 0 $fix_result = {
89 issues_fixed => 0,
90 files_modified => [],
91 };
92 }
93 else {
94 0 $fix_result = await _fix_issues(\@issues, $options, $loop);
95 }
96  
97 $result->add_iteration(
98 issues_found => $issues_found,
99 issues_fixed => $fix_result->{issues_fixed} // 0,
100 0 ▶ files_modified => $fix_result->{files_modified} // [],
Condition0%0/4
$fix_result->{"issues_fixed"} // 0
l!l
00
$fix_result->{"files_modified"} // []
l!l
00
MC/DC0%0/1
$fix_result->{"issues_fixed"} // 0
$fix_result->{"issues_fixed"}
MC/DC0%0/1
$fix_result->{"files_modified"} // []
$fix_result->{"files_modified"}
Truth table0%0/2
A
A= $fix_result->{"issues_fixed"}
Aresult
11
00
Truth table0%0/2
A
A= $fix_result->{"files_modified"}
Aresult
11
00
101 );
102  
103 0 ▶ if ($iteration > 1 && $fix_result->{issues_fixed} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iteration > 1 and $fix_result->{"issues_fixed"} == 0) 0 0 0
Condition0%0/3
$iteration > 1 and $fix_result->{"issues_fixed"} == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$iteration > 1 and $fix_result->{"issues_fixed"} == 0
$iteration > 1 $fix_result->{"issues_fixed"} == 0
Truth table0%0/3
A and B
A= $iteration > 1B= $fix_result->{"issues_fixed"} == 0
ABresult
0X0
100
111
104 0 $result->final_issues($issues_found);
105 0 $result->final_report($report);
106  
107 0 ▶ if ($fix_result->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fix_result->{"error"}) 0 0 0
108 0 $result->error($fix_result->{error});
109 }
110 0 last;
111 }
112  
113 0 $result->final_report($report);
114  
115 }
116  
117 0 ▶ if (!$result->is_clean && !$result->has_error) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result->is_clean or $result->has_error) 0 0 0
Condition0%0/3
$result->is_clean or $result->has_error
l!l&&r!l&&!r
000
MC/DC0%0/2
$result->is_clean or $result->has_error
$result->is_clean $result->has_error
Truth table0%0/3
A or B
A= $result->is_cleanB= $result->has_error
ABresult
1X1
011
000
118 0 my $final_report = await review_files(
119 paths => $paths,
120 options => $review_options,
121 loop => $loop,
122 );
123 0 ▶ if (!defined $final_report) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $final_report) { } 0 0 0
124 0 $result->error('Final review failed to return a report');
125 } else {
126 0 ▶ $result->final_issues(scalar @{$final_report->issues // []});
Condition0%0/2
$final_report->issues // []
l!l
00
MC/DC0%0/1
$final_report->issues // []
$final_report->issues
Truth table0%0/2
A
A= $final_report->issues
Aresult
11
00
127 0 $result->final_report($final_report);
128 0 $result->success(($result->final_issues == 0));
129 }
130 }
131  
132 0 $result->duration_ms(int((time() - $start_time) * 1000));
133  
134 0 return $result;
135 4 }
136  
137 1 ▶async sub refactor_issues {
Subroutine refactor_issues: called CC=10 SCAR=44.2
Pod documented
138 1 my (%args) = @_;
139  
140 1 ▶ my $issues = $args{issues} // die "refactor_issues() requires 'issues' argument";
Condition50%1/2
$args{"issues"} // die("refactor_issues() requires 'issues' argument")
l!l
01
MC/DC0%0/1
$args{"issues"} // die("refactor_issues() requires 'issues' argument")
$args{"issues"}
Truth table50%1/2
A
A= $args{"issues"}
Aresult
11
00
141 0 ▶ my $options = $args{options} // Claude::Agent::Code::Refactor::Options->new();
Condition0%0/3
$args{"options"} // "Claude::Agent::Code::Refactor::Options"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"options"} // "Claude::Agent::Code::Refactor::Options"->new
$args{"options"} "Claude::Agent::Code::Refactor::Options"->new
Truth table0%0/3
A // B
A= $args{"options"}B= "Claude::Agent::Code::Refactor::Options"->new
ABresult
1X1
011
000
142 0 ▶ my $loop = $args{loop} // IO::Async::Loop->new;
Condition0%0/3
$args{"loop"} // "IO::Async::Loop"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"loop"} // "IO::Async::Loop"->new
$args{"loop"} "IO::Async::Loop"->new
Truth table0%0/3
A // B
A= $args{"loop"}B= "IO::Async::Loop"->new
ABresult
1X1
011
000
143  
144 0 my $start_time = time();
145  
146 0 my $result = Claude::Agent::Code::Refactor::Result->new(
147 initial_issues => scalar(@$issues),
148 );
149  
150 0 ▶ if (@$issues == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$issues == 0) 0 0 0
151 0 $result->success(1);
152 0 $result->final_issues(0);
153 0 $result->duration_ms(int((time() - $start_time) * 1000));
154 0 return $result;
155 }
156  
157 0 my $fix_result = await _fix_issues($issues, $options, $loop);
158  
159 $result->add_iteration(
160 issues_found => scalar(@$issues),
161 issues_fixed => $fix_result->{issues_fixed} // 0,
162 0 ▶ files_modified => $fix_result->{files_modified} // [],
Condition0%0/4
$fix_result->{"issues_fixed"} // 0
l!l
00
$fix_result->{"files_modified"} // []
l!l
00
MC/DC0%0/1
$fix_result->{"issues_fixed"} // 0
$fix_result->{"issues_fixed"}
MC/DC0%0/1
$fix_result->{"files_modified"} // []
$fix_result->{"files_modified"}
Truth table0%0/2
A
A= $fix_result->{"issues_fixed"}
Aresult
11
00
Truth table0%0/2
A
A= $fix_result->{"files_modified"}
Aresult
11
00
163 );
164  
165 0 ▶ my $remaining = scalar(@$issues) - ($fix_result->{issues_fixed} // 0);
Condition0%0/2
$fix_result->{"issues_fixed"} // 0
l!l
00
MC/DC0%0/1
$fix_result->{"issues_fixed"} // 0
$fix_result->{"issues_fixed"}
Truth table0%0/2
A
A= $fix_result->{"issues_fixed"}
Aresult
11
00
166 0 ▶ $result->final_issues($remaining > 0 ? $remaining : 0);
Branch: 1 branch
BranchTrueFalseTotal
$remaining > 0 ? : 0 0 0
167 0 $result->success(($result->final_issues == 0));
168 0 $result->duration_ms(int((time() - $start_time) * 1000));
169  
170 0 ▶ if ($fix_result->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fix_result->{"error"}) 0 0 0
171 0 $result->error($fix_result->{error});
172 }
173  
174 0 return $result;
175 4 }
176  
177 0 ▶async sub _fix_issues {
Subroutine _fix_issues: not called CC=20 SCAR=60.4
178 0 my ($issues, $options, $loop) = @_;
179  
180 0 ▶ return { issues_fixed => 0, files_modified => [] } unless @$issues;
Branch: 1 branch
BranchTrueFalseTotal
unless @$issues 0 0 0
181  
182 0 my %severity_rank = (critical => 5, high => 4, medium => 3, low => 2, info => 1);
183 my @sorted = sort {
184 0 ▶ ($severity_rank{$b->severity} // 0) <=> ($severity_rank{$a->severity} // 0)
Condition0%0/4
$severity_rank{$b->severity} // 0
l!l
00
$severity_rank{$a->severity} // 0
l!l
00
MC/DC0%0/1
$severity_rank{$b->severity} // 0
$severity_rank{$b->severity}
MC/DC0%0/1
$severity_rank{$a->severity} // 0
$severity_rank{$a->severity}
Truth table0%0/2
A
A= $severity_rank{$b->severity}
Aresult
11
00
Truth table0%0/2
A
A= $severity_rank{$a->severity}
Aresult
11
00
185 } @$issues;
186  
187 0 my $prompt = _build_fix_prompt(\@sorted);
188  
189 0 my %claude_args = (
190 allowed_tools => ['Read', 'Edit', 'Glob', 'Grep'],
191 permission_mode => $options->permission_mode,
192 system_prompt => _get_fix_system_prompt(),
193 max_turns => $options->max_turns_per_fix,
194 );
195 0 ▶ $claude_args{model} = $options->model if defined $options->model;
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->model 0 0 0
196 0 ▶ $claude_args{effort} = $options->effort if $options->has_effort;
Branch: 1 branch
BranchTrueFalseTotal
if $options->has_effort 0 0 0
197  
198 0 my $claude_options = Claude::Agent::Options->new(%claude_args);
199  
200 0 my $iter = query(
201 prompt => $prompt,
202 options => $claude_options,
203 loop => $loop,
204 );
205  
206 0 my %files_modified;
207 0 my $edit_count = 0;
208  
209 0 my $max_iterations = 1000;
210 0 my $iteration_count = 0;
211 0 my $start_time = time();
212 0 my $timeout_seconds = 300;
213  
214 0 my $timeout_result;
215 0 eval {
216 0 ▶ while (my $msg = await $iter->next_async) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = <custom op await>) 0 0 0
217 0 ▶ if ((time() - $start_time) > $timeout_seconds) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Claude::Agent::Code::Refactor::time() - $start_time > $timeout_seconds) 0 0 0
218 0 $iter->close;
219 0 $timeout_result = { issues_fixed => $edit_count, files_modified => [keys %files_modified], error => 'Fix operation timed out after 5 minutes' };
220 0 last;
221 }
222 0 $iteration_count++;
223 0 ▶ last if $iteration_count >= $max_iterations;
Branch: 1 branch
BranchTrueFalseTotal
if $iteration_count >= $max_iterations 0 0 0
224  
225 0 ▶ if ($msg->isa('Claude::Agent::Message::Assistant')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($msg->isa('Claude::Agent::Message::Assistant')) { } 0 0 0
elsif ($msg->isa('Claude::Agent::Message::Result')) { } 0 0 0
226 0 for my $block (@{$msg->content_blocks}) {
227 0 ▶ if ($block->isa('Claude::Agent::Content::ToolUse')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block->isa("Claude::Agent::Content::ToolUse")) 0 0 0
228 0 ▶ if ($block->name eq 'Edit') {
Branch: 1 branch
BranchTrueFalseTotal
if ($block->name eq "Edit") 0 0 0
229 0 $edit_count++;
230 0 my $input = $block->input;
231 0 ▶ if ($input && ref($input) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($input and ref $input eq "HASH") 0 0 0
Condition0%0/3
$input and ref $input eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$input and ref $input eq "HASH"
$input ref $input eq "HASH"
Truth table0%0/3
A and B
A= $inputB= ref $input eq "HASH"
ABresult
0X0
100
111
232 0 my $file = $input->{file_path};
233 0 ▶ $files_modified{$file} = 1 if defined $file && length $file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $file and length $file 0 0 0
Condition0%0/3
defined $file and length $file
!ll&&!rl&&r
000
MC/DC0%0/2
defined $file and length $file
defined $file length $file
Truth table0%0/3
A and B
A= defined $fileB= length $file
ABresult
0X0
100
111
234 }
235 }
236 }
237 }
238 }
239 elsif ($msg->isa('Claude::Agent::Message::Result')) {
240 0 last;
241 }
242 }
243 };
244  
245 0 $iter->close;
246  
247 0 ▶ return $timeout_result if $timeout_result;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout_result 0 0 0
248 0 my $error = $@;
249 0 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
250 0 warn "Fix error: $error";
251 0 my $safe_error = "An error occurred during fix operation";
252 0 return { issues_fixed => 0, files_modified => [], error => $safe_error };
253 }
254  
255 return {
256 0 issues_fixed => $edit_count,
257 files_modified => [keys %files_modified],
258 };
259 4 }
260  
261 1 ▶sub _build_fix_prompt {
Subroutine _build_fix_prompt: called CC=6 SCAR=18.0
262 1 my ($issues) = @_;
263  
264 1 my $prompt = "Fix the following issues found in code review:\n\n";
265  
266 1 for my $issue (@$issues) {
267 2 $prompt .= sprintf("## Issue in %s (line %d)\n", $issue->file, $issue->line);
268 2 $prompt .= "Severity: " . $issue->severity . "\n";
269 2 $prompt .= "Category: " . $issue->category . "\n";
270 2 $prompt .= "Description: " . $issue->description . "\n";
271  
272 2 ▶ if ($issue->has_explanation) {
Branch: 1 branch
BranchTrueFalseTotal
if ($issue->has_explanation) 0 2 2
273 0 $prompt .= "Explanation: " . $issue->explanation . "\n";
274 }
275  
276 2 ▶ if ($issue->has_suggestion) {
Branch: 1 branch
BranchTrueFalseTotal
if ($issue->has_suggestion) 1 1 2
277 1 $prompt .= "Suggestion: " . $issue->suggestion . "\n";
278 }
279  
280 2 ▶ if ($issue->has_code_before && $issue->has_code_after) {
Branch: 1 branch
BranchTrueFalseTotal
if ($issue->has_code_before and $issue->has_code_after) 1 1 2
Condition66%2/3
$issue->has_code_before and $issue->has_code_after
!ll&&!rl&&r
101
MC/DC50%1/2
$issue->has_code_before and $issue->has_code_after
$issue->has_code_before $issue->has_code_after
Truth table66%2/3
A and B
A= $issue->has_code_beforeB= $issue->has_code_after
ABresult
0X0
100
111
281 1 $prompt .= "Change from:\n```\n" . $issue->code_before . "\n```\n";
282 1 $prompt .= "To:\n```\n" . $issue->code_after . "\n```\n";
283 }
284  
285 2 $prompt .= "\n";
286 }
287  
288 1 $prompt .= "Please fix each issue by reading the file and using the Edit tool.\n";
289  
290 1 return $prompt;
291 }
292  
293 1 ▶sub _get_fix_system_prompt {
Subroutine _get_fix_system_prompt: called CC=1 SCAR=0.0
294 1 return <<'END';
295 You are an expert code fixer. You will be given code issues found by a code review.
296  
297 Your task:
298 1. Read each issue carefully (file, line, description, suggestion)
299 2. Use the Read tool to examine the actual code context
300 3. Use the Edit tool to fix each issue
301 4. Make minimal, focused changes - only fix the reported issue
302 5. Do NOT introduce new issues or change unrelated code
303  
304 For each fix:
305 - Read the file first to understand context
306 - Apply the smallest change that resolves the issue
307 - If code_after is provided, use it as a guide
308 - If you cannot fix an issue safely, skip it and explain why
309  
310 Fix issues in order of severity (critical first, then high, medium, low).
311 END
312 }
313  
314  
315 4 1;
316  
317 __END__
↑ Summary Refactor/Options.pm »