@sassywares/skills 0.17.1 → 0.17.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/README.md +28 -6
  2. package/package.json +1 -1
  3. package/skills/sassy-babysit/SKILL.md +2 -2
  4. package/skills/sassy-dogfood/SKILL.md +11 -8
  5. package/skills/sassy-dogfood/references/check.md +5 -3
  6. package/skills/sassy-dogfood/references/discover.md +23 -12
  7. package/skills/sassy-dogfood/references/run.md +91 -17
  8. package/skills/sassy-setup/SKILL.md +5 -5
  9. package/skills/sassy-setup/assets/dogfood-recipe.md +19 -5
  10. package/skills/sassy-setup/references/anchors.md +1 -1
  11. package/skills/sassy-setup/references/branching.md +1 -1
  12. package/skills/sassy-setup/references/check.md +1 -1
  13. package/skills/sassy-setup/references/config.md +66 -14
  14. package/skills/sassy-setup/references/dogfood.md +181 -40
  15. package/skills/sassy-setup/references/harness.md +1 -1
  16. package/skills/sassy-setup/references/init.md +6 -4
  17. package/skills/sassy-setup/references/plan-resolution.md +1 -1
  18. package/skills/sassy-setup/references/scaffold-header.md +9 -2
  19. package/skills/sassy-setup/references/workspace.md +63 -1
  20. package/skills/sassy-setup/references/worktree.md +1 -1
  21. package/skills/sassy-setup/scripts/detect.sh +49 -1982
  22. package/skills/sassy-setup/scripts/hash-skill.mjs +1 -1
  23. package/skills/sassy-setup/scripts/lib/agents-md.sh +513 -0
  24. package/skills/sassy-setup/scripts/lib/branch.sh +200 -0
  25. package/skills/sassy-setup/scripts/lib/check.sh +940 -0
  26. package/skills/sassy-setup/scripts/lib/common.sh +128 -0
  27. package/skills/sassy-setup/scripts/lib/proposal.sh +248 -0
  28. package/skills/sassy-setup/scripts/lib/sync.sh +43 -0
  29. package/skills/sassy-setup/scripts/lib/worktree.sh +57 -0
  30. package/skills/sassy-setup/scripts/lib/yaml.sh +273 -0
  31. package/skills/sassy-setup/scripts/lockcheck.mjs +1 -1
  32. package/skills/sassy-setup/scripts/worktree-check.mjs +1 -1
  33. package/skills/sassy-ship/SKILL.md +4 -4
  34. package/skills/sassy-ship/references/brief.md +1 -1
  35. package/skills/sassy-ship/references/plan.md +1 -1
  36. package/skills/sassy-ship/references/slice.md +1 -1
@@ -1,4 +1,4 @@
1
- // generated_by: sassy-setup@0.17.0
1
+ // generated_by: sassy-setup@0.17.3
2
2
  // The `skills` CLI's skill-folder hash, as sassy-setup checks it.
3
3
  //
4
4
  // Two eras are live in the wild and the lock's `version` says which one wrote
