3333import java .util .concurrent .atomic .AtomicInteger ;
3434
3535import static org .junit .jupiter .api .Assertions .assertEquals ;
36+ import static org .junit .jupiter .api .Assertions .assertNull ;
3637import static org .junit .jupiter .api .Assertions .assertSame ;
3738import static org .junit .jupiter .api .Assertions .assertTrue ;
3839
@@ -41,7 +42,8 @@ class AgentInvocationAuditBridgeTest {
4142 @ Test
4243 void reusesTerminalIdempotenceAndKeepsAttemptIdentity () {
4344 List <AgentInvocationAuditEvent > records = new CopyOnWriteArrayList <>();
44- AiRequestObservation observation = new AiRequestObservation (AgentInvocationAuditBridge .ai (null , records ::add ), context ("one" ));
45+ AgentRequestContext initial = context ("one" );
46+ AiRequestObservation observation = new AiRequestObservation (AgentInvocationAuditBridge .ai (null , records ::add ), initial );
4547 AiRequestObservation .Attempt primary = observation .startAttempt (observation .getContext ().withTarget ("primary" , "chat" , null ),
4648 AiAttemptEvent .Kind .PRIMARY , "openai" , "chat" , false );
4749 primary .status (503 );
@@ -57,6 +59,11 @@ void reusesTerminalIdempotenceAndKeepsAttemptIdentity() {
5759 assertEquals (List .of (1 , 2 ), records .stream ().filter (event -> event .getAttempt ().isPresent ()).map (event -> event .getAttempt ().orElseThrow ()).toList ());
5860 assertEquals (1 , records .stream ().filter (event -> event .getRecordType () == AgentInvocationAuditEvent .RecordType .LOGICAL ).count ());
5961 assertTrue (records .stream ().allMatch (event -> event .getApiKeyId ().orElseThrow ().equals ("key-id-one" )));
62+ assertNull (initial .getTarget ());
63+ assertEquals ("primary" , records .get (0 ).getTarget ().orElseThrow ());
64+ assertEquals ("fallback" , records .get (1 ).getTarget ().orElseThrow ());
65+ // startAttempt updates the logical owner while each attempt keeps its selected target.
66+ assertEquals ("fallback" , observation .getContext ().getTarget ());
6067 assertEquals ("fallback" , records .get (2 ).getTarget ().orElseThrow ());
6168 }
6269
0 commit comments