@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 @@
1
+ {"mcpServers":{"linear-server":{"type":"http","url":"https://mcp.linear.app/mcp"}}}
@@ -0,0 +1,23 @@
1
+ ---
2
+ name: ocnus-build
3
+ description: Runs the build phase on an approved Linear card in its task: builds it, reviews it and puts it up as pull requests, with the skills its first message names. `ocnus task build` starts its session with this phase profile.
4
+ tools: Bash, Read, Edit, Write, Skill, mcp__linear-server__get_issue, mcp__linear-server__list_comments, mcp__linear-server__save_issue
5
+ model: opus
6
+ ---
7
+
8
+ ocnus started you in a task's tab in Herdr to build a Linear card that the
9
+ human has approved. The folder you start in is the card's task: a git
10
+ worktree of each repo the work needs, all on the task's branch. The human
11
+ reads what you record and answers you in this tab.
12
+
13
+ Your first message names the card, the ocnus space and the skills to
14
+ use, in the order to use them. Load each one with the Skill tool and follow
15
+ it: it says what to read, what to change, and how its step ends. Record each
16
+ hand-off with `ocnus task handoff`, as the skill shows, before you start
17
+ the next skill.
18
+
19
+ Read the card with `ocnus linear read <card>`. Use the Linear tools when
20
+ ocnus cannot read it, as the skills say, and for other reads, such as a
21
+ related card. Read each repo's own docs before you change it, and follow its
22
+ rules for tests and commits. Never merge a pull request, and never turn on
23
+ auto-merge: the human merges. Do every step yourself, in this session.
@@ -0,0 +1,20 @@
1
+ ---
2
+ name: ocnus-write-up
3
+ description: Runs the write-up phase on a Linear card: writes it up so that it is ready to build, with the skills its first message names. `ocnus task new` starts its session with this phase profile.
4
+ tools: Bash, Read, Skill, mcp__linear-server__get_issue, mcp__linear-server__list_comments, mcp__linear-server__list_issues, mcp__linear-server__save_issue
5
+ model: opus
6
+ ---
7
+
8
+ ocnus started you in a tab of an ocnus space, in Herdr, to write a
9
+ Linear card up. The human reads what you record and answers you in this tab.
10
+
11
+ Your first message names the card, the ocnus space and the skills to
12
+ use, in the order to use them. Load each one with the Skill tool and follow
13
+ it: it says what to read, what to write on the card, and how its step ends.
14
+ Record each hand-off with `ocnus task handoff`, as the skill shows, before
15
+ you start the next skill.
16
+
17
+ Read the card with `ocnus linear read <card>`, and the space's repos
18
+ with Read and Bash. Use the Linear tools when ocnus cannot read the card,
19
+ as the skills say, and for other reads, such as a related card. The skills
20
+ say how to write to the card. Do every step yourself, in this session.
package/cli/host-mark ADDED
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env perl
2
+ # One mark of an ocnus agent's time, for recording the host
3
+ # (cli/lib/host.sh). Claude Code runs it from the hooks that `task new` and
4
+ # `task build` give claude while recording is on (host_hooks):
5
+ # host-mark <mark> <file>
6
+ # It appends one line to the file, "<mark> <time>", with the time in seconds
7
+ # since 1970, to the millisecond. The marks are start, prompt, pre, post, ask
8
+ # and stop; any other word is not written. It writes only while the file's
9
+ # folder is there, so once `host off` has deleted it nothing is written, and
10
+ # nothing makes it again.
11
+ #
12
+ # It never reads its stdin, where Claude Code gives a hook the prompt, the
13
+ # reply, or what a tool was called with and gave back, so none of it can
14
+ # reach the file. It prints nothing, since what a SessionStart or
15
+ # UserPromptSubmit hook prints goes into the agent's context, and it always
16
+ # exits 0, since a PreToolUse hook that exits 2 stops the tool: Time::HiRes
17
+ # is loaded only in the eval, which lets go of whatever fails.
18
+ use strict;
19
+ use warnings;
20
+
21
+ open STDERR, '>', '/dev/null';
22
+ open STDOUT, '>', '/dev/null';
23
+ open STDIN, '<', '/dev/null';
24
+ eval {
25
+ my ($mark, $file) = @ARGV;
26
+ if (defined $mark && defined $file && $mark =~ /\A(?:start|prompt|pre|post|ask|stop)\z/ &&
27
+ $file =~ m{\A(.*)/[^/]+\z} && -d $1) {
28
+ require Time::HiRes;
29
+ if (open(my $out, '>>', $file)) {
30
+ printf $out "%s %.3f\n", $mark, Time::HiRes::time();
31
+ close $out;
32
+ }
33
+ }
34
+ 1;
35
+ };
36
+ exit 0;
@@ -0,0 +1,350 @@
1
+ # The agent on each card's current step, as Herdr sees it: waiting on a
2
+ # person in its tab, hit an error, stopped without handing off, or not
3
+ # running. `task log` reads it each time it runs and stores none of it, with
4
+ # the Herdr app open or closed. Sourced by cli/ocnus.
5
+ #
6
+ # A card's agent is the one running its current phase. That is the write-up
7
+ # phase, in the tab `task new` names after the card, until the work starts,
8
+ # then the build phase, in the tab `task start` names after the task's
9
+ # folder, tasks/<card>-... The card's latest hand-off says which. A tab is
10
+ # found by its name among the space's tabs, which come from two places:
11
+ # - herdr-core, which runs every terminal whether or not the app is open.
12
+ # Each live terminal labelled with the space is in the tab its
13
+ # ocnus.tab label names, as `task new` and `task start` label it.
14
+ # - The app, while it answers: its tabs in the Herdr space whose folder is
15
+ # the space's, as `task finish` finds the task's tab. This is how a tab
16
+ # opened before ocnus labelled its terminals is still found.
17
+ # A tab of the same name in both is one tab, and a terminal listed both ways
18
+ # counts once in it.
19
+ #
20
+ # For each terminal in that tab, hc says what its program last reported: its
21
+ # status (idle, working, done, blocked or error), the kind of a blocked one
22
+ # (permission, question or auth) and when the status last changed. A program
23
+ # that reports nothing has no status, and hc clears it when the terminal's own
24
+ # shell comes back to the foreground, as when the agent exits. When a tab has
25
+ # several terminals with a status, the most urgent counts: blocked, then
26
+ # error, working, done, idle.
27
+ #
28
+ # The rules:
29
+ # - Blocked or error always shows.
30
+ # - While a step is under way, an agent whose turn has ended (done or idle)
31
+ # has stopped without handing off. A step is under way when the latest
32
+ # hand-off is an answer, or done with a step an agent runs next: not
33
+ # start (or setup, its old name), which the human runs, nor merged.
34
+ # - While a step is under way, a tab that is closed, or whose terminals all
35
+ # have their own shell in the foreground and no status, has no agent
36
+ # running: it has exited, or never started.
37
+ # - A program other than the shell that reports no status, or a terminal hc
38
+ # cannot answer for, says nothing: what it is doing is not known.
39
+ # - For the first 15 seconds after `task new` or `task start` records its
40
+ # start, the agent is still starting, so it is neither stopped nor not
41
+ # running yet.
42
+ # - While a card's pull requests wait on the human (its latest hand-off is
43
+ # pr needs-human), what can see them shows (merges_seen in merges.sh),
44
+ # unless its agent is blocked or hit an error:
45
+ # - closed or conflict: ocnus's watch is running and saw one closed
46
+ # without merging or in conflict, and no agent runs to be woken for
47
+ # it: the tab is closed, or its own shell is in the foreground.
48
+ # - unwatched: nothing can watch them: hc is not installed, herdr-core
49
+ # is not running, ocnus's watch is not running, or gh could not
50
+ # ask GitHub. That holds whatever the agent in the tab is doing.
51
+ # Otherwise, with ocnus's watch watching, the card shows as it does
52
+ # with no agent state.
53
+ # - No card gets an agent state when jq is missing. None but those whose
54
+ # pull requests wait on the human get one when hc is missing, or when
55
+ # neither place can list the space's tabs: herdr-core cannot list its
56
+ # terminals, and the app is not running or `herdr-app ls` fails. With no
57
+ # tab to look in, no agent is running then.
58
+
59
+ # The seconds after a start row in which the agent is still starting.
60
+ AGENT_START_GRACE=15
61
+
62
+ # The space's tabs, read once per run by agents_read from herdr-core and from
63
+ # the Herdr app: one line per tab, its name and its terminals, separated by
64
+ # the unit separator. AGENTS_ON is 1 once either has been read.
65
+ AGENTS_ON=0
66
+ AGENT_TABS=''
67
+
68
+ # Read the space's tabs: herdr-core's live terminals labelled with the space,
69
+ # each in the tab its ocnus.tab label names, and the app's tabs in the
70
+ # space's folder while it answers. Each tab is one line, and each terminal
71
+ # counts once in it.
72
+ # agents_read <space> <the space's folder>
73
+ agents_read() {
74
+ local name=$1 dir=$2 out t terms gets='' core='' app='' got=0
75
+ AGENTS_ON=0 AGENT_TABS=''
76
+ command -v jq >/dev/null 2>&1 || return 0
77
+ have_hc || return 0
78
+
79
+ # hc ls shows no labels, so each terminal's tab is read with hc get. A
80
+ # terminal hc cannot answer for has ended since it was listed. Each answer
81
+ # goes on one line, as a string in JSON holds no newline, so that one jq
82
+ # reads them all and passes over one it cannot read.
83
+ if out=$(hc_cmd ls --json --label "ocnus.space=$name" 2>/dev/null) &&
84
+ terms=$(printf '%s' "$out" |
85
+ jq -r '.terminals[]? | select((.state // "live") == "live") | .address | strings' 2>/dev/null); then
86
+ got=1
87
+ for t in $terms; do
88
+ out=$(hc_cmd get "$t" --json 2>/dev/null) && [ -n "$out" ] || continue
89
+ gets="$gets{\"t\":\"$(json_str "$t")\",\"hc\":${out//$'\n'/ }}"$'\n'
90
+ done
91
+ [ -z "$gets" ] || core=$(printf '%s' "$gets" | jq -nrR '
92
+ inputs | fromjson? | [(.hc.labels? | objects | .["ocnus.tab"] | strings), .t]
93
+ | select(length == 2) | join("\u001f")' 2>/dev/null) || core=''
94
+ fi
95
+
96
+ # The app's tabs, for a tab opened before ocnus labelled its terminals.
97
+ if herdr_running && out=$(herdr_app ls 2>/dev/null) && [ -n "$out" ] &&
98
+ app=$(printf '%s' "$out" | jq -r --arg dir "$dir" '
99
+ .spaces[]? | select(.folder == $dir) | .tabs[]? | select(.name | type == "string")
100
+ | [.name, ([.terminals[]?.terminal | strings] | join(" "))] | join("\u001f")' 2>/dev/null); then
101
+ got=1
102
+ else
103
+ app=''
104
+ fi
105
+ [ "$got" = 1 ] || return 0
106
+
107
+ # herdr-core's tabs first, then the app's, each name once.
108
+ local sep=$'\x1f' tab i n=0 names=() lists=()
109
+ while IFS=$sep read -r tab terms; do
110
+ [ -n "$tab" ] || continue
111
+ i=0
112
+ while [ "$i" -lt "$n" ] && [ "${names[$i]}" != "$tab" ]; do i=$((i + 1)); done
113
+ [ "$i" -lt "$n" ] || { names[$n]=$tab; lists[$n]=''; n=$((n + 1)); }
114
+ for t in $terms; do
115
+ case " ${lists[$i]} " in *" $t "*) ;; *) lists[$i]=${lists[$i]:+${lists[$i]} }$t ;; esac
116
+ done
117
+ done <<< "$core"$'\n'"$app"
118
+ i=0
119
+ while [ "$i" -lt "$n" ]; do
120
+ AGENT_TABS=$AGENT_TABS${AGENT_TABS:+$'\n'}${names[$i]}$sep${lists[$i]}
121
+ i=$((i + 1))
122
+ done
123
+ AGENTS_ON=1
124
+ }
125
+
126
+ # Set TOOK to a number of seconds as text, as sql_took words it: 50s,
127
+ # 1m 44s, 1h 02m, 3d 04h.
128
+ took_text() {
129
+ local s=$1
130
+ if [ "$s" -lt 60 ]; then printf -v TOOK '%ss' "$s"
131
+ elif [ "$s" -lt 3600 ]; then printf -v TOOK '%dm %02ds' $((s / 60)) $((s % 60))
132
+ elif [ "$s" -lt 86400 ]; then printf -v TOOK '%dh %02dm' $((s / 3600)) $((s % 3600 / 60))
133
+ else printf -v TOOK '%dd %02dh' $((s / 86400)) $((s % 86400 / 3600))
134
+ fi
135
+ }
136
+
137
+ # What the agent in a card's tab is doing, from the tabs agents_read found:
138
+ # AGENT_SEEN is one word, and AGENT_TAB the tab it was seen in. The tab is the
139
+ # write-up phase's, named after the card, until the work starts, then the
140
+ # task's, named after its folder, as the step says. AGENT_SEEN is:
141
+ # closed no such tab is open
142
+ # none a terminal with its own shell in the foreground and no status,
143
+ # or one that has finished: no agent runs there
144
+ # unknown a program that reports no status, or a terminal hc cannot
145
+ # answer for
146
+ # blocked, error, working, done or idle
147
+ # what the program last reported: of several terminals with a
148
+ # status, the most urgent, blocked first, then error, working,
149
+ # done and idle
150
+ # AGENT_KIND is the kind of a blocked one, and AGENT_MS when the status last
151
+ # changed, in milliseconds, or empty. It sets these rather than printing them,
152
+ # which would take a subshell, and starts no program when no tab is open.
153
+ # `task log` reads it for each card (card_agent), and ocnus's watch on
154
+ # the merges for each card whose pull requests have merged (merges.sh).
155
+ # card_seen <card> <the card in lower case> <step>
156
+ AGENT_SEEN=closed AGENT_KIND='' AGENT_MS='' AGENT_TAB=''
157
+ card_seen() {
158
+ local card=$1 low=$2 step=$3
159
+ AGENT_SEEN=closed AGENT_KIND='' AGENT_MS='' AGENT_TAB=''
160
+
161
+ # The write-up phase's tab until the work starts, then the task's, named
162
+ # after its folder: the card in lower case, alone or before a dash.
163
+ local writeup=0 sep=$'\x1f' name terms tabs=() termlists=()
164
+ case $step in new | define | plan | breakdown) writeup=1 ;; esac
165
+ while IFS=$sep read -r name terms; do
166
+ [ -n "$name" ] || continue
167
+ if [ "$writeup" = 1 ]; then
168
+ [ "$name" = "$card" ] || continue
169
+ else
170
+ case $name in "$low" | "$low"-*) ;; *) continue ;; esac
171
+ fi
172
+ tabs+=("$name")
173
+ termlists+=("$terms")
174
+ done <<< "$AGENT_TABS"
175
+ [ "${#tabs[@]}" -gt 0 ] || return 0
176
+
177
+ # The most urgent of the tabs' terminals: what it says, the kind of a
178
+ # blocked one, when its status changed, and the tab it is in.
179
+ local seen kind='' ms='' tab=${tabs[0]} i=0 t out wrapped='' failed=0
180
+ while [ "$i" -lt "${#tabs[@]}" ]; do
181
+ for t in ${termlists[$i]}; do
182
+ if out=$(hc_cmd get "$t" --json 2>/dev/null) && [ -n "$out" ]; then
183
+ wrapped="$wrapped{\"tab\":$i,\"hc\":$out}"$'\n'
184
+ else
185
+ failed=1
186
+ fi
187
+ done
188
+ i=$((i + 1))
189
+ done
190
+ seen=$(printf '%s' "$wrapped" | jq -rs '
191
+ {blocked: 0, error: 1, working: 2, done: 3, idle: 4} as $rank
192
+ | [.[] | .tab as $tab | .hc
193
+ | (.status | if type == "object" then .state else null end) as $s
194
+ | if .state == "finished" then {w: "none"}
195
+ elif .status == null then
196
+ (if (.foreground.pid? // null) != null and .foreground.pid != .pid
197
+ then {w: "unknown"} else {w: "none"} end)
198
+ elif ($s | type) == "string" and ($rank | has($s))
199
+ then {w: $s, r: $rank[$s], kind: .status.kind, ms: .status_ms, tab: $tab}
200
+ else {w: "unknown"} end] as $t
201
+ | ([$t[] | select(.r != null)] | sort_by(.r) | first)
202
+ // (if any($t[]; .w == "unknown") then {w: "unknown"} else {w: "none"} end)
203
+ | [.w, (.kind // "" | tostring), (.ms // "" | tostring), (.tab // "" | tostring)]
204
+ | join("\u001f")' 2>/dev/null) || seen=unknown
205
+ IFS=$sep read -r seen kind ms i <<< "$seen"
206
+ case $i in '' | *[!0-9]*) ;; *) [ "$i" -ge "${#tabs[@]}" ] || tab=${tabs[$i]} ;; esac
207
+ [ "$seen" != none ] || [ "$failed" = 0 ] || seen=unknown
208
+ AGENT_SEEN=${seen:-unknown} AGENT_KIND=$kind AGENT_MS=$ms AGENT_TAB=$tab
209
+ }
210
+
211
+ # Work out a card's agent from its latest hand-off, as AGENT_JSON: null when
212
+ # there is nothing to say, or one line of JSON, {state, kind, tab, label,
213
+ # for, seconds, pulls}. state is blocked, error, stopped, not running,
214
+ # closed, conflict or unwatched; kind is permission, question, auth or null;
215
+ # tab is the tab's name, or null when it is closed; pulls are the URLs of
216
+ # the pull requests the label names, a list that is empty for any state but
217
+ # closed and conflict. for and seconds count from when hc says the status
218
+ # last changed, or from the hand-off when it gives no time, and always from
219
+ # the hand-off for closed, conflict and unwatched, which are about the pull
220
+ # requests. AGENT_LABEL and AGENT_FOR are the label and for as they are, for
221
+ # the card's own log. It runs for every card in the log, so it starts no
222
+ # program for a card with no tab open whose pull requests do not wait on the
223
+ # human, and sets AGENT_JSON rather than printing it, which would take a
224
+ # subshell. With Herdr's tabs not read (AGENTS_ON 0), only a card whose pull
225
+ # requests wait on the human has anything to say: with no tab to look in, no
226
+ # agent is running.
227
+ # card_agent <card> <the card in lower case> <step> <status> <next>
228
+ # <seconds since the hand-off> <space>
229
+ AGENT_JSON=null AGENT_LABEL='' AGENT_FOR=''
230
+ card_agent() {
231
+ local card=$1 low=$2 step=$3 status=$4 next=$5 since=$6 space=$7
232
+ AGENT_JSON=null AGENT_LABEL='' AGENT_FOR=''
233
+ # Its pull requests wait on the human.
234
+ local waits=0
235
+ [ "$step $status" != 'pr needs-human' ] || waits=1
236
+ [ "$AGENTS_ON" = 1 ] || [ "$waits" = 1 ] || return 0
237
+
238
+ local under=0
239
+ case $status in
240
+ answered) under=1 ;;
241
+ done) case $next in '' | start | setup | merged) ;; *) under=1 ;; esac ;;
242
+ esac
243
+
244
+ card_seen "$card" "$low" "$step"
245
+ # With no tab open, only a step under way, or pull requests waiting on the
246
+ # human, have anything to say.
247
+ [ "$AGENT_SEEN" != closed ] || [ "$under" = 1 ] || [ "$waits" = 1 ] || return 0
248
+ local seen=$AGENT_SEEN kind=$AGENT_KIND ms=$AGENT_MS tab=$AGENT_TAB
249
+
250
+ local state=''
251
+ case $seen in
252
+ blocked | error) state=$seen ;;
253
+ done | idle) [ "$under" = 0 ] || state=stopped ;;
254
+ none | closed) [ "$under" = 0 ] || state='not running' ;;
255
+ esac
256
+ # An agent that `task new` or `task start` has only just started.
257
+ case $state in
258
+ stopped | 'not running')
259
+ case $step in new | start) [ "$since" -ge "$AGENT_START_GRACE" ] || state='' ;; esac
260
+ ;;
261
+ esac
262
+
263
+ # What can see the pull requests. One closed or in conflict counts only
264
+ # with no agent running to wake: the tab closed, or its own shell in the
265
+ # foreground. An agent idle there is ocnus's watch to wake.
266
+ local pulls=() u names=''
267
+ if [ -z "$state" ] && [ "$waits" = 1 ]; then
268
+ merges_seen "$space" "$card"
269
+ case $MERGES_SEEN in
270
+ unwatched) state=unwatched ms='' ;;
271
+ closed | conflict)
272
+ case $seen in none | closed) state=$MERGES_SEEN ms='' ;; esac
273
+ ;;
274
+ esac
275
+ case $state in
276
+ closed | conflict)
277
+ read -r -a pulls <<< "$MERGES_PULLS"
278
+ for u in ${pulls[@]+"${pulls[@]}"}; do
279
+ pull_name "$u"
280
+ names=${names:+$names, }$PULL_NAME
281
+ done
282
+ ;;
283
+ esac
284
+ fi
285
+
286
+ local label
287
+ case $state in
288
+ blocked)
289
+ case $kind in
290
+ permission) label='agent waiting on a permission prompt' ;;
291
+ question) label='agent asking a question in its tab' ;;
292
+ auth) label='agent waiting for a sign-in' ;;
293
+ *) kind='' label='agent waiting on you in its tab' ;;
294
+ esac
295
+ ;;
296
+ error) kind='' label='agent hit an error' ;;
297
+ stopped) kind='' label='agent stopped without handing off' ;;
298
+ 'not running') kind='' label='agent not running' ;;
299
+ closed) kind='' label="$names closed without merging, no agent running" ;;
300
+ conflict) kind='' label="$names in conflict, no agent running" ;;
301
+ unwatched) kind='' label="nothing watching its pull requests: $MERGES_WHY" ;;
302
+ *) return 0 ;;
303
+ esac
304
+
305
+ local secs=$since now
306
+ case $ms in
307
+ '' | *[!0-9]*) ;;
308
+ *) now=$(date +%s); secs=$((now - ms / 1000)); [ "$secs" -ge 0 ] || secs=0 ;;
309
+ esac
310
+ local k=null tb=null list=''
311
+ [ -z "$kind" ] || k="\"$kind\""
312
+ [ "$seen" = closed ] || tb="\"$(json_str "$tab")\""
313
+ for u in ${pulls[@]+"${pulls[@]}"}; do list="$list${list:+,}\"$(json_str "$u")\""; done
314
+ took_text "$secs"
315
+ printf -v AGENT_JSON '{"state":"%s","kind":%s,"tab":%s,"label":"%s","for":"%s","seconds":%s,"pulls":[%s]}' \
316
+ "$state" "$k" "$tb" "$(json_str "$label")" "$TOOK" "$secs" "$list"
317
+ AGENT_LABEL=$label AGENT_FOR=$TOOK
318
+ }
319
+
320
+ # Work out the agent of every card in a view of the log, as AGENTS_SQL: the
321
+ # SQL for the table agents (card, agent) that the log's query joins, with
322
+ # each card whose agent has something to say and its JSON. The space's tabs
323
+ # are read here, once. AGENT_NOW is the last such agent's label and how long
324
+ # it has been so, as the list shows them, for the `now:` line of the log of
325
+ # one card. Nothing is worked out without jq, which reads hc's answers, as
326
+ # ocnus's watch on the merges needs it to run.
327
+ # agents_table <view> <space> <the space's folder>
328
+ AGENTS_SQL='agents (card, agent) as (select null, null where 0)'
329
+ AGENT_NOW=''
330
+ agents_table() {
331
+ local view=$1 name=$2 dir=$3 rows sep=$'\x1f' card low step status next since values='' only=''
332
+ AGENTS_SQL='agents (card, agent) as (select null, null where 0)' AGENT_NOW=''
333
+ [ -n "$view" ] || return 0
334
+ command -v jq >/dev/null 2>&1 || return 0
335
+ agents_read "$name" "$dir"
336
+ # With no tabs read, only the cards whose pull requests wait on the human.
337
+ [ "$AGENTS_ON" = 1 ] || only="and step = 'pr' and status = 'needs-human'"
338
+ rows=$(store -separator "$sep" "$STORE" "$view
339
+ select card, lower(card), step, status, coalesce(next, ''),
340
+ max(cast(strftime('%s', 'now') - strftime('%s', at) as integer), 0)
341
+ from shown where after is null $only order by id;") || return 0
342
+ while IFS=$sep read -r card low step status next since; do
343
+ [ -n "$card" ] || continue
344
+ card_agent "$card" "$low" "$step" "$status" "$next" "$since" "$name" || AGENT_JSON=null
345
+ [ "$AGENT_JSON" != null ] || continue
346
+ values="$values${values:+, }($(sql_str "$card"), $(sql_str "$AGENT_JSON"))"
347
+ AGENT_NOW="$AGENT_LABEL, for $AGENT_FOR"
348
+ done <<< "$rows"
349
+ [ -z "$values" ] || AGENTS_SQL="agents (card, agent) as (values $values)"
350
+ }
@@ -0,0 +1,94 @@
1
+ # Whether breakdown asks the human to approve a card, or approves it by
2
+ # itself. Sourced by cli/ocnus.
3
+
4
+ # Breakdown, the last step of the write-up phase, ends with the card
5
+ # approved (skills/sdlc-breakdown). With the breakdown approval on, it asks
6
+ # the human and waits for the answer. That is the default. With it off, it
7
+ # approves a card it has no doubt about by itself and runs `task start`, as
8
+ # for an approval to start now, without asking; a card it has a doubt about,
9
+ # such as one it thinks too big, still waits for the human. The choice is
10
+ # "approval": {"breakdown": true|false} in the settings file (settings.sh),
11
+ # an object so that a later approval gets a key beside it, and on when the
12
+ # file leaves it out. sdlc-breakdown reads it with `ocnus approval
13
+ # --json` when it gets to the approval, so a change reaches every card whose
14
+ # breakdown has not got there yet.
15
+
16
+ # What `ocnus approval` takes after it.
17
+ TAKES_APPROVAL='--breakdown on or --breakdown off, or nothing to say whether the breakdown approval is on (--json for programs)'
18
+
19
+ # ocnus approval [--json | --breakdown on|off]: say whether the
20
+ # breakdown approval is on, or turn it on or off. --json prints it as one
21
+ # line for programs, with the file it is kept in. --breakdown on and off
22
+ # write "approval" into the settings file as archive_set writes "archive":
23
+ # everything is checked before the file is touched, and the new file is
24
+ # checked before it takes the old one's place, so a refusal or a failure
25
+ # leaves the file as it was.
26
+ cmd_approval() {
27
+ local usage="usage: ocnus approval [--json | --breakdown on|off]: it takes $TAKES_APPROVAL"
28
+ case $# in
29
+ 0) approval_show plain ;;
30
+ 1)
31
+ case $1 in
32
+ --json) approval_show json ;;
33
+ --breakdown) die "$usage" ;;
34
+ *) die "'$1' is not something ocnus approval takes. It takes $TAKES_APPROVAL." ;;
35
+ esac
36
+ ;;
37
+ 2)
38
+ case $1 in
39
+ --breakdown)
40
+ case $2 in
41
+ on) approval_set true ;;
42
+ off) approval_set false ;;
43
+ *) die "'$2' is not on or off. ocnus approval takes $TAKES_APPROVAL." ;;
44
+ esac
45
+ ;;
46
+ --json) die "$usage" ;;
47
+ --*) die "'$1' is not an approval: breakdown is the only one. ocnus approval takes $TAKES_APPROVAL." ;;
48
+ *) die "$usage" ;;
49
+ esac
50
+ ;;
51
+ *) die "$usage" ;;
52
+ esac
53
+ }
54
+
55
+ # Say whether the breakdown approval is on: plain, for a person, or json,
56
+ # for programs, as {"file":…,"approval":{"breakdown":true}}.
57
+ approval_show() {
58
+ need_jq
59
+ settings_check
60
+ if [ "$1" = json ]; then
61
+ printf '{"file":"%s","approval":{"breakdown":%s}}\n' "$(json_str "$SETTINGS")" "$SET_APPROVAL_BREAKDOWN"
62
+ elif [ "$SET_APPROVAL_BREAKDOWN" = true ]; then
63
+ say "The breakdown approval is on: breakdown asks you to approve each card, and waits for your answer."
64
+ say "Turn it off with: ocnus approval --breakdown off"
65
+ else
66
+ say "The breakdown approval is off: breakdown approves a card it has no doubt about by itself, and starts its build phase, without asking. A card it has a doubt about, such as one it thinks too big, still waits for you."
67
+ say "Turn it on with: ocnus approval --breakdown on"
68
+ fi
69
+ }
70
+
71
+ # Write "approval": {"breakdown": true or false} into the settings file,
72
+ # leaving the rest of it as it is. With no file yet, the new one starts from
73
+ # the defaults, so that it names every profile.
74
+ approval_set() {
75
+ local want=$1 tmp ok=0
76
+ need_jq
77
+ settings_check
78
+ mkdir -p "${SETTINGS%/*}"
79
+ tmp=$SETTINGS.ocnus.$$
80
+ if { if [ -e "$SETTINGS" ]; then cat "$SETTINGS"; else settings_default; fi; } |
81
+ jq --argjson on "$want" '.approval.breakdown = $on' > "$tmp" 2>/dev/null &&
82
+ [ "$(SETTINGS=$tmp; settings_load && printf '%s' "$SET_APPROVAL_BREAKDOWN")" = "$want" ] &&
83
+ mv -f "$tmp" "$SETTINGS"; then
84
+ ok=1
85
+ fi
86
+ rm -f "$tmp"
87
+ [ "$ok" = 1 ] || die "could not change $SETTINGS, so it is as it was."
88
+ settings_check
89
+ if [ "$want" = true ]; then
90
+ say "The breakdown approval is on now. The next breakdown to reach its approval asks you, and waits for your answer."
91
+ else
92
+ say "The breakdown approval is off now. The next breakdown to reach its approval approves a card it has no doubt about by itself, and starts it."
93
+ fi
94
+ }