Skip to content

Streaming hardening: cancellation leak + concurrent-composition name collision (#4499, #4500)#200

Open
Skobeltsyn wants to merge 2 commits into
feat/antifragility-passfrom
fix/streaming-cancel-and-collision
Open

Streaming hardening: cancellation leak + concurrent-composition name collision (#4499, #4500)#200
Skobeltsyn wants to merge 2 commits into
feat/antifragility-passfrom
fix/streaming-cancel-and-collision

Conversation

@Skobeltsyn

Copy link
Copy Markdown
Contributor

Streaming hardening — two real defects found via TDD probes, fixed

Follow-up to #199: probing complex-composition streaming for collisions and leaks surfaced two genuine bugs. RED probe tests written first, then fixed.

#4499 — cancelling a session no longer leaks the invocation

AgentSession documented that cancelling events collection cancels the underlying invocation. It didn't: the producer ran in a detached SupervisorJob + Dispatchers.Unconfined scope the collector couldn't reach, so a cancelled — or take(1)-abandoned — consumer left the agent making model calls in the background (runaway-work / resource leak + broken contract).

Root cause confirmed by instrumentation: a downstream onCompletion stage is skipped when the collecting coroutine is cancelled externally. Fix puts the scope teardown in the events flow's own flow {} finally, which fires on normal completion, take(1), AND external cancellation; await() cancels the same scope on its own cancellation. Bare-cancellation contract (no synthetic Failed) preserved. Both session producers covered.

#4500 — concurrent composition rejects duplicate agent names

Parallel (/) and Forum (*) demux streamed events by agentId (the agent's name). Two participants sharing a name produced indistinguishable interleaved streams. The single-placement rule caught the same instance placed twice but not two distinct same-named instances — now construction fails loud with a message naming the duplicate, the same stance as duplicate tool/skill names. speculative(n) self-racing is the documented exception.

Tests (TDD, fail-first)

  • SessionCancellationLeakProbeTest (3) — collector-cancel mid-stream, take(1), and await()-cancel each tear down a parked chatStream invocation.
  • CompositionNameCollisionProbeTest (4) — duplicate names rejected in parallel/chained-parallel/forum; distinct names accepted.
  • ComplexStreamingTest (3) — deep chain (head → 3-way parallel → reducer), forum stage, loop stage: no collision (distinct agentIds), no loss (droppedEvents == 0), single terminal.

Gates: full ./gradlew test (all modules, TEST-*.xml clean) + ./gradlew build -x test (detekt) — both green. Docs: CHANGELOG, streaming.md (cancellation contract rewritten honestly + known-gap closures), composition.md, internals adjunct.

Stacked on feat/antifragility-pass (#199) since both touch the session files it modifies; retargets to main once #199 lands. CodeQL java-kotlin expected-red per #4383.

🤖 Generated with Claude Code

Skobeltsyn and others added 2 commits June 13, 2026 16:47
Parallel (/) and Forum (*) demux streamed events by agentId (the agent's
name); two participants sharing a name produced indistinguishable
interleaved streams. The single-placement rule caught the same instance
placed twice but not two distinct same-named instances. requireDistinctAgentNames
now fails loud at construction with a message naming the duplicate — the
same stance as duplicate tool/skill names. speculative(n) self-racing is
the documented exception. 4 tests.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
AgentSession promised cancelling events collection cancels the underlying
invocation; it didn't — the producer ran in a detached SupervisorJob +
Unconfined scope the collector couldn't reach, so a cancelled or take(1)-
abandoned consumer left the agent making model calls in the background.

Fix ties the producer scope to collection: the events flow is now a
flow{} whose finally cancels the scope — it runs on normal completion,
take(1), AND external collector cancellation (a downstream onCompletion
stage is skipped when the collector is cancelled from outside, confirmed
by instrumentation). await() cancels the same scope on its own
cancellation. Bare-cancellation contract (no synthetic Failed) preserved.

Adds SessionCancellationLeakProbeTest (3) + ComplexStreamingTest (deep
chain / forum / loop: no collision, droppedEvents==0, single terminal).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.

1 participant