Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions tests/models/test_ark_llm_client_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest

from veadk.models import ark_llm


class _FakeResponses:
def __init__(self, response=None, error=None):
self._response = response
self._error = error

async def create(self, **kwargs):
if self._error is not None:
raise self._error
return self._response


class _FakeAsyncArk:
def __init__(self, responses):
self.responses = responses


@pytest.mark.asyncio
async def test_aresponses_logs_client_and_request_boundaries(monkeypatch):
messages: list[str] = []
monkeypatch.setattr(
ark_llm.logger,
"info",
lambda message, *args, **kwargs: messages.append(message),
)
sentinel = object()
fake_client = _FakeAsyncArk(_FakeResponses(response=sentinel))
monkeypatch.setattr(ark_llm, "AsyncArk", lambda **kwargs: fake_client)

result = await ark_llm.ArkLlmClient().aresponses(
model="test-model",
stream=True,
input=[],
api_key="test-key",
api_base="http://example.com",
)

assert result is sentinel
assert any("client create begin" in m and "model=test-model" in m for m in messages)
assert any("client create end" in m for m in messages)
assert any("responses.create begin" in m and "stream=True" in m for m in messages)
assert any("responses.create end" in m for m in messages)


@pytest.mark.asyncio
async def test_aresponses_logs_request_error(monkeypatch):
warnings: list[str] = []
monkeypatch.setattr(
ark_llm.logger,
"warning",
lambda message, *args, **kwargs: warnings.append(message),
)
fake_client = _FakeAsyncArk(_FakeResponses(error=RuntimeError("boom")))
monkeypatch.setattr(ark_llm, "AsyncArk", lambda **kwargs: fake_client)

with pytest.raises(RuntimeError, match="boom"):
await ark_llm.ArkLlmClient().aresponses(
model="test-model",
api_key="test-key",
api_base="http://example.com",
)

assert any(
"responses.create error" in m and "error=RuntimeError" in m for m in warnings
)


@pytest.mark.asyncio
async def test_aresponses_survives_logging_failure(monkeypatch):
def fail(*args, **kwargs):
raise RuntimeError("log backend down")

monkeypatch.setattr(ark_llm.logger, "info", fail)
monkeypatch.setattr(ark_llm.logger, "warning", fail)
sentinel = object()
fake_client = _FakeAsyncArk(_FakeResponses(response=sentinel))
monkeypatch.setattr(ark_llm, "AsyncArk", lambda **kwargs: fake_client)

result = await ark_llm.ArkLlmClient().aresponses(
model="test-model",
api_key="test-key",
api_base="http://example.com",
)

assert result is sentinel


@pytest.mark.asyncio
async def test_aresponses_error_is_not_masked_by_logging_failure(monkeypatch):
def fail(*args, **kwargs):
raise RuntimeError("log backend down")

monkeypatch.setattr(ark_llm.logger, "warning", fail)
fake_client = _FakeAsyncArk(_FakeResponses(error=RuntimeError("boom")))
monkeypatch.setattr(ark_llm, "AsyncArk", lambda **kwargs: fake_client)

with pytest.raises(RuntimeError, match="boom"):
await ark_llm.ArkLlmClient().aresponses(
model="test-model",
api_key="test-key",
api_base="http://example.com",
)
24 changes: 24 additions & 0 deletions tests/test_adk_sync_tool_thread_pool_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

from veadk import Agent
from veadk.tools.builtin_tools import run_code as run_code_module
from veadk.utils import patches as patches_module
from veadk.utils.patches import patch_adk_sync_tool_thread_pool


Expand Down Expand Up @@ -133,6 +134,29 @@ def run_code(label: str, delay: float, tool_context: ToolContext) -> dict:
return start_gap, elapsed, parallel_call_counts, windows


@pytest.mark.asyncio
async def test_dispatch_probe_logs_function_and_tool_boundaries(monkeypatch) -> None:
messages: list[str] = []

