@@ -1300,6 +1300,61 @@ public Stream<Event> stream() {
13001300 var unused = contentsProcessor .processRequest (context , initialRequest ).blockingGet ();
13011301 }
13021302
1303+ @ Test
1304+ public void processRequest_siblingBranchSharesNamePrefix_excludesSiblingEvent () {
1305+ Event siblingEvent =
1306+ createBranchedAgentEvent ("agent_1" , "e1" , "sibling output" , "root.agent_1" );
1307+
1308+ List <Content > result =
1309+ runContentsProcessorOnBranch (ImmutableList .of (siblingEvent ), "agent_10" , "root.agent_10" );
1310+
1311+ assertThat (result ).isEmpty ();
1312+ }
1313+
1314+ @ Test
1315+ public void processRequest_sameBranch_includesEvent () {
1316+ Event ownEvent = createBranchedAgentEvent ("agent_10" , "e1" , "own output" , "root.agent_10" );
1317+
1318+ List <Content > result =
1319+ runContentsProcessorOnBranch (ImmutableList .of (ownEvent ), "agent_10" , "root.agent_10" );
1320+
1321+ assertThat (result ).isEqualTo (eventsToContents (ImmutableList .of (ownEvent )));
1322+ }
1323+
1324+ @ Test
1325+ public void processRequest_ancestorBranch_includesEvent () {
1326+ Event ancestorEvent = createBranchedAgentEvent ("agent_10" , "e1" , "ancestor output" , "root" );
1327+
1328+ List <Content > result =
1329+ runContentsProcessorOnBranch (ImmutableList .of (ancestorEvent ), "agent_10" , "root.agent_10" );
1330+
1331+ assertThat (result ).isEqualTo (eventsToContents (ImmutableList .of (ancestorEvent )));
1332+ }
1333+
1334+ @ Test
1335+ public void processRequest_eventWithoutBranch_includesEvent () {
1336+ Event userEvent = createUserEvent ("u1" , "user input" );
1337+
1338+ List <Content > result =
1339+ runContentsProcessorOnBranch (ImmutableList .of (userEvent ), "agent_10" , "root.agent_10" );
1340+
1341+ assertThat (result ).isEqualTo (eventsToContents (ImmutableList .of (userEvent )));
1342+ }
1343+
1344+ @ Test
1345+ public void processRequest_noInvocationBranch_includesBranchedEvent () {
1346+ Event siblingEvent =
1347+ createBranchedAgentEvent ("agent_1" , "e1" , "sibling output" , "root.agent_1" );
1348+
1349+ List <Content > result =
1350+ runContentsProcessorOnBranch (ImmutableList .of (siblingEvent ), "agent_10" , null );
1351+
1352+ assertThat (result )
1353+ .containsExactly (
1354+ Content .fromParts (
1355+ Part .fromText ("For context:" ), Part .fromText ("[agent_1] said: sibling output" )));
1356+ }
1357+
13031358 private static Event createUserEvent (String id , String text ) {
13041359 return Event .builder ()
13051360 .id (id )
@@ -1343,6 +1398,18 @@ private static Event createAgentEvent(String agent, String id, String text) {
13431398 .build ();
13441399 }
13451400
1401+ private static Event createBranchedAgentEvent (
1402+ String agent , String id , String text , String branch ) {
1403+ return Event .builder ()
1404+ .id (id )
1405+ .author (agent )
1406+ .content (
1407+ Content .builder ().role ("model" ).parts (ImmutableList .of (Part .fromText (text ))).build ())
1408+ .invocationId ("invocationId" )
1409+ .branch (branch )
1410+ .build ();
1411+ }
1412+
13461413 private static Event createFunctionCallEvent (String id , String toolName , String callId ) {
13471414 return createFunctionCallEvent (AGENT , id , toolName , callId );
13481415 }
@@ -1527,6 +1594,31 @@ private List<Content> runContentsProcessorGrouped(List<Event> events) {
15271594 return result .updatedRequest ().contents ();
15281595 }
15291596
1597+ private List <Content > runContentsProcessorOnBranch (
1598+ List <Event > events , String agentName , String invocationBranch ) {
1599+ LlmAgent agent =
1600+ LlmAgent .builder ()
1601+ .name (agentName )
1602+ .includeContents (LlmAgent .IncludeContents .DEFAULT )
1603+ .build ();
1604+ Session session =
1605+ sessionService .createSession ("test-app" , "test-user" , null , "test-session" ).blockingGet ();
1606+ session .events ().addAll (events );
1607+ InvocationContext context =
1608+ InvocationContext .builder ()
1609+ .invocationId ("test-invocation" )
1610+ .agent (agent )
1611+ .session (session )
1612+ .sessionService (sessionService )
1613+ .branch (invocationBranch )
1614+ .build ();
1615+
1616+ LlmRequest initialRequest = LlmRequest .builder ().build ();
1617+ RequestProcessor .RequestProcessingResult result =
1618+ contentsProcessor .processRequest (context , initialRequest ).blockingGet ();
1619+ return result .updatedRequest ().contents ();
1620+ }
1621+
15301622 private List <Content > runContentsProcessorWithModel (
15311623 List <Event > events , String modelName , RunConfig runConfig ) {
15321624 LlmAgent agent =
0 commit comments