@@ -462,6 +462,50 @@ void aTransformRebuiltFromTheReadOnlyViewKeepsTheContent() {
462462 assertEquals ("extra instruction" , rebuilt .getSystemMessage ().text ());
463463 }
464464
465+ // issue #298 review, second round: collection values nested inside the context are
466+ // frozen too — clearing such a list through the event must fail and must not
467+ // touch the caller's list.
468+ @ Test
469+ void nestedContextValuesAreFrozenInTheSnapshot () {
470+ List <String > shared = new ArrayList <>(List .of ("a" , "b" ));
471+ Map <String , Object > callerContext = new HashMap <>();
472+ callerContext .put ("history" , shared );
473+ callerContext .put ("flags" , new HashMap <>(Map .of ("k" , "v" )));
474+ ModelRequest request = ModelRequest .builder ().context (callerContext ).build ();
475+ ModelCallInvocation invocation = new ModelCallInvocation (identity (), MODEL_TURN , request );
476+
477+ // The observed nested list is a frozen copy: writes are rejected...
478+ @ SuppressWarnings ("unchecked" )
479+ List <String > observed = (List <String >) invocation .request ().getContext ().get ("history" );
480+ assertThrows (UnsupportedOperationException .class , observed ::clear );
481+ assertThrows (UnsupportedOperationException .class , () -> observed .add ("c" ));
482+ // ...and the caller's list keeps its content and independence.
483+ assertEquals (List .of ("a" , "b" ), shared );
484+ shared .clear ();
485+ assertEquals (List .of ("a" , "b" ), invocation .request ().getContext ().get ("history" ));
486+
487+ // The same freeze applies to nested maps.
488+ @ SuppressWarnings ("unchecked" )
489+ Map <String , Object > observedFlags = (Map <String , Object >) invocation .request ().getContext ().get ("flags" );
490+ assertThrows (UnsupportedOperationException .class , () -> observedFlags .put ("k" , "rewritten" ));
491+ }
492+
493+ // A collection-valued attribute is frozen the same way as a context entry.
494+ @ Test
495+ void nestedAttributeValuesAreFrozenInTheSnapshot () {
496+ List <String > shared = new ArrayList <>(List .of ("x" ));
497+ Map <String , Object > callerAttributes = new HashMap <>();
498+ callerAttributes .put ("trace" , shared );
499+ ModelRequest request = ModelRequest .builder ().attributes (callerAttributes ).build ();
500+ ModelCallInvocation invocation = new ModelCallInvocation (identity (), MODEL_TURN , request );
501+
502+ @ SuppressWarnings ("unchecked" )
503+ List <String > observed = (List <String >) invocation .request ().getAttributes ().get ("trace" );
504+ assertThrows (UnsupportedOperationException .class , observed ::clear );
505+ shared .add ("y" );
506+ assertEquals (List .of ("x" ), invocation .request ().getAttributes ().get ("trace" ));
507+ }
508+
465509 @ Test
466510 void mutationGuardRejectsTrustedExecutionContextSwap () {
467511 TrustedExecutionContext tenantA = TrustedExecutionContext .builder ()
0 commit comments