Skip to content

Commit ed0d6ba

Browse files
committed
Fix exact concept index row matching
1 parent ef235d2 commit ed0d6ba

2 files changed

Lines changed: 28 additions & 9 deletions

File tree

openkb/agent/compiler.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,14 @@ def _get_section_bounds(lines: list[str], heading: str) -> tuple[int, int] | Non
299299

300300

301301
def _section_contains_link(lines: list[str], heading: str, link: str) -> bool:
302-
"""Check whether a wikilink already exists inside the named section."""
302+
"""Check whether an index entry already exists inside the named section."""
303303
bounds = _get_section_bounds(lines, heading)
304304
if bounds is None:
305305
return False
306306

307307
start, end = bounds
308-
return any(link in line for line in lines[start:end])
308+
entry_prefix = f"- {link}"
309+
return any(line.startswith(entry_prefix) for line in lines[start:end])
309310

310311

311312
def _replace_section_entry(lines: list[str], heading: str, link: str, entry: str) -> bool:
@@ -315,8 +316,9 @@ def _replace_section_entry(lines: list[str], heading: str, link: str, entry: str
315316
return False
316317

317318
start, end = bounds
319+
entry_prefix = f"- {link}"
318320
for i in range(start, end):
319-
if link in lines[i]:
321+
if lines[i].startswith(entry_prefix):
320322
lines[i] = entry
321323
return True
322324
return False
@@ -509,7 +511,6 @@ def _backlink_concepts(wiki_dir: Path, doc_name: str, concept_slugs: list[str])
509511
text += f"\n\n## Related Documents\n- {link}\n"
510512
path.write_text(text, encoding="utf-8")
511513

512-
513514
def _update_index(
514515
wiki_dir: Path, doc_name: str, concept_names: list[str],
515516
doc_brief: str = "", concept_briefs: dict[str, str] | None = None,
@@ -519,7 +520,7 @@ def _update_index(
519520
520521
When ``doc_brief`` or entries in ``concept_briefs`` are provided, entries
521522
are written as ``- [[link]] (type) — brief text``. Existing entries are
522-
detected within their own section by the link part only and skipped to
523+
detected within their own section by exact entry prefix and skipped to
523524
avoid duplicates.
524525
``doc_type`` is ``"short"`` or ``"pageindex"`` — shown in the entry so the
525526
query agent knows how to access detailed content.
@@ -534,8 +535,7 @@ def _update_index(
534535
encoding="utf-8",
535536
)
536537

537-
text = index_path.read_text(encoding="utf-8")
538-
lines = text.split("\n")
538+
lines = index_path.read_text(encoding="utf-8").split("\n")
539539

540540
doc_link = f"[[summaries/{doc_name}]]"
541541
if not _section_contains_link(lines, "## Documents", doc_link):
@@ -555,8 +555,7 @@ def _update_index(
555555
else:
556556
_insert_section_entry(lines, "## Concepts", concept_entry)
557557

558-
text = "\n".join(lines)
559-
index_path.write_text(text, encoding="utf-8")
558+
index_path.write_text("\n".join(lines), encoding="utf-8")
560559

561560

562561
# ---------------------------------------------------------------------------

tests/test_compiler.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,26 @@ def test_appends_entries_with_briefs(self, tmp_path):
198198
assert "[[concepts/attention]] — Focus mechanism" in text
199199
assert "[[concepts/transformer]] — NN architecture" in text
200200

201+
def test_updates_only_exact_concept_row(self, tmp_path):
202+
wiki = tmp_path / "wiki"
203+
wiki.mkdir()
204+
(wiki / "index.md").write_text(
205+
"# Index\n\n## Documents\n\n## Concepts\n"
206+
"- [[concepts/transformer]] — Uses [[concepts/attention]] internally\n"
207+
"- [[concepts/attention]] — Old brief\n\n## Explorations\n",
208+
encoding="utf-8",
209+
)
210+
_update_index(
211+
wiki,
212+
"my-doc",
213+
["attention"],
214+
concept_briefs={"attention": "New brief"},
215+
)
216+
text = (wiki / "index.md").read_text()
217+
assert "- [[concepts/transformer]] — Uses [[concepts/attention]] internally" in text
218+
assert "- [[concepts/attention]] — New brief" in text
219+
assert text.count("[[concepts/attention]] — New brief") == 1
220+
201221
def test_no_duplicates(self, tmp_path):
202222
wiki = tmp_path / "wiki"
203223
wiki.mkdir()

0 commit comments

Comments
 (0)