@@ -1508,9 +1508,11 @@ async def test_concepts_plan_context_window_exceeded_keeps_real_summary(self, tm
15081508 """The summary call already succeeded with real content by the time
15091509 concepts-plan runs (unlike TestOversizedDocumentSkip, where the doc
15101510 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."""
1511+ "too large" stub. The concepts-plan call is retried once with the
1512+ summary as source (doc_msg dropped) before giving up; if that retry
1513+ ALSO hits the context window, same fallback as an unparseable/empty
1514+ plan: keep the real v1 summary (ghost-stripped), index it, skip
1515+ concept/entity generation for this doc."""
15141516 wiki , source_path = self ._setup_kb (tmp_path )
15151517
15161518 v1_summary_content = "# Summary\n \n Discusses [[concepts/nonexistent]] here."
@@ -1535,8 +1537,15 @@ def side_effect(*args, **kwargs):
15351537 mock_litellm .completion = MagicMock (side_effect = side_effect )
15361538 # Must not raise out of compile_short_doc.
15371539 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+ # summary + concepts-plan (full doc) + concepts-plan retry (summary
1541+ # only) — both plan attempts are doomed here, no further retries.
1542+ assert mock_litellm .completion .call_count == 3
1543+
1544+ # The retry attempt must not still include the full document message
1545+ # (_SUMMARY_USER's "Full text:" marker only ever appears in doc_msg).
1546+ retry_messages = mock_litellm .completion .call_args_list [2 ].kwargs ["messages" ]
1547+ retry_text = json .dumps (retry_messages )
1548+ assert "Full text:" not in retry_text
15401549
15411550 summary_path = wiki / "summaries" / "doc.md"
15421551 assert summary_path .exists ()
@@ -1550,6 +1559,63 @@ def side_effect(*args, **kwargs):
15501559 assert "[[summaries/doc]]" in index_text
15511560 assert not list ((wiki / "concepts" ).glob ("*.md" ))
15521561
1562+ @pytest .mark .asyncio
1563+ async def test_concepts_plan_context_window_retry_succeeds_with_summary_only (self , tmp_path ):
1564+ """When the retry (summary-only) succeeds, the plan is processed
1565+ normally — concepts are generated from the summary-derived plan
1566+ instead of the doc being treated as incomplete."""
1567+ wiki , source_path = self ._setup_kb (tmp_path )
1568+
1569+ v1_summary_content = "# Summary\n \n Discusses transformers."
1570+ summary_response = json .dumps (
1571+ {"description" : "A real summary" , "content" : v1_summary_content }
1572+ )
1573+ error = litellm .ContextWindowExceededError (
1574+ message = "prompt is too long: 220670 tokens > 200000 maximum" ,
1575+ model = "claude-sonnet-4-5" ,
1576+ llm_provider = "anthropic" ,
1577+ )
1578+ plan_response = json .dumps (
1579+ {
1580+ "create" : [{"name" : "transformer" , "title" : "Transformer" }],
1581+ "update" : [],
1582+ "related" : [],
1583+ }
1584+ )
1585+ concept_response = json .dumps ({"description" : "C" , "content" : "# T\n \n Body." })
1586+ call_count = {"n" : 0 }
1587+
1588+ def side_effect (* args , ** kwargs ):
1589+ idx = call_count ["n" ]
1590+ call_count ["n" ] += 1
1591+ if idx == 0 :
1592+ return [_mock_response (summary_response )]
1593+ if idx == 1 :
1594+ raise error
1595+ return [_mock_response (plan_response )]
1596+
1597+ with (
1598+ patch ("openkb.agent.compiler.litellm" ) as mock_litellm ,
1599+ ):
1600+ mock_litellm .completion = MagicMock (side_effect = side_effect )
1601+ mock_litellm .acompletion = AsyncMock (side_effect = _mock_acompletion ([concept_response ]))
1602+ await compile_short_doc ("doc" , source_path , tmp_path , "claude-sonnet-4-5" )
1603+ # summary + concepts-plan (full doc, fails) + concepts-plan retry
1604+ # (summary only, succeeds) + summary-rewrite (rewrite_summary=True).
1605+ assert mock_litellm .completion .call_count == 4
1606+
1607+ # The successful retry's messages must not include the full document
1608+ # message (_SUMMARY_USER's "Full text:" marker only ever appears in doc_msg).
1609+ retry_messages = mock_litellm .completion .call_args_list [2 ].kwargs ["messages" ]
1610+ retry_text = json .dumps (retry_messages )
1611+ assert "Full text:" not in retry_text
1612+
1613+ concept_path = wiki / "concepts" / "transformer.md"
1614+ assert concept_path .exists ()
1615+
1616+ index_text = (wiki / "index.md" ).read_text ()
1617+ assert "[[concepts/transformer]]" in index_text
1618+
15531619
15541620class TestOversizedDocumentSkip :
15551621 """A document whose content can't fit an LLM call is treated like an
0 commit comments