@@ -345,16 +345,20 @@ private void run() {
345345 context .control ().assertActive ();
346346 ToolRegistry availableTools = resolveTools (state );
347347 AtomicReference <ToolRegistry > effectiveTools = new AtomicReference <>(availableTools );
348- ChatResponse response = modelTurn (messages , state , usage , availableTools , effectiveTools );
349- if ( response . messages (). isEmpty ()) {
350- throw new IllegalStateException ( "Model response did not contain an assistant message" );
351- }
352- for ( ChatMessage message : response . messages ()) {
353- messages . add ( message );
354- emit ( new AgentEvent . Message ( "model-" + turn , message ) );
348+ ChatResponse response = modelTurn (messages , state , usage , availableTools , effectiveTools , turn );
349+ // The turn contract guarantees exactly one assistant message: direct
350+ // responses are validated by ModelResponse, and interceptor-supplied
351+ // streams are validated here, so the turn's complete snapshot and its
352+ // streamed deltas share exactly one invocation-local ID (issue #280).
353+ if ( response . messages (). size () != 1 || response . messages (). get ( 0 ). role () != ChatRole . ASSISTANT ) {
354+ throw new IllegalStateException ( "Model response must contain exactly one assistant message" );
355355 }
356+ ChatMessage message = response .messages ().get (0 );
357+ String messageId = modelMessageId (turn );
358+ messages .add (message );
359+ emit (new AgentEvent .Message (messageId , message ));
356360 if (response .toolCalls ().isEmpty ()) {
357- finish (messages , state , usage .value (), response . messages (). get ( response . messages (). size () - 1 ) );
361+ finish (messages , state , usage .value (), message , messageId );
358362 return ;
359363 }
360364 for (ToolCall call : response .toolCalls ()) {
@@ -366,14 +370,26 @@ private void run() {
366370 return ;
367371 }
368372 if (step .returnDirect ()) {
369- finish (messages , state , usage .value (), messages .get (messages .size () - 1 ));
373+ finish (messages , state , usage .value (), messages .get (messages .size () - 1 ),
374+ toolMessageId (call ));
370375 return ;
371376 }
372377 }
373378 }
374379 throw new IllegalStateException ("ReAct agent exceeded the configured turn limit" );
375380 }
376381
382+ // Message-ID scheme for the invocation-local AgentEvent contract (issue #280):
383+ // exactly one ID per logical message, shared by its streamed deltas and its
384+ // complete snapshot, centralized so the two can never drift apart again.
385+ private static String modelMessageId (int turn ) {
386+ return "model-" + turn ;
387+ }
388+
389+ private static String toolMessageId (ToolCall call ) {
390+ return "tool-" + call .id ();
391+ }
392+
377393 private ToolRegistry resolveTools (Map <String , Object > state ) {
378394 if (dynamicToolProvider == null ) {
379395 return tools ;
@@ -395,8 +411,13 @@ private ToolRegistry resolveTools(Map<String, Object> state) {
395411
396412 private ChatResponse modelTurn (List <ChatMessage > messages , Map <String , Object > state ,
397413 UsageAccumulator usage , ToolRegistry availableTools ,
398- AtomicReference <ToolRegistry > effectiveTools ) {
414+ AtomicReference <ToolRegistry > effectiveTools , int turn ) {
399415 context .control ().assertActive ();
416+ // Streamed deltas and the turn's complete snapshot share the invocation-local
417+ // model-<turn> ID (issue #280): a consumer assembles streamed text per
418+ // message ID and seals it with the same-ID snapshot, never with a fixed
419+ // stream marker shared by every turn.
420+ String streamMessageId = modelMessageId (turn );
400421 ModelRequest request = ModelRequest .builder ().messages (messages ).toolRegistry (availableTools )
401422 .trustedExecutionContext (context .trusted ())
402423 .executionGuard (context .control ())
@@ -406,15 +427,16 @@ private ChatResponse modelTurn(List<ChatMessage> messages, Map<String, Object> s
406427 // The effective tool set is only known at the end of the interceptor chain,
407428 // where selection semantics are resolved; execution in the same turn must use
408429 // exactly the set the model was shown (issue #268: declared and executed agree).
409- ModelCallHandler handler = modelRequest -> invokeModel (modelRequest , usage , effectiveTools );
430+ ModelCallHandler handler = modelRequest -> invokeModel (modelRequest , usage , effectiveTools ,
431+ streamMessageId );
410432 for (int index = interceptors .size () - 1 ; index >= 0 ; index --) {
411433 ModelInterceptor interceptor = interceptors .get (index );
412434 ModelCallHandler next = handler ;
413435 handler = current -> interceptor .interceptModel (current , next );
414436 }
415437 ModelResponse response = Objects .requireNonNull (handler .call (request ), "model interceptor returned no response" );
416438 if (response .isStreaming ()) {
417- return collectModelStream (response .getStream (), usage );
439+ return collectModelStream (response .getStream (), usage , streamMessageId );
418440 }
419441 return response .getChatResponse ();
420442 }
@@ -470,14 +492,14 @@ private List<ChatMessage> composeMessages(ModelRequest request) {
470492 }
471493
472494 private ModelResponse invokeModel (ModelRequest request , UsageAccumulator usage ,
473- AtomicReference <ToolRegistry > effectiveTools ) {
495+ AtomicReference <ToolRegistry > effectiveTools , String streamMessageId ) {
474496 ToolRegistry effective = effectiveToolSet (request );
475497 effectiveTools .set (effective );
476498 ChatClient selectedClient = request .getChatClient () != null ? request .getChatClient () : chatClient ;
477499 ChatRequest chatRequest = new ChatRequest (composeMessages (request ), effective .definitions (),
478500 request .getOptions (), request .getAttributes ());
479501 if (streamModel ) {
480- return ModelResponse .of (collectModelStream (selectedClient , chatRequest , usage ));
502+ return ModelResponse .of (collectModelStream (selectedClient , chatRequest , usage , streamMessageId ));
481503 }
482504 CompletableFuture <ChatResponse > future = selectedClient .call (chatRequest ).toCompletableFuture ();
483505 toolOperation .set (future );
@@ -495,12 +517,15 @@ private ModelResponse invokeModel(ModelRequest request, UsageAccumulator usage,
495517 }
496518 }
497519
498- private ChatResponse collectModelStream (ChatClient selectedClient , ChatRequest request , UsageAccumulator usage ) {
520+ private ChatResponse collectModelStream (ChatClient selectedClient , ChatRequest request , UsageAccumulator usage ,
521+ String messageId ) {
499522 return collectModelStream (
500- Objects .requireNonNull (selectedClient .stream (request ), "chat client returned no stream" ), usage );
523+ Objects .requireNonNull (selectedClient .stream (request ), "chat client returned no stream" ), usage ,
524+ messageId );
501525 }
502526
503- private ChatResponse collectModelStream (Flow .Publisher <ChatEvent > publisher , UsageAccumulator usage ) {
527+ private ChatResponse collectModelStream (Flow .Publisher <ChatEvent > publisher , UsageAccumulator usage ,
528+ String messageId ) {
504529 CompletableFuture <ChatResponse > future = new CompletableFuture <>();
505530 try (AutoCloseable ignored = context .control ().onLoss (() -> {
506531 Flow .Subscription subscription = modelSubscription .getAndSet (null );
@@ -534,7 +559,7 @@ public void onSubscribe(Flow.Subscription subscription) {
534559 public void onNext (ChatEvent event ) {
535560 context .control ().assertActive ();
536561 switch (event .type ()) {
537- case TEXT_DELTA -> emit (new AgentEvent .MessageDelta ("model-stream" , event .text ()));
562+ case TEXT_DELTA -> emit (new AgentEvent .MessageDelta (messageId , event .text ()));
538563 case USAGE -> emit (new AgentEvent .UsageUpdate (usage .valueIncluding (event .usage ())));
539564 case RESPONSE -> response = event .response ();
540565 case TOOL_CALL -> emit (new AgentEvent .Progress ("model.tool-call" , Map .of ("name" ,
@@ -632,7 +657,7 @@ private void appendToolResult(ToolCall call, ToolResult result, List<ChatMessage
632657 }
633658 ChatMessage message = result .toMessage (call );
634659 messages .add (message );
635- emit (new AgentEvent .Message ("tool-" + call . id ( ), message ));
660+ emit (new AgentEvent .Message (toolMessageId ( call ), message ));
636661 }
637662
638663 private void admitPatch (String toolName , ToolResult result , Map <String , Object > patch ,
@@ -666,7 +691,7 @@ private void admitPatch(String toolName, ToolResult result, Map<String, Object>
666691 }
667692
668693 private void finish (List <ChatMessage > messages , Map <String , Object > state , Usage usage ,
669- ChatMessage terminal ) {
694+ ChatMessage terminal , String terminalMessageId ) {
670695 Map <String , Object > outputs = new LinkedHashMap <>(state );
671696 if (outputSchema != null && outputFinalizer != null ) {
672697 AgentResult result = outputFinalizer .finalizeOutput (terminal , outputSchema , usage , context .control ());
@@ -676,11 +701,14 @@ private void finish(List<ChatMessage> messages, Map<String, Object> state, Usage
676701 // A successful repair replaces the delivered terminal: the message that
677702 // actually passed validation becomes the final message while the failed
678703 // original stays in history, and stream consumers see it through the
679- // complete-message event before the finished snapshot.
704+ // complete-message event before the finished snapshot. The repaired
705+ // message is emitted under the replaced terminal's ID (issue #280), so
706+ // an ID-keyed consumer view replaces the failed message and ends the
707+ // run consistent instead of accumulating a separate repair message.
680708 ChatMessage delivered = result .message ();
681709 if (delivered != null && !Objects .equals (delivered .text (), terminal .text ())) {
682710 messages .add (delivered );
683- emit (new AgentEvent .Message ("model-repair" , delivered ));
711+ emit (new AgentEvent .Message (terminalMessageId , delivered ));
684712 }
685713 if (result .data () != null ) {
686714 outputs .put ("structuredOutput" , result .data ());
0 commit comments