Skip to content

Commit 338d1bb

Browse files
author
Sebastian Braun
committed
fix(agent): skip context-window-exceeded concepts-plan without discarding the summary
The existing concept/entity index (_read_concept_briefs/_read_entity_briefs) grows unbounded with the KB and is appended on top of the already-cached document for the concepts-plan call -- a document whose summary call fit comfortably can still blow the context window once that index gets large enough. Previously this exception propagated uncaught out of _compile_concepts(), through compile_short_doc(), retried the whole doc (including a wasted repeat summary call) via cli.py's outer retry, and ultimately failed the add. _compile_concepts() now catches _NON_RETRYABLE_LLM_ERRORS around the concepts-plan call. Since the summary already succeeded by this point (unlike the doc-too-large case in compile_short_doc's own preflight/summary-call catch), it reuses the same fallback already used for an unparseable/empty plan: write the real v1 summary (ghost-wikilink-stripped) and the normal index.md entry, just skipping concept/entity generation for this doc -- instead of discarding a summary that was already successfully generated. Applies to compile_long_doc() too, since it shares _compile_concepts().
1 parent 69c79b0 commit 338d1bb

2 files changed

Lines changed: 80 additions & 19 deletions

File tree

‎openkb/agent/compiler.py‎

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,25 +1871,6 @@ async def _compile_concepts(
18711871
# (system + doc + summary) for the plan call and every concept call.
18721872
summary_msg = {"role": "assistant", "content": _cached_text(summary)}
18731873

1874-
plan_raw = _llm_call(
1875-
model,
1876-
[
1877-
system_msg,
1878-
doc_msg,
1879-
summary_msg,
1880-
{
1881-
"role": "user",
1882-
"content": _CONCEPTS_PLAN_USER.format(
1883-
concept_briefs=concept_briefs,
1884-
entity_briefs=entity_briefs,
1885-
).replace("__ENTITY_TYPES__", types_str),
1886-
},
1887-
],
1888-
"concepts-plan",
1889-
response_format=_JSON_RESPONSE_FORMAT,
1890-
bundle=bundle,
1891-
)
1892-
18931874
def _write_v1_summary_stripped() -> None:
18941875
"""Fallback writer for the v1 summary on early-return paths.
18951876
@@ -1911,6 +1892,39 @@ def _write_v1_summary_stripped() -> None:
19111892
)
19121893
_write_summary(wiki_dir, doc_name, cleaned, description=doc_brief)
19131894

1895+
try:
1896+
plan_raw = _llm_call(
1897+
model,
1898+
[
1899+
system_msg,
1900+
doc_msg,
1901+
summary_msg,
1902+
{
1903+
"role": "user",
1904+
"content": _CONCEPTS_PLAN_USER.format(
1905+
concept_briefs=concept_briefs,
1906+
entity_briefs=entity_briefs,
1907+
).replace("__ENTITY_TYPES__", types_str),
1908+
},
1909+
],
1910+
"concepts-plan",
1911+
response_format=_JSON_RESPONSE_FORMAT,
1912+
bundle=bundle,
1913+
)
1914+
except _NON_RETRYABLE_LLM_ERRORS as exc:
1915+
# The existing concept/entity index grows with the KB (see
1916+
# _read_concept_briefs/_read_entity_briefs) and is added on top of the
1917+
# already-cached document — a doc that was fine for the summary call
1918+
# can still blow the window here once the index gets large enough.
1919+
# The summary itself already exists (unlike the doc-too-large case
1920+
# above), so it's kept — same fallback as an unparseable/empty plan,
1921+
# just skipping concept/entity generation for this doc.
1922+
logger.warning("Skipping concept/entity extraction for %s: %s", doc_name, exc)
1923+
if rewrite_summary:
1924+
_write_v1_summary_stripped()
1925+
_update_index(wiki_dir, doc_name, [], doc_brief=doc_brief, doc_type=doc_type)
1926+
return
1927+
19141928
try:
19151929
parsed = _parse_json(plan_raw)
19161930
except (json.JSONDecodeError, ValueError) as exc:

‎tests/test_compiler.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,6 +1503,53 @@ async def test_scalar_plan_handled_gracefully(self, tmp_path):
15031503
# No concept pages produced from the unusable plan.
15041504
assert not list((wiki / "concepts").glob("*.md"))
15051505

1506+
@pytest.mark.asyncio
1507+
async def test_concepts_plan_context_window_exceeded_keeps_real_summary(self, tmp_path):
1508+
"""The summary call already succeeded with real content by the time
1509+
concepts-plan runs (unlike TestOversizedDocumentSkip, where the doc
1510+
itself is too large) — this must NOT be replaced with a generic
1511+
"too large" stub. Same fallback as an unparseable/empty plan: keep
1512+
the real v1 summary (ghost-stripped), index it, skip concept/entity
1513+
generation for this doc."""
1514+
wiki, source_path = self._setup_kb(tmp_path)
1515+
1516+
v1_summary_content = "# Summary\n\nDiscusses [[concepts/nonexistent]] here."
1517+
summary_response = json.dumps(
1518+
{"description": "A real summary", "content": v1_summary_content}
1519+
)
1520+
error = litellm.ContextWindowExceededError(
1521+
message="prompt is too long: 220670 tokens > 200000 maximum",
1522+
model="claude-sonnet-4-5",
1523+
llm_provider="anthropic",
1524+
)
1525+
call_count = {"n": 0}
1526+
1527+
def side_effect(*args, **kwargs):
1528+
idx = call_count["n"]
1529+
call_count["n"] += 1
1530+
if idx == 0:
1531+
return [_mock_response(summary_response)]
1532+
raise error
1533+
1534+
with patch("openkb.agent.compiler.litellm") as mock_litellm:
1535+
mock_litellm.completion = MagicMock(side_effect=side_effect)
1536+
# Must not raise out of compile_short_doc.
1537+
await compile_short_doc("doc", source_path, tmp_path, "claude-sonnet-4-5")
1538+
# summary + concepts-plan, no wasted retries on the doomed request.
1539+
assert mock_litellm.completion.call_count == 2
1540+
1541+
summary_path = wiki / "summaries" / "doc.md"
1542+
assert summary_path.exists()
1543+
text = summary_path.read_text()
1544+
assert "Discusses" in text # real summary content kept, not a stub
1545+
assert "too large" not in text.lower()
1546+
assert "[[concepts/nonexistent]]" not in text # ghost link stripped
1547+
assert "nonexistent" in text # display text preserved
1548+
1549+
index_text = (wiki / "index.md").read_text()
1550+
assert "[[summaries/doc]]" in index_text
1551+
assert not list((wiki / "concepts").glob("*.md"))
1552+
15061553

15071554
class TestOversizedDocumentSkip:
15081555
"""A document whose content can't fit an LLM call is treated like an

0 commit comments

Comments
 (0)