Skip to content

Commit d6e6d16

Browse files
committed
fix(agent): freeze collection values nested in invocation context and attributes (issue #298 review)
1 parent 8ef2a88 commit d6e6d16

2 files changed

Lines changed: 75 additions & 5 deletions

File tree

‎argi-agent/argi-agent-api/src/main/java/io/github/agentic/ai/agent/lifecycle/ModelCallInvocation.java‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
import io.github.agentic.ai.api.chat.ChatMessage;
2020

2121
import java.util.Collections;
22+
import java.util.LinkedHashMap;
2223
import java.util.List;
2324
import java.util.Map;
2425
import java.util.Objects;
26+
import java.util.Set;
2527

2628
/**
2729
* A model call in the neutral lifecycle contract.
@@ -56,20 +58,44 @@ public record ModelCallInvocation(LifecycleIdentity identity, ModelTurnId modelT
5658
* event: the snapshot is taken from the caller's request, so later changes to the
5759
* caller's collections are invisible; and the snapshot's collection accessors are
5860
* immutable views, so an extension cannot rewrite what subsequent extensions observe
59-
* (clearing the message list or mutating the context map fails). Transforming
60-
* extensions rebuild a replacement through {@link ModelRequest#builder(ModelRequest)};
61-
* the executor keeps running on the request it holds, so the event never becomes a
61+
* (clearing the message list or mutating the context map fails). Collection-valued
62+
* context and attribute entries are frozen as well, so a nested list or map shared
63+
* with the caller cannot become the mutation channel either. Transforming extensions
64+
* rebuild a replacement through {@link ModelRequest#builder(ModelRequest)}; the
65+
* executor keeps running on the request it holds, so the event never becomes a
6266
* mutation channel into dispatch.
6367
*/
6468
private static ModelRequest readOnlySnapshot(ModelRequest request) {
69+
Map<String, Object> context = new LinkedHashMap<>();
70+
request.getContext().forEach((key, value) -> context.put(key, frozen(value)));
71+
Map<String, Object> attributes = new LinkedHashMap<>();
72+
request.getAttributes().forEach((key, value) -> attributes.put(key, frozen(value)));
6573
ModelRequest snapshot = ModelRequest.builder(request)
6674
.messages(List.copyOf(request.getMessages()))
67-
.context(Map.copyOf(request.getContext()))
68-
.attributes(Map.copyOf(request.getAttributes()))
75+
.context(context)
76+
.attributes(attributes)
6977
.build();
7078
return new ReadOnlyModelRequest(snapshot);
7179
}
7280

81+
/**
82+
* Freezes the supported mutable value types. Values of other types (the executor's
83+
* own objects, provider clients, builders) are shared by reference: the contract
84+
* freezes what an extension can reach through collection accessors.
85+
*/
86+
private static Object frozen(Object value) {
87+
if (value instanceof List<?> list) {
88+
return List.copyOf(list);
89+
}
90+
if (value instanceof Map<?, ?> map) {
91+
return Map.copyOf(map);
92+
}
93+
if (value instanceof Set<?> set) {
94+
return Set.copyOf(set);
95+
}
96+
return value;
97+
}
98+
7399
/**
74100
* A request view whose editable collections are exposed as immutable. The framework
75101
* keeps per-subscription {@link ModelRequest} copies mutable by design (subscription

‎argi-agent/argi-agent-api/src/test/java/io/github/agentic/ai/agent/lifecycle/LifecycleContractTest.java‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)