cohorte 3.0.0 → 3.1.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 (69) hide show
  1. package/CHANGELOG.md +122 -0
  2. package/README.md +16 -10
  3. package/bin/cli.js +2 -1
  4. package/core/agents/cohorte-profile-reader.md +21 -31
  5. package/core/agents/cohorte-release.md +6 -3
  6. package/core/agents/cohorte-review.md +31 -5
  7. package/core/agents/implementer.template.md +13 -5
  8. package/core/commands/cohorte-align-ds.md +1 -1
  9. package/core/commands/cohorte-audit.md +11 -4
  10. package/core/commands/cohorte-brainstorm.md +2 -2
  11. package/core/commands/cohorte-build.md +19 -12
  12. package/core/commands/cohorte-doctor.md +11 -9
  13. package/core/commands/cohorte-fix.md +3 -3
  14. package/core/commands/cohorte-fleet.md +2 -2
  15. package/core/commands/cohorte-init-pipeline.md +2 -2
  16. package/core/commands/cohorte-intake.md +4 -4
  17. package/core/commands/cohorte-patch.md +2 -2
  18. package/core/commands/cohorte-refactor.md +73 -8
  19. package/core/commands/cohorte-retro.md +3 -3
  20. package/core/commands/cohorte-revert.md +104 -0
  21. package/core/commands/cohorte-review.md +32 -17
  22. package/core/commands/cohorte-ship.md +26 -9
  23. package/core/commands/cohorte-spec.md +13 -4
  24. package/core/commands/cohorte-update-pipeline.md +12 -8
  25. package/core/commands/cohorte-verify.md +106 -0
  26. package/core/hooks/gate.py +326 -21
  27. package/core/templates/patch.template.md +11 -2
  28. package/core/templates/spec.template.md +23 -2
  29. package/core/templates/steps/init-pipeline/01-detect-stack.md +14 -5
  30. package/core/templates/steps/init-pipeline/02-interview-gaps.md +26 -3
  31. package/core/templates/steps/init-pipeline/03-draft-profile.md +2 -1
  32. package/core/templates/steps/init-pipeline/04-write-render.md +16 -12
  33. package/core/templates/steps/init-pipeline/05-report.md +1 -1
  34. package/core/workflows/audit.js +9 -4
  35. package/core/workflows/loop.js +15 -10
  36. package/core/workflows/refactor.js +116 -9
  37. package/core/workflows/review.js +24 -10
  38. package/lib/doctor.js +37 -3
  39. package/lib/install.js +68 -33
  40. package/lib/project.js +45 -8
  41. package/lib/render.js +3 -3
  42. package/lib/yaml.js +10 -1
  43. package/package.json +2 -2
  44. package/profile/PIPELINE.template.md +39 -1
  45. package/profile/SCHEMA.md +27 -746
  46. package/profile/cohorte.config.template.yaml +1 -1
  47. package/profile/schema/dead-agents.md +29 -0
  48. package/profile/schema/decisions.md +23 -0
  49. package/profile/schema/deferred.md +22 -0
  50. package/profile/schema/feature-diff.md +20 -0
  51. package/profile/schema/kanban.md +95 -0
  52. package/profile/schema/metrics.md +23 -0
  53. package/profile/schema/output-discipline.md +21 -0
  54. package/profile/schema/preflight.md +81 -0
  55. package/profile/schema/profile-block.md +103 -0
  56. package/profile/schema/project-kinds.md +89 -0
  57. package/profile/schema/readiness.md +17 -0
  58. package/profile/schema/reconcile.md +64 -0
  59. package/profile/schema/release-notes.md +29 -0
  60. package/profile/schema/rendering.md +48 -0
  61. package/profile/schema/retrieval.md +58 -0
  62. package/profile/schema/spec-status.md +79 -0
  63. package/profile/schema/specialization.md +41 -0
  64. package/profile/schema/verify.md +115 -0
  65. package/profile/schema/workflows.md +78 -0
  66. package/scripts/kanban-move.sh +45 -3
  67. package/scripts/new-feature.sh.template +7 -1
  68. package/scripts/remove-feature.sh.template +14 -5
  69. package/scripts/verify-run.sh +509 -0
@@ -14,11 +14,20 @@ set -euo pipefail
14
14
 
15
15
  id="${1:-}"
16
16
  drop_db="no"
17
- [[ "${2:-}" == "--drop-db" ]] && drop_db="yes"
18
-
19
- if [[ -z "$id" ]]; then
20
- echo "usage: scripts/remove-feature.sh <feature_id> [--drop-db]" >&2
21
- exit 1
17
+ usage() { echo "usage: scripts/remove-feature.sh <feature_id> [--drop-db]" >&2; exit 1; }
18
+ [[ -n "$id" ]] || usage
19
+ shift
20
+ # Every flag is parsed: an unknown one (a typo'd --drop-db, a --force this script never had) used
21
+ # to be ignored silently, so the teardown ran with options the human did not get.
22
+ for arg in "$@"; do
23
+ case "$arg" in
24
+ --drop-db) drop_db="yes" ;;
25
+ *) echo "error: unknown option \"$arg\"" >&2; usage ;;
26
+ esac
27
+ done
28
+ if [[ ! "$id" =~ ^[a-z0-9][a-z0-9-]{0,62}$ ]]; then
29
+ echo "error: feature id \"$id\" must be kebab-case (a-z, 0-9, -)" >&2
30
+ exit 2
22
31
  fi
23
32
 
24
33
  repo_root="$(git -C "$(dirname "${BASH_SOURCE[0]}")" rev-parse --show-toplevel)"
@@ -0,0 +1,509 @@
1
+ #!/usr/bin/env bash
2
+ #
3
+ # verify-run.sh — the deterministic half of /cohorte-verify (and of /cohorte-revert's down
4
+ # migration). It owns everything a prompt must not improvise: starting the dev server in its
5
+ # own process group, probing it, running the checks under timeouts, stopping the server on
6
+ # EVERY exit path, refusing to migrate a shared database, and writing the machine verdict.
7
+ # Doctrine: profile/schema/verify.md.
8
+ #
9
+ # verify-run.sh run <id> [options] run the checks, write <out>/<id>.<phase>.json + .md
10
+ # verify-run.sh rollback-check <spec.md> exit 1 when `migrations: true` and ## Rollback is empty
11
+ # verify-run.sh stop <id> [--out <dir>] [--phase <p>] reap a server a killed run left behind
12
+ #
13
+ # run options (every command is passed literally, as the profile spells it):
14
+ # --dev "<cmd>" start the app in the background (its output: <evidence>/dev.log)
15
+ # --ready "<url|cmd>" ready probe: an http(s) URL must answer 2xx/3xx, a command exit 0
16
+ # --check "<name>=<cmd>" repeatable: smoke, e2e, smoke:<surface> … run in order, app up
17
+ # --screenshot "<name>=<url>" repeatable: evidence only (a local Playwright CLI, never npx install)
18
+ # --migrate "<cmd>" apply migrations before the app starts
19
+ # --migrate-down "<cmd>" verify phase: down, then --migrate again (check `rollback`);
20
+ # revert phase: [--migrate first, then] down (check `migrate-down`)
21
+ # --db local|shared required with any migration flag (profile verify.db)
22
+ # --db-per-worktree true|false (profile isolation.db_per_worktree)
23
+ # --slots <path> the isolation slot registry (profile isolation.registry, .worktrees/slots.tsv)
24
+ # --skip "<name>=<reason>" record a check as SKIPPED without running anything
25
+ # --digest-base <sha> record feature-diff.sh --digest of the verified tree (ship compares it)
26
+ # --phase verify|revert names the outputs (default verify)
27
+ # --out <dir> default specs/reports
28
+ # --ready-timeout <s> (90) --check-timeout <s> (600) --deadline <s> (540, whole run)
29
+ # --confirmed the human approved, in this session, the commands the gate would ask for
30
+ #
31
+ # The project gate (.claude/hooks/cohorte-gate.py) sees only `verify-run.sh …` — so this script
32
+ # asks it, in `--check` mode, about every command it was handed BEFORE running any of them:
33
+ # `deny` ⇒ that check is SKIPPED (never run); `ask` ⇒ exit 4 with the list, nothing started,
34
+ # unless --confirmed. The gate stays the authority; this script never widens what it allows.
35
+ #
36
+ # Exit: 0 PASS · 1 FAIL · 3 SKIPPED (nothing verified) · 4 needs the human's confirmation · 2 usage.
37
+ # Never touches git state: no checkout, commit, push or merge — it reads HEAD and diffs, that's all.
38
+
39
+ set -u
40
+
41
+ die() { echo "verify-run: $*" >&2; exit 2; }
42
+
43
+ json_str() {
44
+ printf '%s' "$1" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' -e 's/ /\\t/g' -e 's/\r//g' \
45
+ | awk 'NR > 1 { printf "\\n" } { printf "%s", $0 }'
46
+ }
47
+
48
+ # ── rollback-check ─────────────────────────────────────────────────────────────
49
+ rollback_check() {
50
+ spec="${1:?usage: verify-run.sh rollback-check <spec.md>}"
51
+ [ -f "$spec" ] || die "no such spec: $spec"
52
+ # A missing `migrations:` key means false — legacy specs predate the field.
53
+ if ! awk '{ sub(/\r$/, "") } NR == 1 && $0 != "---" { exit 1 } NR > 1 && $0 == "---" { exit 1 }
54
+ NR > 1 && /^migrations:[ \t]*true([ \t]|#|$)/ { found = 1; exit 0 }
55
+ END { exit found ? 0 : 1 }' "$spec"; then
56
+ echo "rollback: spec declares no migrations — nothing to check"
57
+ exit 0
58
+ fi
59
+ # Filled = at least one line in ## Rollback that is not blank, not a `>` note, not a comment,
60
+ # and not a template stub (`- **Label:** …` with nothing but an ellipsis / <placeholder>).
61
+ filled=$(awk '
62
+ { sub(/\r$/, "") }
63
+ /^## / { inside = ($0 ~ /^## Rollback[ \t]*$/); next }
64
+ !inside { next }
65
+ /^[ \t]*$/ || /^[ \t]*>/ || /^[ \t]*<!--/ { next }
66
+ {
67
+ l = $0
68
+ gsub(/<!--.*-->/, "", l)
69
+ sub(/^[ \t]*[-*][ \t]*/, "", l)
70
+ sub(/^\*\*[^*]*\*\*[ \t]*:?[ \t]*/, "", l)
71
+ gsub(/[ \t]/, "", l)
72
+ if (l == "" || l == "…" || l == "..." || l ~ /^<[^>]*>$/ || l == "`none`" || l == "none") next
73
+ n++
74
+ }
75
+ END { print n + 0 }' "$spec")
76
+ if [ "$filled" -gt 0 ]; then
77
+ echo "rollback: filled ($filled line(s))"
78
+ exit 0
79
+ fi
80
+ echo "rollback: EMPTY — the spec says \`migrations: true\` but ## Rollback has nothing to follow" >&2
81
+ exit 1
82
+ }
83
+
84
+ # ── process groups ────────────────────────────────────────────────────────────
85
+ # `pnpm dev` is a tree (pnpm → sh → node → watchers). Killing $! kills the wrapper and orphans the
86
+ # server on its port, so everything is started in its own process GROUP (job control, `set -m`)
87
+ # and stopped as a group, plus any descendant that left it: TERM, a grace period, then KILL.
88
+ # bash, not sh: dash refuses `set -m` without a tty, and stock macOS has no setsid/timeout.
89
+ # Every live group is listed in <evidence>/pgids: the EXIT trap stops them all, and a run that
90
+ # was SIGKILLed (no trap runs) is reaped by `stop` or by the next run from that list.
91
+ spawn() { # spawn <logfile> <cmd> — sets $spawned to the group leader's pid
92
+ set -m
93
+ sh -c "$2" > "$1" 2>&1 < /dev/null &
94
+ spawned=$!
95
+ set +m
96
+ remember "$spawned"
97
+ }
98
+
99
+ remember() { [ -n "${registry:-}" ] && echo "$1" >> "$registry"; return 0; }
100
+ forget() {
101
+ [ -n "${registry:-}" ] && [ -f "$registry" ] || return 0
102
+ grep -vxF "$1" "$registry" > "$registry.tmp" 2>/dev/null; mv -f "$registry.tmp" "$registry"
103
+ [ -s "$registry" ] || rm -f "$registry"
104
+ }
105
+ reap_all() { # reap_all <registry> — stop every group a run recorded
106
+ [ -f "$1" ] || return 0
107
+ for g in $(cat "$1"); do stop_group "$g" 5; done
108
+ rm -f "$1"
109
+ }
110
+
111
+ group_alive() { kill -0 "-$1" 2>/dev/null || kill -0 "$1" 2>/dev/null; }
112
+
113
+ descendants() { # every live descendant pid of $1 (a child that left the group is still ours)
114
+ ps -A -o pid= -o ppid= 2>/dev/null | awk -v root="$1" '
115
+ { kids[$2] = kids[$2] " " $1 }
116
+ END { n = split(root, q, " "); while (n > 0) { p = q[n--]; m = split(kids[p], c, " ");
117
+ for (i = 1; i <= m; i++) { print c[i]; q[++n] = c[i] } } }'
118
+ }
119
+
120
+ stop_group() { # stop_group <pgid> [grace seconds]
121
+ g="$1"; grace="${2:-10}"
122
+ tree=$(descendants "$g")
123
+ group_alive "$g" || [ -n "$tree" ] || return 0
124
+ kill -TERM "-$g" 2>/dev/null || kill -TERM "$g" 2>/dev/null
125
+ for p in $tree; do kill -TERM "$p" 2>/dev/null; done
126
+ i=0
127
+ while [ "$i" -lt "$((grace * 5))" ]; do
128
+ alive=0; group_alive "$g" && alive=1
129
+ for p in $tree; do kill -0 "$p" 2>/dev/null && alive=1; done
130
+ [ "$alive" -eq 1 ] || break
131
+ sleep 0.2 2>/dev/null || sleep 1; i=$((i + 1))
132
+ done
133
+ kill -KILL "-$g" 2>/dev/null
134
+ for p in $tree; do kill -KILL "$p" 2>/dev/null; done
135
+ wait "$g" 2>/dev/null || true
136
+ }
137
+
138
+ now() { date +%s; }
139
+
140
+ # timed <seconds> <logfile> <cmd> — run in its own group; $timed_status = exit code (124 = timeout)
141
+ timed() {
142
+ t="$1"; [ "$t" -gt 0 ] || t=1
143
+ marker="$evdir/.timeout.$$"
144
+ rm -f "$marker"
145
+ spawn "$2" "$3"; job=$spawned
146
+ set -m
147
+ ( sleep "$t"; : > "$marker"; kill -TERM "-$job" 2>/dev/null; sleep 5; kill -KILL "-$job" 2>/dev/null ) > /dev/null 2>&1 &
148
+ dog=$!
149
+ set +m
150
+ remember "$dog"
151
+ wait "$job" 2>/dev/null; timed_status=$?
152
+ stop_group "$dog" 1; forget "$dog"
153
+ # A timed-out check's own children may survive the wrapper: reap the group either way.
154
+ stop_group "$job" 2; forget "$job"
155
+ if [ -f "$marker" ]; then timed_status=124; rm -f "$marker"; fi
156
+ }
157
+
158
+ # ── results ───────────────────────────────────────────────────────────────────
159
+ checks_json=""
160
+ evidence_json=""
161
+ md_rows=""
162
+ any_pass=0
163
+ any_fail=0
164
+
165
+ add_check() { # add_check <name> <PASS|FAIL|SKIPPED> <exit|""> <seconds> <cmd> <log> <note>
166
+ [ "$2" = PASS ] && any_pass=1
167
+ [ "$2" = FAIL ] && any_fail=1
168
+ e="${3:-null}"
169
+ entry="{\"name\":\"$(json_str "$1")\",\"status\":\"$2\",\"exit\":$e,\"seconds\":${4:-0},\"cmd\":\"$(json_str "$5")\",\"log\":\"$(json_str "$6")\",\"note\":\"$(json_str "$7")\"}"
170
+ checks_json="${checks_json:+$checks_json,}$entry"
171
+ md_rows="$md_rows| \`$1\` | $2 | ${6:+\`$6\`} | $7 |
172
+ "
173
+ [ -n "$6" ] && add_evidence "$6"
174
+ echo " $2 $1${7:+ — $7}"
175
+ }
176
+
177
+ add_evidence() {
178
+ case ",$evidence_json," in *"\"$(json_str "$1")\""*) return ;; esac
179
+ evidence_json="${evidence_json:+$evidence_json,}\"$(json_str "$1")\""
180
+ }
181
+
182
+ write_report() { # write_report <verdict> <reason>
183
+ ts=$(date -u +%Y-%m-%dT%H:%M:%SZ)
184
+ head_sha=$(git rev-parse HEAD 2>/dev/null || echo none)
185
+ d1=null; [ -n "$digest" ] && d1="\"$digest\""
186
+ d2=null; [ -n "$digest_after" ] && d2="\"$digest_after\""
187
+ printf '{"id":"%s","phase":"%s","ts":"%s","verdict":"%s","reason":"%s","head":"%s","digest_base":%s,"digest":%s,"digest_after":%s,"checks":[%s],"evidence":[%s]}\n' \
188
+ "$(json_str "$id")" "$phase" "$ts" "$1" "$(json_str "$2")" "$head_sha" \
189
+ "$( [ -n "$digest_base" ] && printf '"%s"' "$digest_base" || printf null)" "$d1" "$d2" \
190
+ "$checks_json" "$evidence_json" > "$json"
191
+ {
192
+ echo "# $phase — $id"
193
+ echo
194
+ echo "**Verdict: $1**${2:+ — $2} "
195
+ echo "\`$ts\` · HEAD \`$head_sha\` · digest \`${digest:-none}\`"
196
+ echo
197
+ echo "| check | status | evidence | note |"
198
+ echo "| --- | --- | --- | --- |"
199
+ printf '%s' "$md_rows"
200
+ echo
201
+ echo "Machine verdict: \`$json\`. Logs and screenshots: \`$evdir/\`."
202
+ } > "$md"
203
+ final_written=1
204
+ }
205
+
206
+ # ── run ───────────────────────────────────────────────────────────────────────
207
+ run() {
208
+ id="${1:?usage: verify-run.sh run <id> [options]}"; shift
209
+ out=specs/reports; phase=verify; dev=""; ready=""; migrate=""; down=""; db=""; dbwt=false; registry=""
210
+ ready_to=90; check_to=600; deadline=540; confirmed=0; digest_base=""; slots=.worktrees/slots.tsv
211
+ nchecks=0; nshots=0; nskips=0
212
+ checks_file=$(mktemp) || die "mktemp failed"; shots_file=$(mktemp); skips_file=$(mktemp)
213
+ while [ "$#" -gt 0 ]; do
214
+ case "$1" in
215
+ --dev) dev="${2-}"; shift 2 ;;
216
+ --ready) ready="${2-}"; shift 2 ;;
217
+ --check) printf '%s\n' "${2-}" >> "$checks_file"; nchecks=$((nchecks + 1)); shift 2 ;;
218
+ --screenshot) printf '%s\n' "${2-}" >> "$shots_file"; nshots=$((nshots + 1)); shift 2 ;;
219
+ --skip) printf '%s\n' "${2-}" >> "$skips_file"; nskips=$((nskips + 1)); shift 2 ;;
220
+ --migrate) migrate="${2-}"; shift 2 ;;
221
+ --migrate-down) down="${2-}"; shift 2 ;;
222
+ --db) db="${2-}"; shift 2 ;;
223
+ --db-per-worktree) dbwt="${2-}"; shift 2 ;;
224
+ --slots) slots="${2-}"; shift 2 ;;
225
+ --digest-base) digest_base="${2-}"; shift 2 ;;
226
+ --phase) phase="${2-}"; shift 2 ;;
227
+ --out) out="${2-}"; shift 2 ;;
228
+ --ready-timeout) ready_to="${2-}"; shift 2 ;;
229
+ --check-timeout) check_to="${2-}"; shift 2 ;;
230
+ --deadline) deadline="${2-}"; shift 2 ;;
231
+ --confirmed) confirmed=1; shift ;;
232
+ *) die "unknown option: $1" ;;
233
+ esac
234
+ done
235
+ case "$phase" in verify|revert) ;; *) die "--phase must be verify or revert" ;; esac
236
+ [ -z "$dev" ] || [ -n "$ready" ] || die "--dev needs --ready (a server nobody probes is a guess)"
237
+ if [ -n "$migrate$down" ]; then
238
+ case "$db" in local|shared) ;; *) die "--db local|shared is required with a migration flag" ;; esac
239
+ fi
240
+ [ "$phase" = verify ] && [ -n "$down" ] && [ -z "$migrate" ] && die "--migrate-down needs --migrate (down, then up again)"
241
+ [ "$phase" = revert ] && [ -z "$down" ] && die "--phase revert needs --migrate-down"
242
+
243
+ top=$(git rev-parse --show-toplevel 2>/dev/null || pwd)
244
+ cd "$top" || die "cannot cd to $top"
245
+ mkdir -p "$out" || die "cannot create $out"
246
+ json="$out/$id.$phase.json"; md="$out/$id.$phase.md"; evdir="$out/$id.$phase"
247
+ mkdir -p "$evdir"
248
+ digest=""; digest_after=""; final_written=0; server=""; started=$(now)
249
+
250
+ # A run that was killed outright (SIGKILL skips traps) left its groups behind: reap them first.
251
+ if [ -f "$evdir/pgids" ]; then
252
+ echo "verify-run: reaping what a killed run left behind ($(tr '\n' ' ' < "$evdir/pgids"))"
253
+ reap_all "$evdir/pgids"
254
+ fi
255
+ registry="$evdir/pgids"
256
+
257
+ # ── ask the gate about every command, before running any ──
258
+ gate="$top/.claude/hooks/cohorte-gate.py"
259
+ if [ ! -f "$gate" ]; then
260
+ common=$(git rev-parse --git-common-dir 2>/dev/null || echo "")
261
+ [ -n "$common" ] && gate="$(cd "$(dirname "$common")" 2>/dev/null && pwd)/.claude/hooks/cohorte-gate.py"
262
+ fi
263
+ denied_file=$(mktemp); asks=""
264
+ gate_verdict() { # prints allow|ask|deny
265
+ [ -f "$gate" ] || { echo allow; return; }
266
+ command -v python3 > /dev/null 2>&1 || { echo ask; return; }
267
+ python3 "$gate" --check --cwd "$top" -- "$1" < /dev/null > /dev/null 2>&1
268
+ case $? in 0) echo allow ;; 2) echo deny ;; *) echo ask ;; esac
269
+ }
270
+ consider() { # consider <name> <cmd>
271
+ [ -n "$2" ] || return 0
272
+ case $(gate_verdict "$2") in
273
+ deny) printf '%s\n' "$1" >> "$denied_file" ;;
274
+ ask) asks="$asks
275
+ - $1: $2" ;;
276
+ esac
277
+ }
278
+ consider migrate "$migrate"
279
+ consider migrate-down "$down"
280
+ consider dev "$dev"
281
+ case "$ready" in http://*|https://*|"") ;; *) consider ready "$ready" ;; esac
282
+ while IFS= read -r spec_line; do
283
+ [ -n "$spec_line" ] || continue
284
+ consider "${spec_line%%=*}" "${spec_line#*=}"
285
+ done < "$checks_file"
286
+ if [ -n "$asks" ] && [ "$confirmed" -ne 1 ]; then
287
+ echo "verify-run: the project gate asks for confirmation before:$asks" >&2
288
+ echo "verify-run: nothing was started. Ask the human; on yes re-run with --confirmed." >&2
289
+ rm -f "$checks_file" "$shots_file" "$skips_file" "$denied_file"
290
+ exit 4
291
+ fi
292
+ is_denied() { grep -qxF "$1" "$denied_file"; }
293
+
294
+ cleanup() {
295
+ [ -n "$server" ] && { stop_group "$server" 10; forget "$server"; server=""; }
296
+ reap_all "$registry"
297
+ if [ "$final_written" -ne 1 ]; then
298
+ add_check run FAIL "" 0 "" "" "interrupted before the run completed"
299
+ write_report FAIL "interrupted"
300
+ fi
301
+ rm -f "$checks_file" "$shots_file" "$skips_file" "$denied_file" 2>/dev/null
302
+ }
303
+ trap cleanup EXIT
304
+ trap 'exit 130' INT
305
+ trap 'exit 143' TERM HUP
306
+
307
+ # Crash-safe: an old PASS must never survive a run that dies half-way.
308
+ printf '{"id":"%s","phase":"%s","verdict":"FAIL","reason":"incomplete — a run is in progress or died","checks":[],"evidence":[]}\n' \
309
+ "$(json_str "$id")" "$phase" > "$json"
310
+
311
+ remaining() { r=$((deadline - ($(now) - started))); [ "$r" -gt 0 ] && echo "$r" || echo 0; }
312
+ feature_digest() {
313
+ fd="$(dirname "$0")/feature-diff.sh"
314
+ [ -n "$digest_base" ] && [ -x "$fd" ] || return 0
315
+ "$fd" --base "$digest_base" --digest 2>/dev/null || true
316
+ }
317
+ digest=$(feature_digest)
318
+
319
+ while IFS= read -r s; do
320
+ [ -n "$s" ] && add_check "${s%%=*}" SKIPPED "" 0 "" "" "${s#*=}"
321
+ done < "$skips_file"
322
+
323
+ # run_step <name> <cmd> <timeout> — returns 0 on PASS
324
+ run_step() {
325
+ if is_denied "$1"; then add_check "$1" SKIPPED "" 0 "$2" "" "denied by the project gate (gate.deny) — not run"; return 2; fi
326
+ left=$(remaining)
327
+ [ "$left" -gt 0 ] || { add_check "$1" FAIL "" 0 "$2" "" "run deadline reached before it started"; return 1; }
328
+ [ "$3" -lt "$left" ] && tmo="$3" || tmo="$left"
329
+ log="$evdir/$(printf '%s' "$1" | tr -c 'A-Za-z0-9._-' '_').log"
330
+ t0=$(now); timed "$tmo" "$log" "$2"; st=$timed_status; dt=$(( $(now) - t0 ))
331
+ if [ "$st" -eq 0 ]; then add_check "$1" PASS 0 "$dt" "$2" "$log" ""; return 0; fi
332
+ if [ "$st" -eq 124 ]; then add_check "$1" FAIL 124 "$dt" "$2" "$log" "timed out after ${tmo}s"; else
333
+ add_check "$1" FAIL "$st" "$dt" "$2" "$log" "exit $st"; fi
334
+ return 1
335
+ }
336
+
337
+ verdict_and_exit() {
338
+ if [ "$any_fail" -eq 1 ]; then v=FAIL; code=1
339
+ elif [ "$any_pass" -eq 1 ]; then v=PASS; code=0
340
+ else v=SKIPPED; code=3; fi
341
+ write_report "$v" "$1"
342
+ echo "verify-run: $v — $json"
343
+ exit "$code"
344
+ }
345
+
346
+ # ── database guard: never a shared DB ──
347
+ if [ -n "$migrate$down" ]; then
348
+ # Its own DB = a linked worktree that new-feature.sh registered: a row for this branch's last
349
+ # path segment (feature/<id>, fix/patch-<slug>, feature/revert-<id>) in the slot registry. A
350
+ # bare `git worktree add` shares the main checkout's .env, and so its database.
351
+ own_db=0
352
+ if [ "$dbwt" = true ] && [ "$(git rev-parse --git-dir 2>/dev/null)" != "$(git rev-parse --git-common-dir 2>/dev/null)" ]; then
353
+ main_dir=$(cd "$(dirname "$(git rev-parse --git-common-dir)")" 2>/dev/null && pwd)
354
+ case "$slots" in /*) reg_file="$slots" ;; *) reg_file="$main_dir/$slots" ;; esac
355
+ wid=$(git rev-parse --abbrev-ref HEAD 2>/dev/null); wid="${wid##*/}"
356
+ [ -n "$wid" ] && [ -f "$reg_file" ] && awk -F'\t' -v w="$wid" '$1 == w { f = 1 } END { exit !f }' "$reg_file" && own_db=1
357
+ fi
358
+ if [ "$db" = shared ] && [ "$own_db" -ne 1 ]; then
359
+ why="the dev database is marked shared (verify.db: shared) and this checkout has no database of its own (a feature worktree registered in $slots, with isolation.db_per_worktree) — refusing to migrate it"
360
+ [ -n "$migrate" ] && add_check migrate SKIPPED "" 0 "$migrate" "" "$why"
361
+ [ -n "$down" ] && add_check "$( [ "$phase" = verify ] && echo rollback || echo migrate-down)" SKIPPED "" 0 "$down" "" "$why"
362
+ verdict_and_exit "shared database: nothing was migrated and the app was not started"
363
+ fi
364
+ fi
365
+
366
+ # revert: bring the (fresh, isolated) dev DB up to the default branch first when asked, then
367
+ # run the down step — BEFORE the revert deletes the migration files that hold its code.
368
+ if [ "$phase" = revert ]; then
369
+ if [ -n "$migrate" ] && ! run_step migrate "$migrate" "$check_to"; then
370
+ verdict_and_exit "migrations did not apply — the down step was not run"
371
+ fi
372
+ run_step migrate-down "$down" "$check_to" || true
373
+ verdict_and_exit ""
374
+ fi
375
+
376
+ if [ -n "$migrate" ]; then
377
+ if ! run_step migrate "$migrate" "$check_to"; then
378
+ verdict_and_exit "migrations did not apply — the app was not started"
379
+ fi
380
+ if [ -n "$down" ]; then
381
+ if is_denied migrate-down; then
382
+ add_check rollback SKIPPED "" 0 "$down" "" "denied by the project gate (gate.deny) — rollback not verified"
383
+ else
384
+ t0=$(now)
385
+ rlog="$evdir/rollback.log"
386
+ timed "$(remaining)" "$rlog" "$down"; s1=$timed_status
387
+ if [ "$s1" -eq 0 ]; then
388
+ timed "$(remaining)" "$evdir/rollback.reup.log" "$migrate"; s2=$timed_status
389
+ cat "$evdir/rollback.reup.log" >> "$rlog" 2>/dev/null; rm -f "$evdir/rollback.reup.log"
390
+ if [ "$s2" -eq 0 ]; then add_check rollback PASS 0 $(( $(now) - t0 )) "$down && $migrate" "$rlog" "down then up again"
391
+ else add_check rollback FAIL "$s2" $(( $(now) - t0 )) "$down && $migrate" "$rlog" "down worked, re-applying failed (exit $s2) — the dev DB is now behind"
392
+ verdict_and_exit "re-applying migrations after the rollback failed"; fi
393
+ else
394
+ add_check rollback FAIL "$s1" $(( $(now) - t0 )) "$down" "$rlog" "down migration failed (exit $s1)"
395
+ verdict_and_exit "the down migration failed"
396
+ fi
397
+ fi
398
+ fi
399
+ fi
400
+
401
+ base_url=""
402
+ case "$ready" in http://*|https://*) base_url=$(printf '%s' "$ready" | sed -E 's#^(https?://[^/]+).*#\1#') ;; esac
403
+ VERIFY_ARTIFACTS="$(cd "$evdir" && pwd)"; export VERIFY_ARTIFACTS
404
+ [ -n "$base_url" ] && { VERIFY_BASE_URL="$base_url"; export VERIFY_BASE_URL; }
405
+
406
+ probe() {
407
+ case "$ready" in
408
+ http://*|https://*)
409
+ if command -v curl > /dev/null 2>&1; then curl -s -f -o /dev/null --max-time 3 "$ready" > /dev/null 2>&1
410
+ elif command -v node > /dev/null 2>&1; then
411
+ node -e 'fetch(process.argv[1],{redirect:"manual",signal:AbortSignal.timeout(3000)}).then(r=>process.exit(r.status<400?0:1),()=>process.exit(1))' "$ready"
412
+ else wget -q -O /dev/null -T 3 "$ready" > /dev/null 2>&1; fi ;;
413
+ *) timed 10 "$evdir/.probe.log" "$ready"; [ "$timed_status" -eq 0 ] ;;
414
+ esac
415
+ }
416
+
417
+ if [ -n "$dev" ]; then
418
+ if is_denied dev; then
419
+ add_check dev SKIPPED "" 0 "$dev" "" "denied by the project gate (gate.deny) — app not started"
420
+ verdict_and_exit "the dev command is denied by the gate"
421
+ fi
422
+ if is_denied ready; then
423
+ add_check ready SKIPPED "" 0 "$ready" "" "the ready probe is denied by the project gate (gate.deny) — app not started"
424
+ verdict_and_exit "the ready probe is denied by the gate"
425
+ fi
426
+ # Something already answering means we would verify someone else's server.
427
+ if probe; then
428
+ add_check ready FAIL "" 0 "$ready" "" "the ready probe answered BEFORE the dev server started — another process holds it (a stale server, the main checkout's app?); stop it and re-run"
429
+ verdict_and_exit "the ready probe was already answering"
430
+ fi
431
+ spawn "$evdir/dev.log" "$dev"; server=$spawned
432
+ add_evidence "$evdir/dev.log"
433
+ t0=$(now)
434
+ [ "$ready_to" -lt "$(remaining)" ] && rt="$ready_to" || rt=$(remaining)
435
+ ok=0
436
+ while [ $(( $(now) - t0 )) -lt "$rt" ]; do
437
+ if ! kill -0 "$server" 2>/dev/null; then
438
+ wait "$server" 2>/dev/null; dst=$?
439
+ add_check ready FAIL "$dst" $(( $(now) - t0 )) "$ready" "$evdir/dev.log" "the dev server exited (status $dst) before it was ready"
440
+ stop_group "$server" 2; forget "$server"; server=""
441
+ verdict_and_exit "the dev server died before it was ready"
442
+ fi
443
+ if probe; then ok=1; break; fi
444
+ sleep 1
445
+ done
446
+ if [ "$ok" -ne 1 ]; then
447
+ add_check ready FAIL "" "$rt" "$ready" "$evdir/dev.log" "not ready after ${rt}s"
448
+ verdict_and_exit "the app never became ready"
449
+ fi
450
+ add_check ready PASS 0 $(( $(now) - t0 )) "$ready" "$evdir/dev.log" ""
451
+ fi
452
+
453
+ while IFS= read -r c; do
454
+ [ -n "$c" ] || continue
455
+ run_step "${c%%=*}" "${c#*=}" "$check_to" || true
456
+ done < "$checks_file"
457
+
458
+ if [ "$nshots" -gt 0 ]; then
459
+ pw=""
460
+ [ -x "$top/node_modules/.bin/playwright" ] && pw="$top/node_modules/.bin/playwright"
461
+ [ -z "$pw" ] && command -v playwright > /dev/null 2>&1 && pw=$(command -v playwright)
462
+ while IFS= read -r s; do
463
+ [ -n "$s" ] || continue
464
+ sname="${s%%=*}"; surl="${s#*=}"
465
+ if [ -z "$pw" ]; then add_check "screenshot:$sname" SKIPPED "" 0 "" "" "no local Playwright CLI (node_modules/.bin/playwright) — evidence not captured"; continue; fi
466
+ f="$evdir/$(printf '%s' "$sname" | tr -c 'A-Za-z0-9._-' '_')"
467
+ slog="$f.screenshot.log"
468
+ timed 60 "$slog" "\"$pw\" screenshot --viewport-size=390,844 \"$surl\" \"$f.mobile.png\" && \"$pw\" screenshot --viewport-size=1280,800 \"$surl\" \"$f.desktop.png\""
469
+ if [ "$timed_status" -eq 0 ]; then
470
+ add_check "screenshot:$sname" PASS 0 0 "$surl" "$f.mobile.png" "evidence for a human eye — it proves the page rendered, not that it is right"
471
+ add_evidence "$f.desktop.png"
472
+ else
473
+ # Evidence tooling failing (no browser installed) never fails the verdict.
474
+ add_check "screenshot:$sname" SKIPPED "$timed_status" 0 "$surl" "$slog" "screenshot failed — see the log"
475
+ fi
476
+ done < "$shots_file"
477
+ fi
478
+
479
+ if [ -n "$server" ]; then stop_group "$server" 10; forget "$server"; server=""; fi
480
+
481
+ if [ -n "$digest" ]; then
482
+ digest_after=$(feature_digest)
483
+ if [ -n "$digest_after" ] && [ "$digest_after" != "$digest" ]; then
484
+ add_check tree FAIL "" 0 "" "" "the checks changed the feature diff (a file written outside specs/ and .claude/, not gitignored) — /cohorte-ship would call the review stale; send outputs to \$VERIFY_ARTIFACTS or gitignore them"
485
+ fi
486
+ fi
487
+ verdict_and_exit ""
488
+ }
489
+
490
+ stop_cmd() {
491
+ id="${1:?usage: verify-run.sh stop <id> [--out <dir>] [--phase <p>]}"; shift
492
+ out=specs/reports; phase=verify
493
+ while [ "$#" -gt 0 ]; do
494
+ case "$1" in --out) out="$2"; shift 2 ;; --phase) phase="$2"; shift 2 ;; *) die "unknown option: $1" ;; esac
495
+ done
496
+ top=$(git rev-parse --show-toplevel 2>/dev/null || pwd); cd "$top" || exit 2
497
+ reg="$out/$id.$phase/pgids"
498
+ [ -f "$reg" ] || { echo "verify-run: nothing recorded as running for $id"; exit 0; }
499
+ gs=$(tr '\n' ' ' < "$reg"); reap_all "$reg"
500
+ echo "verify-run: stopped process group(s) $gs"
501
+ }
502
+
503
+ cmd="${1:-}"; [ "$#" -gt 0 ] && shift
504
+ case "$cmd" in
505
+ run) run "$@" ;;
506
+ rollback-check) rollback_check "$@" ;;
507
+ stop) stop_cmd "$@" ;;
508
+ *) die "usage: verify-run.sh run <id> [options] | rollback-check <spec.md> | stop <id>" ;;
509
+ esac