blib/lib/Langertha/Reasoning/BudgetPolicy.pm

filter: Statementstmt 92.4% 98 / 106 Branchbran 78.2% 36 / 46 Conditioncond 58.0% 18 / 31 MC/DCmcdc 35.0% 7 / 20 Subroutinesub 92.3% 12 / 13 Podpod 75.0% 3 / 4 totaltotal 79.0% 174 / 220 CC 36 SCAR 19.6
CC
36
Cov
83%
_build_levels
12.0
budget_for
9.5
BUILD
9.0
CRAP 7.1
« blib/lib/Langertha/Manifest/Validation.pm ↑ Summary blib/lib/Langertha/Engine/DeepSeek.pm »
1 package Langertha::Reasoning::BudgetPolicy;
2 # ABSTRACT: Invented level<->token-budget interpolation, clamped to a Profile's enforced bounds
3 2 our $VERSION = '0.505';
4 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Langertha::Reasoning::Profile;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 # The normalized ascending reasoning vocabulary ordinal ladder. The enum type
11 # itself lives once in Langertha::Reasoning::Profile (redefining it croaks); this
12 # is the numeric spacing convention (c) interpolates against.
13 2 my %LEVEL_ORDER = (
14 none => 0, minimal => 1, low => 2, medium => 3, high => 4, xhigh => 5, max => 6,
15 );
16  
17 2 enum 'Langertha::Reasoning::BudgetForm' => [qw( range explicit )];
18 2 enum 'Langertha::Reasoning::BudgetCurve' => [qw( linear log )];
19  
20 2 has profile => (
21 is => 'ro',
22 isa => 'Langertha::Reasoning::Profile',
23 required => 1,
24 );
25  
26  
27 2 has form => (
28 is => 'ro',
29 isa => 'Langertha::Reasoning::BudgetForm',
30 lazy => 1,
31 builder => '_build_form',
32 );
33  
34 3 ▶sub _build_form { return $_[0]->has_points ? 'explicit' : 'range' }
Subroutine _build_form: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->has_points ? : 2 1 3
35  
36  
37 2 has curve => (
38 is => 'ro',
39 isa => 'Langertha::Reasoning::BudgetCurve',
40 default => 'linear',
41 );
42  
43  
44 2 has points => (
45 is => 'ro',
46 isa => 'HashRef[Int]',
47 predicate => 'has_points',
48 );
49  
50  
51 2 has levels => (
52 is => 'ro',
53 isa => 'ArrayRef[Langertha::Reasoning::Level]',
54 lazy => 1,
55 builder => '_build_levels',
56 );
57  
58 0 ▶sub _build_levels {
Subroutine _build_levels: not called CC=3 SCAR=24.8
59 0 my ( $self ) = @_;
60 0 ▶ if ( $self->has_points ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_points) 0 0 0
61 0 return [ sort { $LEVEL_ORDER{$a} <=> $LEVEL_ORDER{$b} } keys %{ $self->points } ];
62 }
63 0 my @profile_levels = @{ $self->profile->levels };
64 0 ▶ return @profile_levels ? [@profile_levels] : [qw( low medium high )];
Branch: 1 branch
BranchTrueFalseTotal
@profile_levels ? : 0 0 0
65 }
66  
67  
68 2 has default_bool_level => (
69 is => 'ro',
70 isa => 'Langertha::Reasoning::Level',
71 default => 'medium',
72 );
73  
74  
75 2 has source => (
76 is => 'ro',
77 isa => 'Str',
78 default => 'library convention (invented level<->budget interpolation, not provider wire-truth)',
79 );
80  
81  
82 10 ▶sub BUILD {
Subroutine BUILD: called CC=9 SCAR=22.0
Pod undocumented
83 10 my ( $self ) = @_;
84  
85 10 ▶ if ( $self->has_points ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_points) 3 7 10
86 3 for my $level ( keys %{ $self->points } ) {
87 croak "Langertha::Reasoning::BudgetPolicy: unknown level '$level' in points "
88 . "(expected one of " . join( '|', sort keys %LEVEL_ORDER ) . ")"
89 8 ▶ unless exists $LEVEL_ORDER{$level};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $LEVEL_ORDER{$level} 1 7 8
90 }
91 }
92  
93 9 ▶ if ( $self->form eq 'explicit' && !$self->has_points ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->form eq "explicit" and not $self->has_points) 1 8 9
Condition100%3/3
$self->form eq "explicit" and not $self->has_points
!ll&&!rl&&r
621
MC/DC100%2/2
$self->form eq "explicit" and not $self->has_points
$self->form eq "explicit" not $self->has_points
Truth table100%3/3
A and not(B)
A= $self->form eq "explicit"B= not $self->has_points
ABresult
0X0
100
111
94 1 croak "Langertha::Reasoning::BudgetPolicy: form 'explicit' requires 'points'";
95 }
96  
97 8 ▶ if ( $self->form eq 'range' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->form eq "range") 6 2 8
98 6 my $profile = $self->profile;
99 6 ▶ croak "Langertha::Reasoning::BudgetPolicy: form 'range' requires the profile to "
Branch: 1 branch
BranchTrueFalseTotal
unless $profile->has_budget_min and $profile->has_budget_max 1 5 6
Condition66%2/3
$profile->has_budget_min and $profile->has_budget_max
!ll&&!rl&&r
105
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
100 . "carry both budget_min and budget_max (category-b bounds to interpolate across)"
101 unless $profile->has_budget_min && $profile->has_budget_max;
102 }
103  
104 7 return;
105 }
106  
107 # The firewall: no numeric output may cross the profile's category-(b) bounds.
108 81 ▶sub _clamp {
Subroutine _clamp: called CC=5 SCAR=16.2
109 81 my ( $self, $number ) = @_;
110 81 my $profile = $self->profile;
111 81 ▶ $number = $profile->budget_min
Branch: 1 branch
BranchTrueFalseTotal
if $profile->has_budget_min and $number < $profile->budget_min 10 71 81
Condition66%2/3
$profile->has_budget_min and $number < $profile->budget_min
!ll&&!rl&&r
07110
MC/DC50%1/2
$profile->has_budget_min and $number < $profile->budget_min
$profile->has_budget_min $number < $profile->budget_min
Truth table66%2/3
A and B
A= $profile->has_budget_minB= $number < $profile->budget_min
ABresult
0X0
100
111
112 if $profile->has_budget_min && $number < $profile->budget_min;
113 81 ▶ $number = $profile->budget_max
Branch: 1 branch
BranchTrueFalseTotal
if $profile->has_budget_max and $number > $profile->budget_max 10 71 81
Condition66%2/3
$profile->has_budget_max and $number > $profile->budget_max
!ll&&!rl&&r
07110
MC/DC50%1/2
$profile->has_budget_max and $number > $profile->budget_max
$profile->has_budget_max $number > $profile->budget_max
Truth table66%2/3
A and B
A= $profile->has_budget_maxB= $number > $profile->budget_max
ABresult
0X0
100
111
114 if $profile->has_budget_max && $number > $profile->budget_max;
115 81 return $number;
116 }
117  
118 # The ascending ordinal span of the anchor ladder.
119 34 ▶sub _ladder_span {
Subroutine _ladder_span: called CC=1 SCAR=0.0
120 34 my ( $self ) = @_;
121 34 my @ord = sort { $a <=> $b } map { $LEVEL_ORDER{$_} } @{ $self->levels };
122 34 return ( $ord[0], $ord[-1] );
123 }
124  
125 # The fraction (0..1) a level sits at along the anchor ladder, clamped so a level
126 # below the ladder floor pins to 0 and one above the ceiling pins to 1.
127 26 ▶sub _level_fraction {
Subroutine _level_fraction: called CC=6 SCAR=18.5
128 26 my ( $self, $level ) = @_;
129 26 my $ord = $LEVEL_ORDER{$level};
130 26 ▶ croak "Langertha::Reasoning::BudgetPolicy: unknown level '" . ( $level // '' ) . "'"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ord 0 26 26
Condition0%0/2
$level // ""
l!l
00
MC/DC0%0/1
$level // ""
$level
Truth table0%0/2
A
A= $level
Aresult
11
00
131 unless defined $ord;
132 26 my ( $lo, $hi ) = $self->_ladder_span;
133 26 ▶ return 1 if $hi == $lo;
Branch: 1 branch
BranchTrueFalseTotal
if $hi == $lo 0 26 26
134 26 my $frac = ( $ord - $lo ) / ( $hi - $lo );
135 26 ▶ $frac = 0 if $frac < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $frac < 0 5 21 26
136 26 ▶ $frac = 1 if $frac > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $frac > 1 5 21 26
137 26 return $frac;
138 }
139  
140  
141 45 ▶sub budget_for {
Subroutine budget_for: called CC=9 SCAR=22.5
Pod documented
142 45 my ( $self, $level ) = @_;
143 croak "Langertha::Reasoning::BudgetPolicy: unknown level '" . ( $level // '' ) . "'"
144 45 ▶ unless defined $level && exists $LEVEL_ORDER{$level};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level and exists $LEVEL_ORDER{$level} 0 45 45
Condition20%1/5
defined $level and exists $LEVEL_ORDER{$level}
!ll&&!rl&&r
0045
$level // ""
l!l
00
MC/DC0%0/2
defined $level and exists $LEVEL_ORDER{$level}
defined $level exists $LEVEL_ORDER{$level}
MC/DC0%0/1
$level // ""
$level
Truth table33%1/3
A and B
A= defined $levelB= exists $LEVEL_ORDER{$level}
ABresult
0X0
100
111
Truth table0%0/2
A
A= $level
Aresult
11
00
145  
146 45 my $raw;
147 45 ▶ if ( $self->form eq 'explicit' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->form eq 'explicit') { } 19 26 45
148 19 my $points = $self->points;
149 19 ▶ if ( exists $points->{$level} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$points{$level}) { } 12 7 19
150 12 $raw = $points->{$level};
151 }
152 else {
153 # Nearest curated anchor by ordinal distance (ties break to the lower).
154 7 my $want = $LEVEL_ORDER{$level};
155 my ($nearest) = sort {
156 7 abs( $LEVEL_ORDER{$a} - $want ) <=> abs( $LEVEL_ORDER{$b} - $want )
157 22 ▶ || $LEVEL_ORDER{$a} <=> $LEVEL_ORDER{$b}
Condition66%2/3
abs $LEVEL_ORDER{$a} - $want <=> abs $LEVEL_ORDER{$b} - $want || $LEVEL_ORDER{$a} <=> $LEVEL_ORDER{$b}
l!l&&r!l&&!r
2110
MC/DC0%0/2
abs $LEVEL_ORDER{$a} - $want <=> abs $LEVEL_ORDER{$b} - $want || $LEVEL_ORDER{$a} <=> $LEVEL_ORDER{$b}
abs $LEVEL_ORDER{$a} - $want <=> abs $LEVEL_ORDER{$b} - $want $LEVEL_ORDER{$a} <=> $LEVEL_ORDER{$b}
Truth table66%2/3
A || B
A= abs $LEVEL_ORDER{$a} - $want <=> abs $LEVEL_ORDER{$b} - $wantB= $LEVEL_ORDER{$a} <=> $LEVEL_ORDER{$b}
ABresult
1X1
011
000
158 } keys %$points;
159 7 $raw = $points->{$nearest};
160 }
161 }
162 else {
163 26 my $profile = $self->profile;
164 26 my $min = $profile->budget_min;
165 26 my $max = $profile->budget_max;
166 26 my $frac = $self->_level_fraction($level);
167 26 ▶ if ( $self->curve eq 'log' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->curve eq 'log') { } 10 16 26
168 10 ▶ my $base = $min > 0 ? $min : 1;
Branch: 1 branch
BranchTrueFalseTotal
$min > 0 ? : 10 0 10
169 10 $raw = $base * ( $max / $base )**$frac;
170 }
171 else {
172 16 $raw = $min + ( $max - $min ) * $frac;
173 }
174 }
175  
176 45 return $self->_clamp( int( $raw + 0.5 ) );
177 }
178  
179  
180 13 ▶sub level_for {
Subroutine level_for: called CC=8 SCAR=21.0
Pod documented
181 13 my ( $self, $budget ) = @_;
182 13 ▶ croak "Langertha::Reasoning::BudgetPolicy: level_for needs an integer budget"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $budget 0 13 13
183 unless defined $budget;
184  
185 13 my $profile = $self->profile;
186 13 ▶ return 'none'
Branch: 1 branch
BranchTrueFalseTotal
if $profile->has_off_value and $budget == $profile->off_value 1 12 13
Condition100%3/3
$profile->has_off_value and $budget == $profile->off_value
!ll&&!rl&&r
1111
MC/DC100%2/2
$profile->has_off_value and $budget == $profile->off_value
$profile->has_off_value $budget == $profile->off_value
Truth table100%3/3
A and B
A= $profile->has_off_valueB= $budget == $profile->off_value
ABresult
0X0
100
111
187 if $profile->has_off_value && $budget == $profile->off_value;
188  
189 12 $budget = $self->_clamp($budget);
190  
191 12 ▶ if ( $self->form eq 'explicit' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->form eq "explicit") 4 8 12
192 4 my $points = $self->points;
193 my ($nearest) = sort {
194 4 abs( $self->_clamp( $points->{$a} ) - $budget )
195 <=> abs( $self->_clamp( $points->{$b} ) - $budget )
196 12 ▶ || $LEVEL_ORDER{$a} <=> $LEVEL_ORDER{$b}
Condition33%1/3
abs $self->_clamp($$points{$a}) - $budget <=> abs $self->_clamp($$points{$b}) - $budget || $LEVEL_ORDER{$a} <=> $LEVEL_ORDER{$b}
l!l&&r!l&&!r
1200
MC/DC0%0/2
abs $self->_clamp($$points{$a}) - $budget <=> abs $self->_clamp($$points{$b}) - $budget || $LEVEL_ORDER{$a} <=> $LEVEL_ORDER{$b}
abs $self->_clamp($$points{$a}) - $budget <=> abs $self->_clamp($$points{$b}) - $budget $LEVEL_ORDER{$a} <=> $LEVEL_ORDER{$b}
Truth table33%1/3
A || B
A= abs $self->_clamp($$points{$a}) - $budget <=> abs $self->_clamp($$points{$b}) - $budgetB= $LEVEL_ORDER{$a} <=> $LEVEL_ORDER{$b}
ABresult
1X1
011
000
197 } keys %$points;
198 4 return $nearest;
199 }
200  
201 8 my $min = $profile->budget_min;
202 8 my $max = $profile->budget_max;
203 8 my ( $lo, $hi ) = $self->_ladder_span;
204 8 ▶ my $frac = $max == $min ? 0 : ( $budget - $min ) / ( $max - $min );
Branch: 1 branch
BranchTrueFalseTotal
$max == $min ? : 0 8 8
205 8 my $target = $lo + $frac * ( $hi - $lo );
206 my ($nearest) = sort {
207 abs( $LEVEL_ORDER{$a} - $target ) <=> abs( $LEVEL_ORDER{$b} - $target )
208 21 ▶ || $LEVEL_ORDER{$a} <=> $LEVEL_ORDER{$b}
Condition66%2/3
abs $LEVEL_ORDER{$a} - $target <=> abs $LEVEL_ORDER{$b} - $target || $LEVEL_ORDER{$a} <=> $LEVEL_ORDER{$b}
l!l&&r!l&&!r
1920
MC/DC0%0/2
abs $LEVEL_ORDER{$a} - $target <=> abs $LEVEL_ORDER{$b} - $target || $LEVEL_ORDER{$a} <=> $LEVEL_ORDER{$b}
abs $LEVEL_ORDER{$a} - $target <=> abs $LEVEL_ORDER{$b} - $target $LEVEL_ORDER{$a} <=> $LEVEL_ORDER{$b}
Truth table66%2/3
A || B
A= abs $LEVEL_ORDER{$a} - $target <=> abs $LEVEL_ORDER{$b} - $targetB= $LEVEL_ORDER{$a} <=> $LEVEL_ORDER{$b}
ABresult
1X1
011
000
209 8 } @{ $self->levels };
210 8 return $nearest;
211 }
212  
213  
214 1 ▶sub for_model {
Subroutine for_model: called CC=1 SCAR=0.0
Pod documented
215 1 my ( $class, $id, %opts ) = @_;
216 1 return $class->new(
217 profile => Langertha::Reasoning::Profile->for_model($id),
218 %opts,
219 );
220 }
221  
222 2 __PACKAGE__->meta->make_immutable;
223  
224  
225 2 1;
226  
227 __END__
« blib/lib/Langertha/Manifest/Validation.pm ↑ Summary blib/lib/Langertha/Engine/DeepSeek.pm »