Saisons/Adapter/Aider.pm

filter: Statementstmt 80.1% 101 / 126 Branchbran 73.0% 38 / 52 Conditioncond 73.3% 22 / 30 MC/DCmcdc 58.8% 10 / 17 Subroutinesub 83.3% 15 / 18 Podpod 0.0% 0 / 9 totaltotal 73.8% 186 / 252 CC 43 SCAR 22.2
CC
43
Cov
77%
load_messages
15.2
launch
12.0
delete_session
12.0
CRAP 9.2
« Saisons/Adapter/Codex.pm ↑ Summary Saisons.pm »
1 package App::Saisons::Adapter::Aider;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use App::Saisons ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use POSIX qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use App::Saisons::Launcher ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶sub new { bless {}, shift }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
8 1 ▶sub name { 'Aider' }
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
9 2 ▶sub tag { "a Aid" }
Subroutine tag: called CC=1 SCAR=0.0
Pod undocumented
10 1 ▶sub tag_color { 'bold green' }
Subroutine tag_color: called CC=1 SCAR=0.0
Pod undocumented
11 1 ▶sub find_sessions {
Subroutine find_sessions: called CC=4 SCAR=14.9
Pod undocumented
12 1 my ($self) = @_;
13 1 my @search_dirs;
14 1 ▶ if (my $env = $ENV{SAISONS_SEARCH_DIRS}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $env = $ENV{'SAISONS_SEARCH_DIRS'}) { } 1 0 1
15 1 @search_dirs = split /:/, $env;
16 }
17 else {
18 0 my $home = $ENV{HOME};
19 0 @search_dirs = grep { -d $_ }
20 0 map { "$home/$_" } qw(projects dev src work code),
21 '/projects';
22 }
23 1 my @history_files = _find_history_files(\@search_dirs, 1);
24 1 my $home_file = "$ENV{HOME}/.aider.chat.history.md";
25 1 ▶ push @history_files, $home_file if -f $home_file;
Branch: 1 branch
BranchTrueFalseTotal
if -f $home_file 0 1 1
26 1 my @sessions;
27 1 for my $file (@history_files) {
28 1 (my $cwd = $file) =~ s|/\.aider\.chat\.history\.md$||;
29 1 push @sessions, _parse_history_file($file, $cwd, $self);
30 }
31 1 return @sessions;
32 }
33 1 ▶sub _find_history_files {
Subroutine _find_history_files: called CC=11 SCAR=24.7
34 1 my ($roots, $maxdepth) = @_;
35 1 my @result;
36 1 my @queue = map { [$_, 0] } @$roots;
37 1 ▶ while (my $item = shift @queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = shift @queue) 2 1 3
38 2 my ($dir, $depth) = @$item;
39 2 ▶ opendir my $dh, $dir or next;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or next 2 0 2
40 2 for my $entry (readdir $dh) {
41 6 ▶ next if $entry eq '.' || $entry eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $entry eq "." or $entry eq ".." 4 2 6
Condition100%3/3
$entry eq "." or $entry eq ".."
l!l&&r!l&&!r
222
MC/DC100%2/2
$entry eq "." or $entry eq ".."
$entry eq "." $entry eq ".."
Truth table100%3/3
A or B
A= $entry eq "."B= $entry eq ".."
ABresult
1X1
011
000
42 2 my $path = "$dir/$entry";
43 2 ▶ if (-f $path && $entry eq '.aider.chat.history.md') {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $path and $entry eq '.aider.chat.history.md') { } 1 1 2
elsif (-d $path and not -l $path and $depth < $maxdepth) { } 1 0 1
Condition66%2/3
-f $path and $entry eq ".aider.chat.history.md"
!ll&&!rl&&r
101
MC/DC50%1/2
-f $path and $entry eq ".aider.chat.history.md"
-f $path $entry eq ".aider.chat.history.md"
Truth table66%2/3
A and B
A= -f $pathB= $entry eq ".aider.chat.history.md"
ABresult
0X0
100
111
44 1 push @result, $path;
45 }
46 ▶ elsif (-d $path && !-l $path && $depth < $maxdepth) {
Condition33%2/6
-d $path and not -l $path and $depth < $maxdepth
!ll&&!rl&&r
001
-d $path and not -l $path
!ll&&!rl&&r
001
MC/DC0%0/3
-d $path and not -l $path and $depth < $maxdepth
-d $path not -l $path $depth < $maxdepth
Truth table25%1/4
A and not(B) and C
A= -d $pathB= not -l $pathC= $depth < $maxdepth
ABCresult
0XX0
10X0
1100
1111
47 1 push @queue, [$path, $depth + 1];
48 }
49 }
50 2 closedir $dh;
51 }
52 1 return @result;
53 }
54 0 ▶sub running {
Subroutine running: not called CC=1 SCAR=6.9
Pod undocumented
55 0 return ();
56 }
57 0 ▶sub launch {
Subroutine launch: not called CC=3 SCAR=24.8
Pod undocumented
58 0 my ($self, $sessions, $launcher) = @_;
59 0 my %by_cwd;
60 0 push @{ $by_cwd{$_->{cwd}} }, $_ for @$sessions;
61 0 for my $cwd (keys %by_cwd) {
62 0 my $cmd = "cd \Q$cwd\E && aider --restore-chat-history";
63 0 App::Saisons::Launcher::launch_cmd($cmd, $cwd, $launcher, 'aider');
64 }
65 }
66 0 ▶sub delete_session {
Subroutine delete_session: not called CC=3 SCAR=24.8
Pod undocumented
67 0 my ($self, $session) = @_;
68 0 my $file = $session->{_file};
69 0 ▶ open my $fh, '<:utf8', $file or return 0;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:utf8", $file or return 0 0 0 0
70 0 my $content = do { local $/; <$fh> };
71 0 close $fh;
72 0 my $header = quotemeta($session->{_header});
73 0 $content =~ s/^$header\n.*?(?=^# aider chat started at |\z)//ms;
74 0 ▶ open $fh, '>', $file or return 0;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $file or return 0 0 0 0
75 0 print $fh $content;
76 0 close $fh;
77 0 return 1;
78 }
79 1 ▶sub load_messages {
Subroutine load_messages: called CC=15 SCAR=27.2
Pod undocumented
80 1 my ($self, $session) = @_;
81 1 my @messages;
82 1 my $in_session = 0;
83 1 ▶ open my $fh, '<:utf8', $session->{_file} or return @messages;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:utf8", $session->{"_file"} or return @messages 1 0 1
84 1 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 12 0 12
85 12 chomp $line;
86 12 ▶ if ($line eq $session->{_header}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq $session->{"_header"}) 1 11 12
87 1 $in_session = 1;
88 1 next;
89 }
90 11 ▶ last if $in_session && $line =~ /^# aider chat started at /;
Branch: 1 branch
BranchTrueFalseTotal
if $in_session and $line =~ /^# aider chat started at / 1 10 11
Condition100%3/3
$in_session and $line =~ /^# aider chat started at /
!ll&&!rl&&r
191
MC/DC100%2/2
$in_session and $line =~ /^# aider chat started at /
$in_session $line =~ /^# aider chat started at /
Truth table100%3/3
A and B
A= $in_sessionB= $line =~ /^# aider chat started at /
ABresult
0X0
100
111
91 10 ▶ next unless $in_session;
Branch: 1 branch
BranchTrueFalseTotal
unless $in_session 1 9 10
92 9 ▶ if ($line =~ /^#### (.+)$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^#### (.+)$/) { } 2 7 9
elsif ($line =~ /^[^>#\s]/ and @messages and $messages[-1]{'role'} eq 'user') { } 1 6 7
elsif (@messages and $messages[-1]{'role'} eq 'assistant' and not $line =~ /^>/) { } 1 10 11
93 2 push @messages, { role => 'user', text => $1 };
94 }
95 ▶ elsif ($line =~ /^[^>#\s]/ && @messages && $messages[-1]{role} eq 'user') {
Condition66%4/6
$line =~ /^[^>#\s]/ and @messages and $messages[-1]{"role"} eq "user"
!ll&&!rl&&r
601
$line =~ /^[^>#\s]/ and @messages
!ll&&!rl&&r
601
MC/DC33%1/3
$line =~ /^[^>#\s]/ and @messages and $messages[-1]{"role"} eq "user"
$line =~ /^[^>#\s]/ @messages $messages[-1]{"role"} eq "user"
Truth table50%2/4
A and B and C
A= $line =~ /^[^>#\s]/B= @messagesC= $messages[-1]{"role"} eq "user"
ABCresult
0XX0
10X0
1100
1111
96 1 push @messages, { role => 'assistant', text => $line };
97 }
98 ▶ elsif (@messages && $messages[-1]{role} eq 'assistant' && $line !~ /^>/) {
Condition83%5/6
@messages and $messages[-1]{"role"} eq "assistant" and not $line =~ /^>/
!ll&&!rl&&r
501
@messages and $messages[-1]{"role"} eq "assistant"
!ll&&!rl&&r
141
MC/DC66%2/3
@messages and $messages[-1]{"role"} eq "assistant" and not $line =~ /^>/
@messages $messages[-1]{"role"} eq "assistant" not $line =~ /^>/
Truth table75%3/4
A and B and not(C)
A= @messagesB= $messages[-1]{"role"} eq "assistant"C= not $line =~ /^>/
ABCresult
0XX0
10X0
1100
1111
99 1 $messages[-1]{text} .= "\n$line";
100 }
101 }
102 1 close $fh;
103 1 for my $m (@messages) {
104 3 $m->{text} =~ s/\s+$//;
105 }
106 1 return @messages;
107 }
108 1 ▶sub _parse_history_file {
Subroutine _parse_history_file: called CC=9 SCAR=22.0
109 1 my ($file, $cwd, $adapter) = @_;
110 1 my @sessions;
111 1 ▶ open my $fh, '<:utf8', $file or return @sessions;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:utf8", $file or return @sessions 1 0 1
112 1 my ($header, $date, $epoch, $title, @lines);
113 1 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 16 1 17
114 16 chomp $line;
115 16 ▶ if ($line =~ /^# aider chat started at (\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^# aider chat started at (\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})$/) 2 14 16
116 2 ▶ if ($header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header) 1 1 2
117 1 push @sessions, _make_session($header, $date, $epoch, $cwd, $file, \@lines, $adapter);
118 }
119 2 $date = $1;
120 2 $header = $line;
121 2 $epoch = _parse_epoch($date);
122 2 @lines = ();
123 2 $title = undef;
124 2 next;
125 }
126 14 ▶ next unless $header;
Branch: 1 branch
BranchTrueFalseTotal
unless $header 1 13 14
127 13 push @lines, $line;
128 13 ▶ if (!$title && $line =~ /^#### (.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $title and $line =~ /^#### (.+)$/) 2 11 13
Condition100%3/3
not $title and $line =~ /^#### (.+)$/
!ll&&!rl&&r
922
MC/DC100%2/2
not $title and $line =~ /^#### (.+)$/
not $title $line =~ /^#### (.+)$/
Truth table100%3/3
not(A) and B
A= not $titleB= $line =~ /^#### (.+)$/
ABresult
0X0
100
111
129 2 $title = $1;
130 }
131 }
132 1 close $fh;
133 1 ▶ push @sessions, _make_session($header, $date, $epoch, $cwd, $file, \@lines, $adapter)
Branch: 1 branch
BranchTrueFalseTotal
if $header 1 0 1
134 if $header;
135 1 return @sessions;
136 }
137 2 ▶sub _make_session {
Subroutine _make_session: called CC=3 SCAR=11.0
138 2 my ($header, $date, $epoch, $cwd, $file, $lines, $adapter) = @_;
139 2 my $cwd_sum = 0;
140 2 $cwd_sum = ($cwd_sum * 31 + ord($_)) & 0xFFFFFFFF for split //, $cwd;
141 2 my $id = sprintf('%08x-%04x-%04x-%04x-%012x',
142 $cwd_sum,
143 ($cwd_sum >> 16) & 0xFFFF,
144 ($epoch >> 16) & 0xFFFF,
145 $epoch & 0xFFFF,
146 $epoch);
147 2 my ($title) = grep { /^#### / } @$lines;
148 2 ▶ $title = "(no messages)" unless $title;
Branch: 1 branch
BranchTrueFalseTotal
unless $title 0 2 2
149 2 $title =~ s/^#### //;
150 2 my $size = length(join "\n", @$lines);
151 return {
152 2 id => $id,
153 title => $title,
154 date => $date,
155 epoch => $epoch,
156 cwd => $cwd,
157 project => $cwd,
158 _file => $file,
159 _header => $header,
160 _size => $size,
161 _adapter => $adapter,
162 };
163 }
164 2 ▶sub _parse_epoch {
Subroutine _parse_epoch: called CC=2 SCAR=7.1
165 2 my ($date) = @_;
166 2 ▶ return 0 unless $date =~ /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $date =~ /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/ 0 2 2
167 2 return POSIX::mktime($6, $5, $4, $3, $2-1, $1-1900);
168 }
169 1 1;
« Saisons/Adapter/Codex.pm ↑ Summary Saisons.pm »