@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.
Files changed (57) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +988 -2
  3. package/agents/linear-mcp.json +1 -0
  4. package/agents/ocnus-build.md +23 -0
  5. package/agents/ocnus-write-up.md +20 -0
  6. package/cli/host-mark +36 -0
  7. package/cli/lib/agents.sh +350 -0
  8. package/cli/lib/approval.sh +94 -0
  9. package/cli/lib/archive.sh +447 -0
  10. package/cli/lib/billing.sh +166 -0
  11. package/cli/lib/close.sh +189 -0
  12. package/cli/lib/commands.sh +220 -0
  13. package/cli/lib/common.sh +215 -0
  14. package/cli/lib/finish.sh +288 -0
  15. package/cli/lib/github.sh +162 -0
  16. package/cli/lib/guard.sh +134 -0
  17. package/cli/lib/handoff.sh +551 -0
  18. package/cli/lib/host.sh +1802 -0
  19. package/cli/lib/linear.sh +1389 -0
  20. package/cli/lib/links.sh +135 -0
  21. package/cli/lib/merges.sh +853 -0
  22. package/cli/lib/recipes.sh +54 -0
  23. package/cli/lib/settings.sh +465 -0
  24. package/cli/lib/space.sh +343 -0
  25. package/cli/lib/task.sh +847 -0
  26. package/cli/lib/tests.sh +268 -0
  27. package/cli/lib/trust.sh +70 -0
  28. package/cli/ocnus +630 -0
  29. package/herdr/README.md +2238 -0
  30. package/herdr/layouts/default.json +259 -0
  31. package/herdr/mods/agent-settings/cog.svg +4 -0
  32. package/herdr/mods/agent-settings/main.js +356 -0
  33. package/herdr/mods/agent-settings/mod.json +1 -0
  34. package/herdr/mods/cards/main.js +450 -0
  35. package/herdr/mods/cards/mod.json +1 -0
  36. package/herdr/mods/cards/refresh.svg +4 -0
  37. package/herdr/mods/cpu/main.js +90 -0
  38. package/herdr/mods/cpu/mod.json +1 -0
  39. package/herdr/mods/layout-switch/main.js +48 -0
  40. package/herdr/mods/layout-switch/mod.json +1 -0
  41. package/herdr/mods/names/main.js +69 -0
  42. package/herdr/mods/names/mod.json +1 -0
  43. package/herdr/mods/ocnus-spaces/main.js +376 -0
  44. package/herdr/mods/ocnus-spaces/mod.json +1 -0
  45. package/herdr/mods/steps/main.js +354 -0
  46. package/herdr/mods/steps/mod.json +1 -0
  47. package/herdr/spaces/template/SPACE.md +19 -0
  48. package/package.json +27 -4
  49. package/prompts/task-build.md +11 -0
  50. package/prompts/task-new.md +17 -0
  51. package/skills/brief/SKILL.md +64 -0
  52. package/skills/sdlc-breakdown/SKILL.md +312 -0
  53. package/skills/sdlc-build/SKILL.md +182 -0
  54. package/skills/sdlc-define/SKILL.md +203 -0
  55. package/skills/sdlc-plan/SKILL.md +202 -0
  56. package/skills/sdlc-pr/SKILL.md +339 -0
  57. package/skills/sdlc-review/SKILL.md +227 -0
