blib/lib/App/karr/Cmd/Delete.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/App/karr/Foundation/Runner.pm ↑ Summary blib/lib/App/karr/Cmd/Destroy.pm »
1 # ABSTRACT: Delete a task
2  
3 package App::karr::Cmd::Delete;
4 our $VERSION = '0.602';
5 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use MooX::Cmd;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use MooX::Options (
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 usage_string => 'USAGE: karr delete ID[,ID,...] [--yes] [--claim NAME] [--json]',
9 2 );
10 2 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 0 ▶use App::karr::CrossBoard;
Subroutine BEGIN: called CC=1 SCAR=2.6
12 use App::karr::Role::BoardAccess;
13 use App::karr::Role::Output;
14 use App::karr::Role::TaskMutation;
15 use App::karr::Task;
16  
17 with 'App::karr::Role::BoardAccess', 'App::karr::Role::Output',
18 'App::karr::Role::TaskMutation';
19  
20  
21 option yes => (
22 is => 'ro',
23 short => 'y',
24 doc => 'Skip confirmation',
25 );
26  
27 option claim => (
28 is => 'ro',
29 format => 's',
30 doc => 'Delete a task claimed by this agent',
31 );
32  
33 sub execute {
34 my ($self, $args_ref, $chain_ref) = @_;
35  
36 $self->check_positional_args($args_ref, 1);
37  
38 $self->sync_before;
39 $self->require_board;
40  
41 my @pos = $self->positional_args($args_ref);
42 my $id_str = $pos[0];
43 # See the note in Cmd::Move: length, not truth, or the id "0" is read as no
44 # id at all and answered with a usage error instead of "not found" (#239).
45 die "Usage: karr delete ID[,ID,...] [--yes] [--claim NAME] [--json]\n"
46 unless defined $id_str && length $id_str;
47 # And a comma with no ids around it passes that guard and
48 # splits to nothing, so the command used to exit 0 having done nothing --
49 # which on a delete reads as "deleted", and is the worst possible place for
50 # that ambiguity.
51 my @ids = $self->parse_ids($id_str);
52 die "Usage: karr delete ID[,ID,...] [--yes] [--claim NAME] [--json]\n" unless @ids;
53  
54 # Every id is attempted, whatever the ones before it did. A missing id used to
55 # die from inside this loop, which on delete was the worst version of the bug
56 # in ticket #61: the ids already removed locally never reached sync_after, so
57 # the batch reported failure with the remote still holding cards karr had
58 # deleted.
59 my ($results, $failed) = $self->run_batch(\@ids, sub {
60 my ($id) = @_;
61  
62 my $task = $self->find_task($id);
63 # The unguarded pre-read fires before delete_task_guarded below, so this is
64 # the not-found a caller normally meets; the guard raises the same line only
65 # on the race where the card vanishes in the window. One spelling for both,
66 # and for every other command on the mutation path (ticket k264).
67 die $self->task_not_found($id) unless $task;
68  
69 # A live claim blocks the delete whoever holds it -- unless --claim names
70 # the holder, which is the key the refusal message now offers (ticket
71 # #269). kanban-md's cmd/delete.go still passes an empty claimant and has
72 # no --claim option; karr's delete takes one so the hint the claim check
73 # prints is followable. Releasing the claim (or letting it expire) stays
74 # the other way through, for the holder as much as for anybody else.
75 $self->check_claim($task, $self->claim);
76  
77 # Before the confirmation, never after it. The point of the warning is that
78 # it can still change the answer, and the operator who reads it and types
79 # "n" is the one it worked on -- so it deliberately does not wait for the
80 # write the way App::karr::Role::DependencyCheck/dependency_report waits for
81 # its own. That rule ("a warning about a move that then lost its
82 # compare-and-swap is a warning about something that did not happen") is
83 # about a card that survives to be read afterwards. Here the write is what
84 # removes the card, so a warning that waits for it is a warning about
85 # something nobody can choose to keep any more.
86 #
87 # Under --yes there is no prompt to precede and it comes anyway: --yes is
88 # the mode agents delete in, so a warning only on the interactive path warns
89 # exactly where nobody is left to read it.
90 my @dependents = $self->_dependent_warnings($task);
91 # And what this card says about the boards it cannot see (#242): the far end
92 # of an escalation is a tag on the card being deleted, so naming it costs
93 # nothing remote.
94 my @cross = $self->_cross_board_warnings($task);
95 # The channel App::karr::Role::DependencyCheck argues for one module over,
96 # rather than a third convention: the human copy on STDERR so STDOUT stays
97 # parseable, --quiet silencing that copy, and --json carrying the identical
98 # sentence in the result object below because a JSON consumer never reads
99 # STDERR.
100 print STDERR map { "$_\n" } @dependents, @cross
101 unless $self->json || $self->quiet;
102 my @warning_report = (
103 ( @dependents ? ( dependent_warnings => \@dependents ) : () ),
104 ( @cross ? ( cross_board_warnings => \@cross ) : () ),
105 );
106  
107 unless ($self->yes) {
108 # STDERR, and not only under --json (#248). The question used to go to
109 # STDOUT, which put a bare `Delete task 1: A? [y/N] ` in front of the
110 # result object: the object was there, but the stream as a whole would
111 # not decode, and this is the command whose output a caller is most
112 # likely to read before doing something irreversible.
113 #
114 # The channel is unconditional rather than a branch on --json, for three
115 # reasons. A prompt is not a result -- `deleted` below is the result, and
116 # the question is dialogue, which is what STDERR is for; the rule
117 # App::karr::Role::DependencyCheck states one module over ("the human
118 # copy goes to STDERR so STDOUT stays parseable") is likewise
119 # unconditional, only its *suppression* depends on an option. Second, the
120 # non-JSON path has the same defect in a quieter form: `karr delete 1 >
121 # kept.txt` wrote the question into the file, so the operator at the
122 # terminal was asked nothing and waited at a blank cursor. And third,
123 # making the channel depend on a flag means the fix only reaches the
124 # caller who remembered the flag.
125 #
126 # Rejecting `--json` without `--yes` outright was the other candidate. It
127 # would have deleted a live answer: `deleted => false` with the two
128 # warning keys beside it is exactly the shape #236 and #242 built for a
129 # card the operator declined to delete, and under --yes there is no
130 # prompt to decline at all, so that shape would become unreachable. It
131 # also refuses `printf 'y\nn\ny\n' | karr delete 1,2,3 --json`, a
132 # per-card answer that --yes cannot express because --yes is
133 # all-or-nothing. --json names an output format and --yes a confirmation
134 # policy; they are orthogonal, not the contradicting pair #235 refuses.
135 printf STDERR "Delete task %d: %s? [y/N] ", $task->id, $task->title;
136 # The question has to be out before the read that waits for its answer,
137 # and this printf alone does not put it there: it ends without a newline,
138 # so nothing in the buffering flushes it (#241).
139 #
140 # Whether that showed depended on where stdin came from, which is why it
141 # went unnoticed for so long. With stdin on a terminal PerlIO flushes the
142 # line-buffered handles when it fills its read buffer, so the prompt got
143 # out by somebody else's courtesy -- a detail of the implementation, not
144 # a promise. With stdin anywhere else -- a pipe, a file, an agent harness
145 # feeding answers -- nothing does it, and the question sits in the buffer
146 # until the next newline or process exit pushes it out, which is after
147 # karr has already acted on the answer. `karr delete 1 < answers` in a
148 # terminal printed the question and the outcome together at the end.
149 #
150 # The flush moves with the question, and what it is worth changed under
151 # it: #249 turned autoflush on for STDOUT and STDERR in
152 # App::karr::Encoding::enable_std_utf8, so on the CLI path the question is
153 # already on the wire when this line runs, and t/241 passes without it.
154 # It stays anyway, for the same reason it was written. The promise belongs
155 # to the question -- it must be readable before the read below blocks --
156 # and not to whatever the process-wide handle setup happens to be today: a
157 # caller that reaches this command without bin/karr, an in-process test
158 # that reopened STDERR onto a capture handle and so dropped both the layer
159 # and the autoflush with it, or a later revert of #249, all get a buffered
160 # handle back. ->flush does not care how the handle is buffered, and on an
161 # unbuffered one it costs a call, so the question arrives before the wait
162 # whichever way stderr and stdin are connected.
163 STDERR->flush;
164 my $answer = <STDIN>;
165  
166 # <STDIN> returns undef at EOF, and karr used to run straight on into
167 # `chomp $answer` -- two "Use of uninitialized value $answer" warnings on
168 # stderr, then "Skipped task 2: d", for every agent or CI run that forgot
169 # --yes (ticket #73). What the right answer to a non-answer is depends on
170 # where stdin came from:
171 #
172 # a terminal the user pressed Ctrl-D. That is "no": skip the task and
173 # exit 0, the same as typing n, and now without warnings.
174 #
175 # anything else nobody is there and nobody will be, so there is no
176 # point pretending the prompt happened. Refuse and say what
177 # to do, the way karr's other destructive commands refuse
178 # without --yes and the way kanban-md refuses when
179 # term.IsTerminal is false.
180 #
181 # An answer that *is* there is honoured either way, so piping "y" or "n"
182 # into `karr delete` keeps working.
183 die "No answer on stdin and stdin is not a terminal. Re-run with --yes.\n"
184 if !defined $answer && !-t STDIN;
185  
186 $answer = '' unless defined $answer;
187 chomp $answer;
188 unless ($answer =~ /^y/i) {
189 # Answering "n" is an answer, not a failure: the batch carries on and
190 # the command still exits 0 if nothing else went wrong.
191 printf "Skipped task %d: %s\n", $task->id, $task->title unless $self->json;
192 # The warnings ride along on a card that was kept, too. Under --json
193 # the pair is their only channel, and this is the case they were for:
194 # `deleted => false` sits beside them and says plainly that the delete
195 # they named did not happen.
196 return { id => $task->id, title => $task->title, deleted => \0,
197 @warning_report };
198 }
199 }
200  
201 $self->delete_task_guarded($task->id, $self->claim);
202 printf "Deleted task %d: %s\n", $task->id, $task->title unless $self->json;
203 # Reported here and not before the prompt, because a card the operator
204 # answered "n" for had its claim examined but not overridden. The two
205 # check_claim calls on this path -- the one above and the one inside
206 # delete_task_guarded -- record into the same slot, so this is still one
207 # line (#177). Deleting the card is also the one case where nothing survives
208 # to be read afterwards, which is what makes the trace matter most here.
209 return { id => $task->id, title => $task->title, deleted => \1,
210 @warning_report,
211 $self->expired_claim_report( $task->id ) };
212 });
213  
214 $self->sync_after;
215  
216 $self->print_json_results(@$results);
217  
218 $self->report_batch_failure($failed, scalar @ids);
219 }
220  
221 # The backward search kanban-md runs before a delete (board.FindDependents,
222 # internal/board/board.go:53-72), which karr had no counterpart for: the claim
223 # check was the only thing between `karr delete 2` and card 6 keeping a
224 # depends_on pointing at an id the board no longer has (#236). karr already
225 # *shows* that damage -- `karr show 6` renders "2 (unknown)" through
226 # Cmd::Show::_dependency_label -- so the information existed; it simply arrived
227 # after the last moment anybody could act on it.
228 #
229 # It warns and deletes, as the reference does. Refusing would make a hard delete
230 # depend on cards the operator may not care about, and karr answers the whole
231 # depends_on family by warning rather than blocking (App::karr::Role::
232 # DependencyCheck, #123). What the warning is *for* here is the door karr has
233 # and kanban-md does not: `karr archive` keeps the ref, so the dependency still
234 # resolves and `show` labels it `archived` instead of `unknown`. Naming that
235 # command is the difference between reporting damage and preventing it.
236 #
237 # `parent` is checked beside depends_on, as it is over there. No karr command
238 # sets it (App::karr::Task) so on a karr-native board the branch never fires --
239 # but `karr import` brings the field in from a kanban-md board and Task keeps
240 # it, and unlike a dependency nothing in karr ever renders it afterwards, so
241 # this is the only place an orphaned child would be named at all. The cost is a
242 # comparison over a list this method already holds.
243 #
244 # Deliberately board-local, and not only because it is cheap that way: a card on
245 # another board waiting on this one through a `needs:` link
246 # (App::karr::CrossBoard) is invisible from here, because reading the far board
247 # needs a path this command does not have and must not invent -- the same line
248 # App::karr::Role::DependencyCheck draws for the cross-board half of its own
249 # warning. What can still be said about that board without opening it is
250 # _cross_board_warnings below.
251 #
252 # Re-read per id rather than once for the batch: `karr delete 6,2` should not
253 # warn that 6 depends on 2 after 6 itself has been deleted, so each id is asked
254 # about the board as it stands when its turn comes.
255 sub _dependent_warnings {
256 my ($self, $task) = @_;
257  
258 my $id = $task->id;
259 # Frontmatter carries whatever the document said, so only values that are ids
260 # at all are compared: `depends_on: [abc]` is a hand-edited card, not a
261 # dependent, and `==` on it would add "Argument isn't numeric" to the output
262 # of a delete -- the same care run_batch takes when echoing a non-numeric
263 # batch id.
264 my $names_id = sub {
265 my ($value) = @_;
266 return 0 unless defined $value && $value =~ /\A[0-9]+\z/;
267 return $value + 0 == $id ? 1 : 0;
268 };
269  
270 my @warnings;
271 # load_tasks is already in ascending id order (App::karr::BoardStore).
272 for my $other ($self->load_tasks) {
273 next if $other->id == $id;
274 push @warnings, sprintf
275 'Warning: task %d (%s) depends on task %d, which is being deleted '
276 . '(use "karr archive %d" to keep the card)',
277 $other->id, $other->title, $id, $id
278 if grep { $names_id->($_) } @{ $other->depends_on };
279 push @warnings, sprintf
280 'Warning: task %d (%s) has task %d as its parent, which is being deleted '
281 . '(use "karr archive %d" to keep the card)',
282 $other->id, $other->title, $id, $id
283 if $names_id->( $other->parent );
284 }
285 return @warnings;
286 }
287  
288 # The same arrival-too-late, one repository over (#242). `karr delete 1` on the
289 # board an escalation was raised on said nothing, and `karr needs` on the board
290 # that raised it then reported `task 1 does not exist on board boardA` -- the
291 # cross-board spelling of `2 (unknown)`, and a worse one: App::karr::CrossBoard/
292 # link_state calls that state `missing`, and `karr needs --resolve` refuses on
293 # purpose to settle a link whose card cannot be read, so a card blocked on it
294 # stays blocked with nothing over there able to lift it.
295 #
296 # The far board is still not opened. What makes a warning possible anyway is
297 # that the escalation protocol writes its far end onto *this* card:
298 # `escalated-from:<board>#<id>`, set by `karr create --escalated-from`, is a tag
299 # on the card being deleted. Naming the far card out of this board's own tag is
300 # the honest reach -- resolving it would need the fleet configuration `karr
301 # needs` reads and this command does not, and `karr needs` is the command that
302 # already answers that question.
303 #
304 # `finished or not` is the honest half of the `karr archive` door (#250).
305 # Archiving keeps the link resolvable, and CrossBoard/link_state settles it on
306 # the far board the moment this card is terminal -- whether the card was
307 # finished or given up. karr records progress, not outcome, and a card archived
308 # out of the backlog is frontmatter-identical to one archived after `done`
309 # (CONTEXT.md, Task lifecycle), so nothing here could tell the far board which
310 # it was. Saying it in the warning is all this side can do; reporting it on the
311 # reading side is ticket #258.
312 #
313 # `may be waiting on it` rather than `is`: the two halves of the protocol are a
314 # convention that nothing enforced before this module existed, which is why
315 # App::karr::CrossBoard/link_state has a `verified` field at all. The far card
316 # usually carries the matching `needs:`; this side cannot know that it does.
317 #
318 # The `needs:` direction is reported too and says something else, so it reads as
319 # a different sentence and carries no `karr archive` door: keeping the card
320 # would repair nothing over there, because nothing over there is broken -- the
321 # link is on the card being deleted and goes with it. What ends is anything on
322 # this board waiting for the far card, and once the card is gone no command
323 # mentions it again: `karr needs` reads links off the cards that carry them.
324 sub _cross_board_warnings {
325 my ($self, $task) = @_;
326  
327 my $id = $task->id;
328 my @warnings;
329 # Tag order, as App::karr::CrossBoard returns it. A tag that carries the
330 # prefix but not a well-formed reference is skipped there, and a delete must
331 # not invent a third state for it either.
332 for my $ref ( App::karr::CrossBoard->escalated_from_of($task) ) {
333 push @warnings, sprintf
334 'Warning: task %d (%s) was escalated from %s, which may be waiting on it '
335 . 'from another board (use "karr archive %d" to keep that link resolvable '
336 . '-- it reads as settled over there, finished or not)',
337 $id, $task->title, App::karr::CrossBoard->format_ref($ref), $id;
338 }
339 for my $ref ( App::karr::CrossBoard->needs_of($task) ) {
340 push @warnings, sprintf
341 'Warning: task %d (%s) waits on %s on another board, and nothing here '
342 . 'will be waiting for it once this card is gone',
343 $id, $task->title, App::karr::CrossBoard->format_ref($ref);
344 }
345 return @warnings;
346 }
347  
348 1;
349  
350 __END__
« blib/lib/App/karr/Foundation/Runner.pm ↑ Summary blib/lib/App/karr/Cmd/Destroy.pm »