blib/lib/Langertha/Knarr/CLI/Cmd/Start.pm

filter: Statementstmt 77.7% 122 / 157 Branchbran 50.0% 36 / 72 Conditioncond 29.6% 8 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 1 totaltotal 62.5% 177 / 283 CC 51 SCAR 51.2
CC
51
Cov
65%
execute
215.8
_workers
4.0
_strip_quotes
2.5
CRAP 166.8
↑ Summary blib/lib/Langertha/Knarr/Handler/A2AClient.pm »
1 package Langertha::Knarr::CLI::Cmd::Start;
2 47 our $VERSION = '1.102';
3 # ABSTRACT: Start the Knarr proxy server
4 47 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 47 with 'Langertha::Knarr::CLI::Role::GlobalOptions';
6 47 ▶use MooX::Cmd;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 47 ▶use MooX::Options protect_argv => 0, usage_string => 'USAGE: knarr start [options]';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 47 ▶use Log::Any qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 47 ▶use Log::Any::Adapter;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 option port => (
13 is => 'ro',
14 format => 'i@',
15 short => 'p',
16 doc => 'Port(s) to listen on, on the -H host; repeatable, replaces the config listen: (default: the config listen:, which defaults to 127.0.0.1:8080 and 127.0.0.1:11434)',
17 15 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
18 47 );
19  
20 47 option host => (
21 is => 'ro',
22 format => 's',
23 short => 'H',
24 doc => 'Host the -p ports bind to, no effect without -p (default: 0.0.0.0)',
25 default => '0.0.0.0',
26 );
27  
28 47 option workers => (
29 is => 'ro',
30 format => 'i',
31 short => 'w',
32 doc => 'Number of worker processes; more than 1 forks that many, supervised and restarted by this process (default: the config workers:, else KNARR_WORKERS, else 1, no fork)',
33 predicate => 'has_workers',
34 );
35  
36 47 option from_env => (
37 is => 'ro',
38 doc => 'Build config from environment variables when no config file found',
39 default => 0,
40 );
41  
42 47 option trace_name => (
43 is => 'ro',
44 format => 's',
45 short => 'n',
46 doc => 'Langfuse trace name (default: knarr-proxy, or KNARR_TRACE_NAME env)',
47 predicate => 'has_trace_name',
48 );
49  
50 47 option log_file => (
51 is => 'ro',
52 format => 's',
53 doc => 'JSONL log file path (or KNARR_LOG_FILE env)',
54 predicate => 'has_log_file',
55 );
56  
57 47 option log_dir => (
58 is => 'ro',
59 format => 's',
60 doc => 'Directory for per-request JSON log files (or KNARR_LOG_DIR env)',
61 predicate => 'has_log_dir',
62 );
63  
64 9 ▶sub execute {
Subroutine execute: called CC=47 SCAR=53.7
Pod undocumented
65 9 my ($self, $args, $chain) = @_;
66  
67 9 my $verbose = $self->verbose_enabled($chain);
68 9 ▶ Log::Any::Adapter->set('Stderr', log_level => $verbose ? 'trace' : 'warning');
Branch: 1 branch
BranchTrueFalseTotal
$verbose ? : 0 9 9
69  
70 9 require Langertha::Knarr::Config;
71  
72 9 my $config_file = $self->config_file($chain);
73 9 my $config;
74  
75 9 ▶ if (-f $config_file) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $config_file) { } 8 1 9
elsif ($self->from_env) { } 1 0 1
76 8 $config = Langertha::Knarr::Config->new(file => $config_file);
77 8 my @errors = $config->validate;
78 8 ▶ if (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) 2 6 8
79 2 _err("Configuration errors:");
80 2 _err(" - $_") for @errors;
81 2 exit 1;
82 }
83 6 _log("Config: loaded from $config_file");
84 } elsif ($self->from_env) {
85 1 _log("Config: auto-detecting from environment variables");
86 1 $config = Langertha::Knarr::Config->from_env(include_test => 0);
87 } else {
88 0 print STDERR "Config file not found: $config_file\n";
89 0 print STDERR "\n";
90 0 print STDERR " knarr init > knarr.yaml Generate a config from your environment\n";
91 0 print STDERR " knarr start --from-env Auto-detect config from environment variables\n";
92 0 print STDERR "\n";
93 0 exit 1;
94 }
95  
96 7 my $workers = $self->_workers($config);
97  
98 # Inject CLI trace_name into config
99 5 ▶ if ($self->has_trace_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_trace_name) 0 5 5
100 0 ▶ $config->data->{langfuse} //= {};
Condition0%0/2
$config->data->{"langfuse"} //= {}
l!l
00
MC/DC0%0/1
$config->data->{"langfuse"} //= {}
$config->data->{"langfuse"}
Truth table0%0/2
A
A= $config->data->{"langfuse"}
Aresult
11
00
101 0 $config->data->{langfuse}{trace_name} = $self->trace_name;
102 }
103  
104 # Inject CLI logging options into config
105 5 ▶ if ($self->has_log_file || $self->has_log_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_log_file or $self->has_log_dir) 0 5 5
Condition33%1/3
$self->has_log_file or $self->has_log_dir
l!l&&r!l&&!r
005
MC/DC0%0/2
$self->has_log_file or $self->has_log_dir
$self->has_log_file $self->has_log_dir
Truth table33%1/3
A or B
A= $self->has_log_fileB= $self->has_log_dir
ABresult
1X1
011
000
106 0 ▶ $config->data->{logging} //= {};
Condition0%0/2
$config->data->{"logging"} //= {}
l!l
00
MC/DC0%0/1
$config->data->{"logging"} //= {}
$config->data->{"logging"}
Truth table0%0/2
A
A= $config->data->{"logging"}
Aresult
11
00
107 0 ▶ $config->data->{logging}{file} = $self->log_file if $self->has_log_file;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_log_file 0 0 0
108 0 ▶ $config->data->{logging}{dir} = $self->log_dir if $self->has_log_dir;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_log_dir 0 0 0
109 }
110  
111 # Build listen addresses
112 5 my $listen_addrs;
113 5 my $h = $self->host;
114 5 my @ports = @{ $self->port };
115  
116 5 ▶ if (@ports) {
Branch: 2 branches
BranchTrueFalseTotal
if (@ports) { } 5 0 5
elsif (my $cfg_listen = $config->listen) { } 0 0 0
117 5 $listen_addrs = [ map { "$h:$_" } @ports ];
118 } elsif (my $cfg_listen = $config->listen) {
119 0 $listen_addrs = $cfg_listen;
120 } else {
121 0 $listen_addrs = [ "$h:8080", "$h:11434" ];
122 }
123  
124 # Startup banner
125 5 _log("Knarr LLM Proxy starting...");
126 5 _log("");
127  
128 # Log discovered engines and models
129 5 my $models = $config->models;
130 5 my $model_count = scalar keys %$models;
131 5 ▶ if ($model_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($model_count) { } 5 0 5
132 5 _log("Engines: $model_count provider(s) configured");
133 5 _log("");
134 5 for my $name (sort keys %$models) {
135 5 my $m = $models->{$name};
136 5 my $line = " $name";
137 5 $line .= " => $m->{engine}";
138 5 ▶ $line .= " / $m->{model}" if $m->{model};
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"model"} 0 5 5
139 5 ▶ if ($m->{api_key_env}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m->{"api_key_env"}) 0 5 5
140 0 $line .= " (key from \$$m->{api_key_env})";
141 }
142 5 _log($line);
143 }
144 5 _log("");
145 } else {
146 0 _log("Engines: none (passthrough only mode)");
147 }
148  
149 5 ▶ if ($config->auto_discover) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->auto_discover) 0 5 5
150 0 _log("Auto-discover: enabled (will query provider model lists)");
151 }
152  
153 5 ▶ if ($config->default_engine) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->default_engine) 0 5 5
154 0 _log("Default engine: $config->{data}{default}{engine}");
155 }
156  
157 # Passthrough status
158 5 my $pt = $config->passthrough;
159 5 ▶ if (keys %$pt) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$pt) { } 0 5 5
160 0 my @fmts;
161 0 for my $fmt (sort keys %$pt) {
162 0 push @fmts, "$fmt -> $pt->{$fmt}";
163 }
164 0 _log("Passthrough: " . join(', ', @fmts));
165 } else {
166 5 _log("Passthrough: disabled");
167 }
168  
169 # Langfuse tracing status
170 5 ▶ my $lf_pub = $config->langfuse->{public_key} // _strip_quotes($ENV{LANGFUSE_PUBLIC_KEY});
Condition33%1/3
$config->langfuse->{"public_key"} // _strip_quotes($ENV{"LANGFUSE_PUBLIC_KEY"})
l!l&&r!l&&!r
005
MC/DC0%0/2
$config->langfuse->{"public_key"} // _strip_quotes($ENV{"LANGFUSE_PUBLIC_KEY"})
$config->langfuse->{"public_key"} _strip_quotes($ENV{"LANGFUSE_PUBLIC_KEY"})
Truth table33%1/3
A // B
A= $config->langfuse->{"public_key"}B= _strip_quotes($ENV{"LANGFUSE_PUBLIC_KEY"})
ABresult
1X1
011
000
171 5 ▶ my $lf_sec = $config->langfuse->{secret_key} // _strip_quotes($ENV{LANGFUSE_SECRET_KEY});
Condition33%1/3
$config->langfuse->{"secret_key"} // _strip_quotes($ENV{"LANGFUSE_SECRET_KEY"})
l!l&&r!l&&!r
005
MC/DC0%0/2
$config->langfuse->{"secret_key"} // _strip_quotes($ENV{"LANGFUSE_SECRET_KEY"})
$config->langfuse->{"secret_key"} _strip_quotes($ENV{"LANGFUSE_SECRET_KEY"})
Truth table33%1/3
A // B
A= $config->langfuse->{"secret_key"}B= _strip_quotes($ENV{"LANGFUSE_SECRET_KEY"})
ABresult
1X1
011
000
172 5 ▶ my $lf_url = $config->langfuse->{url} // _strip_quotes($ENV{LANGFUSE_URL}) // _strip_quotes($ENV{LANGFUSE_BASE_URL}) // 'https://cloud.langfuse.com';
Condition37%3/8
(($config->langfuse->{"url"} // _strip_quotes($ENV{"LANGFUSE_URL"})) // _strip_quotes($ENV{"LANGFUSE_BASE_URL"})) // "https://cloud.langfuse.com"
l!l
05
($config->langfuse->{"url"} // _strip_quotes($ENV{"LANGFUSE_URL"})) // _strip_quotes($ENV{"LANGFUSE_BASE_URL"})
l!l&&r!l&&!r
005
$config->langfuse->{"url"} // _strip_quotes($ENV{"LANGFUSE_URL"})
l!l&&r!l&&!r
005
MC/DC0%0/3
(($config->langfuse->{"url"} // _strip_quotes($ENV{"LANGFUSE_URL"})) // _strip_quotes($ENV{"LANGFUSE_BASE_URL"})) // "https://cloud.langfuse.com"
$config->langfuse->{"url"} _strip_quotes($ENV{"LANGFUSE_URL"}) _strip_quotes($ENV{"LANGFUSE_BASE_URL"})
Truth table25%1/4
A // B // C
A= $config->langfuse->{"url"}B= _strip_quotes($ENV{"LANGFUSE_URL"})C= _strip_quotes($ENV{"LANGFUSE_BASE_URL"})
ABCresult
1XX1
01X1
0011
0000
173 5 ▶ if ($lf_pub && $lf_sec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lf_pub and $lf_sec) { } 0 5 5
Condition33%1/3
$lf_pub and $lf_sec
!ll&&!rl&&r
500
MC/DC0%0/2
$lf_pub and $lf_sec
$lf_pub $lf_sec
Truth table33%1/3
A and B
A= $lf_pubB= $lf_sec
ABresult
0X0
100
111
174 0 _log("Langfuse: enabled -> $lf_url (" . $config->langfuse_transport . ")");
175 } else {
176 5 _log("Langfuse: disabled (set LANGFUSE_PUBLIC_KEY + LANGFUSE_SECRET_KEY to enable)");
177 }
178  
179 # Proxy auth status
180 5 ▶ if ($config->has_proxy_api_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->has_proxy_api_key) { } 0 5 5
181 0 _log("Proxy auth: enabled (KNARR_API_KEY)");
182 } else {
183 5 _log("Proxy auth: open (set KNARR_API_KEY to require authentication)");
184 }
185  
186 # Request logging status
187 5 my $log_file = $config->log_file;
188 5 my $log_dir = $config->log_dir;
189 5 ▶ if ($log_file || $log_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log_file or $log_dir) { } 0 5 5
Condition33%1/3
$log_file or $log_dir
l!l&&r!l&&!r
005
MC/DC0%0/2
$log_file or $log_dir
$log_file $log_dir
Truth table33%1/3
A or B
A= $log_fileB= $log_dir
ABresult
1X1
011
000
190 0 my @parts;
191 0 ▶ push @parts, "file: $log_file" if $log_file;
Branch: 1 branch
BranchTrueFalseTotal
if $log_file 0 0 0
192 0 ▶ push @parts, "dir: $log_dir" if $log_dir;
Branch: 1 branch
BranchTrueFalseTotal
if $log_dir 0 0 0
193 0 _log("Logging: " . join(', ', @parts));
194 } else {
195 5 _log("Logging: disabled (set KNARR_LOG_FILE or KNARR_LOG_DIR to enable)");
196 }
197  
198 5 _log("");
199  
200 # Build server
201 5 require Langertha::Knarr;
202 5 require Langertha::Knarr::Router;
203 5 require Langertha::Knarr::Handler::Router;
204 5 require IO::Async::Loop;
205  
206 5 my $loop = IO::Async::Loop->new;
207 5 my $router = Langertha::Knarr::Router->new( config => $config );
208  
209 5 my $passthrough;
210 5 ▶ if ( my $upstreams = $config->passthrough ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $upstreams = $config->passthrough) 5 0 5
211 5 ▶ if ( %$upstreams ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$upstreams) 0 5 5
212 0 require Langertha::Knarr::Handler::Passthrough;
213 0 $passthrough = Langertha::Knarr::Handler::Passthrough->new(
214 upstreams => $upstreams,
215 loop => $loop,
216 timeout => $config->upstream_timeout,
217 stall_timeout => $config->upstream_stall_timeout,
218 );
219 }
220 }
221  
222 5 ▶ my $handler = Langertha::Knarr::Handler::Router->new(
Branch: 1 branch
BranchTrueFalseTotal
$passthrough ? : 0 5 5
223 router => $router,
224 ( $passthrough ? ( passthrough => $passthrough ) : () ),
225 );
226  
227 5 require Langertha::Knarr::Tracing;
228 5 my $tracing = Langertha::Knarr::Tracing->new( config => $config );
229 5 ▶ if ( $tracing->_enabled ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tracing->_enabled) 0 5 5
230 0 require Langertha::Knarr::Handler::Tracing;
231 0 $handler = Langertha::Knarr::Handler::Tracing->new(
232 wrapped => $handler,
233 tracing => $tracing,
234 );
235 }
236  
237 5 require Langertha::Knarr::RequestLog;
238 5 my $rlog = Langertha::Knarr::RequestLog->new( config => $config );
239 5 ▶ if ( $rlog->_enabled ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rlog->_enabled) 0 5 5
240 0 require Langertha::Knarr::Handler::RequestLog;
241 0 $handler = Langertha::Knarr::Handler::RequestLog->new(
242 wrapped => $handler,
243 request_log => $rlog,
244 );
245 }
246  
247 5 ▶ my $knarr = Langertha::Knarr->new(
Branch: 5 branches
BranchTrueFalseTotal
$passthrough ? : 0 5 5
$tracing->_enabled ? : 0 5 5
$config->has_proxy_api_key ? : 0 5 5
defined $config->public_url ? : 0 5 5
defined $config->ollama_compat_version ? : 0 5 5
248 handler => $handler,
249 loop => $loop,
250 listen => $listen_addrs,
251 router => $router,
252 ( $passthrough ? ( raw_passthrough => $passthrough ) : () ),
253 ( $tracing->_enabled ? ( tracing => $tracing ) : () ),
254 ( $config->has_proxy_api_key ? ( auth_token => $config->proxy_api_key ) : () ),
255 ( defined $config->public_url ? ( public_url => $config->public_url ) : () ),
256 ( defined $config->ollama_compat_version
257 ? ( ollama_compat_version => $config->ollama_compat_version ) : () ),
258 protocol_args => $config->protocol_args,
259 workers => $workers,
260 );
261  
262 5 _log("Starting server:");
263 5 for my $addr (@$listen_addrs) {
264 5 _log(" http://$addr");
265 }
266 5 ▶ _log("Workers: $workers") if $workers > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $workers > 1 5 0 5
267 5 _log("");
268  
269 5 $knarr->run;
270 }
271  
272 # The worker count: -w, else the config's workers: / KNARR_WORKERS (k51).
273 # A bad value stops start here, before the banner and before any bind.
274 7 ▶sub _workers {
Subroutine _workers: called CC=4 SCAR=13.9
275 7 my ($self, $config) = @_;
276 7 ▶ if ( $self->has_workers ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_workers) 4 3 7
277 4 ▶ return $self->workers if $self->workers >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->workers >= 1 3 1 4
278 1 _err("-w/--workers must be 1 or more, got " . $self->workers);
279 1 exit 1;
280 }
281 3 my $workers = eval { $config->workers };
282 3 ▶ return $workers if defined $workers;
Branch: 1 branch
BranchTrueFalseTotal
if defined $workers 2 1 3
283 1 ( my $err = $@ ) =~ s/ at \S+ line \d+\.?\n?\z//;
284 1 _err($err);
285 1 exit 1;
286 }
287  
288 82 ▶sub _log { print STDERR "[knarr] $_[0]\n" }
Subroutine _log: called CC=1 SCAR=0.0
289 6 ▶sub _err { print STDERR "[knarr] ERROR: $_[0]\n" }
Subroutine _err: called CC=1 SCAR=0.0
290  
291 20 ▶sub _strip_quotes {
Subroutine _strip_quotes: called CC=2 SCAR=9.2
292 20 my $v = shift;
293 20 ▶ return $v unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 20 0 20
294 0 $v =~ s/^["']|["']$//g;
295 0 return $v;
296 }
297  
298 47 1;
299  
300 __END__
301  
302 =pod
303  
304 =encoding UTF-8
305  
306 =head1 NAME
307  
308 Langertha::Knarr::CLI::Cmd::Start - Start the Knarr proxy server
309  
310 =head1 VERSION
311  
312 version 1.102
313  
314 =head1 DESCRIPTION
315  
316 Implements the C<knarr start> command. Loads the config file, validates it,
317 and starts the server.
318  
319 With C<--from-env>, the config is built automatically from environment
320 variables when no config file is found — this is how the Docker image starts.
321  
322 The listen addresses come from C<-p> when given (each port on C<-H>,
323 default C<0.0.0.0>), otherwise from the config's C<listen:>, which defaults
324 to C<127.0.0.1:8080> and C<127.0.0.1:11434>. C<-H> alone changes nothing.
325  
326 C<-w>/C<--workers> I<N> (else the config's C<workers:> or C<KNARR_WORKERS>,
327 default C<1>; a value below C<1> stops C<start> before the banner) serves
328 from N processes: Knarr binds
329 the listen addresses, runs auto-discovery and the capability probe once,
330 then forks N workers that accept on the same sockets, and supervises them
331 -- a worker that exits is restarted, C<SIGTERM>/C<SIGINT> stops them all.
332 Sessions (a Raider conversation) live in one worker and are not routed
333 back to it. See L<Langertha::Knarr/workers> and L<Langertha::Knarr/run>.
334  
335 See L<knarr> for the full option reference and L<Langertha::Knarr::Config>
336 for the configuration file format.
337  
338 =head1 SEE ALSO
339  
340 =over
341  
342 =item * L<knarr> — CLI synopsis and option reference
343  
344 =item * L<Langertha::Knarr> — Full documentation
345  
346 =back
347  
348 =head1 SUPPORT
349  
350 =head2 Issues
351  
352 Please report bugs and feature requests on GitHub at
353 L<https://github.com/Getty/langertha-knarr/issues>.
354  
355 =head2 IRC
356  
357 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
358  
359 =head1 CONTRIBUTING
360  
361 Contributions are welcome! Please fork the repository and submit a pull request.
362  
363 =head1 AUTHOR
364  
365 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
366  
367 =head1 COPYRIGHT AND LICENSE
368  
369 This software is copyright (c) 2026 by Torsten Raudssus.
370  
371 This is free software; you can redistribute it and/or modify it under
372 the same terms as the Perl 5 programming language system itself.
373  
374 =cut
↑ Summary blib/lib/Langertha/Knarr/Handler/A2AClient.pm »