Skip to content

fix(agents): wrap JSON.parse in readGooglePromptCacheJson with descriptive error - #123632

Closed
coaiMax wants to merge 1 commit into
openclaw:mainfrom
coaiMax:fix/google-prompt-cache-json-parse
Closed

coaiMax wants to merge 1 commit into
openclaw:mainfrom
coaiMax:fix/google-prompt-cache-json-parse

Conversation

@coaiMax

@coaiMax coaiMax commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Problem: readGooglePromptCacheJson at line 296 calls JSON.parse(buffer.toString("utf8")) without a try/catch. If the Google cachedContents endpoint returns a non-JSON response (e.g., HTML error page, truncated body), a raw SyntaxError propagates with no context about its origin.
  • Solution: Wrap JSON.parse in a try/catch and throw a descriptive Error that identifies the response as a Google prompt cache response.
  • What changed: src/agents/embedded-agent-runner/google-prompt-cache.ts - added try/catch with descriptive error; src/agents/embedded-agent-runner/google-prompt-cache.test.ts - added regression test with non-JSON response.
  • What did NOT change: No API changes, no behavioral changes for valid JSON responses.

Real behavior proof

  • Behavior or issue addressed: A non-JSON response from the Google cachedContents endpoint caused an opaque SyntaxError: Unexpected token 'o', "not-json-at-all" is not valid JSON with no indication of which API call failed.
  • Real environment tested: Node.js with vitest, running preparePromptCacheStream with a mock fetch returning "not-json-at-all" as the response body.
  • Exact steps or command run after this patch:
    npx vitest run src/agents/embedded-agent-runner/google-prompt-cache.test.ts
  • Evidence after fix:
    Response body "not-json-at-all" => throws "Google prompt cache response was not valid JSON: ..."
    Response body '{"name":"x"}' => parsed successfully
    
  • Observed result after fix: The error message now clearly identifies the source as a Google prompt cache response, making debugging significantly easier.
  • What was not tested: did not make a live Google API call; only exercised the parsing logic via unit test.

Regression Test Plan

  • Coverage level: Unit test
  • Target test file: src/agents/embedded-agent-runner/google-prompt-cache.test.ts
  • Scenario locked in: A non-JSON response body causes preparePromptCacheStream to reject with an error matching /not valid JSON/i.
  • Why this is the smallest reliable guardrail: The test directly verifies the descriptive error wrapping that was missing.

Root Cause

  • Root cause: The function was written to handle the happy path (valid JSON from Google API) but did not account for non-JSON responses (e.g., HTML error pages, proxy intercepts, truncated bodies).
  • Missing detection / guardrail: No test exercised the non-JSON response path, so the raw SyntaxError propagation was invisible.

@clawsweeper

clawsweeper Bot commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: XS labels Aug 14, 2026
@clawsweeper clawsweeper Bot added P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Aug 14, 2026
@clawsweeper

clawsweeper Bot commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed August 14, 2026, 8:01 AM ET / 12:01 UTC.

ClawSweeper review

What this changes

This PR wraps malformed JSON errors from Google prompt-cache responses with cache-specific context and adds a test for an invalid successful response.

Merge readiness

⛔ Blocked until real behavior proof from a real setup is added - 9 items remain

Keep this PR open: the parser-level fix is in the right owner, but its added regression test already passes against the raw current behavior and therefore does not prove the new diagnostic.

Priority: P3
Reviewed head: 7db9e40202a0b271ab04c38fe44c596676aa8148

Review scores

Measure Result What it means
Overall readiness 🦪 silver shellfish (2/6) The repair is targeted, but its regression assertion is ineffective and its supplied proof is mock-only.
Proof confidence 🦪 silver shellfish (2/6) Needs real behavior proof before merge: The PR body documents a mocked fetch/Vitest run; add redacted after-fix evidence through the real Google cache response boundary before merge. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Patch quality 🦪 silver shellfish (2/6) 1 actionable review finding remain.

Verification

Check Result Evidence
Real behavior Needs proof Needs real behavior proof before merge: The PR body documents a mocked fetch/Vitest run; add redacted after-fix evidence through the real Google cache response boundary before merge. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Evidence reviewed 4 items Current behavior: Current main still directly returns JSON.parse from the prompt-cache response reader, so malformed successful responses retain only the engine parse error.
Regression test does not distinguish the fix: The proposed matcher accepts the unchanged Node SyntaxError for "not-json-at-all", so it passes before the wrapper is applied.
Owner and caller boundary: The parse helper is used by successful cache creation and TTL refresh responses; creation failures propagate through the wrapped stream, while refresh failures intentionally fall back to the still-valid cache.
Findings 1 actionable finding [P2] Assert the new contextual error message
Security None None.

How this fits together

The embedded Google agent runner creates or refreshes provider-side prompt caches before streaming a model response. Successful cache API responses are size-bounded, parsed, and then used to either attach cached content or continue without it.

flowchart LR
  A[Agent prompt] --> B[Google cache request]
  B --> C[Size-bounded response]
  C --> D[JSON parsing]
  D --> E[Cache entry]
  E --> F[Model stream]
Loading

Before merge

  • Add real behavior proof - Needs real behavior proof before merge: The PR body documents a mocked fetch/Vitest run; add redacted after-fix evidence through the real Google cache response boundary before merge. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
  • Assert the new contextual error message (P2) - The added matcher already passes on current main: JSON.parse("not-json-at-all") throws a message containing “not valid JSON.” Assert the Google prompt-cache prefix (and preferably retain/assert the original error as cause) so this regression test fails before the wrapper exists.
  • Resolve merge risk (P1) - The stated regression guard would not catch removal of the contextual wrapper because current raw JSON.parse already matches it.
  • Resolve merge risk (P1) - The submitted evidence is mock-only; no redacted after-fix response-path evidence from the Google cache boundary is present.
  • Resolve merge risk (P1) - Production is net +6 lines for this diagnostics repair; retain one parse-owner boundary rather than adding downstream error translation.
  • Complete next step (P2) - The assertion repair is mechanical, but the remaining real provider proof must come from the contributor; no repair job should be queued.
  • Improve patch quality - Assert the cache-specific error prefix so the regression test fails on the unwrapped parser.
  • Improve patch quality - Preserve the caught parser error as cause and cover that diagnostic chain.
  • Improve patch quality - Add redacted after-fix provider-boundary evidence; updating the PR body should trigger fresh review.

Findings

  • [P2] Assert the new contextual error message — src/agents/embedded-agent-runner/google-prompt-cache.test.ts:817
Agent review details

Security

None.

PR surface

Source +6, Tests +26. Total +32 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 7 1 +6
Tests 1 26 0 +26
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 33 1 +32

Review metrics

Metric Value Why it matters
Production vs test LOC production +6, tests +26 The production growth is small and localized to the parse owner, but the test must demonstrate the added diagnostic rather than existing parser output.

Stored data model

Persistent data-model change detected: persistent cache schema: src/agents/embedded-agent-runner/google-prompt-cache.test.ts, persistent cache schema: src/agents/embedded-agent-runner/google-prompt-cache.ts, serialized state: src/agents/embedded-agent-runner/google-prompt-cache.ts. Confirm migration or upgrade compatibility proof before merge.

Merge-risk options

Maintainer options:

  1. Decide the mitigation before merge
    Keep the parse-owner wrapper, assert its contextual prefix rather than the generic parser text, preserve the original error as its cause, and add redacted provider-boundary proof.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Technical review

Best possible solution:

Keep the parse-owner wrapper, assert its contextual prefix rather than the generic parser text, preserve the original error as its cause, and add redacted provider-boundary proof.

Do we have a high-confidence way to reproduce the issue?

Yes at source level: a successful cache response with malformed JSON reaches the raw parser through cache creation. A live Google response-path reproduction was not supplied.

Is this the best way to solve the issue?

No. Catching at the parse owner is the right shape, but the current test does not prove the new contextual message; preserve the original error as a cause while making the assertion specific.

Full review comments:

  • [P2] Assert the new contextual error message — src/agents/embedded-agent-runner/google-prompt-cache.test.ts:817
    The added matcher already passes on current main: JSON.parse("not-json-at-all") throws a message containing “not valid JSON.” Assert the Google prompt-cache prefix (and preferably retain/assert the original error as cause) so this regression test fails before the wrapper exists.
    Confidence: 0.99

Overall correctness: patch is incorrect
Overall confidence: 0.98

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 94cdb6c46e12.

Labels

Label changes:

  • add P3: This is a narrow diagnostics improvement in an optional provider cache path, with no demonstrated broader workflow failure.
  • add rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🦪 silver shellfish.
  • add status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: The PR body documents a mocked fetch/Vitest run; add redacted after-fix evidence through the real Google cache response boundary before merge. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Label justifications:

  • P3: This is a narrow diagnostics improvement in an optional provider cache path, with no demonstrated broader workflow failure.
  • rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🦪 silver shellfish.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: The PR body documents a mocked fetch/Vitest run; add redacted after-fix evidence through the real Google cache response boundary before merge. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Git history and blame assign the prompt-cache reader, its raw parse, and the wrapper installation path to the feature-introduction commit. (role: feature introducer and current-line author; confidence: high; commits: e51ee089b734; files: src/agents/embedded-agent-runner/google-prompt-cache.ts, src/agents/embedded-agent-runner/run/attempt-prompt-phase.ts)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@clawsweeper

clawsweeper Bot commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

ClawSweeper status: review started.

I am starting a fresh review of this pull request: fix(agents): wrap JSON.parse in readGooglePromptCacheJson with descriptive error This is item 3/3 in the current shard. Shard 23/41.

This placeholder means the worker is alive and reading the current context. I will edit this same comment with the actual review when the claws are done clicking.

Crustacean status: shell secured, claws on keyboard, evidence pebbles being sorted.

@coaiMax

coaiMax commented Aug 16, 2026

Copy link
Copy Markdown
Contributor Author

Closing in favor of newer PR with updated implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. size: XS status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant