Skip to content

fix(streaming): keep compaction metadata when a delta omits encrypted_content - #2050

Open
Lushiyao5 wants to merge 1 commit into
anthropics:mainfrom
Lushiyao5:fix/compaction-delta-metadata
Open

Lushiyao5 wants to merge 1 commit into
anthropics:mainfrom
Lushiyao5:fix/compaction-delta-metadata

Conversation

@Lushiyao5

Copy link
Copy Markdown

What

The beta streaming accumulator assigned encrypted_content from every compaction_delta. The generated delta model defaults an absent field to None, so a later content-only delta ({"type":"compaction_delta","content":"Updated summary."}) erased metadata received earlier in the same block. The accumulator now updates encrypted_content only when the field was actually present in the delta.

Why

encrypted_content is opaque metadata from a prior compaction that must be round-tripped verbatim. Treating "field omitted" as "clear the field" makes the value depend on the order of deltas: any content-only update silently drops it. A field that is explicitly sent must still take effect, including null (clear) and "" (replace with empty).

How

elif event.delta.type == "compaction_delta":
    if content.type == "compaction":
        content.content = event.delta.content
        if "encrypted_content" in event.delta.model_fields_set:
            content.encrypted_content = event.delta.encrypted_content

model_fields_set distinguishes an omitted field from an explicit null. Summary text accumulation is unchanged.

Tests

Added a parameterized test in tests/lib/streaming/test_parsed_content_blocks.py driving accumulate_event with a populated compaction block followed by a content-only delta, for four cases:

delta encrypted_content expected
omitted retains "opaque"
null None
"" ""
"new" "new"
  • Before: → 1 failed, 8 passed (only the omitted case fails).
  • After: → 9 passed.
  • ruff check and ruff format --check pass.

Fixes #1984

@Lushiyao5
Lushiyao5 requested a review from a team as a code owner October 31, 2026 22:21

This branch has not been deployed

No deployments
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.

A compaction delta without encrypted_content clears the accumulated metadata

1 participant