| 1 | package Langertha::Knarr::RequestLog; | ||||||||||||||||||||||||||||||||||||||
| 2 | 6 | our $VERSION = '1.103'; | |||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Local disk logging of proxy requests | ||||||||||||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use Time::HiRes qw( gettimeofday tv_interval ); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use JSON::MaybeXS (); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use Log::Any qw( $log ); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Langertha::Knarr::Image; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use Langertha::Knarr::Tracing; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||
| 13 | # The JSONL log is the running operational record, not the detailed trace, so | ||||||||||||||||||||||||||||||||||||||
| 14 | # tool-call arguments are capped: enough to see what was called, without | ||||||||||||||||||||||||||||||||||||||
| 15 | # spilling large or user-data-bearing payloads onto disk on every request. | ||||||||||||||||||||||||||||||||||||||
| 16 | # The full arguments live in the Langfuse trace (see Langertha::Knarr::Tracing). | ||||||||||||||||||||||||||||||||||||||
| 17 | 6 | my $ARG_PREVIEW_MAX = 200; | |||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||
| 20 | 6 | has config => ( | |||||||||||||||||||||||||||||||||||||
| 21 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||
| 22 | required => 1, | ||||||||||||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||
| 26 | 6 | has log_file => ( | |||||||||||||||||||||||||||||||||||||
| 27 | is => 'lazy', | ||||||||||||||||||||||||||||||||||||||
| 28 | builder => '_build_log_file', | ||||||||||||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||
| 31 | 8 | ▶ | sub _build_log_file { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_log_file: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 32 | 8 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||
| 33 | 8 | return $self->config->log_file; | |||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||
| 37 | 6 | has log_dir => ( | |||||||||||||||||||||||||||||||||||||
| 38 | is => 'lazy', | ||||||||||||||||||||||||||||||||||||||
| 39 | builder => '_build_log_dir', | ||||||||||||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||
| 42 | 8 | ▶ | sub _build_log_dir { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_log_dir: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 43 | 8 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||
| 44 | 8 | return $self->config->log_dir; | |||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||
| 48 | 6 | has _enabled => ( | |||||||||||||||||||||||||||||||||||||
| 49 | is => 'lazy', | ||||||||||||||||||||||||||||||||||||||
| 50 | builder => '_build__enabled', | ||||||||||||||||||||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||
| 53 | 8 | ▶ | sub _build__enabled { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_build__enabled: called CC=3 SCAR=11.7
| |||||||||||||||||||||||||||||||||||||||
| 54 | 8 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||
| 55 | 8 | ▶ | return ($self->log_file || $self->log_dir) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->log_file or $self->log_dir
MC/DC0%0/2
$self->log_file or $self->log_dir
$self->log_file
$self->log_dir
Truth table66%2/3
A or B
A= $self->log_fileB= $self->log_dir
| |||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||
| 58 | 6 | has _json => ( | |||||||||||||||||||||||||||||||||||||
| 59 | is => 'lazy', | ||||||||||||||||||||||||||||||||||||||
| 60 | builder => '_build__json', | ||||||||||||||||||||||||||||||||||||||
| 61 | ); | ||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||
| 63 | 7 | ▶ | sub _build__json { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_build__json: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 64 | 7 | return JSON::MaybeXS->new(utf8 => 1, convert_blessed => 1); | |||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||
| 67 | 6 | has _json_pretty => ( | |||||||||||||||||||||||||||||||||||||
| 68 | is => 'lazy', | ||||||||||||||||||||||||||||||||||||||
| 69 | builder => '_build__json_pretty', | ||||||||||||||||||||||||||||||||||||||
| 70 | ); | ||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||
| 72 | 2 | ▶ | sub _build__json_pretty { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_build__json_pretty: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 73 | 2 | return JSON::MaybeXS->new(utf8 => 1, convert_blessed => 1, pretty => 1, canonical => 1); | |||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||
| 76 | # Sequence number of the next per-request file of this process. | ||||||||||||||||||||||||||||||||||||||
| 77 | 6 | has _file_seq => ( | |||||||||||||||||||||||||||||||||||||
| 78 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||
| 79 | default => 0, | ||||||||||||||||||||||||||||||||||||||
| 80 | ); | ||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||
| 82 | 4 | ▶ | sub _next_file_seq { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_next_file_seq: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 83 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||
| 84 | 4 | return $self->_file_seq( $self->_file_seq + 1 ); | |||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||
| 87 | 6 | has _json_compact => ( | |||||||||||||||||||||||||||||||||||||
| 88 | is => 'lazy', | ||||||||||||||||||||||||||||||||||||||
| 89 | builder => '_build__json_compact', | ||||||||||||||||||||||||||||||||||||||
| 90 | ); | ||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||
| 92 | # Character-string (no utf8) canonical encoder used only to render a stable | ||||||||||||||||||||||||||||||||||||||
| 93 | # preview of tool-call arguments; the preview then rides inside the entry that | ||||||||||||||||||||||||||||||||||||||
| 94 | # _json / _json_pretty encode to bytes. | ||||||||||||||||||||||||||||||||||||||
| 95 | 1 | ▶ | sub _build__json_compact { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_build__json_compact: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 96 | 1 | return JSON::MaybeXS->new(canonical => 1, convert_blessed => 1); | |||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||
| 99 | 15 | ▶ | sub _timestamp { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_timestamp: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 100 | 15 | my ($s, $us) = gettimeofday; | |||||||||||||||||||||||||||||||||||||
| 101 | 15 | my @t = gmtime($s); | |||||||||||||||||||||||||||||||||||||
| 102 | 15 | return sprintf("%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", | |||||||||||||||||||||||||||||||||||||
| 103 | $t[5]+1900, $t[4]+1, $t[3], $t[2], $t[1], $t[0], int($us/1000)); | ||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||
| 106 | # Flatten a Langertha::Usage into its canonical token counts. Langertha's | ||||||||||||||||||||||||||||||||||||||
| 107 | # value objects carry no TO_JSON, so the object itself would die in the | ||||||||||||||||||||||||||||||||||||||
| 108 | # encode below — and since both writers encode inside an eval that only | ||||||||||||||||||||||||||||||||||||||
| 109 | # warns, the whole log entry would silently disappear. A plain hashref (the | ||||||||||||||||||||||||||||||||||||||
| 110 | # shape end_request's own SYNOPSIS documents) passes through untouched; an | ||||||||||||||||||||||||||||||||||||||
| 111 | # object we cannot flatten is dropped rather than costing the entry. | ||||||||||||||||||||||||||||||||||||||
| 112 | # The canonical counts keep their meaning; prompt-cache tokens ride beside | ||||||||||||||||||||||||||||||||||||||
| 113 | # them as cached_tokens / cache_write_tokens when not zero, split the way | ||||||||||||||||||||||||||||||||||||||
| 114 | # the Langfuse trace splits them (k65), so nothing is counted twice (k71). | ||||||||||||||||||||||||||||||||||||||
| 115 | # A usage that counts no token at all is no usage, not a 0/0/0. | ||||||||||||||||||||||||||||||||||||||
| 116 | 15 | ▶ | sub _usage_hash { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_usage_hash: called CC=8 SCAR=21.0
| |||||||||||||||||||||||||||||||||||||||
| 117 | 15 | my ($u) = @_; | |||||||||||||||||||||||||||||||||||||
| 118 | 15 | ▶ | return undef unless defined $u; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 119 | 7 | ▶ | if ( blessed($u) && $u->can('to_hash') ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($u) and $u->can("to_hash")
MC/DC50%1/2
builtin::blessed($u) and $u->can("to_hash")
builtin::blessed($u)
$u->can("to_hash")
Truth table66%2/3
A and B
A= builtin::blessed($u)B= $u->can("to_hash")
| |||||||||||||||||||||||||||||||||||||||
| 120 | 5 | ▶ | my $counts = Langertha::Knarr::Tracing::_usage_hash($u) or return undef; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 121 | return { | ||||||||||||||||||||||||||||||||||||||
| 122 | 4 | ▶ | %{ $u->to_hash }, | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 123 | $counts->{input_cached_tokens} ? ( cached_tokens => $counts->{input_cached_tokens} ) : (), | ||||||||||||||||||||||||||||||||||||||
| 124 | 4 | $counts->{input_cache_creation} ? ( cache_write_tokens => $counts->{input_cache_creation} ) : () | |||||||||||||||||||||||||||||||||||||
| 125 | }; | ||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||
| 127 | 2 | ▶ | return $u if ref($u) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 128 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||
| 131 | # Trim the response's tool calls for the JSONL entry: tool name + call id plus | ||||||||||||||||||||||||||||||||||||||
| 132 | # an arguments preview capped at $ARG_PREVIEW_MAX characters. The full | ||||||||||||||||||||||||||||||||||||||
| 133 | # arguments are recorded in the Langfuse trace instead. | ||||||||||||||||||||||||||||||||||||||
| 134 | 15 | ▶ | sub _tool_calls_trimmed { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_tool_calls_trimmed: called CC=14 SCAR=30.3
| |||||||||||||||||||||||||||||||||||||||
| 135 | 15 | my ($self, $tcs) = @_; | |||||||||||||||||||||||||||||||||||||
| 136 | 15 | ▶ | return undef unless ref $tcs eq 'ARRAY' && @$tcs; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $tcs eq "ARRAY" and @$tcs
MC/DC50%1/2
ref $tcs eq "ARRAY" and @$tcs
ref $tcs eq "ARRAY"
@$tcs
Truth table66%2/3
A and B
A= ref $tcs eq "ARRAY"B= @$tcs
| |||||||||||||||||||||||||||||||||||||||
| 137 | 1 | my @out; | |||||||||||||||||||||||||||||||||||||
| 138 | 1 | for my $tc (@$tcs) { | |||||||||||||||||||||||||||||||||||||
| 139 | 2 | my ($id, $name, $args); | |||||||||||||||||||||||||||||||||||||
| 140 | 2 | ▶ | if ( blessed($tc) ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 141 | 2 | ▶ | $id = $tc->can('id') ? $tc->id : undef; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 142 | 2 | ▶ | $name = $tc->can('name') ? $tc->name : undef; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 143 | 2 | ▶ | $args = $tc->can('arguments') ? $tc->arguments : undef; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||
| 145 | elsif ( ref $tc eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||
| 146 | 0 | ($id, $name, $args) = @{$tc}{qw( id name arguments )}; | |||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||
| 148 | 0 | else { next } | |||||||||||||||||||||||||||||||||||||
| 149 | 2 | ▶ | my $preview = ref $args ? $self->_json_compact->encode($args) | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 150 | : defined $args ? "$args" | ||||||||||||||||||||||||||||||||||||||
| 151 | : ''; | ||||||||||||||||||||||||||||||||||||||
| 152 | 2 | ▶ | my $truncated = length($preview) > $ARG_PREVIEW_MAX ? \1 : \0; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 153 | 2 | ▶ | $preview = substr( $preview, 0, $ARG_PREVIEW_MAX ) if ${$truncated}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 154 | 2 | push @out, { | |||||||||||||||||||||||||||||||||||||
| 155 | id => $id, | ||||||||||||||||||||||||||||||||||||||
| 156 | name => $name, | ||||||||||||||||||||||||||||||||||||||
| 157 | arguments => $preview, | ||||||||||||||||||||||||||||||||||||||
| 158 | truncated => $truncated, | ||||||||||||||||||||||||||||||||||||||
| 159 | }; | ||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||
| 161 | 1 | ▶ | return @out ? \@out : undef; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||
| 164 | 1 | ▶ | sub _file_timestamp { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_file_timestamp: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 165 | 1 | my ($s, $us) = gettimeofday; | |||||||||||||||||||||||||||||||||||||
| 166 | 1 | my @t = gmtime($s); | |||||||||||||||||||||||||||||||||||||
| 167 | 1 | return sprintf("%04d%02d%02d_%02d%02d%02d_%03d", | |||||||||||||||||||||||||||||||||||||
| 168 | $t[5]+1900, $t[4]+1, $t[3], $t[2], $t[1], $t[0], int($us/1000)); | ||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||
| 172 | 15 | ▶ | sub start_request { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
start_request: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 173 | 15 | my ($self, %opts) = @_; | |||||||||||||||||||||||||||||||||||||
| 174 | 15 | ▶ | return undef unless $self->_enabled; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||
| 176 | return { | ||||||||||||||||||||||||||||||||||||||
| 177 | timestamp => _timestamp(), | ||||||||||||||||||||||||||||||||||||||
| 178 | t0 => [gettimeofday], | ||||||||||||||||||||||||||||||||||||||
| 179 | model => $opts{model}, | ||||||||||||||||||||||||||||||||||||||
| 180 | format => $opts{format}, | ||||||||||||||||||||||||||||||||||||||
| 181 | engine => $opts{engine}, | ||||||||||||||||||||||||||||||||||||||
| 182 | path => $opts{path}, | ||||||||||||||||||||||||||||||||||||||
| 183 | stream => $opts{stream} ? \1 : \0, | ||||||||||||||||||||||||||||||||||||||
| 184 | # Image objects (k33) carry no TO_JSON; the log writes plain parts. | ||||||||||||||||||||||||||||||||||||||
| 185 | messages => Langertha::Knarr::Image::plain_messages( $opts{messages} ), | ||||||||||||||||||||||||||||||||||||||
| 186 | params => $opts{params}, | ||||||||||||||||||||||||||||||||||||||
| 187 | 15 | ▶ | }; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||
| 191 | 15 | ▶ | sub end_request { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
end_request: called CC=6 SCAR=18.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 192 | 15 | my ($self, $handle, %opts) = @_; | |||||||||||||||||||||||||||||||||||||
| 193 | 15 | ▶ | return unless $self->_enabled; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 194 | 15 | ▶ | return unless $handle; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||
| 196 | 15 | my $duration_ms = int(tv_interval($handle->{t0}) * 1000); | |||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||
| 198 | my $entry = { | ||||||||||||||||||||||||||||||||||||||
| 199 | timestamp => $handle->{timestamp}, | ||||||||||||||||||||||||||||||||||||||
| 200 | model => $handle->{model}, | ||||||||||||||||||||||||||||||||||||||
| 201 | format => $handle->{format}, | ||||||||||||||||||||||||||||||||||||||
| 202 | engine => $handle->{engine}, | ||||||||||||||||||||||||||||||||||||||
| 203 | path => $handle->{path}, | ||||||||||||||||||||||||||||||||||||||
| 204 | stream => $handle->{stream}, | ||||||||||||||||||||||||||||||||||||||
| 205 | messages => $handle->{messages}, | ||||||||||||||||||||||||||||||||||||||
| 206 | params => $handle->{params}, | ||||||||||||||||||||||||||||||||||||||
| 207 | output => $opts{output}, | ||||||||||||||||||||||||||||||||||||||
| 208 | usage => _usage_hash( $opts{usage} ), | ||||||||||||||||||||||||||||||||||||||
| 209 | tool_calls => $self->_tool_calls_trimmed( $opts{tool_calls} ), | ||||||||||||||||||||||||||||||||||||||
| 210 | duration_ms => $duration_ms, | ||||||||||||||||||||||||||||||||||||||
| 211 | status => $opts{error} ? 'error' : 'ok', | ||||||||||||||||||||||||||||||||||||||
| 212 | error => $opts{error}, | ||||||||||||||||||||||||||||||||||||||
| 213 | 15 | ▶ | }; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||
| 215 | 15 | ▶ | $self->_write_jsonl($entry) if $self->log_file; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 216 | 15 | ▶ | $self->_write_file($entry, $handle) if $self->log_dir; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||
| 219 | 12 | ▶ | sub _write_jsonl { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_write_jsonl: called CC=6 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||
| 220 | 12 | my ($self, $entry) = @_; | |||||||||||||||||||||||||||||||||||||
| 221 | 12 | eval { | |||||||||||||||||||||||||||||||||||||
| 222 | 12 | ▶ | open my $fh, '>>', $self->log_file | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 223 | or die "Cannot open log file " . $self->log_file . ": $!"; | ||||||||||||||||||||||||||||||||||||||
| 224 | 12 | flock($fh, 2); # LOCK_EX | |||||||||||||||||||||||||||||||||||||
| 225 | # One write(2) on the O_APPEND handle: lines from several workers | ||||||||||||||||||||||||||||||||||||||
| 226 | # (k51) never interleave. | ||||||||||||||||||||||||||||||||||||||
| 227 | 12 | my $line = $self->_json->encode($entry) . "\n"; | |||||||||||||||||||||||||||||||||||||
| 228 | 12 | my $written = syswrite( $fh, $line ); | |||||||||||||||||||||||||||||||||||||
| 229 | 12 | ▶ | die "Short write to log file " . $self->log_file . ": " . ( $! || 'partial line' ) | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition20%1/5
defined $written and $written == length $line
$! || 'partial line'
MC/DC0%0/2
defined $written and $written == length $line
defined $written
$written == length $line
MC/DC0%0/1
$! || 'partial line'
$!
Truth table33%1/3
A and B
A= defined $writtenB= $written == length $line
Truth table0%0/2
A
A= $!
| |||||||||||||||||||||||||||||||||||||||
| 230 | unless defined $written && $written == length $line; | ||||||||||||||||||||||||||||||||||||||
| 231 | 12 | close $fh; | |||||||||||||||||||||||||||||||||||||
| 232 | }; | ||||||||||||||||||||||||||||||||||||||
| 233 | 12 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 234 | 0 | $log->warnf("RequestLog write error: %s", $@); | |||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||
| 238 | 4 | ▶ | sub _write_file { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_write_file: called CC=6 SCAR=18.3
| |||||||||||||||||||||||||||||||||||||||
| 239 | 4 | my ($self, $entry, $handle) = @_; | |||||||||||||||||||||||||||||||||||||
| 240 | 4 | eval { | |||||||||||||||||||||||||||||||||||||
| 241 | 4 | my $dir = $self->log_dir; | |||||||||||||||||||||||||||||||||||||
| 242 | 4 | ▶ | unless (-d $dir) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 243 | 1 | require File::Path; | |||||||||||||||||||||||||||||||||||||
| 244 | 1 | File::Path::make_path($dir); | |||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||
| 247 | 4 | ▶ | my $model = $handle->{model} // 'unknown'; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$handle->{"model"} // "unknown"
MC/DC0%0/1
$handle->{"model"} // "unknown"
$handle->{"model"}
Truth table50%1/2
A
A= $handle->{"model"}
| |||||||||||||||||||||||||||||||||||||||
| 248 | 4 | ▶ | my $format = $handle->{format} // 'unknown'; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$handle->{"format"} // "unknown"
MC/DC0%0/1
$handle->{"format"} // "unknown"
$handle->{"format"}
Truth table50%1/2
A
A= $handle->{"format"}
| |||||||||||||||||||||||||||||||||||||||
| 249 | # Sanitize for filename | ||||||||||||||||||||||||||||||||||||||
| 250 | 4 | $model =~ s/[^a-zA-Z0-9._-]/_/g; | |||||||||||||||||||||||||||||||||||||
| 251 | 4 | $format =~ s/[^a-zA-Z0-9._-]/_/g; | |||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||
| 253 | # pid and a per-process sequence number: two requests finishing in the | ||||||||||||||||||||||||||||||||||||||
| 254 | # same millisecond, in one worker or in two (k51), get two files. | ||||||||||||||||||||||||||||||||||||||
| 255 | 4 | my $filename = _file_timestamp() . '_' . $$ . '-' . $self->_next_file_seq | |||||||||||||||||||||||||||||||||||||
| 256 | . "_${format}_${model}.json"; | ||||||||||||||||||||||||||||||||||||||
| 257 | 4 | my $path = File::Spec->catfile($dir, $filename); | |||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||
| 259 | 4 | ▶ | open my $fh, '>', $path | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 260 | or die "Cannot write log file $path: $!"; | ||||||||||||||||||||||||||||||||||||||
| 261 | 4 | print $fh $self->_json_pretty->encode($entry); | |||||||||||||||||||||||||||||||||||||
| 262 | 4 | close $fh; | |||||||||||||||||||||||||||||||||||||
| 263 | }; | ||||||||||||||||||||||||||||||||||||||
| 264 | 4 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 265 | 0 | $log->warnf("RequestLog file write error: %s", $@); | |||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||
| 270 | 6 | 1; | |||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||
| 272 | __END__ | ||||||||||||||||||||||||||||||||||||||