Saisons/Adapter/Codex.pm

filter: Statementstmt 89.2% 100 / 112 Branchbran 60.9% 39 / 64 Conditioncond 46.3% 25 / 54 MC/DCmcdc 11.1% 3 / 27 Subroutinesub 83.3% 15 / 18 Podpod 0.0% 0 / 9 totaltotal 64.0% 182 / 284 CC 62 SCAR 30.2
CC
62
Cov
71%
_parse_session
39.6
_find_session_files
22.6
load_messages
18.3
CRAP 20.4
« Saisons/Adapter/Gemini.pm ↑ Summary Saisons/Adapter/Aider.pm »
1 package App::Saisons::Adapter::Codex;
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 JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use POSIX qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use App::Saisons::Launcher ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶sub new { bless {}, shift }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
9 1 ▶sub name { 'Codex' }
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
10 2 ▶sub tag { "\x{2601} Cdx" }
Subroutine tag: called CC=1 SCAR=0.0
Pod undocumented
11 1 ▶sub tag_color { 'bold cyan' }
Subroutine tag_color: called CC=1 SCAR=0.0
Pod undocumented
12 1 ▶sub find_sessions {
Subroutine find_sessions: called CC=5 SCAR=16.4
Pod undocumented
13 1 my ($self) = @_;
14 1 ▶ my $root = $ENV{CODEX_HOME} // "$ENV{HOME}/.codex";
Condition50%1/2
$ENV{"CODEX_HOME"} // qq[$ENV{"HOME"}/.codex]
l!l
10
MC/DC0%0/1
$ENV{"CODEX_HOME"} // qq[$ENV{"HOME"}/.codex]
$ENV{"CODEX_HOME"}
Truth table50%1/2
A
A= $ENV{"CODEX_HOME"}
Aresult
11
00
15 1 my $sessions_dir = "$root/sessions";
16 1 ▶ return () unless -d $sessions_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $sessions_dir 0 1 1
17 1 my @files = _find_session_files($sessions_dir);
18 1 my @sessions;
19 1 for my $file (@files) {
20 1 ▶ my $s = _parse_session($file, $self) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $s = _parse_session($file, $self) or next 1 0 1
21 1 push @sessions, $s;
22 }
23 1 return @sessions;
24 }
25 0 ▶sub running {
Subroutine running: not called CC=1 SCAR=6.9
Pod undocumented
26 0 return ();
27 }
28 0 ▶sub launch {
Subroutine launch: not called CC=2 SCAR=17.9
Pod undocumented
29 0 my ($self, $sessions, $launcher) = @_;
30 0 for my $s (@$sessions) {
31 0 my $cmd = "cd \Q$s->{cwd}\E && codex resume \Q$s->{_file}\E";
32 0 App::Saisons::Launcher::launch_cmd($cmd, $s->{cwd}, $launcher, $s->{title});
33 }
34 }
35 0 ▶sub delete_session {
Subroutine delete_session: not called CC=1 SCAR=6.9
Pod undocumented
36 0 my ($self, $session) = @_;
37 0 return unlink $session->{_file};
38 }
39 1 ▶sub load_messages {
Subroutine load_messages: called CC=15 SCAR=29.1
Pod undocumented
40 1 my ($self, $session) = @_;
41 1 my @messages;
42 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
43 1 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 3 1 4
44 3 ▶ my $obj = eval { JSON::PP::decode_json($line) } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $obj = eval { do { JSON::PP::decode_json($line) } } or next 3 0 3
45 3 ▶ my $type = $obj->{type} // '';
Condition50%1/2
$obj->{"type"} // ""
l!l
30
MC/DC0%0/1
$obj->{"type"} // ""
$obj->{"type"}
Truth table50%1/2
A
A= $obj->{"type"}
Aresult
11
00
46 3 ▶ my $payload = $obj->{payload} // {};
Condition50%1/2
$obj->{"payload"} // {}
l!l
30
MC/DC0%0/1
$obj->{"payload"} // {}
$obj->{"payload"}
Truth table50%1/2
A
A= $obj->{"payload"}
Aresult
11
00
47 3 ▶ if ($type eq 'event_msg' && ($payload->{type} // '') eq 'user_message') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'event_msg' and ($payload->{'type'} // '') eq 'user_message') { } 1 2 3
elsif ($type eq 'response_item' and ($payload->{'role'} // '') eq 'assistant') { } 1 2 3
Condition60%3/5
$type eq "event_msg" and ($payload->{"type"} // "") eq "user_message"
!ll&&!rl&&r
201
$payload->{"type"} // ""
l!l
10
MC/DC50%1/2
$type eq "event_msg" and ($payload->{"type"} // "") eq "user_message"
$type eq "event_msg" $payload->{"type"}
Truth table66%2/3
A and B
A= $type eq "event_msg"B= $payload->{"type"}
ABresult
0X0
100
111
48 1 ▶ my $text = $payload->{message} // '';
Condition50%1/2
$payload->{"message"} // ""
l!l
10
MC/DC0%0/1
$payload->{"message"} // ""
$payload->{"message"}
Truth table50%1/2
A
A= $payload->{"message"}
Aresult
11
00
49 1 ▶ push @messages, { role => 'user', text => $text } if length $text;
Branch: 1 branch
BranchTrueFalseTotal
if length $text 1 0 1
50 }
51 ▶ elsif ($type eq 'response_item' && ($payload->{role} // '') eq 'assistant') {
Condition60%3/5
$type eq "response_item" and ($payload->{"role"} // "") eq "assistant"
!ll&&!rl&&r
101
$payload->{"role"} // ""
l!l
10
MC/DC50%1/2
$type eq "response_item" and ($payload->{"role"} // "") eq "assistant"
$type eq "response_item" $payload->{"role"}
Truth table66%2/3
A and B
A= $type eq "response_item"B= $payload->{"role"}
ABresult
0X0
100
111
52 1 my $text = _extract_text($payload->{content});
53 1 ▶ push @messages, { role => 'assistant', text => $text } if length $text;
Branch: 1 branch
BranchTrueFalseTotal
if length $text 1 0 1
54 }
55 }
56 1 close $fh;
57 1 return @messages;
58 }
59 1 ▶sub _find_session_files {
Subroutine _find_session_files: called CC=17 SCAR=31.2
60 1 my ($root) = @_;
61 1 my @files;
62 1 ▶ opendir my $dh, $root or return @files;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $root or return @files 1 0 1
63 1 for my $year (sort readdir $dh) {
64 3 ▶ next unless $year =~ /^\d{4}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $year =~ /^\d{4}$/ 2 1 3
65 1 my $ydir = "$root/$year";
66 1 ▶ opendir my $mdh, $ydir or next;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $mdh, $ydir or next 1 0 1
67 1 for my $month (sort readdir $mdh) {
68 3 ▶ next unless $month =~ /^\d{2}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $month =~ /^\d{2}$/ 2 1 3
69 1 my $mdir = "$ydir/$month";
70 1 ▶ opendir my $ddh, $mdir or next;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $ddh, $mdir or next 1 0 1
71 1 for my $day (sort readdir $ddh) {
72 3 ▶ next unless $day =~ /^\d{2}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $day =~ /^\d{2}$/ 2 1 3
73 1 my $ddir = "$mdir/$day";
74 1 ▶ opendir my $fdh, $ddir or next;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $fdh, $ddir or next 1 0 1
75 1 for my $file (sort readdir $fdh) {
76 3 ▶ next unless $file =~ /^rollout-.*\.jsonl$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /^rollout-.*\.jsonl$/ 2 1 3
77 1 push @files, "$ddir/$file";
78 }
79 1 closedir $fdh;
80 }
81 1 closedir $ddh;
82 }
83 1 closedir $mdh;
84 }
85 1 closedir $dh;
86 1 my $archived = "$root/archived_sessions";
87 1 ▶ if (-d $archived) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $archived) 0 1 1
88 0 ▶ opendir my $adh, $archived or return @files;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $adh, $archived or return @files 0 0 0
89 0 for my $file (sort readdir $adh) {
90 0 ▶ next unless $file =~ /^rollout-.*\.jsonl$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /^rollout-.*\.jsonl$/ 0 0 0
91 0 push @files, "$archived/$file";
92 }
93 0 closedir $adh;
94 }
95 1 return @files;
96 }
97 1 ▶sub _parse_session {
Subroutine _parse_session: called CC=21 SCAR=36.8
98 1 my ($file, $adapter) = @_;
99 1 ▶ open my $fh, '<:utf8', $file or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:utf8", $file or return undef 1 0 1
100 1 my ($id, $cwd, $title);
101 1 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 2 0 2
102 2 ▶ my $obj = eval { JSON::PP::decode_json($line) } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $obj = eval { do { JSON::PP::decode_json($line) } } or next 2 0 2
103 2 ▶ my $type = $obj->{type} // '';
Condition50%1/2
$obj->{"type"} // ""
l!l
20
MC/DC0%0/1
$obj->{"type"} // ""
$obj->{"type"}
Truth table50%1/2
A
A= $obj->{"type"}
Aresult
11
00
104 2 ▶ my $payload = $obj->{payload} // {};
Condition50%1/2
$obj->{"payload"} // {}
l!l
20
MC/DC0%0/1
$obj->{"payload"} // {}
$obj->{"payload"}
Truth table50%1/2
A
A= $obj->{"payload"}
Aresult
11
00
105 2 ▶ if ($type eq 'session_meta') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'session_meta') { } 1 1 2
elsif ($type eq 'event_msg' and ($payload->{'type'} // '') eq 'user_message') { } 1 0 1
106 1 ▶ $id //= $payload->{id};
Condition33%1/3
$id //= $payload->{"id"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$id //= $payload->{"id"}
$id $payload->{"id"}
Truth table33%1/3
A //= B
A= $idB= $payload->{"id"}
ABresult
1X1
011
000
107 1 ▶ $cwd //= $payload->{cwd};
Condition33%1/3
$cwd //= $payload->{"cwd"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$cwd //= $payload->{"cwd"}
$cwd $payload->{"cwd"}
Truth table33%1/3
A //= B
A= $cwdB= $payload->{"cwd"}
ABresult
1X1
011
000
108 }
109 ▶ elsif ($type eq 'event_msg' && ($payload->{type} // '') eq 'user_message') {
Condition40%2/5
$type eq "event_msg" and ($payload->{"type"} // "") eq "user_message"
!ll&&!rl&&r
001
$payload->{"type"} // ""
l!l
10
MC/DC0%0/2
$type eq "event_msg" and ($payload->{"type"} // "") eq "user_message"
$type eq "event_msg" $payload->{"type"}
Truth table33%1/3
A and B
A= $type eq "event_msg"B= $payload->{"type"}
ABresult
0X0
100
111
110 1 ▶ $title //= $payload->{message};
Condition33%1/3
$title //= $payload->{"message"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$title //= $payload->{"message"}
$title $payload->{"message"}
Truth table33%1/3
A //= B
A= $titleB= $payload->{"message"}
ABresult
1X1
011
000
111 }
112 2 ▶ last if $id && $cwd && $title;
Branch: 1 branch
BranchTrueFalseTotal
if $id and $cwd and $title 1 1 2
Condition50%3/6
$id and $cwd and $title
!ll&&!rl&&r
011
$id and $cwd
!ll&&!rl&&r
002
MC/DC33%1/3
$id and $cwd and $title
$id $cwd $title
Truth table50%2/4
A and B and C
A= $idB= $cwdC= $title
ABCresult
0XX0
10X0
1100
1111
113 }
114 1 close $fh;
115 1 ▶ return undef unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 1 1
116 1 ▶ $cwd //= $ENV{HOME};
Condition33%1/3
$cwd //= $ENV{"HOME"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$cwd //= $ENV{"HOME"}
$cwd $ENV{"HOME"}
Truth table33%1/3
A //= B
A= $cwdB= $ENV{"HOME"}
ABresult
1X1
011
000
117 1 ▶ $title //= '(no title)';
Condition50%1/2
$title //= "(no title)"
l!l
10
MC/DC0%0/1
$title //= "(no title)"
$title
Truth table50%1/2
A
A= $title
Aresult
11
00
118 1 my ($date_str, $epoch) = ('0000-00-00', 0);
119 1 ▶ if ($file =~ /rollout-(\d{4})-(\d{2})-(\d{2})T(\d{2})-(\d{2})-(\d{2})/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /rollout-(\d{4})-(\d{2})-(\d{2})T(\d{2})-(\d{2})-(\d{2})/) 1 0 1
120 1 $date_str = "$1-$2-$3T$4:$5:$6";
121 1 $epoch = POSIX::mktime($6, $5, $4, $3, $2 - 1, $1 - 1900);
122 }
123 return {
124 1 ▶ id => $id,
Condition50%1/2
-s $file // 0
l!l
10
MC/DC0%0/1
-s $file // 0
-s $file
Truth table50%1/2
A
A= -s $file
Aresult
11
00
125 title => $title,
126 date => $date_str,
127 epoch => $epoch,
128 cwd => $cwd,
129 project => $cwd,
130 _file => $file,
131 _size => (-s $file) // 0,
132 _adapter => $adapter,
133 };
134 }
135 1 ▶sub _extract_text {
Subroutine _extract_text: called CC=7 SCAR=22.9
136 1 my ($content) = @_;
137 1 ▶ return '' unless defined $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 0 1 1
138 1 ▶ return $content unless ref $content;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $content 0 1 1
139 1 ▶ return join '', map { ref $_ eq 'HASH' && ($_->{type}//'') eq 'text' ? $_->{text} : '' }
Branch: 2 branches
BranchTrueFalseTotal
ref $_ eq 'HASH' && ($_->{'type'} // '') eq 'text' ? : 1 0 1
ref $content eq 'ARRAY' ? : 1 0 1
Condition40%2/5
ref $_ eq "HASH" and ($_->{"type"} // "") eq "text"
!ll&&!rl&&r
001
$_->{"type"} // ""
l!l
10
MC/DC0%0/2
ref $_ eq "HASH" and ($_->{"type"} // "") eq "text"
ref $_ eq "HASH" $_->{"type"}
Truth table33%1/3
A and B
A= ref $_ eq "HASH"B= $_->{"type"}
ABresult
0X0
100
111
140 ref $content eq 'ARRAY' ? @$content : ();
141 }
142 1 1;
« Saisons/Adapter/Gemini.pm ↑ Summary Saisons/Adapter/Aider.pm »