From af353fae735e3b20ef9b4ea72bc7fe044733735c Mon Sep 17 00:00:00 2001 From: Rudy Celekli Date: Tue, 13 Oct 2026 08:21:28 -0400 Subject: [PATCH] fix(grep): preserve XLSX cell locators for literal tabs Keep extracted spreadsheet text unchanged and carry actual cell start offsets into grep headers. Add real workbook GrepTool regressions and plain/empty-cell controls. Focused literal-source extraction/ContentPage replay passes; full GrepTool and required CI remain pending. --- nanobot/agent/tools/_search_content.py | 7 ++++- nanobot/utils/document.py | 13 ++++++-- tests/tools/test_search_xlsx_locators.py | 39 ++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 tests/tools/test_search_xlsx_locators.py diff --git a/nanobot/agent/tools/_search_content.py b/nanobot/agent/tools/_search_content.py index ae21e4af0..20718bf4b 100644 --- a/nanobot/agent/tools/_search_content.py +++ b/nanobot/agent/tools/_search_content.py @@ -3,6 +3,7 @@ from __future__ import annotations import re +from bisect import bisect_right from collections import deque from dataclasses import dataclass, field from itertools import chain, islice @@ -43,7 +44,11 @@ def _format_line(line: LocatedDocumentLine, start: int | None, limit: int) -> st 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_index = ( + bisect_right(line.column_starts, start) + if line.column_starts is not None + else line.text[:start].count("\t") + 1 + ) column = "" while column_index: column_index, remainder = divmod(column_index - 1, 26) diff --git a/nanobot/utils/document.py b/nanobot/utils/document.py index edbe38ad7..6b3e03e09 100644 --- a/nanobot/utils/document.py +++ b/nanobot/utils/document.py @@ -87,6 +87,8 @@ class LocatedDocumentLine: extracted_line: int locator: str searchable: bool = True + # XLSX cell offsets in text: a tab inside a cell is not a column separator. + column_starts: tuple[int, ...] | None = None @dataclass(frozen=True, slots=True) @@ -257,11 +259,15 @@ def _open_xlsx_line_source(path: Path) -> DocumentLineSource: worksheet.reset_dimensions() wrote_header = False for row_index, row in enumerate(worksheet.iter_rows(values_only=True), 1): - row_text = "\t".join( - str(cell) if cell is not None else "" for cell in row - ) + cells = [str(cell) if cell is not None else "" for cell in row] + row_text = "\t".join(cells) if not row_text.strip(): continue + column_starts = [] + offset = 0 + for cell in cells: + column_starts.append(offset) + offset += len(cell) + 1 if not wrote_header: if wrote_document_content: extracted_line += 1 @@ -282,6 +288,7 @@ def _open_xlsx_line_source(path: Path) -> DocumentLineSource: row_text, extracted_line, f"sheet={sheet_name!r},row={row_index}", + column_starts=tuple(column_starts), ) finally: workbook.close() diff --git a/tests/tools/test_search_xlsx_locators.py b/tests/tools/test_search_xlsx_locators.py new file mode 100644 index 000000000..51fa3650f --- /dev/null +++ b/tests/tools/test_search_xlsx_locators.py @@ -0,0 +1,39 @@ +"""Spreadsheet grep locators follow cells, including literal tabs in cell values.""" + +from pathlib import Path + +import pytest +from openpyxl import Workbook + +from nanobot.agent.tools.search import GrepTool + + +@pytest.mark.parametrize( + ("cells", "expected_cell"), + [ + (["left\ttab", "needle"], "B1"), + (["before\tneedle", "other"], "A1"), + ([None, "left\ttab", None, "needle"], "D1"), + ([None, "needle"], "B1"), + (["left", "needle"], "B1"), + ], + ids=["tab-in-earlier-cell", "match-after-tab-in-cell", "tab-and-empty-cells", "empty-leading-cell", "plain-cells"], +) +@pytest.mark.asyncio +async def test_grep_xlsx_literal_tabs_keep_source_cell( + tmp_path: Path, cells: list[str | None], expected_cell: str, +) -> None: + workbook = Workbook() + workbook.active.title = "Data" + workbook.active.append(cells) + workbook.save(tmp_path / "data.xlsx") + workbook.close() + + result = await GrepTool(workspace=tmp_path, allowed_dir=tmp_path).execute( + pattern="needle", path="data.xlsx", fixed_strings=True, output_mode="content", + ) + + assert f"sheet='Data',row=1,cell={expected_cell}" in result + # Metadata must not escape or otherwise change the original extracted text. + extracted = "\t".join(cell if cell is not None else "" for cell in cells) + assert f"> 2 [sheet='Data',row=1]| {extracted}" in result