Skip to content

Commit dfb8638

Browse files
GWealecopybara-github
authored andcommitted
chore: fix the cleanup_unused_files tool in yaml agent to use the same directory as other tools
PiperOrigin-RevId: 818846060
1 parent 3c0b99a commit dfb8638

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

contributing/samples/adk_agent_builder_assistant/tools/cleanup_unused_files.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,53 @@
1414

1515
"""Cleanup unused files tool for Agent Builder Assistant."""
1616

17-
from pathlib import Path
1817
from typing import Any
1918
from typing import Dict
2019
from typing import List
2120
from typing import Optional
2221

22+
from google.adk.tools.tool_context import ToolContext
23+
24+
from ..utils.resolve_root_directory import resolve_file_path
25+
from ..utils.resolve_root_directory import resolve_file_paths
26+
2327

2428
async def cleanup_unused_files(
25-
root_directory: str,
2629
used_files: List[str],
30+
tool_context: ToolContext,
2731
file_patterns: Optional[List[str]] = None,
2832
exclude_patterns: Optional[List[str]] = None,
2933
) -> Dict[str, Any]:
3034
"""Identify and optionally delete unused files in project directories.
3135
3236
This tool helps clean up unused tool files when agent configurations change.
3337
It identifies files that match patterns but aren't referenced in used_files
34-
list.
38+
list. Paths are resolved automatically using the tool context.
3539
3640
Args:
37-
root_directory: Root directory to scan for unused files
3841
used_files: List of file paths currently in use (should not be deleted)
42+
tool_context: Tool execution context (provides session state)
3943
file_patterns: List of glob patterns to match files (default: ["*.py"])
4044
exclude_patterns: List of patterns to exclude (default: ["__init__.py"])
4145
4246
Returns:
4347
Dict containing cleanup results:
4448
- success: bool indicating if scan succeeded
45-
- root_directory: absolute path to scanned directory
4649
- unused_files: list of unused files found
4750
- deleted_files: list of files actually deleted
4851
- backup_files: list of backup files created
4952
- errors: list of error messages
5053
- total_freed_space: total bytes freed by deletions
5154
"""
55+
session_state = tool_context.state
56+
root_path = resolve_file_path(".", session_state)
57+
5258
try:
53-
root_path = Path(root_directory).resolve()
54-
used_files_set = {Path(f).resolve() for f in used_files}
59+
root_path = root_path.resolve()
60+
resolved_used_files = {
61+
path.resolve()
62+
for path in resolve_file_paths(used_files or [], session_state)
63+
}
5564

5665
# Set defaults
5766
if file_patterns is None:
@@ -61,7 +70,6 @@ async def cleanup_unused_files(
6170

6271
result = {
6372
"success": False,
64-
"root_directory": str(root_path),
6573
"unused_files": [],
6674
"deleted_files": [],
6775
"backup_files": [],
@@ -85,7 +93,7 @@ async def cleanup_unused_files(
8593
# Identify unused files
8694
unused_files = []
8795
for file_path in all_files:
88-
if file_path not in used_files_set:
96+
if file_path.resolve() not in resolved_used_files:
8997
unused_files.append(file_path)
9098

9199
result["unused_files"] = [str(f) for f in unused_files]
@@ -99,7 +107,6 @@ async def cleanup_unused_files(
99107
except Exception as e:
100108
return {
101109
"success": False,
102-
"root_directory": root_directory,
103110
"unused_files": [],
104111
"deleted_files": [],
105112
"backup_files": [],

0 commit comments

Comments
 (0)