Your lightweight open-source personal agent.
Python agent-frameworkai-agentai-agentschatbotchatopsdiscord-botllm-agentsllmslocal-llmmcpmodel-context-protocolmulti-agentopenai-compatibleopenclawpersonal-ai-assistantpythonself-hostedtelegram-bot-ai-assistantwebuiworkflow-automation
refactor(tools): build grep context pages incrementally
chengyongru27b3e814344b
diff --git a/nanobot/agent/tools/_search_content.py b/nanobot/agent/tools/_search_content.pynew file mode 100644index 000000000..ae21e4af0--- /dev/null+++ b/nanobot/agent/tools/_search_content.py@@ -0,0 +1,146 @@+"""Build one bounded grep page from streams of located source lines."""++from __future__ import annotations++import re+from collections import deque+from dataclasses import dataclass, field+from itertools import chain, islice+from typing import Iterable, Literal++from nanobot.utils.document import LocatedDocumentLine+++class MatchTooLargeError(ValueError):+ """A match and its source locator cannot fit in an empty page."""+++def _clip_line(text: str, limit: int, match_start: int | None) -> str:+ if len(text) <= limit:+ return text+ marker = "..."+ available = limit - len(marker)+ if match_start is None:+ return text[:available] + marker+ start = min(max(0, match_start - available // 3), len(text) - available)+ end = start + available+ prefix = marker if start else ""+ suffix = marker if end < len(text) else ""+ visible = text[start:end]+ if prefix and suffix:+ visible = visible[: available - len(marker)]+ return prefix + visible + suffix+++def _format_line(line: LocatedDocumentLine, start: int | None, limit: int) -> str:+ marker = ">" if start is not None else " "+ coordinate = str(line.extracted_line)+ if line.locator:+ coordinate += f" [{line.locator}]"+ return f"{marker} {coordinate}| {_clip_line(line.text, limit, start)}"+++def _format_header(path: str, line: LocatedDocumentLine, start: int) -> str:+ locator = line.locator+ if locator.startswith("sheet="):+ column_index = line.text[:start].count("\t") + 1+ column = ""+ while column_index:+ column_index, remainder = divmod(column_index - 1, 26)+ column = chr(ord("A") + remainder) + column+ row = re.search(r",row=(\d+)$", locator)+ if row:+ locator += f",cell={column}{row[1]}"+ suffix = f" [{locator}]" if locator else ""+ return f"{path}:{line.extracted_line}{suffix}"+++@dataclass+class ContentPage:+ """Select matches and append only context not already present in the page."""++ limit: int | None+ offset: int+ max_chars: int+ line_limit: int+ blocks: list[list[str]] = field(default_factory=list, init=False)+ seen: int = field(default=0, init=False)+ returned: int = field(default=0, init=False)+ chars: int = field(default=0, init=False)+ stopped: Literal["limit", "size"] | None = field(default=None, init=False)++ def scan(+ self, path: str, lines: Iterable[LocatedDocumentLine], regex: re.Pattern[str],+ before: int, after: int,+ ) -> None:+ source = (line for line in lines if line.searchable)+ history: deque[LocatedDocumentLine] = deque(maxlen=before)+ following = deque(islice(source, after + 1))+ positions: dict[int, int] = {}+ while following:+ match = regex.search(following[0].text)+ if match is not None:+ self.seen += 1+ if self.seen > self.offset:+ if self.limit is not None and self.returned >= self.limit:+ self.stopped = "limit"+ return+ self._append_match(path, history, following, match.start(), positions)+ if self.stopped:+ return+ history.append(following.popleft())+ if (line := next(source, None)) is not None:+ following.append(line)++ def _append_match(+ self, path: str, history: deque[LocatedDocumentLine],+ following: deque[LocatedDocumentLine], start: int, positions: dict[int, int],+ ) -> None:+ match = following[0]+ last_line = next(reversed(positions), 0)+ first_line = history[0] if history else match+ merge = bool(positions and first_line.extracted_line <= last_line)+ header = "" if merge else _format_header(path, match, start)+ replacement = _format_line(match, start, self.line_limit)+ additions = [+ (line.extracted_line, replacement if line is match else _format_line(line, None, self.line_limit))+ for line in chain(history, following)+ if not merge or line.extracted_line > last_line+ ]+ replace_at = positions.get(match.extracted_line) if merge else None+ replacement_chars = (+ len(replacement) - len(self.blocks[-1][replace_at]) if replace_at is not None else 0+ )+ header_chars = len(header) + (2 if self.blocks else 0) if not merge else 0+ added_chars = header_chars + sum(len(text) + 1 for _, text in additions) + replacement_chars+ if self.chars + added_chars > self.max_chars:+ self.stopped = "size"+ if self.blocks:+ return+ # Even an oversized first context must return a match and advance the offset.+ additions = [(match.extracted_line, replacement)]+ added_chars = header_chars + len(replacement) + 1+ if added_chars > self.max_chars:+ raise MatchTooLargeError("match exceeds output budget; narrow path or pattern")+ if not merge:+ self.blocks.append([header])+ positions.clear()+ block = self.blocks[-1]+ if replace_at is not None:+ block[replace_at] = replacement+ for line_number, text in additions:+ positions[line_number] = len(block)+ block.append(text)+ self.chars += added_chars+ self.returned += 1++ def render(self, no_matches: str) -> tuple[str, str | None]:+ result = "\n\n".join("\n".join(block) for block in self.blocks) or no_matches+ if self.stopped == "limit":+ note = f"pagination: limit={self.limit}, offset={self.offset}"+ elif self.stopped == "size":+ note = "output truncated due to size"+ else:+ note = f"(pagination: offset={self.offset})" if self.offset and self.blocks else None+ return result, note+ return result, f"({note}; use offset={self.offset + self.returned} to continue)"diff --git a/nanobot/agent/tools/search.py b/nanobot/agent/tools/search.pyindex f324007ac..349593595 100644--- a/nanobot/agent/tools/search.py+++ b/nanobot/agent/tools/search.py@@ -11,16 +11,17 @@ import osimport reimport threadingimport time-from collections import dequefrom contextlib import suppressfrom dataclasses import dataclassfrom functools import lru_cachefrom pathlib import Path, PurePosixPathfrom typing import Any, Iterable, Iterator, TypeVar+from nanobot.agent.tools._search_content import ContentPage, MatchTooLargeErrorfrom nanobot.agent.tools.base import ToolResultfrom nanobot.agent.tools.filesystem import ListDirTool, _FsToolfrom nanobot.utils.document import (+ DocumentLineSource,LocatedDocumentLine,PdfPageRangeError,open_document_line_source,@@ -54,14 +55,6 @@ _TYPE_GLOB_MAP = {}-@dataclass(slots=True)-class _PendingContextMatch:- lines: list[LocatedDocumentLine]- match_index: int- match_start: int- remaining_after: int--@dataclass(slots=True)class _FindFilesEntry:path: Path@@ -160,15 +153,6 @@ def _is_binary(raw: bytes) -> bool:return (non_text / len(sample)) > 0.2-def _excel_column(index: int) -> str:- """Return a 1-indexed spreadsheet column label without importing openpyxl."""- label = ""- while index > 0:- index, remainder = divmod(index - 1, 26)- label = chr(ord("A") + remainder) + label- return label--def _paginate(items: list[T], limit: int | None, offset: int) -> tuple[list[T], bool]:if limit is None:return items[offset:], False@@ -646,99 +630,21 @@ class GrepTool(_SearchTool):}@staticmethod- def _clip_rendered_line(text: str, match_start: int | None = None) -> str:- limit = GrepTool._MAX_RENDERED_LINE_CHARS- if len(text) <= limit:- return text-- marker = "..."- available = limit - len(marker)- if match_start is None:- return text[:available] + marker-- start = max(0, match_start - available // 3)- start = min(start, len(text) - available)- end = start + available- prefix = marker if start else ""- suffix = marker if end < len(text) else ""- visible = text[start:end]- if prefix and suffix:- visible = visible[: available - len(marker)]- return prefix + visible + suffix-- @staticmethod- def _matching_contexts(- lines: Iterable[LocatedDocumentLine],- regex: re.Pattern[str],- before: int,- after: int,- ) -> Iterable[tuple[list[LocatedDocumentLine], int, int]]:- history: deque[LocatedDocumentLine] = deque(maxlen=before)- pending: list[_PendingContextMatch] = []-- for line in lines:- if not line.searchable:- continue-- still_pending: list[_PendingContextMatch] = []- for item in pending:- item.lines.append(line)- item.remaining_after -= 1- if item.remaining_after == 0:- yield item.lines, item.match_index, item.match_start- else:- still_pending.append(item)- pending = still_pending-- match = regex.search(line.text)- if match is not None:- context_lines = [*history, line]- item = _PendingContextMatch(- lines=context_lines,- match_index=len(context_lines) - 1,- match_start=match.start(),- remaining_after=after,- )- if after == 0:- yield item.lines, item.match_index, item.match_start- else:- pending.append(item)- history.append(line)-- for item in pending:- yield item.lines, item.match_index, item.match_start-- @staticmethod- def _format_block(- display_path: str,- lines: list[LocatedDocumentLine],- match_index: int,- match_start: int = 0,- matches: dict[int, int] | None = None,- ) -> str:- match_line = lines[match_index]- source_line = match_line.extracted_line- match_locator = match_line.locator- if match_locator.startswith("sheet="):- column = _excel_column(match_line.text[:match_start].count("\t") + 1)- row_match = re.search(r",row=(\d+)$", match_locator)- if row_match:- match_locator += f",cell={column}{row_match.group(1)}"- suffix = f" [{match_locator}]" if match_locator else ""- block = [f"{display_path}:{source_line}{suffix}"]- matches = matches if matches is not None else {source_line: match_start}- for line in lines:- is_match = line.extracted_line in matches- marker = ">" if is_match else " "- coordinate = str(line.extracted_line)- if line.locator:- coordinate += f" [{line.locator}]"- rendered = GrepTool._clip_rendered_line(- line.text,- matches.get(line.extracted_line),- )- block.append(f"{marker} {coordinate}| {rendered}")- return "\n".join(block)+ def _open_source(path: Path, pages: str | None, max_bytes: int) -> DocumentLineSource | None:+ if path.suffix.lower() in _DOCUMENT_EXTENSIONS:+ return open_document_line_source(path, pages=pages)+ with path.open("rb") as file:+ raw = file.read(max_bytes + 1)+ if _is_binary(raw):+ return None+ try:+ content = raw.decode("utf-8")+ except UnicodeDecodeError:+ return None+ return DocumentLineSource(+ LocatedDocumentLine(text, line_no, "")+ for line_no, text in enumerate(content.splitlines(), 1)+ )async def execute(self,@@ -827,18 +733,11 @@ class GrepTool(_SearchTool):limit = max_resultselse:limit = _DEFAULT_HEAD_LIMIT- blocks: list[str] = []- result_chars = 0- returned_matches = 0- seen_content_matches = 0- truncated = False- size_truncated = False+ content_page = ContentPage(limit, offset, self._MAX_RESULT_CHARS, self._MAX_RENDERED_LINE_CHARS)skipped_binary = 0skipped_large = 0document_errors: list[str] = []document_continuations: list[str] = []- matching_files: list[str] = []- paged: list[str] = []counts: dict[str, int] = {}file_mtimes: dict[str, float] = {}root = target if target.is_dir() else target.parent@@ -869,116 +768,30 @@ class GrepTool(_SearchTool):source_iterator: Iterator[LocatedDocumentLine] | None = Noneis_document = file_path.suffix.lower() in _DOCUMENT_EXTENSIONStry:- if is_document:- source = open_document_line_source(file_path, pages=pages)- if source is None:- skipped_binary += 1- continue- source_iterator = source.lines- source_lines: Iterable[LocatedDocumentLine] = source_iterator- if source.continuation:- document_continuations.append(- f"({display_path}: continue PDF search with "- f"{source.continuation})"- )- else:- with file_path.open("rb") as file:- raw = file.read(max_file_bytes + 1)- if _is_binary(raw):- skipped_binary += 1- continue- try:- content = raw.decode("utf-8")- except UnicodeDecodeError:- skipped_binary += 1- continue- source_lines = (- LocatedDocumentLine(text, line_no, "")- for line_no, text in enumerate(content.splitlines(), 1)+ source = self._open_source(file_path, pages, max_file_bytes)+ if source is None:+ skipped_binary += 1+ continue+ source_iterator = source.lines+ if source.continuation:+ document_continuations.append(+ f"({display_path}: continue PDF search with {source.continuation})")+ source_lines = checked_lines(source_iterator)file_had_match = Falseif output_mode == "content":- contexts = self._matching_contexts(- checked_lines(source_lines),- regex,- context_before,- context_after,- )- last_lines: list[LocatedDocumentLine] = []- last_matches: dict[int, int] = {}- for context_lines, match_index, match_start in contexts:- file_had_match = True- seen_content_matches += 1- if seen_content_matches <= offset:- continue- if limit is not None and returned_matches >= limit:- truncated = True- break- match_line = context_lines[match_index]- merge = bool(- last_lines- and context_lines[0].extracted_line <= last_lines[-1].extracted_line- )- if merge:- context_lines = last_lines + [- line for line in context_lines- if line.extracted_line > last_lines[-1].extracted_line- ]- matches = {- **(last_matches if merge else {}),- match_line.extracted_line: match_start,- }- first_match = next(iter(matches))- match_index = next(- index for index, line in enumerate(context_lines)- if line.extracted_line == first_match- )- block = self._format_block(- display_path,- context_lines,- match_index,- matches[first_match],- matches,- )- prior_chars = len(blocks[-1]) if merge else 0- extra_sep = 2 if blocks and not merge else 0- reduced_context = False- if result_chars - prior_chars + extra_sep + len(block) > self._MAX_RESULT_CHARS:- if not blocks:- # A large context must still yield a match and advance pagination.- context_lines = [match_line]- block = self._format_block(display_path, context_lines, 0, match_start)- reduced_context = True- if len(block) > self._MAX_RESULT_CHARS:- return ToolResult.error("Error: match exceeds output budget; narrow path or pattern")- else:- size_truncated = True- break- if merge:- blocks[-1] = block- else:- blocks.append(block)- result_chars += extra_sep + len(block) - prior_chars- returned_matches += 1- last_lines, last_matches = context_lines, matches- if reduced_context:- # Start a new page after falling back to a context-free match.- size_truncated = True- break+ content_page.scan(display_path, source_lines, regex, context_before, context_after)else:- for line in checked_lines(source_lines):+ for line in source_lines:if not line.searchable or regex.search(line.text) is None:continuefile_had_match = Trueif output_mode == "count":counts[display_path] = counts.get(display_path, 0) + 1continue- if display_path not in matching_files:- matching_files.append(display_path)- file_mtimes[display_path] = mtimebreak- except (_SearchCancelledError, _SearchBudgetExceededError):+ except (_SearchCancelledError, _SearchBudgetExceededError, MatchTooLargeError):raiseexcept Exception as e:if not is_document:@@ -998,62 +811,34 @@ class GrepTool(_SearchTool):close = getattr(source_iterator, "close", None)if close is not None:close()- if output_mode == "count" and file_had_match:- if display_path not in matching_files:- matching_files.append(display_path)- file_mtimes[display_path] = mtime- if output_mode in {"count", "files_with_matches"} and file_had_match:- continue- if truncated or size_truncated:+ if file_had_match:+ file_mtimes[display_path] = mtime+ if content_page.stopped:break- if output_mode == "files_with_matches":- if not matching_files:- result = f"No matches found for pattern '{pattern}' in {path}"- else:- ordered_files = sorted(- matching_files,- key=lambda name: (-file_mtimes.get(name, 0.0), name),- )- paged, truncated = _text_page(ordered_files, limit, offset, self._MAX_RESULT_CHARS)- result = "\n".join(paged)- elif output_mode == "count":- if not counts:- result = f"No matches found for pattern '{pattern}' in {path}"- else:- ordered_files = sorted(- matching_files,- key=lambda name: (-file_mtimes.get(name, 0.0), name),- )- count_lines = [f"{name}: {counts[name]}" for name in ordered_files]- paged, truncated = _text_page(count_lines, limit, offset, self._MAX_RESULT_CHARS)- result = "\n".join(paged)- else:- if not blocks:- result = f"No matches found for pattern '{pattern}' in {path}"- else:- result = "\n\n".join(blocks)-+ no_matches = f"No matches found for pattern '{pattern}' in {path}"notes: list[str] = []- if output_mode == "content" and truncated:- notes.append(- f"(pagination: limit={limit}, offset={offset}; "- f"use offset={offset + returned_matches} to continue)"+ if output_mode == "content":+ result, note = content_page.render(no_matches)+ if note:+ notes.append(note)+ else:+ ordered_files = sorted(+ file_mtimes, key=lambda name: (-file_mtimes.get(name, 0.0), name),)- elif output_mode == "content" and size_truncated:- notes.append(- "(output truncated due to size; "- f"use offset={offset + returned_matches} to continue)"+ entries = (+ [f"{name}: {counts[name]}" for name in ordered_files]+ if output_mode == "count" else ordered_files)- elif truncated and output_mode in {"count", "files_with_matches"}:- notes.append(- f"(pagination: limit={limit}, offset={offset}; "- f"use offset={offset + len(paged)} to continue)"- )- elif output_mode in {"count", "files_with_matches"} and offset > 0:- notes.append(f"(pagination: offset={offset})")- elif output_mode == "content" and offset > 0 and blocks:- notes.append(f"(pagination: offset={offset})")+ paged, truncated = _text_page(entries, limit, offset, self._MAX_RESULT_CHARS)+ result = "\n".join(paged) if file_mtimes or counts else no_matches+ if truncated:+ notes.append(+ f"(pagination: limit={limit}, offset={offset}; "+ f"use offset={offset + len(paged)} to continue)"+ )+ elif offset > 0:+ notes.append(f"(pagination: offset={offset})")if skipped_binary:notes.append(f"(skipped {skipped_binary} binary/unreadable files)")if skipped_large:@@ -1068,6 +853,8 @@ class GrepTool(_SearchTool):if notes:result += "\n\n" + "\n".join(notes)return result+ except MatchTooLargeError as exc:+ return ToolResult.error(f"Error: {exc}")except _SearchBudgetExceededError as exc:detail = f"{self._MAX_SCAN_PATHS} paths" if str(exc) == "paths" else f"{self._MAX_SCAN_SECONDS:g} seconds"return ToolResult.error(f"Error: grep scan exceeded {detail}; narrow path, glob, or type and retry.")diff --git a/tests/tools/test_search_tools.py b/tests/tools/test_search_tools.pyindex 568c53e61..4c7468df1 100644--- a/tests/tools/test_search_tools.py+++ b/tests/tools/test_search_tools.py@@ -989,3 +989,73 @@ async def test_grep_bounds_scans(tmp_path, monkeypatch, budget, value):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++[email protected]("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]