Skip to content

Commit c39baf0

Browse files
committed
fix(cli): recompile --refresh-schema no-ops when AGENTS.md absent; tighten guard tests
Match the spec (and the helper's own docstring): _refresh_schema returns early when wiki/AGENTS.md is missing rather than materializing the default (get_agents_md already falls back to it at runtime). Tighten the doc/--all guard tests to assert the exact message + that no compile runs, and add the missing-AGENTS.md no-op test.
1 parent 1416b1f commit c39baf0

2 files changed

Lines changed: 31 additions & 10 deletions

File tree

openkb/cli.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,13 +1083,16 @@ def _refresh_schema(wiki_dir: Path) -> bool:
10831083
file is missing or already identical. Returns True if it overwrote.
10841084
"""
10851085
agents_file = wiki_dir / "AGENTS.md"
1086-
current = agents_file.read_text(encoding="utf-8") if agents_file.exists() else ""
1086+
if not agents_file.exists():
1087+
# No-op when missing: get_agents_md() already falls back to the
1088+
# bundled AGENTS_MD default at runtime, so there is nothing to refresh.
1089+
return False
1090+
current = agents_file.read_text(encoding="utf-8")
10871091
if current == AGENTS_MD:
10881092
return False
1089-
if agents_file.exists():
1090-
backup = wiki_dir / "AGENTS.md.bak"
1091-
backup.write_text(current, encoding="utf-8")
1092-
click.echo(f" Backed up existing schema to {backup.relative_to(wiki_dir.parent)}")
1093+
backup = wiki_dir / "AGENTS.md.bak"
1094+
backup.write_text(current, encoding="utf-8")
1095+
click.echo(f" Backed up existing schema to {backup.relative_to(wiki_dir.parent)}")
10931096
agents_file.write_text(AGENTS_MD, encoding="utf-8")
10941097
click.echo(" Refreshed wiki/AGENTS.md to the current schema.")
10951098
return True

tests/test_recompile.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,15 +224,19 @@ def test_recompile_skips_long_missing_summary(kb_dir):
224224

225225
def test_recompile_requires_doc_or_all(kb_dir):
226226
_seed_short(kb_dir)
227-
result = _invoke(kb_dir, ["recompile"])
228-
assert result.exit_code != 0 or "Specify" in result.output or "--all" in result.output
227+
with patch("openkb.agent.compiler.compile_short_doc", new_callable=AsyncMock) as short:
228+
result = _invoke(kb_dir, ["recompile"])
229+
# Usage guard echoes a message and returns (exit 0); no compile runs.
230+
assert "Specify a document name or pass --all" in result.output
231+
short.assert_not_called()
229232

230233

231234
def test_recompile_doc_and_all_conflict(kb_dir):
232235
_seed_short(kb_dir)
233-
result = _invoke(kb_dir, ["recompile", "notes.md", "--all"])
234-
assert "both" in result.output.lower() or "either" in result.output.lower() \
235-
or result.exit_code != 0
236+
with patch("openkb.agent.compiler.compile_short_doc", new_callable=AsyncMock) as short:
237+
result = _invoke(kb_dir, ["recompile", "notes.md", "--all"])
238+
assert "not both" in result.output.lower()
239+
short.assert_not_called()
236240

237241

238242
def test_recompile_unknown_doc_friendly_error(kb_dir):
@@ -294,3 +298,17 @@ def test_recompile_no_refresh_schema_by_default(kb_dir):
294298
# Untouched without the flag
295299
assert agents.read_text(encoding="utf-8") == "OLD CUSTOM SCHEMA\n"
296300
assert not (kb_dir / "wiki" / "AGENTS.md.bak").exists()
301+
302+
303+
def test_recompile_refresh_schema_noop_when_agents_missing(kb_dir):
304+
"""Spec: --refresh-schema is a no-op when AGENTS.md is absent (runtime
305+
already falls back to the bundled default), so nothing is written."""
306+
_seed_short(kb_dir)
307+
agents = kb_dir / "wiki" / "AGENTS.md"
308+
agents.unlink(missing_ok=True)
309+
with patch("openkb.agent.compiler.compile_short_doc", new_callable=AsyncMock) as short:
310+
result = _invoke(kb_dir, ["recompile", "notes.md", "--refresh-schema"])
311+
312+
assert result.exit_code == 0, result.output
313+
assert not agents.exists() # not materialized
314+
assert not (kb_dir / "wiki" / "AGENTS.md.bak").exists()

0 commit comments

Comments
 (0)