Streaming hardening: cancellation leak + concurrent-composition name collision (#4499, #4500)#200
Open
Skobeltsyn wants to merge 2 commits into
Open
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
AgentSessiondocumented that cancellingeventscollection cancels the underlying invocation. It didn't: the producer ran in a detachedSupervisorJob+Dispatchers.Unconfinedscope the collector couldn't reach, so a cancelled — ortake(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
onCompletionstage is skipped when the collecting coroutine is cancelled externally. Fix puts the scope teardown in the events flow's ownflow {} finally, which fires on normal completion,take(1), AND external cancellation;await()cancels the same scope on its own cancellation. Bare-cancellation contract (no syntheticFailed) preserved. Both session producers covered.#4500 — concurrent composition rejects duplicate agent names
Parallel(/) andForum(*) demux streamed events byagentId(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), andawait()-cancel each tear down a parkedchatStreaminvocation.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