Skip to content

Your lightweight open-source personal agent.

37.6 KB
Raw Blame
from __future__ import annotations

import asyncio
import os
import re
import threading
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock
from zipfile import ZipFile

import pytest

from nanobot.agent.loop import AgentLoop
from nanobot.agent.memory import Consolidator
from nanobot.agent.subagent import SubagentManager
from nanobot.agent.tools.registry import is_tool_error_result
from nanobot.agent.tools.search import FindFilesTool, GrepTool
from nanobot.agent.tools.web import WebSearchTool
from nanobot.bus.queue import MessageBus
from nanobot.config.schema import WebSearchConfig
from nanobot.providers.base import GenerationSettings
from nanobot.security.workspace_access import (
    bind_workspace_scope,
    default_workspace_scope,
    reset_workspace_scope,
)
from nanobot.utils.document import extract_text
from nanobot.utils.llm_runtime import LLMRuntime


@pytest.mark.asyncio
async def test_web_search_tool_refreshes_dynamic_config_loader(monkeypatch) -> None:
    tool = WebSearchTool(
        config=WebSearchConfig(provider="brave"),
        config_loader=lambda: WebSearchConfig(provider="duckduckgo", max_results=3),
    )

    async def fake_duckduckgo(self, query: str, n: int) -> str:
        return f"{self.config.provider}:{query}:{n}"

    monkeypatch.setattr(WebSearchTool, "_search_duckduckgo", fake_duckduckgo)

    assert await tool.execute("nanobot") == "duckduckgo:nanobot:3"


@pytest.mark.asyncio
async def test_find_files_filters_by_query_glob_and_type(tmp_path: Path) -> None:
    (tmp_path / "src").mkdir()
    (tmp_path / "src" / "settings_view.tsx").write_text("export {}\n", encoding="utf-8")
    (tmp_path / "src" / "settings_api.py").write_text("pass\n", encoding="utf-8")
    (tmp_path / "README.md").write_text("settings\n", encoding="utf-8")

    tool = FindFilesTool(workspace=tmp_path, allowed_dir=tmp_path)
    result = await tool.execute(
        path=".",
        query="settings",
        glob="src/**",
        type="ts",
    )

    assert result.splitlines() == ["src/settings_view.tsx"]


@pytest.mark.parametrize("tool_name", ["find_files", "grep"])
@pytest.mark.parametrize(
    ("glob", "expected"),
    [
        ("**/*.py", ["main.py", "src/api.py", "src/nested/deep/worker.py"]),
        ("**.py", ["main.py", "src/api.py", "src/nested/deep/worker.py"]),
        ("src/**/*.py", ["src/api.py", "src/nested/deep/worker.py"]),
        ("src/**", ["src/api.py", "src/nested/deep/worker.py"]),
        ("src/*.py", ["src/api.py"]),
        ("src/**/deep/*.py", ["src/nested/deep/worker.py"]),
        (r"src\**\*.py", ["src/api.py", "src/nested/deep/worker.py"]),
    ],
)
async def test_search_recursive_glob_matches_zero_or_more_directories(
    tmp_path: Path, tool_name: str, glob: str, expected: list[str]
) -> None:
    for name in ["main.py", "src/api.py", "src/nested/deep/worker.py", "notes.md"]:
        path = tmp_path / name
        path.parent.mkdir(parents=True, exist_ok=True)
        path.write_text("needle\n", encoding="utf-8")

    if tool_name == "find_files":
        result = await FindFilesTool(workspace=tmp_path, allowed_dir=tmp_path).execute(
            path=".", glob=glob,
        )
    else:
        result = await GrepTool(workspace=tmp_path, allowed_dir=tmp_path).execute(
            pattern="needle", path=".", glob=glob, output_mode="files_with_matches",
        )

    assert sorted(result.splitlines()) == expected


@pytest.mark.asyncio
async def test_find_files_can_include_directories(tmp_path: Path) -> None:
    (tmp_path / "src" / "settings").mkdir(parents=True)
    (tmp_path / "src" / "settings" / "index.ts").write_text("export {}\n", encoding="utf-8")

    tool = FindFilesTool(workspace=tmp_path, allowed_dir=tmp_path)
    result = await tool.execute(path="src", query="settings", include_dirs=True)

    assert "src/settings/" in result.splitlines()
    assert "src/settings/index.ts" in result.splitlines()


@pytest.mark.asyncio
async def test_find_files_supports_modified_sort_and_pagination(tmp_path: Path) -> None:
    (tmp_path / "src").mkdir()
    for idx, name in enumerate(("a.py", "b.py", "c.py"), start=1):
        file_path = tmp_path / "src" / name
        file_path.write_text("pass\n", encoding="utf-8")
        os.utime(file_path, (idx, idx))

    tool = FindFilesTool(workspace=tmp_path, allowed_dir=tmp_path)
    result = await tool.execute(
        path="src",
        type="py",
        sort="modified",
        head_limit=1,
        offset=1,
    )

    assert result.splitlines()[0] == "src/b.py"
    assert "pagination: limit=1, offset=1" in result


@pytest.mark.asyncio
async def test_find_files_rejects_paths_outside_workspace(tmp_path: Path) -> None:
    outside = tmp_path.parent / "outside-find-files.txt"
    outside.write_text("secret\n", encoding="utf-8")

    tool = FindFilesTool(workspace=tmp_path, allowed_dir=tmp_path)
    result = await tool.execute(path=str(outside))

    assert result.startswith("Error:")


@pytest.mark.asyncio
async def test_find_files_worker_preserves_current_workspace_scope(tmp_path: Path) -> None:
    project = tmp_path / "project"
    project.mkdir()
    (project / "inside.txt").write_text("ok\n", encoding="utf-8")
    (tmp_path / "outside.txt").write_text("nope\n", encoding="utf-8")
    tool = FindFilesTool(workspace=tmp_path, restrict_to_workspace=False)
    token = bind_workspace_scope(default_workspace_scope(project, True))
    try:
        result = await tool.execute(path=".")
    finally:
        reset_workspace_scope(token)

    assert result == "inside.txt"


@pytest.mark.asyncio
async def test_find_files_scan_keeps_event_loop_responsive(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    target = tmp_path / "match.txt"
    target.write_text("ok\n", encoding="utf-8")
    tool = FindFilesTool(workspace=tmp_path, allowed_dir=tmp_path)
    original_iter_paths = tool._iter_paths
    started = threading.Event()
    release = threading.Event()

    def blocking_iter_paths(root: Path, *, include_dirs: bool, budget):
        started.set()
        if not release.wait(timeout=1):
            raise TimeoutError("test did not release find_files traversal")
        yield from original_iter_paths(root, include_dirs=include_dirs, budget=budget)

    monkeypatch.setattr(tool, "_iter_paths", blocking_iter_paths)
    task = asyncio.create_task(tool.execute(path="."))
    try:
        assert await asyncio.to_thread(started.wait, 0.5)
        for _ in range(3):
            await asyncio.sleep(0.01)
        assert not task.done()
    finally:
        release.set()

    assert await asyncio.wait_for(task, timeout=0.5) == "match.txt"


@pytest.mark.asyncio
async def test_find_files_cancellation_stops_worker_scan(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    tool = FindFilesTool(workspace=tmp_path, allowed_dir=tmp_path)
    started = threading.Event()
    stopped = threading.Event()

    def cancellable_iter_paths(root: Path, *, include_dirs: bool, budget):
        del root, include_dirs
        started.set()
        if not budget.cancelled.wait(timeout=1):
            raise TimeoutError("find_files worker did not receive cancellation")
        stopped.set()
        if False:
            yield

    monkeypatch.setattr(tool, "_iter_paths", cancellable_iter_paths)
    task = asyncio.create_task(tool.execute(path="."))
    assert await asyncio.to_thread(started.wait, 0.5)

    task.cancel()
    with pytest.raises(asyncio.CancelledError):
        await asyncio.wait_for(task, timeout=0.2)

    assert await asyncio.to_thread(stopped.wait, 0.5)


@pytest.mark.asyncio
async def test_find_files_path_limit_stops_after_pagination_lookahead(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    paths = [tmp_path / name for name in ("a.txt", "b.txt", "c.txt")]
    for path in paths:
        path.write_text("ok\n", encoding="utf-8")
    tool = FindFilesTool(workspace=tmp_path, allowed_dir=tmp_path)
    visited: list[str] = []

    def ordered_iter_paths(root: Path, *, include_dirs: bool, budget):
        del include_dirs
        for path in paths:
            budget.checkpoint()
            visited.append(path.name)
            yield tool._entry(path, root, is_dir=False)

    monkeypatch.setattr(tool, "_iter_paths", ordered_iter_paths)
    result = await tool.execute(path=".", head_limit=1)

    assert result.splitlines()[0] == "a.txt"
    assert "pagination: limit=1, offset=0" in result
    assert visited == ["a.txt", "b.txt"]


@pytest.mark.asyncio
async def test_find_files_path_budget_counts_directories(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    (tmp_path / "one").mkdir()
    (tmp_path / "two").mkdir()
    monkeypatch.setattr(FindFilesTool, "_MAX_SCAN_PATHS", 1)
    tool = FindFilesTool(workspace=tmp_path, allowed_dir=tmp_path)

    result = await tool.execute(path=".")

    assert result.startswith("Error: find_files scan exceeded 1 paths")


@pytest.mark.asyncio
async def test_find_files_enforces_time_budget(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    (tmp_path / "match.txt").write_text("ok\n", encoding="utf-8")
    monkeypatch.setattr(FindFilesTool, "_MAX_SCAN_SECONDS", 0.0)
    tool = FindFilesTool(workspace=tmp_path, allowed_dir=tmp_path)

    result = await tool.execute(path=".")

    assert result.startswith("Error: find_files scan exceeded 0 seconds")


@pytest.mark.asyncio
async def test_find_files_path_sort_matches_existing_lexicographic_contract(
    tmp_path: Path,
) -> None:
    (tmp_path / "a").mkdir()
    (tmp_path / "a" / "inside.txt").write_text("ok\n", encoding="utf-8")
    (tmp_path / "a+").write_text("ok\n", encoding="utf-8")
    (tmp_path / "a.py").write_text("ok\n", encoding="utf-8")
    tool = FindFilesTool(workspace=tmp_path, allowed_dir=tmp_path)

    result = await tool.execute(path=".", head_limit=0)

    assert result.splitlines() == ["a+", "a.py", "a/inside.txt"]


@pytest.mark.asyncio
async def test_grep_respects_glob_filter_and_context(tmp_path: Path) -> None:
    (tmp_path / "src").mkdir()
    (tmp_path / "src" / "main.py").write_text(
        "alpha\nbeta\nmatch_here\ngamma\n",
        encoding="utf-8",
    )
    (tmp_path / "README.md").write_text("match_here\n", encoding="utf-8")

    tool = GrepTool(workspace=tmp_path, allowed_dir=tmp_path)
    result = await tool.execute(
        pattern="match_here",
        path=".",
        glob="*.py",
        output_mode="content",
        context_before=1,
        context_after=1,
    )

    assert "src/main.py:3" in result
    assert "  2| beta" in result
    assert "> 3| match_here" in result
    assert "  4| gamma" in result
    assert "README.md" not in result


@pytest.mark.asyncio
async def test_grep_defaults_to_match_context(tmp_path: Path) -> None:
    (tmp_path / "src").mkdir()
    (tmp_path / "src" / "main.py").write_text(
        "\n".join(f"line {line}" for line in range(1, 6))
        + "\nmatch_here\n"
        + "\n".join(f"line {line}" for line in range(7, 13)),
        encoding="utf-8",
    )

    tool = GrepTool(workspace=tmp_path, allowed_dir=tmp_path)
    result = await tool.execute(
        pattern="match_here",
        path="src",
    )

    assert "src/main.py:6" in result
    assert "  1| line 1" in result
    assert "> 6| match_here" in result
    assert "  11| line 11" in result
    assert "line 12" not in result


@pytest.mark.asyncio
@pytest.mark.parametrize("declared_dimension", ["A1:B2", "A1:A1", "A1:A2", "A1:B1"])
async def test_grep_searches_xlsx_with_sheet_cell_locator(
    tmp_path: Path, declared_dimension: str, monkeypatch: pytest.MonkeyPatch,
) -> None:
    from openpyxl import Workbook

    monkeypatch.setattr("nanobot.config.loader._current_config_path", tmp_path / "config.json")
    workbook_path = tmp_path / "people.xlsx"
    workbook = Workbook()
    sheet = workbook.active
    sheet.title = "People"
    sheet.append(["Name", "Role"])
    sheet.append(["Ada", "Engineer"])
    workbook.save(workbook_path)
    workbook.close()

    # Some XLSX producers report a smaller used range than their actual cells.
    with ZipFile(workbook_path) as archive:
        entries = {item.filename: archive.read(item) for item in archive.infolist()}
    entries["xl/worksheets/sheet1.xml"] = entries["xl/worksheets/sheet1.xml"].replace(
        b'<dimension ref="A1:B2"', f'<dimension ref="{declared_dimension}"'.encode(),
    )
    with ZipFile(workbook_path, "w") as archive:
        for name, data in entries.items():
            archive.writestr(name, data)

    assert extract_text(workbook_path) == "--- Sheet: People ---\nName\tRole\nAda\tEngineer"

    tool = GrepTool(workspace=tmp_path, allowed_dir=tmp_path)
    result = await tool.execute(
        pattern="Engineer",
        path="people.xlsx",
        fixed_strings=True,
    )

    assert "people.xlsx:3" in result
    assert "sheet='People',row=2,cell=B2" in result
    assert "Ada\tEngineer" in result


@pytest.mark.asyncio
async def test_grep_searches_docx_with_paragraph_locator(tmp_path: Path) -> None:
    from docx import Document

    document_path = tmp_path / "notes.docx"
    document = Document()
    document.add_paragraph("Introduction")
    document.add_paragraph("late needle")
    document.save(document_path)

    tool = GrepTool(workspace=tmp_path, allowed_dir=tmp_path)
    result = await tool.execute(
        pattern="late needle",
        path="notes.docx",
        fixed_strings=True,
    )

    assert "notes.docx:3 [paragraph=2]" in result
    assert "late needle" in result


@pytest.mark.asyncio
async def test_grep_searches_pptx_with_slide_locator(tmp_path: Path) -> None:
    from pptx import Presentation
    from pptx.util import Inches

    presentation_path = tmp_path / "deck.pptx"
    presentation = Presentation()
    slide = presentation.slides.add_slide(presentation.slide_layouts[6])
    textbox = slide.shapes.add_textbox(
        Inches(1), Inches(1), Inches(4), Inches(1)
    )
    textbox.text_frame.text = "slide needle"
    presentation.save(presentation_path)

    tool = GrepTool(workspace=tmp_path, allowed_dir=tmp_path)
    result = await tool.execute(
        pattern="slide needle",
        path="deck.pptx",
        fixed_strings=True,
    )

    assert "deck.pptx:2 [slide=1,line=1]" in result
    assert "slide needle" in result


@pytest.mark.asyncio
async def test_grep_searches_pdf_with_page_locator(tmp_path: Path) -> None:
    import fitz

    pdf_path = tmp_path / "notes.pdf"
    document = fitz.open()
    page = document.new_page()
    page.insert_text((72, 72), "pdf needle")
    document.save(pdf_path)
    document.close()

    tool = GrepTool(workspace=tmp_path, allowed_dir=tmp_path)
    result = await tool.execute(
        pattern="pdf needle",
        path="notes.pdf",
        fixed_strings=True,
    )

    assert "notes.pdf:2 [page=1,line=1]" in result
    assert "pdf needle" in result


@pytest.mark.asyncio
async def test_grep_searches_xlsx_beyond_attachment_preview_limit(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    from openpyxl import Workbook

    from nanobot.utils import document as document_utils

    workbook_path = tmp_path / "long.xlsx"
    workbook = Workbook()
    sheet = workbook.active
    sheet.title = "Data"
    for row in range(1, 20):
        sheet.append([f"ordinary-row-{row}"])
    sheet.append(["late-needle"])
    workbook.save(workbook_path)
    workbook.close()
    monkeypatch.setattr(document_utils, "_MAX_TEXT_LENGTH", 50)

    preview = document_utils.extract_text(workbook_path)
    assert preview is not None
    assert "late-needle" not in preview

    tool = GrepTool(workspace=tmp_path, allowed_dir=tmp_path)
    result = await tool.execute(
        pattern="late-needle",
        path="long.xlsx",
        fixed_strings=True,
    )

    assert "late-needle" in result
    assert "sheet='Data',row=20,cell=A20" in result
    assert "No matches found" not in result


@pytest.mark.asyncio
async def test_grep_keeps_an_oversized_matching_line_visible(tmp_path: Path) -> None:
    long_line = "x" * 130_000 + "needle" + "y" * 10_000
    (tmp_path / "huge-line.txt").write_text(long_line, encoding="utf-8")
    tool = GrepTool(workspace=tmp_path, allowed_dir=tmp_path)

    result = await tool.execute(
        pattern="needle",
        path="huge-line.txt",
        fixed_strings=True,
        context_before=0,
        context_after=0,
    )

    assert "huge-line.txt:1" in result
    assert "needle" in result
    assert "No matches found" not in result
    assert len(result) < GrepTool._MAX_RESULT_CHARS


@pytest.mark.asyncio
async def test_grep_size_limit_returns_a_resumable_offset(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    content = "\n".join(f"needle-{line}-" + "x" * 80 for line in range(1, 11))
    (tmp_path / "many.txt").write_text(content, encoding="utf-8")
    monkeypatch.setattr(GrepTool, "_MAX_RESULT_CHARS", 350)
    tool = GrepTool(workspace=tmp_path, allowed_dir=tmp_path)

    first = await tool.execute(
        pattern="needle",
        path="many.txt",
        fixed_strings=True,
        context_before=0,
        context_after=0,
        head_limit=10,
    )
    continuation = re.search(r"use offset=(\d+) to continue", first)

    assert continuation is not None
    next_offset = int(continuation.group(1))
    assert next_offset > 0

    second = await tool.execute(
        pattern="needle",
        path="many.txt",
        fixed_strings=True,
        context_before=0,
        context_after=0,
        head_limit=10,
        offset=next_offset,
    )
    first_headers = {
        line for line in first.splitlines() if line.startswith("many.txt:")
    }
    second_headers = {
        line for line in second.splitlines() if line.startswith("many.txt:")
    }
    assert second_headers
    assert first_headers.isdisjoint(second_headers)


@pytest.mark.asyncio
async def test_grep_reports_an_invalid_pdf_page_range(tmp_path: Path) -> None:
    from pypdf import PdfWriter

    pdf_path = tmp_path / "one-page.pdf"
    writer = PdfWriter()
    writer.add_blank_page(width=100, height=100)
    with pdf_path.open("wb") as output:
        writer.write(output)

    tool = GrepTool(workspace=tmp_path, allowed_dir=tmp_path)
    result = await tool.execute(pattern="needle", path="one-page.pdf", pages="bad")

    assert result.startswith("Error: Invalid PDF page range 'bad'")
    assert "binary/unreadable" not in result

    out_of_bounds = await tool.execute(
        pattern="needle",
        path="one-page.pdf",
        pages="99",
    )
    assert out_of_bounds == (
        "Error: Invalid PDF page range '99': document has 1 page; "
        "use a page number or range within 1-1."
    )


@pytest.mark.asyncio
async def test_grep_supports_case_insensitive_search(tmp_path: Path) -> None:
    (tmp_path / "memory").mkdir()
    (tmp_path / "memory" / "HISTORY.md").write_text(
        "[2026-04-02 10:00] OAuth token rotated\n",
        encoding="utf-8",
    )

    tool = GrepTool(workspace=tmp_path, allowed_dir=tmp_path)
    result = await tool.execute(
        pattern="oauth",
        path="memory/HISTORY.md",
        case_insensitive=True,
        output_mode="content",
    )

    assert "memory/HISTORY.md:1" in result
    assert "OAuth token rotated" in result


@pytest.mark.asyncio
async def test_grep_type_filter_limits_files(tmp_path: Path) -> None:
    (tmp_path / "src").mkdir()
    (tmp_path / "src" / "a.py").write_text("needle\n", encoding="utf-8")
    (tmp_path / "src" / "b.md").write_text("needle\n", encoding="utf-8")

    tool = GrepTool(workspace=tmp_path, allowed_dir=tmp_path)
    result = await tool.execute(
        pattern="needle",
        path="src",
        type="py",
        output_mode="files_with_matches",
    )

    assert result.splitlines() == ["src/a.py"]


@pytest.mark.asyncio
async def test_grep_fixed_strings_treats_regex_chars_literally(tmp_path: Path) -> None:
    (tmp_path / "memory").mkdir()
    (tmp_path / "memory" / "HISTORY.md").write_text(
        "[2026-04-02 10:00] OAuth token rotated\n",
        encoding="utf-8",
    )

    tool = GrepTool(workspace=tmp_path, allowed_dir=tmp_path)
    result = await tool.execute(
        pattern="[2026-04-02 10:00]",
        path="memory/HISTORY.md",
        fixed_strings=True,
        output_mode="content",
    )

    assert "memory/HISTORY.md:1" in result
    assert "[2026-04-02 10:00] OAuth token rotated" in result


@pytest.mark.asyncio
async def test_grep_files_with_matches_mode_returns_unique_paths(tmp_path: Path) -> None:
    (tmp_path / "src").mkdir()
    a = tmp_path / "src" / "a.py"
    b = tmp_path / "src" / "b.py"
    a.write_text("needle\nneedle\n", encoding="utf-8")
    b.write_text("needle\n", encoding="utf-8")
    os.utime(a, (1, 1))
    os.utime(b, (2, 2))

    tool = GrepTool(workspace=tmp_path, allowed_dir=tmp_path)
    result = await tool.execute(
        pattern="needle",
        path="src",
        output_mode="files_with_matches",
    )

    assert result.splitlines() == ["src/b.py", "src/a.py"]


@pytest.mark.asyncio
async def test_grep_files_with_matches_supports_head_limit_and_offset(tmp_path: Path) -> None:
    (tmp_path / "src").mkdir()
    for name in ("a.py", "b.py", "c.py"):
        (tmp_path / "src" / name).write_text("needle\n", encoding="utf-8")

    tool = GrepTool(workspace=tmp_path, allowed_dir=tmp_path)
    result = await tool.execute(
        pattern="needle",
        path="src",
        output_mode="files_with_matches",
        head_limit=1,
        offset=1,
    )

    # Filesystem order is not deterministic across platforms, so just verify:
    # 1. Only one file path is returned (head_limit=1 after offset=1)
    # 2. The pagination info is correct
    assert "pagination: limit=1, offset=1" in result
    file_lines = [line for line in result.splitlines() if line.startswith("src/")]
    assert len(file_lines) == 1


@pytest.mark.asyncio
async def test_grep_count_mode_reports_counts_per_file(tmp_path: Path) -> None:
    (tmp_path / "logs").mkdir()
    (tmp_path / "logs" / "one.log").write_text("warn\nok\nwarn\n", encoding="utf-8")
    (tmp_path / "logs" / "two.log").write_text("warn\n", encoding="utf-8")

    tool = GrepTool(workspace=tmp_path, allowed_dir=tmp_path)
    result = await tool.execute(
        pattern="warn",
        path="logs",
        output_mode="count",
    )

    assert "logs/one.log: 2" in result
    assert "logs/two.log: 1" in result
    assert "total matches: 3 in 2 files" in result


@pytest.mark.asyncio
async def test_grep_files_with_matches_mode_respects_max_results(tmp_path: Path) -> None:
    (tmp_path / "src").mkdir()
    files = []
    for idx, name in enumerate(("a.py", "b.py", "c.py"), start=1):
        file_path = tmp_path / "src" / name
        file_path.write_text("needle\n", encoding="utf-8")
        os.utime(file_path, (idx, idx))
        files.append(file_path)

    tool = GrepTool(workspace=tmp_path, allowed_dir=tmp_path)
    result = await tool.execute(
        pattern="needle",
        path="src",
        output_mode="files_with_matches",
        max_results=2,
    )

    assert result.splitlines()[:2] == ["src/c.py", "src/b.py"]
    assert "pagination: limit=2, offset=0" in result


@pytest.mark.asyncio
async def test_grep_reports_skipped_binary_and_large_files(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    (tmp_path / "binary.bin").write_bytes(b"\x00\x01\x02")
    (tmp_path / "large.txt").write_text("x" * 20, encoding="utf-8")

    monkeypatch.setattr(GrepTool, "_MAX_FILE_BYTES", 10)
    tool = GrepTool(workspace=tmp_path, allowed_dir=tmp_path)
    result = await tool.execute(pattern="needle", path=".")

    assert "No matches found" in result
    assert "skipped 1 binary/unreadable files" in result
    assert "skipped 1 large files" in result


@pytest.mark.asyncio
async def test_grep_uses_a_larger_bounded_limit_for_an_explicit_file(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    large_file = tmp_path / "history.jsonl"
    large_file.write_text("needle\n" + "x" * 20, encoding="utf-8")
    monkeypatch.setattr(GrepTool, "_MAX_FILE_BYTES", 10)
    monkeypatch.setattr(GrepTool, "_MAX_EXPLICIT_FILE_BYTES", 100)
    tool = GrepTool(workspace=tmp_path, allowed_dir=tmp_path)

    explicit_result = await tool.execute(
        pattern="needle",
        path=str(large_file),
        output_mode="content",
    )
    directory_result = await tool.execute(pattern="needle", path=".")
    monkeypatch.setattr(GrepTool, "_MAX_EXPLICIT_FILE_BYTES", 10)
    capped_result = await tool.execute(pattern="needle", path=str(large_file))

    assert "needle" in explicit_result
    assert "skipped 1 large files" in directory_result
    assert "skipped 1 large files" in capped_result


def test_grep_schema_is_concise_and_hides_internal_limits(tmp_path: Path) -> None:
    tool = GrepTool(workspace=tmp_path, allowed_dir=tmp_path)
    properties = tool.parameters["properties"]

    assert len(tool.description) < 150
    assert "head_limit" in properties
    assert "max_matches" not in properties
    assert "max_results" not in properties


@pytest.mark.asyncio
async def test_search_tools_reject_paths_outside_workspace(tmp_path: Path) -> None:
    outside = tmp_path.parent / "outside-search.txt"
    outside.write_text("secret\n", encoding="utf-8")

    grep_tool = GrepTool(workspace=tmp_path, allowed_dir=tmp_path)

    grep_result = await grep_tool.execute(pattern="secret", path=str(outside))

    assert grep_result.startswith("Error:")


@pytest.mark.parametrize("use_rg", [True, False])
def test_agent_loop_registers_search_backend(tmp_path: Path, monkeypatch, use_rg) -> None:
    monkeypatch.setattr("nanobot.agent.tools.rg.RgTool.enabled", lambda ctx: use_rg)
    bus = MessageBus()
    provider = MagicMock()
    provider.get_default_model.return_value = "test-model"

    loop = AgentLoop(bus=bus, provider=provider, workspace=tmp_path, model="test-model")

    assert ("rg" in loop.tools.tool_names) == use_rg
    assert ("find_files" in loop.tools.tool_names) == (not use_rg)
    assert ("grep" in loop.tools.tool_names) == (not use_rg)


@pytest.mark.asyncio
@pytest.mark.parametrize("use_rg", [True, False])
async def test_subagent_registers_search_backend(tmp_path: Path, monkeypatch, use_rg) -> None:
    monkeypatch.setattr("nanobot.agent.tools.rg.RgTool.enabled", lambda ctx: use_rg)
    bus = MessageBus()
    provider = MagicMock()
    provider.get_default_model.return_value = "test-model"
    provider.generation = GenerationSettings()
    mgr = SubagentManager(
        consolidator=MagicMock(spec=Consolidator),
        workspace=tmp_path,
        bus=bus,
        max_tool_result_chars=4096,
    )
    captured: dict[str, list[str]] = {}

    async def fake_run(spec):
        captured["tool_names"] = spec.tools.tool_names
        return SimpleNamespace(
            stop_reason="ok",
            final_content="done",
            tool_events=[],
            error=None,
        )

    mgr.runner.run = fake_run
    mgr._announce_result = AsyncMock()

    await mgr.spawn(
        task="search task",
        label="label",
        origin_channel="cli",
        origin_chat_id="direct",
        runtime=LLMRuntime.capture(provider, "test-model", context_window_tokens=128_000),
    )
    await asyncio.gather(*mgr._running_tasks.values(), return_exceptions=True)

    assert ("rg" in captured["tool_names"]) == use_rg
    assert ("find_files" in captured["tool_names"]) == (not use_rg)
    assert ("grep" in captured["tool_names"]) == (not use_rg)


def test_subagent_prompt_respects_disabled_skills(tmp_path: Path) -> None:
    bus = MessageBus()
    skills_dir = tmp_path / "skills"
    (skills_dir / "alpha").mkdir(parents=True)
    (skills_dir / "alpha" / "SKILL.md").write_text("# Alpha\n\nhidden\n", encoding="utf-8")
    (skills_dir / "beta").mkdir(parents=True)
    (skills_dir / "beta" / "SKILL.md").write_text("# Beta\n\nshown\n", encoding="utf-8")

    mgr = SubagentManager(
        consolidator=MagicMock(spec=Consolidator),
        workspace=tmp_path,
        bus=bus,
        max_tool_result_chars=4096,
        disabled_skills=["alpha"],
    )

    prompt = mgr._build_subagent_prompt()

    assert "alpha" not in prompt
    assert "beta" in prompt


@pytest.mark.parametrize("tool_class", [FindFilesTool, GrepTool])
@pytest.mark.parametrize("glob", ["webui/**/*.{ts,tsx}", "webui/*.{ts,tsx}", r"webui\*.{ts,tsx}"])
async def test_search_braces_and_root_relative_paths(tmp_path, tool_class, glob):
    for name in ["webui/App.tsx", "webui/client.ts", "webui/style.css", "backup/webui/Old.tsx"]:
        target = tmp_path / name
        target.parent.mkdir(parents=True, exist_ok=True)
        target.write_text("needle", encoding="utf-8")
    tool = tool_class(workspace=tmp_path)
    args = {"pattern": "needle", "output_mode": "files_with_matches"} if tool_class is GrepTool else {}

    result = await tool.execute(path=".", glob=glob, **args)

    assert sorted(result.splitlines()) == ["webui/App.tsx", "webui/client.ts"]
    recursive = await tool.execute(path=".", glob="**/webui/*.{ts,tsx}", **args)
    assert "backup/webui/Old.tsx" in recursive


@pytest.mark.parametrize("tool_class", [FindFilesTool, GrepTool])
@pytest.mark.parametrize("glob", ["*.{ts,tsx", "*.{ts}", "{a,b}" * 7])
async def test_search_invalid_glob_is_an_error_even_in_empty_directory(tmp_path, tool_class, glob):
    tool = tool_class(workspace=tmp_path)
    args = {"pattern": "needle"} if tool_class is GrepTool else {}
    result = await tool.execute(glob=glob, **args)
    assert is_tool_error_result(result)
    assert "glob" in result


@pytest.mark.parametrize("tool_class", [FindFilesTool, GrepTool])
async def test_search_skips_generated_directories_but_allows_explicit_roots(tmp_path, tool_class):
    excluded = [".worktrees/other", ".worktree/other", ".nanobot/tool-results", "webui/.verify-run"]
    for name in ["src", ".agent", *excluded]:
        directory = tmp_path / name
        directory.mkdir(parents=True)
        (directory / "data.txt").write_text("needle", encoding="utf-8")
    tool = tool_class(workspace=tmp_path)
    args = {"pattern": "needle", "output_mode": "files_with_matches"} if tool_class is GrepTool else {}

    result = await tool.execute(**args)
    assert sorted(result.splitlines()) == [".agent/data.txt", "src/data.txt"]
    for name in excluded:
        assert await tool.execute(path=name, **args) == f"{name}/data.txt"
        assert await tool.execute(path=f"{name}/data.txt", **args) == f"{name}/data.txt"


async def test_grep_merges_context_and_pages_by_matches(tmp_path):
    (tmp_path / "source.txt").write_text(
        "before\nneedle one\nneedle two\nneedle three\nafter\n", encoding="utf-8",
    )
    tool = GrepTool(workspace=tmp_path)
    result = await tool.execute(pattern="needle", head_limit=2)

    assert result.count("source.txt:") == 1
    assert result.count("| before") == 1
    assert result.count("| needle one") == 1
    assert result.count("| needle two") == 1
    assert "> 2| needle one" in result and "> 3| needle two" in result
    assert "use offset=2 to continue" in result
    second = await tool.execute(pattern="needle", head_limit=2, offset=2)
    assert re.findall(r"^> (\d+)\|", second, re.M) == ["4"]
    assert "to continue" not in second


async def test_grep_size_pages_do_not_lose_merged_matches(tmp_path, monkeypatch):
    (tmp_path / "source.txt").write_text(
        "\n".join(f"needle {n} " + "x" * 50 for n in range(30)), encoding="utf-8",
    )
    monkeypatch.setattr(GrepTool, "_MAX_RESULT_CHARS", 800)
    tool = GrepTool(workspace=tmp_path)
    seen = []
    offset = 0
    for _ in range(30):
        result = await tool.execute(pattern="needle", offset=offset, head_limit=0)
        seen.extend(re.findall(r"^> (\d+)\|", result, re.M))
        continuation = re.search(r"use offset=(\d+) to continue", result)
        if continuation is None:
            break
        next_offset = int(continuation[1])
        assert next_offset > offset
        offset = next_offset
    assert seen == [str(n) for n in range(1, 31)]


async def test_grep_large_first_context_still_advances(tmp_path, monkeypatch):
    (tmp_path / "source.txt").write_text("x" * 1000 + "\nneedle\n" + "x" * 1000, encoding="utf-8")
    monkeypatch.setattr(GrepTool, "_MAX_RESULT_CHARS", 350)
    result = await GrepTool(workspace=tmp_path).execute(pattern="needle")
    assert "> 2| needle" in result
    assert "use offset=1 to continue" in result
    assert "No matches" not in result


@pytest.mark.parametrize("mode", ["find_files", "files_with_matches", "count"])
async def test_search_path_pages_respect_size_and_continue(tmp_path, monkeypatch, mode):
    for n in range(20):
        target = tmp_path / (f"file-{n:02}-" + "x" * 50 + ".txt")
        target.write_text("needle", encoding="utf-8")
        os.utime(target, (1, 1))
    tool_class = FindFilesTool if mode == "find_files" else GrepTool
    monkeypatch.setattr(tool_class, "_MAX_RESULT_CHARS", 200)
    tool = tool_class(workspace=tmp_path)
    args = {} if mode == "find_files" else {"pattern": "needle", "output_mode": mode}
    seen = []
    offset = 0
    for _ in range(20):
        result = await tool.execute(offset=offset, head_limit=0, **args)
        seen.extend(re.findall(r"^(file-.*?\.txt)", result, re.M))
        continuation = re.search(r"use offset=(\d+) to continue", result)
        if continuation is None:
            break
        assert int(continuation[1]) > offset
        offset = int(continuation[1])
    assert seen == sorted(path.name for path in tmp_path.iterdir())


async def test_grep_worker_preserves_scope(tmp_path):
    project = tmp_path / "project"
    project.mkdir()
    (project / "inside.txt").write_text("needle", encoding="utf-8")
    (tmp_path / "outside.txt").write_text("needle", encoding="utf-8")
    tool = GrepTool(workspace=tmp_path, restrict_to_workspace=False)
    token = bind_workspace_scope(default_workspace_scope(project, True))
    try:
        assert await tool.execute(pattern="needle", output_mode="files_with_matches") == "inside.txt"
    finally:
        reset_workspace_scope(token)


async def test_grep_scan_yields_and_cancellation_stops_worker(tmp_path, monkeypatch):
    tool = GrepTool(workspace=tmp_path)
    started, stopped = threading.Event(), threading.Event()

    def blocking_iter_files(root, budget):
        started.set()
        try:
            if not budget.cancelled.wait(timeout=2):
                raise TimeoutError("worker did not receive cancellation")
            budget.checkpoint()
            yield root / "unreachable.txt"
        finally:
            stopped.set()

    monkeypatch.setattr(tool, "_iter_files", blocking_iter_files)
    task = asyncio.create_task(tool.execute(pattern="needle"))
    try:
        assert await asyncio.to_thread(started.wait, 1)
        await asyncio.sleep(0)
        assert not task.done()
    finally:
        task.cancel()
    with pytest.raises(asyncio.CancelledError):
        await asyncio.wait_for(task, timeout=0.5)
    assert await asyncio.to_thread(stopped.wait, 1)


@pytest.mark.parametrize("budget,value", [("_MAX_SCAN_PATHS", 0), ("_MAX_SCAN_SECONDS", 0)])
async def test_grep_bounds_scans(tmp_path, monkeypatch, budget, value):
    (tmp_path / "source.txt").write_text("ordinary", encoding="utf-8")
    monkeypatch.setattr(GrepTool, budget, value)
    result = await GrepTool(workspace=tmp_path).execute(pattern="absent")
    assert result.startswith("Error: grep scan exceeded 0")


async def test_grep_context_page_counts_matches_across_files(tmp_path):
    (tmp_path / "a.txt").write_text("needle a1\nneedle a2\n", encoding="utf-8")
    (tmp_path / "b.txt").write_text("needle b1\nneedle b2\n", encoding="utf-8")
    tool = GrepTool(workspace=tmp_path)

    result = await tool.execute(pattern="needle", offset=1, head_limit=2)

    assert result == (
        "a.txt:2\n  1| needle a1\n> 2| needle a2\n\n"
        "b.txt:1\n> 1| needle b1\n  2| needle b2\n\n"
        "(pagination: limit=2, offset=1; use offset=3 to continue)"
    )
    last_page = await tool.execute(pattern="needle", offset=3, head_limit=2)
    assert last_page == (
        "b.txt:2\n  1| needle b1\n> 2| needle b2\n\n(pagination: offset=3)"
    )


async def test_grep_marks_a_long_line_already_present_as_context(tmp_path):
    (tmp_path / "source.txt").write_text(
        "needle first\n" + "x" * 3000 + "needle late" + "y" * 1000, encoding="utf-8",
    )
    result = await GrepTool(workspace=tmp_path).execute(pattern="needle")

    assert re.findall(r"^> (\d+)\|", result, re.M) == ["1", "2"]
    assert "needle first" in result and "needle late" in result
    assert result.count("source.txt:") == 1
    assert len(result) < 2100


@pytest.mark.parametrize("mode", ["content", "count", "files_with_matches"])
async def test_grep_closes_document_stream_and_preserves_locators(tmp_path, monkeypatch, mode):
    from nanobot.utils.document import DocumentLineSource, LocatedDocumentLine

    (tmp_path / "source.pdf").write_bytes(b"placeholder")
    visited = []
    closed = []

    def document_lines():
        try:
            yield LocatedDocumentLine("Page 1", 1, "", searchable=False)
            for n in range(2, 102):
                visited.append(n)
                yield LocatedDocumentLine("needle", n, f"page=1,line={n - 1}")
        finally:
            closed.append(True)

    monkeypatch.setattr(
        "nanobot.agent.tools.search.open_document_line_source",
        lambda *_args, **_kwargs: DocumentLineSource(document_lines(), "pages=2-3"),
    )
    result = await GrepTool(workspace=tmp_path).execute(
        pattern="needle", path="source.pdf", output_mode=mode,
        head_limit=1, context_before=0, context_after=1,
    )

    assert closed == [True]
    assert "(source.pdf: continue PDF search with pages=2-3)" in result
    if mode == "content":
        assert result.startswith("source.pdf:2 [page=1,line=1]\n> 2 [page=1,line=1]| needle")
        assert "use offset=1 to continue" in result
        assert visited == [2, 3, 4]
    elif mode == "count":
        assert result.startswith("source.pdf: 100")
        assert len(visited) == 100
    else:
        assert result.startswith("source.pdf\n")
        assert visited == [2]