| 1 | # ABSTRACT: Role providing minimal board discovery and config access | ||
| 2 | |||
| 3 | package App::karr::Role::BoardDiscovery; | ||
| 4 | our $VERSION = '0.602'; | ||
| 5 | 2 | ▶ | use Moo::Role; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use MooX::Options; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | # Both loaded without importing, and every call below is qualified. A Moo::Role | ||
| 8 | # composes every sub in its package into its consumers, imported ones included, | ||
| 9 | # so `use Path::Tiny;` here made Path::Tiny's path() a method on ~20 command | ||
| 10 | # classes -- a silent collision waiting for the first command that wants an | ||
| 11 | # attribute called `path` (#38). App::karr::Role::Output states the same rule. | ||
| 12 | 2 | ▶ | use Path::Tiny (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 2 | ▶ | use App::karr::Error (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 2 | ▶ | use App::karr::Role::ExitCodes; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 0 | ▶ | use App::karr::Git; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 16 | use App::karr::BoardStore; | ||
| 17 | use App::karr::Config; | ||
| 18 | |||
| 19 | # Every command that composes this role (directly or via BoardAccess) inherits | ||
| 20 | # the exit-code contract's option-parse half: an unknown option / bad option | ||
| 21 | # value exits 2, not 1. See App::karr::Role::ExitCodes and ADR 0002. The four | ||
| 22 | # board-less commands (agent-name, get-refs, set-refs, skill) compose ExitCodes | ||
| 23 | # on their own. | ||
| 24 | with 'App::karr::Role::ExitCodes'; | ||
| 25 | |||
| 26 | |||
| 27 | # The board-discovery seed. Available on every command that composes this role | ||
| 28 | # (directly or via BoardAccess), so both `karr CMD --dir PATH` and, via the | ||
| 29 | # MooX::Cmd command_chain, the root form `karr --dir PATH CMD` resolve the same | ||
| 30 | # board. format=s also registers dir in _options_data, so positional_args never | ||
| 31 | # mistakes `--dir PATH` (or its value) for a positional argument. | ||
| 32 | option dir => ( | ||
| 33 | is => 'ro', | ||
| 34 | format => 's', | ||
| 35 | doc => 'Path used as the starting point for Git repository discovery', | ||
| 36 | predicate => 1, | ||
| 37 | # `karr CMD --dir PATH` and `karr --dir PATH CMD` resolve the same board | ||
| 38 | # through the same walk, so this option appears on every command. It is | ||
| 39 | # documented in the root help (`karr --help`) and described above; per-command | ||
| 40 | # help suppresses it so the page lists only what differs between commands | ||
| 41 | # (ticket k276). | ||
| 42 | hidden => 1, | ||
| 43 | ); | ||
| 44 | |||
| 45 | has git_root => ( | ||
| 46 | is => 'lazy', | ||
| 47 | isa => sub { | ||
| 48 | die "git_root must be a Path::Tiny object" unless eval { $_[0]->isa('Path::Tiny') }; | ||
| 49 | }, | ||
| 50 | ); | ||
| 51 | |||
| 52 | has store => ( | ||
| 53 | is => 'lazy', | ||
| 54 | ); | ||
| 55 | |||
| 56 | has git => ( | ||
| 57 | is => 'lazy', | ||
| 58 | ); | ||
| 59 | |||
| 60 | has config => ( | ||
| 61 | is => 'lazy', | ||
| 62 | ); | ||
| 63 | |||
| 64 | # Actor role for the activity log identity: 'user' (default) or 'agent'. | ||
| 65 | # Carried to nested karr calls via the KARR_ROLE env var (foundation sets | ||
| 66 | # 'agent'); a --role option on a command overrides this attribute. | ||
| 67 | has role => ( | ||
| 68 | is => 'lazy', | ||
| 69 | builder => sub { $ENV{KARR_ROLE} || 'user' }, | ||
| 70 | ); | ||
| 71 | |||
| 72 | # The effective --dir for this command. A command's own --dir (the | ||
| 73 | # `karr CMD --dir PATH` form) always wins. Otherwise, when MooX::Cmd dispatched | ||
| 74 | # us as a subcommand, the root form `karr --dir PATH CMD` leaves --dir on an | ||
| 75 | # ancestor in the command_chain rather than on this Cmd instance, so adopt it | ||
| 76 | # from there. Consulted from the lazy _build_git_root builder, so the value is | ||
| 77 | # picked up before git_root/store are ever built -- including from | ||
| 78 | # SyncLifecycle's sync_before, which triggers store. | ||
| 79 | sub _effective_dir { | ||
| 80 | my ($self) = @_; | ||
| 81 | return $self->dir if $self->has_dir; | ||
| 82 | |||
| 83 | if ( $self->can('command_chain') && ( my $chain = $self->command_chain ) ) { | ||
| 84 | for my $cmd (@$chain) { | ||
| 85 | next if $cmd == $self; | ||
| 86 | return $cmd->dir if $cmd->can('has_dir') && $cmd->has_dir; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | return undef; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub _build_git_root { | ||
| 93 | my ($self) = @_; | ||
| 94 | |||
| 95 | my $dir = $self->_effective_dir; | ||
| 96 | my $start = defined $dir | ||
| 97 | ? Path::Tiny::path($dir)->absolute | ||
| 98 | : Path::Tiny::path('.')->absolute; | ||
| 99 | |||
| 100 | while (1) { | ||
| 101 | my $git = App::karr::Git->new( dir => $start->stringify ); | ||
| 102 | my $root = $git->repo_root; | ||
| 103 | return $root if $root; | ||
| 104 | last if $start->is_rootdir; | ||
| 105 | $start = $start->parent; | ||
| 106 | } | ||
| 107 | # Not croak: this is the first thing anyone who runs karr outside a | ||
| 108 | # repository sees, and Carp would append this builder's own file and line | ||
| 109 | # to it (#77). Where karr keeps its source is not the reader's problem. | ||
| 110 | App::karr::Error::user_error("Not a git repository. karr requires Git."); | ||
| 111 | } | ||
| 112 | |||
| 113 | sub _build_store { | ||
| 114 | my ($self) = @_; | ||
| 115 | my $git = App::karr::Git->new( dir => $self->git_root->stringify ); | ||
| 116 | return App::karr::BoardStore->new( git => $git ); | ||
| 117 | } | ||
| 118 | |||
| 119 | sub _build_git { | ||
| 120 | my ($self) = @_; | ||
| 121 | return $self->store->git; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub _build_config { | ||
| 125 | my ($self) = @_; | ||
| 126 | my $merged = $self->store->effective_config; | ||
| 127 | return App::karr::Config->from_merged($merged); | ||
| 128 | } | ||
| 129 | |||
| 130 | |||
| 131 | sub require_board { | ||
| 132 | my ($self) = @_; | ||
| 133 | my $store = $self->store; | ||
| 134 | return 1 if $store->board_exists; | ||
| 135 | |||
| 136 | # Nothing under refs/karr/ at all: the sentence every board-less repository | ||
| 137 | # has always got, and the same one Backup/Destroy/Materialize/Repair raise | ||
| 138 | # off has_board_refs, where it means exactly this. No mention of 'karr sync' | ||
| 139 | # here, unlike the read-side message in require_local_board: this method is | ||
| 140 | # called after sync_before, so the pull has already run and found nothing. | ||
| 141 | # | ||
| 142 | # One spelling for all five, not two: the way out is the command itself on | ||
| 143 | # its own last line rather than a name quoted mid-sentence (ticket k263), | ||
| 144 | # and a sentence that reads differently depending on which command raised | ||
| 145 | # it is worse than either wording alone. | ||
| 146 | App::karr::Error::user_error( "No karr board found:\n", | ||
| 147 | App::karr::Error::command_hint('init') ) | ||
| 148 | unless $store->has_board_refs; | ||
| 149 | |||
| 150 | # Refs are there, only refs/karr/config is missing. Saying "no board found" | ||
| 151 | # here was a lie with consequences: an agent got it on a repository holding | ||
| 152 | # 21 tickets, believed the repository had never had a board, and never | ||
| 153 | # looked for them (#133). Name the state, count what is at stake, and say | ||
| 154 | # that completing it keeps the tasks -- not 'karr repair', which only | ||
| 155 | # migrates double-encoded UTF-8 and would die with this very sentence on a | ||
| 156 | # repository that has no refs. | ||
| 157 | my $tasks = $self->_task_refs_held; | ||
| 158 | my $held = | ||
| 159 | $tasks == 0 ? "board metadata is" | ||
| 160 | : $tasks == 1 ? "1 task ref is" | ||
| 161 | : "$tasks task refs are"; | ||
| 162 | die "Half-initialized karr board: refs/karr/config is missing, but $held\n" | ||
| 163 | . "already under refs/karr/, so this repository is not empty.\n" | ||
| 164 | . "Run 'karr init' to complete the board -- it writes the missing config\n" | ||
| 165 | . "and keeps what is already there.\n"; | ||
| 166 | } | ||
| 167 | |||
| 168 | |||
| 169 | sub require_local_board { | ||
| 170 | my ( $self, %args ) = @_; | ||
| 171 | my $store = $self->store; | ||
| 172 | return 1 if $store->board_exists; | ||
| 173 | |||
| 174 | if ( $store->has_board_refs ) { | ||
| 175 | my $tasks = $self->_task_refs_held; | ||
| 176 | my $held = | ||
| 177 | $tasks == 0 ? "no task refs are" | ||
| 178 | : $tasks == 1 ? "1 task ref is" | ||
| 179 | : "$tasks task refs are"; | ||
| 180 | # Never suppressed by --json or --quiet: there is no field in any of | ||
| 181 | # these payloads that could carry it, so STDERR is the only channel. | ||
| 182 | print STDERR | ||
| 183 | "Note: refs/karr/config is missing, so this board is half-initialized:\n" | ||
| 184 | . "$held under refs/karr/, and the name, statuses and defaults shown are\n" | ||
| 185 | . "karr's own, not the board's. Run 'karr init' to complete it -- it keeps\n" | ||
| 186 | . "what is already there.\n"; | ||
| 187 | return 1; | ||
| 188 | } | ||
| 189 | |||
| 190 | # Nothing here at all -- which is what a fresh clone looks like, and what a | ||
| 191 | # repository that never had a board looks like, and the two want opposite | ||
| 192 | # things from the reader. Ask the remote before refusing (#173). | ||
| 193 | return $self->require_local_board(%args) if $self->_autofetch_board; | ||
| 194 | |||
| 195 | my $advice = $store->git->has_remote | ||
| 196 | ? "'git clone' does not fetch refs/karr/*, so a fresh clone starts out like\n" | ||
| 197 | . "this. Run 'karr sync' to fetch the board, or 'karr init' to start one here.\n" | ||
| 198 | : "Run 'karr init' to create one.\n"; | ||
| 199 | die "No karr board in this repository: nothing is stored under refs/karr/.\n" | ||
| 200 | . "This is not an empty board -- nothing was read here at all.\n" | ||
| 201 | . $advice | ||
| 202 | . ( defined $args{hint} ? $args{hint} : '' ); | ||
| 203 | } | ||
| 204 | |||
| 205 | # The fetch require_local_board tries before it refuses (#173). True when the | ||
| 206 | # board is now here, false when it is not -- and either nothing at all or | ||
| 207 | # exactly one line on STDERR, never STDOUT, because the --json and --compact | ||
| 208 | # output this must not disturb is what the agents it exists for parse. | ||
| 209 | # | ||
| 210 | # `git clone` does not carry refs/karr/*, so every fresh clone starts out | ||
| 211 | # holding no board while the whole board sits on its remote. The refusal that | ||
| 212 | # used to be the only answer here was accurate and its advice was right, and it | ||
| 213 | # still cost a second command to act on advice karr could act on itself: the | ||
| 214 | # remote is configured, and `git ls-remote origin refs/karr/*` answers in | ||
| 215 | # milliseconds. Agents took it hardest -- they read "no board" and moved on. | ||
| 216 | # | ||
| 217 | # Four things have to be true, and each of them is a case this must not touch: | ||
| 218 | # | ||
| 219 | # KARR_NO_AUTO_FETCH unset. An opt-OUT, deliberately: an opt-in would be | ||
| 220 | # found only by people who already know their board is on the remote -- | ||
| 221 | # precisely the ones who do not need it, since the state it repairs is | ||
| 222 | # invisible to everyone else. It firing unwanted costs one bounded round | ||
| 223 | # trip; it not existing costs a reader who concludes the board is gone. | ||
| 224 | # | ||
| 225 | # A remote. Without one there is nothing to ask, and `karr init` really is | ||
| 226 | # the answer. | ||
| 227 | # | ||
| 228 | # No unpublished deletions (App::karr::Git/has_pending_deletes). A `karr | ||
| 229 | # destroy` whose push did not land leaves exactly this state -- no local | ||
| 230 | # board, the whole board still on the remote -- and it must not be answered | ||
| 231 | # by fetching the board back. The refs/karr-remote mirror already decides | ||
| 232 | # that correctly on its own: a ref the mirror holds and the board no longer | ||
| 233 | # does reads as this clone's own deletion rather than as something | ||
| 234 | # unfetched, so reconciliation keeps it deleted (measured: with the mirror | ||
| 235 | # in place the pull adopts nothing). This check is the cheap half -- it | ||
| 236 | # costs no round trip per read -- and it is the only guard left where the | ||
| 237 | # mirror is empty but the deletions are unpublished, e.g. a board fetched | ||
| 238 | # into refs/karr by hand. A destroy that did land leaves neither tombstones | ||
| 239 | # nor a remote board, so the question below refuses it instead. | ||
| 240 | # | ||
| 241 | # The remote actually advertises refs/karr/* (App::karr::Git/remote_has_board | ||
| 242 | # -- bounded and never prompting, so an unreachable or silent remote costs | ||
| 243 | # seconds, not the command). A remote without a board leaves the refusal | ||
| 244 | # exactly as it was: there `karr init` is right. | ||
| 245 | # | ||
| 246 | # Only then does it pull, through the ordinary App::karr::Git/pull -- the same | ||
| 247 | # reconciliation `karr sync --pull` runs, with the same guards, on a board that | ||
| 248 | # has nothing local to lose. Nothing is pushed: this is a read. | ||
| 249 | sub _autofetch_board { | ||
| 250 | my ($self) = @_; | ||
| 251 | return 0 if $ENV{KARR_NO_AUTO_FETCH}; | ||
| 252 | |||
| 253 | my $store = $self->store; | ||
| 254 | my $git = $store->git; | ||
| 255 | return 0 unless $git->has_remote; | ||
| 256 | return 0 if $git->has_pending_deletes; | ||
| 257 | |||
| 258 | my $advertised = $git->remote_has_board; | ||
| 259 | if ( !defined $advertised ) { | ||
| 260 | # The probe could not ask. Say so in one line -- the reader has just | ||
| 261 | # waited for it, and the refusal that follows would otherwise look | ||
| 262 | # instant and offline -- then leave the refusal to speak for itself. | ||
| 263 | my $why = $git->last_error // 'unknown error'; | ||
| 264 | $why =~ s/\s*\n.*//s; | ||
| 265 | print STDERR "karr: could not ask the remote whether it has the board: $why\n"; | ||
| 266 | return 0; | ||
| 267 | } | ||
| 268 | return 0 unless $advertised; | ||
| 269 | |||
| 270 | # pull dies on the board-identity and wholesale-wipe guards and on a ref | ||
| 271 | # it could not apply. None of the three can fire on a board with no local | ||
| 272 | # refs, but this is not the place to be the second thing that knows that: | ||
| 273 | # an unasked fetch must not turn a read's refusal into someone else's | ||
| 274 | # exception. | ||
| 275 | my $ok = eval { $git->pull }; | ||
| 276 | unless ($ok) { | ||
| 277 | my $why = $@ || $git->last_error || 'unknown error'; | ||
| 278 | $why =~ s/\s*\n.*//s; | ||
| 279 | print STDERR "karr: could not fetch the board from the remote: $why\n"; | ||
| 280 | return 0; | ||
| 281 | } | ||
| 282 | # The board can still be gone -- destroyed between the probe and the pull. | ||
| 283 | # Then this fetched nothing and the refusal below is the right answer. | ||
| 284 | return 0 unless $store->has_board_refs; | ||
| 285 | |||
| 286 | print STDERR | ||
| 287 | "karr: fetched the board from the remote ('git clone' does not carry refs/karr/*).\n"; | ||
| 288 | return 1; | ||
| 289 | } | ||
| 290 | |||
| 291 | # How many task refs the repository holds. Through a list, because | ||
| 292 | # list_task_refs returns through sort and would answer the number of arguments | ||
| 293 | # sort was handed if it were called in scalar context. | ||
| 294 | sub _task_refs_held { | ||
| 295 | my ($self) = @_; | ||
| 296 | my @ids = $self->store->git->list_task_refs; | ||
| 297 | return scalar @ids; | ||
| 298 | } | ||
| 299 | |||
| 300 | 1; | ||
| 301 | |||
| 302 | __END__ | ||