@sassywares/skills 0.17.0 → 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.
Files changed (31) hide show
  1. package/package.json +1 -1
  2. package/skills/sassy-plan/SKILL.md +1 -1
  3. package/skills/sassy-plan/references/interview.md +1 -1
  4. package/skills/sassy-plan/references/plan-format.md +1 -1
  5. package/skills/sassy-plan/references/revision-format.md +10 -1
  6. package/skills/sassy-plan/references/sketches.md +1 -1
  7. package/skills/sassy-setup/SKILL.md +1 -1
  8. package/skills/sassy-setup/references/anchors.md +1 -1
  9. package/skills/sassy-setup/references/branching.md +1 -1
  10. package/skills/sassy-setup/references/check.md +1 -1
  11. package/skills/sassy-setup/references/config.md +1 -1
  12. package/skills/sassy-setup/references/dogfood.md +1 -1
  13. package/skills/sassy-setup/references/harness.md +1 -1
  14. package/skills/sassy-setup/references/init.md +1 -1
  15. package/skills/sassy-setup/references/plan-resolution.md +1 -1
  16. package/skills/sassy-setup/references/scaffold-header.md +1 -1
  17. package/skills/sassy-setup/references/workspace.md +1 -1
  18. package/skills/sassy-setup/references/worktree.md +1 -1
  19. package/skills/sassy-setup/scripts/detect.sh +49 -1982
  20. package/skills/sassy-setup/scripts/hash-skill.mjs +1 -1
  21. package/skills/sassy-setup/scripts/lib/agents-md.sh +502 -0
  22. package/skills/sassy-setup/scripts/lib/branch.sh +200 -0
  23. package/skills/sassy-setup/scripts/lib/check.sh +673 -0
  24. package/skills/sassy-setup/scripts/lib/common.sh +128 -0
  25. package/skills/sassy-setup/scripts/lib/proposal.sh +221 -0
  26. package/skills/sassy-setup/scripts/lib/sync.sh +43 -0
  27. package/skills/sassy-setup/scripts/lib/worktree.sh +57 -0
  28. package/skills/sassy-setup/scripts/lib/yaml.sh +199 -0
  29. package/skills/sassy-setup/scripts/lockcheck.mjs +1 -1
  30. package/skills/sassy-setup/scripts/worktree-check.mjs +1 -1
  31. package/skills/sassy-work/SKILL.md +8 -2
