@krysoworks/kryso 0.0.2 → 2.0.0-rc.1

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 (163) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +146 -6
  3. package/bin/kryso +31 -0
  4. package/bin/ship-it-status +14 -0
  5. package/bin/ship-ledger +14 -0
  6. package/bin/ship-loop +14 -0
  7. package/bin/ship-pass +14 -0
  8. package/bin/warden +14 -0
  9. package/dist/answer.d.ts +38 -0
  10. package/dist/answer.js +48 -0
  11. package/dist/bytes.d.ts +5 -0
  12. package/dist/bytes.js +35 -0
  13. package/dist/cleanup.d.ts +1 -0
  14. package/dist/cleanup.js +629 -0
  15. package/dist/commands.d.ts +48 -0
  16. package/dist/commands.js +293 -0
  17. package/dist/completions.d.ts +24 -0
  18. package/dist/completions.js +239 -0
  19. package/dist/daemon.d.ts +15 -0
  20. package/dist/daemon.js +23 -0
  21. package/dist/diagnose.d.ts +47 -0
  22. package/dist/diagnose.js +314 -0
  23. package/dist/doctor.d.ts +1 -0
  24. package/dist/doctor.js +241 -0
  25. package/dist/env.d.ts +9 -0
  26. package/dist/env.js +17 -0
  27. package/dist/evidence.d.ts +39 -0
  28. package/dist/evidence.js +327 -0
  29. package/dist/home.d.ts +3 -0
  30. package/dist/home.js +27 -0
  31. package/dist/install.d.ts +59 -0
  32. package/dist/install.js +137 -0
  33. package/dist/isolate.d.ts +28 -0
  34. package/dist/isolate.js +402 -0
  35. package/dist/leftovers.d.ts +5 -0
  36. package/dist/leftovers.js +156 -0
  37. package/dist/machine.d.ts +6 -0
  38. package/dist/machine.js +185 -0
  39. package/dist/palette.d.ts +147 -0
  40. package/dist/palette.js +167 -0
  41. package/dist/plan.d.ts +1 -0
  42. package/dist/plan.js +133 -0
  43. package/dist/proc.d.ts +23 -0
  44. package/dist/proc.js +23 -0
  45. package/dist/read.d.ts +1 -0
  46. package/dist/read.js +297 -0
  47. package/dist/readiness.d.ts +5 -0
  48. package/dist/readiness.js +64 -0
  49. package/dist/remote.d.ts +6 -0
  50. package/dist/remote.js +85 -0
  51. package/dist/self-update.d.ts +1 -0
  52. package/dist/self-update.js +224 -0
  53. package/dist/semver.d.ts +7 -0
  54. package/dist/semver.js +42 -0
  55. package/dist/style.d.ts +71 -0
  56. package/dist/style.js +187 -0
  57. package/dist/update.d.ts +47 -0
  58. package/dist/update.js +190 -0
  59. package/dist/work.d.ts +25 -0
  60. package/dist/work.js +449 -0
  61. package/dist/workflows.d.ts +4 -0
  62. package/dist/workflows.js +73 -0
  63. package/docker/entrypoint.sh +44 -0
  64. package/docker/runner.Dockerfile +39 -0
  65. package/package.json +55 -22
  66. package/scripts/install-skills.sh +607 -0
  67. package/skills/code-review/SKILL.md +231 -0
  68. package/skills/kryso/SKILL.md +107 -0
  69. package/skills/kryso/golden/help.rounded.ansi +23 -0
  70. package/skills/kryso/golden/help.square.ansi +23 -0
  71. package/skills/kryso/golden/help.txt +23 -0
  72. package/skills/kryso/golden/table-narrow.json +11 -0
  73. package/skills/kryso/golden/table-narrow.rounded.ansi +4 -0
  74. package/skills/kryso/golden/table-narrow.square.ansi +4 -0
  75. package/skills/kryso/golden/table.ansi +5 -0
  76. package/skills/kryso/golden/table.json +11 -0
  77. package/skills/kryso/ledger.sh +2146 -0
  78. package/skills/kryso/palette.json +57 -0
  79. package/skills/kryso/reference/resuming.md +30 -0
  80. package/skills/kryso/reference/routing.md +23 -0
  81. package/skills/kryso/reference/stage-1-claim.md +79 -0
  82. package/skills/kryso/reference/stage-2-build.md +133 -0
  83. package/skills/kryso/reference/stage-3-prove.md +51 -0
  84. package/skills/kryso/reference/stage-4-pr.md +76 -0
  85. package/skills/kryso/reference/stage-5-reconcile.md +81 -0
  86. package/skills/kryso/reference/stage-6-merge-or-park.md +73 -0
  87. package/skills/kryso/reference/stage-7-close-out.md +62 -0
  88. package/skills/kryso/reference/terminal.md +7 -0
  89. package/skills/kryso/ship-it-status.sh +1503 -0
  90. package/skills/kryso/ui.py +274 -0
  91. package/skills/kryso/ui.sh +609 -0
  92. package/skills/kryso-build/SKILL.md +168 -0
  93. package/skills/kryso-shift/SKILL.md +167 -0
  94. package/skills/kryso-shift/archive.py +2784 -0
  95. package/skills/kryso-shift/archive_checker.py +462 -0
  96. package/skills/kryso-shift/archive_checker_lambda.py +155 -0
  97. package/skills/kryso-shift/archive_health.py +364 -0
  98. package/skills/kryso-shift/archive_launchd.py +140 -0
  99. package/skills/kryso-shift/archive_remote.py +990 -0
  100. package/skills/kryso-shift/archiving-checker.md +140 -0
  101. package/skills/kryso-shift/archiving-keep.md +115 -0
  102. package/skills/kryso-shift/archiving-recovery.md +112 -0
  103. package/skills/kryso-shift/archiving-remote.md +166 -0
  104. package/skills/kryso-shift/archiving.md +146 -0
  105. package/skills/kryso-shift/coverage.schema.json +225 -0
  106. package/skills/kryso-shift/deadline.sh +254 -0
  107. package/skills/kryso-shift/feedback.schema.json +35 -0
  108. package/skills/kryso-shift/feedback.sh +228 -0
  109. package/skills/kryso-shift/history-manifest.schema.json +331 -0
  110. package/skills/kryso-shift/keep-after.sh +55 -0
  111. package/skills/kryso-shift/measure.sh +611 -0
  112. package/skills/kryso-shift/merge-ready.sh +207 -0
  113. package/skills/kryso-shift/outcome.schema.json +16 -0
  114. package/skills/kryso-shift/pass-rules.md +8 -0
  115. package/skills/kryso-shift/profile.schema.json +42 -0
  116. package/skills/kryso-shift/reader.sh +692 -0
  117. package/skills/kryso-shift/reads.sh +151 -0
  118. package/skills/kryso-shift/reference/allowance.md +21 -0
  119. package/skills/kryso-shift/reference/driver.md +95 -0
  120. package/skills/kryso-shift/reference/measuring.md +67 -0
  121. package/skills/kryso-shift/reference/outputs.md +21 -0
  122. package/skills/kryso-shift/reference/pass-script.md +115 -0
  123. package/skills/kryso-shift/reference/reconcile-and-merge.md +55 -0
  124. package/skills/kryso-shift/reference/second-reader.md +9 -0
  125. package/skills/kryso-shift/reference/sweeps.md +83 -0
  126. package/skills/kryso-shift/repair.schema.json +14 -0
  127. package/skills/kryso-shift/report-template.html +679 -0
  128. package/skills/kryso-shift/report.py +389 -0
  129. package/skills/kryso-shift/report_fixture.py +65 -0
  130. package/skills/kryso-shift/reporting.md +58 -0
  131. package/skills/kryso-shift/result.sh +91 -0
  132. package/skills/kryso-shift/runner.sh +1178 -0
  133. package/skills/kryso-shift/ship-loop.sh +3977 -0
  134. package/skills/kryso-shift/ship-pass.sh +4987 -0
  135. package/skills/kryso-shift/steps.jq +19 -0
  136. package/skills/kryso-shift/workflow.mjs +830 -0
  137. package/skills/kryso-shift/workflows/default.json +22 -0
  138. package/skills/kryso-shift/workflows/ralph.json +13 -0
  139. package/skills/kryso-watch/SKILL.md +100 -0
  140. package/skills/kryso-watch/answer.schema.json +23 -0
  141. package/skills/kryso-watch/base-refs.sh +44 -0
  142. package/skills/kryso-watch/board.sh +58 -0
  143. package/skills/kryso-watch/reference/config.md +23 -0
  144. package/skills/kryso-watch/reference/outputs.md +30 -0
  145. package/skills/kryso-watch/reference/report.md +29 -0
  146. package/skills/kryso-watch/reference/scheduler.md +32 -0
  147. package/skills/kryso-watch/reference/settlement.md +29 -0
  148. package/skills/kryso-watch/reference/shift.md +44 -0
  149. package/skills/kryso-watch/reference/shim-limits.md +22 -0
  150. package/skills/kryso-watch/reference/triage.md +26 -0
  151. package/skills/kryso-watch/schema.jq +40 -0
  152. package/skills/kryso-watch/settle.schema.json +36 -0
  153. package/skills/kryso-watch/stage-common.sh +187 -0
  154. package/skills/kryso-watch/stages/report.sh +525 -0
  155. package/skills/kryso-watch/stages/settle.sh +722 -0
  156. package/skills/kryso-watch/stages/shift.sh +466 -0
  157. package/skills/kryso-watch/stages/triage.sh +379 -0
  158. package/skills/kryso-watch/tracker.sh +27 -0
  159. package/skills/kryso-watch/triage.schema.json +77 -0
  160. package/skills/kryso-watch/warden.launchd.plist +50 -0
  161. package/skills/kryso-watch/warden.schema.json +98 -0
  162. package/skills/kryso-watch/warden.sh +1004 -0
  163. package/bin/kryso.js +0 -11
