Files
openworker/tests/test_list_files_folders.py
Devika Verma f9413810e7 List folders as well as files in list_files (OPE-203)
The aisuite toolkit's `list_files` keeps only files, so a workspace whose top level holds nothing but a subfolder lists as `[]`. The agent then reads that literally ("the workspace is empty"), asks for another folder, and gives up; the project was one level down the whole time. In one long session this cost a task that another attempt solved after a second look with `find`.

Replace the toolkit's listing with a folder-aware one beside the windowed `read_file`: folders end with `/`, the toolkit's skip-list still hides `.git`, `node_modules` and the rest, `pattern`, `recursive` and `max_results` keep their meaning, and multi-root sessions resolve the same way `read_file` does. The Code, Cowork and explorer tool sets all pick it up.
2026-10-02 15:11:21 +05:30

84 lines
2.9 KiB
Python

"""`list_files` shows folders as well as files (OPE-203).
The aisuite toolkit's listing returns files only, so a workspace whose top level holds
nothing but a subfolder lists as `[]` and the agent concludes the workspace is empty.
Ours lists folders too, marked with a trailing `/`.
"""
from __future__ import annotations
from coworker.agents.base import AgentContext
from coworker.catalog import expand
from coworker.tools.files import file_tools
from coworker.tools.todo import TodoList
def _list_files(ws):
return {t.__name__: t for t in file_tools(str(ws))}["list_files"]
def _site(tmp_path):
site = tmp_path / "personal-site"
(site / "_includes").mkdir(parents=True)
(site / "index.html").write_text("<h1>hi</h1>", encoding="utf-8")
(site / "_includes" / "about.md").write_text("me", encoding="utf-8")
return site
def test_a_workspace_of_subfolders_is_not_reported_empty(tmp_path):
_site(tmp_path)
assert _list_files(tmp_path)(path=".", recursive=False) == ["personal-site/"]
def test_recursive_listing_shows_folders_and_files(tmp_path):
_site(tmp_path)
assert _list_files(tmp_path)(path=".", recursive=True) == [
"personal-site/",
"personal-site/_includes/",
"personal-site/_includes/about.md",
"personal-site/index.html",
]
def test_pattern_and_subpath_apply(tmp_path):
_site(tmp_path)
tool = _list_files(tmp_path)
assert tool(path="personal-site", pattern="*.html", recursive=False) == [
"personal-site/index.html"
]
assert tool(path="personal-site", recursive=False) == [
"personal-site/_includes/",
"personal-site/index.html",
]
def test_ignored_folders_stay_hidden(tmp_path):
_site(tmp_path)
(tmp_path / ".git" / "objects").mkdir(parents=True)
(tmp_path / ".git" / "HEAD").write_text("ref", encoding="utf-8")
(tmp_path / "node_modules" / "left-pad").mkdir(parents=True)
listed = _list_files(tmp_path)(path=".", recursive=True)
assert not any(entry.startswith((".git", "node_modules")) for entry in listed)
assert "personal-site/" in listed
def test_max_results_caps_the_listing(tmp_path):
for i in range(5):
(tmp_path / f"f{i}.txt").write_text("x", encoding="utf-8")
assert len(_list_files(tmp_path)(path=".", max_results=3)) == 3
def test_paths_outside_the_session_are_refused(tmp_path):
tool = _list_files(tmp_path / "ws")
(tmp_path / "ws").mkdir()
assert "error" in tool(path="..")
assert "error" in tool(path="missing")
def test_catalog_registers_the_folder_aware_listing(tmp_path):
ctx = AgentContext(workspace=tmp_path, executor=object(), todo=TodoList())
for capability in ("code_files", "files"):
listings = [t for t in expand([capability], ctx) if t.__name__ == "list_files"]
assert len(listings) == 1, capability
assert hasattr(listings[0], "__coworker_schema__"), capability