lib/App/FuguBench/Hook.pm

filter: Statementstmt 28.5% 54 / 189 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 42.5% 17 / 40 Podpod 100% 6 / 6 totaltotal 22.3% 77 / 344 CC 50 SCAR 33.0
CC
50
Cov
19%
_install
90.0
_project
72.0
_run
56.0
CRAP 27.0
« lib/App/FuguBench/Dist.pm ↑ Summary lib/App/FuguBench/Traces.pm »
1 # ex:ts=8 sw=4:
2 # $OpenBSD$
3 #
4 # Copyright (c) 2026 Dick Olsson <hi@senzilla.io>
5 #
6 # Permission to use, copy, modify, and distribute this software for any
7 # purpose with or without fee is hereby granted, provided that the above
8 # copyright notice and this permission notice appear in all copies.
9 #
10 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  
18 package App::FuguBench::Hook;
19 117 our $VERSION = '0.1.1';
20  
21 117 ▶use v5.34;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 117 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 117 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 117 ▶no feature qw(indirect multidimensional bareword_filehandles);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 117 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 117 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 117 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 117 ▶use Fugu::CLI qw(EXIT_SUCCESS EXIT_ERROR);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 117 ▶use Fugu::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 117 ▶use App::FuguBench::Checkout;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 117 ▶use App::FuguBench::Wiki;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 117 ▶use App::FuguBench::Worktree;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 # App::FuguBench::Hook - the hook verb.
38 #
39 # The verb answers one hook event of Claude Code. The harness writes
40 # a JSON payload to standard input, and the verb reads that payload
41 # itself, so no hook command needs jq (D-07). The events are
42 # SessionStart, SessionEnd, WorktreeCreate, and WorktreeRemove. A
43 # word outside the four is a usage error (HOOK-EVENTS-1).
44 #
45 # This verb and the trace verb hold the Claude Code assumptions of
46 # the program, and every other verb is agent-agnostic (D-10). The
47 # payload shape lives here, and in no shared module. The settings
48 # path, the keys of that file, the entries, and the worktree settings
49 # live here too, and the doctor reads each one from this module.
50 #
51 # The payload comes first, and the checkout after. The verb builds
52 # the checkout from the payload cwd, and it sets that checkout on the
53 # dispatcher (HOOK-EVENTS-5). A session can start in a worktree, and
54 # a worktree is a checkout with its own library clone. The option -C
55 # names the root ahead of the payload (CLI-CHECKOUT-1).
56 #
57 # The verb runs the other verbs in process. It calls the body of the
58 # Fugu::CLI entry of a verb with the dispatcher and the arguments, as
59 # the dispatcher does, so no child perl runs and no option parses
60 # twice.
61 #
62 # A session event must never stop a session, so it maps every
63 # non-zero code to a warning and returns zero (HOOK-EVENTS-3).
64 # WorktreeCreate returns the code of worktree create, because the
65 # harness needs the path of the worktree.
66 #
67 # The install subcommand writes the entries of those four events into
68 # the settings file of the checkout (HOOK-INSTALL-1). It reads no
69 # payload, and it takes the checkout of the dispatcher.
70  
71 # The events of the verb (HOOK-EVENTS-1). Each value holds the
72 # handler of the event and the timeout of its settings entry, in
73 # seconds (HOOK-INSTALL-3). One table holds the four events, so the
74 # dispatch and the installer can never name a different set.
75 117 my %EVENT = (
76 SessionStart => [ \&_session_start, 60 ],
77 SessionEnd => [ \&_session_end, 30 ],
78 WorktreeCreate => [ \&_worktree_create, 120 ],
79 WorktreeRemove => [ \&_worktree_remove, 60 ],
80 );
81  
82 # The shape of a project name that comes from a path (WIKI-OPEN-5).
83 # The name reaches git as an argument of wiki open, and a name that
84 # starts with a dash reaches git as an option.
85 117 my $PROJECT = qr{\A[A-Za-z0-9][A-Za-z0-9._-]*\z};
86  
87 # The command of each settings entry (HOOK-INSTALL-2). The harness
88 # expands CLAUDE_PROJECT_DIR, and the quotes hold a root that carries
89 # a space. The command names the shim and the event, and nothing
90 # else, so no entry needs jq (D-07).
91 117 my $SHIM = '"$CLAUDE_PROJECT_DIR/scripts/fugubench" hook ';
92  
93 # The settings file of one checkout, under its root
94 # (HOOK-INSTALL-1), and the two keys of it that the install owns:
95 # hooks for the entries, and worktree for the base reference
96 # (HOOK-INSTALL-4). Claude Code names the path and the keys, so they
97 # live in this verb (D-10), and settings_path, hooks_key, and
98 # worktree_key give them to the doctor.
99 117 ▶use constant SETTINGS_DIR => '.claude';
Subroutine BEGIN: called CC=1 SCAR=0.0
100 117 ▶use constant SETTINGS_FILE => 'settings.json';
Subroutine BEGIN: called CC=1 SCAR=0.0
101 117 ▶use constant HOOKS_KEY => 'hooks';
Subroutine BEGIN: called CC=1 SCAR=0.0
102 117 ▶use constant WORKTREE_KEY => 'worktree';
Subroutine BEGIN: called CC=1 SCAR=0.0
103  
104 # The JSON of the verb: the decoder of one payload, and the decoder
105 # and the encoder of the settings file. It takes the bytes and it
106 # handles the UTF-8 itself. An :encoding layer loads the
107 # PerlIO::encoding extension at the first read, and no row of the
108 # sandbox gives a promise for the load of a shared object.
109 #
110 # The encoder sorts the keys, indents with two spaces, and writes one
111 # space after a colon and none in front of it. Sorted keys make the
112 # second install byte-equal to the first one, and prettier writes
113 # that same shape.
114 117 my $JSON =
115 JSON::PP->new->utf8->canonical->indent->indent_length(2)->space_after;
116  
117 # App::FuguBench::Hook->command($verb):
118 # The entry of the Fugu::CLI table. The module holds one verb,
119 # so it ignores the name.
120 117 ▶sub command ( $, $ )
Subroutine command: called CC=1 SCAR=1.2
Pod documented
121 {
122 return {
123 summary => 'answer one Claude Code hook event',
124 usage => 'install | SessionStart | SessionEnd'
125 . ' | WorktreeCreate | WorktreeRemove',
126 0 ▶ run => sub ( $app, @argv ) { return _run( $app, @argv ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
127 117 };
128 }
129  
130 # _run($app, @argv):
131 # The body of the verb. It reads one word as its first argument,
132 # and it takes no other argument. The word install names the
133 # subcommand, and every other word names an event.
134 #
135 # The subcommand comes in front of the payload read, because it
136 # answers no event and reads no standard input.
137 #
138 # The two early exits answer every event with the code zero. A
139 # payload that does not parse warns (HOOK-EVENTS-2), and a
140 # payload of a sub-agent stops the verb with no change
141 # (HOOK-EVENTS-4). An observer dispatches an operator for each
142 # step and a verifier for each claim, so without that exit each
143 # of them opens a page of its own.
144 0 ▶sub _run ( $app, @argv )
Subroutine _run: not called CC=7 SCAR=40.3
145 {
146 0 my $word = shift @argv;
147 0 ▶ return $app->cli->command_usage_error('hook')
Branch: 1 branch
BranchTrueFalseTotal
if not defined $word or @argv 0 0 0
Condition0%0/3
not defined $word or @argv
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $word or @argv
not defined $word @argv
Truth table0%0/3
not(A) or B
A= not defined $wordB= @argv
ABresult
1X1
011
000
148 if !defined $word || @argv;
149  
150 0 ▶ return _install($app) if $word eq 'install';
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "install" 0 0 0
151  
152 0 my $event = $EVENT{$word};
153 0 ▶ return $app->cli->command_usage_error('hook') unless $event;
Branch: 1 branch
BranchTrueFalseTotal
unless $event 0 0 0
154  
155 0 my $payload = _payload($app);
156 0 ▶ return EXIT_SUCCESS unless $payload;
Branch: 1 branch
BranchTrueFalseTotal
unless $payload 0 0 0
157 0 ▶ return EXIT_SUCCESS if defined $payload->{agent_id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $payload->{"agent_id"} 0 0 0
158  
159 0 return $event->[0]->( $app, $payload );
160 }
161  
162 # _payload($app):
163 # The payload of the event: the whole standard input, decoded as
164 # JSON (HOOK-EVENTS-2). The method warns and returns undef when
165 # the text is no JSON object.
166 0 ▶sub _payload ($app)
Subroutine _payload: not called CC=3 SCAR=24.8
167 {
168 # The harness writes the payload to standard input, so the
169 # verb reads that handle by name. <> and <ARGV> read the files
170 # that @ARGV names, and @ARGV holds the verb and the event.
171 0 my $raw = do {
172 ## no critic (InputOutput::ProhibitExplicitStdin)
173 0 local $/ = undef;
174 0 <STDIN>;
175 };
176 0 ▶ my $payload = eval { $JSON->decode( $raw // q{} ) };
Condition0%0/2
$raw // ""
l!l
00
MC/DC0%0/1
$raw // ""
$raw
Truth table0%0/2
A
A= $raw
Aresult
11
00
177 0 ▶ return $payload if ref $payload eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $payload eq "HASH" 0 0 0
178  
179 0 $app->cli->log->warning('the hook payload does not parse');
180  
181 0 return;
182 }
183  
184 # _checkout($app, $cwd):
185 # The checkout of the payload, set on the dispatcher
186 # (HOOK-EVENTS-5). The method warns and returns undef when no
187 # .toolingrc sits above the cwd.
188 #
189 # The walk starts at the cwd, and it cuts no path at a marker,
190 # so a session in a worktree reads the worktree
191 # (CLI-CHECKOUT-4).
192 #
193 # With -C the dispatcher owns the start, and the value names the
194 # root ahead of the payload (CLI-CHECKOUT-1). The call of the
195 # dispatcher reports a walk that finds nothing.
196 0 ▶sub _checkout ( $app, $cwd )
Subroutine _checkout: not called CC=3 SCAR=24.8
197 {
198 0 ▶ return $app->checkout if defined $app->cli->option('C');
Branch: 1 branch
BranchTrueFalseTotal
if defined $app->cli->option("C") 0 0 0
199  
200 0 my $checkout = App::FuguBench::Checkout->new( start => $cwd );
201 0 ▶ unless ($checkout) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($checkout) 0 0 0
202 0 $app->cli->log->warning( 'no .toolingrc above %s', $cwd );
203 0 return;
204 }
205  
206 0 return $app->checkout($checkout);
207 }
208  
209 # _verb($app, $module, $verb, @args):
210 # Run one verb of the program in process, and return its exit
211 # code. The entry of the Fugu::CLI table holds the body, and the
212 # dispatcher runs that same body.
213 #
214 # The call takes the body, and not the wrapper of the
215 # dispatcher, so no second sandbox entry runs. The row of the
216 # hook verb pledges the promises of every verb that it calls.
217 0 ▶sub _verb ( $app, $module, $verb, @args )
Subroutine _verb: not called CC=1 SCAR=6.9
218 {
219 0 return $module->command($verb)->{run}->( $app, @args );
220 }
221  
222 # _quiet($app, $module, $verb, @args):
223 # Run one verb with its standard output on standard error, and
224 # return its exit code. A hook reads standard output, so the
225 # result line of an event must reach it alone (CLI-PROGRAM-4).
226 # The result of a call inside an event is no result of the
227 # event.
228 #
229 # The alias holds for the call, and local puts the glob back
230 # after it. The verb writes its line to the selected handle, and
231 # that handle is the STDOUT glob.
232 0 ▶sub _quiet ( $app, $module, $verb, @args )
Subroutine _quiet: not called CC=1 SCAR=6.9
233 {
234 0 local *STDOUT = *STDERR;
235  
236 0 return _verb( $app, $module, $verb, @args );
237 }
238  
239 # _never_stop($app, $what, $code):
240 # Map a non-zero code of one call to a warning, and return zero
241 # (HOOK-EVENTS-3). A session event must never stop a session.
242 # The verb of the call reported the reason already, so the
243 # warning names the call alone.
244 0 ▶sub _never_stop ( $app, $what, $code )
Subroutine _never_stop: not called CC=2 SCAR=17.9
245 {
246 0 ▶ $app->cli->log->warning( '%s exited %d', $what, $code )
Branch: 1 branch
BranchTrueFalseTotal
if $code != 0 0 0 0
247 if $code != EXIT_SUCCESS;
248  
249 0 return EXIT_SUCCESS;
250 }
251  
252 # _session($app, $payload):
253 # The cwd and the session identifier of a session event, in that
254 # order. The method warns and returns the empty list when the
255 # payload omits one of the two.
256 #
257 # The identifier reaches git through wiki open, so the method
258 # replaces each character outside a letter, a digit, a dot, a
259 # dash, and an underscore with a dash (HOOK-SESSION-1).
260 0 ▶sub _session ( $app, $payload )
Subroutine _session: not called CC=3 SCAR=24.8
261 {
262 0 my $cwd = $payload->{cwd};
263 0 my $session = $payload->{session_id};
264 0 ▶ unless ( defined $cwd && defined $session ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cwd and defined $session) 0 0 0
Condition0%0/3
defined $cwd and defined $session
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cwd and defined $session
defined $cwd defined $session
Truth table0%0/3
A and B
A= defined $cwdB= defined $session
ABresult
0X0
100
111
265 0 $app->cli->log->warning(
266 'the hook payload names no cwd or no session');
267 0 return;
268 }
269 0 $session =~ s/[^A-Za-z0-9._-]/-/g;
270  
271 0 return ( $cwd, $session );
272 }
273  
274 # _session_start($app, $payload):
275 # Clone the library, and start the session page: wiki init
276 # first, and then wiki open (HOOK-SESSION-1).
277 #
278 # The page name of wiki open is the result of the event, and it
279 # reaches standard output alone (CLI-PROGRAM-4). The harness
280 # adds that line to the context of the session. init writes the
281 # directory of a clone that it makes (WIKI-CLONE-1), so that
282 # call runs with its standard output on standard error.
283 0 ▶sub _session_start ( $app, $payload )
Subroutine _session_start: not called CC=3 SCAR=24.8
284 {
285 0 my ( $cwd, $session ) = _session( $app, $payload );
286 0 ▶ return EXIT_SUCCESS unless defined $session;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $session 0 0 0
287  
288 0 my $checkout = _checkout( $app, $cwd );
289 0 ▶ return EXIT_SUCCESS unless $checkout;
Branch: 1 branch
BranchTrueFalseTotal
unless $checkout 0 0 0
290  
291 0 _never_stop( $app, 'wiki init',
292 _quiet( $app, 'App::FuguBench::Wiki', 'wiki', 'init' ) );
293  
294 0 return _never_stop(
295 $app,
296 'wiki open',
297 _verb(
298 $app, 'App::FuguBench::Wiki', 'wiki', 'open',
299 _project( $app, $checkout, $cwd ), $session
300 ) );
301 }
302  
303 # _session_end($app, $payload):
304 # Close the session page (HOOK-SESSION-3). A session with no
305 # page is normal, and wiki close reports it.
306 0 ▶sub _session_end ( $app, $payload )
Subroutine _session_end: not called CC=3 SCAR=24.8
307 {
308 0 my ( $cwd, $session ) = _session( $app, $payload );
309 0 ▶ return EXIT_SUCCESS unless defined $session;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $session 0 0 0
310  
311 0 ▶ return EXIT_SUCCESS unless _checkout( $app, $cwd );
Branch: 1 branch
BranchTrueFalseTotal
unless _checkout($app, $cwd) 0 0 0
312  
313 0 return _never_stop(
314 $app,
315 'wiki close',
316 _verb(
317 $app, 'App::FuguBench::Wiki', 'wiki', 'close', $session
318 ) );
319 }
320  
321 # _project($app, $checkout, $cwd):
322 # The project of the session (HOOK-SESSION-2): the child of the
323 # projects directory that holds the cwd, and otherwise the
324 # wiki.project value.
325 #
326 # The home of wiki.origin anchors wiki.projects, as it anchors
327 # every wiki. value (CLI-CONFIG-2). A checkout with no
328 # wiki.origin holds no library, and the key has no home there,
329 # so the root anchors the value.
330 #
331 # The value must name a directory below that home
332 # (CLI-CONFIG-3), and the child must hold the token shape of
333 # wiki open. A value that fails the shape check warns, and the
334 # method then gives the wiki.project value.
335 #
336 # The method resolves both paths before the match, because one
337 # of them can hold a symbolic link that the other one resolves.
338 0 ▶sub _project ( $app, $checkout, $cwd )
Subroutine _project: not called CC=8 SCAR=42.8
339 {
340 0 my ($project) = $checkout->config('wiki.project');
341 0 ▶ my $home = ( $checkout->config('wiki.origin') )[1] // $checkout->root;
Condition0%0/3
($checkout->config("wiki.origin"))[1] // $checkout->root
l!l&&r!l&&!r
000
MC/DC0%0/2
($checkout->config("wiki.origin"))[1] // $checkout->root
($checkout->config("wiki.origin"))[1] $checkout->root
Truth table0%0/3
A // B
A= ($checkout->config("wiki.origin"))[1]B= $checkout->root
ABresult
1X1
011
000
342  
343 0 my ($value) = $checkout->config('wiki.projects');
344 0 my $dir = $checkout->dir_value($value);
345 0 ▶ unless ( defined $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dir) 0 0 0
346 0 $app->cli->log->warning( 'wiki.projects: %s',
347 $checkout->error );
348 0 return $project;
349 }
350  
351 0 my $under = Cwd::abs_path( File::Spec->catdir( $home, $dir ) );
352 0 my $abs = Cwd::abs_path($cwd);
353 0 ▶ return $project unless defined $under && defined $abs;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $under and defined $abs 0 0 0
Condition0%0/3
defined $under and defined $abs
!ll&&!rl&&r
000
MC/DC0%0/2
defined $under and defined $abs
defined $under defined $abs
Truth table0%0/3
A and B
A= defined $underB= defined $abs
ABresult
0X0
100
111
354 0 ▶ return $project unless index( $abs, "$under/" ) == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless index($abs, "$under/") == 0 0 0 0
355  
356 0 my ($child) = split m{/}, substr $abs, length "$under/";
357  
358 0 ▶ return defined $child && $child =~ $PROJECT ? $child : $project;
Branch: 1 branch
BranchTrueFalseTotal
defined $child && $child =~ /$PROJECT/u ? : 0 0 0
Condition0%0/3
defined $child and $child =~ /$PROJECT/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $child and $child =~ /$PROJECT/u
defined $child $child =~ /$PROJECT/u
Truth table0%0/3
A and B
A= defined $childB= $child =~ /$PROJECT/u
ABresult
0X0
100
111
359 }
360  
361 # _worktree_create($app, $payload):
362 # Make the worktree of the payload, and return the code of
363 # worktree create (HOOK-WORKTREE-1). That subcommand writes the
364 # path of the worktree to standard output as the only line, and
365 # the harness needs that path (WT-CREATE-5).
366 #
367 # So this event returns the code 1 after a failure, where a
368 # session event returns zero (HOOK-EVENTS-3). A payload that
369 # names no worktree, and a cwd with no checkout above it, both
370 # give that code and no path line.
371 #
372 # Claude Code runs the create hook again when a session
373 # reconnects, with the same name. The subcommand then bootstraps
374 # the worktree again, writes the path again, and returns zero
375 # (HOOK-WORKTREE-3, WT-CREATE-7).
376 0 ▶sub _worktree_create ( $app, $payload )
Subroutine _worktree_create: not called CC=4 SCAR=30.0
377 {
378 0 my $log = $app->cli->log;
379 0 my $name = $payload->{name};
380 0 ▶ unless ( defined $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 0 0
381 0 $log->error('the hook payload names no worktree');
382 0 return EXIT_ERROR;
383 }
384  
385 0 my $cwd = $payload->{cwd};
386 0 ▶ unless ( defined $cwd ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cwd) 0 0 0
387 0 $log->error('the hook payload names no cwd');
388 0 return EXIT_ERROR;
389 }
390 0 ▶ return EXIT_ERROR unless _checkout( $app, $cwd );
Branch: 1 branch
BranchTrueFalseTotal
unless _checkout($app, $cwd) 0 0 0
391  
392 0 return _verb( $app, 'App::FuguBench::Worktree', 'worktree', 'create',
393 $name );
394 }
395  
396 # _worktree_remove($app, $payload):
397 # Keep the worktree, print the manual command, and return zero
398 # (HOOK-WORKTREE-2). The event removes nothing (D-06), and it
399 # exits zero always (HOOK-EVENTS-3).
400 #
401 # Both lines go to standard error, because the event has no
402 # result (CLI-PROGRAM-4).
403 0 ▶sub _worktree_remove ( $app, $payload )
Subroutine _worktree_remove: not called CC=3 SCAR=24.8
404 {
405 0 my $log = $app->cli->log;
406 0 my $path = $payload->{worktree_path};
407 0 ▶ unless ( defined $path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path) 0 0 0
408 0 $log->warning('the hook payload names no worktree path');
409 0 return EXIT_SUCCESS;
410 }
411  
412 0 $log->notice( 'worktree kept: %s', $path );
413  
414 # The value goes in a variable first. A call in the argument
415 # list runs in list context, and a base that is absent then
416 # gives the empty list in place of one argument.
417 0 my $base = _base( $app, $payload );
418 0 my ( $root, $name ) = _split( $path, $base );
419 0 ▶ $log->notice( 'to remove it: make -C %s worktree-remove NAME=%s',
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 0 0 0
420 $root, $name )
421 if defined $name;
422  
423 0 return EXIT_SUCCESS;
424 }
425  
426 # _base($app, $payload):
427 # The worktree.base value of the checkout of the payload, or
428 # undef. That value names the segment that splits the worktree
429 # path.
430 #
431 # A payload with no cwd, a cwd with no checkout above it, and a
432 # value that fails the shape check all give undef. The event
433 # then prints the path alone, and it prints no command with a
434 # root that it guessed.
435 0 ▶sub _base ( $app, $payload )
Subroutine _base: not called CC=4 SCAR=30.0
436 {
437 0 my $cwd = $payload->{cwd};
438 0 ▶ return unless defined $cwd;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cwd 0 0 0
439  
440 0 my $checkout = _checkout( $app, $cwd );
441 0 ▶ return unless $checkout;
Branch: 1 branch
BranchTrueFalseTotal
unless $checkout 0 0 0
442  
443 0 my ($value) = $checkout->config('worktree.base');
444 0 my $base = $checkout->dir_value($value);
445 0 ▶ $app->cli->log->warning( 'worktree.base: %s', $checkout->error )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $base 0 0 0
446 unless defined $base;
447  
448 0 return $base;
449 }
450  
451 # _split($path, $base):
452 # The root and the name of one worktree path, in that order. The
453 # method splits the path at its last <base> segment: the part in
454 # front is the root, and the part after is the name. It returns
455 # the empty list without a base, without such a segment, and
456 # when no name follows the segment.
457 #
458 # The split serves the hint alone, and it finds no checkout, so
459 # CLI-CHECKOUT-4 holds.
460 0 ▶sub _split ( $path, $base )
Subroutine _split: not called CC=4 SCAR=30.0
461 {
462 0 ▶ return unless defined $base;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $base 0 0 0
463  
464 0 my $marker = "/$base/";
465 0 my $at = rindex $path, $marker;
466 0 ▶ return if $at < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $at < 0 0 0 0
467  
468 0 my $name = substr $path, $at + length $marker;
469 0 ▶ return unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 0 0
470  
471 0 return ( substr( $path, 0, $at ), $name );
472 }
473  
474 # App::FuguBench::Hook->entries:
475 # The settings entries of the four events, as a reference to a
476 # hash of the event names. Each value is one list with one
477 # matcher-less group, and the group holds one entry.
478 #
479 # An entry holds three keys: the type, the command, and the
480 # timeout of the event (HOOK-INSTALL-3). It carries no
481 # statusMessage, because the harness names the event itself.
482 #
483 # The doctor reads the same hash, so the report of an entry and
484 # the write of an entry never disagree.
485 0 ▶sub entries ($)
Subroutine entries: not called CC=2 SCAR=17.9
Pod documented
486 {
487 0 my %entries;
488 0 for my $event ( keys %EVENT ) {
489 my $entry = {
490 type => 'command',
491 command => $SHIM . $event,
492 0 timeout => $EVENT{$event}[1],
493 };
494 0 $entries{$event} = [ { hooks => [$entry] } ];
495 }
496  
497 0 return \%entries;
498 }
499  
500 # App::FuguBench::Hook->worktree:
501 # The worktree settings of the install, as a reference to a hash
502 # (HOOK-INSTALL-4). baseRef takes the value head, because a
503 # worktree starts at the local HEAD. Without the hooks, the
504 # built-in creation branches from origin/main and skips the
505 # bootstrap.
506 #
507 # The doctor reads the same hash, so the report of a value and
508 # the write of a value never disagree.
509 0 ▶sub worktree ($)
Subroutine worktree: not called CC=1 SCAR=6.9
Pod documented
510 {
511 0 return { baseRef => 'head' };
512 }
513  
514 # App::FuguBench::Hook->settings_path($root):
515 # The settings file of one checkout root (HOOK-INSTALL-1).
516 # Claude Code reads that path, so this verb holds it (D-10), and
517 # the doctor reads the file here.
518 0 ▶sub settings_path ( $, $root )
Subroutine settings_path: not called CC=1 SCAR=6.9
Pod documented
519 {
520 0 return File::Spec->catfile( $root, SETTINGS_DIR, SETTINGS_FILE );
521 }
522  
523 # App::FuguBench::Hook->hooks_key:
524 # The key of the settings file that holds the entries of the
525 # events (HOOK-INSTALL-1).
526 0 ▶sub hooks_key ($)
Subroutine hooks_key: not called CC=1 SCAR=6.9
Pod documented
527 {
528 0 return HOOKS_KEY;
529 }
530  
531 # App::FuguBench::Hook->worktree_key:
532 # The key of the settings file that holds the worktree settings
533 # (HOOK-INSTALL-4).
534 0 ▶sub worktree_key ($)
Subroutine worktree_key: not called CC=1 SCAR=6.9
Pod documented
535 {
536 0 return WORKTREE_KEY;
537 }
538  
539 # _install($app):
540 # Write the four entries and the base reference into
541 # .claude/settings.json of the checkout, and return the exit
542 # code (HOOK-INSTALL-1).
543 #
544 # The subcommand reads no payload, so it takes the checkout of
545 # the dispatcher. An operator runs it by hand, and -C names the
546 # root (CLI-CHECKOUT-1).
547 #
548 # The method keeps every key that it does not own, at the top
549 # level and under hooks. It replaces the list of each event of
550 # %EVENT, and it leaves every other event as it is, so the
551 # settings of the operator survive the write.
552 #
553 # The worktree method holds the value of baseRef, and the write
554 # takes it from there (HOOK-INSTALL-4). It keeps every other key
555 # of that object, as it keeps every other event.
556 #
557 # The keys reach the file in sorted order, so a second run writes
558 # the same bytes and causes no change (HOOK-INSTALL-1).
559 0 ▶sub _install ($app)
Subroutine _install: not called CC=9 SCAR=45.0
560 {
561 0 my $checkout = $app->checkout;
562 0 ▶ return Fugu::CLI::EXIT_CONFIG_ERROR() unless $checkout;
Branch: 1 branch
BranchTrueFalseTotal
unless $checkout 0 0 0
563  
564 0 my $dir = File::Spec->catdir( $checkout->root, SETTINGS_DIR );
565 0 my $path = __PACKAGE__->settings_path( $checkout->root );
566  
567 0 my $settings = _settings( $app, $path );
568 0 ▶ return EXIT_ERROR unless $settings;
Branch: 1 branch
BranchTrueFalseTotal
unless $settings 0 0 0
569  
570 0 my $hooks = _object( $app, $settings, HOOKS_KEY, $path );
571 0 ▶ return EXIT_ERROR unless $hooks;
Branch: 1 branch
BranchTrueFalseTotal
unless $hooks 0 0 0
572  
573 0 my $entries = __PACKAGE__->entries;
574 0 $hooks->{$_} = $entries->{$_} for keys %$entries;
575  
576 0 my $worktree = _object( $app, $settings, WORKTREE_KEY, $path );
577 0 ▶ return EXIT_ERROR unless $worktree;
Branch: 1 branch
BranchTrueFalseTotal
unless $worktree 0 0 0
578  
579 0 my $want = __PACKAGE__->worktree;
580 0 $worktree->{$_} = $want->{$_} for keys %$want;
581  
582 0 ▶ return EXIT_ERROR unless Fugu::File->ensure_dir($dir);
Branch: 1 branch
BranchTrueFalseTotal
unless "Fugu::File"->ensure_dir($dir) 0 0 0
583  
584 # The encoder ends the text with a newline of its own, after
585 # the last brace.
586 0 ▶ return EXIT_ERROR
Branch: 1 branch
BranchTrueFalseTotal
unless "Fugu::File"->write_atomic($path, $JSON->encode($settings)) 0 0 0
587 unless Fugu::File->write_atomic( $path, $JSON->encode($settings) );
588  
589 0 return EXIT_SUCCESS;
590 }
591  
592 # _settings($app, $path):
593 # The settings of one file, as a hash reference. An absent file
594 # gives an empty hash, and the subcommand then writes a new file.
595 #
596 # The method reports and returns undef when the file does not
597 # read, and when it holds no JSON object. An empty file holds
598 # none, and so does a file that lost a brace. The subcommand then
599 # writes nothing, and the operator repairs the file.
600 0 ▶sub _settings ( $app, $path )
Subroutine _settings: not called CC=4 SCAR=30.0
601 {
602 0 ▶ return {} unless -e $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $path 0 0 0
603  
604 0 my $text = Fugu::File->read($path);
605 0 ▶ unless ( defined $text ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $text) 0 0 0
606 0 $app->cli->log->error( 'cannot read %s', $path );
607 0 return;
608 }
609  
610 0 my $settings = eval { $JSON->decode($text) };
611 0 ▶ return $settings if ref $settings eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $settings eq "HASH" 0 0 0
612  
613 0 $app->cli->log->error( '%s holds no JSON object', $path );
614  
615 0 return;
616 }
617  
618 # _object($app, $settings, $key, $path):
619 # The object under one key of the settings. The method makes an
620 # empty object when the file omits the key, and it reports and
621 # returns undef when the key holds another kind of value.
622 #
623 # A hooks key of another kind belongs to no settings file that
624 # this subcommand can extend. The write then stops, and it
625 # destroys no file of the operator.
626 0 ▶sub _object ( $app, $settings, $key, $path )
Subroutine _object: not called CC=3 SCAR=24.8
627 {
628 0 ▶ my $value = $settings->{$key} //= {};
Condition0%0/2
$settings->{$key} //= {}
l!l
00
MC/DC0%0/1
$settings->{$key} //= {}
$settings->{$key}
Truth table0%0/2
A
A= $settings->{$key}
Aresult
11
00
629 0 ▶ return $value if ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" 0 0 0
630  
631 0 $app->cli->log->error( '%s: the %s key holds no object', $path, $key );
632  
633 0 return;
634 }
635  
636 117 1;
« lib/App/FuguBench/Dist.pm ↑ Summary lib/App/FuguBench/Traces.pm »