@@ -1560,16 +1560,19 @@ async def test_after_tool_callback_logs_correctly(
15601560 assert content_dict ["result" ] == {"res" : "success" }
15611561
15621562 @pytest .mark .asyncio
1563- async def test_after_tool_callback_state_delta_logging (
1563+ async def test_after_tool_callback_no_inline_state_delta (
15641564 self , bq_plugin_inst , mock_write_client , tool_context , dummy_arrow_schema
15651565 ):
1566+ """after_tool_callback does not log STATE_DELTA inline.
1567+
1568+ STATE_DELTA is logged exclusively via on_state_change_callback.
1569+ """
15661570 mock_tool = mock .create_autospec (
15671571 base_tool_lib .BaseTool , instance = True , spec_set = True
15681572 )
15691573 type(mock_tool ).name = mock .PropertyMock (return_value = "StateTool" )
15701574 type(mock_tool ).description = mock .PropertyMock (return_value = "Sets state" )
15711575
1572- # Simulate a tool modifying the state
15731576 tool_context .actions .state_delta ["new_key" ] = "new_value"
15741577
15751578 bigquery_agent_analytics_plugin .TraceManager .push_span (tool_context )
@@ -1581,31 +1584,11 @@ async def test_after_tool_callback_state_delta_logging(
15811584 )
15821585 await asyncio .sleep (0.01 )
15831586
1584- # We should have two events appended: TOOL_COMPLETED and STATE_DELTA
1585- assert mock_write_client .append_rows .call_count >= 1
1586-
1587- # Retrieve all flushed events
1588- rows = await _get_captured_rows_async (mock_write_client , dummy_arrow_schema )
1589- assert len (rows ) == 2
1590-
1591- # Sort by event_type to reliably access them
1592- rows .sort (key = lambda x : x ["event_type" ])
1593-
1594- state_delta_event = (
1595- rows [0 ] if rows [0 ]["event_type" ] == "STATE_DELTA" else rows [1 ]
1596- )
1597- tool_event = (
1598- rows [1 ] if rows [1 ]["event_type" ] == "TOOL_COMPLETED" else rows [0 ]
1587+ # Only TOOL_COMPLETED should be logged
1588+ log_entry = await _get_captured_event_dict_async (
1589+ mock_write_client , dummy_arrow_schema
15991590 )
1600-
1601- assert state_delta_event ["event_type" ] == "STATE_DELTA"
1602- assert tool_event ["event_type" ] == "TOOL_COMPLETED"
1603-
1604- # Verify STATE_DELTA payload
1605- attributes = json .loads (state_delta_event ["attributes" ])
1606- assert "state_delta" in attributes
1607- assert attributes ["state_delta" ] == {"new_key" : "new_value" }
1608- assert state_delta_event ["content" ] is None
1591+ assert log_entry ["event_type" ] == "TOOL_COMPLETED"
16091592
16101593 @pytest .mark .asyncio
16111594 async def test_on_state_change_callback_logs_correctly (
@@ -1615,6 +1598,8 @@ async def test_on_state_change_callback_logs_correctly(
16151598 callback_context ,
16161599 dummy_arrow_schema ,
16171600 ):
1601+ """STATE_DELTA is logged via on_state_change_callback when enabled."""
1602+ bq_plugin_inst .config .log_state_changes = True
16181603 state_delta = {"key" : "value" , "new_key" : 123 }
16191604 bigquery_agent_analytics_plugin .TraceManager .push_span (callback_context )
16201605 await bq_plugin_inst .on_state_change_callback (
@@ -1625,13 +1610,27 @@ async def test_on_state_change_callback_logs_correctly(
16251610 mock_write_client , dummy_arrow_schema
16261611 )
16271612 _assert_common_fields (log_entry , "STATE_DELTA" )
1628- # content should be None (as raw_content was not passed)
16291613 assert log_entry ["content" ] is None
16301614
1631- # state_delta should be in attributes
16321615 attributes = json .loads (log_entry ["attributes" ])
16331616 assert attributes ["state_delta" ] == state_delta
16341617
1618+ @pytest .mark .asyncio
1619+ async def test_on_state_change_callback_disabled (
1620+ self ,
1621+ bq_plugin_inst ,
1622+ mock_write_client ,
1623+ callback_context ,
1624+ ):
1625+ """STATE_DELTA is not logged when log_state_changes is False (default)."""
1626+ state_delta = {"key" : "value" , "new_key" : 123 }
1627+ bigquery_agent_analytics_plugin .TraceManager .push_span (callback_context )
1628+ await bq_plugin_inst .on_state_change_callback (
1629+ callback_context = callback_context , state_delta = state_delta
1630+ )
1631+ await asyncio .sleep (0.01 )
1632+ mock_write_client .append_rows .assert_not_called ()
1633+
16351634 @pytest .mark .asyncio
16361635 async def test_log_event_with_session_metadata (
16371636 self ,
0 commit comments