Skip to content

Fix malformed JSON in Anthropic code-execution deltas - #6918

Merged
tim-smart merged 2 commits into
mainfrom
audit/repro-split-adapters-ai-2-anthropic-malformed-code-delta
Aug 3, 2026
Merged

Fix malformed JSON in Anthropic code-execution deltas#6918
tim-smart merged 2 commits into
mainfrom
audit/repro-split-adapters-ai-2-anthropic-malformed-code-delta

Conversation

@fubhy

@fubhy fubhy commented Aug 3, 2026

Copy link
Copy Markdown
Member

Summary

The first streamed code-execution parameter delta is invalid JSON and poisons both incremental parsing and the accumulated final parameters.

Important

This PR starts with focused failing reproduction tests. Add the implementation fix to this same branch; CI is expected to fail until that fix is included.

Anthropic code-execution deltas contain malformed JSON

Module: ai/anthropic/AnthropicLanguageModel
Audit ID: adapters-ai-2-anthropic-malformed-code-delta
Severity / confidence: high / high

What happens

The first streamed code-execution parameter delta is invalid JSON and poisons both incremental parsing and the accumulated final parameters.

Why it happens

The rewrite interpolates providerName without JSON quotes, producing values such as {"type":bash_code_execution,...}.

Expected behavior

tool-params-delta must contain incremental JSON, and the code-execution type discriminator is a JSON string.

Relevant implementation

These links and excerpts are pinned to audit base c9b56ab507f224426ee8388dc450da447ec4715f.

View problematic code at packages/ai/anthropic/src/AnthropicLanguageModel.ts:2573-2588
                if (
                  contentBlock.firstDelta &&
                  (contentBlock.providerName === "bash_code_execution" ||
                    contentBlock.providerName === "text_editor_code_execution")
                ) {
                  delta = `{"type":${contentBlock.providerName},${delta.substring(1)}}`
                }

                parts.push({
                  type: "tool-params-delta",
                  id: contentBlock.id,
                  delta
                })

                contentBlock.params += delta
                contentBlock.firstDelta = false

View exact lines on GitHub

Reproduction

pnpm test --run packages/ai/anthropic/test/AnthropicMalformedCodeDeltaAudit.test.ts

Observed failure: The emitted delta begins with malformed JSON such as {"type":bash_code_execution,..., causing JSON.parse to throw.

Implementation handoff

The initial reproduction tests on this branch are the regression specification for the implementation fix that should follow in this PR.

  1. Start with the pinned implementation excerpts and the Why it happens analysis above.
  2. Change the implementation so it satisfies the stated Expected behavior; do not weaken or remove the reproduction assertions.
  3. Run the focused reproduction command(s) and confirm the observed failures become passing tests:
pnpm test --run packages/ai/anthropic/test/AnthropicMalformedCodeDeltaAudit.test.ts
  1. Run the affected package's existing tests, then the repository lint and type checks before requesting review.

Audit provenance

  • Audit base: c9b56ab507f224426ee8388dc450da447ec4715f
  • Reproduction base: 8f9499f562729f5f7b08d8bcc4db86b4aeff8a21
  • Findings: adapters-ai-2-anthropic-malformed-code-delta
  • Initial patch: focused reproduction tests; implementation fix pending

Closes EFF-340

@github-project-automation github-project-automation Bot moved this to Discussion Ongoing in PR Backlog Aug 3, 2026
@changeset-bot

changeset-bot Bot commented Aug 3, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5650b8a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 30 packages
Name Type
@effect/ai-anthropic Patch
effect Patch
@effect/opentelemetry Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-deno Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/vitest Patch
@effect/ai-openai-compat Patch
@effect/ai-openai Patch
@effect/ai-openrouter Patch
@effect/atom-react Patch
@effect/atom-solid Patch
@effect/atom-vue Patch
@effect/sql-clickhouse Patch
@effect/sql-d1 Patch
@effect/sql-libsql Patch
@effect/sql-mssql Patch
@effect/sql-mysql2 Patch
@effect/sql-pg Patch
@effect/sql-pglite Patch
@effect/sql-sqlite-bun Patch
@effect/sql-sqlite-do Patch
@effect/sql-sqlite-node Patch
@effect/sql-sqlite-react-native Patch
@effect/sql-sqlite-wasm Patch
@effect/docgen Patch
@effect/doctest Patch
@effect/openapi-generator Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@effect-slopcop effect-slopcop Bot added 4.0 bug Something isn't working labels Aug 3, 2026

@pullfrog pullfrog 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.

Important

The test is missing stream termination events that the existing test patterns consistently include. The implementation fix is also not yet on this branch.

Reviewed changes — a single new reproduction test for the malformed code-execution delta bug.

  • Reproduction test — sets up a mocked SSE stream with a server_tool_use / bash_code_execution content block and asserts the emitted tool-params-delta contains valid JSON with the type discriminator as a string.

⚠️ Missing stream termination events

The test SSE events end after content_block_delta. The existing stream-text test in AnthropicLanguageModel.test.ts consistently includes content_block_stop, message_delta, and message_stop after the delta. While Stream.takeUntil at AnthropicClient.ts:309 may allow the stream to end when the response body is consumed, the message_stop event is the canonical termination signal and content_block_stop emits tool-params-end and processes final params. Omitting these events is inconsistent with the existing test patterns and could lead to incomplete or flaky stream behavior.

Technical details
# Missing stream termination events

## Affected sites
- `packages/ai/anthropic/test/AnthropicMalformedCodeDeltaAudit.test.ts:54` — after the last SSE event, add `content_block_stop`, `message_delta`, and `message_stop` events matching the existing test pattern at `AnthropicLanguageModel.test.ts:57-76`

## Required outcome
- The test should include `content_block_stop` (index 0), `message_delta` (with `stop_reason: "tool_use"`), and `message_stop` events to properly terminate the stream.

⚠️ Missing text_editor_code_execution coverage

The bug at AnthropicLanguageModel.ts:2575-2576 affects both bash_code_execution and text_editor_code_execution provider names, but the test only covers bash_code_execution. Consider adding a second test case or parameterizing the existing one to cover both code paths.

Pullfrog  | Fix all ➔Fix 👍s ➔View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) (Kimi K2 not used — the program covers this model; add its provider key to run your pick) | 𝕏

Comment thread packages/ai/anthropic/test/AnthropicMalformedCodeDeltaAudit.test.ts Outdated
Comment thread packages/ai/anthropic/test/AnthropicMalformedCodeDeltaAudit.test.ts Outdated
Comment thread packages/ai/anthropic/test/AnthropicMalformedCodeDeltaAudit.test.ts Outdated
@fubhy fubhy added the audit Findings originating from the Effect runtime correctness audit label Aug 3, 2026
@tim-smart
tim-smart enabled auto-merge (squash) August 3, 2026 21:31

@pullfrog pullfrog 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.

✅ No new issues found.

Reviewed changes — what changed since the prior Pullfrog review at 9e605f9:

  • Delta interpolation fix — wrapped contentBlock.providerName in JSON.stringify() so the type discriminator is a quoted JSON string, and removed the spurious trailing } from the template literal (the delta.substring(1) already contributes the closing brace).
  • providerExecuted final-params fix — the providerExecuted === true branch now parses finalParams via Tool.unsafeSecureJsonParse, matching the symmetry of the transformToolCallParams branch which already parsed it.
  • Test consolidation and expansion — the standalone audit test file was removed; the tests now live inline in AnthropicLanguageModel.test.ts as parameterized for-loop cases covering both bash_code_execution and text_editor_code_execution. Each test asserts valid JSON in the streamed delta and in the accumulated final tool-call params, and the mock SSE stream includes content_block_stop, message_delta, and message_stop termination events.
  • Changeset — patch-level entry for @effect/ai-anthropic.

All three prior review threads (stream termination, text_editor_code_execution coverage, SSE helper pattern) are retired as addressed.

Pullfrog  | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) (Kimi K2 not used — the program covers this model; add its provider key to run your pick) | 𝕏

@tim-smart
tim-smart merged commit a913136 into main Aug 3, 2026
17 checks passed
@tim-smart
tim-smart deleted the audit/repro-split-adapters-ai-2-anthropic-malformed-code-delta branch August 3, 2026 21:46
@github-project-automation github-project-automation Bot moved this from Discussion Ongoing to Done in PR Backlog Aug 3, 2026
@github-actions

github-actions Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Bundle Size Analysis

Generated from PR build output; treat the content below as untrusted.

File Name Current Size Previous Size Difference
basic.ts 7.06 KB 7.06 KB 0.00 KB (0.00%)
batching.ts 9.86 KB 9.86 KB 0.00 KB (0.00%)
brand.ts 6.34 KB 6.34 KB 0.00 KB (0.00%)
cache.ts 10.62 KB 10.62 KB 0.00 KB (0.00%)
config.ts 20.60 KB 20.60 KB 0.00 KB (0.00%)
differ.ts 20.20 KB 20.20 KB 0.00 KB (0.00%)
http-client.ts 21.49 KB 21.49 KB 0.00 KB (0.00%)
logger.ts 10.76 KB 10.76 KB 0.00 KB (0.00%)
metric.ts 8.99 KB 8.99 KB 0.00 KB (0.00%)
optic.ts 7.18 KB 7.18 KB 0.00 KB (0.00%)
pubsub.ts 14.90 KB 14.90 KB 0.00 KB (0.00%)
queue.ts 11.58 KB 11.58 KB 0.00 KB (0.00%)
schedule.ts 10.74 KB 10.74 KB 0.00 KB (0.00%)
schema-class.ts 19.14 KB 19.14 KB 0.00 KB (0.00%)
schema-fromJsonSchemaDocument.ts 28.96 KB 28.96 KB 0.00 KB (0.00%)
schema-representation-roundtrip.ts 25.29 KB 25.29 KB 0.00 KB (0.00%)
schema-string-transformation.ts 13.30 KB 13.30 KB 0.00 KB (0.00%)
schema-string.ts 10.94 KB 10.94 KB 0.00 KB (0.00%)
schema-template-literal.ts 15.17 KB 15.17 KB 0.00 KB (0.00%)
schema-toArbitraryLazy.ts 21.94 KB 21.94 KB 0.00 KB (0.00%)
schema-toCodeDocument.ts 24.34 KB 24.34 KB 0.00 KB (0.00%)
schema-toCodecJson.ts 19.18 KB 19.18 KB 0.00 KB (0.00%)
schema-toEquivalence.ts 19.01 KB 19.01 KB 0.00 KB (0.00%)
schema-toFormatter.ts 18.87 KB 18.87 KB 0.00 KB (0.00%)
schema-toJsonSchemaDocument.ts 22.60 KB 22.60 KB 0.00 KB (0.00%)
schema-toRepresentation.ts 19.52 KB 19.52 KB 0.00 KB (0.00%)
schema.ts 18.41 KB 18.41 KB 0.00 KB (0.00%)
stm.ts 12.54 KB 12.54 KB 0.00 KB (0.00%)
stream.ts 9.79 KB 9.79 KB 0.00 KB (0.00%)

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

Labels

4.0 audit Findings originating from the Effect runtime correctness audit bug Something isn't working

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants