import os from pathlib import Path import pytest from nanobot.agent.tools.apply_patch import ApplyPatchTool from nanobot.agent.tools.file_state import file_read_context from nanobot.agent.tools.filesystem import ( EditFileTool, ListDirTool, ReadFileTool, WriteFileTool, ) from nanobot.execution.files import WorkspaceFiles from nanobot.security.workspace_access import ( bind_workspace_scope, build_workspace_scope, reset_workspace_scope, ) @pytest.mark.parametrize("access_mode", ["restricted", "full"]) async def test_file_tools_treat_runtime_memory_state_as_read_only(tmp_path, access_mode): agent_workspace = tmp_path / "agent" project = agent_workspace if access_mode == "restricted" else tmp_path / "project" (project / "memory").mkdir(parents=True) (agent_workspace / "memory").mkdir(parents=True, exist_ok=True) read = ReadFileTool(workspace=agent_workspace) write = WriteFileTool(workspace=agent_workspace) edit = EditFileTool(workspace=agent_workspace) patch = ApplyPatchTool(workspace=agent_workspace) token = bind_workspace_scope(build_workspace_scope(project, access_mode)) try: for name in ("history.jsonl", ".cursor", ".dream_cursor"): file = agent_workspace / "memory" / name file.write_text("original runtime state", encoding="utf-8") path = file.relative_to(project).as_posix() if project == agent_workspace else str(file) assert "original runtime state" in await read.execute(path=path) results = ( await write.execute(path=path, content="changed"), await edit.execute(path=path, old_text="original runtime state", new_text="changed"), await patch.execute(edits=[{"path": path, "action": "add", "new_text": "changed"}]), ) assert all(result.is_error and "managed by the runtime" in result for result in results) assert file.read_text(encoding="utf-8") == "original runtime state" for path in ("memory/MEMORY.md", "memory/wiki/topic.md"): await write.execute(path=path, content="Editable notes") assert (project / path).read_text(encoding="utf-8") == "Editable notes" if project != agent_workspace: # Protection belongs to the agent's state, not to every file with the same name. await write.execute(path="memory/.cursor", content="Project-specific cursor") assert (project / "memory" / ".cursor").read_text(encoding="utf-8") == "Project-specific cursor" finally: reset_workspace_scope(token) @pytest.mark.skipif(not hasattr(os, "mkfifo"), reason="requires a POSIX named pipe") @pytest.mark.parametrize("operation", ["write", "patch"]) async def test_file_mutation_rejects_named_pipe_without_opening_it(tmp_path, monkeypatch, operation): pipe = (tmp_path / "pipe").resolve() os.mkfifo(pipe) open_file = Path.open def reject_blocking_open(path, *args, **kwargs): if path == pipe: raise AssertionError("Opening a named pipe can block the agent") return open_file(path, *args, **kwargs) monkeypatch.setattr(Path, "open", reject_blocking_open) if operation == "write": result = await WriteFileTool(workspace=tmp_path, allowed_dir=tmp_path).execute( path="pipe", content="hello", ) else: result = await ApplyPatchTool(workspace=tmp_path, allowed_dir=tmp_path).execute( edits=[{"path": "pipe", "action": "add", "new_text": "hello"}], ) assert result.is_error assert "Not a regular file: pipe" in result @pytest.mark.skipif(not hasattr(os, "O_NOFOLLOW"), reason="requires no-follow file opens") @pytest.mark.parametrize("operation", ["write", "patch"]) async def test_file_mutation_does_not_follow_a_symlink_swapped_in_after_authorization( tmp_path, monkeypatch, operation, ): workspace = (tmp_path / "workspace").resolve() workspace.mkdir() inside = workspace / "inside.txt" outside = tmp_path / "private.txt" inside.write_text("before", encoding="utf-8") outside.write_text("private", encoding="utf-8") authorize = WorkspaceFiles.edit_target def swap_after_authorize(files, path): target = authorize(files, path) inside.unlink() inside.symlink_to(outside) return target monkeypatch.setattr(WorkspaceFiles, "edit_target", swap_after_authorize) if operation == "write": result = await WriteFileTool(workspace=workspace, allowed_dir=workspace).execute( path="inside.txt", content="changed", ) else: result = await ApplyPatchTool(workspace=workspace, allowed_dir=workspace).execute( edits=[{"path": "inside.txt", "action": "add", "new_text": "changed"}], ) assert result.is_error assert outside.read_text(encoding="utf-8") == "private" async def test_exact_edit_grant_does_not_list_sibling_files(tmp_path): workspace = tmp_path / "workspace" workspace.mkdir() outside = tmp_path / "outside" outside.mkdir() target = outside / "approved.txt" (outside / "approved-secret.txt").write_text("private", encoding="utf-8") tool = EditFileTool( workspace=workspace, allowed_dir=workspace, extra_write_allowed_files=[target], ) missing = await tool.execute(path=str(target), old_text="old", new_text="new") assert "File not found" in missing assert "approved-secret.txt" not in missing assert not target.exists() created = await tool.execute(path=str(target), old_text="", new_text="approved contents") assert not getattr(created, "is_error", False) assert target.read_text(encoding="utf-8") == "approved contents" @pytest.mark.asyncio @pytest.mark.parametrize("newline", ["\n", "\r\n", "\r"]) @pytest.mark.parametrize("operation", ["write", "edit_new", "edit_empty"]) async def test_file_creation_preserves_provided_newlines(tmp_path, newline, operation): target = tmp_path / "nested" / "script.py" content = f"first = 1{newline}second = 2{newline}" if operation == "write": result = await WriteFileTool(workspace=tmp_path).execute( path=str(target), content=content, ) else: if operation == "edit_empty": target.parent.mkdir() target.touch() result = await EditFileTool(workspace=tmp_path).execute( path=str(target), old_text="", new_text=content, ) assert "Error" not in result assert target.read_bytes() == content.encode("utf-8") class TestReadFileTool: @pytest.fixture() def tool(self, tmp_path): return ReadFileTool(workspace=tmp_path) @pytest.fixture() def sample_file(self, tmp_path): f = tmp_path / "sample.txt" f.write_text("\n".join(f"line {i}" for i in range(1, 21)), encoding="utf-8") return f @pytest.mark.asyncio async def test_basic_read_has_line_numbers(self, tool, sample_file): result = await tool.execute(path=str(sample_file)) assert "1| line 1" in result assert "20| line 20" in result @pytest.mark.asyncio async def test_offset_and_limit(self, tool, sample_file): result = await tool.execute(path=str(sample_file), offset=5, limit=3) assert "5| line 5" in result assert "7| line 7" in result assert "8| line 8" not in result assert "Use offset=8 to continue" in result @pytest.mark.asyncio async def test_offset_beyond_end(self, tool, sample_file): result = await tool.execute(path=str(sample_file), offset=999) assert "Error" in result assert "beyond end" in result @pytest.mark.asyncio async def test_end_of_file_marker(self, tool, sample_file): result = await tool.execute(path=str(sample_file), offset=1, limit=9999) assert "End of file" in result @pytest.mark.asyncio async def test_empty_file(self, tool, tmp_path): f = tmp_path / "empty.txt" f.write_text("", encoding="utf-8") result = await tool.execute(path=str(f)) assert "Empty file" in result @pytest.mark.asyncio async def test_image_file_returns_multimodal_blocks(self, tool, tmp_path): f = tmp_path / "pixel.png" f.write_bytes(b"\x89PNG\r\n\x1a\nfake-png-data") result = await tool.execute(path=str(f)) assert isinstance(result, list) assert result[0]["type"] == "image_url" assert result[0]["image_url"]["url"].startswith("data:image/png;base64,") assert result[0]["_meta"]["path"] == str(f) assert result[1] == {"type": "text", "text": f"(Image file: {f})"} @pytest.mark.asyncio async def test_file_not_found(self, tool, tmp_path): result = await tool.execute(path=str(tmp_path / "nope.txt")) assert "Error" in result assert "not found" in result @pytest.mark.asyncio async def test_workspace_relative_builtin_skill_read_falls_back_to_packaged_skill(self, tool): result = await tool.execute(path="skills/cron/SKILL.md", limit=5) assert "Error" not in result assert "cron" in result.lower() @pytest.mark.asyncio async def test_missing_path_returns_clear_error(self, tool): result = await tool.execute() assert result == "Error reading file: Unknown path" @pytest.mark.asyncio async def test_char_budget_trims(self, tool, tmp_path): f = tmp_path / "big.txt" f.write_text("\n".join("x" * 110 for _ in range(2000)), encoding="utf-8") result = await tool.execute(path=str(f)) assert len(result) <= ReadFileTool._MAX_CHARS + 500 assert "Use offset=" in result @pytest.mark.asyncio @pytest.mark.parametrize("following", ["", "\nsecond line"]) async def test_oversized_first_line_is_explicitly_truncated(self, tool, tmp_path, following): f = tmp_path / "minified.txt" original = "界" * (ReadFileTool._MAX_CHARS + 100) + "OMITTED" + following f.write_text(original, encoding="utf-8") with file_read_context("read-1", lambda: {}): first = await tool.execute(path=str(f), limit=1) assert first.startswith("1| 界") assert "OMITTED" not in first assert len(first) <= ReadFileTool._MAX_CHARS + 500 assert "Line 1 truncated; its remaining characters are not shown" in first assert "Use exec" in first assert "column" not in tool.parameters["properties"] assert f.read_text(encoding="utf-8") == original with file_read_context("read-2", lambda: {"read-1": first}): repeated = await tool.execute(path=str(f), limit=1) assert "File unchanged" in repeated if following: assert "Use offset=2 to continue" in first with file_read_context("read-3", lambda: {"read-1": first}): second = await tool.execute(path=str(f), offset=2, limit=1) assert "2| second line" in second assert "End of file" in second else: assert "End of file" in first assert "Use offset=" not in first @pytest.mark.asyncio async def test_long_middle_line_advances_to_following_content(self, tool, tmp_path): f = tmp_path / "bundle.txt" f.write_text("first\n" + "z" * (ReadFileTool._MAX_CHARS * 2) + "\nlast\n") first = await tool.execute(path=str(f)) assert "Use offset=2 to continue" in first assert "truncated" not in first second = await tool.execute(path=str(f), offset=2) assert second.startswith("2| z") assert len(second) <= ReadFileTool._MAX_CHARS + 500 assert "Line 2 truncated" in second assert "Use offset=3 to continue" in second third = await tool.execute(path=str(f), offset=3) assert "3| last" in third assert "End of file" in third @pytest.mark.asyncio async def test_line_exactly_fitting_budget_is_not_truncated(self, tool, tmp_path): f = tmp_path / "exact.txt" line = "x" * (ReadFileTool._MAX_CHARS - len("1| ")) f.write_text(line + "\nlast") first = await tool.execute(path=str(f)) assert first.split("\n\n")[0] == "1| " + line assert "truncated" not in first assert "Use offset=2 to continue" in first @pytest.mark.asyncio async def test_oversized_file_is_rejected_before_read(self, tool, tmp_path, monkeypatch): f = tmp_path / "huge.txt" with f.open("wb") as stream: stream.truncate(ReadFileTool._MAX_FILE_SIZE_BYTES + 1) def fail_read_bytes(self): raise AssertionError("oversized file content should not be loaded") monkeypatch.setattr(type(f), "read_bytes", fail_read_bytes) result = await tool.execute(path=str(f)) assert "File too large to read" in result assert "Maximum is 100 MiB" in result class TestEditFileTool: @pytest.fixture() def tool(self, tmp_path): return EditFileTool(workspace=tmp_path) @pytest.mark.asyncio async def test_exact_match(self, tool, tmp_path): f = tmp_path / "a.py" f.write_text("hello world", encoding="utf-8") result = await tool.execute(path=str(f), old_text="world", new_text="earth") assert "Patch applied:" in result assert f.read_text() == "hello earth" @pytest.mark.asyncio async def test_identical_replacement_returns_clear_error(self, tool, tmp_path): f = tmp_path / "a.py" f.write_text("hello world", encoding="utf-8") result = await tool.execute(path=str(f), old_text="world", new_text="world") assert result == "Error: new_text must be different from old_text." assert f.read_text(encoding="utf-8") == "hello world" @pytest.mark.asyncio async def test_crlf_normalisation(self, tool, tmp_path): f = tmp_path / "crlf.py" f.write_bytes(b"line1\r\nline2\r\nline3") result = await tool.execute( path=str(f), old_text="line1\nline2", new_text="LINE1\nLINE2", ) assert "Patch applied:" in result raw = f.read_bytes() assert b"LINE1" in raw assert b"\r\n" in raw @pytest.mark.asyncio async def test_trim_fallback(self, tool, tmp_path): f = tmp_path / "indent.py" f.write_text(" def foo():\n pass\n", encoding="utf-8") result = await tool.execute( path=str(f), old_text="def foo():\n pass", new_text="def bar():\n return 1", ) assert "Patch applied:" in result assert "bar" in f.read_text() @pytest.mark.asyncio async def test_ambiguous_match(self, tool, tmp_path): f = tmp_path / "dup.py" f.write_text("aaa\nbbb\naaa\nbbb\n", encoding="utf-8") result = await tool.execute(path=str(f), old_text="aaa\nbbb", new_text="xxx") assert "appears" in result.lower() or "Warning" in result @pytest.mark.asyncio async def test_replace_all(self, tool, tmp_path): f = tmp_path / "multi.py" f.write_text("foo bar foo bar foo", encoding="utf-8") result = await tool.execute( path=str(f), old_text="foo", new_text="baz", replace_all=True, ) assert "Patch applied:" in result assert f.read_text() == "baz bar baz bar baz" @pytest.mark.asyncio async def test_not_found(self, tool, tmp_path): f = tmp_path / "nf.py" f.write_text("hello", encoding="utf-8") result = await tool.execute(path=str(f), old_text="xyz", new_text="abc") assert "Error" in result assert "not found" in result @pytest.mark.asyncio async def test_missing_new_text_returns_clear_error(self, tool, tmp_path): f = tmp_path / "a.py" f.write_text("hello", encoding="utf-8") result = await tool.execute(path=str(f), old_text="hello") assert result == "Error editing file: Unknown new_text" class TestListDirTool: @pytest.fixture() def tool(self, tmp_path): return ListDirTool(workspace=tmp_path) @pytest.fixture() def populated_dir(self, tmp_path): (tmp_path / "src").mkdir() (tmp_path / "src" / "main.py").write_text("pass") (tmp_path / "src" / "utils.py").write_text("pass") (tmp_path / "README.md").write_text("hi") (tmp_path / ".git").mkdir() (tmp_path / ".git" / "config").write_text("x") (tmp_path / "node_modules").mkdir() (tmp_path / "node_modules" / "pkg").mkdir() return tmp_path @pytest.mark.asyncio async def test_basic_list(self, tool, populated_dir): result = await tool.execute(path=str(populated_dir)) assert "README.md" in result assert "src" in result assert ".git" not in result assert "node_modules" not in result @pytest.mark.asyncio async def test_recursive(self, tool, populated_dir): result = await tool.execute(path=str(populated_dir), recursive=True) normalized = result.replace("\\", "/") assert "src/main.py" in normalized assert "src/utils.py" in normalized assert "README.md" in result assert ".git" not in result assert "node_modules" not in result @pytest.mark.asyncio @pytest.mark.parametrize("relative_root", ["build", "build/project"]) async def test_recursive_ignores_only_descendants(self, tool, tmp_path, relative_root): root = tmp_path / relative_root (root / "src").mkdir(parents=True) (root / "src" / "main.py").write_text("pass") (root / "README.md").write_text("hi") (root / ".git").mkdir() (root / ".git" / "config").write_text("ignored") (root / "src" / "node_modules").mkdir() (root / "src" / "node_modules" / "package.json").write_text("{}") result = await tool.execute(path=str(root), recursive=True) assert set(result.replace("\\", "/").splitlines()) == { "README.md", "src/", "src/main.py", } @pytest.mark.asyncio async def test_max_entries_truncation(self, tool, tmp_path): for i in range(10): (tmp_path / f"file_{i}.txt").write_text("x") result = await tool.execute(path=str(tmp_path), max_entries=3) assert "truncated" in result assert "3 of 10" in result @pytest.mark.asyncio async def test_recursive_limit_preserves_sorted_prefix_and_exact_total(self, tool, tmp_path): (tmp_path / "z").mkdir() (tmp_path / "z" / "a.txt").touch() (tmp_path / "a").mkdir() (tmp_path / "a" / "z.txt").touch() (tmp_path / "b.txt").touch() (tmp_path / ".git").mkdir() (tmp_path / ".git" / "ignored").touch() result = await tool.execute(path=".", recursive=True, max_entries=3) assert result.replace("\\", "/").splitlines() == [ "a/", "a/z.txt", "b.txt", "", "(truncated, showing first 3 of 5 entries)", ] @pytest.mark.asyncio async def test_empty_dir(self, tool, tmp_path): d = tmp_path / "empty" d.mkdir() result = await tool.execute(path=str(d)) assert "empty" in result.lower() @pytest.mark.asyncio async def test_not_found(self, tool, tmp_path): result = await tool.execute(path=str(tmp_path / "nope")) assert "Error" in result assert "not found" in result @pytest.mark.asyncio async def test_missing_path_returns_clear_error(self, tool): result = await tool.execute() assert result == "Error listing directory: Unknown path" class TestWorkspaceRestriction: @pytest.mark.asyncio async def test_read_blocked_outside_workspace(self, tmp_path): workspace = tmp_path / "ws" workspace.mkdir() outside = tmp_path / "outside" outside.mkdir() secret = outside / "secret.txt" secret.write_text("top secret") tool = ReadFileTool(workspace=workspace, allowed_dir=workspace) result = await tool.execute(path=str(secret)) assert "Error" in result assert "outside" in result.lower() @pytest.mark.asyncio async def test_read_allowed_with_extra_dir(self, tmp_path): workspace = tmp_path / "ws" workspace.mkdir() skills_dir = tmp_path / "skills" skills_dir.mkdir() skill_file = skills_dir / "test_skill" / "SKILL.md" skill_file.parent.mkdir() skill_file.write_text("# Test Skill\nDo something.") tool = ReadFileTool( workspace=workspace, allowed_dir=workspace, extra_read_allowed_dirs=[skills_dir], ) result = await tool.execute(path=str(skill_file)) assert "Test Skill" in result assert "Error" not in result @pytest.mark.asyncio async def test_read_allowed_in_media_dir(self, tmp_path, monkeypatch): workspace = tmp_path / "ws" workspace.mkdir() media_dir = tmp_path / "media" media_dir.mkdir() media_file = media_dir / "photo.txt" media_file.write_text("shared media", encoding="utf-8") monkeypatch.setattr("nanobot.config.loader._current_config_path", tmp_path / "config.json") tool = ReadFileTool(workspace=workspace, allowed_dir=workspace) result = await tool.execute(path=str(media_file)) assert "shared media" in result assert "Error" not in result @pytest.mark.asyncio async def test_write_blocked_in_media_dir_by_default(self, tmp_path, monkeypatch): workspace = tmp_path / "ws" workspace.mkdir() media_dir = tmp_path / "media" media_dir.mkdir() monkeypatch.setattr("nanobot.config.loader._current_config_path", tmp_path / "config.json") tool = WriteFileTool(workspace=workspace, allowed_dir=workspace) result = await tool.execute(path=str(media_dir / "hack.txt"), content="pwned") assert "Error" in result assert "outside" in result.lower() assert not (media_dir / "hack.txt").exists() @pytest.mark.asyncio async def test_legacy_extra_allowed_dirs_does_not_widen_write(self, tmp_path): workspace = tmp_path / "ws" workspace.mkdir() skills_dir = tmp_path / "skills" skills_dir.mkdir() tool = WriteFileTool( workspace=workspace, allowed_dir=workspace, extra_allowed_dirs=[skills_dir], ) result = await tool.execute(path=str(skills_dir / "hack.txt"), content="pwned") assert "Error" in result assert "outside" in result.lower() assert not (skills_dir / "hack.txt").exists() @pytest.mark.asyncio async def test_write_allowed_with_extra_write_dir(self, tmp_path): workspace = tmp_path / "ws" workspace.mkdir() writable = tmp_path / "writable" writable.mkdir() tool = WriteFileTool( workspace=workspace, allowed_dir=workspace, extra_write_allowed_dirs=[writable], ) result = await tool.execute(path=str(writable / "ok.txt"), content="allowed") assert "Successfully wrote" in result assert (writable / "ok.txt").read_text(encoding="utf-8") == "allowed" @pytest.mark.asyncio async def test_extra_write_allowed_files_allow_only_exact_file(self, tmp_path): workspace = tmp_path / "ws" workspace.mkdir() outside = tmp_path / "outside" outside.mkdir() allowed_file = outside / "allowed.txt" child_path = allowed_file / "child.txt" tool = WriteFileTool( workspace=workspace, allowed_dir=workspace, extra_write_allowed_files=[allowed_file], ) exact = await tool.execute(path=str(allowed_file), content="allowed") child = await tool.execute(path=str(child_path), content="blocked") assert "Successfully wrote" in exact assert allowed_file.read_text(encoding="utf-8") == "allowed" assert "Error" in child assert "outside" in child.lower() assert not child_path.exists() @pytest.mark.asyncio async def test_read_still_blocked_for_unrelated_dir(self, tmp_path): workspace = tmp_path / "ws" workspace.mkdir() skills_dir = tmp_path / "skills" skills_dir.mkdir() unrelated = tmp_path / "other" unrelated.mkdir() secret = unrelated / "secret.txt" secret.write_text("nope") tool = ReadFileTool( workspace=workspace, allowed_dir=workspace, extra_allowed_dirs=[skills_dir], ) result = await tool.execute(path=str(secret)) assert "Error" in result assert "outside" in result.lower() @pytest.mark.asyncio async def test_workspace_file_still_readable_with_extra_dirs(self, tmp_path): workspace = tmp_path / "ws" workspace.mkdir() ws_file = workspace / "README.md" ws_file.write_text("hello from workspace") skills_dir = tmp_path / "skills" skills_dir.mkdir() tool = ReadFileTool( workspace=workspace, allowed_dir=workspace, extra_allowed_dirs=[skills_dir], ) result = await tool.execute(path=str(ws_file)) assert "hello from workspace" in result assert "Error" not in result @pytest.mark.asyncio async def test_edit_blocked_in_extra_dir(self, tmp_path): workspace = tmp_path / "ws" workspace.mkdir() skills_dir = tmp_path / "skills" skills_dir.mkdir() skill_file = skills_dir / "weather" / "SKILL.md" skill_file.parent.mkdir() skill_file.write_text("# Weather\nOriginal content.") tool = EditFileTool( workspace=workspace, allowed_dir=workspace, extra_allowed_dirs=[skills_dir], ) result = await tool.execute( path=str(skill_file), old_text="Original content.", new_text="Hacked content.", ) assert "Error" in result assert "outside" in result.lower() assert skill_file.read_text() == "# Weather\nOriginal content." @pytest.mark.asyncio async def test_edit_allowed_with_extra_write_dir(self, tmp_path): workspace = tmp_path / "ws" workspace.mkdir() writable = tmp_path / "writable" writable.mkdir() target = writable / "note.txt" target.write_text("before\n", encoding="utf-8") tool = EditFileTool( workspace=workspace, allowed_dir=workspace, extra_write_allowed_dirs=[writable], ) result = await tool.execute( path=str(target), old_text="before", new_text="after", ) assert "Patch applied:" in result assert target.read_text(encoding="utf-8") == "after\n" @pytest.mark.parametrize("offset, limit", [(2.0, 1), (2, 1.0)]) async def test_read_file_accepts_json_integers_without_changing_arguments(tmp_path, offset, limit): from nanobot.agent.tools.registry import ToolRegistry, is_tool_error_result path = tmp_path / "integer-offset.txt" path.write_text("first\nsecond\nthird\n", encoding="utf-8") registry = ToolRegistry() registry.register(ReadFileTool(workspace=tmp_path)) arguments = {"path": str(path), "offset": offset, "limit": limit} result = await registry.execute("read_file", arguments) assert not is_tool_error_result(result) assert result.startswith("2| second\n") assert "Use offset=3 to continue" in result assert type(arguments["offset"]) is type(offset) assert type(arguments["limit"]) is type(limit) async def test_edit_file_accepts_json_integer_occurrence(tmp_path): from nanobot.agent.tools.registry import ToolRegistry, is_tool_error_result path = tmp_path / "integer-occurrence.txt" path.write_text("first first", encoding="utf-8") registry = ToolRegistry() registry.register(EditFileTool(workspace=tmp_path)) arguments = {"path": str(path), "old_text": "first", "new_text": "second", "occurrence": 2.0} result = await registry.execute("edit_file", arguments) assert not is_tool_error_result(result) assert path.read_text(encoding="utf-8") == "first second" assert type(arguments["occurrence"]) is float