Skip to content

_approvalPersistedMessageId is not safe for concurrent streams #921

@threepointone

Description

@threepointone

Problem

_approvalPersistedMessageId is a single instance field on AIChatAgent. If two streams are active simultaneously and both trigger tool-approval-request, the second write overwrites the first, and stream completion for the first stream would use the wrong ID.

Current mitigation

This doesn't happen in practice because:

  • _streamingMessage is also a single reference (same serialization assumption)
  • _tryCatchChat serializes stream handling
  • Approval pauses the stream, so concurrent streaming is unlikely

If concurrent streams are ever supported

Change _approvalPersistedMessageId from string | null to a Map<string, string> keyed by stream ID or message ID. The _streamSSEReply method would set the entry, and the completion block in _reply would look up and remove it.

Context

Introduced in the approval persistence fix (persist streaming message immediately when a tool enters approval-requested state so the approval UI survives page refresh).

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions