@johngaynor/ocnus 2.0.1-next.7 → 2.0.1-next.71

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 (43) hide show
  1. package/README.md +506 -93
  2. package/cli/lib/agents.sh +32 -16
  3. package/cli/lib/asks.sh +762 -0
  4. package/cli/lib/banner.sh +181 -0
  5. package/cli/lib/brief.sh +375 -0
  6. package/cli/lib/chain.sh +363 -0
  7. package/cli/lib/close.sh +5 -3
  8. package/cli/lib/cost.sh +500 -0
  9. package/cli/lib/finish.sh +30 -4
  10. package/cli/lib/github.sh +106 -26
  11. package/cli/lib/guard.sh +39 -10
  12. package/cli/lib/handoff.sh +196 -31
  13. package/cli/lib/host.sh +186 -345
  14. package/cli/lib/linear.sh +552 -56
  15. package/cli/lib/links.sh +6 -3
  16. package/cli/lib/merges.sh +126 -27
  17. package/cli/lib/sessions.sh +132 -0
  18. package/cli/lib/settings.sh +31 -15
  19. package/cli/lib/task.sh +169 -54
  20. package/cli/lib/tests.sh +302 -7
  21. package/cli/lib/usage.sh +204 -0
  22. package/cli/ocnus +264 -72
  23. package/herdr/README.md +867 -223
  24. package/herdr/layouts/default.json +30 -11
  25. package/herdr/layouts/showcase.json +291 -0
  26. package/herdr/layouts/vibe.json +162 -0
  27. package/herdr/layouts/vibe.svg +41 -0
  28. package/herdr/mods/agent-settings/main.js +19 -7
  29. package/herdr/mods/memory/main.js +128 -0
  30. package/herdr/mods/memory/mod.json +1 -0
  31. package/herdr/mods/memory/reading.js +88 -0
  32. package/herdr/mods/names/main.js +22 -4
  33. package/herdr/mods/step-counts/main.js +281 -0
  34. package/herdr/mods/step-counts/mod.json +1 -0
  35. package/herdr/mods/usage/main.js +149 -0
  36. package/herdr/mods/usage/mod.json +1 -0
  37. package/package.json +1 -1
  38. package/prompts/task-new.md +1 -1
  39. package/skills/brief/SKILL.md +8 -0
  40. package/skills/sdlc-breakdown/SKILL.md +66 -18
  41. package/skills/sdlc-build/SKILL.md +6 -2
  42. package/skills/sdlc-pr/SKILL.md +2 -2
  43. package/skills/sdlc-review/SKILL.md +5 -5
