| 1 | # ABSTRACT: Atomically find and claim the next available task | ||
| 2 | |||
| 3 | package App::karr::Cmd::Pick; | ||
| 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 pick --claim NAME [--move STATUS] [--status LIST] [--tags LIST] [--compact]', | |
| 9 | 2 | ); | |
| 10 | 0 | ▶ | use App::karr::Role::BoardAccess; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | use App::karr::Role::Output; | ||
| 12 | use App::karr::Role::CompactOutput; | ||
| 13 | use App::karr::Role::DependencyCheck; | ||
| 14 | use App::karr::Role::PickRules; | ||
| 15 | use App::karr::Role::ClaimDefault; | ||
| 16 | use App::karr::Task; | ||
| 17 | use App::karr::Config; | ||
| 18 | use App::karr::Lock; | ||
| 19 | use App::karr::Error (); | ||
| 20 | use Time::Piece; | ||
| 21 | |||
| 22 | with 'App::karr::Role::BoardAccess', 'App::karr::Role::Output', | ||
| 23 | 'App::karr::Role::CompactOutput', 'App::karr::Role::ClaimTimeout', | ||
| 24 | 'App::karr::Role::DependencyCheck', 'App::karr::Role::PickRules', | ||
| 25 | 'App::karr::Role::ClaimDefault'; | ||
| 26 | |||
| 27 | |||
| 28 | option claim => ( | ||
| 29 | is => 'ro', | ||
| 30 | format => 's', | ||
| 31 | doc => 'Agent name to claim the task for (defaults to KARR_CLAIM)', | ||
| 32 | ); | ||
| 33 | |||
| 34 | option status => ( | ||
| 35 | is => 'ro', | ||
| 36 | format => 's', | ||
| 37 | doc => 'Source status(es) to pick from (comma-separated)', | ||
| 38 | ); | ||
| 39 | |||
| 40 | option move => ( | ||
| 41 | is => 'ro', | ||
| 42 | format => 's', | ||
| 43 | doc => 'Move picked task to this status', | ||
| 44 | ); | ||
| 45 | |||
| 46 | option tags => ( | ||
| 47 | is => 'ro', | ||
| 48 | format => 's', | ||
| 49 | doc => 'Only pick tasks matching at least one tag', | ||
| 50 | ); | ||
| 51 | |||
| 52 | sub execute { | ||
| 53 | my ($self, $args_ref, $chain_ref) = @_; | ||
| 54 | |||
| 55 | # --claim was `required => 1` before ADR 0005; KARR_CLAIM now fills it when the | ||
| 56 | # flag is omitted. pick has to claim under some name, so when neither supplies | ||
| 57 | # one this is a usage error (exit 2, as the old required was) naming both ways. | ||
| 58 | # Before sync_before, so an invalid invocation fails without a network round | ||
| 59 | # trip, the way an option-parse error always did. | ||
| 60 | my $claim = $self->resolved_claim; | ||
| 61 | $self->usage_error( | ||
| 62 | App::karr::Error::mandatory_claim_message( 'pick', 'pick', '--claim', 'NAME' ) ) | ||
| 63 | unless defined $claim && length $claim; | ||
| 64 | |||
| 65 | $self->sync_before; | ||
| 66 | $self->require_board; | ||
| 67 | |||
| 68 | my $ec = $self->store->effective_config; | ||
| 69 | my $timeout = $self->_parse_timeout($ec->{claim_timeout} // '1h'); | ||
| 70 | |||
| 71 | # Before any lock is taken: --move is a plain option value, and a bad one used | ||
| 72 | # to be discovered only after a task had already been locked and claimed, so | ||
| 73 | # the pick parked it in a status that is not a column (ticket #54). Pick does | ||
| 74 | # not go through apply_status_change -- it has its own compare-and-swap loop -- | ||
| 75 | # so the check is here. | ||
| 76 | App::karr::Config->from_merged($ec)->validate_status($self->move) | ||
| 77 | if defined $self->move; | ||
| 78 | |||
| 79 | # --status is a comma-separated list of source statuses, and every element is | ||
| 80 | # a status the board must know -- a typo used to be answered with "No available | ||
| 81 | # tasks to pick." and exit 0, which reads as "no work" when the truth is "no | ||
| 82 | # such status" (ticket #271). The filter form accepts `archived` the way | ||
| 83 | # `list --status` does (App::karr::Config/validate_status_filter). | ||
| 84 | if ( defined $self->status ) { | ||
| 85 | App::karr::Config->from_merged($ec)->validate_status_filter($_) | ||
| 86 | for split /,/, $self->status; | ||
| 87 | } | ||
| 88 | |||
| 89 | # Backlog is held back (ticket k306): no card in it is ever pickable, so a | ||
| 90 | # --status naming it could only ever answer "nothing to pick" -- which reads | ||
| 91 | # as "no work" when the truth is "that work is held back". Refused as a usage | ||
| 92 | # error instead, pointing at the promotion that makes a card pickable. And --move | ||
| 93 | # into it is refused on the same ground from the other side: pick claims the | ||
| 94 | # card it moves, and a card in backlog holds no claim. | ||
| 95 | my $config = App::karr::Config->from_merged($ec); | ||
| 96 | my ($held) = grep { $config->is_held_back_status($_) } split /,/, $self->status // ''; | ||
| 97 | $self->usage_error( "$held is held back and never picked -- promote a card to pick it:\n" | ||
| 98 | . App::karr::Error::command_hint( 'move', 'ID', $config->promotion_status // 'STATUS' ) ) | ||
| 99 | if defined $held; | ||
| 100 | if ( defined $self->move && $config->is_held_back_status( $self->move ) ) { | ||
| 101 | my ($claims_in) = grep { $config->status_requires_claim($_) } $config->statuses; | ||
| 102 | my $move = $self->move; | ||
| 103 | $self->usage_error( | ||
| 104 | "--move $move would leave the picked card in $move, which holds no claim" | ||
| 105 | . ( defined $claims_in | ||
| 106 | ? ":\n" . App::karr::Error::command_hint( 'pick', '--move', $claims_in, | ||
| 107 | ( defined $self->claim && length $self->claim ? ( '--claim', $self->claim ) : () ) ) | ||
| 108 | : '' ) ); | ||
| 109 | } | ||
| 110 | |||
| 111 | # A ranking, not a decision. Every one of these is re-read and re-tested under | ||
| 112 | # its own lock before anything is written (see EXCLUSIVITY above). | ||
| 113 | # | ||
| 114 | # Which cards qualify and what order they come in are both | ||
| 115 | # App::karr::Role::PickRules', not this command's: App::karr::Foundation's | ||
| 116 | # ticket mode has to name the card `karr pick` would hand out, and while the | ||
| 117 | # eligibility test and the class/priority/id sort were written out here and | ||
| 118 | # again in App::karr::Foundation::Picker, they agreed only by having been | ||
| 119 | # copied (ticket #198). What stays here is everything the role deliberately | ||
| 120 | # does not do -- the lock, the re-read and the compare-and-swap below. | ||
| 121 | my %filter = $self->_pick_filter($timeout); | ||
| 122 | my @tasks = $self->pick_candidates( [ $self->load_tasks ], %filter ); | ||
| 123 | |||
| 124 | unless (@tasks) { | ||
| 125 | return $self->_nothing_picked("No available tasks to pick."); | ||
| 126 | } | ||
| 127 | |||
| 128 | # Try to lock + claim. A karr board lives in refs/karr/*, which exist only | ||
| 129 | # inside a Git repo, so reaching this point means we are in one -- the | ||
| 130 | # locking path is unconditional. | ||
| 131 | my $lock = App::karr::Lock->new( | ||
| 132 | git => $self->git, | ||
| 133 | # Not the 1h _parse_timeout falls back to on its own: see LOCK EXPIRY. | ||
| 134 | ttl => $self->_parse_timeout($ec->{lock_timeout}, App::karr::Lock->DEFAULT_TTL), | ||
| 135 | ); | ||
| 136 | my $email = $self->git->git_user_email || $claim; | ||
| 137 | |||
| 138 | my $picked; | ||
| 139 | for my $candidate (@tasks) { | ||
| 140 | my ($ok) = $lock->acquire($candidate->id, $email); | ||
| 141 | next unless $ok; | ||
| 142 | |||
| 143 | # Hold the lock for the claim and nothing else, and give it back on the way | ||
| 144 | # out either way. Before #45 a die in here left the ref behind for good. | ||
| 145 | $picked = eval { $self->_claim_under_lock($candidate->id, \%filter) }; | ||
| 146 | my $err = $@; | ||
| 147 | $lock->release($candidate->id, $email); | ||
| 148 | die $err if $err; | ||
| 149 | |||
| 150 | last if $picked; | ||
| 151 | } | ||
| 152 | |||
| 153 | unless ($picked) { | ||
| 154 | return $self->_nothing_picked( | ||
| 155 | "No available tasks to pick (every candidate was locked or taken)."); | ||
| 156 | } | ||
| 157 | |||
| 158 | # Both writes have to happen before the push, or they never leave this clone: | ||
| 159 | # sync_after is the last thing that talks to the remote and it disarms the | ||
| 160 | # SyncGuard behind it. The lock release above is the same story -- publishing | ||
| 161 | # a lock and then deleting it locally left the ref on the remote forever (#45). | ||
| 162 | $self->append_log($self->git, | ||
| 163 | agent => $claim, | ||
| 164 | action => 'pick', | ||
| 165 | task_id => $picked->id, | ||
| 166 | detail => $picked->status, | ||
| 167 | ); | ||
| 168 | |||
| 169 | $self->sync_after; | ||
| 170 | |||
| 171 | my %dependency = $self->dependency_report( $picked->id ); | ||
| 172 | |||
| 173 | if ($self->json) { | ||
| 174 | $self->print_json({ %{ $picked->to_json_hash }, %dependency }); | ||
| 175 | return; | ||
| 176 | } | ||
| 177 | |||
| 178 | printf "Picked task %d: %s (claimed by %s)\n", $picked->id, $picked->title, $claim; | ||
| 179 | |||
| 180 | # Everything below is the detail block, and --compact is the instruction not | ||
| 181 | # to print it. Until #251 this command composed App::karr::Role::Output, took | ||
| 182 | # --compact from it and never looked at it, so `pick --claim x --compact` came | ||
| 183 | # out character-identical to `pick --claim x` -- an output that looks like an | ||
| 184 | # answer while disobeying the option, which is the failure #225 refused to | ||
| 185 | # leave standing. | ||
| 186 | # | ||
| 187 | # Meeting the option beat withdrawing it, and that was decided on the role | ||
| 188 | # rather than on this command: Role::Output declared --compact beside --json | ||
| 189 | # then, so removing it from one consumer meant splitting the role and moving | ||
| 190 | # the option surface of the seventeen further commands that ignored it just as | ||
| 191 | # silently. #254 did exactly that -- --compact now lives in | ||
| 192 | # App::karr::Role::CompactOutput, which this command composes and the thirteen | ||
| 193 | # that render nothing do not. Pick meanwhile already prints in two | ||
| 194 | # parts -- who got which card, then what the card is -- so there is an obvious | ||
| 195 | # place to cut, and the reference cuts there too: kanban-md's `pick --no-body` | ||
| 196 | # ("suppress full task details after pick", cmd/pick.go:104) prints its | ||
| 197 | # confirmation line and returns before the detail block. | ||
| 198 | # | ||
| 199 | # Only the plaintext half. The --json branch returned above, exactly as | ||
| 200 | # noBody sits after the JSON check over there. | ||
| 201 | return if $self->compact; | ||
| 202 | |||
| 203 | printf "Status: %s | Priority: %s | Class: %s\n", $picked->status, $picked->priority, $picked->class; | ||
| 204 | if ($picked->body) { | ||
| 205 | print "\n" . $picked->body . "\n"; | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | # Both empty results in one place, because both of them have to honour --json. | ||
| 210 | # `pick` is the agent-facing command, so its --json is the one output in karr | ||
| 211 | # most certain to be machine-parsed -- and it was the one that answered a plain | ||
| 212 | # English sentence, which a consumer could only meet with a decode error | ||
| 213 | # (ticket #65). Every other command already had this: `list --json` prints [], | ||
| 214 | # `archive --json` prints its note as an object. | ||
| 215 | # | ||
| 216 | # The payload is deliberately an object rather than a bare `null`: the JSON | ||
| 217 | # encoder App::karr::Encoding hands out has allow_nonref off, so a top-level | ||
| 218 | # null cannot be emitted without loosening that for every other command's | ||
| 219 | # output as well. | ||
| 220 | # | ||
| 221 | # The exit status stays 0. kanban-md raises a NothingToPick error and exits | ||
| 222 | # nonzero, but karr's exit-code contract (ADR 0002) spends 1 on failure and 2 | ||
| 223 | # on usage, and "no work for you right now" is the normal answer to a poll, not | ||
| 224 | # a failure -- a drain loop that treats it as one stops on its first idle pass. | ||
| 225 | sub _nothing_picked { | ||
| 226 | my ($self, $message) = @_; | ||
| 227 | return $self->print_json({ picked => undef }) if $self->json; | ||
| 228 | print "$message\n"; | ||
| 229 | return; | ||
| 230 | } | ||
| 231 | |||
| 232 | # This command's half of "this card is available to me right now": the two | ||
| 233 | # option filters, split from their comma-separated option strings into the shape | ||
| 234 | # App::karr::Role::PickRules/pickable takes. The rule itself is the role's, and | ||
| 235 | # is the same rule App::karr::Foundation::Picker applies with neither filter set | ||
| 236 | # (#198) -- an option is a narrowing of the shared test, never a second one. | ||
| 237 | # | ||
| 238 | # Built once per run and passed on to _claim_under_lock rather than rebuilt | ||
| 239 | # there, for the reason the old private predicate was a method: the pre-lock | ||
| 240 | # ranking and the re-read under the lock have to ask the identical question, or | ||
| 241 | # moving the test inside the lock buys nothing (#86). | ||
| 242 | # | ||
| 243 | # Truthiness decides whether a filter applies, which is what `if ($self->status)` | ||
| 244 | # meant here before: an unset option leaves the key out, and leaving it out is | ||
| 245 | # not the same as passing an empty list -- no `statuses` means "anything but a | ||
| 246 | # terminal status", `statuses => []` would mean nothing qualifies at all. | ||
| 247 | sub _pick_filter { | ||
| 248 | my ($self, $timeout) = @_; | ||
| 249 | return ( | ||
| 250 | timeout => $timeout, | ||
| 251 | ( $self->status ? ( statuses => [ split /,/, $self->status ] ) : () ), | ||
| 252 | ( $self->tags ? ( tags => [ split /,/, $self->tags ] ) : () ), | ||
| 253 | ); | ||
| 254 | } | ||
| 255 | |||
| 256 | # Claim one candidate, or return false if it is no longer ours to claim. | ||
| 257 | # | ||
| 258 | # Everything here reads the card fresh: the ranking in execute was built from a | ||
| 259 | # snapshot taken before any lock existed and is stale by the time we get here. | ||
| 260 | # The write is guarded against the OID that same read came from, so losing to | ||
| 261 | # another agent is a false return rather than a silent overwrite. retry_contended | ||
| 262 | # separates the two ways a compare-and-swap can fail: the card changed under us | ||
| 263 | # (re-read, decide again) versus the card is taken (final, move on). | ||
| 264 | sub _claim_under_lock { | ||
| 265 | my ($self, $id, $filter) = @_; | ||
| 266 | |||
| 267 | return $self->git->retry_contended("the claim on task $id", sub { | ||
| 268 | my ($oid, $task) = $self->store->find_task_with_oid($id); | ||
| 269 | return (0) unless $self->pickable($task, %$filter); | ||
| 270 | |||
| 271 | $task->claimed_by($self->resolved_claim); | ||
| 272 | $task->claimed_at(gmtime->datetime . 'Z'); | ||
| 273 | |||
| 274 | # Outside the --move branch, and before it: on a pick the *claim* is the | ||
| 275 | # taking-up. `karr pick --claim X` with no --move is the commonest call | ||
| 276 | # there is, and after it the agent holds the card and starts work -- | ||
| 277 | # whether the status changed on the way says nothing about whether somebody | ||
| 278 | # should have been told what is still open underneath. Scoped to --move | ||
| 279 | # only, this left #123's own sentence ("karr pick hands it out regardless") | ||
| 280 | # true of the very command it was written about. | ||
| 281 | # | ||
| 282 | # Without --move the card stays where it is, so the status it stays in is | ||
| 283 | # the one to judge. That is not a formality: --status is the one way a card | ||
| 284 | # already in a terminal status reaches this point at all (the shared | ||
| 285 | # App::karr::Role::PickRules/pickable excludes terminal statuses only when | ||
| 286 | # --status is absent), and picking up a finished card must not lecture about | ||
| 287 | # dependencies that stopped mattering when it was finished. | ||
| 288 | # | ||
| 289 | # Pick does not go through apply_status_change (see EXCLUSIVITY above), so | ||
| 290 | # this is its own call to the check every other status change gets there. | ||
| 291 | # And karr deliberately parts company with the reference here: kanban-md | ||
| 292 | # filters a card with unmet dependencies out of the candidate set outright | ||
| 293 | # (internal/board/pick.go:69, filterPickDeps), so pick never offers it; | ||
| 294 | # karr hands it over and warns, because nothing about depends_on blocks | ||
| 295 | # anything (ticket #123). | ||
| 296 | $self->check_dependencies( $task, $self->move // $task->status ); | ||
| 297 | |||
| 298 | if ($self->move) { | ||
| 299 | my $old_status = $task->status; | ||
| 300 | $task->status($self->move); | ||
| 301 | # Same lifecycle rules as every other status change (ticket #68); the | ||
| 302 | # implementation is on the task, mirroring kanban-md's lifecycle.go. | ||
| 303 | # The board's own config goes with it, the way apply_status_change hands | ||
| 304 | # it over everywhere else: without it the terminal question was answered | ||
| 305 | # for the default board, and a pick --move into this board's final | ||
| 306 | # column recorded no completion (ticket #101, the last #67 leftover). | ||
| 307 | my $config = App::karr::Config->from_merged( $self->store->effective_config ); | ||
| 308 | $task->update_timestamps( $old_status, $self->move, | ||
| 309 | ( $config->statuses )[0], $config ); | ||
| 310 | } | ||
| 311 | |||
| 312 | return () unless $self->save_task($task, $oid); | ||
| 313 | return $task; | ||
| 314 | }); | ||
| 315 | } | ||
| 316 | |||
| 317 | 1; | ||
| 318 | |||
| 319 | __END__ | ||