Skip to content

Commit b5c5356

Browse files
committed
fix: change the default save path of evalset and trace file
1 parent 49ba1be commit b5c5356

File tree

6 files changed

+101
-19
lines changed

6 files changed

+101
-19
lines changed

tests/test_misc.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import sys
17+
import types
18+
19+
from veadk.utils.misc import get_agents_dir, get_agent_dir
20+
21+
22+
class GetAgentsDirTest:
23+
def test_get_agents_dir_from_main_file(monkeypatch):
24+
"""
25+
Case 1: __main__.__file__ exists (common in CLI or uv run environments)
26+
"""
27+
fake_main = types.SimpleNamespace(__file__="/tmp/project/testapp/agent.py")
28+
monkeypatch.setitem(sys.modules, "__main__", fake_main)
29+
30+
result = get_agents_dir()
31+
assert result == "/tmp/project"
32+
result = get_agent_dir()
33+
assert result == "/tmp/project/testapp"
34+
35+
def test_get_agents_dir_from_sys_argv(monkeypatch):
36+
"""
37+
Case 2: Fallback to sys.argv[0]
38+
"""
39+
fake_main = types.SimpleNamespace()
40+
monkeypatch.setitem(sys.modules, "__main__", fake_main)
41+
monkeypatch.setattr(sys, "argv", ["/tmp/project/testapp/agent.py"])
42+
43+
result = get_agents_dir()
44+
assert result == "/tmp/project"
45+
result = get_agent_dir()
46+
assert result == "/tmp/project/testapp"
47+
48+
def test_get_agents_dir_from_cwd(monkeypatch, tmp_path):
49+
"""
50+
Case 3: Fallback to current working directory (REPL or no file context)
51+
"""
52+
fake_main = types.SimpleNamespace()
53+
monkeypatch.setitem(sys.modules, "__main__", fake_main)
54+
monkeypatch.setattr(sys, "argv", [])
55+
56+
fake_cwd = tmp_path / "some_dir"
57+
fake_cwd.mkdir()
58+
59+
monkeypatch.setattr(os, "getcwd", lambda: str(fake_cwd))
60+
result = get_agents_dir()
61+
62+
# should return the parent of fake_cwd
63+
assert result == str(tmp_path)
64+
result = get_agent_dir()
65+
assert result == str(tmp_path / "some_dir")

tests/test_runtime_data_collecting.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import json
1616
import os
17+
import uuid
1718

1819
import pytest
1920
from utils import generate_events, generate_session
@@ -25,7 +26,7 @@
2526
USER_ID = "user"
2627
SESSION_ID = "session"
2728

28-
EVAL_SET_ID = "temp_unittest"
29+
EVAL_SET_ID = "temp_unittest" + uuid.uuid4().hex
2930

3031

3132
@pytest.mark.asyncio
@@ -46,7 +47,7 @@ async def test_runtime_data_collecting():
4647
recorder = EvalSetRecorder(session_service=session_service, eval_set_id=EVAL_SET_ID)
4748
dump_path = await recorder.dump(APP_NAME, USER_ID, SESSION_ID)
4849

49-
assert dump_path == f"/tmp/{APP_NAME}/{recorder.eval_set_id}.evalset.json"
50+
# assert dump_path == f"/tmp/{APP_NAME}/{recorder.eval_set_id}.evalset.json"
5051
assert os.path.exists(dump_path) and os.path.isfile(dump_path)
5152
assert os.path.getsize(dump_path) > 0
5253

veadk/evaluation/eval_set_recorder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from google.adk.sessions import BaseSessionService
2222

2323
from veadk.utils.logger import get_logger
24-
from veadk.utils.misc import formatted_timestamp, get_temp_dir
24+
from veadk.utils.misc import formatted_timestamp, get_agents_dir
2525

2626
logger = get_logger(__name__)
2727

