blib/lib/Langertha/Knarr/Config.pm

filter: Statementstmt 94.6% 407 / 430 Branchbran 84.0% 205 / 244 Conditioncond 67.1% 172 / 256 MC/DCmcdc 52.1% 72 / 138 Subroutinesub 96.2% 51 / 53 Podpod 92.8% 13 / 14 totaltotal 81.0% 920 / 1135 CC 236 SCAR 32.0
CC
236
Cov
84%
_token_auth_errors
74.8
_build_probe_capabilities
42.0
validate
28.4
CRAP 24.4
« blib/lib/Langertha/Knarr/Handler/ACPClient.pm ↑ Summary blib/lib/Langertha/Knarr/Handler/Engine.pm »
1 package Langertha::Knarr::Config;
2 46 our $VERSION = '1.103';
3 # ABSTRACT: YAML configuration loader and validator
4 46 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 46 ▶use YAML::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 46 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 46 ▶use JSON::MaybeXS qw( is_bool );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 46 ▶use Log::Any qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 46 ▶use Langertha ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 46 has file => (
13 is => 'ro',
14 predicate => 'has_file',
15 );
16  
17  
18 46 has data => (
19 is => 'lazy',
20 builder => '_build_data',
21 );
22  
23  
24 42 ▶sub _build_data {
Subroutine _build_data: called CC=5 SCAR=16.1
25 42 my ($self) = @_;
26 42 ▶ return {} unless $self->has_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_file 3 39 42
27 39 my $file = $self->file;
28 39 ▶ croak "Config file not found: $file" unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 39 39
29 39 my $ypp = YAML::PP->new;
30 39 my $data = eval { $ypp->load_file($file) };
31 39 ▶ croak "Config file $file is not valid YAML: ".$self->_error_text($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 38 39
32 38 ▶ croak "Config file $file must be a mapping of key: value pairs"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 2 36 38
33 unless ref $data eq 'HASH';
34 36 _interpolate_env($data);
35 36 $log->debugf("Loaded config from %s", $file);
36 36 return $data;
37 }
38  
39 # Recursively interpolate ${ENV_VAR} in string values
40 114 ▶sub _interpolate_env {
Subroutine _interpolate_env: called CC=11 SCAR=26.3
41 114 my ($ref) = @_;
42 114 ▶ if (ref $ref eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ref eq 'HASH') { } 108 6 114
elsif (ref $ref eq 'ARRAY') { } 6 0 6
43 108 for my $key (keys %$ref) {
44 184 ▶ if (ref $ref->{$key}) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ref->{$key}) { } 78 106 184
elsif (defined $ref->{$key}) { } 106 0 106
45 78 _interpolate_env($ref->{$key});
46 } elsif (defined $ref->{$key}) {
47 106 ▶ $ref->{$key} =~ s/\$\{(\w+)\}/$ENV{$1} \/\/ ''/ge;
Condition100%2/2
$ENV{$1} // ""
l!l
31
MC/DC100%1/1
$ENV{$1} // ""
$ENV{$1}
Truth table100%2/2
A
A= $ENV{$1}
Aresult
11
00
48 }
49 }
50 } elsif (ref $ref eq 'ARRAY') {
51 6 for my $i (0..$#$ref) {
52 8 ▶ if (ref $ref->[$i]) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ref->[$i]) { } 0 8 8
elsif (defined $ref->[$i]) { } 8 0 8
53 0 _interpolate_env($ref->[$i]);
54 } elsif (defined $ref->[$i]) {
55 0 ▶ $ref->[$i] =~ s/\$\{(\w+)\}/$ENV{$1} \/\/ ''/ge;
Condition0%0/2
$ENV{$1} // ""
l!l
00
MC/DC0%0/1
$ENV{$1} // ""
$ENV{$1}
Truth table0%0/2
A
A= $ENV{$1}
Aresult
11
00
56 }
57 }
58 }
59 }
60  
61 # Engine catalog: which Langertha engines are worth auto-detecting from the
62 # environment, and under which variable names, in priority order.
63 # First match wins per engine: LANGERTHA_ > bare vendor name > TEST_.
64 #
65 # Only hosted, API-key authenticated engines belong here. Local engines
66 # (Ollama, LMStudio, vLLM, SGLang, LlamaCpp, Whisper) are reached by URL and
67 # have no meaningful key to detect, and protocol variants of an engine
68 # (MiniMaxAnthropic, MoonshotAnthropic, AKIOpenAI, OpenAIResponses) share
69 # their vendor's key — listing them would emit two model entries per key.
70 46 my @ENGINE_DEFS = (
71 { engine => 'OpenAI', vars => [qw( LANGERTHA_OPENAI_API_KEY OPENAI_API_KEY TEST_LANGERTHA_OPENAI_API_KEY )] },
72 { engine => 'Anthropic', vars => [qw( LANGERTHA_ANTHROPIC_API_KEY ANTHROPIC_API_KEY TEST_LANGERTHA_ANTHROPIC_API_KEY )] },
73 { engine => 'Groq', vars => [qw( LANGERTHA_GROQ_API_KEY GROQ_API_KEY TEST_LANGERTHA_GROQ_API_KEY )] },
74 { engine => 'Mistral', vars => [qw( LANGERTHA_MISTRAL_API_KEY MISTRAL_API_KEY TEST_LANGERTHA_MISTRAL_API_KEY )] },
75 { engine => 'DeepSeek', vars => [qw( LANGERTHA_DEEPSEEK_API_KEY DEEPSEEK_API_KEY TEST_LANGERTHA_DEEPSEEK_API_KEY )] },
76 { engine => 'MiniMax', vars => [qw( LANGERTHA_MINIMAX_API_KEY MINIMAX_API_KEY TEST_LANGERTHA_MINIMAX_API_KEY )] },
77 { engine => 'Cerebras', vars => [qw( LANGERTHA_CEREBRAS_API_KEY CEREBRAS_API_KEY TEST_LANGERTHA_CEREBRAS_API_KEY )] },
78 { engine => 'OpenRouter', vars => [qw( LANGERTHA_OPENROUTER_API_KEY OPENROUTER_API_KEY TEST_LANGERTHA_OPENROUTER_API_KEY )] },
79 { engine => 'Perplexity', vars => [qw( LANGERTHA_PERPLEXITY_API_KEY PERPLEXITY_API_KEY TEST_LANGERTHA_PERPLEXITY_API_KEY )] },
80 { engine => 'Replicate', vars => [qw( LANGERTHA_REPLICATE_API_KEY REPLICATE_API_TOKEN TEST_LANGERTHA_REPLICATE_API_KEY )] },
81 { engine => 'HuggingFace', vars => [qw( LANGERTHA_HUGGINGFACE_API_KEY HUGGINGFACE_API_KEY TEST_LANGERTHA_HUGGINGFACE_API_KEY )] },
82 { engine => 'Gemini', vars => [qw( LANGERTHA_GEMINI_API_KEY GEMINI_API_KEY TEST_LANGERTHA_GEMINI_API_KEY )] },
83 { engine => 'XAI', vars => [qw( LANGERTHA_XAI_API_KEY XAI_API_KEY TEST_LANGERTHA_XAI_API_KEY )] },
84 { engine => 'Moonshot', vars => [qw( LANGERTHA_MOONSHOT_API_KEY MOONSHOT_API_KEY TEST_LANGERTHA_MOONSHOT_API_KEY )] },
85 { engine => 'NousResearch', vars => [qw( LANGERTHA_NOUSRESEARCH_API_KEY NOUSRESEARCH_API_KEY TEST_LANGERTHA_NOUSRESEARCH_API_KEY )] },
86 { engine => 'AKI', vars => [qw( LANGERTHA_AKI_API_KEY AKI_API_KEY TEST_LANGERTHA_AKI_API_KEY )] },
87 { engine => 'Scaleway', vars => [qw( LANGERTHA_SCALEWAY_API_KEY SCALEWAY_API_KEY TEST_LANGERTHA_SCALEWAY_API_KEY )] },
88 { engine => 'TSystems', vars => [qw( LANGERTHA_TSYSTEMS_API_KEY TSYSTEMS_API_KEY TEST_LANGERTHA_TSYSTEMS_API_KEY )] },
89 # No bare HETZNER_API_KEY: that name is in wide use for the Hetzner Cloud
90 # infrastructure API and would false-positive into an unusable model entry.
91 { engine => 'Hetzner', vars => [qw( LANGERTHA_HETZNER_API_KEY TEST_LANGERTHA_HETZNER_API_KEY )] },
92 );
93  
94 # Last resort for engines whose Langertha class deliberately has no usable
95 # default_model (it croaks, demanding an explicit model). Everything else is
96 # read off the engine class — see default_model_for. An engine only belongs
97 # here while its class refuses to name a default; t/10-config.t asserts that.
98 46 my %DEFAULT_MODEL_FALLBACK = (
99 Groq => 'llama-3.3-70b-versatile',
100 OpenRouter => 'openai/gpt-4o-mini',
101 );
102  
103 46 my %DEFAULT_MODEL_CACHE;
104  
105  
106 35 ▶sub engine_catalog {
Subroutine engine_catalog: called CC=1 SCAR=0.0
Pod documented
107 35 return [ map { { engine => $_->{engine}, vars => [@{$_->{vars}}] } } @ENGINE_DEFS ];
108 }
109  
110  
111 74 ▶sub default_model_for {
Subroutine default_model_for: called CC=10 SCAR=23.0
Pod documented
112 74 my ($class, $engine) = @_;
113 74 ▶ return undef unless defined $engine && length $engine;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $engine and length $engine 2 72 74
Condition100%3/3
defined $engine and length $engine
!ll&&!rl&&r
1172
MC/DC100%2/2
defined $engine and length $engine
defined $engine length $engine
Truth table100%3/3
A and B
A= defined $engineB= length $engine
ABresult
0X0
100
111
114 72 ▶ return $DEFAULT_MODEL_CACHE{$engine} if exists $DEFAULT_MODEL_CACHE{$engine};
Branch: 1 branch
BranchTrueFalseTotal
if exists $DEFAULT_MODEL_CACHE{$engine} 49 23 72
115  
116 23 my $model;
117 23 my $engine_class = eval { Langertha->resolve_engine_class($engine) };
118 23 ▶ if (!defined $engine_class) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $engine_class) { } 1 22 23
elsif ($engine_class->can('default_model')) { } 22 0 22
119 1 $log->debugf("No Langertha engine class for %s: %s", $engine, $@);
120 } elsif ($engine_class->can('default_model')) {
121 # Engines without a sensible default croak here instead of returning one.
122 22 $model = eval { $engine_class->default_model };
123 22 ▶ $log->debugf("Engine %s names no default model: %s", $engine, $@)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $model 4 18 22
124 unless defined $model;
125 }
126  
127 23 ▶ undef $model unless defined $model && length $model;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $model and length $model 5 18 23
Condition66%2/3
defined $model and length $model
!ll&&!rl&&r
5018
MC/DC50%1/2
defined $model and length $model
defined $model length $model
Truth table66%2/3
A and B
A= defined $modelB= length $model
ABresult
0X0
100
111
128 23 ▶ $model = $DEFAULT_MODEL_FALLBACK{$engine} unless defined $model;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $model 5 18 23
129  
130 23 return $DEFAULT_MODEL_CACHE{$engine} = $model;
131 }
132  
133  
134 # Build config purely from environment variables (zero-config Docker mode)
135 8 ▶sub from_env {
Subroutine from_env: called CC=3 SCAR=11.0
Pod documented
136 8 my ($class, %opts) = @_;
137 8 my $found = $class->scan_env(%opts);
138  
139 8 my %models;
140 8 for my $engine (keys %$found) {
141 26 my $name = lc($engine);
142 $models{$name} = {
143 engine => $engine,
144 model => $class->default_model_for($engine),
145 api_key_env => $found->{$engine}{api_key_env},
146 26 };
147 }
148  
149 8 my %data = (
150 models => \%models,
151 auto_discover => 1,
152 passthrough => 1,
153 );
154  
155 # Set default engine if OpenAI found, reading its key from the variable
156 # that was found: the engine by itself reads only LANGERTHA_OPENAI_API_KEY
157 # (k45)
158 8 ▶ if ($found->{OpenAI}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found->{"OpenAI"}) 6 2 8
159 6 $data{default} = { engine => 'OpenAI', api_key_env => $found->{OpenAI}{api_key_env} };
160 }
161  
162 8 return $class->new(data => \%data);
163 }
164  
165 46 has listen => (
166 is => 'lazy',
167 builder => '_build_listen',
168 );
169  
170  
171 87 ▶sub _build_listen {
Subroutine _build_listen: called CC=4 SCAR=13.9
172 87 my ($self) = @_;
173 87 my $raw = $self->data->{listen};
174 87 ▶ return ['127.0.0.1:8080', '127.0.0.1:11434'] unless defined $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw 73 14 87
175 14 ▶ my $list = ref $raw eq 'ARRAY' ? $raw : [$raw];
Branch: 1 branch
BranchTrueFalseTotal
ref $raw eq 'ARRAY' ? : 10 4 14
176 # A mapping would be bound as "HASH(0x...)" (k67); validate reports it
177 croak "listen must be a host:port string or a list of them"
178 14 ▶ if grep { ref } @$list;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_;} @$list 6 8 14
179 8 return $list;
180 }
181  
182 46 has models => (
183 is => 'lazy',
184 builder => '_build_models',
185 );
186  
187  
188 116 ▶sub _build_models {
Subroutine _build_models: called CC=3 SCAR=11.0
189 116 my ($self) = @_;
190 116 ▶ my $models = $self->_section('models') // {};
Condition100%2/2
$self->_section("models") // {}
l!l
7242
MC/DC100%1/1
$self->_section("models") // {}
$self->_section("models")
Truth table100%2/2
A
A= $self->_section("models")
Aresult
11
00
191 $self->_warn_unused_context_size( "Model '$_'", $models->{$_} )
192 114 for sort keys %$models;
193 114 return $models;
194 }
195  
196 # A context_size only reaches engines that compose Role::ContextSize (k30);
197 # say so once, at load, instead of dropping the operator's intent silently.
198 # An engine class that does not load is left for the router to report.
199 263 ▶sub _warn_unused_context_size {
Subroutine _warn_unused_context_size: called CC=6 SCAR=18.0
200 263 my ($self, $label, $def) = @_;
201 return unless ref $def eq 'HASH'
202 263 ▶ && defined $def->{context_size} && $def->{engine};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $def eq "HASH" and defined $def->{"context_size"} and $def->{"engine"} 257 6 263
Condition83%5/6
ref $def eq "HASH" and defined $def->{"context_size"} and $def->{"engine"}
!ll&&!rl&&r
25706
ref $def eq "HASH" and defined $def->{"context_size"}
!ll&&!rl&&r
691886
MC/DC66%2/3
ref $def eq "HASH" and defined $def->{"context_size"} and $def->{"engine"}
ref $def eq "HASH" defined $def->{"context_size"} $def->{"engine"}
Truth table75%3/4
A and B and C
A= ref $def eq "HASH"B= defined $def->{"context_size"}C= $def->{"engine"}
ABCresult
0XX0
10X0
1100
1111
203 6 my $class = eval { Langertha->resolve_engine_class( $def->{engine} ) };
204 6 ▶ return if !defined $class || $class->can('context_size');
Branch: 1 branch
BranchTrueFalseTotal
if not defined $class or $class->can("context_size") 4 2 6
Condition66%2/3
not defined $class or $class->can("context_size")
l!l&&r!l&&!r
042
MC/DC50%1/2
not defined $class or $class->can("context_size")
not defined $class $class->can("context_size")
Truth table66%2/3
not(A) or B
A= not defined $classB= $class->can("context_size")
ABresult
1X1
011
000
205 $log->warnf( "%s: engine %s does not take context_size, ignoring it",
206 2 $label, $def->{engine} );
207 }
208  
209 46 has default_engine => (
210 is => 'lazy',
211 builder => '_build_default_engine',
212 );
213  
214  
215 82 ▶sub _build_default_engine {
Subroutine _build_default_engine: called CC=1 SCAR=0.0
216 82 my ($self) = @_;
217 82 my $default = $self->_section('default');
218 81 $self->_warn_unused_context_size( 'Default', $default );
219 81 return $default;
220 }
221  
222 46 has log_file => (
223 is => 'lazy',
224 builder => '_build_log_file',
225 );
226  
227  
228 9 ▶sub _build_log_file {
Subroutine _build_log_file: called CC=4 SCAR=14.3
229 9 my ($self) = @_;
230 9 ▶ return ( $self->_section('logging') // {} )->{file} // _strip_quotes($ENV{KNARR_LOG_FILE}) // undef;
Condition71%5/7
(+($self->_section("logging") // {})->{"file"} // _strip_quotes($ENV{"KNARR_LOG_FILE"})) // undef
l!l
71
+($self->_section("logging") // {})->{"file"} // _strip_quotes($ENV{"KNARR_LOG_FILE"})
l!l&&r!l&&!r
701
$self->_section("logging") // {}
l!l
80
MC/DC0%0/2
(+($self->_section("logging") // {})->{"file"} // _strip_quotes($ENV{"KNARR_LOG_FILE"})) // undef
$self->_section("logging") _strip_quotes($ENV{"KNARR_LOG_FILE"})
Truth table33%1/3
A // B
A= $self->_section("logging")B= _strip_quotes($ENV{"KNARR_LOG_FILE"})
ABresult
1X1
011
000
231 }
232  
233 46 has log_dir => (
234 is => 'lazy',
235 builder => '_build_log_dir',
236 );
237  
238  
239 9 ▶sub _build_log_dir {
Subroutine _build_log_dir: called CC=4 SCAR=14.3
240 9 my ($self) = @_;
241 9 ▶ return ( $self->_section('logging') // {} )->{dir} // _strip_quotes($ENV{KNARR_LOG_DIR}) // undef;
Condition71%5/7
(+($self->_section("logging") // {})->{"dir"} // _strip_quotes($ENV{"KNARR_LOG_DIR"})) // undef
l!l
26
+($self->_section("logging") // {})->{"dir"} // _strip_quotes($ENV{"KNARR_LOG_DIR"})
l!l&&r!l&&!r
206
$self->_section("logging") // {}
l!l
80
MC/DC0%0/2
(+($self->_section("logging") // {})->{"dir"} // _strip_quotes($ENV{"KNARR_LOG_DIR"})) // undef
$self->_section("logging") _strip_quotes($ENV{"KNARR_LOG_DIR"})
Truth table33%1/3
A // B
A= $self->_section("logging")B= _strip_quotes($ENV{"KNARR_LOG_DIR"})
ABresult
1X1
011
000
242 }
243  
244 46 has langfuse => (
245 is => 'lazy',
246 builder => '_build_langfuse',
247 );
248  
249  
250 124 ▶sub _build_langfuse {
Subroutine _build_langfuse: called CC=2 SCAR=6.9
251 124 my ($self) = @_;
252 124 ▶ return $self->_section('langfuse') // {};
Condition100%2/2
$self->_section("langfuse") // {}
l!l
12111
MC/DC100%1/1
$self->_section("langfuse") // {}
$self->_section("langfuse")
Truth table100%2/2
A
A= $self->_section("langfuse")
Aresult
11
00
253 }
254  
255 46 has langfuse_transport => (
256 is => 'lazy',
257 builder => '_build_langfuse_transport',
258 );
259  
260  
261 78 ▶sub _build_langfuse_transport {
Subroutine _build_langfuse_transport: called CC=5 SCAR=16.5
262 78 my ($self) = @_;
263 78 ▶ my $value = $self->langfuse->{transport} // _strip_quotes($ENV{KNARR_LANGFUSE_TRANSPORT});
Condition66%2/3
$self->langfuse->{"transport"} // _strip_quotes($ENV{"KNARR_LANGFUSE_TRANSPORT"})
l!l&&r!l&&!r
2076
MC/DC50%1/2
$self->langfuse->{"transport"} // _strip_quotes($ENV{"KNARR_LANGFUSE_TRANSPORT"})
$self->langfuse->{"transport"} _strip_quotes($ENV{"KNARR_LANGFUSE_TRANSPORT"})
Truth table66%2/3
A // B
A= $self->langfuse->{"transport"}B= _strip_quotes($ENV{"KNARR_LANGFUSE_TRANSPORT"})
ABresult
1X1
011
000
264 78 ▶ return 'ingestion' unless defined $value && length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 76 2 78
Condition66%2/3
defined $value and length $value
!ll&&!rl&&r
7602
MC/DC50%1/2
defined $value and length $value
defined $value length $value
Truth table66%2/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
265 2 ▶ croak "langfuse.transport '$value' must be ingestion or otel"
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\A(?:ingestion|otel)\z/i 0 2 2
266 unless $value =~ /\A(?:ingestion|otel)\z/i;
267 2 return lc $value;
268 }
269  
270 46 has langfuse_timeout => (
271 is => 'lazy',
272 builder => '_build_langfuse_timeout',
273 );
274  
275  
276 78 ▶sub _build_langfuse_timeout {
Subroutine _build_langfuse_timeout: called CC=2 SCAR=7.1
277 78 my ($self) = @_;
278 return _seconds( 'langfuse.timeout' =>
279 78 ▶ $self->langfuse->{timeout} // _strip_quotes($ENV{KNARR_LANGFUSE_TIMEOUT}), 15 );
Condition66%2/3
$self->langfuse->{"timeout"} // _strip_quotes($ENV{"KNARR_LANGFUSE_TIMEOUT"})
l!l&&r!l&&!r
2076
MC/DC50%1/2
$self->langfuse->{"timeout"} // _strip_quotes($ENV{"KNARR_LANGFUSE_TIMEOUT"})
$self->langfuse->{"timeout"} _strip_quotes($ENV{"KNARR_LANGFUSE_TIMEOUT"})
Truth table66%2/3
A // B
A= $self->langfuse->{"timeout"}B= _strip_quotes($ENV{"KNARR_LANGFUSE_TIMEOUT"})
ABresult
1X1
011
000
280 }
281  
282 46 has proxy_api_key => (
283 is => 'lazy',
284 builder => '_build_proxy_api_key',
285 );
286  
287  
288 72 ▶sub _build_proxy_api_key {
Subroutine _build_proxy_api_key: called CC=3 SCAR=11.0
289 72 my ($self) = @_;
290 72 ▶ return $self->data->{proxy_api_key} // $ENV{KNARR_API_KEY} // undef;
Condition100%5/5
($self->data->{"proxy_api_key"} // $ENV{"KNARR_API_KEY"}) // undef
l!l
1161
$self->data->{"proxy_api_key"} // $ENV{"KNARR_API_KEY"}
l!l&&r!l&&!r
7461
MC/DC100%2/2
($self->data->{"proxy_api_key"} // $ENV{"KNARR_API_KEY"}) // undef
$self->data->{"proxy_api_key"} $ENV{"KNARR_API_KEY"}
Truth table100%3/3
A // B
A= $self->data->{"proxy_api_key"}B= $ENV{"KNARR_API_KEY"}
ABresult
1X1
011
000
291 }
292  
293 51 ▶sub has_proxy_api_key {
Subroutine has_proxy_api_key: called CC=4 SCAR=13.9
Pod documented
294 51 my ($self) = @_;
295 51 my $key = $self->proxy_api_key;
296 51 ▶ return defined $key && !ref $key && length $key ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $key && !ref($key) && length $key ? : 7 44 51
Condition83%5/6
defined $key and not ref $key and length $key
!ll&&!rl&&r
4137
defined $key and not ref $key
!ll&&!rl&&r
41010
MC/DC66%2/3
defined $key and not ref $key and length $key
defined $key not ref $key length $key
Truth table75%3/4
A and not(B) and C
A= defined $keyB= not ref $keyC= length $key
ABCresult
0XX0
10X0
1100
1111
297 }
298  
299 46 has public_url => (
300 is => 'lazy',
301 builder => '_build_public_url',
302 );
303  
304  
305 0 ▶sub _build_public_url {
Subroutine _build_public_url: not called CC=3 SCAR=24.8
306 0 my ($self) = @_;
307 0 ▶ return $self->data->{public_url} // $ENV{KNARR_PUBLIC_URL} // undef;
Condition0%0/5
($self->data->{"public_url"} // $ENV{"KNARR_PUBLIC_URL"}) // undef
l!l
00
$self->data->{"public_url"} // $ENV{"KNARR_PUBLIC_URL"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($self->data->{"public_url"} // $ENV{"KNARR_PUBLIC_URL"}) // undef
$self->data->{"public_url"} $ENV{"KNARR_PUBLIC_URL"}
Truth table0%0/3
A // B
A= $self->data->{"public_url"}B= $ENV{"KNARR_PUBLIC_URL"}
ABresult
1X1
011
000
308 }
309  
310  
311 46 has admin_key => (
312 is => 'lazy',
313 builder => '_build_admin_key',
314 );
315  
316  
317 73 ▶sub _build_admin_key {
Subroutine _build_admin_key: called CC=3 SCAR=11.0
318 73 my ($self) = @_;
319 73 ▶ return $self->data->{admin_key} // $ENV{KNARR_ADMIN_KEY} // undef;
Condition100%5/5
($self->data->{"admin_key"} // $ENV{"KNARR_ADMIN_KEY"}) // undef
l!l
1558
$self->data->{"admin_key"} // $ENV{"KNARR_ADMIN_KEY"}
l!l&&r!l&&!r
11458
MC/DC100%2/2
($self->data->{"admin_key"} // $ENV{"KNARR_ADMIN_KEY"}) // undef
$self->data->{"admin_key"} $ENV{"KNARR_ADMIN_KEY"}
Truth table100%3/3
A // B
A= $self->data->{"admin_key"}B= $ENV{"KNARR_ADMIN_KEY"}
ABresult
1X1
011
000
320 }
321  
322 80 ▶sub has_admin_key {
Subroutine has_admin_key: called CC=4 SCAR=13.9
Pod documented
323 80 my ($self) = @_;
324 80 my $key = $self->admin_key;
325 80 ▶ return defined $key && !ref $key && length $key ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $key && !ref($key) && length $key ? : 18 62 80
Condition83%5/6
defined $key and not ref $key and length $key
!ll&&!rl&&r
61118
defined $key and not ref $key
!ll&&!rl&&r
61019
MC/DC66%2/3
defined $key and not ref $key and length $key
defined $key not ref $key length $key
Truth table75%3/4
A and not(B) and C
A= defined $keyB= not ref $keyC= length $key
ABCresult
0XX0
10X0
1100
1111
326 }
327  
328  
329 70 ▶sub admin_key_min_length { 16 }
Subroutine admin_key_min_length: called CC=1 SCAR=0.0
Pod documented
330  
331  
332 46 has remote_config => (
333 is => 'lazy',
334 builder => '_build_remote_config',
335 );
336  
337  
338 79 ▶sub _build_remote_config {
Subroutine _build_remote_config: called CC=6 SCAR=17.9
339 79 my ($self) = @_;
340 79 my $value = $self->data->{remote_config};
341 79 ▶ $value = _strip_quotes($ENV{KNARR_REMOTE_CONFIG}) unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 69 10 79
342 79 ▶ return 0 unless defined $value && length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 64 15 79
Condition100%3/3
defined $value and length $value
!ll&&!rl&&r
63115
MC/DC100%2/2
defined $value and length $value
defined $value length $value
Truth table100%3/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
343 15 ▶ return !$value || $value =~ /\A(?:false|no|off)\z/i ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
!$value || $value =~ /\A(?:false|no|off)\z/i ? : 5 10 15
Condition100%3/3
not $value or $value =~ /\A(?:false|no|off)\z/i
l!l&&r!l&&!r
2310
MC/DC100%2/2
not $value or $value =~ /\A(?:false|no|off)\z/i
not $value $value =~ /\A(?:false|no|off)\z/i
Truth table100%3/3
not(A) or B
A= not $valueB= $value =~ /\A(?:false|no|off)\z/i
ABresult
1X1
011
000
344 }
345  
346 46 my %TOKEN_AUTH_KEYS = map { $_ => 1 } qw(
347 backend dsn user password_env relation listen hash columns match_model
348 );
349 46 my %TOKEN_COLUMN_DEFAULTS = (
350 hash => 'token_hash',
351 model => 'model',
352 provider => 'provider',
353 expires => 'expires_at',
354 );
355 46 my %ROUTE_KEYS = map { $_ => 1 } qw( upstream provider key_env url );
356  
357 46 has token_auth => (
358 is => 'lazy',
359 builder => '_build_token_auth',
360 );
361  
362  
363 7 ▶sub _build_token_auth {
Subroutine _build_token_auth: called CC=6 SCAR=17.9
364 7 my ($self) = @_;
365 7 my $raw = $self->_section('token_auth');
366 7 ▶ return undef unless defined $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw 4 3 7
367 3 ▶ my $columns = ref $raw->{columns} eq 'HASH' ? $raw->{columns} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $raw->{'columns'} eq 'HASH' ? : 2 1 3
368 3 my $match = $raw->{match_model};
369 return {
370 %$raw,
371 3 ▶ hash => $raw->{hash} // 'sha256',
Branch: 1 branch
BranchTrueFalseTotal
defined $match && length $match ? : 1 2 3
Condition80%4/5
$raw->{"hash"} // "sha256"
l!l
12
defined $match and length $match
!ll&&!rl&&r
201
MC/DC100%1/1
$raw->{"hash"} // "sha256"
$raw->{"hash"}
MC/DC50%1/2
defined $match and length $match
defined $match length $match
Truth table100%2/2
A
A= $raw->{"hash"}
Aresult
11
00
Truth table66%2/3
A and B
A= defined $matchB= length $match
ABresult
0X0
100
111
372 columns => { %TOKEN_COLUMN_DEFAULTS, %$columns },
373 match_model => ( defined $match && length $match ? _bool($match) : 1 ),
374 };
375 }
376  
377 46 has routes => (
378 is => 'lazy',
379 builder => '_build_routes',
380 );
381  
382  
383 2 ▶sub _build_routes {
Subroutine _build_routes: called CC=2 SCAR=6.9
384 2 my ($self) = @_;
385 2 ▶ return $self->_section('routes') // {};
Condition100%2/2
$self->_section("routes") // {}
l!l
11
MC/DC100%1/1
$self->_section("routes") // {}
$self->_section("routes")
Truth table100%2/2
A
A= $self->_section("routes")
Aresult
11
00
386 }
387  
388 2 ▶sub _bool {
Subroutine _bool: called CC=3 SCAR=11.1
389 2 my ($value) = @_;
390 2 ▶ return !$value || $value =~ /\A(?:false|no|off)\z/i ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
!$value || $value =~ /\A(?:false|no|off)\z/i ? : 1 1 2
Condition66%2/3
not $value or $value =~ /\A(?:false|no|off)\z/i
l!l&&r!l&&!r
101
MC/DC50%1/2
not $value or $value =~ /\A(?:false|no|off)\z/i
not $value $value =~ /\A(?:false|no|off)\z/i
Truth table66%2/3
not(A) or B
A= not $valueB= $value =~ /\A(?:false|no|off)\z/i
ABresult
1X1
011
000
391 }
392  
393 # Errors of token_auth and routes, and of their conflicts with the other
394 # settings (see validate). No text holds a value of the section.
395 68 ▶sub _token_auth_errors {
Subroutine _token_auth_errors: called CC=61 SCAR=43.1
396 68 my ($self) = @_;
397 68 my $raw = $self->data->{token_auth};
398 68 my $routes = $self->data->{routes};
399 68 my @errors;
400  
401 68 ▶ unless ( defined $raw ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $raw) 33 35 68
402 33 ▶ push @errors, "routes needs token_auth" if defined $routes;
Branch: 1 branch
BranchTrueFalseTotal
if defined $routes 1 32 33
403 33 return @errors;
404 }
405  
406 35 ▶ if ( ref $raw eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $raw eq 'HASH') { } 35 0 35
407 push @errors, "token_auth: unknown key '$_'"
408 35 for grep { !$TOKEN_AUTH_KEYS{$_} } sort keys %$raw;
409 35 my $backend = $raw->{backend};
410 35 ▶ push @errors, "token_auth: backend must be 'postgresql'"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $backend and not ref $backend and $backend eq "postgresql" 1 34 35
Condition50%3/6
defined $backend and not ref $backend and $backend eq "postgresql"
!ll&&!rl&&r
0134
defined $backend and not ref $backend
!ll&&!rl&&r
0035
MC/DC33%1/3
defined $backend and not ref $backend and $backend eq "postgresql"
defined $backend not ref $backend $backend eq "postgresql"
Truth table50%2/4
A and not(B) and C
A= defined $backendB= not ref $backendC= $backend eq "postgresql"
ABCresult
0XX0
10X0
1100
1111
411 unless defined $backend && !ref $backend && $backend eq 'postgresql';
412 35 for my $key (qw( dsn relation listen )) {
413 push @errors, "token_auth: $key is required and must be a non-empty string"
414 105 ▶ unless defined $raw->{$key} && !ref $raw->{$key} && length $raw->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw->{$key} and not ref $raw->{$key} and length $raw->{$key} 3 102 105
Condition66%4/6
defined $raw->{$key} and not ref $raw->{$key} and length $raw->{$key}
!ll&&!rl&&r
30102
defined $raw->{$key} and not ref $raw->{$key}
!ll&&!rl&&r
30102
MC/DC33%1/3
defined $raw->{$key} and not ref $raw->{$key} and length $raw->{$key}
defined $raw->{$key} not ref $raw->{$key} length $raw->{$key}
Truth table50%2/4
A and not(B) and C
A= defined $raw->{$key}B= not ref $raw->{$key}C= length $raw->{$key}
ABCresult
0XX0
10X0
1100
1111
415 }
416 35 ▶ push @errors, "token_auth: user must be a string" if ref $raw->{user};
Branch: 1 branch
BranchTrueFalseTotal
if ref $raw->{"user"} 0 35 35
417 35 ▶ if ( defined $raw->{password_env} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $raw->{"password_env"}) 2 33 35
418 push @errors, "token_auth: password_env must be the name of an environment variable,"
419 . " not the password"
420 2 ▶ unless !ref $raw->{password_env} && $raw->{password_env} =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $raw->{"password_env"} and $raw->{"password_env"} =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/ 1 1 2
Condition66%2/3
not ref $raw->{"password_env"} and $raw->{"password_env"} =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
!ll&&!rl&&r
011
MC/DC50%1/2
not ref $raw->{"password_env"} and $raw->{"password_env"} =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
not ref $raw->{"password_env"} $raw->{"password_env"} =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
Truth table66%2/3
not(A) and B
A= not ref $raw->{"password_env"}B= $raw->{"password_env"} =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
ABresult
0X0
100
111
421 }
422 push @errors, "token_auth: hash must be 'sha256'"
423 35 ▶ if defined $raw->{hash} && ( ref $raw->{hash} || $raw->{hash} ne 'sha256' );
Branch: 1 branch
BranchTrueFalseTotal
if defined $raw->{"hash"} and ref $raw->{"hash"} || $raw->{"hash"} ne "sha256" 1 34 35
Condition83%5/6
defined $raw->{"hash"} and ref $raw->{"hash"} || $raw->{"hash"} ne "sha256"
!ll&&!rl&&r
3311
ref $raw->{"hash"} or $raw->{"hash"} ne "sha256"
l!l&&r!l&&!r
011
MC/DC66%2/3
defined $raw->{"hash"} and ref $raw->{"hash"} || $raw->{"hash"} ne "sha256"
defined $raw->{"hash"} ref $raw->{"hash"} $raw->{"hash"} ne "sha256"
Truth table75%3/4
A and B or C
A= defined $raw->{"hash"}B= ref $raw->{"hash"}C= $raw->{"hash"} ne "sha256"
ABCresult
0XX0
1000
11X1
1011
424 35 ▶ if ( defined $raw->{columns} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $raw->{"columns"}) 4 31 35
425 4 ▶ if ( ref $raw->{columns} eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $raw->{'columns'} eq 'HASH') { } 3 1 4
426 3 for my $key ( sort keys %{ $raw->{columns} } ) {
427 push @errors, "token_auth: unknown columns key '$key'"
428 6 ▶ unless exists $TOKEN_COLUMN_DEFAULTS{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $TOKEN_COLUMN_DEFAULTS{$key} 1 5 6
429 push @errors, "token_auth: columns.$key must be a non-empty string"
430 if exists $TOKEN_COLUMN_DEFAULTS{$key}
431 && !( defined $raw->{columns}{$key} && !ref $raw->{columns}{$key}
432 6 ▶ && length $raw->{columns}{$key} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $TOKEN_COLUMN_DEFAULTS{$key} and not defined $raw->{"columns"}{$key} && !ref($raw->{"columns"}{$key}) && length $raw->{"columns"}{$key} 0 6 6
Condition44%4/9
exists $TOKEN_COLUMN_DEFAULTS{$key} and not defined $raw->{"columns"}{$key} && !ref($raw->{"columns"}{$key}) && length $raw->{"columns"}{$key}
!ll&&!rl&&r
150
defined $raw->{'columns'}{$key} && !ref($raw->{'columns'}{$key}) && length $raw->{'columns'}{$key}
!ll&&!rl&&r
005
defined $raw->{"columns"}{$key} and not ref $raw->{"columns"}{$key}
!ll&&!rl&&r
005
MC/DC0%0/4
exists $TOKEN_COLUMN_DEFAULTS{$key} and not defined $raw->{"columns"}{$key} && !ref($raw->{"columns"}{$key}) && length $raw->{"columns"}{$key}
exists $TOKEN_COLUMN_DEFAULTS{$key} defined $raw->{"columns"}{$key} not ref $raw->{"columns"}{$key} length $raw->{'columns'}{$key}
Truth table40%2/5
A and not(B and not(C) && D)
A= exists $TOKEN_COLUMN_DEFAULTS{$key}B= defined $raw->{"columns"}{$key}C= not ref $raw->{"columns"}{$key}D= length $raw->{'columns'}{$key}
ABCDresult
0XXX0
11110
10XX1
110X1
11101
433 }
434 }
435 else {
436 1 push @errors, "token_auth: columns must be a mapping";
437 }
438 }
439 35 ▶ if ( defined $raw->{match_model} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $raw->{"match_model"}) 1 34 35
440 push @errors, "token_auth: match_model must be true or false"
441 unless is_bool( $raw->{match_model} )
442 || ( !ref $raw->{match_model}
443 1 ▶ && $raw->{match_model} =~ /\A(?:true|false|yes|no|on|off|0|1)\z/i );
Branch: 1 branch
BranchTrueFalseTotal
unless is_bool($raw->{"match_model"}) or not ref $raw->{"match_model"} and $raw->{"match_model"} =~ /\A(?:true|false|yes|no|on|off|0|1)\z/i 0 1 1
Condition16%1/6
is_bool($raw->{"match_model"}) or not ref $raw->{"match_model"} and $raw->{"match_model"} =~ /\A(?:true|false|yes|no|on|off|0|1)\z/i
l!l&&r!l&&!r
100
not ref $raw->{"match_model"} and $raw->{"match_model"} =~ /\A(?:true|false|yes|no|on|off|0|1)\z/i
!ll&&!rl&&r
000
MC/DC0%0/3
is_bool($raw->{"match_model"}) or not ref $raw->{"match_model"} and $raw->{"match_model"} =~ /\A(?:true|false|yes|no|on|off|0|1)\z/i
is_bool($raw->{"match_model"}) not ref $raw->{"match_model"} $raw->{"match_model"} =~ /\A(?:true|false|yes|no|on|off|0|1)\z/i
Truth table25%1/4
A or not(B) and C
A= is_bool($raw->{"match_model"})B= not ref $raw->{"match_model"}C= $raw->{"match_model"} =~ /\A(?:true|false|yes|no|on|off|0|1)\z/i
ABCresult
1XX1
0111
00X0
0100
444 }
445 }
446 else {
447 0 push @errors, "token_auth must be a mapping";
448 }
449  
450 # routes
451 35 ▶ if ( !defined $routes ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $routes) { } 1 34 35
elsif (ref $routes ne 'HASH' or not %$routes) { } 1 33 34
452 1 push @errors, "token_auth needs routes";
453 }
454 ▶ elsif ( ref $routes ne 'HASH' || !%$routes ) {
Condition66%2/3
ref $routes ne "HASH" or not %$routes
l!l&&r!l&&!r
0133
MC/DC50%1/2
ref $routes ne "HASH" or not %$routes
ref $routes ne "HASH" not %$routes
Truth table66%2/3
A or not(B)
A= ref $routes ne "HASH"B= not %$routes
ABresult
1X1
011
000
455 1 push @errors, "routes must be a mapping of provider: route, with at least one route";
456 }
457 else {
458 33 my %known = map { lc $_->{engine} => 1 } @{ $self->engine_catalog };
459 33 for my $name ( sort keys %$routes ) {
460 56 my $route = $routes->{$name};
461 56 ▶ unless ( ref $route eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $route eq "HASH") 1 55 56
462 1 push @errors, "routes.$name must be a mapping";
463 1 next;
464 }
465 push @errors, "routes.$name: unknown key '$_'"
466 55 for grep { !$ROUTE_KEYS{$_} } sort keys %$route;
467 55 ▶ my $has_up = defined $route->{upstream} && length $route->{upstream};
Condition66%2/3
defined $route->{'upstream'} && length $route->{'upstream'}
!ll&&!rl&&r
30025
MC/DC50%1/2
defined $route->{'upstream'} && length $route->{'upstream'}
defined $route->{'upstream'} length $route->{'upstream'}
Truth table66%2/3
A && B
A= defined $route->{'upstream'}B= length $route->{'upstream'}
ABresult
0X0
100
111
468 55 ▶ my $has_pr = defined $route->{provider} && length $route->{provider};
Condition66%2/3
defined $route->{'provider'} && length $route->{'provider'}
!ll&&!rl&&r
25030
MC/DC50%1/2
defined $route->{'provider'} && length $route->{'provider'}
defined $route->{'provider'} length $route->{'provider'}
Truth table66%2/3
A && B
A= defined $route->{'provider'}B= length $route->{'provider'}
ABresult
0X0
100
111
469 55 ▶ push @errors, "routes.$name: needs exactly one of upstream and provider"
Branch: 1 branch
BranchTrueFalseTotal
unless $has_up xor $has_pr 2 53 55
Condition100%4/4
$has_up xor $has_pr
l&&rl&&!r!l&&r!l&&!r
124291
MC/DC100%2/2
$has_up xor $has_pr
$has_up $has_pr
Truth table100%4/4
A xor B
A= $has_upB= $has_pr
ABresult
110
101
011
000
470 unless $has_up xor $has_pr;
471 push @errors, "routes.$name: key_env must be the name of an environment variable"
472 unless defined $route->{key_env} && !ref $route->{key_env}
473 55 ▶ && $route->{key_env} =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $route->{"key_env"} and not ref $route->{"key_env"} and $route->{"key_env"} =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/ 2 53 55
Condition83%5/6
defined $route->{"key_env"} and not ref $route->{"key_env"} and $route->{"key_env"} =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
!ll&&!rl&&r
1153
defined $route->{"key_env"} and not ref $route->{"key_env"}
!ll&&!rl&&r
1054
MC/DC66%2/3
defined $route->{"key_env"} and not ref $route->{"key_env"} and $route->{"key_env"} =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
defined $route->{"key_env"} not ref $route->{"key_env"} $route->{"key_env"} =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
Truth table75%3/4
A and not(B) and C
A= defined $route->{"key_env"}B= not ref $route->{"key_env"}C= $route->{"key_env"} =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
ABCresult
0XX0
10X0
1100
1111
474 55 ▶ if ($has_up) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_up) 25 30 55
475 push @errors, "routes.$name: upstream must be an http:// or https:// URL"
476 25 ▶ unless !ref $route->{upstream} && $route->{upstream} =~ m{\Ahttps?://[^\s/]}i;
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $route->{"upstream"} and $route->{"upstream"} =~ m[\Ahttps?://[^\s/]]i 1 24 25
Condition66%2/3
not ref $route->{"upstream"} and $route->{"upstream"} =~ m[\Ahttps?://[^\s/]]i
!ll&&!rl&&r
0124
MC/DC50%1/2
not ref $route->{"upstream"} and $route->{"upstream"} =~ m[\Ahttps?://[^\s/]]i
not ref $route->{"upstream"} $route->{"upstream"} =~ m[\Ahttps?://[^\s/]]i
Truth table66%2/3
not(A) and B
A= not ref $route->{"upstream"}B= $route->{"upstream"} =~ m[\Ahttps?://[^\s/]]i
ABresult
0X0
100
111
477 push @errors, "routes.$name: url goes with provider, not with upstream"
478 25 ▶ if defined $route->{url};
Branch: 1 branch
BranchTrueFalseTotal
if defined $route->{"url"} 1 24 25
479 }
480 55 ▶ if ($has_pr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_pr) 30 25 55
481 push @errors, "routes.$name: unknown provider (see the engine catalog)"
482 30 ▶ unless !ref $route->{provider} && $known{ lc $route->{provider} };
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $route->{"provider"} and $known{lc $route->{"provider"}} 1 29 30
Condition66%2/3
not ref $route->{"provider"} and $known{lc $route->{"provider"}}
!ll&&!rl&&r
0129
MC/DC50%1/2
not ref $route->{"provider"} and $known{lc $route->{"provider"}}
not ref $route->{"provider"} $known{lc $route->{"provider"}}
Truth table66%2/3
not(A) and B
A= not ref $route->{"provider"}B= $known{lc $route->{"provider"}}
ABresult
0X0
100
111
483 push @errors, "routes.$name: url must be an http:// or https:// URL"
484 if defined $route->{url}
485 30 ▶ && ( ref $route->{url} || $route->{url} !~ m{\Ahttps?://[^\s/]}i );
Branch: 1 branch
BranchTrueFalseTotal
if defined $route->{"url"} and ref $route->{"url"} || !($route->{"url"} =~ m[\Ahttps?://[^\s/]]i) 1 29 30
Condition83%5/6
defined $route->{"url"} and ref $route->{"url"} || !($route->{"url"} =~ m[\Ahttps?://[^\s/]]i)
!ll&&!rl&&r
2811
ref $route->{"url"} or not $route->{"url"} =~ m[\Ahttps?://[^\s/]]i
l!l&&r!l&&!r
011
MC/DC66%2/3
defined $route->{"url"} and ref $route->{"url"} || !($route->{"url"} =~ m[\Ahttps?://[^\s/]]i)
defined $route->{"url"} ref $route->{"url"} not $route->{"url"} =~ m[\Ahttps?://[^\s/]]i
Truth table75%3/4
A and B or not(C)
A= defined $route->{"url"}B= ref $route->{"url"}C= not $route->{"url"} =~ m[\Ahttps?://[^\s/]]i
ABCresult
0XX0
1000
11X1
1011
486 }
487 }
488 }
489  
490 # One identity scheme per Knarr: each of these would reach past the
491 # per-model token (a shared key opens every model, a static model or
492 # passthrough serves without a route) or is delivered over the admin route.
493 35 ▶ push @errors, "token_auth cannot be combined with proxy_api_key (or KNARR_API_KEY):"
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_proxy_api_key 2 33 35
494 . " a shared key would open every model the per-model token limits"
495 if $self->has_proxy_api_key;
496 push @errors, "token_auth cannot be combined with models: routing comes from routes"
497 35 ▶ if %{ $self->models };
Branch: 1 branch
BranchTrueFalseTotal
if %{$self->models;} 1 34 35
498 35 ▶ push @errors, "token_auth cannot be combined with a default engine: routing comes from routes"
Branch: 1 branch
BranchTrueFalseTotal
if $self->default_engine 1 34 35
499 if $self->default_engine;
500 push @errors, "token_auth cannot be combined with passthrough: it would send the client's key upstream"
501 35 ▶ if %{ $self->passthrough };
Branch: 1 branch
BranchTrueFalseTotal
if %{$self->passthrough;} 2 33 35
502 35 ▶ push @errors, "token_auth cannot be combined with auto_discover: routing comes from routes"
Branch: 1 branch
BranchTrueFalseTotal
if $self->auto_discover and _bool($self->auto_discover) 1 34 35
Condition66%2/3
$self->auto_discover and _bool($self->auto_discover)
!ll&&!rl&&r
3401
MC/DC50%1/2
$self->auto_discover and _bool($self->auto_discover)
$self->auto_discover _bool($self->auto_discover)
Truth table66%2/3
A and B
A= $self->auto_discoverB= _bool($self->auto_discover)
ABresult
0X0
100
111
503 if $self->auto_discover && _bool( $self->auto_discover );
504 35 ▶ push @errors, "token_auth cannot be combined with remote_config: the setup is not delivered"
Branch: 1 branch
BranchTrueFalseTotal
if $self->remote_config 1 34 35
505 if $self->remote_config;
506 35 return @errors;
507 }
508  
509 # What an admin push may set (k75): the routing setup and the Langfuse
510 # tracing. Everything else is bound when the process starts.
511 46 my @RUNTIME_KEYS = qw(
512 models default passthrough auto_discover proxy_api_key
513 upstream_timeout upstream_stall_timeout probe_capabilities probe_timeout
514 langfuse
515 );
516  
517 3 ▶sub runtime_keys { @RUNTIME_KEYS }
Subroutine runtime_keys: called CC=1 SCAR=0.0
Pod documented
518  
519  
520 69 ▶sub admin_errors {
Subroutine admin_errors: called CC=9 SCAR=22.2
Pod documented
521 69 my ($self) = @_;
522 69 my @errors;
523 69 my $min = $self->admin_key_min_length;
524 69 ▶ if ( $self->has_admin_key ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->has_admin_key) { } 11 58 69
elsif (defined $self->admin_key and ref $self->admin_key) { } 0 116 116
525 11 ▶ push @errors, "admin_key must be at least $min characters"
Branch: 1 branch
BranchTrueFalseTotal
if length $self->admin_key < $min 1 10 11
526 if length $self->admin_key < $min;
527 11 ▶ push @errors, "admin_key must differ from proxy_api_key"
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_proxy_api_key and $self->proxy_api_key eq $self->admin_key 2 9 11
Condition100%3/3
$self->has_proxy_api_key and $self->proxy_api_key eq $self->admin_key
!ll&&!rl&&r
812
MC/DC100%2/2
$self->has_proxy_api_key and $self->proxy_api_key eq $self->admin_key
$self->has_proxy_api_key $self->proxy_api_key eq $self->admin_key
Truth table100%3/3
A and B
A= $self->has_proxy_api_keyB= $self->proxy_api_key eq $self->admin_key
ABresult
0X0
100
111
528 if $self->has_proxy_api_key && $self->proxy_api_key eq $self->admin_key;
529 }
530 ▶ elsif ( defined $self->admin_key && ref $self->admin_key ) {
Condition33%1/3
defined $self->admin_key and ref $self->admin_key
!ll&&!rl&&r
5800
MC/DC0%0/2
defined $self->admin_key and ref $self->admin_key
defined $self->admin_key ref $self->admin_key
Truth table33%1/3
A and B
A= defined $self->admin_keyB= ref $self->admin_key
ABresult
0X0
100
111
531 0 push @errors, "admin_key must be a string";
532 }
533 69 ▶ push @errors, "remote_config needs an admin_key (or KNARR_ADMIN_KEY): without one"
Branch: 1 branch
BranchTrueFalseTotal
if $self->remote_config and not $self->has_admin_key 1 68 69
Condition100%3/3
$self->remote_config and not $self->has_admin_key
!ll&&!rl&&r
6261
MC/DC100%2/2
$self->remote_config and not $self->has_admin_key
$self->remote_config not $self->has_admin_key
Truth table100%3/3
A and not(B)
A= $self->remote_configB= not $self->has_admin_key
ABresult
0X0
100
111
534 . " the setup can never be delivered"
535 if $self->remote_config && !$self->has_admin_key;
536 69 return @errors;
537 }
538  
539  
540 46 has ollama_compat_version => (
541 is => 'lazy',
542 builder => '_build_ollama_compat_version',
543 );
544  
545  
546 67 ▶sub _build_ollama_compat_version {
Subroutine _build_ollama_compat_version: called CC=4 SCAR=19.7
547 67 my ($self) = @_;
548 67 ▶ my $version = $self->data->{ollama_compat_version} // $ENV{KNARR_OLLAMA_COMPAT_VERSION};
Condition33%1/3
$self->data->{"ollama_compat_version"} // $ENV{"KNARR_OLLAMA_COMPAT_VERSION"}
l!l&&r!l&&!r
0067
MC/DC0%0/2
$self->data->{"ollama_compat_version"} // $ENV{"KNARR_OLLAMA_COMPAT_VERSION"}
$self->data->{"ollama_compat_version"} $ENV{"KNARR_OLLAMA_COMPAT_VERSION"}
Truth table33%1/3
A // B
A= $self->data->{"ollama_compat_version"}B= $ENV{"KNARR_OLLAMA_COMPAT_VERSION"}
ABresult
1X1
011
000
549 67 ▶ return undef unless defined $version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 67 0 67
550 # Open WebUI int()s every dotted part (k28): digits and dots only.
551 0 ▶ croak "ollama_compat_version '$version' must be three dot-separated numbers"
Branch: 1 branch
BranchTrueFalseTotal
unless $version =~ /\A\d+\.\d+\.\d+\z/ 0 0 0
552 . " like 0.34.4 (Ollama clients parse each part as an integer)"
553 unless $version =~ /\A\d+\.\d+\.\d+\z/;
554 0 return $version;
555 }
556  
557 46 has a2a_name => (
558 is => 'lazy',
559 builder => '_build_a2a_name',
560 );
561  
562  
563 3 ▶sub _build_a2a_name {
Subroutine _build_a2a_name: called CC=4 SCAR=16.9
564 3 my ($self) = @_;
565 3 ▶ return ( $self->_section('a2a') // {} )->{name} // _strip_quotes($ENV{KNARR_A2A_NAME}) // undef;
Condition42%3/7
(+($self->_section("a2a") // {})->{"name"} // _strip_quotes($ENV{"KNARR_A2A_NAME"})) // undef
l!l
10
+($self->_section("a2a") // {})->{"name"} // _strip_quotes($ENV{"KNARR_A2A_NAME"})
l!l&&r!l&&!r
100
$self->_section("a2a") // {}
l!l
10
MC/DC0%0/2
(+($self->_section("a2a") // {})->{"name"} // _strip_quotes($ENV{"KNARR_A2A_NAME"})) // undef
$self->_section("a2a") _strip_quotes($ENV{"KNARR_A2A_NAME"})
Truth table33%1/3
A // B
A= $self->_section("a2a")B= _strip_quotes($ENV{"KNARR_A2A_NAME"})
ABresult
1X1
011
000
566 }
567  
568 46 has a2a_description => (
569 is => 'lazy',
570 builder => '_build_a2a_description',
571 );
572  
573  
574 1 ▶sub _build_a2a_description {
Subroutine _build_a2a_description: called CC=4 SCAR=24.4
575 1 my ($self) = @_;
576 1 ▶ return ( $self->_section('a2a') // {} )->{description} // _strip_quotes($ENV{KNARR_A2A_DESCRIPTION}) // undef;
Condition0%0/7
(+($self->_section("a2a") // {})->{"description"} // _strip_quotes($ENV{"KNARR_A2A_DESCRIPTION"})) // undef
l!l
00
+($self->_section("a2a") // {})->{"description"} // _strip_quotes($ENV{"KNARR_A2A_DESCRIPTION"})
l!l&&r!l&&!r
000
$self->_section("a2a") // {}
l!l
00
MC/DC0%0/2
(+($self->_section("a2a") // {})->{"description"} // _strip_quotes($ENV{"KNARR_A2A_DESCRIPTION"})) // undef
$self->_section("a2a") _strip_quotes($ENV{"KNARR_A2A_DESCRIPTION"})
Truth table0%0/3
A // B
A= $self->_section("a2a")B= _strip_quotes($ENV{"KNARR_A2A_DESCRIPTION"})
ABresult
1X1
011
000
577 }
578  
579 1 ▶sub protocol_args {
Subroutine protocol_args: called CC=3 SCAR=17.3
Pod documented
580 1 my ($self) = @_;
581 1 ▶ return { A2A => {
Branch: 2 branches
BranchTrueFalseTotal
defined $self->a2a_name ? : 0 0 0
defined $self->a2a_description ? : 0 0 0
582 ( defined $self->a2a_name ? ( agent_name => $self->a2a_name ) : () ),
583 ( defined $self->a2a_description ? ( agent_description => $self->a2a_description ) : () ),
584 } };
585 }
586  
587  
588 46 has upstream_timeout => (
589 is => 'lazy',
590 builder => '_build_upstream_timeout',
591 );
592  
593  
594 111 ▶sub _build_upstream_timeout {
Subroutine _build_upstream_timeout: called CC=2 SCAR=8.1
595 111 my ($self) = @_;
596 return _seconds( upstream_timeout =>
597 111 ▶ $self->data->{upstream_timeout} // _strip_quotes($ENV{KNARR_UPSTREAM_TIMEOUT}), 300 );
Condition33%1/3
$self->data->{"upstream_timeout"} // _strip_quotes($ENV{"KNARR_UPSTREAM_TIMEOUT"})
l!l&&r!l&&!r
00111
MC/DC0%0/2
$self->data->{"upstream_timeout"} // _strip_quotes($ENV{"KNARR_UPSTREAM_TIMEOUT"})
$self->data->{"upstream_timeout"} _strip_quotes($ENV{"KNARR_UPSTREAM_TIMEOUT"})
Truth table33%1/3
A // B
A= $self->data->{"upstream_timeout"}B= _strip_quotes($ENV{"KNARR_UPSTREAM_TIMEOUT"})
ABresult
1X1
011
000
598 }
599  
600 46 has upstream_stall_timeout => (
601 is => 'lazy',
602 builder => '_build_upstream_stall_timeout',
603 );
604  
605  
606 67 ▶sub _build_upstream_stall_timeout {
Subroutine _build_upstream_stall_timeout: called CC=2 SCAR=8.1
607 67 my ($self) = @_;
608 return _seconds( upstream_stall_timeout =>
609 67 ▶ $self->data->{upstream_stall_timeout} // _strip_quotes($ENV{KNARR_UPSTREAM_STALL_TIMEOUT}), 120 );
Condition33%1/3
$self->data->{"upstream_stall_timeout"} // _strip_quotes($ENV{"KNARR_UPSTREAM_STALL_TIMEOUT"})
l!l&&r!l&&!r
0067
MC/DC0%0/2
$self->data->{"upstream_stall_timeout"} // _strip_quotes($ENV{"KNARR_UPSTREAM_STALL_TIMEOUT"})
$self->data->{"upstream_stall_timeout"} _strip_quotes($ENV{"KNARR_UPSTREAM_STALL_TIMEOUT"})
Truth table33%1/3
A // B
A= $self->data->{"upstream_stall_timeout"}B= _strip_quotes($ENV{"KNARR_UPSTREAM_STALL_TIMEOUT"})
ABresult
1X1
011
000
610 }
611  
612 46 has probe_capabilities => (
613 is => 'lazy',
614 builder => '_build_probe_capabilities',
615 );
616  
617  
618 0 ▶sub _build_probe_capabilities {
Subroutine _build_probe_capabilities: not called CC=6 SCAR=37.4
619 0 my ($self) = @_;
620 0 my $value = $self->data->{probe_capabilities};
621 0 ▶ unless ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
622 0 $value = _strip_quotes($ENV{KNARR_PROBE_CAPABILITIES});
623 0 ▶ return 1 unless defined $value && length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 0 0 0
Condition0%0/3
defined $value and length $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value and length $value
defined $value length $value
Truth table0%0/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
624 }
625 0 ▶ return !$value || $value =~ /\A(?:false|no|off)\z/i ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
!$value || $value =~ /\A(?:false|no|off)\z/i ? : 0 0 0
Condition0%0/3
not $value or $value =~ /\A(?:false|no|off)\z/i
l!l&&r!l&&!r
000
MC/DC0%0/2
not $value or $value =~ /\A(?:false|no|off)\z/i
not $value $value =~ /\A(?:false|no|off)\z/i
Truth table0%0/3
not(A) or B
A= not $valueB= $value =~ /\A(?:false|no|off)\z/i
ABresult
1X1
011
000
626 }
627  
628 46 has probe_timeout => (
629 is => 'lazy',
630 builder => '_build_probe_timeout',
631 );
632  
633  
634 67 ▶sub _build_probe_timeout {
Subroutine _build_probe_timeout: called CC=2 SCAR=8.1
635 67 my ($self) = @_;
636 return _seconds( probe_timeout =>
637 67 ▶ $self->data->{probe_timeout} // _strip_quotes($ENV{KNARR_PROBE_TIMEOUT}), 10 );
Condition33%1/3
$self->data->{"probe_timeout"} // _strip_quotes($ENV{"KNARR_PROBE_TIMEOUT"})
l!l&&r!l&&!r
0067
MC/DC0%0/2
$self->data->{"probe_timeout"} // _strip_quotes($ENV{"KNARR_PROBE_TIMEOUT"})
$self->data->{"probe_timeout"} _strip_quotes($ENV{"KNARR_PROBE_TIMEOUT"})
Truth table33%1/3
A // B
A= $self->data->{"probe_timeout"}B= _strip_quotes($ENV{"KNARR_PROBE_TIMEOUT"})
ABresult
1X1
011
000
638 }
639  
640 46 has workers => (
641 is => 'lazy',
642 builder => '_build_workers',
643 );
644  
645  
646 76 ▶sub _build_workers {
Subroutine _build_workers: called CC=5 SCAR=16.1
647 76 my ($self) = @_;
648 76 ▶ my $value = $self->data->{workers} // _strip_quotes($ENV{KNARR_WORKERS});
Condition100%3/3
$self->data->{"workers"} // _strip_quotes($ENV{"KNARR_WORKERS"})
l!l&&r!l&&!r
10264
MC/DC100%2/2
$self->data->{"workers"} // _strip_quotes($ENV{"KNARR_WORKERS"})
$self->data->{"workers"} _strip_quotes($ENV{"KNARR_WORKERS"})
Truth table100%3/3
A // B
A= $self->data->{"workers"}B= _strip_quotes($ENV{"KNARR_WORKERS"})
ABresult
1X1
011
000
649 76 ▶ return 1 unless defined $value && length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 64 12 76
Condition66%2/3
defined $value and length $value
!ll&&!rl&&r
64012
MC/DC50%1/2
defined $value and length $value
defined $value length $value
Truth table66%2/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
650 12 ▶ croak "workers '$value' must be a whole number of 1 or more"
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\A[1-9][0-9]*\z/ 8 4 12
651 unless $value =~ /\A[1-9][0-9]*\z/;
652 4 return $value + 0;
653 }
654  
655 # A config section (models:, default:, logging:, ...) that is not a mapping
656 # croaks when read (k67), instead of dying later as a HASH dereference;
657 # validate reports it.
658 927 ▶sub _section {
Subroutine _section: called CC=4 SCAR=13.9
659 927 my ($self, $key) = @_;
660 927 my $value = $self->data->{$key};
661 927 ▶ return $value if !defined $value || ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value eq "HASH" 905 22 927
Condition100%3/3
not defined $value or ref $value eq "HASH"
l!l&&r!l&&!r
67123422
MC/DC100%2/2
not defined $value or ref $value eq "HASH"
not defined $value ref $value eq "HASH"
Truth table100%3/3
not(A) or B
A= not defined $valueB= ref $value eq "HASH"
ABresult
1X1
011
000
662 22 ▶ croak "$key must be a mapping of key: value pairs, not "
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 5 17 22
663 . ( ref $value eq 'ARRAY' ? 'a list' : "'".$value."'" );
664 }
665  
666 358 ▶sub _seconds {
Subroutine _seconds: called CC=4 SCAR=14.1
667 358 my ($name, $value, $default) = @_;
668 358 ▶ return $default unless defined $value && length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 356 2 358
Condition66%2/3
defined $value and length $value
!ll&&!rl&&r
35602
MC/DC50%1/2
defined $value and length $value
defined $value length $value
Truth table66%2/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
669 2 ▶ croak "$name '$value' must be a number of seconds (0 disables it)"
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\A(?:\d+(?:\.\d*)?|\.\d+)\z/ 0 2 2
670 unless $value =~ /\A(?:\d+(?:\.\d*)?|\.\d+)\z/;
671 2 return $value + 0;
672 }
673  
674 46 has auto_discover => (
675 is => 'lazy',
676 builder => '_build_auto_discover',
677 );
678  
679  
680 77 ▶sub _build_auto_discover {
Subroutine _build_auto_discover: called CC=2 SCAR=6.9
681 77 my ($self) = @_;
682 77 ▶ return $self->data->{auto_discover} // 0;
Condition100%2/2
$self->data->{"auto_discover"} // 0
l!l
3542
MC/DC100%1/1
$self->data->{"auto_discover"} // 0
$self->data->{"auto_discover"}
Truth table100%2/2
A
A= $self->data->{"auto_discover"}
Aresult
11
00
683 }
684  
685 46 my %PASSTHROUGH_DEFAULTS = (
686 anthropic => 'https://api.anthropic.com',
687 openai => 'https://api.openai.com',
688 );
689  
690 46 has passthrough => (
691 is => 'lazy',
692 builder => '_build_passthrough',
693 );
694  
695  
696 115 ▶sub _build_passthrough {
Subroutine _build_passthrough: called CC=10 SCAR=23.1
697 115 my ($self) = @_;
698 115 my $raw = $self->data->{passthrough};
699 115 ▶ return {} unless defined $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw 88 27 115
700  
701 # passthrough: true → enable all with default URLs
702 27 ▶ if (!ref $raw) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $raw) 6 21 27
703 6 ▶ return $raw ? { %PASSTHROUGH_DEFAULTS } : {};
Branch: 1 branch
BranchTrueFalseTotal
$raw ? : 4 2 6
704 }
705  
706 21 ▶ croak "passthrough must be true, false or a mapping of format: URL, not a list"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $raw eq "HASH" 2 19 21
707 unless ref $raw eq 'HASH';
708  
709 19 my %result;
710 19 for my $format (keys %$raw) {
711 20 my $val = $raw->{$format};
712 20 ▶ next unless $val;
Branch: 1 branch
BranchTrueFalseTotal
unless $val 0 20 20
713 20 ▶ if ($val eq '1' || $val eq 'true') {
Branch: 1 branch
BranchTrueFalseTotal
if ($val eq '1' or $val eq 'true') { } 1 19 20
Condition66%2/3
$val eq 1 or $val eq "true"
l!l&&r!l&&!r
1019
MC/DC50%1/2
$val eq 1 or $val eq "true"
$val eq 1 $val eq "true"
Truth table66%2/3
A or B
A= $val eq 1B= $val eq "true"
ABresult
1X1
011
000
714 1 ▶ $result{$format} = $PASSTHROUGH_DEFAULTS{$format} // next;
Condition50%1/2
$PASSTHROUGH_DEFAULTS{$format} // (next)
l!l
10
MC/DC0%0/1
$PASSTHROUGH_DEFAULTS{$format} // (next)
$PASSTHROUGH_DEFAULTS{$format}
Truth table50%1/2
A
A= $PASSTHROUGH_DEFAULTS{$format}
Aresult
11
00
715 } else {
716 # Custom URL
717 19 $result{$format} = $val;
718 }
719 }
720 19 return \%result;
721 }
722  
723 8 ▶sub passthrough_url_for {
Subroutine passthrough_url_for: called CC=1 SCAR=0.0
Pod documented
724 8 my ($self, $format) = @_;
725 8 return $self->passthrough->{$format};
726 }
727  
728  
729  
730 85 ▶sub validate {
Subroutine validate: called CC=28 SCAR=33.5
Pod documented
731 85 my ($self) = @_;
732 85 my @errors;
733  
734 # A file that does not load leaves nothing else to check (k67)
735 85 ▶ return $self->_error_text($@) unless eval { $self->data; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->data; 1 } } 3 82 85
736  
737 # A section that is not a mapping croaks when read (k67)
738 82 for my $key (qw( models default langfuse logging a2a token_auth routes )) {
739 574 ▶ push @errors, $self->_error_text($@) unless eval { $self->_section($key); 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->_section($key); 1 } } 13 561 574
740 }
741 82 for my $attr (qw( passthrough listen )) {
742 164 ▶ push @errors, $self->_error_text($@) unless eval { $self->$attr; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->$attr; 1 } } 4 160 164
743 }
744 82 ▶ return @errors if @errors;
Branch: 1 branch
BranchTrueFalseTotal
if @errors 14 68 82
745  
746 68 my $models = $self->models;
747 68 for my $name (keys %$models) {
748 32 my $def = $models->{$name};
749 32 ▶ if ( defined $def && ref $def ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $def and ref $def ne "HASH") 1 31 32
Condition66%2/3
defined $def and ref $def ne "HASH"
!ll&&!rl&&r
0311
MC/DC50%1/2
defined $def and ref $def ne "HASH"
defined $def ref $def ne "HASH"
Truth table66%2/3
A and B
A= defined $defB= ref $def ne "HASH"
ABresult
0X0
100
111
750 1 push @errors, "Model '$name' must be a mapping of key: value pairs";
751 1 next;
752 }
753 31 ▶ unless ($def->{engine}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($def->{"engine"}) 1 30 31
754 1 push @errors, "Model '$name': missing 'engine' key";
755 }
756 31 ▶ if ( defined $def->{context_size} && $def->{context_size} !~ /\A[1-9][0-9]*\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $def->{"context_size"} and not $def->{"context_size"} =~ /\A[1-9][0-9]*\z/) 1 30 31
Condition100%3/3
defined $def->{"context_size"} and not $def->{"context_size"} =~ /\A[1-9][0-9]*\z/
!ll&&!rl&&r
2641
MC/DC100%2/2
defined $def->{"context_size"} and not $def->{"context_size"} =~ /\A[1-9][0-9]*\z/
defined $def->{"context_size"} not $def->{"context_size"} =~ /\A[1-9][0-9]*\z/
Truth table100%3/3
A and not(B)
A= defined $def->{"context_size"}B= not $def->{"context_size"} =~ /\A[1-9][0-9]*\z/
ABresult
0X0
100
111
757 1 push @errors, "Model '$name': context_size must be a positive integer";
758 }
759 31 ▶ unless ( eval { _seconds( user_agent_timeout => $def->{user_agent_timeout}, 0 ); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { _seconds("user_agent_timeout", $def->{"user_agent_timeout"}, 0); 1 } }) 0 31 31
760 0 push @errors, "Model '$name': user_agent_timeout must be a number of seconds";
761 }
762 31 ▶ push @errors, "Model '$name': api_key must be a string" if ref $def->{api_key};
Branch: 1 branch
BranchTrueFalseTotal
if ref $def->{"api_key"} 1 30 31
763 }
764  
765 68 ▶ if (my $default = $self->default_engine) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $default = $self->default_engine) 4 64 68
766 4 ▶ unless ($default->{engine}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($default->{"engine"}) 0 4 4
767 0 push @errors, "Default: missing 'engine' key";
768 }
769 4 ▶ if ( defined $default->{context_size} && $default->{context_size} !~ /\A[1-9][0-9]*\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default->{"context_size"} and not $default->{"context_size"} =~ /\A[1-9][0-9]*\z/) 1 3 4
Condition100%3/3
defined $default->{"context_size"} and not $default->{"context_size"} =~ /\A[1-9][0-9]*\z/
!ll&&!rl&&r
121
MC/DC100%2/2
defined $default->{"context_size"} and not $default->{"context_size"} =~ /\A[1-9][0-9]*\z/
defined $default->{"context_size"} not $default->{"context_size"} =~ /\A[1-9][0-9]*\z/
Truth table100%3/3
A and not(B)
A= defined $default->{"context_size"}B= not $default->{"context_size"} =~ /\A[1-9][0-9]*\z/
ABresult
0X0
100
111
770 1 push @errors, "Default: context_size must be a positive integer";
771 }
772 4 ▶ unless ( eval { _seconds( user_agent_timeout => $default->{user_agent_timeout}, 0 ); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { _seconds("user_agent_timeout", $default->{"user_agent_timeout"}, 0); 1 } }) 0 4 4
773 0 push @errors, "Default: user_agent_timeout must be a number of seconds";
774 }
775 4 ▶ push @errors, "Default: api_key must be a string" if ref $default->{api_key};
Branch: 1 branch
BranchTrueFalseTotal
if ref $default->{"api_key"} 0 4 4
776 }
777  
778 # Under remote_config the models may arrive later, by admin push (k75).
779 # Under token_auth the routing comes from routes.
780 68 ▶ unless (keys %$models || $self->default_engine || $self->remote_config || defined $self->data->{token_auth}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %$models or $self->default_engine or $self->remote_config or defined $self->data->{"token_auth"}) 2 66 68
Condition100%9/9
keys %$models or $self->default_engine or $self->remote_config or defined $self->data->{"token_auth"}
l!l&&r!l&&!r
34322
keys %$models or $self->default_engine or $self->remote_config
l!l&&r!l&&!r
28634
keys %$models or $self->default_engine
l!l&&r!l&&!r
27140
MC/DC100%4/4
keys %$models or $self->default_engine or $self->remote_config or defined $self->data->{"token_auth"}
keys %$models $self->default_engine $self->remote_config defined $self->data->{"token_auth"}
Truth table100%5/5
A or B or C or D
A= keys %$modelsB= $self->default_engineC= $self->remote_configD= defined $self->data->{"token_auth"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
781 2 push @errors, "No models configured and no default engine set";
782 }
783  
784 68 for my $attr (qw( ollama_compat_version upstream_timeout upstream_stall_timeout probe_timeout workers langfuse_transport langfuse_timeout )) {
785 476 ▶ next if eval { $self->$attr; 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $self->$attr; 1 } } 472 4 476
786 4 push @errors, $self->_error_text($@);
787 }
788  
789 68 ▶ push @errors, "proxy_api_key must be a string"
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->proxy_api_key 1 67 68
790 if ref $self->proxy_api_key;
791  
792 68 push @errors, $self->_token_auth_errors;
793  
794 68 push @errors, $self->admin_errors;
795  
796 68 return @errors;
797 }
798  
799 # A croak as a validation error: without the trailing "at FILE line N."
800 25 ▶sub _error_text {
Subroutine _error_text: called CC=1 SCAR=0.0
801 25 my ($self, $err) = @_;
802 25 $err =~ s/\s+at \S+ line \d+\.?\s*\z//;
803 25 return $err;
804 }
805  
806 2 ▶sub engine_definitions {
Subroutine engine_definitions: called CC=2 SCAR=6.9
Pod undocumented
807 2 my ($self) = @_;
808 2 my %defs;
809 2 my $models = $self->models;
810 1 for my $name (keys %$models) {
811 2 $defs{$name} = { %{$models->{$name}}, name => $name };
812 }
813 1 return \%defs;
814 }
815  
816  
817 # Scan environment and .env files for API keys, return model config suggestions
818 16 ▶sub scan_env {
Subroutine scan_env: called CC=15 SCAR=27.6
Pod documented
819 16 my ($class, %opts) = @_;
820 16 ▶ my @env_files = @{$opts{env_files} // []};
Condition100%2/2
$opts{"env_files"} // []
l!l
214
MC/DC100%1/1
$opts{"env_files"} // []
$opts{"env_files"}
Truth table100%2/2
A
A= $opts{"env_files"}
Aresult
11
00
821 16 my %env = %ENV;
822  
823 # Load .env files
824 16 for my $file (@env_files) {
825 1 ▶ next unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 1 1
826 1 ▶ open my $fh, '<', $file or next;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or next 1 0 1
827 1 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 3 1 4
828 3 chomp;
829 3 ▶ next if /^\s*#/ || /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*#/ or /^\s*$/ 1 2 3
Condition66%2/3
/^\s*#/ or /^\s*$/
l!l&&r!l&&!r
102
MC/DC50%1/2
/^\s*#/ or /^\s*$/
/^\s*#/ /^\s*$/
Truth table66%2/3
A or B
A= /^\s*#/B= /^\s*$/
ABresult
1X1
011
000
830 2 ▶ if (/^\s*(?:export\s+)?(\w+)\s*=\s*['"]?(.*?)['"]?\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*(?:export\s+)?(\w+)\s*=\s*['"]?(.*?)['"]?\s*$/) 2 0 2
831 2 $env{$1} = $2;
832 }
833 }
834 1 close $fh;
835 }
836  
837 16 ▶ my $include_test = $opts{include_test} // 1;
Condition100%2/2
$opts{"include_test"} // 1
l!l
115
MC/DC100%1/1
$opts{"include_test"} // 1
$opts{"include_test"}
Truth table100%2/2
A
A= $opts{"include_test"}
Aresult
11
00
838  
839 16 my %found;
840 16 for my $def (@ENGINE_DEFS) {
841 304 for my $var (@{$def->{vars}}) {
842 797 ▶ next unless $env{$var};
Branch: 1 branch
BranchTrueFalseTotal
unless $env{$var} 743 54 797
843 54 ▶ next if !$include_test && $var =~ /^TEST_/;
Branch: 1 branch
BranchTrueFalseTotal
if not $include_test and $var =~ /^TEST_/ 0 54 54
Condition33%1/3
not $include_test and $var =~ /^TEST_/
!ll&&!rl&&r
5400
MC/DC0%0/2
not $include_test and $var =~ /^TEST_/
not $include_test $var =~ /^TEST_/
Truth table33%1/3
not(A) and B
A= not $include_testB= $var =~ /^TEST_/
ABresult
0X0
100
111
844 $found{$def->{engine}} = {
845 engine => $def->{engine},
846 54 api_key_env => $var,
847 };
848 54 last; # first match wins (priority order)
849 }
850 }
851  
852 16 return \%found;
853 }
854  
855  
856 # Generate a YAML config string from scan results
857 7 ▶sub generate_config {
Subroutine generate_config: called CC=14 SCAR=27.2
Pod documented
858 7 my ($class, %opts) = @_;
859 7 ▶ my $found = $opts{engines} // {};
Condition50%1/2
$opts{"engines"} // {}
l!l
70
MC/DC0%0/1
$opts{"engines"} // {}
$opts{"engines"}
Truth table50%1/2
A
A= $opts{"engines"}
Aresult
11
00
860 7 ▶ my $listen = $opts{listen} // ['127.0.0.1:8080', '127.0.0.1:11434'];
Condition100%2/2
$opts{"listen"} // ["127.0.0.1:8080", "127.0.0.1:11434"]
l!l
16
MC/DC100%1/1
$opts{"listen"} // ["127.0.0.1:8080", "127.0.0.1:11434"]
$opts{"listen"}
Truth table100%2/2
A
A= $opts{"listen"}
Aresult
11
00
861 7 ▶ $listen = [$listen] unless ref $listen eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $listen eq "ARRAY" 0 7 7
862  
863 7 my @lines;
864 7 push @lines, "# Knarr configuration - auto-generated";
865 7 push @lines, "listen:";
866 7 for my $addr (@$listen) {
867 14 push @lines, " - \"$addr\"";
868 }
869 7 push @lines, "";
870 7 push @lines, "models:";
871  
872 7 for my $engine (sort keys %$found) {
873 26 my $info = $found->{$engine};
874 26 my $model = $class->default_model_for($engine);
875 26 my $name = lc($engine);
876 26 ▶ $name .= "-default" if $name eq 'openai' || $name eq 'anthropic';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "openai" or $name eq "anthropic" 9 17 26
Condition100%3/3
$name eq "openai" or $name eq "anthropic"
l!l&&r!l&&!r
6317
MC/DC100%2/2
$name eq "openai" or $name eq "anthropic"
$name eq "openai" $name eq "anthropic"
Truth table100%3/3
A or B
A= $name eq "openai"B= $name eq "anthropic"
ABresult
1X1
011
000
877 26 push @lines, " $name:";
878 26 push @lines, " engine: $engine";
879 26 ▶ push @lines, " model: $model" if $model;
Branch: 1 branch
BranchTrueFalseTotal
if $model 24 2 26
880 26 ▶ push @lines, " api_key_env: $info->{api_key_env}" if $info->{api_key_env};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"api_key_env"} 26 0 26
881 26 push @lines, "";
882 }
883  
884 7 ▶ unless (keys %$found) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %$found) 0 7 7
885 0 ▶ my $example = $class->default_model_for('OpenAI') // 'gpt-4o-mini';
Condition0%0/2
$class->default_model_for("OpenAI") // "gpt-4o-mini"
l!l
00
MC/DC0%0/1
$class->default_model_for("OpenAI") // "gpt-4o-mini"
$class->default_model_for("OpenAI")
Truth table0%0/2
A
A= $class->default_model_for("OpenAI")
Aresult
11
00
886 0 push @lines, " # No API keys found. Add your models here:";
887 0 push @lines, " # my-model:";
888 0 push @lines, " # engine: OpenAI";
889 0 push @lines, " # model: $example";
890 0 push @lines, "";
891 }
892  
893 7 push @lines, "# Default engine for models without explicit config (optional)";
894 7 ▶ if ($found->{OpenAI}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found->{'OpenAI'}) { } 6 1 7
895 6 push @lines, "default:";
896 6 push @lines, " engine: OpenAI";
897 6 ▶ push @lines, " api_key_env: $found->{OpenAI}{api_key_env}" if $found->{OpenAI}{api_key_env};
Branch: 1 branch
BranchTrueFalseTotal
if $found->{"OpenAI"}{"api_key_env"} 6 0 6
898 } else {
899 1 push @lines, "# default:";
900 1 push @lines, "# engine: OpenAI";
901 }
902 7 push @lines, "";
903  
904 7 push @lines, "# Auto-discover models from configured engines";
905 7 push @lines, "auto_discover: true";
906 7 push @lines, "";
907  
908 7 push @lines, "# Optional: proxy authentication";
909 7 push @lines, "# proxy_api_key: your-secret-key";
910 7 push @lines, "";
911  
912 7 push @lines, "# Optional: Langfuse tracing (or set env vars)";
913 7 push @lines, "# langfuse:";
914 7 push @lines, "# url: http://localhost:3000";
915 7 push @lines, "# public_key: pk-lf-...";
916 7 push @lines, "# secret_key: sk-lf-...";
917 7 push @lines, "# transport: otel # default ingestion; otel needs Langfuse Cloud or v3.22+";
918 7 push @lines, "# timeout: 15 # seconds per trace POST, 0 disables";
919  
920 7 return join("\n", @lines) . "\n";
921 }
922  
923 # Strip surrounding quotes from env values (Docker --env-file includes them literally)
924 539 ▶sub _strip_quotes {
Subroutine _strip_quotes: called CC=2 SCAR=6.9
925 539 my $v = shift;
926 539 ▶ return $v unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 531 8 539
927 8 $v =~ s/^["']|["']$//g;
928 8 return $v;
929 }
930  
931  
932 46 1;
933  
934 __END__
« blib/lib/Langertha/Knarr/Handler/ACPClient.pm ↑ Summary blib/lib/Langertha/Knarr/Handler/Engine.pm »