Skip to content

Commit a41d8a9

Browse files
committed
feat(compiler): backfill type+description on long-doc summaries during recompile
1 parent bb844fb commit a41d8a9

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

openkb/agent/compiler.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,6 +2052,18 @@ async def compile_long_doc(
20522052
schema_md = get_agents_md(wiki_dir)
20532053
summary_content = summary_path.read_text(encoding="utf-8")
20542054

2055+
# Backfill OKF fields on the indexer-written summary (idempotent).
2056+
if summary_content.startswith("---"):
2057+
fm_end = summary_content.find("---", 3)
2058+
if fm_end != -1:
2059+
fm = summary_content[:fm_end + 3]
2060+
body = summary_content[fm_end + 3:]
2061+
fm = _set_fm_line(fm, "type", "Summary")
2062+
if doc_description:
2063+
fm = _set_fm_line(fm, "description", doc_description)
2064+
summary_content = fm + body
2065+
summary_path.write_text(summary_content, encoding="utf-8")
2066+
20552067
# Base context A. cache_control marker on the doc message creates a
20562068
# cache breakpoint covering (system + doc) for every concept call.
20572069
system_msg = {"role": "system", "content": _SYSTEM_TEMPLATE.format(

tests/test_recompile.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818

1919
from __future__ import annotations
2020

21+
import asyncio
2122
import json
2223
from pathlib import Path
2324
from unittest.mock import AsyncMock, patch
2425

2526
from click.testing import CliRunner
2627

28+
from openkb.agent import compiler
2729
from openkb.cli import cli
2830
from openkb.schema import AGENTS_MD
2931

@@ -312,3 +314,32 @@ def test_recompile_refresh_schema_noop_when_agents_missing(kb_dir):
312314
assert result.exit_code == 0, result.output
313315
assert not agents.exists() # not materialized
314316
assert not (kb_dir / "wiki" / "AGENTS.md.bak").exists()
317+
318+
319+
# ---------------------------------------------------------------------------
320+
# compile_long_doc backfills type + description on recompile
321+
# ---------------------------------------------------------------------------
322+
323+
324+
def test_compile_long_doc_backfills_summary_frontmatter(tmp_path):
325+
wiki = tmp_path / "wiki"
326+
(wiki / "summaries").mkdir(parents=True)
327+
(wiki / "concepts").mkdir(parents=True)
328+
(tmp_path / ".openkb").mkdir()
329+
(tmp_path / ".openkb" / "config.yaml").write_text(
330+
"model: gpt-4o-mini\nlanguage: en\n", encoding="utf-8")
331+
summary_path = wiki / "summaries" / "long.md"
332+
summary_path.write_text(
333+
"---\ndoc_type: pageindex\nfull_text: sources/long.json\n---\n\n# Long\n",
334+
encoding="utf-8",
335+
)
336+
with patch.object(compiler, "_llm_call", return_value="overview"), \
337+
patch.object(compiler, "_compile_concepts", new=AsyncMock()), \
338+
patch.object(compiler, "_close_async_llm_clients", new=AsyncMock()):
339+
asyncio.run(compiler.compile_long_doc(
340+
"long", summary_path, "doc-1", tmp_path, "gpt-4o-mini",
341+
doc_description="A long report.",
342+
))
343+
text = summary_path.read_text(encoding="utf-8")
344+
assert 'type: "Summary"' in text
345+
assert 'description: "A long report."' in text

0 commit comments

Comments
 (0)