blib/lib/Langertha/Runtime/Knobs.pm

filter: Statementstmt 100% 32 / 32 Branchbran 69.2% 18 / 26 Conditioncond 63.1% 12 / 19 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 7 / 7 Podpod 100% 4 / 4 totaltotal 77.3% 75 / 97 CC 21 SCAR 21.6
CC
21
Cov
81%
to_sglang
13.7
to_llamacpp
9.2
to
4.2
CRAP 8.7
« blib/lib/Langertha/Response.pm ↑ Summary blib/lib/Langertha/Content/Image.pm »
1 package Langertha::Runtime::Knobs;
2 # ABSTRACT: Immutable self-hosted runtime knobs with per-server conversion
3 35 our $VERSION = '0.503';
4 35 ▶use Moose;
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 JSON::MaybeXS qw( JSON );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 35 has prefix_cache_salt => (
10 is => 'ro',
11 isa => 'Str',
12 predicate => 'has_prefix_cache_salt',
13 );
14  
15  
16 35 has cache_prompt => (
17 is => 'ro',
18 isa => 'Bool',
19 predicate => 'has_cache_prompt',
20 );
21  
22  
23 35 has n_cache_reuse => (
24 is => 'ro',
25 isa => 'Int',
26 predicate => 'has_n_cache_reuse',
27 );
28  
29  
30 35 has id_slot => (
31 is => 'ro',
32 isa => 'Int',
33 predicate => 'has_id_slot',
34 );
35  
36  
37 35 has priority => (
38 is => 'ro',
39 isa => 'Int',
40 predicate => 'has_priority',
41 );
42  
43  
44 35 has return_cached_tokens_details => (
45 is => 'ro',
46 isa => 'Bool',
47 predicate => 'has_return_cached_tokens_details',
48 );
49  
50  
51 35 has extra_key => (
52 is => 'ro',
53 isa => 'Str',
54 predicate => 'has_extra_key',
55 );
56  
57  
58 6 ▶sub to_vllm {
Subroutine to_vllm: called CC=2 SCAR=6.9
Pod documented
59 6 my ( $self ) = @_;
60 6 ▶ return () unless $self->has_prefix_cache_salt;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_prefix_cache_salt 2 4 6
61 4 return ( cache_salt => $self->prefix_cache_salt );
62 }
63  
64  
65 4 ▶sub to_sglang {
Subroutine to_sglang: called CC=10 SCAR=26.2
Pod documented
66 4 my ( $self ) = @_;
67 4 ▶ return () unless $self->has_prefix_cache_salt
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_prefix_cache_salt or $self->has_extra_key or $self->has_priority or $self->has_return_cached_tokens_details 2 2 4
Condition66%6/9
$self->has_prefix_cache_salt or $self->has_extra_key or $self->has_priority or $self->has_return_cached_tokens_details
l!l&&r!l&&!r
202
$self->has_prefix_cache_salt or $self->has_extra_key or $self->has_priority
l!l&&r!l&&!r
202
$self->has_prefix_cache_salt or $self->has_extra_key
l!l&&r!l&&!r
202
MC/DC25%1/4
$self->has_prefix_cache_salt or $self->has_extra_key or $self->has_priority or $self->has_return_cached_tokens_details
$self->has_prefix_cache_salt $self->has_extra_key $self->has_priority $self->has_return_cached_tokens_details
Truth table40%2/5
A or B or C or D
A= $self->has_prefix_cache_saltB= $self->has_extra_keyC= $self->has_priorityD= $self->has_return_cached_tokens_details
ABCDresult
1XXX1
01XX1
001X1
00011
00000
68 || $self->has_extra_key
69 || $self->has_priority
70 || $self->has_return_cached_tokens_details;
71 return (
72 2 ▶ ( $self->has_prefix_cache_salt ? ( cache_salt => $self->prefix_cache_salt ) : () ),
Branch: 5 branches
BranchTrueFalseTotal
$self->has_prefix_cache_salt ? : 2 0 2
$self->has_extra_key ? : 2 0 2
$self->has_priority ? : 2 0 2
$self->has_return_cached_tokens_details ? : 2 0 2
$self->return_cached_tokens_details ? : 2 0 2
73 ( $self->has_extra_key ? ( extra_key => $self->extra_key ) : () ),
74 ( $self->has_priority ? ( priority => $self->priority ) : () ),
75 ( $self->has_return_cached_tokens_details
76 ? ( return_cached_tokens_details => $self->return_cached_tokens_details ? JSON->true : JSON->false )
77 : () ),
78 );
79 }
80  
81  
82 4 ▶sub to_llamacpp {
Subroutine to_llamacpp: called CC=8 SCAR=22.2
Pod documented
83 4 my ( $self ) = @_;
84 4 ▶ return () unless $self->has_cache_prompt
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_cache_prompt or $self->has_n_cache_reuse or $self->has_id_slot 2 2 4
Condition66%4/6
$self->has_cache_prompt or $self->has_n_cache_reuse or $self->has_id_slot
l!l&&r!l&&!r
202
$self->has_cache_prompt or $self->has_n_cache_reuse
l!l&&r!l&&!r
202
MC/DC33%1/3
$self->has_cache_prompt or $self->has_n_cache_reuse or $self->has_id_slot
$self->has_cache_prompt $self->has_n_cache_reuse $self->has_id_slot
Truth table50%2/4
A or B or C
A= $self->has_cache_promptB= $self->has_n_cache_reuseC= $self->has_id_slot
ABCresult
1XX1
01X1
0011
0000
85 || $self->has_n_cache_reuse
86 || $self->has_id_slot;
87 return (
88 2 ▶ ( $self->has_cache_prompt ? ( cache_prompt => $self->cache_prompt ? JSON->true : JSON->false ) : () ),
Branch: 4 branches
BranchTrueFalseTotal
$self->has_cache_prompt ? : 2 0 2
$self->cache_prompt ? : 1 1 2
$self->has_n_cache_reuse ? : 2 0 2
$self->has_id_slot ? : 2 0 2
89 ( $self->has_n_cache_reuse ? ( n_cache_reuse => $self->n_cache_reuse ) : () ),
90 ( $self->has_id_slot ? ( id_slot => $self->id_slot ) : () ),
91 );
92 }
93  
94  
95 # Maps a knob_wire_format tag to the per-format serializer method.
96 35 my %TO_METHOD = (
97 vllm => 'to_vllm',
98 sglang => 'to_sglang',
99 llamacpp => 'to_llamacpp',
100 );
101  
102 15 ▶sub to {
Subroutine to: called CC=4 SCAR=14.3
Pod documented
103 15 my ( $self, $fmt ) = @_;
104 15 ▶ my $method = $TO_METHOD{ $fmt // '' }
Branch: 1 branch
BranchTrueFalseTotal
my $method = $TO_METHOD{$fmt // ""} or croak(q[Langertha::Runtime::Knobs: unknown knob wire format '] . ($fmt // "") . q[']) 14 1 15
Condition50%2/4
$fmt // ""
l!l
150
$fmt // ""
l!l
10
MC/DC0%0/1
$fmt // ""
$fmt
MC/DC0%0/1
$fmt // ""
$fmt
Truth table50%1/2
A
A= $fmt
Aresult
11
00
Truth table50%1/2
A
A= $fmt
Aresult
11
00
105 or croak "Langertha::Runtime::Knobs: unknown knob wire format '" . ( $fmt // '' ) . "'";
106 14 return $self->$method;
107 }
108  
109  
110 35 __PACKAGE__->meta->make_immutable;
111  
112  
113 35 1;
114  
115 __END__
116  
117 =pod
118  
119 =encoding UTF-8
120  
121 =head1 NAME
122  
123 Langertha::Runtime::Knobs - Immutable self-hosted runtime knobs with per-server conversion
124  
125 =head1 VERSION
126  
127 version 0.503
128  
129 =head1 SYNOPSIS
130  
131 my $k = Langertha::Runtime::Knobs->new(
132 prefix_cache_salt => 'tenant-a',
133 );
134 my %kwargs = $k->to('vllm');
135 # ( cache_salt => 'tenant-a' )
136  
137 my $s = Langertha::Runtime::Knobs->new(
138 prefix_cache_salt => 'tenant-a',
139 return_cached_tokens_details => 1,
140 );
141 my %kw = $s->to('sglang');
142 # ( cache_salt => 'tenant-a', return_cached_tokens_details => true )
143  
144 =head1 DESCRIPTION
145  
146 Canonical value object for the request-side runtime knobs of the self-hosted
147 OpenAI-compatible engines (vLLM, SGLang, llama.cpp), dispatched by an engine's
148 C<knob_wire_format>. Mirrors L<Langertha::Reasoning> / L<Langertha::PromptCache>:
149 the per-provider placement of the fields lives in this one reviewable place
150 rather than scattered across engines (ADR 0001, ADR 0004 — no raw
151 C<extra_body> side-channel; these knobs serialize as top-level request-body
152 fields exactly like C<reasoning_kwargs_for> / C<prompt_cache_kwargs_for> do).
153  
154 The only genuinely per-request knobs on these three servers are prefix-cache
155 isolation/reuse controls. Each C<to_*> serializer emits only the fields that
156 server's wire accepts and returns the body kwargs to merge into the request.
157  
158 B<What is deliberately NOT modeled here:>
159  
160 =over
161  
162 =item * B<Speculative decoding> — a server-launch concern on all three engines
163 (C<--speculative-config> on vLLM, C<--speculative-*> on SGLang,
164 C<--spec-draft-*> on llama.cpp), restart-only, never a per-request knob.
165  
166 =item * B<Continuous batching> — an internal scheduler flag, not a request knob.
167  
168 =back
169  
170 =head2 prefix_cache_salt
171  
172 Optional prefix-cache salt string. Emitted as C<cache_salt> on the vLLM and
173 SGLang wires.
174  
175 B<This is a cache-ISOLATION feature, not a performance knob.> It exists for
176 multi-tenant privacy and timing-attack mitigation: only requests sharing a
177 salt reuse each other's KV blocks, so a per-tenant salt keeps tenants from
178 observing each other's cache behavior. Setting a random salt per request
179 B<reduces> cache reuse — the opposite of a cache-warming hint. Requests that
180 want to share cached prefix blocks must carry the same salt.
181  
182 B<Version gating:> vLLM's C<cache_salt> requires vLLM v1.x. Older servers
183 silently ignore unknown fields, so setting it against an old server is a
184 no-op, not an error.
185  
186 =head2 cache_prompt
187  
188 Optional llama.cpp C<cache_prompt> boolean: whether to reuse the prompt cache
189 for this request. Emitted as a JSON boolean on the llama.cpp wire.
190  
191 =head2 n_cache_reuse
192  
193 Optional llama.cpp C<n_cache_reuse> integer. B<Counter-intuitive:> C<0> means
194 reuse all cached tokens; higher values B<limit> reuse (the server reuses at
195 most that many cached tokens). The default is C<0> (reuse everything).
196  
197 =head2 id_slot
198  
199 Optional llama.cpp C<id_slot> integer: pin this request to a specific server
200 slot. Useful for stateful / multi-session setups where a request must land on
201 the slot holding the relevant KV cache.
202  
203 =head2 priority
204  
205 Optional SGLang C<priority> integer: request scheduling priority. Higher
206 values are served first.
207  
208 =head2 return_cached_tokens_details
209  
210 Optional SGLang C<return_cached_tokens_details> boolean: ask the server to
211 report how many prompt tokens were served from cache (surfaced on the
212 response as C<usage.prompt_tokens_details.cached_tokens>). Emitted as a JSON
213 boolean on the SGLang wire.
214  
215 =head2 extra_key
216  
217 Optional SGLang C<extra_key> string: an additional cache-key component, used
218 to partition the prefix cache beyond the prompt text itself (e.g. per-tenant
219 or per-request-type isolation).
220  
221 =head2 to_vllm
222  
223 Serializes to the vLLM wire. Emits exactly one field: C<cache_salt> (from
224 L</prefix_cache_salt>). Every other knob is clamped away — vLLM accepts no
225 other per-request runtime knob. Empty list when no salt is set.
226  
227 =head2 to_sglang
228  
229 Serializes to the SGLang wire: C<cache_salt> (from L</prefix_cache_salt>),
230 C<extra_key>, C<priority>, and C<return_cached_tokens_details> (a JSON
231 boolean). Knobs SGLang does not accept (llama.cpp's C<cache_prompt> /
232 C<n_cache_reuse> / C<id_slot>) are clamped away. Empty list when none of the
233 four fields is set.
234  
235 =head2 to_llamacpp
236  
237 Serializes to the llama.cpp wire: C<cache_prompt> (a JSON boolean),
238 C<n_cache_reuse>, and C<id_slot>. Knobs llama.cpp does not accept (vLLM /
239 SGLang's C<cache_salt>, SGLang's C<extra_key> / C<priority> /
240 C<return_cached_tokens_details>) are clamped away. Empty list when none of the
241 three fields is set.
242  
243 =head2 to
244  
245 my %kwargs = $k->to($knob_wire_format);
246  
247 Dispatch to the per-format serializer. Returns the body kwargs to merge into
248 the request (an empty list when nothing applies to that wire). Croaks on an
249 unknown format tag.
250  
251 =head1 SEE ALSO
252  
253 =over
254  
255 =item * L<Langertha::Role::RuntimeKnobs> - The composed role exposing the knob attributes
256  
257 =item * L<Langertha::Reasoning> - Sibling value object for the reasoning-effort knob
258  
259 =item * L<Langertha::PromptCache> - Sibling value object for the prompt-caching knob
260  
261 =back
262  
263 =head1 SUPPORT
264  
265 =head2 Issues
266  
267 Please report bugs and feature requests on GitHub at
268 L<https://github.com/Getty/langertha/issues>.
269  
270 =head2 IRC
271  
272 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
273  
274 =head1 CONTRIBUTING
275  
276 Contributions are welcome! Please fork the repository and submit a pull request.
277  
278 =head1 AUTHOR
279  
280 Torsten Raudssus <getty@cpan.org>
281  
282 =head1 COPYRIGHT AND LICENSE
283  
284 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
285  
286 This is free software; you can redistribute it and/or modify it under
287 the same terms as the Perl 5 programming language system itself.
288  
289 =cut
« blib/lib/Langertha/Response.pm ↑ Summary blib/lib/Langertha/Content/Image.pm »