|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import json |
| 6 | +from pathlib import Path |
6 | 7 | from unittest.mock import AsyncMock, MagicMock, patch |
7 | 8 |
|
8 | 9 | import pytest |
|
16 | 17 | _backlink_summary_entities, |
17 | 18 | _compile_concepts, |
18 | 19 | _filter_entity_items, |
| 20 | + _maybe_truncate_doc, |
19 | 21 | _parse_entities_plan, |
20 | 22 | _parse_json, |
21 | 23 | _prepend_source_to_frontmatter, |
@@ -2830,3 +2832,147 @@ def test_concept_update_malformed_frontmatter_rebuilds(self, tmp_path): |
2830 | 2832 | assert 'type: "Concept"' in text |
2831 | 2833 | # Must have a properly closed frontmatter block (two '---' occurrences). |
2832 | 2834 | assert text.count("---") >= 2 |
| 2835 | + |
| 2836 | + |
| 2837 | +class TestMaybeTruncateDoc: |
| 2838 | + """``_maybe_truncate_doc`` caps oversized source docs for the short-doc path. |
| 2839 | +
|
| 2840 | + Issue #73: the short-doc compiler sends the whole markdown file as one LLM |
| 2841 | + message, so an oversized doc overflows the model context. The helper must |
| 2842 | + truncate over-budget docs with an explicit marker and pass through docs |
| 2843 | + within the budget untouched. |
| 2844 | + """ |
| 2845 | + |
| 2846 | + def test_under_budget_passthrough_unchanged(self): |
| 2847 | + content = "short markdown body" |
| 2848 | + assert _maybe_truncate_doc(content, 1000, "doc") == content |
| 2849 | + |
| 2850 | + def test_exactly_at_budget_passthrough(self): |
| 2851 | + content = "x" * 100 |
| 2852 | + assert _maybe_truncate_doc(content, 100, "doc") == content |
| 2853 | + |
| 2854 | + def test_over_budget_truncates_with_marker(self): |
| 2855 | + content = "A" * 200 |
| 2856 | + out = _maybe_truncate_doc(content, 50, "doc") |
| 2857 | + # Body is capped at the budget... |
| 2858 | + assert out.startswith("A" * 50) |
| 2859 | + # ...and carries an explicit marker so the model knows the tail is gone. |
| 2860 | + assert "truncated at 50 characters" in out |
| 2861 | + assert "not included" in out |
| 2862 | + # The original tail text must NOT appear in the output. |
| 2863 | + assert "A" * 100 not in out |
| 2864 | + |
| 2865 | + def test_marker_reports_configured_budget(self): |
| 2866 | + out = _maybe_truncate_doc("B" * 500, 123, "big-doc") |
| 2867 | + assert "truncated at 123 characters" in out |
| 2868 | + assert "config `max_doc_chars`" in out |
| 2869 | + |
| 2870 | + |
| 2871 | +class TestShortDocContentBudget: |
| 2872 | + """Integration: ``compile_short_doc`` respects ``max_doc_chars`` from config. |
| 2873 | +
|
| 2874 | + An oversized markdown source must be truncated before it reaches the LLM |
| 2875 | + summary call, with the explicit marker visible in the user message. |
| 2876 | + """ |
| 2877 | + |
| 2878 | + def _write_kb(self, tmp_path, max_doc_chars: int) -> Path: |
| 2879 | + wiki = tmp_path / "wiki" |
| 2880 | + (wiki / "sources").mkdir(parents=True) |
| 2881 | + (wiki / "summaries").mkdir(parents=True) |
| 2882 | + (wiki / "concepts").mkdir(parents=True) |
| 2883 | + (wiki / "index.md").write_text( |
| 2884 | + "# Index\n\n## Documents\n\n## Concepts\n", |
| 2885 | + encoding="utf-8", |
| 2886 | + ) |
| 2887 | + (tmp_path / ".openkb").mkdir() |
| 2888 | + (tmp_path / ".openkb" / "config.yaml").write_text( |
| 2889 | + f"max_doc_chars: {max_doc_chars}\n", |
| 2890 | + encoding="utf-8", |
| 2891 | + ) |
| 2892 | + src = wiki / "sources" / "big.md" |
| 2893 | + return src |
| 2894 | + |
| 2895 | + @pytest.mark.asyncio |
| 2896 | + async def test_large_markdown_is_truncated_with_marker(self, tmp_path): |
| 2897 | + src = self._write_kb(tmp_path, max_doc_chars=100) |
| 2898 | + # A document comfortably over the 100-char budget, with a distinctive tail. |
| 2899 | + src.write_text("Start of document.\n" + ("T" * 500) + "\nEND_TAIL_MARKER", encoding="utf-8") |
| 2900 | + |
| 2901 | + captured: list[list[dict]] = [] |
| 2902 | + |
| 2903 | + def sync_side_effect(*args, **kwargs): |
| 2904 | + captured.append(kwargs["messages"]) |
| 2905 | + mock_resp = MagicMock() |
| 2906 | + mock_resp.choices = [MagicMock()] |
| 2907 | + mock_resp.choices[0].message.content = json.dumps( |
| 2908 | + {"description": "d", "content": "summary"} |
| 2909 | + ) |
| 2910 | + mock_resp.usage = MagicMock(prompt_tokens=1, completion_tokens=1) |
| 2911 | + mock_resp.usage.prompt_tokens_details = None |
| 2912 | + return mock_resp |
| 2913 | + |
| 2914 | + async def async_side_effect(*args, **kwargs): |
| 2915 | + mock_resp = MagicMock() |
| 2916 | + mock_resp.choices = [MagicMock()] |
| 2917 | + mock_resp.choices[0].message.content = json.dumps({"brief": "c", "content": "page"}) |
| 2918 | + mock_resp.usage = MagicMock(prompt_tokens=1, completion_tokens=1) |
| 2919 | + mock_resp.usage.prompt_tokens_details = None |
| 2920 | + return mock_resp |
| 2921 | + |
| 2922 | + with patch("openkb.agent.compiler.litellm") as mock_litellm: |
| 2923 | + mock_litellm.completion = MagicMock(side_effect=sync_side_effect) |
| 2924 | + mock_litellm.acompletion = AsyncMock(side_effect=async_side_effect) |
| 2925 | + await compile_short_doc("big", src, tmp_path, "gpt-4o-mini") |
| 2926 | + |
| 2927 | + # The first message sent to the LLM is the document-summary request; its |
| 2928 | + # user payload must be truncated and carry the marker. |
| 2929 | + summary_call = captured[0] |
| 2930 | + doc_user = summary_call[1] |
| 2931 | + sent_text = doc_user["content"] |
| 2932 | + # Content may be a list-of-blocks (from _cached_text) or a plain string; |
| 2933 | + # handle both the way the pipeline emits it. |
| 2934 | + if isinstance(sent_text, list): |
| 2935 | + sent_text = "".join(b.get("text", "") for b in sent_text) |
| 2936 | + assert "truncated at 100 characters" in sent_text, sent_text |
| 2937 | + # The distinctive tail is gone from the payload. |
| 2938 | + assert "END_TAIL_MARKER" not in sent_text |
| 2939 | + |
| 2940 | + @pytest.mark.asyncio |
| 2941 | + async def test_doc_within_budget_passes_through(self, tmp_path): |
| 2942 | + src = self._write_kb(tmp_path, max_doc_chars=100_000) |
| 2943 | + body = "A modest document.\n" + ("P" * 400) |
| 2944 | + src.write_text(body, encoding="utf-8") |
| 2945 | + |
| 2946 | + captured: list[list[dict]] = [] |
| 2947 | + |
| 2948 | + def sync_side_effect(*args, **kwargs): |
| 2949 | + captured.append(kwargs["messages"]) |
| 2950 | + mock_resp = MagicMock() |
| 2951 | + mock_resp.choices = [MagicMock()] |
| 2952 | + mock_resp.choices[0].message.content = json.dumps( |
| 2953 | + {"description": "d", "content": "summary"} |
| 2954 | + ) |
| 2955 | + mock_resp.usage = MagicMock(prompt_tokens=1, completion_tokens=1) |
| 2956 | + mock_resp.usage.prompt_tokens_details = None |
| 2957 | + return mock_resp |
| 2958 | + |
| 2959 | + async def async_side_effect(*args, **kwargs): |
| 2960 | + mock_resp = MagicMock() |
| 2961 | + mock_resp.choices = [MagicMock()] |
| 2962 | + mock_resp.choices[0].message.content = json.dumps({"brief": "c", "content": "page"}) |
| 2963 | + mock_resp.usage = MagicMock(prompt_tokens=1, completion_tokens=1) |
| 2964 | + mock_resp.usage.prompt_tokens_details = None |
| 2965 | + return mock_resp |
| 2966 | + |
| 2967 | + with patch("openkb.agent.compiler.litellm") as mock_litellm: |
| 2968 | + mock_litellm.completion = MagicMock(side_effect=sync_side_effect) |
| 2969 | + mock_litellm.acompletion = AsyncMock(side_effect=async_side_effect) |
| 2970 | + await compile_short_doc("big", src, tmp_path, "gpt-4o-mini") |
| 2971 | + |
| 2972 | + summary_call = captured[0] |
| 2973 | + sent_text = summary_call[1]["content"] |
| 2974 | + if isinstance(sent_text, list): |
| 2975 | + sent_text = "".join(b.get("text", "") for b in sent_text) |
| 2976 | + # No truncation marker when the doc fits the budget. |
| 2977 | + assert "truncated at" not in sent_text |
| 2978 | + assert "P" * 400 in sent_text |
0 commit comments