@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,847 @@
1
+ # Tasks: a card an agent writes up, then a worktree of each repo on one
2
+ # branch. Sourced by cli/ocnus.
3
+
4
+ # A task is one piece of work in a space, usually a Linear card. Two
5
+ # commands make it, in this order:
6
+ # task new an agent writes the card up, in a tab in the space
7
+ # task start a worktree of each repo on the card's branch, in a tab of its
8
+ # own, which runs `task build` to start the agent that builds
9
+ # the card. The write-up agent runs it once the human approves
10
+ # the card, unless they hold it; then they run it.
11
+ # The agent runs `task handoff` as each step ends, and `task log` shows what
12
+ # it recorded. Both are in handoff.sh. While the pull requests wait on the
13
+ # human, `task merges`, ocnus's watch on the merges, watches them on
14
+ # GitHub; it is in merges.sh. Once the work has merged, it records the merge
15
+ # and runs `task finish`, which removes the task; that is in finish.sh.
16
+
17
+ # A card ID is a team key and a number, like ABC-122.
18
+ CARD_RE='^[A-Za-z][A-Za-z0-9]*-[0-9]+$'
19
+ # The skills the agent writes a card up with, in the order it uses them.
20
+ CARD_SKILLS='sdlc-define sdlc-plan sdlc-breakdown'
21
+ # The agent's first message. {card} and {space} are filled in.
22
+ CARD_PROMPT=$REPO/prompts/task-new.md
23
+ # The skills the agent builds a card with, in the order it uses them: the
24
+ # build, then the review, which sends what it finds not met back to the build,
25
+ # then the pull request, which waits for the human to merge it.
26
+ BUILD_SKILLS='sdlc-build sdlc-review sdlc-pr'
27
+ # The build agent's first message. {card} and {space} are filled in.
28
+ BUILD_PROMPT=$REPO/prompts/task-build.md
29
+ # The harness a task's agent runs in: one word, a command on PATH. claude
30
+ # unless OCNUS_HARNESS names another.
31
+ HARNESS=${OCNUS_HARNESS:-claude}
32
+ # The profiles claude starts with: Claude Code agent definitions in agents/,
33
+ # linked into Claude Code's agents folder. Each gives the session only the
34
+ # tools its steps use. The write-up's is for `task new`, the build's for
35
+ # `task build`, whose one session also reviews the build. Each runs on the
36
+ # model and effort the settings file names for it, under the name after it
37
+ # (settings.sh).
38
+ CARD_PROFILE=ocnus-write-up CARD_SETTINGS=write-up
39
+ BUILD_PROFILE=ocnus-build BUILD_SETTINGS=build
40
+ # The MCP servers a profile's session has, and no others: Linear's own, under
41
+ # the name the profiles' tools are given with.
42
+ PROFILE_MCP=$REPO/agents/linear-mcp.json
43
+
44
+ # The flags that start claude with a profile, in PROFILE_ARGS, or stop when
45
+ # the profile is not linked or the settings file is not valid settings.
46
+ # Another harness is started as it is, with none, and the settings file is
47
+ # not read. Each flag holds its value after an =, because --mcp-config takes
48
+ # several values and would take the first message as one of them. --model=
49
+ # and --effort= beat the profile's own model and the human's own Claude Code
50
+ # settings; only CLAUDE_CODE_EFFORT_LEVEL beats --effort=. While recording
51
+ # is on, --settings= gives claude the hooks that mark the time of the agent
52
+ # of that card's phase (host_hooks, in host.sh); when they cannot be given,
53
+ # the agent starts without them.
54
+ #
55
+ # It also leaves in AGENT_ENV the words claude is started after, which run it
56
+ # without ANTHROPIC_API_KEY and ANTHROPIC_AUTH_TOKEN, so that the agent runs
57
+ # on the Claude subscription signed in to with /login: in Claude Code's order
58
+ # of sign-ins either variable comes first, and puts claude on the API. They
59
+ # can be exported in the terminal `task build` runs in, or by the human's
60
+ # shell profile, which the login shell around `task new`'s argv reads before
61
+ # it runs the argv. So env drops them as claude starts: unsetting them in
62
+ # ocnus's own shell would not reach `task new`'s agent.
63
+ # CLAUDE_CODE_OAUTH_TOKEN is a subscription token and is left as it is.
64
+ # Another harness gets no words, and starts as it always has.
65
+ #
66
+ # With billing on api (billing.sh), the agent runs on the Anthropic API key
67
+ # the human saved instead: claude also gets --settings= holding an
68
+ # apiKeyHelper, the command it runs to get the key, which comes before the
69
+ # /login sign-in in Claude Code's order. The key itself is in no argument and
70
+ # in no environment, and env's words still drop the exported variables,
71
+ # which would come before the helper. With no key saved it stops, before an
72
+ # agent is started. The profile's model and effort hold either way.
73
+ # $1 the profile $2 its name in the settings file
74
+ # $3 the space $4 the card $5 the phase, write-up or build
75
+ PROFILE_ARGS=() AGENT_ENV=()
76
+ profile_args() {
77
+ local profile=$1 settings=$2 hooks
78
+ PROFILE_ARGS=() AGENT_ENV=()
79
+ [ "${HARNESS##*/}" = claude ] || return 0
80
+ [ -f "$AGENTS_HOME/$profile.md" ] ||
81
+ die "the $profile agent profile is not in $AGENTS_HOME, so the agent could not start with it. Run: ocnus update"
82
+ settings_check
83
+ PROFILE_ARGS=("--agent=$profile" --strict-mcp-config "--mcp-config=$PROFILE_MCP"
84
+ "--model=$(profile_model "$settings")" "--effort=$(profile_effort "$settings")")
85
+ if hooks=$(host_hooks "$3" "$4" "$5") && [ -n "$hooks" ]; then
86
+ PROFILE_ARGS+=("--settings=$hooks")
87
+ fi
88
+ AGENT_ENV=(env -u ANTHROPIC_API_KEY -u ANTHROPIC_AUTH_TOKEN)
89
+ if [ "$SET_BILLING" = api ]; then
90
+ [ -n "$SAVED_ANTHROPIC_KEY" ] || die "$BILLING_NO_KEY"
91
+ PROFILE_ARGS+=("--settings={\"apiKeyHelper\":\"$(json_str "$(billing_helper)")\"}")
92
+ fi
93
+ }
94
+
95
+ # Stop if Linear has no such card, before an agent is started on it: a
96
+ # mistyped ID is caught now, not by the agent after it has looked for the card.
97
+ # Only a definite no stops it. With no key saved, Linear is not asked; when it
98
+ # cannot be asked, the card starts as before, with a note saying why.
99
+ check_card() {
100
+ local card=$1 why rc=0
101
+ why=$(linear_card_known "$card" 2>&1 >/dev/null) || rc=$?
102
+ case $rc in
103
+ 0 | 2) ;;
104
+ 1) die "Linear has no card $card that the saved key can see. Check the ID, then run this again." ;;
105
+ # Only the first sentence of why: the rest of linear_ask's message is
106
+ # advice for a command that stops, such as to run it again, which here
107
+ # would start a second agent on the card.
108
+ *) why=$(first_sentence "$why")
109
+ say " note could not check $card with Linear${why:+: $why}. Starting it anyway." ;;
110
+ esac
111
+ }
112
+
113
+ # The labels ocnus puts on an agent's terminal in herdr-core, whichever way
114
+ # it was opened, so that the terminal can be found by them with the Herdr app
115
+ # open or not: the space, the card, and the name of the card's tab. They go in
116
+ # AGENT_LABELS.
117
+ # agent_labels <space> <card> <tab name>
118
+ AGENT_LABELS=()
119
+ agent_labels() {
120
+ AGENT_LABELS=("ocnus.space=$1" "ocnus.card=$2" "ocnus.tab=$3")
121
+ }
122
+
123
+ # Say a note that still needs the human, and keep it in START_NOTES. `task
124
+ # start` records the notes it kept as its start row's flags (record_start), so
125
+ # that `task log` shows them once the tab that printed them has gone. `task
126
+ # new` says its notes the same way, and records none.
127
+ START_NOTES=()
128
+ start_note() {
129
+ START_NOTES+=("$1")
130
+ say " note $1"
131
+ }
132
+
133
+ # Print the herdr.space label of the terminal $1, or fail when it has none.
134
+ herdr_space_of() {
135
+ local id
136
+ id=$(hc_cmd get "$1" --json 2>/dev/null | jq -r '.labels["herdr.space"] // empty' 2>/dev/null) || return 1
137
+ [ -n "$id" ] || return 1
138
+ printf '%s\n' "$id"
139
+ }
140
+
141
+ # Print the id of the Herdr space that a space's new agent terminal goes in,
142
+ # for its herdr.space label: the one the space's terminals are already in, so
143
+ # that the Herdr app shows them together. It is the herdr.space of a live
144
+ # terminal labelled with the space, else of a live terminal whose folder is in
145
+ # the space's folder, as one the app opened there has, else a new id, of which
146
+ # the app makes a new space. The labels are read with jq; without it the id is
147
+ # new.
148
+ # space_herdr_id <space> <space folder>
149
+ space_herdr_id() {
150
+ local name=$1 dir=$2 t id
151
+ if command -v jq >/dev/null 2>&1; then
152
+ for t in $(hc_cmd ls --json --label "ocnus.space=$name" 2>/dev/null |
153
+ jq -r '.terminals[]? | .address // empty' 2>/dev/null); do
154
+ if id=$(herdr_space_of "$t"); then printf '%s\n' "$id"; return 0; fi
155
+ done
156
+ for t in $(hc_cmd ls --json 2>/dev/null |
157
+ jq -r --arg dir "$dir" '.terminals[]? | select((.cwd // "") as $c | $c == $dir or ($c | startswith($dir + "/")))
158
+ | .address // empty' 2>/dev/null); do
159
+ if id=$(herdr_space_of "$t"); then printf '%s\n' "$id"; return 0; fi
160
+ done
161
+ fi
162
+ new_herdr_id sp
163
+ }
164
+
165
+ # Print the first string value of the key $1 in the JSON $2, read without jq:
166
+ # tab.new's "terminal": "t8", or hc run's "address":"t9".
167
+ json_value() {
168
+ printf '%s\n' "$2" | grep -o "\"$1\": *\"[^\"]*\"" | head -n 1 | sed 's/^[^:]*: *"//; s/"$//'
169
+ }
170
+
171
+ # Open an agent's tab: a terminal in herdr-core that runs the agent, and that
172
+ # the Herdr app shows as a tab in the space. It is the one way an agent's tab
173
+ # is opened. It leaves the terminal's address in AGENT_TERMINAL, or nothing
174
+ # when it is not known, and in AGENT_IN_APP whether the app opened it. When no
175
+ # tab could be opened it fails, with why in AGENT_ERROR, so that the caller can
176
+ # say what it had made by then.
177
+ # open_agent_tab <space> <space folder> <folder> <tab name> <card> <first message> <argv...>
178
+ # The argv is what the tab runs, and the first message goes after it: the
179
+ # harness and its flags, then the agent's first message, for `task new`; a
180
+ # command that takes none, given '' as its first message, for `task start`.
181
+ #
182
+ # While the app answers, the app opens the tab, as it always has: show_space
183
+ # selects or makes the space, tab.new opens the tab there, and hc puts
184
+ # ocnus's labels on the terminal tab.new names. While it does not,
185
+ # herdr-core starts the terminal itself, with ocnus's labels and Herdr's
186
+ # own two, herdr.space and herdr.tab, which make the app show it as a tab of
187
+ # that space once it is opened (see Herdr's AGENTS.md). Its command is the
188
+ # wrapper Herdr's tab.new puts around an argv, as `hc get` shows for a tab the
189
+ # app opened: the login shell runs the argv, then takes its place, so that the
190
+ # tab is left with a shell when the agent ends. --no-parent keeps the agent
191
+ # running when the terminal that started it ends, since hc otherwise ends a
192
+ # terminal with the one it was started from.
193
+ AGENT_TERMINAL='' AGENT_IN_APP=0 AGENT_ERROR=''
194
+ open_agent_tab() {
195
+ local name=$1 dir=$2 folder=$3 tab=$4 card=$5 message=$6 out arg argv='' task='' shell space_id tab_id label
196
+ local run=()
197
+ shift 6
198
+ AGENT_TERMINAL='' AGENT_IN_APP=0 AGENT_ERROR=''
199
+ agent_labels "$name" "$card" "$tab"
200
+ if herdr_running; then
201
+ if ! (show_space "$name" "$dir"); then
202
+ AGENT_ERROR="could not show the space for '$name', so no agent was started."
203
+ return 1
204
+ fi
205
+ # No `space` argument: the tab goes in the selected space, which
206
+ # show_space has just selected or made. The first message is tab.new's
207
+ # task, which Herdr puts at the end of the argv; with none there is no task.
208
+ for arg in "$@"; do argv=${argv:+$argv,}"\"$(json_str "$arg")\""; done
209
+ [ -z "$message" ] || task=",\"task\":\"$(json_str "$message")\""
210
+ if ! out=$(herdr_app do tab.new "{\"name\":\"$(json_str "$tab")\",\"folder\":\"$(json_str "$folder")\",\"argv\":[$argv]$task}" 2>/dev/null); then
211
+ AGENT_ERROR="could not open a tab for $card. Is Herdr open?"
212
+ return 1
213
+ fi
214
+ AGENT_IN_APP=1
215
+ AGENT_TERMINAL=$(json_value terminal "$out")
216
+ # The agent is running in its tab by now, so a terminal that cannot be
217
+ # labelled is a note, never a failure.
218
+ [ -n "$AGENT_TERMINAL" ] && hc_cmd label "$AGENT_TERMINAL" "${AGENT_LABELS[@]}" >/dev/null 2>&1 ||
219
+ start_note "could not label the tab's terminal in herdr-core; the agent is running in the tab"
220
+ return 0
221
+ fi
222
+ # Each of these says why on its own when it fails.
223
+ AGENT_ERROR="could not start the agent for $card in herdr-core"
224
+ space_id=$(space_herdr_id "$name" "$dir") || return 1
225
+ tab_id=$(new_herdr_id tb) || return 1
226
+ shell=${SHELL:-/bin/sh}
227
+ run=(run --bg --json --keep --no-parent --cwd "$folder")
228
+ for label in "${AGENT_LABELS[@]}" "herdr.space=$space_id" "herdr.tab=$tab_id"; do run+=(--label "$label"); done
229
+ [ -z "$message" ] || set -- "$@" "$message"
230
+ # shellcheck disable=SC2016
231
+ if ! out=$(hc_cmd "${run[@]}" -- "$shell" -lic '"$@"; exec "$0" -li' "$shell" "$@" 2>&1); then
232
+ AGENT_ERROR="$AGENT_ERROR${out:+: $out}"
233
+ return 1
234
+ fi
235
+ AGENT_ERROR=''
236
+ AGENT_TERMINAL=$(json_value address "$out")
237
+ }
238
+
239
+ # Start writing a card up: a tab in the space, in the space's folder, running
240
+ # an agent whose first message names the card and the skills. The tab is a
241
+ # terminal in herdr-core, opened by the Herdr app while it is open, and by
242
+ # herdr-core itself while it is not (open_agent_tab).
243
+ # It makes no branch, worktree or task folder; `task start` does that once the
244
+ # card has a type.
245
+ task_new() {
246
+ local name=${1:-} card=${2:-} dir skill prompt
247
+ dir=$(space_dir "$name")
248
+ [ -n "$card" ] ||
249
+ die "which card? Usage: ocnus task new $name <card>, like: ocnus task new $name ABC-122"
250
+ [[ $card =~ $CARD_RE ]] ||
251
+ die "'$card' is not a card ID like ABC-122. Give the card's ID: once it is written up, ocnus task start $name <card> names its branch."
252
+ card=$(printf '%s' "$card" | tr '[:lower:]' '[:upper:]')
253
+ # The agent runs in a herdr-core terminal, with the Herdr app open or not,
254
+ # so herdr-core has to be running before anything is opened.
255
+ core_ready
256
+ command -v "$HARNESS" >/dev/null 2>&1 ||
257
+ die "'$HARNESS' is not on your PATH, so there is no harness to start the agent in. Install it, or set OCNUS_HARNESS to the one you use."
258
+ for skill in $CARD_SKILLS; do
259
+ [ -f "$SKILLS_HOME/$skill/SKILL.md" ] ||
260
+ die "the $skill skill is not in $SKILLS_HOME, so the agent could not use it. Run: ocnus update"
261
+ done
262
+ profile_args "$CARD_PROFILE" "$CARD_SETTINGS" "$name" "$card" write-up
263
+ [ -f "$CARD_PROMPT" ] || die "the agent's first message is missing from $CARD_PROMPT"
264
+ prompt=$(sed -e "s/{card}/$card/g" -e "s/{space}/$name/g" "$CARD_PROMPT")
265
+ check_card "$card"
266
+
267
+ sync_links "$dir"
268
+ # `space new` does this too. Here it covers a space made before it did,
269
+ # or on a day Claude Code's file could not be written. It comes before the
270
+ # tab, because the agent meets the trust question the moment it starts.
271
+ trust_space "$name"
272
+ # The tab is named after the card, in the space's folder. The argv is env's
273
+ # words, the harness and its profile's flags, then the agent's first message.
274
+ open_agent_tab "$name" "$dir" "$dir" "$card" "$card" "$prompt" ${AGENT_ENV[@]+"${AGENT_ENV[@]}"} \
275
+ "$HARNESS" ${PROFILE_ARGS[@]+"${PROFILE_ARGS[@]}"} || die "$AGENT_ERROR"
276
+ record_start "$name" "$card"
277
+ # An agent is open now, so recording, when it is on, starts (host.sh), and
278
+ # ocnus's watch on the merges, while a card waits on one (merges.sh).
279
+ host_wake
280
+ merges_wake
281
+ if [ "$AGENT_IN_APP" = 1 ]; then
282
+ say "Started $card in the space for '$name'"
283
+ else
284
+ say "Started $card in herdr-core${AGENT_TERMINAL:+, in terminal $AGENT_TERMINAL}, since the Herdr app is not open. Opening Herdr shows it as a tab."
285
+ [ -z "$AGENT_TERMINAL" ] || say " attach to see it here: hc attach $AGENT_TERMINAL"
286
+ fi
287
+ say "The agent writes the card up, then asks you to approve it."
288
+ }
289
+
290
+ # `task start` makes a folder in a space's tasks/ folder, named after its
291
+ # branch's description, holding one git worktree per repo of the space:
292
+ # tasks/abc-122-short-title/api a worktree of api on feat/abc-122-short-title
293
+
294
+ # Branch names follow Conventional Branch 1.1.0 (https://conventionalbranch.org):
295
+ # <type>/<description>. Trunk names (main, master, develop) have no type, so
296
+ # they do not match.
297
+ BRANCH_RE='^(feature|feat|bugfix|fix|hotfix|release|chore|ai|claude|codex|copilot|cursor)/[a-z0-9]+(\.[a-z0-9]+)*(-[a-z0-9]+(\.[a-z0-9]+)*)*$'
298
+
299
+ valid_branch() { [[ ${1:-} =~ $BRANCH_RE ]]; }
300
+
301
+ # A task's name is its branch without the type.
302
+ task_name() { printf '%s\n' "${1#*/}"; }
303
+
304
+ # The first four words of a card's title, on stdin, in lowercase and joined
305
+ # by hyphens: "Export orders as CSV" is export-orders-as-csv. a, an and the
306
+ # are left out, and an apostrophe joins its word ("Ada's" is adas). Anything
307
+ # else that is not a plain letter or digit ends a word, because a branch name
308
+ # cannot hold it. Every letter is spelled out, as in valid_group, and the
309
+ # bytes are read in the C locale, so no locale changes what a letter is.
310
+ short_name() {
311
+ local word short='' n=0
312
+ for word in $(LC_ALL=C sed -e "s/'//g" -e "s/’//g" |
313
+ LC_ALL=C tr -c 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' ' ' |
314
+ LC_ALL=C tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'); do
315
+ case $word in a | an | the) continue ;; esac
316
+ short=${short:+$short-}$word
317
+ n=$((n + 1))
318
+ [ "$n" -lt 4 ] || break
319
+ done
320
+ printf '%s' "$short"
321
+ }
322
+
323
+ # Name the branch a card's task is on, <type>/<card ID>-<short name>: like
324
+ # feat/abc-122-export-orders-as-csv. It leaves the branch in BRANCH, in
325
+ # BRANCH_NOTES what `task start` says about where the type and title came
326
+ # from, and in CARD_PARENT the card it was sliced from, as Linear names it,
327
+ # or nothing when it has none or Linear was not asked. It runs in the main
328
+ # shell, not in $( ), so that it can leave them, and every stop is before
329
+ # anything is made.
330
+ # card_branch <space> <card ID, in upper case> [<type> <title>]
331
+ #
332
+ # Without a type and title, it asks Linear, and stops when Linear cannot be
333
+ # asked, has no such card, or the card has no type. With them, Linear still
334
+ # wins when it can be asked: Linear's type and title name the branch, by the
335
+ # same checks, with a note for each given one that differs. With no key
336
+ # saved, or a Linear that cannot be asked (a rejected key, no network, no
337
+ # curl or jq), the given ones name it, with a note on why.
338
+ BRANCH='' BRANCH_NOTES=() CARD_PARENT=''
339
+ card_branch() {
340
+ local name=$1 card=$2 given_type=${3:-} given_title=${4:-} found out rc=0 type title short branch parent
341
+ BRANCH='' BRANCH_NOTES=() CARD_PARENT=''
342
+ if [ -z "$given_type" ]; then
343
+ [ -n "${LINEAR_API_KEY:-}" ] ||
344
+ die "no Linear key is saved, so ocnus cannot read $card's type and title in Linear. Save a key with ocnus linear key (make one in $KEY_PLACE), or give the type and title: ocnus task start $name $card --type <type> --title '<title>'"
345
+ found=$(linear_card "$card") || exit 1
346
+ else
347
+ # Only 0 prints anything on stdout, and only 3 on stderr, so the two
348
+ # together are the card on 0 and why on 3.
349
+ out=$(linear_card_known "$card" 2>&1) || rc=$?
350
+ case $rc in
351
+ 0) found=${out##*$'\n'} ;;
352
+ 1) die "Linear has no card $card that the saved key can see. Check the ID, then run this again." ;;
353
+ 2) found=''
354
+ BRANCH_NOTES+=("the branch is named from the type and title given, since no Linear key is saved") ;;
355
+ *) found=''
356
+ # Only the first sentence: the rest is advice for a command that
357
+ # stops, and this one goes on.
358
+ out=$(first_sentence "$out")
359
+ BRANCH_NOTES+=("the branch is named from the type and title given, since Linear could not be asked${out:+: $out}") ;;
360
+ esac
361
+ fi
362
+ if [ -n "$found" ]; then
363
+ type=$(printf '%s' "$found" | jq -r '.type // empty')
364
+ [ -n "$type" ] ||
365
+ die "$card has no type in Linear, so its branch cannot be named. Write the card up first: ocnus task new $name $card"
366
+ title=$(printf '%s' "$found" | jq -r '.title')
367
+ # Only a card ID: it is looked for among the space's tabs.
368
+ parent=$(printf '%s' "$found" | jq -r '.parent // empty')
369
+ [[ ! $parent =~ $CARD_RE ]] || CARD_PARENT=$(printf '%s' "$parent" | tr '[:lower:]' '[:upper:]')
370
+ # The notes are said only once the branch is made, so a type of
371
+ # Linear's that no branch can hold stops below before any is said.
372
+ if [ -n "$given_type" ] && [ "$given_type" != "$type" ]; then
373
+ BRANCH_NOTES+=("the type given is $given_type, but $card's in Linear is $type: the branch is named from Linear's")
374
+ fi
375
+ if [ -n "$given_type" ] && [ "$given_title" != "$title" ]; then
376
+ # Linear's title is anyone's text, so it is made plain, as task ls makes it.
377
+ BRANCH_NOTES+=("the title given is not $card's in Linear, which is '$(printf '%s' "$found" | jq -r "$PLAIN_DEF"' .title | plain')': the branch is named from Linear's")
378
+ fi
379
+ else
380
+ type=$given_type title=$given_title
381
+ fi
382
+ short=$(printf '%s' "$title" | short_name)
383
+ branch=$type/$(printf '%s' "$card" | tr '[:upper:]' '[:lower:]')${short:+-$short}
384
+ valid_branch "$branch" ||
385
+ die "$card's type in Linear is '$type', which is not a branch type, so its branch cannot be named. Set its type to feat, fix or chore (see https://conventionalbranch.org)."
386
+ BRANCH=$branch
387
+ }
388
+
389
+ has_origin() { git -C "$1" remote get-url origin >/dev/null 2>&1; }
390
+ resolves() { git -C "$1" rev-parse -q --verify "$2^{commit}" >/dev/null 2>&1; }
391
+
392
+ # Print the ref a repo's new task branch is cut from: the remote's default
393
+ # branch, or the local one in a repo with no origin.
394
+ base_ref() {
395
+ local repo=$1 ref
396
+ if has_origin "$repo"; then
397
+ ref=$(git -C "$repo" symbolic-ref -q --short refs/remotes/origin/HEAD 2>/dev/null) || ref=
398
+ for ref in ${ref:+"$ref"} origin/main origin/master; do
399
+ if resolves "$repo" "$ref"; then printf '%s\n' "$ref"; return; fi
400
+ done
401
+ fi
402
+ for ref in main master; do
403
+ if resolves "$repo" "refs/heads/$ref"; then printf '%s\n' "$ref"; return; fi
404
+ done
405
+ git -C "$repo" rev-parse --abbrev-ref HEAD
406
+ }
407
+
408
+ # What the current `task start` is in the middle of making, so that it can be
409
+ # removed if the run fails or is interrupted. Empty when nothing is half-made.
410
+ UNDO_DIR='' UNDO_BRANCH=''
411
+ MADE=() # the repos a worktree has been started in
412
+
413
+ # Remove a half-made task. The task folder and the branch did not exist before
414
+ # the run (task_start checks both), so this never removes someone's work.
415
+ task_undo() {
416
+ local tdir=$UNDO_DIR branch=$UNDO_BRANCH repo
417
+ [ -n "$tdir" ] || return 0
418
+ UNDO_DIR=''
419
+ for repo in ${MADE[@]+"${MADE[@]}"}; do
420
+ git -C "$repo" worktree remove --force "$tdir/${repo##*/}" >/dev/null 2>&1 || true
421
+ git -C "$repo" branch -q -D "$branch" >/dev/null 2>&1 || true
422
+ done
423
+ rm -f "$tdir/CLAUDE.md" "$tdir/AGENTS.md"
424
+ rmdir "$tdir" 2>/dev/null || true
425
+ rmdir "${tdir%/*}" 2>/dev/null || true
426
+ [ ! -e "$tdir" ] || printf 'ocnus: could not remove everything in %s\n' "$tdir" >&2
427
+ }
428
+
429
+ # The commands that get a new task ready to work in, for the line its tab
430
+ # runs: `just setup` for every repo that provides the recipe (see
431
+ # recipes.sh). They go in SETUP_LINE, empty when there is nothing to run, and
432
+ # the repos they run for in SETUP_READY. Nothing in this step can fail the
433
+ # task: by now it exists.
434
+ #
435
+ # Setup is off unless OCNUS_SETUP is 1, while the local environment each
436
+ # task gets is still being worked out: a recipe that starts a whole stack
437
+ # should not run for every task in the meantime.
438
+ # $1 the task's folder $2... the task's repos
439
+ SETUP_LINE='' SETUP_READY=()
440
+ setup_line() {
441
+ local tdir=$1 repo name count groups i=0
442
+ shift
443
+ count=$#
444
+ SETUP_LINE='' SETUP_READY=()
445
+ if [ "${OCNUS_SETUP:-}" != 1 ]; then
446
+ say " setup not run: it is off for now (OCNUS_SETUP=1 turns it on)"
447
+ return 0
448
+ fi
449
+ if ! have_just; then
450
+ say " setup skipped: just is not installed (https://just.systems)"
451
+ return 0
452
+ fi
453
+ for repo in "$@"; do
454
+ name=${repo##*/}
455
+ # This repo's groups, if its target named any: handed to its recipe as is.
456
+ groups=${TASK_GROUPS[$i]:-}; i=$((i + 1))
457
+ case $(recipe_state "$tdir/$name" setup) in
458
+ ok)
459
+ SETUP_READY+=("$name${groups:+ $groups}")
460
+ # One repo: the tab is already in its worktree. Several: the tab is in
461
+ # the task folder above them, and they run one after another. The ./
462
+ # keeps the shell's CDPATH, and a name starting with a dash, from
463
+ # sending cd somewhere else. The subshells leave the tab in the task
464
+ # folder for what comes after them.
465
+ if [ "$count" -eq 1 ]; then SETUP_LINE="just setup${groups:+ $groups}"
466
+ else SETUP_LINE=${SETUP_LINE:+$SETUP_LINE && }"(cd ./$(sh_quote "$name") && just setup${groups:+ $groups})"
467
+ fi
468
+ ;;
469
+ no-justfile) say " setup $name skipped: no justfile" ;;
470
+ no-recipe) say " setup $name skipped: its justfile has no setup recipe" ;;
471
+ *) say " setup $name skipped: its justfile could not be read by just" ;;
472
+ esac
473
+ done
474
+ }
475
+
476
+ # Say when Claude Code will ask whether to trust a task's tab. A worktree takes
477
+ # the trust of its repo's own checkout, never of a folder above it, and
478
+ # ocnus never answers for a repo (see trust.sh), so this only reads.
479
+ # $1 the repo whose worktree the tab is in
480
+ repo_trust_note() {
481
+ local repo name=${1##*/}
482
+ repo=$(cd -P "$1" 2>/dev/null && pwd) || return 0
483
+ if [ -e "$CLAUDE_STATE" ] && command -v jq >/dev/null 2>&1; then
484
+ ! trust_recorded "$repo" "$CLAUDE_STATE" || return 0
485
+ start_note "Claude Code will ask once in the tab whether to trust $name. It asks once for each repo."
486
+ else
487
+ start_note "Claude Code may ask once in the tab whether to trust $name."
488
+ fi
489
+ }
490
+
491
+ # A group is a part of a repo, named by the repo itself: a plain word, because
492
+ # it goes into the line that /bin/sh runs in the tab. Lowercase letters,
493
+ # digits and dashes, not starting with a dash. Every allowed character is
494
+ # spelled out: a range like a-z means different things in different locales,
495
+ # and in some it lets uppercase letters and shell punctuation through.
496
+ valid_group() {
497
+ case ${1:-} in
498
+ '' | -* | *[!abcdefghijklmnopqrstuvwxyz0123456789-]*) return 1 ;;
499
+ esac
500
+ }
501
+
502
+ # What `task start` was asked to work on, from its targets:
503
+ # TARGET_REPOS the repo each target names
504
+ # TARGET_GROUPS that target's groups, separated by spaces ('' for the whole repo)
505
+ # and, once the task's repos are chosen, in the same order as them:
506
+ # TASK_GROUPS each repo's groups
507
+ TARGET_REPOS=() TARGET_GROUPS=() TASK_GROUPS=()
508
+
509
+ # Read targets of the form <repo>[:<group>[,<group>...]] into TARGET_REPOS and
510
+ # TARGET_GROUPS, or stop. ocnus checks a group's shape only: what a group
511
+ # means, and whether it exists, is the repo's business.
512
+ # $1 the space $2... the space's repos, then --, then the targets
513
+ parse_targets() {
514
+ local name=$1 known=() arg repo groups group found r i
515
+ shift
516
+ while [ "$#" -gt 0 ] && [ "$1" != -- ]; do known+=("${1##*/}"); shift; done
517
+ shift
518
+ TARGET_REPOS=() TARGET_GROUPS=()
519
+ for arg in "$@"; do
520
+ repo=${arg%%:*}
521
+ found=0
522
+ for r in "${known[@]}"; do [ "$r" != "$repo" ] || found=1; done
523
+ [ "$found" = 1 ] ||
524
+ die "'$repo' is not a repo in space '$name'. Its repos are: ${known[*]}"
525
+ i=0
526
+ while [ "$i" -lt "${#TARGET_REPOS[@]}" ]; do
527
+ [ "${TARGET_REPOS[$i]}" != "$repo" ] ||
528
+ die "'$repo' is named twice. Give its groups together, separated by a comma, like: $repo:athlete,coach"
529
+ i=$((i + 1))
530
+ done
531
+ groups=''
532
+ case $arg in
533
+ *:*)
534
+ # Split on commas by hand: an empty group (a trailing or doubled
535
+ # comma) has to be caught, not skipped.
536
+ r=${arg#*:},
537
+ while [ -n "$r" ]; do
538
+ group=${r%%,*}; r=${r#*,}
539
+ valid_group "$group" ||
540
+ die "'$group' in '$arg' is not a group name. A group is lowercase letters, digits and dashes, like: $repo:athlete"
541
+ groups=${groups:+$groups }$group
542
+ done
543
+ ;;
544
+ esac
545
+ TARGET_REPOS+=("$repo"); TARGET_GROUPS+=("$groups")
546
+ done
547
+ }
548
+
549
+ # What `task start` takes after the card.
550
+ START_ARGS='[--type <type> --title <title>] [<repo>[:<group>[,<group>...]] ...]'
551
+
552
+ task_start() {
553
+ local name=${1:-} card=${2:-} dir branch task usage given_type='' given_title='' typed=0 titled=0 note
554
+ local targets=()
555
+ dir=$(space_dir "$name")
556
+ usage="ocnus task start $name <card> $START_ARGS"
557
+ [ -n "$card" ] ||
558
+ die "which card? Usage: $usage, like: ocnus task start $name ABC-122"
559
+ case $card in
560
+ */*) die "task start takes the card now, not a branch, and names the branch from the card's type and title in Linear. Give the card's ID, like: ocnus task start $name ABC-122" ;;
561
+ esac
562
+ [[ $card =~ $CARD_RE ]] ||
563
+ die "'$card' is not a card ID like ABC-122. Usage: $usage"
564
+ card=$(printf '%s' "$card" | tr '[:lower:]' '[:upper:]')
565
+ # The card's type and title, for when Linear cannot be asked for them
566
+ # (card_branch), come from anywhere after the card, both or neither. The
567
+ # rest are targets. All of it is checked before Herdr or Linear is asked.
568
+ shift 2
569
+ while [ "$#" -gt 0 ]; do
570
+ case $1 in
571
+ --type | --title)
572
+ case ${2-} in '' | --type | --title) die "$1 needs a value. Usage: $usage" ;; esac
573
+ if [ "$1" = --type ]; then
574
+ [ "$typed" = 0 ] || die "--type is given twice. Usage: $usage"
575
+ given_type=$2 typed=1
576
+ else
577
+ [ "$titled" = 0 ] || die "--title is given twice. Usage: $usage"
578
+ given_title=$2 titled=1
579
+ fi
580
+ shift 2
581
+ ;;
582
+ --*) die "unknown option for task start: $1. Usage: $usage" ;;
583
+ *) targets+=("$1"); shift ;;
584
+ esac
585
+ done
586
+ [ "$typed" = "$titled" ] ||
587
+ die "give --type and --title together, or neither to have Linear name the branch. Usage: $usage"
588
+ # The check Linear's type gets, on the card's own branch.
589
+ [ "$typed" = 0 ] || valid_branch "$given_type/$(printf '%s' "$card" | tr '[:upper:]' '[:lower:]')" ||
590
+ die "'$given_type' is not a branch type, so the branch cannot be named. Give --type feat, fix or chore (see https://conventionalbranch.org)."
591
+ # The task's agent runs in a herdr-core terminal, with the Herdr app open
592
+ # or not, so herdr-core has to be running before anything is made.
593
+ core_ready
594
+
595
+ # Check everything before making anything.
596
+ local all=() repos=() bases=() repo base i tdir
597
+ while IFS= read -r repo; do all+=("$repo"); done < <(space_repos "$dir")
598
+ [ "${#all[@]}" -gt 0 ] ||
599
+ die "space '$name' has no repos. Add one with: ocnus space add $name <path>"
600
+ # The task's repos are the ones its targets name, or every repo when there
601
+ # are no targets, always in the order of the space's repos file.
602
+ parse_targets "$name" "${all[@]}" -- ${targets[@]+"${targets[@]}"}
603
+ TASK_GROUPS=()
604
+ for repo in "${all[@]}"; do
605
+ if [ "${#TARGET_REPOS[@]}" -eq 0 ]; then
606
+ repos+=("$repo"); TASK_GROUPS+=('')
607
+ continue
608
+ fi
609
+ i=0
610
+ while [ "$i" -lt "${#TARGET_REPOS[@]}" ]; do
611
+ if [ "${TARGET_REPOS[$i]}" = "${repo##*/}" ]; then
612
+ repos+=("$repo"); TASK_GROUPS+=("${TARGET_GROUPS[$i]}")
613
+ fi
614
+ i=$((i + 1))
615
+ done
616
+ done
617
+ for repo in "${repos[@]}"; do
618
+ is_repo "$repo" || die "$repo is missing or is not a git repo (see: ocnus status)"
619
+ done
620
+ # Linear names the branch, or the type and title given when Linear cannot,
621
+ # once everything here has been checked.
622
+ card_branch "$name" "$card" "$given_type" "$given_title"
623
+ branch=$BRANCH
624
+ task=$(task_name "$branch")
625
+ tdir=$dir/tasks/$task
626
+ [ ! -e "$tdir" ] || die "task '$task' already exists at $tdir"
627
+ # Fetch first, so that the branch check below sees what is on the remote
628
+ # and the task starts from the latest default branch.
629
+ for repo in "${repos[@]}"; do
630
+ has_origin "$repo" || continue
631
+ git -C "$repo" fetch -q origin 2>/dev/null ||
632
+ say " warning: could not fetch ${repo##*/}; starting from what is local"
633
+ done
634
+ for repo in "${repos[@]}"; do
635
+ if resolves "$repo" "refs/heads/$branch" || resolves "$repo" "refs/remotes/origin/$branch"; then
636
+ die "branch '$branch' already exists in ${repo##*/}"
637
+ fi
638
+ base=$(base_ref "$repo" 2>/dev/null) && resolves "$repo" "$base" ||
639
+ die "${repo##*/} has no commit to start a branch from"
640
+ bases+=("$base")
641
+ done
642
+ sync_links "$dir"
643
+
644
+ say "Making task '$task' for $card on branch $branch, in $tdir"
645
+ START_NOTES=()
646
+ for note in ${BRANCH_NOTES[@]+"${BRANCH_NOTES[@]}"}; do start_note "$note"; done
647
+ # From here until the worktrees and links are all made, any failure or
648
+ # interruption removes what was made.
649
+ MADE=() UNDO_DIR=$tdir UNDO_BRANCH=$branch
650
+ trap task_undo EXIT
651
+ trap 'exit 130' INT
652
+ trap 'exit 143' TERM
653
+ mkdir -p "$tdir"
654
+ i=0
655
+ for repo in "${repos[@]}"; do
656
+ base=${bases[$i]}; i=$((i + 1))
657
+ # Recorded before the attempt: a failed `worktree add` can leave its branch.
658
+ MADE+=("$repo")
659
+ # --no-track: the branch is new work, not a copy of the default branch, so
660
+ # a plain push or pull must not go to or from it.
661
+ # -f: reuse a path git still has registered for a folder deleted by hand.
662
+ git -C "$repo" worktree add -q -f --no-track -b "$branch" "$tdir/${repo##*/}" "$base" ||
663
+ die "could not make a worktree in ${repo##*/}; the task was not made"
664
+ say " worktree ${repo##*/} (from $base)"
665
+ done
666
+ # With several repos the session starts in the task folder, so the space's
667
+ # doc is linked there for an agent to read.
668
+ if [ "${#repos[@]}" -gt 1 ]; then
669
+ ln -s "../../$SPACE_DOC" "$tdir/CLAUDE.md"
670
+ ln -s "../../$SPACE_DOC" "$tdir/AGENTS.md"
671
+ fi
672
+ UNDO_DIR=''
673
+ trap - EXIT INT TERM
674
+
675
+ # One repo: the tab starts in its worktree, where git and the repo's own
676
+ # docs work as usual. Several: it starts in the task folder above them.
677
+ local folder=$tdir build line ready
678
+ [ "${#repos[@]}" -gt 1 ] || folder=$tdir/${repos[0]##*/}
679
+ # The tab's command is one line: the setup commands, if there are any, then
680
+ # `ocnus task build`, which starts the agent on the card. /bin/sh runs
681
+ # it in the tab, so nothing is typed into a shell, `task start` returns at
682
+ # once, setup is on screen where the work will happen, and the agent cannot
683
+ # start before setup has finished.
684
+ setup_line "$tdir" "${repos[@]}"
685
+ build="ocnus task build $name $card"
686
+ line=${SETUP_LINE:+$SETUP_LINE && }$build
687
+ # The write-up sessions to put away once the build tab is open (close.sh):
688
+ # the card's own, and for a sub-issue its parent's, whose tab it was written
689
+ # up in. Only one with a tab open in the space: a card started with no
690
+ # write-up session starts as before. They are looked for before the build
691
+ # tab opens, which is the last thing the app is asked to do.
692
+ local writeups=() writeup sep=$'\x1f' name_seen rest
693
+ agents_read "$name" "$dir"
694
+ for writeup in "$card" ${CARD_PARENT:+"$CARD_PARENT"}; do
695
+ while IFS=$sep read -r name_seen rest; do
696
+ [ "$name_seen" != "$writeup" ] || { writeups+=("$writeup"); break; }
697
+ done <<< "$AGENT_TABS"
698
+ done
699
+ # The task exists now. If no tab can be opened for it, keep it, and say
700
+ # where it is and how to start its agent.
701
+ open_agent_tab "$name" "$dir" "$folder" "$task" "$card" '' /bin/sh -c "$line" ||
702
+ die "made the task at $folder, but $AGENT_ERROR
703
+ To start its agent, run this in a terminal in that folder: $line"
704
+ for ready in ${SETUP_READY[@]+"${SETUP_READY[@]}"}; do say " setup $ready (running in the tab)"; done
705
+ say " agent starting in the tab: $build"
706
+ if [ "$AGENT_IN_APP" = 0 ]; then
707
+ say " terminal ${AGENT_TERMINAL:+$AGENT_TERMINAL }in herdr-core, since the Herdr app is not open. Opening Herdr shows it as a tab."
708
+ [ -z "$AGENT_TERMINAL" ] || say " attach to see it here: hc attach $AGENT_TERMINAL"
709
+ fi
710
+ [ "${#repos[@]}" -gt 1 ] || repo_trust_note "${repos[0]}"
711
+ host_wake
712
+ merges_wake
713
+ # The work has started, so the card shows it in Linear, and so does the card
714
+ # it was sliced from, if it is a slice: linear_move says one line for each.
715
+ # The task is made and open by now, so a card that cannot be moved is a
716
+ # note, never a failure.
717
+ local moved said notes=''
718
+ moved=$(linear_move_note "$card" started) || true
719
+ while IFS= read -r said; do
720
+ [ -n "$said" ] || continue
721
+ if [[ $said =~ $NOT_MOVED_RE ]]; then start_note "$said"
722
+ else say " card $said"
723
+ fi
724
+ done <<< "$moved"
725
+ # The start is recorded last, so that its flags hold every note above that
726
+ # still needs the human, the move's too, joined by "; ".
727
+ for note in ${START_NOTES[@]+"${START_NOTES[@]}"}; do notes=${notes:+$notes; }$note; done
728
+ record_start "$name" "$card" start "$notes"
729
+ # The build phase has started, so the write-up sessions found above are put
730
+ # away. This comes last, so that a task start that stops leaves them open.
731
+ for writeup in ${writeups[@]+"${writeups[@]}"}; do start_close "$name" "$writeup" "$dir"; done
732
+ say "Opened task '$task' on branch $branch"
733
+ }
734
+
735
+ # Start the build agent on a card, in its task: the task's tab runs this, as
736
+ # the command `task start` gives it, after setup when setup runs. The first
737
+ # message has the agent build the card, review the build and open its pull
738
+ # request. Run it again in the tab to start a new agent when the first one
739
+ # has stopped.
740
+ #
741
+ # With --continue and a message, it continues claude's last session in this
742
+ # folder instead, with that message: ocnus's watch on the merges types
743
+ # it into the tab's shell to wake an agent that has exited (merges.sh). The
744
+ # session comes back with the build profile, model and effort, which a bare
745
+ # `claude --continue` would not give it. Only claude has --continue.
746
+ TASK_BUILD_USAGE='usage: ocnus task build <space> <card> [--continue <message>]'
747
+ task_build() {
748
+ local name=${1:-} card=${2:-} dir here low skill prompt again=''
749
+ dir=$(space_dir "$name")
750
+ [ -n "$card" ] ||
751
+ die "which card? Usage: ocnus task build $name <card>, like: ocnus task build $name ABC-122"
752
+ [[ $card =~ $CARD_RE ]] || die "'$card' is not a card ID like ABC-122."
753
+ case ${3-} in
754
+ --continue)
755
+ [ "$#" = 4 ] && [ -n "$4" ] || die "$TASK_BUILD_USAGE"
756
+ again=$4
757
+ ;;
758
+ *) [ "$#" -le 2 ] || die "$TASK_BUILD_USAGE" ;;
759
+ esac
760
+ [ -z "$again" ] || [ "${HARNESS##*/}" = claude ] ||
761
+ die "--continue is Claude Code's: it continues claude's last session in this folder, and the harness here is '$HARNESS'. Nothing was started."
762
+ card=$(printf '%s' "$card" | tr '[:lower:]' '[:upper:]')
763
+ low=$(printf '%s' "$card" | tr '[:upper:]' '[:lower:]')
764
+ # The agent works in the folder it starts in, so that has to be this card's
765
+ # task: tasks/<card>-<short name>, or tasks/<card> for a title with no plain
766
+ # words, or a worktree inside it. tasks/abc-12-… is not ABC-1's.
767
+ here=$(pwd -P)
768
+ case $here/ in
769
+ "$dir/tasks/$low/"* | "$dir/tasks/$low-"*/*) ;;
770
+ *) die "run this in $card's task, the folder task start made for it in $dir/tasks. To make it: ocnus task start $name $card" ;;
771
+ esac
772
+ command -v "$HARNESS" >/dev/null 2>&1 ||
773
+ die "'$HARNESS' is not on your PATH, so there is no harness to start the agent in. Install it, or set OCNUS_HARNESS to the one you use."
774
+ for skill in $BUILD_SKILLS; do
775
+ [ -f "$SKILLS_HOME/$skill/SKILL.md" ] ||
776
+ die "the $skill skill is not in $SKILLS_HOME, so the agent could not use it. Run: ocnus update"
777
+ done
778
+ profile_args "$BUILD_PROFILE" "$BUILD_SETTINGS" "$name" "$card" build
779
+ if [ -z "$again" ]; then
780
+ [ -f "$BUILD_PROMPT" ] || die "the agent's first message is missing from $BUILD_PROMPT"
781
+ prompt=$(sed -e "s/{card}/$card/g" -e "s/{space}/$name/g" "$BUILD_PROMPT")
782
+ fi
783
+ # The agent is about to start in this tab, so recording, when it is on,
784
+ # starts too, as it does when the build agent is started again here, and
785
+ # so does ocnus's watch on the merges, while a card waits on one.
786
+ host_wake
787
+ merges_wake
788
+ # Every command the agent runs is named its space and card, so that
789
+ # `ocnus tests run` records its test runs as this card's (tests.sh).
790
+ export OCNUS_SPACE=$name OCNUS_CARD=$card
791
+ # The first message goes last, after the profile's flags, and so does the
792
+ # message a session continued with, after --continue. Either way claude
793
+ # starts through env's words, without the variables that would put it on
794
+ # the API (profile_args).
795
+ [ -z "$again" ] ||
796
+ exec ${AGENT_ENV[@]+"${AGENT_ENV[@]}"} "$HARNESS" ${PROFILE_ARGS[@]+"${PROFILE_ARGS[@]}"} --continue "$again"
797
+ exec ${AGENT_ENV[@]+"${AGENT_ENV[@]}"} "$HARNESS" ${PROFILE_ARGS[@]+"${PROFILE_ARGS[@]}"} "$prompt"
798
+ }
799
+
800
+ # List the open cards of a space's Linear teams, grouped by status: the
801
+ # cards `task new` can be given. Linear itself is asked in linear.sh. --json
802
+ # prints the list as one line for programs; the cards mod reads it.
803
+ task_ls() {
804
+ local name=${1:-} json=0 dir team keys list
805
+ local -a teams=()
806
+ dir=$(space_dir "$name")
807
+ case ${2:-} in
808
+ --json) json=1 ;;
809
+ '') ;;
810
+ *) die "unknown option for task ls: $2" ;;
811
+ esac
812
+ [ "$#" -le 2 ] || die "usage: ocnus task ls <space> [--json]"
813
+ # Read first, so that a bad line in the teams file stops it with its own
814
+ # message, not one saying there is no team.
815
+ keys=$(space_teams "$dir") || exit 1
816
+ while IFS= read -r team; do [ -z "$team" ] || teams+=("$team"); done <<< "$keys"
817
+ [ "${#teams[@]}" -gt 0 ] ||
818
+ die "space '$name' has no Linear team, so there are no cards to list. Set it with: ocnus space team $name <team key>, like: ocnus space team $name ABC"
819
+ list=$(linear_open_cards "$name" "${teams[@]}") || exit 1
820
+ if [ "$json" = 1 ]; then
821
+ printf '%s\n' "$list"
822
+ return
823
+ fi
824
+ # Titles and status names are anyone's text. In the list, a newline would
825
+ # start what looks like another card, and an escape sequence would reach the
826
+ # terminal, so every control character becomes a space (PLAIN_DEF, in
827
+ # linear.sh, which `linear read` uses too). --json above keeps the text as
828
+ # it is.
829
+ printf '%s' "$list" |
830
+ jq -r "$PLAIN_DEF"'
831
+ .statuses[] | "\(.name | plain) (\(.cards | length))", (.cards[] | " \(.id | plain) \(.title | plain)")'
832
+ }
833
+
834
+ cmd_task() {
835
+ case ${1:-} in
836
+ new) shift; task_new "$@" ;;
837
+ ls | list) shift; task_ls "$@" ;;
838
+ start) shift; task_start "$@" ;;
839
+ build) shift; task_build "$@" ;;
840
+ finish) shift; task_finish "$@" ;;
841
+ close) shift; task_close "$@" ;;
842
+ merges) shift; task_merges "$@" ;;
843
+ handoff) shift; task_handoff "$@" ;;
844
+ log) shift; task_log "$@" ;;
845
+ *) die "usage: ocnus task ls <space> [--json], or: ocnus task new <space> <card>, or: ocnus task start <space> <card> $START_ARGS, or: ocnus task build <space> <card> [--continue <message>], or: ocnus task close <space> <card>, or: ocnus task finish <space> <card>, or: ocnus task merges [--once | --wake], or: ocnus task handoff <space> <card>, or: ocnus task log <space> [<card>] [--json] (see: ocnus help)" ;;
846
+ esac
847
+ }