blib/lib/App/karr/Role/SkillFile.pm

filter: Statementstmt 50.0% 38 / 76 Branchbran 33.3% 8 / 24 Conditioncond 11.7% 2 / 17 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 46.6% 7 / 15 Podpod n/a totaltotal 39.1% 56 / 143 CC 21 SCAR 27.1
CC
21
Cov
41%
_remove_retired_skills
42.0
_skill_source_dir
30.0
__ANON__
12.0
CRAP 15.0
« blib/lib/App/karr/Foundation/Limits.pm ↑ Summary blib/lib/App/karr/Cmd/Import.pm »
1 # ABSTRACT: The one way karr finds and writes the bundled skill directories
2  
3 package App::karr::Role::SkillFile;
4 3 our $VERSION = '0.602';
5 3 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # All loaded without importing, for the reason spelled out in
7 # App::karr::Role::Output: a Moo::Role composes every sub in its package into
8 # its consumers, so `use App::karr::Error qw( user_error )` here would quietly
9 # make user_error and clean_error methods on `karr skill` and `karr init`, and
10 # `use Path::Tiny;` would do the same with path() (ticket #38, t/121).
11 3 ▶use App::karr::Error ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Path::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use File::ShareDir ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # Nothing is required of the consumer. _skill_content and _skill_files take
16 # only a skill name, _write_skill and _write_skill_files are handed both the target and
17 # the content, and none of them reaches for anything on $self -- which is the
18 # point of the role: `karr skill` is board-less while `karr init` composes
19 # App::karr::Role::BoardDiscovery, and the only way one helper can serve both
20 # is by depending on neither (the rule is ticket #141's, read from the other
21 # side).
22  
23  
24 # Where this file was loaded from, and how far above it the dist root sits.
25 # Both are derived from the package name so they cannot drift apart if the role
26 # is ever renamed or moved: lib/App/karr/Role/SkillFile.pm is four name parts
27 # below lib/, and lib/ is one more below the tree that also holds share/.
28 3 my @NAME_PARTS = split /::/, __PACKAGE__;
29 3 my $OWN_INC_KEY = join( '/', @NAME_PARTS ) . '.pm';
30 3 my $DIST_ROOT_UP = @NAME_PARTS + 1;
31  
32 # The skills that ship, in the order every command handles and reports them.
33 # Each is a directory under share/ and, by the same name, the directory a
34 # target gets (.claude/skills/kanban-issues-karr-ticket, ...). This is the one
35 # list of them: `karr skill` and `karr init` both iterate it, so adding or
36 # splitting a skill is a change here and under share/, nowhere else.
37 1 ▶sub _skill_names { qw( kanban-issues-karr-coordination kanban-issues-karr-ticket ) }
Subroutine _skill_names: called CC=1 SCAR=0.0
38  
39 # Names earlier releases installed and this one no longer ships. A target
40 # directory by one of these names is a leftover: install and update remove it,
41 # check reports it stale. kanban-issues-karr-cli is the single skill the pair
42 # above was split out of; left in place it would brief an agent twice, once
43 # with the old text.
44 0 ▶sub _retired_skill_names { qw( kanban-issues-karr-cli ) }
Subroutine _retired_skill_names: not called CC=1 SCAR=6.9
45  
46 # One bundled skill directory. Two places to look, in order: File::ShareDir,
47 # which is where share/ lands when the dist is installed, and -- when it is
48 # not, i.e. a checkout being run with -Ilib -- share/ in that checkout. A
49 # share dir that answers but holds no NAME/SKILL.md (an App::karr from before
50 # #285, which shipped one claude-skill.md, or one from before the split, which
51 # shipped only kanban-issues-karr-cli/, is exactly that) falls through to the
52 # checkout rather than counting as found. Looked up per skill, so every name
53 # answers from wherever it is actually found.
54 #
55 # The second half has to know where that checkout is, and the only thing that
56 # knows is a file of the dist Perl has already loaded. Cmd::Skill and Cmd::Init
57 # each asked %INC for their own ($INC{'App/karr/Cmd/Skill.pm'} against
58 # $INC{'App/karr/Cmd/Init.pm'}), and that one line was the whole difference
59 # between their two copies of this sub (ticket #146). Naming either command's
60 # file from here would be the wrong fix twice over: it answers for one caller
61 # and sends the other silently on to the die below, and since MooX::Cmd decides
62 # which command classes get loaded, whether the miss happens would depend on how
63 # karr was invoked rather than showing up the first time. This file is the
64 # honest anchor instead. It belongs to the same dist as the share/ being looked
65 # for, it cannot fail to be loaded while one of its own methods is running, and
66 # it sits at the same depth below lib/ as the two command classes, so the climb
67 # is the one both copies made.
68 0 ▶sub _skill_source_dir {
Subroutine _skill_source_dir: not called CC=5 SCAR=34.0
69 0 my ($self, $name) = @_;
70  
71 # Installed dist: File::ShareDir knows where share/ went.
72 0 my $installed = eval {
73 0 my $dir = Path::Tiny::path( File::ShareDir::dist_dir('App-karr') )
74 ->child($name);
75 0 ▶ $dir->child('SKILL.md')->exists ? $dir : undef;
Branch: 1 branch
BranchTrueFalseTotal
$dir->child('SKILL.md')->exists ? : 0 0 0
76 };
77 0 ▶ return $installed if $installed;
Branch: 1 branch
BranchTrueFalseTotal
if $installed 0 0 0
78  
79 # Not installed: the share/ of the tree this file came out of.
80 0 my $own_path = $INC{$OWN_INC_KEY};
81 0 ▶ if ($own_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($own_path) 0 0 0
82 0 my $share = Path::Tiny::path($own_path)->parent($DIST_ROOT_UP)
83 ->child( 'share', $name );
84 0 ▶ return $share if $share->child('SKILL.md')->exists;
Branch: 1 branch
BranchTrueFalseTotal
if $share->child("SKILL.md")->exists 0 0 0
85 }
86  
87 0 die "Could not find $name/SKILL.md. Is App::karr properly installed?\n";
88 }
89  
90 # One bundled skill's SKILL.md, as characters (slurp_utf8 is Path::Tiny's own
91 # character-level read, which is what the file edge is allowed to use; decoding
92 # on top of it would be the double decode App::karr::Encoding forbids). This is
93 # what `karr skill show` prints: the entry point, not the references behind it.
94 0 ▶sub _skill_content {
Subroutine _skill_content: not called CC=1 SCAR=6.9
95 0 my ($self, $name) = @_;
96 0 return $self->_skill_source_dir($name)->child('SKILL.md')->slurp_utf8;
97 }
98  
99 # Every file one skill ships, as (relative path => characters) pairs in sorted
100 # path order: SKILL.md, then references/*.md. Walked from the directory rather
101 # than listed, so a reference file added under share/ ships without anyone
102 # touching this role; only *.md counts, so an editor backup next to them never
103 # lands in someone's .claude. The relative path is what the writers below
104 # rejoin to a target directory, so the layout under share/ IS the layout a
105 # target gets. Read with slurp_utf8 for the reason given on _skill_content.
106 0 ▶sub _skill_files {
Subroutine _skill_files: not called CC=1 SCAR=6.9
107 0 my ($self, $name) = @_;
108 0 my $dir = $self->_skill_source_dir($name);
109 0 my @found;
110 $dir->visit(
111 0 ▶ sub { my ($p) = @_; push @found, $p if $p->is_file && $p->basename =~ /\.md\z/ },
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if $p->is_file and $p->basename =~ /\.md\z/ 0 0 0
Condition0%0/3
$p->is_file and $p->basename =~ /\.md\z/
!ll&&!rl&&r
000
MC/DC0%0/2
$p->is_file and $p->basename =~ /\.md\z/
$p->is_file $p->basename =~ /\.md\z/
Truth table0%0/3
A and B
A= $p->is_fileB= $p->basename =~ /\.md\z/
ABresult
0X0
100
111
112 0 { recurse => 1 },
113 );
114 0 return map { ( $_ => $dir->child($_)->slurp_utf8 ) }
115 0 sort map { $_->relative($dir)->stringify } @found;
116 }
117  
118 # The retired skill directories present under $base -- the directory that
119 # holds the skill directories (.claude/skills, ~/.codex/skills, ...) -- as
120 # Path::Tiny objects. This is what check reports as stale. A dangling symlink
121 # by that name counts too: it is a leftover all the same.
122 0 ▶sub _retired_skill_dirs {
Subroutine _retired_skill_dirs: not called CC=2 SCAR=17.9
123 0 my ($self, $base) = @_;
124 0 ▶ return grep { -e "$_" || -l "$_" }
Condition0%0/3
-e "$_" || -l "$_"
l!l&&r!l&&!r
000
MC/DC0%0/2
-e "$_" || -l "$_"
-e "$_" -l "$_"
Truth table0%0/3
A || B
A= -e "$_"B= -l "$_"
ABresult
1X1
011
000
125 0 map { $base->child($_) } $self->_retired_skill_names;
126 }
127  
128 # Removes every retired skill directory under $base and returns the ones it
129 # removed, for the caller to report. File::Path unlinks a symlink rather than
130 # following it, and a file that is one link of a hardlink chain loses only
131 # this project's link, so another project still sharing the old skill keeps
132 # its copy. A failure is the target's layout, not a karr bug: one clean line
133 # naming the directory, like the write (#77). remove_tree reports some
134 # failures only as warnings, so what counts is whether the path is gone.
135 0 ▶sub _remove_retired_skills {
Subroutine _remove_retired_skills: not called CC=6 SCAR=37.4
136 0 my ($self, $base) = @_;
137 0 my @removed;
138 0 for my $dir ( $self->_retired_skill_dirs($base) ) {
139 0 my $error;
140 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { local $SIG{"__WARN__"} = sub { $error //= $_[0]; } ; $dir->remove_tree({"safe", 0}); 1 } } or $error = $@ 0 0 0
141 0 ▶ local $SIG{__WARN__} = sub { $error //= $_[0] };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/3
$error //= $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$error //= $_[0]
$error $_[0]
Truth table0%0/3
A //= B
A= $errorB= $_[0]
ABresult
1X1
011
000
142 0 $dir->remove_tree( { safe => 0 } );
143 0 1;
144 } or $error = $@;
145 0 ▶ App::karr::Error::user_error( "Could not remove $dir: ",
Branch: 1 branch
BranchTrueFalseTotal
if -e "$dir" or -l "$dir" 0 0 0
Condition0%0/5
-e "$dir" or -l "$dir"
l!l&&r!l&&!r
000
$error // "still there"
l!l
00
MC/DC0%0/2
-e "$dir" or -l "$dir"
-e "$dir" -l "$dir"
MC/DC0%0/1
$error // "still there"
$error
Truth table0%0/3
A or B
A= -e "$dir"B= -l "$dir"
ABresult
1X1
011
000
Truth table0%0/2
A
A= $error
Aresult
11
00
146 App::karr::Error::clean_error( $error // 'still there' ) )
147 if -e "$dir" || -l "$dir";
148 0 push @removed, $dir;
149 }
150 0 return @removed;
151 }
152  
153 # Written in place, on purpose. Path::Tiny's spew_utf8 writes a temp file and
154 # renames it over the target, so the path it wrote comes back on a *new* inode.
155 # For a SKILL.md that is the wrong move: skill files are kept as hardlink
156 # chains (manage-skills), one inode behind the same relative path in dozens of
157 # projects, so the rename silently breaks the updated path out of its chain --
158 # that one path gets the new text, every other project keeps the old inode with
159 # the old text, and the link count drops with nothing said (ticket #142, found
160 # in kubernetes-ocp, where the workaround was `karr skill show` into a shell
161 # redirect; ticket #145 for the same call left standing in `karr init
162 # --claude-skill`, which is why this lives in a role instead of in one command).
163 #
164 # append_utf8 with truncate is the in-place counterpart: Path::Tiny sysopens
165 # the existing inode for writing, locks it, truncates, and writes through it,
166 # so every link sees the new content. It is Path::Tiny's own UTF-8, i.e. still
167 # character-level, which is what the file edge is allowed to use -- encoding on
168 # top of it would be the double encode App::karr::Encoding forbids. A target
169 # that does not exist yet is created by the same call (">" with O_CREAT), so
170 # install, update and init share this one path.
171 11 ▶sub _write_skill {
Subroutine _write_skill: called CC=7 SCAR=19.5
172 11 my ($self, $file, $content) = @_;
173  
174 11 ▶ eval { $file->parent->mkpath; 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $file->parent->mkpath; 1 } } or App::karr::Error::user_error("Could not write ${file}: ", App::karr::Error::clean_error($@)) 11 0 11
175 or App::karr::Error::user_error( "Could not write $file: ",
176 App::karr::Error::clean_error($@) );
177  
178 11 ▶ return if eval { $file->append_utf8( { truncate => 1 }, $content ); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $file->append_utf8({"truncate", 1}, $content); 1 } } 8 3 11
179 3 my $in_place_error = $@;
180  
181 # Opening the file for writing is the one thing the rename never needed: it
182 # only needs a writable *directory*, so it used to update a read-only
183 # SKILL.md happily. Keep that working rather than turning a mode bit into a
184 # failure -- but this is now the only way a chain can break, so when the
185 # target really was hardlinked, say so instead of breaking it silently.
186 3 my $links = ( stat "$file" )[3];
187 3 ▶ eval { $file->spew_utf8($content); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $file->spew_utf8($content); 1 } } or App::karr::Error::user_error("Could not write ${file}: ", App::karr::Error::clean_error($in_place_error)) 2 1 3
188 or App::karr::Error::user_error( "Could not write $file: ",
189 App::karr::Error::clean_error($in_place_error) );
190  
191 2 ▶ if ( $links && $links > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($links and $links > 1) 1 1 2
Condition66%2/3
$links and $links > 1
!ll&&!rl&&r
011
MC/DC50%1/2
$links and $links > 1
$links $links > 1
Truth table66%2/3
A and B
A= $linksB= $links > 1
ABresult
0X0
100
111
192 1 my $others = $links - 1;
193 1 ▶ my $note = $others == 1
Branch: 1 branch
BranchTrueFalseTotal
$others == 1 ? : 1 0 1
194 ? 'one other hardlink to it still holds the previous content.'
195 : "$others other hardlinks to it still hold the previous content.";
196 1 warn "Warning: $file could not be written in place ("
197 . App::karr::Error::clean_error($in_place_error)
198 . ") and was replaced instead;\n$note\n";
199 }
200  
201 2 return;
202 }
203  
204 # A set of files into one target skill directory. $files is keyed the way
205 # _skill_files hands them out -- path relative to the skill directory -- and
206 # holds either the whole set (install, init) or the part of it a caller found
207 # missing or stale (update). Each one goes through _write_skill, so references/
208 # gets created on the way and an existing file keeps its inode. All three
209 # writers come through here: one description of how a skill directory gets
210 # written rather than three that drift (#285).
211 2 ▶sub _write_skill_files {
Subroutine _write_skill_files: called CC=2 SCAR=6.9
212 2 my ($self, $dir, $files) = @_;
213 2 $self->_write_skill( $dir->child($_), $files->{$_} ) for sort keys %$files;
214 2 return;
215 }
216  
217 3 1;
218  
219 __END__
« blib/lib/App/karr/Foundation/Limits.pm ↑ Summary blib/lib/App/karr/Cmd/Import.pm »