@thebassclef/lite 1.9.9 → 1.9.10

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.
@@ -44,8 +44,46 @@ probe_gh_auth() {
44
44
  }
45
45
 
46
46
  probe_mcp_playwright() {
47
- # V1 heuristic — env var contract. V2 may read tool inventory.
47
+ # Fast path — explicit opt-in via env var. Adopters who want to force the
48
+ # probe TRUE set CLAUDE_MCP_PLAYWRIGHT=1 in their shell init. Documented at
49
+ # docs/use-cases/UC-lib-capability-probe-mcp-fallback.md.
48
50
  [ -n "${CLAUDE_MCP_PLAYWRIGHT:-}" ] && return 0
51
+
52
+ # Fallback — look for a Playwright MCP server in known MCP config files.
53
+ # jq is required for parsing. When absent, the probe fails soft to 1.
54
+ if ! command -v jq >/dev/null 2>&1; then
55
+ echo "probe_mcp_playwright: jq not found; env-var fast path only" >&2
56
+ return 1
57
+ fi
58
+
59
+ local project_dir="${CLAUDE_PROJECT_DIR:-.}"
60
+ local home_dir="${HOME:-}"
61
+
62
+ # Check paths in precedence order. First hit wins.
63
+ _probe_mcp_json_has_playwright "${project_dir}/.mcp.json" && return 0
64
+ _probe_mcp_json_has_playwright "${project_dir}/.claude/settings.json" && return 0
65
+ if [ -n "$home_dir" ]; then
66
+ _probe_mcp_json_has_playwright "${home_dir}/.claude/settings.json" && return 0
67
+ _probe_mcp_json_has_playwright "${home_dir}/.claude.json" && return 0
68
+ fi
69
+ return 1
70
+ }
71
+
72
+ # _probe_mcp_json_has_playwright <path>
73
+ # Returns 0 when the JSON file names a Playwright MCP server. Checks two
74
+ # shapes:
75
+ # 1. Top-level "playwright" key (used by `.mcp.json` on some adopters)
76
+ # 2. Nested "mcpServers.playwright" (used by `settings.json` + `~/.claude.json`)
77
+ # Fail-soft on missing file + malformed JSON — returns 1 silently.
78
+ # Per bassclef-upstream Finding #6 (#2036): `local path` is a zsh-magic variable
79
+ # name — ties to $PATH when sourced from zsh. Renamed to `local p`.
80
+ _probe_mcp_json_has_playwright() {
81
+ local p="$1"
82
+ [ -f "$p" ] || return 1
83
+ # Shape 1 — top-level playwright
84
+ jq -e '.playwright // empty' "$p" >/dev/null 2>&1 && return 0
85
+ # Shape 2 — nested mcpServers.playwright
86
+ jq -e '.mcpServers.playwright // empty' "$p" >/dev/null 2>&1 && return 0
49
87
  return 1
50
88
  }
51
89
 
@@ -178,10 +178,14 @@ composer_preflight_check() {
178
178
  echo "check_5_recommendation="
179
179
  pass=$((pass + 1))
180
180
  elif [ -f "$repo_skill" ] || [ -L "$repo_skill" ]; then
181
- echo "check_5_status=warn"
182
- echo "check_5_message=Skill at repo-level only ($repo_skill); user-level symlink missing — harness may not pick up (bassclef#271)"
183
- echo "check_5_recommendation=Run bassclef-sync.sh to create user-level symlink OR restart Claude session"
184
- warn=$((warn + 1))
181
+ # Per cli#321 — Claude Code's harness loads project .claude/skills/
182
+ # directly; user-level symlinks are not required. Repo-level IS a
183
+ # valid discovery path. Previously flagged as WARN, trained adopters
184
+ # to ignore warnings. Now PASS with informational message.
185
+ echo "check_5_status=pass"
186
+ echo "check_5_message=Skill resolves at repo-level: $repo_skill (harness loads project .claude/skills directly)"
187
+ echo "check_5_recommendation="
188
+ pass=$((pass + 1))
185
189
  elif [ -n "${BASSCLEF_DIR:-}" ] && [ -f "$bassclef_skill" ]; then
186
190
  echo "check_5_status=warn"
187
191
  echo "check_5_message=Skill at bassclef only; consumer-side links missing — substrate sync incomplete"
@@ -0,0 +1,369 @@
1
+ #!/bin/bash
2
+ # tier: lite
3
+ # Install-written-paths manifest + accessor lib.
4
+ #
5
+ # Records paths written by bassclef install writers so discipline hooks
6
+ # can skip them. Per bassclef-upstream#2036 Finding #8 structural cure
7
+ # (Kunal Slot 1). See docs/decompositions/2026-10-03-install-written-paths-manifest.md
8
+ # for design + patterns applied (Repository + Deep Module + Null Object + Guard Clause).
9
+ #
10
+ # @pattern patterns/code/fowler/repository.md
11
+ # @pattern patterns/code/ousterhout/deep-module.md
12
+ #
13
+ # Public API (5 verbs):
14
+ # install_written_paths_register <path> <writer>
15
+ # is_install_written_path <path>
16
+ # install_written_paths_rename <old-path> <new-path>
17
+ # install_written_paths_rebuild
18
+ # install_written_paths_verify
19
+ #
20
+ # Override: SKIP_INSTALL_WRITTEN_PATHS=1 — is_install_written_path returns 1 (false)
21
+ # so hooks fire discipline normally (don't skip).
22
+ #
23
+ # Variable naming: internal locals use `tgt` (target path), `arg_*` prefix,
24
+ # or `_iwp_*` namespace to avoid shadowing shell builtins. Reserved names
25
+ # like `PATH`, `CDPATH`, `FPATH`, `MANPATH` are not used as locals per
26
+ # shell-portability discipline (zsh treats lowercase `path` as array-tied
27
+ # to PATH; renaming avoids the kill).
28
+
29
+ set -u
30
+
31
+ # ---- Constants ----
32
+
33
+ _IWP_SCHEMA_VERSION="1.0.0"
34
+
35
+ _IWP_WRITERS="bassclef-hook-connect bassclef-sync-template bassclef-sync-dispatcher schedule-auto-save bassclef-init-npm"
36
+
37
+ # ---- Internal helpers ----
38
+
39
+ _iwp_manifest_path() {
40
+ local root="${CLAUDE_PROJECT_DIR:-$(pwd)}"
41
+ echo "$root/state/install-written-paths.json"
42
+ }
43
+
44
+ _iwp_log() {
45
+ if command -v trace_log >/dev/null 2>&1; then
46
+ trace_log "install-written-paths" "$*"
47
+ fi
48
+ }
49
+
50
+ _iwp_sha256() {
51
+ local file="$1"
52
+ if [ ! -f "$file" ]; then
53
+ echo ""
54
+ return 1
55
+ fi
56
+ if command -v sha256sum >/dev/null 2>&1; then
57
+ sha256sum "$file" 2>/dev/null | awk '{print "sha256:" $1}'
58
+ elif command -v shasum >/dev/null 2>&1; then
59
+ shasum -a 256 "$file" 2>/dev/null | awk '{print "sha256:" $1}'
60
+ else
61
+ echo ""
62
+ return 1
63
+ fi
64
+ }
65
+
66
+ _iwp_valid_writer() {
67
+ local w="$1"
68
+ for valid in $_IWP_WRITERS; do
69
+ [ "$w" = "$valid" ] && return 0
70
+ done
71
+ return 1
72
+ }
73
+
74
+ _iwp_validate_path() {
75
+ local p="$1"
76
+ # Reject path traversal (any component equal to .. OR path exactly ..)
77
+ case "$p" in
78
+ *../*|..) return 1 ;;
79
+ esac
80
+ # Reject absolute paths outside $CLAUDE_PROJECT_DIR
81
+ case "$p" in
82
+ /*)
83
+ local root="${CLAUDE_PROJECT_DIR:-$(pwd)}"
84
+ case "$p" in
85
+ "$root"/*) ;;
86
+ *) return 1 ;;
87
+ esac
88
+ ;;
89
+ esac
90
+ return 0
91
+ }
92
+
93
+ _iwp_ensure_manifest() {
94
+ local mpath
95
+ mpath=$(_iwp_manifest_path)
96
+ if [ ! -f "$mpath" ]; then
97
+ mkdir -p "$(dirname "$mpath")"
98
+ cat > "$mpath" <<EOF
99
+ {
100
+ "schema_version": "$_IWP_SCHEMA_VERSION",
101
+ "entries": []
102
+ }
103
+ EOF
104
+ fi
105
+ }
106
+
107
+ _iwp_manifest_valid() {
108
+ local mpath
109
+ mpath=$(_iwp_manifest_path)
110
+ [ -f "$mpath" ] || return 1
111
+ jq -e . "$mpath" >/dev/null 2>&1 || return 1
112
+ return 0
113
+ }
114
+
115
+ # ---- Public API ----
116
+
117
+ # install_written_paths_register <path> <writer>
118
+ # Records a path as install-written. Idempotent on path.
119
+ # Returns 0 on success, 1 on validation failure.
120
+ install_written_paths_register() {
121
+ local tgt="${1:-}"
122
+ local writer="${2:-}"
123
+
124
+ if [ -z "$tgt" ] || [ -z "$writer" ]; then
125
+ echo "install-written-paths: register requires <path> <writer>" >&2
126
+ return 1
127
+ fi
128
+
129
+ if ! _iwp_validate_path "$tgt"; then
130
+ echo "install-written-paths: path '$tgt' rejected (traversal or outside project)" >&2
131
+ _iwp_log "register-rejected path=$tgt reason=traversal"
132
+ return 1
133
+ fi
134
+
135
+ if ! _iwp_valid_writer "$writer"; then
136
+ echo "install-written-paths: writer '$writer' not in allowed enum" >&2
137
+ _iwp_log "register-rejected path=$tgt writer=$writer reason=unknown-writer"
138
+ return 1
139
+ fi
140
+
141
+ if ! command -v jq >/dev/null 2>&1; then
142
+ echo "install-written-paths: jq not available; cannot register" >&2
143
+ return 1
144
+ fi
145
+
146
+ _iwp_ensure_manifest
147
+ local mpath
148
+ mpath=$(_iwp_manifest_path)
149
+
150
+ local root="${CLAUDE_PROJECT_DIR:-$(pwd)}"
151
+ local full_tgt
152
+ case "$tgt" in
153
+ /*) full_tgt="$tgt" ;;
154
+ *) full_tgt="$root/$tgt" ;;
155
+ esac
156
+
157
+ local hash
158
+ hash=$(_iwp_sha256 "$full_tgt")
159
+ if [ -z "$hash" ]; then
160
+ echo "install-written-paths: unable to compute hash for '$full_tgt'" >&2
161
+ return 1
162
+ fi
163
+
164
+ local now
165
+ now=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
166
+
167
+ # Use flock to serialize writes; fall back to no-lock on BSD without flock
168
+ local tmp
169
+ tmp=$(mktemp "$mpath.tmp.XXXXXX")
170
+
171
+ if command -v flock >/dev/null 2>&1; then
172
+ (
173
+ flock -x 9
174
+ jq --arg path "$tgt" --arg writer "$writer" --arg ts "$now" --arg hash "$hash" \
175
+ '.entries |= (map(select(.path != $path)) + [{"path": $path, "writer": $writer, "written_at": $ts, "content_hash": $hash}])' \
176
+ "$mpath" > "$tmp" && mv "$tmp" "$mpath"
177
+ ) 9>"$mpath.lock"
178
+ else
179
+ jq --arg path "$tgt" --arg writer "$writer" --arg ts "$now" --arg hash "$hash" \
180
+ '.entries |= (map(select(.path != $path)) + [{"path": $path, "writer": $writer, "written_at": $ts, "content_hash": $hash}])' \
181
+ "$mpath" > "$tmp" && mv "$tmp" "$mpath"
182
+ fi
183
+
184
+ rm -f "$tmp" 2>/dev/null || true
185
+ _iwp_log "register path=$tgt writer=$writer"
186
+ return 0
187
+ }
188
+
189
+ # is_install_written_path <path>
190
+ # Returns 0 (true) when path in manifest AND content hash matches.
191
+ # Returns 1 (false) otherwise.
192
+ # Override: SKIP_INSTALL_WRITTEN_PATHS=1 forces return 1 (hooks fire normally).
193
+ is_install_written_path() {
194
+ if [ -n "${SKIP_INSTALL_WRITTEN_PATHS:-}" ]; then
195
+ _iwp_log "check override SKIP_INSTALL_WRITTEN_PATHS=1 path=${1:-}"
196
+ return 1
197
+ fi
198
+
199
+ local tgt="${1:-}"
200
+ if [ -z "$tgt" ]; then
201
+ return 1
202
+ fi
203
+
204
+ local mpath
205
+ mpath=$(_iwp_manifest_path)
206
+
207
+ # Fail-soft: manifest missing or malformed → not install-written
208
+ if ! _iwp_manifest_valid; then
209
+ _iwp_log "check path=$tgt result=false reason=no-manifest-or-malformed"
210
+ [ -f "$mpath" ] && echo "install-written-paths: manifest malformed; treating as not-install-written" >&2
211
+ return 1
212
+ fi
213
+
214
+ if ! command -v jq >/dev/null 2>&1; then
215
+ _iwp_log "check path=$tgt result=false reason=no-jq"
216
+ return 1
217
+ fi
218
+
219
+ # Look up entry
220
+ local recorded_hash
221
+ recorded_hash=$(jq -r --arg path "$tgt" '.entries[] | select(.path == $path) | .content_hash' "$mpath" 2>/dev/null)
222
+ if [ -z "$recorded_hash" ] || [ "$recorded_hash" = "null" ]; then
223
+ _iwp_log "check path=$tgt result=false reason=not-in-manifest"
224
+ return 1
225
+ fi
226
+
227
+ # Compute current hash
228
+ local root="${CLAUDE_PROJECT_DIR:-$(pwd)}"
229
+ local full_tgt
230
+ case "$tgt" in
231
+ /*) full_tgt="$tgt" ;;
232
+ *) full_tgt="$root/$tgt" ;;
233
+ esac
234
+
235
+ if [ ! -f "$full_tgt" ]; then
236
+ _iwp_log "check path=$tgt result=false reason=file-missing"
237
+ return 1
238
+ fi
239
+
240
+ local current_hash
241
+ current_hash=$(_iwp_sha256 "$full_tgt")
242
+ if [ -z "$current_hash" ]; then
243
+ _iwp_log "check path=$tgt result=false reason=hash-fail"
244
+ return 1
245
+ fi
246
+
247
+ if [ "$recorded_hash" = "$current_hash" ]; then
248
+ _iwp_log "check path=$tgt result=true reason=match"
249
+ return 0
250
+ else
251
+ _iwp_log "check path=$tgt result=false reason=hash-mismatch"
252
+ return 1
253
+ fi
254
+ }
255
+
256
+ # install_written_paths_rename <old-path> <new-path>
257
+ # Moves entry; preserves writer; bumps written_at.
258
+ install_written_paths_rename() {
259
+ local old="${1:-}"
260
+ local new="${2:-}"
261
+
262
+ if [ -z "$old" ] || [ -z "$new" ]; then
263
+ echo "install-written-paths: rename requires <old-path> <new-path>" >&2
264
+ return 1
265
+ fi
266
+
267
+ if ! _iwp_validate_path "$new"; then
268
+ echo "install-written-paths: new path '$new' rejected" >&2
269
+ return 1
270
+ fi
271
+
272
+ if ! _iwp_manifest_valid; then
273
+ echo "install-written-paths: manifest missing or malformed" >&2
274
+ return 1
275
+ fi
276
+
277
+ if ! command -v jq >/dev/null 2>&1; then
278
+ echo "install-written-paths: jq not available" >&2
279
+ return 1
280
+ fi
281
+
282
+ local mpath
283
+ mpath=$(_iwp_manifest_path)
284
+
285
+ # Check old exists
286
+ local writer
287
+ writer=$(jq -r --arg path "$old" '.entries[] | select(.path == $path) | .writer' "$mpath" 2>/dev/null)
288
+ if [ -z "$writer" ] || [ "$writer" = "null" ]; then
289
+ echo "install-written-paths: '$old' not in manifest" >&2
290
+ return 1
291
+ fi
292
+
293
+ local now
294
+ now=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
295
+
296
+ # Compute hash from new location if it exists; else keep old hash
297
+ local root="${CLAUDE_PROJECT_DIR:-$(pwd)}"
298
+ local full_new
299
+ case "$new" in
300
+ /*) full_new="$new" ;;
301
+ *) full_new="$root/$new" ;;
302
+ esac
303
+
304
+ local new_hash
305
+ if [ -f "$full_new" ]; then
306
+ new_hash=$(_iwp_sha256 "$full_new")
307
+ else
308
+ new_hash=$(jq -r --arg path "$old" '.entries[] | select(.path == $path) | .content_hash' "$mpath")
309
+ fi
310
+
311
+ local tmp
312
+ tmp=$(mktemp "$mpath.tmp.XXXXXX")
313
+
314
+ jq --arg old "$old" --arg new "$new" --arg writer "$writer" --arg ts "$now" --arg hash "$new_hash" \
315
+ '.entries |= (map(select(.path != $old)) + [{"path": $new, "writer": $writer, "written_at": $ts, "content_hash": $hash}])' \
316
+ "$mpath" > "$tmp" && mv "$tmp" "$mpath"
317
+
318
+ rm -f "$tmp" 2>/dev/null || true
319
+ _iwp_log "rename old=$old new=$new"
320
+ return 0
321
+ }
322
+
323
+ # install_written_paths_rebuild
324
+ # Regenerates the manifest. V1: creates empty manifest when missing.
325
+ # Future: walks install writers' known output locations.
326
+ install_written_paths_rebuild() {
327
+ local mpath
328
+ mpath=$(_iwp_manifest_path)
329
+ if [ -f "$mpath" ] && _iwp_manifest_valid; then
330
+ return 0
331
+ fi
332
+ rm -f "$mpath" 2>/dev/null || true
333
+ _iwp_ensure_manifest
334
+ _iwp_log "rebuild manifest=$mpath"
335
+ return 0
336
+ }
337
+
338
+ # install_written_paths_verify
339
+ # Returns 0 on well-formed manifest, 1 on schema violation.
340
+ install_written_paths_verify() {
341
+ local mpath
342
+ mpath=$(_iwp_manifest_path)
343
+
344
+ if [ ! -f "$mpath" ]; then
345
+ echo "install-written-paths: manifest file missing at $mpath" >&2
346
+ return 1
347
+ fi
348
+
349
+ if ! command -v jq >/dev/null 2>&1; then
350
+ echo "install-written-paths: jq not available" >&2
351
+ return 1
352
+ fi
353
+
354
+ if ! jq -e . "$mpath" >/dev/null 2>&1; then
355
+ echo "install-written-paths: manifest JSON malformed" >&2
356
+ return 1
357
+ fi
358
+
359
+ # Basic shape check — required top-level keys present
360
+ local has_version has_entries
361
+ has_version=$(jq -r 'has("schema_version")' "$mpath" 2>/dev/null)
362
+ has_entries=$(jq -r 'has("entries")' "$mpath" 2>/dev/null)
363
+ if [ "$has_version" != "true" ] || [ "$has_entries" != "true" ]; then
364
+ echo "install-written-paths: manifest missing required keys" >&2
365
+ return 1
366
+ fi
367
+
368
+ return 0
369
+ }
@@ -25,7 +25,7 @@
25
25
  #
26
26
  # Usage from a caller SKILL body:
27
27
  #
28
- # source "${BASSCLEF_DIR:-$HOME/src/sunj-labs/bassclef}/lib/luminary-pick.sh"
28
+ # source "${BASSCLEF_DIR:-${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null || pwd)}}/lib/luminary-pick.sh"
29
29
  # CATALOG=$(luminary_pick_catalog)
30
30
  # # then Claude reasons over $CATALOG + intent, writes JSON in-turn
31
31
  #
@@ -44,22 +44,44 @@ set -euo pipefail
44
44
  # Sort: alphabetical by slug.
45
45
  # Return: 0 on success (including empty catalog); non-zero if dir missing.
46
46
  luminary_pick_catalog() {
47
- local basedir="${BASSCLEF_DIR:-$HOME/src/sunj-labs/bassclef}"
47
+ # Resolve basedir: BASSCLEF_DIR → CLAUDE_PROJECT_DIR → git root → pwd.
48
+ # cli#313 — the former fallback $HOME/src/sunj-labs/bassclef was the
49
+ # engineering team's own home path and failed for every adopter.
50
+ local basedir="${BASSCLEF_DIR:-${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null || pwd)}}"
48
51
  local catalog_dir="${basedir}/state/luminary-implementations"
52
+ local lite_catalog="${basedir}/.claude/luminaries"
49
53
 
50
- if [ ! -d "$catalog_dir" ]; then
51
- echo "[luminary-pick-catalog] catalog dir missing: $catalog_dir" >&2
52
- return 2
54
+ if [ -d "$catalog_dir" ]; then
55
+ (
56
+ shopt -s nullglob
57
+ set +e
58
+ for f in "$catalog_dir"/*.json; do
59
+ jq -r '"\(.luminary_slug) (\(.primary_domain // "unknown")) — \(.name // .luminary_slug)"' "$f" 2>/dev/null || true
60
+ done | sort -u
61
+ )
62
+ return 0
53
63
  fi
54
64
 
55
- (
56
- shopt -s nullglob
57
- set +e
58
- for f in "$catalog_dir"/*.json; do
59
- jq -r '"\(.luminary_slug) (\(.primary_domain // "unknown")) — \(.name // .luminary_slug)"' "$f" 2>/dev/null || true
60
- done | sort -u
61
- )
62
- return 0
65
+ # Lite-tier fallback (cli#313): read frontmatter from .claude/luminaries/*.md.
66
+ # Every tier ships this directory; state/luminary-implementations/ is upstream-only.
67
+ if [ -d "$lite_catalog" ]; then
68
+ (
69
+ shopt -s nullglob
70
+ set +e
71
+ for f in "$lite_catalog"/*.md; do
72
+ local slug domain name
73
+ slug=$(awk '/^slug:[[:space:]]/{sub(/^slug:[[:space:]]*/,""); print; exit}' "$f")
74
+ domain=$(awk '/^primary_domain:[[:space:]]/{sub(/^primary_domain:[[:space:]]*/,""); print; exit}' "$f")
75
+ name=$(awk '/^name:[[:space:]]/{sub(/^name:[[:space:]]*/,""); print; exit}' "$f")
76
+ [ -z "$slug" ] && continue
77
+ echo "${slug} (${domain:-unknown}) — ${name:-$slug}"
78
+ done | sort -u
79
+ )
80
+ return 0
81
+ fi
82
+
83
+ echo "[luminary-pick-catalog] catalog dir missing: $catalog_dir (and no .claude/luminaries/ fallback at $lite_catalog)" >&2
84
+ return 2
63
85
  }
64
86
 
65
87
  # Grace-window stub preserving the old function signature.
@@ -40,8 +40,9 @@ __state_check_binary_deps() {
40
40
  if [ "${__STATE_DEPS_CHECKED:-}" = "1" ]; then
41
41
  return 0
42
42
  fi
43
+ # Baseline deps — awk + jq required unconditionally
43
44
  local missing=""
44
- for bin in awk python3 jq; do
45
+ for bin in awk jq; do
45
46
  command -v "$bin" >/dev/null 2>&1 || missing="$missing $bin"
46
47
  done
47
48
  if [ -n "$missing" ]; then
@@ -49,21 +50,63 @@ __state_check_binary_deps() {
49
50
  STATE ACCESSOR FAILURE
50
51
  Required binary missing from PATH:$missing
51
52
 
52
- These binaries are load-bearing for lib/state.sh accessors (Shape B
53
- YAML frontmatter extraction + JSON tooling).
53
+ awk + jq are load-bearing for lib/state.sh accessors.
54
54
 
55
55
  Resolution:
56
- macOS: brew install gawk python3 jq
57
- Debian: sudo apt-get install gawk python3 jq
58
-
59
- Closes the bassclef#598 silent-failure class.
56
+ macOS: brew install gawk jq
57
+ Debian: sudo apt-get install gawk jq
60
58
  EOF
61
59
  return 4
62
60
  fi
61
+
62
+ # Per cli#308: YAML parser needs EITHER yq OR python3+PyYAML.
63
+ # Stock macOS has python3 but not PyYAML. Pre-cli#308, that mismatch
64
+ # caused silent failures where __state_check_binary_deps passed but
65
+ # the actual yaml parse failed with ModuleNotFoundError. Now we check
66
+ # that at least one working parser exists.
67
+ __state_check_yaml_parser || return 6
63
68
  export __STATE_DEPS_CHECKED=1
64
69
  return 0
65
70
  }
66
71
 
72
+ # YAML parser check (per cli#308). Returns 0 if yq available OR
73
+ # python3+yaml-module importable. Returns 6 if neither.
74
+ __state_check_yaml_parser() {
75
+ # Cache the result per shell process.
76
+ if [ "${__STATE_YAML_PARSER:-}" = "yq" ] || [ "${__STATE_YAML_PARSER:-}" = "python3" ]; then
77
+ return 0
78
+ fi
79
+ # Prefer yq (faster + simpler contract).
80
+ if command -v yq >/dev/null 2>&1; then
81
+ export __STATE_YAML_PARSER=yq
82
+ return 0
83
+ fi
84
+ # Fall back to python3+PyYAML.
85
+ if command -v python3 >/dev/null 2>&1 && python3 -c "import yaml" >/dev/null 2>&1; then
86
+ export __STATE_YAML_PARSER=python3
87
+ return 0
88
+ fi
89
+ cat >&2 <<EOF
90
+ STATE ACCESSOR FAILURE — no YAML parser available (per cli#308)
91
+
92
+ lib/state.sh needs ONE of:
93
+ - yq (preferred — faster, no Python module needed)
94
+ - python3 + PyYAML module
95
+
96
+ Both are missing on this machine.
97
+
98
+ Resolution:
99
+ macOS (preferred): brew install yq
100
+ macOS (alt): python3 -m pip install pyyaml
101
+ Debian: sudo apt-get install yq OR python3 -m pip install pyyaml
102
+
103
+ Per cli#308 — this check closes the silent-failure class where
104
+ python3 existed but PyYAML did not, and callers reported "goal not
105
+ found" (wrong diagnosis).
106
+ EOF
107
+ return 6
108
+ }
109
+
67
110
  # yq2json — extract YAML frontmatter from markdown file, emit JSON.
68
111
  # Used by Shape B (markdown + YAML frontmatter + body) entity accessors.
69
112
  #
@@ -86,7 +129,7 @@ EOF
86
129
  # on invalid input) + @luminary michael-feathers (characterization
87
130
  # tests RED-first via fixtures/2026-09-06-silent-empty/).
88
131
  __state_yq2json() {
89
- __state_check_binary_deps || return 4
132
+ __state_check_binary_deps || return $?
90
133
  local file="$1"
91
134
  if [ ! -f "$file" ]; then
92
135
  return 1
@@ -127,7 +170,23 @@ __state_yq2json() {
127
170
 
128
171
  local out
129
172
  local ec
130
- out=$(awk '/^---$/{c++; next} c==1' "$file" | python3 -c "
173
+ # Per cli#308 — prefer yq when available, fall back to python3+PyYAML.
174
+ # __state_check_binary_deps (called at top of this function via return 4
175
+ # path) already resolved __STATE_YAML_PARSER to one of the two.
176
+ if [ "${__STATE_YAML_PARSER:-}" = "yq" ]; then
177
+ out=$(awk '/^---$/{c++; next} c==1' "$file" | yq -o=json '.' 2>&1)
178
+ ec=$?
179
+ if [ "$ec" -ne 0 ]; then
180
+ printf 'yaml parse failed (yq): %s\n' "$out" >&2
181
+ return 3
182
+ fi
183
+ # yq returns `null` for empty input; coerce to `{}` for consistency
184
+ # with python3 path.
185
+ if [ "$out" = "null" ]; then
186
+ out="{}"
187
+ fi
188
+ else
189
+ out=$(awk '/^---$/{c++; next} c==1' "$file" | python3 -c "
131
190
  import sys, yaml, json
132
191
  try:
133
192
  data = yaml.safe_load(sys.stdin) or {}
@@ -136,7 +195,8 @@ except Exception as e:
136
195
  print(f'yaml parse failed: {e}', file=sys.stderr)
137
196
  sys.exit(3)
138
197
  ")
139
- ec=$?
198
+ ec=$?
199
+ fi
140
200
  # Instrument real-parse count for #1519 tests + observability.
141
201
  __STATE_YQ2JSON_PARSE_COUNT=$(( ${__STATE_YQ2JSON_PARSE_COUNT:-0} + 1 ))
142
202
  if [ "$ec" -ne 0 ]; then
@@ -185,7 +245,7 @@ __state_atomic_write() {
185
245
  # output) + @luminary michael-nygard (silent-fail wrap; write does not
186
246
  # crash caller).
187
247
  __state_yaml_write() {
188
- __state_check_binary_deps || return 4
248
+ __state_check_binary_deps || return $?
189
249
  # Finding #6 (#2036): `local path` is a zsh-magic variable name — ties to
190
250
  # $PATH when sourced from zsh. Renamed to `local p` throughout this file.
191
251
  local p="$1"