@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,268 @@
1
+ # How the runs of ocnus's own tests on this machine use its processors.
2
+ # Sourced by cli/ocnus.
3
+
4
+ # `npm test`, which is test/run.sh, runs its cases side by side, and test
5
+ # runs going at the same time share the machine's processors between them.
6
+ # That is share, and it is the default. With queue, the runs go one at a
7
+ # time instead: a run that starts while another is going waits its turn, in
8
+ # the order the runs started, and then has every processor. The choice is
9
+ # "tests" in the settings file (settings.sh), "share" or "queue", and share
10
+ # when the file leaves it out. test/run.sh reads it with jq each time a run
11
+ # starts, so every run that starts after a change follows it.
12
+
13
+ #
14
+ # `ocnus tests run <command>` is the other half: it runs a repo's test
15
+ # command, any repo's, whole suite or part, and records the test run in the
16
+ # host's readings (host.sh) while recording is on. The build phase's skills
17
+ # run every test command through it.
18
+
19
+ # What `ocnus tests` takes after it.
20
+ TAKES_TESTS='share or queue, or nothing to say which is set (--json for programs), or run and a test command'
21
+ TESTS_RUN_USAGE='usage: ocnus tests run <command> [<arg>...], like: ocnus tests run npm test'
22
+
23
+ # ocnus tests [share|queue|--json]: say how test runs use the
24
+ # processors, or set it. --json prints it as one line for programs, with the
25
+ # file it is kept in, for a Herdr mod. share and queue write "tests" into the
26
+ # settings file as archive_set writes "archive": everything is checked before
27
+ # the file is touched, and the new file is checked before it takes the old
28
+ # one's place, so a refusal or a failure leaves the file as it was.
29
+ # ocnus tests run <command> runs a test command (tests_run).
30
+ cmd_tests() {
31
+ if [ "${1:-}" = run ]; then
32
+ shift
33
+ tests_run "$@"
34
+ fi
35
+ case $# in
36
+ 0) tests_show plain ;;
37
+ 1)
38
+ case $1 in
39
+ --json) tests_show json ;;
40
+ share | queue) tests_set "$1" ;;
41
+ *) die "'$1' is not share or queue. ocnus tests takes $TAKES_TESTS." ;;
42
+ esac
43
+ ;;
44
+ *) die "usage: ocnus tests [share|queue|--json]: it takes $TAKES_TESTS" ;;
45
+ esac
46
+ }
47
+
48
+ # Say which is set: plain, for a person, or json, for programs, as
49
+ # {"file":…,"tests":"share"}.
50
+ tests_show() {
51
+ need_jq
52
+ settings_check
53
+ if [ "$1" = json ]; then
54
+ printf '{"file":"%s","tests":"%s"}\n' "$(json_str "$SETTINGS")" "$SET_TESTS"
55
+ elif [ "$SET_TESTS" = queue ]; then
56
+ say "Test runs go one at a time: a run that starts while another is going waits its turn, in the order the runs started, then has every processor."
57
+ say "Have them share the processors with: ocnus tests share"
58
+ else
59
+ say "Test runs share the processors: runs going at the same time run one case per processor between them."
60
+ say "Have them go one at a time with: ocnus tests queue"
61
+ fi
62
+ }
63
+
64
+ # Write "tests": "share" or "queue" into the settings file. With no file yet,
65
+ # the new one starts from the defaults, so that it names every profile.
66
+ tests_set() {
67
+ local want=$1 tmp ok=0
68
+ need_jq
69
+ settings_check
70
+ mkdir -p "${SETTINGS%/*}"
71
+ tmp=$SETTINGS.ocnus.$$
72
+ if { if [ -e "$SETTINGS" ]; then cat "$SETTINGS"; else settings_default; fi; } |
73
+ jq --arg v "$want" '.tests = $v' > "$tmp" 2>/dev/null &&
74
+ [ "$(SETTINGS=$tmp; settings_load && printf '%s' "$SET_TESTS")" = "$want" ] &&
75
+ mv -f "$tmp" "$SETTINGS"; then
76
+ ok=1
77
+ fi
78
+ rm -f "$tmp"
79
+ [ "$ok" = 1 ] || die "could not change $SETTINGS, so it is as it was."
80
+ settings_check
81
+ if [ "$want" = queue ]; then
82
+ say "Test runs go one at a time now. The next one to start while another is going waits its turn."
83
+ else
84
+ say "Test runs share the processors now. The next one to start runs beside any that are going."
85
+ fi
86
+ }
87
+
88
+ # --- a test run ---------------------------------------------------------------
89
+
90
+ # ocnus tests run <command> [<arg>...]: run a test command as it is, and
91
+ # record it as a test run while recording the host is on. The command's
92
+ # output goes straight through, it gets this command's stdin, and this
93
+ # command exits as it did. A test run is told from any other command by
94
+ # being run through this, in ocnus's repo and in every other.
95
+ #
96
+ # It is recorded only while the host folder is there, with a card and a repo:
97
+ # the card OCNUS_CARD names, a card ID, of the space OCNUS_SPACE
98
+ # names, which `task build` gives its agent, and the repo of the folder it
99
+ # runs in, which has to be one of that space's. The env file cannot set
100
+ # either (load_env). Otherwise, and when sqlite3, perl or time is missing,
101
+ # it becomes the command, and records nothing.
102
+ #
103
+ # The run is the job `time -l -o <file> <command>`, macOS's /usr/bin/time,
104
+ # which writes into the file, in the host folder, how long the command took,
105
+ # the CPU it and everything it waited for used, and the largest process's
106
+ # peak memory: numbers only, whatever the run's length, so a run between two
107
+ # readings is there too. The job's row goes in test_runs as it starts, with
108
+ # its card, repo, start and the job's pid, and the fingerprint of its
109
+ # command, cmd: the first 15 hex digits of the SHA-256 of its arguments, each
110
+ # ended by a NUL, as a number. It is the same for the same command, so that
111
+ # `host report` can set runs of the same command in the same repo against
112
+ # each other, and the command's text cannot be read back from it. It is all
113
+ # that is kept of the command line. While the job runs, the sampler adds up
114
+ # the memory of its processes every second, and writes the most it saw to
115
+ # the row. When the job ends, the row gets its end and the figures, and what
116
+ # this command and the sampler used beside the job, watch_secs, which counts
117
+ # as recording's own CPU, the fingerprint's shasum included. On INT, TERM or
118
+ # HUP, the signal goes on to the command, the row gets its end at once, then
119
+ # its figures once the job has ended.
120
+ TESTS_JOB='' TESTS_ID='' TESTS_GOT=''
121
+ tests_run() {
122
+ local out started ended sampler='' code again figures secs cpu peak watch cmd
123
+ [ "$#" -gt 0 ] || die "$TESTS_RUN_USAGE"
124
+ tests_run_repo || exec "$@"
125
+ host_table >/dev/null 2>&1 || true
126
+ # time writes into this file. Made here, just before the job starts, so
127
+ # that with the host folder gone since, after `host off`, the command runs
128
+ # unrecorded.
129
+ out=$HOST_DIR/run.$$
130
+ { : > "$out"; } 2>/dev/null || exec "$@"
131
+ # The fingerprint, worked out in this shell's child, so that its CPU counts
132
+ # in watch_secs. Without shasum it is empty, and the run is still recorded.
133
+ cmd=$(printf '%s\0' "$@" | /usr/bin/shasum -a 256 2>/dev/null) || cmd=
134
+ cmd=${cmd:0:15}
135
+ case $cmd in
136
+ *[!0-9a-f]*) cmd=NULL ;;
137
+ ???????????????) cmd=$((16#$cmd)) ;;
138
+ *) cmd=NULL ;;
139
+ esac
140
+ started=$(date +%s)
141
+ trap 'tests_run_pass INT' INT
142
+ trap 'tests_run_pass TERM' TERM
143
+ trap 'tests_run_pass HUP' HUP
144
+ # bash ignores Ctrl-C and Ctrl-\ in what it starts in the background, and
145
+ # cannot undo that for it, so perl puts them back as they are for a command
146
+ # run in the foreground, then runs time. The job is in this command's
147
+ # process group, so a Ctrl-C in its terminal, or a kill of the group,
148
+ # reaches it as it would reach the command run alone. <&0: bash would
149
+ # otherwise give the job no stdin.
150
+ perl -e '$SIG{INT} = $SIG{QUIT} = "DEFAULT"; exec { $ARGV[0] } @ARGV or do { print STDERR "ocnus: $ARGV[0]: $!\n"; exit 127 }' \
151
+ time -l -o "$out" "$@" <&0 &
152
+ TESTS_JOB=$!
153
+ TESTS_ID=$(store "$HOST_DB" "insert into test_runs (card, repo, started, pid, cmd)
154
+ values ($(sql_str "$TESTS_CARD"), $(sql_str "$TESTS_REPO"), $started, $TESTS_JOB, $cmd); select last_insert_rowid();") || TESTS_ID=
155
+ case $TESTS_ID in *[!0-9]*) TESTS_ID= ;; esac
156
+ if [ -n "$TESTS_ID" ]; then
157
+ tests_run_watch "$TESTS_JOB" "$TESTS_ID" &
158
+ sampler=$!
159
+ fi
160
+ # A signal cuts the wait short, and went on to the command: wait again,
161
+ # which gives how the job ended once it has. 127 says the job was waited
162
+ # for already, and code holds how it ended.
163
+ code=0
164
+ wait "$TESTS_JOB" || code=$?
165
+ while [ -n "$TESTS_GOT" ]; do
166
+ TESTS_GOT=
167
+ again=0
168
+ wait "$TESTS_JOB" 2>/dev/null || again=$?
169
+ [ "$again" = 127 ] || code=$again
170
+ done
171
+ if [ -n "$sampler" ]; then
172
+ kill "$sampler" 2>/dev/null || true
173
+ wait "$sampler" 2>/dev/null || true
174
+ fi
175
+ if [ -n "$TESTS_ID" ]; then
176
+ # time's figures: seconds, CPU seconds and peak MB, each - when it wrote
177
+ # none, as when it was stopped too.
178
+ figures=$(LC_ALL=C awk '
179
+ function isnum(v) { return v ~ /^[0-9]+(\.[0-9]+)?$/ }
180
+ $2 == "real" && $4 == "user" && $6 == "sys" && isnum($1) && isnum($3) && isnum($5) { real = $1; cpu = $3 + $5; got = 1 }
181
+ / maximum resident set size$/ && $1 ~ /^[0-9]+$/ { peak = $1 }
182
+ END {
183
+ if (got) printf "%.2f %.2f", real, cpu; else printf "- -"
184
+ if (peak != "") printf " %.0f\n", peak / 1048576; else print " -"
185
+ }' "$out" 2>/dev/null) || figures=
186
+ read -r secs cpu peak <<< "$figures"
187
+ # This shell's own CPU, then that of all it has run and waited for: the
188
+ # job, the sampler and every tool they ran. A builtin, so not in a
189
+ # subshell, which would count only its own.
190
+ watch=
191
+ if times > "$out" 2>/dev/null; then
192
+ watch=$(LC_ALL=C awk -v job="${cpu:--}" '
193
+ function took(t, m, s) { m = t; sub(/m.*/, "", m); s = t; sub(/^[0-9]*m/, "", s); sub(/s$/, "", s); return m * 60 + s }
194
+ { for (i = 1; i <= NF; i++) if ($i ~ /^[0-9]+m[0-9.]+s$/) { all += took($i); got = 1 } }
195
+ END { if (!got) exit; w = all - (job == "-" ? 0 : job); printf "%.2f\n", (w > 0 ? w : 0) }' "$out" 2>/dev/null) || watch=
196
+ fi
197
+ [ -n "$secs" ] && [ "$secs" != - ] || secs=NULL
198
+ [ -n "$cpu" ] && [ "$cpu" != - ] || cpu=NULL
199
+ [ -n "$peak" ] && [ "$peak" != - ] || peak=NULL
200
+ ended=$(date +%s)
201
+ store "$HOST_DB" "update test_runs set ended = $ended, secs = coalesce($secs, $ended - started),
202
+ cpu_secs = $cpu, watch_secs = ${watch:-NULL}, cut = 0,
203
+ mem_mb = case when $peak is null then mem_mb when mem_mb is null or mem_mb < $peak then $peak else mem_mb end
204
+ where id = $TESTS_ID;" || true
205
+ fi
206
+ rm -f "$out"
207
+ exit "$code"
208
+ }
209
+
210
+ # Set TESTS_CARD and TESTS_REPO to the card and repo a test run run here is
211
+ # recorded as, or fail when it is not to be recorded (tests_run). The repo is
212
+ # the folder holding the git common dir of the folder this runs in, which is
213
+ # the same for the repo's checkout and every worktree of it, the task's or
214
+ # one made to check a test on the default branch. It is named as the
215
+ # space's links name it, by its folder's name.
216
+ TESTS_CARD='' TESTS_REPO=''
217
+ tests_run_repo() {
218
+ local space=${OCNUS_SPACE:-} card=${OCNUS_CARD:-} common top line real
219
+ [ -d "$HOST_DIR" ] || return 1
220
+ [[ $card =~ $CARD_RE ]] || return 1
221
+ valid_name "$space" && [ -d "$SPACES/$space" ] || return 1
222
+ have_sqlite || return 1
223
+ type -P perl >/dev/null 2>&1 && type -P time >/dev/null 2>&1 || return 1
224
+ common=$(git rev-parse --git-common-dir 2>/dev/null) || return 1
225
+ [ -n "$common" ] || return 1
226
+ case $common in /*) ;; *) common=$PWD/$common ;; esac
227
+ top=$(cd "$common/.." 2>/dev/null && pwd -P) || return 1
228
+ while IFS= read -r line; do
229
+ real=$(cd "$line" 2>/dev/null && pwd -P) || continue
230
+ [ "$real" = "$top" ] || continue
231
+ line=${line%/}
232
+ TESTS_REPO=${line##*/}
233
+ TESTS_CARD=$(printf '%s' "$card" | tr '[:lower:]' '[:upper:]')
234
+ return 0
235
+ done < <(space_repos "$SPACES/$space")
236
+ return 1
237
+ }
238
+
239
+ # The trap for a signal, $1: send it on to the command, the job's child, as
240
+ # a terminal would, and give the row its end now, in case this command is
241
+ # killed next. time itself ignores INT, and would leave its figures unwritten
242
+ # if TERM or HUP stopped it, so it is not sent one.
243
+ tests_run_pass() {
244
+ TESTS_GOT=$1
245
+ [ -z "$TESTS_JOB" ] || pkill -"$1" -P "$TESTS_JOB" 2>/dev/null || true
246
+ [ -z "$TESTS_ID" ] ||
247
+ store "$HOST_DB" "update test_runs set ended = $(date +%s) where id = $TESTS_ID and ended is null;" || true
248
+ }
249
+
250
+ # The sampler, run in the background: while the job $1 runs, it adds up the
251
+ # RSS of the job and every process under it, from ps every second, with the
252
+ # walk host_agents does (HOST_WALK), and writes the most it has seen to the
253
+ # row $2 whenever that grows. A ps line that does not start with three
254
+ # numbers is skipped, and nothing but those numbers is read.
255
+ tests_run_watch() {
256
+ local job=$1 id=$2 top=0 now
257
+ while kill -0 "$job" 2>/dev/null; do
258
+ now=$(ps -A -o pid=,ppid=,rss= 2>/dev/null | LC_ALL=C awk -v root="$job" "$HOST_WALK"'
259
+ $1 ~ /^[0-9]+$/ && $2 ~ /^[0-9]+$/ && $3 ~ /^[0-9]+$/ { there[$1] = 1; kids[$2] = kids[$2] " " $1; rss[$1] = $3 }
260
+ END { p[1] = root; q = walk(1, p); for (h = 0; h < q; h++) kb += rss[procs[h]]; printf "%.0f\n", kb / 1024 }') || now=
261
+ case $now in '' | *[!0-9]*) now=0 ;; esac
262
+ if [ "$now" -gt "$top" ]; then
263
+ top=$now
264
+ store "$HOST_DB" "update test_runs set mem_mb = $top where id = $id and (mem_mb is null or mem_mb < $top);" || true
265
+ fi
266
+ sleep 1
267
+ done
268
+ }
@@ -0,0 +1,70 @@
1
+ # Folders Claude Code starts in without asking. Sourced by cli/ocnus.
2
+
3
+ # Claude Code asks "do you trust this folder?" the first time it starts in one,
4
+ # and keeps the answer in its own file, $CLAUDE_STATE, as
5
+ # .projects["<the folder's real path>"].hasTrustDialogAccepted
6
+ # A folder cannot answer for itself. ocnus answers for the folders it made
7
+ # itself (a space's folder), so that an agent `task new` starts there is not
8
+ # left waiting on the question. It never answers for a repo.
9
+ #
10
+ # The file is Claude Code's. It holds much more than this, and every running
11
+ # session rewrites it. So this is the only place that writes it, it changes one
12
+ # value, and what it cannot do cleanly it leaves undone and says so.
13
+
14
+ # The jq test for "this folder is trusted". $d is the folder.
15
+ TRUST_TEST='.projects[$d].hasTrustDialogAccepted == true'
16
+
17
+ # Whether a state file ($2) says a folder ($1) is trusted. The answer is read
18
+ # from what jq prints, never from its exit code: jq 1.6 exits zero for an
19
+ # empty file even with -e. One "true" and nothing else is a yes, so a file
20
+ # holding two values where there should be one is a no as well.
21
+ trust_recorded() { [ "$(jq --arg d "$1" "$TRUST_TEST" "$2" 2>/dev/null)" = true ]; }
22
+
23
+ # "may ask": not every case below can tell whether the folder is trusted.
24
+ trust_note() { say " note Claude Code may ask to trust $1: $2"; }
25
+
26
+ # Record a folder as trusted. The folder is a real path, as space_dir prints
27
+ # it, because that is what Claude Code looks up. Always returns 0: a space or
28
+ # a task is never held up by this, so every command here that can fail is
29
+ # guarded.
30
+ trust_folder() {
31
+ local dir=$1 file=$CLAUDE_STATE lock tmp tries=0 ok=0
32
+ lock=$file.lock
33
+ tmp=$file.ocnus.$$
34
+ if [ ! -e "$file" ] && [ ! -L "$file" ]; then trust_note "$dir" "there is no $file yet"; return 0; fi
35
+ if ! command -v jq >/dev/null 2>&1; then trust_note "$dir" "jq is not installed"; return 0; fi
36
+ # Reading is harmless, through a link too, so this comes before anything
37
+ # that refuses to write.
38
+ if trust_recorded "$dir" "$file"; then return 0; fi
39
+ # Replacing a link with a plain file would break whatever keeps the real
40
+ # file somewhere else.
41
+ if [ -L "$file" ]; then trust_note "$dir" "$file is a link, so it was left alone"; return 0; fi
42
+
43
+ # Claude Code's own lock: a folder beside the file, held while it writes. It
44
+ # clears a lock that nobody refreshes, so a run killed in here does no
45
+ # lasting harm. A lock someone else holds is waited for, never removed.
46
+ until mkdir "$lock" 2>/dev/null; do
47
+ tries=$((tries + 1))
48
+ if [ "$tries" -ge 10 ]; then trust_note "$dir" "$file is in use, so it was left alone"; return 0; fi
49
+ sleep 0.1 || true
50
+ done
51
+ # The new file is made for its owner's eyes only, as Claude Code makes it.
52
+ # It is checked before it replaces the old one: jq prints nothing for an
53
+ # empty file and still exits zero.
54
+ if (umask 077 && jq --arg d "$dir" \
55
+ 'if type == "object" then .projects[$d].hasTrustDialogAccepted = true else error("not an object") end' \
56
+ "$file" > "$tmp") 2>/dev/null &&
57
+ trust_recorded "$dir" "$tmp" &&
58
+ mv -f "$tmp" "$file" 2>/dev/null; then
59
+ ok=1
60
+ fi
61
+ rm -f "$tmp" 2>/dev/null || true
62
+ rmdir "$lock" 2>/dev/null || true
63
+
64
+ if [ "$ok" = 1 ]; then
65
+ say " trusted $dir (Claude Code will not ask to trust it)"
66
+ else
67
+ trust_note "$dir" "$file could not be updated, so it was left alone"
68
+ fi
69
+ return 0
70
+ }