@@ -1,4 +1,4 @@
1
- // generated_by: sassy-setup@0.17.0
1
+ // generated_by: sassy-setup@0.17.2
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,502 @@
1
+ # generated_by: sassy-setup@0.17.2
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)
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
+ worktrees.dir)
253
+ [[ -n "$v" ]] ;;
254
+ worktrees.create|worktrees.remove)
255
+ ! wt_script_reason "$v" >/dev/null ;;
256
+ hooks.*)
257
+ kind="${key#hooks.}"; kind="${kind%%.*}"
258
+ case "$kind" in pr|push|goal) ;; *) return 1 ;; esac
259
+ rest="${key#hooks.}"; rest="${rest#*.}"
260
+ point="${rest##*.}"
261
+ pat="${rest%.*}"
262
+ case " $(hook_points "$kind") " in *" $point "*) ;; *) return 1 ;; esac
263
+ case "$pat" in
264
+ *"*") [[ "$pat" == "*" || ( -n "${pat%\*}" && "${pat%\*}" != *"*" ) ]] || return 1 ;;
265
+ esac
266
+ while IFS= read -r entry; do
267
+ [[ -n "$entry" ]] || continue
268
+ name="${entry%% *}"
269
+ is_suite_skill "$name" && return 1
270
+ hook_skill_path "$name" >/dev/null 2>&1 || return 1
271
+ done < <(list_items "$v")
272
+ return 0 ;;
273
+ *)
274
+ return 1 ;;
275
+ esac
276
+ }
277
+
278
+ # One parser, two readers: the flat path stack is the same shape yaml_flat
279
+ # builds, so a line can be matched to the current file and to the proposal.
280
+ LEAF_AWK='function trim(s) { sub(/^[ \t]+/, "", s); sub(/[ \t]+$/, "", s); return s }
281
+ function ind(s, m) { m = match(s, /^ */); return m ? RLENGTH : 0 }
282
+ {
283
+ raw = $0
284
+ body0 = trim(raw)
285
+ if (body0 == "" || substr(body0, 1, 1) == "#") next
286
+ line = raw; sub(/#.*$/, "", line)
287
+ d = ind(line); body = trim(line)
288
+ if (body ~ /:$/) {
289
+ k = body; sub(/:$/, "", k); gsub(/"/, "", k)
290
+ while (sp > 0 && sd[sp] >= d) sp--
291
+ sd[++sp] = d; sk[sp] = k
292
+ next
293
+ }
294
+ k = body; sub(/:.*/, "", k); gsub(/"/, "", k)
295
+ while (sp > 0 && sd[sp] >= d) sp--
296
+ p = ""
297
+ for (i = 1; i <= sp; i++) p = p (p == "" ? "" : us) sk[i]
298
+ print p (p == "" ? "" : us) k "\t" raw
299
+ }'
300
+
301
+ # The merge. Records are `kind <TAB> depth <TAB> path <TAB> raw`; a commented
302
+ # example is a record too, with the depth its `#` markers would have if removed
303
+ # (the hash characters themselves add no depth, the spaces after them do), so
304
+ # `# push:` under `hooks:` is known to be the posited `hooks.push` block. When the
305
+ # file declares a valid leaf under such an example, the example is replaced by
306
+ # the file's own active block, verbatim — the one case where a commented example
307
+ # in the proposal is not an example for this project.
308
+ MERGE_AWK='function trim(s) { sub(/^[ \t]+/, "", s); sub(/[ \t]+$/, "", s); return s }
309
+ function ind(s, m) { m = match(s, /^ */); return m ? RLENGTH : 0 }
310
+ function joinall( p, i) { p = ""; for (i = 1; i <= sp; i++) p = p (p == "" ? "" : us) sk[i]; return p }
311
+ function dopop(d) { while (sp > 0 && sd[sp] >= d) sp-- }
312
+ function outf(r, p) { o++; oraw[o] = r; okind[o] = ""; opath[o] = p }
313
+ function outfk(r, d, p) { o++; oraw[o] = r; okind[o] = "key"; odepth[o] = d; opath[o] = p }
314
+ function outfl(r, d, p) { o++; oraw[o] = r; okind[o] = "leaf"; odepth[o] = d; opath[o] = p }
315
+ function under_accepted(P, a) {
316
+ for (a = 1; a <= na; a++) if (index(aorder[a], P us) == 1) return 1
317
+ return 0
318
+ }
319
+ function emit_acc(P, d) { o++; oraw[o] = acc[P]; okind[o] = "leaf"; odepth[o] = d; opath[o] = P; emitted[P] = 1 }
320
+ function emit_block(P, i, j, e, a) {
321
+ b0 = 0
322
+ for (i = 1; i <= cn; i++) if (ckind[i] == "key" && cpath[i] == P) { b0 = i; break }
323
+ if (b0 == 0) { warn[++nw] = P; return }
324
+ e = b0
325
+ for (j = b0 + 1; j <= cn; j++) {
326
+ if (ckind[j] == "blank") continue
327
+ if (cdepth[j] > cdepth[b0]) { e = j; continue }
328
+ break
329
+ }
330
+ for (j = b0; j <= e; j++) {
331
+ if (ckind[j] == "key") outfk(crawl[j], cdepth[j], cpath[j])
332
+ else if (ckind[j] == "leaf") outfl(crawl[j], cdepth[j], cpath[j])
333
+ else outf(crawl[j])
334
+ }
335
+ for (a = 1; a <= na; a++) if (index(aorder[a], P us) == 1) emitted[aorder[a]] = 1
336
+ emitted[P] = 1
337
+ }
338
+ BEGIN { cn = 0; pn = 0; na = 0; sp = 0; o = 0; nw = 0 }
339
+ FILENAME == accf {
340
+ if ($2 != "") { acc[$1] = $2; aorder[++na] = $1 }
341
+ next
342
+ }
343
+ FILENAME == curf {
344
+ raw = $0
345
+ body0 = trim(raw)
346
+ if (body0 == "") { ckind[++cn] = "blank"; crawl[cn] = raw; next }
347
+ if (substr(body0, 1, 1) == "#") { ckind[++cn] = "comment"; crawl[cn] = raw; next }
348
+ line = raw; sub(/#.*$/, "", line)
349
+ d = ind(line); body = trim(line)
350
+ if (body ~ /:$/) {
351
+ k = body; sub(/:$/, "", k); gsub(/"/, "", k)
352
+ dopop(d); sd[++sp] = d; sk[sp] = k
353
+ ckind[++cn] = "key"; cdepth[cn] = d; cpath[cn] = joinall(); crawl[cn] = raw
354
+ } else {
355
+ k = body; sub(/:.*/, "", k); gsub(/"/, "", k)
356
+ dopop(d); p = joinall()
357
+ ckind[++cn] = "leaf"; cdepth[cn] = d; cpath[cn] = p (p == "" ? "" : us) k; crawl[cn] = raw
358
+ }
359
+ next
360
+ }
361
+ FILENAME == propf {
362
+ raw = $0
363
+ body0 = trim(raw)
364
+ if (body0 == "") { pkind[++pn] = "blank"; pdepth[pn] = 0; ppath[pn] = ""; praw[pn] = raw; next }
365
+ if (substr(body0, 1, 1) == "#") {
366
+ rest = body0
367
+ while (substr(rest, 1, 1) == "#") rest = substr(rest, 2)
368
+ m = match(rest, /^ */); spc = m ? RLENGTH : 0
369
+ rest = substr(rest, spc + 1)
370
+ # A commented example sits at its `#` column, and its content starts two
371
+ # columns later ("# "); the spaces after the marker encode the depth of a
372
+ # child, so subtracting the marker leaves the line the example stands for.
373
+ ed = ind(raw) + (spc > 0 ? spc - 1 : 0)
374
+ if (rest ~ /^[^ \t:]+:/) {
375
+ k = rest; sub(/:.*/, "", k); gsub(/"/, "", k)
376
+ v = rest; sub(/^[^:]*:/, "", v); sub(/#.*/, "", v); v = trim(v)
377
+ dopop(ed)
378
+ if (v == "") {
379
+ sd[++sp] = ed; sk[sp] = k
380
+ pkind[++pn] = "example_key"; pdepth[pn] = ed; ppath[pn] = joinall(); praw[pn] = raw
381
+ } else {
382
+ p = joinall()
383
+ pkind[++pn] = "example_leaf"; pdepth[pn] = ed
384
+ ppath[pn] = p (p == "" ? "" : us) k; praw[pn] = raw
385
+ }
386
+ } else {
387
+ pkind[++pn] = "comment"; pdepth[pn] = 0; ppath[pn] = ""; praw[pn] = raw
388
+ }
389
+ next
390
+ }
391
+ line = raw; sub(/#.*$/, "", line)
392
+ d = ind(line); body = trim(line)
393
+ if (body ~ /:$/) {
394
+ k = body; sub(/:$/, "", k); gsub(/"/, "", k)
395
+ dopop(d); sd[++sp] = d; sk[sp] = k
396
+ pkind[++pn] = "key"; pdepth[pn] = d; ppath[pn] = joinall(); praw[pn] = raw
397
+ } else {
398
+ k = body; sub(/:.*/, "", k); gsub(/"/, "", k)
399
+ dopop(d); p = joinall()
400
+ pkind[++pn] = "leaf"; pdepth[pn] = d
401
+ ppath[pn] = p (p == "" ? "" : us) k; praw[pn] = raw
402
+ }
403
+ next
404
+ }
405
+ END {
406
+ i = 1
407
+ while (i <= pn) {
408
+ if (pkind[i] == "blank" || pkind[i] == "comment") { outf(praw[i]); i++; continue }
409
+ if (pkind[i] == "example_key") {
410
+ j = i + 1
411
+ while (j <= pn && (pkind[j] == "example_key" || pkind[j] == "example_leaf") && pdepth[j] > pdepth[i]) j++
412
+ if (under_accepted(ppath[i])) {
413
+ emit_block(ppath[i])
414
+ } else {
415
+ for (m = i; m < j; m++) outf(praw[m])
416
+ }
417
+ i = j; continue
418
+ }
419
+ if (pkind[i] == "example_leaf") {
420
+ if (ppath[i] in acc) emit_acc(ppath[i], pdepth[i]); else outf(praw[i])
421
+ i++; continue
422
+ }
423
+ if (pkind[i] == "leaf") {
424
+ if (ppath[i] in acc) emit_acc(ppath[i], pdepth[i]); else outfl(praw[i], pdepth[i], ppath[i])
425
+ i++; continue
426
+ }
427
+ outfk(praw[i], pdepth[i], ppath[i])
428
+ i++
429
+ }
430
+ # An accepted leaf the proposal has no slot for — an extra base_map rung, a
431
+ # hand-written head — is inserted under its own block, after the last line
432
+ # of that block. Dropping it would be the one thing the sync must never do:
433
+ # change a value the check accepts.
434
+ for (a = 1; a <= na; a++) {
435
+ P = aorder[a]
436
+ if (P in emitted) continue
437
+ par = P; sub(us "[^" us "]*$", "", par)
438
+ if (par == P) { warn[++nw] = P; continue }
439
+ b0 = 0; bd = -1
440
+ for (m = 1; m <= o; m++) if (okind[m] == "key" && opath[m] == par) { b0 = m; bd = odepth[m] }
441
+ if (b0 == 0) { warn[++nw] = P; continue }
442
+ last = b0
443
+ for (m = b0 + 1; m <= o; m++) {
444
+ if (okind[m] == "blank") continue
445
+ if (odepth[m] <= bd) break
446
+ last = m
447
+ }
448
+ insn[last]++
449
+ insraw[last, insn[last]] = acc[P]
450
+ }
451
+ for (m = 1; m <= o; m++) {
452
+ print oraw[m]
453
+ for (q = 1; q <= insn[m]; q++) print insraw[m, q]
454
+ }
455
+ for (m = 1; m <= nw; m++) print "sync: no slot for " warn[m] > "/dev/stderr"
456
+ }'
457
+
458
+ leaf_lines() { # FILE -> `path<TAB>raw line` for every active leaf
459
+ awk -v us="$US" "$LEAF_AWK" "$1"
460
+ }
461
+
462
+ accepted_table() { # -> `path<TAB>raw line` for every leaf --check accepts today
463
+ local path raw value
464
+ leaf_lines "$CONFIG" | while IFS=$'\t' read -r path raw; do
465
+ [[ -n "$path" ]] || continue
466
+ value="$(printf '%s' "$raw" \
467
+ | sed -E 's/^[[:space:]]*[^:]*:[[:space:]]*//; s/[[:space:]]+#.*$//; s/[[:space:]]+$//')"
468
+ value="${value#\"}"; value="${value%\"}"
469
+ if leaf_ok "$path" "$value"; then printf '%s\t%s\n' "$path" "$raw"; fi
470
+ done
471
+ }
472
+
473
+ merged_config() { # the current file merged onto the proposal, on stdout
474
+ local prop acc
475
+ prop="$(mktemp "${TMPDIR:-/tmp}/sassy-sync-proposal.XXXXXX")"
476
+ acc="$(mktemp "${TMPDIR:-/tmp}/sassy-sync-accepted.XXXXXX")"
477
+ emit_proposal > "$prop"
478
+ accepted_table > "$acc"
479
+ awk -F'\t' -v us="$US" -v accf="$acc" -v curf="$CONFIG" -v propf="$prop" \
480
+ "$MERGE_AWK" "$acc" "$CONFIG" "$prop"
481
+ rm -f "$prop" "$acc"
482
+ }
483
+
484
+ sync_phrase() { # MERGED -> what --sync --write would do, for the check's note
485
+ local merged="$1" cur_paths merged_paths acc_paths added="" replaced="" p raw out
486
+ cur_paths="$(leaf_lines "$CONFIG" | cut -f1)"
487
+ merged_paths="$(printf '%s\n' "$merged" | leaf_lines /dev/stdin | cut -f1)"
488
+ acc_paths="$(accepted_table | cut -f1)"
489
+ added="$(comm -13 <(printf '%s\n' "$cur_paths" | sort -u) <(printf '%s\n' "$merged_paths" | sort -u) 2>/dev/null \
490
+ | cut -d"$US" -f1 | sort -u | grep -v '^$' | paste -sd, - | sed 's/,/, /g' || true)"
491
+ while IFS=$'\t' read -r p raw; do
492
+ [[ -n "$p" ]] || continue
493
+ grep -qxF "$p" <<< "$acc_paths" && continue
494
+ grep -qxF "$p" <<< "$merged_paths" || continue
495
+ replaced="${replaced:+$replaced, }${p//$US/.}"
496
+ done < <(leaf_lines "$CONFIG")
497
+ if [[ -z "$added" && -z "$replaced" ]]; then printf 'refresh the comments'; return; fi
498
+ out=""
499
+ [[ -n "$added" ]] && out="add $added"
500
+ [[ -n "$replaced" ]] && out="${out:+$out; }replace $replaced (not valid today)"
501
+ printf '%s and refresh the comments' "$out"
502
+ }