Skip to content

Commit 8ef2a88

Browse files
committed
fix(agent): expose a genuinely read-only request snapshot on model call invocations (issue #298 review)
1 parent 3956636 commit 8ef2a88

2 files changed

Lines changed: 79 additions & 15 deletions

File tree

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

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
package io.github.agentic.ai.agent.lifecycle;
1717

1818
import io.github.agentic.ai.agent.interceptor.ModelRequest;
19+
import io.github.agentic.ai.api.chat.ChatMessage;
1920

21+
import java.util.Collections;
2022
import java.util.List;
2123
import java.util.Map;
2224
import java.util.Objects;
@@ -46,23 +48,52 @@ public record ModelCallInvocation(LifecycleIdentity identity, ModelTurnId modelT
4648
Objects.requireNonNull(identity, "identity must not be null");
4749
Objects.requireNonNull(modelTurn, "modelTurn must not be null");
4850
Objects.requireNonNull(request, "request must not be null");
49-
request = defensiveCopy(request);
51+
request = readOnlySnapshot(request);
5052
}
5153

5254
/**
53-
* Returns a copy of the request whose collection-valued accessors are immutable:
54-
* observers must not be able to rewrite the execution input (clearing the message
55-
* list or mutating the context map) through the invocation reference. Transforming
56-
* extensions observe this copy and rebuild a replacement from it; the executor
57-
* keeps running on the original request it holds, so a mutated copy would have no
58-
* effect on dispatch.
55+
* Returns a genuinely read-only snapshot of the request. Two layers protect the
56+
* event: the snapshot is taken from the caller's request, so later changes to the
57+
* caller's collections are invisible; and the snapshot's collection accessors are
58+
* 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
62+
* mutation channel into dispatch.
5963
*/
60-
private static ModelRequest defensiveCopy(ModelRequest request) {
61-
return ModelRequest.builder(request)
64+
private static ModelRequest readOnlySnapshot(ModelRequest request) {
65+
ModelRequest snapshot = ModelRequest.builder(request)
6266
.messages(List.copyOf(request.getMessages()))
6367
.context(Map.copyOf(request.getContext()))
6468
.attributes(Map.copyOf(request.getAttributes()))
6569
.build();
70+
return new ReadOnlyModelRequest(snapshot);
71+
}
72+
73+
/**
74+
* A request view whose editable collections are exposed as immutable. The framework
75+
* keeps per-subscription {@link ModelRequest} copies mutable by design (subscription
76+
* isolation); the lifecycle event is the read-only surface on top of a snapshot.
77+
*/
78+
private static final class ReadOnlyModelRequest extends ModelRequest {
79+
80+
private ReadOnlyModelRequest(ModelRequest source) {
81+
super(source.getSystemMessage(), source.getMessages(), source.getOptions(), source.getChatClient(),
82+
source.getToolRegistry(), source.getTools(), source.hasToolSelection(),
83+
source.getDynamicToolRegistry(), source.getToolDescriptions(), source.getContext(),
84+
source.getAttributes(), source.getTrustedExecutionContext().orElse(null));
85+
}
86+
87+
@Override
88+
public List<ChatMessage> getMessages() {
89+
return Collections.unmodifiableList(super.getMessages());
90+
}
91+
92+
@Override
93+
public Map<String, Object> getContext() {
94+
return Collections.unmodifiableMap(super.getContext());
95+
}
96+
6697
}
6798

6899
}

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

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

Comments
 (0)