@@ -0,0 +1,363 @@
1
+ # The chain: starting each card once the cards that block it are done.
2
+ # Sourced by cli/ocnus.
3
+
4
+ # sdlc-breakdown records in Linear the cards a card needs merged first, as its
5
+ # blockers, and `task start` holds a card while one of them is not done
6
+ # (task.sh). The chain is what starts a held card once they all are, so that
7
+ # the human does not have to come back and run `task start` again. It runs at
8
+ # two points:
9
+ #
10
+ # - At a merge. Once ocnus's watch on the merges has recorded a card's
11
+ # merge and finished its task (merge_record in merges.sh), merge_chain
12
+ # asks Linear once for the cards that card was blocking, and starts each
13
+ # one that is ready.
14
+ # - At the catch-up. A start can be missed at the merge: `task start`
15
+ # stopped, Linear could not be asked, the chain was off, or the last
16
+ # blocker was closed some other way, such as by hand in Linear. So `ocnus
17
+ # task ready <space>` asks Linear for every card of the space's teams that
18
+ # waits, and starts each one that is ready (ready_cards). The human can
19
+ # run it, and the watch runs it for every space once the last catch-up is
20
+ # READY_EVERY seconds old (merges.sh).
21
+ #
22
+ # A card is ready when it is in a not-started status, such as Todo, which is
23
+ # where the approval puts it; its team is in a space's teams; its description
24
+ # has a Specification and a Plan; and every card that blocks it, or the card
25
+ # it was sliced from, is in a completed status. A blocker that was canceled
26
+ # is not completed, so it still holds the card. A ready card starts with
27
+ # `task start <space> <card>`, the command the human would run, so it gets
28
+ # what any card gets: a branch cut from each repo's default branch as it is on
29
+ # origin now, which is after the merge, a tab that runs its build phase, and a
30
+ # start row. A ready card that was sliced starts through its first sub-issue
31
+ # that is not started, done or canceled, as sdlc-breakdown picks one.
32
+ #
33
+ # Every other card that waits is named, with why it was not started. At a
34
+ # merge, each start and each card not started goes on the merge's flags, so
35
+ # that `task log` shows them (merge_record).
36
+ #
37
+ # The chain is on unless the settings file says "chain": false (settings.sh),
38
+ # which `ocnus chain off` writes. Off, the watch starts no card, at a merge
39
+ # or at a catch-up. `task ready` starts the ready cards whatever the setting
40
+ # says, since the human ran it.
41
+
42
+ # What `chain` takes after it.
43
+ TAKES_CHAIN='on or off, or nothing to say whether the chain is on (--json for programs)'
44
+
45
+ # --- the setting ------------------------------------------------------------
46
+
47
+ # ocnus chain [on|off|--json]: say whether the chain is on, or turn it on
48
+ # or off. --json prints it as one line for programs, with the file it is kept
49
+ # in, for a Herdr mod. on and off write "chain" into the settings file as
50
+ # archive_set writes "archive": everything is checked before the file is
51
+ # touched, and the new file is checked before it takes the old one's place,
52
+ # so a refusal or a failure leaves the file as it was.
53
+ cmd_chain() {
54
+ local usage="usage: ocnus chain [on|off|--json]: it takes $TAKES_CHAIN"
55
+ case $# in
56
+ 0) chain_show plain ;;
57
+ 1)
58
+ case $1 in
59
+ --json) chain_show json ;;
60
+ on) chain_set true ;;
61
+ off) chain_set false ;;
62
+ *) die "'$1' is not on or off. ocnus chain takes $TAKES_CHAIN." ;;
63
+ esac
64
+ ;;
65
+ *) die "$usage" ;;
66
+ esac
67
+ }
68
+
69
+ # Say whether the chain is on: plain, for a person, or json, for programs.
70
+ chain_show() {
71
+ need_jq
72
+ settings_check
73
+ if [ "$1" = json ]; then
74
+ printf '{"file":"%s","chain":%s}\n' "$(json_str "$SETTINGS")" "$SET_CHAIN"
75
+ elif [ "$SET_CHAIN" = true ]; then
76
+ say "The chain is on: once ocnus's watch has recorded a card's merge, it starts each card that card was blocking that is ready. A card is ready when it is in Todo, has a Specification and a Plan, and every card that blocks it is done."
77
+ say "Every half hour, the watch also starts the ready cards a merge did not start. To start them now: ocnus task ready <space>"
78
+ say "Turn it off with: ocnus chain off"
79
+ else
80
+ say "The chain is off: ocnus's watch starts no card. A card that task start held waits in Todo until you start it."
81
+ say "To start the cards that are ready: ocnus task ready <space>"
82
+ say "Turn it on with: ocnus chain on"
83
+ fi
84
+ }
85
+
86
+ # Write "chain": true or false into the settings file. With no file yet, the
87
+ # new one starts from the defaults, so that it names every profile.
88
+ chain_set() {
89
+ local want=$1 tmp ok=0
90
+ need_jq
91
+ settings_check
92
+ mkdir -p "${SETTINGS%/*}"
93
+ tmp=$SETTINGS.ocnus.$$
94
+ if { if [ -e "$SETTINGS" ]; then cat "$SETTINGS"; else settings_default; fi; } |
95
+ jq --argjson on "$want" '.chain = $on' > "$tmp" 2>/dev/null &&
96
+ [ "$(SETTINGS=$tmp; settings_load && printf '%s' "$SET_CHAIN")" = "$want" ] &&
97
+ mv -f "$tmp" "$SETTINGS"; then
98
+ ok=1
99
+ fi
100
+ rm -f "$tmp"
101
+ [ "$ok" = 1 ] || die "could not change $SETTINGS, so it is as it was."
102
+ settings_check
103
+ if [ "$want" = true ]; then
104
+ say "The chain is on now. Once ocnus's watch has recorded a card's merge, it starts each card that card was blocking that is ready, and every half hour it starts the ready cards a merge did not start."
105
+ else
106
+ say "The chain is off now. ocnus's watch starts no card: a card that task start held waits in Todo until you start it, or run: ocnus task ready <space>"
107
+ fi
108
+ }
109
+
110
+ # --- which cards are ready --------------------------------------------------
111
+
112
+ # Every space's Linear teams, as one line of JSON: each team's key, and the
113
+ # spaces that list it, in the order of their names.
114
+ # {"ABC":["demo"],"XYZ":["shop","web"]}
115
+ # A space whose teams file cannot be read lists none here, and space_teams
116
+ # has said why on stderr.
117
+ chain_teams() {
118
+ local d name keys key lines=''
119
+ for d in "$SPACES"/*/; do
120
+ [ -d "$d" ] || continue
121
+ d=${d%/}
122
+ name=${d##*/}
123
+ valid_name "$name" || continue
124
+ keys=$(space_teams "$d") || continue
125
+ for key in $keys; do lines+=$key$'\t'$name$'\n'; done
126
+ done
127
+ printf '%s' "$lines" | jq -Rcn '[inputs | split("\t")] | reduce .[] as $l ({}; .[$l[0]] += [$l[1]])'
128
+ }
129
+
130
+ # Judge each card that waits, from {cards}, as linear_blocks and
131
+ # linear_waiting print them (CHAIN_DEFS in linear.sh), on stdin. $teams is
132
+ # what chain_teams prints, and $home the space to start a card in when its
133
+ # teams hold the card's team, or '' when there is none. It prints a line for
134
+ # each card that is ready and each that is not, its fields separated by the
135
+ # unit separator:
136
+ # start <card> <the card to start> <its space>
137
+ # skip <card> <> <> <why it was not started>
138
+ # The card to start is the card itself, or its first open slice when it was
139
+ # sliced. A card that is started, done or canceled already does not wait, so
140
+ # it gets no line; neither does a sliced card with every slice started or
141
+ # closed.
142
+ #
143
+ # The space is $home when its teams hold the card's team, or else the one
144
+ # space that lists the team. A card's team is what its ID starts with. The
145
+ # checks come in the order the reasons are worth giving: a card that is not
146
+ # approved is not looked at any further.
147
+ #
148
+ # A slice's own blockers are not read with the card it was sliced from, since
149
+ # Linear caps what one question may cost: `task start` reads them, and holds
150
+ # the slice while one is not done. A slice that waits in its own right is a
151
+ # card here too, with its blockers and a line of its own, and chain_start
152
+ # goes by that line.
153
+ CHAIN_VERDICTS='
154
+ def lacks: if (.spec or .plan) | not then "no Specification and no Plan"
155
+ elif .spec | not then "no Specification"
156
+ elif .plan | not then "no Plan"
157
+ else null end;
158
+ def in_status: if .status == "" then "in a status ocnus could not read" else "in \(.status)" end;
159
+ def over: .type == "started" or .type == "completed" or .type == "canceled";
160
+ def blocks($card): "\(.id), \(in_status), still blocks "
161
+ + (if .of == $card then "it" else "\(.of), the card it was sliced from" end);
162
+ def sliced: ([.subs[] | select(over | not)] | .[0]) as $sub
163
+ | if $sub == null then empty
164
+ elif $sub.type != "unstarted" then
165
+ {kind: "skip", why: "its first open slice, \($sub.id), is \($sub | in_status), so it is not approved yet"}
166
+ elif ($sub | lacks) != null then {kind: "skip", why: "its first open slice, \($sub.id), has \($sub | lacks)"}
167
+ else {kind: "start", start: $sub.id}
168
+ end;
169
+ .cards[]
170
+ | . as $c
171
+ | (.id | sub("-[0-9]+$"; "") | ascii_upcase) as $team
172
+ | ($teams[$team] // []) as $spaces
173
+ | (if any($spaces[]; . == $home) then $home
174
+ elif ($spaces | length) == 1 then $spaces[0]
175
+ else null end) as $space
176
+ | [.blockers[] | select(.type != "completed")] as $open
177
+ | (if over then empty
178
+ elif .type != "unstarted" then {kind: "skip", why: "it is \(in_status), so it is not approved yet"}
179
+ elif lacks != null then {kind: "skip", why: "it has \(lacks)"}
180
+ elif ($spaces | length) == 0 then {kind: "skip", why: "its team, \($team), is in the teams of no space"}
181
+ elif $space == null then
182
+ {kind: "skip", why: "its team, \($team), is in the teams of more than one space (\($spaces | join(", "))), so ocnus cannot tell which one to start it in"}
183
+ elif ($open | length) > 0 then {kind: "skip", why: ($open | map(blocks($c.id)) | join(", and "))}
184
+ elif .cut then
185
+ {kind: "skip", why: "it, or the card it was sliced from, has more relations in Linear than ocnus reads, so a card that blocks it may be among the rest"}
186
+ elif .subsCut then
187
+ {kind: "skip", why: "it has more sub-issues in Linear than ocnus reads, so its first open slice may be among the rest"}
188
+ elif (.subs | length) > 0 then sliced
189
+ else {kind: "start", start: .id}
190
+ end) as $v
191
+ | [$v.kind, .id, ($v.start // ""), (if $v.kind == "start" then $space else "" end), ($v.why // "")]
192
+ | join("\u001f")'
193
+
194
+ # Whether a card has a task in a space already: a folder for it under the
195
+ # space's tasks, as task_folder looks for one.
196
+ # chain_has_task <the space's folder> <card ID, in upper case>
197
+ chain_has_task() {
198
+ local low f
199
+ low=$(printf '%s' "$2" | tr '[:upper:]' '[:lower:]')
200
+ for f in "$1/tasks/$low" "$1/tasks/$low-"*; do
201
+ [ ! -d "$f" ] || return 0
202
+ done
203
+ return 1
204
+ }
205
+
206
+ # Whether a card's merge is the last thing recorded of it in a space. Without
207
+ # the store, nothing is recorded of it.
208
+ # chain_merged <space> <card ID, in upper case>
209
+ chain_merged() {
210
+ have_sqlite && [ -e "$STORE" ] || return 1
211
+ [ "$(store "$STORE" "select coalesce((select status = 'done' and next = 'merged' from handoffs
212
+ where space = $(sql_str "$1") and card = $(sql_str "$2") order by id desc limit 1), 0);" 2>/dev/null)" = 1 ]
213
+ }
214
+
215
+ # Start each ready card among the cards given, and print a line, on stdout,
216
+ # for each card started and each card not started:
217
+ # ABC-123 was started
218
+ # ABC-131 was started, the first open slice of ABC-130
219
+ # ABC-124 was not started: ABC-122, in In Review, still blocks it
220
+ # A card is started with `task start <space> <card>`, run as its own command,
221
+ # as the human would run it, so that a start that stops does not stop this.
222
+ # All it prints goes to stderr, for whoever is watching, and a start that
223
+ # stops is a card not started, with why. `task start` reads the card again,
224
+ # and holds it when a card that blocks it is not done after all. A card is
225
+ # never started twice in one run, nor when it has a task on this machine
226
+ # already, nor when its merge is the last thing recorded of it here: Linear
227
+ # may still have such a card where it was, when it could not be moved. Nor
228
+ # is a slice started through the card it was sliced from when it is among
229
+ # the cards given and not ready itself: its own line says why, and a `task
230
+ # start` that could only hold it would ask Linear for nothing.
231
+ #
232
+ # CHAIN_FAILED is 1 when a card that was ready could not be started.
233
+ # chain_start <the cards, as linear_blocks or linear_waiting prints them> <the home space, or ''>
234
+ CHAIN_FAILED=0
235
+ chain_start() {
236
+ local found=$1 home=$2 teams lines sep=$'\x1f' kind card start space why of dir out rc line held stopped tried=' '
237
+ local left=' '
238
+ CHAIN_FAILED=0
239
+ teams=$(chain_teams) || teams='{}'
240
+ if ! lines=$(printf '%s' "$found" |
241
+ jq -r --argjson teams "$teams" --arg home "$home" "$CHAIN_VERDICTS" 2>/dev/null); then
242
+ CHAIN_FAILED=1
243
+ say "no card was started: what Linear said of the cards that wait is not something ocnus can read"
244
+ return 0
245
+ fi
246
+ # The cards that are not ready, each of which has a line of its own.
247
+ while IFS=$sep read -r kind card start space why; do
248
+ [ "$kind" != skip ] || left+="$card "
249
+ done <<< "$lines"
250
+ while IFS=$sep read -r kind card start space why; do
251
+ case $kind in
252
+ skip) say "$card was not started: $why"; continue ;;
253
+ start) ;;
254
+ *) continue ;;
255
+ esac
256
+ # A slice of a sliced card may be on the list itself: started once, and
257
+ # not at all when its own line says it is not ready.
258
+ case $tried in *" $start "*) continue ;; esac
259
+ case $left in *" $start "*) continue ;; esac
260
+ tried+="$start "
261
+ of=''
262
+ [ "$start" = "$card" ] || of=", the first open slice of $card"
263
+ if ! dir=$(space_dir "$space" 2>/dev/null); then
264
+ say "$start was not started: there is no space called '$space'"
265
+ continue
266
+ fi
267
+ if chain_has_task "$dir" "$start"; then
268
+ say "$start was not started: it has a task in '$space' already, though Linear has it as a card that waits"
269
+ continue
270
+ fi
271
+ if chain_merged "$space" "$start"; then
272
+ say "$start was not started: its merge is recorded in '$space', though Linear has it as a card that waits"
273
+ continue
274
+ fi
275
+ say "Starting $start in '$space'$of" >&2
276
+ rc=0
277
+ # Nothing it runs may read the list of cards this loop is reading.
278
+ out=$("$REPO/cli/ocnus" task start "$space" "$start" </dev/null 2>&1) || rc=$?
279
+ [ -z "$out" ] || printf '%s\n' "$out" >&2
280
+ if [ "$rc" = 0 ]; then
281
+ say "$start was started$of"
282
+ continue
283
+ fi
284
+ # Why it stopped is the line die printed, the last one. A card that was
285
+ # held is one that still waits, and each card that holds it is on a line
286
+ # of its own (held_message).
287
+ stopped='' held=''
288
+ while IFS= read -r line; do
289
+ case $line in
290
+ 'ocnus: '*) stopped=$line ;;
291
+ ' blocker '*) held=${held:+$held, and }${line# blocker } ;;
292
+ esac
293
+ done <<< "$out"
294
+ case $stopped in
295
+ "ocnus: $start $HELD_SAYS"*)
296
+ say "$start was not started$of: ${held:-a card that blocks it is not done yet}"
297
+ ;;
298
+ *)
299
+ CHAIN_FAILED=1
300
+ [ -n "$stopped" ] || stopped=${out##*$'\n'}
301
+ stopped=$(first_sentence "$stopped")
302
+ say "$start was not started$of: task start stopped${stopped:+: $stopped}"
303
+ ;;
304
+ esac
305
+ done <<< "$lines"
306
+ }
307
+
308
+ # --- at a merge -------------------------------------------------------------
309
+
310
+ # Start the cards that the card $2, whose merge was just recorded in the
311
+ # space $1, was blocking, and print a line for each card started and each
312
+ # card not started, as chain_start does, for the merge's flags. It asks
313
+ # Linear once. It never stops what called it: what it could not do is a line
314
+ # too, and the catch-up starts what it missed.
315
+ #
316
+ # With the chain off, it does nothing. With no Linear key saved, it does
317
+ # nothing either, and says nothing: Linear is not used on this machine, which
318
+ # the note on the card's move says already.
319
+ # merge_chain <space> <card ID, in upper case>
320
+ merge_chain() {
321
+ local space=$1 card=$2 found
322
+ if ! settings_load; then
323
+ say "the cards $card was blocking were not looked for: $(first_sentence "$SETTINGS_WHY")"
324
+ return 0
325
+ fi
326
+ [ "$SET_CHAIN" = true ] || return 0
327
+ [ -n "${LINEAR_API_KEY:-}" ] || return 0
328
+ # Only the last line is the answer: anything before it was said on stderr.
329
+ if ! found=$(linear_blocks "$card" 2>&1); then
330
+ say "the cards $card was blocking were not looked for: $(first_sentence "$found")"
331
+ return 0
332
+ fi
333
+ found=${found##*$'\n'}
334
+ [ "$(printf '%s' "$found" | jq -r '.cut' 2>/dev/null)" != true ] ||
335
+ say "$card blocks more cards in Linear than ocnus reads at once, so some were not looked at"
336
+ chain_start "$found" "$space"
337
+ }
338
+
339
+ # --- the catch-up -----------------------------------------------------------
340
+
341
+ # Start every card of a space's teams that waits and is ready, and print a
342
+ # line for each card started and each card not started, as chain_start does.
343
+ # It asks Linear once for every WAITING_PAGE cards that wait. It stops, as
344
+ # die does, when the space has no team or Linear cannot be asked.
345
+ #
346
+ # The human names the space, so every card starts in it. The watch names
347
+ # none, so a card whose team more than one space lists is not started: it
348
+ # cannot tell which one the human means.
349
+ # ready_cards <space> <the space's folder> <named|watch>
350
+ ready_cards() {
351
+ local name=$1 dir=$2 how=$3 keys team teams=() found home=$1
352
+ keys=$(space_teams "$dir") || exit 1
353
+ while IFS= read -r team; do [ -z "$team" ] || teams+=("$team"); done <<< "$keys"
354
+ [ "${#teams[@]}" -gt 0 ] ||
355
+ die "space '$name' has no Linear team, so there are no cards to start. Set it with: ocnus space team $name <team key>, like: ocnus space team $name ABC"
356
+ found=$(linear_waiting "${teams[@]}") || exit 1
357
+ if [ "$(printf '%s' "$found" | jq -r '.cards | length' 2>/dev/null)" = 0 ]; then
358
+ say "No card of '$name' waits to start."
359
+ return 0
360
+ fi
361
+ [ "$how" = named ] || home=''
362
+ chain_start "$found" "$home"
363
+ }
package/cli/lib/close.sh CHANGED
@@ -2,8 +2,10 @@
2
2
  # Sourced by cli/ocnus.
3
3
 
4
4
  # Once `task start` has opened a card's build tab, the write-up agent, in the
5
- # tab `task new` named after the card, has nothing left to do. `task start`
6
- # starts `task close` for it (start_close), detached in herdr-core, so that it
5
+ # tab `task new` named after the card, has nothing left to do. Nor has it
6
+ # once `task start` has held the card for a card that blocks it: the card
7
+ # waits in Linear, and the chain opens its build tab later (chain.sh). `task
8
+ # start` starts `task close` for it (start_close), detached in herdr-core, so that it
7
9
  # outlives the turn of the agent that ran `task start`. `task close` waits for
8
10
  # that turn to end, ends the agent's Claude session with /exit, waits for the
9
11
  # tab's own shell to come back, then closes the tab, as `task finish` closes a
@@ -160,7 +162,7 @@ task_close() {
160
162
  }
161
163
 
162
164
  # Start `task close` for a card's write-up session, from `task start` once the
163
- # build tab is open: detached in herdr-core, so that it outlives the turn of
165
+ # build tab is open, or once it has held the card: detached in herdr-core, so that it outlives the turn of
164
166
  # the agent that ran `task start`, and in the space's folder, which stays. Its
165
167
  # terminal has none of ocnus's or Herdr's labels, so the Herdr app does
166
168
  # not show it and `host` does not count it as an agent; no parent, so it goes