Skip to content

fix(batch): harden gateway status/results parsing against null counts and malformed lines - #17

Merged
seonghobae merged 3 commits into
mainfrom
claude/cwlab-pr-audit-governance-1hdcp5
Aug 3, 2026
Merged

fix(batch): harden gateway status/results parsing against null counts and malformed lines#17
seonghobae merged 3 commits into
mainfrom
claude/cwlab-pr-audit-governance-1hdcp5

Conversation

@seonghobae

Copy link
Copy Markdown
Contributor

The defects

Two unhandled external-input crash paths in pg_llm_batch/batch_api_client.py, both parsing an OpenAI-compatible gateway response:

  1. get_batch_statusAttributeError on request_counts: null. The code read counts = result.get("request_counts", {}). When the gateway returns {"request_counts": null, ...} — a legitimate response while a batch is still validating/queued — the key is present, so .get(..., {}) returns None, not {}. The next line, counts.get("total", 0), then raises AttributeError: 'NoneType' object has no attribute 'get', crashing every poll of such a batch (and download_results, which polls first).

  2. download_results — uncaught json.JSONDecodeError on a malformed output line. Results were parsed with [json.loads(line) for line in content.strip().split("\n") if line]. A truncated or corrupt output file (untrusted gateway content) raises a raw json.JSONDecodeError that leaks past the gateway boundary instead of the module's typed GatewayError.

