@sassywares/skills 0.17.1 → 0.17.2

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.
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env bash
2
- # generated_by: sassy-setup@0.17.0
2
+ # generated_by: sassy-setup@0.17.2
3
3
  # Detect project topology and emit a proposed .sassywares/config.yaml.
4
4
  #
5
5
  # Read-only. Writes nothing. sassy-setup runs this, asks the few questions the
@@ -117,6 +117,7 @@ START="${ROOT_ARG:-$PWD}"
117
117
  START="$(cd "$START" && pwd -P)"
118
118
 
119
119
  SKILL_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
120
+ SELF="${BASH_SOURCE[0]}"
120
121
 
121
122
  # The suite roster ships with sassy-setup (assets/roster, one skill name per
122
123
  # line). Only these names are "ours"; a project skill that merely starts with
@@ -129,12 +130,6 @@ if [[ -s "$SKILL_DIR/assets/roster" ]]; then
129
130
  [[ -n "$_n" ]] && SUITE_ROSTER="${SUITE_ROSTER:+$SUITE_ROSTER,}$_n"
130
131
  done < "$SKILL_DIR/assets/roster"
131
132
  fi
132
- is_suite_skill() {
133
- if [[ -n "$SUITE_ROSTER" ]]; then
134
- case ",$SUITE_ROSTER," in *",$1,"*) return 0 ;; *) return 1 ;; esac
135
- fi
136
- [[ "$1" == sassy-* ]]
137
- }
138
133
  ASSET_SNAPSHOT="$SKILL_DIR/assets/snapshot"
139
134
  ASSET_WORKSPACE="$SKILL_DIR/assets/workspace"
140
135
  ASSET_WORKTREE="$SKILL_DIR/assets/worktree"
@@ -181,1981 +176,53 @@ else
181
176
  [[ "$NESTED_FOUND" -eq 1 ]] || TOPOLOGY="none"
182
177
  fi
183
178
 
184
- config_value() {
185
- local key="$1" file="${2:-$CONFIG}"
186
- [[ -f "$file" ]] || return 1
187
- sed -n "s/^[[:space:]]*${key}:[[:space:]]*//p" "$file" \
188
- | head -n 1 \
189
- | sed -E 's/[[:space:]]+#.*$//; s/^["'\'']//; s/["'\'']$//' \
190
- | grep -v '^$' || return 1
191
- }
192
-
193
- block_value() { # $1 block name, $2 key — value only, comments stripped
194
- sed -n "/^$1:/,/^[a-z]/p" "$CONFIG" \
195
- | sed -n "s/^[[:space:]]\{1,\}$2:[[:space:]]*//p" \
196
- | head -n 1 \
197
- | sed -E 's/[[:space:]]+#.*$//; s/[[:space:]]+$//'
198
- }
199
-
200
- # Base branch for one repo. Empty output means unknown — never guessed.
201
- hook_skill_path() { # $1 hook skill name — nearest .agents/skills/<name>/SKILL.md
202
- local name="$1" dir="$ROOT"
203
- while :; do
204
- if [[ -f "$dir/.agents/skills/$name/SKILL.md" ]]; then
205
- printf '%s' "$dir/.agents/skills/$name/SKILL.md"
206
- return 0
207
- fi
208
- [[ "$dir" == "/" ]] && return 1
209
- dir="$(dirname "$dir")"
210
- done
211
- }
212
-
213
- # --- config readers -------------------------------------------------------
214
- # The file is flattened to `path<TAB>value` with paths joined by unit separators,
215
- # which is enough to read the two nested maps we own (git.base_map, hooks.pr.*).
216
- US=$'\037'
217
-
218
- yaml_flat() {
219
- awk -v us="$(printf '\037')" '
220
- function ind(s, m) { m = match(s, /^ */); return m ? RLENGTH : 0 }
221
- {
222
- line = $0; sub(/#.*$/, "", line)
223
- if (line ~ /^[[:space:]]*$/) next
224
- d = ind(line)
225
- body = line; sub(/^[[:space:]]*/, "", body); sub(/[[:space:]]+$/, "", body)
226
- if (body ~ /:$/) {
227
- key = body; sub(/:$/, "", key); gsub(/"/, "", key)
228
- while (sp > 0 && sd[sp] >= d) sp--
229
- sd[++sp] = d; sk[sp] = key
230
- next
231
- }
232
- if (body !~ /:/) next
233
- key = body; sub(/:.*/, "", key); gsub(/"/, "", key)
234
- val = body; sub(/^[^:]*:[[:space:]]*/, "", val); gsub(/^"|"$/, "", val)
235
- path = ""
236
- for (i = 1; i <= sp; i++) if (sd[i] < d) path = path (path == "" ? "" : us) sk[i]
237
- print path (path == "" ? "" : us) key "\t" val
238
- }' "${1:-$CONFIG}"
239
- }
240
-
241
- yflat() { yaml_flat "$CONFIG" | awk -F'\t' -v p="$1" '$1 == p { print $2; exit }'; }
242
-
243
- # yflat_in FILE PATH — the same reader against another config file.
244
- yflat_in() { yaml_flat "$1" | awk -F'\t' -v p="$2" '$1 == p { print $2; exit }'; }
245
-
246
- list_items() {
247
- printf '%s' "$1" | sed -e 's/^\[//' -e 's/\]$//' | tr ',' '\n' \
248
- | sed -E 's/^[[:space:]]+//; s/[[:space:]]+$//; s/^"//; s/"$//' | grep -v '^$' || true
249
- }
250
-
251
- # recipe_lint FILE — structural lint for a dogfood procedure. Prints one message
252
- # per breach; the caller turns each into a drift line. Nothing is executed: a
253
- # check run validates the shape of a procedure, it never runs the product.
254
- recipe_lint() {
255
- awk '
256
- function inventory(rest, kind, n, parts, i, name) {
257
- n = split(rest, parts, ",")
258
- for (i = 1; i <= n; i++) {
259
- name = parts[i]
260
- sub(/^[[:space:]]+/, "", name)
261
- sub(/[[:space:]\(].*$/, "", name)
262
- if (name == "") continue
263
- if (kind == "unc") unc[++nunc] = name
264
- else oos[++noos] = name
265
- }
266
- }
267
- BEGIN { pending = 0; sec = ""; sname = ""; prove = 0; step = ""; incomment = 0; nunc = 0; noos = 0 }
268
- index($0, "<!--") { incomment = 1 }
269
- incomment {
270
- if (index($0, "-->")) incomment = 0
271
- next
272
- }
273
- # The two inventories are file level: names of surfaces, not a surface. Inside a
274
- # section they are a mistake — a section states its own answer with **Prove it:**.
275
- index($0, "**Uncovered:**") || index($0, "**Out of scope:**") {
276
- if (sec != "") {
277
- printf "\"**Uncovered:**\" and \"**Out of scope:**\" are file level only — section \"%s\" states its own answer with **Prove it:** none\n", sname
278
- next
279
- }
280
- line = $0
281
- if (index($0, "**Uncovered:**")) { sub(/^\*\*Uncovered:\*\*[[:space:]]*/, "", line); inventory(line, "unc") }
282
- else { sub(/^\*\*Out of scope:\*\*[[:space:]]*/, "", line); inventory(line, "oos") }
283
- next
284
- }
285
- index($0, "**Prove it:**") {
286
- prove = 1
287
- val = $0; sub(/^.*\*\*Prove it:\*\*[[:space:]]*/, "", val)
288
- low = tolower(val)
289
- if (sname != "") pval[sname] = low
290
- if (low !~ /^none/ && low !~ /^step[[:space:]]+[0-9]/)
291
- printf "a **Prove it:** line must name a step (`step 3`) or say `none` — got \"%s\"\n", val
292
- next
293
- }
294
- /^## / {
295
- if (sec != "" && !prove)
296
- printf "section \"%s\" has no **Prove it:** line — it has no verdict\n", sname
297
- if (pending)
298
- printf "step %s-%s has no **Expect** line — an unstated check is a suggestion\n", sname, step
299
- pending = 0; prove = 0
300
- sec = $0; sub(/^## +/, "", sec)
301
- split(sec, parts, /[ \t]+/); sname = parts[1]
302
- insection[sname] = 1
303
- next
304
- }
305
- /^[0-9]+\. / && index($0, "**Do**") {
306
- if (pending)
307
- printf "step %s-%s has no **Expect** line — an unstated check is a suggestion\n", sname, step
308
- step = $0; sub(/\..*$/, "", step)
309
- pending = 1; next
310
- }
311
- index($0, "**Expect**") { pending = 0; next }
312
- END {
313
- if (pending)
314
- printf "step %s-%s has no **Expect** line — an unstated check is a suggestion\n", sname, step
315
- if (sec != "" && !prove)
316
- printf "section \"%s\" has no **Prove it:** line — it has no verdict\n", sname
317
- if (sec == "")
318
- printf "has no \"## <section>\" heading — there is nothing to prove\n"
319
- # The inventory and the sections must agree, or the gap list lies.
320
- for (i = 1; i <= nunc; i++) {
321
- n = unc[i]
322
- if (!(n in insection))
323
- printf "**Uncovered:** names \"%s\" but there is no \"## %s\" section — an uncovered surface gets a section saying **Prove it:** none\n", n, n
324
- else if (pval[n] !~ /^none/)
325
- printf "**Uncovered:** names \"%s\" but its section has a verdict — uncovered means **Prove it:** none\n", n
326
- }
327
- for (i = 1; i <= noos; i++) {
328
- n = oos[i]
329
- if (n in insection)
330
- printf "\"%s\" is listed **Out of scope:** but gets a \"## %s\" section — out of scope means no section\n", n, n
331
- }
332
- }
333
- ' "$1"
334
- }
335
-
336
- # map_match SECTION HEAD — exact key, then the longest matching family, then "*".
337
- # base_map uses it, and hooks use the same rule, so "which pattern applies" has
338
- # exactly one answer in the whole config.
339
- map_match() {
340
- local section="$1" head="$2" pat stem best="" bestlen=0 v
341
- v="$(yflat "git${US}${section}${US}${head}")"
342
- [[ -n "$v" ]] && { printf '%s' "$v"; return; }
343
- while IFS= read -r pat; do
344
- case "$pat" in *"*") ;; *) continue ;; esac
345
- [[ "$pat" == "*" ]] && continue
346
- stem="${pat%\*}"
347
- if [[ "$head" == "$stem"* ]] && (( ${#stem} > bestlen )); then
348
- best="$pat"; bestlen=${#stem}
349
- fi
350
- done < <(yaml_flat "$CONFIG" | awk -F'\t' -v us="$US" -v sec="$section" \
351
- '{n=split($1,a,us); if (n==3 && a[1]=="git" && a[2]==sec) print a[3]}' | sort -u)
352
- if [[ -n "$best" ]]; then
353
- v="$(yflat "git${US}${section}${US}${best}")"
354
- [[ -n "$v" ]] && { printf '%s' "$v"; return; }
355
- fi
356
- yflat "git${US}${section}${US}*"
357
- }
358
-
359
- base_for_head() { map_match base_map "$1"; }
360
-
361
- # hook_patterns [KIND] — the keys declared under hooks.<kind> (pr by default): a
362
- # head branch, or a goal slug for hooks.goal.
363
- hook_patterns() {
364
- yaml_flat | awk -F'\t' -v us="$US" -v k="${1:-pr}" '{n=split($1,a,us); if (n==4 && a[1]=="hooks" && a[2]==k) print a[3]}' | sort -u
365
- }
366
-
367
- # hook_points KIND — the single source of point names; empty for an unknown kind.
368
- hook_points() {
369
- case "$1" in
370
- pr) printf '%s' "before_create after_create before_merge after_merge" ;;
371
- push) printf '%s' "after_push" ;;
372
- goal) printf '%s' "before_slice after_close" ;;
373
- esac
374
- }
375
-
376
- # hooks_declared KIND — succeeds when hooks.<kind> names at least one skill.
377
- hooks_declared() {
378
- local kind="$1" pat point
379
- while IFS= read -r pat; do
380
- for point in $(hook_points "$kind"); do
381
- [[ -n "$(list_items "$(yflat "hooks${US}${kind}${US}${pat}${US}${point}")")" ]] && return 0
382
- done
383
- done < <(hook_patterns "$kind")
384
- return 1
385
- }
386
-
387
- # hooks_for_head_point HEAD POINT [KIND] — exact head, then the longest matching
388
- # family, then "*". A pattern that declares no entry for this point inherits.
389
- hooks_for_head_point() {
390
- local head="$1" point="$2" kind="${3:-pr}" pat stem best="" bestlen=0 v
391
- v="$(yflat "hooks${US}${kind}${US}${head}${US}${point}")"
392
- [[ -n "$v" ]] && { printf '%s' "$v"; return; }
393
- while IFS= read -r pat; do
394
- case "$pat" in *"*") ;; *) continue ;; esac
395
- [[ "$pat" == "*" ]] && continue
396
- stem="${pat%\*}"
397
- if [[ "$head" == "$stem"* ]] && (( ${#stem} > bestlen )); then
398
- best="$pat"; bestlen=${#stem}
399
- fi
400
- done < <(hook_patterns "$kind")
401
- if [[ -n "$best" ]]; then
402
- v="$(yflat "hooks${US}${kind}${US}${best}${US}${point}")"
403
- [[ -n "$v" ]] && { printf '%s' "$v"; return; }
404
- fi
405
- printf '%s' "$(yflat "hooks${US}${kind}${US}*${US}${point}")"
406
- }
407
-
408
- detect_base() {
409
- local dir="$1" b
410
- b="$(git -C "$dir" symbolic-ref --short refs/remotes/origin/HEAD 2>/dev/null | sed 's|^origin/||' || true)"
411
- if [[ -n "$b" ]]; then printf '%s' "$b"; return 0; fi
412
- if command -v gh >/dev/null 2>&1; then
413
- b="$(cd "$dir" && gh repo view --json defaultBranchRef -q .defaultBranchRef.name 2>/dev/null || true)"
414
- if [[ -n "$b" ]]; then printf '%s' "$b"; return 0; fi
415
- fi
416
- return 0
417
- }
418
-
419
- remote_name() {
420
- local dir="$1" remotes
421
- remotes="$(git -C "$dir" remote 2>/dev/null)"
422
- if printf '%s\n' "$remotes" | grep -qx origin; then printf 'origin'; return 0; fi
423
- printf '%s\n' "$remotes" | head -n 1
424
- }
425
-
426
- nested_repos() {
427
- shopt -s nullglob
428
- local child
429
- for child in "$ROOT"/*/; do
430
- [[ -d "$child/.git" ]] || continue
431
- printf '%s\n' "$(basename "$child")"
432
- done
433
- shopt -u nullglob
434
- }
435
-
436
- path_exists() { [[ -e "$ROOT/$1" ]]; }
437
-
438
- # A configured path that leaves the project is a document written somewhere the
439
- # project does not own, so it fails closed rather than being created.
440
- escapes_root() {
441
- local p="${1#./}"
442
- case "$p" in
443
- ..|../*|*/..|*/../*|*"/../"*) return 0 ;;
444
- esac
445
- return 1
446
- }
447
-
448
- pick_artifact() {
449
- local candidate="$1" fallback="$2"
450
- if path_exists "$candidate"; then printf '%s' "$candidate"; else printf '%s' "$fallback"; fi
451
- }
452
-
453
- # The glossary moved under docs/. Prefer the new home, and fall back to a legacy
454
- # root file only when that file already exists — an existing config is never
455
- # migrated, and a project with neither file gets the new default.
456
- pick_concepts() {
457
- pick_artifact docs/concepts.md "$(pick_artifact CONCEPTS.md docs/concepts.md)"
458
- }
459
-
460
- # wt_script_reason VALUE — succeeds with the reason when a `worktrees.create|remove`
461
- # value has the wrong shape, fails when it is usable. One path, relative to the
462
- # project, no arguments: the script is the lifecycle, so a command line here would
463
- # define a second place it lives. `--check` and `--worktree-script` share this one
464
- # predicate, so a value cannot pass one mode and fail the other over the same
465
- # question.
466
- wt_script_reason() {
467
- local v="$1"
468
- case "$v" in
469
- *[[:space:]]*) printf 'takes one path, no arguments — move the arguments into the script'; return 0 ;;
470
- /*|~*) printf 'is not a path relative to the project root'; return 0 ;;
471
- esac
472
- escapes_root "$v" && { printf 'walks outside the project'; return 0; }
473
- return 1
474
- }
475
-
476
- # --- Worktree script path --------------------------------------------------
477
- # One question, one answer: which absolute path does a run execute for
478
- # `worktrees.create` or `worktrees.remove`? A declared path that is gone is a
479
- # degradation, not an error (D3): warn on stderr naming the key and the missing
480
- # file, then hand back the built-in. Never writes, never creates a directory, and
481
- # answers even when the built-in folder has not been installed yet.
482
- if [[ -n "$WT_SCRIPT" ]]; then
483
- case "$WT_SCRIPT" in
484
- create|remove) ;;
485
- *) echo "unknown --worktree-script: $WT_SCRIPT (expected create or remove)" >&2; exit 2 ;;
486
- esac
487
- [[ -n "$CONFIG" ]] || { echo "no .sassywares/config.yaml found — run sassy-setup" >&2; exit 1; }
488
- # The config's folder is the project root, not the .sassywares folder holding
489
- # the file: declared paths are relative to the project, like every other key.
490
- builtin="$ROOT/.sassywares/worktree/$WT_SCRIPT"
491
- declared="$(yflat "worktrees${US}${WT_SCRIPT}" 2>/dev/null || true)"
492
- if [[ -z "$declared" ]]; then
493
- printf '%s\n' "$builtin"
494
- exit 0
495
- fi
496
- # A wrong shape is the same degradation as a missing file: warn with the reason
497
- # `--check` gives, then hand back the built-in.
498
- if reason="$(wt_script_reason "$declared")"; then
499
- echo "warning: worktrees.$WT_SCRIPT: '$declared' $reason — using the built-in .sassywares/worktree/$WT_SCRIPT" >&2
500
- printf '%s\n' "$builtin"
501
- exit 0
502
- fi
503
- declared_path="$ROOT/${declared#./}"
504
- if [[ -f "$declared_path" && -x "$declared_path" ]]; then
505
- # Physical path, so the identity a run inherits never disagrees over /tmp.
506
- printf '%s\n' "$(cd "$(dirname "$declared_path")" && pwd -P)/$(basename "$declared_path")"
507
- exit 0
508
- fi
509
- echo "warning: worktrees.$WT_SCRIPT names '$declared' but $declared_path is missing or not executable — using the built-in .sassywares/worktree/$WT_SCRIPT" >&2
510
- printf '%s\n' "$builtin"
511
- exit 0
512
- fi
513
-
514
- PLANS="$(pick_artifact docs/plans docs/plans)"
515
- SOLUTIONS="$(pick_artifact docs/solutions docs/solutions)"
516
- HANDBOOKS="$(pick_artifact docs/handbooks docs/handbooks)"
517
- CONCEPTS="$(pick_concepts)"
518
- STRATEGY="$(pick_artifact docs/strategy.md docs/strategy.md)"
519
-
520
- # --- AGENTS.md routing block ----------------------------------------------
521
- # One shipped file, assets/agents-md.md, used two ways: the whole file when the
522
- # project has none, and the marked block appended to an existing one. The
523
- # version lives in the markers, so a later shipped version can replace an older
524
- # block by exact marker match. Placeholders are rendered from the resolved
525
- # artifacts.* paths, never hardcoded here.
526
- AGENTS_ASSET="$SKILL_DIR/assets/agents-md.md"
527
- # The marker grammar in one place, valid as grep ERE, sed BRE, and awk ERE.
528
- AGENTS_OPEN_RE='^<!-- sassywares:skills v[0-9][0-9]* -->$'
529
- AGENTS_CLOSE_RE='^<!-- /sassywares:skills v[0-9][0-9]* -->$'
530
-
531
- agents_md_open_no() { grep -n -E "$AGENTS_OPEN_RE" "$1" 2>/dev/null | head -n 1 | cut -d: -f1; }
532
- agents_md_close_no() { grep -n -E "$AGENTS_CLOSE_RE" "$1" 2>/dev/null | head -n 1 | cut -d: -f1; }
533
- agents_md_version() { sed -n 's/^<!-- sassywares:skills v\([0-9][0-9]*\) -->$/\1/p' "$1" 2>/dev/null | head -n 1; }
534
- agents_md_shipped_version() { agents_md_version "$AGENTS_ASSET"; }
535
- agents_md_close_version() { sed -n 's/^<!-- \/sassywares:skills v\([0-9][0-9]*\) -->$/\1/p' "$1" 2>/dev/null | head -n 1; }
536
-
537
- # agents_md_artifact KEY DEFAULT — the configured path, else the default this
538
- # detector already resolved. Relative to the project root, like every artifact.
539
- agents_md_artifact() {
540
- local v
541
- v="$(block_value artifacts "$1" 2>/dev/null || true)"
542
- [[ -n "$v" ]] || v="$2"
543
- printf '%s' "${v#./}"
544
- }
545
-
546
- # The shipped block, rendered, on stdout.
547
- agents_md_render() {
548
- local strategy concepts plans solutions handbooks
549
- strategy="$(agents_md_artifact strategy "$STRATEGY")"
550
- concepts="$(agents_md_artifact concepts "$CONCEPTS")"
551
- plans="$(agents_md_artifact plans "$PLANS")"
552
- solutions="$(agents_md_artifact solutions "$SOLUTIONS")"
553
- handbooks="$(agents_md_artifact handbooks "$HANDBOOKS")"
554
- # Values go through the environment, not -v: awk's -v interprets escape
555
- # sequences, so a configured path with a backslash would lose it.
556
- SASSY_AGENTS_STRATEGY="$strategy" SASSY_AGENTS_CONCEPTS="$concepts" \
557
- SASSY_AGENTS_PLANS="$plans" SASSY_AGENTS_SOLUTIONS="$solutions" \
558
- SASSY_AGENTS_HANDBOOKS="$handbooks" \
559
- awk -v openre="$AGENTS_OPEN_RE" -v closere="$AGENTS_CLOSE_RE" '
560
- # Splice a value literally: no regex replacement, so an & or a backslash in
561
- # a configured path survives. Scanning resumes past the inserted value, so a
562
- # value that itself contains the placeholder cannot loop.
563
- function put(line, ph, val, i, from) {
564
- from = 1
565
- while ((i = index(substr(line, from), ph)) > 0) {
566
- i = from + i - 1
567
- line = substr(line, 1, i-1) val substr(line, i+length(ph))
568
- from = i + length(val)
569
- }
570
- return line
571
- }
572
- BEGIN {
573
- strategy = ENVIRON["SASSY_AGENTS_STRATEGY"]
574
- concepts = ENVIRON["SASSY_AGENTS_CONCEPTS"]
575
- plans = ENVIRON["SASSY_AGENTS_PLANS"]
576
- solutions = ENVIRON["SASSY_AGENTS_SOLUTIONS"]
577
- handbooks = ENVIRON["SASSY_AGENTS_HANDBOOKS"]
578
- }
579
- $0 ~ openre { f = 1 }
580
- f {
581
- line = $0
582
- line = put(line, "{{artifacts.strategy}}", strategy)
583
- line = put(line, "{{artifacts.concepts}}", concepts)
584
- line = put(line, "{{artifacts.plans}}", plans)
585
- line = put(line, "{{artifacts.solutions}}", solutions)
586
- line = put(line, "{{artifacts.handbooks}}", handbooks)
587
- print line
588
- }
589
- f && $0 ~ closere { exit }' "$AGENTS_ASSET"
590
- }
591
-
592
- # agents_md_block FILE — the block exactly as it sits in FILE, on stdout.
593
- agents_md_block() {
594
- awk -v openre="$AGENTS_OPEN_RE" -v closere="$AGENTS_CLOSE_RE" '
595
- $0 ~ openre { f = 1 }
596
- f { print }
597
- f && $0 ~ closere { exit }' "$1"
598
- }
599
-
600
- # agents_md_plan — what --sync --write would do to AGENTS.md:
601
- # create | append | replace | current | newer | unterminated | mismatch | unreadable
602
- agents_md_plan() {
603
- local target="$ROOT/AGENTS.md" open_no close_no v cv shipped
604
- shipped="$(agents_md_shipped_version)"
605
- [[ -n "$shipped" ]] || { printf 'unreadable'; return; }
606
- [[ -f "$target" ]] || { printf 'create'; return; }
607
- open_no="$(agents_md_open_no "$target")"
608
- [[ -n "$open_no" ]] || { printf 'append'; return; }
609
- close_no="$(agents_md_close_no "$target")"
610
- if [[ -z "$close_no" ]] || (( close_no < open_no )); then printf 'unterminated'; return; fi
611
- v="$(agents_md_version "$target")"
612
- cv="$(agents_md_close_version "$target")"
613
- [[ -n "$v" && "$v" == "$cv" ]] || { printf 'mismatch'; return; }
614
- if (( 10#$v < 10#$shipped )); then printf 'replace'; return; fi
615
- if (( 10#$v > 10#$shipped )); then printf 'newer'; return; fi
616
- if diff -q <(agents_md_block "$target") <(agents_md_render) >/dev/null 2>&1; then
617
- printf 'current'
618
- else
619
- printf 'replace'
620
- fi
621
- }
622
-
623
- # agents_md_write ACTION RENDERED — the block is the only region touched. The
624
- # file is replaced through a temp in the project root, so a failure never leaves
625
- # a half-written AGENTS.md.
626
- agents_md_write() {
627
- local action="$1" rendered="$2" target="$ROOT/AGENTS.md" tmp mode rendered_file
628
- case "$target" in "$ROOT"/*) ;; *) echo "sync: refusing to write outside the project root: $target" >&2; return 1 ;; esac
629
- tmp="$(mktemp "$ROOT/.AGENTS.md.XXXXXX")" || return 1
630
- case "$action" in
631
- create)
632
- printf '%s\n' "$rendered" > "$tmp"
633
- ;;
634
- append)
635
- cat "$target" > "$tmp"
636
- # Every byte the file already carried is kept; only a separator is added.
637
- if [[ -n "$(tail -c 1 "$target")" ]]; then printf '\n' >> "$tmp"; fi
638
- printf '\n%s\n' "$rendered" >> "$tmp"
639
- ;;
640
- replace)
641
- # The rendered block is passed as a file, not -v: awk's -v would try to
642
- # interpret the block's newlines and backslashes as escapes.
643
- rendered_file="$(mktemp "${TMPDIR:-/tmp}/sassy-agents-block.XXXXXX")" || { rm -f "$tmp"; return 1; }
644
- printf '%s\n' "$rendered" > "$rendered_file"
645
- if ! awk -v openre="$AGENTS_OPEN_RE" -v closere="$AGENTS_CLOSE_RE" -v replf="$rendered_file" '
646
- FILENAME == replf { a[++n] = $0; next }
647
- $0 ~ openre { for (i = 1; i <= n; i++) print a[i]; skip = 1; next }
648
- skip && $0 ~ closere { skip = 0; next }
649
- !skip { print }
650
- ' "$rendered_file" "$target" > "$tmp"; then
651
- rm -f "$tmp" "$rendered_file"; return 1
652
- fi
653
- rm -f "$rendered_file"
654
- ;;
655
- *) rm -f "$tmp"; return 1 ;;
656
- esac
657
- if [[ -f "$target" ]]; then
658
- mode="$(stat -f '%Lp' "$target" 2>/dev/null || stat -c '%a' "$target" 2>/dev/null || true)"
659
- [[ -z "$mode" ]] || chmod "$mode" "$tmp" 2>/dev/null || true
660
- fi
661
- mv "$tmp" "$target"
662
- }
663
-
664
- # agents_md_sync DRY — create, append, or replace the block; a second run is a
665
- # no-op that says so. DRY 1 only reports what a write would do.
666
- agents_md_sync() {
667
- local dry="$1" action shipped v
668
- action="$(agents_md_plan)"
669
- shipped="$(agents_md_shipped_version)"
670
- case "$action" in
671
- create|append|replace)
672
- if (( dry )); then
673
- case "$action" in
674
- create) echo "sync: AGENTS.md would be created with routing block v${shipped} — nothing written; rerun with --sync --write" >&2 ;;
675
- append) echo "sync: AGENTS.md would get routing block v${shipped} appended — nothing written; rerun with --sync --write" >&2 ;;
676
- replace) echo "sync: AGENTS.md routing block would be replaced with v${shipped} — nothing written; rerun with --sync --write" >&2 ;;
677
- esac
678
- return 0
679
- fi
680
- agents_md_write "$action" "$(agents_md_render)" || { echo "sync: could not write AGENTS.md" >&2; return 1; }
681
- case "$action" in
682
- create) echo "sync: wrote AGENTS.md (routing block v${shipped} created)" >&2 ;;
683
- append) echo "sync: wrote AGENTS.md (routing block v${shipped} appended)" >&2 ;;
684
- replace) echo "sync: wrote AGENTS.md (routing block replaced with v${shipped})" >&2 ;;
685
- esac
686
- ;;
687
- current)
688
- echo "sync: AGENTS.md is already current" >&2
689
- ;;
690
- newer)
691
- v="$(agents_md_version "$ROOT/AGENTS.md")"
692
- echo "sync: AGENTS.md routing block is v${v}, newer than this skill ships (v${shipped}) — left alone" >&2
693
- ;;
694
- unterminated)
695
- echo "sync: AGENTS.md has an opening sassywares:skills marker with no matching closing marker — fix it by hand, nothing written" >&2
696
- ;;
697
- mismatch)
698
- echo "sync: AGENTS.md routing block markers disagree on the version — fix it by hand, nothing written" >&2
699
- ;;
700
- *)
701
- echo "sync: no routing-block asset found — AGENTS.md untouched" >&2
702
- ;;
703
- esac
704
- return 0
705
- }
706
-
707
- MIRROR_REMOTE=""
708
- MIRROR_BRANCH="main"
709
- REPO_NAME=""
710
- DEFAULT_BRANCH=""
711
-
712
- case "$TOPOLOGY" in
713
- repo)
714
- REPO_NAME="$(remote_name "$ROOT")"
715
- REPO_NAME="${REPO_NAME:-origin}"
716
- # A check or sync run reads the base from the config; probing costs a
717
- # network call. A sync keeps the file's base when it has a valid one and
718
- # leaves the placeholder when it does not, for a human to confirm.
719
- (( CHECK || SYNC )) || DEFAULT_BRANCH="$(detect_base "$ROOT")"
720
- ;;
721
- workspace)
722
- MIRROR_REMOTE="$(config_value remote 2>/dev/null || true)"
723
- MIRROR_BRANCH="$(config_value mirror_branch 2>/dev/null || true)"
724
- MIRROR_BRANCH="${MIRROR_BRANCH:-main}"
725
- ;;
726
- esac
727
-
728
- # --- Template merge -------------------------------------------------------
729
- # --sync starts from the proposal the default mode prints (emit_proposal) and
730
- # merges the existing file onto it: a leaf the file declares and this check
731
- # accepts keeps its line — value and same-line trailing comment — exactly as
732
- # written, and every block, key, comment layer, and example the file lacks comes
733
- # from the proposal. A value --check reports as drift is not accepted, so it is
734
- # replaced by the proposal's, the same judgement --check makes.
735
- #
736
- # "Value --check accepts" is one predicate, leaf_ok, over the schema in
737
- # references/config.md. It deliberately validates only the value, never its
738
- # context: a `worktrees.enabled: true` with no tooling installed is a valid value
739
- # in a wrong state, so the sync keeps it and the check keeps reporting it.
740
- leaf_ok() { # PATH VALUE — PATH joined with US, value comment- and quote-stripped
741
- local path="$1" v="$2" key kind rest pat point entry name
742
- key="${path//$US/.}"
743
- case "$key" in
744
- artifacts.plans|artifacts.solutions|artifacts.handbooks|artifacts.concepts|artifacts.strategy)
745
- case "$v" in ""|/*|~*|.|./) return 1 ;; esac
746
- escapes_root "$v" && return 1
747
- case "$key" in
748
- artifacts.concepts|artifacts.strategy) [[ "$v" == */ ]] && return 1 ;;
749
- esac
750
- return 0 ;;
751
- knowledge.read)
752
- case "$v" in \[*\]) ;; *) return 1 ;; esac
753
- while IFS= read -r entry; do
754
- [[ -n "$entry" ]] || continue
755
- case "$entry" in /*|~*) return 1 ;; esac
756
- escapes_root "$entry" && return 1
757
- done < <(list_items "$v")
758
- return 0 ;;
759
- knowledge.write|pr.babysit|pr.merge|pr.delete_branch|dogfood.enabled|worktrees.enabled)
760
- [[ "$v" == "true" || "$v" == "false" ]] ;;
761
- git.repo)
762
- if [[ "$TOPOLOGY" == "repo" ]]; then
763
- case "$v" in ""|workspace|none) return 1 ;; esac
764
- git -C "$ROOT" remote 2>/dev/null | grep -qx "$v"
765
- else
766
- [[ "$v" == "workspace" || "$v" == "none" ]]
767
- fi ;;
768
- git.remote|git.mirror_branch)
769
- [[ "$TOPOLOGY" != "repo" && -n "$v" ]] ;;
770
- git.base_map.*)
771
- pat="${key#git.base_map.}"
772
- case "$pat" in
773
- *"*") [[ "$pat" == "*" || ( -n "${pat%\*}" && "${pat%\*}" != *"*" ) ]] || return 1 ;;
774
- esac
775
- return 0 ;;
776
- repos.*)
777
- [[ "$TOPOLOGY" != "repo" ]] ;;
778
- commit.branch)
779
- [[ "$v" == "current" || "$v" == "new" ]] ;;
780
- dogfood.timeout)
781
- [[ "$v" =~ ^[1-9][0-9]*$ ]] ;;
782
- dogfood.recipe|dogfood.reports)
783
- case "$v" in ""|/*|~*|.|./) return 1 ;; esac
784
- escapes_root "$v" && return 1
785
- return 0 ;;
786
- worktrees.dir)
787
- [[ -n "$v" ]] ;;
788
- worktrees.create|worktrees.remove)
789
- ! wt_script_reason "$v" >/dev/null ;;
790
- hooks.*)
791
- kind="${key#hooks.}"; kind="${kind%%.*}"
792
- case "$kind" in pr|push|goal) ;; *) return 1 ;; esac
793
- rest="${key#hooks.}"; rest="${rest#*.}"
794
- point="${rest##*.}"
795
- pat="${rest%.*}"
796
- case " $(hook_points "$kind") " in *" $point "*) ;; *) return 1 ;; esac
797
- case "$pat" in
798
- *"*") [[ "$pat" == "*" || ( -n "${pat%\*}" && "${pat%\*}" != *"*" ) ]] || return 1 ;;
799
- esac
800
- while IFS= read -r entry; do
801
- [[ -n "$entry" ]] || continue
802
- name="${entry%% *}"
803
- is_suite_skill "$name" && return 1
804
- hook_skill_path "$name" >/dev/null 2>&1 || return 1
805
- done < <(list_items "$v")
806
- return 0 ;;
807
- *)
808
- return 1 ;;
809
- esac
810
- }
811
-
812
- # One parser, two readers: the flat path stack is the same shape yaml_flat
813
- # builds, so a line can be matched to the current file and to the proposal.
814
- LEAF_AWK='function trim(s) { sub(/^[ \t]+/, "", s); sub(/[ \t]+$/, "", s); return s }
815
- function ind(s, m) { m = match(s, /^ */); return m ? RLENGTH : 0 }
816
- {
817
- raw = $0
818
- body0 = trim(raw)
819
- if (body0 == "" || substr(body0, 1, 1) == "#") next
820
- line = raw; sub(/#.*$/, "", line)
821
- d = ind(line); body = trim(line)
822
- if (body ~ /:$/) {
823
- k = body; sub(/:$/, "", k); gsub(/"/, "", k)
824
- while (sp > 0 && sd[sp] >= d) sp--
825
- sd[++sp] = d; sk[sp] = k
826
- next
827
- }
828
- k = body; sub(/:.*/, "", k); gsub(/"/, "", k)
829
- while (sp > 0 && sd[sp] >= d) sp--
830
- p = ""
831
- for (i = 1; i <= sp; i++) p = p (p == "" ? "" : us) sk[i]
832
- print p (p == "" ? "" : us) k "\t" raw
833
- }'
834
-
835
- # The merge. Records are `kind <TAB> depth <TAB> path <TAB> raw`; a commented
836
- # example is a record too, with the depth its `#` markers would have if removed
837
- # (the hash characters themselves add no depth, the spaces after them do), so
838
- # `# push:` under `hooks:` is known to be the posited `hooks.push` block. When the
839
- # file declares a valid leaf under such an example, the example is replaced by
840
- # the file's own active block, verbatim — the one case where a commented example
841
- # in the proposal is not an example for this project.
842
- MERGE_AWK='function trim(s) { sub(/^[ \t]+/, "", s); sub(/[ \t]+$/, "", s); return s }
843
- function ind(s, m) { m = match(s, /^ */); return m ? RLENGTH : 0 }
844
- function joinall( p, i) { p = ""; for (i = 1; i <= sp; i++) p = p (p == "" ? "" : us) sk[i]; return p }
845
- function dopop(d) { while (sp > 0 && sd[sp] >= d) sp-- }
846
- function outf(r, p) { o++; oraw[o] = r; okind[o] = ""; opath[o] = p }
847
- function outfk(r, d, p) { o++; oraw[o] = r; okind[o] = "key"; odepth[o] = d; opath[o] = p }
848
- function outfl(r, d, p) { o++; oraw[o] = r; okind[o] = "leaf"; odepth[o] = d; opath[o] = p }
849
- function under_accepted(P, a) {
850
- for (a = 1; a <= na; a++) if (index(aorder[a], P us) == 1) return 1
851
- return 0
852
- }
853
- function emit_acc(P, d) { o++; oraw[o] = acc[P]; okind[o] = "leaf"; odepth[o] = d; opath[o] = P; emitted[P] = 1 }
854
- function emit_block(P, i, j, e, a) {
855
- b0 = 0
856
- for (i = 1; i <= cn; i++) if (ckind[i] == "key" && cpath[i] == P) { b0 = i; break }
857
- if (b0 == 0) { warn[++nw] = P; return }
858
- e = b0
859
- for (j = b0 + 1; j <= cn; j++) {
860
- if (ckind[j] == "blank") continue
861
- if (cdepth[j] > cdepth[b0]) { e = j; continue }
862
- break
863
- }
864
- for (j = b0; j <= e; j++) {
865
- if (ckind[j] == "key") outfk(crawl[j], cdepth[j], cpath[j])
866
- else if (ckind[j] == "leaf") outfl(crawl[j], cdepth[j], cpath[j])
867
- else outf(crawl[j])
868
- }
869
- for (a = 1; a <= na; a++) if (index(aorder[a], P us) == 1) emitted[aorder[a]] = 1
870
- emitted[P] = 1
871
- }
872
- BEGIN { cn = 0; pn = 0; na = 0; sp = 0; o = 0; nw = 0 }
873
- FILENAME == accf {
874
- if ($2 != "") { acc[$1] = $2; aorder[++na] = $1 }
875
- next
876
- }
877
- FILENAME == curf {
878
- raw = $0
879
- body0 = trim(raw)
880
- if (body0 == "") { ckind[++cn] = "blank"; crawl[cn] = raw; next }
881
- if (substr(body0, 1, 1) == "#") { ckind[++cn] = "comment"; crawl[cn] = raw; next }
882
- line = raw; sub(/#.*$/, "", line)
883
- d = ind(line); body = trim(line)
884
- if (body ~ /:$/) {
885
- k = body; sub(/:$/, "", k); gsub(/"/, "", k)
886
- dopop(d); sd[++sp] = d; sk[sp] = k
887
- ckind[++cn] = "key"; cdepth[cn] = d; cpath[cn] = joinall(); crawl[cn] = raw
888
- } else {
889
- k = body; sub(/:.*/, "", k); gsub(/"/, "", k)
890
- dopop(d); p = joinall()
891
- ckind[++cn] = "leaf"; cdepth[cn] = d; cpath[cn] = p (p == "" ? "" : us) k; crawl[cn] = raw
892
- }
893
- next
894
- }
895
- FILENAME == propf {
896
- raw = $0
897
- body0 = trim(raw)
898
- if (body0 == "") { pkind[++pn] = "blank"; pdepth[pn] = 0; ppath[pn] = ""; praw[pn] = raw; next }
899
- if (substr(body0, 1, 1) == "#") {
900
- rest = body0
901
- while (substr(rest, 1, 1) == "#") rest = substr(rest, 2)
902
- m = match(rest, /^ */); spc = m ? RLENGTH : 0
903
- rest = substr(rest, spc + 1)
904
- # A commented example sits at its `#` column, and its content starts two
905
- # columns later ("# "); the spaces after the marker encode the depth of a
906
- # child, so subtracting the marker leaves the line the example stands for.
907
- ed = ind(raw) + (spc > 0 ? spc - 1 : 0)
908
- if (rest ~ /^[^ \t:]+:/) {
909
- k = rest; sub(/:.*/, "", k); gsub(/"/, "", k)
910
- v = rest; sub(/^[^:]*:/, "", v); sub(/#.*/, "", v); v = trim(v)
911
- dopop(ed)
912
- if (v == "") {
913
- sd[++sp] = ed; sk[sp] = k
914
- pkind[++pn] = "example_key"; pdepth[pn] = ed; ppath[pn] = joinall(); praw[pn] = raw
915
- } else {
916
- p = joinall()
917
- pkind[++pn] = "example_leaf"; pdepth[pn] = ed
918
- ppath[pn] = p (p == "" ? "" : us) k; praw[pn] = raw
919
- }
920
- } else {
921
- pkind[++pn] = "comment"; pdepth[pn] = 0; ppath[pn] = ""; praw[pn] = raw
922
- }
923
- next
924
- }
925
- line = raw; sub(/#.*$/, "", line)
926
- d = ind(line); body = trim(line)
927
- if (body ~ /:$/) {
928
- k = body; sub(/:$/, "", k); gsub(/"/, "", k)
929
- dopop(d); sd[++sp] = d; sk[sp] = k
930
- pkind[++pn] = "key"; pdepth[pn] = d; ppath[pn] = joinall(); praw[pn] = raw
931
- } else {
932
- k = body; sub(/:.*/, "", k); gsub(/"/, "", k)
933
- dopop(d); p = joinall()
934
- pkind[++pn] = "leaf"; pdepth[pn] = d
935
- ppath[pn] = p (p == "" ? "" : us) k; praw[pn] = raw
936
- }
937
- next
938
- }
939
- END {
940
- i = 1
941
- while (i <= pn) {
942
- if (pkind[i] == "blank" || pkind[i] == "comment") { outf(praw[i]); i++; continue }
943
- if (pkind[i] == "example_key") {
944
- j = i + 1
945
- while (j <= pn && (pkind[j] == "example_key" || pkind[j] == "example_leaf") && pdepth[j] > pdepth[i]) j++
946
- if (under_accepted(ppath[i])) {
947
- emit_block(ppath[i])
948
- } else {
949
- for (m = i; m < j; m++) outf(praw[m])
950
- }
951
- i = j; continue
952
- }
953
- if (pkind[i] == "example_leaf") {
954
- if (ppath[i] in acc) emit_acc(ppath[i], pdepth[i]); else outf(praw[i])
955
- i++; continue
956
- }
957
- if (pkind[i] == "leaf") {
958
- if (ppath[i] in acc) emit_acc(ppath[i], pdepth[i]); else outfl(praw[i], pdepth[i], ppath[i])
959
- i++; continue
960
- }
961
- outfk(praw[i], pdepth[i], ppath[i])
962
- i++
963
- }
964
- # An accepted leaf the proposal has no slot for — an extra base_map rung, a
965
- # hand-written head — is inserted under its own block, after the last line
966
- # of that block. Dropping it would be the one thing the sync must never do:
967
- # change a value the check accepts.
968
- for (a = 1; a <= na; a++) {
969
- P = aorder[a]
970
- if (P in emitted) continue
971
- par = P; sub(us "[^" us "]*$", "", par)
972
- if (par == P) { warn[++nw] = P; continue }
973
- b0 = 0; bd = -1
974
- for (m = 1; m <= o; m++) if (okind[m] == "key" && opath[m] == par) { b0 = m; bd = odepth[m] }
975
- if (b0 == 0) { warn[++nw] = P; continue }
976
- last = b0
977
- for (m = b0 + 1; m <= o; m++) {
978
- if (okind[m] == "blank") continue
979
- if (odepth[m] <= bd) break
980
- last = m
981
- }
982
- insn[last]++
983
- insraw[last, insn[last]] = acc[P]
984
- }
985
- for (m = 1; m <= o; m++) {
986
- print oraw[m]
987
- for (q = 1; q <= insn[m]; q++) print insraw[m, q]
988
- }
989
- for (m = 1; m <= nw; m++) print "sync: no slot for " warn[m] > "/dev/stderr"
990
- }'
991
-
992
- leaf_lines() { # FILE -> `path<TAB>raw line` for every active leaf
993
- awk -v us="$US" "$LEAF_AWK" "$1"
994
- }
995
-
996
- accepted_table() { # -> `path<TAB>raw line` for every leaf --check accepts today
997
- local path raw value
998
- leaf_lines "$CONFIG" | while IFS=$'\t' read -r path raw; do
999
- [[ -n "$path" ]] || continue
1000
- value="$(printf '%s' "$raw" \
1001
- | sed -E 's/^[[:space:]]*[^:]*:[[:space:]]*//; s/[[:space:]]+#.*$//; s/[[:space:]]+$//')"
1002
- value="${value#\"}"; value="${value%\"}"
1003
- if leaf_ok "$path" "$value"; then printf '%s\t%s\n' "$path" "$raw"; fi
1004
- done
1005
- }
1006
-
1007
- merged_config() { # the current file merged onto the proposal, on stdout
1008
- local prop acc
1009
- prop="$(mktemp "${TMPDIR:-/tmp}/sassy-sync-proposal.XXXXXX")"
1010
- acc="$(mktemp "${TMPDIR:-/tmp}/sassy-sync-accepted.XXXXXX")"
1011
- emit_proposal > "$prop"
1012
- accepted_table > "$acc"
1013
- awk -F'\t' -v us="$US" -v accf="$acc" -v curf="$CONFIG" -v propf="$prop" \
1014
- "$MERGE_AWK" "$acc" "$CONFIG" "$prop"
1015
- rm -f "$prop" "$acc"
1016
- }
1017
-
1018
- sync_phrase() { # MERGED -> what --sync --write would do, for the check's note
1019
- local merged="$1" cur_paths merged_paths acc_paths added="" replaced="" p raw out
1020
- cur_paths="$(leaf_lines "$CONFIG" | cut -f1)"
1021
- merged_paths="$(printf '%s\n' "$merged" | leaf_lines /dev/stdin | cut -f1)"
1022
- acc_paths="$(accepted_table | cut -f1)"
1023
- added="$(comm -13 <(printf '%s\n' "$cur_paths" | sort -u) <(printf '%s\n' "$merged_paths" | sort -u) 2>/dev/null \
1024
- | cut -d"$US" -f1 | sort -u | grep -v '^$' | paste -sd, - | sed 's/,/, /g' || true)"
1025
- while IFS=$'\t' read -r p raw; do
1026
- [[ -n "$p" ]] || continue
1027
- grep -qxF "$p" <<< "$acc_paths" && continue
1028
- grep -qxF "$p" <<< "$merged_paths" || continue
1029
- replaced="${replaced:+$replaced, }${p//$US/.}"
1030
- done < <(leaf_lines "$CONFIG")
1031
- if [[ -z "$added" && -z "$replaced" ]]; then printf 'refresh the comments'; return; fi
1032
- out=""
1033
- [[ -n "$added" ]] && out="add $added"
1034
- [[ -n "$replaced" ]] && out="${out:+$out; }replace $replaced (not valid today)"
1035
- printf '%s and refresh the comments' "$out"
1036
- }
1037
-
1038
- # --- Emit a proposal -------------------------------------------------------
1039
- # One function, two callers: the default mode prints it, and --sync/--check
1040
- # merge onto it. Everything it reads (PLANS, REPO_NAME, TOPOLOGY, DEFAULT_BRANCH,
1041
- # MIRROR_*) is computed above, before either caller runs.
1042
- emit_proposal() {
1043
- # Every key carries its meaning and its allowed values. The file is the
1044
- # documentation a teammate reads, so the comments are part of the output, not
1045
- # decoration to be stripped.
1046
-
1047
- # kv KEY VALUE WIDTH COMMENT [INDENT] — prints "key: value" padded so comments
1048
- # align. WIDTH 0 means no padding.
1049
- kv() {
1050
- local indent="${5:- }" line
1051
- line="$indent$1: $2"
1052
- if [[ -n "${4:-}" && "${3:-0}" != "0" ]]; then
1053
- printf '%-*s%s\n' "$3" "$line" "$4"
1054
- elif [[ -n "${4:-}" ]]; then
1055
- printf '%s %s\n' "$line" "$4"
1056
- else
1057
- printf '%s\n' "$line"
1058
- fi
1059
- }
1060
-
1061
- cat <<EOF
1062
- # Sassy Skills project config — written by sassy-setup.
1063
- # Every key is optional: delete a line to fall back to the documented default.
1064
- # Guide: https://github.com/sassywares/sassy-skills/blob/main/skills/sassy-setup/references/config.md
1065
-
1066
- # --- Artifacts ------------------------------------------------------------
1067
- # Where sassy writes its documents. Paths are relative to this file's folder.
1068
- artifacts:
1069
- EOF
1070
- kv plans "$PLANS" 34 '# sassy-plan writes plans here'
1071
- kv solutions "$SOLUTIONS" 34 '# sassy-compound writes learnings here'
1072
- kv handbooks "$HANDBOOKS" 34 '# sassy-docs writes handbooks here'
1073
- kv concepts "$CONCEPTS" 34 '# the glossary sassy-compound maintains (a file)'
1074
- kv strategy "$STRATEGY" 34 '# the direction sassy-strategy writes (a file)'
1075
- cat <<EOF
1076
-
1077
- # --- Knowledge ------------------------------------------------------------
1078
- # read: folders and files searched for prior learnings before planning or
1079
- # debugging. Any paths; one that does not exist is skipped, not fatal.
1080
- # write: true | false — false means this project takes no learnings.
1081
- knowledge:
1082
- EOF
1083
- kv read "[$SOLUTIONS, $CONCEPTS]" 0
1084
- kv write true 34 '# true | false'
1085
-
1086
- case "$TOPOLOGY" in
1087
- repo)
1088
- cat <<EOF
1089
-
1090
- # --- Git ------------------------------------------------------------------
1091
- # repo: a remote name (this folder is the repo) | workspace (repos live below,
1092
- # this folder is not a repo) | none (no git anywhere here).
1093
- # base_map: the base for a HEAD branch, so a PR targets the right rung.
1094
- # "*" is ordinary work; add a rung for a branch that releases elsewhere.
1095
- git:
1096
- EOF
1097
- kv repo "$REPO_NAME" 0
1098
- printf ' base_map:\n'
1099
- if [[ -n "$DEFAULT_BRANCH" ]]; then
1100
- printf ' "*": %s # probed — confirm this is where ordinary work branches from\n' "$DEFAULT_BRANCH"
1101
- else
1102
- printf ' "*": %s # base for ordinary work; never guessed\n' "<confirm by hand>"
1103
- fi
1104
- printf ' # staging: main # uncomment when a branch releases somewhere else\n'
1105
- printf ' # release/*: main # a family of heads, longest prefix wins\n'
1106
- cat <<'EOF'
1107
- # remote: owner/backup # workspace only: the snapshot mirror
1108
- # mirror_branch: main # workspace only: the mirror's branch
1109
-
1110
- # workspace only: each nested repo folder -> the base its PRs target
1111
- # repos:
1112
- # fe: dev
1113
- EOF
1114
- ;;
1115
- workspace)
1116
- cat <<EOF
1117
-
1118
- # --- Git ------------------------------------------------------------------
1119
- # repo: workspace means repos live below and this folder is not a repo.
1120
- # remote: the snapshot mirror (GitHub owner/name or URL), required here.
1121
- # mirror_branch: the branch the mirror pushes to. Not a PR base — nested repos
1122
- # carry their own, and a workspace has no branches of its own.
1123
- git:
1124
- EOF
1125
- kv repo workspace 0
1126
- kv remote "${MIRROR_REMOTE:-<set the snapshot mirror, owner/name or URL>}" 0
1127
- kv mirror_branch "$MIRROR_BRANCH" 0
1128
- cat <<'EOF'
1129
-
1130
- # --- Repos ----------------------------------------------------------------
1131
- # Every nested repo -> the base its PRs target. A touched repo missing here is
1132
- # a question, never a default.
1133
- repos:
1134
- EOF
1135
- any=0
1136
- while IFS= read -r name; do
1137
- [[ -n "$name" ]] || continue
1138
- base="$(detect_base "$ROOT/$name")"
1139
- if [[ -n "$base" ]]; then
1140
- kv "$name" "$base" 34 '# probed — confirm the PR base'
1141
- else
1142
- kv "$name" "<confirm>" 34 '# base unknown; confirm by hand'
1143
- fi
1144
- any=1
1145
- done < <(nested_repos)
1146
- (( any )) || printf ' # no nested repos found\n'
1147
- ;;
1148
- *)
1149
- cat <<EOF
1150
-
1151
- # --- Git ------------------------------------------------------------------
1152
- # repo: none means this folder is deliberately outside git: commit, PR, and
1153
- # babysit refuse here, and work still leaves its changes on disk.
1154
- git:
1155
- EOF
1156
- kv repo none 0
1157
- ;;
1158
- esac
1159
-
1160
- cat <<'EOF'
1161
-
1162
- # --- Commit ---------------------------------------------------------------
1163
- # current: commit on the checked-out branch, even on a base branch, then push
1164
- # it to its upstream. No PR: pr.* and hooks.pr are ignored; hooks.push runs.
1165
- # new: never commit on a base branch — sassy-work and sassy-debug
1166
- # cut a branch at the start of the run and stay on it.
1167
- commit:
1168
- EOF
1169
- kv branch new 34 '# current | new'
1170
- cat <<'EOF'
1171
-
1172
- # --- Pull requests --------------------------------------------------------
1173
- pr:
1174
- EOF
1175
- kv babysit true 24 '# true | false — babysit the PR this run opened'
1176
- kv merge false 24 '# true = merge every babysat PR here'
1177
- kv delete_branch false 24 '# true | false — delete the head branch once merged'
1178
- cat <<'EOF'
1179
-
1180
- # --- Hooks ----------------------------------------------------------------
1181
- # Project steps injected into the PR pipeline, after a push under
1182
- # commit.branch: current, across a worktree's life, or around a goal. Keyed by
1183
- # the branch — the branch the PR comes from, never the one it targets — or by the
1184
- # goal slug for hooks.goal. `"*"` is the default; a name overrides per point, and
1185
- # `release/*` matches a family. Each entry is `<skill> <args…>`, resolved at
1186
- # .agents/skills/<name>/SKILL.md; args are passed to the skill verbatim. Declared
1187
- # order, one worker each, missing skill fails closed.
1188
- hooks:
1189
- pr:
1190
- "*":
1191
- EOF
1192
- kv before_create '[]' 30 '# after the commit, before the PR opens' ' '
1193
- kv after_create '[]' 30 '# the PR was just created' ' '
1194
- kv before_merge '[]' 30 '# this run is about to merge' ' '
1195
- kv after_merge '[]' 30 '# this run observed the merge' ' '
1196
- cat <<'EOF'
1197
- # staging: # per-point override for one head branch
1198
- # before_create: []
1199
- # release/*: # a family of heads
1200
- # after_merge: [notify mode:auto]
1201
- goal: # keyed by the goal slug (the folder under artifacts.plans)
1202
- "*":
1203
- EOF
1204
- kv before_slice '[]' 30 '# sassy-goal mode:next: after the previous slice merges' ' '
1205
- kv after_close '[]' 30 '# sassy-goal mode:close: after the status is persisted' ' '
1206
- cat <<'EOF'
1207
- # migration-v2: # one goal, by slug
1208
- # before_slice: [db-migrate mode:auto]
1209
- # push: # commit.branch: current only; keyed by the pushed branch
1210
- # "*":
1211
- # after_push: []
1212
- EOF
1213
- cat <<'EOF'
1214
-
1215
- # --- Worktrees ------------------------------------------------------------
1216
- # Parallel work in one repo: a checkout per branch, each with its own derived
1217
- # namespace, so two runs never fight over one working tree. enabled: true lets a
1218
- # run offer one instead of working here — the run asks, and stays here unless the
1219
- # answer is yes. It needs commit.branch: new, because `current` never cuts a
1220
- # branch. The built-in scripts in .sassywares/worktree/ own the git mechanics;
1221
- # create: and remove: name this project's own scripts, which wrap them for
1222
- # whatever a checkout needs and are what a run executes. Nothing else reads
1223
- # these values.
1224
- #
1225
- # Repo topology only: a workspace root has no repo to cut a checkout from, and a
1226
- # `none` folder has no git at all. A checkout is addressed by the project's own
1227
- # stack, not by the suite: a port is the project's business, and this block
1228
- # carries no range.
1229
- worktrees:
1230
- EOF
1231
- kv enabled false 34 '# true = a run may offer its own checkout'
1232
- kv dir '~/.sassywares/worktrees' 34 '# parent of the per-repo folder; ~ is allowed'
1233
- cat <<'EOF'
1234
- # create: scripts/worktrees/create # this project's own; runs before and after git
1235
- # remove: scripts/worktrees/remove # this project's own; stop, then dispose
1236
- EOF
1237
- cat <<'EOF'
1238
-
1239
- # --- Dogfood --------------------------------------------------------------
1240
- # Agent QA: a stored procedure that proves a change works in the running
1241
- # product. This block only points at it — commands live in the recipe, because
1242
- # a copy here would duplicate the manifest and could not express step order.
1243
- # The recipe ends each section in a `Prove it:` step whose exit code is the
1244
- # verdict. enabled: true lets a plan write Mode: Auto QA rows, but only for a
1245
- # section that actually has one.
1246
- dogfood:
1247
- EOF
1248
- kv enabled false 42 '# true = plan writes Auto QA rows; false = all Manual'
1249
- kv recipe .sassywares/dogfood/recipe.md 42 '# the procedure; sassy-dogfood writes it'
1250
- kv reports .sassywares/dogfood/reports 42 '# run output; must stay gitignored'
1251
- kv timeout 900 42 '# seconds for the whole procedure'
1252
- }
1253
-
1254
- if (( CHECK )); then
1255
- drift=0
1256
- note() { printf '%s\n' "$*"; }
1257
- bad() { printf ' drift: %s\n' "$*"; drift=1; }
1258
-
1259
- # A node checker prints `<drift|note>\t<message>` lines and exits 0; the caller
1260
- # owns the verdict, so its exit status is a finding too. Silencing a crash into
1261
- # zero output would read as "nothing to report", which is the one thing a
1262
- # verifier must never mean. On failure the first output line names the crash.
1263
- check_with_node() { # <script> <missing-node note> [args...]
1264
- local script="$1" missing="$2" out first kind msg
1265
- shift 2
1266
- if ! command -v node >/dev/null 2>&1; then
1267
- note " $missing"
1268
- return 0
1269
- fi
1270
- if out="$(node "$script" "$@" 2>&1)"; then
1271
- while IFS=$'\t' read -r kind msg; do
1272
- case "$kind" in
1273
- drift) bad "$msg" ;;
1274
- note) note " $msg" ;;
1275
- esac
1276
- done <<< "$out"
1277
- else
1278
- first="$(printf '%s\n' "$out" | head -n 1)"
1279
- bad "$(basename "$script") failed — ${first:-it exited non-zero with no output}"
1280
- fi
1281
- return 0
1282
- }
1283
-
1284
- note "root: ${ROOT}"
1285
- note "config: ${CONFIG:-none found (walking up from $START)}"
1286
-
1287
- if [[ -z "$CONFIG" ]]; then
1288
- bad "no .sassywares/config.yaml found — run sassy-setup"
1289
- exit 1
1290
- fi
1291
-
1292
- configured_repo="$(config_value repo 2>/dev/null || true)"
1293
- if [[ -z "$configured_repo" ]]; then
1294
- bad "git.repo is unset"
1295
- elif [[ "$TOPOLOGY" == "repo" ]]; then
1296
- # The tree is a repo: the configured value must name one of its remotes.
1297
- case "$configured_repo" in
1298
- workspace|none) bad "git.repo is '$configured_repo' but this folder is a git repo" ;;
1299
- *) git -C "$ROOT" remote 2>/dev/null | grep -qx "$configured_repo" \
1300
- || bad "git.repo '$configured_repo' is not a remote of this repo" ;;
1301
- esac
1302
- else
1303
- # Not a repo: `workspace` and `none` are both legal, and which one is a
1304
- # human policy call the tree cannot verify.
1305
- case "$configured_repo" in
1306
- workspace|none) note " not a repo; '$configured_repo' accepted" ;;
1307
- *) bad "git.repo is '$configured_repo' but this folder is not a git repo" ;;
1308
- esac
1309
- fi
1310
-
1311
- # A typo'd key is the quietest failure in the file: it parses, it is ignored,
1312
- # and the run silently falls back to a default the project never chose. The
1313
- # whitelist is the schema in references/config.md; every key there has a
1314
- # validator of its own below, so anything landing here is unknown.
1315
- lost_hooks_reported=0
1316
- while IFS=$'\t' read -r kpath kvalue; do
1317
- [[ -n "$kpath" ]] || continue
1318
- key="${kpath//$US/.}"
1319
- case "$key" in
1320
- artifacts.plans|artifacts.solutions|artifacts.handbooks|artifacts.concepts|artifacts.strategy) ;;
1321
- knowledge.read|knowledge.write) ;;
1322
- git.repo|git.remote|git.mirror_branch) ;;
1323
- git.default_branch) ;; # has its own message below
1324
- git.base_map.*) ;;
1325
- repos.*) ;;
1326
- commit.branch) ;;
1327
- pr.babysit|pr.merge|pr.delete_branch) ;;
1328
- dogfood.enabled|dogfood.recipe|dogfood.reports|dogfood.timeout) ;;
1329
- worktrees.enabled|worktrees.dir) ;;
1330
- worktrees.create|worktrees.remove)
1331
- # A single path, no arguments: the script is the lifecycle, so a command
1332
- # line here would define a second place it lives.
1333
- case "$kvalue" in
1334
- \[*|\{*) bad "$key is a list or block — write one path: $key: scripts/worktrees/${key##*.}" ;;
1335
- esac ;;
1336
- up|down)
1337
- # Removed with the project-script lifecycle (D3/D11): a run stops and
1338
- # starts through the project's own create/remove, not a key. A soft
1339
- # warning would let a project believe its stack still stops.
1340
- bad "$key is gone — the lifecycle moved into worktrees.create and worktrees.remove. Delete the '$key:' line from .sassywares/config.yaml" ;;
1341
- hooks.worktree|hooks.worktree.*)
1342
- # Same migration: what a worktree needed to prepare or tear down is now
1343
- # inside the project's scripts, which the suite runs instead. One block
1344
- # is one fix, so report it once however many points it lists.
1345
- if (( ! lost_hooks_reported )); then
1346
- bad "hooks.worktree is gone — the lifecycle moved into worktrees.create and worktrees.remove, and those scripts replace it. Delete the 'worktree:' block under hooks in .sassywares/config.yaml"
1347
- lost_hooks_reported=1
1348
- fi ;;
1349
- worktrees.ports)
1350
- # Deleted from the schema. The one line that fixes it is the line itself.
1351
- bad "worktrees.ports is gone — delete the 'ports:' line under worktrees in .sassywares/config.yaml" ;;
1352
- hooks.*) ;; # shape checked with the hooks
1353
- *) bad "unknown key '$key' — it is ignored, so the run uses the default" ;;
1354
- esac
1355
- done < <(yaml_flat "$CONFIG")
1356
-
1357
- if [[ -n "$(yflat "git${US}default_branch")" ]]; then
1358
- if [[ "$TOPOLOGY" == "workspace" ]]; then
1359
- bad "git.default_branch is gone — the mirror's branch is git.mirror_branch"
1360
- else
1361
- bad "git.default_branch is gone — use git.base_map with a \"*\" entry"
1362
- fi
1363
- fi
1364
-
1365
- if [[ "$TOPOLOGY" == "repo" ]]; then
1366
- if [[ -n "$(yflat "git${US}remote")" ]]; then
1367
- bad "git.remote is workspace-only — a repo config names its remote in git.repo"
1368
- fi
1369
- if [[ -n "$(yflat "git${US}mirror_branch")" ]]; then
1370
- bad "git.mirror_branch is workspace-only — a repo routes branches in git.base_map"
1371
- fi
1372
- if [[ -z "$(yflat "git${US}base_map${US}*")" ]]; then
1373
- bad "git.base_map has no \"*\" entry — it is the base for ordinary work"
1374
- else
1375
- while IFS= read -r pat; do
1376
- [[ -n "$pat" ]] || continue
1377
- case "$pat" in
1378
- "*") ;;
1379
- *"*") [[ "${pat%\*}" != *"*" ]] || bad "base_map key '$pat': only one trailing * is allowed" ;;
1380
- esac
1381
- done < <(yaml_flat "$CONFIG" | awk -F'\t' -v us="$US" '{n=split($1,a,us); if (n==3 && a[1]=="git" && a[2]=="base_map") print a[3]}' | sort -u)
1382
- head_ref="${HEAD_ARG:-$(git -C "$ROOT" branch --show-current 2>/dev/null || true)}"
1383
- if [[ -n "$head_ref" ]]; then
1384
- # the --head readout below prints this line; avoid printing it twice
1385
- [[ -n "$HEAD_ARG" ]] || note " base for '${head_ref}': $(base_for_head "$head_ref")"
1386
- cur="$head_ref"; hops=0
1387
- while [[ -n "$cur" ]] && (( hops < 8 )); do
1388
- next="$(yflat "git${US}base_map${US}${cur}")"
1389
- [[ -z "$next" ]] && break
1390
- cur="$next"; hops=$((hops + 1))
1391
- if [[ "$cur" == "$head_ref" ]]; then
1392
- bad "git.base_map cycles through '$head_ref'"; break
1393
- fi
1394
- done
1395
- else
1396
- note " detached HEAD — pass --head <branch> to see the resolved base"
1397
- fi
1398
- fi
1399
- fi
1400
-
1401
- # `repos` is the workspace inventory. In a repo config it is a dead map, and
1402
- # a reader would trust it over the base_map that actually applies.
1403
- if [[ "$configured_repo" != "workspace" ]]; then
1404
- while IFS= read -r rname; do
1405
- [[ -n "$rname" ]] || continue
1406
- bad "repos.$rname is workspace-only — git.repo is '${configured_repo:-unset}'"
1407
- done < <(yaml_flat "$CONFIG" \
1408
- | awk -F'\t' -v us="$US" '{n=split($1,a,us); if (n>=2 && a[1]=="repos") print a[2]}' \
1409
- | sort -u)
1410
- fi
1411
-
1412
- if [[ "$TOPOLOGY" == "workspace" ]]; then
1413
- [[ -n "$MIRROR_REMOTE" ]] || bad "git.remote is required when git.repo is workspace"
1414
- installed="$ROOT/.sassywares/workspace/snapshot"
1415
- if [[ -x "$installed" ]]; then
1416
- installed_v="$(sed -n 's/^# snapshot-version: //p' "$installed" | head -n 1)"
1417
- asset_v="$(sed -n 's/^# snapshot-version: //p' "$ASSET_SNAPSHOT" | head -n 1)"
1418
- if [[ -z "$installed_v" ]]; then
1419
- bad "installed .sassywares/workspace/snapshot has no version line"
1420
- elif [[ "$installed_v" != "$asset_v" ]]; then
1421
- bad "installed snapshot is v${installed_v}, this skill ships v${asset_v} — re-run setup"
1422
- else
1423
- note " snapshot v${installed_v}"
1424
- fi
1425
- else
1426
- bad "missing .sassywares/workspace/snapshot"
1427
- fi
1428
-
1429
- # The git helpers are optional as a set, but a half-installed or stale set is
1430
- # drift: they read `repos:` from this config, so a version skew means the
1431
- # project is running a reader written for a different map.
1432
- installed_lib="$ROOT/.sassywares/workspace/_lib.sh"
1433
- if [[ -f "$installed_lib" ]]; then
1434
- lib_v="$(sed -n 's/^# workspace-git-version: //p' "$installed_lib" | head -n 1)"
1435
- asset_lib_v="$(sed -n 's/^# workspace-git-version: //p' "$ASSET_WORKSPACE/_lib.sh" | head -n 1)"
1436
- lib_var="$(sed -n 's/^WORKSPACE_GIT_VERSION="\(.*\)"/\1/p' "$installed_lib" | head -n 1)"
1437
- if [[ -n "$lib_var" && -n "$lib_v" && "$lib_var" != "$lib_v" ]]; then
1438
- bad ".sassywares/workspace/_lib.sh header says v${lib_v} but the script reports v${lib_var} — the file was half-bumped"
1439
- fi
1440
- if [[ -z "$lib_v" ]]; then
1441
- bad "installed .sassywares/workspace/_lib.sh has no workspace-git-version line"
1442
- elif [[ -n "$asset_lib_v" && "$lib_v" != "$asset_lib_v" ]]; then
1443
- bad "installed workspace git helpers are v${lib_v}, this skill ships v${asset_lib_v} — re-run setup"
1444
- else
1445
- note " workspace-git v${lib_v}"
1446
- fi
1447
- for helper in branches pull checkout-bases; do
1448
- if [[ ! -x "$ROOT/.sassywares/workspace/$helper" ]]; then
1449
- bad "missing or not executable: .sassywares/workspace/$helper"
1450
- fi
1451
- done
1452
- else
1453
- note " no workspace git helpers installed (optional)"
1454
- fi
1455
-
1456
- # Aliases are optional: the scripts are run by path, and a project that wired
1457
- # only the ones its runner needs is not drifting. Reported, never failed.
1458
- # The manifest is never enumerated here — sassy-overview's identify.sh names
1459
- # the stack and the file that declares it (D20).
1460
- manifest=""
1461
- identify="$SKILL_DIR/../sassy-overview/scripts/identify.sh"
1462
- if [[ -f "$identify" ]]; then
1463
- stack_line="$(bash "$identify" --root "$ROOT" --key stack 2>/dev/null || true)"
1464
- while IFS=$'\t' read -r _key stack_val stack_src; do
1465
- # Only a named-scripts manifest has an alias slot; anything else is
1466
- # simply not wired, and that is complete.
1467
- case "$stack_val" in
1468
- node|php|deno) if [[ "$stack_src" != "-" ]]; then manifest="$stack_src"; fi ;;
1469
- esac
1470
- done <<< "$stack_line"
1471
- else
1472
- note " sassy-overview is not installed — workspace aliases unchecked"
1473
- fi
1474
- if [[ -n "$manifest" ]]; then
1475
- missing_aliases=()
1476
- missing_list=""
1477
- for script in branches pull checkout-bases snapshot; do
1478
- if ! grep -q "\"workspace:${script}\"" "$ROOT/$manifest"; then
1479
- missing_aliases+=("$script")
1480
- missing_list+="${missing_list:+, }workspace:${script}"
1481
- fi
1482
- done
1483
- if [[ ${#missing_aliases[@]} -eq 0 ]]; then
1484
- note " workspace:* aliases in ${manifest}"
1485
- elif [[ ${#missing_aliases[@]} -lt 4 ]]; then
1486
- note " aliases: ${missing_list} not wired in ${manifest} (optional — the scripts run by path)"
1487
- else
1488
- note " no workspace:* aliases in ${manifest} (optional)"
1489
- fi
1490
- fi
1491
- while IFS= read -r name; do
1492
- [[ -n "$name" ]] || continue
1493
- base="$(detect_base "$ROOT/$name")"
1494
- listed="$(block_value repos "$name" || true)"
1495
- # A nested repo's own config is authoritative; `repos` is the inventory and
1496
- # the fallback for repos that have none.
1497
- nested="$ROOT/$name/.sassywares/config.yaml"
1498
- nested_base=""
1499
- [[ -f "$nested" ]] && nested_base="$(yflat_in "$nested" "git${US}base_map${US}*")"
1500
-
1501
- if [[ -n "$nested_base" ]]; then
1502
- if [[ -n "$listed" && "$listed" != "$nested_base" ]]; then
1503
- bad "repos.$name is '$listed' but $name/.sassywares/config.yaml maps \"*\" to '$nested_base' — the nested config wins; fix or drop the repos entry"
1504
- elif [[ -z "$listed" ]]; then
1505
- note " $name: base '$nested_base' from its own config (no repos entry needed)"
1506
- else
1507
- note " $name: base '$nested_base' (agrees with its own config)"
1508
- fi
1509
- elif [[ -z "$listed" ]]; then
1510
- bad "repos has no entry for '$name' and $name carries no config (remote default: ${base:-unknown})"
1511
- elif [[ -n "$base" && "$listed" != "$base" ]]; then
1512
- note " repos.$name '$listed' overrides the remote default '$base' — expected for an integration branch"
1513
- fi
1514
- done < <(nested_repos)
1515
- fi
1516
-
1517
- # Hooks: head-scoped, so `staging` means "the PR is from staging".
1518
- if [[ -n "$(yflat "hooks${US}pr${US}before_create")" ]]; then
1519
- bad "hooks.pr.<point> is now nested by head branch — write hooks.pr.\"*\".<point>"
1520
- fi
1521
- for kind in pr push goal; do
1522
- while IFS= read -r pat; do
1523
- [[ -n "$pat" ]] || continue
1524
- case "$pat" in
1525
- "*") ;;
1526
- *"*") [[ "${pat%\*}" != *"*" ]] || bad "hook pattern '$pat': only one trailing * is allowed" ;;
1527
- esac
1528
- for point in $(hook_points "$kind"); do
1529
- val="$(yflat "hooks${US}${kind}${US}${pat}${US}${point}")"
1530
- [[ -n "$val" ]] || continue
1531
- while IFS= read -r entry; do
1532
- [[ -n "$entry" ]] || continue
1533
- name="${entry%% *}"
1534
- if is_suite_skill "$name"; then
1535
- bad "hooks.$kind.$pat.$point: '$name' is one of our skills — hooks are project skills only"
1536
- else
1537
- hook_skill_path "$name" >/dev/null 2>&1 \
1538
- || bad "hooks.$kind.$pat.$point: '$name' not found at .agents/skills/$name/SKILL.md"
1539
- fi
1540
- done < <(list_items "$val")
1541
- done
1542
- done < <(hook_patterns "$kind")
1543
- done
1544
-
1545
- # `hooks.<kind>.<head-or-slug>.<point>` is the only shape. hooks.pr has four
1546
- # points, hooks.push one, hooks.goal two; any other point name is a step that
1547
- # never fires. hooks.worktree is reported where the accepted keys are, once.
1548
- while IFS=$'\t' read -r depth hpath; do
1549
- [[ -n "$depth" ]] || continue
1550
- kind="${hpath#hooks$US}"; kind="${kind%%$US*}"
1551
- case "$kind" in
1552
- pr|push|goal) ;;
1553
- worktree) continue ;;
1554
- *) bad "hooks kind '$kind' is not one of pr|push|goal"; continue ;;
1555
- esac
1556
- if [[ "$depth" != "4" ]]; then
1557
- bad "hooks entry '${hpath//$US/.}' is not key -> point -> list"
1558
- continue
1559
- fi
1560
- point="${hpath##*$US}"
1561
- points="$(hook_points "$kind")"
1562
- case " $points " in
1563
- *" $point "*) ;;
1564
- *) bad "hooks.$kind point '$point' is not one of ${points// /|}" ;;
1565
- esac
1566
- done < <(yaml_flat "$CONFIG" \
1567
- | awk -F'\t' -v us="$US" '{n=split($1,a,us); if (n>=2 && a[1]=="hooks") print n "\t" $1}')
1568
-
1569
- # Resolve for one head, so nobody has to merge maps in their head.
1570
- if [[ -n "$HEAD_ARG" ]]; then
1571
- if [[ "$TOPOLOGY" == "repo" ]]; then
1572
- note " base for '$HEAD_ARG': $(base_for_head "$HEAD_ARG")"
1573
- fi
1574
- for kind in pr push; do
1575
- for point in $(hook_points "$kind"); do
1576
- resolved="$(hooks_for_head_point "$HEAD_ARG" "$point" "$kind")"
1577
- note " head '$HEAD_ARG' → $point: $( [[ -n "$resolved" ]] && printf '%s' "$resolved" || printf 'none' )"
1578
- done
1579
- done
1580
- fi
1581
-
1582
- # Goal hooks are keyed by goal slug, not a branch, so they resolve on their own.
1583
- if [[ -n "$GOAL_ARG" ]]; then
1584
- for point in $(hook_points goal); do
1585
- resolved="$(hooks_for_head_point "$GOAL_ARG" "$point" goal)"
1586
- note " goal '$GOAL_ARG' → $point: $( [[ -n "$resolved" ]] && printf '%s' "$resolved" || printf 'none' )"
1587
- done
1588
- fi
1589
-
1590
- # artifacts is the one block that fails closed everywhere it is read, so a
1591
- # bad value here is drift, not a note: a typo must not write docs outside the
1592
- # project. Existence is not drift — the directory is created on first write.
1593
- for key in plans solutions handbooks concepts strategy; do
1594
- val="$(block_value artifacts "$key" || true)"
1595
- [[ -n "$val" ]] || continue
1596
- case "$val" in
1597
- /*|~*) bad "artifacts.$key '$val' is not relative to the config" ;;
1598
- .|./) bad "artifacts.$key is the project root itself" ;;
1599
- *)
1600
- if escapes_root "$val"; then
1601
- bad "artifacts.$key '$val' walks outside the project"
1602
- elif [[ "$val" == */ ]] && [[ "$key" == concepts || "$key" == strategy ]]; then
1603
- bad "artifacts.$key '$val' is a directory — $key is one file (default docs/$key.md)"
1604
- elif path_exists "$val"; then
1605
- note " artifacts.$key: $val (exists)"
1606
- else
1607
- note " artifacts.$key: $val (created on first write)"
1608
- fi
1609
- ;;
1610
- esac
179
+ # --- Modules ---------------------------------------------------------------
180
+ # One file per mode, under lib/. detect.sh owns the shell options, the argument
181
+ # parsing, and the bootstrap paths; a lib defines functions and reads those
182
+ # paths, never recomputes them, and never sets a shell option.
183
+
184
+ # The script file is resolved first, so lib/ is found through either a
185
+ # skill-directory link or a direct file symlink.
186
+ SCRIPT_DIR="$(
187
+ _self="$SELF"
188
+ while [[ -L "$_self" ]]; do
189
+ _dir="$(cd -P "$(dirname "$_self")" && pwd)"
190
+ _self="$(readlink "$_self")"
191
+ [[ "$_self" == /* ]] || _self="$_dir/$_self"
1611
192
  done
1612
-
1613
- # knowledge.read fails soft at read time by design, which is exactly why a
1614
- # broken value has to be visible here: nothing else will ever complain.
1615
- while IFS= read -r kline; do
1616
- [[ -n "$kline" ]] || continue
1617
- bad "knowledge.read must be an inline list — write read: [docs/solutions, docs/concepts.md]"
1618
- done < <(sed -n '/^knowledge:/,/^[a-z]/p' "$CONFIG" | grep -E '^[[:space:]]*-[[:space:]]' || true)
1619
- readv="$(yflat "knowledge${US}read" || true)"
1620
- while IFS= read -r kpath; do
1621
- [[ -n "$kpath" ]] || continue
1622
- case "$kpath" in
1623
- /*|~*) bad "knowledge.read '$kpath' is not a path relative to the config" ;;
1624
- *) if escapes_root "$kpath"; then
1625
- bad "knowledge.read '$kpath' walks outside the project"
1626
- elif ! path_exists "$kpath"; then
1627
- note " knowledge.read '$kpath' does not exist — skipped, not fatal"
1628
- fi ;;
1629
- esac
1630
- done < <(list_items "$readv")
1631
- val="$(yflat "knowledge${US}write" || true)"
1632
- if [[ -n "$val" ]]; then
1633
- case "$val" in
1634
- true|false) note " knowledge.write: $val" ;;
1635
- *) bad "knowledge.write '$val' must be true|false" ;;
1636
- esac
1637
- fi
1638
-
1639
- # pr.* answers a policy question, so a value that is neither boolean is a
1640
- # policy nobody decided. Bare on/off are YAML 1.1 booleans and parse
1641
- # inconsistently across readers.
1642
- for key in babysit merge delete_branch; do
1643
- val="$(block_value pr "$key" || true)"
1644
- [[ -n "$val" ]] || continue
1645
- case "$val" in
1646
- true|false) note " pr.$key: $val" ;;
1647
- *) bad "pr.$key '$val' must be true|false, never on/off" ;;
1648
- esac
1649
- done
1650
-
1651
- # commit.branch steers whether a run may touch a base branch, so an invalid
1652
- # value is reported rather than silently ignored.
1653
- val="$(yflat "commit${US}branch" 2>/dev/null || true)"
1654
- if [[ -n "$val" ]]; then
1655
- case "$val" in
1656
- current|new) note " commit.branch: $val" ;;
1657
- *) bad "commit.branch '$val' is not current|new" ;;
1658
- esac
1659
- fi
1660
-
1661
- # Worktrees: a switch, one mechanical setting, and the project's own lifecycle
1662
- # scripts. Creating, addressing, and refusing all belong to the scripts, so this
1663
- # validates only what a run reads.
1664
- wt_on="$(block_value worktrees enabled || true)"
1665
- case "$wt_on" in
1666
- "") ;;
1667
- true|false) note " worktrees.enabled: $wt_on" ;;
1668
- *) bad "worktrees.enabled '$wt_on' must be true|false" ;;
1669
- esac
1670
-
1671
- # worktrees.create/remove name the project's own lifecycle scripts. A declared
1672
- # path that is missing or not executable degrades to the built-in with a
1673
- # warning at run time (D3), so it is a note here, never drift.
1674
- for mode in create remove; do
1675
- declared="$(yflat "worktrees${US}${mode}" 2>/dev/null || true)"
1676
- [[ -n "$declared" ]] || continue
1677
- if reason="$(wt_script_reason "$declared")"; then
1678
- bad "worktrees.$mode: '$declared' $reason"
1679
- elif [[ -f "$ROOT/${declared#./}" && -x "$ROOT/${declared#./}" ]]; then
1680
- note " worktrees.$mode: ${declared#./}"
1681
- # A declared script is the project's file, so its contents are never drift.
1682
- # Without the header the run cannot tell whether it honors the WT_* contract.
1683
- if ! grep -q '^# sassywares:scaffold' "$ROOT/${declared#./}" 2>/dev/null; then
1684
- note " worktrees.$mode: ${declared#./} carries no sassywares:scaffold header — the run cannot tell whether it prints the WT_* block"
1685
- fi
1686
- else
1687
- note " worktrees.$mode '${declared#./}' is missing or not executable — the built-in .sassywares/worktree/$mode is used instead"
1688
- fi
1689
- done
1690
-
1691
- # The mechanics are suite-owned and read-only (D4/D5), so `--check` proves the
1692
- # installed set byte for byte against the folder this skill ships, using the
1693
- # skills-lock folder hash. A version header cannot: it is hand-maintained, so
1694
- # an edited byte and an unbumped asset both still read as current. Run whenever
1695
- # the set is installed — a project that flipped the switch off still carries
1696
- # files it may not edit — and keep the missing set a switch-on finding.
1697
- wt_lib="$ROOT/.sassywares/worktree/_lib.sh"
1698
- verify_worktree_set() {
1699
- check_with_node "$SKILL_DIR/scripts/worktree-check.mjs" \
1700
- "node not found — .sassywares/worktree is unverified (the skills CLI needs node)" \
1701
- --installed "$ROOT/.sassywares/worktree" \
1702
- --shipped "$ASSET_WORKTREE"
1703
- for helper in create list remove; do
1704
- [[ -x "$ROOT/.sassywares/worktree/$helper" ]] \
1705
- || bad "missing or not executable: .sassywares/worktree/$helper"
1706
- done
1707
- }
1708
-
1709
- if [[ "$wt_on" == "true" ]]; then
1710
- if [[ "$TOPOLOGY" != "repo" ]]; then
1711
- bad "worktrees.enabled needs a repo — this folder is '$TOPOLOGY', and a checkout needs git"
1712
- else
1713
- note " worktrees: a checkout is addressed by the project's own stack, and two checkouts running the same app collide"
1714
- if [[ "$(yflat "commit${US}branch" 2>/dev/null || true)" == "current" ]]; then
1715
- note " worktrees.enabled with commit.branch: current — a run never cuts a branch, so no run offers a checkout"
1716
- fi
1717
-
1718
- if [[ -f "$wt_lib" ]]; then
1719
- verify_worktree_set
1720
- else
1721
- bad "worktrees.enabled but .sassywares/worktree/_lib.sh is missing — re-run setup"
1722
- fi
1723
-
1724
- # A checkout must live outside the working tree — inside it, `git clean
1725
- # -xdf` deletes uncommitted work and every scanner that ignores .gitignore
1726
- # crawls a second copy of the source. And a worktree parked on a configured
1727
- # base is a run that will commit onto a shared branch unobserved.
1728
- root_real="$(cd "$ROOT" && pwd -P)"
1729
- BASE_VALUES="$(yaml_flat "$CONFIG" \
1730
- | awk -F'\t' -v us="$US" '{n=split($1,a,us); if (n==3 && a[1]=="git" && a[2]=="base_map") print $2}' \
1731
- | tr '\n' ' ')"
1732
- wt_paths=()
1733
- while IFS=$'\t' read -r wpath wbranch; do
1734
- [[ -n "$wpath" ]] || continue
1735
- wt_paths+=("$wpath")
1736
- [[ "$wpath" == "$root_real" ]] && continue # the main checkout
1737
- case "$wpath" in
1738
- "$root_real"/*) bad "worktree '$wpath' is inside the working tree — move worktrees.dir outside it" ;;
1739
- esac
1740
- case " $BASE_VALUES " in
1741
- *" $wbranch "*) bad "a worktree holds '$wbranch', which base_map names as a base" ;;
1742
- esac
1743
- done < <(git -C "$ROOT" worktree list --porcelain 2>/dev/null \
1744
- | awk '/^worktree /{p=$2} /^branch /{b=$2; sub(/^refs\/heads\//,"",b); print p "\t" b; p=""} /^detached/{print p "\t(detached)"; p=""}')
1745
-
1746
- # A registration whose folder is gone is a ghost `list` still believes in.
1747
- while IFS= read -r ghost; do
1748
- [[ -n "$ghost" ]] || continue
1749
- bad "worktree '$ghost' is prunable — the folder is gone; run: git worktree prune"
1750
- done < <(git -C "$ROOT" worktree list --porcelain 2>/dev/null \
1751
- | awk '/^worktree /{p=$2} /^prunable /{print p; p=""}')
1752
- fi
1753
- fi
1754
-
1755
- # The switch is off, but the tooling is installed: still suite-owned, still
1756
- # read-only, so still compared. Nothing here needs a repo or an enabled switch.
1757
- if [[ "$wt_on" != "true" && -f "$wt_lib" ]]; then
1758
- verify_worktree_set
1759
- fi
1760
-
1761
- # Dogfood is a pointer and a switch, never commands: the recipe holds the
1762
- # procedure and its `Prove it:` step is the verdict. Everything below is
1763
- # structural. A check run never runs the product — a validator that boots an
1764
- # app is a validator nobody runs.
1765
- dg_enabled="$(block_value dogfood enabled || true)"
1766
- dg_recipe="$(block_value dogfood recipe || true)"
1767
- # The default path applies even when the key is absent: the scripts use it, so
1768
- # --check must lint what they will actually read. Only for a project that has
1769
- # dogfood on, or a recipe already sitting at the default, to avoid noise.
1770
- if [[ -z "$dg_recipe" ]] && { [[ "$dg_enabled" == "true" ]] || [[ -f "$ROOT/.sassywares/dogfood/recipe.md" ]]; }; then
1771
- dg_recipe=".sassywares/dogfood/recipe.md"
1772
- fi
1773
- dg_reports="$(block_value dogfood reports || true)"
1774
- dg_timeout="$(block_value dogfood timeout || true)"
1775
- case "$dg_enabled" in
1776
- "") ;;
1777
- true|false) note " dogfood.enabled: $dg_enabled" ;;
1778
- *) bad "dogfood.enabled '$dg_enabled' must be true|false" ;;
1779
- esac
1780
- for dkey in recipe reports; do
1781
- case "$dkey" in
1782
- recipe) dpath="$dg_recipe" ;;
1783
- *) dpath="$dg_reports" ;;
1784
- esac
1785
- [[ -n "$dpath" ]] || continue
1786
- case "$dpath" in
1787
- /*|~*) bad "dogfood.$dkey '$dpath' is not relative to the config" ;;
1788
- .|./) bad "dogfood.$dkey is the project root itself" ;;
1789
- *) escapes_root "$dpath" && bad "dogfood.$dkey '$dpath' walks outside the project" ;;
1790
- esac
1791
- done
1792
- if [[ -n "$dg_timeout" ]]; then
1793
- case "$dg_timeout" in
1794
- *[!0-9]*) bad "dogfood.timeout '$dg_timeout' is not a positive integer" ;;
1795
- 0) bad "dogfood.timeout is 0 — a procedure that can never finish" ;;
1796
- *) note " dogfood.timeout: ${dg_timeout}s" ;;
1797
- esac
1798
- fi
1799
-
1800
- # Reports are logs and screenshots. A project that drops the ignore line
1801
- # commits them, and nobody notices until the history is heavy. Probed with a
1802
- # child path: `/reports/` names a directory, so git only matches it when the
1803
- # probe goes through the directory — and the directory may not exist yet.
1804
- if [[ "$TOPOLOGY" == "repo" && -n "$dg_reports" ]]; then
1805
- if git -C "$ROOT" check-ignore -q -- "$dg_reports/run.log" 2>/dev/null; then
1806
- note " dogfood.reports is gitignored"
1807
- else
1808
- bad "dogfood.reports '$dg_reports' is not gitignored — a run's logs and screenshots would be committed"
1809
- fi
1810
- fi
1811
-
1812
- # A committed session is a credential leak, not merely a heavy history. Only
1813
- # checked once the folder exists, so a project that never uses one is silent.
1814
- if [[ "$TOPOLOGY" == "repo" && -d "$ROOT/.sassywares/session" ]]; then
1815
- if git -C "$ROOT" check-ignore -q -- ".sassywares/session/probe" 2>/dev/null; then
1816
- note " .sassywares/session is gitignored"
1817
- else
1818
- bad ".sassywares/session is not gitignored — a session artifact must never be committed"
1819
- fi
1820
- fi
1821
-
1822
- if [[ -n "$dg_recipe" ]]; then
1823
- if [[ ! -f "$ROOT/$dg_recipe" ]]; then
1824
- if [[ "$dg_enabled" == "true" ]]; then
1825
- # Enabled with no procedure is the one state that lies: a planner would
1826
- # write Auto rows against a check that does not exist.
1827
- bad "dogfood.recipe '$dg_recipe' does not exist — run sassy-dogfood mode:discover"
1828
- else
1829
- note " dogfood.recipe: $dg_recipe (written by the first sassy-dogfood run)"
1830
- fi
1831
- else
1832
- while IFS= read -r msg; do
1833
- [[ -n "$msg" ]] || continue
1834
- bad "dogfood.recipe $msg"
1835
- done < <(recipe_lint "$ROOT/$dg_recipe")
1836
- while IFS= read -r line0; do
1837
- [[ -n "$line0" ]] || continue
1838
- bad "dogfood.recipe has a secret-shaped string at line $line0 — env var names only, never a value"
1839
- done < <(grep -nE 'sk-[A-Za-z0-9_-]{16,}|ghp_[A-Za-z0-9]{20,}|AKIA[0-9A-Z]{16}|xox[baprs]-[A-Za-z0-9-]{10,}' "$ROOT/$dg_recipe" 2>/dev/null | cut -d: -f1)
1840
- note " dogfood.recipe: $dg_recipe"
1841
- fi
1842
- fi
1843
-
1844
- # Hooks that cannot fire are a note, not drift: the config is legal, the step
1845
- # just never runs. hooks.push needs `current` in a single remote repo.
1846
- if [[ "$val" == "current" ]]; then
1847
- if hooks_declared pr; then
1848
- note " hooks.pr entries are declared but commit.branch is current — no PR, so they never run"
1849
- fi
1850
- if hooks_declared push && [[ "$configured_repo" == "workspace" || "$configured_repo" == "none" ]]; then
1851
- note " hooks.push entries are declared but git.repo is '$configured_repo' — they never run"
1852
- fi
1853
- elif hooks_declared push; then
1854
- note " hooks.push entries are declared but commit.branch is not current — they never run"
1855
- fi
1856
-
1857
- # The pin. A config can match the tree perfectly while the vendored suite is
1858
- # stale, edited, or half-installed, and every skill downstream then runs
1859
- # something other than what the lock says. Offline, no network.
1860
- [[ -n "$SUITE_ROSTER" ]] || note " suite roster not found — falling back to the sassy-* name prefix"
1861
- check_with_node "$SKILL_DIR/scripts/lockcheck.mjs" \
1862
- "node not found — the vendored pin is unverified (the skills CLI needs node)" \
1863
- --root "$ROOT" --roster "$SUITE_ROSTER"
1864
-
1865
- if [[ "$TOPOLOGY" == "repo" ]]; then
1866
- # Tracked, not merely present: a clone has to get the pin without a network
1867
- # call, so an untracked lock is a setup that does not travel.
1868
- # Only a project-scope install has a lock; a user-scope one has nothing here.
1869
- if [[ -n "$(git -C "$ROOT" ls-files -- skills-lock.json 2>/dev/null)" ]]; then
1870
- note " skills-lock.json is tracked"
1871
- elif [[ -f "$ROOT/skills-lock.json" ]]; then
1872
- note " skills-lock.json is not tracked yet — the setup commit carries it"
1873
- fi
1874
- if [[ -e "$ROOT/.claude/skills" ]] && ! git -C "$ROOT" check-ignore -q -- .claude/skills 2>/dev/null; then
1875
- note " .claude/skills is not gitignored — install-time links may get committed"
1876
- fi
1877
- fi
1878
-
1879
- # A config can match the tree and still be behind the template: whole blocks,
1880
- # comment layers, and examples a later version added are missing, so the next
1881
- # reader gets a thinner file than the one sassy-setup documents. It is a note,
1882
- # never drift — every value the file declares is valid — and the sync only adds
1883
- # what is missing, so this is the one passing check worth reading.
1884
- template=""
1885
- if template="$(merged_config)"; then
1886
- if [[ "$template" != "$(cat "$CONFIG")" ]]; then
1887
- note " template: --sync --write would $(sync_phrase "$template") (values the check accepts stay) — run: bash ${BASH_SOURCE[0]} --root $ROOT --sync --write"
1888
- fi
1889
- fi
1890
-
1891
- # The routing block: AGENTS.md is what an agent reads before any skill is
1892
- # invoked. A missing or stale block is a note, never drift — the config is
1893
- # complete without it, and --sync --write is the updater.
1894
- shipped_agents="$(agents_md_shipped_version)"
1895
- case "$(agents_md_plan)" in
1896
- create)
1897
- note " agents-md: AGENTS.md is missing — --sync --write would create it with routing block v${shipped_agents}" ;;
1898
- append)
1899
- note " agents-md: AGENTS.md has no routing block — --sync --write would append v${shipped_agents}" ;;
1900
- replace)
1901
- agents_v="$(agents_md_version "$ROOT/AGENTS.md")"
1902
- if [[ "$agents_v" == "$shipped_agents" ]]; then
1903
- note " agents-md: routing block v${agents_v} differs from this skill's copy — --sync --write replaces it in place"
1904
- else
1905
- note " agents-md: routing block is v${agents_v}, this skill ships v${shipped_agents} — --sync --write replaces it in place"
1906
- fi ;;
1907
- current)
1908
- note " agents-md: routing block v${shipped_agents}" ;;
1909
- newer)
1910
- note " agents-md: routing block is v$(agents_md_version "$ROOT/AGENTS.md"), newer than this skill ships (v${shipped_agents}) — left alone" ;;
1911
- unterminated)
1912
- note " agents-md: AGENTS.md has an opening sassywares:skills marker with no matching closing marker — fix it by hand" ;;
1913
- mismatch)
1914
- note " agents-md: AGENTS.md routing block markers disagree on the version — fix it by hand" ;;
1915
- *)
1916
- note " agents-md: routing-block asset not found — AGENTS.md unchecked" ;;
1917
- esac
1918
-
1919
- if (( drift )); then exit 1; fi
1920
- note " config matches the tree"
1921
- exit 0
1922
- fi
1923
-
1924
- # --- Branch decision -------------------------------------------------------
1925
- # Read-only. Resolves commit.branch into the single decision the entry skills
1926
- # (sassy-work, sassy-debug) make before they start: cut a branch,
1927
- # reuse the one checked out, or commit where you stand.
1928
- #
1929
- # Every base_map VALUE counts as a base branch, not just the remote default.
1930
- # tap2's day-to-day base is `staging`, so comparing only against origin/HEAD
1931
- # declared staging "not a base" and let phase work commit onto it.
1932
- if (( BRANCH )); then
1933
- policy="${POLICY_ARG:-infer}"
1934
- case "$policy" in
1935
- infer|current|new) ;;
1936
- *) echo "unknown --policy: $policy (expected infer, current, or new)" >&2; exit 2 ;;
1937
- esac
1938
- configured="$(yflat "commit${US}branch" 2>/dev/null || true)"
1939
- note=""
1940
- if [[ -n "$configured" && "$configured" != "current" && "$configured" != "new" ]]; then
1941
- # A soft config error must not become a silently wrong branch decision.
1942
- note="commit.branch '$configured' is not current|new; using new"
1943
- configured=""
1944
- fi
1945
- if [[ "$policy" == "infer" ]]; then
1946
- policy="${configured:-new}"
1947
- fi
1948
-
1949
- # base_heads_in DIR — every branch DIR's config treats as a base: each
1950
- # base_map value, the repos[<dir>] rung, and the remote default. A workspace
1951
- # has no base_map of its own, so repos[] is what names its bases; reading
1952
- # base_map alone reported every nested repo as on-branch and let runs commit
1953
- # onto staging, which is the bug this whole mode exists to stop.
1954
- base_heads_in() {
1955
- local dir="$1" r v out=""
1956
- if [[ -f "$dir/.sassywares/config.yaml" ]]; then
1957
- out="$(yaml_flat "$dir/.sassywares/config.yaml" 2>/dev/null | awk -F'\t' -v us="$US" \
1958
- '{n=split($1,a,us); if (n==3 && a[1]=="git" && a[2]=="base_map") print $2}' | sort -u || true)"
1959
- fi
1960
- v="$(yflat "repos${US}$(basename "$dir")" 2>/dev/null || true)"
1961
- [[ -n "$v" ]] && out="$out"$'\n'"$v"
1962
- r="$(remote_name "$dir")"
1963
- if [[ -n "$r" ]]; then
1964
- v="$(git -C "$dir" symbolic-ref --short "refs/remotes/$r/HEAD" 2>/dev/null | sed "s|^$r/||" || true)"
1965
- [[ -n "$v" ]] && out="$out"$'\n'"$v"
1966
- fi
1967
- out="$(printf '%s\n' "$out" | grep -v '^$' | sort -u || true)"
1968
- # Probing through gh costs a network call, so only when nothing else said.
1969
- [[ -n "$out" ]] || out="$(detect_base "$dir" 2>/dev/null || true)"
1970
- printf '%s\n' "$out" | grep -v '^$' || true
1971
- }
1972
-
1973
- # in_base_heads DIR BRANCH — membership without a pipe: `… | grep -q` trips
1974
- # pipefail, because grep exits on the first match and SIGPIPEs the producer,
1975
- # so a branch that IS a base reported as if it were not.
1976
- in_base_heads() {
1977
- local dir="$1" want="$2" b
1978
- while IFS= read -r b; do
1979
- [[ "$b" == "$want" ]] && return 0
1980
- done < <(base_heads_in "$dir")
1981
- return 1
1982
- }
1983
-
1984
- # repo_base DIR — the base a PR from DIR targets, for a brand-new branch name.
1985
- repo_base() {
1986
- local dir="$1" v="" r
1987
- if [[ -f "$dir/.sassywares/config.yaml" ]]; then
1988
- v="$(yflat_in "$dir/.sassywares/config.yaml" "git${US}base_map${US}*" 2>/dev/null || true)"
1989
- fi
1990
- [[ -n "$v" ]] || v="$(yflat "repos${US}$(basename "$dir")" 2>/dev/null || true)"
1991
- if [[ -z "$v" ]]; then
1992
- v="$(detect_base "$dir" 2>/dev/null || true)"
1993
- [[ -n "$v" ]] && printf '%s' "$v" && return 0
1994
- fi
1995
- printf '%s' "$v"
1996
- }
1997
-
1998
- # classify DIR [HEAD] — on-base, on-branch, detached, or unknown.
1999
- classify() {
2000
- local dir="$1" h="$2"
2001
- if [[ -z "$h" ]]; then printf 'detached'; return 0; fi
2002
- if in_base_heads "$dir" "$h"; then printf 'on-base'; else printf 'on-branch'; fi
2003
- }
2004
-
2005
- # start_point DIR HEAD — the upstream ref a new branch is cut from. Prints
2006
- # "none" when there is no upstream ref: cutting from local HEAD would carry
2007
- # commits stranded on the base, which is the failure this prevents.
2008
- start_point() {
2009
- local dir="$1" h="$2" r
2010
- r="$(remote_name "$dir")"
2011
- if [[ -n "$r" && -n "$h" ]] && git -C "$dir" rev-parse --verify --quiet "refs/remotes/$r/$h" >/dev/null 2>&1; then
2012
- printf '%s/%s' "$r" "$h"
2013
- else
2014
- printf 'none'
2015
- fi
2016
- }
2017
-
2018
- # unpublished DIR HEAD — commits on the local base that are not upstream.
2019
- # Unknown on error, never 0: an unreadable count must not read as "clean".
2020
- unpublished() {
2021
- local dir="$1" h="$2" r n
2022
- r="$(remote_name "$dir")"
2023
- [[ -n "$r" && -n "$h" ]] || { printf 'unknown'; return 0; }
2024
- git -C "$dir" rev-parse --verify --quiet "refs/remotes/$r/$h" >/dev/null 2>&1 || { printf 'unknown'; return 0; }
2025
- n="$(git -C "$dir" rev-list --count "$r/$h..$h" 2>/dev/null || true)"
2026
- [[ -n "$n" ]] && printf '%s' "$n" || printf 'unknown'
2027
- }
2028
-
2029
- # decide STATE POLICY START UNPUBLISHED — the action, fail closed.
2030
- decide() {
2031
- local st="$1" pol="$2" start="$3" unp="$4"
2032
- case "$st:$pol" in
2033
- on-branch:*) printf 'reuse'; return 0 ;;
2034
- on-base:new) ;;
2035
- on-base:current) printf 'commit-here'; return 0 ;;
2036
- detached:new) ;;
2037
- *) printf 'refuse'; return 0 ;;
2038
- esac
2039
- # Cutting needs a known-clean upstream base.
2040
- [[ "$start" == "none" ]] && { printf 'refuse'; return 0; }
2041
- [[ "$unp" == "0" ]] || { printf 'stop-and-ask'; return 0; }
2042
- printf 'create'
2043
- }
2044
-
2045
- repo_value="$(yflat "git${US}repo" 2>/dev/null || true)"
2046
-
2047
- [[ -n "$note" ]] && printf 'note: %s\n' "$note"
2048
- printf 'topology: %s\n' "$TOPOLOGY"
2049
- printf 'config: %s\n' "${CONFIG:-none}"
2050
- printf 'configured: %s\n' "${configured:-none}"
2051
- printf 'policy: %s\n' "$policy"
2052
-
2053
- if [[ "$TOPOLOGY" == "none" || "$repo_value" == "none" ]]; then
2054
- if [[ "$repo_value" == "none" ]]; then
2055
- printf 'state: no-git\naction: refuse\nreason: git.repo is none\n'
2056
- else
2057
- printf 'state: no-git\naction: refuse\nreason: not a git work tree\n'
2058
- fi
2059
- exit 0
2060
- fi
2061
-
2062
- if [[ "$TOPOLOGY" == "workspace" ]]; then
2063
- # One policy for the whole run, resolved per repo: a run cannot follow two
2064
- # branch policies, so a nested repo that disagrees is reported as drift.
2065
- while IFS= read -r dir; do
2066
- [[ -n "$dir" ]] || continue
2067
- d="$ROOT/$dir"
2068
- h="$(git -C "$d" branch --show-current 2>/dev/null || true)"
2069
- st="$(classify "$d" "$h")"
2070
- sp="$(start_point "$d" "$h")"
2071
- base="$(repo_base "$d")"
2072
- if [[ "$st" == "on-base" ]]; then unp="$(unpublished "$d" "$h")"; else unp=n/a; fi
2073
- printf 'repo %s: head=%s state=%s base=%s start-point=%s unpublished=%s action=%s\n' \
2074
- "$dir" "${h:-none}" "$st" "${base:-none}" "$sp" "$unp" "$(decide "$st" "$policy" "$sp" "$unp")"
2075
- own=""
2076
- if [[ -f "$d/.sassywares/config.yaml" ]]; then
2077
- own="$(yflat_in "$d/.sassywares/config.yaml" "commit${US}branch" 2>/dev/null || true)"
2078
- fi
2079
- [[ -n "$own" && "$own" != "$policy" ]] && printf 'drift %s: commit.branch=%s\n' "$dir" "$own"
2080
- done < <(nested_repos)
2081
- exit 0
2082
- fi
2083
-
2084
- head="${HEAD_ARG:-$(git -C "$ROOT" branch --show-current 2>/dev/null || true)}"
2085
- actual="$(git -C "$ROOT" branch --show-current 2>/dev/null || true)"
2086
- [[ -n "$HEAD_ARG" && "$HEAD_ARG" != "$actual" ]] && printf 'checked-out: %s\n' "${actual:-none}"
2087
- printf 'head: %s\n' "${head:-none}"
2088
-
2089
- state="$(classify "$ROOT" "$head")"
2090
- printf 'state: %s\n' "$state"
2091
- start="$(start_point "$ROOT" "$head")"
2092
- unp=n/a
2093
- [[ "$state" == "on-base" ]] && unp="$(unpublished "$ROOT" "$head")"
2094
- printf 'action: %s\n' "$(decide "$state" "$policy" "$start" "$unp")"
2095
- printf 'start-point: %s\n' "$start"
2096
- printf 'unpublished: %s\n' "$unp"
2097
-
2098
- # pr-base is provisional for a branch that does not exist yet: the name the
2099
- # skill picks can match a base_map family (release/*, hotfix/*) and retarget
2100
- # the PR. Pass --name once the name is known to get the final answer.
2101
- pr_src=config
2102
- if [[ -n "$NAME_ARG" ]]; then
2103
- pr_base="$(base_for_head "$NAME_ARG" 2>/dev/null || true)"
2104
- elif [[ "$state" == "on-base" || "$state" == "detached" ]]; then
2105
- pr_base="$(repo_base "$ROOT")"
2106
- else
2107
- pr_base="$(base_for_head "$head" 2>/dev/null || true)"
2108
- fi
2109
- if [[ -z "$pr_base" ]]; then
2110
- pr_base="$(detect_base "$ROOT" 2>/dev/null || true)"
2111
- pr_src=probed
2112
- fi
2113
- [[ -n "$pr_base" ]] || pr_src=unknown
2114
- printf 'pr-base: %s\n' "$pr_base"
2115
- printf 'pr-base-source: %s\n' "$pr_src"
2116
- printf 'pr-base-final: %s\n' "$( [[ -n "$NAME_ARG" ]] && printf yes || printf no )"
2117
- exit 0
2118
- fi
2119
-
2120
- # --- Template sync ---------------------------------------------------------
2121
- # The one mode that writes. It computes the same proposal the default mode
2122
- # prints, merges the current file onto it (see --sync at the top), and either
2123
- # prints the result (dry run, the default) or writes it with --write. The file is
2124
- # left untouched when the merge is identical, so a second run is a no-op that
2125
- # says so.
2126
- if (( SYNC )); then
2127
- [[ -n "$CONFIG" ]] || { echo "no .sassywares/config.yaml found — run sassy-setup" >&2; exit 1; }
2128
- # --agents scopes the run to AGENTS.md: the config merge is skipped, not printed.
2129
- if (( ! AGENTS_ONLY )); then
2130
- merged="$(merged_config)"
2131
- current="$(cat "$CONFIG")"
2132
- if [[ "$merged" == "$current" ]]; then
2133
- (( SINGLE )) || printf '%s\n' "$merged"
2134
- echo "sync: $CONFIG is already current" >&2
2135
- else
2136
- before="$(mktemp "${TMPDIR:-/tmp}/sassy-sync-before.XXXXXX")"
2137
- after="$(mktemp "${TMPDIR:-/tmp}/sassy-sync-after.XXXXXX")"
2138
- printf '%s\n' "$current" > "$before"
2139
- printf '%s\n' "$merged" > "$after"
2140
- changed="$(diff -u "$before" "$after" 2>/dev/null | awk '/^\+[^+]|^-[^-]/{n++} END{print n+0}' || true)"
2141
- rm -f "$before" "$after"
2142
- if (( SINGLE )); then
2143
- printf '%s\n' "$merged" > "$CONFIG"
2144
- echo "sync: wrote $CONFIG ($changed line(s) changed)" >&2
2145
- else
2146
- printf '%s\n' "$merged"
2147
- echo "sync: $CONFIG would change ($changed line(s)) — nothing written; rerun with --sync --write" >&2
2148
- fi
2149
- fi
2150
- fi
2151
- # AGENTS.md is the second thing --sync owns. Its block is independent of the
2152
- # config merge, so a run whose config is already current still refreshes it,
2153
- # and an untouched project is a no-op that says so.
2154
- if (( NO_AGENTS )); then
2155
- echo "sync: AGENTS.md untouched (--no-agents)" >&2
2156
- elif (( SINGLE )); then agents_md_sync 0; else agents_md_sync 1; fi
2157
- exit $?
2158
- fi
193
+ cd -P "$(dirname "$_self")" && pwd
194
+ )"
195
+ load() {
196
+ local f="$SCRIPT_DIR/lib/$1" fn="$2"
197
+ [[ -r "$f" ]] || { echo "detect.sh: missing module $f — the sassy-setup skill is incomplete; reinstall it" >&2; exit 2; }
198
+ bash -n "$f" 2>/dev/null \
199
+ || { echo "detect.sh: broken module $f — syntax error; the sassy-setup skill is incomplete; reinstall it" >&2; exit 2; }
200
+ # shellcheck source=/dev/null
201
+ . "$f"
202
+ declare -F "$fn" >/dev/null \
203
+ || { echo "detect.sh: incomplete module $f — missing $fn; the sassy-setup skill is incomplete; reinstall it" >&2; exit 2; }
204
+ }
205
+
206
+ load common.sh is_suite_skill
207
+ load yaml.sh yaml_flat
208
+ load worktree.sh mode_worktree_script
209
+ load agents-md.sh merged_config
210
+ load proposal.sh emit_proposal
211
+ load check.sh mode_check
212
+ load branch.sh mode_branch
213
+ load sync.sh mode_sync
214
+
215
+ # --- Dispatch ---------------------------------------------------------------
216
+ # One order, and it is load-bearing: --worktree-script answers and returns
217
+ # before any mode, then exactly one of --check, --branch, --sync runs, and the
218
+ # proposal is the fall-through default.
219
+ if [[ -n "$WT_SCRIPT" ]]; then mode_worktree_script; fi
220
+
221
+ resolve_artifacts
222
+ resolve_git_defaults
223
+
224
+ if (( CHECK )); then mode_check; fi
225
+ if (( BRANCH )); then mode_branch; fi
226
+ if (( SYNC )); then mode_sync; fi
2159
227
 
2160
228
  emit_proposal
2161
-