@@ -0,0 +1,551 @@
1
+ # Hand-offs: how each step of a task ended, recorded on this machine. Sourced
2
+ # by cli/ocnus.
3
+
4
+ # A step of the lifecycle ends with a handoff block: which step it was, how it
5
+ # ended and what comes next. An agent runs `task handoff` with the block on
6
+ # stdin each time a step ends or pauses. The rules for a block are here, once;
7
+ # the skills only say to run the command.
8
+
9
+ # The store: one SQLite file beside the spaces, so it is never published and
10
+ # an npm reinstall does not delete it.
11
+ STORE=$ENT_HOME/ocnus.db
12
+ # The command that reads and writes it: one word, a command on PATH.
13
+ SQLITE=${OCNUS_SQLITE:-sqlite3}
14
+
15
+ # The lines a block can have, in the order they are printed. approved is on
16
+ # breakdown's done block with start next alone, as `approved: setting`, when
17
+ # the setting approved the card with the breakdown approval off (approval.sh),
18
+ # so that the log can tell it from the human's approval, which is their
19
+ # answered row.
20
+ HANDOFF_KEYS='step status next summary flags approved question options answer reason'
21
+ # The steps a block can name as its step or as what comes next. start is the
22
+ # step after breakdown: `ocnus task start`, which records its own row as
23
+ # start (record_start). Breakdown hands on to it once the human approves the
24
+ # card, or the setting does, but it is only ever what comes next in a block, since it is a command
25
+ # that records its own row, not a skill: the write-up agent runs it, or the
26
+ # human does when they hold the build phase for later. A review that finds the work done hands on to pr, the step that pushes
27
+ # the task's branch, opens its pull requests and waits for the human to merge
28
+ # them. merged is where a card ends: pr hands on to it once every pull request
29
+ # has merged. It too is only ever what comes next, since nothing runs after it.
30
+ HANDOFF_STEPS='define plan breakdown start build review pr merged'
31
+ # A block is a summary. A line longer than this is not one, and a very long
32
+ # line is slow to check and too big to hand to sqlite3 as an argument.
33
+ HANDOFF_LINE_MAX=2000
34
+
35
+ HANDOFF_USAGE="Usage: ocnus task handoff <space> <card>, with the block on stdin:
36
+
37
+ ocnus task handoff <space> <card> <<'EOF'
38
+ step: define
39
+ status: done
40
+ next: plan
41
+ summary: One to three sentences on what was done.
42
+ EOF
43
+
44
+ status is done (needs next and summary, may have flags), needs-human (needs
45
+ question and options, may have summary), answered (needs answer: what the
46
+ human replied to a needs-human hand-off) or failed (needs reason). step and
47
+ next are one of: $HANDOFF_STEPS.
48
+ start and merged are only ever next: \`ocnus task start\` records start,
49
+ and merged is where a card ends. approved: setting goes on breakdown's done
50
+ block with next: start alone, when the setting approved the card, and is
51
+ refused while the breakdown approval is on: the human approves the card then.
52
+ Every value goes on one line."
53
+
54
+ have_sqlite() { command -v "$SQLITE" >/dev/null 2>&1; }
55
+
56
+ # Run one statement against the store.
57
+ # -init /dev/null: a person's own ~/.sqliterc must not change how answers are
58
+ # printed. .timeout: wait for another task's write to finish, not fail at once.
59
+ # -readonly in a worktree's copy that was not asked to act on the machine
60
+ # (guard.sh), so that nothing it runs can change the store, by mistake or not.
61
+ store() {
62
+ local ro=()
63
+ [ "$GUARDED" != 1 ] || ro=(-readonly)
64
+ "$SQLITE" -init /dev/null -batch -cmd '.timeout 5000' ${ro[@]+"${ro[@]}"} "$@" 2>/dev/null
65
+ }
66
+
67
+ # Print a value as an SQL string literal, or NULL when it is empty. Doubling
68
+ # the quote is SQLite's only escape, and no value holds a newline, so nothing
69
+ # in a value can end the literal or start a sqlite3 dot-command.
70
+ # LC_ALL=C: sed must not give up on text that is not valid in this locale.
71
+ sql_str() {
72
+ [ -n "$1" ] || { printf 'NULL'; return; }
73
+ printf "'%s'" "$(printf '%s' "$1" | LC_ALL=C sed "s/'/''/g")"
74
+ }
75
+
76
+ # Whether the store's table has the approved column. A store made before the
77
+ # approved line has none, until a hand-off is recorded in it.
78
+ store_has_approved() {
79
+ [ "$(store "$STORE" "select count(*) from pragma_table_info('handoffs') where name = 'approved';")" = 1 ]
80
+ }
81
+
82
+ # Make the store if this is its first use, and give a store made before the
83
+ # approved line its column. Two hand-offs at once may both find it missing:
84
+ # when adding it fails, it is there all the same if the other added it.
85
+ store_ready() {
86
+ mkdir -p "$ENT_HOME"
87
+ store "$STORE" 'create table if not exists handoffs (
88
+ id integer primary key, at text not null, space text not null,
89
+ card text not null, step text not null, status text not null,
90
+ next text, summary text, flags text, question text, options text,
91
+ answer text, reason text, approved text);' || return 1
92
+ store_has_approved ||
93
+ store "$STORE" 'alter table handoffs add column approved text;' ||
94
+ store_has_approved
95
+ }
96
+
97
+ # Record one hand-off, timed now in UTC, and print the new row's id, read in
98
+ # the same call so that it is this row's and not another task's. With
99
+ # --unless-merged, as `task handoff` records a block, nothing is recorded
100
+ # when the card's latest row is its merge, done with merged next, and it
101
+ # prints merged in place of an id. The check is in the insert itself, so
102
+ # nothing can record between them. `task new` and `task start` record their
103
+ # rows without it: `task new` starts a new run of the card.
104
+ # store_handoff [--unless-merged] <space> <card> <step> <status> <next> <summary> <flags> <question> <options> <answer> <reason> <approved>
105
+ store_handoff() {
106
+ local at values='' v guard=1
107
+ if [ "$1" = --unless-merged ]; then
108
+ shift
109
+ guard="coalesce((select status = 'done' and next = 'merged' from handoffs
110
+ where space = $(sql_str "$1") and card = $(sql_str "$2") order by id desc limit 1), 0) = 0"
111
+ fi
112
+ at=$(date -u +%Y-%m-%dT%H:%M:%SZ)
113
+ for v in "$at" "$@"; do values="$values${values:+, }$(sql_str "$v")"; done
114
+ store "$STORE" "insert into handoffs
115
+ (at, space, card, step, status, next, summary, flags, question, options, answer, reason, approved)
116
+ select $values where $guard;
117
+ select case when changes() = 1 then last_insert_rowid() else 'merged' end;"
118
+ }
119
+
120
+ # A note made one plain line for the store. The log reads a row a line at a
121
+ # time and prints it to a terminal, so each control character in it, such as
122
+ # a newline or an escape from Linear's text, becomes a space.
123
+ plain_note() { printf '%s' "${1//[[:cntrl:]]/ }"; }
124
+
125
+ # Note that work on a card has begun, so the first step's length can be read
126
+ # from the store. `task new` records a `new` row, which starts a run of the
127
+ # card, with define next. `task start` records a `start` row, with build next:
128
+ # it starts no run, so the write-up and the build are one run of the card. Its
129
+ # flags are the notes it printed that still need the human, so that `task log`
130
+ # shows them under the row; with none, it has no flags.
131
+ # Neither command is ever failed by it: it only says when it could not record.
132
+ # record_start <space> <card> [start [<flags>]]
133
+ record_start() {
134
+ local step=new next=define
135
+ [ "${3:-}" != start ] || step=start next=build
136
+ if ! have_sqlite; then
137
+ say " note $SQLITE is not installed, so hand-offs are not recorded"
138
+ return 0
139
+ fi
140
+ { store_ready && store_handoff "$1" "$2" "$step" done "$next" '' "$(plain_note "${4:-}")" '' '' '' '' '' >/dev/null; } ||
141
+ say " note could not record the start in $STORE"
142
+ }
143
+
144
+ # Is $1 one of the words in $2? A value with a space in it never is, so
145
+ # "define plan" does not pass as a step.
146
+ in_list() {
147
+ case $1 in '' | *[[:space:]]*) return 1 ;; esac
148
+ case " $2 " in *" $1 "*) return 0 ;; *) return 1 ;; esac
149
+ }
150
+
151
+ # Is there a space folder with exactly this name? On a Mac the folder demo
152
+ # also answers to Demo, and a hand-off recorded as Demo would never show in
153
+ # demo's log.
154
+ space_named() {
155
+ local d
156
+ for d in "$SPACES"/*/; do
157
+ d=${d%/}
158
+ [ "${d##*/}" != "$1" ] || return 0
159
+ done
160
+ return 1
161
+ }
162
+
163
+ # Check a card ID as the task commands take it, and print it in its usual
164
+ # upper-case form.
165
+ handoff_card() {
166
+ local name=$1 card=$2 what=$3
167
+ [ -n "$card" ] || die "which card? Usage: ocnus task $what $name <card>, like: ocnus task $what $name ABC-122"
168
+ [[ $card =~ $CARD_RE ]] || die "'$card' is not a card ID like ABC-122."
169
+ printf '%s' "$card" | tr '[:lower:]' '[:upper:]'
170
+ }
171
+
172
+ # Read a handoff block on stdin, refuse one that breaks the rules, record it
173
+ # and print it back with its lines in the usual order.
174
+ task_handoff() {
175
+ local name=${1:-} card=${2:-}
176
+ [ -n "$name" ] || die "$HANDOFF_USAGE"
177
+ # Not space_dir: its advice is to make the space, which is not for an
178
+ # agent to do in the middle of a step.
179
+ space_named "$name" ||
180
+ die "there is no ocnus space called '$name', so the hand-off was not recorded. Write the block in your message instead."
181
+ card=$(handoff_card "$name" "$card" handoff) || exit 1
182
+ [ "$#" -le 2 ] || die "$HANDOFF_USAGE"
183
+ [ ! -t 0 ] || die "$HANDOFF_USAGE"
184
+
185
+ local step='' status='' next='' summary='' flags='' approved='' question='' options='' answer='' reason=''
186
+ local line key value n=0 seen=' '
187
+ local re='^[[:space:]]*([a-z]+):[[:space:]]*(.*)$' blank='^[[:space:]]*$' hint='^<.*>$'
188
+ while IFS= read -r line || [ -n "$line" ]; do
189
+ n=$((n + 1))
190
+ [ "${#line}" -le "$HANDOFF_LINE_MAX" ] ||
191
+ die "line $n of the block is ${#line} characters long. A block is a summary: keep each line under $HANDOFF_LINE_MAX characters."
192
+ line=${line%$'\r'}
193
+ [[ $line =~ $blank ]] && continue
194
+ [[ $line =~ $re ]] ||
195
+ die "line $n of the block is not 'key: value'. Every value goes on one line."
196
+ key=${BASH_REMATCH[1]} value=${BASH_REMATCH[2]}
197
+ # Drop spaces after the value.
198
+ value=${value%"${value##*[![:space:]]}"}
199
+ # The line was called phase before it was called step.
200
+ [ "$key" != phase ] ||
201
+ die "a block names its step in a 'step:' line, not 'phase:': write 'step:' in its place."
202
+ in_list "$key" "$HANDOFF_KEYS" ||
203
+ die "a handoff block has no '$key:' line. Its lines are: $HANDOFF_KEYS."
204
+ ! in_list "$key" "$seen" || die "the block has '$key:' twice."
205
+ seen="$seen$key "
206
+ [ -n "$value" ] || die "'$key:' is empty. Leave the line out when there is nothing to say."
207
+ case $value in *[[:cntrl:]]*) die "'$key:' has a tab or another control character in it. Use plain text on one line." ;; esac
208
+ ! [[ $value =~ $hint ]] ||
209
+ die "'$key:' still holds a placeholder in angle brackets. Write what happened, or leave the line out."
210
+ printf -v "$key" '%s' "$value"
211
+ done
212
+ [ "$seen" != ' ' ] || die "there is no block on stdin. $HANDOFF_USAGE"
213
+
214
+ [ -n "$step" ] || die "the block has no 'step:' line, the step that is handing off."
215
+ in_list "$step" "$HANDOFF_STEPS" || die "'$step' is not a step. The steps are: $HANDOFF_STEPS."
216
+ [ "$step" != merged ] ||
217
+ die "'merged' is where a card ends, so it can only come next. The step that hands on to it is pr."
218
+ [ "$step" != start ] ||
219
+ die "'start' is recorded by \`ocnus task start\`, so it can only come next. The step that hands on to it is breakdown."
220
+ [ -n "$status" ] || die "the block has no 'status:' line. Use done, needs-human, answered or failed."
221
+ local need allow
222
+ case $status in
223
+ done) need='next summary' allow='flags' ;;
224
+ needs-human) need='question options' allow='summary' ;;
225
+ answered) need='answer' allow='' ;;
226
+ failed) need='reason' allow='' ;;
227
+ *) die "'$status' is not a status. Use done, needs-human, answered or failed." ;;
228
+ esac
229
+ for key in $need; do
230
+ [ -n "${!key}" ] || die "a hand-off with status $status needs a '$key:' line."
231
+ done
232
+ for key in next summary flags question options answer reason; do
233
+ [ -z "${!key}" ] || in_list "$key" "$need $allow" ||
234
+ die "a hand-off with status $status has no '$key:' line. Take it out."
235
+ done
236
+ [ -z "$next" ] || in_list "$next" "$HANDOFF_STEPS" ||
237
+ die "'$next' is not a step that can come next. The steps are: $HANDOFF_STEPS."
238
+ # The setting approves a card only at breakdown's approval, and only while
239
+ # the breakdown approval is off, as the settings file says now. When the
240
+ # file cannot be read the approval may be on, so it is refused then too.
241
+ if [ -n "$approved" ]; then
242
+ [ "$step $status $next" = 'breakdown done start' ] ||
243
+ die "'approved:' goes only on breakdown's done block with next: start, when the setting approved the card. The setting approves nothing else, and the human approves the card at breakdown: take the line out. Nothing was recorded."
244
+ [ "$approved" = setting ] ||
245
+ die "'approved:' can only be 'setting', for a card the setting approved with the breakdown approval off. When the human approves the card, their answer is the approval: take the line out. Nothing was recorded."
246
+ settings_load ||
247
+ die "the breakdown approval cannot be read, so the setting cannot approve the card: the human approves the card then. Ask them, with the 'approved:' line out. Nothing was recorded. $SETTINGS_WHY"
248
+ [ "$SET_APPROVAL_BREAKDOWN" = false ] ||
249
+ die "the breakdown approval is on, so the setting cannot approve the card: the human approves the card then. Ask them, with the 'approved:' line out. Nothing was recorded."
250
+ fi
251
+
252
+ have_sqlite ||
253
+ die "$SQLITE is not installed, so the hand-off was not recorded. Write the block in your message instead."
254
+ local id
255
+ { store_ready && id=$(store_handoff --unless-merged "$name" "$card" "$step" "$status" "$next" "$summary" "$flags" "$question" "$options" "$answer" "$reason" "$approved"); } ||
256
+ die "the hand-off was not recorded: could not write to $STORE. Write the block in your message instead."
257
+ # Once every pull request for the card has merged and that is recorded, by
258
+ # ocnus's watch on the merges (merges.sh) or by a hand-off, the card's
259
+ # run is over: a second record of the merge, or anything after it, would
260
+ # make the log say it twice.
261
+ [ "$id" != merged ] ||
262
+ die "$card's merge is recorded already, so this hand-off was not recorded. Every pull request for $card has merged, and task log shows $card as merged, so there is nothing more to record for it."
263
+ for key in $HANDOFF_KEYS; do
264
+ [ -z "${!key}" ] || printf '%s: %s\n' "$key" "${!key}"
265
+ done
266
+ # The card moves in Linear at two hand-offs. The hand-off is recorded by
267
+ # now, so a card that cannot be moved is a note, never a failure. It goes to
268
+ # stderr, so that stdout is the block as it was recorded.
269
+ # - The human has approved the card, and its sub-issues exist when it was
270
+ # sliced: sdlc-breakdown records this only then. The card, and each of
271
+ # its sub-issues, moves to its team's first not-started status. Each
272
+ # card that was not moved is added to the row's flags too, after any the
273
+ # agent gave, so that `task log` shows it under the row once the tab
274
+ # that said so has gone.
275
+ # - Every pull request for the card has merged, so its work is finished.
276
+ # Whatever records the merge, ocnus's watch on the merges or a
277
+ # hand-off, moves the card to Done, and a slice's parent once its
278
+ # slices are closed, and archives what is closed (merge_linear).
279
+ if [ "$status" = done ] && [ "$step" = breakdown ] && [ "$next" = start ]; then
280
+ local moves kept=''
281
+ moves=$(linear_move_note "$card" approved) || true
282
+ [ -z "$moves" ] || printf '%s\n' "$moves" >&2
283
+ while IFS= read -r line; do
284
+ [[ $line =~ $NOT_MOVED_RE ]] || continue
285
+ kept=${kept:+$kept; }$(plain_note "$line")
286
+ done <<< "$moves"
287
+ # The id is this row's, from sqlite3 itself; anything else is not used.
288
+ case $id in '' | *[!0-9]*) kept='' ;; esac
289
+ [ -z "$kept" ] ||
290
+ store "$STORE" "update handoffs set flags = coalesce(flags || '; ', '') || $(sql_str "$kept") where id = $id;" ||
291
+ printf 'could not add to the hand-off which cards were not moved, so task log does not show them\n' >&2
292
+ elif [ "$status" = done ] && [ "$next" = merged ]; then
293
+ merge_linear "$name" "$card" >&2
294
+ fi
295
+ # The pull requests wait on the human now, so ocnus's watch on the
296
+ # merges runs, to record their merge, or to wake the agent for one closed
297
+ # or in conflict.
298
+ [ "$step $status" != 'pr needs-human' ] || merges_wake
299
+ }
300
+
301
+ # Once every pull request for a card has merged and that is recorded, by
302
+ # ocnus's watch on the merges or by a hand-off, its work is
303
+ # finished: move the card to its team's first completed status, and for a
304
+ # slice, the card it was sliced from too, once every one of that card's
305
+ # sub-issues is closed; while any is open, the note names them. Then, with
306
+ # archiving on, archive the card, and every other closed card of the space's
307
+ # teams (archive.sh); with it off, Linear is asked nothing more. A card with
308
+ # a parent or sub-issues is archived with its family, the parent and its
309
+ # slices, once all of them are closed. The moves come first, so the merge of
310
+ # a parent's last open slice finds the family closed, and archives all of it.
311
+ # It says one line for each thing done or not done. A card that cannot be
312
+ # moved or archived is a note, never a failure, since the merge is recorded
313
+ # by now.
314
+ # merge_linear <space> <card ID, in upper case>
315
+ merge_linear() {
316
+ linear_move_note "$2" completed || true
317
+ linear_archive_note "$1" "$2" || true
318
+ }
319
+
320
+ # --- the log ----------------------------------------------------------------
321
+
322
+ # One query works out a card's view: each hand-off with its label, how long it
323
+ # took, whether it is a wait, and where the card stood after it. The plain log
324
+ # and the JSON both read it, so they cannot disagree, and a Herdr mod that
325
+ # draws the JSON formats nothing. With no card it works out the view of every
326
+ # card in the space at once, and the list of cards is each one's latest row.
327
+ #
328
+ # Nothing about time is stored beyond each row's own. A step's time is from
329
+ # the row before it to its own row. The time from a needs-human row to its
330
+ # answered row is waiting on the human and belongs to no step. A needs-human
331
+ # row with nothing after it is still waiting, measured to now. A `new` row
332
+ # starts a run, so it has no time, however long ago the run before it ended.
333
+ # A `start` row, from `task start`, shows as a `new` row does but starts no
334
+ # run: the time before it was the human's, and the build is timed from it.
335
+ # A card whose row is done with merged next stands at merged: its pull
336
+ # requests have merged, and nothing comes after.
337
+ #
338
+ # What Herdr says about each card's agent (agents.sh) is read for each run of
339
+ # the log and never stored: it is joined onto the view, as the table agents,
340
+ # after the view has worked out every step and its time, so no row and no
341
+ # step's time depends on it. So is what ocnus's watch on the merges last
342
+ # saw of a card whose pull requests wait on the human (merges_seen): files in
343
+ # the merges folder beside the store, merges/.alive, the time its latest pass
344
+ # began, and merges/<space>/<card>, the time and what that pass saw of the
345
+ # card's pull requests. They are never rows in the store, and nothing in the
346
+ # log reads them but the agent.
347
+
348
+ # SQL for a number of seconds as text: 50s, 1m 44s, 1h 02m, 3d 04h. @ stands
349
+ # for the column that holds the seconds.
350
+ sql_took() {
351
+ local t="case when @ < 60 then @ || 's'
352
+ when @ < 3600 then (@ / 60) || 'm ' || printf('%02d', @ % 60) || 's'
353
+ when @ < 86400 then (@ / 3600) || 'h ' || printf('%02d', @ % 3600 / 60) || 'm'
354
+ else (@ / 86400) || 'd ' || printf('%02d', @ % 86400 / 3600) || 'h' end"
355
+ printf '%s' "${t//@/$1}"
356
+ }
357
+
358
+ # SQL that starts a query on one card's view, or on every card's in the
359
+ # space, as the table `shown`. Each card's rows are worked out on their own.
360
+ # A store made before the approved line has no column for it until a
361
+ # hand-off is recorded in it, and the log never adds one, since it reads a
362
+ # store it cannot write too: its rows are read with approved null.
363
+ # log_view <space> <card, or empty for all>
364
+ log_view() {
365
+ local cards='' approved='null as approved'
366
+ [ -z "$2" ] || cards="and card = $(sql_str "$2")"
367
+ ! store_has_approved || approved=approved
368
+ printf "with base as (
369
+ select id, at, card, step, status, next, summary, flags, question, options,
370
+ answer, reason, %s,
371
+ lag(at) over w as prev_at, lag(status) over w as before, lead(id) over w as after,
372
+ sum(case when step = 'new' then 1 else 0 end) over w as run
373
+ from handoffs where space = %s %s
374
+ window w as (partition by card order by id)),
375
+ timed as (
376
+ select *,
377
+ case when step in ('new', 'start') or prev_at is null then null
378
+ else max(cast(strftime('%%s', at) - strftime('%%s', prev_at) as integer), 0) end as seconds,
379
+ case when after is null and status = 'needs-human'
380
+ then max(cast(strftime('%%s', 'now') - strftime('%%s', at) as integer), 0) end as open_seconds
381
+ from base),
382
+ labeled as (
383
+ select *,
384
+ case when step in ('new', 'start') then 'start'
385
+ when status = 'answered' and before = 'needs-human' then 'wait'
386
+ else 'step' end as kind,
387
+ status || case when next is null then '' else ', then ' || next end as what,
388
+ case when step in ('new', 'start') then 'started'
389
+ when status = 'done' then step || ' done'
390
+ when status = 'needs-human' then step || ' asked a question'
391
+ when status = 'answered' then 'answered'
392
+ when status = 'failed' then step || ' failed'
393
+ else step || ' ' || status end as label,
394
+ case when seconds is null then '' else (%s) end as short,
395
+ case when seconds is null then ''
396
+ when status = 'answered' and before = 'needs-human' then 'waited ' || (%s)
397
+ when before = 'needs-human' then (%s) || ', includes waiting on the human'
398
+ else (%s) end as took,
399
+ case when open_seconds is null then ''
400
+ else 'waiting on the human for ' || (%s) end as waiting
401
+ from timed),
402
+ shown as (
403
+ select *,
404
+ case when status = 'needs-human' then 'waiting on the human'
405
+ when step in ('new', 'start') then 'started'
406
+ when status = 'done' and next = 'merged' then 'merged'
407
+ else label || substr(what, length(status) + 1) end as state
408
+ from labeled)" \
409
+ "$approved" "$(sql_str "$1")" "$cards" \
410
+ "$(sql_took seconds)" "$(sql_took seconds)" "$(sql_took seconds)" "$(sql_took seconds)" \
411
+ "$(sql_took open_seconds)"
412
+ }
413
+
414
+ LOG_USAGE='usage: ocnus task log <space> [<card>] [--json]'
415
+
416
+ # The space's cards that have hand-offs, newest first by their latest row:
417
+ # where each stands and how long it has stood there, or what its agent is
418
+ # doing when Herdr says it is stuck or has stopped. With --json, the same as
419
+ # one line of JSON, with the agent beside where the card stands.
420
+ # log_cards <space> <json: 1 or 0> <view>
421
+ log_cards() {
422
+ local name=$1 json=$2 list
423
+ # Each card's latest row is the one with nothing after it.
424
+ list="$3, latest as (
425
+ select *, max(cast(strftime('%s', 'now') - strftime('%s', at) as integer), 0) as since
426
+ from shown where after is null), $AGENTS_SQL"
427
+ if [ "$json" = 1 ]; then
428
+ if [ ! -e "$STORE" ]; then
429
+ printf '{"space":"%s","cards":[]}\n' "$(json_str "$name")"
430
+ return
431
+ fi
432
+ store "$STORE" "$list
433
+ select json_object('space', $(sql_str "$name"),
434
+ 'cards', (select json_group_array(json(c)) from (
435
+ select json_object('card', card, 'at', at, 'state', state,
436
+ 'waiting', nullif(waiting, ''), 'for', $(sql_took since), 'seconds', since,
437
+ 'run', run, 'step', step, 'status', status, 'next', next,
438
+ 'agent', json(agent)) as c
439
+ from latest left join agents using (card) order by at desc, id desc)));" ||
440
+ die "could not read the hand-offs in $STORE. It may not be ocnus's store."
441
+ return
442
+ fi
443
+
444
+ local sep=$'\x1f' found='' count=0 card state for
445
+ if [ -e "$STORE" ]; then
446
+ found=$(store -separator "$sep" "$STORE" "$list
447
+ select card, coalesce(json_extract(agent, '\$.label'), state),
448
+ coalesce(json_extract(agent, '\$.for'), $(sql_took since))
449
+ from latest left join agents using (card) order by at desc, id desc;") ||
450
+ die "could not read the hand-offs in $STORE. It may not be ocnus's store."
451
+ fi
452
+ while IFS=$sep read -r card state for; do
453
+ [ -n "$card" ] || continue
454
+ [ "$count" -gt 0 ] || say "Cards in '$name' with hand-offs, newest first"
455
+ count=$((count + 1))
456
+ printf '%-10s %-28s for %s\n' "$card" "$state" "$for"
457
+ done <<< "$found"
458
+ [ "$count" -gt 0 ] || say "No hand-offs recorded in '$name'."
459
+ }
460
+
461
+ # List a card's hand-offs, oldest first, with how long each step took. With
462
+ # no card, list the space's cards instead. With --json, the same view as
463
+ # one line of JSON.
464
+ task_log() {
465
+ local json=0 a
466
+ local args=()
467
+ for a in "$@"; do
468
+ case $a in
469
+ --json) json=1 ;;
470
+ -*) die "$LOG_USAGE" ;;
471
+ *) args+=("$a") ;;
472
+ esac
473
+ done
474
+ set -- ${args[@]+"${args[@]}"}
475
+ local name=${1:-} card=${2:-}
476
+ [ -n "$name" ] || die "$LOG_USAGE"
477
+ space_named "$name" || die "there is no ocnus space called '$name'. See: ocnus space ls"
478
+ [ "$#" -le 2 ] || die "$LOG_USAGE"
479
+ [ -z "$card" ] || card=$(handoff_card "$name" "$card" log) || exit 1
480
+ have_sqlite || die "$SQLITE is not installed, so no hand-offs have been recorded on this machine."
481
+
482
+ local view='' found=''
483
+ [ ! -e "$STORE" ] || view=$(log_view "$name" "$card")
484
+ # What each card's agent is doing, from Herdr, as the table agents.
485
+ agents_table "$view" "$name" "$(cd -P "$SPACES/$name" && pwd)"
486
+ if [ -z "$card" ]; then
487
+ log_cards "$name" "$json" "$view"
488
+ return
489
+ fi
490
+
491
+ if [ "$json" = 1 ]; then
492
+ if [ ! -e "$STORE" ]; then
493
+ printf '{"space":"%s","card":"%s","state":null,"waiting":null,"run":null,"agent":null,"steps":[]}\n' \
494
+ "$(json_str "$name")" "$(json_str "$card")"
495
+ return
496
+ fi
497
+ # SQLite builds the JSON, so no value is escaped by hand. A store that
498
+ # cannot be read is an error, not an empty history.
499
+ store "$STORE" "$view, $AGENTS_SQL
500
+ select json_object('space', $(sql_str "$name"), 'card', $(sql_str "$card"),
501
+ 'state', (select state from shown order by id desc limit 1),
502
+ 'waiting', (select nullif(waiting, '') from shown order by id desc limit 1),
503
+ 'run', (select max(run) from shown),
504
+ 'agent', json((select agent from agents limit 1)),
505
+ 'steps', (select json_group_array(json(entry)) from (
506
+ select json_object('run', run, 'at', at, 'clock', strftime('%H:%M', at, 'localtime'),
507
+ 'step', step, 'status', status, 'next', next, 'kind', kind, 'label', label,
508
+ 'took', took, 'short', short, 'seconds', seconds, 'waiting', nullif(waiting, ''),
509
+ 'summary', summary, 'flags', flags, 'approved', approved,
510
+ 'question', question, 'options', options,
511
+ 'answer', answer, 'reason', reason) as entry
512
+ from shown order by id)));" ||
513
+ die "could not read the hand-offs in $STORE. It may not be ocnus's store."
514
+ return
515
+ fi
516
+
517
+ # The unit separator: no value can hold it, and unlike a tab it does not let
518
+ # `read` run two empty fields together.
519
+ local sep=$'\x1f' count=0 key
520
+ local at step what took summary flags approved question options answer reason waiting
521
+ if [ -e "$STORE" ]; then
522
+ found=$(store -separator "$sep" "$STORE" "$view
523
+ select at, step, what, took, coalesce(summary, ''), coalesce(flags, ''),
524
+ coalesce(approved, ''), coalesce(question, ''), coalesce(options, ''),
525
+ coalesce(answer, ''), coalesce(reason, ''), waiting
526
+ from shown order by id;") ||
527
+ die "could not read the hand-offs in $STORE. It may not be ocnus's store."
528
+ fi
529
+ while IFS=$sep read -r at step what took summary flags approved question options answer reason waiting; do
530
+ [ -n "$at" ] || continue
531
+ [ "$count" -gt 0 ] || say "$card in '$name'"
532
+ count=$((count + 1))
533
+ if [ -n "$took" ]; then printf '%s %-10s %-22s %s\n' "$at" "$step" "$what" "$took"
534
+ else printf '%s %-10s %s\n' "$at" "$step" "$what"
535
+ fi
536
+ # The setting's approval, so that it is not taken for the human's.
537
+ [ "$approved" != setting ] ||
538
+ approved='by the setting (ocnus approval --breakdown off), not the human'
539
+ for key in summary flags approved question options answer reason; do
540
+ [ -z "${!key}" ] || printf ' %s: %s\n' "$key" "${!key}"
541
+ done
542
+ [ -z "$waiting" ] || printf ' %s\n' "$waiting"
543
+ done <<< "$found"
544
+ [ "$count" -gt 0 ] || say "No hand-offs recorded for $card in '$name'."
545
+ # Last, what the card's agent is doing, when Herdr says it is stuck or has
546
+ # stopped, or what can see its pull requests, as the list says it
547
+ # (agents_table). An agent is only ever worked out with jq to hand.
548
+ if [ "$count" -gt 0 ] && [ -n "$AGENT_NOW" ]; then
549
+ printf ' now: %s\n' "$AGENT_NOW"
550
+ fi
551
+ }