@codewizard-dt/bootstrap 2.17.0 → 2.19.0

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 (39) hide show
  1. package/lib/hooks/README.md +94 -3
  2. package/lib/hooks/absolute-path-guard.js +5 -0
  3. package/lib/hooks/claude-settings-guard.js +5 -0
  4. package/lib/hooks/env-content-read-guard.js +13 -3
  5. package/lib/hooks/env-file-guard.js +42 -0
  6. package/lib/hooks/interpreter-indirection-guard.js +7 -0
  7. package/lib/hooks/lib/command-parse.js +8 -0
  8. package/lib/hooks/lib/serena-languages.js +94 -1
  9. package/lib/hooks/lib/serena.js +228 -0
  10. package/lib/hooks/mv-absolute-path-block.js +84 -3
  11. package/lib/hooks/package-install-consent.js +11 -0
  12. package/lib/hooks/protected-write-guard.js +7 -0
  13. package/lib/hooks/serena-bash-grep-block.js +507 -44
  14. package/lib/hooks/serena-first-glob-guard.js +65 -20
  15. package/lib/hooks/serena-first-guard.js +98 -2
  16. package/lib/hooks/serena-first-read-guard.js +92 -2
  17. package/lib/hooks/serena-pre-delegation.js +123 -0
  18. package/lib/hooks/serena-write-guard.js +56 -3
  19. package/lib/scripts/README.md +306 -1
  20. package/lib/scripts/bootstrap-prefs.js +734 -0
  21. package/lib/scripts/install-global.sh +188 -18
  22. package/lib/scripts/install-mcps.sh +102 -14
  23. package/lib/scripts/lib.sh +581 -11
  24. package/lib/scripts/merge-gitignore.sh +201 -15
  25. package/lib/scripts/sync-wiki-scaffold.sh +33 -4
  26. package/lib/scripts/templates/CLAUDE-wiki.md +1 -1
  27. package/lib/scripts/templates/bootstrap-prefs-schema.json +175 -0
  28. package/lib/scripts/update-project.sh +42 -9
  29. package/lib/skills/README.md +2 -1
  30. package/lib/skills/bootstrap-config/SKILL.md +154 -0
  31. package/lib/skills/git-commit/SKILL.md +89 -6
  32. package/lib/skills/research/SKILL.md +34 -3
  33. package/lib/skills/simplify/SKILL.md +1 -1
  34. package/lib/skills/uat-auto/SKILL.md +9 -4
  35. package/lib/skills/uat-auto-plus/SKILL.md +6 -6
  36. package/lib/skills/uat-generate/SKILL.md +47 -7
  37. package/lib/skills/uat-walk/UAT-CORE.md +41 -6
  38. package/lib/skills/wiki-query/SKILL.md +48 -8
  39. package/package.json +1 -1
@@ -8,6 +8,20 @@
8
8
  # no associative arrays, no `${var,,}`. Functions return values by printing to
9
9
  # stdout; diagnostics go to stderr.
10
10
 
11
+ # ---------------------------------------------------------------------------
12
+ # BOOTSTRAP_PREFS_JS
13
+ #
14
+ # Absolute path to the preference-store helper (bootstrap-prefs.js), resolved
15
+ # from lib.sh's OWN location at source time.
16
+ #
17
+ # It deliberately does NOT use $SCRIPT_DIR: that variable belongs to the
18
+ # sourcing script, and the callers compute it two different ways —
19
+ # install-global.sh uses `dirname "$0"`, the others use
20
+ # `dirname "${BASH_SOURCE[0]}"`. Deriving the helper path from a caller's
21
+ # convention would break the moment a new caller picks the other one.
22
+ # ---------------------------------------------------------------------------
23
+ BOOTSTRAP_PREFS_JS="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/bootstrap-prefs.js"
24
+
11
25
  # ---------------------------------------------------------------------------
12
26
  # resolve_project_dir <path>
13
27
  #
@@ -164,6 +178,21 @@ run_project_sync() {
164
178
  echo ""
165
179
  }
166
180
 