@@ -53,7 +53,7 @@ def __init__(
5353
Raises:
5454
ValueError: If eval_set_id is invalid.
5555
"""
56-
super().__init__(agents_dir=get_temp_dir())
56+
super().__init__(agents_dir=get_agents_dir())
5757
self.eval_set_id = eval_set_id if eval_set_id != "" else "default"
5858
self.session_service: BaseSessionService = session_service
5959

veadk/tracing/base_tracer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def __init__(self, name: str):
4444
self._trace_file_path = "<unknown_trace_file_path>"
4545

4646
@abstractmethod
47-
def dump(self, user_id: str, session_id: str, path: str = "/tmp") -> str:
47+
def dump(self, user_id: str, session_id: str, path: str) -> str:
4848
"""Dump the collected trace data to a local file.
4949
5050
This method must be implemented by concrete tracer classes to export
@@ -53,6 +53,6 @@ def dump(self, user_id: str, session_id: str, path: str = "/tmp") -> str:
5353
Args:
5454
user_id: User identifier for trace organization and file naming
5555
session_id: Session identifier for filtering and organizing spans
56-
path: Directory path for the output file. Defaults to system temp directory
56+
path: Directory path for the output file
5757
"""
5858
...

veadk/tracing/telemetry/opentelemetry_tracer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from veadk.tracing.telemetry.exporters.base_exporter import BaseExporter
3232
from veadk.tracing.telemetry.exporters.inmemory_exporter import InMemoryExporter
3333
from veadk.utils.logger import get_logger
34-
from veadk.utils.misc import get_temp_dir
34+
from veadk.utils.misc import get_agent_dir
3535
from veadk.utils.patches import patch_google_adk_telemetry
3636

3737
logger = get_logger(__name__)
@@ -254,7 +254,7 @@ def dump(
254254
self,
255255
user_id: str = "unknown_user_id",
256256
session_id: str = "unknown_session_id",
257-
path: str = get_temp_dir(),
257+
path: str = get_agent_dir(),
258258
) -> str:
259259
"""Dump collected trace data to a local JSON file.
260260
@@ -265,7 +265,7 @@ def dump(
265265
Args:
266266
user_id: User identifier for trace organization and file naming
267267
session_id: Session identifier for filtering and organizing spans
268-
path: Directory path for the output file. Defaults to system temp directory
268+
path: Directory path for the output file. Defaults to agents directory
269269
270270
Returns:
271271
str: Full path to the created trace file, or empty string if export fails

veadk/utils/misc.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import requests
2424
from yaml import safe_load
25+
import __main__
2526

2627

2728
def read_file(file_path):
@@ -153,16 +154,31 @@ def set_envs(config_yaml_path: str) -> tuple[dict, dict]:
153154
return config_dict, veadk_environments
154155

155156

156-
def get_temp_dir():
157+
def get_agents_dir():
157158
"""
158-
Return the corresponding temporary directory based on the operating system
159-
- For Windows systems, return the system's default temporary directory
160-
- For other systems (macOS, Linux, etc.), return the /tmp directory
159+
Get the directory of agents.
160+
161+
Returns:
162+
str: The agents directory (parent directory of the app)
163+
"""
164+
return os.path.dirname(get_agent_dir())
165+
166+
167+
def get_agent_dir():
161168
"""
162-
# First determine if it is a Windows system
163-
if sys.platform.startswith("win"):
164-
# Windows systems use the temporary directory from environment variables
165-
return os.environ.get("TEMP", os.environ.get("TMP", r"C:\WINDOWS\TEMP"))
169+
Get the directory of the currently executed entry script.
170+
171+
Returns:
172+
str: The agent directory
173+
"""
174+
# Try using __main__.__file__ (works for most CLI scripts and uv run environments)
175+
if hasattr(__main__, "__file__"):
176+
full_path = os.path.dirname(os.path.abspath(__main__.__file__))
177+
# Fallback to sys.argv[0] (usually gives the entry script path)
178+
elif len(sys.argv) > 0 and sys.argv[0]:
179+
full_path = os.path.dirname(os.path.abspath(sys.argv[0]))
180+
# Fallback to current working directory (for REPL / Jupyter Notebook)
166181
else:
167-
# Non-Windows systems (macOS, Linux, etc.) uniformly return /tmp
168-
return "/tmp"
182+
full_path = os.getcwd()
183+
184+
return full_path

0 commit comments

Comments
 (0)