embabel-agent icon indicating copy to clipboard operation
embabel-agent copied to clipboard

A2A Stack — Thread Safety & Correctness Cleanup

Open igordayen opened this issue 2 months ago • 4 comments

A2A Stack — Thread Safety & Correctness Cleanup

Context

The A2A server/client stack has several thread-safety issues, dead code, and protocol correctness bugs identified during integration test work. None of these are breaking for single-threaded usage but will cause failures under concurrent load or on server-side errors.


Thread Safety — TaskStateManager.kt

  • TaskInfo.events: MutableList<StreamingEventKind> — not thread-safe; concurrent recordEvent calls can corrupt the list. Fix: replace with CopyOnWriteArrayList.

  • updateStreamId() — non-atomic read-copy-write; two concurrent resubscriptions can clobber each other. Fix: ConcurrentHashMap.compute.

  • var completedAt, var currentState — mutated without synchronization. Fix: @Volatile or fold into a single atomic state object.

  • Apply import javax.annotation.concurrent.ThreadSafe and see /embabel-agent/embabel-agent-api/src/test/kotlin/architecture/ThreadSafetyRulesTest.kt


    Dead Code — A2AStreamingHandler.kt

    • private val scheduler: ScheduledExecutorService — allocated on startup, never used, never shut down → thread leak. Fix: remove the field and the shutdown() method.

    Protocol Correctness — AutonomyA2ARequestHandler.kt

    • Line 111: .single() on message parts — crashes on multi-part messages. Fix: .firstOrNull()?.text ?: "".

    • isFinal(true) missing on error path — error TaskStatusUpdateEvent in the handleMessageStream catch block lacks isFinal(true); client latch releases via onComplete(null) → NPE in any caller on server-side error. Fix: add .isFinal(true) to the error event builder.

    • handleTasksGet / handleCancelTask: TODO() — throws NotImplementedError → Sonar block; tasks/get also cannot find non-streaming tasks since handleMessageSend never registers them in TaskStateManager. Fix: implement tasks/get via TaskStateManager; register task on entry in handleMessageSend; return TaskNotCancelableError for tasks/cancel.

    • Wrong error type in handleMessageSend catch — TaskNotFoundError used for internal exceptions. Fix: use InternalError.

    • "Processing task…" SSE event ordering — sent after chooseAndRunAgent() completes, not before. Fix: move before the agent call.

    • // TODO comments on lines 110, 139, 342 → Sonar fail. Fix: resolve or remove.

igordayen avatar Sep 20 '26 04:09 igordayen

@hadirsa @abadongutierrez. @cpage-pivotal, @alexheifetz - looping all involved in A2A stack.

Please let me know if you have bandwidth to work on it.

I think A2A requires both cleanup and validation to align with the most recent A2A standard, and server TCK conformance testing.

I'm working on the Embabel A2A client and multi-agent context propagation; I'll release it soon.

Looping @azanux

Thanks

igordayen avatar Sep 20 '26 04:09 igordayen

Hi @igordayen, I hope you're doing well. I can take this. I'll investigate and come back shortly.

To avoid conflicts:

  • #2038 changes AutonomyA2ARequestHandler. Is it close to merging? Should I branch from main and rebase later, build on top of it, or wait for it to land?

Thanks

hadirsa avatar Sep 29 '26 22:09 hadirsa

@hadirsa -thank you very much for helping. Could you please proceed with #2038 approval/merger? Will keep you updated. thanks again

igordayen avatar Sep 30 '26 02:09 igordayen

Sure, I’ll take a look and handle the review. @igordayen

hadirsa avatar Sep 30 '26 06:09 hadirsa