Skip to content

fix: Invalid JSON crash in result collection loop when provider returns malformed output #3

@quijanoj-dev

Description

@quijanoj-dev

Bug

When a provider script returns malformed or non-JSON output, query-council.sh crashes with:

Error: Invalid JSON input

Root Cause

In the result collection loop (query-council.sh ~line 443), jq silently produces empty output when the result file contains invalid JSON:

result=$(echo "$result" | jq --arg m "$model" '. + {model: $m}')
# ↑ fails silently if result_file was invalid JSON → result=""
RESULTS=$(echo "$RESULTS" | jq --arg p "$provider" --argjson r "$result" '.[$p] = $r')
# ↑ --argjson r "" → "Error: Invalid JSON input" → whole script exits

The same pattern exists in the round 2 (debate mode) collection loop.

Fix

Add a JSON validity guard before processing each result file:

if [[ -f "$result_file" ]]; then
    result=$(cat "$result_file")
    # Guard: invalid JSON from provider script crashes --argjson; wrap as error
    if ! echo "$result" | jq empty 2>/dev/null; then
        result=$(jq -n --arg e "Provider returned invalid JSON: $(echo "$result" | head -c 120)" \
            '{status: "error", error: $e, cached: false}')
    fi
    result=$(echo "$result" | jq --arg m "$model" '. + {model: $m}')
    ...

Apply the same guard to the round 2 collection loop.

Affected Files

  • scripts/query-council.sh — round 1 collection loop (~line 441) and round 2 collection loop (~line 527)

Reproduction

Trigger with a provider that returns ANSI escape codes, connection errors, or any non-JSON stdout. Observed with agy provider returning formatted text output during a status check session.

Environment

  • claude-council 2026.5.3
  • macOS Darwin 25.5.0
  • Triggered by agy provider script returning non-JSON response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions