lib/App/teai.pm

filter: Statementstmt 20.7% 155 / 746 Branchbran 17.5% 79 / 450 Conditioncond 12.6% 37 / 292 MC/DCmcdc 12.0% 19 / 158 Subroutinesub 24.3% 27 / 111 Podpod 0.0% 0 / 13 totaltotal 17.9% 317 / 1770 CC 352 SCAR 54.6
CC
352
Cov
18%
cmd_inbox
1359.0
cmd_ask
650.0
cmd_who
563.6
CRAP 236.2
« lib/App/teai/Dash/Sessions.pm ↑ Summary lib/App/teai/MCP.pm »
1 package App::teai;
2 24 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 24 ▶use App::teai::Err;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 24 ▶use App::teai::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 24 ▶use App::teai::Bus;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 24 ▶use App::teai::Wire ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 24 ▶use Cpanel::JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 24 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 24 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 24 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 24 our $VERSION = '0.08';
14  
15 24 my @COMMANDS = qw(init join session send ask reply inbox pending wait log exec lock mcp hook round who dash);
16 # receive takes a wait of 0 as no bound on the reader-lock wait
17 24 my $MIN_WAIT = 0.001;
18 24 my $MAX_TIMEOUT = 1e9;
19 24 my $SESSION_JSON = Cpanel::JSON::XS->new->utf8->canonical;
20 24 my $NO_PROJECT = 'no project: add --project P, or set TEAI_PROJECT or .teai/id';
21  
22 24 my @HELP = (
23 [ init => 'init', 'create the stream and the KV buckets' ],
24 [ join => 'join [--agent AGENT] [--project P] [--role R] [--lead [--take]]', 'create a session for the caller' ],
25 [ 'session new' => 'session new [--agent AGENT] [--project P] [--role R]', 'create a session for someone else' ],
26 [ 'session list' => 'session list [--json]', 'list the sessions on the hub' ],
27 [ 'session set' => 'session set ID [--project P] [--role R]', 'change the attributes of a session' ],
28 [ 'session lead' => 'session lead P [--take]', "make the caller's session the lead of a project" ],
29 [ 'session rm' => 'session rm [--self] [--force] ID...', 'remove sessions' ],
30 [ send => 'send session:ID|live:P|role:P/R|project:P [--thread T] [BODY|-]', 'send a message' ],
31 [ ask => 'ask session:ID|live:P|role:P/R|project:P [--thread T] [--timeout S] [--async] [--json] [BODY|-]',
32 'send a question and wait for the reply' ],
33 [ reply => 'reply SEQ [BODY|-]', 'answer a message' ],
34 [ inbox => 'inbox [--wait S] [--thread T --count N | [--thread T] --re SEQ,... | --project [P]] [--json] [--no-wait-lock]',
35 'print pending messages' ],
36 [ pending => 'pending', 'print the number of unacknowledged messages' ],
37 [ wait => 'wait [--timeout S] [--follow]', 'wait for new mail in the inbox' ],
38 [ log => 'log --thread T [--json]', 'print the messages of a thread' ],
39 [ exec => 'exec --session ID [--wait S] [--timeout S] [--seq SEQ] -- COMMAND [ARG...]', 'serve one brief by running a command' ],
40 [ lock => 'lock [--wait SECONDS] [--global] -- COMMAND [ARG...]', 'run a command under the project test lock' ],
41 [ mcp => 'mcp', 'serve the mailbox as an MCP server' ],
42 [ hook => 'hook [reset]', 'announce new mail to a Claude Code session' ],
43 [ 'round start' => 'round start --thread T [--brief FILE|-] [--add] [--project P] NAME=SESSION...',
44 'start a round and send the brief' ],
45 [ 'round status' => 'round status T [--project P] [--json]', 'show the state of each round member' ],
46 [ 'round collect' => 'round collect T [--wait S] [--ack] [--project P] [--json]', 'print the newest reply of each member' ],
47 [ 'round list' => 'round list [--project P] [--json]', 'list the rounds of a project' ],
48 [ 'round resend' => 'round resend T [--project P] NAME...', 'send the brief again to members' ],
49 [ who => 'who [--project P] [--role R] [--agent AGENT] [--json]', 'list the live sessions' ],
50 [ dash => 'dash [--listen ADDR:PORT] [--project P]', 'serve a web console of the hub' ],
51 );
52 24 my %HELP = map { $_->[0] => $_ } @HELP;
53  
54 24 ▶sub _usage ($key) {
Subroutine _usage: called CC=2 SCAR=12.4
55 24 ▶ return "usage: teai $HELP{$key}[1]" if $HELP{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $HELP{$key} 24 0 24
56 0 my @subs = map { $_->[1] =~ s/\A\Q$key\E //r } grep { $_->[0] =~ /\A\Q$key\E / } @HELP;
57 0 return "usage: teai $key <" . join('|', @subs) . '>';
58 }
59  
60 5 ▶sub usage ($fh = \*STDERR) {
Subroutine usage: called CC=2 SCAR=6.9
Pod undocumented
61 5 my $width = (sort { $b <=> $a } map { length $_->[0] } @HELP)[0];
62 5 print $fh "usage: teai <@{[ join '|', @COMMANDS ]}> [args]\n\n",
63 5 (map { sprintf " %-${width}s %s\n", @$_[0, 2] } @HELP),
64 "\nteai help COMMAND shows its synopsis; the manual is perldoc App::teai\n";
65 }
66  
67 14 ▶sub _help_for ($cmd, $sub = undef) {
Subroutine _help_for: called CC=6 SCAR=17.9
68 14 my @rows = grep { $_->[0] =~ /\A\Q$cmd\E(?: |\z)/ } @HELP;
69 14 ▶ @rows = grep { $_->[0] eq "$cmd $sub" } @rows if @rows > 1 && defined $sub;
Branch: 1 branch
BranchTrueFalseTotal
if @rows > 1 and defined $sub 5 9 14
Condition100%3/3
@rows > 1 and defined $sub
!ll&&!rl&&r
815
MC/DC100%2/2
@rows > 1 and defined $sub
@rows > 1 defined $sub
Truth table100%3/3
A and B
A= @rows > 1B= defined $sub
ABresult
0X0
100
111
70 14 ▶ return unless @rows;
Branch: 1 branch
BranchTrueFalseTotal
unless @rows 1 13 14
71 13 my $text = join '', map { "usage: teai $_->[1]\n" } @rows;
72 13 ▶ return "$text$rows[0][2]\n" if @rows == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @rows == 1 12 1 13
73 1 my $width = (sort { $b <=> $a } map { length $_->[0] } @rows)[0];
74 1 return join '', $text, "\n", map { sprintf " %-${width}s %s\n", @$_[0, 2] } @rows;
75 }
76  
77 17 ▶sub _print_help ($cmd, $sub = undef) {
Subroutine _print_help: called CC=6 SCAR=17.9
78 17 ▶ my $text = (grep { $_ eq $cmd } @COMMANDS) ? _help_for($cmd, $sub) : undef;
Branch: 1 branch
BranchTrueFalseTotal
grep({$_ eq $cmd;} @COMMANDS) ? : 14 3 17
79 17 ▶ if (!defined $text) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $text) 4 13 17
80 4 ▶ my $what = join ' ', $cmd, (App::teai->can("cmd_$cmd") && defined $sub ? $sub : ());
Branch: 1 branch
BranchTrueFalseTotal
'App::teai'->can("cmd_$cmd") && defined $sub ? : 1 3 4
Condition66%2/3
"App::teai"->can("cmd_$cmd") and defined $sub
!ll&&!rl&&r
301
MC/DC50%1/2
"App::teai"->can("cmd_$cmd") and defined $sub
"App::teai"->can("cmd_$cmd") defined $sub
Truth table66%2/3
A and B
A= "App::teai"->can("cmd_$cmd")B= defined $sub
ABresult
0X0
100
111
81 4 print STDERR "teai: unknown command '$what'\n";
82 4 return 2;
83 }
84 13 print $text;
85 13 return 0;
86 }
87  
88 50 ▶sub _wants_help ($cmd, $args) {
Subroutine _wants_help: called CC=12 SCAR=24.9
89 50 ▶ return 0 if $cmd eq 'hook';
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "hook" 1 49 50
90 49 ▶ return ($args->[0] // '') =~ /\A(?:--help|-h)\z/ ? 1 : 0 if $cmd eq 'reply';
Branch: 2 branches
BranchTrueFalseTotal
if $cmd eq "reply" 3 46 49
($args->[0] // '') =~ /\A(?:--help|-h)\z/u ? : 2 1 3
Condition50%1/2
$args->[0] // ""
l!l
30
MC/DC0%0/1
$args->[0] // ""
$args->[0]
Truth table50%1/2
A
A= $args->[0]
Aresult
11
00
91 46 my $takes = { exec => qr/\A--(?:session|wait|timeout|seq)\z/, lock => qr/\A--wait\z/ }->{$cmd};
92 46 ▶ for (my $i = 0; $i < @$args; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$args) 82 19 101
93 82 my $arg = $args->[$i];
94 82 ▶ return 0 if $arg eq '--';
Branch: 1 branch
BranchTrueFalseTotal
if $arg eq "--" 18 64 82
95 64 ▶ return 1 if $arg eq '--help' || $arg eq '-h';
Branch: 1 branch
BranchTrueFalseTotal
if $arg eq "--help" or $arg eq "-h" 7 57 64
Condition100%3/3
$arg eq "--help" or $arg eq "-h"
l!l&&r!l&&!r
3457
MC/DC100%2/2
$arg eq "--help" or $arg eq "-h"
$arg eq "--help" $arg eq "-h"
Truth table100%3/3
A or B
A= $arg eq "--help"B= $arg eq "-h"
ABresult
1X1
011
000
96 57 ▶ if ($takes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($takes) 24 33 57
97 24 ▶ $i++ if $arg =~ $takes;
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /$takes/u 15 9 24
98 24 ▶ return 0 if $arg !~ /\A-/;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg =~ /\A-/u 2 22 24
99 }
100 }
101 19 return 0;
102 }
103  
104 66 ▶sub run (@argv) {
Subroutine run: called CC=19 SCAR=29.5
Pod undocumented
105 66 $SIG{PIPE} = 'IGNORE';
106 66 my $name = shift @argv;
107 66 ▶ if (defined $name && $name eq 'help' && @argv > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name and $name eq "help" and @argv > 2) 2 64 66
Condition100%6/6
defined $name and $name eq "help" and @argv > 2
!ll&&!rl&&r
54102
defined $name and $name eq "help"
!ll&&!rl&&r
15312
MC/DC100%3/3
defined $name and $name eq "help" and @argv > 2
defined $name $name eq "help" @argv > 2
Truth table100%4/4
A and B and C
A= defined $nameB= $name eq "help"C= @argv > 2
ABCresult
0XX0
10X0
1100
1111
108 2 print STDERR "teai: usage: teai help [COMMAND [SUBCOMMAND]]\n";
109 2 return 2;
110 }
111 64 ▶ return _print_help(@argv) if defined $name && $name eq 'help' && @argv;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name and $name eq "help" and @argv 8 56 64
Condition100%6/6
defined $name and $name eq "help" and @argv
!ll&&!rl&&r
5428
defined $name and $name eq "help"
!ll&&!rl&&r
15310
MC/DC100%3/3
defined $name and $name eq "help" and @argv
defined $name $name eq "help" @argv
Truth table100%4/4
A and B and C
A= defined $nameB= $name eq "help"C= @argv
ABCresult
0XX0
10X0
1100
1111
112 56 ▶ if (defined $name && $name =~ /\A(?:help|--help|-h)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name and $name =~ /\A(?:help|--help|-h)\z/u) 4 52 56
Condition100%3/3
defined $name and $name =~ /\A(?:help|--help|-h)\z/u
!ll&&!rl&&r
1514
MC/DC100%2/2
defined $name and $name =~ /\A(?:help|--help|-h)\z/u
defined $name $name =~ /\A(?:help|--help|-h)\z/u
Truth table100%3/3
A and B
A= defined $nameB= $name =~ /\A(?:help|--help|-h)\z/u
ABresult
0X0
100
111
113 4 usage(\*STDOUT);
114 4 return 0;
115 }
116 52 ▶ if (!defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 1 51 52
117 1 usage();
118 1 return 2;
119 }
120 51 ▶ my $h = (grep { $_ eq $name } @COMMANDS) ? App::teai->can("cmd_$name") : undef;
Branch: 1 branch
BranchTrueFalseTotal
grep({$_ eq $name;} @COMMANDS) ? : 50 1 51
121 51 ▶ if (!$h) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($h) 1 50 51
122 1 print STDERR "teai: unknown command '$name'\n";
123 1 return 2;
124 }
125 50 ▶ return _print_help($name, (defined $argv[0] && $HELP{"$name $argv[0]"} ? $argv[0] : undef)) if _wants_help($name, \@argv);
Branch: 2 branches
BranchTrueFalseTotal
if _wants_help($name, \@argv) 9 41 50
defined $argv[0] && $HELP{"$name $argv[0]"} ? : 3 6 9
Condition66%2/3
defined $argv[0] and $HELP{"$name $argv[0]"}
!ll&&!rl&&r
063
MC/DC50%1/2
defined $argv[0] and $HELP{"$name $argv[0]"}
defined $argv[0] $HELP{"$name $argv[0]"}
Truth table66%2/3
A and B
A= defined $argv[0]B= $HELP{"$name $argv[0]"}
ABresult
0X0
100
111
126 41 my $rc = eval { $h->(\@argv) };
127 35 ▶ if (my $e = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 22 13 35
128 22 ▶ if (ref $e && eval { $e->isa('App::teai::Err') }) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $e and eval { do { $e->isa("App::teai::Err") } }) 21 1 22
Condition66%2/3
ref $e and eval { do { $e->isa("App::teai::Err") } }
!ll&&!rl&&r
1021
MC/DC50%1/2
ref $e and eval { do { $e->isa("App::teai::Err") } }
ref $e eval { do { $e->isa("App::teai::Err") } }
Truth table66%2/3
A and B
A= ref $eB= eval { do { $e->isa("App::teai::Err") } }
ABresult
0X0
100
111
129 21 print STDERR 'teai: ', App::teai::Err::msg($e), "\n";
130 21 return App::teai::Err::code($e);
131 }
132 1 $e =~ s/\s+at \S+ line \d+\.?\s*\z//;
133 1 $e =~ s/\s+\z//;
134 1 print STDERR "teai: $e\n";
135 1 return 3;
136 }
137 13 ▶ return $rc // 0;
Condition50%1/2
$rc // 0
l!l
130
MC/DC0%0/1
$rc // 0
$rc
Truth table50%1/2
A
A= $rc
Aresult
11
00
138 }
139  
140 1 ▶sub cmd_init ($args) {
Subroutine cmd_init: called CC=3 SCAR=21.1
141 1 ▶ App::teai::Err::fail(2, _usage('init')) if @$args;
Branch: 1 branch
BranchTrueFalseTotal
if @$args 1 0 1
142 0 my $bus = App::teai::Bus->new(%{ App::teai::Config::load() }, reconnect => 0);
143 0 ▶ $bus->await(sub ($done) { $bus->init($done) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
144 0 ▶ _write_stdout(join '', "stream TEAI ready\n", map { "bucket $_ ready\n" } App::teai::Bus::bucket_names())
Branch: 1 branch
BranchTrueFalseTotal
_write_stdout(join("", "stream TEAI ready\n", map({"bucket $_ ready\n";} App::teai::Bus::bucket_names()))) or App::teai::Err::fail(3, "write failed: $!") 0 0 0
145 or App::teai::Err::fail(3, "write failed: $!");
146 0 return 0;
147 }
148  
149 0 ▶sub _bus ($reconnect = 0, %o) { App::teai::Bus->new(%{ App::teai::Config::load() }, reconnect => $reconnect, %o) }
Subroutine _bus: not called CC=2 SCAR=17.9
150  
151 30 ▶sub _getopt (@spec) {
Subroutine _getopt: called CC=3 SCAR=11.0
152 30 ▶ my $parser = ref $spec[0] eq 'ARRAY' ? Getopt::Long::Parser->new(config => shift @spec) : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $spec[0] eq 'ARRAY' ? : 17 13 30
153 30 ▶ local $SIG{__WARN__} = sub ($w) { print STDERR "teai: $w" };
Subroutine __ANON__: called CC=1 SCAR=0.0
154 30 ▶ return $parser ? $parser->getoptions(@spec) : Getopt::Long::GetOptions(@spec);
Branch: 1 branch
BranchTrueFalseTotal
$parser ? : 17 13 30
155 }
156  
157 9 ▶sub _check_attr ($name, $value) {
Subroutine _check_attr: called CC=5 SCAR=16.4
158 9 ▶ return unless defined $value && length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 3 6 9
Condition66%2/3
defined $value and length $value
!ll&&!rl&&r
306
MC/DC50%1/2
defined $value and length $value
defined $value length $value
Truth table66%2/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
159 6 ▶ App::teai::Err::fail(2, $name eq 'project' ? "invalid project id '$value'" : "invalid role '$value'")
Branch: 2 branches
BranchTrueFalseTotal
unless App::teai::Wire::valid_id($value) 6 0 6
$name eq 'project' ? : 3 3 6
160 unless App::teai::Wire::valid_id($value);
161 }
162  
163 9 ▶sub _session_opts ($args, $cmd, @extra_spec) {
Subroutine _session_opts: called CC=10 SCAR=25.6
164 9 my ($agent, $project, $role) = ('shell');
165 9 local @ARGV = @$args;
166 9 ▶ _getopt('agent=s' => \$agent, 'project=s' => \$project, 'role=s' => \$role, @extra_spec)
Branch: 1 branch
BranchTrueFalseTotal
_getopt("agent=s", \$agent, "project=s", \$project, "role=s", \$role, @extra_spec) or App::teai::Err::fail(2, _usage($cmd)) 7 2 9
167 or App::teai::Err::fail(2, _usage($cmd));
168 7 ▶ App::teai::Err::fail(2, "unexpected argument '$ARGV[0]'") if @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV 0 7 7
169 7 ▶ App::teai::Err::fail(2, "unknown agent '$agent'") unless App::teai::Config::valid_agent($agent);
Branch: 1 branch
BranchTrueFalseTotal
unless App::teai::Config::valid_agent($agent) 2 5 7
170 5 ▶ $project //= App::teai::Config::project();
Condition66%2/3
$project //= App::teai::Config::project()
l!l&&r!l&&!r
203
MC/DC50%1/2
$project //= App::teai::Config::project()
$project App::teai::Config::project()
Truth table66%2/3
A //= B
A= $projectB= App::teai::Config::project()
ABresult
1X1
011
000
171 5 ▶ $role //= App::teai::Config::role() if $cmd eq 'join';
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "join" 3 2 5
Condition66%2/3
$role //= App::teai::Config::role()
l!l&&r!l&&!r
201
MC/DC50%1/2
$role //= App::teai::Config::role()
$role App::teai::Config::role()
Truth table66%2/3
A //= B
A= $roleB= App::teai::Config::role()
ABresult
1X1
011
000
172 4 _check_attr(project => $project);
173 2 _check_attr(role => $role);
174 0 ▶ $_ = length($_ // '') ? $_ : undef for $project, $role;
Branch: 1 branch
BranchTrueFalseTotal
length($_ // '') ? : 0 0 0
Condition0%0/2
$_ // ""
l!l
00
MC/DC0%0/1
$_ // ""
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
175 0 return { agent => $agent, project => $project, role => $role };
176 }
177  
178 5 ▶sub cmd_join ($args) {
Subroutine cmd_join: called CC=9 SCAR=43.8
Pod undocumented
179 5 my ($lead, $take);
180 5 my $attrs = _session_opts($args, 'join', 'lead' => \$lead, 'take' => \$take);
181 0 ▶ App::teai::Err::fail(2, _usage('join')) if $take && !$lead;
Branch: 1 branch
BranchTrueFalseTotal
if $take and not $lead 0 0 0
Condition0%0/3
$take and not $lead
!ll&&!rl&&r
000
MC/DC0%0/2
$take and not $lead
$take not $lead
Truth table0%0/3
A and not(B)
A= $takeB= not $lead
ABresult
0X0
100
111
182 0 my $project = $attrs->{project};
183 0 ▶ App::teai::Err::fail(2, $NO_PROJECT) if $lead && !defined $project;
Branch: 1 branch
BranchTrueFalseTotal
if $lead and not defined $project 0 0 0
Condition0%0/3
$lead and not defined $project
!ll&&!rl&&r
000
MC/DC0%0/2
$lead and not defined $project
$lead not defined $project
Truth table0%0/3
A and not(B)
A= $leadB= not defined $project
ABresult
0X0
100
111
184 0 my $bus = _bus();
185 0 ▶ if ($lead) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lead) 0 0 0
186 0 require App::teai::Lead;
187 0 ▶ $bus->await(sub ($done) { App::teai::Lead::vet($bus, $project, '', $take, $done) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
188 }
189 0 ▶ my $id = $bus->await(sub ($done) { $bus->new_session($attrs, $done) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
190 0 ▶ if ($lead && !eval { $bus->await(sub ($done) { App::teai::Lead::take($bus, $project, $id, $take, $done) }); 1 }) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if ($lead and not eval { do { $bus->await(sub ($done) { App::teai::Lead::take($bus, $project, $id, $take, $done); } ); 1 } }) 0 0 0
Condition0%0/3
$lead and not eval { do { $bus->await(sub ($done) { App::teai::Lead::take($bus, $project, $id, $take, $done); } ); 1 } }
!ll&&!rl&&r
000
MC/DC0%0/2
$lead and not eval { do { $bus->await(sub ($done) { App::teai::Lead::take($bus, $project, $id, $take, $done); } ); 1 } }
$lead not eval { do { $bus->await(sub ($done) { App::teai::Lead::take($bus, $project, $id, $take, $done); } ); 1 } }
Truth table0%0/3
A and not(B)
A= $leadB= not eval { do { $bus->await(sub ($done) { App::teai::Lead::take($bus, $project, $id, $take, $done); } ); 1 } }
ABresult
0X0
100
111
191 0 my $e = $@;
192 0 ▶ eval { $bus->await(sub ($done) { App::teai::Lead::release($bus, $id, $done) }) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
193 0 ▶ eval { $bus->await(sub ($done) { $bus->remove_session($id, $done) }) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
194 0 die $e;
195 }
196 0 ▶ _write_stdout("export TEAI_SESSION=$id\n") or App::teai::Err::fail(3, "write failed: $!");
Branch: 1 branch
BranchTrueFalseTotal
_write_stdout("export TEAI_SESSION=$id\n") or App::teai::Err::fail(3, "write failed: $!") 0 0 0
197 0 return 0;
198 }
199  
200 4 ▶sub cmd_session ($args) {
Subroutine cmd_session: called CC=3 SCAR=11.3
Pod undocumented
201 4 my ($sub, @rest) = @$args;
202 4 my %run = (
203 new => \&_session_new, list => \&_session_list, set => \&_session_set, lead => \&_session_lead, rm => \&_session_rm,
204 );
205 4 ▶ my $h = defined $sub ? $run{$sub} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $sub ? : 4 0 4
206 4 ▶ App::teai::Err::fail(2, _usage('session')) unless $h;
Branch: 1 branch
BranchTrueFalseTotal
unless $h 0 4 4
207 4 return $h->(\@rest);
208 }
209  
210 4 ▶sub _session_new ($args) {
Subroutine _session_new: called CC=2 SCAR=15.4
211 4 my $attrs = _session_opts($args, 'session new');
212 0 my $bus = _bus();
213 0 ▶ my $id = $bus->await(sub ($done) { $bus->new_session($attrs, $done) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
214 0 ▶ _write_stdout("$id\n") or App::teai::Err::fail(3, "write failed: $!");
Branch: 1 branch
BranchTrueFalseTotal
_write_stdout("$id\n") or App::teai::Err::fail(3, "write failed: $!") 0 0 0
215 0 return 0;
216 }
217  
218 0 ▶sub _session_set ($args) {
Subroutine _session_set: not called CC=16 SCAR=56.1
219 0 my $usage = _usage('session set');
220 0 my %set;
221 0 local @ARGV = @$args;
222 0 ▶ _getopt('project=s' => \$set{project}, 'role=s' => \$set{role}) or App::teai::Err::fail(2, $usage);
Branch: 1 branch
BranchTrueFalseTotal
_getopt("project=s", \$set{"project"}, "role=s", \$set{"role"}) or App::teai::Err::fail(2, $usage) 0 0 0
223 0 delete @set{ grep { !defined $set{$_} } keys %set };
224 0 ▶ App::teai::Err::fail(2, $usage) unless @ARGV == 1 && %set;
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV == 1 and %set 0 0 0
Condition0%0/3
@ARGV == 1 and %set
!ll&&!rl&&r
000
MC/DC0%0/2
@ARGV == 1 and %set
@ARGV == 1 %set
Truth table0%0/3
A and B
A= @ARGV == 1B= %set
ABresult
0X0
100
111
225 0 my $id = $ARGV[0];
226 0 ▶ App::teai::Err::fail(2, "invalid id '$id'") unless App::teai::Wire::valid_id($id);
Branch: 1 branch
BranchTrueFalseTotal
unless App::teai::Wire::valid_id($id) 0 0 0
227 0 _check_attr($_ => $set{$_}) for sort keys %set;
228 0 my $bus = _bus();
229 0 ▶ my $attrs = $bus->await(sub ($done) { $bus->session_attrs($id, $done) }) or App::teai::Err::fail(1, "no session $id");
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
my $attrs = $bus->await(sub ($done) { $bus->session_attrs($id, $done); } ) or App::teai::Err::fail(1, "no session $id") 0 0 0
230 0 ▶ if (!defined $attrs->{agent}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $attrs->{"agent"}) 0 0 0
231 0 ▶ my $shown = (_presence($bus) // {})->{$id} // {};
Condition0%0/4
+(_presence($bus) // {})->{$id} // {}
l!l
00
_presence($bus) // {}
l!l
00
MC/DC0%0/1
+(_presence($bus) // {})->{$id} // {}
_presence($bus)
Truth table0%0/2
A
A= _presence($bus)
Aresult
11
00
232 0 ▶ $set{role} //= $shown->{role} if defined $shown->{role};
Branch: 1 branch
BranchTrueFalseTotal
if defined $shown->{"role"} 0 0 0
Condition0%0/3
$set{"role"} //= $shown->{"role"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$set{"role"} //= $shown->{"role"}
$set{"role"} $shown->{"role"}
Truth table0%0/3
A //= B
A= $set{"role"}B= $shown->{"role"}
ABresult
1X1
011
000
233 0 ▶ $set{project} //= $shown->{project} // App::teai::Config::project()
Condition0%0/9
$set{"project"} //= ($shown->{"project"} // App::teai::Config::project()) // App::teai::Err::fail(2, "session $id has no project: add --project P")
l!l&&r!l&&!r
000
($shown->{"project"} // App::teai::Config::project()) // App::teai::Err::fail(2, "session $id has no project: add --project P")
l!l&&r!l&&!r
000
$shown->{"project"} // App::teai::Config::project()
l!l&&r!l&&!r
000
MC/DC0%0/4
$set{"project"} //= ($shown->{"project"} // App::teai::Config::project()) // App::teai::Err::fail(2, "session $id has no project: add --project P")
$set{"project"} $shown->{"project"} App::teai::Config::project() App::teai::Err::fail(2, "session $id has no project: add --project P")
Truth table0%0/5
A //= B // C // D
A= $set{"project"}B= $shown->{"project"}C= App::teai::Config::project()D= App::teai::Err::fail(2, "session $id has no project: add --project P")
ABCDresult
1XXX1
01XX1
001X1
00011
00000
234 // App::teai::Err::fail(2, "session $id has no project: add --project P");
235 }
236 0 ▶ $bus->await(sub ($done) { $bus->set_session_attrs($id, \%set, $done) }) or App::teai::Err::fail(1, "no session $id");
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$bus->await(sub ($done) { $bus->set_session_attrs($id, \%set, $done); } ) or App::teai::Err::fail(1, "no session $id") 0 0 0
237 0 return 0;
238 }
239  
240 0 ▶sub _session_lead ($args) {
Subroutine _session_lead: not called CC=5 SCAR=34.0
241 0 my $usage = _usage('session lead');
242 0 my $take;
243 0 local @ARGV = @$args;
244 0 ▶ _getopt('take' => \$take) or App::teai::Err::fail(2, $usage);
Branch: 1 branch
BranchTrueFalseTotal
_getopt("take", \$take) or App::teai::Err::fail(2, $usage) 0 0 0
245 0 ▶ App::teai::Err::fail(2, $usage) unless @ARGV == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV == 1 0 0 0
246 0 my $project = _need_project($ARGV[0]);
247 0 my $session = App::teai::Config::session();
248 0 require App::teai::Lead;
249 0 my $bus = _bus();
250 0 ▶ die App::teai::Bus::removed_err($session) if $bus->await(sub ($done) { $bus->removed($session, $done) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $bus->await(sub ($done) { $bus->removed($session, $done); } ) 0 0 0
251 0 ▶ my $r = $bus->await(sub ($done) { App::teai::Lead::take($bus, $project, $session, $take, $done) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
252 0 ▶ _write_stdout("lead $project epoch $r->{epoch}\n") or App::teai::Err::fail(3, "write failed: $!");
Branch: 1 branch
BranchTrueFalseTotal
_write_stdout(qq[lead $project epoch $r->{"epoch"}\n]) or App::teai::Err::fail(3, "write failed: $!") 0 0 0
253 0 return 0;
254 }
255  
256 0 ▶sub _session_list ($args) {
Subroutine _session_list: not called CC=10 SCAR=47.0
257 0 my $usage = _usage('session list');
258 0 my $json;
259 0 local @ARGV = @$args;
260 0 ▶ _getopt('json' => \$json) or App::teai::Err::fail(2, $usage);
Branch: 1 branch
BranchTrueFalseTotal
_getopt("json", \$json) or App::teai::Err::fail(2, $usage) 0 0 0
261 0 ▶ App::teai::Err::fail(2, $usage) if @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV 0 0 0
262 0 my $bus = _bus();
263 0 ▶ my $list = $bus->await(sub ($done) { $bus->sessions($done) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
264 0 my $peers = _presence($bus);
265 my $text = join '', map {
266 0 ▶ my $p = !$peers ? { live => undef, seen => undef } : $peers->{ $_->{session} } // { live => [], seen => undef };
Branch: 1 branch
BranchTrueFalseTotal
!$peers ? : 0 0 0
Condition0%0/2
$$peers{$_->{"session"}} // {"live", [], "seen", undef}
l!l
00
MC/DC0%0/1
$$peers{$_->{"session"}} // {"live", [], "seen", undef}
$$peers{$_->{"session"}}
Truth table0%0/2
A
A= $$peers{$_->{"session"}}
Aresult
11
00
267 $json ? $SESSION_JSON->encode({ %$_, live => $p->{live}, seen => $p->{seen} }) . "\n"
268 : "$_->{session} pending=$_->{pending} held=$_->{held} last=" . ($_->{last_delivery} // 'never')
269 0 ▶ . ' live=' . (!$p->{live} ? '?' : @{ $p->{live} } ? join(',', @{ $p->{live} }) : 'no') . "\n"
Branch: 3 branches
BranchTrueFalseTotal
$json ? : 0 0 0
!$p->{'live'} ? : 0 0 0
@{$p->{'live'};} ? : 0 0 0
Condition0%0/2
$_->{"last_delivery"} // "never"
l!l
00
MC/DC0%0/1
$_->{"last_delivery"} // "never"
$_->{"last_delivery"}
Truth table0%0/2
A
A= $_->{"last_delivery"}
Aresult
11
00
270 } @$list;
271 0 ▶ _write_stdout($text) or App::teai::Err::fail(3, "write failed: $!");
Branch: 1 branch
BranchTrueFalseTotal
_write_stdout($text) or App::teai::Err::fail(3, "write failed: $!") 0 0 0
272 0 return 0;
273 }
274  
275 0 ▶sub _presence ($bus) {
Subroutine _presence: not called CC=1 SCAR=6.9
276 0 require App::teai::Presence;
277 0 ▶ return eval { $bus->await(sub ($done) { App::teai::Presence::list($bus, $done) }) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
278 }
279  
280 0 ▶sub _session_rm ($args) {
Subroutine _session_rm: not called CC=16 SCAR=56.1
281 0 my $usage = _usage('session rm');
282 0 my ($allow_self, $force);
283 0 local @ARGV = @$args;
284 0 ▶ _getopt('self' => \$allow_self, 'force' => \$force) or App::teai::Err::fail(2, $usage);
Branch: 1 branch
BranchTrueFalseTotal
_getopt("self", \$allow_self, "force", \$force) or App::teai::Err::fail(2, $usage) 0 0 0
285 0 ▶ App::teai::Err::fail(2, $usage) unless @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV 0 0 0
286 0 my %seen;
287 0 my @ids = grep { !$seen{$_}++ } @ARGV;
288 0 for my $id (@ids) {
289 0 ▶ App::teai::Err::fail(2, "invalid id '$id'") unless App::teai::Wire::valid_id($id);
Branch: 1 branch
BranchTrueFalseTotal
unless App::teai::Wire::valid_id($id) 0 0 0
290 }
291 0 my $own = $ENV{TEAI_SESSION};
292 App::teai::Err::fail(2, "$own is this session (TEAI_SESSION); add --self to remove it")
293 0 ▶ if !$allow_self && defined $own && $seen{$own};
Branch: 1 branch
BranchTrueFalseTotal
if not $allow_self and defined $own and $seen{$own} 0 0 0
Condition0%0/6
not $allow_self and defined $own and $seen{$own}
!ll&&!rl&&r
000
not $allow_self and defined $own
!ll&&!rl&&r
000
MC/DC0%0/3
not $allow_self and defined $own and $seen{$own}
not $allow_self defined $own $seen{$own}
Truth table0%0/4
not(A) and B and C
A= not $allow_selfB= defined $ownC= $seen{$own}
ABCresult
0XX0
10X0
1100
1111
294 0 ▶ my $by = App::teai::Wire::valid_id($own) ? $own : undef;
Branch: 1 branch
BranchTrueFalseTotal
App::teai::Wire::valid_id($own) ? : 0 0 0
295 0 require App::teai::Session;
296 0 my $bus = _bus();
297 0 ▶ my $peers = $force ? undef : $bus->await(sub ($done) { App::teai::Session::peers($bus, \@ids, $done) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$force ? : 0 0 0
298 0 my $rc = 0;
299 0 for my $id (@ids) {
300 0 ▶ my $r = $bus->await(sub ($done) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
301 0 App::teai::Session::remove($bus, $id, by => $by, force => $force, peers => $peers, $done);
302 0 });
303 0 ▶ if ($r->{missing}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"missing"}) 0 0 0
304 0 print STDERR "teai: no session $id\n";
305 0 $rc = 1;
306 0 next;
307 }
308 0 ▶ if ($r->{busy}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"busy"}) 0 0 0
309 0 print STDERR "teai: session $id is in use (@{ $r->{busy} }); add --force to remove it\n";
310 0 $rc = 1;
311 0 next;
312 }
313 0 ▶ _write_stdout("removed $id\n") or App::teai::Err::fail(3, "write failed: $!");
Branch: 1 branch
BranchTrueFalseTotal
_write_stdout("removed $id\n") or App::teai::Err::fail(3, "write failed: $!") 0 0 0
314 0 ▶ print STDERR "teai: left the runtime files of $id: $r->{left}\n" if defined $r->{left};
Branch: 1 branch
BranchTrueFalseTotal
if defined $r->{"left"} 0 0 0
315 0 ▶ print STDERR "teai: left the leases of $id: $r->{leases}\n" if defined $r->{leases};
Branch: 1 branch
BranchTrueFalseTotal
if defined $r->{"leases"} 0 0 0
316 }
317 0 return $rc;
318 }
319  
320 0 ▶sub _compose ($session, $body, %fields) {
Subroutine _compose: not called CC=4 SCAR=30.0
321 0 my $project = App::teai::Config::project();
322 0 my $role = App::teai::Config::role();
323 0 ▶ my ($text, $bad) = !defined $body || $body eq '-' ? _brief_text('-') : App::teai::Wire::text_in($body);
Branch: 1 branch
BranchTrueFalseTotal
!defined($body) || $body eq '-' ? : 0 0 0
Condition0%0/3
not defined $body or $body eq "-"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $body or $body eq "-"
not defined $body $body eq "-"
Truth table0%0/3
not(A) or B
A= not defined $bodyB= $body eq "-"
ABresult
1X1
011
000
324 0 ▶ print STDERR "teai: input was not valid UTF-8; invalid bytes replaced\n" if $bad;
Branch: 1 branch
BranchTrueFalseTotal
if $bad 0 0 0
325 0 my $payload = {
326 %fields, body => $text, project => $project,
327 agent => App::teai::Config::agent_of($session), role => $role,
328 };
329 0 App::teai::Wire::encode_payload($payload);
330 0 return $payload;
331 }
332  
333 0 ▶sub cmd_send ($args) {
Subroutine cmd_send: not called CC=15 SCAR=54.8
334 0 my $usage = _usage('send');
335 0 my $thread;
336 0 local @ARGV = @$args;
337 0 ▶ _getopt('thread=s' => \$thread) or App::teai::Err::fail(2, $usage);
Branch: 1 branch
BranchTrueFalseTotal
_getopt("thread=s", \$thread) or App::teai::Err::fail(2, $usage) 0 0 0
338 0 my ($target, $body, @extra) = @ARGV;
339 0 ▶ App::teai::Err::fail(2, $usage) if !defined $target || @extra;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $target or @extra 0 0 0
Condition0%0/3
not defined $target or @extra
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $target or @extra
not defined $target @extra
Truth table0%0/3
not(A) or B
A= not defined $targetB= @extra
ABresult
1X1
011
000
340 0 require App::teai::Address;
341 0 my $addr = App::teai::Address::parse($target);
342 0 ▶ App::teai::Err::fail(2, "invalid thread '$thread'")
Branch: 1 branch
BranchTrueFalseTotal
if defined $thread and not App::teai::Wire::valid_id($thread) 0 0 0
Condition0%0/3
defined $thread and not App::teai::Wire::valid_id($thread)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $thread and not App::teai::Wire::valid_id($thread)
defined $thread not App::teai::Wire::valid_id($thread)
Truth table0%0/3
A and not(B)
A= defined $threadB= not App::teai::Wire::valid_id($thread)
ABresult
0X0
100
111
343 if defined $thread && !App::teai::Wire::valid_id($thread);
344 0 my $session = App::teai::Config::session();
345 0 ▶ my $fan = $addr->{type} eq 'live' || $addr->{type} eq 'role';
Condition0%0/3
$addr->{'type'} eq 'live' || $addr->{'type'} eq 'role'
l!l&&r!l&&!r
000
MC/DC0%0/2
$addr->{'type'} eq 'live' || $addr->{'type'} eq 'role'
$addr->{'type'} eq 'live' $addr->{'type'} eq 'role'
Truth table0%0/3
A || B
A= $addr->{'type'} eq 'live'B= $addr->{'type'} eq 'role'
ABresult
1X1
011
000
346 0 ▶ my $payload = _compose($session, $body, kind => 'msg', thread => $thread,
Branch: 1 branch
BranchTrueFalseTotal
$fan ? : 0 0 0
347 $fan ? (via => App::teai::Wire::address_text($addr)) : ());
348 0 my $bus = _bus();
349 0 ▶ if ($addr->{type} eq 'project') {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr->{"type"} eq "project") 0 0 0
350 0 my $seq = _to_project($bus, $addr, $payload, $session);
351 0 ▶ _write_stdout("$seq\n") or App::teai::Err::fail(3, "write failed: $!");
Branch: 1 branch
BranchTrueFalseTotal
_write_stdout("$seq\n") or App::teai::Err::fail(3, "write failed: $!") 0 0 0
352 0 return 0;
353 }
354 0 ▶ if ($fan) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fan) 0 0 0
355 0 ▶ my $copies = $bus->await(sub ($done) { App::teai::Address::deliver($bus, $addr, $payload, $session, $done) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
356 0 my @failed = grep { !defined $_->{seq} } @$copies;
357 0 print STDERR "teai: not sent to $_->{to}: $_->{error}\n" for @failed;
358 0 ▶ _write_stdout(join '', map { "$_->{seq} $_->{to}\n" } grep { defined $_->{seq} } @$copies)
Branch: 1 branch
BranchTrueFalseTotal
_write_stdout(join("", map({qq[$_->{"seq"} $_->{"to"}\n];} grep({defined $_->{"seq"};} @$copies)))) or App::teai::Err::fail(3, "write failed: $!") 0 0 0
359 or App::teai::Err::fail(3, "write failed: $!");
360 0 ▶ return @failed ? 3 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@failed ? : 0 0 0
361 }
362 0 my $to = $addr->{id};
363 0 _warn_no_inbox($bus, $to);
364 0 ▶ my $seq = $bus->await(sub ($done) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
365 0 $bus->publish(App::teai::Wire::session_subject($to, $session), $payload, $done);
366 0 });
367 0 ▶ _write_stdout("$seq\n") or App::teai::Err::fail(3, "write failed: $!");
Branch: 1 branch
BranchTrueFalseTotal
_write_stdout("$seq\n") or App::teai::Err::fail(3, "write failed: $!") 0 0 0
368 0 return 0;
369 }
370  
371 0 ▶sub _to_project ($bus, $addr, $payload, $session) {
Subroutine _to_project: not called CC=2 SCAR=17.9
372 0 ▶ my ($r) = @{ $bus->await(sub ($done) { App::teai::Address::deliver($bus, $addr, $payload, $session, $done) }) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
373 0 my $note = App::teai::Address::note($r);
374 0 ▶ print STDERR "teai: $note\n" if defined $note;
Branch: 1 branch
BranchTrueFalseTotal
if defined $note 0 0 0
375 0 return $r->{seq};
376 }
377  
378 0 ▶sub _warn_no_inbox ($bus, $to) {
Subroutine _warn_no_inbox: not called CC=2 SCAR=17.9
379 0 require App::teai::Address;
380 0 ▶ my $note = eval { $bus->probe(sub ($done) { App::teai::Address::inbox_note($bus, $to, $done) }) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
381 0 ▶ print STDERR "teai: $note\n" if defined $note;
Branch: 1 branch
BranchTrueFalseTotal
if defined $note 0 0 0
382 }
383  
384 0 ▶sub _left ($deadline, $floor) {
Subroutine _left: not called CC=2 SCAR=17.9
385 0 my $left = $deadline - App::teai::Bus::now();
386 0 ▶ return $left > $floor ? $left : $floor;
Branch: 1 branch
BranchTrueFalseTotal
$left > $floor ? : 0 0 0
387 }
388  
389 0 ▶sub _write_stdout ($bytes) {
Subroutine _write_stdout: not called CC=5 SCAR=34.0
390 0 ▶ while (length $bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bytes) 0 0 0
391 0 my $n = syswrite STDOUT, $bytes;
392 0 ▶ if (!defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 0 0 0
393 0 ▶ next if $!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EINTR"} 0 0 0
394 0 ▶ return 0 unless $!{EAGAIN};
Branch: 1 branch
BranchTrueFalseTotal
unless $!{"EAGAIN"} 0 0 0
395 0 IO::Select->new(\*STDOUT)->can_write;
396 0 next;
397 }
398 0 substr $bytes, 0, $n, '';
399 }
400 0 return 1;
401 }
402  
403 0 ▶sub _emitter ($json) {
Subroutine _emitter: not called CC=1 SCAR=6.9
404 0 ▶ return sub ($r) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
405 0 ▶ my $text = $json ? App::teai::Wire::format_json($r) . "\n" : App::teai::Wire::format_human($r);
Branch: 1 branch
BranchTrueFalseTotal
$json ? : 0 0 0
406 0 _write_stdout(Encode::encode('UTF-8', $text));
407 0 };
408 }
409  
410 1 ▶sub cmd_inbox ($args) {
Subroutine cmd_inbox: called CC=38 SCAR=72.1
Pod undocumented
411 1 my $usage = _usage('inbox');
412 1 my ($wait, $thread, $count, $re, $json, $nowait_lock, $project) = (0);
413 1 local @ARGV = @$args;
414 1 ▶ _getopt(
Branch: 1 branch
BranchTrueFalseTotal
_getopt("wait=f", \$wait, "thread=s", \$thread, "count=i", \$count, "re=s", \$re, "json", \$json, "no-wait-lock", \$nowait_lock, "project:s", \$project) or App::teai::Err::fail(2, $usage) 0 1 1
415 'wait=f' => \$wait, 'thread=s' => \$thread, 'count=i' => \$count, 're=s' => \$re,
416 'json' => \$json, 'no-wait-lock' => \$nowait_lock, 'project:s' => \$project,
417 ) or App::teai::Err::fail(2, $usage);
418 0 ▶ App::teai::Err::fail(2, $usage) if @ARGV
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV or defined $project and defined $thread || defined $count || defined $re or defined $re and defined $count or defined $thread and not defined $count and not defined $re or defined $count and not defined $thread 0 0 0
Condition0%0/33
@ARGV or defined $project and defined $thread || defined $count || defined $re or defined $re and defined $count or defined $thread and not defined $count and not defined $re or defined $count and not defined $thread
l!l&&r!l&&!r
000
@ARGV or defined $project and defined $thread || defined $count || defined $re or defined $re and defined $count or defined $thread and not defined $count and not defined $re
l!l&&r!l&&!r
000
@ARGV or defined $project and defined $thread || defined $count || defined $re or defined $re and defined $count
l!l&&r!l&&!r
000
@ARGV or defined $project and defined $thread || defined $count || defined $re
l!l&&r!l&&!r
000
defined $project and defined $thread || defined $count || defined $re
!ll&&!rl&&r
000
defined $thread or defined $count or defined $re
l!l&&r!l&&!r
000
defined $thread or defined $count
l!l&&r!l&&!r
000
defined $re and defined $count
!ll&&!rl&&r
000
defined $thread and not defined $count and not defined $re
!ll&&!rl&&r
000
defined $thread and not defined $count
!ll&&!rl&&r
000
defined $count and not defined $thread
!ll&&!rl&&r
000
MC/DC0%0/12
@ARGV or defined $project and defined $thread || defined $count || defined $re or defined $re and defined $count or defined $thread and not defined $count and not defined $re or defined $count and not defined $thread
@ARGV defined $project defined $thread defined $count defined $re defined $re defined $count defined $thread not defined $count not defined $re defined $count not defined $thread
Truth table0%0/46
A or B and C or D or E or F and G or H and not(I) and not(J) or K and not(L)
A= @ARGVB= defined $projectC= defined $threadD= defined $countE= defined $reF= defined $reG= defined $countH= defined $threadI= not defined $countJ= not defined $reK= defined $countL= not defined $thread
ABCDEFGHIJKLresult
1XXXXXXXXXXX1
011XXXXXXXXX1
0101XXXXXXXX1
01001XXXXXXX1
00XXX11XXXXX1
0100011XXXXX1
00XXX0X111XX1
00XXX10111XX1
010000X111XX1
0100010111XX1
00XXX0X0XX111
00XXX0X10X111
00XXX0X110111
00XXX100XX111
00XXX1010X111
00XXX10110111
010000X0XX111
010000X10X111
010000X110111
01000100XX111
010001010X111
0100010110111
00XXX0X0XX0X0
00XXX0X0XX100
00XXX0X10X0X0
00XXX0X10X100
00XXX0X1100X0
00XXX0X110100
00XXX100XX0X0
00XXX100XX100
00XXX1010X0X0
00XXX1010X100
00XXX101100X0
00XXX10110100
010000X0XX0X0
010000X0XX100
010000X10X0X0
010000X10X100
010000X1100X0
010000X110100
01000100XX0X0
01000100XX100
010001010X0X0
010001010X100
01000101100X0
0100010110100
419 || (defined $project && (defined $thread || defined $count || defined $re))
420 || (defined $re && defined $count)
421 || (defined $thread && !defined $count && !defined $re)
422 || (defined $count && !defined $thread);
423 0 ▶ App::teai::Err::fail(2, "invalid thread '$thread'")
Branch: 1 branch
BranchTrueFalseTotal
if defined $thread and not App::teai::Wire::valid_id($thread) 0 0 0
Condition0%0/3
defined $thread and not App::teai::Wire::valid_id($thread)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $thread and not App::teai::Wire::valid_id($thread)
defined $thread not App::teai::Wire::valid_id($thread)
Truth table0%0/3
A and not(B)
A= defined $threadB= not App::teai::Wire::valid_id($thread)
ABresult
0X0
100
111
424 if defined $thread && !App::teai::Wire::valid_id($thread);
425 0 ▶ App::teai::Err::fail(2, "invalid count '$count'") if defined $count && $count < 1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $count and $count < 1 0 0 0
Condition0%0/3
defined $count and $count < 1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $count and $count < 1
defined $count $count < 1
Truth table0%0/3
A and B
A= defined $countB= $count < 1
ABresult
0X0
100
111
426 0 ▶ App::teai::Err::fail(2, "invalid wait '$wait'") if $wait < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $wait < 0 0 0 0
427 0 my %brief_to;
428 0 ▶ if (defined $re) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $re) 0 0 0
429 0 my @seqs = split /,/, $re, -1;
430 0 ▶ App::teai::Err::fail(2, "invalid seq ''") unless @seqs;
Branch: 1 branch
BranchTrueFalseTotal
unless @seqs 0 0 0
431 0 for my $s (@seqs) {
432 0 ▶ App::teai::Err::fail(2, "invalid seq '$s'") unless $s =~ /\A[1-9][0-9]{0,15}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $s =~ /\A[1-9][0-9]{0,15}\z/u 0 0 0
433 }
434 0 $brief_to{$_ + 0} = undef for @seqs;
435 }
436 0 my $session = App::teai::Config::session();
437 0 ▶ $project = _need_project(length $project ? $project : undef) if defined $project;
Branch: 2 branches
BranchTrueFalseTotal
if defined $project 0 0 0
length $project ? : 0 0 0
438 0 my $emit = _emitter($json);
439 0 my $deadline = App::teai::Bus::now() + $wait;
440 0 my $bus = _bus($wait > 0, until => $deadline);
441 0 my $box;
442 0 ▶ if (defined $project) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $project) 0 0 0
443 0 require App::teai::Lead;
444 0 ▶ $box = $bus->await(sub ($done) { App::teai::Lead::reader($bus, $project, $session, $done) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
445 }
446 0 my ($want, %pending);
447 0 ▶ if (defined $re) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $re) { } 0 0 0
448 0 require App::teai::Address;
449 0 for my $seq (sort { $a <=> $b } keys %brief_to) {
450 0 ▶ my $m = $bus->await(sub ($done) { $bus->lookup($seq, $deadline, $done) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
451 0 ▶ App::teai::Err::fail(2, "#$seq is not a brief") unless $m->{kind} eq 'ask' || $m->{kind} eq 'msg';
Branch: 1 branch
BranchTrueFalseTotal
unless $m->{"kind"} eq "ask" or $m->{"kind"} eq "msg" 0 0 0
Condition0%0/3
$m->{"kind"} eq "ask" or $m->{"kind"} eq "msg"
l!l&&r!l&&!r
000
MC/DC0%0/2
$m->{"kind"} eq "ask" or $m->{"kind"} eq "msg"
$m->{"kind"} eq "ask" $m->{"kind"} eq "msg"
Truth table0%0/3
A or B
A= $m->{"kind"} eq "ask"B= $m->{"kind"} eq "msg"
ABresult
1X1
011
000
452 0 ▶ App::teai::Err::fail(2, "#$seq was not sent by $session") unless $m->{from} eq $session;
Branch: 1 branch
BranchTrueFalseTotal
unless $m->{"from"} eq $session 0 0 0
453 App::teai::Err::fail(2, "#$seq is not on thread $thread")
454 0 ▶ if defined $thread && ($m->{thread} // '') ne $thread;
Branch: 1 branch
BranchTrueFalseTotal
if defined $thread and ($m->{"thread"} // "") ne $thread 0 0 0
Condition0%0/5
defined $thread and ($m->{"thread"} // "") ne $thread
!ll&&!rl&&r
000
$m->{"thread"} // ""
l!l
00
MC/DC0%0/2
defined $thread and ($m->{"thread"} // "") ne $thread
defined $thread $m->{"thread"}
Truth table0%0/3
A and B
A= defined $threadB= $m->{"thread"}
ABresult
0X0
100
111
455 0 $brief_to{$seq} = $m->{to};
456 }
457 0 %pending = %brief_to;
458 0 ▶ $want = sub ($r) {
Subroutine __ANON__: not called CC=9 SCAR=45.0
459 0 my $re_seq = $r->{re};
460 0 ▶ if (($r->{kind} // '') eq 'reply' && defined $re_seq && exists $pending{$re_seq}
Branch: 1 branch
BranchTrueFalseTotal
if (($r->{"kind"} // "") eq "reply" and defined $re_seq and exists $pending{$re_seq} and App::teai::Address::answers($pending{$re_seq}, $r->{"from"}) and !defined($thread) || ($r->{"thread"} // "") eq $thread) 0 0 0
Condition0%0/19
($r->{"kind"} // "") eq "reply" and defined $re_seq and exists $pending{$re_seq} and App::teai::Address::answers($pending{$re_seq}, $r->{"from"}) and !defined($thread) || ($r->{"thread"} // "") eq $thread
!ll&&!rl&&r
000
($r->{"kind"} // "") eq "reply" and defined $re_seq and exists $pending{$re_seq} and App::teai::Address::answers($pending{$re_seq}, $r->{"from"})
!ll&&!rl&&r
000
($r->{"kind"} // "") eq "reply" and defined $re_seq and exists $pending{$re_seq}
!ll&&!rl&&r
000
($r->{"kind"} // "") eq "reply" and defined $re_seq
!ll&&!rl&&r
000
$r->{"kind"} // ""
l!l
00
not defined $thread or ($r->{"thread"} // "") eq $thread
l!l&&r!l&&!r
000
$r->{"thread"} // ""
l!l
00
MC/DC0%0/6
($r->{"kind"} // "") eq "reply" and defined $re_seq and exists $pending{$re_seq} and App::teai::Address::answers($pending{$re_seq}, $r->{"from"}) and !defined($thread) || ($r->{"thread"} // "") eq $thread
$r->{"kind"} defined $re_seq exists $pending{$re_seq} App::teai::Address::answers($pending{$re_seq}, $r->{"from"}) not defined $thread $r->{"thread"}
Truth table0%0/7
A and B and C and D and not(E) or F
A= $r->{"kind"}B= defined $re_seqC= exists $pending{$re_seq}D= App::teai::Address::answers($pending{$re_seq}, $r->{"from"})E= not defined $threadF= $r->{"thread"}
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
1111000
11111X1
1111011
461 && App::teai::Address::answers($pending{$re_seq}, $r->{from})
462 && (!defined $thread || ($r->{thread} // '') eq $thread)) {
463 0 delete $pending{$re_seq};
464 }
465 0 !%pending;
466 0 };
467 }
468 else {
469 0 my $met = 0;
470 $want = defined $count
471 0 ▶ ? sub ($r) { $met++ if ($r->{thread} // '') eq $thread; $met >= $count }
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"thread"} // "") eq $thread 0 0 0
Condition0%0/2
$r->{"thread"} // ""
l!l
00
MC/DC0%0/1
$r->{"thread"} // ""
$r->{"thread"}
Truth table0%0/2
A
A= $r->{"thread"}
Aresult
11
00
472 0 ▶ : sub ($r) { 1 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $count ? : 0 0 0
473 }
474 0 ▶ my $n = $bus->await(sub ($done) {
Subroutine __ANON__: not called CC=4 SCAR=30.0
475 0 ▶ $bus->receive(
Branch: 2 branches
BranchTrueFalseTotal
$box ? : 0 0 0
$wait > 0 ? : 0 0 0
Condition0%0/3
defined $count || defined $re
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $count || defined $re
defined $count defined $re
Truth table0%0/3
A || B
A= defined $countB= defined $re
ABresult
1X1
011
000
476 ($box ? (box => $box) : (session => $session)), wait => $wait > 0 ? _left($deadline, $MIN_WAIT) : 0,
477 want => $want, emit => $emit,
478 drain => !defined $count && !defined $re, nowait_lock => $nowait_lock, $done,
479 );
480 0 });
481 0 ▶ return 0 if $n;
Branch: 1 branch
BranchTrueFalseTotal
if $n 0 0 0
482 0 ▶ if (defined $re) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $re) 0 0 0
483 0 my @missing = sort { $a <=> $b } keys %pending;
484 0 print STDERR "teai: no reply yet to ", join(', ', map { "#$_" } @missing), "\n";
485 }
486 0 return 1;
487 }
488  
489 0 ▶sub cmd_pending ($args) {
Subroutine cmd_pending: not called CC=6 SCAR=37.4
Pod undocumented
490 0 ▶ App::teai::Err::fail(2, _usage('pending')) if @$args;
Branch: 1 branch
BranchTrueFalseTotal
if @$args 0 0 0
491 0 my $session = App::teai::Config::session();
492 0 my $bus = _bus();
493 0 ▶ my $info = $bus->await(sub ($done) { $bus->consumer_info($session, $done) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
494 0 ▶ my $n = $info ? ($info->{num_pending} // 0) + ($info->{num_ack_pending} // 0) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$info ? : 0 0 0
Condition0%0/4
$info->{"num_pending"} // 0
l!l
00
$info->{"num_ack_pending"} // 0
l!l
00
MC/DC0%0/1
$info->{"num_pending"} // 0
$info->{"num_pending"}
MC/DC0%0/1
$info->{"num_ack_pending"} // 0
$info->{"num_ack_pending"}
Truth table0%0/2
A
A= $info->{"num_pending"}
Aresult
11
00
Truth table0%0/2
A
A= $info->{"num_ack_pending"}
Aresult
11
00
495 0 ▶ _write_stdout("$n\n") or App::teai::Err::fail(3, "write failed: $!");
Branch: 1 branch
BranchTrueFalseTotal
_write_stdout("$n\n") or App::teai::Err::fail(3, "write failed: $!") 0 0 0
496 0 return 0;
497 }
498  
499 0 ▶sub cmd_ask ($args) {
Subroutine cmd_ask: not called CC=25 SCAR=64.8
Pod undocumented
500 0 my $usage = _usage('ask');
501 0 my ($thread, $async, $json);
502 0 my $timeout = 120;
503 0 local @ARGV = @$args;
504 0 ▶ _getopt(
Branch: 1 branch
BranchTrueFalseTotal
_getopt("thread=s", \$thread, "timeout=f", \$timeout, "async", \$async, "json", \$json) or App::teai::Err::fail(2, $usage) 0 0 0
505 'thread=s' => \$thread, 'timeout=f' => \$timeout, 'async' => \$async, 'json' => \$json,
506 ) or App::teai::Err::fail(2, $usage);
507 0 my ($target, $body, @extra) = @ARGV;
508 0 ▶ App::teai::Err::fail(2, $usage) if !defined $target || @extra;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $target or @extra 0 0 0
Condition0%0/3
not defined $target or @extra
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $target or @extra
not defined $target @extra
Truth table0%0/3
not(A) or B
A= not defined $targetB= @extra
ABresult
1X1
011
000
509 0 require App::teai::Address;
510 0 my $addr = App::teai::Address::parse($target);
511 0 ▶ App::teai::Err::fail(2, "invalid thread '$thread'")
Branch: 1 branch
BranchTrueFalseTotal
if defined $thread and not App::teai::Wire::valid_id($thread) 0 0 0
Condition0%0/3
defined $thread and not App::teai::Wire::valid_id($thread)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $thread and not App::teai::Wire::valid_id($thread)
defined $thread not App::teai::Wire::valid_id($thread)
Truth table0%0/3
A and not(B)
A= defined $threadB= not App::teai::Wire::valid_id($thread)
ABresult
0X0
100
111
512 if defined $thread && !App::teai::Wire::valid_id($thread);
513 0 ▶ App::teai::Err::fail(2, "invalid timeout '$timeout'") if $timeout <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout <= 0 0 0 0
514 0 my $session = App::teai::Config::session();
515 0 ▶ my $fan = $addr->{type} eq 'live' || $addr->{type} eq 'role';
Condition0%0/3
$addr->{'type'} eq 'live' || $addr->{'type'} eq 'role'
l!l&&r!l&&!r
000
MC/DC0%0/2
$addr->{'type'} eq 'live' || $addr->{'type'} eq 'role'
$addr->{'type'} eq 'live' $addr->{'type'} eq 'role'
Truth table0%0/3
A || B
A= $addr->{'type'} eq 'live'B= $addr->{'type'} eq 'role'
ABresult
1X1
011
000
516 0 ▶ my $payload = _compose($session, $body, kind => 'ask', thread => $thread,
Branch: 1 branch
BranchTrueFalseTotal
$fan ? : 0 0 0
517 $fan ? (via => App::teai::Wire::address_text($addr)) : ());
518 0 my $deadline = App::teai::Bus::now() + $timeout;
519 0 my $bus = _bus(!$async, until => $deadline);
520 0 ▶ my $late = sub { !$async && App::teai::Bus::now() >= $deadline };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
not $async and App::teai::Bus::now() >= $deadline 0 0 0
521 0 ▶ _warn_no_inbox($bus, $addr->{id}) if $addr->{type} eq 'session' && !$late->();
Branch: 1 branch
BranchTrueFalseTotal
if $addr->{"type"} eq "session" and not &$late() 0 0 0
Condition0%0/3
$addr->{"type"} eq "session" and not &$late()
!ll&&!rl&&r
000
MC/DC0%0/2
$addr->{"type"} eq "session" and not &$late()
$addr->{"type"} eq "session" not &$late()
Truth table0%0/3
A and not(B)
A= $addr->{"type"} eq "session"B= not &$late()
ABresult
0X0
100
111
522 0 ▶ if (!$async && !$late->()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($async or &$late()) 0 0 0
Condition0%0/3
$async or &$late()
l!l&&r!l&&!r
000
MC/DC0%0/2
$async or &$late()
$async &$late()
Truth table0%0/3
A or B
A= $asyncB= &$late()
ABresult
1X1
011
000
523 0 ▶ $bus->await(sub ($done) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
524 0 ▶ $bus->ensure_consumer($session, sub ($ok, $e = undef) {
Subroutine __ANON__: not called CC=4 SCAR=30.0
525 0 ▶ defined $e && !App::teai::Bus::lost($e) ? $done->(undef, $e) : $done->(1);
Branch: 1 branch
BranchTrueFalseTotal
defined $e && !App::teai::Bus::lost($e) ? : 0 0 0
Condition0%0/3
defined $e and not App::teai::Bus::lost($e)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $e and not App::teai::Bus::lost($e)
defined $e not App::teai::Bus::lost($e)
Truth table0%0/3
A and not(B)
A= defined $eB= not App::teai::Bus::lost($e)
ABresult
0X0
100
111
526 0 });
527 0 });
528 }
529 0 ▶ App::teai::Err::fail(3, 'timed out before sending') if $late->();
Branch: 1 branch
BranchTrueFalseTotal
if &$late() 0 0 0
530 0 ▶ _pause_send($bus, $ENV{TEAI_TEST_PAUSE_SEND}) if defined $ENV{TEAI_TEST_PAUSE_SEND};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"TEAI_TEST_PAUSE_SEND"} 0 0 0
531 0 my (@sent, $failed);
532 0 ▶ if ($fan) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fan) { } 0 0 0
elsif ($addr->{'type'} eq 'project') { } 0 0 0
533 0 ▶ my $copies = $bus->await(sub ($done) { App::teai::Address::deliver($bus, $addr, $payload, $session, $done) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
534 0 for my $c (@$copies) {
535 0 ▶ if (!defined $c->{seq}) { $failed = 1; print STDERR "teai: not sent to $c->{to}: $c->{error}\n" }
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $c->{'seq'}) { } 0 0 0
536 0 ▶ else { push @sent, $c; print STDERR "teai: ask #$c->{seq} sent to $c->{to}\n" unless $async }
Branch: 1 branch
BranchTrueFalseTotal
unless $async 0 0 0
537 }
538 0 ▶ return 3 unless @sent;
Branch: 1 branch
BranchTrueFalseTotal
unless @sent 0 0 0
539 }
540 elsif ($addr->{type} eq 'project') {
541 0 @sent = ({ to => App::teai::Wire::address_text($addr), seq => _to_project($bus, $addr, $payload, $session) });
542 }
543 else {
544 0 ▶ my $seq = $bus->await(sub ($done) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
545 0 $bus->publish(App::teai::Wire::session_subject($addr->{id}, $session), $payload, $done);
546 0 });
547 0 @sent = ({ to => $addr->{id}, seq => $seq });
548 }
549 0 my $rc = eval { _ask_collect($bus, \@sent, $session, _left($deadline, $MIN_WAIT), $async, $json, $fan) };
550 0 ▶ if (my $e = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 0 0
551 0 ▶ my ($msg, $code) = ref $e && eval { $e->isa('App::teai::Err') }
Branch: 1 branch
BranchTrueFalseTotal
ref $e && eval { do { $e->isa('App::teai::Err') } } ? : 0 0 0
Condition0%0/3
ref $e and eval { do { $e->isa("App::teai::Err") } }
!ll&&!rl&&r
000
MC/DC0%0/2
ref $e and eval { do { $e->isa("App::teai::Err") } }
ref $e eval { do { $e->isa("App::teai::Err") } }
Truth table0%0/3
A and B
A= ref $eB= eval { do { $e->isa("App::teai::Err") } }
ABresult
0X0
100
111
552 ? (App::teai::Err::msg($e), App::teai::Err::code($e))
553 : ($e =~ s/\s+at \S+ line \d+\.?\s*\z//r =~ s/\s+\z//r, 3);
554 0 my @seqs = map { $_->{seq} } @sent;
555 0 print STDERR 'teai: ask ', join(', ', map { "#$_" } @seqs), " sent; $msg; collect the reply with teai inbox --re ",
556 join(',', @seqs), "\n";
557 0 $rc = $code;
558 }
559 0 ▶ return $failed ? 3 : $rc;
Branch: 1 branch
BranchTrueFalseTotal
$failed ? : 0 0 0
560 }
561  
562 0 ▶sub _ask_collect ($bus, $sent, $session, $timeout, $async, $json, $fan = 0) {
Subroutine _ask_collect: not called CC=6 SCAR=37.4
563 0 ▶ if ($async) {
Branch: 1 branch
BranchTrueFalseTotal
if ($async) 0 0 0
564 0 ▶ _write_stdout(join '', map { $fan ? "$_->{seq} $_->{to}\n" : "$_->{seq}\n" } @$sent)
Branch: 2 branches
BranchTrueFalseTotal
_write_stdout(join("", map({$fan ? qq[$_->{"seq"} $_->{"to"}\n] : qq[$_->{"seq"}\n];} @$sent))) or App::teai::Err::fail(3, "write failed: $!") 0 0 0
$fan ? : 0 0 0
565 or App::teai::Err::fail(3, "write failed: $!");
566 0 return 0;
567 }
568 0 my %to = map { $_->{seq} => $_->{to} } @$sent;
569 0 ▶ my $n = $bus->await(sub ($done) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
570 $bus->receive(
571 session => $session, wait => $timeout, drain => 0, emit => _emitter($json),
572 0 ▶ want => sub ($r) {
Subroutine __ANON__: not called CC=5 SCAR=34.0
573 0 my $re = $r->{re};
574 ($r->{kind} // '') eq 'reply' && defined $re && exists $to{$re}
575 0 ▶ && App::teai::Address::answers($to{$re}, $r->{from});
Branch: 1 branch
BranchTrueFalseTotal
($r->{"kind"} // "") eq "reply" and defined $re and exists $to{$re} and App::teai::Address::answers($to{$re}, $r->{"from"}) 0 0 0
Condition0%0/8
($r->{"kind"} // "") eq "reply" and defined $re and exists $to{$re}
!ll&&!rl&&r
000
($r->{"kind"} // "") eq "reply" and defined $re
!ll&&!rl&&r
000
$r->{"kind"} // ""
l!l
00
MC/DC0%0/3
($r->{"kind"} // "") eq "reply" and defined $re and exists $to{$re}
$r->{"kind"} defined $re exists $to{$re}
Truth table0%0/4
A and B and C
A= $r->{"kind"}B= defined $reC= exists $to{$re}
ABCresult
0XX0
10X0
1100
1111
576 0 }, $done,
577 );
578 0 });
579 0 ▶ return 0 if $n;
Branch: 1 branch
BranchTrueFalseTotal
if $n 0 0 0
580 0 my @seqs = map { $_->{seq} } @$sent;
581 0 print STDERR 'teai: no reply to ', join(', ', map { "#$_" } @seqs), ' yet; collect it later with teai inbox --re ',
582 join(',', @seqs), "\n";
583 0 return 1;
584 }
585  
586 0 ▶sub _pause_send ($bus, $path) {
Subroutine _pause_send: not called CC=2 SCAR=17.9
587 0 ▶ if (open my $fh, '>', "$path.ready") { close $fh }
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, ">", "$path.ready") 0 0 0
588 0 my $poll;
589 0 ▶ $bus->await(sub ($done) { $poll = EV::timer 0, 0.05, sub { $done->(1) if -e "$path.go" } });
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if -e "$path.go" 0 0 0
590 }
591  
592 0 ▶sub cmd_reply ($args) {
Subroutine cmd_reply: not called CC=5 SCAR=34.0
593 0 my $usage = _usage('reply');
594 0 local @ARGV = @$args;
595 0 my ($seq, $body, @extra) = @ARGV;
596 0 ▶ App::teai::Err::fail(2, $usage) if !defined $seq || @extra;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $seq or @extra 0 0 0
Condition0%0/3
not defined $seq or @extra
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $seq or @extra
not defined $seq @extra
Truth table0%0/3
not(A) or B
A= not defined $seqB= @extra
ABresult
1X1
011
000
597 0 ▶ App::teai::Err::fail(2, "invalid seq '$seq'") unless $seq =~ /\A[1-9][0-9]{0,15}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $seq =~ /\A[1-9][0-9]{0,15}\z/u 0 0 0
598 0 my $session = App::teai::Config::session();
599 0 my $payload = _compose($session, $body, kind => 'reply', re => $seq + 0);
600 0 my $bus = _bus();
601 0 require App::teai::Address;
602 0 ▶ my $new = $bus->await(sub ($done) { App::teai::Address::reply($bus, $seq + 0, $payload, $session, $done) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
603 0 ▶ _write_stdout("$new\n") or App::teai::Err::fail(3, "write failed: $!");
Branch: 1 branch
BranchTrueFalseTotal
_write_stdout("$new\n") or App::teai::Err::fail(3, "write failed: $!") 0 0 0
604 0 return 0;
605 }
606  
607 0 ▶sub cmd_log ($args) {
Subroutine cmd_log: not called CC=9 SCAR=45.0
Pod undocumented
608 0 my $usage = _usage('log');
609 0 my ($thread, $json);
610 0 local @ARGV = @$args;
611 0 ▶ _getopt('thread=s' => \$thread, 'json' => \$json)
Branch: 1 branch
BranchTrueFalseTotal
_getopt("thread=s", \$thread, "json", \$json) or App::teai::Err::fail(2, $usage) 0 0 0
612 or App::teai::Err::fail(2, $usage);
613 0 ▶ App::teai::Err::fail(2, $usage) if @ARGV || !defined $thread;
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV or not defined $thread 0 0 0
Condition0%0/3
@ARGV or not defined $thread
l!l&&r!l&&!r
000
MC/DC0%0/2
@ARGV or not defined $thread
@ARGV not defined $thread
Truth table0%0/3
A or not(B)
A= @ARGVB= not defined $thread
ABresult
1X1
011
000
614 0 ▶ App::teai::Err::fail(2, "invalid thread '$thread'") unless App::teai::Wire::valid_id($thread);
Branch: 1 branch
BranchTrueFalseTotal
unless App::teai::Wire::valid_id($thread) 0 0 0
615 0 my $bus = _bus();
616 0 ▶ my $records = $bus->await(sub ($done) { $bus->thread_log($thread, $done) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
617 0 my $emit = _emitter($json);
618 0 ▶ App::teai::Err::fail(3, "write failed: $!")
Branch: 1 branch
BranchTrueFalseTotal
unless $json or _write_stdout("# senders are not authenticated in teai v0\n") 0 0 0
Condition0%0/3
$json or _write_stdout("# senders are not authenticated in teai v0\n")
l!l&&r!l&&!r
000
MC/DC0%0/2
$json or _write_stdout("# senders are not authenticated in teai v0\n")
$json _write_stdout("# senders are not authenticated in teai v0\n")
Truth table0%0/3
A or B
A= $jsonB= _write_stdout("# senders are not authenticated in teai v0\n")
ABresult
1X1
011
000
619 unless $json || _write_stdout("# senders are not authenticated in teai v0\n");
620 0 for my $r (@$records) {
621 0 ▶ $emit->($r) or App::teai::Err::fail(3, "write failed: $!");
Branch: 1 branch
BranchTrueFalseTotal
&$emit($r) or App::teai::Err::fail(3, "write failed: $!") 0 0 0
622 }
623 0 return 0;
624 }
625  
626 0 ▶sub _need_project ($given) {
Subroutine _need_project: not called CC=4 SCAR=30.0
627 0 ▶ my $project = $given // App::teai::Config::project();
Condition0%0/3
$given // App::teai::Config::project()
l!l&&r!l&&!r
000
MC/DC0%0/2
$given // App::teai::Config::project()
$given App::teai::Config::project()
Truth table0%0/3
A // B
A= $givenB= App::teai::Config::project()
ABresult
1X1
011
000
628 0 ▶ App::teai::Err::fail(2, $NO_PROJECT) unless defined $project;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $project 0 0 0
629 0 ▶ App::teai::Err::fail(2, "invalid project id '$project'") unless App::teai::Wire::valid_id($project);
Branch: 1 branch
BranchTrueFalseTotal
unless App::teai::Wire::valid_id($project) 0 0 0
630 0 return $project;
631 }
632  
633 0 ▶sub _brief_text ($file) {
Subroutine _brief_text: not called CC=11 SCAR=48.8
634 0 my $max = App::teai::Wire::MAX_PAYLOAD;
635 0 ▶ my $what = $file eq '-' ? 'stdin' : $file;
Branch: 1 branch
BranchTrueFalseTotal
$file eq '-' ? : 0 0 0
636 0 my $fh;
637 0 ▶ if ($file eq '-') { $fh = \*STDIN }
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq '-') { } 0 0 0
638 0 ▶ else { open $fh, '<', $file or App::teai::Err::fail(3, "cannot read $what: $!") }
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $file or App::teai::Err::fail(3, "cannot read ${what}: $!") 0 0 0
639 0 binmode $fh;
640 0 my $body = '';
641 0 ▶ while (length $body <= $max) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $body <= $max) 0 0 0
642 0 my $n = read $fh, $body, $max + 1 - length $body, length $body;
643 0 ▶ last if defined $n && !$n;
Branch: 1 branch
BranchTrueFalseTotal
if defined $n and not $n 0 0 0
Condition0%0/3
defined $n and not $n
!ll&&!rl&&r
000
MC/DC0%0/2
defined $n and not $n
defined $n not $n
Truth table0%0/3
A and not(B)
A= defined $nB= not $n
ABresult
0X0
100
111
644 0 ▶ App::teai::Err::fail(3, "cannot read $what: $!") unless defined $n || $!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n or $!{"EINTR"} 0 0 0
Condition0%0/3
defined $n or $!{"EINTR"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $n or $!{"EINTR"}
defined $n $!{"EINTR"}
Truth table0%0/3
A or B
A= defined $nB= $!{"EINTR"}
ABresult
1X1
011
000
645 }
646 0 ▶ App::teai::Err::fail(2, "message too large (more than $max bytes, limit $max)") if length $body > $max;
Branch: 1 branch
BranchTrueFalseTotal
if length $body > $max 0 0 0
647 0 my ($text, $bad) = App::teai::Wire::text_in($body);
648 0 ▶ print STDERR "teai: input was not valid UTF-8; invalid bytes replaced\n" if $bad;
Branch: 1 branch
BranchTrueFalseTotal
if $bad 0 0 0
649 0 return $text;
650 }
651  
652 0 ▶sub cmd_round ($args) {
Subroutine cmd_round: not called CC=3 SCAR=24.8
Pod undocumented
653 0 my ($sub, @rest) = @$args;
654 0 my %run = (
655 start => \&_round_start, status => \&_round_status, collect => \&_round_collect,
656 list => \&_round_list, resend => \&_round_resend,
657 );
658 0 ▶ my $h = defined $sub ? $run{$sub} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $sub ? : 0 0 0
659 0 ▶ App::teai::Err::fail(2, _usage('round')) unless $h;
Branch: 1 branch
BranchTrueFalseTotal
unless $h 0 0 0
660 0 require App::teai::Round;
661 0 return $h->(\@rest);
662 }
663  
664 0 ▶sub _round_thread ($thread) {
Subroutine _round_thread: not called CC=2 SCAR=17.9
665 0 ▶ App::teai::Err::fail(2, "invalid thread '$thread'") unless App::teai::Wire::valid_id($thread);
Branch: 1 branch
BranchTrueFalseTotal
unless App::teai::Wire::valid_id($thread) 0 0 0
666 0 return $thread;
667 }
668  
669 0 ▶sub _round_sent ($bus, $start) {
Subroutine _round_sent: not called CC=10 SCAR=47.0
670 0 my $res = eval { $bus->await($start) };
671 0 my $e = $@;
672 0 ▶ my $r = $res // (ref $e && eval { $e->isa('App::teai::Err') } ? $e->{result} : undef);
Branch: 1 branch
BranchTrueFalseTotal
ref $e && eval { do { $e->isa('App::teai::Err') } } ? : 0 0 0
Condition0%0/6
$res // (ref $e && eval { do { $e->isa("App::teai::Err") } } ? $e->{"result"} : undef)
l!l&&r!l&&!r
000
ref $e and eval { do { $e->isa("App::teai::Err") } }
!ll&&!rl&&r
000
MC/DC0%0/3
$res // (ref $e && eval { do { $e->isa("App::teai::Err") } } ? $e->{"result"} : undef)
$res ref $e eval { do { $e->isa("App::teai::Err") } }
Truth table0%0/4
A // B and C
A= $resB= ref $eC= eval { do { $e->isa("App::teai::Err") } }
ABCresult
1XX1
0111
00X0
0100
673 0 ▶ if ($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) 0 0 0
674 0 print STDERR "teai: $_\n" for @{ $r->{warnings} };
675 0 ▶ _write_stdout(join '', map { "$_->{name} $_->{session} $_->{seq}\n" } @{ $r->{sent} })
Branch: 1 branch
BranchTrueFalseTotal
_write_stdout(join("", map({qq[$_->{"name"} $_->{"session"} $_->{"seq"}\n];} @{$$r{"sent"};}))) or App::teai::Err::fail(3, "write failed: $!") 0 0 0
676 or App::teai::Err::fail(3, "write failed: $!");
677 0 print STDERR "teai: brief not sent to $_->{name}: $_->{error}\n" for @{ $r->{failed} };
678 }
679 0 ▶ die $e unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
680 0 ▶ return @{ $res->{failed} } ? 3 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@{$res->{'failed'};} ? : 0 0 0
681 }
682  
683 0 ▶sub _round_start ($args) {
Subroutine _round_start: not called CC=10 SCAR=47.0
684 0 my $usage = _usage('round start');
685 0 my ($thread, $brief, $add, $project);
686 0 local @ARGV = @$args;
687 0 ▶ _getopt('thread=s' => \$thread, 'brief=s' => \$brief, 'add' => \$add, 'project=s' => \$project)
Branch: 1 branch
BranchTrueFalseTotal
_getopt("thread=s", \$thread, "brief=s", \$brief, "add", \$add, "project=s", \$project) or App::teai::Err::fail(2, $usage) 0 0 0
688 or App::teai::Err::fail(2, $usage);
689 0 ▶ App::teai::Err::fail(2, $usage) if !defined $thread || !@ARGV || ($add && defined $brief);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $thread && @ARGV or $add and defined $brief 0 0 0
Condition0%0/9
not defined $thread && @ARGV or $add and defined $brief
l!l&&r!l&&!r
000
defined $thread && @ARGV
!ll&&!rl&&r
000
$add and defined $brief
!ll&&!rl&&r
000
MC/DC0%0/4
not defined $thread && @ARGV or $add and defined $brief
defined $thread @ARGV $add defined $brief
Truth table0%0/5
not(A && B) or C and D
A= defined $threadB= @ARGVC= $addD= defined $brief
ABCDresult
0XXX1
10XX1
11111
110X0
11100
690 0 _round_thread($thread);
691 my @members = map {
692 0 my ($name, $session) = /\A([^=]*)=(.*)\z/s;
693 0 ▶ App::teai::Err::fail(2, "invalid member '$_': give NAME=SESSION")
Branch: 1 branch
BranchTrueFalseTotal
unless App::teai::Wire::valid_id($name) and App::teai::Wire::valid_id($session) 0 0 0
Condition0%0/3
App::teai::Wire::valid_id($name) and App::teai::Wire::valid_id($session)
!ll&&!rl&&r
000
MC/DC0%0/2
App::teai::Wire::valid_id($name) and App::teai::Wire::valid_id($session)
App::teai::Wire::valid_id($name) App::teai::Wire::valid_id($session)
Truth table0%0/3
A and B
A= App::teai::Wire::valid_id($name)B= App::teai::Wire::valid_id($session)
ABresult
0X0
100
111
694 unless App::teai::Wire::valid_id($name) && App::teai::Wire::valid_id($session);
695 0 [$name, $session];
696 } @ARGV;
697 0 my $session = App::teai::Config::session();
698 0 $project = _need_project($project);
699 0 my $role = App::teai::Config::role();
700 0 ▶ my $body = $add ? undef : _brief_text($brief // '-');
Branch: 1 branch
BranchTrueFalseTotal
$add ? : 0 0 0
Condition0%0/2
$brief // "-"
l!l
00
MC/DC0%0/1
$brief // "-"
$brief
Truth table0%0/2
A
A= $brief
Aresult
11
00
701 0 my $bus = _bus();
702 0 ▶ return _round_sent($bus, sub ($done) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
703 0 App::teai::Round::start($bus,
704 project => $project, thread => $thread, lead => $session, body => $body,
705 members => \@members, add => $add, role => $role, $done);
706 0 });
707 }
708  
709 0 ▶sub _round_resend ($args) {
Subroutine _round_resend: not called CC=4 SCAR=30.0
710 0 my $usage = _usage('round resend');
711 0 my $project;
712 0 local @ARGV = @$args;
713 0 ▶ _getopt('project=s' => \$project) or App::teai::Err::fail(2, $usage);
Branch: 1 branch
BranchTrueFalseTotal
_getopt("project=s", \$project) or App::teai::Err::fail(2, $usage) 0 0 0
714 0 my ($thread, @names) = @ARGV;
715 0 ▶ App::teai::Err::fail(2, $usage) unless defined $thread && @names;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $thread and @names 0 0 0
Condition0%0/3
defined $thread and @names
!ll&&!rl&&r
000
MC/DC0%0/2
defined $thread and @names
defined $thread @names
Truth table0%0/3
A and B
A= defined $threadB= @names
ABresult
0X0
100
111
716 0 _round_thread($thread);
717 0 my $session = App::teai::Config::session();
718 0 $project = _need_project($project);
719 0 my $role = App::teai::Config::role();
720 0 my $bus = _bus();
721 0 ▶ return _round_sent($bus, sub ($done) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
722 0 App::teai::Round::resend($bus,
723 project => $project, thread => $thread, lead => $session, names => \@names, role => $role, $done);
724 0 });
725 }
726  
727 0 ▶sub _round_status ($args) {
Subroutine _round_status: not called CC=7 SCAR=40.3
728 0 my $usage = _usage('round status');
729 0 my ($project, $json);
730 0 local @ARGV = @$args;
731 0 ▶ _getopt('project=s' => \$project, 'json' => \$json) or App::teai::Err::fail(2, $usage);
Branch: 1 branch
BranchTrueFalseTotal
_getopt("project=s", \$project, "json", \$json) or App::teai::Err::fail(2, $usage) 0 0 0
732 0 my ($thread, @extra) = @ARGV;
733 0 ▶ App::teai::Err::fail(2, $usage) if !defined $thread || @extra;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $thread or @extra 0 0 0
Condition0%0/3
not defined $thread or @extra
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $thread or @extra
not defined $thread @extra
Truth table0%0/3
not(A) or B
A= not defined $threadB= @extra
ABresult
1X1
011
000
734 0 _round_thread($thread);
735 0 $project = _need_project($project);
736 0 my $bus = _bus();
737 0 ▶ my $st = $bus->await(sub ($done) { App::teai::Round::status($bus, $project, $thread, $done) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
738 my $text = join '', map {
739 $json ? $SESSION_JSON->encode($_) . "\n"
740 0 ▶ : "$_->{name} $_->{session} state=$_->{state} briefs=" . join(',', @{ $_->{briefs} })
Condition0%0/2
$_->{"reply"} // "-"
l!l
00
MC/DC0%0/1
$_->{"reply"} // "-"
$_->{"reply"}
Truth table0%0/2
A
A= $_->{"reply"}
Aresult
11
00
741 0 ▶ . ' reply=' . ($_->{reply} // '-') . "\n"
Branch: 1 branch
BranchTrueFalseTotal
$json ? : 0 0 0
742 0 } @{ $st->{members} };
743 0 ▶ _write_stdout($text) or App::teai::Err::fail(3, "write failed: $!");
Branch: 1 branch
BranchTrueFalseTotal
_write_stdout($text) or App::teai::Err::fail(3, "write failed: $!") 0 0 0
744 0 return 0;
745 }
746  
747 0 ▶sub _round_collect ($args) {
Subroutine _round_collect: not called CC=10 SCAR=47.0
748 0 my $usage = _usage('round collect');
749 0 my ($wait, $ack, $project, $json) = (0);
750 0 local @ARGV = @$args;
751 0 ▶ _getopt('wait=f' => \$wait, 'ack' => \$ack, 'project=s' => \$project, 'json' => \$json)
Branch: 1 branch
BranchTrueFalseTotal
_getopt("wait=f", \$wait, "ack", \$ack, "project=s", \$project, "json", \$json) or App::teai::Err::fail(2, $usage) 0 0 0
752 or App::teai::Err::fail(2, $usage);
753 0 my ($thread, @extra) = @ARGV;
754 0 ▶ App::teai::Err::fail(2, $usage) if !defined $thread || @extra;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $thread or @extra 0 0 0
Condition0%0/3
not defined $thread or @extra
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $thread or @extra
not defined $thread @extra
Truth table0%0/3
not(A) or B
A= not defined $threadB= @extra
ABresult
1X1
011
000
755 0 _round_thread($thread);
756 0 ▶ App::teai::Err::fail(2, "invalid wait '$wait'") if $wait < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $wait < 0 0 0 0
757 0 ▶ my $session = $ack ? App::teai::Config::session() : undef;
Branch: 1 branch
BranchTrueFalseTotal
$ack ? : 0 0 0
758 0 $project = _need_project($project);
759 0 ▶ my $until = $wait > 0 ? App::teai::Bus::now() + $wait : undef;
Branch: 1 branch
BranchTrueFalseTotal
$wait > 0 ? : 0 0 0
760 0 my $bus = _bus($wait > 0, until => $until);
761 0 ▶ my $res = $bus->await(sub ($done) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
762 0 App::teai::Round::collect($bus, $project, $thread, until => $until, ack => $session, $done);
763 0 });
764 0 my $emit = _emitter($json);
765 0 for my $r (@{ $res->{replies} }) {
766 0 ▶ $emit->($r) or App::teai::Err::fail(3, "write failed: $!");
Branch: 1 branch
BranchTrueFalseTotal
&$emit($r) or App::teai::Err::fail(3, "write failed: $!") 0 0 0
767 }
768 0 ▶ return 0 unless @{ $res->{missing} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$res->{"missing"};} 0 0 0
769 0 print STDERR 'teai: no reply yet from ', join(', ', @{ $res->{missing} }), "\n";
770 0 return 1;
771 }
772  
773 0 ▶sub _round_list ($args) {
Subroutine _round_list: not called CC=5 SCAR=34.0
774 0 my $usage = _usage('round list');
775 0 my ($project, $json);
776 0 local @ARGV = @$args;
777 0 ▶ _getopt('project=s' => \$project, 'json' => \$json) or App::teai::Err::fail(2, $usage);
Branch: 1 branch
BranchTrueFalseTotal
_getopt("project=s", \$project, "json", \$json) or App::teai::Err::fail(2, $usage) 0 0 0
778 0 ▶ App::teai::Err::fail(2, $usage) if @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV 0 0 0
779 0 $project = _need_project($project);
780 0 my $bus = _bus();
781 0 ▶ my $list = $bus->await(sub ($done) { App::teai::Round::list($bus, $project, $done) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
782 my $text = join '', map {
783 0 $json ? $SESSION_JSON->encode($_) . "\n"
784 0 ▶ : "$_->{thread} lead=$_->{lead} members=" . keys(%{ $_->{members} }) . " created=$_->{created}\n"
Branch: 1 branch
BranchTrueFalseTotal
$json ? : 0 0 0
785 } @$list;
786 0 ▶ _write_stdout($text) or App::teai::Err::fail(3, "write failed: $!");
Branch: 1 branch
BranchTrueFalseTotal
_write_stdout($text) or App::teai::Err::fail(3, "write failed: $!") 0 0 0
787 0 return 0;
788 }
789  
790 0 ▶sub cmd_exec ($args) {
Subroutine cmd_exec: not called CC=11 SCAR=48.8
791 0 my $usage = _usage('exec');
792 0 my ($session, $wait, $timeout, $seq) = (undef, 600);
793 0 local @ARGV = @$args;
794 0 ▶ _getopt(['require_order'], 'session=s' => \$session, 'wait=f' => \$wait, 'timeout=f' => \$timeout, 'seq=s' => \$seq)
Branch: 1 branch
BranchTrueFalseTotal
_getopt(["require_order"], "session=s", \$session, "wait=f", \$wait, "timeout=f", \$timeout, "seq=s", \$seq) or App::teai::Err::fail(2, $usage) 0 0 0
795 or App::teai::Err::fail(2, $usage);
796 0 ▶ App::teai::Err::fail(2, $usage) unless defined $session && @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $session and @ARGV 0 0 0
Condition0%0/3
defined $session and @ARGV
!ll&&!rl&&r
000
MC/DC0%0/2
defined $session and @ARGV
defined $session @ARGV
Truth table0%0/3
A and B
A= defined $sessionB= @ARGV
ABresult
0X0
100
111
797 0 ▶ App::teai::Err::fail(2, "invalid session id '$session'") unless App::teai::Wire::valid_id($session);
Branch: 1 branch
BranchTrueFalseTotal
unless App::teai::Wire::valid_id($session) 0 0 0
798 0 ▶ App::teai::Err::fail(2, "invalid wait '$wait'") if $wait < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $wait < 0 0 0 0
799 0 ▶ App::teai::Err::fail(2, "invalid timeout '$timeout'") if defined $timeout && $timeout <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $timeout and $timeout <= 0 0 0 0
Condition0%0/3
defined $timeout and $timeout <= 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $timeout and $timeout <= 0
defined $timeout $timeout <= 0
Truth table0%0/3
A and B
A= defined $timeoutB= $timeout <= 0
ABresult
0X0
100
111
800 0 ▶ App::teai::Err::fail(2, "invalid seq '$seq'") if defined $seq && $seq !~ /\A[1-9][0-9]{0,15}\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $seq and not $seq =~ /\A[1-9][0-9]{0,15}\z/u 0 0 0
Condition0%0/3
defined $seq and not $seq =~ /\A[1-9][0-9]{0,15}\z/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $seq and not $seq =~ /\A[1-9][0-9]{0,15}\z/u
defined $seq not $seq =~ /\A[1-9][0-9]{0,15}\z/u
Truth table0%0/3
A and not(B)
A= defined $seqB= not $seq =~ /\A[1-9][0-9]{0,15}\z/u
ABresult
0X0
100
111
801 0 require App::teai::Exec;
802 0 my $deadline = App::teai::Bus::now() + $wait;
803 0 ▶ my $bus = App::teai::Bus->new(%{ App::teai::Config::load() }, reconnect => 1,
Branch: 1 branch
BranchTrueFalseTotal
$wait > 0 ? : 0 0 0
804 ($wait > 0 ? (until => $deadline) : ()));
805 0 return App::teai::Exec::run(
806 bus => $bus, session => $session, wait => _left($deadline, 0), timeout => $timeout, seq => $seq, cmd => [@ARGV],
807 );
808 }
809  
810 17 ▶sub cmd_lock ($args) {
Subroutine cmd_lock: called CC=3 SCAR=11.0
811 17 my $usage = _usage('lock');
812 17 my ($wait, $global);
813 17 local @ARGV = @$args;
814 17 ▶ _getopt(["require_order"], "wait=f" => \$wait, "global" => \$global) or App::teai::Err::fail(2, $usage);
Branch: 1 branch
BranchTrueFalseTotal
_getopt(["require_order"], "wait=f", \$wait, "global", \$global) or App::teai::Err::fail(2, $usage) 16 1 17
815 16 ▶ App::teai::Err::fail(2, $usage) unless @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV 1 15 16
816 15 require App::teai::Lock;
817 15 return App::teai::Lock::run(wait => $wait, global => $global, cmd => [@ARGV]);
818 }
819  
820 0 ▶sub cmd_mcp ($args) {
Subroutine cmd_mcp: not called CC=3 SCAR=24.8
Pod undocumented
821 0 ▶ App::teai::Err::fail(2, _usage('mcp')) if @$args;
Branch: 1 branch
BranchTrueFalseTotal
if @$args 0 0 0
822 0 my $session = $ENV{TEAI_SESSION};
823 0 my %server = (server => { name => 'teai', version => $VERSION });
824 0 require App::teai::MCP;
825 0 ▶ unless (App::teai::Wire::valid_id($session)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (App::teai::Wire::valid_id($session)) 0 0 0
826 0 print STDERR "teai mcp: no session; serving no tools\n";
827 0 ▶ my $refuse = sub ($args, $ctx) { $ctx->{done}->(undef, 'no teai session: start the CLI with teai-agent') };
Subroutine __ANON__: not called CC=1 SCAR=6.9
828 0 return App::teai::MCP->new(%server, tools => {}, fallback => { schema => { type => 'object' }, call => $refuse })->run;
829 }
830 0 my $config = App::teai::Config::load();
831 0 require App::teai::MCP::Tools;
832 0 my $tools = App::teai::MCP::Tools->new(session => $session, config => $config);
833 0 require App::teai::Presence;
834 0 ▶ my $presence = App::teai::Presence->new(kind => 'mcp', session => $session, bus => sub { $tools->presence_bus });
Subroutine __ANON__: not called CC=1 SCAR=6.9
835 0 my $rc = App::teai::MCP->new(%server, tools => $tools->tools)->run;
836 0 $presence->stop;
837 0 return $rc;
838 }
839  
840 0 ▶sub cmd_wait ($args) {
Subroutine cmd_wait: not called CC=6 SCAR=37.4
Pod undocumented
841 0 my $usage = _usage('wait');
842 0 my ($timeout, $follow);
843 0 local @ARGV = @$args;
844 0 ▶ _getopt('timeout=f' => \$timeout, 'follow' => \$follow) or App::teai::Err::fail(2, $usage);
Branch: 1 branch
BranchTrueFalseTotal
_getopt("timeout=f", \$timeout, "follow", \$follow) or App::teai::Err::fail(2, $usage) 0 0 0
845 0 ▶ App::teai::Err::fail(2, $usage) if @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV 0 0 0
846 0 ▶ App::teai::Err::fail(2, "invalid timeout '$timeout'")
Branch: 1 branch
BranchTrueFalseTotal
if defined $timeout and not $timeout > 0 && $timeout <= $MAX_TIMEOUT 0 0 0
Condition0%0/6
defined $timeout and not $timeout > 0 && $timeout <= $MAX_TIMEOUT
!ll&&!rl&&r
000
$timeout > 0 && $timeout <= $MAX_TIMEOUT
!ll&&!rl&&r
000
MC/DC0%0/3
defined $timeout and not $timeout > 0 && $timeout <= $MAX_TIMEOUT
defined $timeout $timeout > 0 $timeout <= $MAX_TIMEOUT
Truth table0%0/4
A and not(B && C)
A= defined $timeoutB= $timeout > 0C= $timeout <= $MAX_TIMEOUT
ABCresult
0XX0
1110
10X1
1101
847 if defined $timeout && !($timeout > 0 && $timeout <= $MAX_TIMEOUT);
848 0 my $session = App::teai::Config::session();
849 0 my $config = App::teai::Config::load();
850 0 App::teai::Config::runtime_dir();
851 0 require App::teai::Hook;
852 0 return App::teai::Hook::watch(session => $session, config => $config, timeout => $timeout, follow => $follow);
853 }
854  
855 3 ▶sub cmd_who ($args) {
Subroutine cmd_who: called CC=28 SCAR=63.3
Pod undocumented
856 3 my $usage = _usage('who');
857 3 my (%want, $json);
858 3 local @ARGV = @$args;
859 3 ▶ _getopt('project=s' => \$want{project}, 'role=s' => \$want{role}, 'agent=s' => \$want{agent}, 'json' => \$json)
Branch: 1 branch
BranchTrueFalseTotal
_getopt("project=s", \$want{"project"}, "role=s", \$want{"role"}, "agent=s", \$want{"agent"}, "json", \$json) or App::teai::Err::fail(2, $usage) 3 0 3
860 or App::teai::Err::fail(2, $usage);
861 3 ▶ App::teai::Err::fail(2, $usage) if @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV 0 3 3
862 App::teai::Err::fail(2, "unknown agent '$want{agent}'")
863 3 ▶ if defined $want{agent} && !App::teai::Config::valid_agent($want{agent});
Branch: 1 branch
BranchTrueFalseTotal
if defined $want{"agent"} and not App::teai::Config::valid_agent($want{"agent"}) 1 2 3
Condition66%2/3
defined $want{"agent"} and not App::teai::Config::valid_agent($want{"agent"})
!ll&&!rl&&r
201
MC/DC50%1/2
defined $want{"agent"} and not App::teai::Config::valid_agent($want{"agent"})
defined $want{"agent"} not App::teai::Config::valid_agent($want{"agent"})
Truth table66%2/3
A and not(B)
A= defined $want{"agent"}B= not App::teai::Config::valid_agent($want{"agent"})
ABresult
0X0
100
111
864 2 _check_attr($_ => $want{$_}) for qw(project role);
865 0 delete @want{ grep { !defined $want{$_} } keys %want };
866 0 my $bus = _bus();
867 0 require App::teai::Presence;
868 0 ▶ my $peers = eval { $bus->await(sub ($done) { App::teai::Presence::list($bus, $done) }) } or do {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
my $peers = eval { do { $bus->await(sub ($done) { App::teai::Presence::list($bus, $done); } ) } } or do { my $e = $@; App::teai::Err::fail(3, "the hub has no teai_peers bucket: run teai init") if App::teai::Bus::no_bucket($e); die $e } 0 0 0
869 0 my $e = $@;
870 0 ▶ App::teai::Err::fail(3, 'the hub has no teai_peers bucket: run teai init') if App::teai::Bus::no_bucket($e);
Branch: 1 branch
BranchTrueFalseTotal
if App::teai::Bus::no_bucket($e) 0 0 0
871 0 die $e;
872 };
873 0 require App::teai::Lead;
874 0 ▶ my $leases = $bus->await(sub ($done) { App::teai::Lead::leases($bus, $done) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
875 0 my %leads;
876 0 push @{ $leads{ $leases->{$_}{session} } }, $_ for sort keys %$leases;
877 0 ▶ my $matches = sub ($p, @keys) { !grep { ($p->{$_} // '') ne $want{$_} } @keys };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$p->{$_} // ""
l!l
00
MC/DC0%0/1
$p->{$_} // ""
$p->{$_}
Truth table0%0/2
A
A= $p->{$_}
Aresult
11
00
878 0 ▶ my %row = map { $peers->{$_}{live} && @{ $peers->{$_}{live} } ? ($_ => $peers->{$_}) : () } keys %$peers;
Branch: 1 branch
BranchTrueFalseTotal
$peers->{$_}{'live'} && @{$peers->{$_}{'live'};} ? : 0 0 0
Condition0%0/3
$peers->{$_}{"live"} and @{$peers->{$_}{"live"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$peers->{$_}{"live"} and @{$peers->{$_}{"live"};}
$peers->{$_}{"live"} @{$peers->{$_}{"live"};}
Truth table0%0/3
A and B
A= $peers->{$_}{"live"}B= @{$peers->{$_}{"live"};}
ABresult
0X0
100
111
879 0 my @ids = grep { $matches->($row{$_}, keys %want) } keys %row;
880 0 ▶ my $lead = length($want{project} // '') ? $leases->{ $want{project} } : undef;
Branch: 1 branch
BranchTrueFalseTotal
length($want{'project'} // '') ? : 0 0 0
Condition0%0/2
$want{"project"} // ""
l!l
00
MC/DC0%0/1
$want{"project"} // ""
$want{"project"}
Truth table0%0/2
A
A= $want{"project"}
Aresult
11
00
881 0 ▶ if ($lead && !grep { $_ eq $lead->{session} } @ids) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lead and not grep({$_ eq $lead->{"session"};} @ids)) 0 0 0
Condition0%0/3
$lead and not grep({$_ eq $lead->{"session"};} @ids)
!ll&&!rl&&r
000
MC/DC0%0/2
$lead and not grep({$_ eq $lead->{"session"};} @ids)
$lead not grep({$_ eq $lead->{"session"};} @ids)
Truth table0%0/3
A and not(B)
A= $leadB= not grep({$_ eq $lead->{"session"};} @ids)
ABresult
0X0
100
111
882 0 my $id = $lead->{session};
883 0 ▶ $row{$id} //= do {
Condition0%0/3
$row{$id} //= do { my $at = $bus->await(sub ($done) { $bus->session_attrs($id, $done); } ) // {}; +{"agent", $at->{"agent"} // App::teai::Config::agent_of($id), "project", $at->{"project"}, "role", $at->{"role"}, "live", [], "hosts", [], "seen", undef} }
l!l&&r!l&&!r
000
MC/DC0%0/2
$row{$id} //= do { my $at = $bus->await(sub ($done) { $bus->session_attrs($id, $done); } ) // {}; +{"agent", $at->{"agent"} // App::teai::Config::agent_of($id), "project", $at->{"project"}, "role", $at->{"role"}, "live", [], "hosts", [], "seen", undef} }
$row{$id} do { my $at = $bus->await(sub ($done) { $bus->session_attrs($id, $done); } ) // {}; +{"agent", $at->{"agent"} // App::teai::Config::agent_of($id), "project", $at->{"project"}, "role", $at->{"role"}, "live", [], "hosts", [], "seen", undef} }
Truth table0%0/3
A //= B
A= $row{$id}B= do { my $at = $bus->await(sub ($done) { $bus->session_attrs($id, $done); } ) // {}; +{"agent", $at->{"agent"} // App::teai::Config::agent_of($id), "project", $at->{"project"}, "role", $at->{"role"}, "live", [], "hosts", [], "seen", undef} }
ABresult
1X1
011
000
884 0 ▶ my $at = $bus->await(sub ($done) { $bus->session_attrs($id, $done) }) // {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/2
$bus->await(sub ($done) { $bus->session_attrs($id, $done); } ) // {}
l!l
00
MC/DC0%0/1
$bus->await(sub ($done) { $bus->session_attrs($id, $done); } ) // {}
$bus->await(sub ($done) { $bus->session_attrs($id, $done); } )
Truth table0%0/2
A
A= $bus->await(sub ($done) { $bus->session_attrs($id, $done); } )
Aresult
11
00
885 +{ agent => $at->{agent} // App::teai::Config::agent_of($id), project => $at->{project}, role => $at->{role},
886 0 ▶ live => [], hosts => [], seen => undef };
Condition0%0/3
$at->{"agent"} // App::teai::Config::agent_of($id)
l!l&&r!l&&!r
000
MC/DC0%0/2
$at->{"agent"} // App::teai::Config::agent_of($id)
$at->{"agent"} App::teai::Config::agent_of($id)
Truth table0%0/3
A // B
A= $at->{"agent"}B= App::teai::Config::agent_of($id)
ABresult
1X1
011
000
887 };
888 0 ▶ push @ids, $id if $matches->($row{$id}, grep { $_ ne 'project' } keys %want);
Branch: 1 branch
BranchTrueFalseTotal
if &$matches($row{$id}, grep({$_ ne "project";} keys %want)) 0 0 0
889 }
890 my $text = join '', map {
891 0 my $p = $row{$_};
892 0 my $led = $leads{$_};
893 $json ? $SESSION_JSON->encode({
894 0 ▶ session => $_, (map { $_ => $p->{$_} } qw(agent project role live hosts seen)), ($led ? (lead => $led) : ()),
Branch: 2 branches
BranchTrueFalseTotal
$led ? : 0 0 0
@{$p->{'live'};} ? : 0 0 0
Condition0%0/4
$p->{"project"} // "-"
l!l
00
$p->{"role"} // "-"
l!l
00
MC/DC0%0/1
$p->{"project"} // "-"
$p->{"project"}
MC/DC0%0/1
$p->{"role"} // "-"
$p->{"role"}
Truth table0%0/2
A
A= $p->{"project"}
Aresult
11
00
Truth table0%0/2
A
A= $p->{"role"}
Aresult
11
00
895 }) . "\n"
896 : "$_ agent=$p->{agent} project=" . ($p->{project} // '-') . ' role=' . ($p->{role} // '-')
897 0 ▶ . ' live=' . (@{ $p->{live} } ? join(',', @{ $p->{live} }) : 'no')
Branch: 1 branch
BranchTrueFalseTotal
@{$p->{'hosts'};} ? : 0 0 0
898 0 ▶ . ' host=' . (@{ $p->{hosts} } ? join(',', @{ $p->{hosts} }) : '-')
Branch: 1 branch
BranchTrueFalseTotal
$led ? : 0 0 0
Condition0%0/2
$p->{"seen"} // "-"
l!l
00
MC/DC0%0/1
$p->{"seen"} // "-"
$p->{"seen"}
Truth table0%0/2
A
A= $p->{"seen"}
Aresult
11
00
899 0 ▶ . ' seen=' . ($p->{seen} // '-') . ($led ? ' lead=' . join(',', @$led) : '') . "\n"
Branch: 1 branch
BranchTrueFalseTotal
$json ? : 0 0 0
900 } sort @ids;
901 0 ▶ _write_stdout($text) or App::teai::Err::fail(3, "write failed: $!");
Branch: 1 branch
BranchTrueFalseTotal
_write_stdout($text) or App::teai::Err::fail(3, "write failed: $!") 0 0 0
902 0 return 0;
903 }
904  
905 0 ▶sub cmd_dash ($args) {
Subroutine cmd_dash: not called CC=3 SCAR=24.8
Pod undocumented
906 0 my $usage = _usage('dash');
907 0 my ($listen, $project);
908 0 local @ARGV = @$args;
909 0 ▶ _getopt('listen=s' => \$listen, 'project=s' => \$project) or App::teai::Err::fail(2, $usage);
Branch: 1 branch
BranchTrueFalseTotal
_getopt("listen=s", \$listen, "project=s", \$project) or App::teai::Err::fail(2, $usage) 0 0 0
910 0 ▶ App::teai::Err::fail(2, $usage) if @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV 0 0 0
911 0 require App::teai::Dash;
912 0 return App::teai::Dash::run(listen => $listen, project => $project, config => App::teai::Config::load());
913 }
914  
915 0 ▶sub cmd_hook ($args) {
Subroutine cmd_hook: not called CC=3 SCAR=24.8
916 0 ▶ return 0 unless eval { require App::teai::Hook; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require App::teai::Hook; 1 } } 0 0 0
917 0 ▶ return App::teai::Hook::run(stdin => \*STDIN, env => { %ENV }, reset => ($args->[0] // '') eq 'reset');
Condition0%0/2
$args->[0] // ""
l!l
00
MC/DC0%0/1
$args->[0] // ""
$args->[0]
Truth table0%0/2
A
A= $args->[0]
Aresult
11
00
918 }
919  
920 24 for my $c (grep { !App::teai->can("cmd_$_") } @COMMANDS) {
921 24 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
922 0 ▶ *{"cmd_$c"} = sub { App::teai::Err::fail(2, 'not implemented') };
Subroutine __ANON__: not called CC=1 SCAR=6.9
923 }
924  
925 24 1;
926 __END__
« lib/App/teai/Dash/Sessions.pm ↑ Summary lib/App/teai/MCP.pm »