Skip to content

Commit 0a27c04

Browse files
committed
fix(compiler): silence spurious 'hand-edited' warning on backlink section creation
_backlink_summary_pages / _backlink_pages create ## Entities / ## Related Documents sections as a normal first-time operation; pass quiet=True so _ensure_h2_section no longer logs the index-drift warning in that case. Index-repair callers keep the warning.
1 parent 2f09fad commit 0a27c04

2 files changed

Lines changed: 31 additions & 8 deletions

File tree

openkb/agent/compiler.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -613,20 +613,26 @@ def _get_section_bounds(lines: list[str], heading: str) -> tuple[int, int] | Non
613613
return None
614614

615615

616-
def _ensure_h2_section(lines: list[str], heading: str) -> None:
616+
def _ensure_h2_section(lines: list[str], heading: str, *, quiet: bool = False) -> None:
617617
"""Ensure an H2 section ``heading`` exists in ``lines``; append if missing.
618618
619619
Recovers from hand-edited or drifted index.md files where the expected
620620
section was removed or renamed — without this, downstream inserts would
621621
silently no-op and entries would be dropped.
622+
623+
``quiet=True`` suppresses the drift warning. Use it when adding a section
624+
is the normal, expected operation (e.g. a backlink helper creating a
625+
``## Related Documents`` / ``## Entities`` section on a page for the first
626+
time), as opposed to repairing a drifted index.
622627
"""
623628
if _get_section_bounds(lines, heading) is not None:
624629
return
625-
logger.warning(
626-
"Wiki page is missing %r section; appending it. "
627-
"Check whether the file was hand-edited away from the canonical layout.",
628-
heading,
629-
)
630+
if not quiet:
631+
logger.warning(
632+
"Wiki page is missing %r section; appending it. "
633+
"Check whether the file was hand-edited away from the canonical layout.",
634+
heading,
635+
)
630636
while lines and lines[-1] == "":
631637
lines.pop()
632638
if lines:
@@ -1028,7 +1034,7 @@ def _backlink_summary_pages(
10281034
return
10291035

10301036
lines = text.split("\n")
1031-
_ensure_h2_section(lines, section)
1037+
_ensure_h2_section(lines, section, quiet=True)
10321038
for slug in reversed(missing):
10331039
_insert_section_entry(lines, section, f"- [[{page_dir}/{slug}]]")
10341040
summary_path.write_text("\n".join(lines), encoding="utf-8")
@@ -1050,7 +1056,7 @@ def _backlink_pages(
10501056
if link in text:
10511057
continue
10521058
lines = text.split("\n")
1053-
_ensure_h2_section(lines, "## Related Documents")
1059+
_ensure_h2_section(lines, "## Related Documents", quiet=True)
10541060
_insert_section_entry(lines, "## Related Documents", f"- {link}")
10551061
path.write_text("\n".join(lines), encoding="utf-8")
10561062

tests/test_compiler.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,6 +1788,23 @@ def test_schema_declares_entities():
17881788
assert t in AGENTS_MD
17891789

17901790

1791+
def test_ensure_h2_section_quiet_suppresses_drift_warning(caplog):
1792+
"""Backlink helpers create sections as a normal operation, so quiet=True
1793+
must not emit the 'hand-edited' drift warning; default still warns."""
1794+
import logging
1795+
1796+
from openkb.agent.compiler import _ensure_h2_section
1797+
1798+
with caplog.at_level(logging.WARNING, logger="openkb.agent.compiler"):
1799+
lines = ["# Doc", ""]
1800+
_ensure_h2_section(lines, "## Entities", quiet=True)
1801+
assert "## Entities" in lines
1802+
assert caplog.records == []
1803+
1804+
_ensure_h2_section(["# Doc", ""], "## Entities") # default warns
1805+
assert any("missing" in r.getMessage() for r in caplog.records)
1806+
1807+
17911808
def test_known_targets_prompt_has_entities_rule():
17921809
"""The whitelist message must tell the LLM the [[entities/X]] rule, since
17931810
entity-page prompts instruct writing such links; otherwise entity links

0 commit comments

Comments
 (0)