| 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} // []
MC/DC0%0/1
$self->hooks->{$event} // []
$self->hooks->{$event}
Truth table50%1/2
A
A= $self->hooks->{$event}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 9 | ▶ | next unless blessed($matcher) && $matcher->can('hooks'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($matcher) and $matcher->can("hooks")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 11 | ▶ | my $loop = $self->has_loop ? $self->loop : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
$input->{"hook_event_name"} // $entry->{"event"}
$tool_use_id // $input->{"tool_use_id"}
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"}
Truth table66%2/3
A // B
A= $tool_use_idB= $input->{"tool_use_id"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$context->hook_event_name // "event"
$context->hook_event_name
Truth table50%1/2
A
A= $context->hook_event_name
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | ▶ | my $err = $@ || 'Hook execution failed'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$@ || 'Hook execution failed'
MC/DC0%0/1
$@ || 'Hook execution failed'
$@
Truth table50%1/2
A
A= $@
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
builtin::blessed($result) and $result->isa("Future")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 10 | ▶ | if ($loop && $entry->{matcher}->has_timeout && !$future->is_ready) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$loop and $entry->{"matcher"}->has_timeout and not $future->is_ready
$loop and $entry->{"matcher"}->has_timeout
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 7 | ▶ | return $output unless ref $output eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 7 | ▶ | my $decision = $output->{decision} // ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$output->{"decision"} // ""
MC/DC100%1/1
$output->{"decision"} // ""
$output->{"decision"}
Truth table100%2/2
A
A= $output->{"decision"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 7 | ▶ | return $output unless $decision =~ /^(?:continue|allow|deny)$/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 3 | ▶ | unless ($self->_warned->{$callback_id}++) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 2 | ▶ | $specific{updatedInput} = $output->{updated_input} if defined $output->{updated_input}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 2 | return { hookSpecificOutput => \%specific }; | |||||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 9 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||||
| 146 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||