@@ -413,13 +413,13 @@ void invocationRequestIsIsolatedFromTheCallerAndTheExecutor() {
413413 .context (callerContext ).attributes (callerContext ).build ();
414414 ModelCallInvocation invocation = new ModelCallInvocation (identity (), MODEL_TURN , request );
415415
416- // The invocation holds its own defensive copy , not the request the executor
417- // runs on: mutating what the observer can reach must not touch the caller's
418- // collections or the original request the dispatch keeps using .
416+ // The invocation holds its own read-only snapshot , not the request the executor
417+ // runs on: every collection accessor of the observed request rejects writes, so
418+ // an extension cannot rewrite what subsequent extensions observe .
419419 assertNotSame (request , invocation .request ());
420- invocation .request ().getMessages ().clear ();
421- invocation . request (). getContext (). put ( "k" , "rewritten" );
422- // attributes are transmitted as an immutable map: writes are rejected outright.
420+ assertThrows ( UnsupportedOperationException . class , () -> invocation .request ().getMessages ().clear () );
421+ assertThrows ( UnsupportedOperationException . class ,
422+ () -> invocation . request (). getContext (). put ( "k" , "rewritten" ));
423423 assertThrows (UnsupportedOperationException .class ,
424424 () -> invocation .request ().getAttributes ().put ("k" , "rewritten" ));
425425 assertEquals ("v" , callerContext .get ("k" ));
@@ -429,6 +429,39 @@ void invocationRequestIsIsolatedFromTheCallerAndTheExecutor() {
429429 assertEquals ("v" , request .getContext ().get ("k" ));
430430 }
431431
432+ // The event is a snapshot: later changes to the caller's collections are invisible
433+ // to extensions that observe the same invocation.
434+ @ Test
435+ void invocationSnapshotIsUnaffectedByLaterCallerMutations () {
436+ List <ChatMessage > callerMessages = new ArrayList <>();
437+ callerMessages .add (ChatMessage .user ("hello" ));
438+ Map <String , Object > callerContext = new HashMap <>();
439+ callerContext .put ("k" , "v" );
440+ ModelRequest request = ModelRequest .builder ().messages (callerMessages ).context (callerContext ).build ();
441+ ModelCallInvocation invocation = new ModelCallInvocation (identity (), MODEL_TURN , request );
442+
443+ callerMessages .clear ();
444+ callerMessages .add (ChatMessage .user ("replaced" ));
445+ callerContext .put ("k" , "rewritten" );
446+
447+ assertEquals ("hello" , invocation .request ().getMessages ().get (0 ).text ());
448+ assertEquals (1 , invocation .request ().getMessages ().size ());
449+ assertEquals ("v" , invocation .request ().getContext ().get ("k" ));
450+ }
451+
452+ // A request rebuilt from the read-only view (the transformer path) still carries
453+ // the full content, so the view does not break legitimate transforms.
454+ @ Test
455+ void aTransformRebuiltFromTheReadOnlyViewKeepsTheContent () {
456+ ModelCallInvocation invocation = modelCall ();
457+ ModelRequest rebuilt = ModelRequest .builder (invocation .request ())
458+ .systemMessage (ChatMessage .system ("extra instruction" ))
459+ .build ();
460+
461+ assertEquals (invocation .request ().getMessages (), rebuilt .getMessages ());
462+ assertEquals ("extra instruction" , rebuilt .getSystemMessage ().text ());
463+ }
464+
432465 @ Test
433466 void mutationGuardRejectsTrustedExecutionContextSwap () {
434467 TrustedExecutionContext tenantA = TrustedExecutionContext .builder ()
0 commit comments