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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
test_loop_tools.py -- opt-in loop skill provisioning.
|
| 3 |
+
|
| 4 |
+
The helper recommends skills for a harness-loop goal and installs only
|
| 5 |
+
loadable local wiki rows. The MCP/toolbox surface is intentionally hidden by
|
| 6 |
+
default because it writes into the configured skills directory.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
from __future__ import annotations
|
| 10 |
+
|
| 11 |
+
import json
|
| 12 |
+
from pathlib import Path
|
| 13 |
+
|
| 14 |
+
import pytest
|
| 15 |
+
|
| 16 |
+
from ctx.adapters.claude_code.install import install_utils
|
| 17 |
+
from ctx.adapters.generic import loop_tools
|
| 18 |
+
from ctx.adapters.generic.ctx_core_tools import CtxCoreToolbox
|
| 19 |
+
from ctx.adapters.generic.providers import ToolCall
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
@pytest.fixture()
|
| 23 |
+
def wiki_dir(tmp_path: Path) -> Path:
|
| 24 |
+
root = tmp_path / "wiki"
|
| 25 |
+
(root / "entities" / "skills").mkdir(parents=True)
|
| 26 |
+
(root / "converted").mkdir(parents=True)
|
| 27 |
+
return root
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
@pytest.fixture()
|
| 31 |
+
def skills_dir(tmp_path: Path) -> Path:
|
| 32 |
+
root = tmp_path / "skills"
|
| 33 |
+
root.mkdir()
|
| 34 |
+
return root
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
@pytest.fixture(autouse=True)
|
| 38 |
+
def isolated_manifest(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
|
| 39 |
+
manifest = tmp_path / "skill-manifest.json"
|
| 40 |
+
monkeypatch.setattr(install_utils, "MANIFEST_PATH", manifest)
|
| 41 |
+
return manifest
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def _seed_skill(wiki_dir: Path, slug: str) -> None:
|
| 45 |
+
converted = wiki_dir / "converted" / slug
|
| 46 |
+
converted.mkdir(parents=True, exist_ok=True)
|
| 47 |
+
(converted / "SKILL.md").write_text(
|
| 48 |
+
f"---\nname: {slug}\nstatus: cataloged\n---\nbody\n",
|
| 49 |
+
encoding="utf-8",
|
| 50 |
+
)
|
| 51 |
+
(wiki_dir / "entities" / "skills" / f"{slug}.md").write_text(
|
| 52 |
+
f"---\nname: {slug}\nstatus: cataloged\n---\nbody\n",
|
| 53 |
+
encoding="utf-8",
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def _local_row(slug: str) -> dict[str, str]:
|
| 58 |
+
return {"name": slug, "skill_id": slug, "type": "skill", "status": "cataloged"}
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def _external_row(slug: str, install_command: str) -> dict[str, str]:
|
| 62 |
+
return {
|
| 63 |
+
"name": slug,
|
| 64 |
+
"skill_id": slug,
|
| 65 |
+
"type": "skill",
|
| 66 |
+
"install_command": install_command,
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def _fake_recommend(monkeypatch: pytest.MonkeyPatch, rows: list[dict[str, str]]) -> None:
|
| 71 |
+
monkeypatch.setattr(
|
| 72 |
+
loop_tools,
|
| 73 |
+
"recommend_for_loop",
|
| 74 |
+
lambda **kwargs: {"capabilities": {"skills": rows}},
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def test_provision_installs_and_returns_use_skills(
|
| 79 |
+
wiki_dir: Path,
|
| 80 |
+
skills_dir: Path,
|
| 81 |
+
monkeypatch: pytest.MonkeyPatch,
|
| 82 |
+
) -> None:
|
| 83 |
+
_seed_skill(wiki_dir, "stripe-webhooks")
|
| 84 |
+
_seed_skill(wiki_dir, "idempotency")
|
| 85 |
+
_fake_recommend(monkeypatch, [_local_row("stripe-webhooks"), _local_row("idempotency")])
|
| 86 |
+
|
| 87 |
+
out = loop_tools.provision_skills(
|
| 88 |
+
wiki_dir=wiki_dir,
|
| 89 |
+
skills_dir=skills_dir,
|
| 90 |
+
goal="harden stripe webhook",
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
assert set(out["use_skills"]) == {"stripe-webhooks", "idempotency"}
|
| 94 |
+
assert set(out["installed"]) == {"stripe-webhooks", "idempotency"}
|
| 95 |
+
assert out["skipped"] == []
|
| 96 |
+
assert (skills_dir / "stripe-webhooks" / "SKILL.md").exists()
|
| 97 |
+
assert (skills_dir / "idempotency" / "SKILL.md").exists()
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def test_reprovision_reports_skipped(
|
| 101 |
+
wiki_dir: Path,
|
| 102 |
+
skills_dir: Path,
|
| 103 |
+
monkeypatch: pytest.MonkeyPatch,
|
| 104 |
+
) -> None:
|
| 105 |
+
_seed_skill(wiki_dir, "stripe-webhooks")
|
| 106 |
+
_fake_recommend(monkeypatch, [_local_row("stripe-webhooks")])
|
| 107 |
+
|
| 108 |
+
loop_tools.provision_skills(wiki_dir=wiki_dir, skills_dir=skills_dir, goal="g")
|
| 109 |
+
out = loop_tools.provision_skills(wiki_dir=wiki_dir, skills_dir=skills_dir, goal="g")
|
| 110 |
+
|
| 111 |
+
assert out["installed"] == []
|
| 112 |
+
assert out["skipped"] == ["stripe-webhooks"]
|
| 113 |
+
assert out["use_skills"] == ["stripe-webhooks"]
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def test_topup_excludes_loaded(
|
| 117 |
+
wiki_dir: Path,
|
| 118 |
+
skills_dir: Path,
|
| 119 |
+
monkeypatch: pytest.MonkeyPatch,
|
| 120 |
+
) -> None:
|
| 121 |
+
_seed_skill(wiki_dir, "a")
|
| 122 |
+
_seed_skill(wiki_dir, "b")
|
| 123 |
+
_fake_recommend(monkeypatch, [_local_row("a"), _local_row("b")])
|
| 124 |
+
|
| 125 |
+
out = loop_tools.provision_skills(
|
| 126 |
+
wiki_dir=wiki_dir,
|
| 127 |
+
skills_dir=skills_dir,
|
| 128 |
+
goal="g",
|
| 129 |
+
exclude=["a"],
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
assert out["use_skills"] == ["b"]
|
| 133 |
+
assert not (skills_dir / "a").exists()
|
| 134 |
+
assert (skills_dir / "b" / "SKILL.md").exists()
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
def test_dry_run_installs_nothing(
|
| 138 |
+
wiki_dir: Path,
|
| 139 |
+
skills_dir: Path,
|
| 140 |
+
monkeypatch: pytest.MonkeyPatch,
|
| 141 |
+
) -> None:
|
| 142 |
+
_seed_skill(wiki_dir, "a")
|
| 143 |
+
_fake_recommend(monkeypatch, [_local_row("a")])
|
| 144 |
+
|
| 145 |
+
out = loop_tools.provision_skills(
|
| 146 |
+
wiki_dir=wiki_dir,
|
| 147 |
+
skills_dir=skills_dir,
|
| 148 |
+
goal="g",
|
| 149 |
+
dry_run=True,
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
assert out["dry_run"] is True
|
| 153 |
+
assert out["would_install"] == ["a"]
|
| 154 |
+
assert out["installed"] == []
|
| 155 |
+
assert out["use_skills"] == ["a"]
|
| 156 |
+
assert not (skills_dir / "a").exists()
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
def test_missing_skill_goes_to_failed(
|
| 160 |
+
wiki_dir: Path,
|
| 161 |
+
skills_dir: Path,
|
| 162 |
+
monkeypatch: pytest.MonkeyPatch,
|
| 163 |
+
) -> None:
|
| 164 |
+
_fake_recommend(monkeypatch, [_local_row("ghost")])
|
| 165 |
+
|
| 166 |
+
out = loop_tools.provision_skills(wiki_dir=wiki_dir, skills_dir=skills_dir, goal="g")
|
| 167 |
+
|
| 168 |
+
assert out["use_skills"] == []
|
| 169 |
+
assert [failure["slug"] for failure in out["failed"]] == ["ghost"]
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
def test_external_row_goes_to_manual(
|
| 173 |
+
wiki_dir: Path,
|
| 174 |
+
skills_dir: Path,
|
| 175 |
+
monkeypatch: pytest.MonkeyPatch,
|
| 176 |
+
) -> None:
|
| 177 |
+
_seed_skill(wiki_dir, "local")
|
| 178 |
+
_fake_recommend(monkeypatch, [_local_row("local"), _external_row("remote", "npx remote")])
|
| 179 |
+
|
| 180 |
+
out = loop_tools.provision_skills(wiki_dir=wiki_dir, skills_dir=skills_dir, goal="g")
|
| 181 |
+
|
| 182 |
+
assert out["use_skills"] == ["local"]
|
| 183 |
+
assert [manual["name"] for manual in out["manual"]] == ["remote"]
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
def test_empty_goal_returns_empty(wiki_dir: Path, skills_dir: Path) -> None:
|
| 187 |
+
out = loop_tools.provision_skills(wiki_dir=wiki_dir, skills_dir=skills_dir, goal=" ")
|
| 188 |
+
|
| 189 |
+
assert out["use_skills"] == []
|
| 190 |
+
assert "note" in out
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
def test_toolbox_hides_loop_write_tools_by_default() -> None:
|
| 194 |
+
box = CtxCoreToolbox()
|
| 195 |
+
names = {definition.name for definition in box.tool_definitions()}
|
| 196 |
+
|
| 197 |
+
assert "ctx__loop_provision" not in names
|
| 198 |
+
assert "ctx__loop_topup" not in names
|
| 199 |
+
with pytest.raises(ValueError, match="not allowed"):
|
| 200 |
+
box.dispatch(ToolCall(id="t", name="ctx__loop_provision", arguments={"goal": "g"}))
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
def test_toolbox_advertises_loop_tools_when_explicitly_allowed() -> None:
|
| 204 |
+
allowed = {"ctx__loop_provision", "ctx__loop_topup"}
|
| 205 |
+
names = {
|
| 206 |
+
definition.name
|
| 207 |
+
for definition in CtxCoreToolbox(allowed_tool_names=allowed).tool_definitions()
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
assert names == allowed
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
def test_toolbox_dispatches_allowed_dry_run_provision(
|
| 214 |
+
wiki_dir: Path,
|
| 215 |
+
skills_dir: Path,
|
| 216 |
+
monkeypatch: pytest.MonkeyPatch,
|
| 217 |
+
) -> None:
|
| 218 |
+
import ctx_config
|
| 219 |
+
|
| 220 |
+
_seed_skill(wiki_dir, "a")
|
| 221 |
+
_fake_recommend(monkeypatch, [_local_row("a")])
|
| 222 |
+
monkeypatch.setattr(ctx_config.cfg, "skills_dir", skills_dir)
|
| 223 |
+
box = CtxCoreToolbox(
|
| 224 |
+
wiki_dir=wiki_dir,
|
| 225 |
+
allowed_tool_names={"ctx__loop_provision"},
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
payload = json.loads(
|
| 229 |
+
box.dispatch(
|
| 230 |
+
ToolCall(
|
| 231 |
+
id="t",
|
| 232 |
+
name="ctx__loop_provision",
|
| 233 |
+
arguments={"goal": "g", "dry_run": True},
|
| 234 |
+
)
|
| 235 |
+
)
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
+
assert payload["would_install"] == ["a"]
|
| 239 |
+
assert payload["installed"] == []
|
| 240 |
+
assert not (skills_dir / "a").exists()
|
src/tests/test_mcp_server.py
CHANGED
|
@@ -77,6 +77,10 @@ _READ_ONLY_TOOL_NAMES = {
|
|
| 77 |
"ctx__wiki_search",
|
| 78 |
"ctx__wiki_get",
|
| 79 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
|
| 82 |
# ── Helpers ─────────────────────────────────────────────────────────────────
|
|
@@ -311,6 +315,7 @@ class TestToolsList:
|
|
| 311 |
assert "error" not in response
|
| 312 |
tool_names = {t["name"] for t in response["result"]["tools"]}
|
| 313 |
assert tool_names == _EXPECTED_TOOL_NAMES
|
|
|
|
| 314 |
|
| 315 |
def test_tool_shape_is_mcp_standard(self) -> None:
|
| 316 |
frames = _drive(_encode_request(1, "tools/list"))
|
|
@@ -328,6 +333,13 @@ class TestToolsList:
|
|
| 328 |
|
| 329 |
assert {tool["name"] for tool in result["tools"]} == _READ_ONLY_TOOL_NAMES
|
| 330 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 331 |
|
| 332 |
# ── tools/call ──────────────────────────────────────────────────────────────
|
| 333 |
|
|
@@ -388,6 +400,18 @@ class TestToolsCall:
|
|
| 388 |
)
|
| 389 |
assert frames[0]["error"]["code"] == _ErrorCode.METHOD_NOT_FOUND
|
| 390 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 391 |
def test_scoped_state_rejects_disallowed_tool_call(self) -> None:
|
| 392 |
state = _ServerState(allowed_tool_names=frozenset(_READ_ONLY_TOOL_NAMES))
|
| 393 |
|
|
|
|
| 77 |
"ctx__wiki_search",
|
| 78 |
"ctx__wiki_get",
|
| 79 |
}
|
| 80 |
+
_LOOP_PROVISION_TOOL_NAMES = {
|
| 81 |
+
"ctx__loop_provision",
|
| 82 |
+
"ctx__loop_topup",
|
| 83 |
+
}
|
| 84 |
|
| 85 |
|
| 86 |
# ── Helpers ─────────────────────────────────────────────────────────────────
|
|
|
|
| 315 |
assert "error" not in response
|
| 316 |
tool_names = {t["name"] for t in response["result"]["tools"]}
|
| 317 |
assert tool_names == _EXPECTED_TOOL_NAMES
|
| 318 |
+
assert tool_names.isdisjoint(_LOOP_PROVISION_TOOL_NAMES)
|
| 319 |
|
| 320 |
def test_tool_shape_is_mcp_standard(self) -> None:
|
| 321 |
frames = _drive(_encode_request(1, "tools/list"))
|
|
|
|
| 333 |
|
| 334 |
assert {tool["name"] for tool in result["tools"]} == _READ_ONLY_TOOL_NAMES
|
| 335 |
|
| 336 |
+
def test_state_can_explicitly_allow_loop_provision_tools(self) -> None:
|
| 337 |
+
state = _ServerState(allowed_tool_names=frozenset(_LOOP_PROVISION_TOOL_NAMES))
|
| 338 |
+
|
| 339 |
+
result = _handle_tools_list(state, {})
|
| 340 |
+
|
| 341 |
+
assert {tool["name"] for tool in result["tools"]} == _LOOP_PROVISION_TOOL_NAMES
|
| 342 |
+
|
| 343 |
|
| 344 |
# ── tools/call ──────────────────────────────────────────────────────────────
|
| 345 |
|
|
|
|
| 400 |
)
|
| 401 |
assert frames[0]["error"]["code"] == _ErrorCode.METHOD_NOT_FOUND
|
| 402 |
|
| 403 |
+
def test_default_state_rejects_loop_provision_tool_call(self) -> None:
|
| 404 |
+
with pytest.raises(_JsonRpcError) as ei:
|
| 405 |
+
_handle_tools_call(
|
| 406 |
+
_ServerState(),
|
| 407 |
+
{
|
| 408 |
+
"name": "ctx__loop_provision",
|
| 409 |
+
"arguments": {"goal": "install loop skills", "dry_run": True},
|
| 410 |
+
},
|
| 411 |
+
)
|
| 412 |
+
|
| 413 |
+
assert ei.value.code == _ErrorCode.METHOD_NOT_FOUND
|
| 414 |
+
|
| 415 |
def test_scoped_state_rejects_disallowed_tool_call(self) -> None:
|
| 416 |
state = _ServerState(allowed_tool_names=frozenset(_READ_ONLY_TOOL_NAMES))
|
| 417 |
|