Agent/Hook/Registry.pm

filter: Statementstmt 100% 93 / 93 Branchbran 84.3% 27 / 32 Conditioncond 69.2% 18 / 26 MC/DCmcdc 40.0% 6 / 15 Subroutinesub 100% 17 / 17 Podpod 75.0% 3 / 4 totaltotal 87.7% 164 / 187 CC 31 SCAR 21.0
CC
31
Cov
91%
run
15.3
_normalise
9.1
BUILD
7.3
CRAP 8.2
« Agent/Logger.pm ↑ Summary Agent/MCP/SDKBridge.pm »
1 package Claude::Agent::Hook::Registry;
2  
3 9 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 ▶use Object::Proto ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Claude::Agent::Logger '$log';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Claude::Agent::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Claude::Agent::Hook::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 Object::Proto::define('Claude::Agent::Hook::Registry',
16 'hooks:HashRef:default({})',
17 'loop:predicate',
18 'query:weak:predicate',
19 '_callbacks:HashRef:default({})',
20 '_order:ArrayRef:default([])',
21 '_warned:HashRef:default({})',
22 );
23 }
24  
25 6 ▶sub BUILD {
Subroutine BUILD: called CC=7 SCAR=19.8
Pod undocumented
26 6 my ($self) = @_;
27 6 my $n = 0;
28 6 for my $event (sort keys %{$self->hooks}) {
29 8 ▶ for my $matcher (@{ $self->hooks->{$event} // [] }) {
Condition50%1/2
$self->hooks->{$event} // []
l!l
80
MC/DC0%0/1
$self->hooks->{$event} // []
$self->hooks->{$event}
Truth table50%1/2
A
A= $self->hooks->{$event}
Aresult
11
00
30 9 ▶ next unless blessed($matcher) && $matcher->can('hooks');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($matcher) and $matcher->can("hooks") 0 9 9
Condition33%1/3
builtin::blessed($matcher) and $matcher->can("hooks")
!ll&&!rl&&r
009
MC/DC0%0/2
builtin::blessed($matcher) and $matcher->can("hooks")
builtin::blessed($matcher) $matcher->can("hooks")
Truth table33%1/3
A and B
A= builtin::blessed($matcher)B= $matcher->can("hooks")
ABresult
0X0
100
111
31 9 my @ids;
32 9 for my $callback (@{$matcher->hooks}) {
33 13 my $id = 'hook_' . $n++;
34 13 $self->_callbacks->{$id} = {
35 callback => $callback,
36 matcher => $matcher,
37 event => $event,
38 };
39 13 push @ids, $id;
40 }
41 9 push @{$self->_order}, { event => $event, matcher => $matcher, ids => \@ids };
42 }
43 }
44 6 return;
45 }
46  
47 1 ▶sub callback_ids {
Subroutine callback_ids: called CC=1 SCAR=0.0
Pod documented
48 1 my ($self) = @_;
49 1 return [ map { @{$_->{ids}} } @{$self->_order} ];
50 }
51  
52 4 ▶sub to_initialize_config {
Subroutine to_initialize_config: called CC=3 SCAR=11.0
Pod documented
53 4 my ($self) = @_;
54 4 my %config;
55 4 for my $entry (@{$self->_order}) {
56 7 my $matcher = $entry->{matcher};
57 my $item = {
58 matcher => $matcher->matcher,
59 hookCallbackIds => $entry->{ids},
60 7 };
61 7 ▶ $item->{timeout} = $matcher->timeout if $matcher->has_timeout;
Branch: 1 branch
BranchTrueFalseTotal
if $matcher->has_timeout 2 5 7
62 7 push @{ $config{ $entry->{event} } }, $item;
63 }
64 4 return \%config;
65 }
66  
67 14 ▶sub run {
Subroutine run: called CC=15 SCAR=27.3
Pod documented
68 14 my ($self, $callback_id, $input, $tool_use_id) = @_;
69  
70 14 ▶ my $entry = $self->_callbacks->{$callback_id}
Branch: 1 branch
BranchTrueFalseTotal
my $entry = $self->_callbacks->{$callback_id} or return "Future"->fail("No hook callback found for ID: $callback_id") 11 3 14
71 or return Future->fail("No hook callback found for ID: $callback_id");
72  
73 11 ▶ $input = {} unless ref $input eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $input eq "HASH" 0 11 11
74 11 ▶ my $loop = $self->has_loop ? $self->loop : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_loop ? : 10 1 11
75  
76 my $context = Claude::Agent::Hook::Context->new(
77 Claude::Agent::Types::defined_args(
78 hook_event_name => $input->{hook_event_name} // $entry->{event},
79 session_id => $input->{session_id},
80 cwd => $input->{cwd},
81 transcript_path => $input->{transcript_path},
82 permission_mode => $input->{permission_mode},
83 tool_name => $input->{tool_name},
84 tool_input => $input->{tool_input},
85 tool_use_id => $tool_use_id // $input->{tool_use_id},
86 11 ▶ loop => $loop,
Branch: 1 branch
BranchTrueFalseTotal
$self->has_query ? : 1 10 11
Condition66%4/6
$input->{"hook_event_name"} // $entry->{"event"}
l!l&&r!l&&!r
740
$tool_use_id // $input->{"tool_use_id"}
l!l&&r!l&&!r
1001
MC/DC0%0/2
$input->{"hook_event_name"} // $entry->{"event"}
$input->{"hook_event_name"} $entry->{"event"}
MC/DC50%1/2
$tool_use_id // $input->{"tool_use_id"}
$tool_use_id $input->{"tool_use_id"}
Truth table66%2/3
A // B
A= $input->{"hook_event_name"}B= $entry->{"event"}
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $tool_use_idB= $input->{"tool_use_id"}
ABresult
1X1
011
000
87 query => ($self->has_query ? $self->query : undef),
88 )
89 );
90  
91 11 ▶ $log->debug(sprintf("Hook: running %s for %s", $callback_id, $context->hook_event_name // 'event'));
Condition50%1/2
$context->hook_event_name // "event"
l!l
110
MC/DC0%0/1
$context->hook_event_name // "event"
$context->hook_event_name
Truth table50%1/2
A
A= $context->hook_event_name
Aresult
11
00
92  
93 11 my $result;
94 11 my $ok = eval { $result = $entry->{callback}->($input, $tool_use_id, $context, $loop); 1 };
95 11 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 10 11
96 1 ▶ my $err = $@ || 'Hook execution failed';
Condition50%1/2
$@ || 'Hook execution failed'
l!l
10
MC/DC0%0/1
$@ || 'Hook execution failed'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
97 1 $log->debug(sprintf("Hook: %s died: %s", $callback_id, $err));
98 1 return Future->fail("$err");
99 }
100  
101 10 ▶ my $future = blessed($result) && $result->isa('Future') ? $result : Future->done($result);
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($result) && $result->isa('Future') ? : 4 6 10
Condition66%2/3
builtin::blessed($result) and $result->isa("Future")
!ll&&!rl&&r
604
MC/DC50%1/2
builtin::blessed($result) and $result->isa("Future")
builtin::blessed($result) $result->isa("Future")
Truth table66%2/3
A and B
A= builtin::blessed($result)B= $result->isa("Future")
ABresult
0X0
100
111
102  
103 10 ▶ if ($loop && $entry->{matcher}->has_timeout && !$future->is_ready) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loop and $entry->{"matcher"}->has_timeout and not $future->is_ready) 1 9 10
Condition100%6/6
$loop and $entry->{"matcher"}->has_timeout and not $future->is_ready
!ll&&!rl&&r
721
$loop and $entry->{"matcher"}->has_timeout
!ll&&!rl&&r
163
MC/DC100%3/3
$loop and $entry->{"matcher"}->has_timeout and not $future->is_ready
$loop $entry->{"matcher"}->has_timeout not $future->is_ready
Truth table100%4/4
A and B and not(C)
A= $loopB= $entry->{"matcher"}->has_timeoutC= not $future->is_ready
ABCresult
0XX0
10X0
1100
1111
104 1 my $timeout = $entry->{matcher}->timeout;
105 $future = Future->wait_any(
106 $future,
107 1 ▶ $loop->timeout_future(after => $timeout)->else(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
108 1 return Future->fail("hook $callback_id timed out after ${timeout}s");
109 1 }),
110 );
111 }
112  
113 8 ▶ return $future->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
114 8 my ($output) = @_;
115 8 return Future->done($self->_normalise($callback_id, $output));
116 10 });
117 }
118  
119 8 ▶sub _normalise {
Subroutine _normalise: called CC=9 SCAR=22.1
120 8 my ($self, $callback_id, $output) = @_;
121  
122 8 ▶ return {} unless defined $output;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $output 1 7 8
123 7 ▶ return $output unless ref $output eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $output eq "HASH" 0 7 7
124  
125 7 ▶ my $decision = $output->{decision} // '';
Condition100%2/2
$output->{"decision"} // ""
l!l
34
MC/DC100%1/1
$output->{"decision"} // ""
$output->{"decision"}
Truth table100%2/2
A
A= $output->{"decision"}
Aresult
11
00
126 7 ▶ return $output unless $decision =~ /^(?:continue|allow|deny)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $decision =~ /^(?:continue|allow|deny)$/u 4 3 7
127  
128 3 ▶ unless ($self->_warned->{$callback_id}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_warned->{$callback_id}++) 2 1 3
129 2 warn "Hook callback $callback_id returned the 0.13 {decision => '$decision'} shape; "
130 . "use Claude::Agent::Hook::Result\n";
131 }
132  
133 3 ▶ return {} if $decision eq 'continue';
Branch: 1 branch
BranchTrueFalseTotal
if $decision eq "continue" 1 2 3
134  
135 my %specific = (
136 hookEventName => $self->_callbacks->{$callback_id}{event},
137 2 permissionDecision => $decision,
138 );
139 2 ▶ $specific{permissionDecisionReason} = $output->{reason} if defined $output->{reason};
Branch: 1 branch
BranchTrueFalseTotal
if defined $output->{"reason"} 2 0 2
140 2 ▶ $specific{updatedInput} = $output->{updated_input} if defined $output->{updated_input};
Branch: 1 branch
BranchTrueFalseTotal
if defined $output->{"updated_input"} 0 2 2
141 2 return { hookSpecificOutput => \%specific };
142 }
143  
144 9 1;
145  
146 __END__
« Agent/Logger.pm ↑ Summary Agent/MCP/SDKBridge.pm »