| 1 | package Langertha::Reasoning::BudgetPolicy; | ||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Invented level<->token-budget interpolation, clamped to a Profile's enforced bounds | ||||||||||||||||||||||||||||||||||||||
| 3 | 2 | our $VERSION = '0.503'; | |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||
| 93 | 9 | ▶ | if ( $self->form eq 'explicit' && !$self->has_points ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->form eq "explicit" and not $self->has_points
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
| |||||||||||||||||||||||||||||||||||||||
| 94 | 1 | croak "Langertha::Reasoning::BudgetPolicy: form 'explicit' requires 'points'"; | |||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||
| 97 | 8 | ▶ | if ( $self->form eq 'range' ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 98 | 6 | my $profile = $self->profile; | |||||||||||||||||||||||||||||||||||||
| 99 | 6 | ▶ | croak "Langertha::Reasoning::BudgetPolicy: form 'range' requires the profile to " | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$profile->has_budget_min and $profile->has_budget_max
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
| |||||||||||||||||||||||||||||||||||||||
| 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 | 83 | ▶ | sub _clamp { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_clamp: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||
| 109 | 83 | my ( $self, $number ) = @_; | |||||||||||||||||||||||||||||||||||||
| 110 | 83 | my $profile = $self->profile; | |||||||||||||||||||||||||||||||||||||
| 111 | 83 | ▶ | $number = $profile->budget_min | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$profile->has_budget_min and $number < $profile->budget_min
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
| |||||||||||||||||||||||||||||||||||||||
| 112 | if $profile->has_budget_min && $number < $profile->budget_min; | ||||||||||||||||||||||||||||||||||||||
| 113 | 83 | ▶ | $number = $profile->budget_max | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$profile->has_budget_max and $number > $profile->budget_max
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
| |||||||||||||||||||||||||||||||||||||||
| 114 | if $profile->has_budget_max && $number > $profile->budget_max; | ||||||||||||||||||||||||||||||||||||||
| 115 | 83 | 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
Condition0%0/2
$level // ""
MC/DC0%0/1
$level // ""
$level
Truth table0%0/2
A
A= $level
| |||||||||||||||||||||||||||||||||||||||
| 131 | unless defined $ord; | ||||||||||||||||||||||||||||||||||||||
| 132 | 26 | my ( $lo, $hi ) = $self->_ladder_span; | |||||||||||||||||||||||||||||||||||||
| 133 | 26 | ▶ | return 1 if $hi == $lo; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 134 | 26 | my $frac = ( $ord - $lo ) / ( $hi - $lo ); | |||||||||||||||||||||||||||||||||||||
| 135 | 26 | ▶ | $frac = 0 if $frac < 0; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 136 | 26 | ▶ | $frac = 1 if $frac > 1; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
Condition20%1/5
defined $level and exists $LEVEL_ORDER{$level}
$level // ""
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}
Truth table0%0/2
A
A= $level
| |||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||
| 146 | 45 | my $raw; | |||||||||||||||||||||||||||||||||||||
| 147 | 45 | ▶ | if ( $self->form eq 'explicit' ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 148 | 19 | my $points = $self->points; | |||||||||||||||||||||||||||||||||||||
| 149 | 19 | ▶ | if ( exists $points->{$level} ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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 | 27 | ▶ | || $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}
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}
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 168 | 10 | ▶ | my $base = $min > 0 ? $min : 1; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 183 | unless defined $budget; | ||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||
| 185 | 13 | my $profile = $self->profile; | |||||||||||||||||||||||||||||||||||||
| 186 | 13 | ▶ | return 'none' | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$profile->has_off_value and $budget == $profile->off_value
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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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 | 13 | ▶ | || $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}
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}
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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}
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}
| |||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||
| 229 | =pod | ||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||
| 231 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||
| 233 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||
| 235 | Langertha::Reasoning::BudgetPolicy - Invented level<->token-budget interpolation, clamped to a Profile's enforced bounds | ||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||
| 237 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||
| 239 | version 0.503 | ||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||
| 241 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||
| 243 | my $policy = Langertha::Reasoning::BudgetPolicy->new( | ||||||||||||||||||||||||||||||||||||||
| 244 | profile => Langertha::Reasoning::Profile->for_model('gemini-2.5-pro'), | ||||||||||||||||||||||||||||||||||||||
| 245 | form => 'range', # interpolate across the profile's [min,max] | ||||||||||||||||||||||||||||||||||||||
| 246 | levels => [qw( low medium high )], | ||||||||||||||||||||||||||||||||||||||
| 247 | ); | ||||||||||||||||||||||||||||||||||||||
| 248 | my $budget = $policy->budget_for('medium'); # ~16448, always within 128..32768 | ||||||||||||||||||||||||||||||||||||||
| 249 | my $level = $policy->level_for(4096); # nearest anchor level | ||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||
| 251 | # explicit anchors instead of a curve | ||||||||||||||||||||||||||||||||||||||
| 252 | my $curated = Langertha::Reasoning::BudgetPolicy->new( | ||||||||||||||||||||||||||||||||||||||
| 253 | profile => Langertha::Reasoning::Profile->for_model('gemini-2.5-flash'), | ||||||||||||||||||||||||||||||||||||||
| 254 | points => { none => 0, low => 2048, medium => 8192, high => 24576 }, | ||||||||||||||||||||||||||||||||||||||
| 255 | ); | ||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||
| 257 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||
| 259 | Category (c) of karr k173's three-category reasoning taxonomy (ADR 0023): the | ||||||||||||||||||||||||||||||||||||||
| 260 | B<invented> level-to-token-budget interpolation and its inbound inverse. B<No | ||||||||||||||||||||||||||||||||||||||
| 261 | provider publishes an official reasoning-level to token-budget mapping> — OpenAI | ||||||||||||||||||||||||||||||||||||||
| 262 | and Anthropic effort is adaptive and undocumented, and the only numeric | ||||||||||||||||||||||||||||||||||||||
| 263 | level-to-token formulas in the wild are OpenRouter's, labeled OpenRouter's own | ||||||||||||||||||||||||||||||||||||||
| 264 | convention. So every number this class emits is B<library convention, not | ||||||||||||||||||||||||||||||||||||||
| 265 | wire-truth>, and L</source> should say so honestly. | ||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||
| 267 | Because the numbers are invented, they must never be able to emit a value the | ||||||||||||||||||||||||||||||||||||||
| 268 | API rejects. Every numeric output of this class is B<clamped to the owning | ||||||||||||||||||||||||||||||||||||||
| 269 | L<Langertha::Reasoning::Profile>'s category-(b) enforced bounds> | ||||||||||||||||||||||||||||||||||||||
| 270 | (L<Langertha::Reasoning::Profile/budget_min> / L</budget_max>). This is the | ||||||||||||||||||||||||||||||||||||||
| 271 | firewall rule: convention (c) may B<read> the profile's enforced bounds (b) but | ||||||||||||||||||||||||||||||||||||||
| 272 | can never B<cross> them. Putting those bounds inside this convention layer — as | ||||||||||||||||||||||||||||||||||||||
| 273 | the original karr k173 ticket proposed — would let an override silently violate a | ||||||||||||||||||||||||||||||||||||||
| 274 | hard API bound; keeping them in the Profile and clamping against them here is the | ||||||||||||||||||||||||||||||||||||||
| 275 | single most important correction of the design. | ||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||
| 277 | This is B<not> a capability and is B<not> default-shipped for effort providers. | ||||||||||||||||||||||||||||||||||||||
| 278 | It exists only where a downstream consumer needs budget<->level conversion and | ||||||||||||||||||||||||||||||||||||||
| 279 | constructs it explicitly. Nothing in Langertha wires it in by default. | ||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||
| 281 | =head2 profile | ||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||
| 283 | The owning L<Langertha::Reasoning::Profile>. Its category-(b) bounds | ||||||||||||||||||||||||||||||||||||||
| 284 | (L<Langertha::Reasoning::Profile/budget_min> / L</budget_max>) are the firewall | ||||||||||||||||||||||||||||||||||||||
| 285 | every numeric output is clamped to. A C<range>-form policy needs both bounds | ||||||||||||||||||||||||||||||||||||||
| 286 | present (there is nothing to interpolate across otherwise). | ||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||
| 288 | =head2 form | ||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||
| 290 | C<range> (interpolate a curve across the profile's C<[budget_min, budget_max]>) | ||||||||||||||||||||||||||||||||||||||
| 291 | or C<explicit> (look the budget up in curated L</points>). Defaults to | ||||||||||||||||||||||||||||||||||||||
| 292 | C<explicit> when L</points> is given, otherwise C<range>. | ||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||
| 294 | =head2 curve | ||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||
| 296 | For C<form =E<gt> 'range'>: C<linear> (even spacing) or C<log> (geometric | ||||||||||||||||||||||||||||||||||||||
| 297 | spacing, finer at the low end). Ignored for C<explicit>. Default C<linear>; per | ||||||||||||||||||||||||||||||||||||||
| 298 | karr k173 q3 the curve is convention, so C<log> is offered but not asserted as | ||||||||||||||||||||||||||||||||||||||
| 299 | truth. | ||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||
| 301 | =head2 points | ||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||
| 303 | For C<form =E<gt> 'explicit'>: a curated map of reasoning level to token budget | ||||||||||||||||||||||||||||||||||||||
| 304 | (the invented anchors). Keys must be members of the normalized vocabulary | ||||||||||||||||||||||||||||||||||||||
| 305 | (C<none|minimal|low|medium|high|xhigh|max>). Each looked-up budget is still | ||||||||||||||||||||||||||||||||||||||
| 306 | clamped to the profile's (b) bounds, so a curated anchor outside them can never | ||||||||||||||||||||||||||||||||||||||
| 307 | reach the wire. | ||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||
| 309 | =head2 levels | ||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||
| 311 | The quantization anchor ladder (ascending) this convention spans — the | ||||||||||||||||||||||||||||||||||||||
| 312 | consumer-side anchors that deliberately do B<not> live in the Profile (whose | ||||||||||||||||||||||||||||||||||||||
| 313 | C<levels> is empty for a budget control, keeping it pure wire-truth). Defaults to | ||||||||||||||||||||||||||||||||||||||
| 314 | the profile's C<levels> when it has any, to the sorted keys of L</points> for an | ||||||||||||||||||||||||||||||||||||||
| 315 | explicit policy, else to C<low|medium|high>. | ||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||
| 317 | =head2 default_bool_level | ||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||
| 319 | The normalized level a boolean-control provider's "thinking on" (C<think:true>) | ||||||||||||||||||||||||||||||||||||||
| 320 | corresponds to for this convention, the boolean-to-level half of the inbound | ||||||||||||||||||||||||||||||||||||||
| 321 | bijection (its "off" counterpart is C<none>). Default C<medium>. | ||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||
| 323 | =head2 source | ||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||
| 325 | The honest curation receipt. Because no provider publishes a level-to-budget | ||||||||||||||||||||||||||||||||||||||
| 326 | table, this defaults to a string that says the numbers are convention rather than | ||||||||||||||||||||||||||||||||||||||
| 327 | wire-truth; a consumer curating empirical anchors should replace it with what it | ||||||||||||||||||||||||||||||||||||||
| 328 | measured and when. | ||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||
| 330 | =head2 budget_for | ||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||
| 332 | my $tokens = $policy->budget_for('medium'); | ||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||
| 334 | Convert a normalized reasoning level to an integer token budget. In C<explicit> | ||||||||||||||||||||||||||||||||||||||
| 335 | form the curated L</points> anchor is used (the nearest anchor by ordinal when | ||||||||||||||||||||||||||||||||||||||
| 336 | the exact level is absent); in C<range> form the L</curve> is interpolated across | ||||||||||||||||||||||||||||||||||||||
| 337 | the profile's C<[budget_min, budget_max]>. The result is B<always clamped to the | ||||||||||||||||||||||||||||||||||||||
| 338 | profile's category-(b) bounds> — the firewall — so an invented number can never | ||||||||||||||||||||||||||||||||||||||
| 339 | reach the wire above the ceiling or below the floor. | ||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||
| 341 | =head2 level_for | ||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||
| 343 | my $level = $policy->level_for(4096); | ||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||
| 345 | The inbound inverse: convert an integer token budget to the nearest normalized | ||||||||||||||||||||||||||||||||||||||
| 346 | level on the anchor ladder. The budget is clamped to the profile's (b) bounds | ||||||||||||||||||||||||||||||||||||||
| 347 | first, so an out-of-range budget maps to the boundary level rather than off the | ||||||||||||||||||||||||||||||||||||||
| 348 | ladder; a budget equal to the profile's C<off_value> maps to C<none>. | ||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||
| 350 | =head2 for_model | ||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||
| 352 | my $policy = Langertha::Reasoning::BudgetPolicy->for_model( | ||||||||||||||||||||||||||||||||||||||
| 353 | 'gemini-2.5-pro', form => 'range' ); | ||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||
| 355 | Convenience constructor mirroring L<Langertha::Reasoning::Profile/for_model>: | ||||||||||||||||||||||||||||||||||||||
| 356 | resolves the id to its profile and passes the remaining convention options | ||||||||||||||||||||||||||||||||||||||
| 357 | (L</form>, L</curve>, L</points>, L</levels>, L</source>) through to L</new>. | ||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||
| 359 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||
| 361 | =over | ||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||
| 363 | =item * L<Langertha::Reasoning::Profile> - The wire-truth categories (a)+(b) whose bounds this convention (c) is clamped to | ||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||
| 365 | =item * L<Langertha::Reasoning> - The value object that resolves and consumes profiles for outbound serialization | ||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||
| 367 | =back | ||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||
| 369 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||
| 371 | =head2 Issues | ||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||
| 373 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||||||||
| 374 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||
| 376 | =head2 IRC | ||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||
| 378 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||
| 380 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||
| 382 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||
| 384 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||
| 386 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||
| 388 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||
| 390 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||
| 392 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||
| 393 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||
| 395 | =cut | ||||||||||||||||||||||||||||||||||||||