blib/lib/Langertha/Role/ReasoningEffort.pm

filter: Statementstmt 58.1% 50 / 86 Branchbran 44.8% 35 / 78 Conditioncond 37.5% 6 / 16 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 60.0% 9 / 15 Podpod 100% 4 / 4 totaltotal 51.2% 107 / 209 CC 46 SCAR 27.7
CC
46
Cov
51%
_warn_thinking_enabled_dropped
22.8
_warn_thinking_enabled_dropped
22.8
reasoning_kwargs_for
18.2
CRAP 16.0
« blib/lib/Langertha/Reasoning.pm ↑ Summary blib/lib/Langertha/Manifest/Builder.pm »
1 package Langertha::Role::ReasoningEffort;
2 # ABSTRACT: Role for an engine with a request-side reasoning-effort control
3 189 our $VERSION = '0.505';
4 189 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 189 ▶use Carp qw( carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 189 ▶use Langertha::Reasoning;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 189 has reasoning_effort => (
9 is => 'ro',
10 isa => 'Str',
11 predicate => 'has_reasoning_effort',
12 );
13  
14  
15 189 has thinking_budget => (
16 is => 'ro',
17 isa => 'Int',
18 predicate => 'has_thinking_budget',
19 );
20  
21  
22 189 has thinking_enabled => (
23 is => 'ro',
24 isa => 'Bool',
25 predicate => 'has_thinking_enabled',
26 );
27  
28 # The engine attributes are one layer: a contradicting pair croaks at
29 # construction already, not first at the request build (karr k382), with the
30 # value object's message. $orig has already turned a list or a HashRef into one
31 # HashRef. AnthropicBase's `effort` alias of reasoning_effort is mapped by its
32 # own BUILDARGS wrapper, which may run after this one, so read the alias too.
33 4135 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
34 4135 my ( $orig, $class, @args ) = @_;
35 4135 my $args = $class->$orig(@args);
36 Langertha::Reasoning->assert_switch_agrees(
37 $args->{thinking_enabled},
38 defined $args->{reasoning_effort} ? $args->{reasoning_effort} : $args->{effort},
39 4135 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'reasoning_effort'} ? : 798 3337 4135
40 4126 return $args;
41 189 };
42  
43  
44 189 has thinking_display => (
45 is => 'ro',
46 isa => 'Str',
47 predicate => 'has_thinking_display',
48 );
49  
50  
51 189 has reasoning_wire_format => (
52 is => 'ro',
53 isa => 'Str',
54 lazy => 1,
55 builder => '_build_reasoning_wire_format',
56 );
57  
58 # Defaults to the OpenAI /chat/completions dialect; AnthropicBase, Gemini and
59 # OpenAIResponses override the builder. Deliberately NOT keyed off
60 # tool_wire_format — engines sharing tool_wire_format=openai (DeepSeek, MiniMax,
61 # Groq) disagree on the reasoning field.
62 427 ▶sub _build_reasoning_wire_format { 'openai' }
Subroutine _build_reasoning_wire_format: called CC=1 SCAR=0.0
63  
64  
65 3070 ▶sub reasoning_kwargs_for {
Subroutine reasoning_kwargs_for: called CC=8 SCAR=29.0
Pod documented
66 0 ▶ my ( $self, %args ) = @_;
Subroutine reasoning_kwargs_for: not called CC=8 SCAR=29.0
Pod documented
67  
68 # The wire agrees with the capability registry (karr #204, ADR 0009): an
69 # engine that advertises neither reasoning control takes none, so nothing
70 # is sent. Both flags, not reasoning_effort alone -- Gemini 2.5 clears
71 # reasoning_effort per model yet takes thinkingBudget, and its effort croak
72 # (ADR 0023) must stay loud.
73 return ()
74 0 ▶ unless $self->supports('reasoning_effort') || $self->supports('thinking_budget');
Branch: 2 branches
BranchTrueFalseTotal
unless $self->supports("reasoning_effort") or $self->supports("thinking_budget") 109 2961 3070
unless $self->supports("reasoning_effort") or $self->supports("thinking_budget") 0 0 0
Condition50%3/6
$self->supports("reasoning_effort") or $self->supports("thinking_budget")
l!l&&r!l&&!r
289170109
$self->supports("reasoning_effort") or $self->supports("thinking_budget")
l!l&&r!l&&!r
000
MC/DC100%2/2
$self->supports("reasoning_effort") or $self->supports("thinking_budget")
$self->supports("reasoning_effort") $self->supports("thinking_budget")
MC/DC0%0/2
$self->supports("reasoning_effort") or $self->supports("thinking_budget")
$self->supports("reasoning_effort") $self->supports("thinking_budget")
Truth table100%3/3
A or B
A= $self->supports("reasoning_effort")B= $self->supports("thinking_budget")
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $self->supports("reasoning_effort")B= $self->supports("thinking_budget")
ABresult
1X1
011
000
75  
76 0 my %merged = $self->_reasoning_controls_for(%args);
77 0 ▶ return () unless %merged;
Branch: 2 branches
BranchTrueFalseTotal
unless %merged 1469 1492 2961
unless %merged 0 0 0
78 0 my $fmt = $self->reasoning_wire_format;
79 0 ▶ my $reasoning = Langertha::Reasoning->new(
Branch: 4 branches
BranchTrueFalseTotal
$self->can('chat_model') ? : 1492 0 1492
$self->can('_reasoning_thinking_toggle') && $self->_reasoning_thinking_toggle ? : 305 1187 1492
$self->can('chat_model') ? : 0 0 0
$self->can('_reasoning_thinking_toggle') && $self->_reasoning_thinking_toggle ? : 0 0 0
Condition33%2/6
$self->can("_reasoning_thinking_toggle") and $self->_reasoning_thinking_toggle
!ll&&!rl&&r
11870305
$self->can("_reasoning_thinking_toggle") and $self->_reasoning_thinking_toggle
!ll&&!rl&&r
000
MC/DC50%1/2
$self->can("_reasoning_thinking_toggle") and $self->_reasoning_thinking_toggle
$self->can("_reasoning_thinking_toggle") $self->_reasoning_thinking_toggle
MC/DC0%0/2
$self->can("_reasoning_thinking_toggle") and $self->_reasoning_thinking_toggle
$self->can("_reasoning_thinking_toggle") $self->_reasoning_thinking_toggle
Truth table66%2/3
A and B
A= $self->can("_reasoning_thinking_toggle")B= $self->_reasoning_thinking_toggle
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $self->can("_reasoning_thinking_toggle")B= $self->_reasoning_thinking_toggle
ABresult
0X0
100
111
80 %merged,
81 ( $self->can('chat_model') ? ( model => $self->chat_model ) : () ),
82 # The thinking on/off toggle (MiniMax, both Kimi faces) is an
83 # endpoint's spelling, not a model's: an engine opts in with
84 # _reasoning_thinking_toggle, every other engine serializes a toggle
85 # model's id like any unlisted id (ADR 0023 k209 Update, review I1).
86 ( $self->can('_reasoning_thinking_toggle') && $self->_reasoning_thinking_toggle
87 ? ( thinking_toggle => 1 ) : () ),
88 );
89 0 ▶ $self->_warn_thinking_enabled_dropped( $fmt, $reasoning->thinking_enabled )
Branch: 2 branches
BranchTrueFalseTotal
if $reasoning->drops_thinking_enabled_on($fmt) 8 1282 1290
if $reasoning->drops_thinking_enabled_on($fmt) 0 0 0
90 if $reasoning->drops_thinking_enabled_on($fmt);
91 0 return $reasoning->to($fmt);
92 }
93  
94 # Per-request controls (chat_f, karr #46) beat the engine attributes. %args may
95 # carry effort / thinking_enabled / thinking_budget / thinking_display (or the
96 # canonical control name reasoning_effort, so the whole controls hash can be
97 # passed wholesale). thinking_budget and thinking_display layer per key: a key
98 # %args does not carry falls back to its attribute. effort and thinking_enabled are
99 # ONE concern (karr k382): a per-request value for either replaces both engine
100 # attributes, so engine reasoning_effort=high + per-request thinking_enabled=0
101 # is thinking off, not a contradiction. Only a contradiction within one layer
102 # croaks (Langertha::Reasoning BUILD). An undef per-request value counts as not
103 # set, so it neither replaces the engine values nor is sent. thinking_enabled=0
104 # without an effort is resolved to effort 'none' here as well as in
105 # Langertha::Reasoning (Langertha::Reasoning::normalize_off_switch, not on a
106 # budget generation), so readers of the resolved effort (Engine::OpenAI's ADR
107 # 0025 temperature gate, DeepSeek) see the off switch.
108 3166 ▶sub _reasoning_controls_for {
Subroutine _reasoning_controls_for: called CC=8 SCAR=28.7
109 0 ▶ my ( $self, %args ) = @_;
Subroutine _reasoning_controls_for: not called CC=8 SCAR=28.7
110 my $request_sets_switch =
111 0 grep { defined $args{$_} } qw( effort reasoning_effort thinking_enabled );
112 12084 ▶ my $attr = sub {
Subroutine __ANON__: called CC=2 SCAR=9.2
113 0 ▶ my ( $name, $key ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=9.2
114 0 my $predicate = 'has_' . $name;
115 0 ▶ return $self->$predicate ? ( $key => $self->$name ) : ();
Branch: 2 branches
BranchTrueFalseTotal
$self->$predicate ? : 1359 10725 12084
$self->$predicate ? : 0 0 0
116 0 };
117 my %merged = (
118 ( $request_sets_switch ? (
119 0 ▶ ( map { defined $args{$_} ? ( $_ => $args{$_} ) : () } qw( effort thinking_enabled ) ),
Branch: 4 branches
BranchTrueFalseTotal
defined $args{$_} ? : 13 567 580
defined $args{'reasoning_effort'} ? : 281 9 290
defined $args{$_} ? : 0 0 0
defined $args{'reasoning_effort'} ? : 0 0 0
120 ( defined $args{reasoning_effort} ? ( effort => $args{reasoning_effort} ) : () ),
121 ) : (
122 $attr->( reasoning_effort => 'effort' ),
123 $attr->( thinking_enabled => 'thinking_enabled' ),
124 ) ),
125 $attr->( thinking_budget => 'thinking_budget' ),
126 $attr->( thinking_display => 'thinking_display' ),
127 0 ▶ ( map { exists $args{$_} ? ( $_ => $args{$_} ) : () } qw( thinking_budget thinking_display ) ),
Branch: 4 branches
BranchTrueFalseTotal
$request_sets_switch ? : 290 2876 3166
exists $args{$_} ? : 102 6230 6332
$request_sets_switch ? : 0 0 0
exists $args{$_} ? : 0 0 0
128 );
129 0 ▶ my $model = $self->can('chat_model') ? $self->chat_model : undef;
Branch: 2 branches
BranchTrueFalseTotal
$self->can('chat_model') ? : 3166 0 3166
$self->can('chat_model') ? : 0 0 0
130 0 ▶ my $normalized = Langertha::Reasoning->normalize_off_switch(
Branch: 2 branches
BranchTrueFalseTotal
defined $model ? : 3166 0 3166
defined $model ? : 0 0 0
131 { %merged, ( defined $model ? ( model => $model ) : () ) } );
132 0 ▶ $merged{effort} = $normalized->{effort} if defined $normalized->{effort};
Branch: 2 branches
BranchTrueFalseTotal
if defined $normalized->{"effort"} 1331 1835 3166
if defined $$normalized{"effort"} 0 0 0
133 0 return %merged;
134 }
135  
136 # A switch without an effort level that has no wire form here (karr k382):
137 # thinking_enabled => 1 on a wire without a level-free on-switch, or => 0 on a
138 # budget generation (Gemini 2.5), which takes no effort. Never croak: one
139 # request matrix runs over every engine (Holmgang), so the switch is dropped
140 # with a carp, once per engine instance, value and wire.
141 9 ▶sub _warn_thinking_enabled_dropped {
Subroutine _warn_thinking_enabled_dropped: called CC=8 SCAR=31.3
142 0 ▶ my ( $self, $fmt, $on ) = @_;
Subroutine _warn_thinking_enabled_dropped: not called CC=8 SCAR=31.3
143 0 ▶ $on = 1 unless defined $on;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $on 1 8 9
unless defined $on 0 0 0
144 0 ▶ my $model = $self->can('chat_model') ? ( $self->chat_model // '' ) : '';
Branch: 2 branches
BranchTrueFalseTotal
$self->can('chat_model') ? : 9 0 9
$self->can('chat_model') ? : 0 0 0
Condition25%1/4
$self->chat_model // ""
l!l
90
$self->chat_model // ""
l!l
00
MC/DC0%0/1
$self->chat_model // ""
$self->chat_model
MC/DC0%0/1
$self->chat_model // ""
$self->chat_model
Truth table50%1/2
A
A= $self->chat_model
Aresult
11
00
Truth table0%0/2
A
A= $self->chat_model
Aresult
11
00
145 0 ▶ my $message = ( ref $self ) . ': dropping thinking_enabled => ' . ( $on ? 1 : 0 )
Branch: 4 branches
BranchTrueFalseTotal
$on ? : 6 3 9
$on ? : 6 3 9
$on ? : 0 0 0
$on ? : 0 0 0
146 . ( $on
147 ? " -- reasoning wire '" . $fmt . "' has no thinking on-switch without an "
148 . "effort level; set reasoning_effort (e.g. 'medium') to turn thinking on"
149 : " -- model '" . $model . "' takes a thinking budget, not an effort level, "
150 . "so reasoning wire '" . $fmt . "' has no off switch for it; configure "
151 . 'thinking_budget instead' );
152 0 ▶ my $once = 'thinking_enabled=' . ( $on ? 1 : 0 ) . ' wire=' . $fmt . ' model=' . $model;
Branch: 2 branches
BranchTrueFalseTotal
$on ? : 6 3 9
$on ? : 0 0 0
153 0 ▶ return $self->_langertha_carp( $message, $once ) if $self->can('_langertha_carp');
Branch: 2 branches
BranchTrueFalseTotal
if $self->can("_langertha_carp") 9 0 9
if $self->can("_langertha_carp") 0 0 0
154 0 carp $message;
155 0 return;
156 }
157  
158  
159 0 ▶sub reasoning_kwargs {
Subroutine reasoning_kwargs: not called CC=1 SCAR=6.9
Pod documented
160 0 ▶ my ( $self ) = @_;
Subroutine reasoning_kwargs: not called CC=1 SCAR=6.9
Pod documented
161 0 return $self->reasoning_kwargs_for;
162 }
163  
164  
165  
166 189 1;
167  
168 __END__
« blib/lib/Langertha/Reasoning.pm ↑ Summary blib/lib/Langertha/Manifest/Builder.pm »