@@ -0,0 +1,1178 @@
1
+ #!/usr/bin/env bash
2
+ # The model-runner seam for ship-loop. Sourced, never run. A caller selects one profile,
3
+ # validates it, asks for argv, and normalizes the host result through this file; provider
4
+ # authentication, billing controls, permission flags and output shapes stay here.
5
+
6
+ RUNNER_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
7
+ RUNNER_PROFILE_SCHEMA="$RUNNER_DIR/profile.schema.json"
8
+
9
+ runner_profile_file_valid() {
10
+ local file="$1"
11
+ [[ -f "$RUNNER_PROFILE_SCHEMA" ]] || {
12
+ echo "runner: no profile schema at $RUNNER_PROFILE_SCHEMA" >&2
13
+ return 1
14
+ }
15
+ jq -e --slurpfile schema "$RUNNER_PROFILE_SCHEMA" '
16
+ def type_ok($value; $rule):
17
+ (($rule.type? // null) as $want | $want == null or ($value | type) == $want);
18
+ def scalar_ok($value; $rule):
19
+ type_ok($value; $rule)
20
+ and (($rule.minLength? // null) as $min | $min == null or ($value | length) >= $min)
21
+ and (if ($rule | has("enum")) then any($rule.enum[]; . == $value) else true end);
22
+ def required_ok($value; $rule):
23
+ (($rule.required // []) - (if ($value | type) == "object" then ($value | keys) else [] end) | length) == 0;
24
+ def extras_ok($value; $rule):
25
+ if $rule.additionalProperties == false then
26
+ ((($value | keys) - ($rule.properties | keys)) | length) == 0
27
+ else true end;
28
+
29
+ . as $data
30
+ | $schema[0] as $root
31
+ | $root.properties.profiles as $profiles_rule
32
+ | $profiles_rule.additionalProperties as $profile_rule
33
+ | type_ok($data; $root)
34
+ and required_ok($data; $root)
35
+ and extras_ok($data; $root)
36
+ and scalar_ok($data.profiles; $profiles_rule)
37
+ and ([ $data.profiles | to_entries[] |
38
+ . as $entry
39
+ | $entry.value as $profile
40
+ | ([ $profile_rule.allOf[]
41
+ | select(.if.properties.host.const == $profile.host)
42
+ | .then.properties.effort ][0] // {}) as $host_effort_rule
43
+ | scalar_ok($entry.key; $profiles_rule.propertyNames)
44
+ and type_ok($profile; $profile_rule)
45
+ and required_ok($profile; $profile_rule)
46
+ and extras_ok($profile; $profile_rule)
47
+ and ([ $profile_rule.properties | to_entries[] |
48
+ . as $property
49
+ | if ($profile | has($property.key))
50
+ then scalar_ok($profile[$property.key]; $property.value)
51
+ else true end
52
+ ] | all)
53
+ and (if ($profile | has("effort"))
54
+ then scalar_ok($profile.effort; $host_effort_rule)
55
+ else true end)
56
+ ] | all)
57
+ ' "$file" >/dev/null 2>&1 || {
58
+ echo "runner: profile file '$file' does not match $RUNNER_PROFILE_SCHEMA" >&2
59
+ return 1
60
+ }
61
+ }
62
+
63
+ runner_profile_load() {
64
+ local name="${1:-claude}" file="${2:-}" host="" model="" effort="high"
65
+ case "$name" in
66
+ # Pinned, so a change of the interactive default does not reprice every night: from
67
+ # 2026-09-27 to 2026-10-19, 80 passes inherited Fable 5.1 at 2.5 times Opus 5.5's prices.
68
+ claude) host=claude; model=claude-opus-5-5 ;;
69
+ codex) host=codex ;;
70
+ *)
71
+ if [[ -n "$file" && -f "$file" ]] && runner_profile_file_valid "$file"; then
72
+ host="$(jq -r --arg n "$name" '.profiles[$n].host // empty' "$file" 2>/dev/null)"
73
+ model="$(jq -r --arg n "$name" '.profiles[$n].model // empty' "$file" 2>/dev/null)"
74
+ effort="$(jq -r --arg n "$name" '.profiles[$n].effort // "high"' "$file" 2>/dev/null)"
75
+ elif [[ -n "$file" && -f "$file" ]]; then
76
+ return 1
77
+ fi
78
+ if [[ -z "$host" ]]; then
79
+ echo "runner: unknown profile '$name'" >&2
80
+ return 1
81
+ fi
82
+ ;;
83
+ esac
84
+
85
+ case "$host" in
86
+ claude) RUNNER_BILLING=subscription ;;
87
+ codex) RUNNER_BILLING=chatgpt-subscription ;;
88
+ *)
89
+ echo "runner: profile '$name' names unsupported host '$host'" >&2
90
+ return 1
91
+ ;;
92
+ esac
93
+ RUNNER_PROFILE="$name"
94
+ RUNNER_HOST="$host"
95
+ RUNNER_MODEL="$model"
96
+ RUNNER_EFFORT="$effort"
97
+ }
98
+
99
+ # The most processes a context the pass starts may have, as `ulimit -u` counts them: every
100
+ # process of the user, not only the context's own. A context that forks without end then gets
101
+ # failed forks while the user's other shells keep the rest. Why: reference/driver.md, #451.
102
+ RUNNER_PROCESSES_DEFAULT=2048
103
+
104
+ # runner_process_limit: sets RUNNER_PROCESS_LIMIT to the limit a context starts under, and
105
+ # RUNNER_PROCESS_LIMIT_FROM to where it came from. KRYSO_PROCESSES, a whole number of at least
106
+ # 64, wins. Without it the limit is the default, at most half the user's hard limit so the
107
+ # default stays well below it. Neither goes above the hard limit, which an unprivileged process
108
+ # cannot raise. The profile cannot name it yet, because profile.schema.json is frozen.
109
+ runner_process_limit() {
110
+ local hard want from given="${KRYSO_PROCESSES:-}"
111
+ hard="$(ulimit -H -u)"
112
+ if [[ "$given" =~ ^[0-9]+$ ]] && (( 10#$given >= 64 )); then
113
+ want=$(( 10#$given ))
114
+ from=KRYSO_PROCESSES
115
+ else
116
+ want="$RUNNER_PROCESSES_DEFAULT"
117
+ from=default
118
+ [[ -z "$given" ]] || from="default, because KRYSO_PROCESSES=$given is not a whole number of at least 64"
119
+ if [[ "$hard" != unlimited ]] && (( hard / 2 < want )); then
120
+ want=$(( hard / 2 ))
121
+ from="$from, half the hard limit of $hard"
122
+ fi
123
+ fi
124
+ if [[ "$hard" != unlimited ]] && (( want > hard )); then
125
+ want="$hard"
126
+ from="the hard limit, below KRYSO_PROCESSES"
127
+ fi
128
+ RUNNER_PROCESS_LIMIT="$want"
129
+ RUNNER_PROCESS_LIMIT_FROM="$from"
130
+ }
131
+
132
+ # _runner_limited <command...>: the command in a subshell that first sets RUNNER_PROCESS_LIMIT
133
+ # as both the soft and the hard limit, so nothing the context starts can raise it again. A
134
+ # limit that cannot be set stops the context before the host starts.
135
+ _runner_limited() {
136
+ ( ulimit -u "$RUNNER_PROCESS_LIMIT" || exit 125; exec "$@" )
137
+ }
138
+
139
+ # runner_process_limit_hit <stderr> <result>: prints the first line of the host's stderr or its
140
+ # result that says a fork failed for want of a process slot, which is how a context that reached
141
+ # its limit ends, and returns 1 when there is none.
142
+ runner_process_limit_hit() {
143
+ local text
144
+ text="$(cat "$1" 2>/dev/null; jq -r '.result // empty' "$2" 2>/dev/null)"
145
+ grep -Ei -m1 '(fork|spawn).*(resource temporarily unavailable|EAGAIN)|fork failed' <<< "$text"
146
+ }
147
+
148
+ runner_validate_options() {
149
+ local budget_set="$1" budget="$2" keep_api_key="$3" effort="${4:-$RUNNER_EFFORT}"
150
+ if [[ "$RUNNER_HOST" == codex && "$budget_set" -eq 1 ]]; then
151
+ echo "runner: profile '$RUNNER_PROFILE' takes no --budget, because a ChatGPT subscription run reports usage and has no dollar cap" >&2
152
+ return 1
153
+ fi
154
+ if [[ "$RUNNER_HOST" == codex && "$keep_api_key" -eq 1 ]]; then
155
+ echo "runner: profile '$RUNNER_PROFILE' takes no --api-key, because it signs in with a ChatGPT subscription" >&2
156
+ return 1
157
+ fi
158
+ [[ "$budget" =~ ^[0-9]+(\.[0-9]+)?$ ]] || {
159
+ echo "runner: --budget must be a whole number or a decimal in dollars, got '$budget'" >&2
160
+ return 1
161
+ }
162
+ jq -e --arg host "$RUNNER_HOST" --arg effort "$effort" '
163
+ [.properties.profiles.additionalProperties.allOf[]
164
+ | select(.if.properties.host.const == $host)
165
+ | .then.properties.effort.enum[]]
166
+ | index($effort) != null
167
+ ' "$RUNNER_PROFILE_SCHEMA" >/dev/null 2>&1 || {
168
+ echo "runner: host '$RUNNER_HOST' has no effort '$effort'" >&2
169
+ return 1
170
+ }
171
+ if [[ "$RUNNER_HOST" == claude && "$keep_api_key" -eq 1 ]]; then
172
+ RUNNER_BILLING=api
173
+ elif [[ "$RUNNER_HOST" == claude ]]; then
174
+ RUNNER_BILLING=subscription
175
+ fi
176
+ }
177
+
178
+ runner_validate_extra_args() {
179
+ if [[ "$RUNNER_HOST" != codex ]]; then
180
+ # The host reads the last --settings alone, so a second one would replace the allowlist that
181
+ # passMcp's list form keeps or the overrides that limit a pass's skills. Where neither is
182
+ # set, a passthrough --settings is the person's own choice, and it replaces the lean
183
+ # settings too, which ADR 0056 records.
184
+ [[ "${RUNNER_PASS_MCP:-on}" == keep || "${RUNNER_PASS_SKILLS:-all}" != all ]] || return 0
185
+ local given
186
+ for given in "$@"; do
187
+ case "$given" in
188
+ --settings|--settings=*)
189
+ if [[ "${RUNNER_PASS_MCP:-on}" == keep ]]; then
190
+ echo "runner: passthrough option '--settings' is not allowed while ${RUNNER_PASS_MCP_NAME:-kryso.passMcp} names the servers to keep, since it could replace their allowlist" >&2
191
+ else
192
+ echo "runner: passthrough option '--settings' is not allowed while a pass lists only some skills, since it would replace their overrides. Set skills to all in .kryso/config.json to pass your own" >&2
193
+ fi
194
+ return 1
195
+ ;;
196
+ esac
197
+ done
198
+ return 0
199
+ fi
200
+ local arg expect_value=0
201
+ for arg in "$@"; do
202
+ if [[ "$expect_value" -eq 1 ]]; then
203
+ expect_value=0
204
+ continue
205
+ fi
206
+ case "$arg" in
207
+ --color|--thread-source|--image|-i) expect_value=1 ;;
208
+ --color=*|--thread-source=*|--image=*|--skip-git-repo-check|--strict-config) ;;
209
+ *)
210
+ echo "runner: Codex takes no passthrough option '$arg', because the profile sets the provider, authentication, sandbox, model, effort, output and session" >&2
211
+ return 1
212
+ ;;
213
+ esac
214
+ done
215
+ [[ "$expect_value" -eq 0 ]] || {
216
+ echo "runner: Codex passthrough option '$arg' needs a value" >&2
217
+ return 1
218
+ }
219
+ }
220
+
221
+ # runner_skill_ref <skill>: how this host invokes a skill. Codex takes `$skill`. Claude takes
222
+ # `/skill` for a skill installed on its own, in the repository's .claude/skills or the user's, and
223
+ # `/kryso:skill` when it comes only from the kryso plugin, which namespaces its skills. With
224
+ # neither, it is `/skill`, and the host says the skill is missing.
225
+ runner_skill_ref() {
226
+ local skill="$1" config="${CLAUDE_CONFIG_DIR:-$HOME/.claude}" dir
227
+ if [[ "$RUNNER_HOST" == codex ]]; then printf '$%s' "$skill"; return 0; fi
228
+ for dir in "${REPO_ROOT:-$PWD}/.claude/skills" "$config/skills"; do
229
+ [[ -f "$dir/$skill/SKILL.md" ]] && { printf '/%s' "$skill"; return 0; }
230
+ done
231
+ _runner_plugin_has kryso "$skill" "$config" && { printf '/kryso:%s' "$skill"; return 0; }
232
+ printf '/%s' "$skill"
233
+ }
234
+
235
+ # _runner_plugin_has <plugin> <skill> <config>: status 0 when an install of the plugin that this
236
+ # repository loads carries the skill.
237
+ _runner_plugin_has() {
238
+ [[ -n "$(_runner_plugin_installs "$1" "$2" "$3")" ]]
239
+ }
240
+
241
+ # _runner_plugin_installs <plugin> <skill> <config> [repo]: the directory of each install of the
242
+ # plugin that this repository loads and that carries the skill, one per line. An install loads
243
+ # when it is at user or managed scope, or at project or local scope for this repository, and no
244
+ # settings file sets the plugin to false in enabledPlugins. The skill is read from the
245
+ # install's own skills/ directory, so a skill the plugin does not ship is never found there.
246
+ # The repository defaults to REPO_ROOT, else the current directory.
247
+ _runner_plugin_installs() {
248
+ local plugin="$1" skill="$2" config="$3" raw="${4:-${REPO_ROOT:-$PWD}}" repo path settings
249
+ repo="$(cd "$raw" 2>/dev/null && pwd -P)"
250
+ for settings in "$config/settings.json" "$raw/.claude/settings.json" "$raw/.claude/settings.local.json"; do
251
+ [[ -f "$settings" ]] || continue
252
+ jq -e --arg p "$plugin@" '[.enabledPlugins // {} | to_entries[] | select((.key | startswith($p)) and .value == false)] | length > 0' \
253
+ "$settings" >/dev/null 2>&1 && return 0
254
+ done
255
+ while IFS= read -r path; do
256
+ [[ -n "$path" && -f "$path/skills/$skill/SKILL.md" ]] && printf '%s\n' "$path"
257
+ done < <(jq -r --arg p "$plugin@" --arg repo "$repo" --arg raw "$raw" '
258
+ .plugins // {} | to_entries[] | select(.key | startswith($p)) | .value[]?
259
+ | select(.scope == "user" or .scope == "managed" or ((.projectPath // "") as $at | $at != "" and ($at == $repo or $at == $raw)))
260
+ | .installPath // empty' "$config/plugins/installed_plugins.json" 2>/dev/null)
261
+ return 0
262
+ }
263
+
264
+ # The tracker tokens a commands stage, in a pass or around a shift, runs without, so only the
265
+ # driver writes outside the worktree (ADR 0046). Both drivers unset these, and point gh at an
266
+ # empty configuration of its own, so a stored login is out of reach too.
267
+ RUNNER_TRACKER_TOKENS=(GH_TOKEN GITHUB_TOKEN GH_ENTERPRISE_TOKEN GITHUB_ENTERPRISE_TOKEN)
268
+ # The skills kryso carries, which an installed copy can drift from, and which kryso's own Claude
269
+ # plugin ships.
270
+ RUNNER_CARRIED_SKILLS=(kryso kryso-build kryso-shift kryso-watch code-review)
271
+ # What an installed copy is compared without: the test files, which the npm package leaves out
272
+ # ("!skills/**/test-*.sh" in packages/cli/package.json), so a package install and a GitHub
273
+ # install of the same skill compare equal.
274
+ RUNNER_SKILL_COMPARE_EXCLUDE='test-*.sh'
275
+
276
+ # _runner_skill_digest <dir>: a checksum and the path of every file in the skill directory but
277
+ # its test files, one per line in path order. A directory that holds only test files leaves no
278
+ # line, as the package that drops them leaves no directory.
279
+ _runner_skill_digest() {
280
+ ( cd "$1" && find -L . -type f ! -name "$RUNNER_SKILL_COMPARE_EXCLUDE" -print0 | sort -z | xargs -0 cksum ) 2>/dev/null
281
+ }
282
+
283
+ # runner_skill_copies <repo> <skill>: the directory of each copy of the skill the current host
284
+ # can load by its own name, one per line. Claude looks in the repository's .claude/skills and in
285
+ # the user's ${CLAUDE_CONFIG_DIR:-~/.claude}/skills, and, for a skill kryso carries, in each
286
+ # install of the kryso plugin the repository loads, the way runner_skill_ref names it. A
287
+ # plugin's copy of any other skill is namespaced (/plugin:tdd), so the bare name the build stage
288
+ # runs does not reach it. Codex looks in the repository's .agents/skills, in ~/.agents/skills,
289
+ # as ship-pass.sh's search for code-review does, and in ${CODEX_HOME:-~/.codex}/skills. A copy
290
+ # where only the other host looks is no copy for this one.
291
+ runner_skill_copies() {
292
+ local repo="$1" skill="$2" config="${CLAUDE_CONFIG_DIR:-$HOME/.claude}" dir path carried
293
+ local -a dirs=("$repo/.claude/skills" "$config/skills")
294
+ [[ "$RUNNER_HOST" != codex ]] || dirs=("$repo/.agents/skills" "$HOME/.agents/skills" "${CODEX_HOME:-$HOME/.codex}/skills")
295
+ for dir in "${dirs[@]}"; do
296
+ [[ -f "$dir/$skill/SKILL.md" ]] && printf '%s\n' "$dir/$skill"
297
+ done
298
+ [[ "$RUNNER_HOST" != codex ]] || return 0
299
+ for carried in "${RUNNER_CARRIED_SKILLS[@]}"; do
300
+ [[ "$carried" == "$skill" ]] || continue
301
+ while IFS= read -r path; do
302
+ [[ -z "$path" ]] || printf '%s\n' "$path/skills/$skill"
303
+ done < <(_runner_plugin_installs kryso "$skill" "$config" "$repo")
304
+ done
305
+ return 0
306
+ }
307
+
308
+ # _runner_and_list <word...>: the words as prose, "a", "a and b", "a, b and c".
309
+ _runner_and_list() {
310
+ local out="" i
311
+ for (( i = 1; i <= $#; i++ )); do
312
+ if (( i == 1 )); then out="${!i}"
313
+ elif (( i == $# )); then out+=" and ${!i}"
314
+ else out+=", ${!i}"; fi
315
+ done
316
+ printf '%s' "$out"
317
+ }
318
+
319
+ # runner_skills_drifted <repo> <source> <skill...>: prints each named skill that has a copy this
320
+ # host can load whose files, test files aside, differ from <source>/<skill>, one per line. A
321
+ # skill with no copy at <source> is skipped, and so is a skill one of whose loaded copies is the
322
+ # source copy itself: the drivers run from that install, and there is nothing to compare.
323
+ runner_skills_drifted() {
324
+ local repo="$1" source="$2" skill own copy
325
+ local -a copies=()
326
+ shift 2
327
+ for skill in "$@"; do
328
+ [[ -d "$source/$skill" ]] || continue
329
+ own="$(cd "$source/$skill" && pwd -P)"
330
+ copies=()
331
+ while IFS= read -r copy; do
332
+ [[ -n "$copy" ]] || continue
333
+ copy="$(cd "$copy" && pwd -P)"
334
+ [[ "$copy" != "$own" ]] || continue 2
335
+ copies+=("$copy")
336
+ done < <(runner_skill_copies "$repo" "$skill")
337
+ [[ "${#copies[@]}" -gt 0 ]] || continue
338
+ own="$(_runner_skill_digest "$own")"
339
+ for copy in "${copies[@]}"; do
340
+ [[ "$(_runner_skill_digest "$copy")" != "$own" ]] || continue
341
+ printf '%s\n' "$skill"
342
+ break
343
+ done
344
+ done
345
+ }
346
+
347
+ runner_skill_prompt() {
348
+ local skill="$1" suffix="${2:-}" prompt
349
+ prompt="$(runner_skill_ref "$skill")"
350
+ [[ -z "$suffix" ]] || prompt+=" $suffix"
351
+ printf '%s' "$prompt"
352
+ }
353
+
354
+ runner_session_resume_supported() {
355
+ [[ "$RUNNER_HOST" == codex ]]
356
+ }
357
+
358
+ runner_initial_cost() {
359
+ if [[ "$RUNNER_HOST" == codex ]]; then printf 'null'; else printf '0'; fi
360
+ }
361
+
362
+ runner_shift_limit_summary() {
363
+ if [[ "$RUNNER_HOST" == claude ]]; then
364
+ printf 'the shift takes at most %s distinct issues, at $%s per run' "$1" "$2"
365
+ else
366
+ printf 'the shift takes at most %s distinct issues, and reports token usage but no dollar cost or cap' "$1"
367
+ fi
368
+ }
369
+
370
+ runner_preflight_warnings() {
371
+ [[ "$RUNNER_HOST" == claude ]] || return 0
372
+ local repo="$1" allow_rules
373
+ allow_rules="$(jq -r '.permissions.allow[]?' "$HOME/.claude/settings.json" "$repo/.claude/settings.json" "$repo/.claude/settings.local.json" 2>/dev/null)"
374
+ if ! grep -qE '^Bash\(gh( issue| \*|:\*|\*)' <<< "$allow_rules"; then
375
+ echo "ship-loop: warning: no allow rule for 'gh issue' in the settings, so --permission-prompts none denies /kryso's tracker reads. Add \"Bash(gh issue *)\" and the other gh rules to permissions.allow" >&2
376
+ fi
377
+ if [[ "$(jq -r --arg r "$repo" '.projects[$r].hasTrustDialogAccepted // false' "$HOME/.claude.json" 2>/dev/null)" != true ]]; then
378
+ echo "ship-loop: warning: this workspace is not trusted, so print mode ignores its .claude/settings.json allow rules. Run claude here once and accept the trust dialog" >&2
379
+ fi
380
+ }
381
+
382
+ _runner_shell_quoted() {
383
+ printf '%s' "$1" | sed -e 's/[\\"$`]/\\&/g'
384
+ }
385
+
386
+ # The repository's kryso settings in git config. Each is kryso.<name>. playbook.<name>, its
387
+ # name before the rename, is read where the new key is absent, until 3.0.0. reader.sh, which
388
+ # can run without this file, reads them the same way.
389
+ RUNNER_CONFIG_NAMES=(passMcp passSkills secondReader secondReaderModel jevKeychain)
390
+
391
+ # runner_config_key <repo> <name>: the key that holds the setting, kryso.<name> or
392
+ # playbook.<name>. Nothing printed and status 1 when neither is set.
393
+ runner_config_key() {
394
+ local key
395
+ for key in "kryso.$2" "playbook.$2"; do
396
+ git -C "$1" config --get "$key" > /dev/null 2>&1 && { printf '%s' "$key"; return 0; }
397
+ done
398
+ return 1
399
+ }
400
+
401
+ # runner_config <repo> <name>: the setting's value, read from the key runner_config_key names.
402
+ # Nothing printed and status 1 when neither is set. Nothing goes to stderr, because callers
403
+ # capture what a read prints.
404
+ runner_config() {
405
+ local key
406
+ key="$(runner_config_key "$1" "$2")" || return 1
407
+ git -C "$1" config --get "$key" 2>/dev/null
408
+ }
409
+
410
+ # runner_config_legacy <repo>: each setting the repository still holds under its playbook.*
411
+ # key alone, one key per line, for the preflight's warning. passMcp and passSkills have moved on
412
+ # to .kryso/config.json, so runner_config_moves warns for them instead, once.
413
+ runner_config_legacy() {
414
+ local name
415
+ for name in "${RUNNER_CONFIG_NAMES[@]}"; do
416
+ [[ -z "$(_runner_config_file_key "$name")" ]] || continue
417
+ [[ "$(runner_config_key "$1" "$name")" != "playbook.$name" ]] || printf 'playbook.%s\n' "$name"
418
+ done
419
+ }
420
+
421
+ # The repository's own kryso configuration, .kryso/config.json (ADR 0046): the gate, the MCP
422
+ # servers a pass keeps under `mcp` and the skills it lists as well under `skills`. The two
423
+ # settings were kryso.passMcp and kryso.passSkills in git config, which are read where the file
424
+ # sets neither until 3.0.0. A person's own model, effort and budget are never the repository's:
425
+ # they belong in ~/.kryso/config.json, so a repository file that sets one is refused.
426
+ RUNNER_REPO_CONFIG_NAME=.kryso/config.json
427
+ RUNNER_REPO_CONFIG='{}'
428
+ RUNNER_MEMBER_KEYS=(model effort budget)
429
+
430
+ # _runner_config_file_key <name>: the key in .kryso/config.json that replaced git config's
431
+ # kryso.<name>, or nothing.
432
+ _runner_config_file_key() {
433
+ case "$1" in passMcp) printf mcp ;; passSkills) printf skills ;; esac
434
+ }
435
+
436
+ # runner_repo_config <repo> [ref]: .kryso/config.json into RUNNER_REPO_CONFIG, from the commit
437
+ # <ref> when one is given, which a pass gives as its base, else from the file in <repo>. `{}`
438
+ # when there is none. A file that is not one JSON object, or that sets a member's own key, is
439
+ # one line on stderr and status 1.
440
+ runner_repo_config() {
441
+ local text="" key
442
+ RUNNER_REPO_CONFIG='{}'
443
+ if [[ -n "${2:-}" ]]; then text="$(git -C "$1" show "$2:$RUNNER_REPO_CONFIG_NAME" 2>/dev/null)" || text=""
444
+ elif [[ -f "$1/$RUNNER_REPO_CONFIG_NAME" ]]; then text="$(cat "$1/$RUNNER_REPO_CONFIG_NAME")"; fi
445
+ [[ -n "$text" ]] || return 0
446
+ if ! jq -e 'type == "object"' <<< "$text" > /dev/null 2>&1; then
447
+ echo "runner: $RUNNER_REPO_CONFIG_NAME is not one JSON object" >&2
448
+ return 1
449
+ fi
450
+ for key in "${RUNNER_MEMBER_KEYS[@]}"; do
451
+ jq -e --arg k "$key" 'has($k)' <<< "$text" > /dev/null 2>&1 || continue
452
+ echo "runner: $RUNNER_REPO_CONFIG_NAME sets $key, which is each person's own choice: it belongs in ~/.kryso/config.json, not in the repository" >&2
453
+ return 1
454
+ done
455
+ RUNNER_REPO_CONFIG="$(jq -c . <<< "$text")"
456
+ }
457
+
458
+ # runner_setting <repo> <name>: a pass setting, from its key in .kryso/config.json, else from
459
+ # kryso.<name> or playbook.<name> in git config. Sets RUNNER_SETTING (the value, a list joined
460
+ # by ", "), RUNNER_SETTING_KEY (the name the lines that report it use) and RUNNER_SETTING_SOURCE
461
+ # (that name and where it is). Status 1 when neither sets it, and 2, with one line on stderr,
462
+ # when the file holds something other than a string or a list of strings.
463
+ runner_setting() {
464
+ local file_key key
465
+ file_key="$(_runner_config_file_key "$2")"
466
+ RUNNER_SETTING=""
467
+ if [[ -n "$file_key" ]] && jq -e --arg k "$file_key" 'has($k)' <<< "$RUNNER_REPO_CONFIG" > /dev/null 2>&1; then
468
+ RUNNER_SETTING_KEY="$file_key in $RUNNER_REPO_CONFIG_NAME"
469
+ RUNNER_SETTING_SOURCE="$RUNNER_SETTING_KEY"
470
+ if ! RUNNER_SETTING="$(jq -er --arg k "$file_key" '.[$k] | if type == "string" then . elif type == "array" and all(.[]; type == "string") then join(", ") else error end' <<< "$RUNNER_REPO_CONFIG" 2>/dev/null)"; then
471
+ echo "runner: $RUNNER_SETTING_KEY must be a string or a list of strings" >&2
472
+ return 2
473
+ fi
474
+ return 0
475
+ fi
476
+ key="$(runner_config_key "$1" "$2")" || { RUNNER_SETTING_KEY="$file_key in $RUNNER_REPO_CONFIG_NAME"; RUNNER_SETTING_SOURCE="$RUNNER_SETTING_KEY"; return 1; }
477
+ RUNNER_SETTING_KEY="$key"
478
+ RUNNER_SETTING_SOURCE="$key in git config"
479
+ RUNNER_SETTING="$(git -C "$1" config --get "$key" 2>/dev/null)"
480
+ }
481
+
482
+ # runner_config_moves <repo>: one warning line for each pass setting still in git config, after
483
+ # runner_repo_config: its move to .kryso/config.json, or that it is ignored where the file sets it.
484
+ runner_config_moves() {
485
+ local name file_key key
486
+ for name in "${RUNNER_CONFIG_NAMES[@]}"; do
487
+ file_key="$(_runner_config_file_key "$name")"
488
+ [[ -n "$file_key" ]] || continue
489
+ key="$(runner_config_key "$1" "$name")" || continue
490
+ if jq -e --arg k "$file_key" 'has($k)' <<< "$RUNNER_REPO_CONFIG" > /dev/null 2>&1; then
491
+ printf '%s in git config is ignored, since %s sets %s. Remove it with: git config --unset %s\n' "$key" "$RUNNER_REPO_CONFIG_NAME" "$file_key" "$key"
492
+ else
493
+ printf '%s in git config has moved to %s in %s, and kryso reads the old place only until 3.0.0\n' "$key" "$file_key" "$RUNNER_REPO_CONFIG_NAME"
494
+ fi
495
+ done
496
+ }
497
+
498
+ # runner_member_defaults: a person's own defaults for profile, model, effort and budget, from
499
+ # ~/.kryso/config.json, into RUNNER_MEMBER as one JSON object. A missing or empty file is `{}`.
500
+ # A file that is not one JSON object is one line on stderr and status 1.
501
+ RUNNER_MEMBER_CONFIG_NAME='~/.kryso/config.json'
502
+ RUNNER_MEMBER='{}'
503
+ runner_member_defaults() {
504
+ local file="$HOME/.kryso/config.json" text=""
505
+ RUNNER_MEMBER='{}'
506
+ [[ ! -f "$file" ]] || text="$(cat "$file")"
507
+ [[ -n "${text//[[:space:]]/}" ]] || return 0
508
+ if ! jq -e 'type == "object"' <<< "$text" > /dev/null 2>&1; then
509
+ echo "runner: $RUNNER_MEMBER_CONFIG_NAME is not one JSON object" >&2
510
+ return 1
511
+ fi
512
+ RUNNER_MEMBER="$(jq -c '{profile, model, effort, budget} | with_entries(select(.value != null) | .value |= tostring)' <<< "$text")"
513
+ }
514
+
515
+ # runner_pass_mcp <repo>: whether a Claude pass in the repo loads MCP servers. The value is
516
+ # KRYSO_PASS_MCP when it is set, which a shift's or a pass's --mcp exports, else `mcp` in
517
+ # .kryso/config.json, else kryso.passMcp in git config (playbook.passMcp before the rename). A caller that exported it from its flag
518
+ # sets RUNNER_PASS_MCP_FLAG to the flag's name, so the lines that report it name the flag. `on`, the default, loads what the user's
519
+ # settings name. `off` adds --strict-mcp-config with no config, so the pass starts without
520
+ # them. RUNNER_PASS_MCP_SOURCE names where the value came from, for the lines that report it.
521
+ # What that saves, and how to choose between the forms, is in reference/driver.md under
522
+ # Profiles and models.
523
+ #
524
+ # Any other value is a comma-separated list of the servers to keep, named as `claude mcp list`
525
+ # prints them. RUNNER_PASS_MCP is then `keep`, RUNNER_PASS_MCP_KEPT the list as written, and
526
+ # RUNNER_PASS_MCP_SETTINGS the --settings JSON whose allowedMcpServers the host enforces.
527
+ # --strict-mcp-config is not used for it, because that drops every claude.ai connector. The
528
+ # allowlist matches a connector only by its URL (by its name it is blocked: the #398 probes),
529
+ # and every other server by its name. A server that shares a kept connector's URL, such as a
530
+ # plugin's copy of it, is kept with it.
531
+ # On a Claude host the list is checked against `claude mcp list`, run in the repository so its
532
+ # project servers count, and a name the host does not show is a stop. Sets RUNNER_PASS_MCP,
533
+ # RUNNER_PASS_MCP_KEPT, RUNNER_PASS_MCP_SETTINGS and RUNNER_PASS_MCP_NAME, the key or variable
534
+ # the value came from. A value it cannot use is one line on stderr and status 1.
535
+ runner_pass_mcp() {
536
+ local value name parts=() names=() listing line rest entries="" entry err key src
537
+ if [[ -n "${KRYSO_PASS_MCP:-}" ]]; then
538
+ value="$KRYSO_PASS_MCP"; key="${RUNNER_PASS_MCP_FLAG:-KRYSO_PASS_MCP}"; src="$key"
539
+ else
540
+ runner_setting "$1" passMcp; [[ $? -ne 2 ]] || return 1
541
+ value="$RUNNER_SETTING"; key="$RUNNER_SETTING_KEY"; src="$RUNNER_SETTING_SOURCE"
542
+ fi
543
+ RUNNER_PASS_MCP_NAME="$key"
544
+ RUNNER_PASS_MCP_SOURCE="$src"
545
+ RUNNER_PASS_MCP_KEPT=""
546
+ RUNNER_PASS_MCP_SETTINGS=""
547
+ case "${value:-on}" in
548
+ on|off) RUNNER_PASS_MCP="${value:-on}"; return 0 ;;
549
+ esac
550
+ IFS=, read -r -a parts <<< "$value"
551
+ for name in ${parts[@]+"${parts[@]}"}; do
552
+ name="$(sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' <<< "$name")"
553
+ [[ -z "$name" ]] || names+=("$name")
554
+ done
555
+ if [[ "${#names[@]}" -eq 0 ]]; then
556
+ echo "runner: $key is '$value'${src#"$key"}, not on, off or a comma-separated list of server names" >&2
557
+ return 1
558
+ fi
559
+ RUNNER_PASS_MCP=keep
560
+ RUNNER_PASS_MCP_KEPT="$(printf '%s, ' "${names[@]}")"
561
+ RUNNER_PASS_MCP_KEPT="${RUNNER_PASS_MCP_KEPT%, }"
562
+ [[ "$RUNNER_HOST" == claude ]] || return 0
563
+ err="$(mktemp)"
564
+ if ! listing="$(cd "$1" && claude mcp list 2> "$err")"; then
565
+ echo "runner: $key names servers to keep, and claude mcp list failed: $(head -1 "$err")" >&2
566
+ rm -f "$err"
567
+ return 1
568
+ fi
569
+ rm -f "$err"
570
+ for name in "${names[@]}"; do
571
+ entry=""
572
+ while IFS= read -r line; do
573
+ [[ "$line" == "$name: "* ]] || continue
574
+ if [[ "$name" == "claude.ai "* ]]; then
575
+ rest="${line#"$name: "}"
576
+ entry="$(jq -nc --arg url "${rest%% *}" '{serverUrl: $url}')"
577
+ else
578
+ entry="$(jq -nc --arg name "$name" '{serverName: $name}')"
579
+ fi
580
+ break
581
+ done <<< "$listing"
582
+ if [[ -z "$entry" ]]; then
583
+ echo "runner: $key names '$name', which claude mcp list does not show" >&2
584
+ return 1
585
+ fi
586
+ entries+="$entry"$'\n'
587
+ done
588
+ RUNNER_PASS_MCP_SETTINGS="$(jq -sc '{allowedMcpServers: .}' <<< "$entries")"
589
+ }
590
+
591
+ # The skills a Claude pass lists by name when the repository adds none: kryso's own and the
592
+ # unpinned one its build runs. The skills a workflow pins, tdd and implement among them, reach
593
+ # the pass through its own plugin, RUNNER_PINS_PLUGIN, so a copy of the user's by the same name
594
+ # is turned off with every other (ADR 0046).
595
+ RUNNER_PASS_SKILLS_DEFAULT=("${RUNNER_CARRIED_SKILLS[@]}" diagnosing-bugs)
596
+ # The name of the plugin a pass assembles from its workflow's pins, which prefixes each pinned
597
+ # skill as the session lists it: /kryso-pins:tdd.
598
+ RUNNER_PINS_PLUGIN=kryso-pins
599
+
600
+ # runner_fetch_pins <workflow.mjs> <repo> <ref> <workflow>: every skill the workflow pins at the
601
+ # ref, in kryso's store, the resolver fetching each one the store lacks (ADR 0046). The pins go
602
+ # in the RUNNER_PIN_* arrays, one entry a pin: stage, skill, source, path, commit and the skill's
603
+ # folder in the store. RUNNER_PINS_SAID says which were fetched and which were already stored,
604
+ # as in "implement fetched, tdd stored". A pin the resolver cannot fetch returns 1 with its
605
+ # reason in RUNNER_PINS_FAILURE.
606
+ # shellcheck disable=SC2034 # the arrays and the two lines are read by ship-loop.sh and ship-pass.sh
607
+ runner_fetch_pins() {
608
+ local err out stage skill source path commit dir status fetched=() stored=()
609
+ RUNNER_PIN_STAGES=() RUNNER_PIN_SKILLS=() RUNNER_PIN_SOURCES=() RUNNER_PIN_PATHS=() RUNNER_PIN_COMMITS=() RUNNER_PIN_DIRS=()
610
+ RUNNER_PINS_SAID="" RUNNER_PINS_FAILURE=""
611
+ err="$(mktemp)"
612
+ if ! out="$(node "$1" fetch "$4" --repo "$2" --ref "$3" 2>"$err")"; then
613
+ RUNNER_PINS_FAILURE="$(sed 's/^kryso workflow: //' "$err" | head -1)"; rm -f "$err"
614
+ RUNNER_PINS_FAILURE="${RUNNER_PINS_FAILURE:-the pins of the workflow $4 cannot be fetched}"
615
+ return 1
616
+ fi
617
+ rm -f "$err"
618
+ while IFS=$'\t' read -r stage skill source path commit dir status; do
619
+ [[ -n "$skill" ]] || continue
620
+ RUNNER_PIN_STAGES+=("$stage") RUNNER_PIN_SKILLS+=("$skill") RUNNER_PIN_SOURCES+=("$source")
621
+ RUNNER_PIN_PATHS+=("$path") RUNNER_PIN_COMMITS+=("$commit") RUNNER_PIN_DIRS+=("$dir")
622
+ if [[ "$status" == fetched ]]; then fetched+=("$skill"); else stored+=("$skill"); fi
623
+ done <<< "$out"
624
+ [[ "${#fetched[@]}" -eq 0 ]] || RUNNER_PINS_SAID="$(_runner_and_list "${fetched[@]}") fetched"
625
+ [[ "${#stored[@]}" -eq 0 ]] || RUNNER_PINS_SAID+="${RUNNER_PINS_SAID:+, }$(_runner_and_list "${stored[@]}") stored"
626
+ RUNNER_PINS_SAID="${RUNNER_PINS_SAID:-none}"
627
+ }
628
+
629
+ # runner_pins_plugin <dir> <stage>: the plugin RUNNER_PINS_PLUGIN at <dir>, made afresh, whose
630
+ # skills/<skill> links to the folder in the store of each pin of the stage, after
631
+ # runner_fetch_pins. Sets RUNNER_PLUGIN_DIR to the directory, absolute, since a start reads the
632
+ # flag from wherever it runs, and leaves it empty, with no directory, when the stage pins nothing.
633
+ runner_pins_plugin() {
634
+ local dir="$1" i
635
+ RUNNER_PLUGIN_DIR=""
636
+ [[ -n "$dir" ]] || return 0
637
+ rm -rf "$dir"
638
+ for i in ${RUNNER_PIN_SKILLS[@]+"${!RUNNER_PIN_SKILLS[@]}"}; do
639
+ [[ "${RUNNER_PIN_STAGES[$i]}" == "$2" ]] || continue
640
+ if [[ -z "$RUNNER_PLUGIN_DIR" ]]; then
641
+ mkdir -p "$dir/.claude-plugin" "$dir/skills"
642
+ jq -n --arg name "$RUNNER_PINS_PLUGIN" \
643
+ '{name: $name, description: "The skills this run'"'"'s workflow pins, linked from kryso'"'"'s store"}' > "$dir/.claude-plugin/plugin.json"
644
+ RUNNER_PLUGIN_DIR="$(cd "$dir" && pwd)"
645
+ fi
646
+ ln -sfn "${RUNNER_PIN_DIRS[$i]}" "$dir/skills/${RUNNER_PIN_SKILLS[$i]}"
647
+ done
648
+ }
649
+
650
+ # runner_pass_skills <repo>: which skills a Claude pass in the repo lists, from
651
+ # `skills` in .kryso/config.json, else kryso.passSkills in git config (playbook.passSkills
652
+ # before the rename). Unset, a pass lists
653
+ # the default set alone. `all` lists every skill the user has, as before #547. Any other value
654
+ # is a comma-separated list of skills to list as well, named as a session lists them: `name`
655
+ # for a skill folder, `plugin:name` for a plugin's skill.
656
+ #
657
+ # Claude Code has no allowlist for skills, so the runner builds the deny side (#547 probes):
658
+ # a skillOverrides entry of "off" for every other skill folder in the repository's
659
+ # .claude/skills, the user's skills and the user's synced claude.ai skills, every command file
660
+ # beside them, disableBundledSkills for the skills Claude Code ships, and an enabledPlugins
661
+ # entry of false for every plugin that carries skills, because skillOverrides does not reach a
662
+ # plugin's skills. A plugin stays on when it carries a listed skill (kryso's own by bare name),
663
+ # carries hooks, which turning it off would also drop, or carries a server passMcp
664
+ # keeps, so run runner_pass_mcp first. All of it reaches the host through --settings, which
665
+ # only that start reads, so the user's own sessions are unchanged.
666
+ #
667
+ # Sets RUNNER_PASS_SKILLS (`all` or `only`), RUNNER_PASS_SKILLS_ADDED (the repository's
668
+ # additions as written), RUNNER_PASS_SKILLS_SETTINGS (the JSON, empty for `all` and off a
669
+ # Claude host) and RUNNER_PASS_SKILLS_KEY (the key the value came from). A name no folder or plugin carries is one line on stderr and status 1.
670
+ runner_pass_skills() {
671
+ local repo="$1" value config="${CLAUDE_CONFIG_DIR:-$HOME/.claude}" name parts=() added=() kept=() hidden=() found=()
672
+ local dir file plugin id path off=() keep skill key
673
+ runner_setting "$repo" passSkills; [[ $? -ne 2 ]] || return 1
674
+ value="$RUNNER_SETTING"; key="$RUNNER_SETTING_KEY"
675
+ RUNNER_PASS_SKILLS_KEY="$key"
676
+ RUNNER_PASS_SKILLS_SOURCE="$RUNNER_SETTING_SOURCE"
677
+ RUNNER_PASS_SKILLS_ADDED=""
678
+ RUNNER_PASS_SKILLS_SETTINGS=""
679
+ if [[ "$value" == all ]]; then RUNNER_PASS_SKILLS=all; return 0; fi
680
+ IFS=, read -r -a parts <<< "$value"
681
+ for name in ${parts[@]+"${parts[@]}"}; do
682
+ name="$(sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' <<< "$name")"
683
+ [[ -z "$name" ]] || added+=("$name")
684
+ done
685
+ if [[ -n "$value" && "${#added[@]}" -eq 0 ]]; then
686
+ echo "runner: $key is '$value'${RUNNER_PASS_SKILLS_SOURCE#"$key"}, not all or a comma-separated list of skill names" >&2
687
+ return 1
688
+ fi
689
+ RUNNER_PASS_SKILLS=only
690
+ if [[ "${#added[@]}" -gt 0 ]]; then
691
+ RUNNER_PASS_SKILLS_ADDED="$(printf '%s, ' "${added[@]}")"
692
+ RUNNER_PASS_SKILLS_ADDED="${RUNNER_PASS_SKILLS_ADDED%, }"
693
+ fi
694
+ [[ "$RUNNER_HOST" == claude ]] || return 0
695
+ kept=("${RUNNER_PASS_SKILLS_DEFAULT[@]}" ${added[@]+"${added[@]}"})
696
+
697
+ # Every name a skill folder or command file gives, with the synced skills under their
698
+ # anthropic-skills: name as well, since a bare name another skill holds does not reach them.
699
+ for dir in "$repo/.claude/skills" "$config/skills"; do
700
+ for file in "$dir"/*/SKILL.md; do [[ -f "$file" ]] && found+=("$(basename "$(dirname "$file")")"); done
701
+ done
702
+ for file in "$config/skills/synced"/*/*/SKILL.md; do
703
+ [[ -f "$file" ]] || continue
704
+ name="$(basename "$(dirname "$file")")"
705
+ found+=("$name" "anthropic-skills:$name")
706
+ done
707
+ for dir in "$repo/.claude/commands" "$config/commands"; do
708
+ for file in "$dir"/*.md "$dir"/*/*.md; do
709
+ [[ -f "$file" ]] || continue
710
+ name="${file#"$dir/"}"; name="${name%.md}"
711
+ found+=("${name//\//:}")
712
+ done
713
+ done
714
+ for name in ${found[@]+"${found[@]}"}; do
715
+ _runner_in "$name" "${kept[@]}" || hidden+=("$name")
716
+ done
717
+
718
+ # Every plugin that carries skills or commands: the installs this config records and the
719
+ # plugins claude.ai syncs, which load as <name>@synced.
720
+ while IFS=$'\t' read -r id path; do
721
+ [[ -n "$id" && ( -d "$path/skills" || -d "$path/commands" ) ]] || continue
722
+ plugin="${id%%@*}"
723
+ keep=0
724
+ for skill in "${kept[@]}"; do
725
+ if [[ "$skill" == "$plugin:"* && -f "$path/skills/${skill#"$plugin:"}/SKILL.md" ]] \
726
+ || [[ "$plugin" == kryso && -f "$path/skills/$skill/SKILL.md" ]]; then keep=1; fi
727
+ done
728
+ [[ ! -f "$path/hooks/hooks.json" ]] || keep=1
729
+ jq -e '.hooks' "$path/.claude-plugin/plugin.json" >/dev/null 2>&1 && keep=1
730
+ [[ ", $RUNNER_PASS_MCP_KEPT," != *", plugin:$plugin:"* ]] || keep=1
731
+ [[ "$keep" -eq 1 ]] || _runner_in "$id" ${off[@]+"${off[@]}"} || off+=("$id")
732
+ for skill in "$path"/skills/*/SKILL.md; do
733
+ [[ -f "$skill" ]] && found+=("$plugin:$(basename "$(dirname "$skill")")")
734
+ done
735
+ [[ "$plugin" != kryso ]] || for skill in "$path"/skills/*/SKILL.md; do
736
+ [[ -f "$skill" ]] && found+=("$(basename "$(dirname "$skill")")")
737
+ done
738
+ done < <(_runner_plugins "$config")
739
+
740
+ for name in ${added[@]+"${added[@]}"}; do
741
+ _runner_in "$name" ${found[@]+"${found[@]}"} && continue
742
+ echo "runner: $key names '$name', which no skill folder, command or plugin of this host carries" >&2
743
+ return 1
744
+ done
745
+ RUNNER_PASS_SKILLS_SETTINGS="$(jq -nc \
746
+ --argjson hidden "$(jq -nc '$ARGS.positional' --args ${hidden[@]+"${hidden[@]}"})" \
747
+ --argjson off "$(jq -nc '$ARGS.positional' --args ${off[@]+"${off[@]}"})" '
748
+ (if ($hidden | length) > 0 then {skillOverrides: ($hidden | map({(.): "off"}) | add)} else {} end)
749
+ + {disableBundledSkills: true}
750
+ + (if ($off | length) > 0 then {enabledPlugins: ($off | map({(.): false}) | add)} else {} end)')"
751
+ }
752
+
753
+ # _runner_in <word> <list...>: status 0 when the word is in the list.
754
+ _runner_in() {
755
+ local word="$1" item
756
+ shift
757
+ for item in "$@"; do [[ "$item" != "$word" ]] || return 0; done
758
+ return 1
759
+ }
760
+
761
+ # _runner_plugins <config>: the id and install directory of each plugin the config knows,
762
+ # tab-separated, one per line: every install installed_plugins.json records, and each plugin
763
+ # claude.ai syncs under plugins/synced, by the name its manifest gives.
764
+ _runner_plugins() {
765
+ local config="$1" path name
766
+ jq -r '.plugins // {} | to_entries[] | .key as $id | .value[]? | select(.installPath) | "\($id)\t\(.installPath)"' \
767
+ "$config/plugins/installed_plugins.json" 2>/dev/null
768
+ for path in "$config/plugins/synced"/*/*/; do
769
+ path="${path%/}"
770
+ name="$(jq -r '.name // empty' "$path/.claude-plugin/plugin.json" 2>/dev/null)"
771
+ [[ -z "$name" ]] || printf '%s@synced\t%s\n' "$name" "$path"
772
+ done
773
+ }
774
+
775
+ # runner_pass_skills_line: the preflight's sentence for what a pass lists.
776
+ runner_pass_skills_line() {
777
+ if [[ "$RUNNER_HOST" != claude ]]; then
778
+ printf 'a %s pass lists every skill, since passSkills limits Claude passes alone' "$RUNNER_HOST"
779
+ elif [[ "${RUNNER_PASS_SKILLS:-all}" == all ]]; then
780
+ printf 'a pass lists every skill, from %s, its pinned skills under %s among them' "${RUNNER_PASS_SKILLS_SOURCE:-kryso.passSkills in git config}" "$RUNNER_PINS_PLUGIN"
781
+ elif [[ -n "${RUNNER_PASS_SKILLS_ADDED:-}" ]]; then
782
+ printf 'a pass lists the kryso pass skills and %s, from %s, and its pinned skills under %s' "$RUNNER_PASS_SKILLS_ADDED" "${RUNNER_PASS_SKILLS_SOURCE:-kryso.passSkills in git config}" "$RUNNER_PINS_PLUGIN"
783
+ else
784
+ printf 'a pass lists the kryso pass skills alone: %s, and its pinned skills under %s' "$(_runner_and_list "${RUNNER_PASS_SKILLS_DEFAULT[@]}")" "$RUNNER_PINS_PLUGIN"
785
+ fi
786
+ }
787
+
788
+ # What every Claude pass context starts without and with (#681, ADR 0056). The four tools no
789
+ # pass calls are denied, which takes them out of the tool list. The person's own CLAUDE.md and
790
+ # rules folder are excluded and auto memory is off, so private notes stay out of unattended work.
791
+ # The safety rules a pass still needs from that file travel in pass-rules.md, appended to the
792
+ # system prompt. The call bound outlasts the longest gate, so a fork runs its receipts attached
793
+ # and the pass deadline stops a hung one; it reaches the host through env, never this shell.
794
+ RUNNER_PASS_DENIED_TOOLS=(Workflow ScheduleWakeup ReportFindings ListAgents)
795
+ RUNNER_PASS_RULES="$RUNNER_DIR/pass-rules.md"
796
+ RUNNER_CALL_BOUND_MS=1800000
797
+
798
+ # _runner_lean_settings: the denied tools, the excluded instruction files and memory, as JSON.
799
+ _runner_lean_settings() {
800
+ local config="${CLAUDE_CONFIG_DIR:-$HOME/.claude}"
801
+ jq -nc --arg md "$config/CLAUDE.md" --arg rules "$config/rules/**" \
802
+ '{permissions: {deny: $ARGS.positional}, claudeMdExcludes: [$md, $rules], autoMemoryEnabled: false}' \
803
+ --args "${RUNNER_PASS_DENIED_TOOLS[@]}"
804
+ }
805
+
806
+ # _runner_pass_settings: the one --settings JSON a Claude start carries: passMcp's allowlist,
807
+ # passSkills' overrides and the lean settings together. The host reads only the last
808
+ # --settings it is given, so they cannot travel apart.
809
+ _runner_pass_settings() {
810
+ local mcp="" skills=""
811
+ [[ "${RUNNER_PASS_MCP:-on}" != keep ]] || mcp="$RUNNER_PASS_MCP_SETTINGS"
812
+ [[ "${RUNNER_PASS_SKILLS:-all}" == all ]] || skills="${RUNNER_PASS_SKILLS_SETTINGS:-}"
813
+ printf '%s\n%s\n%s\n' "$mcp" "$skills" "$(_runner_lean_settings)" | jq -sc 'add'
814
+ }
815
+
816
+ # Print a start command a person can rerun without exposing host policy to the driver.
817
+ runner_printable_command() {
818
+ local prompt="$1" schema="$2" schema_display="$3" final="$4" model="$5" budget="$6" effort="$7" keep_api_key="$8"
819
+ shift 8
820
+ local extras=("$@") out="" settings
821
+ # An empty model is the profile's, as in runner_prepare_command, so the printed command is the run one.
822
+ [[ -n "$model" ]] || model="$RUNNER_MODEL"
823
+ runner_validate_extra_args ${extras[@]+"${extras[@]}"} || return 1
824
+ if [[ "$RUNNER_HOST" == claude ]]; then
825
+ [[ "$keep_api_key" -eq 1 ]] || out="env -u ANTHROPIC_API_KEY "
826
+ out+="claude -p \"$(_runner_shell_quoted "$prompt")\" --permission-mode auto --permission-prompts none"
827
+ out+=" --output-format json --append-system-prompt-file \"$(_runner_shell_quoted "$RUNNER_PASS_RULES")\" --json-schema \"\$(cat \"$schema_display\")\" --max-budget-usd $budget"
828
+ [[ -z "$model" ]] || out+=" --model $model"
829
+ out+=" --effort $effort"
830
+ [[ "${RUNNER_PASS_MCP:-on}" != off ]] || out+=" --strict-mcp-config"
831
+ settings="$(_runner_pass_settings)"
832
+ [[ -z "$settings" ]] || out+=" --settings \"$(_runner_shell_quoted "$settings")\""
833
+ [[ -z "${RUNNER_PLUGIN_DIR:-}" ]] || out+=" --plugin-dir \"$(_runner_shell_quoted "$RUNNER_PLUGIN_DIR")\""
834
+ [[ "${#extras[@]}" -eq 0 ]] || out+=" ${extras[*]}"
835
+ printf '%s' "$out"
836
+ return 0
837
+ fi
838
+ RUNNER_EXTRA_ARGS=()
839
+ [[ "${#extras[@]}" -eq 0 ]] || RUNNER_EXTRA_ARGS=("${extras[@]}")
840
+ runner_prepare_command start "$prompt" "$schema" "$final" "$model" "$budget" "" "$effort" || return 1
841
+ printf 'env -u OPENAI_API_KEY -u CODEX_API_KEY '
842
+ printf '%q ' "${RUNNER_CMD[@]}"
843
+ }
844
+
845
+ _runner_version_lt() {
846
+ [[ "$1" == "$2" ]] && return 1
847
+ [[ "$(printf '%s\n%s\n' "$1" "$2" | sort -t. -k1,1n -k2,2n -k3,3n | head -1)" == "$1" ]]
848
+ }
849
+
850
+ # runner_preflight: the host is installed, logged in the way the profile requires, and has the
851
+ # flags the profile uses. It also sets RUNNER_VERSION, and RUNNER_AGENTS to 1 when the host can
852
+ # define a sub-agent type with its own tools, which is how reviewers are held to reading.
853
+ runner_preflight() {
854
+ local help resume_help login
855
+ RUNNER_AGENTS=0
856
+ command -v "$RUNNER_HOST" >/dev/null 2>&1 || {
857
+ echo "runner: $RUNNER_HOST is not on PATH" >&2
858
+ return 1
859
+ }
860
+ if [[ "$RUNNER_HOST" == claude ]]; then
861
+ RUNNER_VERSION="$(claude --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)"
862
+ [[ -n "$RUNNER_VERSION" ]] || { echo "runner: cannot read the claude version" >&2; return 1; }
863
+ _runner_version_lt "$RUNNER_VERSION" "2.1.218" && {
864
+ echo "runner: claude $RUNNER_VERSION is older than 2.1.218" >&2
865
+ return 1
866
+ }
867
+ # Read whole before it is searched: grep -q stops at the first match, and a host still
868
+ # writing its help then dies of SIGPIPE, which pipefail reports as a failed check.
869
+ help="$(claude --help 2>/dev/null)"
870
+ grep -q -- '--effort' <<< "$help" || {
871
+ echo "runner: claude has no --effort flag, and every pass needs it" >&2
872
+ return 1
873
+ }
874
+ grep -q -- '--agents' <<< "$help" && RUNNER_AGENTS=1
875
+ # A subscription pass starts with the API key stripped, so it holds only while the login
876
+ # does. Read locally, with no model request. An API-billed pass keeps its key and needs none.
877
+ # Read with grep, not jq, so a machine without jq is not told its login fails.
878
+ if [[ "${RUNNER_BILLING:-subscription}" != api ]]; then
879
+ login="$(env -u ANTHROPIC_API_KEY claude auth status --json 2>/dev/null)"
880
+ grep -Eq '"loggedIn"[[:space:]]*:[[:space:]]*true' <<< "$login" || {
881
+ echo "runner: profile '$RUNNER_PROFILE' needs a claude login, and claude auth status says it is not logged in; run claude auth login" >&2
882
+ return 1
883
+ }
884
+ fi
885
+ return 0
886
+ fi
887
+
888
+ RUNNER_VERSION="$(codex --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)"
889
+ [[ -n "$RUNNER_VERSION" ]] || { echo "runner: cannot read the codex version" >&2; return 1; }
890
+ help="$(codex exec --help 2>/dev/null)"
891
+ resume_help="$(codex exec resume --help 2>/dev/null)"
892
+ for flag in --json --output-schema --output-last-message --approve-for-me --add-dir; do
893
+ grep -q -- "$flag" <<< "$help" || {
894
+ echo "runner: codex exec has no $flag flag, and every pass needs it" >&2
895
+ return 1
896
+ }
897
+ done
898
+ for flag in --json --output-schema --output-last-message; do
899
+ grep -q -- "$flag" <<< "$resume_help" || {
900
+ echo "runner: codex exec resume has no $flag flag, and every pass needs it" >&2
901
+ return 1
902
+ }
903
+ done
904
+ login="$(env -u OPENAI_API_KEY -u CODEX_API_KEY codex login status 2>&1)"
905
+ grep -q 'Logged in using ChatGPT' <<< "$login" || {
906
+ echo "runner: profile '$RUNNER_PROFILE' needs 'codex login' with a ChatGPT subscription, and codex login status says ${login:-nothing}" >&2
907
+ return 1
908
+ }
909
+ }
910
+
911
+ # The reviewer agent: a sub-agent type the build fork dispatches its two reviewers as, with the
912
+ # tools to read and search and nothing else. No shell means no shell-mediated write, which
913
+ # taking Edit and Write away alone would leave open. Hosts that cannot define one get nothing,
914
+ # and the caller records the gap and keeps its tree check.
915
+ RUNNER_REVIEWER_AGENT=ship-it-reviewer
916
+ RUNNER_REVIEWER_TOOLS=(Read Grep Glob)
917
+
918
+ # runner_reviewer_restriction: prints `restricted` when this host can hold reviewers to the
919
+ # reviewer agent's tools, else `unrestricted <why>`, the why naming the host.
920
+ runner_reviewer_restriction() {
921
+ if [[ "$RUNNER_HOST" == claude && "${RUNNER_AGENTS:-0}" -eq 1 ]]; then
922
+ echo restricted
923
+ elif [[ "$RUNNER_HOST" == claude ]]; then
924
+ echo "unrestricted claude ${RUNNER_VERSION:-unknown} has no --agents flag to define a read-only reviewer"
925
+ else
926
+ echo "unrestricted codex ${RUNNER_VERSION:-unknown} has no per-agent tool restriction the pass can set"
927
+ fi
928
+ }
929
+
930
+ # runner_reviewer_agents <file>: writes the reviewer agent's definition where the host reads
931
+ # it. The caller sets RUNNER_AGENTS_FILE to the file so the start command carries it.
932
+ runner_reviewer_agents() {
933
+ jq -n --arg name "$RUNNER_REVIEWER_AGENT" --args '{($name): {
934
+ description: "Read-only reviewer for /code-review in an unattended pass: reads the saved diff and the files it touches, and reports findings with quotes.",
935
+ prompt: "You are reviewing, not changing. You can read and search files and nothing else: read the saved diff, the files it touches, the standards and the spec, and report every finding with a quote from the diff.",
936
+ tools: $ARGS.positional}}' "${RUNNER_REVIEWER_TOOLS[@]}" > "$1"
937
+ }
938
+
939
+ # runner_prepare_command <start|resume> <prompt> <schema> <final-file> <model> <budget> <session> <effort>
940
+ # fills RUNNER_CMD. RUNNER_EXTRA_ARGS, when declared by the caller, is appended before the
941
+ # prompt. RUNNER_AGENTS_FILE, when set, names the reviewer agent's definition for a host that
942
+ # reads one. RUNNER_PLUGIN_DIR, when set, is the plugin of pinned skills a Claude start loads
943
+ # with --plugin-dir. A Claude start or resume runs under env with the pass's call bound, and
944
+ # carries the rules file and the lean settings. Resume deliberately omits the initial-only Codex
945
+ # approval flag.
946
+ # _runner_schema_text <file>: the schema as claude's --json-schema takes it. Claude Code 2.1.332
947
+ # validates that argument against draft-07 alone and refuses a draft 2020-12 "$schema" line (#459),
948
+ # so a schema that declares one is passed without it. Any other schema is passed byte for byte.
949
+ _runner_schema_text() {
950
+ if jq -e 'has("$schema")' "$1" > /dev/null 2>&1; then jq 'del(."$schema")' "$1"; else cat "$1"; fi
951
+ }
952
+
953
+ runner_prepare_command() {
954
+ local mode="$1" prompt="$2" schema="$3" final="$4" model="$5" budget="$6" session="$7" effort="${8:-$RUNNER_EFFORT}"
955
+ local extras=() settings
956
+ if declare -p RUNNER_EXTRA_ARGS >/dev/null 2>&1 && [[ "${#RUNNER_EXTRA_ARGS[@]}" -gt 0 ]]; then
957
+ extras=("${RUNNER_EXTRA_ARGS[@]}")
958
+ fi
959
+ runner_validate_extra_args ${extras[@]+"${extras[@]}"} || return 1
960
+ [[ -n "$model" ]] || model="$RUNNER_MODEL"
961
+
962
+ if [[ "$RUNNER_HOST" == claude ]]; then
963
+ RUNNER_CMD=(env "BASH_MAX_TIMEOUT_MS=$RUNNER_CALL_BOUND_MS" claude -p)
964
+ if [[ "$mode" == resume ]]; then RUNNER_CMD+=(--resume "$session"); elif [[ -n "$prompt" ]]; then RUNNER_CMD+=("$prompt"); fi
965
+ RUNNER_CMD+=(--permission-mode auto --permission-prompts none --output-format json)
966
+ RUNNER_CMD+=(--append-system-prompt-file "$RUNNER_PASS_RULES")
967
+ [[ -z "$schema" ]] || RUNNER_CMD+=(--json-schema "$(_runner_schema_text "$schema")")
968
+ RUNNER_CMD+=(--max-budget-usd "$budget")
969
+ [[ -z "$model" ]] || RUNNER_CMD+=(--model "$model")
970
+ RUNNER_CMD+=(--effort "$effort")
971
+ [[ "${RUNNER_PASS_MCP:-on}" != off ]] || RUNNER_CMD+=(--strict-mcp-config)
972
+ settings="$(_runner_pass_settings)"
973
+ [[ -z "$settings" ]] || RUNNER_CMD+=(--settings "$settings")
974
+ [[ -z "${RUNNER_PLUGIN_DIR:-}" ]] || RUNNER_CMD+=(--plugin-dir "$RUNNER_PLUGIN_DIR")
975
+ [[ -z "${RUNNER_AGENTS_FILE:-}" ]] || RUNNER_CMD+=(--agents "$RUNNER_AGENTS_FILE")
976
+ [[ "${#extras[@]}" -eq 0 ]] || RUNNER_CMD+=("${extras[@]}")
977
+ return 0
978
+ fi
979
+
980
+ if [[ "$mode" == resume ]]; then
981
+ RUNNER_CMD=(codex exec resume --json)
982
+ else
983
+ RUNNER_CMD=(codex exec --json)
984
+ fi
985
+ [[ -z "$schema" ]] || RUNNER_CMD+=(--output-schema "$schema")
986
+ RUNNER_CMD+=(-o "$final")
987
+ # The workspace-write sandbox --approve-for-me puts a start in can write the worktree alone,
988
+ # and a worktree's git directory, the gate lock and the pass lock live in the main checkout's
989
+ # common dir. Without it every commit and receipt fails once and is retried escalated. A resume
990
+ # keeps the sandbox its session started with.
991
+ if [[ "$mode" != resume ]]; then
992
+ RUNNER_CMD+=(--approve-for-me)
993
+ local common
994
+ common="$(git rev-parse --path-format=absolute --git-common-dir 2>/dev/null)" && RUNNER_CMD+=(--add-dir "$common")
995
+ fi
996
+ [[ -z "$model" ]] || RUNNER_CMD+=(--model "$model")
997
+ RUNNER_CMD+=(-c "model_reasoning_effort=\"$effort\"")
998
+ [[ "${#extras[@]}" -eq 0 ]] || RUNNER_CMD+=("${extras[@]}")
999
+ [[ "$mode" == resume ]] && RUNNER_CMD+=("$session")
1000
+ if [[ -n "$prompt" ]]; then RUNNER_CMD+=("$prompt"); else RUNNER_CMD+=(-); fi
1001
+ }
1002
+
1003
+ # ---- the bare classification mode ----------------------------------------------------
1004
+
1005
+ # A context that answers typed questions and does nothing else: no tools, no settings sources,
1006
+ # no MCP servers, no skills, a given system prompt, a structured-output schema and a wall-time
1007
+ # cap. The second reader's LLM backend runs here, on the profile's host and subscription, with
1008
+ # the API keys stripped the way every other context is started. RUNNER_CLASSIFY_SECONDS
1009
+ # overrides the cap, which the tests set low.
1010
+ RUNNER_CLASSIFY_SECONDS_DEFAULT=90
1011
+
1012
+ # runner_classify_command <system-prompt-file> <prompt-file> <schema-file> <final-file> <model> <effort>
1013
+ # fills RUNNER_CMD. An empty model is the profile's, which on the Claude profiles is the host's
1014
+ # default. Codex has no tool switch, so the read-only sandbox, no user config and no rules
1015
+ # files are the nearest it has, and its instructions travel as a config value.
1016
+ runner_classify_command() {
1017
+ local system="$1" prompt="$2" schema="$3" final="$4" model="$5" effort="${6:-$RUNNER_EFFORT}"
1018
+ [[ -f "$system" && -f "$prompt" && -f "$schema" ]] || {
1019
+ echo "runner: classify needs a system prompt, a prompt and a schema file" >&2
1020
+ return 1
1021
+ }
1022
+ [[ -n "$model" ]] || model="$RUNNER_MODEL"
1023
+ if [[ "$RUNNER_HOST" == claude ]]; then
1024
+ RUNNER_CMD=(claude -p "$(cat "$prompt")" --system-prompt "$(cat "$system")"
1025
+ --tools "" --setting-sources "" --strict-mcp-config --disable-slash-commands --no-session-persistence
1026
+ --output-format json --json-schema "$(_runner_schema_text "$schema")")
1027
+ [[ -z "$model" ]] || RUNNER_CMD+=(--model "$model")
1028
+ RUNNER_CMD+=(--effort "$effort")
1029
+ return 0
1030
+ fi
1031
+ RUNNER_CMD=(codex exec --json --output-schema "$schema" -o "$final"
1032
+ --sandbox read-only --ignore-user-config --ignore-rules --ephemeral --skip-git-repo-check
1033
+ -c 'mcp_servers={}' -c "developer_instructions=$(jq -Rs . "$system")")
1034
+ [[ -z "$model" ]] || RUNNER_CMD+=(--model "$model")
1035
+ RUNNER_CMD+=(-c "model_reasoning_effort=\"$effort\"" "$(cat "$prompt")")
1036
+ }
1037
+
1038
+ # _runner_bounded <seconds> <command...>: the command, killed past the cap. Returns its exit,
1039
+ # or 124 when the cap killed it.
1040
+ _runner_bounded() {
1041
+ local secs="$1" pid dog code
1042
+ shift
1043
+ "$@" &
1044
+ pid=$!
1045
+ ( trap 'kill "$s" 2>/dev/null; exit 0' TERM; sleep "$secs" & s=$!; wait "$s"; echo killed > "${RUNNER_BOUNDED_MARK:-/dev/null}"; kill "$pid" 2>/dev/null ) &
1046
+ dog=$!
1047
+ wait "$pid"
1048
+ code=$?
1049
+ kill "$dog" 2>/dev/null
1050
+ wait "$dog" 2>/dev/null
1051
+ if [[ -n "${RUNNER_BOUNDED_MARK:-}" && -s "$RUNNER_BOUNDED_MARK" ]]; then return 124; fi
1052
+ return "$code"
1053
+ }
1054
+
1055
+ # runner_classify <result> <final> <stderr> <system-prompt-file> <prompt-file> <schema-file> <model> <effort>
1056
+ # runs one bare context and writes the common result envelope. Returns the host's exit, 124
1057
+ # when the cap killed it, with an error envelope that says so.
1058
+ runner_classify() {
1059
+ local result="$1" final="$2" err="$3" system="$4" prompt="$5" schema="$6" model="$7" effort="${8:-$RUNNER_EFFORT}"
1060
+ local raw="${result}.raw" secs="${RUNNER_CLASSIFY_SECONDS:-$RUNNER_CLASSIFY_SECONDS_DEFAULT}" host_exit
1061
+ runner_classify_command "$system" "$prompt" "$schema" "$final" "$model" "$effort" || return 1
1062
+ rm -f "$raw" "$final"
1063
+ RUNNER_BOUNDED_MARK="${result}.killed"
1064
+ rm -f "$RUNNER_BOUNDED_MARK"
1065
+ if [[ "$RUNNER_HOST" == codex ]]; then
1066
+ _runner_bounded "$secs" env -u OPENAI_API_KEY -u CODEX_API_KEY "${RUNNER_CMD[@]}" > "$raw" 2> "$err"
1067
+ else
1068
+ _runner_bounded "$secs" env -u ANTHROPIC_API_KEY "${RUNNER_CMD[@]}" > "$raw" 2> "$err"
1069
+ fi
1070
+ host_exit=$?
1071
+ rm -f "$RUNNER_BOUNDED_MARK"
1072
+ unset RUNNER_BOUNDED_MARK
1073
+ if [[ "$host_exit" -eq 124 ]]; then
1074
+ jq -n --arg profile "$RUNNER_PROFILE" --arg host "$RUNNER_HOST" --arg billing "$RUNNER_BILLING" --arg effort "$effort" \
1075
+ --arg result "timed out after $secs s" \
1076
+ '{type:"result",subtype:"error_during_execution",is_error:true,profile:$profile,host:$host,billing:$billing,effort:$effort,cost_usd:null,reported_usage:null,model_observed:null,subagents_spawned:null,usage:null,session_id:"none",result:$result,structured_output:null}' > "$result"
1077
+ return 124
1078
+ fi
1079
+ if [[ "$RUNNER_HOST" == codex ]]; then
1080
+ runner_normalize_codex "$raw" "$final" "$result" "$host_exit" "$err"
1081
+ else
1082
+ RUNNER_EFFORT="$effort" runner_normalize_claude "$raw" "$result"
1083
+ fi
1084
+ return "$host_exit"
1085
+ }
1086
+
1087
+ runner_normalize_claude() {
1088
+ local raw="$1" result="$2"
1089
+ if ! jq --arg profile "$RUNNER_PROFILE" --arg host "$RUNNER_HOST" --arg billing "$RUNNER_BILLING" --arg effort "$RUNNER_EFFORT" '
1090
+ . + {
1091
+ profile: $profile,
1092
+ host: $host,
1093
+ billing: $billing,
1094
+ effort: $effort,
1095
+ cost_usd: (.total_cost_usd // null),
1096
+ reported_usage: (.usage // null),
1097
+ model_observed: (.modelUsage // {} | [to_entries[] | .value.canonicalModel // .key] | if length == 0 then null else . end),
1098
+ subagents_spawned: (.subagent_stats.spawned // null),
1099
+ usage: (.usage // {
1100
+ input_tokens: (.input_tokens // 0),
1101
+ cache_creation_input_tokens: (.cache_creation_input_tokens // 0),
1102
+ cache_read_input_tokens: (.cache_read_input_tokens // 0),
1103
+ output_tokens: (.output_tokens // 0)
1104
+ })
1105
+ }' "$raw" > "$result" 2>/dev/null; then
1106
+ jq -n --arg profile "$RUNNER_PROFILE" --arg host "$RUNNER_HOST" --arg billing "$RUNNER_BILLING" --arg effort "$RUNNER_EFFORT" \
1107
+ '{type:"result",subtype:"error_during_execution",is_error:true,profile:$profile,host:$host,billing:$billing,effort:$effort,cost_usd:null,reported_usage:null,model_observed:null,subagents_spawned:null,usage:null,session_id:"none",result:"host returned invalid JSON",structured_output:null}' > "$result"
1108
+ fi
1109
+ }
1110
+
1111
+ runner_normalize_codex() {
1112
+ local events="$1" final="$2" result="$3" host_exit="$4" stderr_file="${5:-}" final_text="" diagnostic=""
1113
+ [[ ! -f "$final" ]] || final_text="$(cat "$final")"
1114
+ if [[ "$host_exit" -ne 0 && -n "$stderr_file" && -f "$stderr_file" ]]; then
1115
+ diagnostic="$(LC_ALL=C sed -n '/[^[:space:]]/{s/^[[:space:]]*//;s/[[:space:]]*$//;p;q;}' "$stderr_file" | cut -c1-300)"
1116
+ fi
1117
+ if ! jq -s --arg profile "$RUNNER_PROFILE" --arg host "$RUNNER_HOST" --arg billing "$RUNNER_BILLING" --arg effort "$RUNNER_EFFORT" \
1118
+ --arg final "$final_text" --arg diagnostic "$diagnostic" --argjson host_exit "$host_exit" '
1119
+ ([.[] | select(.type == "thread.started") | .thread_id] | first // "none") as $session
1120
+ | ([.[] | select(.type == "turn.completed") | .usage] | last // null) as $usage
1121
+ | ([.[] | select(.type == "turn.failed" or .type == "error")
1122
+ | (.error.message // .message // .error // empty)] | last // "") as $error
1123
+ | ([.[] | .. | objects | (.resetsAt // .resets_at // empty)] | first // null) as $reset
1124
+ | (try ($final | fromjson) catch null) as $structured
1125
+ | {
1126
+ type: "result",
1127
+ subtype: (if $host_exit == 0 then "success" else "error_during_execution" end),
1128
+ is_error: ($host_exit != 0 or ($error | length) > 0),
1129
+ profile: $profile,
1130
+ host: $host,
1131
+ billing: $billing,
1132
+ effort: $effort,
1133
+ cost_usd: null,
1134
+ total_cost_usd: null,
1135
+ usage: $usage,
1136
+ reported_usage: $usage,
1137
+ model_observed: null,
1138
+ subagents_spawned: null,
1139
+ session_id: $session,
1140
+ result: (if ($error | length) > 0 then $error
1141
+ elif $host_exit != 0 and ($final | length) == 0 then $diagnostic
1142
+ else $final end),
1143
+ structured_output: $structured,
1144
+ resets_at: $reset
1145
+ }' "$events" > "$result" 2>/dev/null; then
1146
+ jq -n --arg profile "$RUNNER_PROFILE" --arg billing "$RUNNER_BILLING" --arg effort "$RUNNER_EFFORT" --arg diagnostic "$diagnostic" \
1147
+ '{type:"result",subtype:"error_during_execution",is_error:true,profile:$profile,host:"codex",billing:$billing,effort:$effort,cost_usd:null,total_cost_usd:null,usage:null,reported_usage:null,model_observed:null,subagents_spawned:null,session_id:"none",result:(if ($diagnostic | length) > 0 then $diagnostic else "host returned invalid JSONL" end),structured_output:null}' > "$result"
1148
+ fi
1149
+ }
1150
+
1151
+ # runner_execute <start|resume> <result> <final> <stderr> <prompt> <stdin-file> <session>
1152
+ # <model> <budget> <keep-api-key> <effort>. Writes the common result envelope and returns the host exit.
1153
+ runner_execute() {
1154
+ local mode="$1" result="$2" final="$3" err="$4" prompt="$5" input="$6" session="$7"
1155
+ local model="$8" budget="$9" keep_api_key="${10}" effort="${11:-$RUNNER_EFFORT}" raw="${result}.raw" host_exit
1156
+ RUNNER_EFFORT="$effort"
1157
+ runner_prepare_command "$mode" "$prompt" "${RUNNER_SCHEMA:-}" "$final" "$model" "$budget" "$session" "$effort" || return 1
1158
+ runner_process_limit
1159
+ rm -f "$raw" "$final"
1160
+ if [[ "$RUNNER_HOST" == codex ]]; then
1161
+ if [[ -n "$input" ]]; then
1162
+ _runner_limited env -u OPENAI_API_KEY -u CODEX_API_KEY "${RUNNER_CMD[@]}" < "$input" > "$raw" 2> "$err"
1163
+ else
1164
+ _runner_limited env -u OPENAI_API_KEY -u CODEX_API_KEY "${RUNNER_CMD[@]}" > "$raw" 2> "$err"
1165
+ fi
1166
+ host_exit=$?
1167
+ runner_normalize_codex "$raw" "$final" "$result" "$host_exit" "$err"
1168
+ else
1169
+ if [[ -n "$input" ]]; then
1170
+ if [[ "$keep_api_key" -eq 1 ]]; then _runner_limited "${RUNNER_CMD[@]}" < "$input" > "$raw" 2> "$err"; else _runner_limited env -u ANTHROPIC_API_KEY "${RUNNER_CMD[@]}" < "$input" > "$raw" 2> "$err"; fi
1171
+ else
1172
+ if [[ "$keep_api_key" -eq 1 ]]; then _runner_limited "${RUNNER_CMD[@]}" > "$raw" 2> "$err"; else _runner_limited env -u ANTHROPIC_API_KEY "${RUNNER_CMD[@]}" > "$raw" 2> "$err"; fi
1173
+ fi
1174
+ host_exit=$?
1175
+ runner_normalize_claude "$raw" "$result"
1176
+ fi
1177
+ return "$host_exit"
1178
+ }