#6979·CopilotKit

Feature Request: incremental run event finalizer with bounded state

Author: itsklimovCreated Sep 9, 2026Updated Sep 17, 2026

Pre-flight Checklist

  • I have searched existing issues, and this hasn't been requested yet.

Problem or Motivation

finalizeRunEvents in @copilotkit/shared closes text messages and tool calls that never got their END or RESULT events when a stream ends abruptly. It works on the full event array.

The runners (in-memory, sqlite, intelligence) keep that array anyway, for persistence and replay, so the array form costs them nothing extra. handleSuggestAgent is different: it has no persistence, and it collects every provider event only so it can call finalizeRunEvents once at the end. A long or misbehaving provider stream holds every payload for the whole suggestion run for no other reason.

Tell us your idea

Add an incremental finalizer next to the array API and use it on the suggest path:

typescript
const finalizer = createRunEventFinalizer();
onEvent: ({ event }) => { finalizer.observe(event); subscriber.next(event); }
// on abrupt end:
const closers = finalizer.finalize({ stopRequested: false });
  • observe keeps only the ids of lifecycles that are still open and drops them as soon as they close. Event payloads are never retained.
  • finalizeRunEvents keeps its signature as the array form, so the runners do not change.

Would you like to work on this?

  • Yes, I’d love to work on it!
  • I’m open to collaborating but need guidance.
  • No, I’m just sharing the idea.