Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Pipeline Plan 242

Seth Ford edited this page Mar 10, 2026 · 5 revisions

Plan: Fix Misleading "jq not available" Warning (#242)

Root Cause Analysis

Root Cause Hypothesis (ranked by likelihood)

  1. Case 2 only handles JSON arrays, not objects (99% confidence) — The _extract_text_from_json function at scripts/sw-loop.sh:579 checks first_char == "[" before attempting jq parsing. When Claude outputs a JSON object ({...}), this condition is false, so jq is never tried. Control falls through to Case 3 (line 599), which catches both [ and { and unconditionally prints "jq not available" — even though jq was never attempted for objects.

  2. Claude --output-format json changed output format — Claude's JSON output mode may now sometimes emit a single JSON object instead of an array. The original code was written assuming arrays only.

  3. Warning message is simply wrong — Even if we don't want to parse objects, the message should distinguish "jq not available" from "unexpected JSON format".

Evidence Gathered

  • File:line: scripts/sw-loop.sh:579-603
  • Case 2 (line 579): if [[ "$first_char" == "[" ]] && command -v jq >/dev/null 2>&1 — only enters jq parsing for arrays
  • Case 3 (line 599): if [[ "$first_char" == "[" || "$first_char" == "{" ]] — catches objects BUT prints misleading "jq not available" warning
  • Impact: Cosmetic only — the raw JSON is copied to the log file and the loop continues correctly
  • No previous failed attempt — plan.md was empty, this is the first plan

Fix Strategy

Option A (recommended): Extend Case 2 to handle both JSON arrays and objects with jq. When first_char is {, extract .result // empty directly (no array indexing). This gives proper text extraction for both formats. Case 3 becomes a true "jq not available" fallback.

This is the minimal, targeted fix that:

  • Properly extracts .result from JSON objects (not just arrays)
  • Keeps the "jq not available" warning accurate (only shown when jq is genuinely missing)
  • Maintains backward compatibility for all existing cases

Files to Modify

File Action Purpose
scripts/sw-loop.sh Modify Fix _extract_text_from_json() Case 2 to handle JSON objects
scripts/sw-loop-test.sh Modify Add test for JSON object extraction + misleading warning regression

Implementation Steps

Step 1: Fix _extract_text_from_json() in sw-loop.sh

Modify lines 578-603 to handle both [ and { first characters when jq is available:

# Case 2: Valid JSON (array or object) — extract .result with jq
if [[ "$first_char" == "[" || "$first_char" == "{" ]] && command -v jq >/dev/null 2>&1; then
 local extracted
 if [[ "$first_char" == "[" ]]; then
 extracted=$(jq -r '.[-1].result // empty' "$json_file" 2>/dev/null) || true
 else
 extracted=$(jq -r '.result // empty' "$json_file" 2>/dev/null) || true
 fi
 if [[ -n "$extracted" ]]; then
 echo "$extracted" > "$log_file"
 return 0
 fi
 # jq succeeded but result was null/empty — try .content or raw text
 if [[ "$first_char" == "[" ]]; then
 extracted=$(jq -r '.[].content // empty' "$json_file" 2>/dev/null | head -500) || true
 else
 extracted=$(jq -r '.content // empty' "$json_file" 2>/dev/null | head -500) || true
 fi
 if [[ -n "$extracted" ]]; then
 echo "$extracted" > "$log_file"
 return 0
 fi
 # JSON parsed but no text found — write placeholder
 warn "JSON output has no .result field — check $json_file"
 echo "(no text result in JSON output)" > "$log_file"
 return 0
fi
# Case 3: Looks like JSON but no jq — can't parse, use raw
if [[ "$first_char" == "[" || "$first_char" == "{" ]]; then
 warn "JSON output but jq not available — using raw output"
 cp "$json_file" "$log_file"
 return 0
fi

Key changes:

  • Case 2 condition: "$first_char" == "[""$first_char" == "[" || "$first_char" == "{"
  • Inside Case 2: branch on first_char to use .[-1].result for arrays vs .result for objects
  • Same branching for the .content fallback
  • Case 3 is now only reachable when jq is genuinely not installed

Step 2: Add tests in sw-loop-test.sh

Add two new test cases after the existing Test 21 block (after line 368):

  1. JSON object extraction: Verify _extract_text_from_json correctly extracts .result from a JSON object {"type":"result","result":"Object extraction works"}
  2. No misleading warning: Verify that when jq IS available and input is a JSON object, the "jq not available" warning is NOT emitted

Task Checklist

  • Task 1: Modify Case 2 condition in _extract_text_from_json() to accept both [ and {
  • Task 2: Add array vs object branching for .result extraction inside Case 2
  • Task 3: Add array vs object branching for .content fallback inside Case 2
  • Task 4: Add test: JSON object .result extraction works
  • Task 5: Add test: no misleading "jq not available" warning for JSON objects when jq is present
  • Task 6: Run sw-loop-test.sh to verify all tests pass
  • Task 7: Verify existing tests still pass (no regression)

Testing Approach

  1. Unit test — JSON object extraction: Create a temp file with {"type":"result","result":"Object extraction works"}, run _extract_text_from_json, verify output contains "Object extraction works"
  2. Unit test — warning suppression: Run _extract_text_from_json on a JSON object with warn() that captures output, verify "jq not available" is NOT in the output
  3. Regression: Run full sw-loop-test.sh — all existing tests (15-21) must still pass
  4. Manual verification: If possible, run a real shipwright loop iteration that produces JSON object output and confirm no misleading warning

Definition of Done

  • _extract_text_from_json() correctly extracts .result from both JSON arrays and JSON objects
  • "jq not available" warning only appears when jq is genuinely not installed
  • New test cases cover JSON object extraction
  • All existing sw-loop-test.sh tests pass (no regression)
  • Code follows Bash 3.2 compatibility requirements
  • No unnecessary changes outside the bug scope

Verification Plan

# Run the loop test suite
./scripts/sw-loop-test.sh
# Verify specific new behavior manually
tmpdir=$(mktemp -d)
echo '{"type":"result","result":"Object works"}' > "$tmpdir/obj.json"
# Extract the function and test it
_extract_fn=$(sed -n '/^_extract_text_from_json()/,/^}/p' scripts/sw-loop.sh)
bash -c "
warn() { echo \"WARN: \$*\" >&2; }
$_extract_fn
_extract_text_from_json '$tmpdir/obj.json' '$tmpdir/obj_out.log' ''
" 2>"$tmpdir/warnings.txt"
cat "$tmpdir/obj_out.log" # Should contain "Object works"
cat "$tmpdir/warnings.txt" # Should be empty (no misleading warning)
rm -rf "$tmpdir"

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /