feat(mcp): add OpenCode ingest readiness checks
Expose a check_ingest_opencode_readiness MCP tool for remote operators and run the same static preflight before launching OpenCode ingest jobs. The preflight validates the OpenCode binary, required run flags, project AGENTS/skill context files, and can run a no-write smoke test that proves wiki-ingest loads successfully. Document the remote ingest workflow and clarify that wiki-file-assets is tool-first, so empty resources/prompts lists are not a failure signal. Validation: python3 -m unittest discover -v; python3 -m py_compile obsidian_wiki/mcp_ingest.py obsidian_wiki/mcp_server.py tests/test_mcp_ingest.py tests/test_mcp_server_remote.py; git diff --check; real OpenCode smoke test passed with opencode 1.17.13.
This commit is contained in:
+131
-2
@@ -23,6 +23,7 @@ class McpIngestTests(unittest.TestCase):
|
||||
self.runtime = self.project / ".runtime" / "mcp-ingest"
|
||||
self.vault.mkdir()
|
||||
self.project.mkdir()
|
||||
self._write_opencode_skill_fixture(self.project)
|
||||
self.manager = IngestManager(
|
||||
vault_path=self.vault,
|
||||
project_root=self.project,
|
||||
@@ -58,6 +59,78 @@ class McpIngestTests(unittest.TestCase):
|
||||
runtime_root=self.vault / ".runtime" / "mcp-ingest",
|
||||
)
|
||||
|
||||
def test_check_opencode_readiness_static_success(self):
|
||||
with patch("obsidian_wiki.mcp_ingest.subprocess.run", side_effect=self._fake_static_readiness_run) as run_mock:
|
||||
result = self.manager.check_opencode_readiness()
|
||||
|
||||
self.assertTrue(result["ok"])
|
||||
self.assertTrue(result["static_preflight"])
|
||||
self.assertEqual(result["runtime_smoke_test"], "skipped")
|
||||
self.assertEqual(result["opencode_version"], "1.17.13")
|
||||
self.assertFalse(result["errors"])
|
||||
self.assertTrue(any(path.endswith(".agents/skills/wiki-ingest/SKILL.md") for path in result["checked_paths"]))
|
||||
self.assertEqual(run_mock.call_count, 2)
|
||||
|
||||
def test_check_opencode_readiness_reports_missing_skill_files(self):
|
||||
missing_project = self.root / "missing-project"
|
||||
missing_project.mkdir()
|
||||
(missing_project / "AGENTS.md").write_text("# Agent rules\n", encoding="utf-8")
|
||||
manager = IngestManager(
|
||||
vault_path=self.vault,
|
||||
project_root=missing_project,
|
||||
runtime_root=self.root / "runtime-missing",
|
||||
opencode_path=Path("/usr/bin/opencode"),
|
||||
)
|
||||
|
||||
with patch("obsidian_wiki.mcp_ingest.subprocess.run", side_effect=self._fake_static_readiness_run):
|
||||
result = manager.check_opencode_readiness()
|
||||
|
||||
self.assertFalse(result["ok"])
|
||||
self.assertFalse(result["static_preflight"])
|
||||
self.assertIn(".agents/skills/wiki-ingest/SKILL.md", "\n".join(result["errors"]))
|
||||
self.assertIn(".agents/skills/llm-wiki/SKILL.md", "\n".join(result["errors"]))
|
||||
|
||||
def test_check_opencode_readiness_smoke_test_success(self):
|
||||
with patch("obsidian_wiki.mcp_ingest.subprocess.run", side_effect=self._fake_smoke_success_run) as run_mock:
|
||||
result = self.manager.check_opencode_readiness(run_smoke_test=True)
|
||||
|
||||
self.assertTrue(result["ok"])
|
||||
self.assertEqual(result["runtime_smoke_test"], "passed")
|
||||
self.assertEqual(run_mock.call_count, 3)
|
||||
smoke_args = run_mock.call_args_list[-1].args[0]
|
||||
self.assertEqual(smoke_args[:4], ["/usr/bin/opencode", "run", "--format", "json"])
|
||||
self.assertIn("--auto", smoke_args)
|
||||
self.assertIn("--dir", smoke_args)
|
||||
self.assertIn("MCP ingest readiness smoke test", smoke_args)
|
||||
|
||||
def test_check_opencode_readiness_smoke_test_failure_and_timeout(self):
|
||||
with patch("obsidian_wiki.mcp_ingest.subprocess.run", side_effect=self._fake_smoke_failure_run):
|
||||
failed = self.manager.check_opencode_readiness(run_smoke_test=True)
|
||||
self.assertFalse(failed["ok"])
|
||||
self.assertEqual(failed["runtime_smoke_test"], "failed")
|
||||
self.assertIn("SKILL_LOAD_FAILED", "\n".join(failed["errors"]))
|
||||
|
||||
with patch("obsidian_wiki.mcp_ingest.subprocess.run", side_effect=self._fake_smoke_timeout_run):
|
||||
timed_out = self.manager.check_opencode_readiness(run_smoke_test=True)
|
||||
self.assertFalse(timed_out["ok"])
|
||||
self.assertEqual(timed_out["runtime_smoke_test"], "failed")
|
||||
self.assertIn("timed out", "\n".join(timed_out["errors"]))
|
||||
|
||||
def test_start_ingest_job_fails_before_opencode_when_static_readiness_fails(self):
|
||||
payload = b"source data"
|
||||
upload = self._complete_upload("source.pdf", payload)
|
||||
(self.project / ".agents" / "skills" / "wiki-ingest" / "SKILL.md").unlink()
|
||||
|
||||
with (
|
||||
patch("obsidian_wiki.mcp_ingest.subprocess.run", side_effect=self._fake_static_readiness_run),
|
||||
patch("obsidian_wiki.mcp_ingest.subprocess.Popen") as popen_mock,
|
||||
):
|
||||
job = self.manager.start_ingest_job([upload["upload_id"]])
|
||||
|
||||
self.assertEqual(job["state"], "failed")
|
||||
self.assertIn("OpenCode readiness failed", job["error"])
|
||||
popen_mock.assert_not_called()
|
||||
|
||||
def test_upload_chunk_lifecycle_verifies_offset_size_and_sha(self):
|
||||
payload = b"hello ingest"
|
||||
upload = self.manager.create_upload(
|
||||
@@ -114,7 +187,10 @@ class McpIngestTests(unittest.TestCase):
|
||||
upload = self._complete_upload("source.pdf", payload)
|
||||
popen = FakePopen()
|
||||
|
||||
with patch("obsidian_wiki.mcp_ingest.subprocess.Popen", return_value=popen) as popen_mock:
|
||||
with (
|
||||
patch("obsidian_wiki.mcp_ingest.subprocess.run", side_effect=self._fake_static_readiness_run),
|
||||
patch("obsidian_wiki.mcp_ingest.subprocess.Popen", return_value=popen) as popen_mock,
|
||||
):
|
||||
job = self.manager.start_ingest_job([upload["upload_id"]], note="customer asked for drawings")
|
||||
|
||||
args = popen_mock.call_args.args[0]
|
||||
@@ -142,7 +218,10 @@ class McpIngestTests(unittest.TestCase):
|
||||
second = self._complete_upload("second.pdf", b"second")
|
||||
popen = FakePopen(returncode=None)
|
||||
|
||||
with patch("obsidian_wiki.mcp_ingest.subprocess.Popen", return_value=popen):
|
||||
with (
|
||||
patch("obsidian_wiki.mcp_ingest.subprocess.run", side_effect=self._fake_static_readiness_run),
|
||||
patch("obsidian_wiki.mcp_ingest.subprocess.Popen", return_value=popen),
|
||||
):
|
||||
first_job = self.manager.start_ingest_job([first["upload_id"]])
|
||||
second_job = self.manager.start_ingest_job([second["upload_id"]])
|
||||
|
||||
@@ -171,6 +250,56 @@ class McpIngestTests(unittest.TestCase):
|
||||
|
||||
return hashlib.sha256(payload).hexdigest()
|
||||
|
||||
def _write_opencode_skill_fixture(self, root: Path) -> None:
|
||||
(root / "AGENTS.md").write_text("# Agent rules\n", encoding="utf-8")
|
||||
wiki_ingest = root / ".agents" / "skills" / "wiki-ingest"
|
||||
llm_wiki = root / ".agents" / "skills" / "llm-wiki"
|
||||
wiki_ingest_refs = wiki_ingest / "references"
|
||||
wiki_ingest_refs.mkdir(parents=True)
|
||||
llm_wiki.mkdir(parents=True)
|
||||
(wiki_ingest / "SKILL.md").write_text(
|
||||
"---\nname: wiki-ingest\ndescription: Ingest documents into the wiki.\n---\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
(llm_wiki / "SKILL.md").write_text(
|
||||
"---\nname: llm-wiki\ndescription: Shared wiki framework instructions.\n---\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
(wiki_ingest_refs / "ingest-prompts.md").write_text("# Prompt templates\n", encoding="utf-8")
|
||||
|
||||
def _fake_static_readiness_run(self, args, **_kwargs):
|
||||
if args == ["/usr/bin/opencode", "--version"]:
|
||||
return subprocess.CompletedProcess(args, 0, stdout="1.17.13\n", stderr="")
|
||||
if args == ["/usr/bin/opencode", "run", "--help"]:
|
||||
return subprocess.CompletedProcess(
|
||||
args,
|
||||
0,
|
||||
stdout="--format\n--auto\n--dir\n--title\n",
|
||||
stderr="",
|
||||
)
|
||||
raise AssertionError(f"unexpected subprocess.run args: {args}")
|
||||
|
||||
def _fake_smoke_success_run(self, args, **kwargs):
|
||||
if args in (["/usr/bin/opencode", "--version"], ["/usr/bin/opencode", "run", "--help"]):
|
||||
return self._fake_static_readiness_run(args, **kwargs)
|
||||
if args[:3] == ["/usr/bin/opencode", "run", "--format"]:
|
||||
return subprocess.CompletedProcess(args, 0, stdout='{"message":"WIKI_INGEST_SKILL_LOADED"}\n', stderr="")
|
||||
raise AssertionError(f"unexpected subprocess.run args: {args}")
|
||||
|
||||
def _fake_smoke_failure_run(self, args, **kwargs):
|
||||
if args in (["/usr/bin/opencode", "--version"], ["/usr/bin/opencode", "run", "--help"]):
|
||||
return self._fake_static_readiness_run(args, **kwargs)
|
||||
if args[:3] == ["/usr/bin/opencode", "run", "--format"]:
|
||||
return subprocess.CompletedProcess(args, 0, stdout="SKILL_LOAD_FAILED: missing\n", stderr="")
|
||||
raise AssertionError(f"unexpected subprocess.run args: {args}")
|
||||
|
||||
def _fake_smoke_timeout_run(self, args, **kwargs):
|
||||
if args in (["/usr/bin/opencode", "--version"], ["/usr/bin/opencode", "run", "--help"]):
|
||||
return self._fake_static_readiness_run(args, **kwargs)
|
||||
if args[:3] == ["/usr/bin/opencode", "run", "--format"]:
|
||||
raise subprocess.TimeoutExpired(args, kwargs.get("timeout"))
|
||||
raise AssertionError(f"unexpected subprocess.run args: {args}")
|
||||
|
||||
|
||||
class FakePopen:
|
||||
def __init__(self, returncode=None):
|
||||
|
||||
@@ -186,6 +186,7 @@ class RemoteMcpServerTests(unittest.TestCase):
|
||||
self.assertIn("get_ingest_job_status", FakeMCP.tool_names)
|
||||
self.assertIn("get_ingest_job_result", FakeMCP.tool_names)
|
||||
self.assertIn("cancel_ingest_job", FakeMCP.tool_names)
|
||||
self.assertIn("check_ingest_opencode_readiness", FakeMCP.tool_names)
|
||||
|
||||
def test_run_http_server_fails_before_server_creation_without_token(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
|
||||
Reference in New Issue
Block a user