Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/basic_memory/man/man3/bm-read(3).md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ continue.
bm_move and bm_delete take to guard a change.

A text file that is not a note (`.txt`, `.json`, ...) takes `start_line` and
`end_line` too, with the same coordinates and continuation; `section` and
`max_tokens` apply to Markdown notes only. Images come back as images and other
`end_line` too, with the same coordinates and continuation (its header gives only
the range, as there is no frontmatter); `section` and `max_tokens` apply to
Markdown notes only. Images come back as images and other
binary files as documents, always whole. A missing note is a `NOT_FOUND` error;
find notes with bm_search.

Expand Down
11 changes: 9 additions & 2 deletions src/basic_memory/markdown/line_scanning.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,16 @@ def format_line_read(
start_line: int,
end_line: int,
total_lines: int,
includes_frontmatter: bool,
) -> str:
"""Render a slice with copyable coordinates and a bounded continuation hint."""
header = f"Lines {start_line}-{end_line} of {total_lines} (document, including frontmatter)"
"""Render a slice with copyable coordinates and a bounded continuation hint.

includes_frontmatter says the coordinates count a Markdown note's frontmatter;
a plain text file has none, so its header names only the range (#1976).
"""
header = f"Lines {start_line}-{end_line} of {total_lines}"
if includes_frontmatter:
header += " (document, including frontmatter)"
# The slice has no terminal newline. Splitting on '\n' preserves a final
# blank selected line; the coordinate range distinguishes it from EOF.
numbered = (
Expand Down
16 changes: 14 additions & 2 deletions src/basic_memory/mcp/tools/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,13 @@ async def bm_read(
f"token budget\n{payload['content']}\n\n"
f"(Continue with start_line={payload['continue_line']}.)"
)
return format_line_read(payload["content"], start_line=first, end_line=last, total_lines=total)
return format_line_read(
payload["content"],
start_line=first,
end_line=last,
total_lines=total,
includes_frontmatter=True,
)


def _continuation(first: int, last: int, total: int) -> dict[str, Any]:
Expand Down Expand Up @@ -254,7 +260,13 @@ def _text_file_lines(
**_continuation(first, last, total),
}
)
return format_line_read(sliced.content, start_line=first, end_line=last, total_lines=total)
return format_line_read(
sliced.content,
start_line=first,
end_line=last,
total_lines=total,
includes_frontmatter=False,
)


def _present_file(
Expand Down
24 changes: 20 additions & 4 deletions tests/markdown/test_line_scanning.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,35 @@ def test_no_literal_match(content: str) -> None:


def test_numbered_read_preserves_blank_lines_and_bounds_continuation() -> None:
text = format_line_read("alpha\n", start_line=4, end_line=5, total_lines=6)
text = format_line_read(
"alpha\n", start_line=4, end_line=5, total_lines=6, includes_frontmatter=True
)
assert "4: alpha\n5: " in text
assert "next: start_line=6, end_line=6" in text


def test_an_empty_document_numbers_no_lines() -> None:
text = format_line_read("", start_line=0, end_line=0, total_lines=0)
text = format_line_read("", start_line=0, end_line=0, total_lines=0, includes_frontmatter=True)

assert text == "Lines 0-0 of 0 (document, including frontmatter); EOF"


@pytest.mark.parametrize(("first", "last", "total"), [(1, 0, 0), (20, 10, 10)])
def test_numbered_empty_or_past_eof(first: int, last: int, total: int) -> None:
assert format_line_read("", start_line=first, end_line=last, total_lines=total).endswith(
"; EOF"
assert format_line_read(
"", start_line=first, end_line=last, total_lines=total, includes_frontmatter=True
).endswith("; EOF")


def test_text_file_header_names_only_the_range() -> None:
"""A plain text file has no frontmatter, so the header does not claim it (#1976)."""
text = format_line_read(
"ten\neleven\ntwelve",
start_line=10,
end_line=12,
total_lines=30,
includes_frontmatter=False,
)

assert text.startswith("Lines 10-12 of 30; next: start_line=13, end_line=15")
assert "frontmatter" not in text
6 changes: 4 additions & 2 deletions tests/mcp/test_tool_bm_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ async def test_a_text_file_reads_a_line_range(client, test_project, non_note_fil
assert (payload["next_start_line"], payload["next_end_line"]) == (4, 5)
assert payload["file_path"] == "source.txt"
assert payload["content_type"].startswith("text/plain")
assert text.startswith("Lines 2-3 of 5")
# A text file has no frontmatter, so the header names only the range (#1976).
assert text.startswith("Lines 2-3 of 5; next: start_line=4, end_line=5")
assert "frontmatter" not in text
assert "3: Call me Ishmael." in text


Expand All @@ -262,7 +264,7 @@ async def test_an_empty_text_file_reads_as_the_empty_range(client, test_project,
assert payload["content"] == ""
assert (payload["start_line"], payload["end_line"], payload["total_lines"]) == (0, 0, 0)
assert payload["has_more"] is False
assert text == "Lines 0-0 of 0 (document, including frontmatter); EOF"
assert text == "Lines 0-0 of 0; EOF"


@pytest.mark.asyncio
Expand Down
Loading