Skip to content

Commit e3eca64

Browse files
committed
fix(skills): call on_error for inaccessible skill directories
Path.is_dir() returns False for metadata OSErrors on Python 3.14 and 3.15, so an inaccessible skill directory was skipped before the listing error handler. Check the directory with stat() inside that handler so the same on_error policy runs on every supported Python. Fixes google#7749
1 parent 3b227d6 commit e3eca64

3 files changed

Lines changed: 199 additions & 18 deletions

File tree

‎docs/guides/skills/skill/index.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,8 @@ Use the mode.
366366
reading only the frontmatter of each `SKILL.md`. It is far more forgiving
367367
than `load_skills_from_dir`, because an invalid skill is logged and skipped,
368368
and a base path that is not a directory produces a warning and an empty dict
369-
instead of an exception.
369+
instead of an exception. A child whose directory metadata cannot be read is
370+
reported through the same log or `on_error` path.
370371
* **Seeing what was skipped**: a skipped skill only reaches a log, so it
371372
otherwise vanishes from the catalog with no signal. Pass `on_error` to get
372373
the skill ID and the exception instead of the warning: return from it to

‎src/google/adk/skills/_utils.py‎

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
from __future__ import annotations
1818

1919
import asyncio
20+
import errno
2021
import io
2122
import logging
2223
import pathlib
24+
import stat
2325
from typing import Any
2426
from typing import Callable
2527
from typing import cast
@@ -537,6 +539,18 @@ def _read_skill_properties(
537539
return models.Frontmatter.model_validate(parsed)
538540

539541

542+
# OS errors pathlib treated as "not a directory" before Python 3.14.
543+
# A missing path or broken symlink is still not an inaccessible skill.
544+
# Every other OSError, including a permission failure, must reach on_error:
545+
# Path.is_dir() returns False for those on Python 3.14 and later.
546+
_NON_DIRECTORY_STAT_ERRNOS = frozenset({
547+
errno.ENOENT,
548+
errno.ENOTDIR,
549+
errno.EBADF,
550+
errno.ELOOP,
551+
})
552+
553+
540554
def _list_skills_in_dir(
541555
skills_base_path: Union[str, pathlib.Path],
542556
on_error: Callable[[str, Exception], None] | None = None,
@@ -546,10 +560,11 @@ def _list_skills_in_dir(
546560
Args:
547561
skills_base_path: Path to the base directory containing skills.
548562
on_error: Called with the skill ID and the error when a skill cannot be
549-
listed, in place of the default warning log. Return normally to skip that
550-
skill and carry on, or raise to abort the listing. Without it an invalid
551-
skill is only logged, so it drops out of the result with no other signal
552-
to the caller.
563+
listed, in place of the default warning log. That includes a child whose
564+
directory metadata cannot be read. Return normally to skip that skill
565+
and carry on, or raise to abort the listing. Without it an invalid skill
566+
is only logged, so it drops out of the result with no other signal to
567+
the caller.
553568
554569
Returns:
555570
Dictionary mapping skill IDs to their frontmatter.
@@ -566,11 +581,33 @@ def _list_skills_in_dir(
566581
)
567582
return skills
568583

584+
def _report_skill_error(skill_id: str, error: Exception) -> None:
585+
if on_error is not None:
586+
on_error(skill_id, error)
587+
return
588+
# log invalid skills during listing and skip them
589+
logging.warning(
590+
"Skipping invalid skill '%s' in directory '%s': %s",
591+
skill_id,
592+
skills_base_path,
593+
error,
594+
)
595+
569596
for skill_dir in sorted(skills_base_path.iterdir()):
570-
if not skill_dir.is_dir():
597+
skill_id = skill_dir.name
598+
try:
599+
# stat() raises when metadata cannot be read. Path.is_dir() returns
600+
# False for every OSError on Python 3.14+, so that check would skip
601+
# the child before on_error runs.
602+
child_is_dir = stat.S_ISDIR(skill_dir.stat().st_mode)
603+
except OSError as error:
604+
if error.errno in _NON_DIRECTORY_STAT_ERRNOS:
605+
continue
606+
_report_skill_error(skill_id, error)
607+
continue
608+
if not child_is_dir:
571609
continue
572610

573-
skill_id = skill_dir.name
574611
try:
575612
frontmatter = _read_skill_properties(skill_dir)
576613
if skill_id != frontmatter.name:
@@ -579,17 +616,8 @@ def _list_skills_in_dir(
579616
f" name '{skill_id}'."
580617
)
581618
skills[skill_id] = frontmatter
582-
except (FileNotFoundError, ValueError, ValidationError) as e:
583-
if on_error is not None:
584-
on_error(skill_id, e)
585-
continue
586-
# log invalid skills during listing and skip them
587-
logging.warning(
588-
"Skipping invalid skill '%s' in directory '%s': %s",
589-
skill_id,
590-
skills_base_path,
591-
e,
592-
)
619+
except (FileNotFoundError, ValueError, ValidationError) as error:
620+
_report_skill_error(skill_id, error)
593621
return skills
594622

595623

‎tests/unittests/skills/test__utils.py‎

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
import asyncio
1818
import builtins
19+
import contextlib
20+
import errno
1921
import io
22+
import os
2023
import pathlib
2124
import struct
2225
import threading
@@ -586,6 +589,155 @@ def strict(skill_id, error):
586589
list_skills_in_dir(_skills_dir_with_one_invalid(tmp_path), on_error=strict)
587590

588591

592+
def _skills_dir_with_blocked_child(tmp_path):
593+
"""Builds one valid skill, one plain file, and one blocked directory."""
594+
skills_dir = tmp_path / "skills"
595+
skills_dir.mkdir()
596+
(skills_dir / "good").mkdir()
597+
(skills_dir / "good" / "SKILL.md").write_text(
598+
"---\nname: good\ndescription: desc\n---\nbody"
599+
)
600+
blocked = skills_dir / "blocked-skill"
601+
blocked.mkdir()
602+
(blocked / "SKILL.md").write_text(
603+
"---\nname: blocked-skill\ndescription: desc\n---\nbody"
604+
)
605+
(skills_dir / "notes.txt").write_text("not a skill")
606+
return skills_dir, blocked
607+
608+
609+
@contextlib.contextmanager
610+
def _deny_child_metadata(blocked):
611+
"""Raise PermissionError from os.stat for one child.
612+
613+
The failure is injected at the stat boundary. Path.is_dir() is forced to
614+
return False for that child, which is what Python 3.14 and 3.15 do when
615+
stat fails. Mocking is_dir() to raise would hide that change.
616+
"""
617+
real_stat = os.stat
618+
real_is_dir = pathlib.Path.is_dir
619+
denied = {
620+
os.fspath(blocked),
621+
os.fspath(blocked.resolve()),
622+
}
623+
624+
def fake_stat(path, *args, **kwargs):
625+
if os.fspath(path) in denied:
626+
raise PermissionError(
627+
errno.EACCES, "Permission denied", os.fspath(blocked)
628+
)
629+
return real_stat(path, *args, **kwargs)
630+
631+
def fake_is_dir(self, *args, **kwargs):
632+
if os.fspath(self) in denied:
633+
return False
634+
return real_is_dir(self, *args, **kwargs)
635+
636+
with (
637+
mock.patch("os.stat", side_effect=fake_stat),
638+
mock.patch.object(pathlib.Path, "is_dir", fake_is_dir),
639+
):
640+
yield
641+
642+
643+
@mock.patch("logging.warning")
644+
def test_list_skills_in_dir_reports_inaccessible_directory_to_on_error(
645+
mock_logging_warning, tmp_path
646+
):
647+
"""on_error receives a child whose directory metadata cannot be read."""
648+
skills_dir, blocked = _skills_dir_with_blocked_child(tmp_path)
649+
failures = []
650+
651+
with _deny_child_metadata(blocked):
652+
skills = list_skills_in_dir(
653+
skills_dir,
654+
on_error=lambda skill_id, error: failures.append((skill_id, error)),
655+
)
656+
657+
assert list(skills) == ["good"]
658+
assert [skill_id for skill_id, _ in failures] == ["blocked-skill"]
659+
assert isinstance(failures[0][1], PermissionError)
660+
mock_logging_warning.assert_not_called()
661+
662+
663+
@mock.patch("logging.warning")
664+
def test_list_skills_in_dir_logs_inaccessible_directory_without_on_error(
665+
mock_logging_warning, tmp_path
666+
):
667+
"""Without on_error, an inaccessible child is logged and skipped."""
668+
skills_dir, blocked = _skills_dir_with_blocked_child(tmp_path)
669+
670+
with _deny_child_metadata(blocked):
671+
skills = list_skills_in_dir(skills_dir)
672+
673+
assert list(skills) == ["good"]
674+
mock_logging_warning.assert_called_once()
675+
args, _ = mock_logging_warning.call_args
676+
assert "Skipping invalid skill" in args[0]
677+
assert args[1] == "blocked-skill"
678+
679+
680+
def test_list_skills_in_dir_on_error_can_abort_for_inaccessible_directory(
681+
tmp_path,
682+
):
683+
"""Raising from on_error aborts when directory metadata cannot be read."""
684+
skills_dir, blocked = _skills_dir_with_blocked_child(tmp_path)
685+
686+
def strict(skill_id, error):
687+
del skill_id # Unused: the error already names the skill.
688+
raise error
689+
690+
with _deny_child_metadata(blocked):
691+
with pytest.raises(PermissionError, match="Permission denied"):
692+
list_skills_in_dir(skills_dir, on_error=strict)
693+
694+
695+
def test_list_skills_in_dir_reports_unsearchable_directory(tmp_path):
696+
"""Children of a non searchable skills directory are reported to on_error."""
697+
if not hasattr(os, "geteuid") or os.geteuid() == 0:
698+
pytest.skip("directory search permission is not enforced for this user")
699+
700+
skills_dir, _ = _skills_dir_with_blocked_child(tmp_path)
701+
failures = []
702+
# Readable, but not searchable: listing works and stat of each child fails.
703+
skills_dir.chmod(0o644)
704+
try:
705+
skills = list_skills_in_dir(
706+
skills_dir,
707+
on_error=lambda skill_id, error: failures.append((skill_id, error)),
708+
)
709+
finally:
710+
skills_dir.chmod(0o755)
711+
712+
assert skills == {}
713+
assert sorted(skill_id for skill_id, _ in failures) == [
714+
"blocked-skill",
715+
"good",
716+
"notes.txt",
717+
]
718+
assert all(isinstance(error, PermissionError) for _, error in failures)
719+
720+
721+
def test_list_skills_in_dir_skips_broken_symlink(tmp_path):
722+
"""A dangling symlink is skipped and is not reported as a skill failure."""
723+
skills_dir = tmp_path / "skills"
724+
skills_dir.mkdir()
725+
(skills_dir / "good").mkdir()
726+
(skills_dir / "good" / "SKILL.md").write_text(
727+
"---\nname: good\ndescription: desc\n---\nbody"
728+
)
729+
(skills_dir / "missing").symlink_to(skills_dir / "nowhere")
730+
failures = []
731+
732+
skills = list_skills_in_dir(
733+
skills_dir,
734+
on_error=lambda skill_id, error: failures.append((skill_id, error)),
735+
)
736+
737+
assert list(skills) == ["good"]
738+
assert failures == []
739+
740+
589741
def test__load_skill_from_zip_bytes():
590742
"""Tests loading a skill directly from in-memory zip file bytes."""
591743

0 commit comments

Comments
 (0)