Skip to content

Commit 0ac35b2

Browse files
GWealecopybara-github
authored andcommitted
docs: Add path sanitization for model-generated file paths
Co-authored-by: George Weale <gweale@google.com> PiperOrigin-RevId: 834344352
1 parent 857de04 commit 0ac35b2

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

contributing/samples/adk_agent_builder_assistant/tools/write_config_files.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import yaml
3232

3333
from ..utils import load_agent_config_schema
34+
from ..utils.path_normalizer import sanitize_generated_file_path
3435
from ..utils.resolve_root_directory import resolve_file_path
3536
from .write_files import write_files
3637

@@ -177,8 +178,9 @@ async def write_config_files(
177178

178179
# Step 1: Validate all configs before writing any files
179180
for file_path, config_content in configs.items():
181+
normalized_input_path = sanitize_generated_file_path(file_path)
180182
file_result = _validate_single_config(
181-
file_path, config_content, project_folder_name
183+
normalized_input_path, config_content, project_folder_name
182184
)
183185
result["files"][file_path] = file_result
184186

@@ -197,7 +199,7 @@ async def write_config_files(
197199
rename_applied,
198200
sanitized_name,
199201
rename_warning,
200-
) = _determine_target_file_path(file_path, agent_name)
202+
) = _determine_target_file_path(normalized_input_path, agent_name)
201203

202204
file_result["target_file_path"] = target_path
203205
file_result["rename_applied"] = rename_applied
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright 2025 Google LLC
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+
"""Helpers for normalizing file path strings produced by the model."""
16+
17+
from __future__ import annotations
18+
19+
import re
20+
21+
_SEGMENT_SPLIT_PATTERN = re.compile(r"([/\\])")
22+
_BOUNDARY_CHARS = " \t\r\n'\"`"
23+
24+
25+
def sanitize_generated_file_path(file_path: str) -> str:
26+
"""Strip stray quotes/whitespace around each path segment.
27+
28+
The agent occasionally emits quoted paths such as `'tools/web.yaml'` which
29+
would otherwise create directories literally named `'<name>`. This helper
30+
removes leading/trailing whitespace and quote-like characters from the path
31+
and from each path component while preserving intentional interior
32+
characters.
33+
34+
Args:
35+
file_path: Path string provided by the model or user.
36+
37+
Returns:
38+
Sanitized path string safe to feed into pathlib.Path.
39+
"""
40+
if not isinstance(file_path, str):
41+
file_path = str(file_path)
42+
43+
trimmed = file_path.strip()
44+
if not trimmed:
45+
return trimmed
46+
47+
segments = _SEGMENT_SPLIT_PATTERN.split(trimmed)
48+
sanitized_segments: list[str] = []
49+
50+
for segment in segments:
51+
if not segment:
52+
sanitized_segments.append(segment)
53+
continue
54+
if segment in ("/", "\\"):
55+
sanitized_segments.append(segment)
56+
continue
57+
sanitized_segments.append(segment.strip(_BOUNDARY_CHARS))
58+
59+
sanitized = "".join(sanitized_segments).strip(_BOUNDARY_CHARS)
60+
return sanitized or trimmed

contributing/samples/adk_agent_builder_assistant/utils/resolve_root_directory.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
from typing import List
2222
from typing import Optional
2323

24+
from .path_normalizer import sanitize_generated_file_path
25+
2426

2527
def resolve_file_path(
2628
file_path: str,
@@ -40,7 +42,8 @@ def resolve_file_path(
4042
Returns:
4143
Resolved absolute Path object
4244
"""
43-
file_path_obj = Path(file_path)
45+
normalized_path = sanitize_generated_file_path(file_path)
46+
file_path_obj = Path(normalized_path)
4447

4548
# If already absolute, use as-is
4649
if file_path_obj.is_absolute():
@@ -63,7 +66,7 @@ def resolve_file_path(
6366
resolved_root = Path(os.getcwd()) / root_directory
6467

6568
# Resolve file path relative to root directory
66-
return resolved_root / file_path
69+
return resolved_root / file_path_obj
6770

6871

6972
def resolve_file_paths(

0 commit comments

Comments
 (0)