@johngaynor/ocnus 0.0.0-stage → 2.0.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.
- package/LICENSE +21 -0
- package/README.md +988 -2
- package/agents/linear-mcp.json +1 -0
- package/agents/ocnus-build.md +23 -0
- package/agents/ocnus-write-up.md +20 -0
- package/cli/host-mark +36 -0
- package/cli/lib/agents.sh +350 -0
- package/cli/lib/approval.sh +94 -0
- package/cli/lib/archive.sh +447 -0
- package/cli/lib/billing.sh +166 -0
- package/cli/lib/close.sh +189 -0
- package/cli/lib/commands.sh +220 -0
- package/cli/lib/common.sh +215 -0
- package/cli/lib/finish.sh +288 -0
- package/cli/lib/github.sh +162 -0
- package/cli/lib/guard.sh +134 -0
- package/cli/lib/handoff.sh +551 -0
- package/cli/lib/host.sh +1802 -0
- package/cli/lib/linear.sh +1389 -0
- package/cli/lib/links.sh +135 -0
- package/cli/lib/merges.sh +853 -0
- package/cli/lib/recipes.sh +54 -0
- package/cli/lib/settings.sh +465 -0
- package/cli/lib/space.sh +343 -0
- package/cli/lib/task.sh +847 -0
- package/cli/lib/tests.sh +268 -0
- package/cli/lib/trust.sh +70 -0
- package/cli/ocnus +630 -0
- package/herdr/README.md +2238 -0
- package/herdr/layouts/default.json +259 -0
- package/herdr/mods/agent-settings/cog.svg +4 -0
- package/herdr/mods/agent-settings/main.js +356 -0
- package/herdr/mods/agent-settings/mod.json +1 -0
- package/herdr/mods/cards/main.js +450 -0
- package/herdr/mods/cards/mod.json +1 -0
- package/herdr/mods/cards/refresh.svg +4 -0
- package/herdr/mods/cpu/main.js +90 -0
- package/herdr/mods/cpu/mod.json +1 -0
- package/herdr/mods/layout-switch/main.js +48 -0
- package/herdr/mods/layout-switch/mod.json +1 -0
- package/herdr/mods/names/main.js +69 -0
- package/herdr/mods/names/mod.json +1 -0
- package/herdr/mods/ocnus-spaces/main.js +376 -0
- package/herdr/mods/ocnus-spaces/mod.json +1 -0
- package/herdr/mods/steps/main.js +354 -0
- package/herdr/mods/steps/mod.json +1 -0
- package/herdr/spaces/template/SPACE.md +19 -0
- package/package.json +27 -4
- package/prompts/task-build.md +11 -0
- package/prompts/task-new.md +17 -0
- package/skills/brief/SKILL.md +64 -0
- package/skills/sdlc-breakdown/SKILL.md +312 -0
- package/skills/sdlc-build/SKILL.md +182 -0
- package/skills/sdlc-define/SKILL.md +203 -0
- package/skills/sdlc-plan/SKILL.md +202 -0
- package/skills/sdlc-pr/SKILL.md +339 -0
- package/skills/sdlc-review/SKILL.md +227 -0
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
# Finishing a task: once its work has merged, remove what `task start` made
|
|
2
|
+
# for it. Sourced by cli/ocnus.
|
|
3
|
+
|
|
4
|
+
# `task finish` removes a task's worktrees, its branch in each repo and its
|
|
5
|
+
# folder, then closes its tab, or with the Herdr app closed ends the task's
|
|
6
|
+
# terminals in herdr-core. Like `task start`, it checks everything before
|
|
7
|
+
# it changes anything, and here the checks are what keep work safe. It
|
|
8
|
+
# removes nothing when that would lose work, and names every problem in every
|
|
9
|
+
# repo of the task in one go. What would be lost:
|
|
10
|
+
# - changes that are not committed: staged, unstaged or untracked;
|
|
11
|
+
# - commits on the branch that are not in the remote's default branch, even
|
|
12
|
+
# pushed ones, such as a fix pushed after the pull request merged;
|
|
13
|
+
# - ignored files that the repo's setup does not make again (setup_makes,
|
|
14
|
+
# in recipes.sh), which `git worktree remove` deletes without a word;
|
|
15
|
+
# - anything in the task folder outside its worktrees.
|
|
16
|
+
# scripts/after-merge makes the same checks for Claude Code's own worktrees.
|
|
17
|
+
# A task that would lose work is still removed by hand.
|
|
18
|
+
|
|
19
|
+
# Lines on stdin, indented under a heading: at most 20, then how many more.
|
|
20
|
+
listed() { awk 'NR <= 20 { print " " $0 } END { if (NR > 20) print " and " NR - 20 " more" }'; }
|
|
21
|
+
|
|
22
|
+
# Print the folder of a card's task, or stop when it has none on this
|
|
23
|
+
# machine: tasks/<card>-<short name>, or tasks/<card> for a title with no
|
|
24
|
+
# plain words, as `task build` finds it. Linear is not asked.
|
|
25
|
+
# $1 the space's folder $2 the card, in upper case
|
|
26
|
+
task_folder() {
|
|
27
|
+
local dir=$1 card=$2 low f found=()
|
|
28
|
+
low=$(printf '%s' "$card" | tr '[:upper:]' '[:lower:]')
|
|
29
|
+
for f in "$dir/tasks/$low" "$dir/tasks/$low-"*; do
|
|
30
|
+
[ ! -d "$f" ] || found+=("$f")
|
|
31
|
+
done
|
|
32
|
+
case ${#found[@]} in
|
|
33
|
+
0) die "$card has no task on this machine: there is no folder for it in $dir/tasks. Nothing was changed." ;;
|
|
34
|
+
1) printf '%s\n' "${found[0]}" ;;
|
|
35
|
+
*) die "$card has more than one task folder in $dir/tasks: ${found[*]##*/}. Remove the ones that are not its task by hand, then run this again. Nothing was changed." ;;
|
|
36
|
+
esac
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
# Print git's record of a worktree, from `git worktree list --porcelain` in
|
|
40
|
+
# its repo: its worktree and HEAD lines, branch or detached, and locked when
|
|
41
|
+
# it is locked. Nothing when git has no record of it.
|
|
42
|
+
# $1 the repo $2 the worktree's real path
|
|
43
|
+
worktree_record() {
|
|
44
|
+
local repo=$1 want=$2 line on=0
|
|
45
|
+
while IFS= read -r line; do
|
|
46
|
+
case $line in
|
|
47
|
+
'worktree '*)
|
|
48
|
+
on=0
|
|
49
|
+
[ "$(cd -P "${line#worktree }" 2>/dev/null && pwd)" != "$want" ] || on=1
|
|
50
|
+
;;
|
|
51
|
+
esac
|
|
52
|
+
[ "$on" = 0 ] || printf '%s\n' "$line"
|
|
53
|
+
done < <(git -C "$repo" worktree list --porcelain 2>/dev/null)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
# What check_worktree found: the problems, as lines under the worktree's name,
|
|
57
|
+
# and the repo, branch and commit to remove once there are none.
|
|
58
|
+
FOUND='' WT_REPO='' WT_BRANCH='' WT_HEAD=''
|
|
59
|
+
add_problem() { FOUND=$FOUND$*$'\n'; }
|
|
60
|
+
add_list() { FOUND=$FOUND$(listed)$'\n'; }
|
|
61
|
+
|
|
62
|
+
# Check one worktree of a task for what removing it would lose. It changes
|
|
63
|
+
# nothing but the repo's copy of origin, which it fetches.
|
|
64
|
+
# $1 the worktree $2 the task's name $3 a folder for the checks' files
|
|
65
|
+
check_worktree() {
|
|
66
|
+
local wt=$1 task=$2 scratch=$3 common record out base why
|
|
67
|
+
FOUND='' WT_REPO='' WT_BRANCH='' WT_HEAD=''
|
|
68
|
+
# git names the worktree's repo. A .git file that leads nowhere names none.
|
|
69
|
+
common=$(cd "$wt" 2>/dev/null && c=$(git rev-parse --git-common-dir 2>/dev/null) && [ -n "$c" ] &&
|
|
70
|
+
cd "$c" 2>/dev/null && pwd -P) || common=
|
|
71
|
+
if [ -z "$common" ]; then
|
|
72
|
+
add_problem " git cannot read it as a worktree, so what removing it would lose is not known"
|
|
73
|
+
return 0
|
|
74
|
+
fi
|
|
75
|
+
WT_REPO=$common
|
|
76
|
+
[ "${common##*/}" != .git ] || WT_REPO=${common%/.git}
|
|
77
|
+
record=$(worktree_record "$WT_REPO" "$(cd -P "$wt" && pwd)")
|
|
78
|
+
if [ -z "$record" ]; then
|
|
79
|
+
add_problem " $WT_REPO has no record of it as a worktree, so what removing it would lose is not known"
|
|
80
|
+
return 0
|
|
81
|
+
fi
|
|
82
|
+
WT_HEAD=$(printf '%s\n' "$record" | sed -n 's/^HEAD //p')
|
|
83
|
+
WT_BRANCH=$(printf '%s\n' "$record" | sed -n 's|^branch refs/heads/||p')
|
|
84
|
+
|
|
85
|
+
if printf '%s\n' "$record" | grep -q '^locked\( \|$\)'; then
|
|
86
|
+
why=$(printf '%s\n' "$record" | sed -n 's/^locked \{0,1\}//p')
|
|
87
|
+
add_problem " it is locked${why:+ ($why)}: something may still be working in it"
|
|
88
|
+
fi
|
|
89
|
+
if [ -z "$WT_BRANCH" ]; then
|
|
90
|
+
add_problem " it is not on a branch, so it is not on the task's branch"
|
|
91
|
+
elif ! valid_branch "$WT_BRANCH" || [ "$(task_name "$WT_BRANCH")" != "$task" ]; then
|
|
92
|
+
add_problem " it is on the branch $WT_BRANCH, not on the task's branch"
|
|
93
|
+
fi
|
|
94
|
+
|
|
95
|
+
# Every commit up to the worktree's HEAD has to be in the remote's default
|
|
96
|
+
# branch: ancestry, since pull requests here merge with a merge commit. A
|
|
97
|
+
# branch merged by squash or rebase is refused, which loses nothing. A fetch
|
|
98
|
+
# that fails leaves what is local, which can only refuse more.
|
|
99
|
+
if has_origin "$WT_REPO" && ! git -C "$WT_REPO" fetch -q origin 2>/dev/null; then
|
|
100
|
+
say " warning: could not fetch ${wt##*/}; checking it against what is local"
|
|
101
|
+
fi
|
|
102
|
+
base=$(base_ref "$WT_REPO" 2>/dev/null) || base=
|
|
103
|
+
if [ -z "$base" ] || [ "$base" = "$WT_BRANCH" ] || ! resolves "$WT_REPO" "$base"; then
|
|
104
|
+
add_problem " it has no default branch to check the branch's commits against"
|
|
105
|
+
elif ! git -C "$WT_REPO" merge-base --is-ancestor "$WT_HEAD" "$base" 2>/dev/null; then
|
|
106
|
+
out=$(git -C "$WT_REPO" log --oneline "$base..$WT_HEAD" 2>&1) || true
|
|
107
|
+
add_problem " commits that are not in $base:"
|
|
108
|
+
add_list <<< "$out"
|
|
109
|
+
fi
|
|
110
|
+
|
|
111
|
+
# Changed, staged and untracked files, each one by name.
|
|
112
|
+
if ! out=$(git -C "$wt" status --porcelain --untracked-files=all 2>&1); then
|
|
113
|
+
add_problem " git status failed, so what is not committed is not known:"
|
|
114
|
+
add_list <<< "$out"
|
|
115
|
+
elif [ -n "$out" ]; then
|
|
116
|
+
add_problem " changes that are not committed:"
|
|
117
|
+
add_list <<< "$out"
|
|
118
|
+
fi
|
|
119
|
+
|
|
120
|
+
# Ignored files, file by file: each counts as work unless it is, or is in,
|
|
121
|
+
# something the patterns that setup_makes prints match. git quotes an odd
|
|
122
|
+
# name the same way in both lists, so they can be compared line by line.
|
|
123
|
+
if ! git -C "$wt" ls-files --others --ignored --exclude-standard > "$scratch/ignored" 2>/dev/null; then
|
|
124
|
+
add_problem " git could not list its ignored files, so what removing it would lose is not known"
|
|
125
|
+
elif [ -s "$scratch/ignored" ]; then
|
|
126
|
+
why=''
|
|
127
|
+
if setup_makes "$wt" > "$scratch/patterns" 2> "$scratch/why"; then
|
|
128
|
+
git -C "$wt" ls-files --others --ignored --exclude-from="$scratch/patterns" > "$scratch/made" 2>/dev/null ||
|
|
129
|
+
{ why="git could not read what its made-by-setup recipe printed"; : > "$scratch/made"; }
|
|
130
|
+
else
|
|
131
|
+
why=$(head -n 1 "$scratch/why"); : > "$scratch/made"
|
|
132
|
+
fi
|
|
133
|
+
out=$(awk 'FILENAME == ARGV[1] { made[$0]; next } !($0 in made)' "$scratch/made" "$scratch/ignored")
|
|
134
|
+
if [ -n "$out" ]; then
|
|
135
|
+
if [ -n "$why" ]; then add_problem " ignored files, all of them counted as work since $why:"
|
|
136
|
+
else add_problem " ignored files that its setup does not make again:"
|
|
137
|
+
fi
|
|
138
|
+
add_list <<< "$out"
|
|
139
|
+
fi
|
|
140
|
+
fi
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
# Close a tab of a space: the one of that name, in the space, while the Herdr
|
|
144
|
+
# app answers; otherwise its terminals in herdr-core (end_task_terminals).
|
|
145
|
+
# `task finish` closes a task's tab with it, and `task close` a card's
|
|
146
|
+
# write-up tab (close.sh). It is the last thing `task finish` does, because
|
|
147
|
+
# run in that tab, closing it ends the command. No `confirmed`, so Herdr asks
|
|
148
|
+
# first when it is the space's last tab, since closing that one closes the
|
|
149
|
+
# space.
|
|
150
|
+
# $1 the space $2 its folder $3 the tab's name
|
|
151
|
+
close_task_tab() {
|
|
152
|
+
local name=$1 dir=$2 tab=$3 id
|
|
153
|
+
if ! herdr_running; then
|
|
154
|
+
end_task_terminals "$name" "$tab"
|
|
155
|
+
return 0
|
|
156
|
+
fi
|
|
157
|
+
if ! command -v jq >/dev/null 2>&1; then
|
|
158
|
+
say " tab not closed: jq is not installed, so the tab could not be found. Close the tab $tab yourself."
|
|
159
|
+
return 0
|
|
160
|
+
fi
|
|
161
|
+
id=$(herdr_app ls 2>/dev/null | jq -r --arg dir "$dir" --arg tab "$tab" \
|
|
162
|
+
'first(.spaces[]? | select(.folder == $dir) | .tabs[]? | select(.name == $tab) | .id | strings) // empty' \
|
|
163
|
+
2>/dev/null) || id=
|
|
164
|
+
if [ -z "$id" ]; then
|
|
165
|
+
say " tab none to close: no tab named $tab is open in the space for '$name'"
|
|
166
|
+
return 0
|
|
167
|
+
fi
|
|
168
|
+
say " tab closing $tab"
|
|
169
|
+
# Not in a subshell: run in this tab, the command ends here.
|
|
170
|
+
herdr_app do tab.close "{\"id\":\"$(json_str "$id")\"}" >/dev/null 2>&1 ||
|
|
171
|
+
say " tab could not close $tab. Close it yourself."
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
# End a tab's terminals in herdr-core, for when the Herdr app does not answer:
|
|
175
|
+
# each live terminal labelled with the space and with the tab's name, as
|
|
176
|
+
# `task new` and `task start` label the ones they start. The terminal the
|
|
177
|
+
# command runs in, $HC_TERMINAL_ID, is ended last, since ending it ends the
|
|
178
|
+
# command. With quiet, it says nothing when none is labelled for the tab, as
|
|
179
|
+
# when the app has just closed the tab.
|
|
180
|
+
# $1 the space $2 the tab's name $3 quiet, or nothing
|
|
181
|
+
end_task_terminals() {
|
|
182
|
+
local name=$1 tab=$2 quiet=${3:-} out t own='' terms=()
|
|
183
|
+
if ! have_hc; then
|
|
184
|
+
say " tab not closed: hc is not installed, so the tab's terminals could not be found. Close the tab $tab yourself."
|
|
185
|
+
return 0
|
|
186
|
+
fi
|
|
187
|
+
if ! command -v jq >/dev/null 2>&1; then
|
|
188
|
+
say " tab not closed: jq is not installed, so the tab's terminals could not be found. Close the tab $tab yourself."
|
|
189
|
+
return 0
|
|
190
|
+
fi
|
|
191
|
+
if ! out=$(hc_cmd ls --json --label "ocnus.space=$name" --label "ocnus.tab=$tab" 2>&1); then
|
|
192
|
+
out=$(printf '%s' "$out" | tr -s '\n' ' ')
|
|
193
|
+
say " tab not closed: herdr-core could not list the tab's terminals${out:+ (${out% })}. Close the tab $tab yourself."
|
|
194
|
+
return 0
|
|
195
|
+
fi
|
|
196
|
+
while IFS= read -r t; do
|
|
197
|
+
if [ "$t" = "${HC_TERMINAL_ID:-}" ]; then own=$t
|
|
198
|
+
elif [ -n "$t" ]; then terms+=("$t")
|
|
199
|
+
fi
|
|
200
|
+
done < <(printf '%s' "$out" | jq -r '.terminals[]? | .address // empty' 2>/dev/null)
|
|
201
|
+
[ -z "$own" ] || terms+=("$own")
|
|
202
|
+
if [ "${#terms[@]}" -eq 0 ]; then
|
|
203
|
+
[ -n "$quiet" ] || say " tab none to close: no terminal in herdr-core is labelled for $tab in '$name'"
|
|
204
|
+
return 0
|
|
205
|
+
fi
|
|
206
|
+
for t in "${terms[@]}"; do
|
|
207
|
+
say " tab ending $t in herdr-core"
|
|
208
|
+
# Not in a subshell: run in this terminal, the command ends here.
|
|
209
|
+
hc_cmd kill "$t" >/dev/null 2>&1 || say " tab could not end $t. End it yourself: hc kill $t"
|
|
210
|
+
done
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
task_finish() {
|
|
214
|
+
local name=${1:-} card=${2:-} dir tdir task
|
|
215
|
+
dir=$(space_dir "$name")
|
|
216
|
+
[ -n "$card" ] ||
|
|
217
|
+
die "which card? Usage: ocnus task finish $name <card>, like: ocnus task finish $name ABC-122"
|
|
218
|
+
[[ $card =~ $CARD_RE ]] || die "'$card' is not a card ID like ABC-122."
|
|
219
|
+
[ "$#" -le 2 ] || die "usage: ocnus task finish <space> <card>"
|
|
220
|
+
card=$(printf '%s' "$card" | tr '[:lower:]' '[:upper:]')
|
|
221
|
+
tdir=$(task_folder "$dir" "$card") || exit 1
|
|
222
|
+
task=${tdir##*/}
|
|
223
|
+
|
|
224
|
+
# What the task folder holds: a worktree of each repo, and with several
|
|
225
|
+
# repos the links to the space's doc. Anything else, hidden or not, would
|
|
226
|
+
# be lost with the folder.
|
|
227
|
+
local f wts=() others=()
|
|
228
|
+
for f in "$tdir"/* "$tdir"/.[!.]* "$tdir"/..?*; do
|
|
229
|
+
[ -e "$f" ] || [ -L "$f" ] || continue
|
|
230
|
+
if [ -f "$f/.git" ] && [ ! -L "$f" ]; then wts+=("$f"); continue; fi
|
|
231
|
+
case ${f##*/} in CLAUDE.md | AGENTS.md) [ ! -L "$f" ] || continue ;; esac
|
|
232
|
+
others+=("${f##*/}")
|
|
233
|
+
done
|
|
234
|
+
|
|
235
|
+
# Check everything before removing anything.
|
|
236
|
+
local scratch wt problems='' repos=() branches=() heads=()
|
|
237
|
+
scratch=$(mktemp -d) || die "could not make a folder for the checks. Nothing was changed."
|
|
238
|
+
trap "rm -rf $(sh_quote "$scratch")" EXIT
|
|
239
|
+
trap 'exit 130' INT
|
|
240
|
+
trap 'exit 143' TERM
|
|
241
|
+
for wt in ${wts[@]+"${wts[@]}"}; do
|
|
242
|
+
check_worktree "$wt" "$task" "$scratch"
|
|
243
|
+
[ -z "$FOUND" ] || problems=$problems"${wt##*/}, in ${wt#"$dir"/}:"$'\n'$FOUND
|
|
244
|
+
repos+=("$WT_REPO"); branches+=("$WT_BRANCH"); heads+=("$WT_HEAD")
|
|
245
|
+
done
|
|
246
|
+
if [ "${#others[@]}" -gt 0 ]; then
|
|
247
|
+
problems=$problems"the task folder, ${tdir#"$dir"/}, outside its worktrees:"$'\n'
|
|
248
|
+
problems=$problems$(printf '%s\n' "${others[@]}" | listed)$'\n'
|
|
249
|
+
fi
|
|
250
|
+
rm -rf "$scratch"
|
|
251
|
+
trap - EXIT INT TERM
|
|
252
|
+
if [ -n "$problems" ]; then
|
|
253
|
+
printf "Task '%s' was not finished: finishing it would lose this.\n%s" "$task" "$problems" >&2
|
|
254
|
+
die "nothing was removed. Keep that work first: commit it, push it and merge it into the default branch, or move it out of the task. Then run this again. To throw it away instead, remove the task by hand (see \"Finishing a task\" in herdr/README.md)."
|
|
255
|
+
fi
|
|
256
|
+
|
|
257
|
+
# The command may be running in a worktree it is about to remove.
|
|
258
|
+
cd "$dir"
|
|
259
|
+
say "Finishing task '$task' in $tdir"
|
|
260
|
+
local i=0 repo branch head
|
|
261
|
+
for wt in ${wts[@]+"${wts[@]}"}; do
|
|
262
|
+
repo=${repos[$i]} branch=${branches[$i]} head=${heads[$i]}
|
|
263
|
+
i=$((i + 1))
|
|
264
|
+
# No --force: git's own refusal, for changes made since the checks, stays
|
|
265
|
+
# as a second safety net.
|
|
266
|
+
git -C "$repo" worktree remove "$wt" ||
|
|
267
|
+
die "could not remove the worktree $wt (git says why above). Everything listed before this was removed; the rest of the task is as it was."
|
|
268
|
+
say " worktree ${wt##*/} removed"
|
|
269
|
+
# -D, since the check above is what keeps the branch's work safe: -d
|
|
270
|
+
# passes any branch whose upstream has it. Only while the branch is still
|
|
271
|
+
# on the commit that was checked.
|
|
272
|
+
if [ "$(git -C "$repo" rev-parse -q --verify "refs/heads/$branch" 2>/dev/null)" = "$head" ] &&
|
|
273
|
+
git -C "$repo" branch -q -D "$branch" 2>/dev/null; then
|
|
274
|
+
say " branch $branch deleted in ${wt##*/}"
|
|
275
|
+
else
|
|
276
|
+
say " branch $branch kept in ${wt##*/}: it moved after it was checked, or git would not delete it"
|
|
277
|
+
fi
|
|
278
|
+
done
|
|
279
|
+
for f in CLAUDE.md AGENTS.md; do
|
|
280
|
+
[ ! -L "$tdir/$f" ] || rm -f "$tdir/$f"
|
|
281
|
+
done
|
|
282
|
+
# rmdir, never rm -rf: what is still there was not checked.
|
|
283
|
+
rmdir "$tdir" 2>/dev/null ||
|
|
284
|
+
die "removed the task's worktrees, but not its folder $tdir: something was put in it after the checks. Look at what is there, then remove it yourself."
|
|
285
|
+
say " folder ${tdir#"$dir"/} removed"
|
|
286
|
+
say "Finished task '$task'"
|
|
287
|
+
close_task_tab "$name" "$dir" "$task"
|
|
288
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# Asking GitHub about the pull requests of the cards that wait on their
|
|
2
|
+
# merge. Sourced by cli/ocnus.
|
|
3
|
+
#
|
|
4
|
+
# ocnus's watch on the merges (merges.sh) asks GitHub about the pull
|
|
5
|
+
# requests of every card that waits on its merge at once, in one GraphQL call
|
|
6
|
+
# a pass, with `gh api graphql` (github_pulls), rather than with one `gh pr
|
|
7
|
+
# list` in each worktree of each card. So while cards wait on their merges,
|
|
8
|
+
# GitHub is asked once a minute, however many cards wait, in however many
|
|
9
|
+
# spaces and repos. The call names each worktree's repo, from its origin
|
|
10
|
+
# (github_repo), and its branch, and asks for that branch's newest pull
|
|
11
|
+
# request. Its answer is read by these rules (GITHUB_READ): only the newest
|
|
12
|
+
# pull request counts, an open one that GitHub says conflicts with its
|
|
13
|
+
# default branch is conflict, and one whose mergeable is still UNKNOWN,
|
|
14
|
+
# while GitHub works that out, is open. Asking
|
|
15
|
+
# GitHub sits here alone, so that another host, such as GitLab, can sit
|
|
16
|
+
# beside it later.
|
|
17
|
+
#
|
|
18
|
+
# `gh search prs` asks across repos too, but it cannot say whether an open
|
|
19
|
+
# pull request is in conflict.
|
|
20
|
+
|
|
21
|
+
# The most worktrees one call asks about. More are asked about in calls of
|
|
22
|
+
# this many, so the common case stays one call.
|
|
23
|
+
GITHUB_BATCH=100
|
|
24
|
+
|
|
25
|
+
# A worktree's repo on GitHub, from the URL of its origin as git keeps it,
|
|
26
|
+
# before any url.<base>.insteadOf rewrites it:
|
|
27
|
+
# https://github.com/<owner>/<name> or git@github.com:<owner>/<name>, each
|
|
28
|
+
# with or without .git. It sets GITHUB_OWNER and GITHUB_NAME, or fails with
|
|
29
|
+
# why in GITHUB_WHY: the worktree has no origin, or its origin is not on
|
|
30
|
+
# GitHub.
|
|
31
|
+
# github_repo <worktree>
|
|
32
|
+
GITHUB_OWNER='' GITHUB_NAME='' GITHUB_WHY=''
|
|
33
|
+
github_repo() {
|
|
34
|
+
local url re='^(https://github\.com/|git@github\.com:)([^/[:space:]]+)/([^/[:space:]]+)/?$'
|
|
35
|
+
GITHUB_OWNER='' GITHUB_NAME='' GITHUB_WHY=''
|
|
36
|
+
url=$(git -C "$1" config --get remote.origin.url 2>/dev/null) || url=''
|
|
37
|
+
if [ -z "$url" ]; then
|
|
38
|
+
GITHUB_WHY='it has no origin, so GitHub cannot be asked about it'
|
|
39
|
+
return 1
|
|
40
|
+
fi
|
|
41
|
+
if [[ $url =~ $re ]] && [ -n "${BASH_REMATCH[3]%.git}" ]; then
|
|
42
|
+
GITHUB_OWNER=${BASH_REMATCH[2]} GITHUB_NAME=${BASH_REMATCH[3]%.git}
|
|
43
|
+
return 0
|
|
44
|
+
fi
|
|
45
|
+
GITHUB_WHY="its origin, $url, is not on GitHub"
|
|
46
|
+
return 1
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
# GitHub's answer to github_ask's call, read as one line for each branch it
|
|
50
|
+
# asked about, p0 to p<$n - 1>, in order: merged, open, closed or conflict,
|
|
51
|
+
# then the pull request's URL; none when the branch has none; or failed and
|
|
52
|
+
# GitHub's words when it answered that repo with an error, such as one it
|
|
53
|
+
# cannot find. A closed pull request is closed, whatever GitHub says of its
|
|
54
|
+
# conflicts. An answer that is empty, or holds no data, is an error, so that
|
|
55
|
+
# the whole call counts as failed.
|
|
56
|
+
GITHUB_READ='input
|
|
57
|
+
| if (.data | type) != "object" then error("no data") else . end
|
|
58
|
+
| . as $all
|
|
59
|
+
| ($all.errors // []) as $errors
|
|
60
|
+
| range(0; $n) | "p\(.)" as $alias
|
|
61
|
+
| ([$errors[] | select((.path // [])[0] == $alias) | .message] | first) as $own
|
|
62
|
+
| $all.data[$alias] as $repo
|
|
63
|
+
| if $own != null then "failed \($own)"
|
|
64
|
+
elif $repo == null then "failed \(([$errors[] | .message] | first) // "GitHub gave no answer for it")"
|
|
65
|
+
else ($repo.pullRequests.nodes // [])[0] as $pr
|
|
66
|
+
| if $pr == null then "none"
|
|
67
|
+
elif $pr.state == "MERGED" then "merged \($pr.url)"
|
|
68
|
+
elif $pr.state == "CLOSED" then "closed \($pr.url)"
|
|
69
|
+
elif $pr.state == "OPEN" and $pr.mergeable == "CONFLICTING" then "conflict \($pr.url)"
|
|
70
|
+
else "open \($pr.url)" end
|
|
71
|
+
end
|
|
72
|
+
| split("\r") | join(" ") | split("\n") | join(" ")'
|
|
73
|
+
|
|
74
|
+
# Whether gh needs signing in, as the last github_pulls found: 1 when a call
|
|
75
|
+
# failed because gh is signed out, or GitHub refused its token.
|
|
76
|
+
GITHUB_SIGN_IN=0
|
|
77
|
+
# gh's words when a call of the last github_pulls failed as a whole, the
|
|
78
|
+
# first such call's, or empty when none did. A repo that GitHub answered with
|
|
79
|
+
# an error is not a call that failed.
|
|
80
|
+
GITHUB_CALL_WHY=''
|
|
81
|
+
|
|
82
|
+
# Ask GitHub for the newest pull request of each branch given, in one call,
|
|
83
|
+
# or in calls of GITHUB_BATCH when more are given. Each line on stdin is a
|
|
84
|
+
# key, which holds no space, the worktree, and its branch, with the unit
|
|
85
|
+
# separator between them. It prints a line for each, in the same order:
|
|
86
|
+
# <key> merged|open|closed|conflict <the pull request's URL>
|
|
87
|
+
# <key> none the branch has no pull request
|
|
88
|
+
# <key> failed <the worktree's folder>: <why>
|
|
89
|
+
# A call that fails as a whole, such as with GitHub out of reach, fails
|
|
90
|
+
# every line it asked about, with gh's words, which GITHUB_CALL_WHY keeps.
|
|
91
|
+
# GITHUB_SIGN_IN is 1 then when gh needs signing in: it exits 4 when it has
|
|
92
|
+
# no sign-in (gh help exit-codes), and names HTTP 401 when GitHub refuses its
|
|
93
|
+
# token. A repo that GitHub answers with an error fails only its own lines,
|
|
94
|
+
# with GitHub's words, and a worktree whose origin is not on GitHub is not
|
|
95
|
+
# asked about.
|
|
96
|
+
# github_pulls <a folder for gh's answers>
|
|
97
|
+
github_pulls() {
|
|
98
|
+
local scratch=$1 sep=$'\x1f' key wt branch keys=() wts=() lines=() asks=() i n
|
|
99
|
+
GITHUB_SIGN_IN=0 GITHUB_CALL_WHY=''
|
|
100
|
+
while IFS=$sep read -r key wt branch; do
|
|
101
|
+
[ -n "$key" ] || continue
|
|
102
|
+
i=${#keys[@]}
|
|
103
|
+
keys+=("$key") wts+=("$wt") lines+=('')
|
|
104
|
+
if github_repo "$wt"; then
|
|
105
|
+
asks+=("$i$sep$GITHUB_OWNER$sep$GITHUB_NAME$sep$branch")
|
|
106
|
+
else
|
|
107
|
+
lines[i]="failed ${wt##*/}: $GITHUB_WHY"
|
|
108
|
+
fi
|
|
109
|
+
done
|
|
110
|
+
[ "${#keys[@]}" -gt 0 ] || return 0
|
|
111
|
+
for ((n = 0; n < ${#asks[@]}; n += GITHUB_BATCH)); do
|
|
112
|
+
github_ask "$scratch" "${asks[@]:n:GITHUB_BATCH}"
|
|
113
|
+
done
|
|
114
|
+
for ((i = 0; i < ${#keys[@]}; i++)); do
|
|
115
|
+
printf '%s %s\n' "${keys[i]}" "${lines[i]}"
|
|
116
|
+
done
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
# One call to GitHub, for the branches given, as github_pulls's asks: the
|
|
120
|
+
# line's number, the repo's owner, its name and the branch, with the unit
|
|
121
|
+
# separator between them. Each branch is a repository() of its own in the
|
|
122
|
+
# query, p0, p1 and so on, with its owner, name and branch passed as
|
|
123
|
+
# variables, so that nothing in them is read as GraphQL. It sets
|
|
124
|
+
# github_pulls's line for each, in its lines, reading its wts for the
|
|
125
|
+
# worktree's folder, and GITHUB_SIGN_IN and GITHUB_CALL_WHY when the call
|
|
126
|
+
# fails as a whole.
|
|
127
|
+
# github_ask <a folder for gh's answers> <ask>...
|
|
128
|
+
github_ask() {
|
|
129
|
+
local scratch=$1 sep=$'\x1f' ask i owner name branch n=0 at=() args=() vars='' body='' code=0 why answer line
|
|
130
|
+
shift
|
|
131
|
+
for ask in "$@"; do
|
|
132
|
+
IFS=$sep read -r i owner name branch <<< "$ask"
|
|
133
|
+
at+=("$i")
|
|
134
|
+
lines[i]="failed ${wts[i]##*/}: its answer could not be read"
|
|
135
|
+
vars="$vars, \$o$n: String!, \$n$n: String!, \$b$n: String!"
|
|
136
|
+
body="$body p$n: repository(owner: \$o$n, name: \$n$n) { pullRequests(headRefName: \$b$n, states: [OPEN, CLOSED, MERGED], first: 1, orderBy: {field: CREATED_AT, direction: DESC}) { nodes { url state mergeable } } }"
|
|
137
|
+
args+=(-f "o$n=$owner" -f "n$n=$name" -f "b$n=$branch")
|
|
138
|
+
n=$((n + 1))
|
|
139
|
+
done
|
|
140
|
+
# gh prints GitHub's answer even when it names an error for some repo, and
|
|
141
|
+
# then exits 1, so the answer is read whatever gh exits with.
|
|
142
|
+
gh api graphql -f "query=query(${vars#, }) {$body }" "${args[@]}" > "$scratch/answer" 2> "$scratch/err" ||
|
|
143
|
+
code=$?
|
|
144
|
+
if answer=$(jq -rn --argjson n "$n" "$GITHUB_READ" "$scratch/answer" 2>/dev/null); then
|
|
145
|
+
n=0
|
|
146
|
+
while IFS= read -r line; do
|
|
147
|
+
[ "$n" -lt "${#at[@]}" ] || break
|
|
148
|
+
i=${at[n]}
|
|
149
|
+
case $line in
|
|
150
|
+
'failed '*) lines[i]="failed ${wts[i]##*/}: ${line#failed }" ;;
|
|
151
|
+
*) lines[i]=$line ;;
|
|
152
|
+
esac
|
|
153
|
+
n=$((n + 1))
|
|
154
|
+
done <<< "$answer"
|
|
155
|
+
return 0
|
|
156
|
+
fi
|
|
157
|
+
why=$(sed -n '/[^[:space:]]/{p;q;}' "$scratch/err" 2>/dev/null) || why=''
|
|
158
|
+
[ "$code" != 0 ] || why='its answer could not be read'
|
|
159
|
+
if [ "$code" = 4 ] || grep -qF 'HTTP 401' "$scratch/err" 2>/dev/null; then GITHUB_SIGN_IN=1; fi
|
|
160
|
+
[ -n "$GITHUB_CALL_WHY" ] || GITHUB_CALL_WHY=${why:-gh failed}
|
|
161
|
+
for i in "${at[@]}"; do lines[i]="failed ${wts[i]##*/}: ${why:-gh failed}"; done
|
|
162
|
+
}
|
package/cli/lib/guard.sh
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# The guard on a worktree's copy of ocnus. Sourced by cli/ocnus.
|
|
2
|
+
|
|
3
|
+
# A copy of ocnus in a linked git worktree is a change being built, not
|
|
4
|
+
# the copy this machine runs. Run without being asked to act on the real
|
|
5
|
+
# machine, it runs only the commands that read, and stops before any other
|
|
6
|
+
# does anything: a slip must not open a real tab, make a real task, change a
|
|
7
|
+
# card in Linear, or change the human's spaces, hand-offs, links or skills.
|
|
8
|
+
# OCNUS_LIVE=1, set for the run, lets it act on the machine for real. The
|
|
9
|
+
# file cannot set it (see load_env in cli/ocnus), so it is never left on
|
|
10
|
+
# for every run. The main checkout and an npm install are never guarded.
|
|
11
|
+
|
|
12
|
+
# 1 while this run is a worktree's copy that was not asked to act on the real
|
|
13
|
+
# machine. guard_worktree sets it; `store` in handoff.sh then opens the
|
|
14
|
+
# hand-off store read-only, so that a read cannot change it either.
|
|
15
|
+
GUARDED=0
|
|
16
|
+
|
|
17
|
+
# Whether this copy is in a linked git worktree. Its .git is then a file whose
|
|
18
|
+
# gitdir: line names a folder in the worktrees/ folder of the repo it was made
|
|
19
|
+
# from. The main checkout has a .git folder, an npm install has no .git, and
|
|
20
|
+
# a submodule's .git file points into modules/. The shell reads the file, so
|
|
21
|
+
# no git is needed. Only where this script is counts, never the folder it is
|
|
22
|
+
# run from, so plain `ocnus` run inside a worktree is not guarded.
|
|
23
|
+
is_worktree_copy() {
|
|
24
|
+
local line='' re='^gitdir:[[:space:]]*(.*/)?worktrees/[^/]+/?$'
|
|
25
|
+
[ -f "$REPO/.git" ] || return 1
|
|
26
|
+
{ IFS= read -r line || [ -n "$line" ]; } < "$REPO/.git" 2>/dev/null || return 1
|
|
27
|
+
line=${line%$'\r'}
|
|
28
|
+
[[ $line =~ $re ]]
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
# Whether a command line only reads. The list names what may run, not what
|
|
32
|
+
# may not, so a command added later is refused until it is added here as a
|
|
33
|
+
# read. Each of these leaves the machine as it was, the hand-off store
|
|
34
|
+
# included; `task ls` asks Linear, and only reads. `task merges` asks
|
|
35
|
+
# GitHub, but it records the merges it sees and moves their cards in Linear,
|
|
36
|
+
# so it is not a read and is not here.
|
|
37
|
+
# `profile ls` reads the settings file; `profile set` changes it,
|
|
38
|
+
# and the review subagents in Claude Code's agents folder. `linear archive`
|
|
39
|
+
# alone, or with --json, reads whether archiving is on; with on or off it
|
|
40
|
+
# changes the settings file. `tests` alone, or with --json, reads how the
|
|
41
|
+
# test runs use the processors; with share or queue it changes the settings
|
|
42
|
+
# file too.
|
|
43
|
+
# `approval` alone, or with --json, reads whether the breakdown approval is
|
|
44
|
+
# on; with --breakdown on or off it changes the settings file. `billing`
|
|
45
|
+
# alone, or with --json, reads which account the agents run on, and whether
|
|
46
|
+
# an Anthropic key is saved; with api or subscription it changes the
|
|
47
|
+
# settings file, and with key the env file. `billing key --print` changes
|
|
48
|
+
# nothing, but it prints the saved key, so it is not a read either: it is for
|
|
49
|
+
# an agent's apiKeyHelper, which runs the copy that started the agent. `host`
|
|
50
|
+
# alone says whether the host is recorded, and `host report` reads its
|
|
51
|
+
# readings; `host on` and `host off` make and delete them, and `host record`
|
|
52
|
+
# writes them.
|
|
53
|
+
is_read() {
|
|
54
|
+
case ${1:-help} in
|
|
55
|
+
help | -h | --help | status) return 0 ;;
|
|
56
|
+
space)
|
|
57
|
+
case ${2:-} in
|
|
58
|
+
ls | list) return 0 ;;
|
|
59
|
+
# With keys after the name it writes the space's teams.
|
|
60
|
+
team) [ "$#" -le 3 ] ;;
|
|
61
|
+
*) return 1 ;;
|
|
62
|
+
esac
|
|
63
|
+
;;
|
|
64
|
+
profile)
|
|
65
|
+
case ${2:-} in
|
|
66
|
+
ls | list) return 0 ;;
|
|
67
|
+
*) return 1 ;;
|
|
68
|
+
esac
|
|
69
|
+
;;
|
|
70
|
+
linear)
|
|
71
|
+
[ "${2:-}" = archive ] || return 1
|
|
72
|
+
case $# in
|
|
73
|
+
2) return 0 ;;
|
|
74
|
+
3) [ "$3" = --json ] ;;
|
|
75
|
+
*) return 1 ;;
|
|
76
|
+
esac
|
|
77
|
+
;;
|
|
78
|
+
tests)
|
|
79
|
+
case $# in
|
|
80
|
+
1) return 0 ;;
|
|
81
|
+
2) [ "$2" = --json ] ;;
|
|
82
|
+
*) return 1 ;;
|
|
83
|
+
esac
|
|
84
|
+
;;
|
|
85
|
+
approval | billing)
|
|
86
|
+
case $# in
|
|
87
|
+
1) return 0 ;;
|
|
88
|
+
2) [ "$2" = --json ] ;;
|
|
89
|
+
*) return 1 ;;
|
|
90
|
+
esac
|
|
91
|
+
;;
|
|
92
|
+
task)
|
|
93
|
+
case ${2:-} in
|
|
94
|
+
ls | list | log) return 0 ;;
|
|
95
|
+
*) return 1 ;;
|
|
96
|
+
esac
|
|
97
|
+
;;
|
|
98
|
+
host)
|
|
99
|
+
case ${2:-} in
|
|
100
|
+
'') [ "$#" -le 1 ] ;;
|
|
101
|
+
report) [ "$#" -le 3 ] ;;
|
|
102
|
+
*) return 1 ;;
|
|
103
|
+
esac
|
|
104
|
+
;;
|
|
105
|
+
*) return 1 ;;
|
|
106
|
+
esac
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
# A command line as a shell would read it back, each word quoted only when it
|
|
110
|
+
# needs to be.
|
|
111
|
+
shell_words() {
|
|
112
|
+
local w out=''
|
|
113
|
+
for w in "$@"; do
|
|
114
|
+
case $w in
|
|
115
|
+
'' | *[!A-Za-z0-9_./:=@%+,-]*) w=$(sh_quote "$w") ;;
|
|
116
|
+
esac
|
|
117
|
+
out="$out${out:+ }$w"
|
|
118
|
+
done
|
|
119
|
+
printf '%s' "$out"
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
# Stop a worktree's copy before a command that would change the machine, and
|
|
123
|
+
# say which copy to run instead. Run before the command is dispatched.
|
|
124
|
+
guard_worktree() {
|
|
125
|
+
is_worktree_copy || return 0
|
|
126
|
+
[ "${OCNUS_LIVE:-}" != 1 ] || return 0
|
|
127
|
+
GUARDED=1
|
|
128
|
+
! is_read "$@" || return 0
|
|
129
|
+
die "this is the copy of ocnus in the worktree $REPO. It runs only the commands that read (help, status, space ls, space team <name>, profile ls, linear archive, tests, approval, billing, task ls, task log, host and host report), so \`$(shell_words "$@")\` was not run: it could change this machine's Herdr, spaces and tasks, hand-offs, host readings, Linear key or cards, or Claude Code's skills, agents and trust file.
|
|
130
|
+
- For the card's own steps, such as recording a hand-off, run plain \`ocnus\`: the copy this machine runs, not this one.
|
|
131
|
+
- To try a command that changes the machine, write a test case for it, in test/.
|
|
132
|
+
- To run it on this machine for real, only when that is wanted:
|
|
133
|
+
OCNUS_LIVE=1 $(shell_words "$REPO/cli/ocnus" "$@")"
|
|
134
|
+
}
|