blib/lib/Skilletor/Hooks.pm

filter: Statementstmt 99.4% 352 / 354 Branchbran 87.5% 133 / 152 Conditioncond 74.1% 109 / 147 MC/DCmcdc 48.0% 37 / 77 Subroutinesub 100% 58 / 58 Podpod 50.0% 3 / 6 totaltotal 87.1% 692 / 794 CC 137 SCAR 19.7
CC
137
Cov
91%
_move_checkout_of
23.7
_moved_output
12.0
_session_start
10.0
CRAP 7.2
« lib/Skilletor/Source/Git.pm ↑ Summary lib/Skilletor/JSString.pm »
1 package Skilletor::Hooks;
2 # ABSTRACT: The session hooks – SessionStart, UserPromptSubmit, the background sync and the checkout moves (spec §8)
3  
4 205 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 205 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 205 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 205 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 205 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 205 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 205 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 205 ▶use Skilletor::FSUtil qw( decode_utf8_lossy encode_text node_fs_error path_join path_resolve same_path );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 205 ▶use Skilletor::GitEnv qw( git_env );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 205 ▶use Skilletor::JSON qw( js_ordered js_type );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 205 ▶use Skilletor::Process qw( run_command spawn_detached );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 205 our $VERSION = '0.008000';
18  
19 205 our @EXPORT_OK = qw( run_hook project_root_of default_background SESSION_START_TIMEOUT_MS DEFAULT_INTERVAL MOVE_DEADLINE_MS );
20  
21 # A SessionStart check and sync give each source this long (the reference's 5 s).
22 205 ▶use constant SESSION_START_TIMEOUT_MS => 5_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # The in-session check's interval when the config cannot be loaded; the config's own default
25 # (checkInterval, 1800 s) is Skilletor::Config's.
26 205 ▶use constant DEFAULT_INTERVAL => 600;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 # SessionStart or a move's sync writes nothing once this has passed (spec §8): Claude Code kills
29 # the hook at 30 s, which leaves the local rest of a sync 10 s. The two move hooks wait for the
30 # sync lock up to it as well. The exported name predates SessionStart's use of the same budget.
31 205 ▶use constant MOVE_DEADLINE_MS => 20_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # State, Config and Engine are loaded by the events that use them: a prompt with nothing due
34 # loads no engine. The report module is loaded by the first answer that says something (k199).
35 730 ▶sub display_safe { require Skilletor::Report; goto &Skilletor::Report::display_safe }
Subroutine display_safe: called CC=1 SCAR=0.0
Pod undocumented
36 22 ▶sub empty_scope_report { require Skilletor::Report; goto &Skilletor::Report::empty_scope_report }
Subroutine empty_scope_report: called CC=1 SCAR=0.0
Pod undocumented
37 1103 ▶sub report_hook { require Skilletor::Report; goto &Skilletor::Report::report_hook }
Subroutine report_hook: called CC=1 SCAR=0.0
Pod undocumented
38  
39 # The project of a directory, as the hooks and the CLI find it (spec §14.5): git's top level of
40 # $cwd – of the repository found from $cwd, never one an exported GIT_DIR names (k91) – else $cwd
41 # itself. The reference's projectRootOf: one git run, 2 s, git's environment. Never dies.
42 1497 ▶sub project_root_of {
Subroutine project_root_of: called CC=7 SCAR=20.2
Pod documented
43 1497 my ( $cwd ) = @_;
44 1497 my $r = eval {
45 1497 run_command( cmd => [ 'git', '-C', encode_text($cwd), 'rev-parse', '--show-toplevel' ], env => git_env(), timeout_ms => 2_000 );
46 };
47 1492 ▶ return $cwd unless $r && !$r->{spawn_error} && !$r->{timed_out} && defined $r->{code} && $r->{code} == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $r and not $r->{"spawn_error"} and not $r->{"timed_out"} and defined $r->{"code"} and $r->{"code"} == 0 1299 193 1492
Condition66%8/12
$r and not $r->{"spawn_error"} and not $r->{"timed_out"} and defined $r->{"code"} and $r->{"code"} == 0
!ll&&!rl&&r
1172127193
$r and not $r->{"spawn_error"} and not $r->{"timed_out"} and defined $r->{"code"}
!ll&&!rl&&r
11720320
$r and not $r->{"spawn_error"} and not $r->{"timed_out"}
!ll&&!rl&&r
01172320
$r and not $r->{"spawn_error"}
!ll&&!rl&&r
001492
MC/DC40%2/5
$r and not $r->{"spawn_error"} and not $r->{"timed_out"} and defined $r->{"code"} and $r->{"code"} == 0
$r not $r->{"spawn_error"} not $r->{"timed_out"} defined $r->{"code"} $r->{"code"} == 0
Truth table50%3/6
A and not(B) and not(C) and D and E
A= $rB= not $r->{"spawn_error"}C= not $r->{"timed_out"}D= defined $r->{"code"}E= $r->{"code"} == 0
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
48 193 ( my $top = decode_utf8_lossy( $r->{stdout} ) ) =~ s/\A\s+|\s+\z//g;
49 193 ▶ return length $top ? path_resolve($top) : $cwd;
Branch: 1 branch
BranchTrueFalseTotal
length $top ? : 193 0 193
50 }
51  
52 # A string's truthiness in JavaScript: set and not empty.
53 14902 ▶sub _truthy { defined $_[0] && length $_[0] }
Subroutine _truthy: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and length $_[0] 4274 10628 14902
54  
55 # A hook input's string field, when it is a non-empty one.
56 2316 ▶sub _text { js_type( $_[0] ) eq 'string' && length $_[0] ? $_[0] : undef }
Subroutine _text: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
js_type($_[0]) eq 'string' && length $_[0] ? : 1201 1115 2316
Condition66%2/3
js_type($_[0]) eq "string" and length $_[0]
!ll&&!rl&&r
111501201
MC/DC50%1/2
js_type($_[0]) eq "string" and length $_[0]
js_type($_[0]) eq "string" length $_[0]
Truth table66%2/3
A and B
A= js_type($_[0]) eq "string"B= length $_[0]
ABresult
0X0
100
111
57  
58 # The session a hook input belongs to; '' when it names none.
59 516 ▶sub _session_of { js_type( $_[0]{session_id} ) eq 'string' ? $_[0]{session_id} : '' }
Subroutine _session_of: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
js_type($_[0]{'session_id'}) eq 'string' ? : 505 11 516
60  
61 1980 ▶sub _project_key {
Subroutine _project_key: called CC=3 SCAR=13.1
62 1980 my ( $ctx, $input ) = @_;
63 1980 ▶ return $ctx->{project_dir} // _text( $input->{cwd} ) // '';
Condition40%2/5
($ctx->{"project_dir"} // _text($input->{"cwd"})) // ""
l!l
19800
$ctx->{"project_dir"} // _text($input->{"cwd"})
l!l&&r!l&&!r
198000
MC/DC0%0/2
($ctx->{"project_dir"} // _text($input->{"cwd"})) // ""
$ctx->{"project_dir"} _text($input->{"cwd"})
Truth table33%1/3
A // B
A= $ctx->{"project_dir"}B= _text($input->{"cwd"})
ABresult
1X1
011
000
64 }
65  
66 # The project dir, unless it is the home dir itself: then there is no project scope.
67 713 ▶sub _project_dir_of {
Subroutine _project_dir_of: called CC=3 SCAR=11.0
68 713 my ( $ctx ) = @_;
69 713 my $p = $ctx->{project_dir};
70 713 ▶ return undef if !_truthy($p) || same_path( $p, $ctx->{home} );
Branch: 1 branch
BranchTrueFalseTotal
if not _truthy($p) or same_path($p, $ctx->{"home"}) 33 680 713
Condition66%2/3
not _truthy($p) or same_path($p, $ctx->{"home"})
l!l&&r!l&&!r
033680
MC/DC50%1/2
not _truthy($p) or same_path($p, $ctx->{"home"})
not _truthy($p) same_path($p, $ctx->{"home"})
Truth table66%2/3
not(A) or B
A= not _truthy($p)B= same_path($p, $ctx->{"home"})
ABresult
1X1
011
000
71 680 return $p;
72 }
73  
74 # Claude Code's config dir as the hook's context gives it, else CLAUDE_CONFIG_DIR.
75 2997 ▶sub _claude_config_dir {
Subroutine _claude_config_dir: called CC=3 SCAR=11.7
76 2997 my ( $ctx ) = @_;
77 2997 ▶ return $ctx->{claude_config_dir} if _truthy( $ctx->{claude_config_dir} );
Branch: 1 branch
BranchTrueFalseTotal
if _truthy($ctx->{"claude_config_dir"}) 0 2997 2997
78 2997 ▶ return _truthy( $ENV{CLAUDE_CONFIG_DIR} ) ? decode_utf8_lossy( $ENV{CLAUDE_CONFIG_DIR} ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
_truthy($ENV{'CLAUDE_CONFIG_DIR'}) ? : 0 2997 2997
79 }
80  
81 # What a caught error says: a Skilletor::Error's message, else Perl's text without the
82 # " at FILE line N." it appends.
83 87 ▶sub _message {
Subroutine _message: called CC=3 SCAR=11.0
84 87 my ( $err ) = @_;
85 87 ▶ return $err->message if blessed $err && $err->can('message');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($err) and $err->can("message") 18 69 87
Condition66%2/3
builtin::blessed($err) and $err->can("message")
!ll&&!rl&&r
69018
MC/DC50%1/2
builtin::blessed($err) and $err->can("message")
builtin::blessed($err) $err->can("message")
Truth table66%2/3
A and B
A= builtin::blessed($err)B= $err->can("message")
ABresult
0X0
100
111
86 69 my $text = "$err";
87 69 $text =~ s/ at (?:(?! at ).)+ line \d+(?:, <[^>]*> (?:line|chunk) \d+)?\.\n\z//s;
88 69 chomp $text;
89 69 return $text;
90 }
91  
92 104 ▶sub _lock_timeout { blessed $_[0] && $_[0]->isa('Skilletor::Error::LockTimeout') }
Subroutine _lock_timeout: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) and $_[0]->isa("Skilletor::Error::LockTimeout") 36 68 104
93  
94 # One warning line; the message – an error, git's stderr, a name from a config – display-safe.
95 279 ▶sub _warn { js_ordered( systemMessage => display_safe( 'skilletor: '.$_[0] ) ) }
Subroutine _warn: called CC=1 SCAR=0.0
96  
97 # A sync report as hook output: the line and, under the event's name, the context.
98 1103 ▶sub _to_output {
Subroutine _to_output: called CC=5 SCAR=16.3
99 1103 my ( $report, $event ) = @_;
100 1103 my $h = report_hook($report);
101 1103 ▶ return js_ordered() unless _truthy( $h->{systemMessage} ) || _truthy( $h->{additionalContext} );
Branch: 1 branch
BranchTrueFalseTotal
unless _truthy($h->{"systemMessage"}) or _truthy($h->{"additionalContext"}) 389 714 1103
Condition66%2/3
_truthy($h->{"systemMessage"}) or _truthy($h->{"additionalContext"})
l!l&&r!l&&!r
7140389
MC/DC50%1/2
_truthy($h->{"systemMessage"}) or _truthy($h->{"additionalContext"})
_truthy($h->{"systemMessage"}) _truthy($h->{"additionalContext"})
Truth table66%2/3
A or B
A= _truthy($h->{"systemMessage"})B= _truthy($h->{"additionalContext"})
ABresult
1X1
011
000
102 return js_ordered(
103 ( defined $h->{systemMessage} ? ( systemMessage => $h->{systemMessage} ) : () ),
104 hookSpecificOutput =>
105 714 ▶ js_ordered( hookEventName => $event, ( defined $h->{additionalContext} ? ( additionalContext => $h->{additionalContext} ) : () ) ),
Branch: 2 branches
BranchTrueFalseTotal
defined $h->{'systemMessage'} ? : 714 0 714
defined $h->{'additionalContext'} ? : 705 9 714
106 );
107 }
108  
109 2125 ▶sub _state {
Subroutine _state: called CC=1 SCAR=0.0
110 2125 my ( $ctx ) = @_;
111 2125 require Skilletor::State;
112 2125 return Skilletor::State->new( root => $ctx->{state_root} );
113 }
114  
115 # The engine for the hook's project; %defaults (a per-source timeout) yield to the context's own
116 # engine arguments.
117 2284 ▶sub _engine {
Subroutine _engine: called CC=2 SCAR=6.9
118 2284 my ( $ctx, %defaults ) = @_;
119 2284 require Skilletor::Engine;
120 return Skilletor::Engine->new(
121 %defaults,
122 home => $ctx->{home},
123 project_dir => $ctx->{project_dir},
124 claude_config_dir => _claude_config_dir($ctx),
125 state_root => $ctx->{state_root},
126 2284 ▶ %{ $ctx->{engine} // {} },
Condition100%2/2
$ctx->{"engine"} // {}
l!l
22795
MC/DC100%1/1
$ctx->{"engine"} // {}
$ctx->{"engine"}
Truth table100%2/2
A
A= $ctx->{"engine"}
Aresult
11
00
127 );
128 }
129  
130 18 ▶sub _cwd {
Subroutine _cwd: called CC=2 SCAR=6.9
131 18 my $cwd = Cwd::getcwd();
132 18 ▶ die "ENOENT: process.cwd failed with error no such file or directory, uv_cwd\n" unless defined $cwd;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cwd 1 17 18
133 17 return decode_utf8_lossy($cwd);
134 }
135  
136 2116 ▶sub run_hook {
Subroutine run_hook: called CC=2 SCAR=6.9
Pod documented
137 2116 my ( $event, $input, $hook_ctx ) = @_;
138 2116 my $out;
139 2116 ▶ return $out if eval { $out = _dispatch( $event, $input, $hook_ctx ); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $out = _dispatch($event, $input, $hook_ctx); 1 } } 2013 1 2014
140 1 return _warn( _message($@) );
141 }
142  
143 2116 ▶sub _dispatch {
Subroutine _dispatch: called CC=8 SCAR=20.8
144 2116 my ( $event, $input, $hook_ctx ) = @_;
145 # The checkout the session moved into is the project, never the session's own (spec §8).
146 2116 ▶ return _cwd_changed( $input, $hook_ctx ) if $event eq 'cwd-changed';
Branch: 1 branch
BranchTrueFalseTotal
if $event eq "cwd-changed" 110 2006 2116
147 2006 ▶ return _post_tool_use( $input, $hook_ctx ) if $event eq 'post-tool-use';
Branch: 1 branch
BranchTrueFalseTotal
if $event eq "post-tool-use" 7 1999 2006
148 1999 ▶ my $ctx = _truthy( $hook_ctx->{project_dir} ) ? $hook_ctx : { %$hook_ctx, project_dir => project_root_of( _text( $input->{cwd} ) // _cwd() ) };
Branch: 1 branch
BranchTrueFalseTotal
_truthy($hook_ctx->{'project_dir'}) ? : 1480 519 1999
Condition100%3/3
_text($input->{"cwd"}) // _cwd()
l!l&&r!l&&!r
501171
MC/DC0%0/2
_text($input->{"cwd"}) // _cwd()
_text($input->{"cwd"}) _cwd()
Truth table66%2/3
A // B
A= _text($input->{"cwd"})B= _cwd()
ABresult
1X1
011
000
149 1998 ▶ return _session_start( $input, $ctx ) if $event eq 'session-start';
Branch: 1 branch
BranchTrueFalseTotal
if $event eq "session-start" 1183 815 1998
150 815 ▶ return _user_prompt_submit( $input, $ctx ) if $event eq 'user-prompt-submit';
Branch: 1 branch
BranchTrueFalseTotal
if $event eq "user-prompt-submit" 743 72 815
151 72 ▶ if ( $event eq '__sync-background' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event eq "__sync-background") 54 18 72
152 54 _sync_background( $input, $ctx );
153 52 return js_ordered();
154 }
155 18 return _warn("unknown hook event: $event");
156 }
157  
158 # Under Codex a new session (no source, `startup` or `clear`) gets the rules context; a resumed
159 # one still has the first copy in its history (spec §14.8). Under Hermes, while `hermes` is no
160 # target, the answer says how to opt in (spec §16.2).
161 1183 ▶sub _session_start {
Subroutine _session_start: called CC=10 SCAR=23.0
162 1183 my ( $input, $ctx ) = @_;
163 1183 my $out;
164 1183 ▶ $out = _warn( _message($@) ) unless eval { $out = _check_and_sync( $input, $ctx ); 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $out = _check_and_sync($input, $ctx); 1 } } 72 1064 1136
165 1136 _record_session_cwd( $input, $ctx );
166 1136 my $source = $input->{source};
167 1136 ▶ if ( ( $ctx->{harness} // '' ) eq 'codex'
Branch: 1 branch
BranchTrueFalseTotal
if (($ctx->{"harness"} // "") eq "codex" and !defined($source) || js_type($source) eq "string" && ($source eq "startup" || $source eq "clear")) 523 613 1136
Condition100%14/14
($ctx->{"harness"} // "") eq "codex" and !defined($source) || js_type($source) eq "string" && ($source eq "startup" || $source eq "clear")
!ll&&!rl&&r
52984523
$ctx->{"harness"} // ""
l!l
883253
not defined $source or js_type($source) eq "string" and $source eq "startup" || $source eq "clear"
l!l&&r!l&&!r
6645784
js_type($source) eq "string" and $source eq "startup" || $source eq "clear"
!ll&&!rl&&r
2856457
$source eq "startup" or $source eq "clear"
l!l&&r!l&&!r
4213656
MC/DC100%5/5
($ctx->{"harness"} // "") eq "codex" and !defined($source) || js_type($source) eq "string" && ($source eq "startup" || $source eq "clear")
$ctx->{"harness"} not defined $source js_type($source) eq "string" $source eq "startup" $source eq "clear"
Truth table100%6/6
A and not(B) or C and D or E
A= $ctx->{"harness"}B= not defined $sourceC= js_type($source) eq "string"D= $source eq "startup"E= $source eq "clear"
ABCDEresult
0XXXX0
100XX0
101000
11XXX1
1011X1
101011
168 && ( !defined $source || ( js_type($source) eq 'string' && ( $source eq 'startup' || $source eq 'clear' ) ) ) ) {
169 523 $out = _with_codex_rules( $out, $ctx );
170 }
171 1136 ▶ $out = _with_hermes_opt_in( $out, $ctx ) if ( $ctx->{harness} // '' ) eq 'hermes';
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx->{"harness"} // "") eq "hermes" 276 860 1136
Condition100%2/2
$ctx->{"harness"} // ""
l!l
883253
MC/DC100%1/1
$ctx->{"harness"} // ""
$ctx->{"harness"}
Truth table100%2/2
A
A= $ctx->{"harness"}
Aresult
11
00
172 1136 return $out;
173 }
174  
175 # Under Hermes while `hermes` is no target (spec §16.2): the opt-in line, once per answer – after
176 # its warning line, and after its context when it has one, since the plugin hands the model the
177 # context and the warning line only without one (spec §16.4).
178 276 ▶sub _with_hermes_opt_in {
Subroutine _with_hermes_opt_in: called CC=4 SCAR=13.9
179 276 my ( $out, $ctx ) = @_;
180 276 my $hint = _engine($ctx)->hermes_opt_in_hint;
181 276 ▶ return $out unless _truthy($hint);
Branch: 1 branch
BranchTrueFalseTotal
unless _truthy($hint) 183 93 276
182 93 my $line = _warn($hint)->{systemMessage};
183 93 my $result = js_ordered( map { ( $_ => $out->{$_} ) } keys %$out );
184 93 $result->{systemMessage} = join '; ', grep { _truthy($_) } $out->{systemMessage}, $line;
185 93 my $hso = $out->{hookSpecificOutput};
186 93 ▶ my $context = ref $hso eq 'HASH' ? $hso->{additionalContext} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $hso eq 'HASH' ? : 62 31 93
187 93 ▶ if ( _truthy($context) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_truthy($context)) 62 31 93
188 62 my $copy = js_ordered( map { ( $_ => $hso->{$_} ) } keys %$hso );
189 62 $copy->{additionalContext} = "$context\n$line";
190 62 $result->{hookSpecificOutput} = $copy;
191 }
192 93 return $result;
193 }
194  
195 # The Codex rules files, read from disk, at the start of the output's additionalContext, so the
196 # message begins with the rules marker the pointer names; a file that cannot be read is one more
197 # warning.
198 523 ▶sub _with_codex_rules {
Subroutine _with_codex_rules: called CC=7 SCAR=19.5
199 523 my ( $out, $ctx ) = @_;
200 523 my ( @texts, @problems );
201 523 for my $file ( _engine($ctx)->codex_rules_files ) {
202 839 my $text = _read_rules($file);
203 839 ▶ if ( ref $text ) { push @problems, "cannot read $file ($$text)" }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $text) { } 17 822 839
204 822 else { push @texts, $text }
205 }
206 523 my $result = js_ordered( map { ( $_ => $out->{$_} ) } keys %$out );
207 523 ▶ $result->{systemMessage} = join '; ', grep { _truthy($_) } $out->{systemMessage}, _warn( join '; ', @problems )->{systemMessage} if @problems;
Branch: 1 branch
BranchTrueFalseTotal
if @problems 17 506 523
208 523 ▶ if (@texts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@texts) 428 95 523
209 428 my $rules = join "\n", @texts;
210 428 ▶ my $report = ( $out->{hookSpecificOutput} // {} )->{additionalContext};
Condition100%2/2
$out->{"hookSpecificOutput"} // {}
l!l
251177
MC/DC100%1/1
$out->{"hookSpecificOutput"} // {}
$out->{"hookSpecificOutput"}
Truth table100%2/2
A
A= $out->{"hookSpecificOutput"}
Aresult
11
00
211 428 ▶ $result->{hookSpecificOutput} = js_ordered( hookEventName => 'SessionStart', additionalContext => _truthy($report) ? "$rules\n$report" : $rules );
Branch: 1 branch
BranchTrueFalseTotal
_truthy($report) ? : 251 177 428
212 }
213 523 return $result;
214 }
215  
216 # A file's text as readFileSync(file, "utf8") gives it, or a reference to Node's message.
217 856 ▶sub _read_rules {
Subroutine _read_rules: called CC=3 SCAR=11.0
218 856 my ( $file ) = @_;
219 856 ▶ open( my $fh, '<:raw', encode_text($file) ) or return \node_fs_error( 0 + $!, 'open', $file );
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", encode_text($file) or return \node_fs_error(0 + $!, "open", $file) 839 17 856
220 839 my $raw = do { local $/; <$fh> };
221 839 ▶ return \node_fs_error( 0 + $!, 'read' ) unless defined $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw 17 822 839
222 822 return decode_utf8_lossy($raw);
223 }
224  
225 1183 ▶sub _check_and_sync {
Subroutine _check_and_sync: called CC=7 SCAR=19.5
226 1183 my ( $input, $ctx ) = @_;
227 # The check, sync-lock wait and source resolution all precede the engine's first-write guard,
228 # so SessionStart's write budget starts before the check. An earlier caller deadline stays so.
229 1183 my $budget_deadline = _now_ms() + MOVE_DEADLINE_MS;
230 1183 ▶ my $deadline = defined $ctx->{deadline} && $ctx->{deadline} < $budget_deadline ? $ctx->{deadline} : $budget_deadline;
Branch: 1 branch
BranchTrueFalseTotal
defined $ctx->{'deadline'} && $ctx->{'deadline'} < $budget_deadline ? : 14 1169 1183
Condition66%2/3
defined $ctx->{"deadline"} and $ctx->{"deadline"} < $budget_deadline
!ll&&!rl&&r
1169014
MC/DC50%1/2
defined $ctx->{"deadline"} and $ctx->{"deadline"} < $budget_deadline
defined $ctx->{"deadline"} $ctx->{"deadline"} < $budget_deadline
Truth table66%2/3
A and B
A= defined $ctx->{"deadline"}B= $ctx->{"deadline"} < $budget_deadline
ABresult
0X0
100
111
231 1183 my $state = _state($ctx);
232 1183 my $key = _project_key( $ctx, $input );
233 1183 my $engine = _engine( $ctx, timeout_ms => SESSION_START_TIMEOUT_MS );
234  
235 1183 my $chk = $engine->check;
236 1159 ▶ return _warn( $chk->{error} ) if _truthy( $chk->{error} );
Branch: 1 branch
BranchTrueFalseTotal
if _truthy($chk->{"error"}) 64 1095 1159
237 1095 $state->mark_checked($key);
238  
239 1095 ▶ return _unchanged_output($chk) unless $chk->{changed};
Branch: 1 branch
BranchTrueFalseTotal
unless $chk->{"changed"} 87 1008 1095
240 1008 my $report;
241 1008 ▶ unless ( eval { $report = $engine->sync( deadline => $deadline ); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $report = $engine->sync("deadline", $deadline); 1 } }) 72 913 985
242 72 my $err = $@;
243 72 ▶ $state->due_again($key) if _lock_timeout($err);
Branch: 1 branch
BranchTrueFalseTotal
if _lock_timeout($err) 18 54 72
244 72 die $err;
245 }
246 913 return _to_output( $report, 'SessionStart' );
247 }
248  
249 # A check with nothing to sync: its warnings in one line – or, when a source awaits trust, the
250 # report a sync would give, trust requests and warnings alike (spec §4.3, §8).
251 87 ▶sub _unchanged_output {
Subroutine _unchanged_output: called CC=4 SCAR=14.0
252 87 my ( $chk ) = @_;
253 87 ▶ unless ( @{ $chk->{trustRequests} // [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$chk->{"trustRequests"} // [];}) 74 13 87
Condition100%2/2
$chk->{"trustRequests"} // []
l!l
1374
MC/DC100%1/1
$chk->{"trustRequests"} // []
$chk->{"trustRequests"}
Truth table100%2/2
A
A= $chk->{"trustRequests"}
Aresult
11
00
254 0 ▶ return @{ $chk->{warnings} } ? _warn( join '; ', @{ $chk->{warnings} } ) : js_ordered();
Branch: 1 branch
BranchTrueFalseTotal
@{$chk->{'warnings'};} ? : 0 74 74
255 }
256 13 return _to_output( _trust_report($chk), 'SessionStart' );
257 }
258  
259 # The report a sync would give of a check whose sources await trust (at least one).
260 22 ▶sub _trust_report {
Subroutine _trust_report: called CC=3 SCAR=11.0
261 22 my ( $chk ) = @_;
262 22 my $report = js_ordered( scopes => [] );
263 22 my %by_scope;
264 22 for my $t ( @{ $chk->{trustRequests} } ) {
265 22 ▶ my $rep = $by_scope{ $t->{scope} } //= do {
Condition33%1/3
$by_scope{$t->{"scope"}} //= do { my $r = empty_scope_report($t->{"scope"}); push @{$$report{"scopes"};}, $r; $r }
l!l&&r!l&&!r
0220
MC/DC0%0/2
$by_scope{$t->{"scope"}} //= do { my $r = empty_scope_report($t->{"scope"}); push @{$$report{"scopes"};}, $r; $r }
$by_scope{$t->{"scope"}} do { my $r = empty_scope_report($t->{"scope"}); push @{$$report{"scopes"};}, $r; $r }
Truth table33%1/3
A //= B
A= $by_scope{$t->{"scope"}}B= do { my $r = empty_scope_report($t->{"scope"}); push @{$$report{"scopes"};}, $r; $r }
ABresult
1X1
011
000
266 22 my $r = empty_scope_report( $t->{scope} );
267 22 push @{ $report->{scopes} }, $r;
268 22 $r;
269 };
270 22 push @{ $rep->{trustRequests} }, js_ordered( map { ( $_ => $t->{$_} ) } grep { $_ ne 'scope' } keys %$t );
271 }
272 22 push @{ $report->{scopes}[0]{warnings} }, @{ $chk->{warnings} };
273 22 return $report;
274 }
275  
276 # Where a session's SessionStart looked (k149). Best effort: without the record the first move
277 # looks once more.
278 1136 ▶sub _record_session_cwd {
Subroutine _record_session_cwd: called CC=2 SCAR=6.9
279 1136 my ( $input, $ctx ) = @_;
280 1136 ▶ my $cwd = _text( $input->{cwd} ) // return;
Condition100%2/2
_text($input->{"cwd"}) // (return)
l!l
138998
MC/DC100%1/1
_text($input->{"cwd"}) // (return)
_text($input->{"cwd"})
Truth table100%2/2
A
A= _text($input->{"cwd"})
Aresult
11
00
281 138 eval { _state($ctx)->put_session_cwd( _session_of($input), $cwd ); 1 };
282 138 return;
283 }
284  
285 # ---- checkout moves (spec §8) ----------------------------------------------------------------
286  
287 1448 ▶sub _now_ms { int( Time::HiRes::time() * 1000 ) }
Subroutine _now_ms: called CC=1 SCAR=0.0
288  
289 # A session moved into $move->{cwd} (the reference's moveCheckout): the checkout is the git top
290 # level of the new cwd, never the session's project. Nothing to do – no source asked, nothing
291 # written – when it is the old cwd's checkout, the home dir, has no `.claude/skilletor.json`, or
292 # its lock already matches its config (a local check). Otherwise its project scope syncs as at
293 # SessionStart – 5 s per source, nothing written once the deadline has passed – unless, once the
294 # sync lock is held, its lock matches after all: another run synced it meanwhile. One line names
295 # the checkout and, when something is left to do, the command to run there. Never dies.
296 #
297 # $move: cwd, old_cwd (when the event names one), event (the answer's hookEventName),
298 # lock_until_deadline (wait for the sync lock until the deadline, not the usual 5 s: Claude Code
299 # fires two move hooks for one `cd`, and the second waits out the other's sync). Returns the
300 # answer and – when it answered for the checkout, with a sync or with what is left to do there –
301 # that checkout. Under Hermes, once skills landed, the line also names `hermes skills trust` as
302 # one that may be needed (k152).
303 434 ▶sub _move_checkout {
Subroutine _move_checkout: called CC=3 SCAR=11.0
304 434 my ( $move, $ctx ) = @_;
305 434 my $root = project_root_of( $move->{cwd} );
306 434 my @result;
307 434 ▶ return @result if eval { @result = _move_checkout_of( $root, $move, $ctx ); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { @result = _move_checkout_of($root, $move, $ctx); 1 } } 381 1 382
308 1 my $err = $@;
309 # Timed out on the sync lock: another run holds it – a background sync of this very checkout,
310 # maybe – and synced nothing here, so the interval check is left to retry (k89).
311 1 ▶ return ( _cannot_sync( $root, _message($err) ), _lock_timeout($err) ? undef : $root );
Branch: 1 branch
BranchTrueFalseTotal
_lock_timeout($err) ? : 0 1 1
312 }
313  
314 434 ▶sub _move_checkout_of {
Subroutine _move_checkout_of: called CC=21 SCAR=31.7
315 434 my ( $root, $move, $ctx ) = @_;
316 434 ▶ return js_ordered() if same_path( $root, $ctx->{home} ) || !-e encode_text( path_join( $root, '.claude', 'skilletor.json' ) );
Branch: 1 branch
BranchTrueFalseTotal
if same_path($root, $ctx->{"home"}) or not -e encode_text(path_join($root, ".claude", "skilletor.json")) 186 248 434
Condition100%3/3
same_path($root, $ctx->{"home"}) or not -e encode_text(path_join($root, ".claude", "skilletor.json"))
l!l&&r!l&&!r
42144248
MC/DC100%2/2
same_path($root, $ctx->{"home"}) or not -e encode_text(path_join($root, ".claude", "skilletor.json"))
same_path($root, $ctx->{"home"}) not -e encode_text(path_join($root, ".claude", "skilletor.json"))
Truth table100%3/3
A or not(B)
A= same_path($root, $ctx->{"home"})B= not -e encode_text(path_join($root, ".claude", "skilletor.json"))
ABresult
1X1
011
000
317 248 ▶ return js_ordered() if defined $move->{old_cwd} && same_path( project_root_of( $move->{old_cwd} ), $root );
Branch: 1 branch
BranchTrueFalseTotal
if defined $move->{"old_cwd"} and same_path(project_root_of($move->{"old_cwd"}), $root) 0 248 248
Condition66%2/3
defined $move->{"old_cwd"} and same_path(project_root_of($move->{"old_cwd"}), $root)
!ll&&!rl&&r
382100
MC/DC0%0/2
defined $move->{"old_cwd"} and same_path(project_root_of($move->{"old_cwd"}), $root)
defined $move->{"old_cwd"} same_path(project_root_of($move->{"old_cwd"}), $root)
Truth table66%2/3
A and B
A= defined $move->{"old_cwd"}B= same_path(project_root_of($move->{"old_cwd"}), $root)
ABresult
0X0
100
111
318 248 ▶ my $deadline = $ctx->{deadline} // _now_ms() + MOVE_DEADLINE_MS;
Condition50%1/2
$ctx->{"deadline"} // _now_ms() + 20000
l!l
0248
MC/DC0%0/1
$ctx->{"deadline"} // _now_ms() + 20000
$ctx->{"deadline"}
Truth table50%1/2
A
A= $ctx->{"deadline"}
Aresult
11
00
319 # Past the deadline the sync writes nothing: a longer wait would be for nothing.
320 248 ▶ my $lock_timeout = ( $ctx->{engine} // {} )->{lock_timeout_ms};
Condition50%1/2
$ctx->{"engine"} // {}
l!l
2480
MC/DC0%0/1
$ctx->{"engine"} // {}
$ctx->{"engine"}
Truth table50%1/2
A
A= $ctx->{"engine"}
Aresult
11
00
321 248 ▶ if ( !defined $lock_timeout && $move->{lock_until_deadline} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $lock_timeout and $move->{"lock_until_deadline"}) 59 189 248
Condition66%2/3
not defined $lock_timeout and $move->{"lock_until_deadline"}
!ll&&!rl&&r
018959
MC/DC50%1/2
not defined $lock_timeout and $move->{"lock_until_deadline"}
not defined $lock_timeout $move->{"lock_until_deadline"}
Truth table66%2/3
not(A) and B
A= not defined $lock_timeoutB= $move->{"lock_until_deadline"}
ABresult
0X0
100
111
322 59 $lock_timeout = $deadline - _now_ms();
323 59 ▶ $lock_timeout = 0 if $lock_timeout < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $lock_timeout < 0 0 59 59
324 }
325 248 my $engine = _engine( { %$ctx, project_dir => $root }, timeout_ms => SESSION_START_TIMEOUT_MS );
326 248 my $chk = $engine->check( scope => 'project', local => 1 );
327 230 ▶ return ( _cannot_sync( $root, $chk->{error} ), $root ) if _truthy( $chk->{error} );
Branch: 1 branch
BranchTrueFalseTotal
if _truthy($chk->{"error"}) 0 230 230
328 230 ▶ unless ( $chk->{changed} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($chk->{"changed"}) 48 182 230
329 48 ▶ return ( _moved_output( _trust_report($chk), $root, $move->{event} ), $root ) if @{ $chk->{trustRequests} // [] };
Branch: 1 branch
BranchTrueFalseTotal
if @{$chk->{"trustRequests"} // [];} 9 39 48
Condition100%2/2
$chk->{"trustRequests"} // []
l!l
939
MC/DC100%1/1
$chk->{"trustRequests"} // []
$chk->{"trustRequests"}
Truth table100%2/2
A
A= $chk->{"trustRequests"}
Aresult
11
00
330 0 ▶ return @{ $chk->{warnings} } ? ( _cannot_sync( $root, join '; ', @{ $chk->{warnings} } ), $root ) : js_ordered();
Branch: 1 branch
BranchTrueFalseTotal
@{$chk->{'warnings'};} ? : 0 39 39
331 }
332 # No scopes: a run this one waited for on the sync lock – the other Claude Code hook of the
333 # same `cd`, say – synced the checkout meanwhile, and its answer was that run's to give.
334 182 my $report = $engine->sync( scope => 'project', if_changed => 1, deadline => $deadline, lock_timeout_ms => $lock_timeout );
335 147 ▶ return js_ordered() if !_truthy( $report->{error} ) && !@{ $report->{scopes} };
Branch: 1 branch
BranchTrueFalseTotal
unless _truthy($report->{"error"}) or @{$report->{"scopes"};} 0 147 147
Condition33%1/3
_truthy($report->{"error"}) or @{$report->{"scopes"};}
l!l&&r!l&&!r
01470
MC/DC0%0/2
_truthy($report->{"error"}) or @{$report->{"scopes"};}
_truthy($report->{"error"}) @{$report->{"scopes"};}
Truth table33%1/3
A or B
A= _truthy($report->{"error"})B= @{$report->{"scopes"};}
ABresult
1X1
011
000
336 147 ▶ return ( _cannot_sync( $root, $report->{error} ), $root ) if _truthy( $report->{error} );
Branch: 1 branch
BranchTrueFalseTotal
if _truthy($report->{"error"}) 0 147 147
337 147 my $out = _moved_output( $report, $root, $move->{event} );
338 147 ▶ $out = _with_hermes_trust( $out, $root ) if ( $ctx->{harness} // '' ) eq 'hermes' && grep { _adds_hermes_skill($_) } @{ $report->{scopes} };
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx->{"harness"} // "") eq "hermes" and grep {_adds_hermes_skill($_);} @{$$report{"scopes"};} 74 73 147
Condition100%5/5
($ctx->{"harness"} // "") eq "hermes" and grep {_adds_hermes_skill($_);} @{$$report{"scopes"};}
!ll&&!rl&&r
502374
$ctx->{"harness"} // ""
l!l
12027
MC/DC100%2/2
($ctx->{"harness"} // "") eq "hermes" and grep {_adds_hermes_skill($_);} @{$$report{"scopes"};}
$ctx->{"harness"} grep {_adds_hermes_skill($_);} @{$$report{"scopes"};}
Truth table100%3/3
A and B
A= $ctx->{"harness"}B= grep {_adds_hermes_skill($_);} @{$$report{"scopes"};}
ABresult
0X0
100
111
339 147 return ( $out, $root );
340 }
341  
342 # Did a scope's sync add or update a skill Hermes loads from the project – its own copy, or the
343 # Codex copy in `.agents/skills` it reads instead (spec §16.2)?
344 97 ▶sub _adds_hermes_skill {
Subroutine _adds_hermes_skill: called CC=8 SCAR=21.6
345 97 my ( $s ) = @_;
346 97 require Skilletor::Lock;
347 97 ▶ for my $it ( @{ $s->{added} // [] }, @{ $s->{updated} // [] } ) {
Condition50%2/4
$s->{"added"} // []
l!l
970
$s->{"updated"} // []
l!l
970
MC/DC0%0/1
$s->{"added"} // []
$s->{"added"}
MC/DC0%0/1
$s->{"updated"} // []
$s->{"updated"}
Truth table50%1/2
A
A= $s->{"added"}
Aresult
11
00
Truth table50%1/2
A
A= $s->{"updated"}
Aresult
11
00
348 88 my $k = Skilletor::Lock::parse_lock_key( $it->{key} );
349 88 ▶ return 1 if $k->{type} eq 'skill' && defined $k->{harness} && ( $k->{harness} eq 'hermes' || $k->{harness} eq 'codex' );
Branch: 1 branch
BranchTrueFalseTotal
if $k->{"type"} eq "skill" and defined $k->{"harness"} and $k->{"harness"} eq "hermes" || $k->{"harness"} eq "codex" 74 14 88
Condition66%6/9
$k->{"type"} eq "skill" and defined $k->{"harness"} and $k->{"harness"} eq "hermes" || $k->{"harness"} eq "codex"
!ll&&!rl&&r
14074
$k->{"type"} eq "skill" and defined $k->{"harness"}
!ll&&!rl&&r
14074
$k->{"harness"} eq "hermes" or $k->{"harness"} eq "codex"
l!l&&r!l&&!r
57170
MC/DC25%1/4
$k->{"type"} eq "skill" and defined $k->{"harness"} and $k->{"harness"} eq "hermes" || $k->{"harness"} eq "codex"
$k->{"type"} eq "skill" defined $k->{"harness"} $k->{"harness"} eq "hermes" $k->{"harness"} eq "codex"
Truth table60%3/5
A and B and C or D
A= $k->{"type"} eq "skill"B= defined $k->{"harness"}C= $k->{"harness"} eq "hermes"D= $k->{"harness"} eq "codex"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
350 }
351 23 return 0;
352 }
353  
354 # A Hermes move that put skills into a checkout (k152): Hermes loads a project's skills only once
355 # `hermes skills trust` named its root, and skilletor does not read that list (spec §16.5) – so
356 # the line and the context name the command as one that may be needed, the context as the user's
357 # to run.
358 74 ▶sub _with_hermes_trust {
Subroutine _with_hermes_trust: called CC=4 SCAR=14.0
359 74 my ( $out, $dir ) = @_;
360 74 my $command = "hermes skills trust $dir";
361 74 my $result = js_ordered( map { ( $_ => $out->{$_} ) } keys %$out );
362 74 ▶ $result->{systemMessage} = display_safe( ( $out->{systemMessage} // 'undefined' )." — if Hermes does not trust this project yet: $command" );
Condition50%1/2
$out->{"systemMessage"} // "undefined"
l!l
740
MC/DC0%0/1
$out->{"systemMessage"} // "undefined"
$out->{"systemMessage"}
Truth table50%1/2
A
A= $out->{"systemMessage"}
Aresult
11
00
363 74 my $hso = $out->{hookSpecificOutput};
364 74 ▶ my $context = ref $hso eq 'HASH' ? $hso->{additionalContext} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $hso eq 'HASH' ? : 74 0 74
365 74 ▶ if ( _truthy($context) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_truthy($context)) 74 0 74
366 74 my $line = "- Hermes loads project skills only from a project it trusts; if this one is not yet, the user can run: $command";
367 74 my $copy = js_ordered( map { ( $_ => $hso->{$_} ) } keys %$hso );
368 74 $copy->{additionalContext} = "$context\n".display_safe($line);
369 74 $result->{hookSpecificOutput} = $copy;
370 }
371 74 return $result;
372 }
373  
374 # The one line of a move that could not sync: the checkout, why, and what to run there.
375 1 ▶sub _cannot_sync {
Subroutine _cannot_sync: called CC=1 SCAR=0.0
376 1 my ( $dir, $message ) = @_;
377 1 return _warn("cannot sync $dir: $message — run there: skilletor sync");
378 }
379  
380 # A move's report: the line says where, and what to run there for what is left – trusting a
381 # source, or a sync that shows each warning and conflict; the context says where first.
382 156 ▶sub _moved_output {
Subroutine _moved_output: called CC=11 SCAR=24.9
383 156 my ( $report, $dir, $event ) = @_;
384 156 my $out = _to_output( $report, $event );
385 156 ▶ return $out unless _truthy( $out->{systemMessage} );
Branch: 1 branch
BranchTrueFalseTotal
unless _truthy($out->{"systemMessage"}) 0 156 156
386 156 my ( @fixes, %seen );
387 156 for my $s ( @{ $report->{scopes} } ) {
388 156 ▶ push @fixes, grep { !$seen{$_}++ } map { "skilletor trust $_->{name}" } @{ $s->{trustRequests} // [] };
Condition50%1/2
$s->{"trustRequests"} // []
l!l
1560
MC/DC0%0/1
$s->{"trustRequests"} // []
$s->{"trustRequests"}
Truth table50%1/2
A
A= $s->{"trustRequests"}
Aresult
11
00
389 }
390 156 ▶ push @fixes, 'skilletor sync' if grep { @{ $_->{warnings} // [] } || @{ $_->{conflicts} // [] } } @{ $report->{scopes} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {@{$_->{"warnings"} // [];} or @{$_->{"conflicts"} // [];};} @{$$report{"scopes"};} 0 156 156
Condition42%3/7
@{$_->{'warnings'} // [];} || @{$_->{'conflicts'} // [];}
l!l&&r!l&&!r
00156
$_->{"warnings"} // []
l!l
1560
$_->{"conflicts"} // []
l!l
1560
MC/DC0%0/2
@{$_->{'warnings'} // [];} || @{$_->{'conflicts'} // [];}
@{$_->{'warnings'} // [];} @{$_->{'conflicts'} // [];}
MC/DC0%0/1
$_->{"warnings"} // []
$_->{"warnings"}
MC/DC0%0/1
$_->{"conflicts"} // []
$_->{"conflicts"}
Truth table33%1/3
A || B
A= @{$_->{'warnings'} // [];}B= @{$_->{'conflicts'} // [];}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $_->{"warnings"}
Aresult
11
00
Truth table50%1/2
A
A= $_->{"conflicts"}
Aresult
11
00
391 156 ▶ my $result = js_ordered( systemMessage => display_safe( "$out->{systemMessage} in $dir".( @fixes ? ' — run there: '.join( '; ', @fixes ) : '' ) ) );
Branch: 1 branch
BranchTrueFalseTotal
@fixes ? : 9 147 156
392 156 ▶ my $context = ( $out->{hookSpecificOutput} // {} )->{additionalContext};
Condition50%1/2
$out->{"hookSpecificOutput"} // {}
l!l
1560
MC/DC0%0/1
$out->{"hookSpecificOutput"} // {}
$out->{"hookSpecificOutput"}
Truth table50%1/2
A
A= $out->{"hookSpecificOutput"}
Aresult
11
00
393 156 ▶ if ( _truthy($context) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_truthy($context)) 147 9 156
394 147 $result->{hookSpecificOutput} = js_ordered( hookEventName => $event, additionalContext => display_safe("skilletor: in $dir:")."\n$context" );
395 }
396 156 return $result;
397 }
398  
399 # Claude Code's CwdChanged (spec §8): it is not awaited, and its output takes no context (only
400 # `watchPaths`), so the line is all it gives – it reaches the user, not the model. It never
401 # writes the session's cwd record: PostToolUse, fired for the same `cd`, would take it for a move
402 # already done and answer before this hook's sync is.
403 110 ▶sub _cwd_changed {
Subroutine _cwd_changed: called CC=4 SCAR=13.9
404 110 my ( $input, $ctx ) = @_;
405 110 ▶ my $cwd = _text( $input->{new_cwd} ) // _text( $input->{cwd} ) // return js_ordered();
Condition80%4/5
(_text($input->{"new_cwd"}) // _text($input->{"cwd"})) // (return js_ordered())
l!l
9218
_text($input->{"new_cwd"}) // _text($input->{"cwd"})
l!l&&r!l&&!r
92018
MC/DC50%1/2
(_text($input->{"new_cwd"}) // _text($input->{"cwd"})) // (return js_ordered())
_text($input->{"new_cwd"}) _text($input->{"cwd"})
Truth table66%2/3
A // B
A= _text($input->{"new_cwd"})B= _text($input->{"cwd"})
ABresult
1X1
011
000
406 92 my ($out) = _move_checkout( { cwd => $cwd, old_cwd => _text( $input->{old_cwd} ), event => 'CwdChanged', lock_until_deadline => 1 }, $ctx );
407 73 ▶ return _truthy( $out->{systemMessage} ) ? js_ordered( systemMessage => $out->{systemMessage} ) : js_ordered();
Branch: 1 branch
BranchTrueFalseTotal
_truthy($out->{'systemMessage'}) ? : 33 40 73
408 }
409  
410 # Claude Code's PostToolUse, which its hooks file runs on a Bash call with a `cd` only (spec §8):
411 # awaited – the next tool call waits for it – with the cwd after the command as `cwd`, and no old
412 # one: the session's record stands in. Its context reaches the model.
413 7 ▶sub _post_tool_use {
Subroutine _post_tool_use: called CC=1 SCAR=0.0
414 7 my ( $input, $ctx ) = @_;
415 7 return _session_move( $input, $ctx, _state($ctx), { event => 'PostToolUse', lock_until_deadline => 1 } );
416 }
417  
418 # A move a hook sees only as the session's cwd (the reference's sessionMove): Codex's and Hermes'
419 # prompt hook – a session there moves only between turns, and that hook is the first to see the
420 # new cwd, awaited, before the model's request (k149) – and Claude Code's PostToolUse on a Bash
421 # `cd`.
422 # The cwd the session's hook last looked at is kept per session id (written at SessionStart): the
423 # same cwd again asks nothing; another goes to the move core, which tells a move within one
424 # checkout from one into another. A move that answered for its checkout marks it checked, so the
425 # interval check starts no second sync of it beside this one.
426 441 ▶sub _session_move {
Subroutine _session_move: called CC=5 SCAR=16.1
427 441 my ( $input, $ctx, $state, $event ) = @_;
428 441 ▶ my $cwd = _text( $input->{cwd} ) // return js_ordered();
Condition100%2/2
_text($input->{"cwd"}) // (return js_ordered())
l!l
37863
MC/DC100%1/1
_text($input->{"cwd"}) // (return js_ordered())
_text($input->{"cwd"})
Truth table100%2/2
A
A= _text($input->{"cwd"})
Aresult
11
00
429 378 my $session = _session_of($input);
430 378 my $last = $state->session_cwd($session);
431 378 ▶ return js_ordered() if defined $last && same_path( $last, $cwd );
Branch: 1 branch
BranchTrueFalseTotal
if defined $last and same_path($last, $cwd) 36 342 378
Condition100%3/3
defined $last and same_path($last, $cwd)
!ll&&!rl&&r
3830436
MC/DC100%2/2
defined $last and same_path($last, $cwd)
defined $last same_path($last, $cwd)
Truth table100%3/3
A and B
A= defined $lastB= same_path($last, $cwd)
ABresult
0X0
100
111
432 342 my ( $out, $answered ) = _move_checkout( { cwd => $cwd, old_cwd => $last, %$event }, $ctx );
433 309 eval { # the state dir cannot be written: the next move looks again
434 309 ▶ $state->mark_checked($answered) if defined $answered;
Branch: 1 branch
BranchTrueFalseTotal
if defined $answered 124 185 309
435 309 $state->put_session_cwd( $session, $cwd );
436 309 1;
437 };
438 309 return $out;
439 }
440  
441 # A pending report and a move's answer as one (k149): both lines, and – when either has a context –
442 # both in it, a line standing in for an answer without one. Without a move's answer, the report as
443 # is.
444 712 ▶sub _joined {
Subroutine _joined: called CC=7 SCAR=19.5
445 712 my ( $pending, $moved ) = @_;
446 712 ▶ my $context_of = sub { ( ref $_[0]{hookSpecificOutput} eq 'HASH' ? $_[0]{hookSpecificOutput} : {} )->{additionalContext} };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0]{'hookSpecificOutput'} eq 'HASH' ? : 69 638 707
447 712 ▶ my $says = sub { $_[0] && ( _truthy( $_[0]{systemMessage} ) || _truthy( $context_of->( $_[0] ) ) ) };
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
$_[0] and _truthy($_[0]{"systemMessage"}) || _truthy(&$context_of($_[0])) 735 97 832
Condition83%5/6
$_[0] && (_truthy($_[0]{'systemMessage'}) || _truthy(&$context_of($_[0])))
!ll&&!rl&&r
97592143
_truthy($_[0]{"systemMessage"}) or _truthy(&$context_of($_[0]))
l!l&&r!l&&!r
1430592
MC/DC66%2/3
$_[0] && (_truthy($_[0]{'systemMessage'}) || _truthy(&$context_of($_[0])))
$_[0] _truthy($_[0]{"systemMessage"}) _truthy(&$context_of($_[0]))
Truth table75%3/4
A && B or C
A= $_[0]B= _truthy($_[0]{"systemMessage"})C= _truthy(&$context_of($_[0]))
ABCresult
0XX0
1000
11X1
1011
448 712 ▶ return $pending // js_ordered() unless $says->($moved);
Branch: 1 branch
BranchTrueFalseTotal
unless &$says($moved) 592 120 712
Condition66%2/3
$pending // js_ordered()
l!l&&r!l&&!r
455470
MC/DC0%0/2
$pending // js_ordered()
$pending js_ordered()
Truth table66%2/3
A // B
A= $pendingB= js_ordered()
ABresult
1X1
011
000
449 120 ▶ return $moved unless $says->($pending);
Branch: 1 branch
BranchTrueFalseTotal
unless &$says($pending) 97 23 120
450 23 my @parts = ( $pending, $moved );
451 23 my $out = js_ordered();
452 23 my @lines = grep { _truthy($_) } map { $_->{systemMessage} } @parts;
453 23 ▶ $out->{systemMessage} = join '; ', @lines if @lines;
Branch: 1 branch
BranchTrueFalseTotal
if @lines 23 0 23
454 23 ▶ if ( grep { _truthy( $context_of->($_) ) } @parts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {_truthy(&$context_of($_));} @parts) 23 0 23
455 23 ▶ my @context = grep { _truthy($_) } map { _truthy( $context_of->($_) ) ? $context_of->($_) : $_->{systemMessage} } @parts;
Branch: 1 branch
BranchTrueFalseTotal
_truthy(&$context_of($_)) ? : 23 23 46
456 23 $out->{hookSpecificOutput} = js_ordered( hookEventName => 'UserPromptSubmit', additionalContext => join "\n", @context );
457 }
458 23 return $out;
459 }
460  
461 # Codex's and Hermes' hooks, which notice a move at the next prompt (k149).
462 743 ▶sub _follows_moves { my $h = $_[0]{harness} // ''; return $h eq 'codex' || $h eq 'hermes' }
Subroutine _follows_moves: called CC=3 SCAR=11.0
Condition100%5/5
$_[0]{"harness"} // ""
l!l
434309
$h eq 'codex' || $h eq 'hermes'
l!l&&r!l&&!r
127307309
MC/DC100%1/1
$_[0]{"harness"} // ""
$_[0]{"harness"}
MC/DC100%2/2
$h eq 'codex' || $h eq 'hermes'
$h eq 'codex' $h eq 'hermes'
Truth table100%2/2
A
A= $_[0]{"harness"}
Aresult
11
00
Truth table100%3/3
A || B
A= $h eq 'codex'B= $h eq 'hermes'
ABresult
1X1
011
000
463  
464 # Codex's and Hermes' prompt hook follows a move first (k149).
465 743 ▶sub _user_prompt_submit {
Subroutine _user_prompt_submit: called CC=5 SCAR=16.1
466 743 my ( $input, $ctx ) = @_;
467 743 my $state = _state($ctx);
468 743 my $key = _project_key( $ctx, $input );
469 743 ▶ my $moved = _follows_moves($ctx) ? _session_move( $input, $ctx, $state, { event => 'UserPromptSubmit' } ) : js_ordered();
Branch: 1 branch
BranchTrueFalseTotal
_follows_moves($ctx) ? : 434 309 743
470 713 my $pending = $state->take_pending_report($key);
471  
472 713 my $interval = DEFAULT_INTERVAL;
473 713 eval { # a config error: the fallback throttle; the background sync reports it
474 713 require Skilletor::Config;
475 713 $interval = Skilletor::Config->load( home => $ctx->{home}, claude_config_dir => _claude_config_dir($ctx), project_dir => _project_dir_of($ctx) )
476 ->check_interval;
477 674 1;
478 };
479  
480 # checkInterval <= 0 turns the in-session check off entirely (SessionStart still syncs).
481 713 ▶ if ( $interval > 0 && $state->is_due( $key, $interval ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interval > 0 and $state->is_due($key, $interval)) 272 441 713
Condition100%3/3
$interval > 0 and $state->is_due($key, $interval)
!ll&&!rl&&r
98343272
MC/DC100%2/2
$interval > 0 and $state->is_due($key, $interval)
$interval > 0 $state->is_due($key, $interval)
Truth table100%3/3
A and B
A= $interval > 0B= $state->is_due($key, $interval)
ABresult
0X0
100
111
482 272 $state->mark_checked($key);
483 272 ▶ ( $ctx->{background} // \&default_background )->($ctx);
Condition100%2/2
$ctx->{"background"} // \&Skilletor::Hooks::default_background
l!l
2702
MC/DC100%1/1
$ctx->{"background"} // \&Skilletor::Hooks::default_background
$ctx->{"background"}
Truth table100%2/2
A
A= $ctx->{"background"}
Aresult
11
00
484 }
485 712 return _joined( $pending, $moved );
486 }
487  
488 # Runs in the detached child: sync, then leave the result as a pending report – its stdout
489 # reaches no one. A sync that dies leaves its error, the line SessionStart would give; one that
490 # timed out on the sync lock leaves no report – the run that holds the lock reports its own sync
491 # (spec §6.5) – and the project due, so the next prompt retries.
492 54 ▶sub _sync_background {
Subroutine _sync_background: called CC=5 SCAR=16.2
493 54 my ( $input, $ctx ) = @_;
494 54 my $key = _project_key( $ctx, $input );
495 54 my $state = _state($ctx);
496 54 my $output;
497 54 ▶ unless ( eval { $output = _to_output( _engine($ctx)->sync, 'UserPromptSubmit' ); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $output = _to_output(_engine($ctx)->sync, "UserPromptSubmit"); 1 } }) 31 21 52
498 31 my $err = $@;
499 31 ▶ return $state->due_again($key) if _lock_timeout($err);
Branch: 1 branch
BranchTrueFalseTotal
if _lock_timeout($err) 18 13 31
500 13 $output = _warn( _message($err) );
501 }
502 34 ▶ $state->put_pending_report( $key, $output ) if _truthy( $output->{systemMessage} ) || $output->{hookSpecificOutput};
Branch: 1 branch
BranchTrueFalseTotal
if _truthy($output->{"systemMessage"}) or $output->{"hookSpecificOutput"} 34 0 34
Condition33%1/3
_truthy($output->{"systemMessage"}) or $output->{"hookSpecificOutput"}
l!l&&r!l&&!r
3400
MC/DC0%0/2
_truthy($output->{"systemMessage"}) or $output->{"hookSpecificOutput"}
_truthy($output->{"systemMessage"}) $output->{"hookSpecificOutput"}
Truth table33%1/3
A or B
A= _truthy($output->{"systemMessage"})B= $output->{"hookSpecificOutput"}
ABresult
1X1
011
000
503 34 return;
504 }
505  
506 # Start `hook __sync-background` of this CLI, detached (the default background).
507 29 ▶sub default_background {
Subroutine default_background: called CC=4 SCAR=13.9
Pod documented
508 29 my ( $ctx, $spawn ) = @_;
509 29 ▶ my $cmd = $ctx->{self_command} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $cmd = $ctx->{"self_command"} or return 20 9 29
510 ( $spawn // \&spawn_detached )->(
511 cmd => [ @$cmd, 'hook', '__sync-background' ],
512 20 ▶ env => { %ENV, SKILLETOR_PROJECT_DIR => encode_text( $ctx->{project_dir} // '' ) },
Condition75%3/4
$ctx->{"project_dir"} // ""
l!l
200
$spawn // \&Skilletor::Hooks::spawn_detached
l!l
182
MC/DC0%0/1
$ctx->{"project_dir"} // ""
$ctx->{"project_dir"}
MC/DC100%1/1
$spawn // \&Skilletor::Hooks::spawn_detached
$spawn
Truth table50%1/2
A
A= $ctx->{"project_dir"}
Aresult
11
00
Truth table100%2/2
A
A= $spawn
Aresult
11
00
513 );
514 19 return;
515 }
516  
517 205 1;
518  
519 __END__
« lib/Skilletor/Source/Git.pm ↑ Summary lib/Skilletor/JSString.pm »