@@ -0,0 +1,513 @@
1
+ # generated_by: sassy-setup@0.17.3
2
+ # lib/agents-md.sh — the AGENTS.md routing block and the template merge.
3
+ # Sourced by detect.sh. Never sets shell options; reads the paths it set. The
4
+ # merge starts from emit_proposal (lib/proposal.sh) from inside a function body,
5
+ # so it runs after every module is loaded.
6
+
7
+ # --- AGENTS.md routing block ----------------------------------------------
8
+ # One shipped file, assets/agents-md.md, used two ways: the whole file when the
9
+ # project has none, and the marked block appended to an existing one. The
10
+ # version lives in the markers, so a later shipped version can replace an older
11
+ # block by exact marker match. Placeholders are rendered from the resolved
12
+ # artifacts.* paths, never hardcoded here.
13
+ AGENTS_ASSET="$SKILL_DIR/assets/agents-md.md"
14
+ # The marker grammar in one place, valid as grep ERE, sed BRE, and awk ERE.
15
+ AGENTS_OPEN_RE='^<!-- sassywares:skills v[0-9][0-9]* -->$'
16
+ AGENTS_CLOSE_RE='^<!-- /sassywares:skills v[0-9][0-9]* -->$'
17
+
18
+ agents_md_open_no() { grep -n -E "$AGENTS_OPEN_RE" "$1" 2>/dev/null | head -n 1 | cut -d: -f1; }
19
+ agents_md_close_no() { grep -n -E "$AGENTS_CLOSE_RE" "$1" 2>/dev/null | head -n 1 | cut -d: -f1; }
20
+ agents_md_version() { sed -n 's/^<!-- sassywares:skills v\([0-9][0-9]*\) -->$/\1/p' "$1" 2>/dev/null | head -n 1; }
21
+ agents_md_shipped_version() { agents_md_version "$AGENTS_ASSET"; }
22
+ agents_md_close_version() { sed -n 's/^<!-- \/sassywares:skills v\([0-9][0-9]*\) -->$/\1/p' "$1" 2>/dev/null | head -n 1; }
23
+
24
+ # agents_md_artifact KEY DEFAULT — the configured path, else the default this
25
+ # detector already resolved. Relative to the project root, like every artifact.
26
+ agents_md_artifact() {
27
+ local v
28
+ v="$(block_value artifacts "$1" 2>/dev/null || true)"
29
+ [[ -n "$v" ]] || v="$2"
30
+ printf '%s' "${v#./}"
31
+ }
32
+
33
+ # The shipped block, rendered, on stdout.
34
+ agents_md_render() {
35
+ local strategy concepts plans solutions handbooks
36
+ strategy="$(agents_md_artifact strategy "$STRATEGY")"
37
+ concepts="$(agents_md_artifact concepts "$CONCEPTS")"
38
+ plans="$(agents_md_artifact plans "$PLANS")"
39
+ solutions="$(agents_md_artifact solutions "$SOLUTIONS")"
40
+ handbooks="$(agents_md_artifact handbooks "$HANDBOOKS")"
41
+ # Values go through the environment, not -v: awk's -v interprets escape
42
+ # sequences, so a configured path with a backslash would lose it.
43
+ SASSY_AGENTS_STRATEGY="$strategy" SASSY_AGENTS_CONCEPTS="$concepts" \
44
+ SASSY_AGENTS_PLANS="$plans" SASSY_AGENTS_SOLUTIONS="$solutions" \
45
+ SASSY_AGENTS_HANDBOOKS="$handbooks" \
46
+ awk -v openre="$AGENTS_OPEN_RE" -v closere="$AGENTS_CLOSE_RE" '
47
+ # Splice a value literally: no regex replacement, so an & or a backslash in
48
+ # a configured path survives. Scanning resumes past the inserted value, so a
49
+ # value that itself contains the placeholder cannot loop.
50
+ function put(line, ph, val, i, from) {
51
+ from = 1
52
+ while ((i = index(substr(line, from), ph)) > 0) {
53
+ i = from + i - 1
54
+ line = substr(line, 1, i-1) val substr(line, i+length(ph))
55
+ from = i + length(val)
56
+ }
57
+ return line
58
+ }
59
+ BEGIN {
60
+ strategy = ENVIRON["SASSY_AGENTS_STRATEGY"]
61
+ concepts = ENVIRON["SASSY_AGENTS_CONCEPTS"]
62
+ plans = ENVIRON["SASSY_AGENTS_PLANS"]
63
+ solutions = ENVIRON["SASSY_AGENTS_SOLUTIONS"]
64
+ handbooks = ENVIRON["SASSY_AGENTS_HANDBOOKS"]
65
+ }
66
+ $0 ~ openre { f = 1 }
67
+ f {
68
+ line = $0
69
+ line = put(line, "{{artifacts.strategy}}", strategy)
70
+ line = put(line, "{{artifacts.concepts}}", concepts)
71
+ line = put(line, "{{artifacts.plans}}", plans)
72
+ line = put(line, "{{artifacts.solutions}}", solutions)
73
+ line = put(line, "{{artifacts.handbooks}}", handbooks)
74
+ print line
75
+ }
76
+ f && $0 ~ closere { exit }' "$AGENTS_ASSET"
77
+ }
78
+
79
+ # agents_md_block FILE — the block exactly as it sits in FILE, on stdout.
80
+ agents_md_block() {
81
+ awk -v openre="$AGENTS_OPEN_RE" -v closere="$AGENTS_CLOSE_RE" '
82
+ $0 ~ openre { f = 1 }
83
+ f { print }
84
+ f && $0 ~ closere { exit }' "$1"
85
+ }
86
+
87
+ # agents_md_plan — what --sync --write would do to AGENTS.md:
88
+ # create | append | replace | current | newer | unterminated | mismatch | unreadable
89
+ agents_md_plan() {
90
+ local target="$ROOT/AGENTS.md" open_no close_no v cv shipped
91
+ shipped="$(agents_md_shipped_version)"
92
+ [[ -n "$shipped" ]] || { printf 'unreadable'; return; }
93
+ [[ -f "$target" ]] || { printf 'create'; return; }
94
+ open_no="$(agents_md_open_no "$target")"
95
+ [[ -n "$open_no" ]] || { printf 'append'; return; }
96
+ close_no="$(agents_md_close_no "$target")"
97
+ if [[ -z "$close_no" ]] || (( close_no < open_no )); then printf 'unterminated'; return; fi
98
+ v="$(agents_md_version "$target")"
99
+ cv="$(agents_md_close_version "$target")"
100
+ [[ -n "$v" && "$v" == "$cv" ]] || { printf 'mismatch'; return; }
101
+ if (( 10#$v < 10#$shipped )); then printf 'replace'; return; fi
102
+ if (( 10#$v > 10#$shipped )); then printf 'newer'; return; fi
103
+ if diff -q <(agents_md_block "$target") <(agents_md_render) >/dev/null 2>&1; then
104
+ printf 'current'
105
+ else
106
+ printf 'replace'
107
+ fi
108
+ }
109
+
110
+ # agents_md_write ACTION RENDERED — the block is the only region touched. The
111
+ # file is replaced through a temp in the project root, so a failure never leaves
112
+ # a half-written AGENTS.md.
113
+ agents_md_write() {
114
+ local action="$1" rendered="$2" target="$ROOT/AGENTS.md" tmp mode rendered_file
115
+ case "$target" in "$ROOT"/*) ;; *) echo "sync: refusing to write outside the project root: $target" >&2; return 1 ;; esac
116
+ tmp="$(mktemp "$ROOT/.AGENTS.md.XXXXXX")" || return 1
117
+ case "$action" in
118
+ create)
119
+ printf '%s\n' "$rendered" > "$tmp"
120
+ ;;
121
+ append)
122
+ cat "$target" > "$tmp"
123
+ # Every byte the file already carried is kept; only a separator is added.
124
+ if [[ -n "$(tail -c 1 "$target")" ]]; then printf '\n' >> "$tmp"; fi
125
+ printf '\n%s\n' "$rendered" >> "$tmp"
126
+ ;;
127
+ replace)
128
+ # The rendered block is passed as a file, not -v: awk's -v would try to
129
+ # interpret the block's newlines and backslashes as escapes.
130
+ rendered_file="$(mktemp "${TMPDIR:-/tmp}/sassy-agents-block.XXXXXX")" || { rm -f "$tmp"; return 1; }
131
+ printf '%s\n' "$rendered" > "$rendered_file"
132
+ if ! awk -v openre="$AGENTS_OPEN_RE" -v closere="$AGENTS_CLOSE_RE" -v replf="$rendered_file" '
133
+ FILENAME == replf { a[++n] = $0; next }
134
+ $0 ~ openre { for (i = 1; i <= n; i++) print a[i]; skip = 1; next }
135
+ skip && $0 ~ closere { skip = 0; next }
136
+ !skip { print }
137
+ ' "$rendered_file" "$target" > "$tmp"; then
138
+ rm -f "$tmp" "$rendered_file"; return 1
139
+ fi
140
+ rm -f "$rendered_file"
141
+ ;;
142
+ *) rm -f "$tmp"; return 1 ;;
143
+ esac
144
+ if [[ -f "$target" ]]; then
145
+ mode="$(stat -f '%Lp' "$target" 2>/dev/null || stat -c '%a' "$target" 2>/dev/null || true)"
146
+ [[ -z "$mode" ]] || chmod "$mode" "$tmp" 2>/dev/null || true
147
+ fi
148
+ mv "$tmp" "$target"
149
+ }
150
+
151
+ # agents_md_sync DRY — create, append, or replace the block; a second run is a
152
+ # no-op that says so. DRY 1 only reports what a write would do.
153
+ agents_md_sync() {
154
+ local dry="$1" action shipped v
155
+ action="$(agents_md_plan)"
156
+ shipped="$(agents_md_shipped_version)"
157
+ case "$action" in
158
+ create|append|replace)
159
+ if (( dry )); then
160
+ case "$action" in
161
+ create) echo "sync: AGENTS.md would be created with routing block v${shipped} — nothing written; rerun with --sync --write" >&2 ;;
162
+ append) echo "sync: AGENTS.md would get routing block v${shipped} appended — nothing written; rerun with --sync --write" >&2 ;;
163
+ replace) echo "sync: AGENTS.md routing block would be replaced with v${shipped} — nothing written; rerun with --sync --write" >&2 ;;
164
+ esac
165
+ return 0
166
+ fi
167
+ agents_md_write "$action" "$(agents_md_render)" || { echo "sync: could not write AGENTS.md" >&2; return 1; }
168
+ case "$action" in
169
+ create) echo "sync: wrote AGENTS.md (routing block v${shipped} created)" >&2 ;;
170
+ append) echo "sync: wrote AGENTS.md (routing block v${shipped} appended)" >&2 ;;
171
+ replace) echo "sync: wrote AGENTS.md (routing block replaced with v${shipped})" >&2 ;;
172
+ esac
173
+ ;;
174
+ current)
175
+ echo "sync: AGENTS.md is already current" >&2
176
+ ;;
177
+ newer)
178
+ v="$(agents_md_version "$ROOT/AGENTS.md")"
179
+ echo "sync: AGENTS.md routing block is v${v}, newer than this skill ships (v${shipped}) — left alone" >&2
180
+ ;;
181
+ unterminated)
182
+ echo "sync: AGENTS.md has an opening sassywares:skills marker with no matching closing marker — fix it by hand, nothing written" >&2
183
+ ;;
184
+ mismatch)
185
+ echo "sync: AGENTS.md routing block markers disagree on the version — fix it by hand, nothing written" >&2
186
+ ;;
187
+ *)
188
+ echo "sync: no routing-block asset found — AGENTS.md untouched" >&2
189
+ ;;
190
+ esac
191
+ return 0
192
+ }
193
+
194
+ # --- Template merge -------------------------------------------------------
195
+ # --sync starts from the proposal the default mode prints (emit_proposal) and
196
+ # merges the existing file onto it: a leaf the file declares and this check
197
+ # accepts keeps its line — value and same-line trailing comment — exactly as
198
+ # written, and every block, key, comment layer, and example the file lacks comes
199
+ # from the proposal. A value --check reports as drift is not accepted, so it is
200
+ # replaced by the proposal's, the same judgement --check makes.
201
+ #
202
+ # "Value --check accepts" is one predicate, leaf_ok, over the schema in
203
+ # references/config.md. It deliberately validates only the value, never its
204
+ # context: a `worktrees.enabled: true` with no tooling installed is a valid value
205
+ # in a wrong state, so the sync keeps it and the check keeps reporting it.
206
+ leaf_ok() { # PATH VALUE — PATH joined with US, value comment- and quote-stripped
207
+ local path="$1" v="$2" key kind rest pat point entry name
208
+ key="${path//$US/.}"
209
+ case "$key" in
210
+ artifacts.plans|artifacts.solutions|artifacts.handbooks|artifacts.concepts|artifacts.strategy)
211
+ case "$v" in ""|/*|~*|.|./) return 1 ;; esac
212
+ escapes_root "$v" && return 1
213
+ case "$key" in
214
+ artifacts.concepts|artifacts.strategy) [[ "$v" == */ ]] && return 1 ;;
215
+ esac
216
+ return 0 ;;
217
+ knowledge.read)
218
+ case "$v" in \[*\]) ;; *) return 1 ;; esac
219
+ while IFS= read -r entry; do
220
+ [[ -n "$entry" ]] || continue
221
+ case "$entry" in /*|~*) return 1 ;; esac
222
+ escapes_root "$entry" && return 1
223
+ done < <(list_items "$v")
224
+ return 0 ;;
225
+ knowledge.write|pr.babysit|pr.merge|pr.delete_branch|dogfood.enabled|worktrees.enabled)
226
+ [[ "$v" == "true" || "$v" == "false" ]] ;;
227
+ git.repo)
228
+ if [[ "$TOPOLOGY" == "repo" ]]; then
229
+ case "$v" in ""|workspace|none) return 1 ;; esac
230
+ git -C "$ROOT" remote 2>/dev/null | grep -qx "$v"
231
+ else
232
+ [[ "$v" == "workspace" || "$v" == "none" ]]
233
+ fi ;;
234
+ git.remote|git.mirror_branch)
235
+ [[ "$TOPOLOGY" != "repo" && -n "$v" ]] ;;
236
+ git.base_map.*)
237
+ pat="${key#git.base_map.}"
238
+ case "$pat" in
239
+ *"*") [[ "$pat" == "*" || ( -n "${pat%\*}" && "${pat%\*}" != *"*" ) ]] || return 1 ;;
240
+ esac
241
+ return 0 ;;
242
+ repos.*)
243
+ [[ "$TOPOLOGY" != "repo" ]] ;;
244
+ commit.branch)
245
+ [[ "$v" == "current" || "$v" == "new" ]] ;;
246
+ dogfood.timeout|dogfood.keep)
247
+ [[ "$v" =~ ^[1-9][0-9]*$ ]] ;;
248
+ dogfood.recipe|dogfood.reports)
249
+ case "$v" in ""|/*|~*|.|./) return 1 ;; esac
250
+ escapes_root "$v" && return 1
251
+ return 0 ;;
252
+ dogfood.recipes.*)
253
+ # Workspace only, like repos: a repo config keeps the single `recipe:`.
254
+ # A value is a path inside the project or `false`; the repo-key membership
255
+ # rule is context, not value, so the sync keeps a key only --check judges.
256
+ [[ "$TOPOLOGY" != "repo" ]] || return 1
257
+ case "$v" in
258
+ false) return 0 ;;
259
+ ""|true|yes|no|on|off|/*|~*|.|./) return 1 ;;
260
+ esac
261
+ escapes_root "$v" && return 1
262
+ return 0 ;;
263
+ worktrees.dir)
264
+ [[ -n "$v" ]] ;;
265
+ worktrees.create|worktrees.remove)
266
+ ! wt_script_reason "$v" >/dev/null ;;
267
+ hooks.*)
268
+ kind="${key#hooks.}"; kind="${kind%%.*}"
269
+ case "$kind" in pr|push|goal) ;; *) return 1 ;; esac
270
+ rest="${key#hooks.}"; rest="${rest#*.}"
271
+ point="${rest##*.}"
272
+ pat="${rest%.*}"
273
+ case " $(hook_points "$kind") " in *" $point "*) ;; *) return 1 ;; esac
274
+ case "$pat" in
275
+ *"*") [[ "$pat" == "*" || ( -n "${pat%\*}" && "${pat%\*}" != *"*" ) ]] || return 1 ;;
276
+ esac
277
+ while IFS= read -r entry; do
278
+ [[ -n "$entry" ]] || continue
279
+ name="${entry%% *}"
280
+ is_suite_skill "$name" && return 1
281
+ hook_skill_path "$name" >/dev/null 2>&1 || return 1
282
+ done < <(list_items "$v")
283
+ return 0 ;;
284
+ *)
285
+ return 1 ;;
286
+ esac
287
+ }
288
+
289
+ # One parser, two readers: the flat path stack is the same shape yaml_flat
290
+ # builds, so a line can be matched to the current file and to the proposal.
291
+ LEAF_AWK='function trim(s) { sub(/^[ \t]+/, "", s); sub(/[ \t]+$/, "", s); return s }
292
+ function ind(s, m) { m = match(s, /^ */); return m ? RLENGTH : 0 }
293
+ {
294
+ raw = $0
295
+ body0 = trim(raw)
296
+ if (body0 == "" || substr(body0, 1, 1) == "#") next
297
+ line = raw; sub(/#.*$/, "", line)
298
+ d = ind(line); body = trim(line)
299
+ if (body ~ /:$/) {
300
+ k = body; sub(/:$/, "", k); gsub(/"/, "", k)
301
+ while (sp > 0 && sd[sp] >= d) sp--
302
+ sd[++sp] = d; sk[sp] = k
303
+ next
304
+ }
305
+ k = body; sub(/:.*/, "", k); gsub(/"/, "", k)
306
+ while (sp > 0 && sd[sp] >= d) sp--
307
+ p = ""
308
+ for (i = 1; i <= sp; i++) p = p (p == "" ? "" : us) sk[i]
309
+ print p (p == "" ? "" : us) k "\t" raw
310
+ }'
311
+
312
+ # The merge. Records are `kind <TAB> depth <TAB> path <TAB> raw`; a commented
313
+ # example is a record too, with the depth its `#` markers would have if removed
314
+ # (the hash characters themselves add no depth, the spaces after them do), so
315
+ # `# push:` under `hooks:` is known to be the posited `hooks.push` block. When the
316
+ # file declares a valid leaf under such an example, the example is replaced by
317
+ # the file's own active block, verbatim — the one case where a commented example
318
+ # in the proposal is not an example for this project.
319
+ MERGE_AWK='function trim(s) { sub(/^[ \t]+/, "", s); sub(/[ \t]+$/, "", s); return s }
320
+ function ind(s, m) { m = match(s, /^ */); return m ? RLENGTH : 0 }
321
+ function joinall( p, i) { p = ""; for (i = 1; i <= sp; i++) p = p (p == "" ? "" : us) sk[i]; return p }
322
+ function dopop(d) { while (sp > 0 && sd[sp] >= d) sp-- }
323
+ function outf(r, p) { o++; oraw[o] = r; okind[o] = ""; opath[o] = p }
324
+ function outfk(r, d, p) { o++; oraw[o] = r; okind[o] = "key"; odepth[o] = d; opath[o] = p }
325
+ function outfl(r, d, p) { o++; oraw[o] = r; okind[o] = "leaf"; odepth[o] = d; opath[o] = p }
326
+ function under_accepted(P, a) {
327
+ for (a = 1; a <= na; a++) if (index(aorder[a], P us) == 1) return 1
328
+ return 0
329
+ }
330
+ function emit_acc(P, d) { o++; oraw[o] = acc[P]; okind[o] = "leaf"; odepth[o] = d; opath[o] = P; emitted[P] = 1 }
331
+ function emit_block(P, i, j, e, a) {
332
+ b0 = 0
333
+ for (i = 1; i <= cn; i++) if (ckind[i] == "key" && cpath[i] == P) { b0 = i; break }
334
+ if (b0 == 0) { warn[++nw] = P; return }
335
+ e = b0
336
+ for (j = b0 + 1; j <= cn; j++) {
337
+ if (ckind[j] == "blank") continue
338
+ if (cdepth[j] > cdepth[b0]) { e = j; continue }
339
+ break
340
+ }
341
+ for (j = b0; j <= e; j++) {
342
+ if (ckind[j] == "key") outfk(crawl[j], cdepth[j], cpath[j])
343
+ else if (ckind[j] == "leaf") outfl(crawl[j], cdepth[j], cpath[j])
344
+ else outf(crawl[j])
345
+ }
346
+ for (a = 1; a <= na; a++) if (index(aorder[a], P us) == 1) emitted[aorder[a]] = 1
347
+ emitted[P] = 1
348
+ }
349
+ BEGIN { cn = 0; pn = 0; na = 0; sp = 0; o = 0; nw = 0 }
350
+ FILENAME == accf {
351
+ if ($2 != "") { acc[$1] = $2; aorder[++na] = $1 }
352
+ next
353
+ }
354
+ FILENAME == curf {
355
+ raw = $0
356
+ body0 = trim(raw)
357
+ if (body0 == "") { ckind[++cn] = "blank"; crawl[cn] = raw; next }
358
+ if (substr(body0, 1, 1) == "#") { ckind[++cn] = "comment"; crawl[cn] = raw; next }
359
+ line = raw; sub(/#.*$/, "", line)
360
+ d = ind(line); body = trim(line)
361
+ if (body ~ /:$/) {
362
+ k = body; sub(/:$/, "", k); gsub(/"/, "", k)
363
+ dopop(d); sd[++sp] = d; sk[sp] = k
364
+ ckind[++cn] = "key"; cdepth[cn] = d; cpath[cn] = joinall(); crawl[cn] = raw
365
+ } else {
366
+ k = body; sub(/:.*/, "", k); gsub(/"/, "", k)
367
+ dopop(d); p = joinall()
368
+ ckind[++cn] = "leaf"; cdepth[cn] = d; cpath[cn] = p (p == "" ? "" : us) k; crawl[cn] = raw
369
+ }
370
+ next
371
+ }
372
+ FILENAME == propf {
373
+ raw = $0
374
+ body0 = trim(raw)
375
+ if (body0 == "") { pkind[++pn] = "blank"; pdepth[pn] = 0; ppath[pn] = ""; praw[pn] = raw; next }
376
+ if (substr(body0, 1, 1) == "#") {
377
+ rest = body0
378
+ while (substr(rest, 1, 1) == "#") rest = substr(rest, 2)
379
+ m = match(rest, /^ */); spc = m ? RLENGTH : 0
380
+ rest = substr(rest, spc + 1)
381
+ # A commented example sits at its `#` column, and its content starts two
382
+ # columns later ("# "); the spaces after the marker encode the depth of a
383
+ # child, so subtracting the marker leaves the line the example stands for.
384
+ ed = ind(raw) + (spc > 0 ? spc - 1 : 0)
385
+ if (rest ~ /^[^ \t:]+:/) {
386
+ k = rest; sub(/:.*/, "", k); gsub(/"/, "", k)
387
+ v = rest; sub(/^[^:]*:/, "", v); sub(/#.*/, "", v); v = trim(v)
388
+ dopop(ed)
389
+ if (v == "") {
390
+ sd[++sp] = ed; sk[sp] = k
391
+ pkind[++pn] = "example_key"; pdepth[pn] = ed; ppath[pn] = joinall(); praw[pn] = raw
392
+ } else {
393
+ p = joinall()
394
+ pkind[++pn] = "example_leaf"; pdepth[pn] = ed
395
+ ppath[pn] = p (p == "" ? "" : us) k; praw[pn] = raw
396
+ }
397
+ } else {
398
+ pkind[++pn] = "comment"; pdepth[pn] = 0; ppath[pn] = ""; praw[pn] = raw
399
+ }
400
+ next
401
+ }
402
+ line = raw; sub(/#.*$/, "", line)
403
+ d = ind(line); body = trim(line)
404
+ if (body ~ /:$/) {
405
+ k = body; sub(/:$/, "", k); gsub(/"/, "", k)
406
+ dopop(d); sd[++sp] = d; sk[sp] = k
407
+ pkind[++pn] = "key"; pdepth[pn] = d; ppath[pn] = joinall(); praw[pn] = raw
408
+ } else {
409
+ k = body; sub(/:.*/, "", k); gsub(/"/, "", k)
410
+ dopop(d); p = joinall()
411
+ pkind[++pn] = "leaf"; pdepth[pn] = d
412
+ ppath[pn] = p (p == "" ? "" : us) k; praw[pn] = raw
413
+ }
414
+ next
415
+ }
416
+ END {
417
+ i = 1
418
+ while (i <= pn) {
419
+ if (pkind[i] == "blank" || pkind[i] == "comment") { outf(praw[i]); i++; continue }
420
+ if (pkind[i] == "example_key") {
421
+ j = i + 1
422
+ while (j <= pn && (pkind[j] == "example_key" || pkind[j] == "example_leaf") && pdepth[j] > pdepth[i]) j++
423
+ if (under_accepted(ppath[i])) {
424
+ emit_block(ppath[i])
425
+ } else {
426
+ for (m = i; m < j; m++) outf(praw[m])
427
+ }
428
+ i = j; continue
429
+ }
430
+ if (pkind[i] == "example_leaf") {
431
+ if (ppath[i] in acc) emit_acc(ppath[i], pdepth[i]); else outf(praw[i])
432
+ i++; continue
433
+ }
434
+ if (pkind[i] == "leaf") {
435
+ if (ppath[i] in acc) emit_acc(ppath[i], pdepth[i]); else outfl(praw[i], pdepth[i], ppath[i])
436
+ i++; continue
437
+ }
438
+ outfk(praw[i], pdepth[i], ppath[i])
439
+ i++
440
+ }
441
+ # An accepted leaf the proposal has no slot for — an extra base_map rung, a
442
+ # hand-written head — is inserted under its own block, after the last line
443
+ # of that block. Dropping it would be the one thing the sync must never do:
444
+ # change a value the check accepts.
445
+ for (a = 1; a <= na; a++) {
446
+ P = aorder[a]
447
+ if (P in emitted) continue
448
+ par = P; sub(us "[^" us "]*$", "", par)
449
+ if (par == P) { warn[++nw] = P; continue }
450
+ b0 = 0; bd = -1
451
+ for (m = 1; m <= o; m++) if (okind[m] == "key" && opath[m] == par) { b0 = m; bd = odepth[m] }
452
+ if (b0 == 0) { warn[++nw] = P; continue }
453
+ last = b0
454
+ for (m = b0 + 1; m <= o; m++) {
455
+ if (okind[m] == "blank") continue
456
+ if (odepth[m] <= bd) break
457
+ last = m
458
+ }
459
+ insn[last]++
460
+ insraw[last, insn[last]] = acc[P]
461
+ }
462
+ for (m = 1; m <= o; m++) {
463
+ print oraw[m]
464
+ for (q = 1; q <= insn[m]; q++) print insraw[m, q]
465
+ }
466
+ for (m = 1; m <= nw; m++) print "sync: no slot for " warn[m] > "/dev/stderr"
467
+ }'
468
+
469
+ leaf_lines() { # FILE -> `path<TAB>raw line` for every active leaf
470
+ awk -v us="$US" "$LEAF_AWK" "$1"
471
+ }
472
+
473
+ accepted_table() { # -> `path<TAB>raw line` for every leaf --check accepts today
474
+ local path raw value
475
+ leaf_lines "$CONFIG" | while IFS=$'\t' read -r path raw; do
476
+ [[ -n "$path" ]] || continue
477
+ value="$(printf '%s' "$raw" \
478
+ | sed -E 's/^[[:space:]]*[^:]*:[[:space:]]*//; s/[[:space:]]+#.*$//; s/[[:space:]]+$//')"
479
+ value="${value#\"}"; value="${value%\"}"
480
+ if leaf_ok "$path" "$value"; then printf '%s\t%s\n' "$path" "$raw"; fi
481
+ done
482
+ }
483
+
484
+ merged_config() { # the current file merged onto the proposal, on stdout
485
+ local prop acc
486
+ prop="$(mktemp "${TMPDIR:-/tmp}/sassy-sync-proposal.XXXXXX")"
487
+ acc="$(mktemp "${TMPDIR:-/tmp}/sassy-sync-accepted.XXXXXX")"
488
+ emit_proposal > "$prop"
489
+ accepted_table > "$acc"
490
+ awk -F'\t' -v us="$US" -v accf="$acc" -v curf="$CONFIG" -v propf="$prop" \
491
+ "$MERGE_AWK" "$acc" "$CONFIG" "$prop"
492
+ rm -f "$prop" "$acc"
493
+ }
494
+
495
+ sync_phrase() { # MERGED -> what --sync --write would do, for the check's note
496
+ local merged="$1" cur_paths merged_paths acc_paths added="" replaced="" p raw out
497
+ cur_paths="$(leaf_lines "$CONFIG" | cut -f1)"
498
+ merged_paths="$(printf '%s\n' "$merged" | leaf_lines /dev/stdin | cut -f1)"
499
+ acc_paths="$(accepted_table | cut -f1)"
500
+ added="$(comm -13 <(printf '%s\n' "$cur_paths" | sort -u) <(printf '%s\n' "$merged_paths" | sort -u) 2>/dev/null \
501
+ | cut -d"$US" -f1 | sort -u | grep -v '^$' | paste -sd, - | sed 's/,/, /g' || true)"
502
+ while IFS=$'\t' read -r p raw; do
503
+ [[ -n "$p" ]] || continue
504
+ grep -qxF "$p" <<< "$acc_paths" && continue
505
+ grep -qxF "$p" <<< "$merged_paths" || continue
506
+ replaced="${replaced:+$replaced, }${p//$US/.}"
507
+ done < <(leaf_lines "$CONFIG")
508
+ if [[ -z "$added" && -z "$replaced" ]]; then printf 'refresh the comments'; return; fi
509
+ out=""
510
+ [[ -n "$added" ]] && out="add $added"
511
+ [[ -n "$replaced" ]] && out="${out:+$out; }replace $replaced (not valid today)"
512
+ printf '%s and refresh the comments' "$out"
513
+ }