« blib/lib/Langertha/Raider/CLI/Provider.pm
↑ Summary
blib/lib/Langertha/Raider/Session/Journal.pm »
| 1 | package Langertha::Raider::CLI::REPL; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Internal interactive loop of the raider CLI | ||||||||||||||||||||||||||||
| 3 | 56 | our $VERSION = '0.503'; | |||||||||||||||||||||||||||
| 4 | 56 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 56 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 56 | ▶ | use Config; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 56 | ▶ | use Encode qw( decode encode FB_QUIET ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 56 | ▶ | use POSIX (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 56 | ▶ | use Path::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 56 | ▶ | use Term::ANSIColor qw( color ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 56 | ▶ | use Term::ReadLine; # Core; upgrades to Gnu if installed | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 56 | ▶ | use IO::Prompt::Tiny qw( prompt ); # Fallback when Gnu is not available | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 56 | ▶ | use Langertha::Raider::CLI::Commands; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 56 | ▶ | use Langertha::Raider::CLI::Runner; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 56 | has app => ( | |||||||||||||||||||||||||||
| 18 | is => 'ro', | ||||||||||||||||||||||||||||
| 19 | isa => 'Langertha::Raider::CLI', | ||||||||||||||||||||||||||||
| 20 | required => 1, | ||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 56 | has output => ( | |||||||||||||||||||||||||||
| 24 | is => 'ro', | ||||||||||||||||||||||||||||
| 25 | isa => 'Langertha::Raider::CLI::Output', | ||||||||||||||||||||||||||||
| 26 | required => 1, | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | has in => ( | ||||||||||||||||||||||||||||
| 30 | is => 'ro', | ||||||||||||||||||||||||||||
| 31 | 0 | ▶ | default => sub { \*STDIN }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 32 | 56 | ); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | has active_profiles => ( | ||||||||||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||||||||
| 36 | isa => 'ArrayRef[Str]', | ||||||||||||||||||||||||||||
| 37 | 54 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 56 | ); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | has saved_profiles => ( | ||||||||||||||||||||||||||||
| 41 | is => 'ro', | ||||||||||||||||||||||||||||
| 42 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 43 | 54 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 56 | ); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 56 | has customize_prompt => ( | |||||||||||||||||||||||||||
| 47 | is => 'ro', | ||||||||||||||||||||||||||||
| 48 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 49 | default => 0, | ||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 56 | has session_store => ( | |||||||||||||||||||||||||||
| 54 | is => 'ro', | ||||||||||||||||||||||||||||
| 55 | isa => 'Langertha::Raider::SessionStore', | ||||||||||||||||||||||||||||
| 56 | predicate => 'has_session_store', | ||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 56 | has session => ( | |||||||||||||||||||||||||||
| 60 | is => 'ro', | ||||||||||||||||||||||||||||
| 61 | isa => 'Langertha::Raider::Session', | ||||||||||||||||||||||||||||
| 62 | writer => '_set_session', | ||||||||||||||||||||||||||||
| 63 | predicate => 'has_session', | ||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | has notes => ( | ||||||||||||||||||||||||||||
| 68 | is => 'ro', | ||||||||||||||||||||||||||||
| 69 | isa => 'ArrayRef[Str]', | ||||||||||||||||||||||||||||
| 70 | 55 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 71 | 56 | ); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 56 | has commands => ( | |||||||||||||||||||||||||||
| 74 | is => 'ro', | ||||||||||||||||||||||||||||
| 75 | lazy => 1, | ||||||||||||||||||||||||||||
| 76 | builder => '_build_commands', | ||||||||||||||||||||||||||||
| 77 | ); | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 5 | ▶ | sub _build_commands { | ||||||||||||||||||||||||||
|
Subroutine
_build_commands: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 80 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 81 | 5 | return Langertha::Raider::CLI::Commands->new(app => $self->app, output => $self->output, in => $self->in); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 56 | has runner => ( | |||||||||||||||||||||||||||
| 85 | is => 'ro', | ||||||||||||||||||||||||||||
| 86 | lazy => 1, | ||||||||||||||||||||||||||||
| 87 | builder => '_build_runner', | ||||||||||||||||||||||||||||
| 88 | ); | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 28 | ▶ | sub _build_runner { | ||||||||||||||||||||||||||
|
Subroutine
_build_runner: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 91 | 28 | my ($self) = @_; | |||||||||||||||||||||||||||
| 92 | 28 | return Langertha::Raider::CLI::Runner->new(app => $self->app, output => $self->output); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 56 | my $LOGO = <<'LOGO'; | |||||||||||||||||||||||||||
| 96 | __ __ | ||||||||||||||||||||||||||||
| 97 | .----.---.-.|__|.--| |.-----.----. | ||||||||||||||||||||||||||||
| 98 | | _| _ || || _ || -__| _| | ||||||||||||||||||||||||||||
| 99 | |__| |___._||__||_____||_____|__| | ||||||||||||||||||||||||||||
| 100 | LOGO | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 82 | ▶ | sub banner { | ||||||||||||||||||||||||||
|
Subroutine
banner: called CC=17 SCAR=28.8
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | 82 | my ( $self, $rl_impl ) = @_; | |||||||||||||||||||||||||||
| 105 | 82 | my $app = $self->app; | |||||||||||||||||||||||||||
| 106 | 82 | my $out = $self->output; | |||||||||||||||||||||||||||
| 107 | 82 | ▶ | my $line = sub { $out->emit($out->c(meta => $_[0]), $out->c(title => $_[1]), "\n") }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 82 | my $env = $app->api_key_env; | |||||||||||||||||||||||||||
| 110 | 82 | my $resolver = $app->engine_resolver; | |||||||||||||||||||||||||||
| 111 | my $env_display = $resolver->has_provider | ||||||||||||||||||||||||||||
| 112 | # --provider: the key comes from -k, never from the environment, and | ||||||||||||||||||||||||||||
| 113 | # only an endpoint with an auth_ref needs one. Never print the key. | ||||||||||||||||||||||||||||
| 114 | ? ( defined $resolver->provider->{auth} | ||||||||||||||||||||||||||||
| 115 | ? 'auth '.$resolver->provider->{auth}.' ('.( length $resolver->api_key ? 'set' : 'missing' ).')' | ||||||||||||||||||||||||||||
| 116 | : '(no API key required)' ) | ||||||||||||||||||||||||||||
| 117 | : defined $env | ||||||||||||||||||||||||||||
| 118 | 82 | ▶ | ? ($ENV{$env} ? $env.' (set)' : $env.' (missing)') | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 119 | : '(no API key required)'; | ||||||||||||||||||||||||||||
| 120 | 82 | ▶ | my $model = $app->has_model ? $app->model : '(engine default)'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 82 | my $source = $app->mission_source; | |||||||||||||||||||||||||||
| 122 | 82 | my $file = $app->instructions->label; | |||||||||||||||||||||||||||
| 123 | 82 | ▶ | my $persona = $source eq '-M' ? 'from -M ('.$file.' not used)' | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 124 | : $source eq $file ? 'custom ('.$file.' loaded)' | ||||||||||||||||||||||||||||
| 125 | : 'Langertha (default)'; | ||||||||||||||||||||||||||||
| 126 | 82 | ▶ | $persona .= ', bare (no '.$file.' or skills, only explicit packs)' if $app->bare; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 82 | $out->emit($out->c(brand => $LOGO)); | |||||||||||||||||||||||||||
| 129 | 82 | $out->emit($out->c(accent => ' perl agent - powered by Langertha'), "\n\n"); | |||||||||||||||||||||||||||
| 130 | 82 | $line->('persona: ', $persona); | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 82 | ▶ | if (my @profiles = @{ $self->active_profiles }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 1 | ▶ | $line->('profiles: ', join(', ', map { $_.($self->saved_profiles->{$_} ? ' (saved)' : '') } @profiles)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 82 | my @skills = $app->loaded_skill_names; | |||||||||||||||||||||||||||
| 137 | 82 | ▶ | $line->('skills: ', @skills | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | ? sprintf('%d loaded (%s)', scalar @skills, join(', ', @skills)) | ||||||||||||||||||||||||||||
| 139 | : 'none'); | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 82 | my @active_packs = @{$app->packs->active_pack_names}; | |||||||||||||||||||||||||||
| 142 | 82 | ▶ | $line->('packs: ', join(', ', @active_packs)) if @active_packs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 82 | for my $ign ($app->ignored_agent_files) { | |||||||||||||||||||||||||||
| 145 | $out->emit($out->c(meta => ' '), | ||||||||||||||||||||||||||||
| 146 | 0 | $out->c(warn => 'seeing '.$ign->{path}.', ignoring (use --'.$ign->{profile}.' to load)'), "\n"); | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 82 | $line->('engine: ', $app->engine_name); | |||||||||||||||||||||||||||
| 149 | 82 | $line->('model: ', $model); | |||||||||||||||||||||||||||
| 150 | 82 | $line->('api key: ', $env_display); | |||||||||||||||||||||||||||
| 151 | 82 | $line->('root: ', $app->root); | |||||||||||||||||||||||||||
| 152 | 82 | ▶ | $line->('session: ', $self->has_session ? $self->session->id.' ('.$self->session->path.')' | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 153 | : $self->has_session_store ? 'new, saved from the first prompt on' | ||||||||||||||||||||||||||||
| 154 | : 'off'); | ||||||||||||||||||||||||||||
| 155 | 82 | $line->('readline: ', $rl_impl); | |||||||||||||||||||||||||||
| 156 | 82 | $out->emit($out->c(meta => 'type '), $out->c(accent => '/help'), $out->c(meta => ' for commands, '), | |||||||||||||||||||||||||||
| 157 | $out->c(accent => '/quit'), $out->c(meta => ' to leave'), "\n\n"); | ||||||||||||||||||||||||||||
| 158 | 82 | return; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 74 | ▶ | sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=13 SCAR=25.8
Pod
documented
| |||||||||||||||||||||||||||||
| 163 | 74 | my ( $self, @first ) = @_; | |||||||||||||||||||||||||||
| 164 | 74 | my $out = $self->output; | |||||||||||||||||||||||||||
| 165 | 74 | my $in = $self->in; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | my $rl = -t $in ? $self->_terminal_reader : { | ||||||||||||||||||||||||||||
| 168 | 195 | ▶ | read => sub { my $l = <$in>; $l }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 169 | 74 | ▶ | impl => 'none (input is not a terminal)', | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | }; | ||||||||||||||||||||||||||||
| 171 | 74 | ▶ | my $call = sub { my $cb = $rl->{ $_[0] }; $cb->(@_[ 1 .. $#_ ]) if $cb }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | # Two-strike Ctrl-C: first press cancels the turn in progress (or only | ||||||||||||||||||||||||||||
| 174 | # warns at the prompt), second within 2s exits. Saves the user from | ||||||||||||||||||||||||||||
| 175 | # accidentally killing raider and removes the old "Ctrl-C + Return" | ||||||||||||||||||||||||||||
| 176 | # Docker quirk. Cancelling and leaving end the tool commands still | ||||||||||||||||||||||||||||
| 177 | # running: a bash command sits in a process group of its own and would | ||||||||||||||||||||||||||||
| 178 | # outlive raider. | ||||||||||||||||||||||||||||
| 179 | 3 | ▶ | my $leave = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 180 | 3 | my ( $signal ) = @_; | |||||||||||||||||||||||||||
| 181 | 3 | local $SIG{INT} = 'IGNORE'; | |||||||||||||||||||||||||||
| 182 | 3 | local $SIG{TERM} = 'IGNORE'; | |||||||||||||||||||||||||||
| 183 | 3 | $self->runner->terminate_children; | |||||||||||||||||||||||||||
| 184 | 3 | $self->runner->abandon($signal); | |||||||||||||||||||||||||||
| 185 | 3 | $call->('save'); | |||||||||||||||||||||||||||
| 186 | 3 | $out->emit($out->c(meta => "\nbye."), "\n"); | |||||||||||||||||||||||||||
| 187 | 3 | exit 0; | |||||||||||||||||||||||||||
| 188 | 74 | }; | |||||||||||||||||||||||||||
| 189 | 74 | my $last_sigint = 0; | |||||||||||||||||||||||||||
| 190 | 6 | ▶ | local $SIG{INT} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.6
| |||||||||||||||||||||||||||||
| 191 | 6 | my $now = time; | |||||||||||||||||||||||||||
| 192 | 6 | ▶ | $leave->('INT') if $last_sigint && $now - $last_sigint <= 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$last_sigint and $now - $last_sigint <= 2
MC/DC50%1/2
$last_sigint and $now - $last_sigint <= 2
$last_sigint
$now - $last_sigint <= 2
Truth table66%2/3
A and B
A= $last_sigintB= $now - $last_sigint <= 2
| |||||||||||||||||||||||||||||
| 193 | 4 | $last_sigint = $now; | |||||||||||||||||||||||||||
| 194 | # Only a flag and signals: a die here would be swallowed by an eval. | ||||||||||||||||||||||||||||
| 195 | 4 | ▶ | if ($self->runner->cancel_run) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 4 | $out->emit($out->c(meta => "\n(cancelling the turn; press Ctrl-C again within 2s to quit)"), "\n"); | |||||||||||||||||||||||||||
| 197 | 4 | return; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | 0 | $out->emit($out->c(meta => "\n(press Ctrl-C again within 2s to quit)"), "\n"); | |||||||||||||||||||||||||||
| 200 | 0 | $call->('redisplay'); | |||||||||||||||||||||||||||
| 201 | 74 | }; | |||||||||||||||||||||||||||
| 202 | 74 | local $SIG{TERM} = $leave; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 74 | $self->banner($rl->{impl}); | |||||||||||||||||||||||||||
| 205 | 74 | $out->say_meta($_) for @{ $self->notes }; | |||||||||||||||||||||||||||
| 206 | 74 | ▶ | $self->commands->cmd_prompt if $self->customize_prompt; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 74 | ▶ | $self->run_prompt(join ' ', @first) if @first; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 74 | ▶ | while (defined(my $line = $rl->{read}->())) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 137 | $line =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||
| 211 | 137 | ▶ | next unless length $line; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 136 | ▶ | if ($line =~ m{^(?:/quit|/exit|:q|quit|exit)$}i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | 3 | $out->emit($out->c(meta => 'bye.'), "\n"); | |||||||||||||||||||||||||||
| 215 | 3 | last; | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | 133 | $call->(add => $line); | |||||||||||||||||||||||||||
| 218 | 133 | ▶ | if ($line =~ m{^/}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | 7 | $self->commands->dispatch($line); | |||||||||||||||||||||||||||
| 220 | # A resume must not bring back what /clear removed (ADR 0015). | ||||||||||||||||||||||||||||
| 221 | 7 | ▶ | $self->runner->record($self->session, 'history.cleared') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->has_session and $line =~ m[\A/clear(?:\s|\z)]
MC/DC50%1/2
$self->has_session and $line =~ m[\A/clear(?:\s|\z)]
$self->has_session
$line =~ m[\A/clear(?:\s|\z)]
Truth table66%2/3
A and B
A= $self->has_sessionB= $line =~ m[\A/clear(?:\s|\z)]
| |||||||||||||||||||||||||||||
| 222 | if $self->has_session && $line =~ m{\A/clear(?:\s|\z)}; | ||||||||||||||||||||||||||||
| 223 | 7 | next; | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | 126 | ▶ | if ($line =~ m{\A([!?])\s*(\S.*)\z}s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | 87 | ▶ | $1 eq '!' ? $self->run_shell($2) : $self->run_shell_prompt($2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | 75 | next; | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | 39 | $self->run_prompt($line); | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 59 | $call->('save'); | |||||||||||||||||||||||||||
| 233 | 59 | return; | |||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 64 | ▶ | sub run_prompt { | ||||||||||||||||||||||||||
|
Subroutine
run_prompt: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 238 | 64 | my ( $self, $text ) = @_; | |||||||||||||||||||||||||||
| 239 | 64 | return $self->runner->run_prompt($text, session => scalar $self->_session_for_run); | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 58 | ▶ | sub run_shell { | ||||||||||||||||||||||||||
|
Subroutine
run_shell: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 244 | 58 | my ( $self, $command ) = @_; | |||||||||||||||||||||||||||
| 245 | # Ctrl-C reaches the command and raider alike; it is for the command. | ||||||||||||||||||||||||||||
| 246 | 58 | local $SIG{INT} = 'IGNORE'; | |||||||||||||||||||||||||||
| 247 | 58 | local $SIG{QUIT} = 'IGNORE'; | |||||||||||||||||||||||||||
| 248 | 58 | ▶ | my $pid = $self->_spawn_shell($command) // return; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->_spawn_shell($command) // (return)
MC/DC0%0/1
$self->_spawn_shell($command) // (return)
$self->_spawn_shell($command)
Truth table50%1/2
A
A= $self->_spawn_shell($command)
| |||||||||||||||||||||||||||||
| 249 | 51 | waitpid $pid, 0; | |||||||||||||||||||||||||||
| 250 | 51 | my $status = $?; | |||||||||||||||||||||||||||
| 251 | 51 | ▶ | $self->output->say_meta($self->_shell_status_text($status)) if $status; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | 51 | return $status; | |||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 29 | ▶ | sub run_shell_prompt { | ||||||||||||||||||||||||||
|
Subroutine
run_shell_prompt: called CC=11 SCAR=26.3
Pod
documented
| |||||||||||||||||||||||||||||
| 257 | 29 | my ( $self, $command ) = @_; | |||||||||||||||||||||||||||
| 258 | 29 | my $out = $self->output; | |||||||||||||||||||||||||||
| 259 | 29 | my ( $text, $status ) = ( '' ); | |||||||||||||||||||||||||||
| 260 | { | ||||||||||||||||||||||||||||
| 261 | 29 | local $SIG{INT} = 'IGNORE'; | |||||||||||||||||||||||||||
| 262 | 29 | local $SIG{QUIT} = 'IGNORE'; | |||||||||||||||||||||||||||
| 263 | 29 | my ( $r, $w ); | |||||||||||||||||||||||||||
| 264 | 29 | ▶ | unless (pipe $r, $w) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 265 | 0 | $out->say_error('cannot run the command: '.$!); | |||||||||||||||||||||||||||
| 266 | 0 | return 0; | |||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | 29 | my $pid = $self->_spawn_shell($command, $w); | |||||||||||||||||||||||||||
| 269 | 24 | close $w; | |||||||||||||||||||||||||||
| 270 | 24 | ▶ | unless (defined $pid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | 0 | close $r; | |||||||||||||||||||||||||||
| 272 | 0 | return 0; | |||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | 24 | my $bytes = ''; | |||||||||||||||||||||||||||
| 275 | 24 | while (1) { | |||||||||||||||||||||||||||
| 276 | 48 | my $n = sysread $r, $bytes, 65536, length $bytes; | |||||||||||||||||||||||||||
| 277 | 48 | ▶ | next if !defined $n && $!{EINTR}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $n and $!{"EINTR"}
MC/DC0%0/2
not defined $n and $!{"EINTR"}
not defined $n
$!{"EINTR"}
Truth table33%1/3
not(A) and B
A= not defined $nB= $!{"EINTR"}
| |||||||||||||||||||||||||||||
| 278 | 48 | ▶ | last unless $n; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 279 | 24 | my $chunk = $self->_decode_output(\$bytes); | |||||||||||||||||||||||||||
| 280 | 24 | $out->emit($chunk); | |||||||||||||||||||||||||||
| 281 | 24 | $out->out->flush; | |||||||||||||||||||||||||||
| 282 | 24 | $text .= $chunk; | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | 24 | close $r; | |||||||||||||||||||||||||||
| 285 | 24 | ▶ | if (length $bytes) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 286 | # What is left can be no character: replaced. | ||||||||||||||||||||||||||||
| 287 | 0 | my $rest = decode('UTF-8', $bytes); | |||||||||||||||||||||||||||
| 288 | 0 | $out->emit($rest); | |||||||||||||||||||||||||||
| 289 | 0 | $text .= $rest; | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | 24 | ▶ | $out->emit("\n") if length $text && $text !~ /\n\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
length $text and not $text =~ /\n\z/
MC/DC50%1/2
length $text and not $text =~ /\n\z/
length $text
not $text =~ /\n\z/
Truth table66%2/3
A and not(B)
A= length $textB= not $text =~ /\n\z/
| |||||||||||||||||||||||||||||
| 292 | 24 | waitpid $pid, 0; | |||||||||||||||||||||||||||
| 293 | 24 | $status = $?; | |||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | 24 | ▶ | if ($self->_signal_name($status & 127) eq 'INT') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 296 | 0 | $out->say_meta('command interrupted (SIGINT), nothing sent to the model'); | |||||||||||||||||||||||||||
| 297 | 0 | return 0; | |||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | 24 | ▶ | $out->say_meta($self->_shell_status_text($status)) if $status; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | 24 | return $self->run_prompt($self->shell_prompt($command, $status, $text)); | |||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | 18 | ▶ | sub shell_output_limit { 20_000 } | ||||||||||||||||||||||||||
|
Subroutine
shell_output_limit: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | 24 | ▶ | sub shell_prompt { | ||||||||||||||||||||||||||
|
Subroutine
shell_prompt: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 307 | 24 | my ( $self, $command, $status, $output ) = @_; | |||||||||||||||||||||||||||
| 308 | 24 | my $limit = $self->shell_output_limit; | |||||||||||||||||||||||||||
| 309 | 24 | ▶ | if (length $output > $limit) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | 6 | my $half = int($limit / 2); | |||||||||||||||||||||||||||
| 311 | 6 | $output = substr($output, 0, $half) | |||||||||||||||||||||||||||
| 312 | ."\n[... ".(length($output) - 2 * $half)." characters omitted ...]\n" | ||||||||||||||||||||||||||||
| 313 | .substr($output, -$half); | ||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||
| 315 | 24 | $output =~ s/\n\z//; | |||||||||||||||||||||||||||
| 316 | 24 | ▶ | return join "\n", | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | 'I ran a shell command in '.$self->app->root.':', | ||||||||||||||||||||||||||||
| 318 | '', | ||||||||||||||||||||||||||||
| 319 | '$ '.$command, | ||||||||||||||||||||||||||||
| 320 | '', | ||||||||||||||||||||||||||||
| 321 | 'Result: '.$self->_shell_status_text($status), | ||||||||||||||||||||||||||||
| 322 | '', | ||||||||||||||||||||||||||||
| 323 | length $output ? ( 'Output (stdout and stderr):', $output ) : '(no output)'; | ||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | # Forks the shell running $command in the app's root. With $capture, its | ||||||||||||||||||||||||||||
| 327 | # stdout and stderr go there and stdin comes from /dev/null; without, it | ||||||||||||||||||||||||||||
| 328 | # has raider's terminal. The pid, or undef after an error line. | ||||||||||||||||||||||||||||
| 329 | 87 | ▶ | sub _spawn_shell { | ||||||||||||||||||||||||||
|
Subroutine
_spawn_shell: called CC=6 SCAR=19.3
| |||||||||||||||||||||||||||||
| 330 | 87 | my ( $self, $command, $capture ) = @_; | |||||||||||||||||||||||||||
| 331 | 87 | ▶ | my $shell = $ENV{SHELL} || '/bin/sh'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$ENV{'SHELL'} || '/bin/sh'
MC/DC0%0/1
$ENV{'SHELL'} || '/bin/sh'
$ENV{'SHELL'}
Truth table50%1/2
A
A= $ENV{'SHELL'}
| |||||||||||||||||||||||||||||
| 332 | 87 | my $root = $self->app->root; | |||||||||||||||||||||||||||
| 333 | 87 | my $pid = fork; | |||||||||||||||||||||||||||
| 334 | 87 | ▶ | unless (defined $pid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 335 | 0 | $self->output->say_error('cannot run the command: '.$!); | |||||||||||||||||||||||||||
| 336 | 0 | return; | |||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||
| 338 | 87 | ▶ | return $pid if $pid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | # The child: never back into raider's code, whatever fails. | ||||||||||||||||||||||||||||
| 341 | 12 | $SIG{INT} = $SIG{QUIT} = 'DEFAULT'; | |||||||||||||||||||||||||||
| 342 | 12 | ▶ | if ($capture) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 343 | 5 | open STDIN, '<', '/dev/null'; | |||||||||||||||||||||||||||
| 344 | 5 | open STDOUT, '>&', $capture; | |||||||||||||||||||||||||||
| 345 | 5 | open STDERR, '>&', $capture; | |||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||
| 347 | 12 | ▶ | unless (chdir $root) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 348 | 0 | print STDERR 'raider: cannot change to '.$root.': '.$!."\n"; | |||||||||||||||||||||||||||
| 349 | 0 | POSIX::_exit(126); | |||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||
| 351 | 12 | { exec { $shell } $shell, '-c', encode('UTF-8', $command) } | |||||||||||||||||||||||||||
| 352 | 0 | print STDERR 'raider: cannot run '.$shell.': '.$!."\n"; | |||||||||||||||||||||||||||
| 353 | 0 | POSIX::_exit(127); | |||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | # The complete UTF-8 characters at the start of $$bytes, which keeps a | ||||||||||||||||||||||||||||
| 357 | # character cut off at its end for the next read. A byte that starts no | ||||||||||||||||||||||||||||
| 358 | # character becomes U+FFFD. | ||||||||||||||||||||||||||||
| 359 | 24 | ▶ | sub _decode_output { | ||||||||||||||||||||||||||
|
Subroutine
_decode_output: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||
| 360 | 24 | my ( $self, $bytes ) = @_; | |||||||||||||||||||||||||||
| 361 | 24 | my $text = ''; | |||||||||||||||||||||||||||
| 362 | 24 | ▶ | while (length $$bytes) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 363 | 24 | $text .= decode('UTF-8', $$bytes, FB_QUIET); | |||||||||||||||||||||||||||
| 364 | 24 | ▶ | last if length $$bytes < 4; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 365 | 0 | substr($$bytes, 0, 1, ''); | |||||||||||||||||||||||||||
| 366 | 0 | $text .= "\x{FFFD}"; | |||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||
| 368 | 24 | return $text; | |||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | # A wait status for the user: "exit status N", "interrupted (SIGINT)", | ||||||||||||||||||||||||||||
| 372 | # "killed by SIGTERM". | ||||||||||||||||||||||||||||
| 373 | 41 | ▶ | sub _shell_status_text { | ||||||||||||||||||||||||||
|
Subroutine
_shell_status_text: called CC=3 SCAR=15.1
| |||||||||||||||||||||||||||||
| 374 | 41 | my ( $self, $status ) = @_; | |||||||||||||||||||||||||||
| 375 | 41 | my $signal = $status & 127; | |||||||||||||||||||||||||||
| 376 | 41 | ▶ | return 'exit status '.($status >> 8) unless $signal; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 377 | 0 | my $name = $self->_signal_name($signal); | |||||||||||||||||||||||||||
| 378 | 0 | ▶ | return $name eq 'INT' ? 'interrupted (SIGINT)' : 'killed by SIG'.$name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | # The name of the signal number (INT), empty for 0. | ||||||||||||||||||||||||||||
| 382 | 24 | ▶ | sub _signal_name { | ||||||||||||||||||||||||||
|
Subroutine
_signal_name: called CC=3 SCAR=15.4
| |||||||||||||||||||||||||||||
| 383 | 24 | my ( $self, $number ) = @_; | |||||||||||||||||||||||||||
| 384 | 24 | ▶ | return $number ? ( split ' ', $Config{sig_name} )[$number] // $number : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
(split(' ', $Langertha::Raider::CLI::REPL::Config{"sig_name"}, 0))[$number] // $number
MC/DC0%0/2
(split(' ', $Langertha::Raider::CLI::REPL::Config{"sig_name"}, 0))[$number] // $number
(split(' ', $Langertha::Raider::CLI::REPL::Config{"sig_name"}, 0))[$number]
$number
Truth table0%0/3
A // B
A= (split(' ', $Langertha::Raider::CLI::REPL::Config{"sig_name"}, 0))[$number]B= $number
| |||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | 64 | ▶ | sub _session_for_run { | ||||||||||||||||||||||||||
|
Subroutine
_session_for_run: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||
| 388 | 64 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 389 | 64 | ▶ | return $self->session if $self->has_session; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 390 | 56 | ▶ | return unless $self->has_session_store; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 391 | 13 | my $out = $self->output; | |||||||||||||||||||||||||||
| 392 | 13 | my $session = eval { $self->session_store->create }; | |||||||||||||||||||||||||||
| 393 | 13 | ▶ | unless ($session) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 394 | 0 | my $error = $out->error_text($@); | |||||||||||||||||||||||||||
| 395 | 0 | $out->say_error('session not saved: '.$error); | |||||||||||||||||||||||||||
| 396 | 0 | return; | |||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||
| 398 | 13 | $self->_set_session($session); | |||||||||||||||||||||||||||
| 399 | 13 | $out->say_meta('session '.$session->id.' ('.$session->path.')'); | |||||||||||||||||||||||||||
| 400 | 13 | return $session; | |||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | # Line reader on a terminal: { read, impl, add, save, redisplay }. | ||||||||||||||||||||||||||||
| 404 | 0 | ▶ | sub _terminal_reader { | ||||||||||||||||||||||||||
|
Subroutine
_terminal_reader: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 405 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 406 | # Prefer Term::ReadLine::Gnu (line editing + persistent history). | ||||||||||||||||||||||||||||
| 407 | # Fall back to IO::Prompt::Tiny when Gnu isn't installed. | ||||||||||||||||||||||||||||
| 408 | 0 | my $term = Term::ReadLine->new('raider'); | |||||||||||||||||||||||||||
| 409 | return { | ||||||||||||||||||||||||||||
| 410 | 0 | ▶ | read => sub { prompt('raider>') }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 411 | 0 | ▶ | impl => 'IO::Prompt::Tiny (install Term::ReadLine::Gnu for history)', | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 412 | } unless $term->ReadLine =~ /Gnu/; | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | 0 | $term->ornaments(0); | |||||||||||||||||||||||||||
| 415 | 0 | ▶ | my $histfile = path($ENV{HOME} // '.')->child('.raider_history')->stringify; | ||||||||||||||||||||||||||
|
Condition0%0/2
$ENV{"HOME"} // "."
MC/DC0%0/1
$ENV{"HOME"} // "."
$ENV{"HOME"}
Truth table0%0/2
A
A= $ENV{"HOME"}
| |||||||||||||||||||||||||||||
| 416 | 0 | ▶ | eval { $term->ReadHistory($histfile) } if -f $histfile; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 417 | # Take SIGINT away from readline so our Perl handler always runs — with | ||||||||||||||||||||||||||||
| 418 | # catch_signals=1 (the default) Gnu's internal handler swallows Ctrl-C and | ||||||||||||||||||||||||||||
| 419 | # ours only fires after the next Return, which is the Docker annoyance. | ||||||||||||||||||||||||||||
| 420 | 0 | eval { $term->Attribs->{catch_signals} = 0 }; | |||||||||||||||||||||||||||
| 421 | # Gray prompt, plain-color user input. Non-printing sequences are | ||||||||||||||||||||||||||||
| 422 | # wrapped in \x01...\x02 so readline computes prompt width correctly. | ||||||||||||||||||||||||||||
| 423 | 0 | my $ps = "\x01".color('bright_black')."\x02".'raider> '."\x01".color('reset')."\x02"; | |||||||||||||||||||||||||||
| 424 | return { | ||||||||||||||||||||||||||||
| 425 | 0 | ▶ | read => sub { $term->readline($ps) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 426 | impl => $term->ReadLine, | ||||||||||||||||||||||||||||
| 427 | 0 | ▶ | add => sub { $term->addhistory($_[0]) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 428 | 0 | ▶ | save => sub { eval { $term->WriteHistory($histfile) } }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 429 | 0 | ▶ | redisplay => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 430 | 0 | eval { | |||||||||||||||||||||||||||
| 431 | 0 | $term->replace_line('', 0); | |||||||||||||||||||||||||||
| 432 | 0 | $term->Attribs->{point} = 0; | |||||||||||||||||||||||||||
| 433 | 0 | $term->Attribs->{end} = 0; | |||||||||||||||||||||||||||
| 434 | 0 | $term->on_new_line; | |||||||||||||||||||||||||||
| 435 | 0 | $term->redisplay; | |||||||||||||||||||||||||||
| 436 | }; | ||||||||||||||||||||||||||||
| 437 | }, | ||||||||||||||||||||||||||||
| 438 | 0 | }; | |||||||||||||||||||||||||||
| 439 | } | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | 56 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | 56 | 1; | |||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | __END__ | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | =pod | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | =head1 NAME | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | Langertha::Raider::CLI::REPL - Internal interactive loop of the raider CLI | ||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | =head1 VERSION | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | version 0.503 | ||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | # Internal to Langertha-Raider -- no API promise. | ||||||||||||||||||||||||||||
| 462 | Langertha::Raider::CLI::REPL->new(app => $app, output => $out)->run(@first_prompt); | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | B<Internal module.> Its interface may change without notice. | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | The F<raider> REPL: banner, line reading, slash commands via | ||||||||||||||||||||||||||||
| 469 | L<Langertha::Raider::CLI::Commands>, every other line a prompt via | ||||||||||||||||||||||||||||
| 470 | L<Langertha::Raider::CLI::Runner>, until C</quit>, C</exit>, C<:q>, | ||||||||||||||||||||||||||||
| 471 | C<quit>, C<exit> or the end of input. | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | A line C<!CMD> runs C<CMD> in the shell and nothing else | ||||||||||||||||||||||||||||
| 474 | (L</run_shell>); a line C<?CMD> runs it and then sends the command, its | ||||||||||||||||||||||||||||
| 475 | exit status and its output to the model as the next prompt | ||||||||||||||||||||||||||||
| 476 | (L</run_shell_prompt>). A line that is only C<!> or C<?> is a prompt. | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | On a terminal, lines come from L<Term::ReadLine::Gnu> (line editing, | ||||||||||||||||||||||||||||
| 479 | F<~/.raider_history>) or L<IO::Prompt::Tiny>. | ||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | The first C<SIGINT> (Ctrl-C) while a prompt runs cancels that turn | ||||||||||||||||||||||||||||
| 482 | (L<Langertha::Raider::CLI::Runner/cancel_run>): the tool commands still | ||||||||||||||||||||||||||||
| 483 | running are ended, the raid stops at its next safe point, C<turn | ||||||||||||||||||||||||||||
| 484 | cancelled> is printed, the session journal ends the run as C<cancelled>, | ||||||||||||||||||||||||||||
| 485 | and the REPL reads the next line. At the prompt the first Ctrl-C only | ||||||||||||||||||||||||||||
| 486 | warns. A second one within two seconds, or a C<SIGTERM>, leaves the REPL | ||||||||||||||||||||||||||||
| 487 | with exit status 0, after ending the tool commands still running | ||||||||||||||||||||||||||||
| 488 | (L<Langertha::Raider::CLI::Runner/terminate_children>). While a C<!CMD> | ||||||||||||||||||||||||||||
| 489 | or C<?CMD> runs, raider ignores C<SIGINT>, so Ctrl-C ends the command | ||||||||||||||||||||||||||||
| 490 | alone. When L</in> | ||||||||||||||||||||||||||||
| 491 | is not a terminal, lines are read from it as they are, without prompt, so | ||||||||||||||||||||||||||||
| 492 | piped input ends the REPL at its end. | ||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | =head2 app | ||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||
| 496 | The L<Langertha::Raider::CLI>. Required. | ||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||
| 498 | =head2 output | ||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||
| 500 | The L<Langertha::Raider::CLI::Output> to print to. Required. | ||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||
| 502 | =head2 in | ||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | Filehandle lines are read from when it is not a terminal. Defaults to | ||||||||||||||||||||||||||||
| 505 | C<STDIN>. | ||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||
| 507 | =head2 active_profiles | ||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | Agent profiles to show in the banner (C<claude>, C<openai>). | ||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | =head2 saved_profiles | ||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||
| 513 | Profiles saved to the config file by this start, marked C<(saved)>. | ||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | =head2 customize_prompt | ||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | Start with the prompt-builder (C<--customize-prompt>). | ||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||
| 519 | =head2 session_store | ||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | The L<Langertha::Raider::SessionStore> a new session is created in, with | ||||||||||||||||||||||||||||
| 522 | the first prompt. Without one (C<--no-session>) nothing is recorded. | ||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||
| 524 | =head2 session | ||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||
| 526 | The L<Langertha::Raider::Session> the prompts are recorded in, once there | ||||||||||||||||||||||||||||
| 527 | is one. A C</clear> is recorded there too, as C<history.cleared>, so | ||||||||||||||||||||||||||||
| 528 | resuming the session starts with an empty history again. | ||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||
| 530 | =head2 notes | ||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | Lines printed right after the banner: what resuming L</session> found | ||||||||||||||||||||||||||||
| 533 | (L<Langertha::Raider::CLI::Sessions/restore>). | ||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | =head2 banner | ||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||
| 537 | $repl->banner($readline_impl); | ||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||
| 539 | Prints the logo and the live configuration: mission source, profiles, | ||||||||||||||||||||||||||||
| 540 | skills, packs, ignored agent files, engine, model, API key, root. | ||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | =head2 run | ||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||
| 544 | $repl->run(@first_prompt); | ||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||
| 546 | Runs the REPL; C<@first_prompt> (the words given on the command line) is | ||||||||||||||||||||||||||||
| 547 | sent first. Returns when the user leaves. | ||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||
| 549 | =head2 run_prompt | ||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||
| 551 | $repl->run_prompt($text); | ||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||
| 553 | Runs one prompt through the runner, recorded in L</session>. The first | ||||||||||||||||||||||||||||
| 554 | prompt with a L</session_store> starts the session and names it. | ||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||
| 556 | =head2 run_shell | ||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||
| 558 | my $status = $repl->run_shell('git status'); | ||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||
| 560 | The REPL's C<!CMD>: runs the command with C<$SHELL -c> (C</bin/sh> | ||||||||||||||||||||||||||||
| 561 | without C<SHELL>) in the app's root, on the terminal raider runs on, so | ||||||||||||||||||||||||||||
| 562 | C<less> or C<vim> work. A non-zero exit status is printed, as C<exit | ||||||||||||||||||||||||||||
| 563 | status N>, a command ended by a signal as C<interrupted (SIGINT)> or | ||||||||||||||||||||||||||||
| 564 | C<killed by SIGTERM>. Nothing goes to the model or into the session | ||||||||||||||||||||||||||||
| 565 | journal. Returns the wait status, or undef when the command could not be | ||||||||||||||||||||||||||||
| 566 | started. | ||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | =head2 run_shell_prompt | ||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||
| 570 | my $ok = $repl->run_shell_prompt('make test'); | ||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||
| 572 | The REPL's C<?CMD>: runs the command as L</run_shell> does, but with its | ||||||||||||||||||||||||||||
| 573 | standard output and standard error shown as they come and captured, and | ||||||||||||||||||||||||||||
| 574 | standard input from F</dev/null>. Then the command, its exit status and | ||||||||||||||||||||||||||||
| 575 | the captured output (L</shell_prompt>) go to the model through | ||||||||||||||||||||||||||||
| 576 | L</run_prompt>, as any prompt: the model answers, the session journal | ||||||||||||||||||||||||||||
| 577 | records the turn. A command ended by Ctrl-C (C<SIGINT>) sends nothing, | ||||||||||||||||||||||||||||
| 578 | since Ctrl-C in the REPL cancels the turn. Returns what L</run_prompt> | ||||||||||||||||||||||||||||
| 579 | returns, false when nothing was sent. | ||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||
| 581 | =head2 shell_prompt | ||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||
| 583 | my $prompt = $repl->shell_prompt($command, $wait_status, $output); | ||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||
| 585 | The prompt L</run_shell_prompt> sends: where the command ran, the command, | ||||||||||||||||||||||||||||
| 586 | its exit status and its output. An output longer than | ||||||||||||||||||||||||||||
| 587 | L</shell_output_limit> characters keeps its head and its tail, half the | ||||||||||||||||||||||||||||
| 588 | limit each, with a line C<[... N characters omitted ...]> between them. | ||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||
| 590 | =head2 shell_output_limit | ||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||
| 592 | How many characters of a C<?CMD> output reach the model: 20000. | ||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||
| 594 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||
| 596 | =over | ||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||
| 598 | =item * L<Langertha::Raider::CLI::Main> | ||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||
| 600 | =back | ||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||
| 602 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||
| 604 | =head2 Issues | ||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||
| 606 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||
| 607 | L<https://github.com/Getty/langertha-raider/issues>. | ||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||
| 609 | =head2 IRC | ||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||
| 611 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||
| 613 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||
| 615 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||
| 617 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||
| 619 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||
| 621 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||
| 623 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||
| 625 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 626 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||
| 628 | =cut | ||||||||||||||||||||||||||||