Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(skills): improve read_skill file recovery (#1347) (#1370)

evan188199-tech9ad83d0630dd
diff --git a/deeptutor/tools/builtin/__init__.py b/deeptutor/tools/builtin/__init__.py
index 5aedf8e07..e6d6df4ca 100644
--- a/deeptutor/tools/builtin/__init__.py
+++ b/deeptutor/tools/builtin/__init__.py
@@ -210,6 +210,12 @@ class KbFilesTool(_PromptHintsMixin, BaseTool):
)
+# How many of a skill's files a not-found message names before it truncates.
+# Enough to identify the right path in any skill shaped like the ones shipped;
+# short enough that a skill carrying a reference tree cannot flood the turn.
+_SKILL_FILE_LIST_LIMIT = 40
+
+
def _kb_files_limit(raw: Any) -> int:
"""Clamp a model-supplied ``limit`` into range; fall back on anything unusable."""
try:
@@ -1429,10 +1435,19 @@ class ReadSkillTool(_PromptHintsMixin, BaseTool):
try:
content = service.read_skill_file(name, rel_path)
except SkillFileNotFoundError:
+ # The skill resolved, so the name was right and only the path
+ # was wrong — naming its actual files is what ends the retry
+ # loop. A skill with a large references/ tree would otherwise
+ # spend the turn's context listing itself, so the list is
+ # bounded and says when it was cut.
+ available = service.list_skill_files(name)
+ shown = ", ".join(available[:_SKILL_FILE_LIST_LIMIT]) or "none"
+ if len(available) > _SKILL_FILE_LIST_LIMIT:
+ shown += f", … ({len(available) - _SKILL_FILE_LIST_LIMIT} more)"
return ToolResult(
content=(
- f"(file not found in skill {name!r}: {rel_path!r} — "
- "check the skill's SKILL.md or references/ for the correct path)"
+ f"(file not found: {rel_path!r} does not exist in skill "
+ f"{name!r}, which holds: {shown})"
),
success=False,
)
@@ -1444,9 +1459,15 @@ class ReadSkillTool(_PromptHintsMixin, BaseTool):
content=content,
metadata={"skill": name, "file": rel_path, "char_count": len(content)},
)
+ available_names: set[str] = set()
+ for service in services:
+ available_names.update(skill.name for skill in service.list_skills())
+ available_skills = ", ".join(sorted(available_names))
+ available_hint = f". Available skills: {available_skills}" if available_skills else ""
return ToolResult(
content=(
- f"(skill not found: {name!r} — use a name exactly as listed in the Skills section)"
+ f"(skill not found: {name!r} — use a name exactly as listed in the "
+ f"Skills section{available_hint})"
),
success=False,
)
diff --git a/tests/tools/builtin/test_read_skill_tool.py b/tests/tools/builtin/test_read_skill_tool.py
new file mode 100644
index 000000000..a5fa2bc8c
--- /dev/null
+++ b/tests/tools/builtin/test_read_skill_tool.py
@@ -0,0 +1,103 @@
+from __future__ import annotations
+
+from pathlib import Path
+from types import SimpleNamespace
+
+import pytest
+
+from deeptutor.services.skill.service import (
+ SkillFileNotFoundError,
+ SkillService,
+)
+from deeptutor.tools.builtin import ReadSkillTool
+
+
+def _write_skill(root: Path, name: str) -> None:
+ skill_dir = root / name
+ skill_dir.mkdir(parents=True)
+ (skill_dir / "SKILL.md").write_text("---\nname: demo\ndescription: Demo\n---\n")
+
+
+def _use_service(monkeypatch: pytest.MonkeyPatch, service: SkillService) -> None:
+ monkeypatch.setattr("deeptutor.services.skill.get_skill_service", lambda: service)
+ monkeypatch.setattr(
+ "deeptutor.multi_user.context.get_current_user",
+ lambda: SimpleNamespace(id="u_test", is_admin=False),
+ )
+ monkeypatch.setattr(
+ "deeptutor.multi_user.skill_access.assigned_skill_ids",
+ lambda _user_id: set(),
+ )
+
+
+async def test_missing_file_names_the_existing_skill(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ root = tmp_path / "skills"
+ _write_skill(root, "ncea-tutor")
+ (root / "ncea-tutor" / "references").mkdir()
+ (root / "ncea-tutor" / "references" / "outline.md").write_text("outline")
+ _use_service(monkeypatch, SkillService(root=root, builtin_root=None))
+
+ result = await ReadSkillTool().execute(name="ncea-tutor", file="references/qbank.md")
+
+ assert result.success is False
+ assert "file not found: 'references/qbank.md'" in result.content
+ assert "skill 'ncea-tutor'" in result.content
+ # Naming the files it does hold is what ends the retry loop — the model
+ # can see the path it should have asked for.
+ assert "SKILL.md, references/outline.md" in result.content
+
+
+async def test_missing_skill_lists_available_skills(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ root = tmp_path / "skills"
+ _write_skill(root, "another-skill")
+ _use_service(monkeypatch, SkillService(root=root, builtin_root=None))
+
+ result = await ReadSkillTool().execute(name="ghost-skill")
+
+ assert result.success is False
+ assert "skill not found: 'ghost-skill'" in result.content
+ assert "Available skills: another-skill" in result.content
+
+
+def test_missing_file_has_a_distinct_service_exception(tmp_path: Path) -> None:
+ root = tmp_path / "skills"
+ _write_skill(root, "demo")
+ service = SkillService(root=root, builtin_root=None)
+
+ with pytest.raises(SkillFileNotFoundError):
+ service.read_skill_file("demo", "references/missing.md")
+
+
+async def test_a_large_skill_does_not_flood_the_turn_with_its_own_file_tree(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """The listing identifies the right path; it is not an inventory.
+
+ A skill carrying a big references/ tree would otherwise spend the turn's
+ context listing itself on every wrong path.
+ """
+ from deeptutor.tools.builtin import _SKILL_FILE_LIST_LIMIT
+
+ root = tmp_path / "skills"
+ _write_skill(root, "big-skill")
+ refs = root / "big-skill" / "references"
+ refs.mkdir()
+ extra = 25
+ # +1 for the SKILL.md every skill has: the cut counts total files, not
+ # just the ones this loop writes.
+ for index in range(_SKILL_FILE_LIST_LIMIT + extra):
+ (refs / f"note-{index:03d}.md").write_text("x")
+ _use_service(monkeypatch, SkillService(root=root, builtin_root=None))
+
+ result = await ReadSkillTool().execute(name="big-skill", file="references/absent.md")
+
+ assert result.success is False
+ assert result.content.count(".md") <= _SKILL_FILE_LIST_LIMIT + 1
+ assert f"({extra + 1} more)" in result.content