def capture(message: str, *args, **kwargs) -> None:
messages.append(message)

monkeypatch.setattr(patches_module.logger, "info", capture)
patch_adk_sync_tool_thread_pool()

await _run_two_sync_tool_calls(RunConfig(max_llm_calls=5))

assert messages[0].startswith(
"[tool-dispatch] function_calls enter count=2 tools=blocking_tool,blocking_tool"
)
assert messages[-1].startswith(
"[tool-dispatch] function_calls exit count=2 tools=blocking_tool,blocking_tool"
)
assert sum(m.startswith("[tool-dispatch] call_tool enter") for m in messages) == 2
assert sum(m.startswith("[tool-dispatch] call_tool exit") for m in messages) == 2
assert any("[tool-dispatch] call_tool config" in m for m in messages)


@pytest.mark.asyncio
async def test_sync_tool_calls_stay_serial_without_thread_pool_config(
monkeypatch,
Expand Down
31 changes: 31 additions & 0 deletions tests/test_logger_safe_emit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from veadk.utils import logger as logger_module


class _RaisingLogger:
def info(self, message):
raise RuntimeError("info failed")

def warning(self, message):
raise RuntimeError("warning failed")


def test_safe_info_swallows_logging_errors():
logger_module.safe_info(_RaisingLogger(), "message")


def test_safe_warning_swallows_logging_errors():
logger_module.safe_warning(_RaisingLogger(), "message")
107 changes: 107 additions & 0 deletions tests/test_runner_dispatch_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from types import SimpleNamespace

import pytest
from google.adk.agents import RunConfig

from veadk import runner as runner_module


class _FakeProcessor:
def process_run(self, runner, message):
def decorator(func):
return func

return decorator


def _make_runner():
runner = object.__new__(runner_module.Runner)
runner.agent = SimpleNamespace(skills=[])
runner.short_term_memory = None
runner.app_name = "test-app"
runner.user_id = "test-user"
runner.upload_inline_data_to_tos = False
runner.run_processor = _FakeProcessor()
runner._print_trace_id = lambda: None
runner.save_tracing_file = lambda session_id: ""

async def run_async(**kwargs):
return
yield

runner.run_async = run_async
return runner


@pytest.mark.asyncio
async def test_run_logs_turn_boundaries(monkeypatch):
messages: list[str] = []
monkeypatch.setattr(
runner_module.logger,
"info",
lambda message, *args, **kwargs: messages.append(message),
)
monkeypatch.setattr(
runner_module, "_convert_messages", lambda *args, **kwargs: ["converted"]
)

output = await _make_runner().run("hello", run_config=RunConfig())

assert output == ""
assert messages[0].startswith("[turn-dispatch] run begin")
assert messages[0].endswith("messages=1")
assert messages[-1].startswith("[turn-dispatch] run end")
assert "output_chars=0" in messages[-1]


@pytest.mark.asyncio
async def test_run_logs_trace_save_boundaries(monkeypatch):
messages: list[str] = []
monkeypatch.setattr(
runner_module.logger,
"info",
lambda message, *args, **kwargs: messages.append(message),
)
monkeypatch.setattr(
runner_module, "_convert_messages", lambda *args, **kwargs: ["converted"]
)

await _make_runner().run(
"hello",
run_config=RunConfig(),
save_tracing_data=True,
)

assert any("trace save begin" in message for message in messages)
assert any("trace save end" in message for message in messages)


@pytest.mark.asyncio
async def test_run_survives_logging_failure(monkeypatch):
original_info = runner_module.logger.info

def fail_info(message, *args, **kwargs):
if "[turn-dispatch]" in str(message):
raise RuntimeError("log backend down")
return original_info(message, *args, **kwargs)

monkeypatch.setattr(runner_module.logger, "info", fail_info)
monkeypatch.setattr(
runner_module, "_convert_messages", lambda *args, **kwargs: ["converted"]
)

assert await _make_runner().run("hello", run_config=RunConfig()) == ""
Loading
Loading