blib/lib/Langertha/Knarr/Reasoning.pm

filter: Statementstmt 100% 71 / 71 Branchbran 90.7% 49 / 54 Conditioncond 54.8% 17 / 31 MC/DCmcdc 27.7% 5 / 18 Subroutinesub 100% 13 / 13 Podpod 85.7% 6 / 7 totaltotal 82.9% 161 / 194 CC 41 SCAR 18.8
CC
41
Cov
88%
from_anthropic
11.0
level_for_budget
8.1
BUILD
7.0
CRAP 6.5
« blib/lib/Langertha/Knarr/Handler/Tracing.pm ↑ Summary blib/lib/Langertha/Knarr/Protocol/Anthropic.pm »
1 package Langertha::Knarr::Reasoning;
2 # ABSTRACT: Map a face's native reasoning controls onto the normalized reasoning_effort level
3 7 our $VERSION = '1.103';
4 7 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 # The normalized ascending reasoning vocabulary (Langertha::Reasoning::Level).
10 7 my %LEVEL = map { $_ => 1 } qw( none minimal low medium high xhigh max );
11  
12 7 has default_level => (
13 is => 'ro',
14 isa => 'Str',
15 lazy => 1,
16 builder => '_build_default_level',
17 );
18  
19 6 ▶sub _build_default_level {
Subroutine _build_default_level: called CC=5 SCAR=17.8
20 6 my ( $self ) = @_;
21 6 ▶ return 'medium' unless $self->budget_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->budget_available 2 4 6
22 4 my $attr = Langertha::Reasoning::BudgetPolicy->meta->find_attribute_by_name('default_bool_level');
23 4 ▶ my $level = $attr && $attr->default;
Condition33%1/3
$attr && $attr->default
!ll&&!rl&&r
004
MC/DC0%0/2
$attr && $attr->default
$attr $attr->default
Truth table33%1/3
A && B
A= $attrB= $attr->default
ABresult
0X0
100
111
24 4 ▶ return defined $level && $LEVEL{$level} ? $level : 'medium';
Branch: 1 branch
BranchTrueFalseTotal
defined $level && $LEVEL{$level} ? : 4 0 4
Condition33%1/3
defined $level and $LEVEL{$level}
!ll&&!rl&&r
004
MC/DC0%0/2
defined $level and $LEVEL{$level}
defined $level $LEVEL{$level}
Truth table33%1/3
A and B
A= defined $levelB= $LEVEL{$level}
ABresult
0X0
100
111
25 }
26  
27  
28 7 has budget_points => (
29 is => 'ro',
30 isa => 'HashRef[Int]',
31 predicate => 'has_budget_points',
32 );
33  
34  
35 39 ▶sub BUILD {
Subroutine BUILD: called CC=7 SCAR=19.5
Pod undocumented
36 39 my ( $self, $args ) = @_;
37 croak __PACKAGE__."->new: unknown default_level '".$args->{default_level}."'"
38 39 ▶ if exists $args->{default_level} && !$LEVEL{ $args->{default_level} // '' };
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"default_level"} and not $LEVEL{$args->{"default_level"} // ""} 1 38 39
Condition80%4/5
exists $args->{"default_level"} and not $LEVEL{$args->{"default_level"} // ""}
!ll&&!rl&&r
3711
$args->{"default_level"} // ""
l!l
20
MC/DC100%2/2
exists $args->{"default_level"} and not $LEVEL{$args->{"default_level"} // ""}
exists $args->{"default_level"} not $LEVEL{$args->{"default_level"} // ""}
MC/DC0%0/1
$args->{"default_level"} // ""
$args->{"default_level"}
Truth table100%3/3
A and not(B)
A= exists $args->{"default_level"}B= not $LEVEL{$args->{"default_level"} // ""}
ABresult
0X0
100
111
Truth table50%1/2
A
A= $args->{"default_level"}
Aresult
11
00
39 38 ▶ if ( $self->has_budget_points ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_budget_points) 2 36 38
40 2 for my $level ( sort keys %{ $self->budget_points } ) {
41 4 ▶ croak __PACKAGE__."->new: unknown level '".$level."' in budget_points" unless $LEVEL{$level};
Branch: 1 branch
BranchTrueFalseTotal
unless $LEVEL{$level} 1 3 4
42 }
43 }
44 37 return;
45 }
46  
47 7 has budget_available => (
48 is => 'ro',
49 isa => 'Bool',
50 lazy => 1,
51 builder => '_build_budget_available',
52 init_arg => undef,
53 );
54  
55 14 ▶sub _build_budget_available {
Subroutine _build_budget_available: called CC=2 SCAR=7.1
56 # Optional core feature (Langertha > 0.503): probed, not a hard dependency.
57 14 ▶ return eval { require Langertha::Reasoning::BudgetPolicy; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Langertha::Reasoning::BudgetPolicy; 1 } } ? : 14 0 14
58 }
59  
60  
61 6 ▶sub fallback_budget_points {
Subroutine fallback_budget_points: called CC=1 SCAR=0.0
Pod documented
62 6 return { low => 2048, medium => 8192, high => 24576 };
63 }
64  
65  
66 11 ▶sub budget_policy {
Subroutine budget_policy: called CC=5 SCAR=16.1
Pod documented
67 11 my ( $self, $model ) = @_;
68 11 ▶ return unless $self->budget_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->budget_available 1 10 11
69 10 my $policy = eval {
70 10 my $profile = Langertha::Reasoning::Profile->for_model($model);
71 10 ▶ my %opts = $self->has_budget_points
Branch: 2 branches
BranchTrueFalseTotal
$self->has_budget_points ? : 2 8 10
$profile->has_budget_min && $profile->has_budget_max ? : 2 6 8
Condition66%2/3
$profile->has_budget_min and $profile->has_budget_max
!ll&&!rl&&r
602
MC/DC50%1/2
$profile->has_budget_min and $profile->has_budget_max
$profile->has_budget_min $profile->has_budget_max
Truth table66%2/3
A and B
A= $profile->has_budget_minB= $profile->has_budget_max
ABresult
0X0
100
111
72 ? ( points => $self->budget_points )
73 : $profile->has_budget_min && $profile->has_budget_max
74 ? ()
75 : ( points => $self->fallback_budget_points );
76 10 Langertha::Reasoning::BudgetPolicy->new( profile => $profile, %opts );
77 };
78 10 return $policy;
79 }
80  
81  
82 12 ▶sub level_for_budget {
Subroutine level_for_budget: called CC=7 SCAR=21.0
Pod documented
83 12 my ( $self, $budget, $model ) = @_;
84 12 ▶ return unless defined $budget && !ref $budget && $budget =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $budget and not ref $budget and $budget =~ /\A[0-9]+\z/ 1 11 12
Condition50%3/6
defined $budget and not ref $budget and $budget =~ /\A[0-9]+\z/
!ll&&!rl&&r
0111
defined $budget and not ref $budget
!ll&&!rl&&r
0012
MC/DC33%1/3
defined $budget and not ref $budget and $budget =~ /\A[0-9]+\z/
defined $budget not ref $budget $budget =~ /\A[0-9]+\z/
Truth table50%2/4
A and not(B) and C
A= defined $budgetB= not ref $budgetC= $budget =~ /\A[0-9]+\z/
ABCresult
0XX0
10X0
1100
1111
85 11 ▶ my $policy = $self->budget_policy($model) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $policy = $self->budget_policy($model) or return 10 1 11
86 10 my $level = eval { $policy->level_for( $budget + 0 ) };
87 10 ▶ return defined $level && $LEVEL{$level} ? $level : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $level && $LEVEL{$level} ? : 10 0 10
Condition33%1/3
defined $level and $LEVEL{$level}
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $level and $LEVEL{$level}
defined $level $LEVEL{$level}
Truth table33%1/3
A and B
A= defined $levelB= $LEVEL{$level}
ABresult
0X0
100
111
88 }
89  
90  
91 5 ▶sub level_for_bool {
Subroutine level_for_bool: called CC=2 SCAR=6.9
Pod documented
92 5 my ( $self, $on ) = @_;
93 5 ▶ return $on ? $self->default_level : 'none';
Branch: 1 branch
BranchTrueFalseTotal
$on ? : 3 2 5
94 }
95  
96  
97 95 ▶sub _explicit_level {
Subroutine _explicit_level: called CC=4 SCAR=14.2
98 95 my ( $self, $value ) = @_;
99 95 ▶ return defined $value && !ref $value && length $value ? $value : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $value && !ref($value) && length $value ? : 4 91 95
Condition66%4/6
defined $value and not ref $value and length $value
!ll&&!rl&&r
9104
defined $value and not ref $value
!ll&&!rl&&r
9104
MC/DC33%1/3
defined $value and not ref $value and length $value
defined $value not ref $value length $value
Truth table50%2/4
A and not(B) and C
A= defined $valueB= not ref $valueC= length $value
ABCresult
0XX0
10X0
1100
1111
100 }
101  
102 34 ▶sub from_anthropic {
Subroutine from_anthropic: called CC=11 SCAR=24.0
Pod documented
103 34 my ( $self, $data ) = @_;
104 34 ▶ return unless ref $data eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 0 34 34
105 34 ▶ my $output_config = ref $data->{output_config} eq 'HASH' ? $data->{output_config} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $data->{'output_config'} eq 'HASH' ? : 2 32 34
106 34 for my $explicit ( $data->{reasoning_effort}, $output_config->{effort} ) {
107 67 my ($level) = $self->_explicit_level($explicit);
108 67 ▶ return $level if defined $level;
Branch: 1 branch
BranchTrueFalseTotal
if defined $level 3 64 67
109 }
110 31 my $thinking = $data->{thinking};
111 31 ▶ return unless ref $thinking eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $thinking eq "HASH" 11 20 31
112 20 ▶ my $type = $thinking->{type} // '';
Condition50%1/2
$thinking->{"type"} // ""
l!l
200
MC/DC0%0/1
$thinking->{"type"} // ""
$thinking->{"type"}
Truth table50%1/2
A
A= $thinking->{"type"}
Aresult
11
00
113 20 ▶ return 'none' if $type eq 'disabled';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "disabled" 2 18 20
114 18 ▶ return $self->default_level if $type eq 'adaptive';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "adaptive" 4 14 18
115 14 ▶ return unless $type eq 'enabled';
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "enabled" 1 13 14
116 13 ▶ return $self->default_level unless defined $thinking->{budget_tokens};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $thinking->{"budget_tokens"} 1 12 13
117 12 return $self->level_for_budget( $thinking->{budget_tokens}, $data->{model} );
118 }
119  
120  
121 28 ▶sub from_ollama {
Subroutine from_ollama: called CC=6 SCAR=17.9
Pod documented
122 28 my ( $self, $think, $explicit ) = @_;
123 28 my ($level) = $self->_explicit_level($explicit);
124 28 ▶ return $level if defined $level;
Branch: 1 branch
BranchTrueFalseTotal
if defined $level 1 27 28
125 27 ▶ return unless defined $think;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $think 16 11 27
126 11 ▶ return $self->level_for_bool($think) if JSON::MaybeXS::is_bool($think);
Branch: 1 branch
BranchTrueFalseTotal
if JSON::MaybeXS::is_bool($think) 5 6 11
127 6 ▶ return if ref $think;
Branch: 1 branch
BranchTrueFalseTotal
if ref $think 0 6 6
128 6 ▶ return $LEVEL{$think} ? $think : ();
Branch: 1 branch
BranchTrueFalseTotal
$LEVEL{$think} ? : 4 2 6
129 }
130  
131  
132 7 __PACKAGE__->meta->make_immutable;
133  
134  
135 7 1;
136  
137 __END__
« blib/lib/Langertha/Knarr/Handler/Tracing.pm ↑ Summary blib/lib/Langertha/Knarr/Protocol/Anthropic.pm »