| 1 | package Langertha::Raider::Hall::ACP; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 72 | our $VERSION = '0.503'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Internal ACP (Agent Client Protocol) adapter that exposes the hall over TCP | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 72 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 72 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 72 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 72 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 72 | ▶ | use IO::Async::Listener; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 72 | ▶ | use IO::Async::Stream; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 72 | ▶ | use IO::Socket::IP; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 72 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 72 | ▶ | use Langertha::Raider::Hall::ACP::SubStream; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 72 | has hall => ( | |||||||||||||||||||||||||||||||||||||||||
| 17 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 18 | isa => 'Langertha::Raider::Hall', | ||||||||||||||||||||||||||||||||||||||||||
| 19 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 20 | weak_ref => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 72 | has port => ( | |||||||||||||||||||||||||||||||||||||||||
| 24 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 25 | isa => 'Int', | ||||||||||||||||||||||||||||||||||||||||||
| 26 | default => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 72 | has host => ( | |||||||||||||||||||||||||||||||||||||||||
| 30 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 31 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 32 | default => '127.0.0.1', | ||||||||||||||||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | has _sessions => ( | ||||||||||||||||||||||||||||||||||||||||||
| 36 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 14 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 72 | ); | |||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 72 | has _listener => ( | |||||||||||||||||||||||||||||||||||||||||
| 41 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 72 | our $PROTOCOL_VERSION = 1; | |||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | ▶ | sub start { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
start: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | my $loop = $self->hall->loop; | |||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | ▶ | my $sock = IO::Socket::IP->new( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | LocalHost => $self->host, | ||||||||||||||||||||||||||||||||||||||||||
| 52 | LocalPort => $self->port, | ||||||||||||||||||||||||||||||||||||||||||
| 53 | Listen => 10, | ||||||||||||||||||||||||||||||||||||||||||
| 54 | ReuseAddr => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 55 | ) or die "ACP: cannot listen on " . $self->host . ":" . $self->port . ": $!"; | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | my $listener = IO::Async::Listener->new( | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 2 | ▶ | on_accept => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 2 | my ($l, $client) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 2 | $self->_handle_client($client); | |||||||||||||||||||||||||||||||||||||||||
| 61 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||
| 63 | 1 | $loop->add($listener); | |||||||||||||||||||||||||||||||||||||||||
| 64 | 1 | $listener->listen(handle => $sock); | |||||||||||||||||||||||||||||||||||||||||
| 65 | 1 | $self->_listener($listener); | |||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 1 | my ($port) = $sock->sockport; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | $self->hall->_emit('acp.started', { | |||||||||||||||||||||||||||||||||||||||||
| 69 | host => $self->host, port => $port, | ||||||||||||||||||||||||||||||||||||||||||
| 70 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 1 | return $port; | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 1 | ▶ | sub stop { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stop: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 1 | ▶ | my $l = $self->_listener or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 1 | eval { $self->hall->loop->remove($l) }; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 1 | $self->_listener(undef); | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 2 | ▶ | sub _handle_client { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_client: called CC=1 SCAR=0.1
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 2 | my ($self, $sock) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 83 | my $stream = IO::Async::Stream->new( | ||||||||||||||||||||||||||||||||||||||||||
| 84 | handle => $sock, | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 5 | ▶ | on_read => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 5 | my ($stream, $bufref, $eof) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 5 | ▶ | while ($$bufref =~ s/^(.*?)\r?\n//) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 3 | $self->_dispatch($stream, $1); | |||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 5 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 91 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 2 | ▶ | on_closed => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=18.6
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 2 | my ($stream) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 94 | # Clean up any sessions bound to this stream. | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 2 | for my $sid (keys %{$self->_sessions}) { | |||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | my $s = $self->_sessions->{$sid}; | |||||||||||||||||||||||||||||||||||||||||
| 97 | $self->_forget_session($sid) | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 1 | ▶ | if $s && $s->{stream} && $s->{stream} == $stream; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$s and $s->{"stream"} and $s->{"stream"} == $stream
$s and $s->{"stream"}
MC/DC0%0/3
$s and $s->{"stream"} and $s->{"stream"} == $stream
$s
$s->{"stream"}
$s->{"stream"} == $stream
Truth table25%1/4
A and B and C
A= $sB= $s->{"stream"}C= $s->{"stream"} == $stream
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 2 | ); | |||||||||||||||||||||||||||||||||||||||||
| 102 | 2 | $self->hall->loop->add($stream); | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 10 | ▶ | sub _dispatch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dispatch: called CC=10 SCAR=24.2
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 10 | my ($self, $stream, $line) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 10 | ▶ | return unless length $line; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 10 | my $msg = eval { JSON::MaybeXS->new->decode($line) }; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 10 | ▶ | if ($@ || ref $msg ne 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$@ or ref $msg ne "HASH"
MC/DC0%0/2
$@ or ref $msg ne "HASH"
$@
ref $msg ne "HASH"
Truth table33%1/3
A or B
A= $@B= ref $msg ne "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | return $self->_reply_err($stream, undef, -32700, 'parse error'); | |||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 10 | my $method = $msg->{method}; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 10 | my $id = $msg->{id}; | |||||||||||||||||||||||||||||||||||||||||
| 116 | 10 | ▶ | my $params = $msg->{params} // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$msg->{"params"} // {}
MC/DC100%1/1
$msg->{"params"} // {}
$msg->{"params"}
Truth table100%2/2
A
A= $msg->{"params"}
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 10 | ▶ | if (!defined $method) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | # It's a response to something we sent — ignore, we don't call out | ||||||||||||||||||||||||||||||||||||||||||
| 120 | # mid-stream in this minimal adapter. | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 10 | ▶ | if ($method eq 'initialize') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 2 | return $self->_reply($stream, $id, { | |||||||||||||||||||||||||||||||||||||||||
| 126 | protocolVersion => $PROTOCOL_VERSION, | ||||||||||||||||||||||||||||||||||||||||||
| 127 | agentCapabilities => { | ||||||||||||||||||||||||||||||||||||||||||
| 128 | loadSession => JSON::MaybeXS::false(), | ||||||||||||||||||||||||||||||||||||||||||
| 129 | promptCapabilities => { | ||||||||||||||||||||||||||||||||||||||||||
| 130 | image => JSON::MaybeXS::false(), | ||||||||||||||||||||||||||||||||||||||||||
| 131 | audio => JSON::MaybeXS::false(), | ||||||||||||||||||||||||||||||||||||||||||
| 132 | embeddedContext => JSON::MaybeXS::false(), | ||||||||||||||||||||||||||||||||||||||||||
| 133 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 134 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 135 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 8 | ▶ | if ($method eq 'session/new') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 4 | return $self->_session_new($stream, $id, $params); | |||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 4 | ▶ | if ($method eq 'session/prompt') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 2 | return $self->_session_prompt($stream, $id, $params); | |||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | 2 | ▶ | if ($method eq 'session/cancel') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | return $self->_session_cancel($stream, $id, $params); | |||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | 2 | return $self->_reply_err($stream, $id, -32601, "method not found: $method"); | |||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 4 | ▶ | sub _session_new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_session_new: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 4 | my ($self, $stream, $id, $params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | 4 | my $raider_name = $params->{raiderName}; | |||||||||||||||||||||||||||||||||||||||||
| 157 | 4 | ▶ | if (!$raider_name) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 4 | ▶ | my $raiders = $self->hall->config->{raiders} // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->hall->config->{"raiders"} // {}
MC/DC0%0/1
$self->hall->config->{"raiders"} // {}
$self->hall->config->{"raiders"}
Truth table50%1/2
A
A= $self->hall->config->{"raiders"}
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 4 | ($raider_name) = sort keys %$raiders; | |||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 4 | ▶ | if (!$raider_name) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | return $self->_reply_err($stream, $id, -32602, | |||||||||||||||||||||||||||||||||||||||||
| 163 | 'no raiders configured and none specified'); | ||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 4 | my $session_id = 'acp-' . sprintf('%08x', int(rand(2**32))); | |||||||||||||||||||||||||||||||||||||||||
| 167 | 4 | $self->_sessions->{$session_id} = { | |||||||||||||||||||||||||||||||||||||||||
| 168 | stream => $stream, | ||||||||||||||||||||||||||||||||||||||||||
| 169 | raider_name => $raider_name, | ||||||||||||||||||||||||||||||||||||||||||
| 170 | current_raider_id => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 171 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | 4 | $self->_reply($stream, $id, { sessionId => $session_id }); | |||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | # The hall session binding of an ACP session: its prompts continue one | ||||||||||||||||||||||||||||||||||||||||||
| 177 | # raider session; it ends with the ACP session, the journal stays. | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 25 | ▶ | sub _binding_of { 'acp:'.$_[1] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_binding_of: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | 2 | ▶ | sub _forget_session { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_forget_session: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 2 | my ($self, $session_id) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 182 | 2 | my $session = delete $self->_sessions->{$session_id}; | |||||||||||||||||||||||||||||||||||||||||
| 183 | 2 | ▶ | if ($session) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 2 | $self->_end_run_subscription($session); | |||||||||||||||||||||||||||||||||||||||||
| 185 | 2 | ▶ | if (my $watch = delete $session->{_spawn_watch}) { $watch->close } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||
| 187 | 2 | ▶ | my $hall = $self->hall or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | # Prompts still waiting for the session have no client left to answer. | ||||||||||||||||||||||||||||||||||||||||||
| 189 | 2 | $hall->drop_queued($self->_binding_of($session_id)); | |||||||||||||||||||||||||||||||||||||||||
| 190 | 2 | $hall->unbind_session($self->_binding_of($session_id)); | |||||||||||||||||||||||||||||||||||||||||
| 191 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | 15 | ▶ | sub _session_prompt { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_session_prompt: called CC=10 SCAR=24.4
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | 15 | my ($self, $stream, $id, $params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | my $session_id = $params->{sessionId} | ||||||||||||||||||||||||||||||||||||||||||
| 198 | 15 | ▶ | or return $self->_reply_err($stream, $id, -32602, 'missing sessionId'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | 15 | ▶ | my $session = $self->_sessions->{$session_id} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 200 | or return $self->_reply_err($stream, $id, -32602, "unknown session: $session_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | # Flatten the prompt content into text. | ||||||||||||||||||||||||||||||||||||||||||
| 203 | 15 | ▶ | my @blocks = @{ $params->{prompt} // [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$params->{"prompt"} // []
MC/DC0%0/1
$params->{"prompt"} // []
$params->{"prompt"}
Truth table50%1/2
A
A= $params->{"prompt"}
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | my $text = join("\n", map { | ||||||||||||||||||||||||||||||||||||||||||
| 205 | 15 | my $b = $_; | |||||||||||||||||||||||||||||||||||||||||
| 206 | 15 | ▶ | ref $b eq 'HASH' && defined $b->{text} ? $b->{text} : '' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $b eq "HASH" and defined $b->{"text"}
MC/DC0%0/2
ref $b eq "HASH" and defined $b->{"text"}
ref $b eq "HASH"
defined $b->{"text"}
Truth table33%1/3
A and B
A= ref $b eq "HASH"B= defined $b->{"text"}
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | } @blocks); | ||||||||||||||||||||||||||||||||||||||||||
| 208 | 15 | $text =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||
| 209 | 15 | ▶ | return $self->_reply_err($stream, $id, -32602, 'empty prompt') unless length $text; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | my $spawn = $self->hall->spawn( | ||||||||||||||||||||||||||||||||||||||||||
| 212 | name => $session->{raider_name}, | ||||||||||||||||||||||||||||||||||||||||||
| 213 | 15 | mission => $text, | |||||||||||||||||||||||||||||||||||||||||
| 214 | binding => $self->_binding_of($session_id), | ||||||||||||||||||||||||||||||||||||||||||
| 215 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 216 | 15 | ▶ | if ($spawn->{error}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | return $self->_reply_err($stream, $id, -32000, "spawn failed: $spawn->{error}"); | |||||||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | # Queued (1name or the session busy): ACP has no stop reason for a turn | ||||||||||||||||||||||||||||||||||||||||||
| 221 | # that has not started, so the request stays open until the queued run | ||||||||||||||||||||||||||||||||||||||||||
| 222 | # starts and ends, and is answered like any other. | ||||||||||||||||||||||||||||||||||||||||||
| 223 | 15 | ▶ | if ($spawn->{queued}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 4 | ▶ | push @{ $session->{waiting} //= [] }, $id; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$session->{"waiting"} //= []
MC/DC0%0/1
$session->{"waiting"} //= []
$session->{"waiting"}
Truth table50%1/2
A
A= $session->{"waiting"}
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | 4 | $self->_watch_spawns($session, $session_id, $stream); | |||||||||||||||||||||||||||||||||||||||||
| 226 | 4 | return; | |||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | 11 | my $raider_id = $spawn->{id}; | |||||||||||||||||||||||||||||||||||||||||
| 230 | 11 | $session->{current_raider_id} = $raider_id; | |||||||||||||||||||||||||||||||||||||||||
| 231 | 11 | $session->{pending_request_id} = $id; | |||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | # Subscribe this session's stream to hall events for this raider. | ||||||||||||||||||||||||||||||||||||||||||
| 234 | # We reuse the hall's subscriber list but filter in _on_hall_event. | ||||||||||||||||||||||||||||||||||||||||||
| 235 | 11 | $self->_attach_subscription($session, $raider_id, $stream); | |||||||||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | # Waiting prompts start in order on the session's binding: each | ||||||||||||||||||||||||||||||||||||||||||
| 239 | # raider.spawned there takes the oldest one and streams its run to it. So | ||||||||||||||||||||||||||||||||||||||||||
| 240 | # does a hall.session_error there, for the run it started unbound. | ||||||||||||||||||||||||||||||||||||||||||
| 241 | 4 | ▶ | sub _watch_spawns { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_watch_spawns: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 242 | 4 | my ($self, $session, $session_id, $stream) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 243 | 4 | ▶ | return if $session->{_spawn_watch}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | 4 | my $binding = $self->_binding_of($session_id); | |||||||||||||||||||||||||||||||||||||||||
| 245 | 11 | ▶ | $session->{_spawn_watch} = $self->_subscribe('', sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.2
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | 11 | my ($evt) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 247 | 11 | ▶ | my $t = $evt->{type} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$evt->{"type"} // ""
MC/DC0%0/1
$evt->{"type"} // ""
$evt->{"type"}
Truth table50%1/2
A
A= $evt->{"type"}
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | 11 | ▶ | return unless $t eq 'raider.spawned' || $t eq 'hall.session_error'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$t eq "raider.spawned" or $t eq "hall.session_error"
MC/DC100%2/2
$t eq "raider.spawned" or $t eq "hall.session_error"
$t eq "raider.spawned"
$t eq "hall.session_error"
Truth table100%3/3
A or B
A= $t eq "raider.spawned"B= $t eq "hall.session_error"
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | 4 | ▶ | return unless ($evt->{binding} // '') eq $binding && defined $evt->{id}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
($evt->{"binding"} // "") eq $binding and defined $evt->{"id"}
$evt->{"binding"} // ""
MC/DC50%1/2
($evt->{"binding"} // "") eq $binding and defined $evt->{"id"}
$evt->{"binding"}
defined $evt->{"id"}
Truth table66%2/3
A and B
A= $evt->{"binding"}B= defined $evt->{"id"}
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | 3 | ▶ | my $rid = shift @{ $session->{waiting} // [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$session->{"waiting"} // []
MC/DC0%0/1
$session->{"waiting"} // []
$session->{"waiting"}
Truth table50%1/2
A
A= $session->{"waiting"}
| |||||||||||||||||||||||||||||||||||||||||||
| 251 | 3 | ▶ | return unless defined $rid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | 3 | $session->{current_raider_id} = $evt->{id}; | |||||||||||||||||||||||||||||||||||||||||
| 253 | 3 | $session->{pending_request_id} = $rid; | |||||||||||||||||||||||||||||||||||||||||
| 254 | 3 | $self->_attach_subscription($session, $evt->{id}, $stream); | |||||||||||||||||||||||||||||||||||||||||
| 255 | 4 | }); | |||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||
| 258 | # A callback on the hall event bus: the hall writes each event as a JSON | ||||||||||||||||||||||||||||||||||||||||||
| 259 | # line into the stream of a subscriber, so the callback sits behind a | ||||||||||||||||||||||||||||||||||||||||||
| 260 | # SubStream. Closing that stream ends the subscription. | ||||||||||||||||||||||||||||||||||||||||||
| 261 | 21 | ▶ | sub _subscribe { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_subscribe: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | 21 | my ($self, $filter, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 263 | 32 | ▶ | my $sub_stream = Langertha::Raider::Hall::ACP::SubStream->new(cb => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 32 | my ($json_line) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 265 | 32 | my $evt = eval { JSON::MaybeXS->new->decode($json_line) }; | |||||||||||||||||||||||||||||||||||||||||
| 266 | 32 | ▶ | return if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | 32 | $cb->($evt); | |||||||||||||||||||||||||||||||||||||||||
| 268 | 21 | }); | |||||||||||||||||||||||||||||||||||||||||
| 269 | 21 | ▶ | push @{ $self->hall->{_subscribers} ||= [] }, { | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->hall->{"_subscribers"} ||= []
MC/DC100%1/1
$self->hall->{"_subscribers"} ||= []
$self->hall->{"_subscribers"}
Truth table100%2/2
A
A= $self->hall->{"_subscribers"}
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | stream => $sub_stream, filter => $filter, | ||||||||||||||||||||||||||||||||||||||||||
| 271 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 272 | 21 | return $sub_stream; | |||||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | 17 | ▶ | sub _attach_subscription { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_attach_subscription: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | 17 | my ($self, $session, $raider_id, $stream) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | 21 | ▶ | my $handler = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=20 SCAR=33.7
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | 21 | my ($evt) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 280 | 21 | ▶ | my $t = $evt->{type} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$evt->{"type"} // ""
MC/DC0%0/1
$evt->{"type"} // ""
$evt->{"type"}
Truth table50%1/2
A
A= $evt->{"type"}
| |||||||||||||||||||||||||||||||||||||||||||
| 281 | 21 | ▶ | return unless $t =~ /^raider\./; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | 21 | ▶ | return unless ($evt->{id} // '') eq $raider_id; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$evt->{"id"} // ""
MC/DC100%1/1
$evt->{"id"} // ""
$evt->{"id"}
Truth table100%2/2
A
A= $evt->{"id"}
| |||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | 17 | ▶ | if ($t eq 'raider.done') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 285 | # The hall reports every finished run as raider.done, carrying the | ||||||||||||||||||||||||||||||||||||||||||
| 286 | # outcome from its run.finished event: status plus a response, or an | ||||||||||||||||||||||||||||||||||||||||||
| 287 | # error when the run failed or ended without one. | ||||||||||||||||||||||||||||||||||||||||||
| 288 | 17 | ▶ | my $status = $evt->{status} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$evt->{"status"} // ""
MC/DC0%0/1
$evt->{"status"} // ""
$evt->{"status"}
Truth table50%1/2
A
A= $evt->{"status"}
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | my $cancelled = $evt->{signaled} | ||||||||||||||||||||||||||||||||||||||||||
| 290 | 17 | ▶ | || $status =~ /^(?:cancelled|interrupted)$/; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$evt->{'signaled'} || $status =~ /^(?:cancelled|interrupted)$/
MC/DC100%2/2
$evt->{'signaled'} || $status =~ /^(?:cancelled|interrupted)$/
$evt->{'signaled'}
$status =~ /^(?:cancelled|interrupted)$/
Truth table100%3/3
A || B
A= $evt->{'signaled'}B= $status =~ /^(?:cancelled|interrupted)$/
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | # A failed run is a run error, not a model answer: end the turn as a | ||||||||||||||||||||||||||||||||||||||||||
| 293 | # JSON-RPC error and do NOT stream its error text as an | ||||||||||||||||||||||||||||||||||||||||||
| 294 | # agent_message_chunk — an editor would render that as a normal | ||||||||||||||||||||||||||||||||||||||||||
| 295 | # reply and, with stopReason end_turn, never learn the run broke. | ||||||||||||||||||||||||||||||||||||||||||
| 296 | 17 | ▶ | if (!$cancelled && $status ne 'completed') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $cancelled and $status ne "completed"
MC/DC100%2/2
not $cancelled and $status ne "completed"
not $cancelled
$status ne "completed"
Truth table100%3/3
not(A) and B
A= not $cancelledB= $status ne "completed"
| |||||||||||||||||||||||||||||||||||||||||||
| 297 | return $self->_fail_run($session, $stream, | ||||||||||||||||||||||||||||||||||||||||||
| 298 | 1 | ▶ | $evt->{error} // 'run ended without a result'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$evt->{"error"} // "run ended without a result"
MC/DC0%0/1
$evt->{"error"} // "run ended without a result"
$evt->{"error"}
Truth table50%1/2
A
A= $evt->{"error"}
| |||||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | # Completed (or cancelled): forward the outcome text as a final | ||||||||||||||||||||||||||||||||||||||||||
| 302 | # agent_message_chunk before closing the turn — otherwise the client | ||||||||||||||||||||||||||||||||||||||||||
| 303 | # only ever sees status events. | ||||||||||||||||||||||||||||||||||||||||||
| 304 | 16 | ▶ | my $body = $evt->{response} // ($cancelled ? $evt->{error} : undef); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$evt->{"response"} // ($cancelled ? $evt->{"error"} : undef)
MC/DC100%2/2
$evt->{"response"} // ($cancelled ? $evt->{"error"} : undef)
$evt->{"response"}
($cancelled ? $evt->{"error"} : undef)
Truth table100%3/3
A // B
A= $evt->{"response"}B= ($cancelled ? $evt->{"error"} : undef)
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | 16 | ▶ | if (defined $body && length $body) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $body and length $body
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
| |||||||||||||||||||||||||||||||||||||||||||
| 306 | 13 | $self->_notify($stream, 'session/update', { | |||||||||||||||||||||||||||||||||||||||||
| 307 | sessionId => $self->_session_id_for($session), | ||||||||||||||||||||||||||||||||||||||||||
| 308 | update => { | ||||||||||||||||||||||||||||||||||||||||||
| 309 | sessionUpdate => 'agent_message_chunk', | ||||||||||||||||||||||||||||||||||||||||||
| 310 | content => { type => 'text', text => $body }, | ||||||||||||||||||||||||||||||||||||||||||
| 311 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 312 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||||||||||
| 314 | 16 | $self->_end_run_subscription($session); | |||||||||||||||||||||||||||||||||||||||||
| 315 | 16 | my $rid = delete $session->{pending_request_id}; | |||||||||||||||||||||||||||||||||||||||||
| 316 | 16 | ▶ | $self->_reply($stream, $rid, { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 317 | stopReason => $cancelled ? 'cancelled' : 'end_turn', | ||||||||||||||||||||||||||||||||||||||||||
| 318 | }) if defined $rid; | ||||||||||||||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||||||||||||||
| 320 | elsif ($t eq 'raider.failed') { | ||||||||||||||||||||||||||||||||||||||||||
| 321 | # A run failure the hall might report out of band: treat it exactly | ||||||||||||||||||||||||||||||||||||||||||
| 322 | # like a failed raider.done — a JSON-RPC error, never 'refusal'. | ||||||||||||||||||||||||||||||||||||||||||
| 323 | # 'refusal' in ACP means the *model* declined, not that the run broke. | ||||||||||||||||||||||||||||||||||||||||||
| 324 | $self->_fail_run($session, $stream, | ||||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | ▶ | $evt->{error} // 'raider run failed'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$evt->{"error"} // "raider run failed"
MC/DC0%0/1
$evt->{"error"} // "raider run failed"
$evt->{"error"}
Truth table0%0/2
A
A= $evt->{"error"}
| |||||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||
| 327 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 328 | # Forward other raider.* events as plaintext chunks so clients get | ||||||||||||||||||||||||||||||||||||||||||
| 329 | # *some* streaming. A richer mapping (tool_call → tool_use update | ||||||||||||||||||||||||||||||||||||||||||
| 330 | # etc.) can grow from here. | ||||||||||||||||||||||||||||||||||||||||||
| 331 | my $chunk = JSON::MaybeXS->new(canonical => 1)->encode({ | ||||||||||||||||||||||||||||||||||||||||||
| 332 | 0 | ▶ | type => $t, ($evt->{data} ? (data => $evt->{data}) : ()), | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 333 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 334 | 0 | $self->_notify($stream, 'session/update', { | |||||||||||||||||||||||||||||||||||||||||
| 335 | sessionId => $self->_session_id_for($session), | ||||||||||||||||||||||||||||||||||||||||||
| 336 | update => { | ||||||||||||||||||||||||||||||||||||||||||
| 337 | sessionUpdate => 'agent_message_chunk', | ||||||||||||||||||||||||||||||||||||||||||
| 338 | content => { type => 'text', text => $chunk }, | ||||||||||||||||||||||||||||||||||||||||||
| 339 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 340 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||||||||||||
| 342 | 17 | }; | |||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | 17 | $session->{_sub_stream} = $self->_subscribe('raider.', $handler); | |||||||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||
| 347 | # A failed run ends session/prompt as a JSON-RPC error, so an editor can | ||||||||||||||||||||||||||||||||||||||||||
| 348 | # tell a broken run apart from a normal answer. The failure text rides in | ||||||||||||||||||||||||||||||||||||||||||
| 349 | # the JSON-RPC error message, never as an agent_message_chunk. | ||||||||||||||||||||||||||||||||||||||||||
| 350 | 1 | ▶ | sub _fail_run { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fail_run: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 351 | 1 | my ($self, $session, $stream, $error) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 352 | 1 | $self->_end_run_subscription($session); | |||||||||||||||||||||||||||||||||||||||||
| 353 | 1 | my $rid = delete $session->{pending_request_id}; | |||||||||||||||||||||||||||||||||||||||||
| 354 | 1 | ▶ | $self->_reply_err($stream, $rid, -32000, "raider run failed: $error") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 355 | if defined $rid; | ||||||||||||||||||||||||||||||||||||||||||
| 356 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||
| 359 | # The run of the session's current prompt ended: stop listening to it. | ||||||||||||||||||||||||||||||||||||||||||
| 360 | 19 | ▶ | sub _end_run_subscription { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_end_run_subscription: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 361 | 19 | my ($self, $session) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 362 | 19 | ▶ | my $sub_stream = delete $session->{_sub_stream} or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 363 | 17 | $sub_stream->close; | |||||||||||||||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||
| 366 | 13 | ▶ | sub _session_id_for { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_session_id_for: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||
| 367 | 13 | my ($self, $session) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 368 | 13 | for my $sid (keys %{$self->_sessions}) { | |||||||||||||||||||||||||||||||||||||||||
| 369 | 14 | ▶ | return $sid if $self->_sessions->{$sid} == $session; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||||||||||||||||
| 371 | 0 | return ''; | |||||||||||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | 2 | ▶ | sub _session_cancel { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_session_cancel: called CC=6 SCAR=18.2
| |||||||||||||||||||||||||||||||||||||||||||
| 375 | 2 | my ($self, $stream, $id, $params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 376 | my $session_id = $params->{sessionId} | ||||||||||||||||||||||||||||||||||||||||||
| 377 | 2 | ▶ | or return $self->_reply_err($stream, $id, -32602, 'missing sessionId'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 378 | 2 | ▶ | my $session = $self->_sessions->{$session_id} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 379 | or return $self->_reply_err($stream, $id, -32602, "unknown session: $session_id"); | ||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||
| 381 | # Prompts still waiting end cancelled, without running. | ||||||||||||||||||||||||||||||||||||||||||
| 382 | 2 | $self->hall->drop_queued($self->_binding_of($session_id)); | |||||||||||||||||||||||||||||||||||||||||
| 383 | 2 | ▶ | for my $rid (splice @{ $session->{waiting} // [] }) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$session->{"waiting"} // []
MC/DC100%1/1
$session->{"waiting"} // []
$session->{"waiting"}
Truth table100%2/2
A
A= $session->{"waiting"}
| |||||||||||||||||||||||||||||||||||||||||||
| 384 | 1 | $self->_reply($stream, $rid, { stopReason => 'cancelled' }); | |||||||||||||||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||||||||||||||||
| 386 | 2 | ▶ | if (my $rid = $session->{current_raider_id}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 387 | 2 | $self->hall->cancel_raider($rid); | |||||||||||||||||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||||||||||||||||
| 389 | 2 | $self->_reply($stream, $id, {}); | |||||||||||||||||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||
| 392 | 25 | ▶ | sub _reply { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_reply: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 393 | 25 | my ($self, $stream, $id, $result) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 394 | 25 | ▶ | return unless defined $id; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 395 | 25 | my $msg = { jsonrpc => '2.0', id => $id, result => $result }; | |||||||||||||||||||||||||||||||||||||||||
| 396 | 25 | $stream->write(JSON::MaybeXS->new->encode($msg) . "\n"); | |||||||||||||||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||
| 399 | 3 | ▶ | sub _reply_err { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_reply_err: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 400 | 3 | my ($self, $stream, $id, $code, $message) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 401 | 3 | ▶ | return unless defined $id; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 402 | 3 | my $msg = { jsonrpc => '2.0', id => $id, error => { code => $code, message => $message } }; | |||||||||||||||||||||||||||||||||||||||||
| 403 | 3 | $stream->write(JSON::MaybeXS->new->encode($msg) . "\n"); | |||||||||||||||||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||
| 406 | 13 | ▶ | sub _notify { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_notify: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 407 | 13 | my ($self, $stream, $method, $params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 408 | 13 | my $msg = { jsonrpc => '2.0', method => $method, params => $params }; | |||||||||||||||||||||||||||||||||||||||||
| 409 | 13 | $stream->write(JSON::MaybeXS->new->encode($msg) . "\n"); | |||||||||||||||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | 72 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||
| 414 | 72 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||
| 416 | __END__ | ||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||
| 418 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||
| 420 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||
| 422 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||
| 424 | Langertha::Raider::Hall::ACP - Internal ACP (Agent Client Protocol) adapter that exposes the hall over TCP | ||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||
| 426 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||
| 428 | version 0.503 | ||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||
| 430 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||
| 432 | B<Internal module.> Its interface may change without notice. | ||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | Minimal ACP server. Speaks JSON-RPC 2.0 line-framed over TCP. Clients | ||||||||||||||||||||||||||||||||||||||||||
| 435 | that speak ACP over TCP (C<raider acp>, L<Langertha::Raider::ACP::Client>) | ||||||||||||||||||||||||||||||||||||||||||
| 436 | connect to the configured port and drive raiders through the hall. An | ||||||||||||||||||||||||||||||||||||||||||
| 437 | editor that starts its agent as a command over stdio needs the stdio | ||||||||||||||||||||||||||||||||||||||||||
| 438 | server of ADR 0010, which does not exist yet. | ||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||
| 440 | =head2 Supported methods | ||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||
| 442 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||
| 444 | =item * C<initialize> — handshake. Returns C<protocolVersion> and | ||||||||||||||||||||||||||||||||||||||||||
| 445 | C<agentCapabilities> (promptCapabilities: image=false, audio=false, | ||||||||||||||||||||||||||||||||||||||||||
| 446 | embeddedContext=false). | ||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||
| 448 | =item * C<session/new> — create a session bound to a raider slot. | ||||||||||||||||||||||||||||||||||||||||||
| 449 | Params: C<{ cwd, mcpServers?, raiderName? }>. If C<raiderName> is given | ||||||||||||||||||||||||||||||||||||||||||
| 450 | it selects the configured raider; otherwise the first entry in | ||||||||||||||||||||||||||||||||||||||||||
| 451 | C<.raider-hall.yml> is used. | ||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | =item * C<session/prompt> — spawn the raider with the user turn's text | ||||||||||||||||||||||||||||||||||||||||||
| 454 | content, stream C<session/update> notifications from the hall event bus, | ||||||||||||||||||||||||||||||||||||||||||
| 455 | return C<{ stopReason }> when the raider finishes. A run that | ||||||||||||||||||||||||||||||||||||||||||
| 456 | B<completes> ends C<end_turn>, a B<cancelled> run ends C<cancelled>, and a | ||||||||||||||||||||||||||||||||||||||||||
| 457 | B<failed> run (an engine error, a crash, a run that ended without a | ||||||||||||||||||||||||||||||||||||||||||
| 458 | result) ends C<session/prompt> as a JSON-RPC error (code C<-32000>), | ||||||||||||||||||||||||||||||||||||||||||
| 459 | not as a normal answer: its error text rides in the JSON-RPC error | ||||||||||||||||||||||||||||||||||||||||||
| 460 | message and is never streamed as an C<agent_message_chunk>, so an editor | ||||||||||||||||||||||||||||||||||||||||||
| 461 | tells a broken run apart from a reply. All prompts of an ACP | ||||||||||||||||||||||||||||||||||||||||||
| 462 | session run in one raider session, bound as C<acp:SESSION> until the | ||||||||||||||||||||||||||||||||||||||||||
| 463 | client disconnects (see L<Langertha::Raider::Hall/session_bindings>). | ||||||||||||||||||||||||||||||||||||||||||
| 464 | A prompt that has to wait -- its C<1name> slot or its session is busy -- | ||||||||||||||||||||||||||||||||||||||||||
| 465 | gets no answer until its run has started and ended; then it is answered | ||||||||||||||||||||||||||||||||||||||||||
| 466 | like any other -- C<end_turn>, C<cancelled> or a JSON-RPC error -- also | ||||||||||||||||||||||||||||||||||||||||||
| 467 | when the hall had to start that run without the session | ||||||||||||||||||||||||||||||||||||||||||
| 468 | (C<hall.session_error>). | ||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||
| 470 | =item * C<session/cancel> — cancel the running raider's run with a | ||||||||||||||||||||||||||||||||||||||||||
| 471 | C<SIGINT> (L<Langertha::Raider::Hall/cancel_raider>): it ends the run as | ||||||||||||||||||||||||||||||||||||||||||
| 472 | C<cancelled>, and its prompt is answered C<cancelled>. A raider the | ||||||||||||||||||||||||||||||||||||||||||
| 473 | signal does not end gets C<SIGTERM>, then C<SIGKILL>, each after the | ||||||||||||||||||||||||||||||||||||||||||
| 474 | hall's C<cancel_grace>. Prompts still | ||||||||||||||||||||||||||||||||||||||||||
| 475 | waiting are answered C<cancelled> and never run. | ||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||
| 477 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||
| 479 | Features intentionally omitted in this first cut: authentication, | ||||||||||||||||||||||||||||||||||||||||||
| 480 | filesystem push-edits, embedded context resources, audio/image content | ||||||||||||||||||||||||||||||||||||||||||
| 481 | blocks. Calls unknown to this adapter return JSON-RPC error | ||||||||||||||||||||||||||||||||||||||||||
| 482 | C<-32601 method not found>. | ||||||||||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||||||||||
| 484 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||||||||
| 486 | =head2 Issues | ||||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||
| 488 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||||||||||||
| 489 | L<https://github.com/Getty/langertha-raider/issues>. | ||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||
| 491 | =head2 IRC | ||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||
| 493 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||||||
| 495 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||
| 497 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||
| 499 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||
| 501 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||||||||
| 503 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||
| 505 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||||||||||
| 507 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 508 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||
| 510 | =cut | ||||||||||||||||||||||||||||||||||||||||||