@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,447 @@
1
+ # Archiving closed cards in Linear, when the human turns it on. Sourced by
2
+ # cli/ocnus.
3
+
4
+ # Linear's free plan stops making cards once 250 are not archived, and a card
5
+ # that is done stays in its team's lists until Linear's own auto-archive takes
6
+ # it, months later. With archiving on, recording a card's merge archives the
7
+ # card, once its move to Done has put it in a closed status, and every other
8
+ # closed card of the space's teams: done, canceled or a duplicate. An
9
+ # archived card is not deleted: it can still be opened by its ID, and
10
+ # restored from its team's archive. Archiving is off unless the settings file
11
+ # says "archive": true (settings.sh), which `linear archive on` writes.
12
+ #
13
+ # A card that has a parent card or sub-issues is archived with its family: the
14
+ # parent and its sub-issues, the slices it was cut into. A family is archived
15
+ # only once all of it is closed, since archiving a card archives its
16
+ # sub-issues with it: each slice, and then the parent. A family that is open
17
+ # anywhere, or deeper than a parent and its slices, is left as it is. The
18
+ # merge of a parent's last open slice closes the parent (linear.sh), so that
19
+ # merge archives the whole family.
20
+
21
+ # What `linear archive` takes after it.
22
+ TAKES_ARCHIVE='on or off, or nothing to say whether archiving is on (--json for programs)'
23
+
24
+ # --- the setting ------------------------------------------------------------
25
+
26
+ # ocnus linear archive [on|off] [--json]: say whether archiving is on, or
27
+ # turn it on or off. --json prints it as one line for programs, with the file
28
+ # it is kept in, for a Herdr mod. on and off write "archive" into the
29
+ # settings file as profile_set writes a profile: everything is checked before
30
+ # the file is touched, and the new file is checked before it takes the old
31
+ # one's place, so a refusal or a failure leaves the file as it was.
32
+ linear_archive_cmd() {
33
+ local usage="usage: ocnus linear archive [on|off|--json]: it takes $TAKES_ARCHIVE"
34
+ case $# in
35
+ 0) archive_show plain ;;
36
+ 1)
37
+ case $1 in
38
+ --json) archive_show json ;;
39
+ on) archive_set true ;;
40
+ off) archive_set false ;;
41
+ *) die "'$1' is not on or off. ocnus linear archive takes $TAKES_ARCHIVE." ;;
42
+ esac
43
+ ;;
44
+ *) die "$usage" ;;
45
+ esac
46
+ }
47
+
48
+ # Say whether archiving is on: plain, for a person, or json, for programs.
49
+ archive_show() {
50
+ need_jq
51
+ settings_check
52
+ if [ "$1" = json ]; then
53
+ printf '{"file":"%s","archive":%s}\n' "$(json_str "$SETTINGS")" "$SET_ARCHIVE"
54
+ elif [ "$SET_ARCHIVE" = true ]; then
55
+ say "Archiving is on: recording a card's merge archives it in Linear, and every other closed card of the space's teams. A sliced card is archived with its slices, once all of them are closed."
56
+ say "Turn it off with: ocnus linear archive off"
57
+ else
58
+ say "Archiving is off: a merged card stays in Done in Linear."
59
+ say "Turn it on with: ocnus linear archive on"
60
+ fi
61
+ }
62
+
63
+ # Write "archive": true or false into the settings file. With no file yet,
64
+ # the new one starts from the defaults, so that it names every profile.
65
+ archive_set() {
66
+ local want=$1 tmp ok=0
67
+ need_jq
68
+ settings_check
69
+ mkdir -p "${SETTINGS%/*}"
70
+ tmp=$SETTINGS.ocnus.$$
71
+ if { if [ -e "$SETTINGS" ]; then cat "$SETTINGS"; else settings_default; fi; } |
72
+ jq --argjson on "$want" '.archive = $on' > "$tmp" 2>/dev/null &&
73
+ [ "$(SETTINGS=$tmp; settings_load && printf '%s' "$SET_ARCHIVE")" = "$want" ] &&
74
+ mv -f "$tmp" "$SETTINGS"; then
75
+ ok=1
76
+ fi
77
+ rm -f "$tmp"
78
+ [ "$ok" = 1 ] || die "could not change $SETTINGS, so it is as it was."
79
+ settings_check
80
+ if [ "$want" = true ]; then
81
+ say "Archiving is on now. The next merge recorded archives its card in Linear, and every other closed card of the space's teams."
82
+ else
83
+ say "Archiving is off now. A merged card stays in Done in Linear."
84
+ fi
85
+ }
86
+
87
+ # --- archiving at the merge -------------------------------------------------
88
+
89
+ # The merged card's question: its status, its parent with its status, and
90
+ # whether it has sub-issues.
91
+ ARCHIVE_CARD_QUERY='query($team: String!, $number: Float!) {
92
+ issues(first: 1, filter: {team: {key: {eq: $team}}, number: {eq: $number}}) {
93
+ nodes { id identifier state { name type } parent { identifier state { name type } }
94
+ children(first: 1) { nodes { identifier } } }
95
+ }
96
+ }'
97
+ # The kinds of status a closed card is in: done, and canceled, which is where
98
+ # a duplicate is too.
99
+ CLOSED_TYPES='["completed","canceled"]'
100
+ # One page of the closed cards of some teams, in the order they were made,
101
+ # as linear_open_cards pages through the open ones.
102
+ CLOSED_QUERY='query($teams: [String!], $types: [String!], $first: Int, $after: String) {
103
+ issues(first: $first, after: $after, orderBy: createdAt,
104
+ filter: {team: {key: {in: $teams}}, state: {type: {in: $types}}}) {
105
+ nodes { id identifier state { name type } parent { identifier state { name type } }
106
+ children(first: 1) { nodes { identifier } } }
107
+ pageInfo { hasNextPage endCursor }
108
+ }
109
+ }'
110
+ # How many sub-issues of a parent are read, to tell whether all are closed.
111
+ FAMILY_SUBS=50
112
+ # A family's question: the parent card, its status, whether it has a parent
113
+ # of its own, and its sub-issues, each with its status and whether it has
114
+ # sub-issues of its own. It is asked once for each family, not for each card
115
+ # on a page of closed cards: a hundred cards with fifty sub-issues each would
116
+ # cost more than Linear lets one question cost.
117
+ FAMILY_QUERY='query($team: String!, $number: Float!) {
118
+ issues(first: 1, filter: {team: {key: {eq: $team}}, number: {eq: $number}}) {
119
+ nodes { id identifier state { name type } parent { identifier }
120
+ children(first: '$FAMILY_SUBS') {
121
+ nodes { id identifier state { name type } children(first: 1) { nodes { identifier } } }
122
+ pageInfo { hasNextPage } } }
123
+ }
124
+ }'
125
+ # Archive one card. Without trash, so it is archived, not deleted.
126
+ ARCHIVE_MUTATION='mutation($id: String!) { issueArchive(id: $id) { success } }'
127
+
128
+ # Whether a card, in the answer to the merged card's question or on a page
129
+ # of closed cards, is to be archived. why is why it is left as it is, or null
130
+ # when it is to be archived, alone or with its family; family is the ID of
131
+ # its family's parent, which is its own parent when it is a slice and itself
132
+ # when it has sub-issues, or null when it has neither. A slice whose parent
133
+ # is open is left without asking about the family. A card whose id, ID,
134
+ # status, parent or sub-issues cannot be read did not come from Linear, so
135
+ # the whole answer is unreadable and nothing is archived on its say; a card
136
+ # is never just left out. A status's name is anyone's text, so a control
137
+ # character in it becomes a space, as `task ls` does, and the why stays on
138
+ # one line.
139
+ ARCHIVE_WHY='
140
+ def plain: explode | map(if . < 32 or (. > 126 and . < 160) then 32 else . end) | implode;
141
+ def is_card_id: (.identifier | type) == "string" and (.identifier | test("^[A-Za-z][A-Za-z0-9]*-[0-9]+$"));
142
+ def closed: .state.type | . == "completed" or . == "canceled";
143
+ def status: (.state.name // .state.type) | tostring | plain;
144
+ def why:
145
+ if (.id | type) != "string" or (.id | test("^\\S+$") | not) or (is_card_id | not)
146
+ or (.state | type) != "object" or (.state.type | type) != "string"
147
+ or (.children | type) != "object" or (.children.nodes | type) != "array"
148
+ or (.parent != null and ((.parent | type) != "object" or (.parent | is_card_id | not)
149
+ or (.parent.state | type) != "object" or (.parent.state.type | type) != "string"))
150
+ then error("unreadable")
151
+ elif (closed | not) then "it is in \(status), which is not closed"
152
+ elif .parent != null and (.parent | closed | not)
153
+ then "its parent \(.parent.identifier | ascii_upcase) is in \(.parent | status), which is not closed"
154
+ else null end;
155
+ def family:
156
+ if .parent != null then .parent.identifier | ascii_upcase
157
+ elif (.children.nodes | length) > 0 then .identifier | ascii_upcase
158
+ else null end;'
159
+
160
+ # From the answer to FAMILY_QUERY, on stdin, what to do with a family: the
161
+ # cards to archive, in order, each slice in Linear's order and then the
162
+ # parent, as {"cards": [{"card": "ABC-123", "id": <Linear's id>}]}; or why
163
+ # the family is left as it is, as {"why": <the rest of a sentence about the
164
+ # parent>}, such as "is in In Progress, which is not closed". A family is
165
+ # archived only when the parent is closed and is no one's sub-issue, every
166
+ # one of its sub-issues is closed and has none of its own, and Linear has
167
+ # listed them all. An answer that cannot be read did not come from Linear.
168
+ FAMILY_PLAN='
169
+ def plain: explode | map(if . < 32 or (. > 126 and . < 160) then 32 else . end) | implode;
170
+ def card_id: if (.identifier | type) != "string" or (.identifier | test("^[A-Za-z][A-Za-z0-9]*-[0-9]+$") | not)
171
+ then error("unreadable") else .identifier | ascii_upcase end;
172
+ def read: if (.id | type) != "string" or (.id | test("^\\S+$") | not)
173
+ or (.state | type) != "object" or (.state.type | type) != "string"
174
+ or (.children | type) != "object" or (.children.nodes | type) != "array"
175
+ then error("unreadable") else . end;
176
+ def closed: .state.type | . == "completed" or . == "canceled";
177
+ if (.issues.nodes | type) != "array" then error("no list of cards")
178
+ elif (.issues.nodes | length) == 0 then {why: "is not a card the saved key can see"}
179
+ else .issues.nodes[0] | read
180
+ | if (.children.pageInfo.hasNextPage | type) != "boolean"
181
+ or (.parent != null and (.parent | type) != "object")
182
+ then error("unreadable") else . end
183
+ | {card: card_id, id} as $parent
184
+ | [.children.nodes[] | read | {card: card_id, id, closed: closed, deep: ((.children.nodes | length) > 0)}] as $subs
185
+ | [$subs[] | select(.closed | not) | .card] as $open
186
+ | if (closed | not) then {why: "is in \((.state.name // .state.type) | tostring | plain), which is not closed"}
187
+ elif .parent != null then {why: "is itself a sub-issue of \((.parent.identifier // "another card") | tostring | plain)"}
188
+ elif .children.pageInfo.hasNextPage then {why: "has more than '$FAMILY_SUBS' sub-issues, more than ocnus reads"}
189
+ elif ($open | length) > 0
190
+ then {why: "has \($open | length) of its \($subs | length) slices still open (\($open | join(", ")))"}
191
+ elif any($subs[]; .deep)
192
+ then {why: "has a slice with sub-issues of its own (\([$subs[] | select(.deep) | .card] | first))"}
193
+ else {cards: ([$subs[] | {card, id}] + [$parent])}
194
+ end
195
+ end'
196
+
197
+ # The variables of a question about one card, by its team and number, or
198
+ # stop and say why.
199
+ # archive_vars <card ID>
200
+ archive_vars() {
201
+ jq -cn --arg team "${1%-*}" --arg number "${1##*-}" \
202
+ '{team: $team, number: ($number | tonumber)}' 2>/dev/null ||
203
+ die "'$1' is not a card ID like ABC-122."
204
+ }
205
+
206
+ # What to do with the merged card, as one line of JSON, or stop and say why:
207
+ # {"id": <Linear's id for it>, "why": <why it is left as it is, or null>,
208
+ # "family": <its family's parent, or null>}
209
+ # archive_card_plan <key> <card ID, in upper case>
210
+ archive_card_plan() {
211
+ local key=$1 card=$2 vars data plan
212
+ vars=$(archive_vars "$card") || exit 1
213
+ data=$(linear_ask "$key" "$ARCHIVE_CARD_QUERY" "$vars") || exit 1
214
+ plan=$(printf '%s' "$data" | jq -c "$ARCHIVE_WHY"'
215
+ if (.issues.nodes | type) != "array" then error("no list of cards")
216
+ elif (.issues.nodes | length) == 0 then "none"
217
+ else .issues.nodes[0] | {id, why: why, family: family} end' 2>/dev/null) || plan=
218
+ case $plan in
219
+ '{'*) printf '%s\n' "$plan" ;;
220
+ '"none"') die "Linear has no card $card that the saved key can see." ;;
221
+ *) die "Linear's answer was not one ocnus can read." ;;
222
+ esac
223
+ }
224
+
225
+ # What to do with a family, by FAMILY_PLAN, as one line of JSON, or stop and
226
+ # say why.
227
+ # family_plan <key> <the family's parent, in upper case>
228
+ family_plan() {
229
+ local key=$1 card=$2 vars data plan
230
+ vars=$(archive_vars "$card") || exit 1
231
+ data=$(linear_ask "$key" "$FAMILY_QUERY" "$vars") || exit 1
232
+ plan=$(printf '%s' "$data" | jq -c "$FAMILY_PLAN" 2>/dev/null) || plan=
233
+ case $plan in
234
+ '{'*) printf '%s\n' "$plan" ;;
235
+ *) die "Linear's answer was not one ocnus can read." ;;
236
+ esac
237
+ }
238
+
239
+ # Why a card of a family that is left as it is was not archived, from why
240
+ # the family is left, which is about its parent: about the card itself when
241
+ # it is the parent, and else about its parent.
242
+ # family_why <card ID> <the family's parent> <why, from FAMILY_PLAN>
243
+ family_why() {
244
+ if [ "$1" = "$2" ]; then printf 'it %s' "$3"
245
+ else printf 'its parent %s %s' "$2" "$3"
246
+ fi
247
+ }
248
+
249
+ # The closed cards of some teams, but for the merged card and its family, as
250
+ # one line of JSON, or stop and say why. They are taken a family at a time,
251
+ # in the order of the first of each that is listed, so that each family is
252
+ # asked about once:
253
+ # {"more": <whether Linear had more than it was asked for>,
254
+ # "units": [{"card": "ABC-1", "id": <Linear's id>, "why": <or null>},
255
+ # {"family": "ABC-7", "cards": ["ABC-8", "ABC-7"]}]}
256
+ # A card with no family, or one left as it is without asking about its
257
+ # family, such as a slice whose parent is open, is a unit of its own; the
258
+ # rest are their family's, with the cards of it that are listed.
259
+ # Every page is fetched before a card is archived, since a card archived
260
+ # drops out of the pages after it. Past LINEAR_PAGES pages, the cards listed
261
+ # so far are the list, and the next merge lists the rest.
262
+ # archive_closed_cards <key> <the teams, as a JSON list> <card ID> <its family's parent, or nothing>
263
+ archive_closed_cards() {
264
+ local key=$1 teams=$2 card=$3 family=${4:-} vars data pages='' after='' more asked=0 rest=false list
265
+ while :; do
266
+ asked=$((asked + 1))
267
+ [ "$asked" -le "$LINEAR_PAGES" ] || { rest=true; break; }
268
+ vars=$(jq -cn --argjson teams "$teams" --argjson types "$CLOSED_TYPES" --argjson first "$LINEAR_PAGE" \
269
+ --arg after "$after" \
270
+ '{teams: $teams, types: $types, first: $first, after: (if $after == "" then null else $after end)}')
271
+ data=$(linear_ask "$key" "$CLOSED_QUERY" "$vars") || exit 1
272
+ pages+=$data$'\n'
273
+ more=$(printf '%s' "$data" | jq -r '.issues.pageInfo.hasNextPage == true' 2>/dev/null) ||
274
+ die "Linear's answer was not one ocnus can read."
275
+ [ "$more" = true ] || break
276
+ after=$(printf '%s' "$data" | jq -r '.issues.pageInfo.endCursor | strings' 2>/dev/null) || after=
277
+ [ -n "$after" ] ||
278
+ die "Linear said it had more closed cards but not where the next ones start."
279
+ done
280
+ # A card on two pages is listed once, where it came first.
281
+ list=$(printf '%s' "$pages" | jq -cs --arg card "$card" --arg family "$family" --argjson rest "$rest" "$ARCHIVE_WHY"'
282
+ if any(.[]; (.issues.nodes | type) != "array") then error("no list of cards") else . end
283
+ | [.[].issues.nodes[]]
284
+ | if any(.[]; (.identifier | type) != "string") then error("unreadable") else . end
285
+ | reduce .[] as $c ({seen: {}, cards: []};
286
+ if .seen[$c.identifier] then . else .seen[$c.identifier] = true | .cards += [$c] end)
287
+ | {more: $rest,
288
+ units: ([.cards[] | {card: (.identifier | ascii_upcase), id, why: why, family: family}
289
+ | select(.card != $card and .family != $family)]
290
+ | reduce .[] as $c ({units: [], at: {}};
291
+ if $c.why != null or $c.family == null then .units += [{card: $c.card, id: $c.id, why: $c.why}]
292
+ elif .at[$c.family] != null then .at[$c.family] as $i | .units[$i].cards += [$c.card]
293
+ else .at[$c.family] = (.units | length) | .units += [{family: $c.family, cards: [$c.card]}]
294
+ end)
295
+ | .units)}' 2>/dev/null) ||
296
+ die "Linear's answer was not one ocnus can read."
297
+ printf '%s\n' "$list"
298
+ }
299
+
300
+ # Archive one card, and read Linear's word that it did, or stop and say why.
301
+ # archive_card <key> <Linear's id for the card>
302
+ archive_card() {
303
+ local data
304
+ data=$(linear_ask "$1" "$ARCHIVE_MUTATION" "$(jq -cn --arg id "$2" '{id: $id}')") || exit 1
305
+ [ "$(printf '%s' "$data" | jq -r '.issueArchive.success' 2>/dev/null)" = true ] ||
306
+ die "Linear did not say it archived the card."
307
+ }
308
+
309
+ # Archive cards in order, one "<card ID><tab><Linear's id>" a line, until one
310
+ # fails. Each card archived adds one to ARCHIVE_COUNT, and with say, it says
311
+ # so. At the first that fails it says why, naming the card, sets
312
+ # ARCHIVE_STOPPED to it and returns 1: the cards after it are not tried.
313
+ # archive_cards <key> <the cards> [say]
314
+ archive_cards() {
315
+ local key=$1 c id out
316
+ while IFS=$'\t' read -r c id; do
317
+ [ -n "$c" ] || continue
318
+ if out=$(archive_card "$key" "$id" 2>&1); then
319
+ ARCHIVE_COUNT=$((ARCHIVE_COUNT + 1))
320
+ [ "${3:-}" != say ] || say "$c archived in Linear"
321
+ else
322
+ say "$c was not archived in Linear: $(first_sentence "$out")"
323
+ ARCHIVE_STOPPED=$c
324
+ return 1
325
+ fi
326
+ done <<< "$2"
327
+ }
328
+
329
+ # The cards of a family plan that is to be archived, as archive_cards takes
330
+ # them.
331
+ # family_cards <a plan from family_plan>
332
+ family_cards() { printf '%s' "$1" | jq -r '.cards[] | .card + "\t" + .id'; }
333
+
334
+ # Archive the merged card, with its family when it has one, and the other
335
+ # closed cards of the space's teams, once the merge is recorded and the card
336
+ # moved to Done, when archiving is on; with it off, Linear is asked nothing.
337
+ # The merged card's family is asked about after the moves, so a merge that
338
+ # closed the last open slice, and with it the parent, finds the family closed.
339
+ # The merge is recorded by now, so this never stops: each part that can stop
340
+ # runs in a subshell of its own. It says one line for each card it archives
341
+ # at the merge, and for each card it leaves as it is, the card's ID and then
342
+ # why, in the first sentence of it, and one line with how many other closed
343
+ # cards it archived. The first call to Linear that fails ends it, naming the
344
+ # card it was on: a key that cannot write, or a Linear out of reach, would
345
+ # fail every card after it the same way, and each call waits for Linear, so
346
+ # going on could keep the hand-off for minutes. The next merge tries again.
347
+ # It returns 1 when a card was not archived.
348
+ # linear_archive_note <space> <card ID, in upper case>
349
+ linear_archive_note() {
350
+ local project=$1 card=$2 key plan id why family='' fam teams shown list each kind c cards
351
+ local stopped='' rc=0 more noun line
352
+ ARCHIVE_COUNT=0 ARCHIVE_STOPPED=
353
+ if ! settings_load; then
354
+ say "$card was not archived in Linear: $(first_sentence "$SETTINGS_WHY")"
355
+ return 1
356
+ fi
357
+ [ "$SET_ARCHIVE" = true ] || return 0
358
+ # The merged card, with its family when it has one.
359
+ if ! key=$( { linear_tools && linear_saved_key; } 2>&1); then
360
+ stopped=$key
361
+ elif ! plan=$(archive_card_plan "$key" "$card" 2>&1); then
362
+ stopped=$plan
363
+ else
364
+ id=$(printf '%s' "$plan" | jq -r '.id')
365
+ why=$(printf '%s' "$plan" | jq -r '.why // empty')
366
+ family=$(printf '%s' "$plan" | jq -r '.family // empty')
367
+ if [ -n "$why" ]; then
368
+ say "$card was not archived in Linear: $why"
369
+ rc=1
370
+ elif [ -z "$family" ]; then
371
+ archive_cards "$key" "$card"$'\t'"$id" say || true
372
+ elif ! fam=$(family_plan "$key" "$family" 2>&1); then
373
+ stopped=$fam
374
+ elif why=$(printf '%s' "$fam" | jq -r '.why // empty') && [ -n "$why" ]; then
375
+ say "$card was not archived in Linear: $(family_why "$card" "$family" "$why")"
376
+ rc=1
377
+ else
378
+ archive_cards "$key" "$(family_cards "$fam")" say || true
379
+ fi
380
+ fi
381
+ if [ -n "$stopped" ]; then
382
+ say "$card was not archived in Linear: $(first_sentence "$stopped")"
383
+ ARCHIVE_STOPPED=$card
384
+ fi
385
+ if [ -n "$ARCHIVE_STOPPED" ]; then
386
+ say "The other closed cards were not archived in Linear: archiving stopped at $ARCHIVE_STOPPED, and the next merge tries again"
387
+ return 1
388
+ fi
389
+ # The other closed cards of the space's teams, but for the merged card's
390
+ # family, which is dealt with above.
391
+ if ! teams=$( { dir=$(space_dir "$project") && space_teams "$dir"; } 2>&1); then
392
+ say "The other closed cards were not archived in Linear: $(first_sentence "$teams")"
393
+ return 1
394
+ fi
395
+ if [ -z "$teams" ]; then
396
+ say "The other closed cards were not archived in Linear: space '$project' has no Linear team"
397
+ return "$rc"
398
+ fi
399
+ teams=$(printf '%s\n' "$teams" | jq -Rcn '[inputs]')
400
+ shown=$(printf '%s' "$teams" | jq -r 'join(", ")')
401
+ if ! list=$(archive_closed_cards "$key" "$teams" "$card" "$family" 2>&1); then
402
+ say "The other closed cards of $shown were not archived in Linear: $(first_sentence "$list")"
403
+ return 1
404
+ fi
405
+ more=$(printf '%s' "$list" | jq -r '.more')
406
+ # One line for each card on its own: card, its ID, Linear's id for it, and
407
+ # why it is left, when it is. The why comes last, so that an empty one is
408
+ # no field at all. And one for each family: family, its parent's ID, and the
409
+ # IDs of its cards that were listed.
410
+ each=$(printf '%s' "$list" | jq -r '.units[]
411
+ | if .family then ["family", .family, (.cards | join(" "))] else ["card", .card, .id, (.why // "")] end
412
+ | join("\t")')
413
+ ARCHIVE_COUNT=0
414
+ while IFS=$'\t' read -r kind c id why; do
415
+ [ -n "$c" ] || continue
416
+ if [ "$kind" = family ]; then
417
+ cards=$id
418
+ if ! fam=$(family_plan "$key" "$c" 2>&1); then
419
+ say "$c was not archived in Linear: $(first_sentence "$fam")"
420
+ ARCHIVE_STOPPED=$c rc=1
421
+ break
422
+ fi
423
+ why=$(printf '%s' "$fam" | jq -r '.why // empty')
424
+ if [ -n "$why" ]; then
425
+ for id in $cards; do say "$id was not archived in Linear: $(family_why "$id" "$c" "$why")"; done
426
+ rc=1
427
+ elif ! archive_cards "$key" "$(family_cards "$fam")"; then
428
+ rc=1
429
+ break
430
+ fi
431
+ elif [ -n "$why" ]; then
432
+ say "$c was not archived in Linear: $why"
433
+ rc=1
434
+ elif ! archive_cards "$key" "$c"$'\t'"$id"; then
435
+ rc=1
436
+ break
437
+ fi
438
+ done <<< "$each"
439
+ noun=cards
440
+ [ "$ARCHIVE_COUNT" != 1 ] || noun=card
441
+ line="Archived $ARCHIVE_COUNT other closed $noun of $shown in Linear"
442
+ if [ -n "$ARCHIVE_STOPPED" ]; then line="$line, and stopped at $ARCHIVE_STOPPED: the next merge tries the rest"
443
+ elif [ "$more" = true ]; then line="$line; Linear has more, which the next merge archives"
444
+ fi
445
+ say "$line"
446
+ return "$rc"
447
+ }
@@ -0,0 +1,166 @@
1
+ # Which account every agent ocnus starts runs on: the human's Claude
2
+ # subscription, or an Anthropic API key they have saved. Sourced by
3
+ # cli/ocnus.
4
+
5
+ # The choice is "billing" in the settings file (settings.sh), "subscription"
6
+ # or "api", and the subscription when the file leaves it out. It is read as
7
+ # `task new` and `task build` start claude (profile_args, in task.sh), so a
8
+ # change reaches the next agent started, and an agent already running keeps
9
+ # the account it started on.
10
+ #
11
+ # The key is the line ANTHROPIC_API_KEY=… in ocnus's env file, which
12
+ # `billing key` writes by the rules `linear key` follows (common.sh).
13
+ # cli/ocnus's load_env reads it from the file alone, into
14
+ # SAVED_ANTHROPIC_KEY, which is not exported: an ANTHROPIC_API_KEY exported
15
+ # in the shell never takes its place, and no program ocnus starts finds
16
+ # the saved key in its environment.
17
+ #
18
+ # On api, claude starts with --settings= holding an apiKeyHelper: a command
19
+ # claude runs to get the key, `ocnus billing key --print`. So the key is
20
+ # in no argument given to claude, hc or herdr-app, and in no agent's
21
+ # environment. The helper prints the saved key whatever billing is set to
22
+ # when claude asks, so an agent started on the API keeps it after a switch,
23
+ # and an agent started on the subscription has no helper to ask.
24
+
25
+ # What `ocnus billing` takes after it.
26
+ TAKES_BILLING='api or subscription, key to save an Anthropic API key, or nothing to say which is set (--json for programs)'
27
+ BILLING_USAGE="usage: ocnus billing [--json | api | subscription | key]: it takes $TAKES_BILLING"
28
+ BILLING_KEY_USAGE='usage: ocnus billing key (it asks for the key, or reads it from stdin; the key is never an argument)'
29
+ # What an Anthropic API key is made of: sk-ant- and then letters, digits, -
30
+ # and _, as in sk-ant-api03-…. Every letter is spelled out, as in
31
+ # valid_group, so no locale changes what a letter is. A key with anything
32
+ # else in it could not be read back from the env file as it was written.
33
+ ANTHROPIC_KEY_RE='^sk-ant-[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-]+$'
34
+ # Where the human makes a key.
35
+ ANTHROPIC_KEY_PLACE='the Claude Console, under API keys'
36
+ # What stops `task new` and `task build` on api with no key saved.
37
+ BILLING_NO_KEY="billing is on api, and no Anthropic API key is saved, so no agent was started. Save one with: ocnus billing key, or go back to the subscription with: ocnus billing subscription"
38
+
39
+ # ocnus billing [--json | api | subscription | key | key --print]: say
40
+ # which account the agents run on, set it, or save the key. --json prints it
41
+ # as one line for programs, with the file it is kept in and whether a key is
42
+ # saved. api and subscription write "billing" into the settings file as
43
+ # approval_set writes "approval": everything is checked before the file is
44
+ # touched, and the new file is checked before it takes the old one's place,
45
+ # so a refusal or a failure leaves the file as it was. `key --print` is for
46
+ # claude's apiKeyHelper alone.
47
+ cmd_billing() {
48
+ if [ "${1:-}" = key ]; then
49
+ shift
50
+ case $# in
51
+ 0) billing_key ;;
52
+ 1) [ "$1" = --print ] || die "$BILLING_KEY_USAGE"
53
+ billing_key_print ;;
54
+ *) die "$BILLING_KEY_USAGE" ;;
55
+ esac
56
+ return
57
+ fi
58
+ case $# in
59
+ 0) billing_show plain ;;
60
+ 1)
61
+ case $1 in
62
+ --json) billing_show json ;;
63
+ api | subscription) billing_set "$1" ;;
64
+ *) die "'$1' is not api or subscription. ocnus billing takes $TAKES_BILLING." ;;
65
+ esac
66
+ ;;
67
+ *) die "$BILLING_USAGE" ;;
68
+ esac
69
+ }
70
+
71
+ # Whether an Anthropic API key is saved: true or false.
72
+ billing_key_saved() { if [ -n "$SAVED_ANTHROPIC_KEY" ]; then printf true; else printf false; fi; }
73
+
74
+ # Say which account the agents run on: plain, for a person, or json, for
75
+ # programs, as {"file":…,"billing":"subscription","key":true}.
76
+ billing_show() {
77
+ need_jq
78
+ settings_check
79
+ if [ "$1" = json ]; then
80
+ printf '{"file":"%s","billing":"%s","key":%s}\n' "$(json_str "$SETTINGS")" "$SET_BILLING" "$(billing_key_saved)"
81
+ elif [ "$SET_BILLING" = api ]; then
82
+ if [ -n "$SAVED_ANTHROPIC_KEY" ]; then
83
+ say "Billing is on the API: every agent task new and task build start runs on the Anthropic API key saved in $ENV_FILE."
84
+ else
85
+ say "Billing is on the API: every agent task new and task build start runs on an Anthropic API key, and none is saved, so they stop before an agent starts."
86
+ say "Save one with: ocnus billing key"
87
+ fi
88
+ say "Go back to the subscription with: ocnus billing subscription"
89
+ else
90
+ say "Billing is on the subscription: every agent task new and task build start runs on the Claude subscription Claude Code is signed in to."
91
+ if [ -n "$SAVED_ANTHROPIC_KEY" ]; then
92
+ say "An Anthropic API key is saved. Run the agents on it with: ocnus billing api"
93
+ else
94
+ say "To run them on an Anthropic API key, save one with: ocnus billing key, then run: ocnus billing api"
95
+ fi
96
+ fi
97
+ }
98
+
99
+ # Write "billing": "subscription" or "api" into the settings file, leaving
100
+ # the rest of it as it is. With no file yet, the new one starts from the
101
+ # defaults, so that it names every profile.
102
+ billing_set() {
103
+ local want=$1 tmp ok=0
104
+ need_jq
105
+ settings_check
106
+ mkdir -p "${SETTINGS%/*}"
107
+ tmp=$SETTINGS.ocnus.$$
108
+ if { if [ -e "$SETTINGS" ]; then cat "$SETTINGS"; else settings_default; fi; } |
109
+ jq --arg v "$want" '.billing = $v' > "$tmp" 2>/dev/null &&
110
+ [ "$(SETTINGS=$tmp; settings_load && printf '%s' "$SET_BILLING")" = "$want" ] &&
111
+ mv -f "$tmp" "$SETTINGS"; then
112
+ ok=1
113
+ fi
114
+ rm -f "$tmp"
115
+ [ "$ok" = 1 ] || die "could not change $SETTINGS, so it is as it was."
116
+ settings_check
117
+ if [ "$want" = api ]; then
118
+ say "Billing is on the API now. The next agent task new or task build starts runs on the saved Anthropic API key; an agent already running keeps the account it started on."
119
+ [ -n "$SAVED_ANTHROPIC_KEY" ] ||
120
+ say " note no Anthropic API key is saved, so task new and task build stop until you save one with: ocnus billing key"
121
+ else
122
+ say "Billing is on the subscription now. The next agent task new or task build starts runs on your Claude subscription; an agent already running keeps the account it started on."
123
+ fi
124
+ }
125
+
126
+ # Save the human's Anthropic API key as ANTHROPIC_API_KEY in ocnus's env
127
+ # file. The key is asked for or read from stdin, never taken as an argument,
128
+ # so that it stays out of the shell's history, and it is not checked with
129
+ # Anthropic.
130
+ billing_key() {
131
+ local key
132
+ # Before the key is asked for, so nobody pastes one for nothing.
133
+ env_file_writable ANTHROPIC_API_KEY
134
+ read_secret 'Anthropic API key (it is not shown as you type or paste it): '
135
+ key=$SECRET
136
+ [ -n "$key" ] || die "no key was given. Make one in $ANTHROPIC_KEY_PLACE, then run this again."
137
+ [[ $key =~ $ANTHROPIC_KEY_RE ]] ||
138
+ die "that is not an Anthropic API key. A key is sk-ant- and then letters, digits, - and _. Make one in $ANTHROPIC_KEY_PLACE."
139
+ env_file_put ANTHROPIC_API_KEY "$key" || die "could not write $ENV_FILE, so the key was not saved."
140
+ SAVED_ANTHROPIC_KEY=$key
141
+ say "Saved the Anthropic API key in $ENV_FILE"
142
+ # Which account the agents run on now, when the settings can be read.
143
+ if settings_load; then
144
+ if [ "$SET_BILLING" = api ]; then
145
+ say "Billing is on the API, so the next agent task new or task build starts runs on it."
146
+ else
147
+ say "Billing is on the subscription, so no agent runs on it until you run: ocnus billing api"
148
+ fi
149
+ fi
150
+ }
151
+
152
+ # Print the saved key, for claude's apiKeyHelper, or fail, printing nothing
153
+ # on stdout, when none is saved. It prints it whatever billing is set to.
154
+ billing_key_print() {
155
+ [ -n "$SAVED_ANTHROPIC_KEY" ] ||
156
+ die "no Anthropic API key is saved in $ENV_FILE. Save one with: ocnus billing key"
157
+ printf '%s\n' "$SAVED_ANTHROPIC_KEY"
158
+ }
159
+
160
+ # The command claude's apiKeyHelper runs: this copy of ocnus, told where
161
+ # ocnus's folder is, so that it reads the same env file whatever the
162
+ # agent's environment says, printing the saved key. Each word is quoted for
163
+ # the shell claude runs it in.
164
+ billing_helper() {
165
+ printf 'OCNUS_HOME=%s %s billing key --print' "$(sh_quote "$ENT_HOME")" "$(sh_quote "$REPO/cli/ocnus")"
166
+ }