@feigi/fleet-ctl 3.46.2 → 3.46.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/scripts/worktree.sh +87 -15
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@feigi/fleet-ctl",
3
- "version": "3.46.2",
3
+ "version": "3.46.3",
4
4
  "description": "Agent fleet: run-team controller, merge bot, PR reviewer, and the ticket pipeline they share",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -748,11 +748,33 @@ wt_count_pair() {
748
748
  wt_count_linked || return 1
749
749
  }
750
750
 
751
+ # Read the `gitdir` back-link of the admin dir $1 into `$wt_bl_back`: the
752
+ # worktree directory it names, made absolute against $1 when it is relative.
753
+ # 0 read; 1 the file could not be read. `$wt_bl_rel` holds the path as
754
+ # written, so a caller can tell an empty file from a path.
755
+ wt_read_backlink() {
756
+ if ! wt_bl_back=$(cat "$1/gitdir" 2>/dev/null && echo x); then
757
+ return 1
758
+ fi
759
+ # `cat` adds no newline of its own, so the sentinel alone comes off, then
760
+ # the one newline git writes after the path, if it is there.
761
+ wt_bl_back=${wt_bl_back%x}
762
+ wt_bl_back=${wt_bl_back%"
763
+ "}
764
+ wt_bl_back=${wt_bl_back%/.git}
765
+ wt_bl_rel=$wt_bl_back
766
+ case $wt_bl_back in
767
+ /*) ;;
768
+ *) wt_bl_back=$1/$wt_bl_back ;;
769
+ esac
770
+ }
771
+
751
772
  # Is any fleet worktree standing with its registration removed under it? 0
752
773
  # when none is; 1 with `$wt_why` naming the first `.worktrees/*` pointer git
753
- # does not list that names an admin dir the registry does not hold, or that
754
- # could not be read. Reads `$wt_list` for the main checkout and the listed
755
- # paths, and `$wt_root` for the registry, both as `wt_count_pair` left them.
774
+ # does not list that names an admin dir the registry does not hold, that
775
+ # names one whose registration is now another worktree's, or that could not
776
+ # be read. Reads `$wt_list` for the main checkout and the listed paths, and
777
+ # `$wt_root` for the registry, both as `wt_count_pair` left them.
756
778
  #
757
779
  # The counts cannot see this: git lists a linked worktree only from its admin
758
780
  # dir, so a registry `rm -rf`'d whole, or one entry removed, drops the entry
@@ -780,10 +802,30 @@ wt_count_pair() {
780
802
  # directory, pointer or named admin dir that cannot be read is a refusal: what
781
803
  # it names is unknown, and unknown is never "registered".
782
804
  #
805
+ # An unlisted directory whose pointer names a held admin dir of its own name is
806
+ # asked one more question, because that registration may have been reissued: the
807
+ # worktree was orphaned, and a later `git worktree add` of another worktree of
808
+ # the same basename took the id. Its branch then reads as held by nothing, and
809
+ # the admin dir answers for a live worktree elsewhere. The admin dir's `gitdir`
810
+ # back-link says whose it is now: a target that exists and is not this
811
+ # directory is a refusal; one that does not exist is a repo moved without `git
812
+ # worktree repair`, which git still holds through the registration's HEAD, but
813
+ # only where its nearest existing ancestor can be searched, since a target
814
+ # that cannot be stat'ed is unknown, not absent, and is a refusal too; and
815
+ # one that is this directory spelled another way (`-ef`) is the registration
816
+ # itself. A directory whose name is not its admin dir's id is a copy, skipped
817
+ # unanswered: a copy and a reissued id read the same through the back-link
818
+ # (another worktree's, that git lists), so the names are the only signal, git
819
+ # taking an id from the basename it was given.
820
+ #
783
821
  # Ceilings, left open on purpose. A `.worktrees` directory that cannot be
784
822
  # listed is skipped, not refused: every worktree in it that git still lists is
785
823
  # answered by the caller's own guards, each with its own remedy, and an
786
824
  # unlisted one behind it needs that fault and a removed registry entry at once.
825
+ # An orphan whose original id carried a number, `1-held1` for a worktree named
826
+ # `1-held`, is not caught: reissuing that id needs a new worktree with the
827
+ # literal basename `1-held1`, so the directory's own name and the registration's
828
+ # differ, and it reads as a copy.
787
829
  # And only `.worktrees/*` itself is read, the depth claim-ticket.sh creates
788
830
  # worktrees at: one nested deeper (`.worktrees/feature/x`) is not, since
789
831
  # reaching it means walking every directory under `.worktrees/` that holds no
@@ -866,6 +908,44 @@ worktree $wt_cp_wt
866
908
  wt_why="worktree pointer $wt_cp_ptr names admin dir $wt_cp_admin, an empty directory git does not list — git lists no worktree there, so no absence it reports can be trusted; inspect the directory and, once its work is saved, remove it by hand"
867
909
  return 1
868
910
  fi
911
+ # Past here the pointer names a registration that is held. A COPY of a
912
+ # worktree directory carries a pointer naming an admin dir git still
913
+ # lists for the ORIGINAL, and so does a directory whose registration was
914
+ # reissued: the admin dir's back-link is another worktree's in both. Only
915
+ # the names tell them apart. Git takes an id from the basename passed to
916
+ # `git worktree add`, so an id can be reissued only to a worktree of that
917
+ # same basename; a directory whose own name is not its admin dir's id is a
918
+ # copy and is skipped.
919
+ [ "${wt_cp_admin##*/}" = "${wt_cp_wt##*/}" ] || continue
920
+ # Git drops an admin dir with no readable back-link from its listing, and
921
+ # the counts refuse on that; this refusal covers only a fault landing
922
+ # between the listing and this read.
923
+ if ! wt_read_backlink "$wt_cp_admin" || [ -z "$wt_bl_rel" ]; then
924
+ wt_why="worktree pointer $wt_cp_ptr names admin dir $wt_cp_admin, whose gitdir back-link could not be read, so which worktree it is registered for is unknown; inspect the directory and, once its work is saved, remove it by hand"
925
+ return 1
926
+ fi
927
+ # A back-link to nothing is a repo moved without `git worktree repair`:
928
+ # git still holds the branch through that registration's HEAD. A target
929
+ # that cannot be stat'ed is not a target that is absent: if the nearest
930
+ # ancestor that exists is a directory that cannot be searched, what
931
+ # the back-link names is unknown, and unknown is never "moved".
932
+ if ! [ -e "$wt_bl_back" ]; then
933
+ wt_cp_up=${wt_bl_back%/*}
934
+ while [ -n "$wt_cp_up" ] && ! [ -e "$wt_cp_up" ]; do
935
+ wt_cp_up=${wt_cp_up%/*}
936
+ done
937
+ if ! [ -d "${wt_cp_up:-/}" ] || [ -x "${wt_cp_up:-/}" ]; then continue; fi
938
+ wt_why="could not look up the worktree $wt_bl_back that admin dir $wt_cp_admin is registered for (${wt_cp_up:-/} cannot be searched), so whether its registration is reissued is unknown"
939
+ return 1
940
+ fi
941
+ # shellcheck disable=SC3013,SC2319 # -ef as above; the else's $? is the `[ -ef ]` test's own rc
942
+ if [ "$wt_bl_back" -ef "$wt_cp_wt" ]; then continue; else wt_cp_rc=$?; fi
943
+ if [ "$wt_cp_rc" -ge 2 ]; then
944
+ wt_why="could not compare the worktree $wt_bl_back that admin dir $wt_cp_admin is registered for with $wt_cp_wt (test -ef exited $wt_cp_rc), so whether its registration is reissued is unknown"
945
+ return 1
946
+ fi
947
+ wt_why="worktree pointer $wt_cp_ptr names admin dir $wt_cp_admin, whose registration is now another worktree's ($wt_bl_back) — git lists no worktree at $wt_cp_wt, so its branch is held by nothing; inspect the directory and, once its work is saved, remove it by hand"
948
+ return 1
869
949
  done
870
950
  return 0
871
951
  }
@@ -884,7 +964,8 @@ worktree $wt_cp_wt
884
964
  # 1: refused — `$wt_why` names the cause: the common directory or the
885
965
  # registry could not be read, the listing could not be counted, the
886
966
  # counts still disagree, or a `.worktrees/*` pointer names an admin dir the
887
- # registry does not hold (`wt_count_pointers`). A standing disagreement is
967
+ # registry does not hold or whose registration is now another worktree's
968
+ # (`wt_count_pointers`). A standing disagreement is
888
969
  # named by the DIRECTION observed. FEWER listed than registered is git
889
970
  # dropping an entry it could not read — the fault this check exists to
890
971
  # catch. MORE listed than registered is the reverse, the registry read
@@ -1072,20 +1153,11 @@ EOF
1072
1153
  wt_why="$1 is a symbolic link through which git could not report its admin dir, so which worktree it stands for is unknown${wt_lk_err:+: $wt_lk_err}"
1073
1154
  return 1
1074
1155
  fi
1075
- if ! wt_lk_back=$(cat "${wt_lk_admin%?x}/gitdir" 2>/dev/null && echo x); then
1156
+ if ! wt_read_backlink "${wt_lk_admin%?x}"; then
1076
1157
  wt_why="$1 is a symbolic link whose admin dir's gitdir back-pointer could not be read, so which worktree it stands for is unknown"
1077
1158
  return 1
1078
1159
  fi
1079
- # `cat` adds no newline of its own, so the sentinel alone comes off, then
1080
- # the one newline git writes after the path, if it is there.
1081
- wt_lk_back=${wt_lk_back%x}
1082
- wt_lk_back=${wt_lk_back%"
1083
- "}
1084
- wt_lk_back=${wt_lk_back%/.git}
1085
- case $wt_lk_back in
1086
- /*) ;;
1087
- *) wt_lk_back=${wt_lk_admin%?x}/$wt_lk_back ;;
1088
- esac
1160
+ wt_lk_back=$wt_bl_back
1089
1161
  case $1 in
1090
1162
  */*) wt_lk_up=${1%/*} ;;
1091
1163
  *) wt_lk_up=. ;;