@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,189 @@
1
+ # Putting a card's write-up session away once its build phase has started.
2
+ # Sourced by cli/ocnus.
3
+
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
7
+ # outlives the turn of the agent that ran `task start`. `task close` waits for
8
+ # that turn to end, ends the agent's Claude session with /exit, waits for the
9
+ # tab's own shell to come back, then closes the tab, as `task finish` closes a
10
+ # task's.
11
+ #
12
+ # The clean exit is what puts the session away in the Claude app too. With
13
+ # Remote Control on, Claude Code archives its session in the app's list as it
14
+ # exits, when it tears its bridge down. `hc kill` hangs up and then
15
+ # escalates, which can skip that, so a live agent is never killed: the tab is
16
+ # closed only once every agent in it has exited. The transcript is not
17
+ # touched, so the session can still be opened with `claude --resume`.
18
+ #
19
+ # Only the tab named after the card, in the space, is put away. Each of its
20
+ # terminals counts by what hc says it is doing (agents.sh reads it the same
21
+ # way):
22
+ # - working, blocked or error: its turn goes on, or waits on a person, so
23
+ # task close waits for it to end, however long that takes;
24
+ # - idle or done: its turn has ended, so it gets /exit;
25
+ # - its own shell in the foreground with no status, or finished: no agent
26
+ # runs there, so there is nothing to end;
27
+ # - a program that reports no status, or a terminal hc cannot answer for:
28
+ # what it is doing is not known, so the tab is left open.
29
+
30
+ # The seconds task close waits for an agent to exit once it has sent /exit,
31
+ # checking every second.
32
+ CLOSE_EXIT_WAIT=30
33
+ # The seconds between checks on an agent that waits on a person, blocked or
34
+ # hit an error, since `hc wait` comes back at once for it.
35
+ CLOSE_EVERY=5
36
+
37
+ # Print what a terminal is doing, as one word: working, blocked, error, idle
38
+ # or done, as its program last reported; none for its own shell in the
39
+ # foreground with no status, or a finished terminal; unknown for a program
40
+ # that reports no status, or a terminal hc cannot answer for.
41
+ # terminal_doing <terminal>
42
+ terminal_doing() {
43
+ local out
44
+ if ! out=$(hc_cmd get "$1" --json 2>/dev/null) || [ -z "$out" ]; then
45
+ printf 'unknown\n'
46
+ return 0
47
+ fi
48
+ printf '%s' "$out" | jq -r '
49
+ (.status | if type == "object" then .state else null end) as $s
50
+ | if .state == "finished" then "none"
51
+ elif .status == null then
52
+ (if (.foreground.pid? // null) != null and .foreground.pid != .pid then "unknown" else "none" end)
53
+ elif ($s | type) == "string" and ({working: 1, blocked: 1, error: 1, idle: 1, done: 1} | has($s)) then $s
54
+ else "unknown" end' 2>/dev/null || printf 'unknown\n'
55
+ }
56
+
57
+ # End the agent in a terminal of the write-up tab once its turn has ended:
58
+ # wait while it works, or waits on a person, then send /exit and wait for its
59
+ # shell. It returns 0 once no agent runs there, 2 when what the terminal runs
60
+ # is not known, and 1 when the agent did not exit. With nowait, an agent
61
+ # whose turn goes on is not waited for: it returns 1 at once, and the agent
62
+ # is left running. ocnus's watch on the merges passes it (merges.sh), so
63
+ # that a pass never waits on a turn; `task close` passes nothing.
64
+ # end_agent <terminal> [nowait]
65
+ end_agent() {
66
+ local t=$1 nowait=${2:-} doing said=0 n=0
67
+ while :; do
68
+ doing=$(terminal_doing "$t")
69
+ case $doing in
70
+ working | blocked | error)
71
+ if [ "$nowait" = nowait ]; then
72
+ say " agent $t is $doing, so it is left running"
73
+ return 1
74
+ fi
75
+ [ "$said" = 1 ] || say " agent $t is $doing: waiting for its turn to end"
76
+ said=1
77
+ # It comes back when the turn ends, and every so often while it goes on.
78
+ hc_cmd wait "$t" >/dev/null 2>&1 || sleep "$CLOSE_EVERY"
79
+ case $doing in blocked | error) sleep "$CLOSE_EVERY" ;; esac
80
+ ;;
81
+ idle | done) break ;;
82
+ none) return 0 ;;
83
+ *)
84
+ say " agent $t runs a program that reports no status, or that hc cannot answer for, so it is left running"
85
+ return 2
86
+ ;;
87
+ esac
88
+ done
89
+ say " agent ending the session in $t with /exit"
90
+ if ! hc_cmd send "$t" /exit >/dev/null 2>&1; then
91
+ say " agent could not send /exit to $t, so it is left running"
92
+ return 1
93
+ fi
94
+ while :; do
95
+ [ "$(terminal_doing "$t")" != none ] || return 0
96
+ if [ "$n" -ge "$CLOSE_EXIT_WAIT" ]; then
97
+ say " agent $t did not exit within $CLOSE_EXIT_WAIT seconds of /exit, so it is left running"
98
+ return 1
99
+ fi
100
+ sleep 1
101
+ n=$((n + 1))
102
+ done
103
+ }
104
+
105
+ # Put a card's write-up session away, once its build phase has started: end
106
+ # the agent in each terminal of the tab named after the card, as end_agent
107
+ # does, then close the tab. `task start` runs it detached, so it waits for the
108
+ # turn of the agent that ran `task start`. It refuses to run in one of the
109
+ # tab's own terminals, which would wait on its own turn.
110
+ task_close() {
111
+ local name=${1:-} card=${2:-} dir sep=$'\x1f' tab terms='' found=0 t rc left=0
112
+ dir=$(space_dir "$name")
113
+ [ -n "$card" ] ||
114
+ die "which card? Usage: ocnus task close $name <card>, like: ocnus task close $name ABC-122"
115
+ [[ $card =~ $CARD_RE ]] || die "'$card' is not a card ID like ABC-122."
116
+ [ "$#" -le 2 ] || die "usage: ocnus task close <space> <card>"
117
+ card=$(printf '%s' "$card" | tr '[:lower:]' '[:upper:]')
118
+ command -v jq >/dev/null 2>&1 ||
119
+ die "jq is not installed, so $card's write-up tab could not be found. Nothing was changed."
120
+ have_hc || die "Herdr's hc is not installed, so $card's write-up tab could not be found. Nothing was changed."
121
+
122
+ agents_read "$name" "$dir"
123
+ [ "$AGENTS_ON" = 1 ] ||
124
+ die "neither herdr-core nor the Herdr app could list the tabs of the space for '$name', so $card's write-up tab could not be found. Nothing was changed."
125
+ while IFS=$sep read -r tab t; do
126
+ [ "$tab" = "$card" ] || continue
127
+ found=1 terms=$t
128
+ done <<< "$AGENT_TABS"
129
+ if [ "$found" = 0 ]; then
130
+ say "Nothing to put away: no write-up tab named $card is open in the space for '$name'"
131
+ return 0
132
+ fi
133
+ for t in $terms; do
134
+ [ "$t" != "${HC_TERMINAL_ID:-}" ] ||
135
+ die "this runs in $card's write-up tab, in $t, so it would wait on its own turn. Run it in another terminal. Nothing was changed."
136
+ done
137
+ # Before any agent is ended: a tab that cannot be closed is left whole.
138
+ for t in $terms; do
139
+ [ "$(terminal_doing "$t")" != unknown ] ||
140
+ die "$t, in $card's write-up tab, runs a program that reports no status, or hc cannot answer for it, so the tab is left open. Close the tab $card yourself once it is done with. Nothing was changed."
141
+ done
142
+
143
+ say "Putting away $card's write-up session in the space for '$name'"
144
+ for t in $terms; do
145
+ rc=0
146
+ end_agent "$t" || rc=$?
147
+ [ "$rc" = 0 ] || left=1
148
+ done
149
+ [ "$left" = 0 ] ||
150
+ die "left $card's write-up tab open, since what runs there has not ended. Close the tab $card yourself once it has."
151
+ # The app's tab first, then whatever herdr-core still has labelled for it,
152
+ # which covers a tab the app shows under another name.
153
+ if herdr_running; then
154
+ close_task_tab "$name" "$dir" "$card"
155
+ end_task_terminals "$name" "$card" quiet
156
+ else
157
+ end_task_terminals "$name" "$card"
158
+ fi
159
+ say "Put away $card's write-up session"
160
+ }
161
+
162
+ # 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
164
+ # the agent that ran `task start`, and in the space's folder, which stays. Its
165
+ # terminal has none of ocnus's or Herdr's labels, so the Herdr app does
166
+ # not show it and `host` does not count it as an agent; no parent, so it goes
167
+ # on when the tab that started it closes; no --keep, so nothing brings it back
168
+ # after a restart; and --rm, so it is gone once it has ended. hc lets one live
169
+ # terminal hold a name, so a second `task start` for the card finds one
170
+ # already waiting.
171
+ # start_close <space> <card> <the space's folder>
172
+ start_close() {
173
+ local name=$1 card=$2 dir=$3 low out state
174
+ low=$(printf '%s' "$card" | tr '[:upper:]' '[:lower:]')
175
+ if out=$(hc_cmd run --bg --no-parent --rm --name "ocnus-close-$low" --cwd "$dir" \
176
+ -- "$REPO/cli/ocnus" task close "$name" "$card" </dev/null 2>&1); then
177
+ say " write-up $card's write-up session is put away once its agent's turn ends"
178
+ return 0
179
+ fi
180
+ state=$(hc_cmd get "ocnus-close-$low" --json 2>/dev/null) || state=
181
+ case $state in
182
+ *'"state":"live"'* | *'"state": "live"'*)
183
+ say " write-up $card's write-up session is already waiting to be put away"
184
+ return 0
185
+ ;;
186
+ esac
187
+ out=$(printf '%s' "$out" | tr -s '\n' ' ')
188
+ say " note $card's write-up session stays open: herdr-core could not start what puts it away${out:+ (${out% })}. Close its tab yourself once its agent's turn has ended."
189
+ }
@@ -0,0 +1,220 @@
1
+ # The top-level commands: install, update, status. Sourced by cli/ocnus.
2
+
3
+ # What install and update link, and where. Before Herdr's first run that is
4
+ # the skills and the profiles, and the mods and layouts wait.
5
+ link_line() {
6
+ if herdr_opened; then
7
+ say "Linking mods and layouts into $APP_HOME, skills into $SKILLS_HOME, and agent profiles into $AGENTS_HOME"
8
+ else
9
+ say "Linking skills into $SKILLS_HOME, and agent profiles into $AGENTS_HOME"
10
+ fi
11
+ }
12
+
13
+ cmd_install() {
14
+ require_herdr
15
+ CHANGED=0 SKIPPED=0
16
+ link_line
17
+ link_all
18
+ herdr_opened || say " waiting $HERDR_WAITS"
19
+ settings_init
20
+ write_review_profiles
21
+
22
+ # npm already put the command on PATH; a clone has to do it itself.
23
+ if is_clone; then
24
+ say "Putting ocnus on your PATH"
25
+ mkdir -p "$BIN_DIR"
26
+ local me=$REPO/cli/ocnus dst=$BIN_DIR/ocnus
27
+ case $(state_of "$me" "$dst") in
28
+ linked) say " already at $dst" ;;
29
+ missing) ln -s "$me" "$dst"; say " linked $dst" ;;
30
+ *) say " skipped $dst (something else is already there)" ;;
31
+ esac
32
+ case ":$PATH:" in
33
+ *":$BIN_DIR:"*) ;;
34
+ *) say " note: $BIN_DIR is not on your PATH; add it to run 'ocnus' from anywhere" ;;
35
+ esac
36
+ fi
37
+ # What brings ocnus's watch on the merges back after a restart.
38
+ merges_launchd
39
+
40
+ # Before Herdr's first run there is no app to refresh, and nothing linked
41
+ # for it to read.
42
+ if herdr_opened; then
43
+ say "Refreshing Herdr"
44
+ refresh_herdr
45
+ fi
46
+ say "Done. Switch Herdr to the default layout: pick it from the Layout dropdown, or press ctrl-shift-l until it shows, as you must from Herdr's own layout, which has no dropdown."
47
+ }
48
+
49
+ cmd_update() {
50
+ local pull=1
51
+ case ${1:-} in
52
+ --no-pull) pull=0 ;;
53
+ '') ;;
54
+ *) die "unknown option for update: $1" ;;
55
+ esac
56
+ require_herdr
57
+ CHANGED=0 SKIPPED=0
58
+ local before=none after=none
59
+ if [ "$pull" = 1 ] && ! is_clone; then
60
+ # Installed by npm: reinstall, then hand over to the new copy to relink.
61
+ command -v npm >/dev/null 2>&1 || die "npm is needed to update this install, and it is not on your PATH."
62
+ say "Updating ocnus with npm ($NPM_SPEC)"
63
+ npm install -g "$NPM_SPEC" || die "npm install failed; nothing was changed in Herdr."
64
+ exec ocnus update --no-pull
65
+ fi
66
+ if is_clone; then before=$(git -C "$REPO" rev-parse HEAD 2>/dev/null || echo none); fi
67
+ if [ "$pull" = 1 ]; then
68
+ say "Pulling $REPO"
69
+ git -C "$REPO" pull --ff-only || die "git pull failed; fix that in $REPO and run update again."
70
+ fi
71
+ if is_clone; then after=$(git -C "$REPO" rev-parse HEAD 2>/dev/null || echo none); fi
72
+
73
+ link_line
74
+ link_all
75
+ prune
76
+ herdr_opened || say " waiting $HERDR_WAITS"
77
+ settings_init
78
+ write_review_profiles
79
+ # What brings ocnus's watch on the merges back after a restart, written
80
+ # again when it has changed, such as with another PATH.
81
+ merges_launchd
82
+
83
+ if herdr_opened; then
84
+ say "Refreshing Herdr"
85
+ if [ "$before" != "$after" ] || [ "$CHANGED" -gt 0 ] || [ "$pull" = 0 ]; then
86
+ refresh_herdr
87
+ else
88
+ say " nothing changed"
89
+ fi
90
+ fi
91
+ # ocnus's watch on the merges, for a card that waits on one, such as
92
+ # one that waited before the watch was there.
93
+ # Only while herdr-core runs: an update never starts it (merges.sh).
94
+ merges_wake
95
+ }
96
+
97
+ cmd_status() {
98
+ say "Repo: $REPO"
99
+ say "Herdr: $APP_HOME"
100
+ say "Skills: $SKILLS_HOME"
101
+ say "Agents: $AGENTS_HOME"
102
+ local src dst s problems=0 herdr_problems=0 settings_problems=0 unread='' core=none
103
+ # herdr-core runs each agent's terminal, so whether it is running is what
104
+ # matters, not whether the app's window is open. Not running is in order: task new and
105
+ # task start start it. herdr-app and hc both come with Herdr, so either one
106
+ # missing is Herdr not installed. It is asked once, and what it answered
107
+ # goes to the watch on the merges below.
108
+ if ! { have_herdr && have_hc; }; then
109
+ printf ' %-10s %s\n' problem "Herdr is not installed. Install it: $HERDR_INSTALL"
110
+ herdr_problems=1
111
+ elif hc_cmd server status --json >/dev/null 2>&1; then
112
+ core=up
113
+ printf ' %-10s %s\n' herdr-core running
114
+ else
115
+ core=down
116
+ printf ' %-10s %s\n' herdr-core "not running; ocnus starts it when a task needs it"
117
+ fi
118
+ # Before Herdr's first run the mods and layouts wait for its folder, which
119
+ # is in order.
120
+ if ! herdr_opened; then
121
+ while IFS=$'\t' read -r src dst; do
122
+ printf ' %-10s %s\n' waiting "$(label "$dst")"
123
+ done < <(herdr_wanted)
124
+ fi
125
+ while IFS=$'\t' read -r src dst; do
126
+ s=$(state_of "$src" "$dst")
127
+ [ "$s" = linked ] || problems=$((problems + 1))
128
+ printf ' %-10s %s\n' "$s" "$(label "$dst")"
129
+ done < <(wanted)
130
+ # The settings file, which stops task new and task build when it is not
131
+ # valid settings. Without it read, a review subagent ocnus wrote cannot
132
+ # be compared with what it would write now.
133
+ if ! settings_load; then
134
+ say " problem $SETTINGS_WHY"
135
+ settings_problems=1 unread=1
136
+ fi
137
+ # The review subagents a clone writes, rather than links (settings.sh).
138
+ if is_clone; then
139
+ for src in "$REVIEW_SOURCES"/*.md; do
140
+ [ -f "$src" ] || continue
141
+ is_profile "$(review_profile_of "$src")" || continue
142
+ dst=$AGENTS_HOME/${src##*/}
143
+ s=$(review_state "$src" "$dst" "$unread")
144
+ [ "$s" = written ] || problems=$((problems + 1))
145
+ printf ' %-10s %s\n' "$s" "$(label "$dst")"
146
+ done
147
+ fi
148
+ [ "$problems" = 0 ] || say "$problems not linked or not up to date. Run: ocnus update"
149
+ herdr_opened || say "Herdr has not been opened yet, so the mods and layouts wait. Open it once, then run: ocnus update"
150
+ say "Spaces: $SPACES"
151
+ local space_problems=0
152
+ check_spaces || space_problems=$?
153
+ # Only worth saying on a machine that has spaces to start tasks in.
154
+ if ! have_just && [ -n "$(ls -d "$SPACES"/*/ 2>/dev/null)" ]; then
155
+ say " note just is not installed, so tasks start without setup. Install it: https://just.systems"
156
+ fi
157
+ if ! have_sqlite && [ -n "$(ls -d "$SPACES"/*/ 2>/dev/null)" ]; then
158
+ say " note $SQLITE is not installed, so hand-offs are not recorded."
159
+ fi
160
+ # ocnus's watch on the merges, and the launchd agent that brings it
161
+ # back after a restart (merges.sh). It only reads: it starts neither.
162
+ local merges_problems=0
163
+ merges_status "$core" || merges_problems=$?
164
+ # The Linear key, asked of Linear itself. A key Linear rejects, one that is
165
+ # no key at all, and one that works but cannot change cards are problems.
166
+ # With none saved the rest of ocnus still works, and a key that could
167
+ # not be checked may work too.
168
+ local key_problems=0 key_rc=0 key_out writes_rc=0 writes_out
169
+ if [ -n "$KEY_EXPORTED" ]; then say "Linear key: LINEAR_API_KEY, exported in your environment"
170
+ else say "Linear key: $ENV_FILE"
171
+ fi
172
+ key_out=$(linear_key_state 2>&1) || key_rc=$?
173
+ case $key_rc in
174
+ 0) printf ' %-10s %s\n' works "the key of ${key_out:-a user with no name}"
175
+ # Only a key that works is asked whether it can change cards too.
176
+ writes_out=$(linear_key_writes "$LINEAR_API_KEY" 2>&1) || writes_rc=$?
177
+ case $writes_rc in
178
+ 0) printf ' %-10s %s\n' writes "the key can change cards" ;;
179
+ 1) printf ' %-10s %s\n' problem "$NO_WRITE_WHY"
180
+ key_problems=1 ;;
181
+ *) printf ' %-10s %s\n' note "whether the key can change cards was not checked${writes_out:+: $writes_out}" ;;
182
+ esac ;;
183
+ 1) key_out=${key_out#ocnus: }
184
+ printf ' %-10s %s\n' problem "${key_out%%$'\n'*}"
185
+ key_problems=1 ;;
186
+ 2) printf ' %-10s %s\n' note "no Linear key is saved, and task ls and the Cards panel need one, as does task start unless it is given the card's type and title. Make one in $KEY_PLACE, then save it with: ocnus linear key" ;;
187
+ # Only the first sentence of why, as task new gives it: the rest of
188
+ # linear_ask's message is advice for a command that stops.
189
+ *) key_out=$(first_sentence "$key_out")
190
+ printf ' %-10s %s\n' note "the key was not checked${key_out:+: $key_out}" ;;
191
+ esac
192
+ # Which account the agents run on (billing.sh), and whether an Anthropic
193
+ # API key is saved, never the key itself. On api with none saved, task new
194
+ # and task build stop before an agent starts, so that is a problem. From a
195
+ # settings file that cannot be read, listed above, nothing is said.
196
+ local billing_problems=0
197
+ if [ -z "$unread" ]; then
198
+ say "Billing: $SETTINGS"
199
+ if [ "$SET_BILLING" = api ]; then
200
+ printf ' %-10s %s\n' mode "api: every agent task new and task build start runs on an Anthropic API key"
201
+ else
202
+ printf ' %-10s %s\n' mode "subscription: every agent task new and task build start runs on the Claude subscription Claude Code is signed in to"
203
+ fi
204
+ if [ -n "$SAVED_ANTHROPIC_KEY" ]; then
205
+ printf ' %-10s %s\n' key "an Anthropic API key is saved in $ENV_FILE"
206
+ elif [ "$SET_BILLING" = api ]; then
207
+ printf ' %-10s %s\n' problem "no Anthropic API key is saved, so task new and task build stop before an agent starts. Save one with: ocnus billing key, or go back to the subscription with: ocnus billing subscription"
208
+ billing_problems=1
209
+ else
210
+ printf ' %-10s %s\n' key "no Anthropic API key is saved, and the subscription needs none"
211
+ fi
212
+ fi
213
+ if [ "$problems" -gt 0 ] || [ "$herdr_problems" -gt 0 ] || [ "$space_problems" -gt 0 ] ||
214
+ [ "$merges_problems" -gt 0 ] || [ "$settings_problems" -gt 0 ] || [ "$key_problems" -gt 0 ] ||
215
+ [ "$billing_problems" -gt 0 ]; then
216
+ [ "$space_problems" = 0 ] || say "$space_problems space problem(s)."
217
+ return 1
218
+ fi
219
+ say "Everything is in order."
220
+ }
@@ -0,0 +1,215 @@
1
+ # Helpers every part of the CLI uses. Sourced by cli/ocnus.
2
+
3
+ # A git clone updates with `git pull`; an npm install updates through npm.
4
+ is_clone() { [ -e "$REPO/.git" ]; }
5
+
6
+ say() { printf '%s\n' "$*"; }
7
+ die() { printf 'ocnus: %s\n' "$*" >&2; exit 1; }
8
+
9
+ # Run from inside a Claude Code session, as an agent runs it, ocnus has
10
+ # that session's variables, such as CLAUDECODE and CLAUDE_CODE_CHILD_SESSION.
11
+ # Any hc command starts herdr-core when it is not running, and herdr-core
12
+ # keeps the environment of the command that started it for every terminal it
13
+ # runs until it is stopped. A claude that sees CLAUDE_CODE_CHILD_SESSION
14
+ # saves no transcript, so `claude --continue` there has nothing to resume.
15
+ # So hc and herdr-app run without any variable whose name starts with
16
+ # CLAUDE, apart from CLAUDE_CONFIG_DIR, which says where Claude Code's folder
17
+ # is for them as it does for ocnus. Call it in a subshell: it unsets them
18
+ # for the rest of the shell it runs in.
19
+ without_claude_session() {
20
+ local name
21
+ for name in $(compgen -e); do
22
+ case $name in
23
+ CLAUDE_CONFIG_DIR) ;;
24
+ CLAUDE*) unset "$name" ;;
25
+ esac
26
+ done
27
+ }
28
+
29
+ herdr_app() {
30
+ if command -v herdr-app >/dev/null 2>&1; then (without_claude_session; exec herdr-app "$@")
31
+ elif [ -x "$BIN_DIR/herdr-app" ]; then (without_claude_session; exec "$BIN_DIR/herdr-app" "$@")
32
+ else return 127
33
+ fi
34
+ }
35
+
36
+ have_herdr() { command -v herdr-app >/dev/null 2>&1 || [ -x "$BIN_DIR/herdr-app" ]; }
37
+
38
+ # hc is the command for Herdr's terminals; it is installed beside herdr-app.
39
+ hc_cmd() {
40
+ if command -v hc >/dev/null 2>&1; then (without_claude_session; exec hc "$@")
41
+ elif [ -x "$BIN_DIR/hc" ]; then (without_claude_session; exec "$BIN_DIR/hc" "$@")
42
+ else return 127
43
+ fi
44
+ }
45
+
46
+ have_hc() { command -v hc >/dev/null 2>&1 || [ -x "$BIN_DIR/hc" ]; }
47
+
48
+ # herdr-core runs every terminal, an agent's among them, whether or not the
49
+ # Herdr app's window is open, so starting an agent needs it and not the
50
+ # window. Make sure it is running, starting it when it is not, or stop before
51
+ # anything is made or started. herdr-app and hc both come with Herdr, so
52
+ # either one missing is Herdr not installed.
53
+ core_ready() {
54
+ local started status
55
+ { have_herdr && have_hc; } || die "Herdr is not installed. Install it, then run this again:
56
+ $HERDR_INSTALL"
57
+ hc_cmd server status --json >/dev/null 2>&1 && return 0
58
+ started=$(hc_cmd server start 2>&1) || true
59
+ if status=$(hc_cmd server status --json 2>&1); then
60
+ say "Started herdr-core"
61
+ return 0
62
+ fi
63
+ # What hc said: why it did not start, or else why it does not answer.
64
+ started=$(printf '%s' "${started:-$status}" | tr -s '\n' ' ')
65
+ started=${started% }
66
+ die "herdr-core is not running, and it did not start${started:+: $started}. Start it with: hc server start, then run this again."
67
+ }
68
+
69
+ # A new id in the form Herdr gives its own: sp for a space or tb for a tab,
70
+ # then 32 hex digits. The Herdr app makes a new space or tab of an id it does
71
+ # not know.
72
+ new_herdr_id() {
73
+ local hex
74
+ hex=$(LC_ALL=C od -A n -t x1 -N 16 /dev/urandom | LC_ALL=C tr -d ' \n')
75
+ [ "${#hex}" = 32 ] || die "could not read /dev/urandom to make an id for Herdr"
76
+ printf '%s%s\n' "$1" "$hex"
77
+ }
78
+
79
+ # Print a string quoted for a shell to read back as one word: it's -> 'it'\''s'.
80
+ # LC_ALL=C: sed must not give up on a name that is not valid text in this locale.
81
+ sh_quote() { printf "'%s'" "$(printf '%s' "$1" | LC_ALL=C sed "s/'/'\\\\''/g")"; }
82
+
83
+ # Herdr answers `status` with the layout it shows only while its window is open.
84
+ herdr_running() { herdr_app status 2>/dev/null | grep -q '"layout"'; }
85
+
86
+ # Only that Herdr is installed: install and update link what they can before
87
+ # its first run, and space open asks the app itself whether it is open.
88
+ require_herdr() {
89
+ have_herdr || die "Herdr is not installed. Install it, then run this again:
90
+ $HERDR_INSTALL"
91
+ }
92
+
93
+ # Herdr makes its folder on its first start, so until it has been opened once
94
+ # there is nowhere to link the mods and layouts.
95
+ herdr_opened() { [ -d "$APP_HOME" ]; }
96
+
97
+ # What install and update say, once, before Herdr's first run.
98
+ HERDR_WAITS='mods and layouts: Herdr has not been opened yet. Open it once, then run: ocnus update'
99
+
100
+ # Print a string as the inside of a JSON string. Most strings have nothing to
101
+ # escape and are printed as they are, with no program started. For the rest,
102
+ # sed and awk do the work so that the result does not depend on the bash
103
+ # version's quoting rules.
104
+ json_str() {
105
+ case $1 in
106
+ *[\\\"[:cntrl:]]*) ;;
107
+ *) printf '%s' "$1"; return ;;
108
+ esac
109
+ printf '%s' "$1" | tr '\t\r' ' ' |
110
+ sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' |
111
+ awk 'BEGIN { ORS = "" } NR > 1 { print "\\n" } { print }'
112
+ }
113
+
114
+ # --- ocnus's env file ---------------------------------------------------
115
+ #
116
+ # The keys the human saves, Linear's with `linear key` and Anthropic's with
117
+ # `billing key`, are lines of ocnus's env file, $ENV_FILE. These are the
118
+ # rules both commands save a key by.
119
+
120
+ # Print the top folder of the git repo a folder is in, through any links on
121
+ # the way, or fail when it is in none. A folder not made yet is looked for
122
+ # from the nearest folder above it that is, where mkdir -p would make it.
123
+ # git_repo_around <folder>
124
+ git_repo_around() {
125
+ local dir=$1
126
+ while [ ! -d "$dir" ]; do dir=$(dirname "$dir"); done
127
+ dir=$(cd -P "$dir" 2>/dev/null && pwd) || return 1
128
+ while [ ! -e "$dir/.git" ]; do
129
+ [ "$dir" != / ] || return 1
130
+ dir=$(dirname "$dir")
131
+ done
132
+ printf '%s\n' "$dir"
133
+ }
134
+
135
+ # Stop before a key is asked for when the env file is not one ocnus may
136
+ # write a key into, so that nobody pastes one for nothing. $1 is the name of
137
+ # the key's line, which the message tells the human to add by hand.
138
+ # env_file_writable <name>
139
+ env_file_writable() {
140
+ local name=$1 repo
141
+ # A linked env file is often kept in a dotfiles repo. Replacing the link
142
+ # would cut it loose, and writing through it would put the key in that
143
+ # repo, so it is left alone, as trust.sh leaves a linked Claude Code file.
144
+ # (Anything else there that is not a file has stopped cli/ocnus already.)
145
+ [ ! -L "$ENV_FILE" ] ||
146
+ die "$ENV_FILE is a link, so ocnus left it alone and saved no key. Add the line $name=<your key> to the file it links to yourself, or put a plain file in its place and run this again."
147
+ # The same goes for a plain file in a git repo, where the key could be
148
+ # committed and pushed. GNU Stow, for one, links a whole folder into a
149
+ # dotfiles repo when it can, so ocnus's folder may lead into one.
150
+ if repo=$(git_repo_around "$ENT_HOME"); then
151
+ die "$ENT_HOME is in the git repo at $repo, where a saved key could be committed and pushed, so ocnus saved no key. Add the line $name=<your key> to $ENV_FILE yourself if that repo ignores the file, or move $ENT_HOME out of the repo and run this again."
152
+ fi
153
+ }
154
+
155
+ # Read a secret into SECRET: asked for without echo when stdin is a
156
+ # terminal, with the prompt $1 on stderr, or else read as the first line of
157
+ # stdin. Spaces and a line ending around it, which a pasted key often brings,
158
+ # are taken off. It is never an argument, so that it stays out of the
159
+ # shell's history and out of ps.
160
+ # read_secret <prompt>
161
+ SECRET=''
162
+ read_secret() {
163
+ local tty_was
164
+ SECRET=''
165
+ if [ -t 0 ]; then
166
+ printf '%s' "$1" >&2
167
+ # read -s turns the terminal's echo off, and Ctrl-C at the prompt would
168
+ # leave it off. Put the terminal back as it was, then stop as Ctrl-C would
169
+ # have, so that whatever ran this sees it end the same way.
170
+ tty_was=$(stty -g 2>/dev/null) || tty_was=
171
+ trap 'stty "$tty_was" 2>/dev/null || stty echo 2>/dev/null; printf "\n" >&2; trap - INT; kill -INT $$' INT
172
+ IFS= read -rs SECRET || true
173
+ trap - INT
174
+ printf '\n' >&2
175
+ else
176
+ IFS= read -r SECRET || true
177
+ fi
178
+ SECRET=${SECRET%$'\r'}
179
+ SECRET=${SECRET#"${SECRET%%[![:space:]]*}"}
180
+ SECRET=${SECRET%"${SECRET##*[![:space:]]}"}
181
+ }
182
+
183
+ # Write the line <name>=<value> into the env file, in place of every line
184
+ # the loader in cli/ocnus would read as that name, one that starts with
185
+ # the mark some editors put at the start of a file included. The file is the
186
+ # human's too: every other line of it is kept. It is written beside itself
187
+ # and moved into place whole, so a run cut short leaves the old file or the
188
+ # new one, never half of one, and it is made for its owner's eyes only.
189
+ # Fails, leaving the file as it was, when it cannot be written.
190
+ # env_file_put <name> <value>
191
+ env_file_put() {
192
+ local name=$1 value=$2 tmp
193
+ local line=$'^(\357\273\277)?[[:space:]]*(export[[:space:]]+)?'"$name"'[[:space:]]*='
194
+ mkdir -p "$ENT_HOME" 2>/dev/null || return 1
195
+ tmp=$ENV_FILE.ocnus.$$
196
+ # grep -a: a file with a NUL or another odd byte in it is still text here.
197
+ # Without it grep calls the file binary and prints a note in place of its
198
+ # lines. LC_ALL=C: macOS grep in a UTF-8 locale does not match that mark
199
+ # in such a file, so its old line would be kept. grep exits 1 when no line
200
+ # is left, which is fine; 2 is an error, and must not leave a file with
201
+ # only the new line in it.
202
+ if (umask 077
203
+ {
204
+ if [ -f "$ENV_FILE" ]; then
205
+ LC_ALL=C grep -a -v -E "$line" "$ENV_FILE"
206
+ [ "$?" -le 1 ]
207
+ fi &&
208
+ printf '%s=%s\n' "$name" "$value"
209
+ } > "$tmp") 2>/dev/null && mv -f "$tmp" "$ENV_FILE" 2>/dev/null; then
210
+ chmod 600 "$ENV_FILE" 2>/dev/null || true
211
+ return 0
212
+ fi
213
+ rm -f "$tmp" 2>/dev/null || true
214
+ return 1
215
+ }