Saisons/Adapter/Claude.pm

filter: Statementstmt 75.9% 123 / 162 Branchbran 45.7% 43 / 94 Conditioncond 36.8% 14 / 38 MC/DCmcdc 15.7% 3 / 19 Subroutinesub 84.2% 16 / 19 Podpod 0.0% 0 / 10 totaltotal 58.1% 199 / 342 CC 67 SCAR 37.5
CC
67
Cov
61%
find_sessions
94.1
running
90.0
_extract_meta
19.7
CRAP 42.5
« Saisons/Adapter/Opencode.pm ↑ Summary Saisons/Launcher.pm »
1 package App::Saisons::Adapter::Claude;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use App::Saisons ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use POSIX qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Time::Local qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use App::Saisons::Launcher ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶sub new { bless {}, shift }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
10 1 ▶sub name { 'Claude Code' }
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
11 2 ▶sub tag { "\x{2736} Cld" }
Subroutine tag: called CC=1 SCAR=0.0
Pod undocumented
12 1 ▶sub tag_color { 'bold red' }
Subroutine tag_color: called CC=1 SCAR=0.0
Pod undocumented
13 3 ▶sub find_sessions {
Subroutine find_sessions: called CC=23 SCAR=45.4
Pod undocumented
14 3 my ($self) = @_;
15 3 my $projects_dir = "$ENV{HOME}/.claude/projects";
16 3 my %running_names;
17 3 my $sessions_dir = "$ENV{HOME}/.claude/sessions";
18 3 ▶ if (opendir my $sdh, $sessions_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (opendir my $sdh, $sessions_dir) 0 3 3
19 0 ▶ while (my $f = readdir $sdh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $f = readdir $sdh)) 0 0 0
20 0 ▶ next unless $f =~ /^\d+\.json$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $f =~ /^\d+\.json$/ 0 0 0
21 0 ▶ open my $fh, '<', "$sessions_dir/$f" or next;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", "$sessions_dir/$f" or next 0 0 0
22 0 my $json = do { local $/; <$fh> };
23 0 close $fh;
24 0 ▶ my $obj = eval { decode_json($json) } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $obj = eval { do { decode_json($json) } } or next 0 0 0
25 0 ▶ next unless $obj->{sessionId} && $obj->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->{"sessionId"} and $obj->{"name"} 0 0 0
Condition0%0/3
$obj->{"sessionId"} and $obj->{"name"}
!ll&&!rl&&r
000
MC/DC0%0/2
$obj->{"sessionId"} and $obj->{"name"}
$obj->{"sessionId"} $obj->{"name"}
Truth table0%0/3
A and B
A= $obj->{"sessionId"}B= $obj->{"name"}
ABresult
0X0
100
111
26 0 $running_names{ $obj->{sessionId} } = $obj->{name};
27 }
28 0 closedir $sdh;
29 }
30 3 my %by_id;
31 3 ▶ opendir my $dh, $projects_dir or return ();
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $projects_dir or return () 3 0 3
32 3 for my $project (sort readdir $dh) {
33 9 ▶ next if $project =~ /^\./;
Branch: 1 branch
BranchTrueFalseTotal
if $project =~ /^\./ 6 3 9
34 3 my $dir = "$projects_dir/$project";
35 3 ▶ next unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 3 3
36 3 ▶ opendir my $pdh, $dir or next;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $pdh, $dir or next 3 0 3
37 3 for my $file (sort readdir $pdh) {
38 18 ▶ next unless $file =~ /^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.jsonl$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.jsonl$/ 12 6 18
39 6 my $id = $1;
40 6 my ($title, $date, $epoch, $cwd, $first_msg) = _extract_meta("$dir/$file");
41 6 ▶ $cwd //= _derive_path($project);
Condition33%1/3
$cwd //= _derive_path($project)
l!l&&r!l&&!r
600
MC/DC0%0/2
$cwd //= _derive_path($project)
$cwd _derive_path($project)
Truth table33%1/3
A //= B
A= $cwdB= _derive_path($project)
ABresult
1X1
011
000
42 6 ▶ $title //= $running_names{$id} // $first_msg // '(no title)';
Condition14%1/7
$title //= ($running_names{$id} // $first_msg) // "(no title)"
l!l
60
($running_names{$id} // $first_msg) // "(no title)"
l!l
00
$running_names{$id} // $first_msg
l!l&&r!l&&!r
000
MC/DC0%0/1
$title //= ($running_names{$id} // $first_msg) // "(no title)"
$title
Truth table50%1/2
A
A= $title
Aresult
11
00
43 6 ▶ my $size = -s "$dir/$file" // 0;
Condition50%1/2
-s "$dir/$file" // 0
l!l
60
MC/DC0%0/1
-s "$dir/$file" // 0
-s "$dir/$file"
Truth table50%1/2
A
A= -s "$dir/$file"
Aresult
11
00
44 6 my $session_dir = "$dir/$id";
45 6 ▶ my $candidate = {
Branch: 1 branch
BranchTrueFalseTotal
-d $session_dir ? : 6 0 6
46 id => $id,
47 project => $project,
48 cwd => $cwd,
49 title => $title,
50 date => $date,
51 epoch => $epoch,
52 _file => "$dir/$file",
53 _dir => (-d $session_dir ? $session_dir : undef),
54 _size => $size,
55 _adapter => $self,
56 };
57 # Deduplicate: keep the copy with the larger file (more content).
58 # Duplicates arise when Claude writes back to the old project dir
59 # after a move_session. The moved copy has the full conversation;
60 # the leftover is tiny (only metadata records, no cwd).
61 6 ▶ if (!exists $by_id{$id} || $size > $by_id{$id}{_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $by_id{$id} or $size > $by_id{$id}{"_size"}) 6 0 6
Condition33%1/3
not exists $by_id{$id} or $size > $by_id{$id}{"_size"}
l!l&&r!l&&!r
600
MC/DC0%0/2
not exists $by_id{$id} or $size > $by_id{$id}{"_size"}
not exists $by_id{$id} $size > $by_id{$id}{"_size"}
Truth table33%1/3
not(A) or B
A= not exists $by_id{$id}B= $size > $by_id{$id}{"_size"}
ABresult
1X1
011
000
62 6 $by_id{$id} = $candidate;
63 }
64 }
65 3 closedir $pdh;
66 }
67 3 closedir $dh;
68 3 return values %by_id;
69 }
70 0 ▶sub running {
Subroutine running: not called CC=9 SCAR=45.0
Pod undocumented
71 0 my ($self) = @_;
72 0 my %running;
73 0 my $sessions_dir = "$ENV{HOME}/.claude/sessions";
74 0 ▶ opendir my $sdh, $sessions_dir or return %running;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $sdh, $sessions_dir or return %running 0 0 0
75 0 ▶ while (my $f = readdir $sdh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $f = readdir $sdh)) 0 0 0
76 0 ▶ next unless $f =~ /^\d+\.json$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $f =~ /^\d+\.json$/ 0 0 0
77 0 ▶ open my $fh, '<', "$sessions_dir/$f" or next;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", "$sessions_dir/$f" or next 0 0 0
78 0 my $json = do { local $/; <$fh> };
79 0 close $fh;
80 0 ▶ my $obj = eval { decode_json($json) } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $obj = eval { do { decode_json($json) } } or next 0 0 0
81 0 ▶ my $pid = $obj->{pid} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $pid = $obj->{"pid"} or next 0 0 0
82 0 ▶ my $sid = $obj->{sessionId} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $sid = $obj->{"sessionId"} or next 0 0 0
83 0 ▶ next unless kill(0, $pid);
Branch: 1 branch
BranchTrueFalseTotal
unless kill 0, $pid 0 0 0
84 0 $running{$sid} = 1;
85 }
86 0 closedir $sdh;
87 0 return %running;
88 }
89 0 ▶sub launch {
Subroutine launch: not called CC=2 SCAR=17.9
Pod undocumented
90 0 my ($self, $sessions, $launcher) = @_;
91 0 for my $s (@$sessions) {
92 0 my $cmd = "cd \Q$s->{cwd}\E && claude --resume \Q$s->{id}\E";
93 0 App::Saisons::Launcher::launch_cmd($cmd, $s->{cwd}, $launcher, $s->{title});
94 }
95 }
96 0 ▶sub delete_session {
Subroutine delete_session: not called CC=1 SCAR=6.9
Pod undocumented
97 0 my ($self, $session) = @_;
98 0 return unlink $session->{_file};
99 }
100 1 ▶sub move_session {
Subroutine move_session: called CC=8 SCAR=21.3
Pod undocumented
101 1 my ($self, $session, $new_cwd) = @_;
102 1 my $projects_dir = "$ENV{HOME}/.claude/projects";
103 1 my $old_cwd = $session->{cwd};
104 1 my $old_file = $session->{_file};
105 1 (my $new_project = $new_cwd) =~ s|/|-|g;
106 1 $new_project =~ s/^-//;
107 1 $new_project = "-$new_project";
108 1 my $new_dir = "$projects_dir/$new_project";
109 1 my $new_file = "$new_dir/$session->{id}.jsonl";
110 1 ▶ unless (-d $new_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $new_dir) 0 1 1
111 0 ▶ mkdir $new_dir or return (0, "Cannot create $new_dir: $!");
Branch: 1 branch
BranchTrueFalseTotal
mkdir $new_dir or return 0, "Cannot create ${new_dir}: $!" 0 0 0
112 }
113 1 ▶ open my $in, '<', $old_file or return (0, "Cannot read $old_file: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<", $old_file or return 0, "Cannot read ${old_file}: $!" 1 0 1
114 1 ▶ open my $out, '>', $new_file or return (0, "Cannot write $new_file: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">", $new_file or return 0, "Cannot write ${new_file}: $!" 1 0 1
115 1 my $old_quoted = quotemeta($old_cwd);
116 1 ▶ while (my $line = <$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $in)) 4 1 5
117 4 $line =~ s/"cwd":"$old_quoted"/"cwd":"$new_cwd"/g;
118 4 print $out $line;
119 }
120 1 close $in;
121 1 close $out;
122 1 unlink $old_file;
123 1 my $old_dir = "$projects_dir/$session->{project}";
124 1 ▶ opendir my $dh, $old_dir or return (1, '');
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $old_dir or return 1, "" 1 0 1
125 1 my @remaining = grep { !/^\./ } readdir $dh;
126 1 closedir $dh;
127 1 ▶ rmdir $old_dir unless @remaining;
Branch: 1 branch
BranchTrueFalseTotal
unless @remaining 0 1 1
128 1 $session->{cwd} = $new_cwd;
129 1 $session->{project} = $new_project;
130 1 $session->{_file} = $new_file;
131 1 return (1, '');
132 }
133 1 ▶sub load_messages {
Subroutine load_messages: called CC=13 SCAR=27.7
Pod undocumented
134 1 my ($self, $session) = @_;
135 1 my @messages;
136 1 ▶ open my $fh, '<', $session->{_file} or return @messages;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $session->{"_file"} or return @messages 1 0 1
137 1 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 4 1 5
138 4 ▶ next unless /"role":"(user|assistant)"/;
Branch: 1 branch
BranchTrueFalseTotal
unless /"role":"(user|assistant)"/ 1 3 4
139 3 my $role = $1;
140 3 ▶ my $obj = eval { decode_json($_) } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $obj = eval { do { decode_json($_) } } or next 3 0 3
141 3 ▶ my $content = $obj->{message}{content} // next;
Condition50%1/2
$obj->{"message"}{"content"} // (next)
l!l
30
MC/DC0%0/1
$obj->{"message"}{"content"} // (next)
$obj->{"message"}{"content"}
Truth table50%1/2
A
A= $obj->{"message"}{"content"}
Aresult
11
00
142 3 my $text = '';
143 3 ▶ if (!ref $content) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $content) { } 0 3 3
elsif (ref $content eq 'ARRAY') { } 3 0 3
144 0 $text = $content;
145 }
146 elsif (ref $content eq 'ARRAY') {
147 3 for my $block (@$content) {
148 3 ▶ next unless ref $block eq 'HASH' && ($block->{type} // '') eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $block eq "HASH" and ($block->{"type"} // "") eq "text" 0 3 3
Condition40%2/5
ref $block eq "HASH" and ($block->{"type"} // "") eq "text"
!ll&&!rl&&r
003
$block->{"type"} // ""
l!l
30
MC/DC0%0/2
ref $block eq "HASH" and ($block->{"type"} // "") eq "text"
ref $block eq "HASH" $block->{"type"}
Truth table33%1/3
A and B
A= ref $block eq "HASH"B= $block->{"type"}
ABresult
0X0
100
111
149 3 $text .= $block->{text};
150 }
151 }
152 3 ▶ next unless length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 0 3 3
153 3 push @messages, { role => $role, text => $text };
154 }
155 1 close $fh;
156 1 return @messages;
157 }
158 6 ▶sub _extract_meta {
Subroutine _extract_meta: called CC=17 SCAR=29.8
159 6 my ($file) = @_;
160 6 my ($title, $date, $epoch, $cwd, $first_msg) = (undef, '0000-00-00', 0, undef, undef);
161 6 print STDERR "\033[2KLoading $file ...\r";
162 6 ▶ open my $fh, '<:utf8', $file or return ($title, $date, $epoch, $cwd);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:utf8", $file or return $title, $date, $epoch, $cwd 6 0 6
163 6 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 21 6 27
164 21 ▶ $title = $1 if !defined $title && /"aiTitle":"([^"]+)"/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $title and /"aiTitle":"([^"]+)"/ 6 15 21
Condition66%2/3
not defined $title and /"aiTitle":"([^"]+)"/
!ll&&!rl&&r
0156
MC/DC50%1/2
not defined $title and /"aiTitle":"([^"]+)"/
not defined $title /"aiTitle":"([^"]+)"/
Truth table66%2/3
not(A) and B
A= not defined $titleB= /"aiTitle":"([^"]+)"/
ABresult
0X0
100
111
165 21 ▶ $cwd = $1 if !$cwd && /"cwd":"([^"]+)"/;
Branch: 1 branch
BranchTrueFalseTotal
if not $cwd and /"cwd":"([^"]+)"/ 6 15 21
Condition66%2/3
not $cwd and /"cwd":"([^"]+)"/
!ll&&!rl&&r
0156
MC/DC50%1/2
not $cwd and /"cwd":"([^"]+)"/
not $cwd /"cwd":"([^"]+)"/
Truth table66%2/3
not(A) and B
A= not $cwdB= /"cwd":"([^"]+)"/
ABresult
0X0
100
111
166 21 ▶ if (!$first_msg && /"role":"user"/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $first_msg and /"role":"user"/) 6 15 21
Condition66%2/3
not $first_msg and /"role":"user"/
!ll&&!rl&&r
1506
MC/DC50%1/2
not $first_msg and /"role":"user"/
not $first_msg /"role":"user"/
Truth table66%2/3
not(A) and B
A= not $first_msgB= /"role":"user"/
ABresult
0X0
100
111
167 6 ▶ my $obj = eval { decode_json($_) } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $obj = eval { do { decode_json($_) } } or next 6 0 6
168 6 my $c = $obj->{message}{content};
169 6 ▶ my $txt = ref $c eq 'ARRAY' ? ($c->[0]{text} // '') : ($c // '');
Branch: 1 branch
BranchTrueFalseTotal
ref $c eq 'ARRAY' ? : 6 0 6
Condition25%1/4
$c->[0]{"text"} // ""
l!l
60
$c // ""
l!l
00
MC/DC0%0/1
$c->[0]{"text"} // ""
$c->[0]{"text"}
MC/DC0%0/1
$c // ""
$c
Truth table50%1/2
A
A= $c->[0]{"text"}
Aresult
11
00
Truth table0%0/2
A
A= $c
Aresult
11
00
170 6 ▶ next if $txt =~ /^This session is being continued from a previous conversation/;
Branch: 1 branch
BranchTrueFalseTotal
if $txt =~ /^This session is being continued from a previous conversation/ 0 6 6
171 6 ▶ ($first_msg) = split /\n/, $txt if length $txt;
Branch: 1 branch
BranchTrueFalseTotal
if length $txt 6 0 6
172 }
173 21 ▶ if (/"timestamp":"(\d{4}-\d{2}-\d{2})T(\d{2}):(\d{2}):(\d{2})/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/"timestamp":"(\d{4}-\d{2}-\d{2})T(\d{2}):(\d{2}):(\d{2})/) 21 0 21
174 21 my $ts = "$1T$2:$3:$4";
175 21 ▶ if ($ts gt $date) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ts gt $date) 15 6 21
176 15 $date = $ts;
177 15 $epoch = Time::Local::timegm($4, $3, $2,
178 substr($1,8,2), substr($1,5,2)-1, substr($1,0,4)-1900);
179 }
180 }
181 }
182 6 close $fh;
183 6 print STDERR "\033[2K\r";
184 6 return ($title, $date, $epoch, $cwd, $first_msg);
185 }
186 3 ▶sub _derive_path {
Subroutine _derive_path: called CC=1 SCAR=0.0
187 3 my ($project) = @_;
188 3 return $project;
189 }
190 2 1;
« Saisons/Adapter/Opencode.pm ↑ Summary Saisons/Launcher.pm »