"""List tools return rows that say what a record is, never what it says (#4061). list_tasks once returned every row's to_dict(), body included. A project's todo list came to 93-165k characters, past what an MCP client accepts as a tool result, so the list arrived as a file to page through — the failure #4045 fixed for enter_project, one call over. A list is for choosing which record to open; get_task / get_note read one in full. """ import json from datetime import datetime, timezone from types import SimpleNamespace from unittest.mock import AsyncMock, patch import pytest from scribe.mcp.tools.notes import list_notes from scribe.mcp.tools.systems import list_system_records from scribe.mcp.tools.tasks import list_tasks from scribe.services.notes import brief_row pytestmark = pytest.mark.usefixtures("_bind_user") STEP_PLAN = "A step plan paragraph long enough to matter. " * 110 # ~5k chars WHEN = datetime(2026, 9, 15, tzinfo=timezone.utc) def _row(nid: int, *, is_task: bool = True, milestone_id: int | None = 415) -> SimpleNamespace: """A real-shaped Note: every attribute brief_row or to_dict could read.""" return SimpleNamespace( id=nid, title=f"Step {nid} — something worth doing", body=STEP_PLAN, description=None, note_type="note", project_id=2, tags=["retrieval"], updated_at=WHEN, created_at=WHEN, is_task=is_task, status="todo" if is_task else None, task_kind="work", priority="high", milestone_id=milestone_id if is_task else None, parent_id=None, due_date=None, ) def test_a_task_row_names_what_it_is_and_where_it_sits_but_not_its_body(): row = brief_row(_row(7), {415: "An existing plan is found"}) assert "body" not in row assert row["milestone_title"] == "An existing plan is found" assert {"id", "title", "status", "task_kind", "priority", "milestone_id", "tags", "updated_at", "project_id"} <= set(row) # Empty optional fields are left off rather than sent as null (#2483). assert not {"description", "parent_id", "due_date"} & set(row) assert row["updated_at"] == WHEN.isoformat() def test_a_note_row_carries_no_task_fields(): row = brief_row(_row(8, is_task=False)) assert "body" not in row and "status" not in row and "milestone_title" not in row def test_a_task_outside_any_milestone_says_so_rather_than_inventing_a_title(): row = brief_row(_row(9, milestone_id=None), {}) assert row["milestone_id"] is None and "milestone_title" not in row @pytest.mark.asyncio async def test_a_full_page_of_long_tasks_fits_inline(): """The ceiling. 100 rows (list_tasks' own limit cap) of ~5k-character step plans: as to_dict() rows this was over half a million characters.""" rows = [_row(i) for i in range(100)] with patch("scribe.mcp.tools.tasks.notes_svc.list_notes", AsyncMock(return_value=(rows, 100))), \ patch("scribe.mcp.tools.tasks.milestones_svc.titles_for", AsyncMock(return_value={415: "An existing plan is found"})) as titles: out = await list_tasks(project_id=2, limit=100) size = len(json.dumps(out)) assert size < 40_000, f"list_tasks page is {size} characters" assert titles.await_args.args[0] == {415} assert out["tasks"][0]["milestone_title"] == "An existing plan is found" @pytest.mark.asyncio async def test_list_notes_rows_leave_the_body_to_get_note(): rows = [_row(i, is_task=False) for i in range(3)] with patch("scribe.mcp.tools.notes.notes_svc.list_notes", AsyncMock(return_value=(rows, 3))): out = await list_notes(project_id=2) assert all("body" not in r for r in out["notes"]) @pytest.mark.asyncio async def test_list_system_records_rows_leave_the_body_to_the_getters(): rows = [_row(1), _row(2, is_task=False)] with patch("scribe.mcp.tools.systems.systems_svc.list_records_for_system", AsyncMock(return_value=rows)), \ patch("scribe.mcp.tools.systems.milestones_svc.titles_for", AsyncMock(return_value={415: "Plan"})): out = await list_system_records(system_id=3) assert [r["id"] for r in out["records"]] == [1, 2] assert all("body" not in r for r in out["records"])