Skip to content

Action exits with code 2: jq stats extraction fails despite successful Claude execution #878

@fandasson

Description

@fandasson

Describe the bug
Action exits with code 2 during stats extraction post-processing, despite Claude analysis completing successfully. The jq command fails with "invalid JSON text passed to --argjson" when building execution statistics.

To Reproduce

Conversation ends with message:

{
  "type": "result",
  "subtype": "success",
  "is_error": false,
  "duration_ms": 158935,
  "num_turns": 39,
  "total_cost_usd": 0.5077904999999999,
  "permission_denials": [
  ]
}

Then it in the log it is followed by:

Run # Try to format the turns, but if it fails, dump the raw JSON
Successfully formatted Claude Code report

and then:

Run EXEC_FILE="/home/runner/work/_temp/claude-execution-output.json"
  EXEC_FILE="/home/runner/work/_temp/claude-execution-output.json"
  if [[ -f "$EXEC_FILE" ]]; then
    TURNS=$(grep -c '"type":"assistant"' "$EXEC_FILE" 2>/dev/null || echo "0")
    INPUT_TOKENS=$(jq -s '[.[] | select(.usage) | .usage.input_tokens // 0] | add // 0' "$EXEC_FILE" 2>/dev/null || echo "0")
    OUTPUT_TOKENS=$(jq -s '[.[] | select(.usage) | .usage.output_tokens // 0] | add // 0' "$EXEC_FILE" 2>/dev/null || echo "0")
  
    STATS=$(jq -n \
      --argjson turns "$TURNS" \
      --argjson input "$INPUT_TOKENS" \
      --argjson output "$OUTPUT_TOKENS" \
      '{turns: $turns, input_tokens: $input, output_tokens: $output, total_tokens: ($input + $output)}')
  
    echo "stats=$STATS" >> "$GITHUB_OUTPUT"
    echo "### Execution Statistics" >> "$GITHUB_STEP_SUMMARY"
    echo "| Metric | Value |" >> "$GITHUB_STEP_SUMMARY"
    echo "|--------|-------|" >> "$GITHUB_STEP_SUMMARY"
    echo "| Turns | $TURNS |" >> "$GITHUB_STEP_SUMMARY"
    echo "| Input Tokens | $INPUT_TOKENS |" >> "$GITHUB_STEP_SUMMARY"
    echo "| Output Tokens | $OUTPUT_TOKENS |" >> "$GITHUB_STEP_SUMMARY"
  else
    echo "stats={}" >> "$GITHUB_OUTPUT"
  fi
  shell: /usr/bin/bash --noprofile --norc -e -o pipefail {0}
jq: invalid JSON text passed to --argjson
Use jq --help for help with command-line options,
or see the jq manpage, or online docs  at https://jqlang.github.io/jq
Error: Process completed with exit code 2.

Expected behavior
Action should exit with code 0 after successful Claude execution. Stats extraction should handle edge cases gracefully (empty values, missing fields) and fall back to "0" as intended by the || echo "0" pattern.

Workflow yml file

with:
  anthropic_api_key: ***
  max_turns: 60
  github_token: ***
  show_debug_output: false
  claude_args: |
    --model claude-sonnet-4-20250514
    --max-turns 60
    --allowedTools "Bash(gh:*),Bash(curl:*),Bash(jq:*),Read,Grep,Glob,WebFetch,WebSearch"

API Provider

[x] Anthropic First-Party API (default)
[ ] AWS Bedrock
[ ] GCP Vertex

Additional context

  • Action version: claude-code-action@2817c54db8f44f3a0485d57292566bf07d428a25
  • Claude Code version: 2.1.22
  • The bug is in post-processing script that extracts INPUT_TOKENS/OUTPUT_TOKENS variables. The || echo "0" fallback doesn't properly handle the case where jq returns invalid JSON
  • This causes workflow to fail even though Claude completed successfully and posted analysis to PR
  • Suggested fix: Validate variables are valid JSON numbers before passing to final jq command, or use default values if extraction fails

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingp2Non-showstopper bug or popular feature requestprovider:1pAnthropic First-Party API

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions