@johngaynor/ocnus 2.0.1-next.27 → 2.0.1-next.69
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/README.md +442 -40
- package/cli/lib/asks.sh +762 -0
- package/cli/lib/banner.sh +181 -0
- package/cli/lib/brief.sh +375 -0
- package/cli/lib/chain.sh +363 -0
- package/cli/lib/close.sh +5 -3
- package/cli/lib/cost.sh +500 -0
- package/cli/lib/github.sh +75 -11
- package/cli/lib/guard.sh +38 -11
- package/cli/lib/handoff.sh +115 -31
- package/cli/lib/host.sh +172 -1
- package/cli/lib/linear.sh +198 -1
- package/cli/lib/links.sh +6 -3
- package/cli/lib/merges.sh +103 -15
- package/cli/lib/sessions.sh +132 -0
- package/cli/lib/settings.sh +31 -15
- package/cli/lib/task.sh +159 -64
- package/cli/lib/tests.sh +300 -4
- package/cli/lib/usage.sh +70 -25
- package/cli/ocnus +234 -37
- package/herdr/README.md +659 -76
- package/herdr/layouts/showcase.json +291 -0
- package/herdr/layouts/vibe.json +162 -0
- package/herdr/layouts/vibe.svg +41 -0
- package/herdr/mods/agent-settings/main.js +19 -7
- package/herdr/mods/names/main.js +22 -4
- package/package.json +1 -1
- package/prompts/task-new.md +1 -1
- package/skills/brief/SKILL.md +8 -0
- package/skills/sdlc-breakdown/SKILL.md +5 -5
package/cli/lib/asks.sh
ADDED
|
@@ -0,0 +1,762 @@
|
|
|
1
|
+
# Questions: what waits on the human in a space, and answering one with no
|
|
2
|
+
# one typing in the agent's tab. Sourced by cli/ocnus.
|
|
3
|
+
#
|
|
4
|
+
# An agent stops for the human in two ways. It records a needs-human hand-off
|
|
5
|
+
# (handoff.sh), with its question and the answers it expects, and ends its
|
|
6
|
+
# turn. Or Claude Code stops it at a permission prompt, before a tool runs.
|
|
7
|
+
# `task asks` lists both for a space, and `task answer` answers one. A
|
|
8
|
+
# question has one of four kinds:
|
|
9
|
+
# approval sdlc-breakdown asking to approve the card: a needs-human
|
|
10
|
+
# hand-off whose step is breakdown and whose first option
|
|
11
|
+
# starts with approve
|
|
12
|
+
# merge sdlc-pr's wait for the human to merge the card's pull
|
|
13
|
+
# requests: a needs-human hand-off whose step is pr and whose
|
|
14
|
+
# first option is merged. It is listed with merge in that
|
|
15
|
+
# option's place, and with the links of its open pull requests
|
|
16
|
+
# choice any other needs-human hand-off: the agent's own list of
|
|
17
|
+
# options
|
|
18
|
+
# permission Claude Code asking to allow a tool
|
|
19
|
+
#
|
|
20
|
+
# An approval, a choice or a merge is a card's latest hand-off, for as long as
|
|
21
|
+
# that is needs-human, read from the store as `task log` reads it. The row's
|
|
22
|
+
# id is the question's id. So an answer the agent has recorded, in the tab or
|
|
23
|
+
# after an earlier `task answer`, or a newer question, makes the id stale, and
|
|
24
|
+
# an answer to it is refused. The option, or the human's own words, goes to
|
|
25
|
+
# the card's agent with `hc send`, as if typed in its tab: to the terminal of
|
|
26
|
+
# its tab whose turn has ended, found as ocnus's watch on the merges finds
|
|
27
|
+
# one (card_seen, in agents.sh). An agent still ending its turn is waited
|
|
28
|
+
# for, ANSWER_TURN_WAIT seconds, and one that is not running takes no answer:
|
|
29
|
+
# starting an agent again is `task build`'s. The answer counts as taken once
|
|
30
|
+
# hc has sent it and the terminal says the agent is working, or at a prompt
|
|
31
|
+
# it has come to since.
|
|
32
|
+
#
|
|
33
|
+
# A merge answered with merge is not sent to the agent: it merges each of the
|
|
34
|
+
# card's open pull requests on GitHub (github.sh), by the method GitHub's own
|
|
35
|
+
# merge button would use for the human in that repo, and only when GitHub
|
|
36
|
+
# would merge every one of them as it stands. Then ocnus's watch on the
|
|
37
|
+
# merges (merges.sh) records the merge and finishes the task, as it does when
|
|
38
|
+
# the human merges on GitHub. Nothing is ever forced, and no auto-merge is
|
|
39
|
+
# turned on.
|
|
40
|
+
#
|
|
41
|
+
# A permission prompt is seen through a hook. Every claude that `task new`
|
|
42
|
+
# and `task build` start gets a PermissionRequest hook (ask_hooks, given with
|
|
43
|
+
# --settings= by profile_args, in task.sh): `ocnus task hold <space>
|
|
44
|
+
# <card>`. Claude Code runs it beside the prompt, with the tool and its
|
|
45
|
+
# input on stdin, and the decision it prints closes the prompt. The hook
|
|
46
|
+
# writes the question to asks/<space>/<card>/<id>.json in ocnus's folder,
|
|
47
|
+
# and waits. `task answer` leaves the option beside that file, as
|
|
48
|
+
# <id>.answer, put there whole and only when no answer is there. The hook
|
|
49
|
+
# reads it, takes it by renaming it <id>.taken, which only one of the two can
|
|
50
|
+
# do, and prints the decision: allow, allow with the prompt's own suggestions
|
|
51
|
+
# as updatedPermissions, or deny with a message that the human refused it,
|
|
52
|
+
# and no interrupt. An option the hook has not taken in ANSWER_HOOK_WAIT
|
|
53
|
+
# seconds is withdrawn, by renaming it too, so an answer is either taken or
|
|
54
|
+
# withdrawn, never both. While it waits it asks hc about its terminal, and
|
|
55
|
+
# once that is no longer blocked it ends with no decision: the prompt was
|
|
56
|
+
# answered in the tab, or the agent has gone. The list shows a prompt only
|
|
57
|
+
# while its file is there, its hook is alive and hc says its terminal is
|
|
58
|
+
# blocked. The hook leaves AskUserQuestion and ExitPlanMode alone, which only
|
|
59
|
+
# the person in the tab can answer, and any other Claude Code prompt, such as
|
|
60
|
+
# a sign-in, runs no such hook. Pressing keys in the tab with `hc keys` would
|
|
61
|
+
# answer whatever prompt is there by then, so nothing here does.
|
|
62
|
+
#
|
|
63
|
+
# Only the human answers: run from inside the session of an agent ocnus
|
|
64
|
+
# started, `task answer` sends nothing and merges nothing.
|
|
65
|
+
|
|
66
|
+
ASKS_DIR=$ENT_HOME/asks
|
|
67
|
+
ASKS_USAGE='usage: ocnus task asks <space> [--json]'
|
|
68
|
+
ANSWER_USAGE='usage: ocnus task answer <space> <card> <question> <answer>'
|
|
69
|
+
# The options of a permission prompt. The last is there only when the prompt
|
|
70
|
+
# came with suggestions for allowing it from now on.
|
|
71
|
+
ASK_ALLOW='allow' ASK_REFUSE='refuse' ASK_ALWAYS='allow from now on'
|
|
72
|
+
# What the agent is told when the human refuses a permission prompt.
|
|
73
|
+
ASK_REFUSED='The human refused this, with ocnus task answer.'
|
|
74
|
+
# Seconds between a hook's looks at its terminal and for an answer. A look
|
|
75
|
+
# costs a few milliseconds, and the sooner a hook sees that its prompt was
|
|
76
|
+
# answered in the tab, the less often the agent's next prompt finds it still
|
|
77
|
+
# listed.
|
|
78
|
+
HOLD_EVERY=1
|
|
79
|
+
# How many looks in a row a hook lets pass with its terminal not known to be
|
|
80
|
+
# blocked, before it has seen it blocked or while hc cannot answer for it.
|
|
81
|
+
HOLD_GRACE=10
|
|
82
|
+
# Seconds Claude Code lets the hook wait before it ends it: a week.
|
|
83
|
+
HOLD_TIMEOUT=604800
|
|
84
|
+
# Seconds an answer waits for an agent that is still ending its turn, for the
|
|
85
|
+
# agent to start on what it was sent, and for a hook to take an option.
|
|
86
|
+
ANSWER_TURN_WAIT=30
|
|
87
|
+
ANSWER_START_WAIT=15
|
|
88
|
+
ANSWER_HOOK_WAIT=15
|
|
89
|
+
|
|
90
|
+
# --- the hook behind a permission prompt ------------------------------------
|
|
91
|
+
|
|
92
|
+
# The settings, as one line of JSON for claude's --settings=, that give the
|
|
93
|
+
# agent of a card the hook behind its permission prompts: a command that runs
|
|
94
|
+
# this copy of ocnus, told where ocnus's folder is, as billing_helper's
|
|
95
|
+
# is, with each path quoted for the shell that runs it. Claude Code ends a
|
|
96
|
+
# hook after its timeout, in seconds, so it is HOLD_TIMEOUT. It is put
|
|
97
|
+
# together without jq, so that an agent starts with it on a machine that has
|
|
98
|
+
# none. It fails for a space or a card that could not be part of a command.
|
|
99
|
+
# ask_hooks <space> <card>
|
|
100
|
+
ask_hooks() {
|
|
101
|
+
local space=$1 card=$2 cmd
|
|
102
|
+
valid_name "$space" && [[ $card =~ $CARD_RE ]] || return 1
|
|
103
|
+
cmd="OCNUS_HOME=$(sh_quote "$ENT_HOME") $(sh_quote "$REPO/cli/ocnus") task hold $space $card"
|
|
104
|
+
printf '{"hooks":{"PermissionRequest":[{"matcher":"*","hooks":[{"type":"command","command":"%s","timeout":%s}]}]}}' \
|
|
105
|
+
"$(json_str "$cmd")" "$HOLD_TIMEOUT"
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
# jq: what a permission prompt asks, from the hook's input: the tool, and
|
|
109
|
+
# what it would do.
|
|
110
|
+
ASK_TEXT_DEF='def ask_text:
|
|
111
|
+
(.tool_name | if type == "string" and . != "" then . else "a tool" end) as $tool
|
|
112
|
+
| (.tool_input | if type == "object" then . else {} end) as $in
|
|
113
|
+
| if $tool == "Bash" and ($in.command | type) == "string" then "Allow Bash to run: \($in.command)"
|
|
114
|
+
elif ($in.file_path | type) == "string" then "Allow \($tool) on the file: \($in.file_path)"
|
|
115
|
+
elif ($in.notebook_path | type) == "string" then "Allow \($tool) on the notebook: \($in.notebook_path)"
|
|
116
|
+
elif ($in.url | type) == "string" then "Allow \($tool) to fetch: \($in.url)"
|
|
117
|
+
elif ($in | length) == 0 then "Allow \($tool)"
|
|
118
|
+
else ($in | tojson) as $json
|
|
119
|
+
| "Allow \($tool) with: \(if ($json | length) > 2000 then $json[0:2000] + " ..." else $json end)"
|
|
120
|
+
end;'
|
|
121
|
+
|
|
122
|
+
# Print what became of the hook of a permission prompt, from its pid: alive,
|
|
123
|
+
# while its process is there and is `task hold`; gone, when there is no such
|
|
124
|
+
# process, or it is another program that has been given the pid since;
|
|
125
|
+
# unknown, when ps does not say. -ww has ps print the whole command line,
|
|
126
|
+
# whatever width a terminal or COLUMNS gives it: the words come after the
|
|
127
|
+
# path of this copy of ocnus.
|
|
128
|
+
# hold_state <pid>
|
|
129
|
+
hold_state() {
|
|
130
|
+
local args
|
|
131
|
+
case ${1:-} in '' | *[!0-9]*) printf 'gone\n'; return 0 ;; esac
|
|
132
|
+
kill -0 "$1" 2>/dev/null || { printf 'gone\n'; return 0; }
|
|
133
|
+
args=$(ps -ww -p "$1" -o args= 2>/dev/null) || args=''
|
|
134
|
+
case $args in
|
|
135
|
+
*'task hold '*) printf 'alive\n' ;;
|
|
136
|
+
'') printf 'unknown\n' ;;
|
|
137
|
+
*) printf 'gone\n' ;;
|
|
138
|
+
esac
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
# Whether the hook of a permission prompt still waits. A question is listed
|
|
142
|
+
# and answered only while it does.
|
|
143
|
+
# ask_alive <pid>
|
|
144
|
+
ask_alive() { [ "$(hold_state "${1:-}")" = alive ]; }
|
|
145
|
+
|
|
146
|
+
# Remove an emptied folder of questions, and the folders above it once they
|
|
147
|
+
# hold nothing.
|
|
148
|
+
# asks_tidy <the card's folder under asks/>
|
|
149
|
+
asks_tidy() { rmdir "$1" "${1%/*}" "$ASKS_DIR" 2>/dev/null || true; }
|
|
150
|
+
|
|
151
|
+
# The question file of the hook that is running, for hold_done.
|
|
152
|
+
HOLD_FILE=''
|
|
153
|
+
# Remove the hook's question as it ends, however it ends. An option it took
|
|
154
|
+
# stays, as <id>.taken, for the answer that left it to see.
|
|
155
|
+
hold_done() {
|
|
156
|
+
[ -n "$HOLD_FILE" ] || return 0
|
|
157
|
+
rm -f "$HOLD_FILE" "$HOLD_FILE.new" 2>/dev/null || true
|
|
158
|
+
asks_tidy "${HOLD_FILE%/*}"
|
|
159
|
+
HOLD_FILE=''
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
# Print the decision for an option, as Claude Code reads one from a
|
|
163
|
+
# PermissionRequest hook. From now on gives the prompt's own suggestions back
|
|
164
|
+
# as the permissions to add, and a refusal does not interrupt the agent.
|
|
165
|
+
# Anything else prints nothing, which decides nothing.
|
|
166
|
+
# hold_decide <option> <the hook's input>
|
|
167
|
+
hold_decide() {
|
|
168
|
+
case $1 in
|
|
169
|
+
"$ASK_ALLOW")
|
|
170
|
+
printf '%s\n' '{"hookSpecificOutput":{"hookEventName":"PermissionRequest","decision":{"behavior":"allow"}}}'
|
|
171
|
+
;;
|
|
172
|
+
"$ASK_ALWAYS")
|
|
173
|
+
printf '%s' "$2" | jq -c '{hookSpecificOutput: {hookEventName: "PermissionRequest",
|
|
174
|
+
decision: ({behavior: "allow"}
|
|
175
|
+
+ ((.permission_suggestions | if type == "array" and length > 0 then {updatedPermissions: .} else {} end)))}}' \
|
|
176
|
+
2>/dev/null || true
|
|
177
|
+
;;
|
|
178
|
+
"$ASK_REFUSE")
|
|
179
|
+
printf '{"hookSpecificOutput":{"hookEventName":"PermissionRequest","decision":{"behavior":"deny","message":"%s"}}}\n' \
|
|
180
|
+
"$(json_str "$ASK_REFUSED")"
|
|
181
|
+
;;
|
|
182
|
+
esac
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
# The hook: `task hold <space> <card>`, which Claude Code runs beside a
|
|
186
|
+
# permission prompt of that card's agent, with the prompt on stdin. It writes
|
|
187
|
+
# the question, then waits for an answer or for the prompt to go, looking
|
|
188
|
+
# every HOLD_EVERY seconds. It takes an answer only while hc says its
|
|
189
|
+
# terminal is blocked, so that one left as the prompt was answered in the tab
|
|
190
|
+
# is not taken. Whatever is missing or goes wrong, it prints nothing and
|
|
191
|
+
# exits zero, which decides nothing and leaves the prompt in the tab as it
|
|
192
|
+
# was: an error would show in the agent's tab, and anything else it printed
|
|
193
|
+
# would be read as its decision. It has no terminal to ask hc about outside
|
|
194
|
+
# herdr-core, so there it ends at once.
|
|
195
|
+
task_hold() {
|
|
196
|
+
local space=${1:-} card=${2:-} term=${HC_TERMINAL_ID:-} input tool id dir file old
|
|
197
|
+
[ "$#" = 2 ] && valid_name "$space" && [[ $card =~ $CARD_RE ]] || return 0
|
|
198
|
+
[ ! -t 0 ] || return 0
|
|
199
|
+
input=$(cat) || return 0
|
|
200
|
+
[ -n "$term" ] && have_hc && command -v jq >/dev/null 2>&1 || return 0
|
|
201
|
+
tool=$(printf '%s' "$input" | jq -r 'if type == "object"
|
|
202
|
+
and (.hook_event_name // "PermissionRequest") == "PermissionRequest"
|
|
203
|
+
then (.tool_name | strings) else empty end' 2>/dev/null) || return 0
|
|
204
|
+
case $tool in '' | AskUserQuestion | ExitPlanMode) return 0 ;; esac
|
|
205
|
+
card=$(printf '%s' "$card" | tr '[:lower:]' '[:upper:]')
|
|
206
|
+
dir=$ASKS_DIR/$space/$card
|
|
207
|
+
# The question copies what the tool would run, which may hold a secret, so
|
|
208
|
+
# the folders and the file this makes are the human's alone.
|
|
209
|
+
umask 077
|
|
210
|
+
mkdir -p "$dir" 2>/dev/null || return 0
|
|
211
|
+
# What an earlier hook or answer of this card left when it was stopped
|
|
212
|
+
# part-way: the question of a hook that has gone, and an option or a mark
|
|
213
|
+
# older than any answer waits. A question whose hook ps cannot answer for
|
|
214
|
+
# is left, since the hook may be waiting.
|
|
215
|
+
for old in "$dir"/p*.json; do
|
|
216
|
+
[ -f "$old" ] || continue
|
|
217
|
+
[ "$(hold_state "$(jq -r '.pid // empty' "$old" 2>/dev/null)")" != gone ] || rm -f "$old" 2>/dev/null || true
|
|
218
|
+
done
|
|
219
|
+
find "$dir" -type f \( -name 'p*.answer' -o -name 'p*.answer.*' -o -name 'p*.taken' -o -name 'p*.json.new' \) \
|
|
220
|
+
-mmin +5 -exec rm -f {} + 2>/dev/null || true
|
|
221
|
+
|
|
222
|
+
id=$(LC_ALL=C od -A n -t x1 -N 4 /dev/urandom 2>/dev/null | LC_ALL=C tr -d ' \n') || id=''
|
|
223
|
+
[ "${#id}" = 8 ] || id=$(printf '%08x' "$(( ($(date +%s) * 31 + $$) % 4294967296 ))")
|
|
224
|
+
id=p$id
|
|
225
|
+
file=$dir/$id.json
|
|
226
|
+
HOLD_FILE=$file
|
|
227
|
+
trap hold_done EXIT
|
|
228
|
+
trap 'exit 0' HUP INT TERM
|
|
229
|
+
printf '%s' "$input" | jq -c --arg id "$id" --arg space "$space" --arg card "$card" --arg terminal "$term" \
|
|
230
|
+
--argjson pid "$$" --argjson asked "$(date +%s)" "$ASK_TEXT_DEF"'
|
|
231
|
+
{id: $id, space: $space, card: $card, at: ($asked | todate), asked: $asked, pid: $pid,
|
|
232
|
+
terminal: $terminal, tool: .tool_name, text: ask_text,
|
|
233
|
+
always: (.permission_suggestions | type == "array" and length > 0)}' > "$file.new" 2>/dev/null &&
|
|
234
|
+
mv -f "$file.new" "$file" 2>/dev/null || return 0
|
|
235
|
+
|
|
236
|
+
local seen=0 misses=0 option
|
|
237
|
+
while :; do
|
|
238
|
+
case $(terminal_doing "$term") in
|
|
239
|
+
blocked)
|
|
240
|
+
seen=1 misses=0
|
|
241
|
+
# Read first, then take it: the rename fails when the answer was
|
|
242
|
+
# withdrawn meanwhile, and nothing is decided then.
|
|
243
|
+
option=''
|
|
244
|
+
if [ -f "$dir/$id.answer" ] && { IFS= read -r option < "$dir/$id.answer"; } 2>/dev/null &&
|
|
245
|
+
mv "$dir/$id.answer" "$dir/$id.taken" 2>/dev/null; then
|
|
246
|
+
hold_decide "$option" "$input"
|
|
247
|
+
return 0
|
|
248
|
+
fi
|
|
249
|
+
;;
|
|
250
|
+
unknown)
|
|
251
|
+
misses=$((misses + 1))
|
|
252
|
+
[ "$misses" -lt "$HOLD_GRACE" ] || return 0
|
|
253
|
+
;;
|
|
254
|
+
*)
|
|
255
|
+
# The prompt is gone: answered in the tab, or the agent has gone on.
|
|
256
|
+
[ "$seen" = 0 ] || return 0
|
|
257
|
+
misses=$((misses + 1))
|
|
258
|
+
[ "$misses" -lt "$HOLD_GRACE" ] || return 0
|
|
259
|
+
;;
|
|
260
|
+
esac
|
|
261
|
+
sleep "$HOLD_EVERY" || return 0
|
|
262
|
+
done
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
# --- the list ---------------------------------------------------------------
|
|
266
|
+
|
|
267
|
+
# The kind of a needs-human hand-off, as ASK_KIND, from its step and its
|
|
268
|
+
# options: the option lines sdlc-breakdown and sdlc-pr record are pinned in
|
|
269
|
+
# test/skills.sh.
|
|
270
|
+
# ask_kind <step> <options>
|
|
271
|
+
ASK_KIND=choice
|
|
272
|
+
ask_kind() {
|
|
273
|
+
local step=$1 first=${2%% | *}
|
|
274
|
+
first=${first#"${first%%[![:space:]]*}"}
|
|
275
|
+
first=${first%"${first##*[![:space:]]}"}
|
|
276
|
+
ASK_KIND=choice
|
|
277
|
+
case $step in
|
|
278
|
+
pr) [ "$first" != merged ] || ASK_KIND=merge ;;
|
|
279
|
+
breakdown) case $first in approve*) ASK_KIND=approval ;; esac ;;
|
|
280
|
+
esac
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
# The links of the open pull requests of a card that waits on its merge, a
|
|
284
|
+
# line each: the ones ocnus's watch on the merges last saw open, one in
|
|
285
|
+
# conflict among them, from the card's .pulls file (merges_check, in
|
|
286
|
+
# merges.sh), or before its first pass the links in the question's own text.
|
|
287
|
+
# ask_pulls <space> <card> <the question>
|
|
288
|
+
ask_pulls() {
|
|
289
|
+
local file=$ENT_HOME/merges/$1/$2.pulls text=$3 state url
|
|
290
|
+
local re='https?://[A-Za-z0-9.-]+(/[A-Za-z0-9._~-]+)+/pull/[0-9]+'
|
|
291
|
+
if [ -f "$file" ]; then
|
|
292
|
+
while read -r state url; do
|
|
293
|
+
case $state in open | conflict) [ -z "$url" ] || printf '%s\n' "$url" ;; esac
|
|
294
|
+
done < "$file"
|
|
295
|
+
return 0
|
|
296
|
+
fi
|
|
297
|
+
while [[ $text =~ $re ]]; do
|
|
298
|
+
printf '%s\n' "${BASH_REMATCH[0]}"
|
|
299
|
+
text=${text#*"${BASH_REMATCH[0]}"}
|
|
300
|
+
done
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
# Every question that waits on the human in a space, a line of JSON each, as
|
|
304
|
+
# `task asks --json` gives one, and with it what `task answer` needs: asked,
|
|
305
|
+
# the time it was asked in seconds since 1970, its step for a hand-off, and
|
|
306
|
+
# its terminal and pid for a permission prompt. It reads the store, the
|
|
307
|
+
# watch's files, the hooks' files and hc, and changes nothing. The space's
|
|
308
|
+
# tabs are read only when a hand-off waits, and a hand-off's agent counts as
|
|
309
|
+
# not running when its tab is closed or holds its own shell alone, as it is
|
|
310
|
+
# with no tab to look in.
|
|
311
|
+
# asks_collect <space> <the space's folder>
|
|
312
|
+
asks_collect() {
|
|
313
|
+
local space=$1 dir=$2 sep=$'\x1f' rows='' tabs=0 now
|
|
314
|
+
local id card step at asked seconds text options summary running pulls
|
|
315
|
+
if [ -e "$STORE" ]; then
|
|
316
|
+
rows=$(store -separator "$sep" "$STORE" "select id, card, step, at,
|
|
317
|
+
coalesce(cast(strftime('%s', at) as integer), 0),
|
|
318
|
+
coalesce(max(cast(strftime('%s', 'now') - strftime('%s', at) as integer), 0), 0),
|
|
319
|
+
coalesce(question, ''), coalesce(options, ''), coalesce(summary, '')
|
|
320
|
+
from (select *, row_number() over (partition by card order by id desc) as nth
|
|
321
|
+
from handoffs where space = $(sql_str "$space"))
|
|
322
|
+
where nth = 1 and status = 'needs-human' order by id;") ||
|
|
323
|
+
die "could not read the hand-offs in $STORE. It may not be ocnus's store."
|
|
324
|
+
fi
|
|
325
|
+
while IFS=$sep read -r id card step at asked seconds text options summary; do
|
|
326
|
+
[ -n "$id" ] || continue
|
|
327
|
+
[ "$tabs" = 1 ] || { agents_read "$space" "$dir"; tabs=1; }
|
|
328
|
+
card_seen "$card" "$(printf '%s' "$card" | tr '[:upper:]' '[:lower:]')" "$step"
|
|
329
|
+
running=true
|
|
330
|
+
case $AGENT_SEEN in closed | none) running=false ;; esac
|
|
331
|
+
ask_kind "$step" "$options"
|
|
332
|
+
pulls=''
|
|
333
|
+
[ "$ASK_KIND" != merge ] || pulls=$(ask_pulls "$space" "$card" "$text")
|
|
334
|
+
jq -cn --arg id "$id" --arg card "$card" --arg kind "$ASK_KIND" --arg step "$step" --arg text "$text" \
|
|
335
|
+
--arg options "$options" --arg at "$at" --argjson asked "$asked" --argjson seconds "$seconds" \
|
|
336
|
+
--arg summary "$summary" --argjson running "$running" --arg pulls "$pulls" '
|
|
337
|
+
{id: $id, card: $card, kind: $kind, text: $text,
|
|
338
|
+
options: ($options | split(" | ") | map(gsub("^\\s+|\\s+$"; "")) | map(select(. != ""))
|
|
339
|
+
| if $kind == "merge" and length > 0 then .[0] = "merge" else . end),
|
|
340
|
+
at: $at, seconds: $seconds, summary: (if $summary == "" then null else $summary end),
|
|
341
|
+
words: true, running: $running}
|
|
342
|
+
+ (if $kind == "merge" then {pulls: ($pulls | split("\n") | map(select(. != "")))} else {} end)
|
|
343
|
+
+ {asked: $asked, step: $step}' ||
|
|
344
|
+
die "could not put $card's question together with jq."
|
|
345
|
+
done <<< "$rows"
|
|
346
|
+
|
|
347
|
+
local f q pid term
|
|
348
|
+
now=$(date +%s)
|
|
349
|
+
for f in "$ASKS_DIR/$space"/*/p*.json; do
|
|
350
|
+
[ -f "$f" ] || continue
|
|
351
|
+
q=$(jq -c 'select(type == "object" and (.id | type) == "string" and (.card | type) == "string")' "$f" 2>/dev/null) &&
|
|
352
|
+
[ -n "$q" ] || continue
|
|
353
|
+
pid=$(printf '%s' "$q" | jq -r '.pid // empty') term=$(printf '%s' "$q" | jq -r '.terminal // empty')
|
|
354
|
+
ask_alive "$pid" && [ -n "$term" ] && [ "$(terminal_doing "$term")" = blocked ] || continue
|
|
355
|
+
printf '%s' "$q" | jq -c --argjson now "$now" --arg allow "$ASK_ALLOW" --arg refuse "$ASK_REFUSE" \
|
|
356
|
+
--arg always "$ASK_ALWAYS" '
|
|
357
|
+
(.asked | if type == "number" then . else $now end) as $asked
|
|
358
|
+
| {id, card, kind: "permission", text: (.text | if type == "string" then . else "" end),
|
|
359
|
+
options: ([$allow, $refuse] + (if .always == true then [$always] else [] end)),
|
|
360
|
+
at: (.at | if type == "string" then . else ($asked | todate) end),
|
|
361
|
+
seconds: ([$now - $asked, 0] | max), summary: null, words: false, running: true,
|
|
362
|
+
tool: (.tool | if type == "string" then . else null end), asked: $asked, terminal, pid}' || true
|
|
363
|
+
done
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
# jq: a question as `task asks --json` gives it, without what only `task
|
|
367
|
+
# answer` reads.
|
|
368
|
+
ASK_PUBLIC='del(.asked, .step, .terminal, .pid)'
|
|
369
|
+
|
|
370
|
+
# List the questions that wait on the human in a space, oldest first: each
|
|
371
|
+
# with its card, its kind, its id, when it was asked, its text, any summary,
|
|
372
|
+
# its options, and for a merge its open pull requests. One whose agent is not
|
|
373
|
+
# running says so. With --json, the same as one line of JSON, for programs.
|
|
374
|
+
task_asks() {
|
|
375
|
+
local json=0 a
|
|
376
|
+
local args=()
|
|
377
|
+
for a in "$@"; do
|
|
378
|
+
case $a in
|
|
379
|
+
--json) json=1 ;;
|
|
380
|
+
-*) die "$ASKS_USAGE" ;;
|
|
381
|
+
*) args+=("$a") ;;
|
|
382
|
+
esac
|
|
383
|
+
done
|
|
384
|
+
set -- ${args[@]+"${args[@]}"}
|
|
385
|
+
local name=${1:-} dir list
|
|
386
|
+
[ -n "$name" ] && [ "$#" = 1 ] || die "$ASKS_USAGE"
|
|
387
|
+
space_named "$name" || die "there is no ocnus space called '$name'. See: ocnus space ls"
|
|
388
|
+
dir=$(cd -P "$SPACES/$name" && pwd)
|
|
389
|
+
have_sqlite || die "$SQLITE is not installed, so no hand-offs have been recorded on this machine."
|
|
390
|
+
command -v jq >/dev/null 2>&1 || die "jq is not installed, so the questions cannot be listed. Install it."
|
|
391
|
+
list=$(asks_collect "$name" "$dir") || exit 1
|
|
392
|
+
list=$(printf '%s' "$list" | jq -cs --arg space "$name" \
|
|
393
|
+
"{space: \$space, questions: (sort_by(.asked, .id) | map($ASK_PUBLIC))}") ||
|
|
394
|
+
die "could not put the questions together with jq."
|
|
395
|
+
if [ "$json" = 1 ]; then
|
|
396
|
+
printf '%s\n' "$list"
|
|
397
|
+
return 0
|
|
398
|
+
fi
|
|
399
|
+
|
|
400
|
+
# Text is anyone's: a newline in a command would start what looks like
|
|
401
|
+
# another line of the list, and an escape sequence would reach the
|
|
402
|
+
# terminal, so each control character becomes a space (PLAIN_DEF, in
|
|
403
|
+
# linear.sh). --json keeps the text as it is.
|
|
404
|
+
local sep=$'\x1f' rows count=0 card kind id at seconds running text summary options pulls line
|
|
405
|
+
rows=$(printf '%s' "$list" | jq -r "$PLAIN_DEF"'
|
|
406
|
+
.questions[] | [(.card | plain), .kind, (.id | plain), (.at | plain), (.seconds | tostring),
|
|
407
|
+
(.running | tostring), (.text | plain), (.summary // "" | plain), (.options | join(" | ") | plain),
|
|
408
|
+
((.pulls // []) | join(" ") | plain)]
|
|
409
|
+
| join("\u001f")') || die "could not put the questions together with jq."
|
|
410
|
+
while IFS=$sep read -r card kind id at seconds running text summary options pulls; do
|
|
411
|
+
[ -n "$card" ] || continue
|
|
412
|
+
[ "$count" -gt 0 ] || say "Questions waiting on you in '$name', oldest first"
|
|
413
|
+
count=$((count + 1))
|
|
414
|
+
took_text "$seconds"
|
|
415
|
+
line="$card $kind $id asked $at, $TOOK ago"
|
|
416
|
+
[ "$running" != false ] || line="$line, agent not running"
|
|
417
|
+
say "$line"
|
|
418
|
+
say " $text"
|
|
419
|
+
[ -z "$summary" ] || say " summary: $summary"
|
|
420
|
+
[ -z "$pulls" ] || say " pull requests: $pulls"
|
|
421
|
+
say " options: $options"
|
|
422
|
+
done <<< "$rows"
|
|
423
|
+
if [ "$count" = 0 ]; then
|
|
424
|
+
say "No questions waiting on you in '$name'."
|
|
425
|
+
return 0
|
|
426
|
+
fi
|
|
427
|
+
say "Answer one with: ocnus task answer $name <card> <question> <answer>, where <question> is the id after its kind, and <answer> is one of its options, or your own words for any kind but a permission prompt."
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
# --- answering --------------------------------------------------------------
|
|
431
|
+
|
|
432
|
+
# Whether this runs inside the session of an agent ocnus started: claude
|
|
433
|
+
# with one of its profiles, which Claude Code names to what the agent runs as
|
|
434
|
+
# CLAUDE_CODE_AGENT, or any harness `task build` started, which it names the
|
|
435
|
+
# space and the card. The env file can set none of the three (load_env, in
|
|
436
|
+
# cli/ocnus).
|
|
437
|
+
answer_from_agent() {
|
|
438
|
+
case ${CLAUDE_CODE_AGENT:-} in "$CARD_PROFILE" | "$BUILD_PROFILE") return 0 ;; esac
|
|
439
|
+
[ -n "${OCNUS_CARD:-}" ] || [ -n "${OCNUS_SPACE:-}" ]
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
# What an answer in progress leaves to be removed as it ends: its lock, and a
|
|
443
|
+
# folder for GitHub's answers.
|
|
444
|
+
ANSWER_LOCK='' ANSWER_SCRATCH=''
|
|
445
|
+
answer_done() {
|
|
446
|
+
[ -z "$ANSWER_SCRATCH" ] || rm -rf "$ANSWER_SCRATCH" 2>/dev/null || true
|
|
447
|
+
[ -n "$ANSWER_LOCK" ] || return 0
|
|
448
|
+
rm -f "$ANSWER_LOCK" 2>/dev/null || true
|
|
449
|
+
asks_tidy "${ANSWER_LOCK%/*}"
|
|
450
|
+
ANSWER_LOCK=''
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
# Take the lock on a hand-off's question, so that two answers given at once
|
|
454
|
+
# are not both sent: a file holding this run's pid, made only when there is
|
|
455
|
+
# none. One left by an answer that was stopped part-way, whose process has
|
|
456
|
+
# gone, is replaced. It fails while another answer holds it.
|
|
457
|
+
# answer_lock <space> <card> <question>
|
|
458
|
+
answer_lock() {
|
|
459
|
+
local lock=$ASKS_DIR/$1/$2/$3.lock pid=''
|
|
460
|
+
mkdir -p "${lock%/*}" 2>/dev/null || return 1
|
|
461
|
+
if ! (set -o noclobber; printf '%s\n' "$$" > "$lock") 2>/dev/null; then
|
|
462
|
+
{ read -r pid < "$lock"; } 2>/dev/null || pid=''
|
|
463
|
+
case $pid in '' | *[!0-9]*) pid='' ;; esac
|
|
464
|
+
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then return 1; fi
|
|
465
|
+
rm -f "$lock" 2>/dev/null || true
|
|
466
|
+
(set -o noclobber; printf '%s\n' "$$" > "$lock") 2>/dev/null || return 1
|
|
467
|
+
fi
|
|
468
|
+
ANSWER_LOCK=$lock
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
# Stop unless the hand-off question still waits: it is the card's latest
|
|
472
|
+
# hand-off, and that is needs-human. ASK_STEP and ASK_OPTIONS are its step
|
|
473
|
+
# and its options. An id that was a question of the card's says why it no
|
|
474
|
+
# longer waits.
|
|
475
|
+
# answer_waits <space> <card> <question>
|
|
476
|
+
ASK_STEP='' ASK_OPTIONS=''
|
|
477
|
+
answer_waits() {
|
|
478
|
+
local name=$1 card=$2 id=$3 s c sep=$'\x1f' row='' last='' last_status='' status=''
|
|
479
|
+
ASK_STEP='' ASK_OPTIONS=''
|
|
480
|
+
s=$(sql_str "$name") c=$(sql_str "$card")
|
|
481
|
+
if [ -e "$STORE" ]; then
|
|
482
|
+
row=$(store -separator "$sep" "$STORE" "select
|
|
483
|
+
coalesce((select id from handoffs where space = $s and card = $c order by id desc limit 1), ''),
|
|
484
|
+
coalesce((select status from handoffs where space = $s and card = $c order by id desc limit 1), ''),
|
|
485
|
+
coalesce((select status from handoffs where space = $s and card = $c and id = $id), ''),
|
|
486
|
+
coalesce((select step from handoffs where space = $s and card = $c and id = $id), ''),
|
|
487
|
+
coalesce((select options from handoffs where space = $s and card = $c and id = $id), '');") ||
|
|
488
|
+
die "could not read the hand-offs in $STORE. It may not be ocnus's store."
|
|
489
|
+
fi
|
|
490
|
+
IFS=$sep read -r last last_status status ASK_STEP ASK_OPTIONS <<< "$row"
|
|
491
|
+
[ "$status" = needs-human ] ||
|
|
492
|
+
die "$card has no question $id in '$name', so nothing was sent or merged. See what waits with: ocnus task asks $name"
|
|
493
|
+
[ "$last" != "$id" ] || return 0
|
|
494
|
+
[ "$last_status" != needs-human ] ||
|
|
495
|
+
die "$card's question $id no longer waits: its agent has asked another since, question $last. Nothing was sent or merged. See it with: ocnus task asks $name"
|
|
496
|
+
die "$card's question $id no longer waits: it was answered already, in the tab or by an earlier answer. Nothing was sent or merged."
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
# The first terminal of the card's tab whose turn has ended, as hc says: the
|
|
500
|
+
# write-up phase's tab, named after the card, until the work starts, then the
|
|
501
|
+
# task's, as card_seen picks it.
|
|
502
|
+
# answer_terminal <card> <the card in lower case> <step>
|
|
503
|
+
answer_terminal() {
|
|
504
|
+
local card=$1 low=$2 step=$3 sep=$'\x1f' name terms t writeup=0
|
|
505
|
+
case $step in new | define | plan | breakdown) writeup=1 ;; esac
|
|
506
|
+
while IFS=$sep read -r name terms; do
|
|
507
|
+
[ -n "$name" ] || continue
|
|
508
|
+
if [ "$writeup" = 1 ]; then
|
|
509
|
+
[ "$name" = "$card" ] || continue
|
|
510
|
+
else
|
|
511
|
+
case $name in "$low" | "$low"-*) ;; *) continue ;; esac
|
|
512
|
+
fi
|
|
513
|
+
for t in $terms; do
|
|
514
|
+
case $(terminal_doing "$t") in idle | done) printf '%s\n' "$t"; return 0 ;; esac
|
|
515
|
+
done
|
|
516
|
+
done <<< "$AGENT_TABS"
|
|
517
|
+
return 1
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
# Send an answer to the agent of a hand-off's question, as if typed in its
|
|
521
|
+
# tab, and stop unless the agent starts on it. It goes only to a terminal
|
|
522
|
+
# whose turn has ended, so the answer counts as taken once that terminal says
|
|
523
|
+
# the agent is working, or blocked, as one that has come to a prompt in the
|
|
524
|
+
# turn the answer began.
|
|
525
|
+
# answer_send <space> <the space's folder> <card> <question> <step> <answer>
|
|
526
|
+
answer_send() {
|
|
527
|
+
local name=$1 dir=$2 card=$3 id=$4 step=$5 answer=$6 low t='' n=0 why
|
|
528
|
+
low=$(printf '%s' "$card" | tr '[:upper:]' '[:lower:]')
|
|
529
|
+
command -v jq >/dev/null 2>&1 ||
|
|
530
|
+
die "jq is not installed, so $card's agent could not be found. Nothing was sent."
|
|
531
|
+
have_hc || die "Herdr's hc is not installed, so $card's agent could not be found. Nothing was sent."
|
|
532
|
+
answer_lock "$name" "$card" "$id" ||
|
|
533
|
+
die "another answer to $card's question $id is being given now. Nothing was sent."
|
|
534
|
+
agents_read "$name" "$dir"
|
|
535
|
+
while :; do
|
|
536
|
+
card_seen "$card" "$low" "$step"
|
|
537
|
+
case $AGENT_SEEN in
|
|
538
|
+
idle | done)
|
|
539
|
+
t=$(answer_terminal "$card" "$low" "$step") && break
|
|
540
|
+
;;
|
|
541
|
+
working) ;;
|
|
542
|
+
closed | none)
|
|
543
|
+
die "$card's agent is not running: its tab is closed, or holds its own shell alone. An answer goes to a running agent, so nothing was sent. Start it again in its tab first, or answer there."
|
|
544
|
+
;;
|
|
545
|
+
blocked)
|
|
546
|
+
die "$card's agent waits on a prompt in its tab, so it cannot take the answer yet, and nothing was sent. Answer that prompt first: ocnus task asks $name"
|
|
547
|
+
;;
|
|
548
|
+
error)
|
|
549
|
+
die "$card's agent hit an error in its tab, so nothing was sent. Look in its tab."
|
|
550
|
+
;;
|
|
551
|
+
*)
|
|
552
|
+
die "what $card's agent is doing is not known: its tab runs a program that reports no status, or hc cannot answer for it. Nothing was sent."
|
|
553
|
+
;;
|
|
554
|
+
esac
|
|
555
|
+
[ "$n" -lt "$ANSWER_TURN_WAIT" ] ||
|
|
556
|
+
die "$card's agent is still working after $ANSWER_TURN_WAIT seconds, so nothing was sent. It may have taken an earlier answer. Try again once its turn has ended."
|
|
557
|
+
[ "$n" -gt 0 ] || say "$card's agent is still ending its turn: waiting up to $ANSWER_TURN_WAIT seconds for it"
|
|
558
|
+
sleep 1 || die "stopped while waiting for $card's agent. Nothing was sent."
|
|
559
|
+
n=$((n + 1))
|
|
560
|
+
done
|
|
561
|
+
# The agent may have recorded an answer, or asked again, while its turn ended.
|
|
562
|
+
[ "$n" = 0 ] || answer_waits "$name" "$card" "$id"
|
|
563
|
+
|
|
564
|
+
why=$(hc_cmd send "$t" "$answer" 2>&1 >/dev/null) || {
|
|
565
|
+
why=$(printf '%s' "$why" | tr -s '\n' ' ')
|
|
566
|
+
die "hc did not send the answer to $card's agent in $t${why:+ (${why% })}. Nothing was sent."
|
|
567
|
+
}
|
|
568
|
+
n=0
|
|
569
|
+
while :; do
|
|
570
|
+
case $(terminal_doing "$t") in working | blocked) break ;; esac
|
|
571
|
+
[ "$n" -lt "$ANSWER_START_WAIT" ] ||
|
|
572
|
+
die "the answer was sent to $card's agent in $t, but the agent has not started on it after $ANSWER_START_WAIT seconds. Look in its tab: the answer may still be in its prompt box."
|
|
573
|
+
sleep 1 || die "stopped while waiting for $card's agent to start on the answer. Look in its tab."
|
|
574
|
+
n=$((n + 1))
|
|
575
|
+
done
|
|
576
|
+
say "$card's agent, in $t, took the answer: $answer"
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
# How a merge method reads in a sentence.
|
|
580
|
+
# merge_how <MERGE|SQUASH|REBASE>
|
|
581
|
+
merge_how() {
|
|
582
|
+
case $1 in
|
|
583
|
+
SQUASH) printf 'by squash' ;;
|
|
584
|
+
REBASE) printf 'by rebase' ;;
|
|
585
|
+
*) printf 'with a merge commit' ;;
|
|
586
|
+
esac
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
# Answer a merge with merge: merge each open pull request of the card's task
|
|
590
|
+
# on GitHub. One call asks what the merge of each branch's newest pull
|
|
591
|
+
# request needs (github_mergeable). Only when GitHub would merge every open
|
|
592
|
+
# one as it stands, clean, or with checks that are not required still to
|
|
593
|
+
# pass, is each merged, by the method GitHub's merge button would use for the
|
|
594
|
+
# human in its repo and at the commit that call saw (github_merge). One
|
|
595
|
+
# closed without merging, in conflict, blocked, behind, a draft, or not
|
|
596
|
+
# worked out yet stops it with none merged, naming it and why. Then the
|
|
597
|
+
# watch is woken to record the merge. It merges whatever the agent is doing,
|
|
598
|
+
# running or not: the merge is the human's own.
|
|
599
|
+
# answer_merge <space> <the space's folder> <card>
|
|
600
|
+
answer_merge() {
|
|
601
|
+
local name=$1 dir=$2 card=$3 tdir sep=$'\x1f' j
|
|
602
|
+
command -v gh >/dev/null 2>&1 ||
|
|
603
|
+
die "gh is not installed, so $card's pull requests could not be merged. Install it (https://cli.github.com), sign in with: gh auth login. Nothing was merged."
|
|
604
|
+
command -v jq >/dev/null 2>&1 || die "jq is not installed, so GitHub's answers cannot be read. Nothing was merged."
|
|
605
|
+
tdir=$(task_folder "$dir" "$card") || exit 1
|
|
606
|
+
task_worktrees "$dir" "$tdir" "$card" || die "$TASK_WHY Nothing was merged."
|
|
607
|
+
ANSWER_SCRATCH=$(mktemp -d) || die "could not make a folder for GitHub's answers. Nothing was merged."
|
|
608
|
+
local scratch=$ANSWER_SCRATCH
|
|
609
|
+
: > "$scratch/branches"
|
|
610
|
+
for ((j = 0; j < ${#TASK_WTS[@]}; j++)); do
|
|
611
|
+
printf '%s\n' "$j$sep${TASK_WTS[j]}$sep${TASK_BRANCHES[j]}" >> "$scratch/branches"
|
|
612
|
+
done
|
|
613
|
+
GITHUB_SIGN_IN=0 GITHUB_CALL_WHY=''
|
|
614
|
+
github_mergeable "$scratch" < "$scratch/branches" > "$scratch/pulls"
|
|
615
|
+
|
|
616
|
+
# What GitHub said of each worktree's branch: the open pull requests it
|
|
617
|
+
# would merge as they stand, and what stands in the way of any other.
|
|
618
|
+
local key state rest status method pull oid url why merged=0 problems=''
|
|
619
|
+
local ids=() methods=() oids=() urls=()
|
|
620
|
+
while read -r key state rest; do
|
|
621
|
+
why='' url=$rest
|
|
622
|
+
case $state in
|
|
623
|
+
none | '') ;;
|
|
624
|
+
merged) merged=$((merged + 1)) ;;
|
|
625
|
+
closed) why='it was closed without merging' ;;
|
|
626
|
+
open)
|
|
627
|
+
read -r status method pull oid url <<< "$rest"
|
|
628
|
+
case $status in
|
|
629
|
+
CLEAN | HAS_HOOKS | UNSTABLE) ids+=("$pull") methods+=("$method") oids+=("$oid") urls+=("$url") ;;
|
|
630
|
+
DIRTY) why='it is in conflict with its base branch' ;;
|
|
631
|
+
BLOCKED) why='GitHub blocks its merge, as with a required check not passed or a required review missing' ;;
|
|
632
|
+
BEHIND) why='it is behind its base branch, and its repo wants it up to date first' ;;
|
|
633
|
+
DRAFT) why='it is a draft' ;;
|
|
634
|
+
*) why='GitHub has not worked out yet whether it can be merged; try again in a moment' ;;
|
|
635
|
+
esac
|
|
636
|
+
;;
|
|
637
|
+
*)
|
|
638
|
+
# failed <the worktree's folder>: <why>
|
|
639
|
+
url="the pull request of ${rest%%: *}" why="GitHub could not be asked about it (${rest#*: })"
|
|
640
|
+
;;
|
|
641
|
+
esac
|
|
642
|
+
[ -z "$why" ] || problems="$problems"$'\n'" $url: $why"
|
|
643
|
+
done < "$scratch/pulls"
|
|
644
|
+
[ "$GITHUB_SIGN_IN" = 0 ] ||
|
|
645
|
+
problems="$problems"$'\n'" gh needs signing in to GitHub: it is signed out, or GitHub refused its token. Sign in with: gh auth login"
|
|
646
|
+
[ -z "$problems" ] ||
|
|
647
|
+
die "$card's pull requests were not merged, since GitHub would not merge every one of them as it stands:$problems
|
|
648
|
+
None was merged, nothing was forced and no auto-merge was turned on."
|
|
649
|
+
if [ "${#ids[@]}" = 0 ]; then
|
|
650
|
+
[ "$merged" -gt 0 ] || die "$card has no pull request to merge: GitHub has none on its task's branch. Nothing was merged."
|
|
651
|
+
merges_wake
|
|
652
|
+
die "$card's pull requests have merged already, so nothing was merged. ocnus's watch records the merge within a minute."
|
|
653
|
+
fi
|
|
654
|
+
|
|
655
|
+
local done_urls=''
|
|
656
|
+
for ((j = 0; j < ${#ids[@]}; j++)); do
|
|
657
|
+
if ! github_merge "$scratch" "${ids[j]}" "${methods[j]}" "${oids[j]}"; then
|
|
658
|
+
die "GitHub did not merge ${urls[j]}: $GITHUB_MERGE_WHY${done_urls:+. Merged before it:$done_urls}. Nothing was forced, and no auto-merge was turned on."
|
|
659
|
+
fi
|
|
660
|
+
say "merged ${urls[j]} $(merge_how "${methods[j]}")"
|
|
661
|
+
done_urls="$done_urls ${urls[j]}"
|
|
662
|
+
done
|
|
663
|
+
merges_wake
|
|
664
|
+
say "Every open pull request of $card has merged. ocnus's watch records the merge and finishes the task, within a minute."
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
# Answer a permission prompt with one of its options: leave the option beside
|
|
668
|
+
# the hook's question, only when no answer is there, and wait for the hook to
|
|
669
|
+
# take it. An option the hook does not take within ANSWER_HOOK_WAIT seconds,
|
|
670
|
+
# as when the prompt was answered in the tab meanwhile, is withdrawn, so that
|
|
671
|
+
# nothing is allowed or refused later.
|
|
672
|
+
# answer_permission <space> <card> <question> <answer>
|
|
673
|
+
answer_permission() {
|
|
674
|
+
local name=$1 card=$2 id=$3 answer=$4 dir=$ASKS_DIR/$1/$2 q pid term options n=0 gone
|
|
675
|
+
gone="$card has no permission prompt $id waiting: it was answered already, in the tab or by an earlier answer, or its agent has gone on. Nothing was sent. See what waits with: ocnus task asks $name"
|
|
676
|
+
command -v jq >/dev/null 2>&1 || die "jq is not installed, so the prompt cannot be read. Nothing was sent."
|
|
677
|
+
have_hc || die "Herdr's hc is not installed, so the prompt's terminal cannot be asked about. Nothing was sent."
|
|
678
|
+
[[ $id =~ ^p[0-9a-f]+$ ]] && [ -f "$dir/$id.json" ] || die "$gone"
|
|
679
|
+
q=$(jq -c 'select(type == "object")' "$dir/$id.json" 2>/dev/null) && [ -n "$q" ] || die "$gone"
|
|
680
|
+
pid=$(printf '%s' "$q" | jq -r '.pid // empty') term=$(printf '%s' "$q" | jq -r '.terminal // empty')
|
|
681
|
+
ask_alive "$pid" && [ -n "$term" ] || die "$gone"
|
|
682
|
+
[ "$(terminal_doing "$term")" = blocked ] ||
|
|
683
|
+
die "$card's permission prompt $id no longer waits: its terminal, $term, is no longer at a prompt, so it was answered in the tab. Nothing was sent."
|
|
684
|
+
options="$ASK_ALLOW | $ASK_REFUSE"
|
|
685
|
+
[ "$(printf '%s' "$q" | jq -r '.always == true')" != true ] || options="$options | $ASK_ALWAYS"
|
|
686
|
+
case " | $options | " in
|
|
687
|
+
*" | $answer | "*) ;;
|
|
688
|
+
*) die "a permission prompt takes only one of its options, not your own words: $options. Nothing was sent." ;;
|
|
689
|
+
esac
|
|
690
|
+
# The option is written whole beside the question, then linked into place,
|
|
691
|
+
# which fails when an answer is there: the hook never reads half of one,
|
|
692
|
+
# and two answers given at once are not both left.
|
|
693
|
+
printf '%s\n' "$answer" > "$dir/$id.answer.$$" 2>/dev/null ||
|
|
694
|
+
die "could not write the answer in $dir. Nothing was sent."
|
|
695
|
+
if ! ln "$dir/$id.answer.$$" "$dir/$id.answer" 2>/dev/null; then
|
|
696
|
+
rm -f "$dir/$id.answer.$$" 2>/dev/null || true
|
|
697
|
+
die "$card's permission prompt $id has an answer already, which its hook is taking. Nothing was sent."
|
|
698
|
+
fi
|
|
699
|
+
rm -f "$dir/$id.answer.$$" 2>/dev/null || true
|
|
700
|
+
while [ ! -e "$dir/$id.taken" ]; do
|
|
701
|
+
if [ ! -e "$dir/$id.json" ] || [ "$n" -ge "$ANSWER_HOOK_WAIT" ]; then
|
|
702
|
+
# Withdrawn by renaming it, which fails when the hook took it first.
|
|
703
|
+
if mv "$dir/$id.answer" "$dir/$id.answer.$$" 2>/dev/null; then
|
|
704
|
+
rm -f "$dir/$id.answer.$$" 2>/dev/null || true
|
|
705
|
+
asks_tidy "$dir"
|
|
706
|
+
[ -e "$dir/$id.json" ] ||
|
|
707
|
+
die "$card's permission prompt $id no longer waits: it was answered in the tab, or its agent has gone on, before its hook took the answer. Nothing was allowed or refused."
|
|
708
|
+
die "the hook of $card's permission prompt $id did not take the answer within $ANSWER_HOOK_WAIT seconds, so the answer was withdrawn: nothing was allowed or refused. The prompt still waits in its tab."
|
|
709
|
+
fi
|
|
710
|
+
break
|
|
711
|
+
fi
|
|
712
|
+
sleep 1 || true
|
|
713
|
+
n=$((n + 1))
|
|
714
|
+
done
|
|
715
|
+
rm -f "$dir/$id.taken" 2>/dev/null || true
|
|
716
|
+
asks_tidy "$dir"
|
|
717
|
+
say "Claude Code was given the answer to $card's permission prompt: $answer"
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
# Answer one question, the one `task asks` listed under that id, with one of
|
|
721
|
+
# its options or, for any kind but a permission prompt, the human's own
|
|
722
|
+
# words. Words after the first are joined to it, so the answer need not be
|
|
723
|
+
# quoted. It exits zero only once the agent has taken the answer, the hook
|
|
724
|
+
# has, or the pull requests have merged.
|
|
725
|
+
task_answer() {
|
|
726
|
+
local name=${1:-} card=${2:-} id=${3:-} answer plain dir
|
|
727
|
+
[ "$#" -ge 4 ] && [ -n "$name" ] && [ -n "$id" ] || die "$ANSWER_USAGE"
|
|
728
|
+
shift 3
|
|
729
|
+
answer=$*
|
|
730
|
+
! answer_from_agent ||
|
|
731
|
+
die "this runs inside the session of an agent ocnus started, and only the human answers a question. Nothing was sent and nothing was merged."
|
|
732
|
+
space_named "$name" || die "there is no ocnus space called '$name'. See: ocnus space ls"
|
|
733
|
+
dir=$(cd -P "$SPACES/$name" && pwd)
|
|
734
|
+
card=$(handoff_card "$name" "$card" answer) || exit 1
|
|
735
|
+
answer=${answer#"${answer%%[![:space:]]*}"}
|
|
736
|
+
answer=${answer%"${answer##*[![:space:]]}"}
|
|
737
|
+
[ -n "$answer" ] || die "the answer is empty. $ANSWER_USAGE"
|
|
738
|
+
# A line break is the human's own, in their own words. Any other control
|
|
739
|
+
# character, such as an escape, would be typed into the agent's terminal.
|
|
740
|
+
plain=${answer//$'\n'/}
|
|
741
|
+
plain=${plain//$'\t'/}
|
|
742
|
+
case $plain in
|
|
743
|
+
*[[:cntrl:]]*) die "the answer has a control character in it. Give plain text. Nothing was sent." ;;
|
|
744
|
+
esac
|
|
745
|
+
trap answer_done EXIT
|
|
746
|
+
case $id in
|
|
747
|
+
p*) answer_permission "$name" "$card" "$id" "$answer" ;;
|
|
748
|
+
*[!0-9]*)
|
|
749
|
+
die "'$id' is not a question's id. See the questions and their ids with: ocnus task asks $name"
|
|
750
|
+
;;
|
|
751
|
+
*)
|
|
752
|
+
have_sqlite || die "$SQLITE is not installed, so no hand-offs are recorded on this machine."
|
|
753
|
+
answer_waits "$name" "$card" "$id"
|
|
754
|
+
ask_kind "$ASK_STEP" "$ASK_OPTIONS"
|
|
755
|
+
if [ "$ASK_KIND" = merge ] && [ "$answer" = merge ]; then
|
|
756
|
+
answer_merge "$name" "$dir" "$card"
|
|
757
|
+
else
|
|
758
|
+
answer_send "$name" "$dir" "$card" "$id" "$ASK_STEP" "$answer"
|
|
759
|
+
fi
|
|
760
|
+
;;
|
|
761
|
+
esac
|
|
762
|
+
}
|