|
16 | 16 | package io.github.agentic.ai.agent.lifecycle; |
17 | 17 |
|
18 | 18 | 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; |
19 | 24 | import io.github.agentic.ai.agent.interceptor.ModelResponse; |
20 | 25 | import io.github.agentic.ai.api.chat.ChatMessage; |
21 | 26 | import java.io.IOException; |
|
24 | 29 | import java.lang.reflect.Modifier; |
25 | 30 | import java.nio.charset.StandardCharsets; |
26 | 31 | import java.util.ArrayList; |
| 32 | +import java.util.HashMap; |
27 | 33 | import java.util.List; |
| 34 | +import java.util.Map; |
28 | 35 | import java.util.Optional; |
29 | 36 | import java.util.Set; |
30 | 37 | import java.util.concurrent.atomic.AtomicInteger; |
|
33 | 40 | import static org.junit.jupiter.api.Assertions.assertEquals; |
34 | 41 | import static org.junit.jupiter.api.Assertions.assertFalse; |
35 | 42 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 43 | +import static org.junit.jupiter.api.Assertions.assertNotSame; |
36 | 44 | import static org.junit.jupiter.api.Assertions.assertThrows; |
37 | 45 | import static org.junit.jupiter.api.Assertions.assertTrue; |
38 | 46 |
|
@@ -393,6 +401,86 @@ void modelCallContractCarriesTheTrustedContextUnchanged() { |
393 | 401 | assertEquals("model unavailable", failed.error()); |
394 | 402 | } |
395 | 403 |
|
| 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 | + |
396 | 484 | @Test |
397 | 485 | void neutralContractDoesNotDependOnGraphOrLoop() { |
398 | 486 | assertThrows(ClassNotFoundException.class, () -> Class.forName("io.github.agentic.ai.workflow.graph.KeyStrategy")); |
|
0 commit comments