181
+ # ---------------------------------------------------------------------------
182
+ # has_tty
183
+ #
184
+ # True when stdin is an interactive terminal, OR when BOOTSTRAP_ASSUME_TTY=1.
185
+ #
186
+ # The override is a TEST SEAM, not a prompt bypass: Node's spawnSync hands the
187
+ # child a pipe, so `[ -t 0 ]` is always false under the test harness and no
188
+ # test could otherwise reach the body of any prompt. It gates only the tty
189
+ # DETECTION — `read` still runs, so a test that supplies no stdin still takes
190
+ # the real EOF path.
191
+ # ---------------------------------------------------------------------------
192
+ has_tty() {
193
+ [ -t 0 ] || [ "${BOOTSTRAP_ASSUME_TTY:-}" = "1" ]
194
+ }
195
+
167
196
  # ---------------------------------------------------------------------------
168
197
  # prompt_yn <prompt>
169
198
  #
@@ -173,7 +202,7 @@ run_project_sync() {
173
202
  prompt_yn() {
174
203
  local prompt reply
175
204
  prompt="$1"
176
- if [ -t 0 ]; then
205
+ if has_tty; then
177
206
  read -r -p "$prompt" reply
178
207
  else
179
208
  echo " Non-interactive terminal: skipping prompt, answering no."
@@ -186,21 +215,562 @@ prompt_yn() {
186
215
  }
187
216
 
188
217
  # ---------------------------------------------------------------------------
189
- # prompt_scope <name>
218
+ # prompt_yn_sticky <key> <selector> <prompt>
219
+ #
220
+ # A yes/no question that is asked once and then remembered. Returns 0 for yes
221
+ # and 1 for no, exactly like prompt_yn, so call sites stay
222
+ # `if prompt_yn_sticky ...; then`. The answer is never printed to stdout.
223
+ #
224
+ # <selector> is either the literal string `--global` or an absolute project
225
+ # directory, and it is used for BOTH the read and the write. That is
226
+ # deliberate: bootstrap-prefs.js resolution is scope-constrained, so a
227
+ # `global`-scope key read with --project skips the project file anyway, and a
228
+ # `project`-scope key read with --project never consults the global one. The
229
+ # one case where the write layer would be the WRONG read layer is a
230
+ # `scope: either` key — it must be read with --project so both layers and the
231
+ # schema default are consulted — and no call site of this helper reads one. Do
232
+ # not "simplify" the selector away.
233
+ #
234
+ # THE LOAD-BEARING RULE: only an answer that was actually asked interactively
235
+ # is ever recorded. A non-interactive run auto-answers `no` and records
236
+ # NOTHING — the prefs_set below is unreachable from that branch by return, not
237
+ # by flag. One CI run must never bake a permanent `no` into a user's store: an
238
+ # unattended decline that persists is strictly worse than the re-prompting
239
+ # this mechanism exists to remove, because there is no prompt left to change
240
+ # your mind with.
190
241
  #
191
- # Asks whether to register <name> at user (default) or project scope.
192
- # Prints "user" or "project". Defaults to "user" in a non-interactive terminal.
242
+ # `ask` and `unset` are NOT the same state. `unset` is an unanswered question
243
+ # that the next answer settles; `ask` is a settled answer whose content is
244
+ # "keep asking, never persist". Collapsing the two would let the next reply
245
+ # silently overwrite a user's explicit `ask`.
246
+ #
247
+ # The explicit has_tty check here fires BEFORE the delegation to prompt_yn,
248
+ # which has a non-interactive branch of its own printing the same note. Because
249
+ # this one returns first, prompt_yn is never reached without a tty and the note
250
+ # is printed exactly once (verified empirically).
251
+ # ---------------------------------------------------------------------------
252
+ prompt_yn_sticky() {
253
+ local key selector prompt stored record status
254
+ key="$1"
255
+ selector="$2"
256
+ prompt="$3"
257
+ record=true
258
+
259
+ stored="$(prefs_get "$key" "$selector")"
260
+ case "$stored" in
261
+ true)
262
+ echo " $key: using remembered answer (yes) — change with /bootstrap-config"
263
+ return 0
264
+ ;;
265
+ false)
266
+ echo " $key: using remembered answer (no) — change with /bootstrap-config"
267
+ return 1
268
+ ;;
269
+ ask)
270
+ # A settled answer meaning "keep asking". Prompt every run, record nothing.
271
+ record=false
272
+ ;;
273
+ unset)
274
+ # Unanswered — prompt, and let this run's answer settle it.
275
+ : ;;
276
+ *)
277
+ echo " Warning: $key holds unrecognized value \"$stored\" — treating it as unset." >&2
278
+ ;;
279
+ esac
280
+
281
+ if ! has_tty; then
282
+ # Non-interactive: answer no and record NOTHING. This return is what makes
283
+ # the rule structural rather than a flag check — no path from here can
284
+ # reach the prefs_set below.
285
+ echo " Non-interactive terminal: skipping prompt, answering no."
286
+ return 1
287
+ fi
288
+
289
+ # Captured with if/else, not a bare call: prompt_yn returning 1 is a valid
290
+ # answer, and a bare call would abort a consumer under `set -euo pipefail`.
291
+ if prompt_yn "$prompt"; then
292
+ status=0
293
+ else
294
+ status=1
295
+ fi
296
+
297
+ if [ "$record" = true ]; then
298
+ if [ "$status" -eq 0 ]; then
299
+ prefs_set "$key" "$selector" true
300
+ else
301
+ prefs_set "$key" "$selector" false
302
+ fi
303
+ fi
304
+
305
+ return "$status"
306
+ }
307
+
308
+ # ---------------------------------------------------------------------------
309
+ # prompt_scope <name> [<pref-key> <selector>]
310
+ #
311
+ # Asks whether to register <name> at user (default) or project scope. Prints
312
+ # "user" or "project", and defaults to "user" in a non-interactive terminal.
313
+ #
314
+ # With NO extra arguments this is exactly the function it has always been: no
315
+ # preference lookup, no store write, not one call to bootstrap-prefs.js. That
316
+ # is load-bearing — bootstrap-serena.sh and every other caller that has not
317
+ # opted in must be unaffected, including on machines with no prefs helper at
318
+ # all.
319
+ #
320
+ # With <pref-key> AND <selector> it becomes sticky, running the same
321
+ # stored/ask/unset ladder as prompt_choice_sticky — shared via _sticky_lookup,
322
+ # so that ladder is written once and not a third time — with legal names
323
+ # `user project` and default `user`. Both arguments are required to opt in; a
324
+ # key with no selector has no layer to read or write, so it warns and stays
325
+ # non-sticky rather than guessing one.
326
+ #
327
+ # WHY THIS DOES NOT SIMPLY CALL prompt_choice_sticky. The two RESOLVE A REPLY
328
+ # DIFFERENTLY, and the difference is a published contract, not an accident.
329
+ # prompt_choice_sticky matches a digit index or an EXACT name; this function
330
+ # matches on FIRST LETTER — `[pP]*` is project and everything else (empty, EOF,
331
+ # `pineapple`, garbage) is user. That two-answer first-letter rule is what
332
+ # bootstrap-prefs-schema.json's mcp.context7Scope detail documents to users:
333
+ # "any reply that does not begin with p or P falls through to user".
334
+ # Delegating the read would silently turn a bare `p` — neither a digit nor the
335
+ # exact name `project` — into `user`, a regression this function's own callers
336
+ # and the schema both depend on not happening. So only the ladder is shared;
337
+ # the resolver stays here.
338
+ #
339
+ # STDOUT IS THE RETURN VALUE, as it always was: the resolved scope is the only
340
+ # thing that reaches stdout, and every notice goes to stderr.
341
+ #
342
+ # THE LOAD-BEARING RULE, identical to the other two sticky helpers: only an
343
+ # answer that was actually asked interactively is ever recorded. The
344
+ # non-interactive branch prints `user` and RETURNS before the prefs_set below,
345
+ # so the write is unreachable from it by return, not by flag.
193
346
  # ---------------------------------------------------------------------------
194
347
  prompt_scope() {
195
- local name reply
348
+ local name key selector record state reply resolved
196
349
  name="$1"
197
- if [ -t 0 ]; then
198
- read -r -p " Scope for $name — [u]ser (default) or [p]roject? " reply
199
- else
200
- reply="u"
350
+ key="${2:-}"
351
+ selector="${3:-}"
352
+ record=false
353
+
354
+ if [ -n "$key" ] && [ -n "$selector" ]; then
355
+ record=true
356
+ state="$(_sticky_lookup "$key" "$selector" user project)"
357
+ case "$state" in
358
+ hit:*)
359
+ # A legal remembered answer; its notice already went to stderr.
360
+ printf '%s\n' "${state#hit:}"
361
+ return 0
362
+ ;;
363
+ ask)
364
+ # A settled answer meaning "keep asking". Prompt every run, record nothing.
365
+ record=false
366
+ ;;
367
+ unset)
368
+ # Unanswered — prompt, and let this run's answer settle it.
369
+ : ;;
370
+ esac
371
+ elif [ -n "$key" ]; then
372
+ echo " Warning: prompt_scope given \"$key\" with no selector — answering normally and remembering nothing." >&2
201
373
  fi
374
+
375
+ if ! has_tty; then
376
+ # Silently default to user. This question has never printed a
377
+ # non-interactive note and adding one now would change installer output.
378
+ # Records NOTHING — this return is what makes that structural.
379
+ printf '%s\n' "user"
380
+ return 0
381
+ fi
382
+
383
+ # `|| reply=""` keeps read's non-zero EOF status from aborting a consumer
384
+ # running under `set -euo pipefail`. EOF leaves the reply empty, which the
385
+ # case below resolves to `user` — the same answer an empty line gives.
386
+ reply=""
387
+ read -r -p " Scope for $name — [u]ser (default) or [p]roject? " reply || reply=""
202
388
  case "$reply" in
203
- [pP]*) echo "project" ;;
204
- *) echo "user" ;;
389
+ [pP]*) resolved="project" ;;
390
+ *) resolved="user" ;;
205
391
  esac
392
+
393
+ if [ "$record" = true ]; then
394
+ prefs_set "$key" "$selector" "$resolved"
395
+ fi
396
+
397
+ printf '%s\n' "$resolved"
398
+ }
399
+
400
+ # ---------------------------------------------------------------------------
401
+ # prompt_choice_sticky <key> <selector> <default-name> <prompt> <name>...
402
+ #
403
+ # A multiple-choice question that is asked once and then remembered. Prints the
404
+ # resolved NAME and returns 0, so call sites read
405
+ # `choice="$(prompt_choice_sticky ...)"`. The trailing <name> arguments are the
406
+ # legal answers, listed in menu order.
407
+ #
408
+ # STDOUT IS THE RETURN VALUE. The resolved name is the only thing that ever
409
+ # reaches stdout; every notice, warning, and the non-interactive note go to
410
+ # stderr with `>&2`. A remembered-answer notice printed on stdout would be
411
+ # captured by the caller AS the answer. (`read -r -p` writes its own prompt to
412
+ # stderr, so the prompt text is already safe.)
413
+ #
414
+ # NAMES, NOT DIGITS, ARE WHAT GET STORED. mcp.playwrightConflict stores
415
+ # `shared | alongside | skip`; its menu prints [1]/[2]/[3] and users type
416
+ # digits, but a digit is an INPUT FORM only, never a stored value. A stored `2`
417
+ # would silently change meaning the day the menu is reordered — it would still
418
+ # resolve, and it would resolve to the wrong thing.
419
+ #
420
+ # <prompt> is handed to `read -r -p` verbatim, so a caller keeps its exact
421
+ # prompt text (including a one-line inline menu).
422
+ #
423
+ # <selector> is the literal string `--global` or an absolute project directory,
424
+ # and is used for BOTH the read and the write — see prompt_yn_sticky's banner
425
+ # for the full reasoning. Short version: bootstrap-prefs.js resolution is
426
+ # scope-constrained, so the layer a value is read from must not drift from the
427
+ # layer it was written to. Do not "simplify" the selector away.
428
+ #
429
+ # THE LOAD-BEARING RULE, identical to prompt_yn_sticky: only an answer that was
430
+ # actually asked interactively is ever recorded. A non-interactive run echoes
431
+ # <default-name> and records NOTHING — that branch RETURNS before the prefs_set
432
+ # below, so the write is unreachable from it by return, not by flag. One CI run
433
+ # must never bake a permanent choice into a user's store.
434
+ #
435
+ # `ask` and `unset` are NOT the same state. `unset` is an unanswered question
436
+ # that the next answer settles; `ask` is a settled answer whose content is
437
+ # "keep asking, never persist". Collapsing the two would let the next reply
438
+ # silently overwrite a user's explicit `ask`.
439
+ # ---------------------------------------------------------------------------
440
+ prompt_choice_sticky() {
441
+ local key selector default_name prompt state record reply resolved name i
442
+ key="$1"
443
+ selector="$2"
444
+ default_name="$3"
445
+ prompt="$4"
446
+ shift 4
447
+ # What remains in "$@" is the legal name list, in menu order — it is both the
448
+ # set of accepted typed answers and what the digits index into.
449
+ record=true
450
+
451
+ # The stored/ask/unset ladder lives in _sticky_lookup, shared with
452
+ # prompt_scope so it is written once — see its banner for the token grammar.
453
+ state="$(_sticky_lookup "$key" "$selector" "$@")"
454
+ case "$state" in
455
+ hit:*)
456
+ # A legal remembered answer; its notice already went to stderr.
457
+ printf '%s\n' "${state#hit:}"
458
+ return 0
459
+ ;;
460
+ ask)
461
+ # A settled answer meaning "keep asking". Prompt every run, record nothing.
462
+ record=false
463
+ ;;
464
+ unset)
465
+ # Unanswered — prompt, and let this run's answer settle it.
466
+ : ;;
467
+ esac
468
+
469
+ if ! has_tty; then
470
+ # Non-interactive: answer with the default and record NOTHING. This return
471
+ # is what makes the rule structural rather than a flag check — no path from
472
+ # here can reach the prefs_set below.
473
+ echo " Non-interactive terminal: skipping prompt, choosing $default_name." >&2
474
+ printf '%s\n' "$default_name"
475
+ return 0
476
+ fi
477
+
478
+ # `|| reply=""` keeps read's non-zero EOF status from aborting a consumer
479
+ # running under `set -euo pipefail`. EOF leaves the reply empty and therefore
480
+ # resolves to <default-name>, exactly like an empty line.
481
+ reply=""
482
+ read -r -p "$prompt" reply || reply=""
483
+
484
+ resolved="$default_name"
485
+ case "$reply" in
486
+ ''|*[!0-9]*)
487
+ # Empty or not all digits: an exact <name> match wins; anything else
488
+ # (including EOF's empty reply) is <default-name>.
489
+ for name in "$@"; do
490
+ if [ "$reply" = "$name" ]; then
491
+ resolved="$name"
492
+ break
493
+ fi
494
+ done
495
+ ;;
496
+ *)
497
+ # All digits: the Nth name, 1-based, in the order given. A digit outside
498
+ # 1..N falls through to <default-name> — a mistyped digit is a mis-hit,
499
+ # not a decision, and the default is the safe reading of one.
500
+ i=1
501
+ for name in "$@"; do
502
+ if [ "$reply" = "$i" ]; then
503
+ resolved="$name"
504
+ break
505
+ fi
506
+ i=$(( i + 1 ))
507
+ done
508
+ ;;
509
+ esac
510
+
511
+ if [ "$record" = true ]; then
512
+ prefs_set "$key" "$selector" "$resolved"
513
+ fi
514
+
515
+ printf '%s\n' "$resolved"
516
+ }
517
+
518
+ # ---------------------------------------------------------------------------
519
+ # _sticky_lookup <key> <selector> <name>...
520
+ #
521
+ # The stored/ask/unset ladder shared by prompt_choice_sticky and prompt_scope,
522
+ # written once. Reads <key> at <selector>'s layer and prints exactly ONE token
523
+ # telling the caller what to do next:
524
+ #
525
+ # hit:<name> a legal remembered answer — print it and return, do not prompt
526
+ # ask a settled "keep asking" — prompt, and record NOTHING
527
+ # unset unanswered — prompt, and let this run's answer settle it
528
+ #
529
+ # `ask` and `unset` are NOT the same state. `unset` is an unanswered question
530
+ # that the next answer settles; `ask` is a settled answer whose content is
531
+ # "keep asking, never persist". Collapsing the two would let the next reply
532
+ # silently overwrite a user's explicit `ask`.
533
+ #
534
+ # A stored value matching no legal name — the menu was reordered, or the schema
535
+ # changed under a stored answer — warns and degrades to `unset`. Re-asking is
536
+ # strictly better than handing the caller a name it has no branch for.
537
+ #
538
+ # EVERY notice and warning goes to STDERR, including the remembered-answer one.
539
+ # Both callers print their resolved answer to stdout, so a notice on stdout
540
+ # would be captured by THEIR callers as the answer.
541
+ #
542
+ # prompt_yn_sticky deliberately does not use this: its stored grammar is
543
+ # `true`/`false` rather than a name list, and its notice goes to stdout (it
544
+ # returns its answer as an exit status, so stdout is free there). Bending this
545
+ # helper to cover that shape would cost more than the ladder it saves.
546
+ # ---------------------------------------------------------------------------
547
+ _sticky_lookup() {
548
+ local key selector stored name
549
+ key="$1"
550
+ selector="$2"
551
+ shift 2
552
+
553
+ stored="$(prefs_get "$key" "$selector")"
554
+ case "$stored" in
555
+ ask)
556
+ printf '%s\n' "ask"
557
+ return 0
558
+ ;;
559
+ unset)
560
+ printf '%s\n' "unset"
561
+ return 0
562
+ ;;
563
+ esac
564
+
565
+ for name in "$@"; do
566
+ if [ "$stored" = "$name" ]; then
567
+ echo " $key: using remembered answer ($stored) — change with /bootstrap-config" >&2
568
+ printf '%s\n' "hit:$stored"
569
+ return 0
570
+ fi
571
+ done
572
+
573
+ echo " Warning: $key holds unrecognized value \"$stored\" — treating it as unset." >&2
574
+ printf '%s\n' "unset"
575
+ }
576
+
577
+ # ---------------------------------------------------------------------------
578
+ # _prefs_selector_args <selector>
579
+ #
580
+ # Maps the single selector argument the preference helpers take onto
581
+ # bootstrap-prefs.js's layer-selector flags: the literal string `--global`
582
+ # stays `--global`; anything else is treated as an absolute project directory
583
+ # and becomes `--project <dir>`.
584
+ #
585
+ # Both the read and the write go through this one function, so the layer a
586
+ # value is read from can never drift from the layer it was written to.
587
+ #
588
+ # Intended to be substituted UNQUOTED so it word-splits into two arguments —
589
+ # which also means it assumes a project path without whitespace, the same
590
+ # assumption detect_installed_mcps already makes of its output.
591
+ # ---------------------------------------------------------------------------
592
+ _prefs_selector_args() {
593
+ if [ "$1" = "--global" ]; then
594
+ printf '%s\n' "--global"
595
+ else
596
+ printf '%s %s\n' "--project" "$1"
597
+ fi
598
+ }
599
+
600
+ # ---------------------------------------------------------------------------
601
+ # prefs_get <key> <selector>
602
+ #
603
+ # Prints the stored value for <key> as resolved at <selector>'s layer, or the
604
+ # literal word `unset`. Never prints an empty line, and never creates the
605
+ # store file.
606
+ #
607
+ # Degrades to `unset` when the helper is missing or node is not on PATH: a
608
+ # partial install must not abort an installer run. Only stdout is captured —
609
+ # the helper's own stderr warnings pass straight through to the user.
610
+ # ---------------------------------------------------------------------------
611
+ prefs_get() {
612
+ local key selector out
613
+ key="$1"
614
+ selector="$2"
615
+ if [ ! -f "$BOOTSTRAP_PREFS_JS" ] || ! command -v node >/dev/null 2>&1; then
616
+ printf '%s\n' "unset"
617
+ return 0
618
+ fi
619
+ # Unquoted on purpose: word-split the selector into its flag(s).
620
+ out="$(node "$BOOTSTRAP_PREFS_JS" --get "$key" $(_prefs_selector_args "$selector"))" || out=""
621
+ if [ -z "$out" ]; then
622
+ printf '%s\n' "unset"
623
+ return 0
624
+ fi
625
+ printf '%s\n' "$out"
626
+ }
627
+
628
+ # ---------------------------------------------------------------------------
629
+ # prefs_set <key> <selector> <value>
630
+ #
631
+ # Records <value> for <key> at <selector>'s layer.
632
+ #
633
+ # bootstrap-prefs.js exits 1 only when the CALLER is wrong (a value outside
634
+ # the key's grammar, a missing layer selector) — a bug in the calling script,
635
+ # not a user error. Its stderr is left visible so the mistake shows up in the
636
+ # log, but the non-zero status is swallowed here so it cannot abort a consumer
637
+ # running under `set -euo pipefail`. A broken preference write must never cost
638
+ # the user their install.
639
+ #
640
+ # Its stdout (a `<layer>: <key> = <value>` confirmation) IS suppressed: the
641
+ # calling sticky-prompt helpers print their own, user-facing notice, so the
642
+ # helper's line would only duplicate it in a different vocabulary.
643
+ # ---------------------------------------------------------------------------
644
+ prefs_set() {
645
+ local key selector value
646
+ key="$1"
647
+ selector="$2"
648
+ value="$3"
649
+ if [ ! -f "$BOOTSTRAP_PREFS_JS" ] || ! command -v node >/dev/null 2>&1; then
650
+ return 0
651
+ fi
652
+ # Unquoted on purpose: word-split the selector into its flag(s).
653
+ node "$BOOTSTRAP_PREFS_JS" --set "$key" --value "$value" $(_prefs_selector_args "$selector") >/dev/null || {
654
+ echo " Warning: could not record preference $key=$value (see error above)." >&2
655
+ }
656
+ return 0
657
+ }
658
+
659
+ # ---------------------------------------------------------------------------
660
+ # prefs_stored_global <key>
661
+ #
662
+ # True when <key> has an answer actually STORED in ~/.claude/bootstrap-prefs.json,
663
+ # as opposed to merely RESOLVING to something. Prints nothing; the answer is the
664
+ # exit status.
665
+ #
666
+ # WHY THIS CANNOT BE prefs_get. `--get` is a resolution, and resolution falls
667
+ # through to the schema `default` after the files (bootstrap-prefs.js:376). Four
668
+ # of the five `consumer: skill` keys carry a non-null default — `auto`, `false`,
669
+ # `true`, `true` — so `prefs_get gitCommit.versionBump --global` prints `auto`
670
+ # whether the user chose `auto` or has never been asked in their life. A
671
+ # prefs_get-based "is this unanswered?" test therefore reports EVERY one of those
672
+ # keys as settled, and a sync pass built on it would silently never ask anything
673
+ # at all. That failure is invisible: the install looks clean, the store stays
674
+ # empty, and every skill quietly keeps its default forever.
675
+ #
676
+ # `--list`'s `[layer]` column is the only surface that separates the three
677
+ # outcomes — `[global]` (a stored answer), `[default]` (the schema's fallback),
678
+ # `[unset]` (nothing anywhere) — which is why the read goes through --list and
679
+ # matches the layer token rather than the value.
680
+ #
681
+ # READ-ONLY, and that is load-bearing: `--list` never creates a values file
682
+ # (pinned by test/bootstrap-prefs.test.js), so probing a key on a machine that
683
+ # has never answered one leaves the store non-existent rather than empty.
684
+ #
685
+ # Degrades to "not stored" when the helper is missing or node is not on PATH,
686
+ # matching prefs_get. The cost of that direction is a re-asked question; the
687
+ # other direction would be a question that can never be asked.
688
+ # ---------------------------------------------------------------------------
689
+ prefs_stored_global() {
690
+ local key
691
+ key="$1"
692
+ if [ ! -f "$BOOTSTRAP_PREFS_JS" ] || ! command -v node >/dev/null 2>&1; then
693
+ return 1
694
+ fi
695
+ # `--list` with no --project consults the global layer only, which is exactly
696
+ # the layer this pass writes to. awk compares the key and the layer token as
697
+ # whole fields — a substring match would let `[default]` satisfy a test for
698
+ # `[global]`'s absence in the wrong direction.
699
+ node "$BOOTSTRAP_PREFS_JS" --list --global 2>/dev/null |
700
+ awk -v key="$key" '$1 == key && $2 == "=" && $NF == "[global]" { found = 1 } END { exit !found }'
701
+ }
702
+
703
+ # ---------------------------------------------------------------------------
704
+ # prompt_letter_choice <default-name> <prompt> <name>...
705
+ #
706
+ # A multiple-choice question resolved on the FIRST LETTER of the reply, printing
707
+ # the resolved NAME to stdout. Non-sticky by construction: it never reads the
708
+ # preference store and never writes to it, so the caller owns both the "should I
709
+ # even ask?" decision and the recording of the answer.
710
+ #
711
+ # STDOUT IS THE RETURN VALUE. The resolved name is the only thing that reaches
712
+ # stdout; the non-interactive note goes to stderr, and `read -r -p` writes its
713
+ # own prompt to stderr already.
714
+ #
715
+ # WHY THIS IS NOT prompt_choice_sticky, AND WHY IT DOES NOT CALL IT.
716
+ #
717
+ # 1. THE STORE. prompt_choice_sticky consults the store before prompting and
718
+ # returns a remembered answer without asking. For a key with a non-null
719
+ # schema default that read can never come back `unset` (see
720
+ # prefs_stored_global above), so it would resolve to the default and return
721
+ # WITHOUT PROMPTING — every single time, for four of the five keys the sync
722
+ # pass exists to settle. It is the right helper for a `global`/`project`
723
+ # key with a null default and the wrong one here.
724
+ # 2. THE RESOLVER. prompt_choice_sticky matches a digit index or an EXACT
725
+ # name; this matches the first letter, the same rule prompt_scope uses
726
+ # ([pP]* is project, everything else is user). First letters are what the
727
+ # sync prompts print — `[y]es / [n]o / [a]sk` — and typing `y` must not
728
+ # fall through to the default the way an inexact name does there.
729
+ #
730
+ # So prompt_choice_sticky is left untouched: its behaviour is pinned by a large
731
+ # body of tests, and bending it to cover a defaulted key would put the store read
732
+ # behind a flag in a helper whose whole contract is that the store read happens.
733
+ #
734
+ # THE DEFAULT IS DELIBERATELY A NON-ANSWER AT EVERY CALL SITE IN THE SYNC PASS.
735
+ # An empty reply (bare Enter, or EOF) resolves to <default-name>, and the pass
736
+ # passes `skip` there rather than a real value: a keystroke nobody meant must not
737
+ # settle a question permanently. Recording only what was actually typed is the
738
+ # same rule the sticky helpers enforce for the non-interactive case.
739
+ #
740
+ # Case-insensitive on the reply, case-sensitive on the names, which are lower
741
+ # case throughout. bash 3.2 has no ${var,,}, hence the tr.
742
+ # ---------------------------------------------------------------------------
743
+ prompt_letter_choice() {
744
+ local default_name prompt reply first name resolved
745
+ default_name="$1"
746
+ prompt="$2"
747
+ shift 2
748
+
749
+ if ! has_tty; then
750
+ # Records nothing and asks nothing — the caller gets the default and, in the
751
+ # sync pass, that default is `skip`, so an unattended run leaves the key
752
+ # unanswered instead of baking in a value nobody chose.
753
+ echo " Non-interactive terminal: skipping prompt, choosing $default_name." >&2
754
+ printf '%s\n' "$default_name"
755
+ return 0
756
+ fi
757
+
758
+ # `|| reply=""` keeps read's non-zero EOF status from aborting a consumer
759
+ # running under `set -euo pipefail`. EOF leaves the reply empty, which resolves
760
+ # to <default-name> exactly like an empty line.
761
+ reply=""
762
+ read -r -p "$prompt" reply || reply=""
763
+
764
+ first="$(printf '%s' "$reply" | cut -c1 | tr '[:upper:]' '[:lower:]')"
765
+ resolved="$default_name"
766
+ if [ -n "$first" ]; then
767
+ for name in "$@"; do
768
+ if [ "$first" = "$(printf '%s' "$name" | cut -c1)" ]; then
769
+ resolved="$name"
770
+ break
771
+ fi
772
+ done
773
+ fi
774
+
775
+ printf '%s\n' "$resolved"
206
776
  }