blib/lib/Langertha/Role/RuntimeKnobs.pm

filter: Statementstmt 87.8% 29 / 33 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 4 / 6 Podpod 100% 2 / 2 totaltotal 87.2% 41 / 47 CC 5 SCAR 11.6
CC
5
Cov
90%
knobs_kwargs_for
5.0
_build_knob_wire_format
2.0
knobs_kwargs
2.0
CRAP 3.2
« blib/lib/Langertha/PromptCache.pm ↑ Summary blib/lib/Langertha/ImageGen.pm »
1 package Langertha::Role::RuntimeKnobs;
2 # ABSTRACT: Role for a self-hosted engine with per-request prefix-cache runtime knobs
3 35 our $VERSION = '0.503';
4 35 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 35 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 35 ▶use Langertha::Runtime::Knobs;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 35 has prefix_cache_salt => (
9 is => 'ro',
10 isa => 'Str',
11 predicate => 'has_prefix_cache_salt',
12 );
13  
14  
15 35 has cache_prompt => (
16 is => 'ro',
17 isa => 'Bool',
18 predicate => 'has_cache_prompt',
19 );
20  
21  
22 35 has n_cache_reuse => (
23 is => 'ro',
24 isa => 'Int',
25 predicate => 'has_n_cache_reuse',
26 );
27  
28  
29 35 has id_slot => (
30 is => 'ro',
31 isa => 'Int',
32 predicate => 'has_id_slot',
33 );
34  
35  
36 35 has priority => (
37 is => 'ro',
38 isa => 'Int',
39 predicate => 'has_priority',
40 );
41  
42  
43 35 has return_cached_tokens_details => (
44 is => 'ro',
45 isa => 'Bool',
46 predicate => 'has_return_cached_tokens_details',
47 );
48  
49  
50 35 has extra_key => (
51 is => 'ro',
52 isa => 'Str',
53 predicate => 'has_extra_key',
54 );
55  
56  
57 35 has knob_wire_format => (
58 is => 'ro',
59 isa => 'Str',
60 lazy => 1,
61 builder => '_build_knob_wire_format',
62 );
63  
64 # No shared default exists — an 'openai' knob dialect does not exist (the
65 # self-hosted servers each speak their own field set), so every engine that
66 # composes this role must set its tag explicitly. Engines override the builder
67 # (sub _build_knob_wire_format { 'vllm' }) — the codebase convention for
68 # wire-format tags (cf. _build_tool_wire_format).
69 0 ▶sub _build_knob_wire_format {
Subroutine _build_knob_wire_format: not called CC=1 SCAR=6.9
70 0 my ( $self ) = @_;
71 0 croak ref($self) . " composes Langertha::Role::RuntimeKnobs but does not set "
72 . "knob_wire_format — there is no shared default (an 'openai' knob dialect "
73 . "does not exist); set it explicitly (vllm | sglang | llamacpp)";
74 }
75  
76  
77 35 my @KNOBS = qw(
78 prefix_cache_salt
79 cache_prompt
80 n_cache_reuse
81 id_slot
82 priority
83 return_cached_tokens_details
84 extra_key
85 );
86  
87 216 ▶sub knobs_kwargs_for {
Subroutine knobs_kwargs_for: called CC=5 SCAR=16.1
Pod documented
88 216 my ( $self, %args ) = @_;
89  
90 # Per-request controls (chat_f, karr #46) beat the engine attributes on a
91 # per-key basis: %args may carry any of the canonical knob names (the same
92 # names as the attributes — there is no separate alias vocabulary), and any
93 # key it does not carry falls back to the configured attribute.
94 216 my %merged;
95 216 for my $knob (@KNOBS) {
96 1512 my $has = "has_$knob";
97 1512 ▶ $merged{$knob} = $self->$knob if $self->$has;
Branch: 1 branch
BranchTrueFalseTotal
if $self->$has 9 1503 1512
98 1512 ▶ $merged{$knob} = $args{$knob} if exists $args{$knob};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$knob} 4 1508 1512
99 }
100 216 ▶ return () unless %merged;
Branch: 1 branch
BranchTrueFalseTotal
unless %merged 210 6 216
101 return Langertha::Runtime::Knobs->new(
102 6 map { $_ => $merged{$_} } keys %merged,
103 )->to( $self->knob_wire_format );
104 }
105  
106  
107 0 ▶sub knobs_kwargs {
Subroutine knobs_kwargs: not called CC=1 SCAR=6.9
Pod documented
108 0 my ( $self ) = @_;
109 0 return $self->knobs_kwargs_for;
110 }
111  
112  
113  
114 35 1;
115  
116 __END__
117  
118 =pod
119  
120 =encoding UTF-8
121  
122 =head1 NAME
123  
124 Langertha::Role::RuntimeKnobs - Role for a self-hosted engine with per-request prefix-cache runtime knobs
125  
126 =head1 VERSION
127  
128 version 0.503
129  
130 =head2 prefix_cache_salt
131  
132 Optional prefix-cache salt string, emitted as C<cache_salt> on the vLLM and
133 SGLang wires. B<Cache isolation, not a performance knob:> only requests
134 sharing a salt reuse each other's KV blocks, so a random per-request salt
135 B<reduces> cache reuse. vLLM's C<cache_salt> requires vLLM v1.x; older
136 servers silently ignore unknown fields. See L<Langertha::Runtime::Knobs>.
137  
138 =head2 cache_prompt
139  
140 Optional llama.cpp C<cache_prompt> boolean: reuse the prompt cache for this
141 request. Emitted as a JSON boolean on the llama.cpp wire.
142  
143 =head2 n_cache_reuse
144  
145 Optional llama.cpp C<n_cache_reuse> integer. B<Counter-intuitive:> C<0> means
146 reuse all cached tokens; higher values B<limit> reuse. See
147 L<Langertha::Runtime::Knobs>.
148  
149 =head2 id_slot
150  
151 Optional llama.cpp C<id_slot> integer: pin this request to a specific server
152 slot (useful when the relevant KV cache lives on one slot).
153  
154 =head2 priority
155  
156 Optional SGLang C<priority> integer: request scheduling priority. Higher
157 values are served first.
158  
159 =head2 return_cached_tokens_details
160  
161 Optional SGLang C<return_cached_tokens_details> boolean: ask the server to
162 report cached prompt tokens (C<usage.prompt_tokens_details.cached_tokens> on
163 the response). Emitted as a JSON boolean on the SGLang wire.
164  
165 =head2 extra_key
166  
167 Optional SGLang C<extra_key> string: an additional cache-key component beyond
168 the prompt text, used to partition the prefix cache.
169  
170 =head2 knob_wire_format
171  
172 knob_wire_format => 'vllm'
173  
174 The per-engine enum naming which self-hosted runtime-knob dialect this engine
175 speaks — C<vllm> | C<sglang> | C<llamacpp>. Drives the value-object dispatch
176 in L</knobs_kwargs>. B<There is no shared default:> each engine must set its
177 tag explicitly (override C<_build_knob_wire_format>), because the three
178 servers accept disjoint field sets and no generic 'openai' knob dialect
179 exists. Accessing the attribute on an engine that forgot the tag croaks with
180 a message naming the role and the missing tag.
181  
182 =head2 knobs_kwargs_for
183  
184 my %kwargs = $engine->knobs_kwargs_for( prefix_cache_salt => 'tenant-a' );
185 my %kwargs = $engine->knobs_kwargs_for( %$controls );
186  
187 Returns the body kwargs to merge into a chat request for the runtime knobs,
188 serialized for L</knob_wire_format> via L<Langertha::Runtime::Knobs>. C<%args>
189 may carry any of the canonical knob names (C<prefix_cache_salt>,
190 C<cache_prompt>, C<n_cache_reuse>, C<id_slot>, C<priority>,
191 C<return_cached_tokens_details>, C<extra_key> — the same names as the
192 attributes, so the whole controls hash from chat_f can be passed wholesale);
193 keys it does not carry fall back to the engine attributes, so a per-request
194 control (chat_f, karr #46) beats the configured attribute on a per-key basis.
195 Empty list when neither a per-request value nor an attribute is set, or when
196 none of the set knobs is supported on the engine's wire.
197  
198 =head2 knobs_kwargs
199  
200 my %kwargs = $engine->knobs_kwargs;
201  
202 Returns the body kwargs to merge into a chat request for the configured
203 runtime knobs, serialized for L</knob_wire_format> via
204 L<Langertha::Runtime::Knobs>. Empty list when nothing is set, or when none of
205 the set knobs is supported on the engine's wire. Delegates to
206 L</knobs_kwargs_for> with no per-request overrides.
207  
208 =head1 SEE ALSO
209  
210 =over
211  
212 =item * L<Langertha::Runtime::Knobs> - The value object this role dispatches to
213  
214 =item * L<Langertha::Role::Capabilities> - Where C<prefix_caching> is registered
215  
216 =item * L<Langertha::Role::ReasoningEffort> - Sibling request-side reasoning control
217  
218 =back
219  
220 =head1 SUPPORT
221  
222 =head2 Issues
223  
224 Please report bugs and feature requests on GitHub at
225 L<https://github.com/Getty/langertha/issues>.
226  
227 =head2 IRC
228  
229 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
230  
231 =head1 CONTRIBUTING
232  
233 Contributions are welcome! Please fork the repository and submit a pull request.
234  
235 =head1 AUTHOR
236  
237 Torsten Raudssus <getty@cpan.org>
238  
239 =head1 COPYRIGHT AND LICENSE
240  
241 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
242  
243 This is free software; you can redistribute it and/or modify it under
244 the same terms as the Perl 5 programming language system itself.
245  
246 =cut
« blib/lib/Langertha/PromptCache.pm ↑ Summary blib/lib/Langertha/ImageGen.pm »