lib/Langertha/Skeid/Proxy.pm

filter: Statementstmt 97.9% 1438 / 1468 Branchbran 88.0% 627 / 712 Conditioncond 65.1% 425 / 652 MC/DCmcdc 39.5% 141 / 357 Subroutinesub 97.5% 200 / 205 Podpod 100% 2 / 2 totaltotal 83.4% 2833 / 3396 CC 635 SCAR 27.0
CC
635
Cov
88%
__ANON__
90.0
_record_usage_event
63.5
__ANON__
40.9
CRAP 14.9
« blib/lib/Langertha/Skeid/UsageStore/DBI.pm ↑ Summary lib/Langertha/Skeid/Protocol/Ollama/Stream.pm »
1 package Langertha::Skeid::Proxy;
2 86 our $VERSION = '0.004';
3 # ABSTRACT: Multi-format LLM proxy (OpenAI, Anthropic, Ollama) powered by Langertha::Skeid routing
4 86 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 86 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 86 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 86 ▶use Mojolicious;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 86 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 86 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 86 ▶use JSON::MaybeXS qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 86 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 86 ▶use Mojo::Util qw(decode encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 86 ▶use Langertha::Skeid;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 86 ▶use Langertha::Skeid::CapacityProbe;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 86 ▶use Langertha::Skeid::OpenAPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 86 ▶use Langertha::Skeid::Registry;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 86 ▶use Langertha::Skeid::SwaggerUI;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 86 ▶use Langertha::Skeid::Secret;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 86 ▶use Langertha::Skeid::Proxy::Realtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 86 ▶use Langertha::Skeid::Proxy::RelayContent;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 86 ▶use Langertha::Skeid::Protocol;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 86 ▶use Langertha::Skeid::Protocol::Anthropic;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 86 ▶use Langertha::Skeid::Protocol::Anthropic::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 86 ▶use Langertha::Skeid::Protocol::Audio;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 86 ▶use Langertha::Skeid::Protocol::Documents;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 86 ▶use Langertha::Skeid::Protocol::MCP;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 86 ▶use Langertha::Skeid::Protocol::Ollama;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 86 ▶use Langertha::Skeid::Protocol::Ollama::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 86 ▶use Langertha::Skeid::Protocol::RawBody;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 86 ▶use Langertha::Skeid::Protocol::Realtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 86 ▶use Langertha::Skeid::Protocol::Rerank;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 86 ▶use Langertha::ToolCall;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34  
35 # What the request limit of an upload route allows on top of uploads.max_bytes (skeid k91): the
36 # request's head, which the server counts into its message size and caps well below this itself
37 # (100 lines of 8 KiB). The body alone is checked exactly when the route runs.
38 86 my $UPLOAD_HEAD_ALLOWANCE = 1048576;
39  
40 513 ▶sub build_app {
Subroutine build_app: called CC=26 SCAR=35.1
Pod documented
41 513 my ($class, %opts) = @_;
42  
43 # Auto-detect OpenBao KeyBroker if OPENBAO_ROLE_ID is set
44 513 ▶ my @skeid_opts = ($opts{config_file} ? (config_file => $opts{config_file}) : ());
Branch: 1 branch
BranchTrueFalseTotal
$opts{'config_file'} ? : 2 511 513
45 513 ▶ if ($ENV{OPENBAO_ROLE_ID} && $ENV{OPENBAO_SECRET_ID}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OPENBAO_ROLE_ID"} and $ENV{"OPENBAO_SECRET_ID"}) 0 513 513
Condition33%1/3
$ENV{"OPENBAO_ROLE_ID"} and $ENV{"OPENBAO_SECRET_ID"}
!ll&&!rl&&r
51300
MC/DC0%0/2
$ENV{"OPENBAO_ROLE_ID"} and $ENV{"OPENBAO_SECRET_ID"}
$ENV{"OPENBAO_ROLE_ID"} $ENV{"OPENBAO_SECRET_ID"}
Truth table33%1/3
A and B
A= $ENV{"OPENBAO_ROLE_ID"}B= $ENV{"OPENBAO_SECRET_ID"}
ABresult
0X0
100
111
46 0 eval {
47 0 require Langertha::Skeid::KeyBroker::OpenBao;
48 push @skeid_opts, key_broker => Langertha::Skeid::KeyBroker::OpenBao->new(
49 addr => $ENV{OPENBAO_ADDR} // 'http://127.0.0.1:8200',
50 role_id => $ENV{OPENBAO_ROLE_ID},
51 secret_id => $ENV{OPENBAO_SECRET_ID},
52 0 ▶ );
Condition0%0/2
$ENV{"OPENBAO_ADDR"} // "http://127.0.0.1:8200"
l!l
00
MC/DC0%0/1
$ENV{"OPENBAO_ADDR"} // "http://127.0.0.1:8200"
$ENV{"OPENBAO_ADDR"}
Truth table0%0/2
A
A= $ENV{"OPENBAO_ADDR"}
Aresult
11
00
53 };
54 0 ▶ warn "Failed to initialize OpenBao KeyBroker: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
55 }
56  
57 # The explicit admin API key goes into new(), so it is in force for the first config already
58 # (a registry block checks for it); an existing Skeid gets it set (skeid k64).
59 push @skeid_opts, admin_api_key => $opts{admin_api_key}
60 513 ▶ if defined($opts{admin_api_key}) && length($opts{admin_api_key});
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"admin_api_key"} and length $opts{"admin_api_key"} 7 506 513
Condition66%2/3
defined $opts{"admin_api_key"} and length $opts{"admin_api_key"}
!ll&&!rl&&r
50607
MC/DC50%1/2
defined $opts{"admin_api_key"} and length $opts{"admin_api_key"}
defined $opts{"admin_api_key"} length $opts{"admin_api_key"}
Truth table66%2/3
A and B
A= defined $opts{"admin_api_key"}B= length $opts{"admin_api_key"}
ABresult
0X0
100
111
61 513 ▶ my $skeid = $opts{skeid} || Langertha::Skeid->new(@skeid_opts);
Condition100%3/3
$opts{'skeid'} || 'Langertha::Skeid'->new(@skeid_opts)
l!l&&r!l&&!r
51111
MC/DC0%0/2
$opts{'skeid'} || 'Langertha::Skeid'->new(@skeid_opts)
$opts{'skeid'} 'Langertha::Skeid'->new(@skeid_opts)
Truth table66%2/3
A || B
A= $opts{'skeid'}B= 'Langertha::Skeid'->new(@skeid_opts)
ABresult
1X1
011
000
62 512 ▶ $skeid->set_admin_api_key($opts{admin_api_key}) if exists $opts{admin_api_key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"admin_api_key"} 7 505 512
63 # How many processes share these nodes. Set before anything reads max_conns or starts a
64 # timer, since both are divided by it (ADR 0010).
65 512 ▶ if (defined $opts{worker_count} && $opts{worker_count} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"worker_count"} and $opts{"worker_count"} > 0) 0 512 512
Condition33%1/3
defined $opts{"worker_count"} and $opts{"worker_count"} > 0
!ll&&!rl&&r
51200
MC/DC0%0/2
defined $opts{"worker_count"} and $opts{"worker_count"} > 0
defined $opts{"worker_count"} $opts{"worker_count"} > 0
Truth table33%1/3
A and B
A= defined $opts{"worker_count"}B= $opts{"worker_count"} > 0
ABresult
0X0
100
111
66 0 $skeid->worker_count(0 + $opts{worker_count});
67 }
68  
69 # Renew the vault token on a timer rather than when a request discovers it expired. A request
70 # that has to renew first pays the round-trip in its own latency, and it is the request least
71 # able to afford it -- the first one after a quiet period.
72 512 ▶ if ($skeid->has_key_broker && $skeid->key_broker->can('start_renewal')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skeid->has_key_broker and $skeid->key_broker->can("start_renewal")) 0 512 512
Condition66%2/3
$skeid->has_key_broker and $skeid->key_broker->can("start_renewal")
!ll&&!rl&&r
428840
MC/DC0%0/2
$skeid->has_key_broker and $skeid->key_broker->can("start_renewal")
$skeid->has_key_broker $skeid->key_broker->can("start_renewal")
Truth table66%2/3
A and B
A= $skeid->has_key_brokerB= $skeid->key_broker->can("start_renewal")
ABresult
0X0
100
111
73 0 $skeid->key_broker->start_renewal;
74 }
75  
76 # Capacity probes (ADR 0009). Held by the app, because a probe that goes out of scope stops
77 # polling. Nodes with no capacity block get none, which is plain inflight admission.
78 512 my $probes = Langertha::Skeid::CapacityProbe->start_for_skeid($skeid);
79 512 my $probe_key = $skeid->_probe_inventory_key;
80  
81 # The customer source's fetcher (ADR 0026): on the loop, never on the request path. Its
82 # connections open with the first fetch, in the process that runs the loop.
83 512 $skeid->start_customer_source;
84  
85 512 my $app = Mojolicious->new;
86 512 $app->secrets(['skeid-proxy']);
87  
88 # A write-behind usage store (usage_store.flush_interval_ms) writes after the request was
89 # answered, so its failures cannot come back through _record_usage_event; they come here and
90 # get the same log line (skeid k78). Weak: the app holds the skeid, the skeid holds this.
91 512 weaken(my $weak_app = $app);
92 1 ▶ $skeid->on_usage_lost(sub {
Subroutine __ANON__: called CC=4 SCAR=22.5
93 1 my ($skeid, $event, $err) = @_;
94 1 ▶ return _log_lost_usage_event($weak_app, $skeid, $event, $err) if $weak_app;
Branch: 1 branch
BranchTrueFalseTotal
if $weak_app 1 0 1
95 0 ▶ warn 'skeid: usage event lost: request_id=' . ($event->{request_id} // '') . ': '
Condition0%0/4
$event->{"request_id"} // ""
l!l
00
$err // "unknown error"
l!l
00
MC/DC0%0/1
$event->{"request_id"} // ""
$event->{"request_id"}
MC/DC0%0/1
$err // "unknown error"
$err
Truth table0%0/2
A
A= $event->{"request_id"}
Aresult
11
00
Truth table0%0/2
A
A= $err
Aresult
11
00
96 . ($err // 'unknown error') . "\n";
97 0 return;
98 512 });
99 512 $app->ua->connect_timeout(10);
100 # One number for how long an upstream may take and how long it may be silent: a model that
101 # thinks sends nothing until its first token, and Mojo::UserAgent closes a connection that
102 # was silent for 40s whatever the request timeout allows. The client's side follows the same
103 # number per request, see _extend_client_timeout.
104 my $upstream_timeout
105 = (defined($ENV{SKEID_UPSTREAM_TIMEOUT}) && $ENV{SKEID_UPSTREAM_TIMEOUT} =~ /^\d+$/
106 && $ENV{SKEID_UPSTREAM_TIMEOUT} > 0)
107 ? 0 + $ENV{SKEID_UPSTREAM_TIMEOUT}
108 512 ▶ : 300;
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'SKEID_UPSTREAM_TIMEOUT'} && $ENV{'SKEID_UPSTREAM_TIMEOUT'} =~ /^\d+$/ && $ENV{'SKEID_UPSTREAM_TIMEOUT'} > 0 ? : 1 511 512
Condition100%6/6
defined $ENV{"SKEID_UPSTREAM_TIMEOUT"} and $ENV{"SKEID_UPSTREAM_TIMEOUT"} =~ /^\d+$/ and $ENV{"SKEID_UPSTREAM_TIMEOUT"} > 0
!ll&&!rl&&r
51011
defined $ENV{"SKEID_UPSTREAM_TIMEOUT"} and $ENV{"SKEID_UPSTREAM_TIMEOUT"} =~ /^\d+$/
!ll&&!rl&&r
50642
MC/DC100%3/3
defined $ENV{"SKEID_UPSTREAM_TIMEOUT"} and $ENV{"SKEID_UPSTREAM_TIMEOUT"} =~ /^\d+$/ and $ENV{"SKEID_UPSTREAM_TIMEOUT"} > 0
defined $ENV{"SKEID_UPSTREAM_TIMEOUT"} $ENV{"SKEID_UPSTREAM_TIMEOUT"} =~ /^\d+$/ $ENV{"SKEID_UPSTREAM_TIMEOUT"} > 0
Truth table100%4/4
A and B and C
A= defined $ENV{"SKEID_UPSTREAM_TIMEOUT"}B= $ENV{"SKEID_UPSTREAM_TIMEOUT"} =~ /^\d+$/C= $ENV{"SKEID_UPSTREAM_TIMEOUT"} > 0
ABCresult
0XX0
10X0
1100
1111
109 512 $app->ua->request_timeout($upstream_timeout);
110 512 $app->ua->inactivity_timeout($upstream_timeout);
111 # Mojo::UserAgent pools 5 upstream connections by default. A proxy serving more concurrent
112 # requests than that reconnects for the surplus on every request, which shows up as latency
113 # that grows with concurrency for no visible reason. Sized for the concurrency a single
114 # Skeid process can actually sustain, not for the number of nodes.
115 $app->ua->max_connections(
116 (defined($ENV{SKEID_UPSTREAM_POOL}) && $ENV{SKEID_UPSTREAM_POOL} =~ /^\d+$/)
117 ? 0 + $ENV{SKEID_UPSTREAM_POOL}
118 512 ▶ : 100
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'SKEID_UPSTREAM_POOL'} && $ENV{'SKEID_UPSTREAM_POOL'} =~ /^\d+$/ ? : 0 512 512
Condition33%1/3
defined $ENV{"SKEID_UPSTREAM_POOL"} and $ENV{"SKEID_UPSTREAM_POOL"} =~ /^\d+$/
!ll&&!rl&&r
51200
MC/DC0%0/2
defined $ENV{"SKEID_UPSTREAM_POOL"} and $ENV{"SKEID_UPSTREAM_POOL"} =~ /^\d+$/
defined $ENV{"SKEID_UPSTREAM_POOL"} $ENV{"SKEID_UPSTREAM_POOL"} =~ /^\d+$/
Truth table33%1/3
A and B
A= defined $ENV{"SKEID_UPSTREAM_POOL"}B= $ENV{"SKEID_UPSTREAM_POOL"} =~ /^\d+$/
ABresult
0X0
100
111
119 );
120 512 ▶ $app->helper(skeid => sub { $skeid });
Subroutine __ANON__: called CC=1 SCAR=0.0
121 # The open realtime sessions of this app (skeid k95, ADR 0029): what a graceful stop closes.
122 512 my $realtime = Langertha::Skeid::Proxy::Realtime->new(skeid => $skeid, log => $app->log);
123 512 ▶ $app->helper(realtime => sub { $realtime });
Subroutine __ANON__: called CC=1 SCAR=0.0
124  
125 # A config reload replaces the whole inventory, so probes have to follow it or they keep
126 # polling for nodes that are gone and never start for new ones. They follow the probe key,
127 # not the inventory generation: a health flip moves the generation but not what a probe
128 # polls, and a restart makes every probe forget its own reading (skeid #40) -- readings from
129 # other sources, such as a rate-limit backoff, survive it. The key is recomputed only when the
130 # generation has moved, so an unchanged inventory costs an integer compare per request.
131 2255 ▶ $app->hook(before_dispatch => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
132 2255 my $key = $skeid->_probe_inventory_key;
133 2255 ▶ return if $key eq $probe_key;
Branch: 1 branch
BranchTrueFalseTotal
if $key eq $probe_key 2250 5 2255
134 5 $probe_key = $key;
135 5 $_->stop for values %$probes;
136 5 $probes = Langertha::Skeid::CapacityProbe->start_for_skeid($skeid);
137 512 });
138  
139 # The request's id is fixed here, before anything can fail or hang up: the client gets it back
140 # as x-request-id whatever the answer turns out to be, and the usage event and the lost-event
141 # log line carry the same one. It lives in the stash because a request whose client is gone
142 # has no transaction to read it from any more.
143 2255 ▶ $app->hook(before_dispatch => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
144 2255 my ($c) = @_;
145 2255 my $id = _request_id($c);
146 2255 $c->stash('skeid.request_id' => $id);
147 2255 $c->res->headers->header('x-request-id' => $id);
148 512 });
149  
150 # The upload routes get their own request limit (uploads.max_bytes, skeid k91), set the moment
151 # the request's head is parsed and before any of its body is: see _limit_upload. The
152 # transaction is held weakly -- the listener sits on its own request content, and a connection
153 # that closes before the head is complete would otherwise keep both alive.
154 2332 ▶ $app->hook(after_build_tx => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
155 2332 my ($tx, $app) = @_;
156 2332 weaken $tx;
157 2332 ▶ $tx->req->content->once(body => sub { _limit_upload($app, $tx) if $tx });
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if $tx 2220 0 2220
158 512 });
159  
160 # Swagger UI assets: fetched on the first request for the UI, only when the config switches
161 # it on (skeid k105, ADR 0025). Nothing is fetched at build. The object is injectable (the
162 # swagger_ui option of build_app) so a test never reaches the network.
163 512 ▶ my $swagger_ui = $opts{swagger_ui} || Langertha::Skeid::SwaggerUI->new;
Condition66%2/3
$opts{'swagger_ui'} || 'Langertha::Skeid::SwaggerUI'->new
l!l&&r!l&&!r
105020
MC/DC0%0/2
$opts{'swagger_ui'} || 'Langertha::Skeid::SwaggerUI'->new
$opts{'swagger_ui'} 'Langertha::Skeid::SwaggerUI'->new
Truth table66%2/3
A || B
A= $opts{'swagger_ui'}B= 'Langertha::Skeid::SwaggerUI'->new
ABresult
1X1
011
000
164 512 ▶ $app->helper(swagger_ui => sub { $swagger_ui });
Subroutine __ANON__: called CC=1 SCAR=0.0
165  
166 # Every route is registered from route_table, and the OpenAPI documents are built from the
167 # same entries: a route without an entry cannot exist, which t/78-route-table.t proves by
168 # walking the router. A group names the bridge the entry sits behind. Bridges are made when
169 # the first entry of their group comes, so the order of the table is the order of matching.
170 #
171 # Client routes sit behind _authorize_client (skeid k90): a bridge, so only these routes are
172 # gated and an unknown path stays a 404. Each face's bridge sets what its 401 needs before the
173 # gate can render it -- the error dialect, the manifest's cache headers.
174 512 my $r = $app->routes;
175 my %make_bridge = (
176 512 ▶ root => sub { $r },
Subroutine __ANON__: called CC=1 SCAR=0.0
177 512 ▶ manifest => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
178 32 ▶ $r->under(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
179 32 my ($c) = @_;
180 32 _manifest_cache_headers($c);
181 32 return _authorize_client($c);
182 512 });
183 },
184 512 ▶ openai => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
185 512 ▶ $r->under(sub { _authorize_client($_[0]) });
Subroutine __ANON__: called CC=1 SCAR=0.0
186 },
187 # Every error this request produces, wherever it is rendered -- the gate's 401 included --
188 # has to be Anthropic-shaped (core karr #224). _render_error reads this.
189 512 ▶ anthropic => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
190 206 ▶ $r->under(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
191 206 my ($c) = @_;
192 206 $c->stash('skeid.error_format' => 'anthropic');
193 206 return _authorize_client($c);
194 512 });
195 },
196 # Every error on these routes has to be Ollama-shaped, {"error": "<string>"}: an Ollama
197 # client decodes the error as a string and fails on the OpenAI object (skeid #47).
198 512 ▶ ollama => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
199 297 ▶ $r->under(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
200 297 my ($c) = @_;
201 297 $c->stash('skeid.error_format' => 'ollama');
202 297 return _authorize_client($c);
203 512 });
204 },
205 # The MCP bridge answers as an unknown path while the config has no mcp: section, before
206 # the client gate can turn that 404 into a 401.
207 512 ▶ mcp => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
208 512 ▶ $r->under(sub { _authorize_mcp_client($_[0]) });
Subroutine __ANON__: called CC=1 SCAR=0.0
209 },
210 512 ▶ admin => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
211 512 ▶ $r->under(sub { _authorize_admin($_[0]) });
Subroutine __ANON__: called CC=1 SCAR=0.0
212 },
213 512 );
214 512 my %bridge;
215 512 for my $entry ($class->route_table) {
216 my $make = $make_bridge{$entry->{group}}
217 15360 ▶ or croak "route table: unknown group '$entry->{group}' of $entry->{id}";
Branch: 1 branch
BranchTrueFalseTotal
my $make = $make_bridge{$entry->{"group"}} or croak(qq[route table: unknown group '$entry->{"group"}' of $entry->{"id"}]) 15360 0 15360
218 15360 ▶ my $parent = $bridge{$entry->{group}} //= $make->();
Condition66%2/3
$bridge{$entry->{"group"}} //= &$make()
l!l&&r!l&&!r
1177635840
MC/DC0%0/2
$bridge{$entry->{"group"}} //= &$make()
$bridge{$entry->{"group"}} &$make()
Truth table66%2/3
A //= B
A= $bridge{$entry->{"group"}}B= &$make()
ABresult
1X1
011
000
219 # A WebSocket route matches only an upgrade request (ADR 0029); everything else is as for
220 # any route: behind its group's bridge, so the client gate answers before an upgrade.
221 15360 ▶ if ($entry->{websocket}) { $parent->websocket($_ => $entry->{handler}) for @{$entry->{paths}} }
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->{'websocket'}) { } 512 14848 15360
222 14848 else { $parent->any($entry->{methods} => $_ => $entry->{handler}) for @{$entry->{paths}} }
223 }
224  
225 512 return $app;
226 }
227  
228  
229 86 my @ROUTE_TABLE;
230  
231 530 ▶sub route_table {
Subroutine route_table: called CC=2 SCAR=6.9
Pod documented
232 530 my ($class) = @_;
233 530 ▶ @ROUTE_TABLE = $class->_build_route_table unless @ROUTE_TABLE;
Branch: 1 branch
BranchTrueFalseTotal
unless @ROUTE_TABLE 65 465 530
234 530 return @ROUTE_TABLE;
235 }
236  
237 65 ▶sub _build_route_table {
Subroutine _build_route_table: called CC=8 SCAR=21.7
238 65 my ($class) = @_;
239 65 my @t;
240 65 ▶ my $add = sub { push @t, { methods => ['GET'], document => 'client', @_ } };
Subroutine __ANON__: called CC=1 SCAR=0.0
241  
242 # Still 'ok' while a config reload is failing: the proxy serves under the config it kept, so
243 # it is not unhealthy, and a probe that restarted it would lose that config. The reload
244 # state is shown without its message, which can name customers; that is on /skeid/config.
245 $add->(id => 'health', group => 'root', paths => ['/health'], tag => 'Service', public => 1,
246 summary => 'Liveness and config reload state',
247 53 ▶ handler => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
248 53 my ($c) = @_;
249 53 my $reload = $c->skeid->reload_status;
250 53 delete $reload->{error};
251 # The customer source likewise: whether it fetches, never why not (ADR 0026).
252 53 my $source = $c->skeid->customer_source_status;
253 $source &&= {
254 ok => $source->{ok},
255 53 ▶ ($source->{failures} ? (failed_at => $source->{failed_at}, failures => $source->{failures}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$source->{'failures'} ? : 1 2 3
Condition100%2/2
$source &&= {"ok", $source->{"ok"}, $source->{"failures"} ? ("failed_at", $source->{"failed_at"}, "failures", $source->{"failures"}) : ()}
!ll
503
MC/DC100%1/1
$source &&= {"ok", $source->{"ok"}, $source->{"failures"} ? ("failed_at", $source->{"failed_at"}, "failures", $source->{"failures"}) : ()}
$source
Truth table100%2/2
A
A= $source
Aresult
00
11
256 };
257 53 ▶ $c->render(json => { status => 'ok', proxy => 'skeid', config_reload => $reload,
Branch: 1 branch
BranchTrueFalseTotal
$source ? : 3 50 53
258 ($source ? (customer_source => $source) : ()) });
259 65 });
260  
261 # Provider manifest (skeid #29, ADR 0015): per customer key, never the whole catalog.
262 $add->(id => 'manifest', group => 'manifest', paths => ['/.well-known/langertha.json'],
263 tag => 'Manifest', requires => 'manifest',
264 summary => 'Provider manifest for the presented key',
265 65 ▶ handler => sub { _handle_manifest($_[0]) });
Subroutine __ANON__: called CC=1 SCAR=0.0
266  
267 # OpenAI format
268 $add->(id => 'models', group => 'openai', paths => ['/v1/models'], tag => 'OpenAI',
269 summary => 'List the models this key may use', response => 'ModelList',
270 25 ▶ handler => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
271 25 my ($c) = @_;
272 my @data = map {
273 +{
274 id => $_->{model},
275 31 object => 'model',
276 created => int(time),
277 owned_by => 'skeid',
278 }
279 25 } @{$c->skeid->list_models(api_key_id => _request_api_key_id($c))};
280 25 $c->render(json => { object => 'list', data => \@data });
281 65 });
282  
283 $add->(id => 'chat_completions', group => 'openai', methods => ['POST'],
284 paths => ['/v1/chat/completions'], tag => 'OpenAI', request => 'ChatCompletionRequest',
285 stream => 1, summary => 'Chat completion',
286 65 ▶ handler => sub { _handle_openai_chat($_[0]) });
Subroutine __ANON__: called CC=1 SCAR=0.0
287  
288 $add->(id => 'embeddings', group => 'openai', methods => ['POST'], paths => ['/v1/embeddings'],
289 tag => 'OpenAI', request => 'EmbeddingsRequest', summary => 'Embeddings',
290 65 ▶ handler => sub { _handle_openai_embeddings($_[0]) });
Subroutine __ANON__: called CC=1 SCAR=0.0
291  
292 # Audio (skeid k91): relayed in the upstream's own shape, not translated (ADR 0021).
293 65 for my $endpoint (Langertha::Skeid::Protocol::Audio->routes) {
294 130 (my $id = $endpoint) =~ s{[^A-Za-z0-9]+}{_}g;
295 $add->(id => 'audio' . $id, group => 'openai', methods => ['POST'], paths => [$endpoint],
296 tag => 'OpenAI', request => 'AudioRequest', request_type => 'multipart', stream => 1,
297 summary => 'Audio ' . ($endpoint =~ /translations/ ? 'translation' : 'transcription'),
298 description => 'Relayed to the node as the multipart form was sent; the answer is the node\'s own.',
299 130 ▶ handler => sub { _handle_openai_audio($_[0], $endpoint) });
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$endpoint =~ /translations/ ? : 65 65 130
300 }
301  
302 # Rerank (skeid k92): /v1/rerank and its alias /rerank, one route. Relayed like audio, except
303 # to a node whose rerank_format names another upstream dialect.
304 65 for my $endpoint (Langertha::Skeid::Protocol::Rerank->routes) {
305 130 (my $id = $endpoint) =~ s{[^A-Za-z0-9]+}{_}g;
306 $add->(id => 'rerank' . $id, group => 'openai', methods => ['POST'], paths => [$endpoint],
307 tag => 'OpenAI', request => 'RerankRequest', summary => 'Rerank documents',
308 130 ▶ handler => sub { _handle_openai_rerank($_[0]) });
Subroutine __ANON__: called CC=1 SCAR=0.0
309 }
310  
311 # Documents (skeid k94, ADR 0028): docling-serve's synchronous conversion, under its own paths.
312 # Relayed like audio; planned over the nodes of kind `documents`, not by a model name.
313 65 for my $endpoint (Langertha::Skeid::Protocol::Documents->routes) {
314 130 my $file = $endpoint =~ /file\z/;
315 130 (my $id = $endpoint) =~ s{[^A-Za-z0-9]+}{_}g;
316 $add->(id => 'documents' . $id, group => 'openai', methods => ['POST'], paths => [$endpoint],
317 tag => 'Documents', summary => 'Convert ' . ($file ? 'uploaded files' : 'sources') . ' to a document',
318 request => ($file ? 'DocumentsFileRequest' : 'DocumentsSourceRequest'),
319 ($file ? (request_type => 'multipart') : ()),
320 description => 'docling-serve\'s own call, relayed to a node of kind documents; the answer '
321 . '(JSON, or a zip) is the node\'s. Authorised under the model name documents.',
322 130 ▶ handler => sub { _handle_documents($_[0], $endpoint) });
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 3 branches
BranchTrueFalseTotal
$file ? : 65 65 130
$file ? : 65 65 130
$file ? : 65 65 130
323 }
324  
325 # Realtime transcription (skeid k95, ADR 0029): a WebSocket, relayed as a session. Not a
326 # request/answer route, so it has its own handler and not a $relay of the proxy functions.
327 $add->(id => 'realtime', group => 'openai', paths => [Langertha::Skeid::Protocol::Realtime->route],
328 tag => 'OpenAI', websocket => 1,
329 query => [
330 { name => 'model', required => 1, description => 'The model of the session; the key\'s policy applies to it.' },
331 { name => 'intent', description => 'Passed to the node, as sent (transcription).' },
332 ],
333 summary => 'Realtime transcription (WebSocket)',
334 description => 'Upgrades to a WebSocket relayed to the node\'s realtime endpoint; the events of '
335 . 'both directions are the node\'s own, nothing is translated. A key can also be sent in the '
336 . '"openai-insecure-api-key.<key>" subprotocol, beside "realtime". One session holds one slot '
337 . 'of its node until it closes, and is recorded as one usage event.',
338 65 ▶ handler => sub { _handle_openai_realtime($_[0]) });
Subroutine __ANON__: called CC=1 SCAR=0.0
339  
340 # The description of the client routes themselves. Behind the client gate like /v1/models:
341 # the examples carry model names, and those are per key.
342 $add->(id => 'openapi', group => 'openai', paths => ['/openapi.json'], tag => 'Service',
343 summary => 'This OpenAPI 3.1 document',
344 65 ▶ handler => sub { _handle_openapi($_[0], 'client') });
Subroutine __ANON__: called CC=1 SCAR=0.0
345  
346 # Anthropic format.
347 $add->(id => 'messages', group => 'anthropic', methods => ['POST'], paths => ['/v1/messages'],
348 tag => 'Anthropic', request => 'AnthropicMessagesRequest', stream => 1,
349 summary => 'Anthropic Messages',
350 65 ▶ handler => sub { _handle_anthropic_messages($_[0]) });
Subroutine __ANON__: called CC=1 SCAR=0.0
351  
352 # Ollama format.
353 $add->(id => 'ollama_chat', group => 'ollama', methods => ['POST'], paths => ['/api/chat'],
354 tag => 'Ollama', request => 'OllamaChatRequest', stream => 1, summary => 'Ollama chat',
355 65 ▶ handler => sub { _handle_ollama($_[0], 'chat') });
Subroutine __ANON__: called CC=1 SCAR=0.0
356  
357 $add->(id => 'ollama_generate', group => 'ollama', methods => ['POST'], paths => ['/api/generate'],
358 tag => 'Ollama', request => 'OllamaGenerateRequest', stream => 1, summary => 'Ollama generate',
359 65 ▶ handler => sub { _handle_ollama($_[0], 'generate') });
Subroutine __ANON__: called CC=1 SCAR=0.0
360  
361 $add->(id => 'ollama_tags', group => 'ollama', paths => ['/api/tags'], tag => 'Ollama',
362 summary => 'The models this key may use, Ollama shaped',
363 13 ▶ handler => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
364 13 my ($c) = @_;
365 13 $c->render(json => Langertha::Skeid::Protocol::Ollama->tags_from_models(
366 $c->skeid->list_models(api_key_id => _request_api_key_id($c))));
367 65 });
368  
369 $add->(id => 'ollama_ps', group => 'ollama', paths => ['/api/ps'], tag => 'Ollama',
370 summary => 'Always an empty list',
371 65 ▶ handler => sub { $_[0]->render(json => { models => [] }) });
Subroutine __ANON__: called CC=1 SCAR=0.0
372  
373 # MCP gateway (skeid k103, ADR 0022): the MCP servers assigned to the presented key, listed
374 # and relayed. The relaxed placeholder takes a name with a dot in it; what follows the name
375 # is the sub-path, for an upstream with more than one endpoint.
376 $add->(id => 'mcp_servers', group => 'mcp', paths => [Langertha::Skeid::Protocol::MCP->listing_path],
377 tag => 'MCP', requires => 'mcp', summary => 'The MCP servers assigned to this key',
378 65 ▶ handler => sub { _handle_mcp_servers($_[0]) });
Subroutine __ANON__: called CC=1 SCAR=0.0
379  
380 $add->(id => 'mcp_relay', group => 'mcp', methods => [qw( GET POST DELETE )],
381 paths => ['/mcp/#mcp_server', '/mcp/#mcp_server/*mcp_path'], tag => 'MCP', requires => 'mcp',
382 summary => 'Relay MCP Streamable HTTP to an assigned MCP server', stream => 1,
383 65 ▶ handler => sub { _handle_mcp($_[0]) });
Subroutine __ANON__: called CC=1 SCAR=0.0
384  
385 # Skeid-to-Skeid registry (skeid #18, ADR 0017). Registered before the /skeid admin block so
386 # it is not behind _authorize_admin: it also takes the registry read key (skeid #49), which
387 # that block must never accept.
388 $add->(id => 'registry_snapshot', group => 'root', paths => ['/skeid/registry/snapshot'],
389 document => 'admin', requires => 'registry', tag => 'Registry',
390 summary => 'Signed capacity snapshot, for a fronting Skeid',
391 description => 'Takes the admin key or the registry read key. The body is signed in '
392 . 'X-Skeid-Registry-Signature.',
393 65 ▶ handler => sub { _handle_registry_snapshot($_[0]) });
Subroutine __ANON__: called CC=1 SCAR=0.0
394  
395 # Lightweight admin API for live control-plane updates.
396 $add->(id => 'admin_nodes', group => 'admin', paths => ['/skeid/nodes'], document => 'admin',
397 tag => 'Admin', summary => 'List nodes', response => 'NodeList',
398 60 ▶ handler => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
399 60 my ($c) = @_;
400 60 $c->render(json => { nodes => $c->skeid->list_nodes });
401 65 });
402  
403 $add->(id => 'admin_nodes_add', group => 'admin', methods => ['POST'], paths => ['/skeid/nodes'],
404 document => 'admin', tag => 'Admin', summary => 'Add or replace a node',
405 1 ▶ handler => sub {
Subroutine __ANON__: called CC=5 SCAR=17.6
406 1 my ($c) = @_;
407 1 ▶ my $body = $c->req->json || {};
Condition50%1/2
$c->req->json || {}
l!l
10
MC/DC0%0/1
$c->req->json || {}
$c->req->json
Truth table50%1/2
A
A= $c->req->json
Aresult
11
00
408 1 my $ok = eval { $c->skeid->call_function('nodes.add', $body)->{ok} };
409 1 ▶ if (!$ok || $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ok or $@) 1 0 1
Condition33%1/3
not $ok or $@
l!l&&r!l&&!r
100
MC/DC0%0/2
not $ok or $@
not $ok $@
Truth table33%1/3
not(A) or B
A= not $okB= $@
ABresult
1X1
011
000
410 1 ▶ my $msg = $@ ? "$@" : 'invalid node payload';
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 1 0 1
411 1 $msg =~ s/\s+$//;
412 1 $c->render(json => { error => { message => $msg, type => 'invalid_request_error' } }, status => 400);
413 1 return;
414 }
415 0 $c->render(json => { ok => 1, nodes => $c->skeid->list_nodes });
416 65 });
417  
418 $add->(id => 'admin_node_health', group => 'admin', methods => ['POST'],
419 paths => ['/skeid/nodes/:id/health'], document => 'admin', tag => 'Admin',
420 summary => 'Set a node healthy or unhealthy',
421 16 ▶ handler => sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
422 16 my ($c) = @_;
423 16 ▶ my $body = $c->req->json || {};
Condition50%1/2
$c->req->json || {}
l!l
160
MC/DC0%0/1
$c->req->json || {}
$c->req->json
Truth table50%1/2
A
A= $c->req->json
Aresult
11
00
424 16 my $id = $c->param('id');
425 my $ok = $c->skeid->call_function('nodes.set_health', {
426 id => $id,
427 healthy => ($body->{healthy} ? 1 : 0),
428 16 ▶ })->{ok};
Branch: 1 branch
BranchTrueFalseTotal
$body->{'healthy'} ? : 6 10 16
429 16 ▶ $c->render(json => { ok => $ok ? 1 : 0 });
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 16 0 16
430 65 });
431  
432 $add->(id => 'admin_config', group => 'admin', paths => ['/skeid/config'], document => 'admin',
433 tag => 'Admin', summary => 'Config reload status',
434 9 ▶ handler => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
435 9 my ($c) = @_;
436 9 my $reload = $c->skeid->call_function('config.status', {});
437 9 my $source = $c->skeid->customer_source_status;
438 9 ▶ $c->render(json => {
Branch: 1 branch
BranchTrueFalseTotal
$source ? : 7 2 9
439 reload => $reload,
440 ($source ? (
441 customer_source => $source,
442 customer_names => $c->skeid->customer_names,
443 customer_report => $c->skeid->customer_report,
444 ) : ()),
445 });
446 65 });
447  
448 $add->(id => 'admin_metrics_nodes', group => 'admin', paths => ['/skeid/metrics/nodes'],
449 document => 'admin', tag => 'Admin', summary => 'Per-node counters',
450 0 ▶ handler => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
451 0 my ($c) = @_;
452 0 $c->render(json => { metrics => $c->skeid->node_metrics });
453 65 });
454  
455 $add->(id => 'admin_usage', group => 'admin', paths => ['/skeid/usage'], document => 'admin',
456 tag => 'Admin', summary => 'Usage report',
457 0 ▶ handler => sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
458 0 my ($c) = @_;
459 0 ▶ my $report = $c->skeid->call_function('usage.report', {
Branch: 3 branches
BranchTrueFalseTotal
defined $c->param('since') && length $c->param('since') ? : 0 0 0
defined $c->param('api_key_id') && length $c->param('api_key_id') ? : 0 0 0
defined $c->param('model') && length $c->param('model') ? : 0 0 0
Condition0%0/11
defined $c->param("since") and length $c->param("since")
!ll&&!rl&&r
000
defined $c->param("api_key_id") and length $c->param("api_key_id")
!ll&&!rl&&r
000
defined $c->param("model") and length $c->param("model")
!ll&&!rl&&r
000
$c->param("limit") // 50
l!l
00
MC/DC0%0/2
defined $c->param("since") and length $c->param("since")
defined $c->param("since") length $c->param("since")
MC/DC0%0/2
defined $c->param("api_key_id") and length $c->param("api_key_id")
defined $c->param("api_key_id") length $c->param("api_key_id")
MC/DC0%0/2
defined $c->param("model") and length $c->param("model")
defined $c->param("model") length $c->param("model")
MC/DC0%0/1
$c->param("limit") // 50
$c->param("limit")
Truth table0%0/3
A and B
A= defined $c->param("since")B= length $c->param("since")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $c->param("api_key_id")B= length $c->param("api_key_id")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $c->param("model")B= length $c->param("model")
ABresult
0X0
100
111
Truth table0%0/2
A
A= $c->param("limit")
Aresult
11
00
460 (defined($c->param('since')) && length($c->param('since')) ? (since => $c->param('since')) : ()),
461 (defined($c->param('api_key_id')) && length($c->param('api_key_id')) ? (api_key_id => $c->param('api_key_id')) : ()),
462 (defined($c->param('model')) && length($c->param('model')) ? (model => $c->param('model')) : ()),
463 limit => ($c->param('limit') // 50),
464 });
465 0 ▶ my $status = ($report->{ok} ? 200 : 400);
Branch: 1 branch
BranchTrueFalseTotal
$report->{'ok'} ? : 0 0 0
466 0 $c->render(status => $status, json => $report);
467 65 });
468  
469 $add->(id => 'admin_openapi', group => 'admin', paths => ['/skeid/openapi.json'],
470 document => 'admin', tag => 'Admin', summary => 'The OpenAPI 3.1 document of the admin routes',
471 65 ▶ handler => sub { _handle_openapi($_[0], 'admin') });
Subroutine __ANON__: called CC=1 SCAR=0.0
472  
473 # The Swagger UI (skeid k105): off unless openapi.swagger_ui, an unknown path then. No
474 # document lists it; it carries no data, so it is outside the client gate -- the browser
475 # that loads it has no key yet. The description it shows is behind the gate.
476 $add->(id => 'docs', group => 'root', paths => ['/docs'], document => undef,
477 65 ▶ handler => sub { _handle_docs($_[0]) });
Subroutine __ANON__: called CC=1 SCAR=0.0
478 $add->(id => 'docs_file', group => 'root', paths => ['/docs/#file'], document => undef,
479 65 ▶ handler => sub { _handle_docs($_[0]) });
Subroutine __ANON__: called CC=1 SCAR=0.0
480  
481 65 return @t;
482 }
483  
484 # Whether the config has the section an entry's route needs.
485 192 ▶sub _route_enabled {
Subroutine _route_enabled: called CC=9 SCAR=22.1
486 192 my ($skeid, $entry) = @_;
487 192 my $need = $entry->{requires};
488 192 ▶ return 1 unless defined $need;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $need 164 28 192
489 28 ▶ return $skeid->manifest_enabled && $skeid->manifest_available ? 1 : 0 if $need eq 'manifest';
Branch: 2 branches
BranchTrueFalseTotal
if $need eq "manifest" 8 20 28
$skeid->manifest_enabled && $skeid->manifest_available ? : 1 7 8
Condition66%2/3
$skeid->manifest_enabled and $skeid->manifest_available
!ll&&!rl&&r
701
MC/DC50%1/2
$skeid->manifest_enabled and $skeid->manifest_available
$skeid->manifest_enabled $skeid->manifest_available
Truth table66%2/3
A and B
A= $skeid->manifest_enabledB= $skeid->manifest_available
ABresult
0X0
100
111
490 20 ▶ return $skeid->mcp_enabled ? 1 : 0 if $need eq 'mcp';
Branch: 2 branches
BranchTrueFalseTotal
if $need eq "mcp" 16 4 20
$skeid->mcp_enabled ? : 4 12 16
491 4 ▶ return $skeid->registry_enabled ? 1 : 0 if $need eq 'registry';
Branch: 2 branches
BranchTrueFalseTotal
if $need eq "registry" 4 0 4
$skeid->registry_enabled ? : 2 2 4
492 0 return 0;
493 }
494  
495 # GET /openapi.json and GET /skeid/openapi.json: the document of the routes the config enables
496 # (ADR 0025). The model names in the examples are the ones /v1/models lists for this caller.
497 12 ▶sub _handle_openapi {
Subroutine _handle_openapi: called CC=6 SCAR=17.9
498 12 my ($c, $kind) = @_;
499 12 my $skeid = $c->skeid;
500 12 ▶ my @entries = grep { ($_->{document} // '') eq $kind && _route_enabled($skeid, $_) }
Condition100%5/5
($_->{'document'} // '') eq $kind && _route_enabled($skeid, $_)
!ll&&!rl&&r
16821171
$_->{"document"} // ""
l!l
33624
MC/DC100%2/2
($_->{'document'} // '') eq $kind && _route_enabled($skeid, $_)
$_->{"document"} _route_enabled($skeid, $_)
Truth table100%3/3
A && B
A= $_->{"document"}B= _route_enabled($skeid, $_)
ABresult
0X0
100
111
501 Langertha::Skeid::Proxy->route_table;
502 my @models = $kind eq 'client'
503 12 ▶ ? map { $_->{model} } @{$skeid->list_models(api_key_id => _request_api_key_id($c))}
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'client' ? : 8 4 12
504 : ();
505 12 ▶ _manifest_cache_headers($c) if $kind eq 'client';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "client" 8 4 12
506 12 ▶ $c->res->headers->header('Cache-Control' => 'no-store') if $kind eq 'admin';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "admin" 4 8 12
507 12 $c->render(json => Langertha::Skeid::OpenAPI->document(
508 kind => $kind,
509 entries => \@entries,
510 models => \@models,
511 client_auth => $skeid->client_auth_enabled,
512 version => $Langertha::Skeid::Proxy::VERSION,
513 ));
514 }
515  
516 # /docs and /docs/<file>: the Swagger UI, from the cache directory (skeid k105). Unknown path
517 # unless the config switches it on; the assets are fetched on the first request that needs them,
518 # without blocking the loop. A fetch that fails answers 502, and 503 while the failure is
519 # remembered -- the client is told, never left waiting, and no request starts a second download
520 # within the window.
521 25 ▶sub _handle_docs {
Subroutine _handle_docs: called CC=7 SCAR=19.5
522 25 my ($c) = @_;
523 25 my $skeid = $c->skeid;
524 25 $skeid->maybe_reload_config;
525 25 ▶ return $c->render(status => 404, text => 'Not Found') unless $skeid->openapi_swagger_ui;
Branch: 1 branch
BranchTrueFalseTotal
unless $skeid->openapi_swagger_ui 4 21 25
526  
527 21 my $ui = $c->app->swagger_ui;
528 21 my $file = $c->stash('file');
529 21 ▶ if (defined $file && $file eq 'init.js') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file and $file eq "init.js") 1 20 21
Condition100%3/3
defined $file and $file eq "init.js"
!ll&&!rl&&r
1551
MC/DC100%2/2
defined $file and $file eq "init.js"
defined $file $file eq "init.js"
Truth table100%3/3
A and B
A= defined $fileB= $file eq "init.js"
ABresult
0X0
100
111
530 1 $c->res->headers->content_type('application/javascript; charset=utf-8');
531 1 return $c->render(data => $ui->init_js);
532 }
533 20 ▶ return $c->render(status => 404, text => 'Not Found') if defined($file) && !$ui->is_asset($file);
Branch: 1 branch
BranchTrueFalseTotal
if defined $file and not $ui->is_asset($file) 2 18 20
Condition100%3/3
defined $file and not $ui->is_asset($file)
!ll&&!rl&&r
1532
MC/DC100%2/2
defined $file and not $ui->is_asset($file)
defined $file not $ui->is_asset($file)
Truth table100%3/3
A and not(B)
A= defined $fileB= not $ui->is_asset($file)
ABresult
0X0
100
111
534  
535 18 ▶ my $dir = $skeid->openapi_cache_dir // $ui->default_cache_dir;
Condition33%1/3
$skeid->openapi_cache_dir // $ui->default_cache_dir
l!l&&r!l&&!r
1800
MC/DC0%0/2
$skeid->openapi_cache_dir // $ui->default_cache_dir
$skeid->openapi_cache_dir $ui->default_cache_dir
Truth table33%1/3
A // B
A= $skeid->openapi_cache_dirB= $ui->default_cache_dir
ABresult
1X1
011
000
536 18 $c->render_later;
537 18 ▶ $ui->ensure($dir, sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
538 18 my ($ok, $kind, $message) = @_;
539 18 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 7 11 18
540 # The message names the pinned URL and the check that failed, nothing of a response.
541 7 ▶ $c->app->log->error('swagger ui: ' . $message) if $kind eq 'fetch';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "fetch" 5 2 7
542 7 ▶ $c->res->headers->header('Retry-After' => $ui->retry_after) if $kind eq 'backoff';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "backoff" 2 5 7
543 7 ▶ return _render_error($c, ($kind eq 'backoff' ? 503 : 502),
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'backoff' ? : 2 5 7
544 'The Swagger UI assets could not be fetched', 'upstream_error', 'swagger_ui_unavailable');
545 }
546 11 ▶ unless (defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 9 2 11
547 9 $c->res->headers->content_type('text/html; charset=utf-8');
548 9 return $c->render(data => $ui->index_html);
549 }
550 2 $c->res->headers->cache_control('public, max-age=3600');
551 2 $c->reply->file($ui->asset_path($dir, $file));
552 18 });
553 18 return;
554 }
555  
556 # GET /skeid/registry/snapshot (skeid #18, ADR 0017): a fronting tier's CapacityProbe::Registry
557 # pulls this. 404 unless registry.enabled, and never unsigned. No store may keep it: a cached
558 # snapshot is a stale one.
559 27 ▶sub _handle_registry_snapshot {
Subroutine _handle_registry_snapshot: called CC=7 SCAR=19.7
560 27 my ($c) = @_;
561 27 ▶ return unless _authorize_registry_read($c);
Branch: 1 branch
BranchTrueFalseTotal
unless _authorize_registry_read($c) 4 23 27
562 23 my $skeid = $c->skeid;
563 23 $c->res->headers->header('Cache-Control' => 'no-store');
564 23 ▶ unless ($skeid->registry_enabled) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($skeid->registry_enabled) 3 20 23
565 3 $c->render(status => 404,
566 json => { error => { message => 'No registry snapshot is published here', type => 'not_found' } });
567 3 return;
568 }
569 20 my ($body, $signature) = eval { Langertha::Skeid::Registry->signed_snapshot($skeid) };
570 20 ▶ unless (defined $body) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $body) 1 19 20
571 1 ▶ my $err = $@ || 'unknown error';
Condition50%1/2
$@ || 'unknown error'
l!l
10
MC/DC0%0/1
$@ || 'unknown error'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
572 1 ▶ unless (length($skeid->registry_secret // '')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length($skeid->registry_secret // "")) 0 1 1
Condition50%1/2
$skeid->registry_secret // ""
l!l
10
MC/DC0%0/1
$skeid->registry_secret // ""
$skeid->registry_secret
Truth table50%1/2
A
A= $skeid->registry_secret
Aresult
11
00
573 0 $c->render(status => 503,
574 json => { error => { message => 'Registry secret is not set', type => 'unavailable' } });
575 0 return;
576 }
577 # Anything else is a bug in building the snapshot. The operator gets the cause in the
578 # log; the caller gets nothing that could describe this process's internals.
579 1 $err =~ s/\s+\z//;
580 1 $c->app->log->error("registry snapshot failed: $err");
581 1 $c->render(status => 500,
582 json => { error => { message => 'Registry snapshot could not be built', type => 'server_error' } });
583 1 return;
584 }
585 19 $c->res->headers->header(Langertha::Skeid::Registry->SIGNATURE_HEADER => $signature);
586 19 $c->render(data => $body, format => 'json');
587 }
588  
589 126 ▶sub _authorize_admin {
Subroutine _authorize_admin: called CC=8 SCAR=20.8
590 126 my ($c) = @_;
591 126 $c->skeid->maybe_reload_config;
592  
593 126 ▶ my $admin_api_key = $c->skeid->admin_api_key // '';
Condition50%1/2
$c->skeid->admin_api_key // ""
l!l
1260
MC/DC0%0/1
$c->skeid->admin_api_key // ""
$c->skeid->admin_api_key
Truth table50%1/2
A
A= $c->skeid->admin_api_key
Aresult
11
00
594 126 ▶ if (!length($admin_api_key)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $admin_api_key) 7 119 126
595 7 $c->render(status => 404, text => 'Not Found');
596 7 return undef;
597 }
598  
599 119 ▶ my $auth = $c->req->headers->authorization // '';
Condition100%2/2
$c->req->headers->authorization // ""
l!l
1127
MC/DC100%1/1
$c->req->headers->authorization // ""
$c->req->headers->authorization
Truth table100%2/2
A
A= $c->req->headers->authorization
Aresult
11
00
600 119 my ($scheme, $token) = $auth =~ /\A(\S+)\s+(.+)\z/;
601 119 ▶ my $ok = defined($scheme) && lc($scheme) eq 'bearer' && defined($token)
Condition88%8/9
defined $scheme && lc $scheme eq 'bearer' && defined $token && 'Langertha::Skeid::Secret'->equal($token, $admin_api_key)
!ll&&!rl&&r
101990
defined $scheme and lc $scheme eq "bearer" and defined $token
!ll&&!rl&&r
100109
defined $scheme and lc $scheme eq "bearer"
!ll&&!rl&&r
91109
MC/DC75%3/4
defined $scheme && lc $scheme eq 'bearer' && defined $token && 'Langertha::Skeid::Secret'->equal($token, $admin_api_key)
defined $scheme lc $scheme eq "bearer" defined $token 'Langertha::Skeid::Secret'->equal($token, $admin_api_key)
Truth table80%4/5
A and B and C && D
A= defined $schemeB= lc $scheme eq "bearer"C= defined $tokenD= 'Langertha::Skeid::Secret'->equal($token, $admin_api_key)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
602 && Langertha::Skeid::Secret->equal($token, $admin_api_key);
603 119 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 29 90 119
604 29 $c->res->headers->header('WWW-Authenticate' => 'Bearer realm="skeid-admin"');
605 29 $c->render(
606 status => 401,
607 json => {
608 error => {
609 type => 'unauthorized',
610 message => 'Missing or invalid admin bearer token',
611 },
612 },
613 );
614 29 return undef;
615 }
616 90 return 1;
617 }
618  
619 # The snapshot route's gate: the admin API key (compat) or the registry read key, each compared
620 # in constant time. 404 when neither is configured, like every closed /skeid route; 401 with the
621 # admin route's challenge otherwise.
622 27 ▶sub _authorize_registry_read {
Subroutine _authorize_registry_read: called CC=10 SCAR=23.8
623 27 my ($c) = @_;
624 27 my $skeid = $c->skeid;
625 27 $skeid->maybe_reload_config;
626  
627 27 ▶ my @accepted = grep { length } ($skeid->admin_api_key // '', $skeid->registry_read_key // '');
Condition50%2/4
$skeid->admin_api_key // ""
l!l
270
$skeid->registry_read_key // ""
l!l
270
MC/DC0%0/1
$skeid->admin_api_key // ""
$skeid->admin_api_key
MC/DC0%0/1
$skeid->registry_read_key // ""
$skeid->registry_read_key
Truth table50%1/2
A
A= $skeid->admin_api_key
Aresult
11
00
Truth table50%1/2
A
A= $skeid->registry_read_key
Aresult
11
00
628 27 ▶ unless (@accepted) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@accepted) 0 27 27
629 0 $c->render(status => 404, text => 'Not Found');
630 0 return undef;
631 }
632  
633 27 ▶ my $auth = $c->req->headers->authorization // '';
Condition100%2/2
$c->req->headers->authorization // ""
l!l
243
MC/DC100%1/1
$c->req->headers->authorization // ""
$c->req->headers->authorization
Truth table100%2/2
A
A= $c->req->headers->authorization
Aresult
11
00
634 27 my ($scheme, $token) = $auth =~ /\A(\S+)\s+(.+)\z/;
635 27 my $ok = 0;
636 27 ▶ if (defined($scheme) && lc($scheme) eq 'bearer' && defined($token)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $scheme and lc $scheme eq "bearer" and defined $token) 24 3 27
Condition66%4/6
defined $scheme and lc $scheme eq "bearer" and defined $token
!ll&&!rl&&r
3024
defined $scheme and lc $scheme eq "bearer"
!ll&&!rl&&r
3024
MC/DC33%1/3
defined $scheme and lc $scheme eq "bearer" and defined $token
defined $scheme lc $scheme eq "bearer" defined $token
Truth table50%2/4
A and B and C
A= defined $schemeB= lc $scheme eq "bearer"C= defined $token
ABCresult
0XX0
10X0
1100
1111
637 # No short-circuit: both candidates are compared whichever one matches.
638 24 $ok |= Langertha::Skeid::Secret->equal($token, $_) for @accepted;
639 }
640 27 ▶ return 1 if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 23 4 27
641  
642 4 $c->res->headers->header('WWW-Authenticate' => 'Bearer realm="skeid-admin"');
643 4 $c->render(
644 status => 401,
645 json => {
646 error => {
647 type => 'unauthorized',
648 message => 'Missing or invalid registry bearer token',
649 },
650 },
651 );
652 4 return undef;
653 }
654  
655 # The client routes' gate (skeid k90), run by their bridges before anything else of the request:
656 # before the body is parsed, a node is admitted (request.start), a key resolved, an upstream
657 # called or a usage event written -- a refused request has nothing to give back. It reloads the
658 # config first, so switching client authentication on or off or rotating an id holds from the
659 # next request on every client route, a GET included. That is what the reload throttle
660 # (config_reload_interval) and the no-op on an unchanged config are for: an anonymous GET does
661 # not rerun the loader, nor restart the node probes, per request. Off without client_auth.
662 # The identity is the one routing uses, so with routing.trust_key_id_header the header's id is
663 # what must be on the list. A refusal is not logged -- the routes are public, and a line per
664 # rejected request would be anybody's to flood -- and its body names neither the key nor its id.
665 1409 ▶sub _authorize_client {
Subroutine _authorize_client: called CC=5 SCAR=16.1
666 1409 my ($c) = @_;
667 1409 my $skeid = $c->skeid;
668 1409 $skeid->maybe_reload_config;
669 1409 ▶ return 1 unless $skeid->client_auth_enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $skeid->client_auth_enabled 1173 236 1409
670  
671 236 my $api_key_id = _request_api_key_id($c);
672 236 ▶ return 1 if $skeid->client_key_allowed($api_key_id);
Branch: 1 branch
BranchTrueFalseTotal
if $skeid->client_key_allowed($api_key_id) 147 89 236
673  
674 89 ▶ my $missing = !defined($api_key_id) || $api_key_id eq 'anonymous';
Condition66%2/3
!defined($api_key_id) || $api_key_id eq 'anonymous'
l!l&&r!l&&!r
04148
MC/DC50%1/2
!defined($api_key_id) || $api_key_id eq 'anonymous'
!defined($api_key_id) $api_key_id eq 'anonymous'
Truth table66%2/3
not(A) || B
A= !defined($api_key_id)B= $api_key_id eq 'anonymous'
ABresult
1X1
011
000
675 89 $c->res->headers->header('WWW-Authenticate' => 'Bearer realm="skeid"');
676 89 ▶ _render_error($c, 401, ($missing ? 'Missing API key' : 'Invalid API key'),
Branch: 1 branch
BranchTrueFalseTotal
$missing ? : 41 48 89
677 'invalid_request_error', 'invalid_api_key');
678 89 return undef;
679 }
680  
681 # What a key is shown depends on who presents it, so no cache may hand one key's answer to
682 # another: every answer -- the client gate's 401 included -- is private, not stored, and varies on
683 # each header that can carry the identity. Set by the route's bridge, before the gate.
684 46 ▶sub _manifest_cache_headers {
Subroutine _manifest_cache_headers: called CC=1 SCAR=0.0
685 46 my ($c) = @_;
686 46 my $headers = $c->res->headers;
687 46 $headers->header('Cache-Control' => 'private, no-store');
688 46 $headers->header(Vary => 'Authorization, X-Api-Key, X-Skeid-Key-Id, X-Api-Key-Id');
689 46 return;
690 }
691  
692 # The config is reloaded by the client gate before this runs (see _authorize_client), throttled
693 # by config_reload_interval and a no-op when nothing changed. 404 when nothing is published
694 # (disabled, or a Langertha without Langertha::Manifest), 401 without a key (ADR 0015: no
695 # anonymous manifest, not even a minimal one), 403 for a key without a manifest: grant, else the
696 # manifest built for that key id.
697 28 ▶sub _handle_manifest {
Subroutine _handle_manifest: called CC=6 SCAR=17.9
698 28 my ($c) = @_;
699 28 my $skeid = $c->skeid;
700  
701 28 my $headers = $c->res->headers;
702  
703 28 ▶ unless ($skeid->manifest_enabled && $skeid->manifest_available) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($skeid->manifest_enabled and $skeid->manifest_available) 6 22 28
Condition66%2/3
$skeid->manifest_enabled and $skeid->manifest_available
!ll&&!rl&&r
6022
MC/DC50%1/2
$skeid->manifest_enabled and $skeid->manifest_available
$skeid->manifest_enabled $skeid->manifest_available
Truth table66%2/3
A and B
A= $skeid->manifest_enabledB= $skeid->manifest_available
ABresult
0X0
100
111
704 6 $c->render(status => 404,
705 json => { error => { message => 'No provider manifest is published here', type => 'not_found' } });
706 6 return;
707 }
708  
709 22 my $api_key_id = _request_api_key_id($c);
710 22 ▶ if (!defined($api_key_id) || $api_key_id eq 'anonymous') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $api_key_id or $api_key_id eq "anonymous") 5 17 22
Condition66%2/3
not defined $api_key_id or $api_key_id eq "anonymous"
l!l&&r!l&&!r
0517
MC/DC50%1/2
not defined $api_key_id or $api_key_id eq "anonymous"
not defined $api_key_id $api_key_id eq "anonymous"
Truth table66%2/3
not(A) or B
A= not defined $api_key_idB= $api_key_id eq "anonymous"
ABresult
1X1
011
000
711 5 $headers->header('WWW-Authenticate' => 'Bearer realm="skeid"');
712 5 $c->render(status => 401,
713 json => { error => { message => 'An API key is required for the provider manifest', type => 'unauthorized' } });
714 5 return;
715 }
716  
717 17 my $json = $skeid->manifest_for_key($api_key_id);
718 17 ▶ unless (defined $json) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $json) 3 14 17
719 3 $c->render(status => 403,
720 json => { error => { message => 'No provider manifest is published for this key', type => 'permission_error' } });
721 3 return;
722 }
723  
724 14 $c->render(data => $json, format => 'json');
725 14 return;
726 }
727  
728 # The client's connection is silent while the upstream works, and the server closes a connection
729 # that was silent for its inactivity timeout (30s unless the server was told otherwise). So a
730 # request that calls an upstream gets, on top of that, what the upstream may take: the client
731 # outlasts the upstream and is still there for the answer, or for the error. Read from the user
732 # agent rather than kept beside it, so the two sides cannot drift apart. It holds for this
733 # request only: the server sets its own timeout again for the next one on the connection. A
734 # timeout of 0 is none, on either side, and stays none.
735 1204 ▶sub _extend_client_timeout {
Subroutine _extend_client_timeout: called CC=5 SCAR=17.8
736 1204 my ($c) = @_;
737 1204 ▶ my $stream = Mojo::IOLoop->stream($c->tx->connection // '') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $stream = "Mojo::IOLoop"->stream($c->tx->connection // "") or return 1204 0 1204
Condition50%1/2
$c->tx->connection // ""
l!l
12040
MC/DC0%0/1
$c->tx->connection // ""
$c->tx->connection
Truth table50%1/2
A
A= $c->tx->connection
Aresult
11
00
738 1204 my $own = $stream->timeout;
739 1204 my $upstream = $c->app->ua->request_timeout;
740 1204 ▶ $stream->timeout(($own && $upstream) ? $own + $upstream : 0);
Branch: 1 branch
BranchTrueFalseTotal
$own && $upstream ? : 1204 0 1204
Condition33%1/3
$own and $upstream
!ll&&!rl&&r
001204
MC/DC0%0/2
$own and $upstream
$own $upstream
Truth table33%1/3
A and B
A= $ownB= $upstream
ABresult
0X0
100
111
741 1204 return;
742 }
743  
744 278 ▶sub _handle_openai_chat {
Subroutine _handle_openai_chat: called CC=3 SCAR=11.0
745 278 my ($c) = @_;
746 278 _extend_client_timeout($c);
747 278 my $body = $c->req->json;
748 278 ▶ unless (ref($body) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $body eq "HASH") 2 276 278
749 2 $c->render(json => { error => { message => 'Invalid JSON body', type => 'invalid_request_error' } }, status => 400);
750 2 return;
751 }
752  
753 276 ▶ my $model = $body->{model} // '';
Condition50%1/2
$body->{"model"} // ""
l!l
2760
MC/DC0%0/1
$body->{"model"} // ""
$body->{"model"}
Truth table50%1/2
A
A= $body->{"model"}
Aresult
11
00
754 276 my $api_key_id = _request_api_key_id($c);
755 276 ▶ _begin_route_async($c, $model, $api_key_id, sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
756 276 my ($route, $node_id, $started, $tier) = @_;
757 276 ▶ return unless $route;
Branch: 1 branch
BranchTrueFalseTotal
unless $route 28 248 276
758  
759 # The alias layer means the model the client asked for and the model the node is asked for
760 # are two different strings (ADR 0008). The upstream body carries the served model; the
761 # usage event carries both, or cost attribution silently loses which product was used.
762 248 my $served_model = _served_model($tier, $model);
763 248 ▶ $body->{model} = $served_model if ref($body) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $body eq "HASH" 248 0 248
764  
765 248 my $url = _endpoint_url_for_node($route->{url}, '/chat/completions');
766 my $meta = {
767 api_format => 'openai',
768 endpoint => '/v1/chat/completions',
769 api_key_id => $api_key_id,
770 provider => 'skeid',
771 engine => ($route->{engine} // 'openaibase'),
772 model => $served_model,
773 requested_model => $model,
774 248 ▶ route_url => ($route->{url} // ''),
Condition50%2/4
$route->{"engine"} // "openaibase"
l!l
2480
$route->{"url"} // ""
l!l
2480
MC/DC0%0/1
$route->{"engine"} // "openaibase"
$route->{"engine"}
MC/DC0%0/1
$route->{"url"} // ""
$route->{"url"}
Truth table50%1/2
A
A= $route->{"engine"}
Aresult
11
00
Truth table50%1/2
A
A= $route->{"url"}
Aresult
11
00
775 };
776  
777 248 my $relay = _verbatim_relay($c, $served_model);
778  
779 248 ▶ if ($body->{stream}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body->{"stream"}) 69 179 248
780 69 _proxy_openai_stream($c, $url, $body, $node_id, $started, $meta, undef, $relay);
781 69 return;
782 }
783  
784 179 $c->render_later;
785 179 ▶ _proxy_openai_json_async($c, $url, $body, $node_id, $started, $meta, sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
786 179 my ($res, $err, $status) = @_;
787 179 ▶ return if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 27 152 179
788 152 _render_upstream_response($c, $res, $node_id);
789 179 }, undef, $relay);
790 276 });
791 }
792  
793 67 ▶sub _handle_openai_embeddings {
Subroutine _handle_openai_embeddings: called CC=3 SCAR=11.2
794 67 my ($c) = @_;
795 67 _extend_client_timeout($c);
796 67 my $body = $c->req->json;
797 67 ▶ unless (ref($body) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $body eq "HASH") 0 67 67
798 0 $c->render(json => { error => { message => 'Invalid JSON body', type => 'invalid_request_error' } }, status => 400);
799 0 return;
800 }
801  
802 67 ▶ my $model = $body->{model} // '';
Condition50%1/2
$body->{"model"} // ""
l!l
670
MC/DC0%0/1
$body->{"model"} // ""
$body->{"model"}
Truth table50%1/2
A
A= $body->{"model"}
Aresult
11
00
803 67 my $api_key_id = _request_api_key_id($c);
804 67 ▶ _begin_route_async($c, $model, $api_key_id, sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
805 67 my ($route, $node_id, $started, $tier) = @_;
806 67 ▶ return unless $route;
Branch: 1 branch
BranchTrueFalseTotal
unless $route 6 61 67
807  
808 # The alias layer means the model the client asked for and the model the node is asked for
809 # are two different strings (ADR 0008). The upstream body carries the served model; the
810 # usage event carries both, or cost attribution silently loses which product was used.
811 61 my $served_model = _served_model($tier, $model);
812 61 ▶ $body->{model} = $served_model if ref($body) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $body eq "HASH" 61 0 61
813  
814 61 my $url = _endpoint_url_for_node($route->{url}, '/embeddings');
815 my $meta = {
816 api_format => 'openai',
817 endpoint => '/v1/embeddings',
818 api_key_id => $api_key_id,
819 provider => 'skeid',
820 engine => ($route->{engine} // 'openaibase'),
821 model => $served_model,
822 requested_model => $model,
823 61 ▶ route_url => ($route->{url} // ''),
Condition50%2/4
$route->{"engine"} // "openaibase"
l!l
610
$route->{"url"} // ""
l!l
610
MC/DC0%0/1
$route->{"engine"} // "openaibase"
$route->{"engine"}
MC/DC0%0/1
$route->{"url"} // ""
$route->{"url"}
Truth table50%1/2
A
A= $route->{"engine"}
Aresult
11
00
Truth table50%1/2
A
A= $route->{"url"}
Aresult
11
00
824 };
825 61 $c->render_later;
826 61 ▶ _proxy_openai_json_async($c, $url, $body, $node_id, $started, $meta, sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
827 61 my ($res, $err, $status) = @_;
828 61 ▶ return if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 8 53 61
829 53 _render_upstream_response($c, $res, $node_id);
830 61 }, undef, _verbatim_relay($c, $served_model));
831 67 });
832 }
833  
834 # Runs once per request, when its head is parsed and its body is still on the wire (the request
835 # content's `body` event). An upload route gets its own limit there, because afterwards is too
836 # late for either thing it is for (skeid k91):
837 #
838 # - uploads.max_bytes replaces the server's request limit, which is 16 MiB and so below the
839 # default upload limit. A request that declares a larger body is cut off at once: the limit is
840 # set below what was already read, the parser stops, and the route answers 413 without the body
841 # having been received. A body of undeclared length (chunked) is cut off when it passes the
842 # limit plus the head allowance, so it is never buffered whole either.
843 # - A caller the client gate is going to refuse is cut off the same way, so its 401 does not
844 # cost an upload spooled to disk first. The gate itself still runs and renders the 401.
845 #
846 # Which limit a route has is the route table below: the audio routes the uploads.max_bytes, the
847 # documents routes (a form, and base64 in JSON for /v1/convert/source) uploads.documents_max_bytes.
848 # A new upload route is an entry there, or it runs under the server's 16 MiB.
849 #
850 # The limit is read off the Skeid here, after picking up a changed config, so a reload holds
851 # from the next upload. Every other route keeps the server's own limit: nothing is set for it.
852 # A failure here leaves the server's limit in force and the route's own checks to decide.
853 my %UPLOAD_ROUTE = (
854 172 (map { $_ => 'upload_max_bytes' } Langertha::Skeid::Protocol::Audio->routes),
855 86 (map { $_ => 'documents_max_bytes' } Langertha::Skeid::Protocol::Documents->routes),
856 );
857  
858 2220 ▶sub _limit_upload {
Subroutine _limit_upload: called CC=13 SCAR=25.9
859 2220 my ($app, $tx) = @_;
860 2220 my $req = $tx->req;
861 2220 (my $path = $req->url->path->to_route) =~ s{/\z}{};
862 2220 ▶ return _limit_mcp_request($app, $tx) if $path =~ m{\A/mcp/};
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[\A/mcp/] 73 2147 2220
863 2147 ▶ return unless uc($req->method // '') eq 'POST';
Branch: 1 branch
BranchTrueFalseTotal
unless uc($req->method // "") eq "POST" 505 1642 2147
Condition50%1/2
$req->method // ""
l!l
21470
MC/DC0%0/1
$req->method // ""
$req->method
Truth table50%1/2
A
A= $req->method
Aresult
11
00
864 1642 ▶ my $limit = $UPLOAD_ROUTE{$path} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $limit = $UPLOAD_ROUTE{$path} or return 177 1465 1642
865  
866 177 my $done = eval {
867 177 my $c = $app->build_controller($tx);
868 177 my $skeid = $c->skeid;
869 177 $skeid->maybe_reload_config;
870  
871 177 ▶ if ($skeid->client_auth_enabled && !$skeid->client_key_allowed(_request_api_key_id($c))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skeid->client_auth_enabled and not $skeid->client_key_allowed(_request_api_key_id($c))) 9 168 177
Condition100%3/3
$skeid->client_auth_enabled and not $skeid->client_key_allowed(_request_api_key_id($c))
!ll&&!rl&&r
16629
MC/DC100%2/2
$skeid->client_auth_enabled and not $skeid->client_key_allowed(_request_api_key_id($c))
$skeid->client_auth_enabled not $skeid->client_key_allowed(_request_api_key_id($c))
Truth table100%3/3
A and not(B)
A= $skeid->client_auth_enabledB= not $skeid->client_key_allowed(_request_api_key_id($c))
ABresult
0X0
100
111
872 9 $req->max_message_size(1);
873 9 return 1;
874 }
875  
876 168 my $max = $skeid->$limit;
877 168 my $declared = $req->headers->content_length;
878 168 ▶ if (!$req->content->is_chunked && defined($declared) && $declared =~ /\A[0-9]+\z/ && $declared > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $req->content->is_chunked and defined $declared and $declared =~ /\A[0-9]+\z/ and $declared > $max) 8 160 168
Condition77%7/9
not $req->content->is_chunked and defined $declared and $declared =~ /\A[0-9]+\z/ and $declared > $max
!ll&&!rl&&r
21588
not $req->content->is_chunked and defined $declared and $declared =~ /\A[0-9]+\z/
!ll&&!rl&&r
20166
not $req->content->is_chunked and defined $declared
!ll&&!rl&&r
20166
MC/DC50%2/4
not $req->content->is_chunked and defined $declared and $declared =~ /\A[0-9]+\z/ and $declared > $max
not $req->content->is_chunked defined $declared $declared =~ /\A[0-9]+\z/ $declared > $max
Truth table60%3/5
not(A) and B and C and D
A= not $req->content->is_chunkedB= defined $declaredC= $declared =~ /\A[0-9]+\z/D= $declared > $max
ABCDresult
0XXX0
10XX0
110X0
11100
11111
879 8 $req->max_message_size(1);
880 8 return 1;
881 }
882 160 $req->max_message_size($max + $UPLOAD_HEAD_ALLOWANCE);
883 160 1;
884 };
885 177 ▶ $app->log->error('Setting the upload limit failed: ' . ($@ || 'unknown error')) unless $done;
Branch: 1 branch
BranchTrueFalseTotal
unless $done 0 177 177
Condition0%0/2
$@ || 'unknown error'
l!l
00
MC/DC0%0/1
$@ || 'unknown error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
886 177 return;
887 }
888  
889 # The body limit of the MCP routes: mcp.max_request_bytes, not the server's own. Set on the
890 # request as its body begins, like the upload limit. A body over it is cut off and _handle_mcp
891 # answers 413. A failure here leaves the server's limit in force.
892 73 ▶sub _limit_mcp_request {
Subroutine _limit_mcp_request: called CC=3 SCAR=11.4
893 73 my ($app, $tx) = @_;
894 73 my $done = eval {
895 73 my $skeid = $app->build_controller($tx)->skeid;
896 73 $skeid->maybe_reload_config;
897 73 $tx->req->max_message_size($skeid->mcp_max_request_bytes);
898 73 1;
899 };
900 73 ▶ $app->log->error('Setting the MCP request limit failed: ' . ($@ || 'unknown error')) unless $done;
Branch: 1 branch
BranchTrueFalseTotal
unless $done 0 73 73
Condition0%0/2
$@ || 'unknown error'
l!l
00
MC/DC0%0/1
$@ || 'unknown error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
901 73 return;
902 }
903  
904 # POST /v1/audio/transcriptions and /v1/audio/translations (skeid k91). The request is a
905 # multipart form and the answer is whatever the node's transcription endpoint says -- JSON, plain
906 # text, subtitles or an event stream. Neither is translated (ADR 0021): the form goes upstream
907 # part for part as the client sent it, the answer comes back byte for byte. Skeid reads two
908 # fields of the form, `model` to route by and `stream` to pick the relay, and writes one, `model`
909 # again, when an alias tier serves another model than the one asked for (ADR 0008). What the
910 # form and the answers look like on the wire is Langertha::Skeid::Protocol::Audio's to know.
911 82 ▶sub _handle_openai_audio {
Subroutine _handle_openai_audio: called CC=17 SCAR=28.4
912 82 my ($c, $endpoint) = @_;
913 82 my $audio = 'Langertha::Skeid::Protocol::Audio';
914 82 _extend_client_timeout($c);
915 82 my $req = $c->req;
916  
917 # Before anything is read off the body: a request cut off by _limit_upload has no complete
918 # form. The body size is checked again, exactly, for what the limit set there lets through.
919 82 my $max = $c->skeid->upload_max_bytes;
920 82 ▶ if ($req->is_limit_exceeded || ($req->body_size // 0) > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->is_limit_exceeded or ($req->body_size // 0) > $max) 6 76 82
Condition80%4/5
$req->is_limit_exceeded or ($req->body_size // 0) > $max
l!l&&r!l&&!r
5176
$req->body_size // 0
l!l
770
MC/DC0%0/2
$req->is_limit_exceeded or ($req->body_size // 0) > $max
$req->is_limit_exceeded $req->body_size
Truth table66%2/3
A or B
A= $req->is_limit_exceededB= $req->body_size
ABresult
1X1
011
000
921 6 _render_error($c, 413, "Maximum content size limit ($max bytes) exceeded", 'invalid_request_error');
922 6 return;
923 }
924  
925 76 my $content = $req->content;
926 76 ▶ unless ($content->is_multipart) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($content->is_multipart) 2 74 76
927 2 _render_error($c, 400, 'Expected a multipart/form-data body', 'invalid_request_error');
928 2 return;
929 }
930  
931 # One `model` field, no more: with two, Skeid would route -- and apply the key's policy -- by
932 # one while the node may well serve the other.
933 74 my ($field, $problem) = $audio->form_fields($content);
934 74 ▶ my @models = @{$field->{model} || []};
Condition100%2/2
$field->{'model'} || []
l!l
722
MC/DC100%1/1
$field->{'model'} || []
$field->{'model'}
Truth table100%2/2
A
A= $field->{'model'}
Aresult
11
00
935 74 ▶ my $model = @models == 1 ? $models[0]{value} : undef;
Branch: 1 branch
BranchTrueFalseTotal
@models == 1 ? : 64 10 74
936 74 ▶ $model = decode('UTF-8', $model) // $model if defined $model;
Branch: 1 branch
BranchTrueFalseTotal
if defined $model 63 11 74
Condition33%1/3
decode("UTF-8", $model) // $model
l!l&&r!l&&!r
6300
MC/DC0%0/2
decode("UTF-8", $model) // $model
decode("UTF-8", $model) $model
Truth table33%1/3
A // B
A= decode("UTF-8", $model)B= $model
ABresult
1X1
011
000
937 74 ▶ $problem //= !@models ? "The 'model' form field is required"
Branch: 3 branches
BranchTrueFalseTotal
!@models ? : 1 72 73
@models > 1 ? : 8 64 72
!(defined $model && length $model) ? : 2 62 64
Condition100%5/5
$problem //= !@models ? "The 'model' form field is required" : (@models > 1 ? "The 'model' form field may be given only once" : (!(defined $model && length $model) ? "The 'model' form field is empty or too long" : undef))
l!l
173
defined $model && length $model
!ll&&!rl&&r
1162
MC/DC100%1/1
$problem //= !@models ? "The 'model' form field is required" : (@models > 1 ? "The 'model' form field may be given only once" : (!(defined $model && length $model) ? "The 'model' form field is empty or too long" : undef))
$problem
MC/DC100%2/2
defined $model && length $model
defined $model length $model
Truth table100%2/2
A
A= $problem
Aresult
11
00
Truth table100%3/3
A && B
A= defined $modelB= length $model
ABresult
0X0
100
111
938 : @models > 1 ? "The 'model' form field may be given only once"
939 : !(defined($model) && length($model)) ? "The 'model' form field is empty or too long"
940 : undef;
941 74 ▶ if (defined $problem) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $problem) 12 62 74
942 12 _render_error($c, 400, $problem, 'invalid_request_error');
943 12 return;
944 }
945  
946 # The last `stream` field decides, as it does for the node that parses the same form.
947 62 ▶ my $streamed = @{$field->{stream} || []} ? $audio->is_true($field->{stream}[-1]{value}) : 0;
Branch: 1 branch
BranchTrueFalseTotal
@{$field->{'stream'} or [];} ? : 28 34 62
Condition100%2/2
$field->{'stream'} || []
l!l
2834
MC/DC100%1/1
$field->{'stream'} || []
$field->{'stream'}
Truth table100%2/2
A
A= $field->{'stream'}
Aresult
11
00
948  
949 62 my $api_key_id = _request_api_key_id($c);
950 62 ▶ _begin_route_async($c, $model, $api_key_id, sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
951 62 my ($route, $node_id, $started, $tier) = @_;
952 62 ▶ return unless $route;
Branch: 1 branch
BranchTrueFalseTotal
unless $route 3 59 62
953  
954 # Requested and served model are two strings once an alias is in play (ADR 0008): the form
955 # that goes upstream carries the served one, the usage event carries both.
956 59 my $served_model = _served_model($tier, $model);
957 59 ▶ my %replace = $served_model eq $model ? () : ($models[0]{index} => encode('UTF-8', $served_model));
Branch: 1 branch
BranchTrueFalseTotal
$served_model eq $model ? : 57 2 59
958  
959 59 my $url = _endpoint_url_for_node($route->{url}, $audio->upstream_path($endpoint));
960 my $meta = {
961 api_format => 'openai',
962 endpoint => $endpoint,
963 api_key_id => $api_key_id,
964 provider => 'skeid',
965 engine => ($route->{engine} // 'openaibase'),
966 model => $served_model,
967 requested_model => $model,
968 59 ▶ route_url => ($route->{url} // ''),
Condition50%2/4
$route->{"engine"} // "openaibase"
l!l
590
$route->{"url"} // ""
l!l
590
MC/DC0%0/1
$route->{"engine"} // "openaibase"
$route->{"engine"}
MC/DC0%0/1
$route->{"url"} // ""
$route->{"url"}
Truth table50%1/2
A
A= $route->{"engine"}
Aresult
11
00
Truth table50%1/2
A
A= $route->{"url"}
Aresult
11
00
969 };
970 my $relay = {
971 # The upstream form is built from the parts the server already parsed: a part is its
972 # headers and an asset, and an upload above 256 KiB is an asset on disk, so the file is
973 # streamed from there and never copied into memory. The client's Content-Type travels in
974 # the forwarded headers and its boundary is the one the body is written with.
975 57 ▶ request => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
976 57 my ($ua, $url, $headers) = @_;
977 57 return $ua->build_tx(POST => $url, $headers,
978 multipart => $audio->upstream_parts($content, \%replace));
979 },
980 45 ▶ units => sub { $audio->usage_units(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
981 54 ▶ delta_text => sub { $audio->delta_text(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
982 59 };
983 59 my $body = { model => $served_model };
984  
985 59 ▶ if ($streamed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($streamed) 19 40 59
986 19 _proxy_openai_stream($c, $url, $body, $node_id, $started, $meta, undef, $relay);
987 19 return;
988 }
989  
990 40 $c->render_later;
991 40 ▶ _proxy_openai_json_async($c, $url, $body, $node_id, $started, $meta, sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
992 40 my ($res, $err, $status) = @_;
993 40 ▶ return if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 4 36 40
994 36 _render_upstream_response($c, $res, $node_id);
995 40 }, undef, $relay);
996 62 });
997 }
998  
999 # POST /v1/convert/file and POST /v1/convert/source (skeid k94, ADR 0028): docling-serve's
1000 # synchronous conversion under its own paths, relayed in its own shape like the audio routes
1001 # (ADR 0021) -- a form goes upstream part for part, a source body is decoded once and written
1002 # again so the node reads exactly what Skeid did, and the answer (JSON, or a zip) comes back byte
1003 # for byte. There is no `model` field: the route is planned over the nodes of kind `documents`
1004 # under the routing key `documents`, which is what a key's policy grants. Skeid refuses what makes
1005 # the node fetch from an address the client names, unless documents.allow_remote_sources: that
1006 # is a server-side request from inside the cluster network. The wire is
1007 # Langertha::Skeid::Protocol::Documents's to know.
1008 86 ▶sub _handle_documents {
Subroutine _handle_documents: called CC=10 SCAR=23.0
1009 86 my ($c, $endpoint) = @_;
1010 86 my $docs = 'Langertha::Skeid::Protocol::Documents';
1011 86 _extend_client_timeout($c);
1012 86 my $req = $c->req;
1013 86 my $skeid = $c->skeid;
1014  
1015 # A request cut off by _limit_upload has no complete body. The size is checked again,
1016 # exactly, for what the limit set there lets through.
1017 86 my $max = $skeid->documents_max_bytes;
1018 86 ▶ if ($req->is_limit_exceeded || ($req->body_size // 0) > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->is_limit_exceeded or ($req->body_size // 0) > $max) 4 82 86
Condition60%3/5
$req->is_limit_exceeded or ($req->body_size // 0) > $max
l!l&&r!l&&!r
4082
$req->body_size // 0
l!l
820
MC/DC0%0/2
$req->is_limit_exceeded or ($req->body_size // 0) > $max
$req->is_limit_exceeded $req->body_size
Truth table66%2/3
A or B
A= $req->is_limit_exceededB= $req->body_size
ABresult
1X1
011
000
1019 4 _render_error($c, 413, "Maximum content size limit ($max bytes) exceeded", 'invalid_request_error');
1020 4 return;
1021 }
1022  
1023 82 my ($remote, $count, $body, $content);
1024 82 ▶ if ($endpoint =~ /file\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($endpoint =~ /file\z/) { } 58 24 82
1025 58 $content = $req->content;
1026 58 ▶ unless ($content->is_multipart) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($content->is_multipart) 1 57 58
1027 1 _render_error($c, 400, 'Expected a multipart/form-data body', 'invalid_request_error');
1028 1 return;
1029 }
1030 57 my ($names, $problem) = $docs->form_remote_fields($content);
1031 57 ▶ if (defined $problem) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $problem) 1 56 57
1032 1 _render_error($c, 400, $problem, 'invalid_request_error');
1033 1 return;
1034 }
1035 56 $remote = $names;
1036 56 $count = $docs->document_count($content);
1037 }
1038 else {
1039 24 $body = $req->json;
1040 24 ▶ unless (ref($body) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $body eq "HASH") 5 19 24
1041 5 _render_error($c, 400, 'Expected a JSON object', 'invalid_request_error');
1042 5 return;
1043 }
1044 19 $remote = $docs->json_remote_fields($body, $req->body);
1045 19 $count = $docs->document_count($body);
1046 }
1047  
1048 # Before admission, so it costs no slot and no event. The message names the field and never
1049 # what it holds: the value is an address the caller chose.
1050 75 ▶ if (@$remote && !$skeid->documents_allow_remote_sources) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$remote and not $skeid->documents_allow_remote_sources) 17 58 75
Condition100%3/3
@$remote and not $skeid->documents_allow_remote_sources
!ll&&!rl&&r
56217
MC/DC100%2/2
@$remote and not $skeid->documents_allow_remote_sources
@$remote not $skeid->documents_allow_remote_sources
Truth table100%3/3
A and not(B)
A= @$remoteB= not $skeid->documents_allow_remote_sources
ABresult
0X0
100
111
1051 17 _render_error($c, 400,
1052 "The '$remote->[0]' field is not accepted: requests that make the node fetch from or call "
1053 . 'an address are disabled (documents.allow_remote_sources)', 'invalid_request_error');
1054 17 return;
1055 }
1056  
1057 58 my $routing_key = $docs->routing_key;
1058 58 my $api_key_id = _request_api_key_id($c);
1059 58 ▶ _begin_route_async($c, $routing_key, $api_key_id, sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
1060 58 my ($route, $node_id, $started, $tier) = @_;
1061 58 ▶ return unless $route;
Branch: 1 branch
BranchTrueFalseTotal
unless $route 5 53 58
1062  
1063 53 my $url = _endpoint_url_for_node($route->{url}, $docs->upstream_path($endpoint));
1064 my $meta = {
1065 api_format => $docs->api_format,
1066 endpoint => $endpoint,
1067 api_key_id => $api_key_id,
1068 provider => 'skeid',
1069 engine => ($route->{engine} // 'openaibase'),
1070 model => $routing_key,
1071 requested_model => $routing_key,
1072 53 ▶ route_url => ($route->{url} // ''),
Condition50%2/4
$route->{"engine"} // "openaibase"
l!l
530
$route->{"url"} // ""
l!l
530
MC/DC0%0/1
$route->{"engine"} // "openaibase"
$route->{"engine"}
MC/DC0%0/1
$route->{"url"} // ""
$route->{"url"}
Truth table50%1/2
A
A= $route->{"engine"}
Aresult
11
00
Truth table50%1/2
A
A= $route->{"url"}
Aresult
11
00
1073 };
1074 my $relay = {
1075 # The form is built from the parts the server parsed: a part is its headers and an asset,
1076 # and a file above 256 KiB is an asset on disk, sent from there and never copied. The
1077 # client's Content-Type, boundary included, travels in the forwarded headers.
1078 43 ▶ ($content ? (request => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1079 43 my ($ua, $url, $headers) = @_;
1080 43 return $ua->build_tx(POST => $url, $headers,
1081 multipart => Langertha::Skeid::Protocol::Audio->upstream_parts($content, {}));
1082 }) : ()),
1083 # Not decoded past what is worth the CPU of the event loop; and no tokens to read.
1084 max_parse_bytes => $docs->max_parse_bytes,
1085 46 ▶ usage => sub { return },
Subroutine __ANON__: called CC=1 SCAR=0.0
1086 46 ▶ units => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1087 46 my ($payload, $res) = @_;
1088 46 ▶ return unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 46 46
1089 46 my $pages = $docs->pages($payload, $res);
1090 46 ▶ return (documents => $count, (defined $pages ? (pages => $pages) : ()));
Branch: 1 branch
BranchTrueFalseTotal
defined $pages ? : 36 10 46
1091 },
1092 53 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
$content ? : 44 9 53
1093  
1094 53 $c->render_later;
1095 53 ▶ _proxy_openai_json_async($c, $url, ($body // {}), $node_id, $started, $meta, sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1096 53 my ($res, $err, $status) = @_;
1097 53 ▶ return if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 7 46 53
1098 46 _render_upstream_response($c, $res, $node_id);
1099 53 ▶ }, undef, $relay);
Condition100%2/2
$body // {}
l!l
944
MC/DC100%1/1
$body // {}
$body
Truth table100%2/2
A
A= $body
Aresult
11
00
1100 58 }, kind => 'documents');
1101 }
1102  
1103 # GET /v1/realtime (skeid k95, ADR 0029): live transcription over the OpenAI realtime protocol.
1104 # The handshake is an ordinary request that has a slot and a node to get -- the client gate has
1105 # run already (a bridge, so a refused key is a plain 401 and no socket), the model comes from the
1106 # query, the key's policy, tiers and admission apply, and route_wait happens before the upgrade.
1107 # The node is opened FIRST: only a node that took the WebSocket handshake gets a client that is
1108 # upgraded, so a node that refuses is an HTTP 502 and not an accepted-then-closed socket. From the
1109 # upgrade on the session is Langertha::Skeid::Proxy::Realtime's; this function ends with handing
1110 # it both transactions, and its finish callback is the only place request.finish and the usage
1111 # event happen for a session that got that far (once, on whichever path the session ends).
1112 99 ▶sub _handle_openai_realtime {
Subroutine _handle_openai_realtime: called CC=2 SCAR=6.9
1113 99 my ($c) = @_;
1114 99 my $protocol = 'Langertha::Skeid::Protocol::Realtime';
1115 99 _extend_client_timeout($c);
1116 99 $c->render_later;
1117  
1118 99 my ($model, $problem) = $protocol->query_model($c->req->url->query);
1119 99 ▶ if (defined $problem) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $problem) 4 95 99
1120 4 _render_error($c, 400, $problem, 'invalid_request_error');
1121 4 return;
1122 }
1123  
1124 95 my $api_key_id = _request_api_key_id($c);
1125 95 ▶ _begin_route_async($c, $model, $api_key_id, sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
1126 95 my ($route, $node_id, $started, $tier) = @_;
1127 95 ▶ return unless $route;
Branch: 1 branch
BranchTrueFalseTotal
unless $route 4 91 95
1128  
1129 91 my $served_model = _served_model($tier, $model);
1130 my $meta = {
1131 api_format => 'openai',
1132 endpoint => $protocol->route,
1133 api_key_id => $api_key_id,
1134 provider => 'skeid',
1135 engine => ($route->{engine} // 'openaibase'),
1136 model => $served_model,
1137 requested_model => $model,
1138 91 ▶ route_url => ($route->{url} // ''),
Condition50%2/4
$route->{"engine"} // "openaibase"
l!l
910
$route->{"url"} // ""
l!l
910
MC/DC0%0/1
$route->{"engine"} // "openaibase"
$route->{"engine"}
MC/DC0%0/1
$route->{"url"} // ""
$route->{"url"}
Truth table50%1/2
A
A= $route->{"engine"}
Aresult
11
00
Truth table50%1/2
A
A= $route->{"url"}
Aresult
11
00
1139 };
1140 my $url = $protocol->upstream_url(
1141 91 _endpoint_url_for_node($route->{url}, $protocol->upstream_path), $served_model,
1142 $c->req->url->query);
1143  
1144 # What the client sent for the handshake of its own connection is not the node's: its
1145 # Sec-WebSocket-* headers (the subprotocol list may carry the browser's key -- never passed
1146 # on, ADR 0003), its Origin and its cookies stay here. The user agent makes the node's own.
1147 91 my %fwd_headers = _forward_headers($c);
1148 91 delete @fwd_headers{ grep { /\A(?:sec-websocket-|origin\z|cookie\z)/i } keys %fwd_headers };
1149  
1150 91 ▶ _inject_node_auth_async(\%fwd_headers, $c->skeid, $node_id, sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
1151 91 my ($no_key) = @_;
1152  
1153 # The client left while the node's key was being resolved: nothing went upstream, the
1154 # slot goes back and nothing is metered, as for a request that was not forwarded.
1155 91 ▶ if (_client_gone($c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_client_gone($c)) 0 91 91
1156 0 $c->skeid->call_function('request.finish', {
1157 id => $node_id, ok => 0, aborted => 1, duration_ms => _duration_ms($started),
1158 });
1159 0 return;
1160 }
1161 91 ▶ if (defined $no_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $no_key) 1 90 91
1162 1 _refuse_unkeyed_node($c, $node_id, $started, $meta, $no_key);
1163 1 return;
1164 }
1165  
1166 90 my $client = $c->tx;
1167 90 my $up_tx = $c->app->ua->build_websocket_tx($url => \%fwd_headers);
1168 90 my $settled = 0; # set by whichever comes first: the node's answer or the client leaving
1169 90 ▶ my $handshake = $client->can('handshake') ? $client->handshake : $client;
Branch: 1 branch
BranchTrueFalseTotal
$client->can('handshake') ? : 90 0 90
1170  
1171 # The client hangs up while the node is being opened. Its upgrade request is the
1172 # transaction that closes (the WebSocket one does not exist yet), and the node's is
1173 # cancelled -- a node that sees the handshake finish would otherwise hold a session
1174 # for nobody. The slot is given back and the request recorded, as for a plain request.
1175 90 ▶ $handshake->once(finish => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1176 90 ▶ return if $settled;
Branch: 1 branch
BranchTrueFalseTotal
if $settled 89 1 90
1177 1 $settled = 1;
1178 1 my $duration_ms = _duration_ms($started);
1179 1 $c->skeid->call_function('request.finish', {
1180 id => $node_id, ok => 0, aborted => 1, duration_ms => $duration_ms,
1181 });
1182 1 _record_usage_event($c, {
1183 %$meta, _client_abort_event(), node_id => $node_id, duration_ms => $duration_ms,
1184 metrics => {},
1185 });
1186 1 _cancel_upstream($up_tx);
1187 90 });
1188  
1189 90 ▶ $c->app->ua->start($up_tx => sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
1190 90 my ($ua, $tx) = @_;
1191 90 ▶ return if $settled;
Branch: 1 branch
BranchTrueFalseTotal
if $settled 1 89 90
1192 89 $settled = 1;
1193 89 my $duration_ms = _duration_ms($started);
1194 89 _observe_capacity($c, $node_id, $tx->res);
1195  
1196 # The node did not take the handshake: HTTP 502 in the client's shape, no socket. A
1197 # 4xx of the node (its key, its capacity) is Skeid's problem to the client, not theirs.
1198 89 ▶ unless ($tx->is_websocket) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tx->is_websocket) 1 88 89
1199 1 ▶ my $err = $tx->error || {};
Condition50%1/2
$tx->error || {}
l!l
10
MC/DC0%0/1
$tx->error || {}
$tx->error
Truth table50%1/2
A
A= $tx->error
Aresult
11
00
1200 1 $c->skeid->call_function('request.finish', {
1201 id => $node_id, ok => 0, duration_ms => $duration_ms,
1202 });
1203 _record_usage_event($c, {
1204 %$meta,
1205 node_id => $node_id,
1206 status_code => 502,
1207 ok => 0,
1208 duration_ms => $duration_ms,
1209 error_type => 'upstream_error',
1210 error_message => 'The node refused the realtime handshake: '
1211 1 ▶ . ($err->{code} ? $err->{code} . ' ' : '') . ($err->{message} // 'no WebSocket'),
Branch: 1 branch
BranchTrueFalseTotal
$err->{'code'} ? : 1 0 1
Condition50%1/2
$err->{"message"} // "no WebSocket"
l!l
10
MC/DC0%0/1
$err->{"message"} // "no WebSocket"
$err->{"message"}
Truth table50%1/2
A
A= $err->{"message"}
Aresult
11
00
1212 metrics => {},
1213 });
1214 1 _render_error($c, 502, 'The node did not accept the realtime connection', 'upstream_error');
1215 1 return;
1216 }
1217  
1218 # Upgrade the client now, choosing the one subprotocol Skeid ever answers with -- never
1219 # the entry that carries a key. From here the session owns both ends.
1220 88 $client->with_protocols($protocol->subprotocol);
1221 88 $c->res->headers->header('x-skeid-node' => $node_id);
1222 88 $c->rendered(101);
1223  
1224 88 my $skeid = $c->skeid;
1225 $c->app->realtime->start_session(
1226 client => $client,
1227 upstream => $tx,
1228 requested_model => $model,
1229 served_model => $served_model,
1230 started => $started,
1231 limits => {
1232 idle_timeout => $skeid->realtime_idle_timeout,
1233 max_session_seconds => $skeid->realtime_max_session_seconds,
1234 max_message_bytes => $skeid->realtime_max_message_bytes,
1235 max_queued_bytes => $skeid->realtime_max_queued_bytes,
1236 },
1237 88 ▶ finish => sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
1238 88 my ($result) = @_;
1239 $skeid->call_function('request.finish', {
1240 id => $node_id,
1241 ok => $result->{ok},
1242 ($result->{aborted} ? (aborted => 1) : ()),
1243 ($result->{rejected} ? (rejected => 1) : ()),
1244 duration_ms => $result->{duration_ms},
1245 88 ▶ });
Branch: 2 branches
BranchTrueFalseTotal
$result->{'aborted'} ? : 17 71 88
$result->{'rejected'} ? : 5 83 88
1246 # One event for the whole session (ADR 0029): the usage blocks of its completed
1247 # transcriptions summed, priced like any other answer's, and the duration.
1248 my $metrics = ref($result->{usage}) eq 'HASH'
1249 ? _stream_metrics($c, $meta, { model => $served_model }, $result->{duration_ms},
1250 $result->{usage})
1251 88 ▶ : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $result->{'usage'} eq 'HASH' ? : 6 82 88
1252 _record_usage_event($c, {
1253 %$meta,
1254 node_id => $node_id,
1255 status_code => $result->{status_code},
1256 ok => $result->{ok},
1257 duration_ms => $result->{duration_ms},
1258 error_type => $result->{error_type},
1259 error_message => $result->{error_message},
1260 metrics => $metrics,
1261 (defined $result->{audio_seconds} ? (audio_seconds => $result->{audio_seconds}) : ()),
1262 88 ▶ (defined $result->{content_bytes} ? (content_bytes => $result->{content_bytes}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
defined $result->{'audio_seconds'} ? : 3 85 88
defined $result->{'content_bytes'} ? : 5 83 88
1263 });
1264 },
1265 88 );
1266 90 });
1267 91 });
1268 95 });
1269 95 return;
1270 }
1271  
1272 # POST /v1/rerank and POST /rerank (skeid k92). A JSON body in the shape Cohere, vLLM, Jina and
1273 # infinity share, routed by its `model` like a chat request. To a node that speaks that shape
1274 # the body is relayed with only `model` replaced, and the answer comes back byte for byte (ADR
1275 # 0021). A node marked `rerank_format` speaks another dialect and is translated in both
1276 # directions -- on the upstream side, the one place Skeid does that. Either way the wire's names
1277 # are Langertha::Skeid::Protocol::Rerank's to know.
1278 103 ▶sub _handle_openai_rerank {
Subroutine _handle_openai_rerank: called CC=2 SCAR=7.0
1279 103 my ($c) = @_;
1280 103 my $rerank = 'Langertha::Skeid::Protocol::Rerank';
1281 103 _extend_client_timeout($c);
1282 103 my $body = $c->req->json;
1283 103 ▶ if (defined(my $problem = $rerank->request_problem($body))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $problem = $rerank->request_problem($body))) 33 70 103
1284 33 _render_error($c, 400, $problem, 'invalid_request_error');
1285 33 return;
1286 }
1287  
1288 70 my $model = $body->{model};
1289 70 my $documents = $rerank->document_count($body);
1290 70 my $api_key_id = _request_api_key_id($c);
1291 70 ▶ _begin_route_async($c, $model, $api_key_id, sub {
Subroutine __ANON__: called CC=10 SCAR=23.2
1292 70 my ($route, $node_id, $started, $tier) = @_;
1293 70 ▶ return unless $route;
Branch: 1 branch
BranchTrueFalseTotal
unless $route 4 66 70
1294  
1295 # Requested and served model are two strings once an alias is in play (ADR 0008): the body
1296 # that goes upstream carries the served one, the usage event carries both.
1297 66 my $served_model = _served_model($tier, $model);
1298 66 ▶ my $format = $route->{rerank_format} // '';
Condition100%2/2
$route->{"rerank_format"} // ""
l!l
3036
MC/DC100%1/1
$route->{"rerank_format"} // ""
$route->{"rerank_format"}
Truth table100%2/2
A
A= $route->{"rerank_format"}
Aresult
11
00
1299 my $meta = {
1300 api_format => 'openai',
1301 endpoint => $rerank->endpoint,
1302 api_key_id => $api_key_id,
1303 provider => 'skeid',
1304 engine => ($route->{engine} // 'openaibase'),
1305 model => $served_model,
1306 requested_model => $model,
1307 66 ▶ route_url => ($route->{url} // ''),
Condition50%2/4
$route->{"engine"} // "openaibase"
l!l
660
$route->{"url"} // ""
l!l
660
MC/DC0%0/1
$route->{"engine"} // "openaibase"
$route->{"engine"}
MC/DC0%0/1
$route->{"url"} // ""
$route->{"url"}
Truth table50%1/2
A
A= $route->{"engine"}
Aresult
11
00
Truth table50%1/2
A
A= $route->{"url"}
Aresult
11
00
1308 };
1309  
1310 # Which dialect the request has to be put in is known only now, with the node. A request
1311 # this node's dialect cannot carry -- a document that is not text, for a node that takes
1312 # texts -- is the client's to fix, but it holds a slot already.
1313 66 my $upstream_body = eval { $rerank->request_to_upstream($body, $served_model, $format) };
1314 66 ▶ unless (ref($upstream_body) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $upstream_body eq "HASH") 6 60 66
1315 6 my $err = $@;
1316 6 ▶ my $message = blessed($err) && $err->isa('Langertha::Skeid::Protocol::Refusal')
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($err) && $err->isa('Langertha::Skeid::Protocol::Refusal') ? : 6 0 6
Condition33%1/3
builtin::blessed($err) and $err->isa("Langertha::Skeid::Protocol::Refusal")
!ll&&!rl&&r
006
MC/DC0%0/2
builtin::blessed($err) and $err->isa("Langertha::Skeid::Protocol::Refusal")
builtin::blessed($err) $err->isa("Langertha::Skeid::Protocol::Refusal")
Truth table33%1/3
A and B
A= builtin::blessed($err)B= $err->isa("Langertha::Skeid::Protocol::Refusal")
ABresult
0X0
100
111
1317 ? $err->message : 'Invalid request';
1318 6 _refuse_unsendable_request($c, $node_id, $started, $meta, $message);
1319 6 return;
1320 }
1321  
1322 my $url = $rerank->at_server_root($format)
1323 ? _root_url_for_node($route->{url}, $rerank->upstream_path)
1324 60 ▶ : _endpoint_url_for_node($route->{url}, $rerank->upstream_path);
Branch: 1 branch
BranchTrueFalseTotal
$rerank->at_server_root($format) ? : 24 36 60
1325 my $relay = {
1326 # Tokens are reported in three places, none of them where a chat answer has them.
1327 50 ▶ usage => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1328 50 my ($payload, $res) = @_;
1329 50 return $rerank->usage($payload, $res->headers, $format);
1330 },
1331 # Counted off the request, so it needs nothing from the node but its answer.
1332 50 ▶ units => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
1333 50 my ($payload, $res) = @_;
1334 50 ▶ return $res->is_success ? (documents => $documents) : ();
Branch: 1 branch
BranchTrueFalseTotal
$res->is_success ? : 50 0 50
1335 },
1336 22 ▶ (length($format) ? (answer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1337 22 my ($payload, $res) = @_;
1338 22 return $rerank->response_from_upstream($payload, $res->headers, $body, $served_model, $format);
1339 60 ▶ }) : ()),
Branch: 1 branch
BranchTrueFalseTotal
length $format ? : 24 36 60
1340 };
1341  
1342 60 $c->render_later;
1343 60 ▶ _proxy_openai_json_async($c, $url, $upstream_body, $node_id, $started, $meta, sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
1344 60 my ($res, $err, $status, $upstream, $payload) = @_;
1345 60 ▶ return if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 10 50 60
1346 50 ▶ return _render_upstream_response($c, $res, $node_id) unless length $format;
Branch: 1 branch
BranchTrueFalseTotal
unless length $format 29 21 50
1347 21 ▶ $c->res->code($status || 200);
Condition50%1/2
$status || 200
l!l
210
MC/DC0%0/1
$status || 200
$status
Truth table50%1/2
A
A= $status
Aresult
11
00
1348 21 $c->res->headers->header('x-skeid-node' => $node_id);
1349 21 $c->render(json => $payload);
1350 60 }, undef, $relay);
1351 70 });
1352 }
1353  
1354 200 ▶sub _handle_anthropic_messages {
Subroutine _handle_anthropic_messages: called CC=7 SCAR=19.5
1355 200 my ($c) = @_;
1356 200 _extend_client_timeout($c);
1357 200 my $body = $c->req->json;
1358 200 ▶ unless (ref($body) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $body eq "HASH") 2 198 200
1359 2 _render_error($c, 400, 'Invalid JSON body', 'invalid_request_error');
1360 2 return;
1361 }
1362  
1363 198 ▶ my $wants_stream = $body->{stream} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$body->{'stream'} ? : 73 125 198
1364  
1365 # Translation reads the client's body, so a failure there is the client's malformed request
1366 # -- a provider built-in tool skeid cannot forward, or a shape the translator cannot read.
1367 # Uncaught it escapes as Mojolicious' HTML 500; answer a 400 an Anthropic SDK can parse, before
1368 # anything is routed or metered (core karr #216).
1369 198 my $openai_body = eval { Langertha::Skeid::Protocol::Anthropic->request_to_openai($body) };
1370 198 ▶ unless ($openai_body) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($openai_body) 10 188 198
1371 # A deliberate refusal carries a message written for the client. Any other exception can quote
1372 # the request, so its text is neither sent nor logged (k75, k82).
1373 10 my $err = $@;
1374 10 ▶ my $msg = blessed($err) && $err->isa('Langertha::Skeid::Protocol::Refusal')
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($err) && $err->isa('Langertha::Skeid::Protocol::Refusal') ? : 7 3 10
Condition66%2/3
builtin::blessed($err) and $err->isa("Langertha::Skeid::Protocol::Refusal")
!ll&&!rl&&r
037
MC/DC50%1/2
builtin::blessed($err) and $err->isa("Langertha::Skeid::Protocol::Refusal")
builtin::blessed($err) $err->isa("Langertha::Skeid::Protocol::Refusal")
Truth table66%2/3
A and B
A= builtin::blessed($err)B= $err->isa("Langertha::Skeid::Protocol::Refusal")
ABresult
0X0
100
111
1375 ? $err->message : 'Invalid request';
1376 10 _render_error($c, 400, $msg, 'invalid_request_error');
1377 10 return;
1378 }
1379 188 ▶ my $model = $openai_body->{model} // '';
Condition50%1/2
$openai_body->{"model"} // ""
l!l
1880
MC/DC0%0/1
$openai_body->{"model"} // ""
$openai_body->{"model"}
Truth table50%1/2
A
A= $openai_body->{"model"}
Aresult
11
00
1380 188 my $api_key_id = _request_api_key_id($c);
1381  
1382 188 ▶ _begin_route_async($c, $model, $api_key_id, sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
1383 188 my ($route, $node_id, $started, $tier) = @_;
1384 188 ▶ return unless $route;
Branch: 1 branch
BranchTrueFalseTotal
unless $route 7 181 188
1385  
1386 # The alias layer means the model the client asked for and the model the node is asked for
1387 # are two different strings (ADR 0008). The upstream body carries the served model; the
1388 # usage event carries both, or cost attribution silently loses which product was used.
1389 181 my $served_model = _served_model($tier, $model);
1390 181 $openai_body->{model} = $served_model;
1391  
1392 181 my $url = _endpoint_url_for_node($route->{url}, '/chat/completions');
1393 my $meta = {
1394 api_format => 'anthropic',
1395 endpoint => '/v1/messages',
1396 api_key_id => $api_key_id,
1397 provider => 'skeid',
1398 engine => ($route->{engine} // 'openaibase'),
1399 model => $served_model,
1400 requested_model => $model,
1401 181 ▶ route_url => ($route->{url} // ''),
Condition50%2/4
$route->{"engine"} // "openaibase"
l!l
1810
$route->{"url"} // ""
l!l
1810
MC/DC0%0/1
$route->{"engine"} // "openaibase"
$route->{"engine"}
MC/DC0%0/1
$route->{"url"} // ""
$route->{"url"}
Truth table50%1/2
A
A= $route->{"engine"}
Aresult
11
00
Truth table50%1/2
A
A= $route->{"url"}
Aresult
11
00
1402 };
1403  
1404 181 ▶ if ($wants_stream) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wants_stream) 69 112 181
1405 # Ask upstream for a stream in the one dialect Skeid speaks to nodes, and rewrite it at
1406 # the client edge (ADR 0001). include_usage because Anthropic clients read token counts
1407 # from message_delta, and an OpenAI stream omits usage unless asked.
1408 69 $openai_body->{stream} = \1;
1409 69 $openai_body->{stream_options} = { include_usage => \1 };
1410 69 _proxy_openai_stream($c, $url, $openai_body, $node_id, $started, $meta,
1411 Langertha::Skeid::Protocol::Anthropic::Stream->new(model => $model),
1412 _schema_relay($c, $openai_body, 'input_schema'));
1413 69 return;
1414 }
1415  
1416 112 delete $openai_body->{stream};
1417 112 $c->render_later;
1418 112 ▶ _proxy_openai_json_async($c, $url, $openai_body, $node_id, $started, $meta, sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1419 112 my ($res, $err, $status, $upstream, $payload) = @_;
1420 112 ▶ return if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 12 100 112
1421 100 ▶ $c->res->code($status || 200);
Condition50%1/2
$status || 200
l!l
1000
MC/DC0%0/1
$status || 200
$status
Truth table50%1/2
A
A= $status
Aresult
11
00
1422 100 $c->res->headers->header('x-skeid-node' => $node_id);
1423 100 $c->render(json => $payload);
1424 101 ▶ }, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1425 # response_from_openai reads a decoded OpenAI response ($upstream->{choices}, ...); the
1426 # raw Mojo $res would read as all-undef and yield a well-formed but empty envelope (karr #26).
1427 101 return Langertha::Skeid::Protocol::Anthropic->response_from_openai($_[0], $model);
1428 112 }, _schema_relay($c, $openai_body, 'input_schema'));
1429 188 });
1430 }
1431  
1432 # Serves both Ollama completion routes. /api/chat and /api/generate differ only at the edge --
1433 # generate's prompt/system/images become one chat conversation on the way up, and the answer
1434 # carries `response` instead of `message` on the way back (skeid #43) -- so they share the
1435 # route, admission, metering and pricing below and cannot drift apart.
1436 86 my %OLLAMA_FACE = (
1437 chat => {
1438 endpoint => '/api/chat',
1439 request => 'request_to_openai',
1440 response => 'response_from_openai',
1441 shape => 'chat',
1442 },
1443 generate => {
1444 endpoint => '/api/generate',
1445 request => 'generate_request_to_openai',
1446 response => 'generate_response_from_openai',
1447 shape => 'generate',
1448 },
1449 );
1450  
1451 260 ▶sub _handle_ollama {
Subroutine _handle_ollama: called CC=6 SCAR=17.9
1452 260 my ($c, $kind) = @_;
1453 260 _extend_client_timeout($c);
1454 260 my $face = $OLLAMA_FACE{$kind};
1455 260 my $body = $c->req->json;
1456 260 ▶ unless (ref($body) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $body eq "HASH") 4 256 260
1457 4 _render_error($c, 400, 'Invalid JSON body', 'invalid_request_error');
1458 4 return;
1459 }
1460  
1461 # Ollama defaults stream to true when the field is absent, unlike everyone else. A client
1462 # that omits it is asking for a stream and will sit waiting for newline-delimited JSON.
1463 256 ▶ my $wants_stream = exists $body->{stream} ? ($body->{stream} ? 1 : 0) : 1;
Branch: 2 branches
BranchTrueFalseTotal
exists $body->{'stream'} ? : 217 39 256
$body->{'stream'} ? : 70 147 217
1464  
1465 256 my $request_method = $face->{request};
1466 # A body the translator cannot read is the client's malformed request. Uncaught it escapes as
1467 # Mojolicious' HTML 500; answer a 400 in Ollama's shape before anything is routed or metered.
1468 # The exception's text stays out of the answer and the log, as with a dying response translator.
1469 256 my $openai_body = eval { Langertha::Skeid::Protocol::Ollama->$request_method($body) };
1470 256 ▶ unless (ref($openai_body) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $openai_body eq "HASH") 4 252 256
1471 4 _render_error($c, 400, 'Invalid request', 'invalid_request_error');
1472 4 return;
1473 }
1474 252 ▶ my $model = $openai_body->{model} // '';
Condition50%1/2
$openai_body->{"model"} // ""
l!l
2520
MC/DC0%0/1
$openai_body->{"model"} // ""
$openai_body->{"model"}
Truth table50%1/2
A
A= $openai_body->{"model"}
Aresult
11
00
1475 252 my $api_key_id = _request_api_key_id($c);
1476  
1477 252 ▶ _begin_route_async($c, $model, $api_key_id, sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
1478 252 my ($route, $node_id, $started, $tier) = @_;
1479 252 ▶ return unless $route;
Branch: 1 branch
BranchTrueFalseTotal
unless $route 13 239 252
1480  
1481 # The alias layer means the model the client asked for and the model the node is asked for
1482 # are two different strings (ADR 0008). The upstream body carries the served model; the
1483 # usage event carries both, or cost attribution silently loses which product was used.
1484 239 my $served_model = _served_model($tier, $model);
1485 239 $openai_body->{model} = $served_model;
1486  
1487 239 my $url = _endpoint_url_for_node($route->{url}, '/chat/completions');
1488 my $meta = {
1489 api_format => 'ollama',
1490 endpoint => $face->{endpoint},
1491 api_key_id => _request_api_key_id($c),
1492 provider => 'skeid',
1493 engine => ($route->{engine} // 'openaibase'),
1494 model => $served_model,
1495 requested_model => $model,
1496 239 ▶ route_url => ($route->{url} // ''),
Condition50%2/4
$route->{"engine"} // "openaibase"
l!l
2390
$route->{"url"} // ""
l!l
2390
MC/DC0%0/1
$route->{"engine"} // "openaibase"
$route->{"engine"}
MC/DC0%0/1
$route->{"url"} // ""
$route->{"url"}
Truth table50%1/2
A
A= $route->{"engine"}
Aresult
11
00
Truth table50%1/2
A
A= $route->{"url"}
Aresult
11
00
1497 };
1498  
1499 239 ▶ if ($wants_stream) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wants_stream) 104 135 239
1500 104 $openai_body->{stream} = \1;
1501 104 $openai_body->{stream_options} = { include_usage => \1 };
1502 _proxy_openai_stream($c, $url, $openai_body, $node_id, $started, $meta,
1503 104 Langertha::Skeid::Protocol::Ollama::Stream->new(model => $model, shape => $face->{shape}),
1504 _schema_relay($c, $openai_body, 'function', 'parameters'));
1505 104 return;
1506 }
1507  
1508 135 delete $openai_body->{stream};
1509 135 $c->render_later;
1510 135 ▶ _proxy_openai_json_async($c, $url, $openai_body, $node_id, $started, $meta, sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1511 135 my ($res, $err, $status, $upstream, $payload) = @_;
1512 135 ▶ return if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 23 112 135
1513 112 ▶ $c->res->code($status || 200);
Condition50%1/2
$status || 200
l!l
1120
MC/DC0%0/1
$status || 200
$status
Truth table50%1/2
A
A= $status
Aresult
11
00
1514 112 $c->res->headers->header('x-skeid-node' => $node_id);
1515 112 $c->render(json => $payload);
1516 114 ▶ }, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1517 # See the Anthropic path above: the translator needs the decoded upstream body, not the
1518 # Mojo response object, or every field reads undef and the client gets empty content (karr #26).
1519 114 my $response_method = $face->{response};
1520 114 return Langertha::Skeid::Protocol::Ollama->$response_method($_[0]);
1521 135 }, _schema_relay($c, $openai_body, 'function', 'parameters'));
1522 252 });
1523 }
1524  
1525 # Walks the tiers of a requested model (ADR 0008) until one admits the request.
1526 #
1527 # The two ways a tier can fail are not the same and must not be treated the same. A tier with
1528 # no eligible node is skipped immediately -- waiting cannot conjure a node that does not exist.
1529 # A tier whose nodes are all busy is waited on for its own wait_ms, because capacity comes back.
1530 # Only when every tier is exhausted does the request fail, and which failure it is depends on
1531 # whether any tier ever had an eligible node: none did means the model is unroutable (503),
1532 # some did means everything was busy (429).
1533 #
1534 # $cb is called with ($route, $node_id, $started, $tier) on success and with nothing on failure,
1535 # after the error has been rendered.
1536 1071 ▶sub _begin_route_async {
Subroutine _begin_route_async: called CC=16 SCAR=28.4
1537 1071 my ($c, $model, $api_key_id, $cb, %route) = @_;
1538 1071 ▶ $cb ||= sub { };
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$cb ||= sub { }
l!l&&r!l&&!r
107100
MC/DC0%0/2
$cb ||= sub { }
$cb sub { }
Truth table33%1/3
A ||= B
A= $cbB= sub { }
ABresult
1X1
011
000
1539 # What the route is over besides a model name: `documents` plans over the nodes of that kind
1540 # (ADR 0028). Chat routes pass nothing.
1541 1071 ▶ my @kind = length($route{kind} // '') ? (kind => $route{kind}) : ();
Branch: 1 branch
BranchTrueFalseTotal
length($route{'kind'} // '') ? : 58 1013 1071
Condition100%2/2
$route{"kind"} // ""
l!l
581013
MC/DC100%1/1
$route{"kind"} // ""
$route{"kind"}
Truth table100%2/2
A
A= $route{"kind"}
Aresult
11
00
1542 1071 ▶ my $wait_poll_ms = 0 + ($c->skeid->route_wait_poll_ms // 25);
Condition50%1/2
$c->skeid->route_wait_poll_ms // 25
l!l
10710
MC/DC0%0/1
$c->skeid->route_wait_poll_ms // 25
$c->skeid->route_wait_poll_ms
Truth table50%1/2
A
A= $c->skeid->route_wait_poll_ms
Aresult
11
00
1543 1071 ▶ $wait_poll_ms = 1 if $wait_poll_ms < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $wait_poll_ms < 1 0 1071 1071
1544  
1545 1071 ▶ my $decision = $c->skeid->call_function('route.plan', {
Condition50%1/2
$model // ""
l!l
10710
MC/DC0%0/1
$model // ""
$model
Truth table50%1/2
A
A= $model
Aresult
11
00
1546 model => ($model // ''),
1547 api_key_id => $api_key_id,
1548 @kind,
1549 });
1550  
1551 # The key's policy does not grant this model, or grants it but forbids every tier that serves
1552 # it. Both are permission answers, and neither improves by retrying -- so neither may be
1553 # reported as a capacity problem.
1554 1071 ▶ if (!$decision->{permitted}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($decision->{"permitted"}) 14 1057 1071
1555 14 _render_error($c, 403, "Model '$model' is not available for this key", 'permission_error');
1556 14 $cb->();
1557 14 return;
1558 }
1559  
1560 1057 ▶ my $plan = $decision->{tiers} || [];
Condition50%1/2
$decision->{'tiers'} || []
l!l
10570
MC/DC0%0/1
$decision->{'tiers'} || []
$decision->{'tiers'}
Truth table50%1/2
A
A= $decision->{'tiers'}
Aresult
11
00
1561 1057 my $started = time;
1562 1057 my $saw_eligible = 0;
1563 1057 my $last_node_id = '';
1564 1057 my $index = 0;
1565 1057 my $tier_deadline = 0;
1566  
1567 1057 my $tick;
1568 my $wait_timer;
1569 56 ▶ my $fail = sub {
Subroutine __ANON__: called CC=12 SCAR=25.0
1570 # Nothing eligible can mean two different things once a policy is in play: the model is
1571 # unroutable, or it is routable and this key is not allowed at the nodes that serve it.
1572 # Only the failure path pays for telling them apart.
1573 56 ▶ if (!$saw_eligible && grep { @{$_->{deny_tags} || []} } @$plan) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $saw_eligible and grep {@{$_->{"deny_tags"} or [];};} @$plan) 13 43 56
Condition80%4/5
not $saw_eligible and grep {@{$_->{"deny_tags"} or [];};} @$plan
!ll&&!rl&&r
232013
$_->{'deny_tags'} || []
l!l
340
MC/DC100%2/2
not $saw_eligible and grep {@{$_->{"deny_tags"} or [];};} @$plan
not $saw_eligible grep {@{$_->{"deny_tags"} or [];};} @$plan
MC/DC0%0/1
$_->{'deny_tags'} || []
$_->{'deny_tags'}
Truth table100%3/3
not(A) and B
A= not $saw_eligibleB= grep {@{$_->{"deny_tags"} or [];};} @$plan
ABresult
0X0
100
111
Truth table50%1/2
A
A= $_->{'deny_tags'}
Aresult
11
00
1574 13 my $without_deny = 0;
1575 13 for my $tier (@$plan) {
1576 my $state = $c->skeid->call_function('route.state', {
1577 model => ($tier->{model} // ''),
1578 13 ▶ tags => ($tier->{tags} || []),
Condition50%2/4
$tier->{"model"} // ""
l!l
130
$tier->{'tags'} || []
l!l
130
MC/DC0%0/1
$tier->{"model"} // ""
$tier->{"model"}
MC/DC0%0/1
$tier->{'tags'} || []
$tier->{'tags'}
Truth table50%1/2
A
A= $tier->{"model"}
Aresult
11
00
Truth table50%1/2
A
A= $tier->{'tags'}
Aresult
11
00
1579 @kind,
1580 });
1581 13 ▶ $without_deny = 1, last if ref($state) eq 'HASH' && $state->{has_eligible};
Branch: 1 branch
BranchTrueFalseTotal
if ref $state eq "HASH" and $state->{"has_eligible"} 8 5 13
Condition66%2/3
ref $state eq "HASH" and $state->{"has_eligible"}
!ll&&!rl&&r
058
MC/DC50%1/2
ref $state eq "HASH" and $state->{"has_eligible"}
ref $state eq "HASH" $state->{"has_eligible"}
Truth table66%2/3
A and B
A= ref $state eq "HASH"B= $state->{"has_eligible"}
ABresult
0X0
100
111
1582 }
1583 13 ▶ if ($without_deny) {
Branch: 1 branch
BranchTrueFalseTotal
if ($without_deny) 8 5 13
1584 8 _render_error($c, 403, "Model '$model' is not available for this key", 'permission_error');
1585 8 undef $tick;
1586 8 $cb->();
1587 8 return;
1588 }
1589 }
1590  
1591 48 ▶ if (!$saw_eligible) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $saw_eligible) { } 25 23 48
1592 25 _render_error($c, 503, "No healthy node available for model '$model'", 'model_not_found');
1593 } else {
1594 23 my $waited_ms = int((time - $started) * 1000);
1595 23 ▶ my $msg = length($last_node_id)
Branch: 1 branch
BranchTrueFalseTotal
length $last_node_id ? : 0 23 23
1596 ? "Timed out waiting for free capacity on node '$last_node_id' (waited ${waited_ms}ms)"
1597 : "Timed out waiting for free capacity for model '$model' (waited ${waited_ms}ms)";
1598 23 _render_error($c, 429, $msg, 'rate_limit_error');
1599 }
1600 48 undef $tick;
1601 48 $cb->();
1602 48 return;
1603 1057 };
1604  
1605 1344 ▶ $tick = sub {
Subroutine __ANON__: called CC=16 SCAR=28.7
1606 1344 ▶ return $fail->() if $index > $#$plan;
Branch: 1 branch
BranchTrueFalseTotal
if $index > $plan->$#* 56 1288 1344
1607  
1608 1288 my $tier = $plan->[$index];
1609 my %selector = (
1610 model => ($tier->{model} // ''),
1611 tags => ($tier->{tags} || []),
1612 deny_tags => ($tier->{deny_tags} || []),
1613 1288 ▶ (length($tier->{engine} // '') ? (engine => $tier->{engine}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
length($tier->{'engine'} // '') ? : 0 1288 1288
Condition50%4/8
$tier->{"model"} // ""
l!l
12880
$tier->{'tags'} || []
l!l
12880
$tier->{'deny_tags'} || []
l!l
12880
$tier->{"engine"} // ""
l!l
12880
MC/DC0%0/1
$tier->{"model"} // ""
$tier->{"model"}
MC/DC0%0/1
$tier->{'tags'} || []
$tier->{'tags'}
MC/DC0%0/1
$tier->{'deny_tags'} || []
$tier->{'deny_tags'}
MC/DC0%0/1
$tier->{"engine"} // ""
$tier->{"engine"}
Truth table50%1/2
A
A= $tier->{"model"}
Aresult
11
00
Truth table50%1/2
A
A= $tier->{'tags'}
Aresult
11
00
Truth table50%1/2
A
A= $tier->{'deny_tags'}
Aresult
11
00
Truth table50%1/2
A
A= $tier->{"engine"}
Aresult
11
00
1614 @kind,
1615 );
1616  
1617 1288 my $state = $c->skeid->call_function('route.state', \%selector);
1618 1288 ▶ if (ref($state) eq 'HASH' && $state->{has_eligible}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $state eq "HASH" and $state->{"has_eligible"}) 1251 37 1288
Condition66%2/3
ref $state eq "HASH" and $state->{"has_eligible"}
!ll&&!rl&&r
0371251
MC/DC50%1/2
ref $state eq "HASH" and $state->{"has_eligible"}
ref $state eq "HASH" $state->{"has_eligible"}
Truth table66%2/3
A and B
A= ref $state eq "HASH"B= $state->{"has_eligible"}
ABresult
0X0
100
111
1619 1251 $saw_eligible = 1;
1620  
1621 1251 my $route = $c->skeid->call_function('route.next', \%selector)->{node};
1622 1251 ▶ if ($route && ref($route) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($route and ref $route eq "HASH") 999 252 1251
Condition66%2/3
$route and ref $route eq "HASH"
!ll&&!rl&&r
2520999
MC/DC50%1/2
$route and ref $route eq "HASH"
$route ref $route eq "HASH"
Truth table66%2/3
A and B
A= $routeB= ref $route eq "HASH"
ABresult
0X0
100
111
1623 999 my $node_id = $route->{id};
1624 999 ▶ $last_node_id = $node_id if defined $node_id;
Branch: 1 branch
BranchTrueFalseTotal
if defined $node_id 999 0 999
1625 999 ▶ if ($c->skeid->call_function('request.start', { id => $node_id })->{ok}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->skeid->call_function("request.start", {"id", $node_id})->{"ok"}) 999 0 999
1626 # Break the recursive callback's self-reference before control moves into the upstream
1627 # lifecycle. The active call frame keeps it alive until this invocation returns.
1628 999 undef $tick;
1629 999 $cb->($route, $node_id, $started, $tier);
1630 999 return;
1631 }
1632 }
1633  
1634 # Eligible but nothing free: this tier is worth waiting on, up to its own window.
1635 252 ▶ if (time < $tier_deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Langertha::Skeid::Proxy::time() < $tier_deadline) 225 27 252
1636 225 $wait_timer = Mojo::IOLoop->timer($wait_poll_ms / 1000, $tick);
1637 225 return;
1638 }
1639 }
1640  
1641 64 $index++;
1642 64 ▶ $tier_deadline = time + (($plan->[$index] ? ($plan->[$index]{wait_ms} // 0) : 0) / 1000);
Branch: 1 branch
BranchTrueFalseTotal
$plan->[$index] ? : 8 56 64
Condition50%1/2
$plan->[$index]{"wait_ms"} // 0
l!l
80
MC/DC0%0/1
$plan->[$index]{"wait_ms"} // 0
$plan->[$index]{"wait_ms"}
Truth table50%1/2
A
A= $plan->[$index]{"wait_ms"}
Aresult
11
00
1643 64 $tick->();
1644 64 return;
1645 1057 };
1646  
1647 # A client that hangs up while its request waits for capacity stops waiting: the next poll
1648 # would take a slot for nobody and never give it back. $tick is set exactly as long as
1649 # admission is undecided, which tells this finish from the one every answered request emits
1650 # as well. Nothing is rendered, there is nobody to read it. A stand-in controller has no
1651 # transaction.
1652 # On a WebSocket route that has not been upgraded the transaction that closes when the client
1653 # leaves is the handshake's, not the WebSocket one the controller holds (skeid k95).
1654 1057 ▶ if ($c->can('tx') && $c->tx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->can("tx") and $c->tx) 1054 3 1057
Condition66%2/3
$c->can("tx") and $c->tx
!ll&&!rl&&r
301054
MC/DC50%1/2
$c->can("tx") and $c->tx
$c->can("tx") $c->tx
Truth table66%2/3
A and B
A= $c->can("tx")B= $c->tx
ABresult
0X0
100
111
1655 1054 my $tx = $c->tx;
1656 1054 ▶ $tx = $tx->handshake if $tx->is_websocket && !$tx->established && $tx->can('handshake');
Branch: 1 branch
BranchTrueFalseTotal
if $tx->is_websocket and not $tx->established and $tx->can("handshake") 94 960 1054
Condition66%4/6
$tx->is_websocket and not $tx->established and $tx->can("handshake")
!ll&&!rl&&r
960094
$tx->is_websocket and not $tx->established
!ll&&!rl&&r
960094
MC/DC33%1/3
$tx->is_websocket and not $tx->established and $tx->can("handshake")
$tx->is_websocket not $tx->established $tx->can("handshake")
Truth table50%2/4
A and not(B) and C
A= $tx->is_websocketB= not $tx->establishedC= $tx->can("handshake")
ABCresult
0XX0
10X0
1100
1111
1657 1054 ▶ $tx->on(finish => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1658 1054 ▶ return unless $tick;
Branch: 1 branch
BranchTrueFalseTotal
unless $tick 1052 2 1054
1659 2 ▶ Mojo::IOLoop->remove($wait_timer) if defined $wait_timer;
Branch: 1 branch
BranchTrueFalseTotal
if defined $wait_timer 2 0 2
1660 2 undef $tick;
1661 2 $cb->();
1662 1054 });
1663 }
1664  
1665 1057 ▶ $tier_deadline = time + ((@$plan ? ($plan->[0]{wait_ms} // 0) : 0) / 1000);
Branch: 1 branch
BranchTrueFalseTotal
@$plan ? : 1057 0 1057
Condition50%1/2
$plan->[0]{"wait_ms"} // 0
l!l
10570
MC/DC0%0/1
$plan->[0]{"wait_ms"} // 0
$plan->[0]{"wait_ms"}
Truth table50%1/2
A
A= $plan->[0]{"wait_ms"}
Aresult
11
00
1666 1057 $tick->();
1667 1057 return;
1668 }
1669  
1670 # True once the client of this request cannot be answered any more: its transaction was closed,
1671 # or is destroyed already -- the controller holds it weakly. Only asked before anything was
1672 # rendered, because a transaction that was answered is finished as well.
1673 1021 ▶sub _client_gone {
Subroutine _client_gone: called CC=3 SCAR=11.0
1674 1021 my ($c) = @_;
1675 1021 my $tx = $c->tx;
1676 1021 ▶ return (!$tx || $tx->is_finished) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
!$tx || $tx->is_finished ? : 3 1018 1021
Condition66%2/3
not $tx or $tx->is_finished
l!l&&r!l&&!r
301018
MC/DC50%1/2
not $tx or $tx->is_finished
not $tx $tx->is_finished
Truth table66%2/3
not(A) or B
A= not $txB= $tx->is_finished
ABresult
1X1
011
000
1677 }
1678  
1679 # Ends an upstream transaction in flight by closing its connection. The user agent then drops
1680 # the connection instead of pooling it and runs the transaction's completion callback, and the
1681 # node sees the connection close, which is what makes it stop generating. A transaction that
1682 # is still connecting is closed as soon as it has a connection -- one tick later, because the
1683 # user agent is in the middle of setting that connection up when it announces it. A finished
1684 # transaction is left alone: its connection may be serving another request by now.
1685 22 ▶sub _cancel_upstream {
Subroutine _cancel_upstream: called CC=2 SCAR=6.9
1686 22 my ($tx) = @_;
1687 22 ▶ my $close = sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
1688 22 my ($id) = @_;
1689 22 ▶ return if $tx->is_finished;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->is_finished 0 22 22
1690 22 ▶ my $stream = Mojo::IOLoop->stream($id) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $stream = "Mojo::IOLoop"->stream($id) or return 22 0 22
1691 22 $stream->close;
1692 22 return;
1693 22 };
1694 22 ▶ if (defined(my $id = $tx->connection)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $id = $tx->connection)) 21 1 22
1695 21 $close->($id);
1696 21 return;
1697 }
1698 1 ▶ $tx->once(connection => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1699 1 my (undef, $id) = @_;
1700 1 ▶ Mojo::IOLoop->next_tick(sub { $close->($id) });
Subroutine __ANON__: called CC=1 SCAR=0.0
1701 1 });
1702 1 return;
1703 }
1704  
1705 # A status that says the request, not the node, was at fault (k99, ADR 0024). 401, 403 and 429
1706 # are not here: they speak of the node's key or capacity and stay node errors.
1707 95 ▶sub _client_fault {
Subroutine _client_fault: called CC=3 SCAR=11.0
1708 95 my ($status) = @_;
1709 95 ▶ return (defined $status && $status =~ /\A(?:400|404|413|422)\z/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $status && $status =~ /\A(?:400|404|413|422)\z/ ? : 20 75 95
Condition100%3/3
defined $status and $status =~ /\A(?:400|404|413|422)\z/
!ll&&!rl&&r
96620
MC/DC100%2/2
defined $status and $status =~ /\A(?:400|404|413|422)\z/
defined $status $status =~ /\A(?:400|404|413|422)\z/
Truth table100%3/3
A and B
A= defined $statusB= $status =~ /\A(?:400|404|413|422)\z/
ABresult
0X0
100
111
1710 }
1711  
1712 # What a request its client abandoned records (ADR 0004): failed, under the status nginx made
1713 # the convention for "client closed request" -- no answer was delivered, and 499 can be told
1714 # from every status a node or Skeid itself answers with.
1715 14 ▶sub _client_abort_event {
Subroutine _client_abort_event: called CC=1 SCAR=0.0
1716 return (
1717 14 status_code => 499,
1718 ok => 0,
1719 error_type => 'client_abort',
1720 error_message => 'Client closed the connection before the response was complete',
1721 );
1722 }
1723  
1724 # $translate is an optional coderef that turns the decoded upstream answer into what the client
1725 # gets. It runs before the request is finished and metered, so a translator that dies is one
1726 # failed request -- one usage event with ok => 0, an error in the face's own shape -- and the
1727 # callback gets the translated payload as its fifth argument, or nothing after an error.
1728 #
1729 # $relay is what a route relayed in the upstream's own shape brings (ADR 0021), all optional:
1730 # `request`, a code ref called with the user agent, the URL and the upstream headers that builds
1731 # the upstream transaction instead of the JSON one made from $body; `units`, a code ref that
1732 # reads the route's own usage units off the decoded answer and returns them as event fields;
1733 # and, for a stream, `delta_text`, which returns the text of a frame in the route's own dialect.
1734 # Two more for an answer that is not chat-shaped (rerank, skeid k92), both called with the
1735 # decoded answer -- whatever JSON it is -- and the upstream response: `usage` returns the usage
1736 # block to meter, or nothing, where the answer reports its tokens somewhere else than a chat
1737 # answer does; `answer` is $translate for a node whose own dialect is not the client's, and
1738 # fails the same way.
1739 640 ▶sub _proxy_openai_json_async {
Subroutine _proxy_openai_json_async: called CC=4 SCAR=13.9
1740 640 my ($c, $url, $body, $node_id, $started, $meta, $cb, $translate, $relay) = @_;
1741 640 ▶ $meta ||= {};
Condition50%1/2
$meta ||= {}
l!l
6400
MC/DC0%0/1
$meta ||= {}
$meta
Truth table50%1/2
A
A= $meta
Aresult
11
00
1742 640 ▶ $cb ||= sub { };
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$cb ||= sub { }
l!l&&r!l&&!r
64000
MC/DC0%0/2
$cb ||= sub { }
$cb sub { }
Truth table33%1/3
A ||= B
A= $cbB= sub { }
ABresult
1X1
011
000
1743 640 ▶ $relay ||= {};
Condition100%2/2
$relay ||= {}
l!l
396244
MC/DC100%1/1
$relay ||= {}
$relay
Truth table100%2/2
A
A= $relay
Aresult
11
00
1744  
1745 640 my %fwd_headers = _forward_headers($c);
1746 640 ▶ _inject_node_auth_async(\%fwd_headers, $c->skeid, $node_id, sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1747 640 my ($no_key) = @_;
1748  
1749 # The client hung up while the node key was being resolved, whether or not it resolved -- asked
1750 # before the refusal, which would meter and answer a transaction nobody holds any more. Nothing went upstream: the slot
1751 # is given back and nothing is metered, as for every request that was not forwarded.
1752 640 ▶ if (_client_gone($c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_client_gone($c)) 2 638 640
1753 2 $c->skeid->call_function('request.finish', {
1754 id => $node_id,
1755 ok => 0,
1756 aborted => 1,
1757 duration_ms => _duration_ms($started),
1758 });
1759 2 $cb->(undef, 1, 499);
1760 2 return;
1761 }
1762  
1763 638 ▶ if (defined $no_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $no_key) 34 604 638
1764 34 _refuse_unkeyed_node($c, $node_id, $started, $meta, $no_key);
1765 34 $cb->(undef, 1, 503);
1766 34 return;
1767 }
1768 604 my $tx = _upstream_tx($c, $url, \%fwd_headers, $body, $relay);
1769  
1770 # Set by whichever comes first, the upstream's completion or the client hanging up, so that
1771 # request.finish and the usage event happen once.
1772 604 my $closed = 0;
1773  
1774 604 ▶ $c->tx->on(finish => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1775 604 ▶ return if $closed;
Branch: 1 branch
BranchTrueFalseTotal
if $closed 597 7 604
1776 7 $closed = 1;
1777 7 my $duration_ms = _duration_ms($started);
1778 7 $c->skeid->call_function('request.finish', {
1779 id => $node_id,
1780 ok => 0,
1781 aborted => 1,
1782 duration_ms => $duration_ms,
1783 });
1784 7 _record_usage_event($c, {
1785 %$meta,
1786 _client_abort_event(),
1787 node_id => $node_id,
1788 duration_ms => $duration_ms,
1789 metrics => {},
1790 });
1791 7 _cancel_upstream($tx);
1792 7 $cb->(undef, 1, 499);
1793 604 });
1794  
1795 604 ▶ $c->app->ua->start($tx => sub {
Subroutine __ANON__: called CC=25 SCAR=32.3
1796 604 my ($ua, $done) = @_;
1797 # The client hung up and its request was closed then; this is the cancelled transaction.
1798 604 ▶ return if $closed;
Branch: 1 branch
BranchTrueFalseTotal
if $closed 7 597 604
1799 597 $closed = 1;
1800 597 my $duration_ms = _duration_ms($started);
1801 597 my $res = $done->res;
1802  
1803 # Mojo reports an HTTP 4xx/5xx through tx->error too. Observe the response that actually
1804 # arrived before taking that error return, so a 429's Retry-After can gate the next request.
1805 # A transport failure has no useful status or headers and _observe_capacity records nothing.
1806 597 _observe_capacity($c, $node_id, $res);
1807  
1808 597 ▶ if (my $err = $done->error) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $done->error) 44 553 597
1809 $c->skeid->call_function('request.finish', {
1810 id => $node_id,
1811 ok => 0,
1812 44 ▶ _client_fault($err->{code}) ? (rejected => 1) : (),
Branch: 1 branch
BranchTrueFalseTotal
_client_fault($err->{'code'}) ? : 13 31 44
1813 duration_ms => $duration_ms,
1814 });
1815 _record_usage_event($c, {
1816 %$meta,
1817 node_id => $node_id,
1818 status_code => ($err->{code} || 502),
1819 ok => 0,
1820 duration_ms => $duration_ms,
1821 error_type => 'upstream_error',
1822 44 ▶ error_message => ($err->{message} // 'unknown'),
Condition75%3/4
$err->{'code'} || 502
l!l
359
$err->{"message"} // "unknown"
l!l
440
MC/DC100%1/1
$err->{'code'} || 502
$err->{'code'}
MC/DC0%0/1
$err->{"message"} // "unknown"
$err->{"message"}
Truth table100%2/2
A
A= $err->{'code'}
Aresult
11
00
Truth table50%1/2
A
A= $err->{"message"}
Aresult
11
00
1823 metrics => {},
1824 });
1825 44 ▶ _render_error($c, ($err->{code} || 502),
Condition100%2/2
$err->{'code'} || 502
l!l
359
MC/DC100%1/1
$err->{'code'} || 502
$err->{'code'}
Truth table100%2/2
A
A= $err->{'code'}
Aresult
11
00
1826 'Upstream error: ' . _upstream_error_message($err, $done->res->body), 'upstream_error');
1827 44 ▶ $cb->(undef, 1, ($err->{code} || 502));
Condition100%2/2
$err->{'code'} || 502
l!l
359
MC/DC100%1/1
$err->{'code'} || 502
$err->{'code'}
Truth table100%2/2
A
A= $err->{'code'}
Aresult
11
00
1828 44 return;
1829 }
1830  
1831 553 ▶ my $status = $res->code // 200;
Condition50%1/2
$res->code // 200
l!l
5530
MC/DC0%0/1
$res->code // 200
$res->code
Truth table50%1/2
A
A= $res->code
Aresult
11
00
1832 # A route that names a bound leaves a larger answer undecoded (a converted document is
1833 # megabytes, and decoding it is CPU on the loop); its units then say nothing.
1834 my $payload = (defined($relay->{max_parse_bytes}) && ($res->body_size // 0) > $relay->{max_parse_bytes})
1835 553 ▶ ? undef : eval { $res->json };
Branch: 1 branch
BranchTrueFalseTotal
defined $relay->{'max_parse_bytes'} && ($res->body_size // 0) > $relay->{'max_parse_bytes'} ? : 1 552 553
Condition80%4/5
defined $relay->{"max_parse_bytes"} and ($res->body_size // 0) > $relay->{"max_parse_bytes"}
!ll&&!rl&&r
507451
$res->body_size // 0
l!l
460
MC/DC50%1/2
defined $relay->{"max_parse_bytes"} and ($res->body_size // 0) > $relay->{"max_parse_bytes"}
defined $relay->{"max_parse_bytes"} $res->body_size
Truth table66%2/3
A and B
A= defined $relay->{"max_parse_bytes"}B= $res->body_size
ABresult
0X0
100
111
1836  
1837 553 my $translated;
1838 553 ▶ if ($translate || $relay->{answer}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($translate or $relay->{"answer"}) 237 316 553
Condition100%3/3
$translate or $relay->{"answer"}
l!l&&r!l&&!r
21522316
MC/DC100%2/2
$translate or $relay->{"answer"}
$translate $relay->{"answer"}
Truth table100%3/3
A or B
A= $translateB= $relay->{"answer"}
ABresult
1X1
011
000
1839 237 $translated = eval {
1840 $relay->{answer}
1841 237 ▶ ? $relay->{answer}->($payload, $res)
Branch: 2 branches
BranchTrueFalseTotal
$relay->{'answer'} ? : 22 215 237
ref $payload eq 'HASH' ? : 214 1 215
1842 : $translate->(ref($payload) eq 'HASH' ? $payload : {});
1843 };
1844 237 ▶ unless (defined $translated) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $translated) 4 233 237
1845 4 $c->app->log->error('Translating the answer of node ' . $node_id . ' failed');
1846 4 $c->skeid->call_function('request.finish', {
1847 id => $node_id,
1848 ok => 0,
1849 duration_ms => $duration_ms,
1850 });
1851 4 _record_usage_event($c, {
1852 %$meta,
1853 node_id => $node_id,
1854 status_code => 500,
1855 ok => 0,
1856 duration_ms => $duration_ms,
1857 error_type => 'translation_error',
1858 error_message => 'Response translation failed',
1859 metrics => {},
1860 });
1861 4 _render_error($c, 500, 'Response translation failed', 'api_error');
1862 4 $cb->(undef, 1, 500);
1863 4 return;
1864 }
1865 }
1866  
1867 549 ▶ $c->skeid->call_function('request.finish', {
Branch: 1 branch
BranchTrueFalseTotal
$status < 500 ? : 549 0 549
1868 id => $node_id,
1869 ok => ($status < 500) ? 1 : 0,
1870 duration_ms => $duration_ms,
1871 });
1872  
1873 549 my $metrics = {};
1874 549 ▶ if ($relay->{usage}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($relay->{'usage'}) { } 96 453 549
elsif (ref $payload eq 'HASH') { } 449 4 453
1875 96 my $usage = $relay->{usage}->($payload, $res);
1876 96 ▶ $metrics = _priced_metrics($c, $meta, $body, $duration_ms, { usage => $usage })
Branch: 1 branch
BranchTrueFalseTotal
if ref $usage eq "HASH" 48 48 96
1877 if ref($usage) eq 'HASH';
1878 } elsif (ref($payload) eq 'HASH') {
1879 449 ▶ my $tool_calls = eval { [ map { $_->to_hash } Langertha::ToolCall->extract('openai', $payload) ] } || [];
Condition50%1/2
eval { do { [map({$_->to_hash;} 'Langertha::ToolCall'->extract('openai', $payload))] } } || []
l!l
4490
MC/DC0%0/1
eval { do { [map({$_->to_hash;} 'Langertha::ToolCall'->extract('openai', $payload))] } } || []
eval { do { [map({$_->to_hash;} 'Langertha::ToolCall'->extract('openai', $payload))] } }
Truth table50%1/2
A
A= eval { do { [map({$_->to_hash;} 'Langertha::ToolCall'->extract('openai', $payload))] } }
Aresult
11
00
1880 449 $metrics = _priced_metrics($c, $meta, $body, $duration_ms, $payload, $tool_calls);
1881 }
1882  
1883 _record_usage_event($c, {
1884 %$meta,
1885 node_id => $node_id,
1886 status_code => $status,
1887 ok => ($status < 500) ? 1 : 0,
1888 duration_ms => $duration_ms,
1889 metrics => $metrics,
1890 549 ▶ ($relay->{units} ? $relay->{units}->($payload, $res) : ()),
Branch: 2 branches
BranchTrueFalseTotal
$status < 500 ? : 549 0 549
$relay->{'units'} ? : 132 417 549
1891 });
1892  
1893 549 ▶ $cb->($res, 0, $status, (ref($payload) eq 'HASH' ? $payload : {}), $translated);
Branch: 1 branch
BranchTrueFalseTotal
ref $payload eq 'HASH' ? : 520 29 549
1894 604 });
1895 640 });
1896  
1897 640 return;
1898 }
1899  
1900 # $stream is an optional translator (Protocol::*::Stream). Without one the upstream's bytes are
1901 # relayed untouched, which is what an OpenAI client wants and the only path that cannot lose
1902 # anything in translation. With one, each OpenAI chunk is decoded and re-emitted in the
1903 # client's own format -- the same edge-translation seam as the non-streaming path (ADR 0001).
1904 # $relay is as for _proxy_openai_json_async.
1905 261 ▶sub _proxy_openai_stream {
Subroutine _proxy_openai_stream: called CC=3 SCAR=11.0
1906 261 my ($c, $url, $body, $node_id, $started, $meta, $stream, $relay) = @_;
1907 261 ▶ $meta ||= {};
Condition50%1/2
$meta ||= {}
l!l
2610
MC/DC0%0/1
$meta ||= {}
$meta
Truth table50%1/2
A
A= $meta
Aresult
11
00
1908 261 ▶ $relay ||= {};
Condition100%2/2
$relay ||= {}
l!l
90171
MC/DC100%1/1
$relay ||= {}
$relay
Truth table100%2/2
A
A= $relay
Aresult
11
00
1909  
1910 261 my %fwd_headers = _forward_headers($c);
1911  
1912 # render_later before the key resolution, not after: a cold cache means the callback runs on
1913 # a later tick, and Mojolicious would have rendered an empty response by then.
1914 261 $c->render_later;
1915  
1916 261 ▶ _inject_node_auth_async(\%fwd_headers, $c->skeid, $node_id, sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
1917 261 my ($no_key) = @_;
1918  
1919 # The client hung up while the node key was being resolved, whether or not it resolved -- asked
1920 # before the refusal, which would meter and answer a transaction nobody holds any more. Nothing
1921 # went upstream: the slot is given back and nothing is metered, as for every request that was
1922 # not forwarded. Nothing is set up yet, so there is nothing to take apart.
1923 261 ▶ if (_client_gone($c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_client_gone($c)) 1 260 261
1924 1 $c->skeid->call_function('request.finish', {
1925 id => $node_id,
1926 ok => 0,
1927 aborted => 1,
1928 duration_ms => _duration_ms($started),
1929 });
1930 1 return;
1931 }
1932  
1933 260 ▶ return _refuse_unkeyed_node($c, $node_id, $started, $meta, $no_key) if defined $no_key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $no_key 25 235 260
1934 235 my $tx = _upstream_tx($c, $url, \%fwd_headers, $body, $relay);
1935 # Mojolicious would parse an unchunked, exactly-text/event-stream body into its own `sse`
1936 # events and never emit `read` -- the relay would forward nothing (skeid karr #30).
1937 235 $tx->res->content(Langertha::Skeid::Proxy::RelayContent->new);
1938  
1939 235 my $headers_sent = 0;
1940 235 my $had_error = 0;
1941 # Taken now: the header goes out before the first byte, and the request may be past its
1942 # transaction by then.
1943 235 my $request_id = _request_id($c);
1944 # A translator that can report errors in its own format (Anthropic, Ollama) takes the failure
1945 # paths too: an upstream error status before the stream opens becomes a plain HTTP error in
1946 # the client's shape, a failure after it becomes an in-band error event (core karr #224).
1947 235 ▶ my $stream_errors = $stream && $stream->can('error_event');
Condition66%2/3
$stream && $stream->can('error_event')
!ll&&!rl&&r
800155
MC/DC50%1/2
$stream && $stream->can('error_event')
$stream $stream->can('error_event')
Truth table66%2/3
A && B
A= $streamB= $stream->can('error_event')
ABresult
0X0
100
111
1948 235 my $upstream_failed = 0;
1949 235 my $upstream_error_body = '';
1950 235 my $status = 200;
1951 # The upstream's own usage block, verbatim, so a stream is priced by the same metrics.normalize
1952 # call as a non-streamed answer (skeid #41). undef until a frame carries one.
1953 235 my $upstream_usage;
1954 235 my $accumulated_content_bytes = 0;
1955 # UTF-8 bytes of the content this stream relayed or translated, for the usage event (skeid #36).
1956 # A relayed stream counts off the OpenAI deltas it read along; a translated one takes its
1957 # translator's own count -- the text it actually wrote in the client's format. An observation
1958 # recorded beside the token counts, never a substitute for them.
1959 235 ▶ my $content_bytes = sub { $stream ? 0 + ((($stream->usage)[2]) // 0) : $accumulated_content_bytes };
Subroutine __ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
$stream ? : 155 80 235
Condition50%1/2
($stream->usage)[2] // 0
l!l
1550
MC/DC0%0/1
($stream->usage)[2] // 0
($stream->usage)[2]
Truth table50%1/2
A
A= ($stream->usage)[2]
Aresult
11
00
1960 # The route's own usage units (ADR 0021), read off the usage the stream carried so far -- the
1961 # same block a non-streamed answer would have carried. Nothing until a frame had one.
1962 235 ▶ my $units = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1963 235 ▶ return () unless $relay->{units} && ref($upstream_usage) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless $relay->{"units"} and ref $upstream_usage eq "HASH" 226 9 235
Condition100%3/3
$relay->{"units"} and ref $upstream_usage eq "HASH"
!ll&&!rl&&r
21799
MC/DC100%2/2
$relay->{"units"} and ref $upstream_usage eq "HASH"
$relay->{"units"} ref $upstream_usage eq "HASH"
Truth table100%3/3
A and B
A= $relay->{"units"}B= ref $upstream_usage eq "HASH"
ABresult
0X0
100
111
1964 9 return $relay->{units}->({ usage => $upstream_usage });
1965 235 };
1966  
1967 # Upstream chunks arrive faster than they can be written out, so they are queued and drained
1968 # one at a time. Writing each chunk directly would end the response after the first one:
1969 # a dynamic Mojolicious response with no drain callback is finished once its write queue
1970 # empties, and every later chunk then hits a destroyed transaction. The client sees headers,
1971 # no body, and no error.
1972 235 my @queue;
1973 235 my $draining = 0;
1974 235 my $upstream_done = 0;
1975 235 my $finished = 0;
1976  
1977 235 my $drain;
1978 961 ▶ $drain = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
1979 961 ▶ if (!@queue) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@queue) 442 519 961
1980 442 $draining = 0;
1981 442 ▶ if ($upstream_done && !$finished) {
Branch: 1 branch
BranchTrueFalseTotal
if ($upstream_done and not $finished) 184 258 442
Condition66%2/3
$upstream_done and not $finished
!ll&&!rl&&r
2580184
MC/DC50%1/2
$upstream_done and not $finished
$upstream_done not $finished
Truth table66%2/3
A and not(B)
A= $upstream_doneB= not $finished
ABresult
0X0
100
111
1982 184 $finished = 1;
1983 184 $c->finish;
1984 # write_chunk retains its last drain callback. Clear the recursive callback scalar once
1985 # the queue is complete so that callback cannot retain the controller through $drain.
1986 184 undef $drain;
1987 }
1988 442 return;
1989 }
1990 519 $draining = 1;
1991 519 my $chunk = shift @queue;
1992 519 ▶ $c->write_chunk($chunk => sub { $drain->() if $drain });
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if $drain 518 0 518
1993 235 };
1994  
1995 # SSE frames do not respect read boundaries: one read can carry half a frame, and the half
1996 # that completes it arrives in the next. Parsing per read would silently drop the split
1997 # frame -- usually the last one, which is the one carrying usage.
1998 235 my $pending = '';
1999  
2000 # Set by whichever comes first, the upstream's completion, the client hanging up or a
2001 # translator that died, so that request.finish and the usage event happen once.
2002 235 my $closed = 0;
2003  
2004 # A translator that dies on a chunk ends the request here, not in the upstream callback the
2005 # exception would escape from: the upstream is cancelled, the slot given back, one failed usage
2006 # event written, and the client answered in its own face's error shape -- an HTTP error when
2007 # nothing was queued for it yet, an in-band error event when the stream is open. The
2008 # exception's text is neither logged nor sent: it is the translator's own and may quote the
2009 # request.
2010 235 my $wrote = 0;
2011 7 ▶ my $fail_translation = sub {
Subroutine __ANON__: called CC=10 SCAR=25.3
2012 7 ▶ return if $closed;
Branch: 1 branch
BranchTrueFalseTotal
if $closed 0 7 7
2013 7 $closed = 1;
2014 7 $tx->res->content->unsubscribe('read');
2015 7 my $duration_ms = _duration_ms($started);
2016 7 $c->skeid->call_function('request.finish', {
2017 id => $node_id,
2018 ok => 0,
2019 duration_ms => $duration_ms,
2020 });
2021 7 _record_usage_event($c, {
2022 %$meta,
2023 node_id => $node_id,
2024 status_code => 500,
2025 ok => 0,
2026 duration_ms => $duration_ms,
2027 error_type => 'translation_error',
2028 error_message => 'Stream translation failed',
2029 content_bytes => $content_bytes->(),
2030 metrics => _stream_metrics($c, $meta, $body, $duration_ms, $upstream_usage),
2031 $units->(),
2032 });
2033 7 _cancel_upstream($tx);
2034 7 $upstream_done = 1;
2035 7 ▶ if (!$wrote) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($wrote) 4 3 7
2036 4 $c->res->headers->remove($_) for @{$c->res->headers->names};
2037 4 $c->res->headers->header('x-request-id' => $request_id);
2038 4 _render_error($c, 500, 'Stream translation failed', 'api_error');
2039 4 undef $drain;
2040 4 return;
2041 }
2042 3 ▶ my $frame = eval { $stream_errors ? $stream->error_event(500, 'Stream translation failed') : '' };
Branch: 1 branch
BranchTrueFalseTotal
$stream_errors ? : 3 0 3
2043 3 ▶ push @queue, $frame if defined $frame && length $frame;
Branch: 1 branch
BranchTrueFalseTotal
if defined $frame and length $frame 3 0 3
Condition33%1/3
defined $frame and length $frame
!ll&&!rl&&r
003
MC/DC0%0/2
defined $frame and length $frame
defined $frame length $frame
Truth table33%1/3
A and B
A= defined $frameB= length $frame
ABresult
0X0
100
111
2044 3 ▶ $drain->() unless $draining;
Branch: 1 branch
BranchTrueFalseTotal
unless $draining 3 0 3
2045 3 ▶ if (!$draining && !$finished) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $draining || $finished) 0 3 3
Condition33%1/3
$draining || $finished
l!l&&r!l&&!r
300
MC/DC0%0/2
$draining || $finished
$draining $finished
Truth table33%1/3
A || B
A= $drainingB= $finished
ABresult
1X1
011
000
2046 0 $finished = 1;
2047 0 $c->finish;
2048 0 undef $drain;
2049 }
2050 235 };
2051  
2052 632 ▶ $tx->res->content->unsubscribe('read')->on(read => sub {
Subroutine __ANON__: called CC=29 SCAR=33.7
2053 632 my ($content, $bytes) = @_;
2054 # Kept only to lift the upstream's own error message into the error the client gets.
2055 632 ▶ return $upstream_error_body .= $bytes if $upstream_failed;
Branch: 1 branch
BranchTrueFalseTotal
if $upstream_failed 0 632 632
2056 632 ▶ unless ($headers_sent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($headers_sent) 230 402 632
2057 230 ▶ $status = $tx->res->code // 200;
Condition50%1/2
$tx->res->code // 200
l!l
2300
MC/DC0%0/1
$tx->res->code // 200
$tx->res->code
Truth table50%1/2
A
A= $tx->res->code
Aresult
11
00
2058 # The upstream refused before streaming anything. Opening an SSE response for it would
2059 # hand the client a 4xx/5xx event stream with no events in it; leave the response unsent
2060 # and let the completion callback answer it as an error, as the non-streaming path does.
2061 230 ▶ if ($stream_errors && $status >= 400) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stream_errors and $status >= 400) 11 219 230
Condition100%3/3
$stream_errors and $status >= 400
!ll&&!rl&&r
7814111
MC/DC100%2/2
$stream_errors and $status >= 400
$stream_errors $status >= 400
Truth table100%3/3
A and B
A= $stream_errorsB= $status >= 400
ABresult
0X0
100
111
2062 11 $upstream_failed = 1;
2063 11 $upstream_error_body .= $bytes;
2064 11 return;
2065 }
2066 219 $c->res->code($status);
2067 219 for my $name (@{$tx->res->headers->names}) {
2068 921 my $lc = lc($name);
2069 921 ▶ next if _skip_response_header($lc);
Branch: 1 branch
BranchTrueFalseTotal
if _skip_response_header($lc) 221 700 921
2070 # A translated stream is not the upstream's media type any more. Relaying
2071 # text/event-stream to an Ollama client tells it to parse something it does not speak.
2072 700 ▶ next if $stream && $lc eq 'content-type';
Branch: 1 branch
BranchTrueFalseTotal
if $stream and $lc eq "content-type" 141 559 700
Condition100%3/3
$stream and $lc eq "content-type"
!ll&&!rl&&r
256303141
MC/DC100%2/2
$stream and $lc eq "content-type"
$stream $lc eq "content-type"
Truth table100%3/3
A and B
A= $streamB= $lc eq "content-type"
ABresult
0X0
100
111
2073 559 $c->res->headers->header($name => $tx->res->headers->header($name));
2074 }
2075 219 ▶ $c->res->headers->header('content-type' => $stream->content_type) if $stream;
Branch: 1 branch
BranchTrueFalseTotal
if $stream 141 78 219
2076 219 $c->res->headers->header('x-skeid-node' => $node_id);
2077 219 $c->res->headers->header('x-request-id' => $request_id);
2078 219 $headers_sent = 1;
2079 }
2080  
2081 # Parse SSE lines and accumulate usage + content bytes. Without a translator the relayed
2082 # bytes are never modified -- this reads along, it does not rewrite.
2083 621 $pending .= $bytes;
2084 621 my $translated = '';
2085 621 ▶ while ($pending =~ s/\A([^\n]*)\n//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pending =~ s/\A([^\n]*)\n//) 1945 614 2559
2086 1945 my $line = $1;
2087 # SSE makes the space after the colon optional: `data:{...}` is as valid as `data: {...}`.
2088 1945 ▶ next unless $line =~ /^data:\s?(.*?)\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^data:\s?(.*?)\s*$/ 971 974 1945
2089 974 my $payload = $1;
2090 974 ▶ next unless length $payload;
Branch: 1 branch
BranchTrueFalseTotal
unless length $payload 0 974 974
2091  
2092 # OpenAI closes with a literal [DONE] sentinel, which is not JSON and has no equivalent
2093 # in either target format -- the translated stream ends with its own closing events.
2094 974 ▶ next if $payload eq '[DONE]';
Branch: 1 branch
BranchTrueFalseTotal
if $payload eq "[DONE]" 175 799 974
2095  
2096 799 my $json = eval { decode_json($payload) };
2097 799 ▶ next unless $json && ref($json) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless $json and ref $json eq "HASH" 0 799 799
Condition33%1/3
$json and ref $json eq "HASH"
!ll&&!rl&&r
00799
MC/DC0%0/2
$json and ref $json eq "HASH"
$json ref $json eq "HASH"
Truth table33%1/3
A and B
A= $jsonB= ref $json eq "HASH"
ABresult
0X0
100
111
2098  
2099 799 ▶ if (my $delta = $json->{choices}[0]{delta}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $delta = $json->{"choices"}[0]{"delta"}) 718 81 799
2100 718 ▶ if (my $delta_content = $delta->{content}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $delta_content = $delta->{"content"}) 572 146 718
2101 572 $accumulated_content_bytes += Langertha::Skeid::Protocol::utf8_length($delta_content);
2102 }
2103 }
2104 799 ▶ if ($relay->{delta_text}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($relay->{"delta_text"}) 54 745 799
2105 54 my $text = $relay->{delta_text}->($json);
2106 54 ▶ $accumulated_content_bytes += Langertha::Skeid::Protocol::utf8_length($text)
Branch: 1 branch
BranchTrueFalseTotal
if defined $text and length $text 8 46 54
Condition66%2/3
defined $text and length $text
!ll&&!rl&&r
4608
MC/DC50%1/2
defined $text and length $text
defined $text length $text
Truth table66%2/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
2107 if defined($text) && length($text);
2108 }
2109  
2110 799 ▶ if (ref($json->{usage}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $json->{"usage"} eq "HASH") 210 589 799
2111 210 $upstream_usage = _merge_usage($upstream_usage, $json->{usage});
2112 }
2113  
2114 799 ▶ if ($stream) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stream) 405 394 799
2115 405 my $out = eval { $stream->delta($json) };
2116 405 ▶ unless (defined $out) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $out) 7 398 405
2117 7 $c->app->log->error('Translating a stream chunk failed for node ' . $node_id);
2118 7 return $fail_translation->();
2119 }
2120 398 $translated .= $out;
2121 }
2122 }
2123  
2124 # The first read event fires with an empty chunk as soon as the upstream headers are
2125 # parsed, and writing an empty chunk finalizes a Mojolicious response. Relaying it would
2126 # end the stream before its first token -- headers, no body, no error.
2127 614 ▶ if ($stream) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stream) { } 352 262 614
2128 352 ▶ return unless length $translated;
Branch: 1 branch
BranchTrueFalseTotal
unless length $translated 126 226 352
2129 226 $wrote = 1;
2130 226 push @queue, $translated;
2131 } else {
2132 262 ▶ return unless length $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless length $bytes 18 244 262
2133 244 $wrote = 1;
2134 244 push @queue, $bytes;
2135 }
2136 470 ▶ $drain->() unless $draining;
Branch: 1 branch
BranchTrueFalseTotal
unless $draining 384 86 470
2137 235 });
2138  
2139 235 ▶ $c->tx->on(finish => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
2140 # Also emitted when the answer is complete, and then there is nothing left to do here. When
2141 # the client hung up, what is queued has no reader and the drain callback that would take
2142 # the next chunk does not run again: drop the queue and the callback's self-reference, or
2143 # they keep the controller alive.
2144 235 @queue = ();
2145 235 $finished = 1;
2146 235 undef $drain;
2147 235 ▶ return if $closed;
Branch: 1 branch
BranchTrueFalseTotal
if $closed 230 5 235
2148 5 $closed = 1;
2149 5 $tx->res->content->unsubscribe('read');
2150  
2151 # Billed from what the stream reported before the client left; the node spent that, whoever
2152 # read it (ADR 0004).
2153 5 my $duration_ms = _duration_ms($started);
2154 5 $c->skeid->call_function('request.finish', {
2155 id => $node_id,
2156 ok => 0,
2157 aborted => 1,
2158 duration_ms => $duration_ms,
2159 });
2160 5 _record_usage_event($c, {
2161 %$meta,
2162 _client_abort_event(),
2163 node_id => $node_id,
2164 duration_ms => $duration_ms,
2165 content_bytes => $content_bytes->(),
2166 metrics => _stream_metrics($c, $meta, $body, $duration_ms, $upstream_usage),
2167 $units->(),
2168 });
2169 5 _cancel_upstream($tx);
2170 235 });
2171  
2172 235 ▶ $c->app->ua->start($tx => sub {
Subroutine __ANON__: called CC=38 SCAR=37.1
2173 235 my ($ua, $tx_done) = @_;
2174  
2175 # The read listener closes over both the upstream transaction and the client controller.
2176 # Completion means no further bytes can arrive, so remove it before returning from any path;
2177 # otherwise the completed transaction owns the listener that owns the transaction forever.
2178 235 $tx_done->res->content->unsubscribe('read');
2179  
2180 # The client hung up and its request was closed then; this is the cancelled transaction.
2181 235 ▶ return if $closed;
Branch: 1 branch
BranchTrueFalseTotal
if $closed 12 223 235
2182 223 $closed = 1;
2183  
2184 # As on the JSON path, an HTTP error is still a response whose capacity headers matter.
2185 # Observe it before the pre-stream error return; transport failures contribute nothing.
2186 223 _observe_capacity($c, $node_id, $tx_done->res);
2187  
2188 223 ▶ if (my $err = $tx_done->error) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $tx_done->error) 44 179 223
2189 44 $had_error = 1;
2190 44 ▶ unless ($headers_sent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($headers_sent) 16 28 44
2191 16 my $duration_ms = _duration_ms($started);
2192 16 ▶ my $err_status = $err->{code} || 502;
Condition100%2/2
$err->{'code'} || 502
l!l
115
MC/DC100%1/1
$err->{'code'} || 502
$err->{'code'}
Truth table100%2/2
A
A= $err->{'code'}
Aresult
11
00
2193 16 ▶ $c->skeid->call_function('request.finish', {
Branch: 1 branch
BranchTrueFalseTotal
_client_fault($err_status) ? : 3 13 16
2194 id => $node_id,
2195 ok => 0,
2196 _client_fault($err_status) ? (rejected => 1) : (),
2197 duration_ms => $duration_ms,
2198 });
2199 _record_usage_event($c, {
2200 %$meta,
2201 node_id => $node_id,
2202 status_code => $err_status,
2203 ok => 0,
2204 duration_ms => $duration_ms,
2205 error_type => 'upstream_error',
2206 16 ▶ error_message => ($err->{message} // 'unknown'),
Condition50%1/2
$err->{"message"} // "unknown"
l!l
160
MC/DC0%0/1
$err->{"message"} // "unknown"
$err->{"message"}
Truth table50%1/2
A
A= $err->{"message"}
Aresult
11
00
2207 content_bytes => $content_bytes->(),
2208 metrics => _stream_metrics($c, $meta, $body, _duration_ms($started), $upstream_usage),
2209 $units->(),
2210 });
2211 16 _render_error($c, $err_status,
2212 'Upstream error: ' . _upstream_error_message($err, $upstream_error_body), 'upstream_error');
2213 16 undef $drain;
2214 16 return;
2215 }
2216 }
2217  
2218 # The stream is open, so the status is already sent. Mojo::UserAgent reports an upstream
2219 # that hangs up mid-body as no error at all once the status line has arrived, so the body's
2220 # own framing (the chunked terminator, Content-Length) is the witness for that case; a
2221 # close-delimited body cannot be told apart from a complete one. A cut stream failed on
2222 # every face: the usage event and request.finish must not mean something different
2223 # depending on the client's dialect (ADR 0004).
2224 207 my $cut = 0;
2225 207 ▶ if ($headers_sent && !$had_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headers_sent and not $had_error) 179 28 207
Condition66%2/3
$headers_sent and not $had_error
!ll&&!rl&&r
028179
MC/DC50%1/2
$headers_sent and not $had_error
$headers_sent not $had_error
Truth table66%2/3
A and not(B)
A= $headers_sentB= not $had_error
ABresult
0X0
100
111
2226 179 my $content = $tx_done->res->content;
2227 179 ▶ my $framed = $content->is_chunked || length($content->headers->content_length // '');
Condition80%4/5
$content->is_chunked || length($content->headers->content_length // '')
l!l&&r!l&&!r
01718
$content->headers->content_length // ""
l!l
1718
MC/DC50%1/2
$content->is_chunked || length($content->headers->content_length // '')
$content->is_chunked $content->headers->content_length
Truth table66%2/3
A || B
A= $content->is_chunkedB= $content->headers->content_length
ABresult
1X1
011
000
2228 179 ▶ $cut = $framed && !$content->is_finished;
Condition66%2/3
$framed && !$content->is_finished
!ll&&!rl&&r
81710
MC/DC0%0/2
$framed && !$content->is_finished
$framed !$content->is_finished
Truth table66%2/3
A && not(B)
A= $framedB= !$content->is_finished
ABresult
0X0
100
111
2229 179 ▶ $had_error = 1 if $cut;
Branch: 1 branch
BranchTrueFalseTotal
if $cut 0 179 179
2230 }
2231  
2232 # A translator that can say so in-band ends a failed stream with its error event instead of
2233 # a closing sequence that would read as a complete answer.
2234 207 ▶ if ($stream_errors && $headers_sent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stream_errors and $headers_sent) 132 75 207
Condition66%2/3
$stream_errors and $headers_sent
!ll&&!rl&&r
750132
MC/DC50%1/2
$stream_errors and $headers_sent
$stream_errors $headers_sent
Truth table66%2/3
A and B
A= $stream_errorsB= $headers_sent
ABresult
0X0
100
111
2235 132 ▶ if ($had_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($had_error) 14 118 132
2236 14 ▶ my $reason = $cut ? 'Premature connection close' : ($tx_done->error->{message} // 'unknown');
Branch: 1 branch
BranchTrueFalseTotal
$cut ? : 0 14 14
Condition50%1/2
$tx_done->error->{"message"} // "unknown"
l!l
140
MC/DC0%0/1
$tx_done->error->{"message"} // "unknown"
$tx_done->error->{"message"}
Truth table50%1/2
A
A= $tx_done->error->{"message"}
Aresult
11
00
2237 14 my $frame = $stream->error_event(500, "Upstream error: $reason");
2238 14 ▶ if (length $frame) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $frame) 14 0 14
2239 14 push @queue, $frame;
2240 14 ▶ $drain->() unless $draining;
Branch: 1 branch
BranchTrueFalseTotal
unless $draining 14 0 14
2241 }
2242 }
2243 }
2244  
2245 # Finalize a translated stream before closing admission or recording its Usage event: a
2246 # translator can discover a terminal error only when it sees that no more upstream frames
2247 # are coming. Its tail is queued like any other chunk so it still lands after deltas already
2248 # in flight. Keep an unexpected finalizer exception inside the callback boundary and use the
2249 # translator's existing in-band error shape without reflecting internal details.
2250 207 ▶ if ($stream && $headers_sent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stream and $headers_sent) 132 75 207
Condition66%2/3
$stream and $headers_sent
!ll&&!rl&&r
750132
MC/DC50%1/2
$stream and $headers_sent
$stream $headers_sent
Truth table66%2/3
A and B
A= $streamB= $headers_sent
ABresult
0X0
100
111
2251 132 my $tail = '';
2252 132 my $finalized = eval {
2253 132 $tail = $stream->finish;
2254 131 1;
2255 };
2256 132 ▶ unless ($finalized) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($finalized) 1 131 132
2257 1 $had_error = 1;
2258 1 ▶ $tail = eval { $stream_errors
Branch: 1 branch
BranchTrueFalseTotal
$stream_errors ? : 1 0 1
2259 ? $stream->error_event(500, 'Stream translation failed')
2260 : '' };
2261 }
2262 132 ▶ $tail = '' unless defined $tail;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tail 0 132 132
2263 132 ▶ if (length $tail) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $tail) 113 19 132
2264 113 push @queue, $tail;
2265 113 ▶ $drain->() unless $draining;
Branch: 1 branch
BranchTrueFalseTotal
unless $draining 42 71 113
2266 }
2267 }
2268  
2269 # An upstream that reported its failure inside the stream, or a translator that could only
2270 # detect one while finalizing, was answered with an error event; the request still failed.
2271 207 ▶ $had_error = 1 if $stream && $stream->can('errored') && $stream->errored;
Branch: 1 branch
BranchTrueFalseTotal
if $stream and $stream->can("errored") and $stream->errored 21 186 207
Condition83%5/6
$stream and $stream->can("errored") and $stream->errored
!ll&&!rl&&r
7511121
$stream and $stream->can("errored")
!ll&&!rl&&r
750132
MC/DC66%2/3
$stream and $stream->can("errored") and $stream->errored
$stream $stream->can("errored") $stream->errored
Truth table75%3/4
A and B and C
A= $streamB= $stream->can("errored")C= $stream->errored
ABCresult
0XX0
10X0
1100
1111
2272  
2273 207 my $duration_ms = _duration_ms($started);
2274 207 ▶ $c->skeid->call_function('request.finish', {
Branch: 2 branches
BranchTrueFalseTotal
$had_error || $status >= 500 ? : 35 172 207
$had_error && _client_fault($status) ? : 4 203 207
Condition83%5/6
$had_error or $status >= 500
l!l&&r!l&&!r
350172
$had_error and _client_fault($status)
!ll&&!rl&&r
172314
MC/DC50%1/2
$had_error or $status >= 500
$had_error $status >= 500
MC/DC100%2/2
$had_error and _client_fault($status)
$had_error _client_fault($status)
Truth table66%2/3
A or B
A= $had_errorB= $status >= 500
ABresult
1X1
011
000
Truth table100%3/3
A and B
A= $had_errorB= _client_fault($status)
ABresult
0X0
100
111
2275 id => $node_id,
2276 ok => ($had_error || $status >= 500) ? 0 : 1,
2277 # An upstream 4xx relayed after the headers went out is still the client's to fix.
2278 ($had_error && _client_fault($status)) ? (rejected => 1) : (),
2279 duration_ms => $duration_ms,
2280 });
2281  
2282 # Priced from whatever usage the stream carried, also when it was cut: a cut stream still
2283 # spent what its frames reported, and one that reported nothing records zero (ADR 0004).
2284 207 ▶ _record_usage_event($c, {
Branch: 1 branch
BranchTrueFalseTotal
$had_error || $status >= 500 ? : 35 172 207
Condition66%2/3
$had_error or $status >= 500
l!l&&r!l&&!r
350172
MC/DC50%1/2
$had_error or $status >= 500
$had_error $status >= 500
Truth table66%2/3
A or B
A= $had_errorB= $status >= 500
ABresult
1X1
011
000
2285 %$meta,
2286 node_id => $node_id,
2287 status_code => $status,
2288 ok => ($had_error || $status >= 500) ? 0 : 1,
2289 duration_ms => $duration_ms,
2290 content_bytes => $content_bytes->(),
2291 metrics => _stream_metrics($c, $meta, $body, $duration_ms, $upstream_usage),
2292 $units->(),
2293 });
2294  
2295 # Only finish once the queue has drained, or the tail of the stream is cut off. If the
2296 # drain loop is still running it will finish for us when it empties.
2297 207 $upstream_done = 1;
2298 207 ▶ if (!$draining && !$finished) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $draining || $finished) 25 182 207
Condition66%2/3
$draining || $finished
l!l&&r!l&&!r
182025
MC/DC50%1/2
$draining || $finished
$draining $finished
Truth table66%2/3
A || B
A= $drainingB= $finished
ABresult
1X1
011
000
2299 25 $finished = 1;
2300 25 $c->finish;
2301 25 undef $drain;
2302 }
2303 235 });
2304 261 });
2305 }
2306  
2307 # The request of a route on the OpenAI face, whose node speaks the wire the client spoke: the
2308 # client's own bytes with only `model` replaced (Langertha::Skeid::Protocol::RawBody), never the
2309 # decoded body encoded again. Re-encoding sorts the keys of every object, and a node renders
2310 # tool definitions into the prompt in the order they arrive -- so it would hand the model a
2311 # prompt the client did not write, and its prompt cache a prefix it has not seen.
2312 #
2313 # undef, and with it the encoded body, only for bytes the scanner cannot follow.
2314 309 ▶sub _verbatim_relay {
Subroutine _verbatim_relay: called CC=2 SCAR=7.0
2315 309 my ($c, $served_model) = @_;
2316 309 my $bytes = Langertha::Skeid::Protocol::RawBody::with_top_level($c->req->body, model => $served_model);
2317 309 ▶ return undef unless defined $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 309 309
2318 288 ▶ return { request => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2319 288 my ($ua, $url, $headers) = @_;
2320 288 return $ua->build_tx(POST => $url, $headers, $bytes);
2321 309 } };
2322 }
2323  
2324 # The request of a translated route (the Anthropic and Ollama faces): the translated body, with
2325 # each tool's JSON schema written as the client spelled it and the tool definition around it in
2326 # the order the function-calling wire spells it. The body is new, so it cannot go up as the
2327 # client's bytes -- but the tool definitions are what a node renders into the prompt verbatim,
2328 # and those are kept (Langertha::Skeid::Protocol::RawBody). @path is where a tool carries its
2329 # schema on the client's wire.
2330 #
2331 # The body is encoded when the request is built, after the caller has set `model` and `stream`
2332 # on it. undef for a request without tools; a body whose tools no longer line up with the
2333 # client's goes up encoded as before.
2334 420 ▶sub _schema_relay {
Subroutine _schema_relay: called CC=2 SCAR=6.9
2335 420 my ($c, $openai_body, @path) = @_;
2336 420 ▶ my $texts = Langertha::Skeid::Protocol::RawBody::tool_schemas($c->req->body, @path) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $texts = Langertha::Skeid::Protocol::RawBody::tool_schemas($c->req->body, @path) or return undef 5 415 420
2337 5 ▶ return { request => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
2338 5 my ($ua, $url, $headers) = @_;
2339 5 my $bytes = Langertha::Skeid::Protocol::RawBody::encode_with_schemas($openai_body, $texts);
2340 5 ▶ return $ua->build_tx(POST => $url, $headers, json => $openai_body) unless defined $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 5 5
2341 5 delete @{$headers}{ grep { lc($_) eq 'content-type' } keys %$headers };
2342 5 return $ua->build_tx(POST => $url, { %$headers, 'Content-Type' => 'application/json' }, $bytes);
2343 5 } };
2344 }
2345  
2346 # The upstream request of an admitted call: the one JSON POST every translated route makes (ADR
2347 # 0001), or what a relayed route's own `request` builder returns (ADR 0021).
2348 839 ▶sub _upstream_tx {
Subroutine _upstream_tx: called CC=3 SCAR=11.0
2349 839 my ($c, $url, $headers, $body, $relay) = @_;
2350 839 my $ua = $c->app->ua;
2351 839 ▶ return $relay->{request}->($ua, $url, $headers) if $relay && $relay->{request};
Branch: 1 branch
BranchTrueFalseTotal
if $relay and $relay->{"request"} 393 446 839
Condition66%2/3
$relay and $relay->{"request"}
!ll&&!rl&&r
0446393
MC/DC50%1/2
$relay and $relay->{"request"}
$relay $relay->{"request"}
Truth table66%2/3
A and B
A= $relayB= $relay->{"request"}
ABresult
0X0
100
111
2352 446 return $ua->build_tx(POST => $url, $headers, json => $body);
2353 }
2354  
2355 # Renders an error in the shape of the face the client called. The Anthropic Messages face
2356 # gets Anthropic's envelope, {type: "error", error: {type, message}}, with the type taken from
2357 # the HTTP status, because that is what an Anthropic SDK parses and raises on (core karr #224).
2358 # The Ollama face gets Ollama's {error: "<message>"}, a plain string, because that is what the
2359 # Ollama clients decode (skeid #47). Every other face keeps the OpenAI shape it always had, with
2360 # $openai_type as its type and $openai_code, when given, as its code (OpenAI's own 401 carries
2361 # invalid_api_key). The face is read off the stash, which the /v1/messages and /api/* bridges
2362 # set; a controller without one (a unit test's stand-in) is an OpenAI face.
2363 440 ▶sub _render_error {
Subroutine _render_error: called CC=11 SCAR=24.0
2364 440 my ($c, $status, $message, $openai_type, $openai_code) = @_;
2365 # A refused WebSocket handshake is a plain HTTP answer: the upgrade headers the server
2366 # prepared for a 101 do not belong on it (skeid k95).
2367 440 ▶ my $handshake = $c->can('tx') ? $c->tx : undef;
Branch: 1 branch
BranchTrueFalseTotal
$c->can('tx') ? : 438 2 440
2368 440 ▶ if ($handshake && $handshake->is_websocket && !$handshake->established) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handshake and $handshake->is_websocket and not $handshake->established) 13 427 440
Condition83%5/6
$handshake and $handshake->is_websocket and not $handshake->established
!ll&&!rl&&r
427013
$handshake and $handshake->is_websocket
!ll&&!rl&&r
242513
MC/DC66%2/3
$handshake and $handshake->is_websocket and not $handshake->established
$handshake $handshake->is_websocket not $handshake->established
Truth table75%3/4
A and B and not(C)
A= $handshakeB= $handshake->is_websocketC= not $handshake->established
ABCresult
0XX0
10X0
1100
1111
2369 13 $c->res->headers->remove($_) for qw( Upgrade Connection Sec-WebSocket-Accept );
2370 }
2371 440 ▶ my $format = $c->can('stash') ? ($c->stash('skeid.error_format') // '') : '';
Branch: 1 branch
BranchTrueFalseTotal
$c->can('stash') ? : 438 2 440
Condition100%2/2
$c->stash("skeid.error_format") // ""
l!l
135303
MC/DC100%1/1
$c->stash("skeid.error_format") // ""
$c->stash("skeid.error_format")
Truth table100%2/2
A
A= $c->stash("skeid.error_format")
Aresult
11
00
2372 440 ▶ if ($format eq 'anthropic') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq "anthropic") 50 390 440
2373 50 $c->render(json => Langertha::Skeid::Protocol::Anthropic->error_body($status, $message),
2374 status => $status);
2375 50 return;
2376 }
2377 390 ▶ if ($format eq 'ollama') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq "ollama") 85 305 390
2378 85 $c->render(json => Langertha::Skeid::Protocol::Ollama->error_body($message), status => $status);
2379 85 return;
2380 }
2381 305 ▶ $c->render(json => { error => {
Branch: 1 branch
BranchTrueFalseTotal
defined $openai_code ? : 77 228 305
2382 message => $message,
2383 type => $openai_type,
2384 defined($openai_code) ? ( code => $openai_code ) : (),
2385 } }, status => $status);
2386 305 return;
2387 }
2388  
2389 # The message to put after "Upstream error: ". Mojo sets $err->{message} to the HTTP reason
2390 # phrase for a 4xx/5xx ("Bad Request"); the upstream usually said more in its own body, in the
2391 # OpenAI dialect every node speaks (ADR 0001), and that is what the client needs to act on.
2392 60 ▶sub _upstream_error_message {
Subroutine _upstream_error_message: called CC=9 SCAR=22.3
2393 60 my ($err, $body) = @_;
2394 60 ▶ my $json = (defined($body) && length($body)) ? eval { decode_json($body) } : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $body && length $body ? : 46 14 60
Condition66%2/3
defined $body and length $body
!ll&&!rl&&r
01446
MC/DC50%1/2
defined $body and length $body
defined $body length $body
Truth table66%2/3
A and B
A= defined $bodyB= length $body
ABresult
0X0
100
111
2395 60 ▶ if (ref($json) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $json eq "HASH") 46 14 60
2396 46 my $e = $json->{error};
2397 46 ▶ my $msg = ref($e) eq 'HASH' ? $e->{message} : $e;
Branch: 1 branch
BranchTrueFalseTotal
ref $e eq 'HASH' ? : 41 5 46
2398 46 ▶ return "$msg" if defined($msg) && !ref($msg) && length($msg);
Branch: 1 branch
BranchTrueFalseTotal
if defined $msg and not ref $msg and length $msg 43 3 46
Condition66%4/6
defined $msg and not ref $msg and length $msg
!ll&&!rl&&r
3043
defined $msg and not ref $msg
!ll&&!rl&&r
3043
MC/DC33%1/3
defined $msg and not ref $msg and length $msg
defined $msg not ref $msg length $msg
Truth table50%2/4
A and not(B) and C
A= defined $msgB= not ref $msgC= length $msg
ABCresult
0XX0
10X0
1100
1111
2399 }
2400 17 ▶ return $err->{message} // 'unknown';
Condition50%1/2
$err->{"message"} // "unknown"
l!l
170
MC/DC0%0/1
$err->{"message"} // "unknown"
$err->{"message"}
Truth table50%1/2
A
A= $err->{"message"}
Aresult
11
00
2401 }
2402  
2403 316 ▶sub _render_upstream_response {
Subroutine _render_upstream_response: called CC=3 SCAR=11.0
2404 316 my ($c, $res, $node_id) = @_;
2405  
2406 316 $c->res->code($res->code);
2407 316 for my $name (@{$res->headers->names}) {
2408 1511 my $lc = lc($name);
2409 1511 ▶ next if _skip_response_header($lc);
Branch: 1 branch
BranchTrueFalseTotal
if _skip_response_header($lc) 342 1169 1511
2410 1169 $c->res->headers->header($name => $res->headers->header($name));
2411 }
2412 316 $c->res->headers->header('x-skeid-node' => $node_id);
2413 316 $c->res->headers->header('x-request-id' => _request_id($c));
2414 316 $c->res->body($res->body);
2415 316 $c->rendered;
2416 }
2417  
2418 # Hop-by-hop headers describe the client's connection to Skeid, not the request. Forwarding
2419 # them upstream is wrong per RFC 7230 and expensive here in particular: a client that sends
2420 # `Connection: close` -- most benchmark tools and plenty of HTTP libraries do -- made Skeid
2421 # tear down its own upstream connection after every single request, so the connection pool
2422 # never held anything and each request paid for a fresh TCP handshake.
2423 86 my %HOP_BY_HOP = map { $_ => 1 } qw(
2424 connection
2425 keep-alive
2426 proxy-authenticate
2427 proxy-authorization
2428 te
2429 trailer
2430 transfer-encoding
2431 upgrade
2432 );
2433  
2434 # An upstream response header Skeid does not relay to the client: hop-by-hop headers describe
2435 # the node's connection to Skeid, not the client's (a relayed `Connection: close` makes Skeid
2436 # drop the customer's keep-alive connection), and the body is re-framed and never re-encoded
2437 # here. One filter for the JSON, stream and MCP relays (skeid k107). `$lc` is the lowercased name.
2438 2432 ▶sub _skip_response_header {
Subroutine _skip_response_header: called CC=4 SCAR=13.9
2439 2432 my ($lc) = @_;
2440 2432 ▶ return $HOP_BY_HOP{$lc} || $lc eq 'content-length' || $lc eq 'content-encoding' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$HOP_BY_HOP{$lc} || $lc eq 'content-length' || $lc eq 'content-encoding' ? : 563 1869 2432
Condition83%5/6
$HOP_BY_HOP{$lc} or $lc eq "content-length" or $lc eq "content-encoding"
l!l&&r!l&&!r
56301869
$HOP_BY_HOP{$lc} or $lc eq "content-length"
l!l&&r!l&&!r
1264371869
MC/DC66%2/3
$HOP_BY_HOP{$lc} or $lc eq "content-length" or $lc eq "content-encoding"
$HOP_BY_HOP{$lc} $lc eq "content-length" $lc eq "content-encoding"
Truth table75%3/4
A or B or C
A= $HOP_BY_HOP{$lc}B= $lc eq "content-length"C= $lc eq "content-encoding"
ABCresult
1XX1
01X1
0011
0000
2441 }
2442  
2443 992 ▶sub _forward_headers {
Subroutine _forward_headers: called CC=7 SCAR=19.5
2444 992 my ($c) = @_;
2445 992 my %fwd_headers;
2446 992 for my $name (@{$c->req->headers->names}) {
2447 5573 my $lc = lc($name);
2448 5573 ▶ next if $HOP_BY_HOP{$lc};
Branch: 1 branch
BranchTrueFalseTotal
if $HOP_BY_HOP{$lc} 182 5391 5573
2449 5391 ▶ next if $lc eq 'host' || $lc eq 'content-length' || $lc eq 'accept-encoding';
Branch: 1 branch
BranchTrueFalseTotal
if $lc eq "host" or $lc eq "content-length" or $lc eq "accept-encoding" 2863 2528 5391
Condition100%6/6
$lc eq "host" or $lc eq "content-length" or $lc eq "accept-encoding"
l!l&&r!l&&!r
18939702528
$lc eq "host" or $lc eq "content-length"
l!l&&r!l&&!r
9929013498
MC/DC100%3/3
$lc eq "host" or $lc eq "content-length" or $lc eq "accept-encoding"
$lc eq "host" $lc eq "content-length" $lc eq "accept-encoding"
Truth table100%4/4
A or B or C
A= $lc eq "host"B= $lc eq "content-length"C= $lc eq "accept-encoding"
ABCresult
1XX1
01X1
0011
0000
2450 # The body is in hand, so nothing is expected of the node. A node that answered `Expect:
2451 # 100-continue` with its `100 Continue` would have the user agent start a fresh response
2452 # object, one the stream relay is not listening on (skeid k96).
2453 2528 ▶ next if $lc eq 'expect';
Branch: 1 branch
BranchTrueFalseTotal
if $lc eq "expect" 4 2524 2528
2454 2524 $fwd_headers{$name} = $c->req->headers->header($name);
2455 }
2456 992 return %fwd_headers;
2457 }
2458  
2459 # Sets the upstream Authorization header for the selected node, from the KeyBroker
2460 # (api_key_ref) or from the environment (api_key_env), overriding whatever the client sent.
2461 # The callback runs exactly once, and always. It is called with nothing when the request may go
2462 # upstream: the node's key is in place, or the node names no key source and forwards the
2463 # client's header untouched. It is called with a reason when the node names a key source and
2464 # none produced a key, or when the node is no longer in the inventory and what it named cannot
2465 # be known -- the caller must then refuse the request (_refuse_unkeyed_node) rather than call
2466 # the node, because the only credential left in the headers is the customer's own, and the
2467 # pass-through would hand it to the provider (ADR 0003). The reason names the node, the key
2468 # reference and the variable, never a key, and is for the log and the usage event.
2469 #
2470 # Wherever the client's header is not what goes upstream -- a key was injected, or the request
2471 # is refused -- the client's credentials are taken out of the headers first, in any spelling.
2472 #
2473 # Async because resolution can mean a vault round-trip, and this sits between routing and the
2474 # upstream call -- doing it synchronously stalls every other in-flight request for that
2475 # round-trip (ADR 0005). key_async answers from cache without touching the loop, so the
2476 # blocking case is a cold cache, and even then only one request per reference pays for it.
2477 1008 ▶sub _inject_node_auth_async {
Subroutine _inject_node_auth_async: called CC=4 SCAR=13.9
2478 1008 my ($headers_ref, $skeid, $node_id, $cb) = @_;
2479 1008 ▶ $cb ||= sub { };
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$cb ||= sub { }
l!l&&r!l&&!r
100800
MC/DC0%0/2
$cb ||= sub { }
$cb sub { }
Truth table33%1/3
A ||= B
A= $cbB= sub { }
ABresult
1X1
011
000
2480  
2481 1008 ▶ my ($node) = grep { ($_->{id} // '') eq $node_id } @{$skeid->nodes};
Condition50%1/2
$_->{"id"} // ""
l!l
22760
MC/DC0%0/1
$_->{"id"} // ""
$_->{"id"}
Truth table50%1/2
A
A= $_->{"id"}
Aresult
11
00
2482 1008 ▶ unless ($node) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node) 10 998 1008
2483 10 _drop_client_credentials($headers_ref);
2484 10 return $cb->("node '$node_id' is no longer in the inventory, its key source is unknown");
2485 }
2486  
2487 998 ▶ _upstream_key_async($skeid, $node, sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
2488 998 my ($key, $reason) = @_;
2489  
2490 998 ▶ if (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 93 905 998
2491 93 _drop_client_credentials($headers_ref);
2492 # The header the node names carries the raw key; without one it is a Bearer token.
2493 93 my $header = $node->{api_key_header};
2494 93 ▶ if (defined($header) && length($header)) { $headers_ref->{$header} = $key }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $header and length $header) { } 2 91 93
Condition66%2/3
defined $header and length $header
!ll&&!rl&&r
9102
MC/DC50%1/2
defined $header and length $header
defined $header length $header
Truth table66%2/3
A and B
A= defined $headerB= length $header
ABresult
0X0
100
111
2495 91 else { $headers_ref->{Authorization} = "Bearer $key" }
2496 93 return $cb->();
2497 }
2498  
2499 # A node with no key of its own: the documented pass-through.
2500 905 ▶ return $cb->() unless defined $reason;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reason 847 58 905
2501  
2502 # A key source was named and produced nothing. Take the client's credentials out of what
2503 # would go upstream as well, so a caller that ignored the reason still could not leak them.
2504 58 _drop_client_credentials($headers_ref);
2505 58 $cb->($reason);
2506 998 });
2507 998 return;
2508 }
2509  
2510 # Resolves the upstream key of $source -- a node, or an MCP server (skeid k103): whatever names
2511 # its key by api_key_ref and api_key_env. The broker is asked first, the variable stands in when
2512 # the reference resolves nothing. The callback runs exactly once, and always: with the key; with
2513 # nothing when $source names no key source; with undef and a reason when it names one and none
2514 # produced a key. The reason names the key reference and the variable, never a key, and is for
2515 # the log and the usage event (ADR 0003).
2516 1027 ▶sub _upstream_key_async {
Subroutine _upstream_key_async: called CC=7 SCAR=19.5
2517 1027 my ($skeid, $source, $cb) = @_;
2518  
2519 1027 my $ref = $source->{api_key_ref};
2520 1027 ▶ $ref = undef unless defined($ref) && length($ref);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ref and length $ref 937 90 1027
Condition66%2/3
defined $ref and length $ref
!ll&&!rl&&r
937090
MC/DC50%1/2
defined $ref and length $ref
defined $ref length $ref
Truth table66%2/3
A and B
A= defined $refB= length $ref
ABresult
0X0
100
111
2521 1027 my $env_name = $source->{api_key_env};
2522 1027 ▶ $env_name = undef unless defined($env_name) && length($env_name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $env_name and length $env_name 919 108 1027
Condition66%2/3
defined $env_name and length $env_name
!ll&&!rl&&r
9190108
MC/DC50%1/2
defined $env_name and length $env_name
defined $env_name length $env_name
Truth table66%2/3
A and B
A= defined $env_nameB= length $env_name
ABresult
0X0
100
111
2523  
2524 1027 ▶ my $apply = sub {
Subroutine __ANON__: called CC=12 SCAR=24.9
2525 1027 my ($key, $ref_failure) = @_;
2526  
2527 # Fallback: env var
2528 1027 ▶ if (!defined($key) || !length($key)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key and length $key) 981 46 1027
Condition66%2/3
defined $key and length $key
!ll&&!rl&&r
981046
MC/DC50%1/2
defined $key and length $key
defined $key length $key
Truth table66%2/3
A and B
A= defined $keyB= length $key
ABresult
0X0
100
111
2529 981 ▶ if (defined $env_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $env_name) 108 873 981
2530 108 ▶ $key = $ENV{$env_name} // '';
Condition100%2/2
$ENV{$env_name} // ""
l!l
8226
MC/DC100%1/1
$ENV{$env_name} // ""
$ENV{$env_name}
Truth table100%2/2
A
A= $ENV{$env_name}
Aresult
11
00
2531 }
2532 }
2533  
2534 1027 ▶ return $cb->($key) if defined($key) && length($key);
Branch: 1 branch
BranchTrueFalseTotal
if defined $key and length $key 118 909 1027
Condition100%3/3
defined $key and length $key
!ll&&!rl&&r
87336118
MC/DC100%2/2
defined $key and length $key
defined $key length $key
Truth table100%3/3
A and B
A= defined $keyB= length $key
ABresult
0X0
100
111
2535 909 ▶ return $cb->() unless defined($ref) || defined($env_name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ref or defined $env_name 850 59 909
Condition100%3/3
defined $ref or defined $env_name
l!l&&r!l&&!r
3425850
MC/DC100%2/2
defined $ref or defined $env_name
defined $ref defined $env_name
Truth table100%3/3
A or B
A= defined $refB= defined $env_name
ABresult
1X1
011
000
2536  
2537 $cb->(undef, join('; ',
2538 (defined($ref) ? "api_key_ref '$ref' $ref_failure" : ()),
2539 (defined($env_name)
2540 59 ▶ ? "api_key_env '$env_name' is " . (defined($ENV{$env_name}) ? 'empty' : 'not set')
Branch: 3 branches
BranchTrueFalseTotal
defined $ref ? : 34 25 59
defined $env_name ? : 36 23 59
defined $ENV{$env_name} ? : 10 26 36
2541 : ()),
2542 ));
2543 1027 };
2544  
2545 1027 ▶ if (defined($ref) && $skeid->has_key_broker) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ref and $skeid->has_key_broker) 80 947 1027
Condition100%3/3
defined $ref and $skeid->has_key_broker
!ll&&!rl&&r
9371080
MC/DC100%2/2
defined $ref and $skeid->has_key_broker
defined $ref $skeid->has_key_broker
Truth table100%3/3
A and B
A= defined $refB= $skeid->has_key_broker
ABresult
0X0
100
111
2546 80 ▶ $skeid->key_broker->key_async($ref, sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
2547 80 my ($key, $error) = @_;
2548 # The reference may be logged; what it resolves to may not, and neither may a vault
2549 # response body that might carry it (ADR 0003).
2550 80 ▶ warn "KeyBroker resolve failed for '$ref': $error"
Branch: 1 branch
BranchTrueFalseTotal
if defined $error and not defined $key 32 48 80
Condition66%2/3
defined $error and not defined $key
!ll&&!rl&&r
48032
MC/DC50%1/2
defined $error and not defined $key
defined $error not defined $key
Truth table66%2/3
A and not(B)
A= defined $errorB= not defined $key
ABresult
0X0
100
111
2551 if defined($error) && !defined($key);
2552 80 $apply->($key, 'did not resolve');
2553 80 });
2554 80 return;
2555 }
2556  
2557 947 $apply->(undef, 'has no key broker to resolve it');
2558 947 return;
2559 }
2560  
2561 # Header names are case-insensitive and Mojolicious hands an unknown one on as the client
2562 # spelled it, so `X-Api-Key` is as much the client's key as `x-api-key`. An exact-case delete
2563 # lets it travel upstream beside the node's own key.
2564 161 ▶sub _drop_client_credentials {
Subroutine _drop_client_credentials: called CC=2 SCAR=6.9
2565 161 my ($headers_ref) = @_;
2566 161 delete @{$headers_ref}{
2567 161 ▶ grep { lc($_) eq 'authorization' || lc($_) eq 'x-api-key' } keys %$headers_ref
Condition100%3/3
lc $_ eq 'authorization' || lc $_ eq 'x-api-key'
l!l&&r!l&&!r
13483309
MC/DC100%2/2
lc $_ eq 'authorization' || lc $_ eq 'x-api-key'
lc $_ eq 'authorization' lc $_ eq 'x-api-key'
Truth table100%3/3
A || B
A= lc $_ eq 'authorization'B= lc $_ eq 'x-api-key'
ABresult
1X1
011
000
2568 };
2569 161 return;
2570 }
2571  
2572 # The answer to a request whose node names a key source that produced no key: no upstream call,
2573 # and everything an admitted request is owed -- its request.finish, one usage event, an error in
2574 # the shape of the face that was called. 503, not 502: no upstream was asked, so nothing came
2575 # back bad; this Skeid cannot serve the request until its broker or its environment is put
2576 # right, and a client may retry. The client is told no more than that -- the reason carries a
2577 # key reference, which belongs in the log and the usage event, not in a customer's answer.
2578 60 ▶sub _refuse_unkeyed_node {
Subroutine _refuse_unkeyed_node: called CC=1 SCAR=0.0
2579 60 my ($c, $node_id, $started, $meta, $reason) = @_;
2580 60 my $duration_ms = _duration_ms($started);
2581 60 $c->app->log->error("No upstream key for node '$node_id', request refused: $reason");
2582 60 $c->skeid->call_function('request.finish', {
2583 id => $node_id,
2584 ok => 0,
2585 duration_ms => $duration_ms,
2586 });
2587 60 _record_usage_event($c, {
2588 %$meta,
2589 node_id => $node_id,
2590 status_code => 503,
2591 ok => 0,
2592 duration_ms => $duration_ms,
2593 error_type => 'upstream_key_unavailable',
2594 error_message => $reason,
2595 metrics => {},
2596 });
2597 60 _render_error($c, 503, 'The upstream key for this model is not available',
2598 'upstream_key_unavailable');
2599 60 return;
2600 }
2601  
2602 # The answer to a request that holds a slot on a node and cannot be put to it: the body is in a
2603 # shape the node's dialect has no place for, which only shows once the node is known (rerank to a
2604 # `rerank_format` node, skeid k92). As for an unkeyed node, no upstream call and everything an
2605 # admitted request is owed -- its request.finish, one usage event, an error in the face's shape
2606 # -- but 400: the request is the client's to change, and no retry will get it through.
2607 # $message is written for the client and quotes nothing of the request.
2608 6 ▶sub _refuse_unsendable_request {
Subroutine _refuse_unsendable_request: called CC=1 SCAR=0.0
2609 6 my ($c, $node_id, $started, $meta, $message) = @_;
2610 6 my $duration_ms = _duration_ms($started);
2611 6 $c->skeid->call_function('request.finish', {
2612 id => $node_id,
2613 ok => 0,
2614 rejected => 1,
2615 duration_ms => $duration_ms,
2616 });
2617 6 _record_usage_event($c, {
2618 %$meta,
2619 node_id => $node_id,
2620 status_code => 400,
2621 ok => 0,
2622 duration_ms => $duration_ms,
2623 error_type => 'invalid_request_error',
2624 error_message => $message,
2625 metrics => {},
2626 });
2627 6 _render_error($c, 400, $message, 'invalid_request_error');
2628 6 return;
2629 }
2630  
2631 # The bridge of the MCP routes (skeid k103, ADR 0022). Three answers before a handler runs, in
2632 # this order. Without an mcp: section the routes do not exist: the answer is the one an unknown
2633 # path gets, and it comes before the client gate, so a Skeid that has no MCP servers does not
2634 # start answering 401 where it answered 404. Then the client gate, as on every client route.
2635 # Then a key at all, also while client authentication is off: an MCP server is assigned to a
2636 # customer key id, `anonymous` can hold none, and a caller that forgot its key is better told
2637 # so than shown an empty list.
2638 85 ▶sub _authorize_mcp_client {
Subroutine _authorize_mcp_client: called CC=5 SCAR=16.1
2639 85 my ($c) = @_;
2640 85 my $skeid = $c->skeid;
2641 85 $skeid->maybe_reload_config;
2642 85 ▶ unless ($skeid->mcp_enabled) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($skeid->mcp_enabled) 14 71 85
2643 14 $c->reply->not_found;
2644 14 return undef;
2645 }
2646 71 ▶ return undef unless _authorize_client($c);
Branch: 1 branch
BranchTrueFalseTotal
unless _authorize_client($c) 6 65 71
2647  
2648 65 my $api_key_id = _request_api_key_id($c);
2649 65 ▶ return 1 if defined($api_key_id) && $api_key_id ne 'anonymous';
Branch: 1 branch
BranchTrueFalseTotal
if defined $api_key_id and $api_key_id ne "anonymous" 60 5 65
Condition66%2/3
defined $api_key_id and $api_key_id ne "anonymous"
!ll&&!rl&&r
0560
MC/DC50%1/2
defined $api_key_id and $api_key_id ne "anonymous"
defined $api_key_id $api_key_id ne "anonymous"
Truth table66%2/3
A and B
A= defined $api_key_idB= $api_key_id ne "anonymous"
ABresult
0X0
100
111
2650  
2651 5 $c->res->headers->header('WWW-Authenticate' => 'Bearer realm="skeid"');
2652 5 _render_error($c, 401, 'Missing API key', 'invalid_request_error', 'invalid_api_key');
2653 5 return undef;
2654 }
2655  
2656 # GET /v1/mcp/servers: the servers assigned to the presented key, and only what a client needs
2657 # to connect -- the public URL of a relayed server, the command of a stdio one. Whose answer it
2658 # is depends on the key, so it carries the manifest's cache headers. The public URL is the
2659 # config's, else the one this request came in on.
2660 6 ▶sub _handle_mcp_servers {
Subroutine _handle_mcp_servers: called CC=2 SCAR=6.9
2661 6 my ($c) = @_;
2662 6 my $mcp = 'Langertha::Skeid::Protocol::MCP';
2663 6 my $skeid = $c->skeid;
2664 6 _manifest_cache_headers($c);
2665  
2666 6 my $base = $skeid->mcp_public_url;
2667 6 ▶ unless (defined $base) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $base) 1 5 6
2668 1 my $abs = $c->req->url->to_abs;
2669 1 $base = $abs->scheme . '://' . $abs->host_port;
2670 }
2671 $c->render(json => {
2672 object => 'list',
2673 6 data => [ map { $mcp->listing_entry($_, $base) } @{$skeid->mcp_servers_for_key(_request_api_key_id($c))} ],
2674 });
2675 6 return;
2676 }
2677  
2678 # POST|GET|DELETE /mcp/<name>[/<path>] (skeid k103): relayed to the MCP server of that name, if
2679 # it is assigned to the presented key. Everything that is not such a server is one 404 -- an
2680 # unknown name, another key's server, a stdio entry, a sub-path that climbs -- so a key cannot
2681 # learn which names exist. Skeid reads nothing of the request (ADR 0022): the body goes up as
2682 # the bytes it came in, with the headers Protocol::MCP lets through, the server's static
2683 # headers and the server's own key. The client's credentials are never among them.
2684 54 ▶sub _handle_mcp {
Subroutine _handle_mcp: called CC=6 SCAR=17.9
2685 54 my ($c) = @_;
2686 54 my $mcp = 'Langertha::Skeid::Protocol::MCP';
2687 54 my $skeid = $c->skeid;
2688 54 my $req = $c->req;
2689  
2690 54 my $api_key_id = _request_api_key_id($c);
2691 54 my $server = $skeid->mcp_server_for_key($c->stash('mcp_server'), $api_key_id);
2692 my $url = ($server && $mcp->is_relayed($server->{transport}))
2693 54 ▶ ? $mcp->upstream_url($server->{url}, $c->stash('mcp_path'), $req->url->query)
Branch: 1 branch
BranchTrueFalseTotal
$server && $mcp->is_relayed($server->{'transport'}) ? : 37 17 54
Condition100%3/3
$server and $mcp->is_relayed($server->{"transport"})
!ll&&!rl&&r
14337
MC/DC100%2/2
$server and $mcp->is_relayed($server->{"transport"})
$server $mcp->is_relayed($server->{"transport"})
Truth table100%3/3
A and B
A= $serverB= $mcp->is_relayed($server->{"transport"})
ABresult
0X0
100
111
2694 : undef;
2695 54 ▶ unless ($url) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url) 23 31 54
2696 23 _render_error($c, 404, 'No MCP server of this name is available for this key', 'not_found');
2697 23 return;
2698 }
2699  
2700 # A body cut off at the server's request limit is not the body the client sent.
2701 31 ▶ if ($req->is_limit_exceeded) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->is_limit_exceeded) 2 29 31
2702 2 _render_error($c, 413, 'Maximum message size exceeded', 'invalid_request_error');
2703 2 return;
2704 }
2705  
2706 29 _extend_client_timeout($c);
2707 # Before the key resolution: a cold broker cache answers on a later tick.
2708 29 $c->render_later;
2709 29 my $started = time;
2710 my $meta = {
2711 api_format => 'mcp',
2712 endpoint => $mcp->endpoint($server->{name}),
2713 api_key_id => $api_key_id,
2714 provider => 'skeid',
2715 route_url => $server->{url},
2716 29 };
2717 29 ▶ my %headers = ($mcp->request_headers($req->headers), %{$server->{headers} || {}});
Condition50%1/2
$server->{'headers'} || {}
l!l
290
MC/DC0%0/1
$server->{'headers'} || {}
$server->{'headers'}
Truth table50%1/2
A
A= $server->{'headers'}
Aresult
11
00
2718  
2719 29 ▶ _upstream_key_async($skeid, $server, sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
2720 29 my ($key, $no_key) = @_;
2721  
2722 # The client hung up while the key was being resolved: nothing went upstream, nothing is
2723 # metered, as on every other route.
2724 29 ▶ return if _client_gone($c);
Branch: 1 branch
BranchTrueFalseTotal
if _client_gone($c) 0 29 29
2725  
2726 # A key source was named and produced no key: the server is not called without it.
2727 29 ▶ if (defined $no_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $no_key) 1 28 29
2728 1 $c->app->log->error("No upstream key for MCP server '$server->{name}', request refused: $no_key");
2729 1 _record_usage_event($c, {
2730 %$meta,
2731 status_code => 503,
2732 ok => 0,
2733 duration_ms => _duration_ms($started),
2734 error_type => 'upstream_key_unavailable',
2735 error_message => $no_key,
2736 metrics => {},
2737 });
2738 1 _render_error($c, 503, 'The upstream key for this MCP server is not available',
2739 'upstream_key_unavailable');
2740 1 return;
2741 }
2742  
2743 28 ▶ $headers{Authorization} = "Bearer $key" if defined $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key 25 3 28
2744 28 _relay_mcp($c, $server, $url, \%headers, $meta, $started);
2745 29 });
2746 29 return;
2747 }
2748  
2749 # The relay of one MCP request. Its own function, not _proxy_openai_stream: there is no node --
2750 # no slot to give back, no capacity headers, no usage frame to read along -- and an answer's
2751 # shape is not known before it arrives: one JSON body, an event stream, or nothing (202).
2752 # So every answer is relayed the same way, as it arrives. The response is opened -- status and
2753 # headers written out -- the moment the upstream's headers are parsed, before any body byte: a
2754 # standing GET stream is open for the client before its first event. Every chunk is then written
2755 # as it was read. Mojolicious buffers what the client has not taken yet and keeps the order.
2756 #
2757 # Two answers are not relayed, because they are about this gateway and not for its client. An
2758 # upstream 401: towards the client 401 says "your Skeid key is not let in", and an MCP client
2759 # acts on it; the upstream's 401 is about the key Skeid presented. And a redirect: its Location
2760 # is an upstream URL, which a customer is never shown, and Skeid follows none -- the server's
2761 # key would travel along. Both are the operator's to fix: logged, answered 502.
2762 #
2763 # One usage event per request, written when it ends. An event stream has no end that could be
2764 # checked without reading MCP, so however it ends -- the client leaves, the upstream closes it,
2765 # the time limit cuts it -- it is recorded under the status it was opened with. Any other answer
2766 # is held to its framing, and a client that leaves before it is complete is a 499.
2767 28 ▶sub _relay_mcp {
Subroutine _relay_mcp: called CC=2 SCAR=7.0
2768 28 my ($c, $server, $url, $headers, $meta, $started) = @_;
2769 28 my $mcp = 'Langertha::Skeid::Protocol::MCP';
2770  
2771 28 my $body = $c->req->body;
2772 28 ▶ my $tx = $c->app->ua->build_tx($c->req->method => $url => $headers => (length($body) ? ($body) : ()));
Branch: 1 branch
BranchTrueFalseTotal
length $body ? : 24 4 28
2773 # As for every relayed stream: the bytes, not Mojolicious' own SSE events (skeid karr #30).
2774 28 $tx->res->content(Langertha::Skeid::Proxy::RelayContent->new);
2775 # A standing stream has no size: the user agent's response limit (2 GiB by default) would cut
2776 # it at its sum, however long it lived. The time limits bound it, not its length.
2777 28 $tx->res->max_message_size(0);
2778  
2779 # Taken now: the request may be past its transaction when the answer opens.
2780 28 my $request_id = _request_id($c);
2781 28 my ($opened, $event_stream, $status) = (0, 0, 0);
2782 # Why the upstream's answer is not relayed, once its status says so: see above.
2783 27 ▶ my $unrelayed = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
2784 27 my ($code) = @_;
2785 27 ▶ return undef unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 1 26 27
2786 26 ▶ return 'unauthorized' if $code == 401;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 401 1 25 26
2787 25 ▶ return 'redirect' if $code >= 300 && $code < 400;
Branch: 1 branch
BranchTrueFalseTotal
if $code >= 300 and $code < 400 1 24 25
Condition100%3/3
$code >= 300 and $code < 400
!ll&&!rl&&r
2221
MC/DC100%2/2
$code >= 300 and $code < 400
$code >= 300 $code < 400
Truth table100%3/3
A and B
A= $code >= 300B= $code < 400
ABresult
0X0
100
111
2788 24 return undef;
2789 28 };
2790 28 my $refused;
2791 # Set by whichever comes first, the upstream's completion or the client hanging up, so that
2792 # the usage event is written once.
2793 28 my $closed = 0;
2794  
2795 28 ▶ my $record = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2796 28 my (%event) = @_;
2797 28 _record_usage_event($c, { %$meta, duration_ms => _duration_ms($started), metrics => {}, %event });
2798 28 };
2799 24 ▶ my $open = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
2800 24 my ($res) = @_;
2801 24 $status = $res->code;
2802 24 $c->res->code($status);
2803 24 for my $name (@{$res->headers->names}) {
2804 135 ▶ next unless $mcp->response_header_allowed($name);
Branch: 1 branch
BranchTrueFalseTotal
unless $mcp->response_header_allowed($name) 90 45 135
2805 45 $c->res->headers->header($name => $res->headers->header($name));
2806 }
2807 24 $c->res->headers->header('x-request-id' => $request_id);
2808 24 $opened = 1;
2809 28 };
2810  
2811 66 ▶ $tx->res->content->unsubscribe('read')->on(read => sub {
Subroutine __ANON__: called CC=6 SCAR=18.2
2812 66 my ($content, $bytes) = @_;
2813 66 ▶ return if $closed || $refused;
Branch: 1 branch
BranchTrueFalseTotal
if $closed or $refused 0 66 66
Condition33%1/3
$closed or $refused
l!l&&r!l&&!r
0066
MC/DC0%0/2
$closed or $refused
$closed $refused
Truth table33%1/3
A or B
A= $closedB= $refused
ABresult
1X1
011
000
2814 66 ▶ unless ($opened) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opened) 22 44 66
2815 # The first read comes with the upstream's headers, with or without a first chunk.
2816 22 ▶ return if defined($refused = $unrelayed->($tx->res->code));
Branch: 1 branch
BranchTrueFalseTotal
if defined($refused = &$unrelayed($tx->res->code)) 1 21 22
2817 21 $open->($tx->res);
2818 21 $event_stream = $mcp->is_event_stream($tx->res->headers->content_type);
2819 21 $c->write_chunk;
2820 }
2821 65 ▶ $c->write_chunk($bytes) if length $bytes;
Branch: 1 branch
BranchTrueFalseTotal
if length $bytes 61 4 65
2822 28 });
2823  
2824 28 ▶ $c->tx->on(finish => sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
2825 # Also emitted when the answer is complete, and then there is nothing left to do here.
2826 28 ▶ return if $closed;
Branch: 1 branch
BranchTrueFalseTotal
if $closed 26 2 28
2827 2 $closed = 1;
2828 2 $tx->res->content->unsubscribe('read');
2829 2 ▶ $record->(($opened && $event_stream)
Branch: 2 branches
BranchTrueFalseTotal
$opened && $event_stream ? : 1 1 2
$status < 500 ? : 1 0 1
Condition66%2/3
$opened and $event_stream
!ll&&!rl&&r
101
MC/DC50%1/2
$opened and $event_stream
$opened $event_stream
Truth table66%2/3
A and B
A= $openedB= $event_stream
ABresult
0X0
100
111
2830 ? (status_code => $status, ok => ($status < 500 ? 1 : 0))
2831 : _client_abort_event());
2832 2 _cancel_upstream($tx);
2833 28 });
2834  
2835 28 ▶ $c->app->ua->start($tx => sub {
Subroutine __ANON__: called CC=31 SCAR=35.3
2836 28 my ($ua, $done) = @_;
2837 # The read listener closes over the upstream transaction and the client controller: remove
2838 # it on every path, or the completed transaction owns the listener that owns it.
2839 28 $done->res->content->unsubscribe('read');
2840  
2841 # The client hung up and its request was closed then; this is the cancelled transaction.
2842 28 ▶ return if $closed;
Branch: 1 branch
BranchTrueFalseTotal
if $closed 2 26 28
2843 26 $closed = 1;
2844  
2845 26 my $res = $done->res;
2846 26 my $err = $done->error;
2847 26 my $code = $res->code;
2848 # Mojo reports an HTTP 4xx/5xx as an error with a code; one without is the connection's.
2849 26 ▶ my $broken = ($err && !$err->{code}) ? ($err->{message} // 'unknown') : undef;
Branch: 1 branch
BranchTrueFalseTotal
$err && !$err->{'code'} ? : 2 24 26
Condition80%4/5
$err and not $err->{"code"}
!ll&&!rl&&r
2132
$err->{"message"} // "unknown"
l!l
20
MC/DC100%2/2
$err and not $err->{"code"}
$err not $err->{"code"}
MC/DC0%0/1
$err->{"message"} // "unknown"
$err->{"message"}
Truth table100%3/3
A and not(B)
A= $errB= not $err->{"code"}
ABresult
0X0
100
111
Truth table50%1/2
A
A= $err->{"message"}
Aresult
11
00
2850  
2851 26 ▶ $refused //= $unrelayed->($code) unless $opened;
Branch: 1 branch
BranchTrueFalseTotal
unless $opened 6 20 26
Condition100%3/3
$refused //= &$unrelayed($code)
l!l&&r!l&&!r
114
MC/DC100%2/2
$refused //= &$unrelayed($code)
$refused &$unrelayed($code)
Truth table100%3/3
A //= B
A= $refusedB= &$unrelayed($code)
ABresult
1X1
011
000
2852 26 ▶ if (defined($refused) && $refused eq 'unauthorized') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $refused and $refused eq "unauthorized") 1 25 26
Condition100%3/3
defined $refused and $refused eq "unauthorized"
!ll&&!rl&&r
2411
MC/DC100%2/2
defined $refused and $refused eq "unauthorized"
defined $refused $refused eq "unauthorized"
Truth table100%3/3
A and B
A= defined $refusedB= $refused eq "unauthorized"
ABresult
0X0
100
111
2853 $c->app->log->error("MCP server '$server->{name}' answered 401 to the key Skeid presented ("
2854 1 ▶ . (join(', ', map { "$_ '$server->{$_}'" } grep { defined $server->{$_} } qw( api_key_ref api_key_env ))
Condition50%1/2
join(', ', map({"$_ '$server->{$_}'";} grep({defined $server->{$_};} 'api_key_ref', 'api_key_env'))) || 'it names no key source'
l!l
10
MC/DC0%0/1
join(', ', map({"$_ '$server->{$_}'";} grep({defined $server->{$_};} 'api_key_ref', 'api_key_env'))) || 'it names no key source'
join(', ', map({"$_ '$server->{$_}'";} grep({defined $server->{$_};} 'api_key_ref', 'api_key_env')))
Truth table50%1/2
A
A= join(', ', map({"$_ '$server->{$_}'";} grep({defined $server->{$_};} 'api_key_ref', 'api_key_env')))
Aresult
11
00
2855 || 'it names no key source') . ')');
2856 1 $record->(status_code => 502, ok => 0, error_type => 'upstream_unauthorized',
2857 error_message => 'The MCP server answered 401 to the upstream key');
2858 1 _render_error($c, 502, 'Upstream error: the MCP server did not accept the gateway\'s credential',
2859 'upstream_error');
2860 1 return;
2861 }
2862 25 ▶ if (defined $refused) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $refused) 1 24 25
2863 1 ▶ my $location = $res->headers->location // '';
Condition50%1/2
$res->headers->location // ""
l!l
10
MC/DC0%0/1
$res->headers->location // ""
$res->headers->location
Truth table50%1/2
A
A= $res->headers->location
Aresult
11
00
2864 1 $c->app->log->error("MCP server '$server->{name}' answered $code with a redirect to '$location': "
2865 . 'no redirect is followed, configure the URL it leads to');
2866 1 $record->(status_code => 502, ok => 0, error_type => 'upstream_redirect',
2867 error_message => "The MCP server answered $code with a redirect");
2868 1 _render_error($c, 502, 'Upstream error: the MCP server answered with a redirect', 'upstream_error');
2869 1 return;
2870 }
2871  
2872 24 ▶ unless ($opened) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opened) 4 20 24
2873 # No answer, or one whose connection broke before its body began.
2874 4 ▶ if (!defined($code) || defined($broken)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $code or defined $broken) 1 3 4
Condition66%2/3
not defined $code or defined $broken
l!l&&r!l&&!r
103
MC/DC50%1/2
not defined $code or defined $broken
not defined $code defined $broken
Truth table66%2/3
not(A) or B
A= not defined $codeB= defined $broken
ABresult
1X1
011
000
2875 1 ▶ my $message = $broken // 'unknown';
Condition50%1/2
$broken // "unknown"
l!l
10
MC/DC0%0/1
$broken // "unknown"
$broken
Truth table50%1/2
A
A= $broken
Aresult
11
00
2876 1 $record->(status_code => 502, ok => 0, error_type => 'upstream_error', error_message => $message);
2877 1 _render_error($c, 502, "Upstream error: $message", 'upstream_error');
2878 1 return;
2879 }
2880 # An answer without a body (202, 204): no read ever came.
2881 3 $open->($res);
2882 3 ▶ $record->(status_code => $status, ok => ($status < 500 ? 1 : 0));
Branch: 1 branch
BranchTrueFalseTotal
$status < 500 ? : 3 0 3
2883 3 $c->rendered;
2884 3 return;
2885 }
2886  
2887 # The answer is open, so its status is sent. An event stream ends however it ends. Anything
2888 # else is held to its framing: Mojo::UserAgent reports an upstream that hangs up mid-body as
2889 # no error at all once the status line has arrived.
2890 20 my $cut;
2891 20 ▶ unless ($event_stream) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($event_stream) 17 3 20
2892 17 my $content = $res->content;
2893 17 ▶ my $framed = $content->is_chunked || length($content->headers->content_length // '');
Condition40%2/5
$content->is_chunked || length($content->headers->content_length // '')
l!l&&r!l&&!r
0170
$content->headers->content_length // ""
l!l
170
MC/DC0%0/2
$content->is_chunked || length($content->headers->content_length // '')
$content->is_chunked $content->headers->content_length
Truth table33%1/3
A || B
A= $content->is_chunkedB= $content->headers->content_length
ABresult
1X1
011
000
2894 17 ▶ $cut = $broken // (($framed && !$content->is_finished) ? 'Premature connection close' : undef);
Branch: 1 branch
BranchTrueFalseTotal
$framed && !$content->is_finished ? : 0 17 17
Condition40%2/5
$broken // ($framed && !$content->is_finished ? "Premature connection close" : undef)
l!l
017
$framed and not $content->is_finished
!ll&&!rl&&r
0170
MC/DC0%0/1
$broken // ($framed && !$content->is_finished ? "Premature connection close" : undef)
$broken
Truth table50%1/2
A
A= $broken
Aresult
11
00
2895 }
2896 # An event stream has no framing to check, but a time limit cutting it is told by the user
2897 # agent: that one is not a clean end, whatever status it was opened with.
2898 20 ▶ my $timeout = ($event_stream && defined($broken) && $broken =~ /timeout/i) ? $broken : undef;
Branch: 1 branch
BranchTrueFalseTotal
$event_stream && defined $broken && $broken =~ /timeout/i ? : 1 19 20
Condition83%5/6
$event_stream and defined $broken and $broken =~ /timeout/i
!ll&&!rl&&r
1901
$event_stream and defined $broken
!ll&&!rl&&r
1721
MC/DC66%2/3
$event_stream and defined $broken and $broken =~ /timeout/i
$event_stream defined $broken $broken =~ /timeout/i
Truth table75%3/4
A and B and C
A= $event_streamB= defined $brokenC= $broken =~ /timeout/i
ABCresult
0XX0
10X0
1100
1111
2899 20 ▶ $record->(
Branch: 3 branches
BranchTrueFalseTotal
!(defined $cut || defined $timeout) && $status < 500 ? : 18 2 20
defined $cut ? : 0 20 20
defined $timeout ? : 1 19 20
Condition83%5/6
not defined $cut || defined $timeout and $status < 500
!ll&&!rl&&r
1118
defined $cut || defined $timeout
l!l&&r!l&&!r
0119
MC/DC66%2/3
not defined $cut || defined $timeout and $status < 500
defined $cut defined $timeout $status < 500
Truth table75%3/4
not(A || B) and C
A= defined $cutB= defined $timeoutC= $status < 500
ABCresult
1XX0
01X0
0000
0011
2900 status_code => $status,
2901 ok => (!defined($cut) && !defined($timeout) && $status < 500) ? 1 : 0,
2902 (defined($cut) ? (error_type => 'upstream_error', error_message => $cut) : ()),
2903 (defined($timeout) ? (error_type => 'upstream_timeout', error_message => $timeout) : ()),
2904 );
2905 20 $c->finish;
2906 28 });
2907 28 return;
2908 }
2909  
2910 # The free capacity probe (ADR 0009): a commercial provider will not tell us its queue depth,
2911 # but it puts its rate-limit state on every response we already have in hand. Reading it costs
2912 # no extra request -- which is the whole reason this is worth doing on the request path at all.
2913 #
2914 # Pulls the handful of headers by name rather than walking all of them; this runs per response.
2915 86 my @CAPACITY_HEADERS = Langertha::Skeid->capacity_header_names;
2916  
2917 909 ▶sub _observe_capacity {
Subroutine _observe_capacity: called CC=10 SCAR=23.5
2918 909 my ($c, $node_id, $res) = @_;
2919 909 ▶ return unless defined($node_id) && length($node_id);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node_id and length $node_id 0 909 909
Condition33%1/3
defined $node_id and length $node_id
!ll&&!rl&&r
00909
MC/DC0%0/2
defined $node_id and length $node_id
defined $node_id length $node_id
Truth table33%1/3
A and B
A= defined $node_idB= length $node_id
ABresult
0X0
100
111
2920 909 ▶ return unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 909 909
2921 909 ▶ my $headers = $res->headers or return;
Branch: 1 branch
BranchTrueFalseTotal
my $headers = $res->headers or return 909 0 909
2922  
2923 909 my %found;
2924 909 for my $name (@CAPACITY_HEADERS) {
2925 13635 my $value = $headers->header($name);
2926 13635 ▶ $found{$name} = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 7 13628 13635
2927 }
2928 909 ▶ my $status = $res->code // 0;
Condition100%2/2
$res->code // 0
l!l
89514
MC/DC100%1/1
$res->code // 0
$res->code
Truth table100%2/2
A
A= $res->code
Aresult
11
00
2929 909 ▶ return unless %found || $status == 429;
Branch: 1 branch
BranchTrueFalseTotal
unless %found or $status == 429 902 7 909
Condition100%3/3
%found or $status == 429
l!l&&r!l&&!r
52902
MC/DC100%2/2
%found or $status == 429
%found $status == 429
Truth table100%3/3
A or B
A= %foundB= $status == 429
ABresult
1X1
011
000
2930  
2931 7 $c->skeid->call_function('capacity.observe', {
2932 id => $node_id,
2933 headers => \%found,
2934 status => $status,
2935 });
2936 7 return;
2937 }
2938  
2939 1738 ▶sub _extract_request_api_key {
Subroutine _extract_request_api_key: called CC=7 SCAR=19.5
2940 1738 my ($c) = @_;
2941 1738 my $auth = $c->req->headers->authorization;
2942 1738 my $x_api_key = $c->req->headers->header('x-api-key');
2943 1738 ▶ my $raw = defined($auth) ? $auth : (defined($x_api_key) ? $x_api_key : '');
Branch: 2 branches
BranchTrueFalseTotal
defined $auth ? : 793 945 1738
defined $x_api_key ? : 101 844 945
2944 # A browser cannot set a header on a WebSocket: the realtime route takes its key from the
2945 # subprotocol list as a last resort (skeid k95), only ever for the one route, and never
2946 # before a header. It is read here and answered nowhere.
2947 1738 ▶ if (!length($raw) && $c->req->url->path->to_route =~ m{\A/v1/realtime/?\z}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not length $raw and $c->req->url->path->to_route =~ m[\A/v1/realtime/?\z]) 88 1650 1738
Condition100%3/3
not length $raw and $c->req->url->path->to_route =~ m[\A/v1/realtime/?\z]
!ll&&!rl&&r
89475688
MC/DC100%2/2
not length $raw and $c->req->url->path->to_route =~ m[\A/v1/realtime/?\z]
not length $raw $c->req->url->path->to_route =~ m[\A/v1/realtime/?\z]
Truth table100%3/3
not(A) and B
A= not length $rawB= $c->req->url->path->to_route =~ m[\A/v1/realtime/?\z]
ABresult
0X0
100
111
2948 88 my $key = Langertha::Skeid::Protocol::Realtime->subprotocol_key(
2949 $c->req->headers->sec_websocket_protocol);
2950 88 ▶ return ($key, $key) if defined $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key 6 82 88
2951 }
2952 1732 ▶ my $api_key = $raw // '';
Condition50%1/2
$raw // ""
l!l
17320
MC/DC0%0/1
$raw // ""
$raw
Truth table50%1/2
A
A= $raw
Aresult
11
00
2953 1732 $api_key =~ s/^Bearer\s+//i;
2954 1732 return ($raw, $api_key);
2955 }
2956  
2957 # Who the caller is. Everything downstream hangs off this: the routing policy that decides
2958 # which nodes they may reach, and the usage event they get billed for. So it may only be
2959 # derived from something the caller had to prove -- the key they presented.
2960 #
2961 # x-skeid-key-id is honoured only when the deployment says it authenticates the caller before
2962 # Skeid sees the request (routing.trust_key_id_header). Believing it unconditionally would let
2963 # any client name itself into another customer's policy, and into another customer's bill.
2964 1747 ▶sub _request_api_key_id {
Subroutine _request_api_key_id: called CC=5 SCAR=16.8
2965 1747 my ($c) = @_;
2966  
2967 1747 ▶ if ($c->skeid->trust_key_id_header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->skeid->trust_key_id_header) 9 1738 1747
2968 9 ▶ my $forced = $c->req->headers->header('x-skeid-key-id')
Condition66%2/3
$c->req->headers->header("x-skeid-key-id") // $c->req->headers->header("x-api-key-id")
l!l&&r!l&&!r
720
MC/DC0%0/2
$c->req->headers->header("x-skeid-key-id") // $c->req->headers->header("x-api-key-id")
$c->req->headers->header("x-skeid-key-id") $c->req->headers->header("x-api-key-id")
Truth table66%2/3
A // B
A= $c->req->headers->header("x-skeid-key-id")B= $c->req->headers->header("x-api-key-id")
ABresult
1X1
011
000
2969 // $c->req->headers->header('x-api-key-id');
2970 9 ▶ return $forced if defined($forced) && length($forced);
Branch: 1 branch
BranchTrueFalseTotal
if defined $forced and length $forced 9 0 9
Condition33%1/3
defined $forced and length $forced
!ll&&!rl&&r
009
MC/DC0%0/2
defined $forced and length $forced
defined $forced length $forced
Truth table33%1/3
A and B
A= defined $forcedB= length $forced
ABresult
0X0
100
111
2971 }
2972  
2973 1738 my (undef, $api_key) = _extract_request_api_key($c);
2974 1738 return $c->skeid->key_id_for_key($api_key);
2975 }
2976  
2977 # The id taken when the request arrived; a client's own x-request-id is taken over, else one is
2978 # made up. Before that hook ran (a bare controller) it is worked out from the request, and once
2979 # the transaction is gone it must come from the stash.
2980 3858 ▶sub _request_id {
Subroutine _request_id: called CC=5 SCAR=16.2
2981 3858 my ($c) = @_;
2982 3858 my $kept = $c->stash('skeid.request_id');
2983 3858 ▶ return $kept if defined($kept) && length($kept);
Branch: 1 branch
BranchTrueFalseTotal
if defined $kept and length $kept 1603 2255 3858
Condition66%2/3
defined $kept and length $kept
!ll&&!rl&&r
225501603
MC/DC50%1/2
defined $kept and length $kept
defined $kept length $kept
Truth table66%2/3
A and B
A= defined $keptB= length $kept
ABresult
0X0
100
111
2984 2255 my $rid = $c->req->headers->header('x-request-id');
2985 2255 ▶ return $rid if defined($rid) && length($rid);
Branch: 1 branch
BranchTrueFalseTotal
if defined $rid and length $rid 25 2230 2255
Condition66%2/3
defined $rid and length $rid
!ll&&!rl&&r
2230025
MC/DC50%1/2
defined $rid and length $rid
defined $rid length $rid
Truth table66%2/3
A and B
A= defined $ridB= length $rid
ABresult
0X0
100
111
2986 2230 return 'req_' . int(time * 1000) . '_' . int(rand(1_000_000));
2987 }
2988  
2989 1024 ▶sub _record_usage_event {
Subroutine _record_usage_event: called CC=38 SCAR=41.5
2990 1024 my ($c, $args) = @_;
2991 1024 ▶ $args ||= {};
Condition50%1/2
$args ||= {}
l!l
10240
MC/DC0%0/1
$args ||= {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
2992 1024 ▶ my $metrics = ref($args->{metrics}) eq 'HASH' ? $args->{metrics} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $args->{'metrics'} eq 'HASH' ? : 1024 0 1024
2993 1024 ▶ my $usage = ref($metrics->{usage}) eq 'HASH' ? $metrics->{usage} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $metrics->{'usage'} eq 'HASH' ? : 0 1024 1024
2994 my $safe_metrics = {
2995 %$metrics,
2996 usage => {
2997 input => 0 + ($usage->{input} // $usage->{prompt_tokens} // 0),
2998 output => 0 + ($usage->{output} // $usage->{completion_tokens} // 0),
2999 total => 0 + ($usage->{total} // 0),
3000 # This reshaping drops prompt_tokens_details, so the cache count has to be carried across
3001 # it explicitly or record_usage never sees it. Both paths carry it as a flat
3002 # metrics->{cached_tokens}, from metrics.normalize or the raw payload (k27, skeid #41).
3003 1024 ▶ cached => (_cached_tokens($usage) || 0 + ($metrics->{cached_tokens} // 0)),
Condition52%9/17
($usage->{"input"} // $usage->{"prompt_tokens"}) // 0
l!l
01024
$usage->{"input"} // $usage->{"prompt_tokens"}
l!l&&r!l&&!r
001024
($usage->{"output"} // $usage->{"completion_tokens"}) // 0
l!l
01024
$usage->{"output"} // $usage->{"completion_tokens"}
l!l&&r!l&&!r
001024
$usage->{"total"} // 0
l!l
01024
_cached_tokens($usage) || 0 + ($metrics->{'cached_tokens'} // 0)
l!l&&r!l&&!r
0102922
$metrics->{"cached_tokens"} // 0
l!l
102922
MC/DC0%0/2
($usage->{"input"} // $usage->{"prompt_tokens"}) // 0
$usage->{"input"} $usage->{"prompt_tokens"}
MC/DC0%0/2
($usage->{"output"} // $usage->{"completion_tokens"}) // 0
$usage->{"output"} $usage->{"completion_tokens"}
MC/DC0%0/1
$usage->{"total"} // 0
$usage->{"total"}
MC/DC50%1/2
_cached_tokens($usage) || 0 + ($metrics->{'cached_tokens'} // 0)
_cached_tokens($usage) 0 + ($metrics->{'cached_tokens'} // 0)
MC/DC100%1/1
$metrics->{"cached_tokens"} // 0
$metrics->{"cached_tokens"}
Truth table33%1/3
A // B
A= $usage->{"input"}B= $usage->{"prompt_tokens"}
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $usage->{"output"}B= $usage->{"completion_tokens"}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $usage->{"total"}
Aresult
11
00
Truth table66%2/3
A || B
A= _cached_tokens($usage)B= 0 + ($metrics->{'cached_tokens'} // 0)
ABresult
1X1
011
000
Truth table100%2/2
A
A= $metrics->{"cached_tokens"}
Aresult
11
00
3004 },
3005 };
3006  
3007 1024 my $request_id = _request_id($c);
3008 1024 my $recorded = eval {
3009 $c->skeid->call_function('usage.record', {
3010 created_at => Langertha::Skeid::Protocol::iso8601_now(),
3011 request_id => $request_id,
3012 api_format => ($args->{api_format} // ''),
3013 requested_model => ($args->{requested_model} // $args->{model} // ''),
3014 endpoint => ($args->{endpoint} // ''),
3015 api_key_id => ($args->{api_key_id} // 'anonymous'),
3016 provider => ($args->{provider} // 'skeid'),
3017 engine => ($args->{engine} // ''),
3018 model => ($args->{model} // ''),
3019 node_id => ($args->{node_id} // ''),
3020 route_url => ($args->{route_url} // ''),
3021 status_code => 0 + ($args->{status_code} // 0),
3022 ok => ($args->{ok} ? 1 : 0),
3023 duration_ms => 0 + ($args->{duration_ms} // 0),
3024 error_type => ($args->{error_type} // ''),
3025 error_message => ($args->{error_message} // ''),
3026 # Streamed requests only; absent otherwise, so the event says "not measured" (skeid #36).
3027 (defined($args->{content_bytes}) ? (content_bytes => 0 + $args->{content_bytes}) : ()),
3028 # Audio routes only, and only when the node reported it (skeid k91, ADR 0021).
3029 (defined($args->{audio_seconds}) ? (audio_seconds => 0 + $args->{audio_seconds}) : ()),
3030 # The rerank route only, and only for a request the node answered (skeid k92).
3031 (defined($args->{documents}) ? (documents => 0 + $args->{documents}) : ()),
3032 # The documents routes, when the answer carried a page count (skeid k94, ADR 0028).
3033 1024 ▶ (defined($args->{pages}) ? (pages => 0 + $args->{pages}) : ()),
Branch: 5 branches
BranchTrueFalseTotal
$args->{'ok'} ? : 802 222 1024
defined $args->{'content_bytes'} ? : 240 784 1024
defined $args->{'audio_seconds'} ? : 30 994 1024
defined $args->{'documents'} ? : 96 928 1024
defined $args->{'pages'} ? : 36 988 1024
Condition72%21/29
$args->{"api_format"} // ""
l!l
10240
($args->{"requested_model"} // $args->{"model"}) // ""
l!l
99529
$args->{"requested_model"} // $args->{"model"}
l!l&&r!l&&!r
995029
$args->{"endpoint"} // ""
l!l
10240
$args->{"api_key_id"} // "anonymous"
l!l
10240
$args->{"provider"} // "skeid"
l!l
10240
$args->{"engine"} // ""
l!l
99529
$args->{"model"} // ""
l!l
99529
$args->{"node_id"} // ""
l!l
99529
$args->{"route_url"} // ""
l!l
10240
$args->{"status_code"} // 0
l!l
10240
$args->{"duration_ms"} // 0
l!l
10240
$args->{"error_type"} // ""
l!l
245779
$args->{"error_message"} // ""
l!l
245779
MC/DC0%0/1
$args->{"api_format"} // ""
$args->{"api_format"}
MC/DC50%1/2
($args->{"requested_model"} // $args->{"model"}) // ""
$args->{"requested_model"} $args->{"model"}
MC/DC0%0/1
$args->{"endpoint"} // ""
$args->{"endpoint"}
MC/DC0%0/1
$args->{"api_key_id"} // "anonymous"
$args->{"api_key_id"}
MC/DC0%0/1
$args->{"provider"} // "skeid"
$args->{"provider"}
MC/DC100%1/1
$args->{"engine"} // ""
$args->{"engine"}
MC/DC100%1/1
$args->{"model"} // ""
$args->{"model"}
MC/DC100%1/1
$args->{"node_id"} // ""
$args->{"node_id"}
MC/DC0%0/1
$args->{"route_url"} // ""
$args->{"route_url"}
MC/DC0%0/1
$args->{"status_code"} // 0
$args->{"status_code"}
MC/DC0%0/1
$args->{"duration_ms"} // 0
$args->{"duration_ms"}
MC/DC100%1/1
$args->{"error_type"} // ""
$args->{"error_type"}
MC/DC100%1/1
$args->{"error_message"} // ""
$args->{"error_message"}
Truth table50%1/2
A
A= $args->{"api_format"}
Aresult
11
00
Truth table66%2/3
A // B
A= $args->{"requested_model"}B= $args->{"model"}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $args->{"endpoint"}
Aresult
11
00
Truth table50%1/2
A
A= $args->{"api_key_id"}
Aresult
11
00
Truth table50%1/2
A
A= $args->{"provider"}
Aresult
11
00
Truth table100%2/2
A
A= $args->{"engine"}
Aresult
11
00
Truth table100%2/2
A
A= $args->{"model"}
Aresult
11
00
Truth table100%2/2
A
A= $args->{"node_id"}
Aresult
11
00
Truth table50%1/2
A
A= $args->{"route_url"}
Aresult
11
00
Truth table50%1/2
A
A= $args->{"status_code"}
Aresult
11
00
Truth table50%1/2
A
A= $args->{"duration_ms"}
Aresult
11
00
Truth table100%2/2
A
A= $args->{"error_type"}
Aresult
11
00
Truth table100%2/2
A
A= $args->{"error_message"}
Aresult
11
00
3034 metrics => $safe_metrics,
3035 });
3036 };
3037 1024 my $err;
3038 1024 ▶ if ($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 1024 1024
elsif (ref $recorded eq 'HASH' and not $recorded->{'ok'} and $recorded->{'enabled'} // 1) { } 3 2018 2021
3039 0 $err = "$@";
3040 0 $recorded = { ok => 0, error => $err };
3041 ▶ } elsif (ref($recorded) eq 'HASH' && !$recorded->{ok} && ($recorded->{enabled} // 1)) {
Condition87%7/8
ref $recorded eq "HASH" and not $recorded->{"ok"} and $recorded->{"enabled"} // 1
!ll&&!rl&&r
997243
ref $recorded eq "HASH" and not $recorded->{"ok"}
!ll&&!rl&&r
099727
$recorded->{"enabled"} // 1
l!l
243
MC/DC66%2/3
ref $recorded eq "HASH" and not $recorded->{"ok"} and $recorded->{"enabled"} // 1
ref $recorded eq "HASH" not $recorded->{"ok"} $recorded->{"enabled"}
Truth table75%3/4
A and not(B) and C
A= ref $recorded eq "HASH"B= not $recorded->{"ok"}C= $recorded->{"enabled"}
ABCresult
0XX0
10X0
1100
1111
3042 # A store reports a failed write in its answer (the JsonLog contract); no sink at all
3043 # answers enabled => 0 and has nothing to lose.
3044 3 ▶ $err = $recorded->{error} // 'unknown error';
Condition50%1/2
$recorded->{"error"} // "unknown error"
l!l
30
MC/DC0%0/1
$recorded->{"error"} // "unknown error"
$recorded->{"error"}
Truth table50%1/2
A
A= $recorded->{"error"}
Aresult
11
00
3045 }
3046 1024 ▶ if (defined $err) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 3 1021 1024
3047 _log_lost_usage_event($c->app, $c->skeid, {
3048 request_id => $request_id,
3049 api_key_id => ($args->{api_key_id} // 'anonymous'),
3050 model => $args->{model},
3051 status_code => $args->{status_code},
3052 3 ▶ }, $err);
Condition50%1/2
$args->{"api_key_id"} // "anonymous"
l!l
30
MC/DC0%0/1
$args->{"api_key_id"} // "anonymous"
$args->{"api_key_id"}
Truth table50%1/2
A
A= $args->{"api_key_id"}
Aresult
11
00
3053 }
3054  
3055 1024 return $recorded;
3056 }
3057  
3058 # The event is the billing unit (ADR 0004) and it is gone: say so at a level production keeps,
3059 # with what an operator needs to reconcile it by hand. The request id and the key id, never the
3060 # key -- the key id is a digest (ADR 0016), the key is a secret (ADR 0003). One line for both
3061 # ways an event is lost: a write that failed while the request waited, and a queued one a
3062 # write-behind flush could not write later (skeid k78).
3063 4 ▶sub _log_lost_usage_event {
Subroutine _log_lost_usage_event: called CC=6 SCAR=19.9
3064 4 my ($app, $skeid, $event, $err) = @_;
3065 4 ▶ $err //= 'unknown error';
Condition50%1/2
$err //= "unknown error"
l!l
40
MC/DC0%0/1
$err //= "unknown error"
$err
Truth table50%1/2
A
A= $err
Aresult
11
00
3066 4 $err =~ s/\s+$//;
3067 $app->log->error('usage event lost: request_id=' . ($event->{request_id} // '')
3068 . ' store=' . _usage_sink_name($skeid)
3069 . ' api_key_id=' . ($event->{api_key_id} // 'anonymous')
3070 . ' model=' . ($event->{model} // '')
3071 4 ▶ . ' status=' . ($event->{status_code} // 0)
Condition50%4/8
$event->{"request_id"} // ""
l!l
40
$event->{"api_key_id"} // "anonymous"
l!l
40
$event->{"model"} // ""
l!l
40
$event->{"status_code"} // 0
l!l
40
MC/DC0%0/1
$event->{"request_id"} // ""
$event->{"request_id"}
MC/DC0%0/1
$event->{"api_key_id"} // "anonymous"
$event->{"api_key_id"}
MC/DC0%0/1
$event->{"model"} // ""
$event->{"model"}
MC/DC0%0/1
$event->{"status_code"} // 0
$event->{"status_code"}
Truth table50%1/2
A
A= $event->{"request_id"}
Aresult
11
00
Truth table50%1/2
A
A= $event->{"api_key_id"}
Aresult
11
00
Truth table50%1/2
A
A= $event->{"model"}
Aresult
11
00
Truth table50%1/2
A
A= $event->{"status_code"}
Aresult
11
00
3072 . ': ' . $err);
3073 4 return;
3074 }
3075  
3076 # Which sink a lost usage event was meant for, for the log line: the store's backend name, or
3077 # how the embedding application took the event over. Never the DSN or path, which may carry
3078 # credentials.
3079 4 ▶sub _usage_sink_name {
Subroutine _usage_sink_name: called CC=5 SCAR=17.5
3080 4 my ($skeid) = @_;
3081 4 ▶ return 'store_usage_event' if $skeid->has_store_usage_event;
Branch: 1 branch
BranchTrueFalseTotal
if $skeid->has_store_usage_event 1 3 4
3082 3 my $cfg = $skeid->usage_store;
3083 3 ▶ return (ref($cfg) eq 'HASH' && length($cfg->{backend} // '')) ? $cfg->{backend} : 'custom';
Branch: 1 branch
BranchTrueFalseTotal
ref $cfg eq 'HASH' && length($cfg->{'backend'} // '') ? : 3 0 3
Condition40%2/5
ref $cfg eq "HASH" and length($cfg->{"backend"} // "")
!ll&&!rl&&r
003
$cfg->{"backend"} // ""
l!l
30
MC/DC0%0/2
ref $cfg eq "HASH" and length($cfg->{"backend"} // "")
ref $cfg eq "HASH" $cfg->{"backend"}
Truth table33%1/3
A and B
A= ref $cfg eq "HASH"B= $cfg->{"backend"}
ABresult
0X0
100
111
3084 }
3085  
3086 # The prompt-cache read count off a raw OpenAI-shaped upstream usage hash (k27). OpenAI nests it
3087 # under prompt_tokens_details.cached_tokens; some OpenAI-compatible servers expose a flat
3088 # cached_tokens, an Anthropic-spelled block cache_read_input_tokens, and a caller of usage.record may pass it as `cached`. Missing -> 0, the
3089 # same fault-tolerance the other token reads here have. This reads a count off a response Skeid
3090 # already holds -- every upstream answers in the OpenAI dialect (ADR 0001) -- it does not
3091 # translate a client format. Both paths prefer the count metrics.normalize reads through
3092 # Langertha::Usage and prices (skeid #28, #41); this is the fallback on Langertha 0.503.
3093 1591 ▶sub _cached_tokens {
Subroutine _cached_tokens: called CC=7 SCAR=19.5
3094 1591 my ($usage) = @_;
3095 1591 ▶ return 0 unless ref($usage) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $usage eq "HASH" 29 1562 1591
3096 1562 my $details = $usage->{prompt_tokens_details};
3097 return 0 + ($usage->{cached}
3098 // $usage->{cached_tokens}
3099 // (ref($details) eq 'HASH' ? $details->{cached_tokens} : undef)
3100 // $usage->{cache_read_input_tokens}
3101 1562 ▶ // 0);
Branch: 1 branch
BranchTrueFalseTotal
ref $details eq 'HASH' ? : 1 1559 1560
Condition90%10/11
((($usage->{"cached"} // $usage->{"cached_tokens"}) // (ref $details eq "HASH" ? $details->{"cached_tokens"} : undef)) // $usage->{"cache_read_input_tokens"}) // 0
l!l
31559
(($usage->{"cached"} // $usage->{"cached_tokens"}) // (ref $details eq "HASH" ? $details->{"cached_tokens"} : undef)) // $usage->{"cache_read_input_tokens"}
l!l&&r!l&&!r
301559
($usage->{"cached"} // $usage->{"cached_tokens"}) // (ref $details eq "HASH" ? $details->{"cached_tokens"} : undef)
l!l&&r!l&&!r
211559
$usage->{"cached"} // $usage->{"cached_tokens"}
l!l&&r!l&&!r
111560
MC/DC75%3/4
((($usage->{"cached"} // $usage->{"cached_tokens"}) // (ref $details eq "HASH" ? $details->{"cached_tokens"} : undef)) // $usage->{"cache_read_input_tokens"}) // 0
$usage->{"cached"} $usage->{"cached_tokens"} (ref $details eq "HASH" ? $details->{"cached_tokens"} : undef) $usage->{"cache_read_input_tokens"}
Truth table80%4/5
A // B // C // D
A= $usage->{"cached"}B= $usage->{"cached_tokens"}C= (ref $details eq "HASH" ? $details->{"cached_tokens"} : undef)D= $usage->{"cache_read_input_tokens"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
3102 }
3103  
3104 # Prices an upstream answer: its usage block goes through metrics.normalize (Langertha::Usage +
3105 # Langertha::Pricing), the one pricing path for every face, streamed or not (skeid #28, #41).
3106 # $payload is the decoded upstream body, or for a stream a body holding only the usage block
3107 # the stream carried -- the same usage, so the same cost.
3108 663 ▶sub _priced_metrics {
Subroutine _priced_metrics: called CC=12 SCAR=27.5
3109 663 my ($c, $meta, $body, $duration_ms, $payload, $tool_calls) = @_;
3110 663 my $metrics = eval {
3111 $c->skeid->call_function('metrics.normalize', {
3112 provider => ($meta->{provider} || 'skeid'),
3113 engine => ($meta->{engine} || 'openaibase'),
3114 model => ($meta->{model} || ($body->{model} // '')),
3115 663 ▶ route => ($meta->{endpoint} || ''),
Condition46%6/13
$meta->{'provider'} || 'skeid'
l!l
6630
$meta->{'engine'} || 'openaibase'
l!l
6630
$meta->{'model'} || ($body->{'model'} // '')
l!l&&r!l&&!r
66300
$body->{"model"} // ""
l!l
00
$meta->{'endpoint'} || ''
l!l
6630
$tool_calls || []
l!l
449214
MC/DC0%0/1
$meta->{'provider'} || 'skeid'
$meta->{'provider'}
MC/DC0%0/1
$meta->{'engine'} || 'openaibase'
$meta->{'engine'}
MC/DC0%0/2
$meta->{'model'} || ($body->{'model'} // '')
$meta->{'model'} $body->{"model"}
MC/DC0%0/1
$meta->{'endpoint'} || ''
$meta->{'endpoint'}
MC/DC100%1/1
$tool_calls || []
$tool_calls
Truth table50%1/2
A
A= $meta->{'provider'}
Aresult
11
00
Truth table50%1/2
A
A= $meta->{'engine'}
Aresult
11
00
Truth table33%1/3
A || B
A= $meta->{'model'}B= $body->{"model"}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $meta->{'endpoint'}
Aresult
11
00
Truth table100%2/2
A
A= $tool_calls
Aresult
11
00
3116 duration_ms => $duration_ms,
3117 response => $payload,
3118 tool_calls => ($tool_calls || []),
3119 });
3120 };
3121 663 ▶ $metrics = {} unless ref($metrics) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $metrics eq "HASH" 0 663 663
3122 # metrics.normalize reports the prompt-cache counts off Langertha::Usage, from every wire
3123 # spelling it knows (skeid #28). Langertha 0.503's Usage has no such counts, so there they are
3124 # pulled straight off the raw upstream usage and carried flat, the way input/output survive
3125 # as metrics->{*_tokens} (k27).
3126 663 ▶ if (!defined $metrics->{cached_tokens}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $metrics->{"cached_tokens"}) 561 102 663
3127 561 my $cached = _cached_tokens($payload->{usage});
3128 561 ▶ $metrics->{cached_tokens} = $cached if $cached;
Branch: 1 branch
BranchTrueFalseTotal
if $cached 0 561 561
3129 }
3130 663 ▶ if (!defined $metrics->{cache_write_tokens}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $metrics->{"cache_write_tokens"}) 567 96 663
3131 567 my $written = _cache_write_tokens($payload->{usage});
3132 567 ▶ $metrics->{cache_write_tokens} = $written if $written;
Branch: 1 branch
BranchTrueFalseTotal
if $written 0 567 567
3133 }
3134 663 return $metrics;
3135 }
3136  
3137 # A stream's metrics: its verbatim usage priced exactly as a non-streamed body carrying it would
3138 # be. A stream that carried no usage has nothing to price -- no token count or cost is invented.
3139 241 ▶sub _stream_metrics {
Subroutine _stream_metrics: called CC=2 SCAR=6.9
3140 241 my ($c, $meta, $body, $duration_ms, $usage) = @_;
3141 241 ▶ return {} unless ref($usage) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $usage eq "HASH" 75 166 241
3142 166 return _priced_metrics($c, $meta, $body, $duration_ms, { usage => $usage });
3143 }
3144  
3145 # Folds one stream frame's usage block into what the stream reported so far, key by key, a later
3146 # frame's value replacing an earlier one (nested blocks such as prompt_tokens_details likewise).
3147 # Usage counts on a stream are running totals, never increments: OpenAI's final frame carries
3148 # the whole request, a server that reports on every chunk repeats the growing total, and a wire
3149 # that splits the block (input on the first frame, output on the last) is completed rather
3150 # than lost. Summing frames would bill the same tokens twice.
3151 246 ▶sub _merge_usage {
Subroutine _merge_usage: called CC=6 SCAR=17.9
3152 246 my ($into, $frame) = @_;
3153 246 ▶ my %merged = ref($into) eq 'HASH' ? %$into : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $into eq 'HASH' ? : 86 160 246
3154 246 for my $key (keys %$frame) {
3155 758 my $value = $frame->{$key};
3156 758 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 758 758
3157 $merged{$key} = (ref($value) eq 'HASH' && ref($merged{$key}) eq 'HASH')
3158 758 ▶ ? _merge_usage($merged{$key}, $value)
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'HASH' && ref $merged{$key} eq 'HASH' ? : 36 722 758
Condition100%3/3
ref $value eq "HASH" and ref $merged{$key} eq "HASH"
!ll&&!rl&&r
6794336
MC/DC100%2/2
ref $value eq "HASH" and ref $merged{$key} eq "HASH"
ref $value eq "HASH" ref $merged{$key} eq "HASH"
Truth table100%3/3
A and B
A= ref $value eq "HASH"B= ref $merged{$key} eq "HASH"
ABresult
0X0
100
111
3159 : $value;
3160 }
3161 246 return \%merged;
3162 }
3163  
3164 # The prompt-cache write count off a raw upstream usage hash, for a Langertha that cannot read it
3165 # itself (0.503): OpenAI Chat nests it under prompt_tokens_details.cache_write_tokens, an
3166 # Anthropic-shaped block carries cache_creation_input_tokens. Missing -> 0. A count, never priced
3167 # here -- pricing is Langertha::Pricing's (skeid #41).
3168 567 ▶sub _cache_write_tokens {
Subroutine _cache_write_tokens: called CC=6 SCAR=19.6
3169 567 my ($usage) = @_;
3170 567 ▶ return 0 unless ref($usage) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $usage eq "HASH" 28 539 567
3171 539 my $details = $usage->{prompt_tokens_details};
3172 return 0 + ((ref($details) eq 'HASH' ? $details->{cache_write_tokens} : undef)
3173 // $usage->{cache_write_tokens}
3174 // $usage->{cache_creation_input_tokens}
3175 539 ▶ // 0);
Branch: 1 branch
BranchTrueFalseTotal
ref $details eq 'HASH' ? : 6 533 539
Condition37%3/8
(((ref $details eq "HASH" ? $details->{"cache_write_tokens"} : undef) // $usage->{"cache_write_tokens"}) // $usage->{"cache_creation_input_tokens"}) // 0
l!l
0539
((ref $details eq "HASH" ? $details->{"cache_write_tokens"} : undef) // $usage->{"cache_write_tokens"}) // $usage->{"cache_creation_input_tokens"}
l!l&&r!l&&!r
00539
(ref $details eq "HASH" ? $details->{"cache_write_tokens"} : undef) // $usage->{"cache_write_tokens"}
l!l&&r!l&&!r
00539
MC/DC0%0/3
(((ref $details eq "HASH" ? $details->{"cache_write_tokens"} : undef) // $usage->{"cache_write_tokens"}) // $usage->{"cache_creation_input_tokens"}) // 0
(ref $details eq "HASH" ? $details->{"cache_write_tokens"} : undef) $usage->{"cache_write_tokens"} $usage->{"cache_creation_input_tokens"}
Truth table25%1/4
A // B // C
A= (ref $details eq "HASH" ? $details->{"cache_write_tokens"} : undef)B= $usage->{"cache_write_tokens"}C= $usage->{"cache_creation_input_tokens"}
ABCresult
1XX1
01X1
0011
0000
3176 }
3177  
3178 # The model a tier asks its nodes for. Falls back to what the client requested, which is what
3179 # makes an aliasless deployment behave exactly as it did before tiers existed.
3180 945 ▶sub _served_model {
Subroutine _served_model: called CC=4 SCAR=15.6
3181 945 my ($tier, $requested) = @_;
3182 945 ▶ return $requested unless ref($tier) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tier eq "HASH" 0 945 945
3183 945 my $model = $tier->{model};
3184 945 ▶ return (defined($model) && length($model)) ? $model : $requested;
Branch: 1 branch
BranchTrueFalseTotal
defined $model && length $model ? : 945 0 945
Condition33%1/3
defined $model and length $model
!ll&&!rl&&r
00945
MC/DC0%0/2
defined $model and length $model
defined $model length $model
Truth table33%1/3
A and B
A= defined $modelB= length $model
ABresult
0X0
100
111
3185 }
3186  
3187 970 ▶sub _endpoint_url_for_node {
Subroutine _endpoint_url_for_node: called CC=6 SCAR=19.1
3188 970 my ($base, $path) = @_;
3189 970 ▶ $base //= '';
Condition50%1/2
$base //= ""
l!l
9700
MC/DC0%0/1
$base //= ""
$base
Truth table50%1/2
A
A= $base
Aresult
11
00
3190 970 ▶ $path //= '';
Condition50%1/2
$path //= ""
l!l
9700
MC/DC0%0/1
$path //= ""
$path
Truth table50%1/2
A
A= $path
Aresult
11
00
3191 970 $base =~ s{/\z}{};
3192  
3193 970 ▶ return $base . $path if $base =~ m{/v1\z} && $path =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
if $base =~ m[/v1\z] and $path =~ m[^/] 916 54 970
Condition66%2/3
$base =~ m[/v1\z] and $path =~ m[^/]
!ll&&!rl&&r
540916
MC/DC50%1/2
$base =~ m[/v1\z] and $path =~ m[^/]
$base =~ m[/v1\z] $path =~ m[^/]
Truth table66%2/3
A and B
A= $base =~ m[/v1\z]B= $path =~ m[^/]
ABresult
0X0
100
111
3194 54 ▶ return $base . '/v1' . $path if $path =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[^/] 54 0 54
3195 0 return $base . '/v1/' . $path;
3196 }
3197  
3198 # The URL of a route a node serves at its server root rather than below /v1 (a TEI node's
3199 # /rerank): a node URL is a base, and one written with a trailing /v1 names the same server.
3200 24 ▶sub _root_url_for_node {
Subroutine _root_url_for_node: called CC=2 SCAR=7.0
3201 24 my ($base, $path) = @_;
3202 24 ▶ $base //= '';
Condition50%1/2
$base //= ""
l!l
240
MC/DC0%0/1
$base //= ""
$base
Truth table50%1/2
A
A= $base
Aresult
11
00
3203 24 $base =~ s{/\z}{};
3204 24 $base =~ s{/v1\z}{};
3205 24 return $base . $path;
3206 }
3207  
3208 1043 ▶sub _duration_ms {
Subroutine _duration_ms: called CC=1 SCAR=0.0
3209 1043 my ($started) = @_;
3210 1043 return int((time - $started) * 1000);
3211 }
3212  
3213  
3214 86 1;
3215  
3216 __END__
« blib/lib/Langertha/Skeid/UsageStore/DBI.pm ↑ Summary lib/Langertha/Skeid/Protocol/Ollama/Stream.pm »