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/Protocol/A2A.pm ↑ Summary blib/lib/Langertha/Knarr/Handler/ACPClient.pm »
1 package Langertha::Knarr::Reasoning;
2 # ABSTRACT: Map a face's native reasoning controls onto the normalized reasoning_effort level
3 47 our $VERSION = '1.102';
4 47 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 47 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 47 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 # The normalized ascending reasoning vocabulary (Langertha::Reasoning::Level).
10 47 my %LEVEL = map { $_ => 1 } qw( none minimal low medium high xhigh max );
11  
12 47 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 47 has budget_points => (
29 is => 'ro',
30 isa => 'HashRef[Int]',
31 predicate => 'has_budget_points',
32 );
33  
34  
35 101 ▶sub BUILD {
Subroutine BUILD: called CC=7 SCAR=19.5
Pod undocumented
36 101 my ( $self, $args ) = @_;
37 croak __PACKAGE__."->new: unknown default_level '".$args->{default_level}."'"
38 101 ▶ 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 100 101
Condition80%4/5
exists $args->{"default_level"} and not $LEVEL{$args->{"default_level"} // ""}
!ll&&!rl&&r
9911
$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 100 ▶ if ( $self->has_budget_points ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_budget_points) 2 98 100
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 99 return;
45 }
46  
47 47 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 634 ▶sub _explicit_level {
Subroutine _explicit_level: called CC=4 SCAR=14.2
98 634 my ( $self, $value ) = @_;
99 634 ▶ return defined $value && !ref $value && length $value ? $value : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $value && !ref($value) && length $value ? : 4 630 634
Condition66%4/6
defined $value and not ref $value and length $value
!ll&&!rl&&r
63004
defined $value and not ref $value
!ll&&!rl&&r
63004
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 214 ▶sub from_anthropic {
Subroutine from_anthropic: called CC=11 SCAR=24.0
Pod documented
103 214 my ( $self, $data ) = @_;
104 214 ▶ return unless ref $data eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 0 214 214
105 214 ▶ my $output_config = ref $data->{output_config} eq 'HASH' ? $data->{output_config} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $data->{'output_config'} eq 'HASH' ? : 2 212 214
106 214 for my $explicit ( $data->{reasoning_effort}, $output_config->{effort} ) {
107 427 my ($level) = $self->_explicit_level($explicit);
108 427 ▶ return $level if defined $level;
Branch: 1 branch
BranchTrueFalseTotal
if defined $level 3 424 427
109 }
110 211 my $thinking = $data->{thinking};
111 211 ▶ return unless ref $thinking eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $thinking eq "HASH" 191 20 211
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 207 ▶sub from_ollama {
Subroutine from_ollama: called CC=6 SCAR=17.9
Pod documented
122 207 my ( $self, $think, $explicit ) = @_;
123 207 my ($level) = $self->_explicit_level($explicit);
124 207 ▶ return $level if defined $level;
Branch: 1 branch
BranchTrueFalseTotal
if defined $level 1 206 207
125 206 ▶ return unless defined $think;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $think 195 11 206
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 47 __PACKAGE__->meta->make_immutable;
133  
134  
135 47 1;
136  
137 __END__
138  
139 =pod
140  
141 =encoding UTF-8
142  
143 =head1 NAME
144  
145 Langertha::Knarr::Reasoning - Map a face's native reasoning controls onto the normalized reasoning_effort level
146  
147 =head1 VERSION
148  
149 version 1.102
150  
151 =head1 SYNOPSIS
152  
153 my $reasoning = Langertha::Knarr::Reasoning->new;
154  
155 # Anthropic face: thinking / output_config.effort
156 my $level = $reasoning->from_anthropic($body);
157  
158 # Ollama face: think (boolean or level string)
159 my $level = $reasoning->from_ollama( $body->{think} );
160  
161 # exactly overridable
162 my $mine = Langertha::Knarr::Reasoning->new(
163 default_level => 'high',
164 budget_points => { low => 1024, medium => 4096, high => 16384 },
165 );
166  
167 =head1 DESCRIPTION
168  
169 Only the OpenAI face carries the normalized C<reasoning_effort> enum
170 natively. The Anthropic face carries C<thinking> (C<enabled> with a
171 C<budget_tokens> integer, C<adaptive>, C<disabled>) and the Ollama face
172 carries C<think> (a boolean, or a level string on GPT-OSS). This class turns
173 those into the one normalized level L<Langertha::Knarr::Request/reasoning_effort>
174 holds, so the routed path forwards a client's thinking request to whatever
175 engine the model routes to, capability-gated like every other control.
176  
177 The mapping is:
178  
179 =over
180  
181 =item * C<thinking.type = disabled>, C<think: false> - C<none>
182  
183 =item * C<thinking.type = adaptive>, C<thinking.type = enabled> without a
184 budget, C<think: true> - L</default_level> (C<medium>)
185  
186 =item * C<think: "low">, ... - the level as sent, when it is one of
187 C<none minimal low medium high xhigh max>; any other string maps to nothing
188  
189 =item * C<thinking.budget_tokens = N> - the nearest level of a
190 L<Langertha::Reasoning::BudgetPolicy> for the request's model (see
191 L</budget_points>)
192  
193 =back
194  
195 A face's own explicit effort always wins over a derived one: a top-level
196 C<reasoning_effort>, and on the Anthropic face C<output_config.effort>, are
197 taken as sent. Every value that does not fit maps to nothing, never to an
198 invented level, and nothing here croaks on a client body.
199  
200 Budget mapping needs a core with L<Langertha::Reasoning::BudgetPolicy>
201 (L</budget_available>). On an older core (Langertha 0.503) C<budget_tokens>
202 stays unmapped - the request carries no C<reasoning_effort> from it - while
203 the boolean and level-string forms, which need no budget convention, still
204 map.
205  
206 =head2 default_level
207  
208 The level a client's "thinking on" without a level maps to (C<think: true>,
209 C<thinking.type = adaptive>, C<thinking.type = enabled> without a budget).
210 Defaults to L<Langertha::Reasoning::BudgetPolicy/default_bool_level> on a
211 core that has it, else C<medium>. Its "off" counterpart is always C<none>.
212  
213 =head2 budget_points
214  
215 Optional curated level-to-token anchors, e.g.
216 C<< { low => 1024, medium => 4096, high => 16384 } >>. When set, every
217 C<budget_tokens> maps through an C<explicit>
218 L<Langertha::Reasoning::BudgetPolicy> with these points. When unset, a model
219 whose L<Langertha::Reasoning::Profile> carries both budget bounds (Gemini 2.5)
220 maps through the C<range> policy across those bounds, and every other model
221 through L</fallback_budget_points>. The policy always clamps to the profile's
222 enforced bounds.
223  
224 =head2 budget_available
225  
226 True when the installed core has L<Langertha::Reasoning::BudgetPolicy>, i.e.
227 when C<budget_tokens> maps at all.
228  
229 =head2 fallback_budget_points
230  
231 The anchors used for a model whose profile carries no budget bounds (every
232 Claude model) when L</budget_points> is unset:
233 C<< { low => 2048, medium => 8192, high => 24576 } >> - the curated example
234 anchors of L<Langertha::Reasoning::BudgetPolicy>. No provider publishes a
235 level-to-budget table, so these are convention, not wire-truth. There is no
236 C<none> anchor: a client that enabled thinking with a budget never maps to
237 C<none>. Override in a subclass, or set L</budget_points>.
238  
239 =head2 budget_policy
240  
241 my $policy = $reasoning->budget_policy('claude-sonnet-4-6');
242  
243 The L<Langertha::Reasoning::BudgetPolicy> a budget for C<$model> maps
244 through (see L</budget_points>), or nothing when L</budget_available> is
245 false or the policy cannot be built.
246  
247 =head2 level_for_budget
248  
249 my $level = $reasoning->level_for_budget( 4096, $model );
250  
251 The normalized level a token budget maps to for C<$model>, or nothing for a
252 budget that is not a non-negative integer or when budgets do not map (see
253 L</budget_available>).
254  
255 =head2 level_for_bool
256  
257 my $level = $reasoning->level_for_bool(1); # default_level
258 my $level = $reasoning->level_for_bool(0); # 'none'
259  
260 =head2 from_anthropic
261  
262 my $level = $reasoning->from_anthropic($decoded_body);
263  
264 The level an Anthropic Messages body asks for: a top-level
265 C<reasoning_effort> or C<output_config.effort> as sent, else what
266 C<thinking> maps to (see L</DESCRIPTION>), else nothing.
267  
268 =head2 from_ollama
269  
270 my $level = $reasoning->from_ollama( $body->{think}, $body->{reasoning_effort} );
271  
272 The level an Ollama C<think> value asks for: a JSON boolean maps through
273 L</level_for_bool>, a level string of the normalized vocabulary is kept, any
274 other value maps to nothing. An explicit C<reasoning_effort> wins.
275  
276 =head1 SEE ALSO
277  
278 =over
279  
280 =item * L<Langertha::Knarr::Request> - Holds the resulting C<reasoning_effort>, capability-gated in C<chat_f_args>
281  
282 =item * L<Langertha::Reasoning::BudgetPolicy> - The budget-to-level convention budgets map through
283  
284 =item * L<Langertha::Reasoning::Profile> - The per-model bounds that convention is clamped to
285  
286 =back
287  
288 =head1 SUPPORT
289  
290 =head2 Issues
291  
292 Please report bugs and feature requests on GitHub at
293 L<https://github.com/Getty/langertha-knarr/issues>.
294  
295 =head2 IRC
296  
297 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
298  
299 =head1 CONTRIBUTING
300  
301 Contributions are welcome! Please fork the repository and submit a pull request.
302  
303 =head1 AUTHOR
304  
305 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
306  
307 =head1 COPYRIGHT AND LICENSE
308  
309 This software is copyright (c) 2026 by Torsten Raudssus.
310  
311 This is free software; you can redistribute it and/or modify it under
312 the same terms as the Perl 5 programming language system itself.
313  
314 =cut
« blib/lib/Langertha/Knarr/Protocol/A2A.pm ↑ Summary blib/lib/Langertha/Knarr/Handler/ACPClient.pm »