Skip to content

fix(llm): surface claude-cli envelope errors on both exit paths (#2554) - #2555

Closed
annieyii wants to merge 1 commit into
Graphify-Labs:v8from
annieyii:fix/claude-cli-envelope-errors
Closed

annieyii wants to merge 1 commit into
Graphify-Labs:v8from
annieyii:fix/claude-cli-envelope-errors

Conversation

@annieyii

@annieyii annieyii commented Aug 8, 2026

Copy link
Copy Markdown

Fixes #2554

Problem

claude -p reports API failures in the stdout JSON envelope (is_error: true) with an empty stderr, and on a rate limit it may exit 0. Both call sites in llm.py decide success from returncode and read the cause from stderr only, so three failures go unreported:

  • A. Non-zero exit prints claude -p exited 1: with nothing after the colon.
  • B. Zero exit feeds "API Error: Rate limit reached" to _parse_llm_json; the empty parse reads as truncation and adaptive retry bisects the chunk, re-issuing requests that are still being refused.
  • C. _call_llm returns that same string as the model's reply, so community labeling and the dedup tiebreaker can write API Error: Rate limit reached into graph.json as a label.

Change

_claude_cli_error(stdout) returns the envelope's result text when is_error is set, and "" otherwise, including when stdout will not parse, so the existing "unparseable JSON envelope" error still takes precedence.

Both call sites (_call_claude_cli, _call_llm) then:

  • prefer stderr on a non-zero exit and fall back to the envelope text, so a genuine CLI crash or spawn failure still reports its own diagnostics;
  • raise on a zero exit when the flag is set, which fixes B and C together since both stem from that string flowing downstream.

Failing fast is intended here: extract_corpus_parallel already catches per-chunk exceptions and continues, so each chunk now reports the real cause instead of silently bisecting during an outage.

Relationship to #1864

#1864 found the same root cause and fixes A via proc.stdout.strip()[-500:]. It has not landed; llm.py:1541 on v8 still reads stderr only.

This PR is a superset: it also covers B and C, which occur on a zero exit that #1864's non-zero branch never reaches, and it surfaces the envelope's result field rather than a 500-character tail of raw JSON. Happy to rebase onto #1864 instead if the author would rather extend that PR.

Verification

Four tests added to tests/test_claude_cli_backend.py, following the file's existing MagicMock + patch("subprocess.run") convention, so CI needs no claude binary:

  • test_nonzero_exit_surfaces_envelope_error_when_stderr_empty
  • test_raises_on_error_envelope_with_zero_exit
  • test_call_llm_raises_on_error_envelope
  • test_call_llm_nonzero_exit_surfaces_envelope_error

Each was written and observed failing before the implementation.

tests/test_claude_cli_backend.py tests/test_llm_backends.py
tests/test_llm_parser.py tests/test_backend_extras.py     136 passed

full suite                                               4041 passed, 36 skipped

Four unrelated failures in tests/test_ollama_retry_cap.py are pre-existing on my machine and come from the optional openai package not being installed (ModuleNotFoundError: No module named 'openai' raised by monkeypatch), not from this change.

Against a live rate-limited account:

before  [graphify] chunk 1/11 failed: claude -p exited 1:
after   [graphify] chunk 1/11 failed: claude -p exited 1: API Error: Rate limit reached

…hify-Labs#2554)

`claude -p` reports API failures in the stdout JSON envelope with `is_error`
set and an empty stderr, and on a rate limit it can exit 0. Both call sites
decided success from `returncode` and read the cause from `stderr` only, so
three failures went unreported: a non-zero exit printed a bare "exited 1:"
with no cause; a zero exit fed "API Error: Rate limit reached" to the JSON
parser, where the empty parse read as truncation and adaptive retry bisected
the chunk against an outage that could not succeed; and `_call_llm` returned
that same string as the model's reply, letting community labeling and the
dedup tiebreaker persist it into graph.json as a label.

`_claude_cli_error` reads the envelope's `is_error` flag instead of inferring
failure from the exit code. On a non-zero exit stderr keeps priority so a
genuine CLI crash still reports its own diagnostics, with the envelope text as
the fallback; on a zero exit the flag now raises. Failing fast is intended:
`extract_corpus_parallel` already catches per-chunk exceptions and continues.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

@graphify-labs graphify-labs Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graphify reviewed this change.

Looks safe to merge — no coupling regressions and no blocking issues, checked against the code graph (not a self-assessment).


Graphify review — findings

This PR adds error detection for the claude -p CLI backend by introducing a new _claude_cli_error helper that inspects the stdout JSON envelope for an is_error flag and extracts the CLI's error text. It wires this check into both _call_claude_cli and _call_llm so that error envelopes raise RuntimeError (including when the CLI exits 0 or reports errors only in stdout rather than stderr), and enriches the existing non-zero-exit error message with the envelope detail as a fallback. New tests in test_claude_cli_backend.py cover the error-envelope scenarios for both entry points across zero and non-zero exit codes.

No blocking issues surfaced. 2 lower-confidence candidates did not survive cross-model review.

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 657 functions depend on the 207 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: _call_claude_cli() — 28 callers, 9 callees
  • worse: _call_llm() — 9 callers, 17 callees

Verification — 657 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 428 function(s) in the blast radius were not formally verified this run

· 2 more finding(s) on lines outside this diff (see the check run).

safishamsi added a commit that referenced this pull request Aug 8, 2026
…e errors; bump to 0.9.37 (#2543, #2554)

#2543 (adopts PR #2546, thanks @michaelxer): a failed extraction is no
longer stamped in the incremental manifest as up-to-date, so graphify
update retries it instead of skipping it forever; a manifest already
poisoned by the old behavior is healed on the next run; genuinely
unchanged files are not re-processed. Extended to the watch save_manifest
paths too.

#2554 (adopts PR #2555, thanks @annieyii): the claude-cli backend now
inspects the stdout envelope for an is_error result (e.g. a rate limit
returned with exit code 0) and raises it on both the zero and non-zero
exit paths, instead of parsing it as an empty success and bisecting
against a live rate limit.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@safishamsi

Copy link
Copy Markdown
Member

Shipped in v0.9.37. Adopted essentially verbatim: _claude_cli_error surfaces an is_error envelope on both exit paths (#2554). Credited in the release notes. Thanks @annieyii!

@safishamsi safishamsi closed this Aug 8, 2026
PerryLin-0420 pushed a commit to PerryLin-0420/graphify-decouple that referenced this pull request Aug 13, 2026
…e errors; bump to 0.9.37 (Graphify-Labs#2543, Graphify-Labs#2554)

Graphify-Labs#2543 (adopts PR Graphify-Labs#2546, thanks @michaelxer): a failed extraction is no
longer stamped in the incremental manifest as up-to-date, so graphify
update retries it instead of skipping it forever; a manifest already
poisoned by the old behavior is healed on the next run; genuinely
unchanged files are not re-processed. Extended to the watch save_manifest
paths too.

Graphify-Labs#2554 (adopts PR Graphify-Labs#2555, thanks @annieyii): the claude-cli backend now
inspects the stdout envelope for an is_error result (e.g. a rate limit
returned with exit code 0) and raises it on both the zero and non-zero
exit paths, instead of parsing it as an empty success and bisecting
against a live rate limit.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants