Skip to content

docs(schema): warn against in-place mutation of shared stream elements#1074

Open
hi-pender wants to merge 1 commit into
alpha/10from
docs/stream-element-cow
Open

docs(schema): warn against in-place mutation of shared stream elements#1074
hi-pender wants to merge 1 commit into
alpha/10from
docs/stream-element-cow

Conversation

@hi-pender

Copy link
Copy Markdown
Contributor

What this PR does

Adds GoDoc warnings about a recurring concurrency pitfall: StreamReader.Copy fan-out duplicates readers, not elements, so pointer elements (e.g. *schema.Message) are shared across branches and may be consumed concurrently. Mutating a received message in place — especially map fields like msg.Extra[k] = v — causes concurrent map read/write panics.

Comment-only change; no behavior change.

Where the guidance was added

  • schema/stream.goStreamReader.Copy: copies share the same element values; treat received elements as read-only, use copy-on-write to modify
  • schema/stream.goStreamReaderWithConvert: the convert callback must not modify its input in place; return a new value (shallow-copy + clone changed map/slice fields)
  • schema/message.goMessage.Extra: never write to Extra on a message you did not create; copy-on-write steps
  • schema/agentic_message.goAgenticMessage.Extra: same guidance
  • adk/handler.goTypedChatModelAgentMiddleware.WrapModel: wrappers transforming the response stream must shallow-copy + clone instead of writing in place

Why

Users writing stream convert callbacks or ADK middleware commonly attach metadata via msg.Extra[k] = v on messages received from streams, which intermittently panics under fan-out. The doc comments now steer users to the copy-on-write pattern at every place they're likely to look.

🤖 Generated with Claude Code

StreamReader.Copy duplicates readers, not elements, so pointer elements
(e.g. *Message) are shared across fan-out branches and may be consumed
concurrently. In-place writes to map fields such as Message.Extra cause
concurrent map read/write panics.

Add GoDoc guidance at the places users are most likely to hit this:
- StreamReader.Copy: copies share elements; treat them as read-only
- StreamReaderWithConvert: convert must not modify its input in place
- Message.Extra / AgenticMessage.Extra: copy-on-write instructions
- adk WrapModel: stream-transform wrappers must return copies

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@codecov

codecov Bot commented Jun 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (alpha/10@12955df). Learn more about missing BASE report.

Additional details and impacted files
@@             Coverage Diff             @@
##             alpha/10    #1074   +/-   ##
===========================================
  Coverage            ?   83.07%           
===========================================
  Files               ?      172           
  Lines               ?    28228           
  Branches            ?        0           
===========================================
  Hits                ?    23449           
  Misses              ?     3222           
  Partials            ?     1557           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant