Skip to content

Commit 3956636

Browse files
committed
fix(agent): isolate observed model requests and preserve the trusted execution context (issue #298 review)
1 parent 34252f0 commit 3956636

3 files changed

Lines changed: 114 additions & 0 deletions

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ public static Optional<String> check(LifecycleEvent original, LifecycleEvent rew
6363
if (!before.invocation().modelTurn().equals(after.invocation().modelTurn())) {
6464
return Optional.of("model turn identity must not change");
6565
}
66+
// The trusted execution context is run identity: a replacement that swaps
67+
// the tenant, principal or budget (or nulls it out) must be rejected even
68+
// when the lifecycle IDs are preserved.
69+
if (!java.util.Objects.equals(before.invocation().request().getTrustedExecutionContext(),
70+
after.invocation().request().getTrustedExecutionContext())) {
71+
return Optional.of("trusted execution context must not change");
72+
}
6673
return Optional.empty();
6774
}
6875
if (original instanceof LifecycleEvent.ToolCallRequested before) {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import io.github.agentic.ai.agent.interceptor.ModelRequest;
1919

20+
import java.util.List;
21+
import java.util.Map;
2022
import java.util.Objects;
2123

2224
/**
@@ -44,6 +46,23 @@ public record ModelCallInvocation(LifecycleIdentity identity, ModelTurnId modelT
4446
Objects.requireNonNull(identity, "identity must not be null");
4547
Objects.requireNonNull(modelTurn, "modelTurn must not be null");
4648
Objects.requireNonNull(request, "request must not be null");
49+
request = defensiveCopy(request);
50+
}
51+
52+
/**
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.
59+
*/
60+
private static ModelRequest defensiveCopy(ModelRequest request) {
61+
return ModelRequest.builder(request)
62+
.messages(List.copyOf(request.getMessages()))
63+
.context(Map.copyOf(request.getContext()))
64+
.attributes(Map.copyOf(request.getAttributes()))
65+
.build();
4766
}
4867

4968
}

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

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
package io.github.agentic.ai.agent.lifecycle;
1717

1818
import io.github.agentic.ai.agent.interceptor.ModelRequest;
19+
import io.github.agentic.ai.runtime.execution.ExecutionBudget;
20+
import io.github.agentic.ai.runtime.execution.DelegationScope;
21+
import io.github.agentic.ai.runtime.execution.PrincipalReference;
22+
import io.github.agentic.ai.runtime.execution.TenantReference;
23+
import io.github.agentic.ai.runtime.execution.TrustedExecutionContext;
1924
import io.github.agentic.ai.agent.interceptor.ModelResponse;
2025
import io.github.agentic.ai.api.chat.ChatMessage;
2126
import java.io.IOException;
@@ -24,7 +29,9 @@
2429
import java.lang.reflect.Modifier;
2530
import java.nio.charset.StandardCharsets;
2631
import java.util.ArrayList;
32+
import java.util.HashMap;
2733
import java.util.List;
34+
import java.util.Map;
2835
import java.util.Optional;
2936
import java.util.Set;
3037
import java.util.concurrent.atomic.AtomicInteger;
@@ -33,6 +40,7 @@
3340
import static org.junit.jupiter.api.Assertions.assertEquals;
3441
import static org.junit.jupiter.api.Assertions.assertFalse;
3542
import static org.junit.jupiter.api.Assertions.assertNotNull;
43+
import static org.junit.jupiter.api.Assertions.assertNotSame;
3644
import static org.junit.jupiter.api.Assertions.assertThrows;
3745
import static org.junit.jupiter.api.Assertions.assertTrue;
3846

@@ -393,6 +401,86 @@ void modelCallContractCarriesTheTrustedContextUnchanged() {
393401
assertEquals("model unavailable", failed.error());
394402
}
395403

404+
// issue #298 review: request isolation and trusted-context preservation
405+
406+
@Test
407+
void invocationRequestIsIsolatedFromTheCallerAndTheExecutor() {
408+
List<ChatMessage> callerMessages = new ArrayList<>();
409+
callerMessages.add(ChatMessage.user("hello"));
410+
Map<String, Object> callerContext = new HashMap<>();
411+
callerContext.put("k", "v");
412+
ModelRequest request = ModelRequest.builder().messages(callerMessages)
413+
.context(callerContext).attributes(callerContext).build();
414+
ModelCallInvocation invocation = new ModelCallInvocation(identity(), MODEL_TURN, request);
415+
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.
419+
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.
423+
assertThrows(UnsupportedOperationException.class,
424+
() -> invocation.request().getAttributes().put("k", "rewritten"));
425+
assertEquals("v", callerContext.get("k"));
426+
assertEquals(1, callerMessages.size());
427+
// The executor's own request keeps its content.
428+
assertEquals("hello", request.getMessages().get(0).text());
429+
assertEquals("v", request.getContext().get("k"));
430+
}
431+
432+
@Test
433+
void mutationGuardRejectsTrustedExecutionContextSwap() {
434+
TrustedExecutionContext tenantA = TrustedExecutionContext.builder()
435+
.tenant(new TenantReference("run", "tenant-a"))
436+
.principal(new PrincipalReference("run", "principal-a"))
437+
.runId("run").correlationId("correlation").budget(ExecutionBudget.unlimited())
438+
.delegationScope(DelegationScope.none()).build();
439+
TrustedExecutionContext tenantB = TrustedExecutionContext.builder()
440+
.tenant(new TenantReference("run", "tenant-b"))
441+
.principal(new PrincipalReference("run", "principal-b"))
442+
.runId("run").correlationId("correlation").budget(ExecutionBudget.unlimited())
443+
.delegationScope(DelegationScope.none()).build();
444+
ModelCallInvocation original = modelCallWithTrustedContext(tenantA);
445+
// Swapping the tenant (or nulling the context out) must be rejected even when
446+
// the lifecycle IDs are preserved.
447+
ModelCallInvocation swappedTenant = new ModelCallInvocation(original.identity(), original.modelTurn(),
448+
ModelRequest.builder(original.request()).trustedExecutionContext(tenantB).build());
449+
ModelCallInvocation nulledContext = new ModelCallInvocation(original.identity(), original.modelTurn(),
450+
ModelRequest.builder(original.request()).trustedExecutionContext(null).build());
451+
LifecycleEvent originalEvent = new LifecycleEvent.ModelCallRequested(original);
452+
assertEquals("trusted execution context must not change",
453+
LifecycleMutationGuard.check(originalEvent,
454+
new LifecycleEvent.ModelCallRequested(swappedTenant)).orElseThrow());
455+
assertEquals("trusted execution context must not change",
456+
LifecycleMutationGuard.check(originalEvent,
457+
new LifecycleEvent.ModelCallRequested(nulledContext)).orElseThrow());
458+
}
459+
460+
@Test
461+
void mutationGuardStillAllowsLegitimateRequestTransform() {
462+
TrustedExecutionContext tenantA = TrustedExecutionContext.builder()
463+
.tenant(new TenantReference("run", "tenant-a"))
464+
.principal(new PrincipalReference("run", "principal-a"))
465+
.runId("run").correlationId("correlation").budget(ExecutionBudget.unlimited())
466+
.delegationScope(DelegationScope.none()).build();
467+
ModelCallInvocation original = modelCallWithTrustedContext(tenantA);
468+
// A legitimate rewrite keeps the trusted execution context and changes only
469+
// message content (e.g. a compaction or redaction pass).
470+
ModelCallInvocation rewrittenInvocation = new ModelCallInvocation(original.identity(),
471+
original.modelTurn(), ModelRequest.builder(original.request())
472+
.messages(List.of(ChatMessage.user("redacted"))).build());
473+
LifecycleEvent originalEvent = new LifecycleEvent.ModelCallRequested(original);
474+
LifecycleEvent rewrittenEvent = new LifecycleEvent.ModelCallRequested(rewrittenInvocation);
475+
assertTrue(LifecycleMutationGuard.check(originalEvent, rewrittenEvent).isEmpty());
476+
}
477+
478+
private static ModelCallInvocation modelCallWithTrustedContext(TrustedExecutionContext trusted) {
479+
ModelRequest request = ModelRequest.builder().messages(List.of(ChatMessage.user("hello")))
480+
.trustedExecutionContext(trusted).build();
481+
return new ModelCallInvocation(identity(), MODEL_TURN, request);
482+
}
483+
396484
@Test
397485
void neutralContractDoesNotDependOnGraphOrLoop() {
398486
assertThrows(ClassNotFoundException.class, () -> Class.forName("io.github.agentic.ai.workflow.graph.KeyStrategy"));

0 commit comments

Comments
 (0)