@johngaynor/entourage 0.1.0 → 0.2.0

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.
@@ -0,0 +1,184 @@
1
+ # Projects: groups of repos shown in Herdr as spaces. Sourced by cli/entourage.
2
+
3
+ # A project is a folder in $PROJECTS:
4
+ # PROJECT.md what the project is and what belongs in each repo
5
+ # repos one repo path per line (~/ for the home folder, # for comments)
6
+ # links/ generated: a link to each repo, named after its folder
7
+ # CLAUDE.md, AGENTS.md generated: links to PROJECT.md, so agents read it
8
+
9
+ valid_name() { case ${1:-} in '' | *[!a-z0-9-]*) return 1 ;; esac; }
10
+
11
+ # Print a project's folder, or stop if there is no such project.
12
+ project_dir() {
13
+ local name=${1:-}
14
+ [ -n "$name" ] || die "which project? See: entourage project ls"
15
+ valid_name "$name" && [ -d "$PROJECTS/$name" ] ||
16
+ die "no project called '$name'. Make it with: entourage project new $name"
17
+ (cd -P "$PROJECTS/$name" && pwd)
18
+ }
19
+
20
+ # Print a project's repo paths, absolute, one per line.
21
+ project_repos() {
22
+ local dir=$1 line
23
+ [ -f "$dir/repos" ] || return 0
24
+ while IFS= read -r line || [ -n "$line" ]; do
25
+ case $line in
26
+ '' | '#'*) continue ;;
27
+ '~/'*) line=$HOME/${line#'~/'} ;;
28
+ esac
29
+ printf '%s\n' "$line"
30
+ done < "$dir/repos"
31
+ }
32
+
33
+ is_repo() { [ -e "$1/.git" ]; }
34
+
35
+ # Rebuild what is generated in a project folder. Only links are ever removed
36
+ # or replaced; a real file in the way is reported and left alone.
37
+ sync_links() {
38
+ local dir=$1 src dst base names=$'\n'
39
+ mkdir -p "$dir/links"
40
+ while IFS= read -r src; do
41
+ base=${src##*/}
42
+ dst=$dir/links/$base
43
+ names+=$base$'\n'
44
+ is_repo "$src" || say " warning: $src is missing or is not a git repo"
45
+ if [ -L "$dst" ]; then
46
+ [ "$(readlink "$dst")" = "$src" ] || { rm "$dst"; ln -s "$src" "$dst"; }
47
+ elif [ -e "$dst" ]; then
48
+ say " skipped links/$base (a real file is in the way)"
49
+ else
50
+ ln -s "$src" "$dst"
51
+ fi
52
+ done < <(project_repos "$dir")
53
+ for dst in "$dir"/links/*; do
54
+ [ -L "$dst" ] || continue
55
+ case $names in *$'\n'"${dst##*/}"$'\n'*) ;; *) rm "$dst" ;; esac
56
+ done
57
+ for base in CLAUDE.md AGENTS.md; do
58
+ [ -e "$dir/$base" ] || [ -L "$dir/$base" ] || ln -s PROJECT.md "$dir/$base"
59
+ done
60
+ }
61
+
62
+ project_new() {
63
+ local name=${1:-} dir
64
+ valid_name "$name" || die "a project name is lowercase letters, digits and dashes, like: my-project"
65
+ dir=$PROJECTS/$name
66
+ [ ! -e "$dir" ] || die "project '$name' already exists at $dir"
67
+ [ -f "$TEMPLATE/PROJECT.md" ] || die "the project template is missing from $TEMPLATE"
68
+ mkdir -p "$dir"
69
+ sed "s/__NAME__/$name/" "$TEMPLATE/PROJECT.md" > "$dir/PROJECT.md"
70
+ : > "$dir/repos"
71
+ sync_links "$dir"
72
+ say "Made project '$name' at $dir"
73
+ say "Next:"
74
+ say " entourage project add $name <path to a repo>"
75
+ say " fill in $dir/PROJECT.md"
76
+ say " entourage project open $name"
77
+ }
78
+
79
+ project_add() {
80
+ local name=${1:-} path=${2:-} dir abs other stored
81
+ dir=$(project_dir "$name")
82
+ [ -n "$path" ] || die "which repo? Usage: entourage project add $name <path>"
83
+ [ -d "$path" ] || die "$path is not a folder"
84
+ abs=$(cd "$path" && pwd -P)
85
+ is_repo "$abs" || die "$abs is not a git repo (the top folder of a clone is needed)"
86
+ while IFS= read -r other; do
87
+ [ "$other" != "$abs" ] || die "$abs is already in project '$name'"
88
+ [ "${other##*/}" != "${abs##*/}" ] ||
89
+ die "project '$name' already has a repo called ${abs##*/} ($other); two repos in a project cannot share a folder name"
90
+ done < <(project_repos "$dir")
91
+ case $abs in
92
+ "$HOME"/*) stored="~/${abs#"$HOME"/}" ;;
93
+ *) stored=$abs ;;
94
+ esac
95
+ printf '%s\n' "$stored" >> "$dir/repos"
96
+ sync_links "$dir"
97
+ say "Added ${abs##*/} to project '$name'"
98
+ say "Describe what it owns in $dir/PROJECT.md"
99
+ }
100
+
101
+ # Show the Herdr space for a project, making it if it is not open.
102
+ show_space() {
103
+ local name=$1 dir=$2 id
104
+ # Find an open space whose folder is this project's. `herdr-app ls` prints
105
+ # indented JSON; a space's own "id" and "folder" sit at six spaces. Closed
106
+ # spaces are listed after "recent" in the same shape, so stop there.
107
+ id=$(herdr_app ls 2>/dev/null | awk -v want="$dir" '
108
+ /^ "recent"/ { exit }
109
+ /^ "id": / { id = $2; gsub(/[",]/, "", id) }
110
+ /^ "folder": / { f = $0; sub(/^ *"folder": "/, "", f); sub(/",?$/, "", f)
111
+ if (f == want) { print id; exit } }') || true
112
+ if [ -n "$id" ]; then
113
+ herdr_app do space.select "{\"id\":\"$id\"}" >/dev/null 2>&1 ||
114
+ die "could not show the space for '$name'. Is Herdr open?"
115
+ say "Showing the space for '$name'"
116
+ else
117
+ herdr_app do space.new "{\"name\":\"$name\",\"folder\":\"$dir\"}" >/dev/null 2>&1 ||
118
+ die "could not make a space for '$name'. Is Herdr open?"
119
+ say "Opened a space for '$name' in $dir"
120
+ fi
121
+ }
122
+
123
+ project_open() {
124
+ local name=${1:-} dir
125
+ dir=$(project_dir "$name")
126
+ require_herdr
127
+ sync_links "$dir"
128
+ show_space "$name" "$dir"
129
+ }
130
+
131
+ project_ls() {
132
+ local d any=0 repo
133
+ for d in "$PROJECTS"/*/; do
134
+ [ -d "$d" ] || continue
135
+ any=1
136
+ d=${d%/}
137
+ say "${d##*/}"
138
+ while IFS= read -r repo; do say " $repo"; done < <(project_repos "$d")
139
+ done
140
+ [ "$any" = 1 ] || say "No projects yet. Make one with: entourage project new <name>"
141
+ }
142
+
143
+ # Print what is wrong with each project; return how many problems there are.
144
+ check_projects() {
145
+ local d repo problems=0 bad
146
+ for d in "$PROJECTS"/*/; do
147
+ [ -d "$d" ] || continue
148
+ d=${d%/}
149
+ bad=0
150
+ if [ ! -f "$d/PROJECT.md" ]; then
151
+ say " problem ${d##*/}: PROJECT.md is missing"; bad=$((bad + 1))
152
+ elif grep -qF -- "$TEMPLATE_MARK" "$d/PROJECT.md"; then
153
+ say " problem ${d##*/}: PROJECT.md is still the template; fill it in and delete its first line"; bad=$((bad + 1))
154
+ fi
155
+ while IFS= read -r repo; do
156
+ is_repo "$repo" || { say " problem ${d##*/}: $repo is missing or is not a git repo"; bad=$((bad + 1)); }
157
+ done < <(project_repos "$d")
158
+ [ "$bad" -gt 0 ] || say " ok ${d##*/}"
159
+ # Notes say what a repo does not provide. They are not problems: a task
160
+ # in the project still starts, only without that step.
161
+ if have_just; then
162
+ while IFS= read -r repo; do
163
+ is_repo "$repo" || continue
164
+ case $(recipe_state "$repo" setup) in
165
+ no-justfile) say " note ${d##*/}: ${repo##*/} has no justfile, so tasks start without setup" ;;
166
+ no-recipe) say " note ${d##*/}: ${repo##*/}'s justfile has no setup recipe" ;;
167
+ unreadable) say " note ${d##*/}: ${repo##*/}'s justfile could not be read by just" ;;
168
+ esac
169
+ done < <(project_repos "$d")
170
+ fi
171
+ problems=$((problems + bad))
172
+ done
173
+ return "$problems"
174
+ }
175
+
176
+ cmd_project() {
177
+ case ${1:-} in
178
+ new) shift; project_new "$@" ;;
179
+ add) shift; project_add "$@" ;;
180
+ open) shift; project_open "$@" ;;
181
+ ls | list) shift; project_ls ;;
182
+ *) die "usage: entourage project new|add|open|ls (see: entourage help)" ;;
183
+ esac
184
+ }
@@ -0,0 +1,32 @@
1
+ # What a repo provides: recipes in a justfile at its root (https://just.systems).
2
+ # entourage knows recipes by name only, never what they do. This file is the
3
+ # only place that runs `just`.
4
+
5
+ have_just() { command -v just >/dev/null 2>&1; }
6
+
7
+ # Print the justfile at a folder's own root, or return 1 when it has none.
8
+ # `just` would also look in the folders above; a recipe from another project's
9
+ # justfile must never be run in a repo, so only the root counts.
10
+ root_justfile() {
11
+ local dir=$1 name
12
+ for name in justfile Justfile .justfile; do
13
+ if [ -f "$dir/$name" ]; then printf '%s\n' "$dir/$name"; return 0; fi
14
+ done
15
+ return 1
16
+ }
17
+
18
+ # Print whether a folder provides a recipe, as one word:
19
+ # ok its justfile has the recipe
20
+ # no-justfile it has no justfile at its root
21
+ # no-recipe its justfile has no recipe of that name
22
+ # unreadable just could not read its justfile
23
+ recipe_state() {
24
+ local dir=$1 recipe=$2 file names
25
+ file=$(root_justfile "$dir") || { echo no-justfile; return 0; }
26
+ # --summary prints the recipe names on one line, separated by spaces.
27
+ names=$(just --justfile "$file" --summary 2>/dev/null) || { echo unreadable; return 0; }
28
+ case " $(printf '%s' "$names" | tr '\n\t' ' ') " in
29
+ *" $recipe "*) echo ok ;;
30
+ *) echo no-recipe ;;
31
+ esac
32
+ }
@@ -0,0 +1,317 @@
1
+ # Tasks: a card an agent writes up, then a worktree of each repo on one
2
+ # branch. Sourced by cli/entourage.
3
+
4
+ # A task is one piece of work in a project, usually a Linear card. It has two
5
+ # commands, in the order they are used:
6
+ # task new an agent writes the card up, in a tab in the project's space
7
+ # task start a worktree of each repo on the card's branch, in a tab of its own
8
+
9
+ # A card ID is a team key and a number, like ABC-122.
10
+ CARD_RE='^[A-Za-z][A-Za-z0-9]*-[0-9]+$'
11
+ # The skills the agent writes a card up with, in the order it uses them.
12
+ CARD_SKILLS='sdlc-define sdlc-plan sdlc-breakdown'
13
+ # The agent's first message. {card} and {project} are filled in.
14
+ CARD_PROMPT=$REPO/prompts/task-new.md
15
+ # The agent a new task's tab runs: one word, a command on PATH.
16
+ AGENT=${ENTOURAGE_AGENT:-claude}
17
+
18
+ # Start writing a card up: a tab in the project's space, in the project's
19
+ # folder, running an agent whose first message names the card and the skills.
20
+ # It makes no branch, worktree or task folder; `task start` does that once the
21
+ # card has a type.
22
+ task_new() {
23
+ local name=${1:-} card=${2:-} dir skill prompt
24
+ dir=$(project_dir "$name")
25
+ [ -n "$card" ] ||
26
+ die "which card? Usage: entourage task new $name <card>, like: entourage task new $name ABC-122"
27
+ [[ $card =~ $CARD_RE ]] ||
28
+ die "'$card' is not a card ID like ABC-122. To make a worktree for a branch, use: entourage task start $name <type>/<description>"
29
+ card=$(printf '%s' "$card" | tr '[:lower:]' '[:upper:]')
30
+ # The agent runs in a tab, so Herdr has to be there before anything is opened.
31
+ require_herdr
32
+ herdr_running || die "Herdr is not running. Open it, then run this again."
33
+ command -v "$AGENT" >/dev/null 2>&1 ||
34
+ die "'$AGENT' is not on your PATH, so there is no agent to start. Install it, or set ENTOURAGE_AGENT to the one you use."
35
+ for skill in $CARD_SKILLS; do
36
+ [ -f "$SKILLS_HOME/$skill/SKILL.md" ] ||
37
+ die "the $skill skill is not in $SKILLS_HOME, so the agent could not use it. Run: entourage update"
38
+ done
39
+ [ -f "$CARD_PROMPT" ] || die "the agent's first message is missing from $CARD_PROMPT"
40
+ prompt=$(sed -e "s/{card}/$card/g" -e "s/{project}/$name/g" "$CARD_PROMPT")
41
+
42
+ sync_links "$dir"
43
+ (show_space "$name" "$dir") ||
44
+ die "could not show the space for '$name', so no agent was started."
45
+ # No `space` argument: the tab goes in the selected space, which show_space
46
+ # has just selected or made. argv is the agent and task is its first message.
47
+ herdr_app do tab.new "{\"name\":\"$card\",\"folder\":\"$(json_str "$dir")\",\"argv\":[\"$(json_str "$AGENT")\"],\"task\":\"$(json_str "$prompt")\"}" >/dev/null 2>&1 ||
48
+ die "could not open a tab for $card. Is Herdr open?"
49
+ say "Started $card in the space for '$name'"
50
+ say "The agent writes the card up, then asks you to approve it."
51
+ }
52
+
53
+ # `task start` makes a folder in a project's tasks/ folder, named after its
54
+ # branch's description, holding one git worktree per repo of the project:
55
+ # tasks/abc-122-short-title/api a worktree of api on feat/abc-122-short-title
56
+
57
+ # Branch names follow Conventional Branch 1.1.0 (https://conventionalbranch.org):
58
+ # <type>/<description>. Trunk names (main, master, develop) have no type, so
59
+ # they do not match.
60
+ BRANCH_RE='^(feature|feat|bugfix|fix|hotfix|release|chore|ai|claude|codex|copilot|cursor)/[a-z0-9]+(\.[a-z0-9]+)*(-[a-z0-9]+(\.[a-z0-9]+)*)*$'
61
+
62
+ valid_branch() { [[ ${1:-} =~ $BRANCH_RE ]]; }
63
+
64
+ # A task's name is its branch without the type.
65
+ task_name() { printf '%s\n' "${1#*/}"; }
66
+
67
+ has_origin() { git -C "$1" remote get-url origin >/dev/null 2>&1; }
68
+ resolves() { git -C "$1" rev-parse -q --verify "$2^{commit}" >/dev/null 2>&1; }
69
+
70
+ # Print the ref a repo's new task branch is cut from: the remote's default
71
+ # branch, or the local one in a repo with no origin.
72
+ base_ref() {
73
+ local repo=$1 ref
74
+ if has_origin "$repo"; then
75
+ ref=$(git -C "$repo" symbolic-ref -q --short refs/remotes/origin/HEAD 2>/dev/null) || ref=
76
+ for ref in ${ref:+"$ref"} origin/main origin/master; do
77
+ if resolves "$repo" "$ref"; then printf '%s\n' "$ref"; return; fi
78
+ done
79
+ fi
80
+ for ref in main master; do
81
+ if resolves "$repo" "refs/heads/$ref"; then printf '%s\n' "$ref"; return; fi
82
+ done
83
+ git -C "$repo" rev-parse --abbrev-ref HEAD
84
+ }
85
+
86
+ # What the current `task start` is in the middle of making, so that it can be
87
+ # removed if the run fails or is interrupted. Empty when nothing is half-made.
88
+ UNDO_DIR='' UNDO_BRANCH=''
89
+ MADE=() # the repos a worktree has been started in
90
+
91
+ # Remove a half-made task. The task folder and the branch did not exist before
92
+ # the run (task_start checks both), so this never removes someone's work.
93
+ task_undo() {
94
+ local tdir=$UNDO_DIR branch=$UNDO_BRANCH repo
95
+ [ -n "$tdir" ] || return 0
96
+ UNDO_DIR=''
97
+ for repo in ${MADE[@]+"${MADE[@]}"}; do
98
+ git -C "$repo" worktree remove --force "$tdir/${repo##*/}" >/dev/null 2>&1 || true
99
+ git -C "$repo" branch -q -D "$branch" >/dev/null 2>&1 || true
100
+ done
101
+ rm -f "$tdir/CLAUDE.md" "$tdir/AGENTS.md"
102
+ rmdir "$tdir" 2>/dev/null || true
103
+ rmdir "${tdir%/*}" 2>/dev/null || true
104
+ [ ! -e "$tdir" ] || printf 'entourage: could not remove everything in %s\n' "$tdir" >&2
105
+ }
106
+
107
+ # Get a new task ready to work in: type `just setup` into its tab's shell for
108
+ # every repo that provides the recipe (see recipes.sh). It runs there, not
109
+ # here, so that `task start` returns at once and the install is on screen
110
+ # where the work will happen. Nothing in this step can fail the task: by now
111
+ # it exists and its tab is open.
112
+ # $1 the task's folder $2 what `tab.new` printed $3... the task's repos
113
+ run_setup() {
114
+ local tdir=$1 tab=$2 repo name line='' term count ready=() groups i=0
115
+ shift 2
116
+ count=$#
117
+ if ! have_just; then
118
+ say " setup skipped: just is not installed (https://just.systems)"
119
+ return 0
120
+ fi
121
+ for repo in "$@"; do
122
+ name=${repo##*/}
123
+ # This repo's groups, if its target named any: handed to its recipe as is.
124
+ groups=${TASK_GROUPS[$i]:-}; i=$((i + 1))
125
+ case $(recipe_state "$tdir/$name" setup) in
126
+ ok)
127
+ ready+=("$name${groups:+ $groups}")
128
+ # One repo: the tab is already in its worktree. Several: the tab is in
129
+ # the task folder above them, and they run one after another. The ./
130
+ # keeps the shell's CDPATH, and a name starting with a dash, from
131
+ # sending cd somewhere else.
132
+ if [ "$count" -eq 1 ]; then line="just setup${groups:+ $groups}"
133
+ else line=${line:+$line && }"(cd ./$(sh_quote "$name") && just setup${groups:+ $groups})"
134
+ fi
135
+ ;;
136
+ no-justfile) say " setup $name skipped: no justfile" ;;
137
+ no-recipe) say " setup $name skipped: its justfile has no setup recipe" ;;
138
+ *) say " setup $name skipped: its justfile could not be read by just" ;;
139
+ esac
140
+ done
141
+ [ -n "$line" ] || return 0
142
+ # tab.new prints the address of the terminal it made, as "terminal": "t8".
143
+ term=$(printf '%s\n' "$tab" | sed -n 's/.*"terminal": *"\([^"]*\)".*/\1/p' | head -n 1) || term=
144
+ # hc types the line once the new shell has gone quiet: a second or two.
145
+ if [ -n "$term" ] && have_hc && hc_cmd send "$term" "$line" >/dev/null 2>&1; then
146
+ for name in "${ready[@]}"; do say " setup $name (running in the tab)"; done
147
+ return 0
148
+ fi
149
+ say " setup could not be started; run it yourself in the tab: $line"
150
+ }
151
+
152
+ # A group is a part of a repo, named by the repo itself: a plain word, because
153
+ # it is typed into the tab's shell. Lowercase letters, digits and dashes, not
154
+ # starting with a dash. Every allowed character is spelled out: a range like
155
+ # a-z means different things in different locales, and in some it lets
156
+ # uppercase letters and shell punctuation through.
157
+ valid_group() {
158
+ case ${1:-} in
159
+ '' | -* | *[!abcdefghijklmnopqrstuvwxyz0123456789-]*) return 1 ;;
160
+ esac
161
+ }
162
+
163
+ # What `task start` was asked to work on, from its targets:
164
+ # TARGET_REPOS the repo each target names
165
+ # TARGET_GROUPS that target's groups, separated by spaces ('' for the whole repo)
166
+ # and, once the task's repos are chosen, in the same order as them:
167
+ # TASK_GROUPS each repo's groups
168
+ TARGET_REPOS=() TARGET_GROUPS=() TASK_GROUPS=()
169
+
170
+ # Read targets of the form <repo>[:<group>[,<group>...]] into TARGET_REPOS and
171
+ # TARGET_GROUPS, or stop. entourage checks a group's shape only: what a group
172
+ # means, and whether it exists, is the repo's business.
173
+ # $1 the project $2... the project's repos, then --, then the targets
174
+ parse_targets() {
175
+ local name=$1 known=() arg repo groups group found r i
176
+ shift
177
+ while [ "$#" -gt 0 ] && [ "$1" != -- ]; do known+=("${1##*/}"); shift; done
178
+ shift
179
+ TARGET_REPOS=() TARGET_GROUPS=()
180
+ for arg in "$@"; do
181
+ repo=${arg%%:*}
182
+ found=0
183
+ for r in "${known[@]}"; do [ "$r" != "$repo" ] || found=1; done
184
+ [ "$found" = 1 ] ||
185
+ die "'$repo' is not a repo in project '$name'. Its repos are: ${known[*]}"
186
+ i=0
187
+ while [ "$i" -lt "${#TARGET_REPOS[@]}" ]; do
188
+ [ "${TARGET_REPOS[$i]}" != "$repo" ] ||
189
+ die "'$repo' is named twice. Give its groups together, separated by a comma, like: $repo:athlete,coach"
190
+ i=$((i + 1))
191
+ done
192
+ groups=''
193
+ case $arg in
194
+ *:*)
195
+ # Split on commas by hand: an empty group (a trailing or doubled
196
+ # comma) has to be caught, not skipped.
197
+ r=${arg#*:},
198
+ while [ -n "$r" ]; do
199
+ group=${r%%,*}; r=${r#*,}
200
+ valid_group "$group" ||
201
+ die "'$group' in '$arg' is not a group name. A group is lowercase letters, digits and dashes, like: $repo:athlete"
202
+ groups=${groups:+$groups }$group
203
+ done
204
+ ;;
205
+ esac
206
+ TARGET_REPOS+=("$repo"); TARGET_GROUPS+=("$groups")
207
+ done
208
+ }
209
+
210
+ task_start() {
211
+ local name=${1:-} branch=${2:-} dir task
212
+ dir=$(project_dir "$name")
213
+ [ -n "$branch" ] ||
214
+ die "which branch? Usage: entourage task start $name <type>/<description> [<repo>[:<group>[,<group>...]] ...] (see https://conventionalbranch.org)"
215
+ valid_branch "$branch" ||
216
+ die "'$branch' is not a branch name entourage accepts. Use <type>/<description>, like: feat/abc-122-short-title (see https://conventionalbranch.org)"
217
+ task=$(task_name "$branch")
218
+ # A task is shown as a tab, so Herdr has to be there before anything is made.
219
+ require_herdr
220
+ herdr_running || die "Herdr is not running. Open it, then run this again."
221
+
222
+ # Check everything before making anything.
223
+ local all=() repos=() bases=() repo base i tdir=$dir/tasks/$task
224
+ while IFS= read -r repo; do all+=("$repo"); done < <(project_repos "$dir")
225
+ [ "${#all[@]}" -gt 0 ] ||
226
+ die "project '$name' has no repos. Add one with: entourage project add $name <path>"
227
+ # The task's repos are the ones its targets name, or every repo when there
228
+ # are no targets, always in the order of the project's repos file.
229
+ shift 2
230
+ parse_targets "$name" "${all[@]}" -- "$@"
231
+ TASK_GROUPS=()
232
+ for repo in "${all[@]}"; do
233
+ if [ "${#TARGET_REPOS[@]}" -eq 0 ]; then
234
+ repos+=("$repo"); TASK_GROUPS+=('')
235
+ continue
236
+ fi
237
+ i=0
238
+ while [ "$i" -lt "${#TARGET_REPOS[@]}" ]; do
239
+ if [ "${TARGET_REPOS[$i]}" = "${repo##*/}" ]; then
240
+ repos+=("$repo"); TASK_GROUPS+=("${TARGET_GROUPS[$i]}")
241
+ fi
242
+ i=$((i + 1))
243
+ done
244
+ done
245
+ for repo in "${repos[@]}"; do
246
+ is_repo "$repo" || die "$repo is missing or is not a git repo (see: entourage status)"
247
+ done
248
+ [ ! -e "$tdir" ] || die "task '$task' already exists at $tdir"
249
+ # Fetch first, so that the branch check below sees what is on the remote
250
+ # and the task starts from the latest default branch.
251
+ for repo in "${repos[@]}"; do
252
+ has_origin "$repo" || continue
253
+ git -C "$repo" fetch -q origin 2>/dev/null ||
254
+ say " warning: could not fetch ${repo##*/}; starting from what is local"
255
+ done
256
+ for repo in "${repos[@]}"; do
257
+ if resolves "$repo" "refs/heads/$branch" || resolves "$repo" "refs/remotes/origin/$branch"; then
258
+ die "branch '$branch' already exists in ${repo##*/}"
259
+ fi
260
+ base=$(base_ref "$repo" 2>/dev/null) && resolves "$repo" "$base" ||
261
+ die "${repo##*/} has no commit to start a branch from"
262
+ bases+=("$base")
263
+ done
264
+ sync_links "$dir"
265
+
266
+ say "Making task '$task' in $tdir"
267
+ # From here until the worktrees and links are all made, any failure or
268
+ # interruption removes what was made.
269
+ MADE=() UNDO_DIR=$tdir UNDO_BRANCH=$branch
270
+ trap task_undo EXIT
271
+ trap 'exit 130' INT
272
+ trap 'exit 143' TERM
273
+ mkdir -p "$tdir"
274
+ i=0
275
+ for repo in "${repos[@]}"; do
276
+ base=${bases[$i]}; i=$((i + 1))
277
+ # Recorded before the attempt: a failed `worktree add` can leave its branch.
278
+ MADE+=("$repo")
279
+ # --no-track: the branch is new work, not a copy of the default branch, so
280
+ # a plain push or pull must not go to or from it.
281
+ # -f: reuse a path git still has registered for a folder deleted by hand.
282
+ git -C "$repo" worktree add -q -f --no-track -b "$branch" "$tdir/${repo##*/}" "$base" ||
283
+ die "could not make a worktree in ${repo##*/}; the task was not made"
284
+ say " worktree ${repo##*/} (from $base)"
285
+ done
286
+ # With several repos the session starts in the task folder, so the project's
287
+ # doc is linked there for an agent to read.
288
+ if [ "${#repos[@]}" -gt 1 ]; then
289
+ ln -s ../../PROJECT.md "$tdir/CLAUDE.md"
290
+ ln -s ../../PROJECT.md "$tdir/AGENTS.md"
291
+ fi
292
+ UNDO_DIR=''
293
+ trap - EXIT INT TERM
294
+
295
+ # One repo: the tab starts in its worktree, where git and the repo's own
296
+ # docs work as usual. Several: it starts in the task folder above them.
297
+ local folder=$tdir
298
+ [ "${#repos[@]}" -gt 1 ] || folder=$tdir/${repos[0]##*/}
299
+ # The task exists now. If Herdr cannot show it, keep it and say where it is.
300
+ (show_space "$name" "$dir") ||
301
+ die "made the task at $folder but could not show it in Herdr. Open that folder in the project's space yourself."
302
+ # No `space` argument: the tab goes in the selected space, which show_space
303
+ # has just selected or made.
304
+ local tab
305
+ tab=$(herdr_app do tab.new "{\"name\":\"$task\",\"folder\":\"$folder\"}" 2>/dev/null) ||
306
+ die "made the task at $folder but could not open its tab. Is Herdr open?"
307
+ run_setup "$tdir" "$tab" "${repos[@]}"
308
+ say "Opened task '$task' on branch $branch"
309
+ }
310
+
311
+ cmd_task() {
312
+ case ${1:-} in
313
+ new) shift; task_new "$@" ;;
314
+ start) shift; task_start "$@" ;;
315
+ *) die "usage: entourage task new <project> <card>, or: entourage task start <project> <branch> [<repo>[:<group>[,<group>...]] ...] (see: entourage help)" ;;
316
+ esac
317
+ }