The fix

  • counts = result.get("request_counts") or {} — treats null like absent, so the progress math never dereferences None (0% progress for a not-yet-counted batch).
  • Wrap the per-line parse and raise GatewayError (the module's existing error type, already raised elsewhere in these functions) with the offending line number, so malformed downloads surface as a typed, boundary-appropriate error.

Two regression tests added: a poll with request_counts: null (crashed before, now 0%) and a malformed result line (leaked JSONDecodeError before, now GatewayError).

Verification

  • uv run pytest tests/test_batch_api_client.py12 passed (10 existing + 2 new).
  • ruff check pg_llm_batch/batch_api_client.py tests/test_batch_api_client.py → clean.
  • Non-duplicate: none of the repo's 5 open PRs (fix(security): annotate reviewed Semgrep p/default false-positives #16 Semgrep suppressions in other files; 4 dependabot bumps) touch batch_api_client.py.

🤖 Generated with Claude Code

https://claude.ai/code/session_01HdCssGnNMhKHNu3TXFstWH


Generated by Claude Code

… and malformed lines

`get_batch_status` read `result.get("request_counts", {})`, which returns None
(not `{}`) when the OpenAI-compatible gateway sends `"request_counts": null` —
a legitimate response while a batch is still validating/queued. The following
`counts.get("total", ...)` then raised `AttributeError`, crashing every poll of
such a batch. Use `or {}` so null is treated like absent (0% progress).

`download_results` parsed the output file with a bare list comprehension over
`json.loads(line)`; a truncated or corrupt line (untrusted gateway content)
raised an uncaught `json.JSONDecodeError`, leaking an internal parse error past
the gateway boundary. Wrap the parse and raise the module's typed `GatewayError`
with the offending line number instead.

Adds two regression tests (null request_counts poll; malformed result line ->
GatewayError). Full tests/test_batch_api_client.py: 12 passed; ruff clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HdCssGnNMhKHNu3TXFstWH
@coderabbitai

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@seonghobae, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 21 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 12680399-1bf8-4fe5-b225-f227343b8b02

📥 Commits

Reviewing files that changed from the base of the PR and between 29559da and f8b694b.

📒 Files selected for processing (5)
  • docker/postgres/Dockerfile
  • pg_llm_batch/batch_api_client.py
  • pg_llm_batch/config.py
  • pg_llm_batch/token_counter.py
  • tests/test_batch_api_client.py

Comment @coderabbitai help to get the list of available commands.

Copy link
Copy Markdown
Contributor Author

The failing Semgrep (multi-language SAST) check is a pre-existing base-branch failure, not caused by this diff. The gate scans the whole merge tree with p/default, and the 15 Medium+ findings live in pg_llm_batch/config.py, pg_llm_batch/token_counter.py, and docker/postgres/Dockerfile — none of which this PR touches (its only changes are two lines in batch_api_client.py plus tests). It reproduces identically on every open pg-llm-batch PR.

Those base findings are the exact set being annotated as reviewed false-positives in #16; this PR goes green on Semgrep once that base fix (or an equivalent # nosemgrep pass) lands. I'm not duplicating those suppressions here to avoid overlapping with #16.


Generated by Claude Code

@opencode-agent opencode-agent Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

OpenCode cannot approve yet because required coverage evidence did not pass.

Review outcome

1. HIGH .github/workflows/opencode-review.yml:1 - Coverage evidence did not prove required test/docstring evidence

  • Problem: The required coverage-evidence job result was failure, so OpenCode cannot establish approval sufficiency for this head.

  • Root cause: Automated approval is only valid when the same-head coverage-evidence job proves supported repository test suites passed and configured docstring gates passed or were advisory, or reports not applicable because no supported source files or package manifests exist. Missing, failed, skipped, unavailable, or unsupported-tooling test evidence is a blocker.

  • Fix: Install or configure the repository test/docstring evidence tooling when source files or package manifests exist, rerun the current-head coverage-evidence job, and approve only after it reports success with required evidence or explicit no-source not-applicable evidence.

  • Regression test: Keep the approval branch checking needs.coverage-evidence.result == success before posting APPROVE, and publish REQUEST_CHANGES when coverage-evidence blocker states such as cancelled, skipped, failed, unsupported-tooling, or below-100 evidence are present.

  • Result: REQUEST_CHANGES

  • Reason: coverage-evidence result was failure, so required test/docstring evidence was not proven for current head 4614901061fd1158009902984a1d96dc2004c70b.

  • Head SHA: 4614901061fd1158009902984a1d96dc2004c70b

  • Workflow run: 30514784578

  • Workflow attempt: 1

Coverage evidence

Coverage Decision

  • Result: FAIL
  • Test evidence: not proven passing
  • Docstring evidence: not proven passing when configured
  • Failure count: 1

Changed-File Evidence Map

flowchart LR
  PR["PR changed files"] --> Evidence["OpenCode bounded evidence"]
  Evidence --> S1["Changed file: batch_api_client.py"]
  S1 --> I1["repository behavior"]
  I1 --> R1["Review risk: Changed file: batch_api_client.py"]
  R1 --> V1["required checks"]
  Evidence --> S2["Test: test_batch_api_client.py"]
  S2 --> I2["regression suite"]
  I2 --> R2["Review risk: Test: test_batch_api_client.py"]
  R2 --> V2["targeted test run"]
Loading

@opencode-agent

Copy link
Copy Markdown
Contributor

OpenCode Review Overview

  • Head SHA: 4614901061fd1158009902984a1d96dc2004c70b
  • Workflow run: 30514784578
  • Workflow attempt: 1
  • Gate result: REQUEST_CHANGES (approval step)

Pull request overview

OpenCode cannot approve yet because required coverage evidence did not pass.

Review outcome

1. HIGH .github/workflows/opencode-review.yml:1 - Coverage evidence did not prove required test/docstring evidence

  • Problem: The required coverage-evidence job result was failure, so OpenCode cannot establish approval sufficiency for this head.

  • Root cause: Automated approval is only valid when the same-head coverage-evidence job proves supported repository test suites passed and configured docstring gates passed or were advisory, or reports not applicable because no supported source files or package manifests exist. Missing, failed, skipped, unavailable, or unsupported-tooling test evidence is a blocker.

  • Fix: Install or configure the repository test/docstring evidence tooling when source files or package manifests exist, rerun the current-head coverage-evidence job, and approve only after it reports success with required evidence or explicit no-source not-applicable evidence.

  • Regression test: Keep the approval branch checking needs.coverage-evidence.result == success before posting APPROVE, and publish REQUEST_CHANGES when coverage-evidence blocker states such as cancelled, skipped, failed, unsupported-tooling, or below-100 evidence are present.

  • Result: REQUEST_CHANGES

  • Reason: coverage-evidence result was failure, so required test/docstring evidence was not proven for current head 4614901061fd1158009902984a1d96dc2004c70b.

  • Head SHA: 4614901061fd1158009902984a1d96dc2004c70b

  • Workflow run: 30514784578

  • Workflow attempt: 1

Coverage evidence

Coverage Decision

  • Result: FAIL
  • Test evidence: not proven passing
  • Docstring evidence: not proven passing when configured
  • Failure count: 1

Changed-File Evidence Map

flowchart LR
  PR["PR changed files"] --> Evidence["OpenCode bounded evidence"]
  Evidence --> S1["Changed file: batch_api_client.py"]
  S1 --> I1["repository behavior"]
  I1 --> R1["Review risk: Changed file: batch_api_client.py"]
  R1 --> V1["required checks"]
  Evidence --> S2["Test: test_batch_api_client.py"]
  S2 --> I2["regression suite"]
  I2 --> R2["Review risk: Test: test_batch_api_client.py"]
  R2 --> V2["targeted test run"]
Loading

Annotate 15 reviewed Semgrep false positives without weakening the SAST gate. Current head is tree-identical to the exact code snapshot whose Semgrep and Security Scan runs passed; the stale old-head OpenCode review was dismissed after the central coverage workflow was repaired.
@seonghobae
seonghobae dismissed opencode-agent[bot]’s stale review August 3, 2026 01:29

Superseded by current head f8b694b. The old REQUEST_CHANGES was solely a central coverage-evidence infrastructure failure on head 4614901; the central workflow has since been repaired. The branch was refreshed onto main after #16, and compare d8d1889...f8b694b confirms the effective PR diff remains exactly the two reviewed source/test files. Fresh current-head checks have been triggered.

@seonghobae
seonghobae merged commit 2fbebe7 into main Aug 3, 2026
19 checks passed
@seonghobae
seonghobae deleted the claude/cwlab-pr-audit-governance-1hdcp5 branch August 3, 2026 01:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants