File size: 52,801 Bytes
31e282f 039a22e 31e282f b102502 31e282f 039a22e 31e282f 039a22e 31e282f 039a22e 31e282f 039a22e 94e1c67 0350621 31e282f 2ddde20 31e282f 2ddde20 31e282f b102502 31e282f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 | """Tests for ctx_init — bootstrap ~/.claude/ scaffolding."""
from __future__ import annotations
import builtins
import hashlib
import io
import json
import sqlite3
import sys
import tarfile
import zlib
from pathlib import Path
from types import SimpleNamespace
import networkx as nx
import pytest
import ctx_init as ci
from ctx.core.graph.graph_packs import write_base_pack
from ctx.core.graph.graph_store import validate_graph_store
from ctx.core.wiki.wiki_packs import write_wiki_base_pack
def _write_dashboard_index(path: Path, *, export_id: str = "test-export") -> None:
conn = sqlite3.connect(path)
try:
conn.execute("CREATE TABLE meta(key TEXT PRIMARY KEY, value TEXT NOT NULL)")
conn.execute(
"CREATE TABLE nodes(id TEXT PRIMARY KEY,label TEXT,type TEXT,tags TEXT,"
"description TEXT,quality_score REAL,usage_score REAL,degree INTEGER)"
)
conn.execute(
"CREATE TABLE slug_index(slug TEXT,type TEXT,node_id TEXT,"
"PRIMARY KEY(slug,type,node_id))"
)
conn.execute("CREATE TABLE neighbors(source TEXT PRIMARY KEY, payload BLOB NOT NULL)")
conn.executemany(
"INSERT INTO meta VALUES(?,?)",
[
("export_id", json.dumps(export_id)),
("nodes_count", "1"),
("edges_count", "0"),
("max_degree", "1"),
("top_k", "40"),
],
)
conn.execute(
"INSERT INTO nodes VALUES(?,?,?,?,?,?,?,?)",
("skill:current", "current", "skill", "[]", "", None, None, 0),
)
conn.execute("INSERT INTO slug_index VALUES(?,?,?)", ("current", "skill", "skill:current"))
conn.execute("INSERT INTO neighbors VALUES(?,?)", ("skill:current", zlib.compress(b"[]")))
conn.commit()
finally:
conn.close()
def _artifact_sha256_or_lfs_oid(path: Path, *, normalize_text: bool = False) -> str:
data = path.read_bytes()
if data.startswith(b"version https://git-lfs.github.com/spec/v1\n"):
for line in data.decode("utf-8").splitlines():
if line.startswith("oid sha256:"):
return line.removeprefix("oid sha256:")
if normalize_text:
data = data.replace(b"\r\n", b"\n")
return hashlib.sha256(data).hexdigest()
def test_ensure_directories_creates_standard_tree(tmp_path: Path) -> None:
created = ci.ensure_directories(tmp_path)
# First call should create every standard subdir.
assert len(created) == len(ci._STANDARD_SUBDIRS)
for sub in ci._STANDARD_SUBDIRS:
assert (tmp_path / sub).is_dir(), f"missing {sub}"
def test_ensure_directories_is_idempotent(tmp_path: Path) -> None:
first = ci.ensure_directories(tmp_path)
assert len(first) > 0
second = ci.ensure_directories(tmp_path)
assert second == [], "second call should not recreate anything"
def test_seed_user_config_writes_once(tmp_path: Path) -> None:
tmp_path.mkdir(exist_ok=True)
first = ci.seed_user_config(tmp_path)
assert first is not None
assert first.exists()
body = first.read_text(encoding="utf-8")
assert "skill-system-config.json" in body
# Second call returns None (file already exists, force=False).
second = ci.seed_user_config(tmp_path)
assert second is None
def test_seed_user_config_respects_force(tmp_path: Path) -> None:
target = tmp_path / "skill-system-config.json"
target.write_text("user-custom-content", encoding="utf-8")
# Without force → don't touch.
assert ci.seed_user_config(tmp_path, force=False) is None
assert target.read_text() == "user-custom-content"
# With force → overwrite.
result = ci.seed_user_config(tmp_path, force=True)
assert result == target
assert "skill-system-config.json" in target.read_text()
def test_main_creates_everything_in_dry_mode(tmp_path: Path, monkeypatch,
capsys) -> None:
"""End-to-end: ``ctx-init`` (no flags) creates dirs + config + toolboxes
without touching hooks or graph."""
monkeypatch.setattr(ci, "_claude_dir", lambda: tmp_path)
# Short-circuit subprocess.run to avoid spawning a real toolbox/graph CLI
# in tests. Verify that main() doesn't call install_hooks or build_graph
# when those flags are absent.
calls: list[list[str]] = []
class _FakeResult:
returncode = 0
stdout = ""
stderr = ""
def fake_run(cmd, **kwargs):
calls.append(list(cmd))
return _FakeResult()
monkeypatch.setattr(ci.subprocess, "run", fake_run)
rc = ci.main([])
assert rc == 0
# toolbox init should have been invoked
toolbox_calls = [c for c in calls if "toolbox" in " ".join(c)]
assert toolbox_calls, "toolbox init not invoked"
# inject_hooks / wiki_graphify must NOT be invoked without flags
for c in calls:
assert "inject_hooks" not in " ".join(c)
assert "wiki_graphify" not in " ".join(c)
out = capsys.readouterr().out
assert "[ok]" in out
assert "[skip] hook injection" in out
assert "[skip] graph install" in out
def test_main_treats_existing_toolboxes_as_idempotent_skip(
tmp_path: Path,
monkeypatch,
capsys,
) -> None:
monkeypatch.setattr(ci, "_claude_dir", lambda: tmp_path)
class _FakeResult:
returncode = 1
stdout = ""
stderr = (
"Global config already has 5 toolbox(es). "
"Use --force to overwrite."
)
monkeypatch.setattr(ci.subprocess, "run", lambda *_args, **_kwargs: _FakeResult())
rc = ci.main(["--model-mode", "skip"])
captured = capsys.readouterr()
assert rc == 0
assert "starter toolboxes already present" in captured.out
assert "toolbox init returned" not in captured.err
assert "Global config already has" not in captured.err
def test_main_auto_wizard_in_terminal_configures_custom_model(
tmp_path: Path,
monkeypatch,
) -> None:
monkeypatch.setattr(ci, "_claude_dir", lambda: tmp_path)
monkeypatch.setattr(ci, "_stdio_is_interactive", lambda: True)
monkeypatch.setattr(
ci,
"recommend_harnesses",
lambda goal, top_k=5, model_provider=None, model=None: [],
)
answers = iter([
"y", # hooks
"enriched", # knowledge mode
"n", # graph
"custom", # model mode
"openai/gpt-5.5", # model
"", # provider default: openai
"", # api key env default: OPENAI_API_KEY
"", # base URL
"build CAD artifacts",
"windows python", # runtime / OS
"supervised", # autonomy
"filesystem shell", # allowed tools
"pytest ruff", # verification
"private repo", # privacy / network
"mcp", # attach mode
"n", # validate model
])
monkeypatch.setattr(builtins, "input", lambda _prompt: next(answers))
calls: list[list[str]] = []
class _FakeResult:
returncode = 0
stdout = ""
stderr = ""
def _fake_run(cmd: list[str], **_kwargs: object) -> _FakeResult:
calls.append(list(cmd))
return _FakeResult()
monkeypatch.setattr(ci.subprocess, "run", _fake_run)
rc = ci.main([])
assert rc == 0
assert any("ctx.adapters.claude_code.inject_hooks" in c for c in calls)
assert not any("ctx.core.wiki.wiki_graphify" in c for c in calls)
profile = json.loads((tmp_path / "ctx-model-profile.json").read_text())
assert profile["mode"] == "custom"
assert profile["provider"] == "openai"
assert profile["model"] == "openai/gpt-5.5"
assert profile["api_key_env"] == "OPENAI_API_KEY"
assert profile["goal"] == "build CAD artifacts"
assert profile["knowledge_mode"] == "enriched"
assert profile["harness_requirements"] == {
"runtime": "windows python",
"autonomy": "supervised",
"tools": "filesystem shell",
"verification": "pytest ruff",
"privacy": "private repo",
"attach_mode": "mcp",
"api_key_env": "OPENAI_API_KEY",
}
user_config = json.loads((tmp_path / "skill-system-config.json").read_text())
assert user_config["knowledge"]["mode"] == "enriched"
def test_wizard_flag_prompts_without_tty(tmp_path: Path, monkeypatch) -> None:
monkeypatch.setattr(ci, "_claude_dir", lambda: tmp_path)
monkeypatch.setattr(ci, "_stdio_is_interactive", lambda: False)
monkeypatch.setattr(ci, "seed_toolboxes", lambda force=False: 0)
monkeypatch.setattr(
ci,
"recommend_harnesses",
lambda goal, top_k=5, model_provider=None, model=None: [],
)
answers = iter([
"n", # hooks
"local", # knowledge mode
"claude-code", # model mode
"maintain FastAPI services",
])
monkeypatch.setattr(builtins, "input", lambda _prompt: next(answers))
rc = ci.main(["--wizard"])
assert rc == 0
profile = json.loads((tmp_path / "ctx-model-profile.json").read_text())
assert profile["mode"] == "claude-code"
assert profile["goal"] == "maintain FastAPI services"
assert profile["knowledge_mode"] == "local"
user_config = json.loads((tmp_path / "skill-system-config.json").read_text())
assert user_config["knowledge"]["mode"] == "local"
def test_explicit_args_do_not_auto_wizard_in_terminal(
tmp_path: Path,
monkeypatch,
) -> None:
monkeypatch.setattr(ci, "_claude_dir", lambda: tmp_path)
monkeypatch.setattr(ci, "_stdio_is_interactive", lambda: True)
monkeypatch.setattr(ci, "seed_toolboxes", lambda force=False: 0)
monkeypatch.setattr(
builtins,
"input",
lambda _prompt: (_ for _ in ()).throw(AssertionError("unexpected prompt")),
)
assert ci.main(["--model-mode", "skip", "--knowledge-mode", "local"]) == 0
user_config = json.loads((tmp_path / "skill-system-config.json").read_text())
assert user_config["knowledge"]["mode"] == "local"
def test_main_with_hooks_flag_invokes_inject(tmp_path: Path, monkeypatch) -> None:
monkeypatch.setattr(ci, "_claude_dir", lambda: tmp_path)
calls: list[list[str]] = []
class _FakeResult:
returncode = 0
stdout = ""
stderr = ""
def _fake_run(cmd: list[str], **_kwargs: object) -> _FakeResult:
calls.append(list(cmd))
return _FakeResult()
monkeypatch.setattr(ci.subprocess, "run", _fake_run)
rc = ci.main(["--hooks"])
assert rc == 0
assert any("ctx.adapters.claude_code.inject_hooks" in c for c in calls)
assert not any(c == "inject_hooks" for call in calls for c in call)
def _write_graph_archive(
tmp_path: Path,
*,
include_graph_pack: bool = False,
graph_pack_export_id: str = "test-export",
include_wiki_pack: bool = False,
wiki_pack_export_id: str = "test-export",
) -> Path:
source = tmp_path / "archive-source"
graph_out = source / "graphify-out"
graph_out.mkdir(parents=True)
(graph_out / "graph.json").write_text(
json.dumps({"graph": {"export_id": "test-export"}, "nodes": [], "links": []}),
encoding="utf-8",
)
(graph_out / "graph-delta.json").write_text(
json.dumps({"export_id": "test-export", "nodes": [], "edges": []}),
encoding="utf-8",
)
(graph_out / "communities.json").write_text(
json.dumps({"export_id": "test-export", "total_communities": 0}),
encoding="utf-8",
)
(graph_out / "graph-report.md").write_text(
"# Graph Report\n\n> Export ID: test-export\n",
encoding="utf-8",
)
(graph_out / "graph-export-manifest.json").write_text(
json.dumps({
"version": 1,
"export_id": "test-export",
"artifacts": {
"graph": "graph.json",
"delta": "graph-delta.json",
"communities": "communities.json",
"report": "graph-report.md",
},
}),
encoding="utf-8",
)
_write_dashboard_index(graph_out / "dashboard-neighborhoods.sqlite3")
external = source / "external-catalogs" / "skills-sh"
external.mkdir(parents=True)
(external / "catalog.json").write_text("{}", encoding="utf-8")
entities = source / "entities" / "skills"
entities.mkdir(parents=True)
(entities / "current.md").write_text("# Current\n", encoding="utf-8")
(source / "index.md").write_text("# Wiki\n", encoding="utf-8")
if include_graph_pack:
graph = nx.Graph()
graph.add_node("skill:current", label="current", type="skill")
write_base_pack(
pack_dir=graph_out / "packs" / f"base-{graph_pack_export_id}",
pack_id=f"base-{graph_pack_export_id}",
base_export_id=graph_pack_export_id,
config_hash="config-1",
model_id="model-1",
graph=graph,
)
if include_wiki_pack:
write_wiki_base_pack(
pack_dir=source / "wiki-packs" / f"base-{wiki_pack_export_id}",
pack_id=f"base-{wiki_pack_export_id}",
base_export_id=wiki_pack_export_id,
pages={
"index.md": "# Wiki\n",
"entities/skills/current.md": "# Current\n",
},
)
archive = tmp_path / "wiki-graph.tar.gz"
with tarfile.open(archive, "w:gz") as tf:
for path in sorted(source.rglob("*")):
if path.is_file():
tf.add(path, arcname=path.relative_to(source).as_posix())
return archive
def _tar_text(tf: tarfile.TarFile, name: str, text: str) -> None:
payload = text.encode("utf-8")
info = tarfile.TarInfo(name)
info.size = len(payload)
tf.addfile(info, io.BytesIO(payload))
def _tar_bytes(tf: tarfile.TarFile, name: str, payload: bytes) -> None:
info = tarfile.TarInfo(name)
info.size = len(payload)
tf.addfile(info, io.BytesIO(payload))
def test_download_graph_archive_verifies_sha256(
tmp_path: Path,
monkeypatch,
) -> None:
payload = b"graph archive bytes"
class _Response(io.BytesIO):
def __enter__(self) -> "_Response":
return self
def __exit__(self, *_args: object) -> None:
return None
monkeypatch.setattr(
ci.urllib.request,
"urlopen",
lambda _url, timeout=120: _Response(payload),
)
destination = tmp_path / "wiki-graph.tar.gz"
ci._download_graph_archive(
destination,
url="https://example.invalid/wiki-graph.tar.gz",
expected_sha256=hashlib.sha256(payload).hexdigest(),
)
assert destination.read_bytes() == payload
bad_destination = tmp_path / "bad-wiki-graph.tar.gz"
with pytest.raises(ValueError, match="checksum mismatch"):
ci._download_graph_archive(
bad_destination,
url="https://example.invalid/wiki-graph.tar.gz",
expected_sha256="0" * 64,
)
assert not bad_destination.exists()
def test_graph_download_checksums_match_shipped_artifacts() -> None:
root = Path(__file__).resolve().parent.parent.parent
for mode, archive_name in ci._GRAPH_ARCHIVE_NAMES.items():
path = root / "graph" / archive_name
assert ci._GRAPH_ARCHIVE_SHA256[mode] == _artifact_sha256_or_lfs_oid(path)
overlay_path = root / "graph" / ci._GRAPH_ENTITY_OVERLAY_NAME
assert ci._GRAPH_ENTITY_OVERLAY_SHA256 == _artifact_sha256_or_lfs_oid(
overlay_path,
normalize_text=True,
)
def test_local_graph_archive_checksum_is_verified(tmp_path: Path) -> None:
archive = tmp_path / "wiki-graph-runtime.tar.gz"
archive.write_bytes(b"not the shipped runtime archive")
with pytest.raises(ValueError, match="local graph archive checksum mismatch"):
ci._verify_local_graph_archive(archive, requested_install_mode="runtime")
def test_lfs_pointer_graph_archive_is_ignored(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
graph_dir = tmp_path / "graph"
graph_dir.mkdir()
(graph_dir / "wiki-graph-runtime.tar.gz").write_text(
"version https://git-lfs.github.com/spec/v1\n"
"oid sha256:993fc08377fdb09edcff4414c59b10fc121189b4a161bf796e3f8f6600907bb1\n"
"size 122141091\n",
encoding="utf-8",
)
cwd = tmp_path / "cwd"
cwd.mkdir()
monkeypatch.chdir(cwd)
monkeypatch.setattr(ci, "__file__", str(tmp_path / "src" / "ctx_init.py"))
assert ci._find_local_graph_archive("runtime") is None
def test_custom_graph_url_requires_checksum_or_explicit_opt_out(
tmp_path: Path,
monkeypatch,
) -> None:
monkeypatch.setattr(ci, "_find_local_graph_archive", lambda _mode: None)
monkeypatch.setattr(
ci,
"_download_graph_archive",
lambda *_args, **_kwargs: (_ for _ in ()).throw(
AssertionError("download should be blocked before network access")
),
)
rc = ci.build_graph(
tmp_path / "home",
graph_url="https://example.invalid/wiki-graph.tar.gz",
)
assert rc == 1
def test_custom_graph_url_bypasses_local_archive(
tmp_path: Path,
monkeypatch,
) -> None:
archive = _write_graph_archive(tmp_path)
archive_bytes = archive.read_bytes()
calls: list[dict[str, object]] = []
monkeypatch.setattr(
ci,
"_find_local_graph_archive",
lambda _mode: (_ for _ in ()).throw(
AssertionError("explicit graph_url must not use local archive")
),
)
def fake_download(destination: Path, **kwargs: object) -> None:
calls.append(dict(kwargs))
destination.write_bytes(archive_bytes)
monkeypatch.setattr(ci, "_download_graph_archive", fake_download)
monkeypatch.setattr(ci, "_install_graph_entity_overlay", lambda *_a, **_k: None)
rc = ci.build_graph(
tmp_path / "home",
graph_url="https://example.invalid/custom-wiki-graph.tar.gz",
graph_sha256=hashlib.sha256(archive_bytes).hexdigest(),
)
assert rc == 0
assert calls == [
{
"url": "https://example.invalid/custom-wiki-graph.tar.gz",
"expected_sha256": hashlib.sha256(archive_bytes).hexdigest(),
}
]
def test_main_with_graph_flag_installs_prebuilt_graph(
tmp_path: Path,
monkeypatch,
) -> None:
claude = tmp_path / "home"
archive = _write_graph_archive(tmp_path)
monkeypatch.setattr(ci, "_claude_dir", lambda: claude)
monkeypatch.setattr(ci, "seed_toolboxes", lambda force=False: 0)
monkeypatch.setattr(
ci,
"_find_local_graph_archive",
lambda _install_mode="runtime": archive,
raising=False,
)
monkeypatch.setattr(ci, "_verify_local_graph_archive", lambda *_a, **_k: None)
monkeypatch.setattr(
ci,
"_download_graph_archive",
lambda _dest, **_kwargs: (_ for _ in ()).throw(
AssertionError("unexpected release download")
),
raising=False,
)
calls: list[list[str]] = []
class _FakeResult:
returncode = 0
stdout = ""
stderr = ""
def _fake_run(cmd: list[str], **_kwargs: object) -> _FakeResult:
calls.append(list(cmd))
return _FakeResult()
monkeypatch.setattr(ci.subprocess, "run", _fake_run)
rc = ci.main(["--graph", "--model-mode", "skip"])
assert rc == 0
graph_json = claude / "skill-wiki" / "graphify-out" / "graph.json"
graph_payload = json.loads(graph_json.read_text(encoding="utf-8"))
assert graph_payload["graph"]["export_id"] == "test-export"
assert not (
claude / "skill-wiki" / "entities" / "skills" / "current.md"
).exists()
assert not any("ctx.core.wiki.wiki_graphify" in c for c in calls)
assert not any(c == "wiki_graphify" for call in calls for c in call)
def test_graph_install_copies_local_entity_overlay(
tmp_path: Path,
monkeypatch,
) -> None:
claude = tmp_path / "home"
archive = _write_graph_archive(tmp_path)
overlay = tmp_path / "entity-overlays.jsonl"
overlay.write_text(
json.dumps({
"overlay_id": "test-overlay",
"nodes": [{"id": "harness:mirage", "type": "harness"}],
"edges": [
{
"source": "harness:mirage",
"target": "skill:codex-review",
"weight": 0.5,
"similarity_score": 0.5,
"method": "manual_direct_overlay_v1",
"rank": 1,
"provenance": "manual_overlay_v1",
}
],
})
+ "\n",
encoding="utf-8",
)
monkeypatch.setattr(
ci,
"_find_local_graph_archive",
lambda _install_mode="runtime": archive,
raising=False,
)
monkeypatch.setattr(ci, "_find_local_graph_entity_overlay", lambda: overlay)
monkeypatch.setattr(ci, "_verify_local_graph_archive", lambda *_a, **_k: None)
assert ci.build_graph(claude) == 0
installed = claude / "skill-wiki" / "graphify-out" / "entity-overlays.jsonl"
payload = json.loads(installed.read_text(encoding="utf-8"))
assert payload["overlay_id"] == "test-overlay"
assert payload["edges"][0]["method"] == "manual_direct_overlay_v1"
@pytest.mark.parametrize("field", ["semantic_sim", "tag_sim", "token_sim"])
def test_graph_overlay_validation_rejects_out_of_range_similarity_fields(
tmp_path: Path,
field: str,
) -> None:
overlay = tmp_path / "entity-overlays.jsonl"
overlay.write_text(
json.dumps({
"nodes": [{"id": "skill:a"}],
"edges": [
{
"source": "skill:a",
"target": "skill:b",
"weight": 0.5,
"final_weight": 0.5,
field: 2.0,
},
],
})
+ "\n",
encoding="utf-8",
)
with pytest.raises(ValueError, match=f"{field} must be 0..1"):
ci._validate_graph_entity_overlay(overlay)
def test_graph_overlay_validation_rejects_weight_final_weight_drift(
tmp_path: Path,
) -> None:
overlay = tmp_path / "entity-overlays.jsonl"
overlay.write_text(
json.dumps({
"nodes": [{"id": "skill:a"}],
"edges": [
{
"source": "skill:a",
"target": "skill:b",
"weight": 0.7,
"final_weight": 0.5,
},
],
})
+ "\n",
encoding="utf-8",
)
with pytest.raises(ValueError, match="weight must equal final_weight"):
ci._validate_graph_entity_overlay(overlay)
def test_runtime_graph_install_extracts_harness_pages_after_required_files(
tmp_path: Path,
monkeypatch,
) -> None:
archive = tmp_path / "ordered-runtime-wiki-graph.tar.gz"
with tarfile.open(archive, "w:gz") as tf:
_tar_text(
tf,
"graphify-out/graph.json",
json.dumps({
"graph": {"export_id": "test-export"},
"nodes": [{"id": "harness:text-to-cad", "type": "harness"}],
"links": [],
}),
)
_tar_text(
tf,
"graphify-out/graph-delta.json",
json.dumps({"export_id": "test-export", "nodes": [], "edges": []}),
)
_tar_text(
tf,
"graphify-out/communities.json",
json.dumps({"export_id": "test-export", "total_communities": 0}),
)
_tar_text(tf, "graphify-out/graph-report.md", "# Graph Report\n")
_tar_text(
tf,
"graphify-out/graph-export-manifest.json",
json.dumps({
"version": 1,
"export_id": "test-export",
"artifacts": {
"graph": "graph.json",
"delta": "graph-delta.json",
"communities": "communities.json",
"report": "graph-report.md",
},
}),
)
index_path = tmp_path / "runtime-dashboard-neighborhoods.sqlite3"
_write_dashboard_index(index_path)
_tar_bytes(
tf,
"graphify-out/dashboard-neighborhoods.sqlite3",
index_path.read_bytes(),
)
_tar_text(tf, "external-catalogs/skills-sh/catalog.json", "{}")
_tar_text(tf, "index.md", "# Wiki\n")
_tar_text(tf, "entities/harnesses/text-to-cad.md", "# Text to CAD\n")
_tar_text(tf, "entities/skills/not-runtime.md", "# Not runtime\n")
claude = tmp_path / "home"
monkeypatch.setattr(
ci,
"_find_local_graph_archive",
lambda _install_mode="runtime": archive,
)
monkeypatch.setattr(ci, "_verify_local_graph_archive", lambda *_a, **_k: None)
assert ci.build_graph(claude) == 0
assert (
claude / "skill-wiki" / "entities" / "harnesses" / "text-to-cad.md"
).is_file()
assert not (
claude / "skill-wiki" / "entities" / "skills" / "not-runtime.md"
).exists()
def test_runtime_graph_install_preserves_existing_non_harness_entities(
tmp_path: Path,
monkeypatch,
) -> None:
archive = _write_graph_archive(tmp_path)
claude = tmp_path / "home"
local_skill = claude / "skill-wiki" / "entities" / "skills" / "private.md"
local_agent = claude / "skill-wiki" / "entities" / "agents" / "private.md"
local_mcp = claude / "skill-wiki" / "entities" / "mcp-servers" / "p" / "private.md"
local_harness = claude / "skill-wiki" / "entities" / "harnesses" / "old.md"
for path in (local_skill, local_agent, local_mcp, local_harness):
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(f"# {path.stem}\n", encoding="utf-8")
monkeypatch.setattr(
ci,
"_find_local_graph_archive",
lambda _install_mode="runtime": archive,
)
monkeypatch.setattr(ci, "_verify_local_graph_archive", lambda *_a, **_k: None)
monkeypatch.setattr(ci, "_install_graph_entity_overlay", lambda *_a, **_k: None)
assert ci.build_graph(claude, force=True, install_mode="runtime") == 0
assert local_skill.is_file()
assert local_agent.is_file()
assert local_mcp.is_file()
assert not local_harness.exists()
def test_graph_install_rejects_incomplete_archive(
tmp_path: Path,
monkeypatch,
) -> None:
source = tmp_path / "incomplete-source"
graph_out = source / "graphify-out"
graph_out.mkdir(parents=True)
(graph_out / "graph.json").write_text(
json.dumps({"graph": {"export_id": "partial"}, "nodes": []}),
encoding="utf-8",
)
archive = tmp_path / "incomplete-wiki-graph.tar.gz"
with tarfile.open(archive, "w:gz") as tf:
tf.add(graph_out / "graph.json", arcname="graphify-out/graph.json")
claude = tmp_path / "home"
monkeypatch.setattr(
ci,
"_find_local_graph_archive",
lambda _install_mode="runtime": archive,
)
monkeypatch.setattr(ci, "_verify_local_graph_archive", lambda *_a, **_k: None)
assert ci.build_graph(claude) == 1
assert not (claude / "skill-wiki" / "graphify-out" / "graph.json").exists()
def test_graph_install_validation_does_not_parse_full_graph_json(
tmp_path: Path,
monkeypatch,
) -> None:
wiki = tmp_path / "wiki"
graph_out = wiki / "graphify-out"
graph_out.mkdir(parents=True)
(wiki / "index.md").write_text("# Wiki\n", encoding="utf-8")
(graph_out / "graph.json").write_text(
json.dumps({"graph": {"export_id": "test-export"}, "nodes": [], "links": []}),
encoding="utf-8",
)
(graph_out / "graph-delta.json").write_text(
json.dumps({"export_id": "test-export", "nodes": [], "edges": []}),
encoding="utf-8",
)
(graph_out / "communities.json").write_text(
json.dumps({"export_id": "test-export", "total_communities": 0}),
encoding="utf-8",
)
(graph_out / "graph-report.md").write_text(
"# Graph Report\n\n> Export ID: test-export\n",
encoding="utf-8",
)
(graph_out / "graph-export-manifest.json").write_text(
json.dumps({
"version": 1,
"export_id": "test-export",
"artifacts": {
"graph": "graph.json",
"delta": "graph-delta.json",
"communities": "communities.json",
"report": "graph-report.md",
},
}),
encoding="utf-8",
)
_write_dashboard_index(graph_out / "dashboard-neighborhoods.sqlite3")
external = wiki / "external-catalogs" / "skills-sh"
external.mkdir(parents=True)
(external / "catalog.json").write_text("{}", encoding="utf-8")
def guarded_read(path: Path) -> object:
if path.name == "graph.json":
raise AssertionError("install validation must not parse full graph.json")
return json.loads(path.read_text(encoding="utf-8"))
monkeypatch.setattr(ci, "_read_json_file", guarded_read)
ci._validate_graph_install_tree(wiki)
def test_graph_json_outline_scans_middle_for_edges_key(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(ci, "_GRAPH_JSON_OUTLINE_BYTES", 32)
filler = "x" * 256
graph_json = tmp_path / "graph.json"
graph_json.write_text(
'{"nodes":["' + filler + '"],"edges":[],"graph":"' + filler + '"}',
encoding="utf-8",
)
ci._validate_graph_json_outline(graph_json)
def test_graph_install_validation_accepts_base_pack_without_graph_json(
tmp_path: Path,
) -> None:
wiki = tmp_path / "wiki"
graph_out = wiki / "graphify-out"
graph_out.mkdir(parents=True)
(wiki / "index.md").write_text("# Wiki\n", encoding="utf-8")
graph = nx.Graph()
graph.add_node("skill:pack-only", label="pack-only", type="skill")
write_base_pack(
pack_dir=graph_out / "packs" / "base-export-1",
pack_id="base-export-1",
base_export_id="test-export",
config_hash="config-1",
model_id="model-1",
graph=graph,
)
(graph_out / "graph-delta.json").write_text(
json.dumps({"export_id": "test-export", "nodes": [], "edges": []}),
encoding="utf-8",
)
(graph_out / "communities.json").write_text(
json.dumps({"export_id": "test-export", "total_communities": 0}),
encoding="utf-8",
)
(graph_out / "graph-report.md").write_text(
"# Graph Report\n\n> Export ID: test-export\n",
encoding="utf-8",
)
(graph_out / "graph-export-manifest.json").write_text(
json.dumps({
"version": 1,
"export_id": "test-export",
"artifacts": {
"graph": "graph.json",
"delta": "graph-delta.json",
"communities": "communities.json",
"report": "graph-report.md",
},
}),
encoding="utf-8",
)
_write_dashboard_index(graph_out / "dashboard-neighborhoods.sqlite3")
external = wiki / "external-catalogs" / "skills-sh"
external.mkdir(parents=True)
(external / "catalog.json").write_text("{}", encoding="utf-8")
ci._validate_graph_install_tree(wiki)
def test_runtime_graph_install_extracts_and_validates_wiki_packs(
tmp_path: Path,
) -> None:
archive = _write_graph_archive(tmp_path, include_wiki_pack=True)
wiki = tmp_path / "installed-wiki"
ci._extract_graph_archive(archive, wiki, install_mode="runtime")
assert (wiki / "wiki-packs" / "base-test-export" / "wiki-pack-manifest.json").is_file()
assert not (wiki / "entities" / "skills" / "current.md").exists()
ci._validate_graph_install_tree(wiki)
assert ci._graph_full_install_complete(wiki) is True
def test_runtime_graph_install_extracts_and_validates_graph_packs(
tmp_path: Path,
) -> None:
archive = _write_graph_archive(tmp_path, include_graph_pack=True)
wiki = tmp_path / "installed-wiki"
ci._extract_graph_archive(archive, wiki, install_mode="runtime")
assert (
wiki / "graphify-out" / "packs" / "base-test-export" / "graph-pack-manifest.json"
).is_file()
ci._validate_graph_install_tree(wiki)
def test_build_graph_refreshes_operational_graph_store(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
archive = _write_graph_archive(tmp_path, include_graph_pack=True)
claude = tmp_path / "home"
monkeypatch.setattr(
ci,
"_find_local_graph_archive",
lambda _install_mode="runtime": archive,
)
monkeypatch.setattr(ci, "_verify_local_graph_archive", lambda *_a, **_k: None)
monkeypatch.setattr(ci, "_install_graph_entity_overlay", lambda *_a, **_k: None)
assert ci.build_graph(claude) == 0
graph_dir = claude / "skill-wiki" / "graphify-out"
store = graph_dir / "graph-store.sqlite3"
assert validate_graph_store(store, graph_dir)["ok"] is True
store.unlink()
assert ci.build_graph(claude) == 0
assert validate_graph_store(store, graph_dir)["ok"] is True
def test_graph_install_rejects_mismatched_graph_pack_export_id(
tmp_path: Path,
) -> None:
archive = _write_graph_archive(
tmp_path,
include_graph_pack=True,
graph_pack_export_id="wrong-export",
)
with pytest.raises(ValueError, match="graphify-out/packs export_id mismatch"):
ci._extract_graph_archive(archive, tmp_path / "installed-wiki", install_mode="runtime")
def test_graph_install_rejects_mismatched_wiki_pack_export_id(
tmp_path: Path,
) -> None:
archive = _write_graph_archive(
tmp_path,
include_wiki_pack=True,
wiki_pack_export_id="wrong-export",
)
with pytest.raises(ValueError, match="wiki-packs export_id mismatch"):
ci._extract_graph_archive(archive, tmp_path / "installed-wiki", install_mode="runtime")
def test_runtime_graph_install_without_full_entities_is_not_full_install(
tmp_path: Path,
) -> None:
archive = _write_graph_archive(tmp_path)
wiki = tmp_path / "installed-wiki"
ci._extract_graph_archive(archive, wiki, install_mode="runtime")
assert ci._graph_install_complete(wiki) is True
assert ci._graph_full_install_complete(wiki) is False
def test_full_graph_install_uses_system_tar_after_validation(
tmp_path: Path,
monkeypatch,
) -> None:
archive = _write_graph_archive(tmp_path)
wiki = tmp_path / "installed-wiki"
calls: list[list[str]] = []
def fake_run(cmd: list[str], **_kwargs: object) -> SimpleNamespace:
calls.append(list(cmd))
target = Path(cmd[cmd.index("-C") + 1])
with tarfile.open(archive, "r:gz") as tf:
tf.extractall(target)
return SimpleNamespace(returncode=0, stdout="", stderr="")
monkeypatch.setattr(ci.shutil, "which", lambda _name: "tar")
monkeypatch.setattr(ci.subprocess, "run", fake_run)
ci._extract_graph_archive(archive, wiki, install_mode="full")
assert len(calls) == 1
assert calls[0][:3] == ["tar", "-xzf", str(archive)]
assert calls[0][3] == "-C"
assert Path(calls[0][4]).name.startswith(".installed-wiki-stage-")
assert ci._graph_full_install_complete(wiki) is True
assert (wiki / "entities" / "skills" / "current.md").is_file()
def test_full_graph_install_prefers_wiki_packs_over_expanded_entities(
tmp_path: Path,
monkeypatch,
) -> None:
archive = _write_graph_archive(tmp_path, include_wiki_pack=True)
wiki = tmp_path / "installed-wiki"
def fail_if_system_tar_is_used(*_args: object, **_kwargs: object) -> SimpleNamespace:
pytest.fail("packed full install should use filtered extraction")
monkeypatch.setattr(ci.shutil, "which", lambda _name: "tar")
monkeypatch.setattr(ci.subprocess, "run", fail_if_system_tar_is_used)
ci._extract_graph_archive(archive, wiki, install_mode="full")
assert (wiki / "wiki-packs" / "base-test-export" / "wiki-pack-manifest.json").is_file()
assert not (wiki / "entities" / "skills" / "current.md").exists()
assert ci._graph_full_install_complete(wiki) is True
def test_graph_install_force_prunes_stale_generated_files(
tmp_path: Path,
monkeypatch,
capsys,
) -> None:
archive = _write_graph_archive(tmp_path)
claude = tmp_path / "home"
stale = claude / "skill-wiki" / "entities" / "skills" / "stale.md"
stale.parent.mkdir(parents=True)
stale.write_text("# Stale\n", encoding="utf-8")
monkeypatch.setattr(ci, "_claude_dir", lambda: claude)
monkeypatch.setattr(ci, "seed_toolboxes", lambda force=False: 0)
monkeypatch.setattr(
ci,
"_find_local_graph_archive",
lambda _install_mode="runtime": archive,
)
monkeypatch.setattr(ci, "_verify_local_graph_archive", lambda *_a, **_k: None)
assert ci.main([
"--graph",
"--graph-install-mode", "full",
"--force",
"--model-mode", "skip",
]) == 0
out = capsys.readouterr().out
assert "full graph install expands the markdown LLM-wiki" in out
assert "runtime mode is enough for recommendations" in out
assert not stale.exists()
assert (claude / "skill-wiki" / "entities" / "skills" / "current.md").is_file()
def test_graph_install_rejects_path_traversal_archive(
tmp_path: Path,
monkeypatch,
) -> None:
archive = tmp_path / "malicious-wiki-graph.tar.gz"
payload = b"owned"
with tarfile.open(archive, "w:gz") as tf:
info = tarfile.TarInfo("../evil.txt")
info.size = len(payload)
tf.addfile(info, io.BytesIO(payload))
claude = tmp_path / "home"
monkeypatch.setattr(
ci,
"_find_local_graph_archive",
lambda _install_mode="runtime": archive,
)
monkeypatch.setattr(ci, "_verify_local_graph_archive", lambda *_a, **_k: None)
assert ci.build_graph(claude) == 1
assert not (tmp_path / "evil.txt").exists()
assert not (claude / "evil.txt").exists()
def test_main_with_requested_hook_failure_exits_nonzero(
tmp_path: Path, monkeypatch
) -> None:
monkeypatch.setattr(ci, "_claude_dir", lambda: tmp_path)
class _FakeResult:
def __init__(self, returncode: int) -> None:
self.returncode = returncode
self.stdout = ""
self.stderr = ""
def fake_run(cmd, **kwargs):
if "ctx.adapters.claude_code.inject_hooks" in cmd:
return _FakeResult(7)
return _FakeResult(0)
monkeypatch.setattr(ci.subprocess, "run", fake_run)
assert ci.main(["--hooks"]) == 7
def test_main_custom_model_writes_profile_and_recommends_harness(
tmp_path: Path,
monkeypatch,
capsys,
) -> None:
monkeypatch.setattr(ci, "_claude_dir", lambda: tmp_path)
monkeypatch.setattr(ci, "seed_toolboxes", lambda force=False: 0)
recommendation_calls: list[dict[str, object]] = []
def fake_recommend(
goal: str,
top_k: int = 5,
model_provider: str | None = None,
model: str | None = None,
) -> list[dict[str, object]]:
recommendation_calls.append({
"goal": goal,
"top_k": top_k,
"model_provider": model_provider,
"model": model,
})
return [{"name": "text-to-cad", "type": "harness", "score": 0.8}]
monkeypatch.setattr(
ci,
"recommend_harnesses",
fake_recommend,
)
rc = ci.main([
"--model-mode", "custom",
"--model", "openai/gpt-5.5",
"--goal", "turn text prompts into CAD",
])
assert rc == 0
profile = json.loads((tmp_path / "ctx-model-profile.json").read_text())
assert profile["mode"] == "custom"
assert profile["provider"] == "openai"
assert profile["model"] == "openai/gpt-5.5"
assert profile["api_key_env"] == "OPENAI_API_KEY"
assert recommendation_calls[0]["model_provider"] == "openai"
assert recommendation_calls[0]["model"] == "openai/gpt-5.5"
assert "text-to-cad" in capsys.readouterr().out
def test_main_custom_model_records_structured_harness_requirements(
tmp_path: Path,
monkeypatch,
) -> None:
monkeypatch.setattr(ci, "_claude_dir", lambda: tmp_path)
monkeypatch.setattr(ci, "seed_toolboxes", lambda force=False: 0)
recommendation_calls: list[dict[str, object]] = []
def fake_recommend(
goal: str,
top_k: int = 5,
model_provider: str | None = None,
model: str | None = None,
) -> list[dict[str, object]]:
recommendation_calls.append({
"goal": goal,
"top_k": top_k,
"model_provider": model_provider,
"model": model,
})
return []
monkeypatch.setattr(ci, "recommend_harnesses", fake_recommend)
rc = ci.main([
"--model-mode", "custom",
"--model", "openai/gpt-5.5",
"--goal", "build a code agent",
"--harness-runtime", "windows python",
"--harness-autonomy", "supervised",
"--harness-tools", "filesystem shell browser",
"--harness-verify", "pytest ruff",
"--harness-privacy", "private repo no secrets",
"--harness-attach-mode", "mcp",
])
assert rc == 0
profile = json.loads((tmp_path / "ctx-model-profile.json").read_text())
assert profile["harness_requirements"] == {
"runtime": "windows python",
"autonomy": "supervised",
"tools": "filesystem shell browser",
"verification": "pytest ruff",
"privacy": "private repo no secrets",
"attach_mode": "mcp",
}
query = str(recommendation_calls[0]["goal"])
assert "windows python" in query
assert "filesystem shell browser" in query
assert "pytest ruff" in query
assert "private repo no secrets" in query
assert "mcp" in query
def test_main_custom_model_no_fit_points_to_harness_plan(
tmp_path: Path,
monkeypatch,
capsys,
) -> None:
monkeypatch.setattr(ci, "_claude_dir", lambda: tmp_path)
monkeypatch.setattr(ci, "seed_toolboxes", lambda force=False: 0)
monkeypatch.setattr(ci, "recommend_harnesses", lambda *args, **kwargs: [])
rc = ci.main([
"--model-mode", "custom",
"--model", "ollama/llama3.1",
"--model-provider", "ollama",
"--goal", "private local CAD workflow",
"--harness-runtime", "linux server",
"--harness-tools", "filesystem shell",
"--harness-verify", "pytest",
"--harness-privacy", "offline source code",
"--harness-attach-mode", "mcp",
])
assert rc == 0
output = capsys.readouterr().out
assert "no harness recommendations matched yet" in output
assert "ctx-harness-install --recommend" in output
assert "--model-provider \"ollama\"" in output
assert "--harness-runtime \"linux server\"" in output
assert "--harness-tools \"filesystem shell\"" in output
assert "--harness-verify \"pytest\"" in output
assert "--harness-privacy \"offline source code\"" in output
assert "--harness-attach-mode \"mcp\"" in output
assert "--plan-on-no-fit" in output
def test_recommend_harnesses_uses_wiki_frontmatter_for_fit(
tmp_path: Path,
monkeypatch,
) -> None:
wiki = tmp_path / "wiki"
page = wiki / "entities" / "harnesses" / "text-to-cad.md"
page.parent.mkdir(parents=True)
page.write_text(
"""---
title: Text to CAD
type: harness
tags:
- cad
runtimes:
- python
model_providers:
- openai
capabilities:
- Generate CAD artifacts from natural language prompts
with OpenSCAD and mesh validation
repo_url: https://github.com/earthtojake/text-to-cad
---
# Text to CAD
""",
encoding="utf-8",
)
graph = nx.Graph()
graph.add_node(
"harness:text-to-cad",
label="text-to-cad",
type="harness",
tags=["cad"],
)
monkeypatch.setattr(ci, "_load_recommendation_graph", lambda: graph)
import ctx_config
monkeypatch.setattr(
ctx_config,
"cfg",
SimpleNamespace(
wiki_dir=wiki,
claude_dir=tmp_path / ".claude",
recommendation_top_k=5,
harness_recommendation_min_fit_score=0.85,
),
)
results = ci.recommend_harnesses(
"turn text prompts into CAD openscad openai gpt-5 harness",
model_provider="openai",
model="openai/gpt-5.5",
)
assert results
assert results[0]["name"] == "text-to-cad"
assert results[0]["fit_score"] >= 0.85
assert "openai" in results[0]["fit_signals"]
assert "gpt-5" not in results[0]["fit_signals"]
assert "gpt-5" not in results[0]["missing_signals"]
assert "openscad" in results[0]["fit_signals"]
def test_load_recommendation_graph_uses_configured_wiki_dir(
tmp_path: Path,
monkeypatch,
) -> None:
wiki = tmp_path / "custom-wiki"
out = wiki / "graphify-out"
out.mkdir(parents=True)
graph = nx.Graph()
graph.add_node("harness:custom", label="custom", type="harness")
data = nx.node_link_data(graph)
(out / "graph.json").write_text(json.dumps(data), encoding="utf-8")
import ctx_config
monkeypatch.setattr(ctx_config, "cfg", SimpleNamespace(wiki_dir=wiki))
loaded = ci._load_recommendation_graph()
assert "harness:custom" in loaded
def test_recommend_harnesses_surfaces_reliability_rubric(
tmp_path: Path,
monkeypatch,
) -> None:
wiki = tmp_path / "wiki"
harness_dir = wiki / "entities" / "harnesses"
harness_dir.mkdir(parents=True)
(harness_dir / "reliable-agent.md").write_text(
"""---
title: Reliable Agent
type: harness
tags:
- agents
model_providers:
- openai
capabilities:
- Persistent project context and task state
- Permission limits, sandbox rules, and policy checks
- Automated tests, evals, retry loops, and validation gates
verify_commands:
- pytest
repo_url: https://example.test/reliable-agent
---
# Reliable Agent
""",
encoding="utf-8",
)
graph = nx.Graph()
graph.add_node(
"harness:reliable-agent",
label="reliable-agent",
type="harness",
tags=["agents"],
)
monkeypatch.setattr(ci, "_load_recommendation_graph", lambda: graph)
import ctx_config
monkeypatch.setattr(
ctx_config,
"cfg",
SimpleNamespace(
wiki_dir=wiki,
claude_dir=tmp_path / ".claude",
recommendation_top_k=5,
harness_recommendation_min_fit_score=0.20,
harness_reliability_weights={
"context": 0.34,
"constraints": 0.33,
"convergence": 0.33,
},
),
)
results = ci.recommend_harnesses(
"openai agent workflow with tests and sandbox",
model_provider="openai",
model="openai/gpt-5.5",
)
assert results
recommendation = results[0]
assert recommendation["name"] == "reliable-agent"
assert recommendation["reliability_score"] >= 0.90
assert set(recommendation["reliability_dimensions"]) == {
"context",
"constraints",
"convergence",
}
assert recommendation["reliability_dimensions"]["context"]["matched_terms"]
assert recommendation["reliability_dimensions"]["constraints"]["matched_terms"]
assert recommendation["reliability_dimensions"]["convergence"]["matched_terms"]
assert "context" in recommendation["reliability_reason"]
assert "constraints" in recommendation["reliability_reason"]
assert "convergence" in recommendation["reliability_reason"]
def test_recommend_harnesses_prefers_reliable_harness_when_fit_ties(
tmp_path: Path,
monkeypatch,
) -> None:
wiki = tmp_path / "wiki"
harness_dir = wiki / "entities" / "harnesses"
harness_dir.mkdir(parents=True)
(harness_dir / "thin-agent.md").write_text(
"""---
title: Thin Agent
type: harness
tags:
- agents
model_providers:
- openai
capabilities:
- Agent workflow orchestration
repo_url: https://example.test/thin-agent
---
# Thin Agent
""",
encoding="utf-8",
)
(harness_dir / "reliable-agent.md").write_text(
"""---
title: Reliable Agent
type: harness
tags:
- agents
model_providers:
- openai
capabilities:
- Agent workflow orchestration
- Persistent context state and durable task documents
- Permission limits, sandbox boundaries, and approval policies
- Automated tests, evals, validation gates, and retry loops
verify_commands:
- pytest
repo_url: https://example.test/reliable-agent
---
# Reliable Agent
""",
encoding="utf-8",
)
graph = nx.Graph()
for slug in ("thin-agent", "reliable-agent"):
graph.add_node(
f"harness:{slug}",
label=slug,
type="harness",
tags=["agents"],
)
monkeypatch.setattr(ci, "_load_recommendation_graph", lambda: graph)
import ctx_config
monkeypatch.setattr(
ctx_config,
"cfg",
SimpleNamespace(
wiki_dir=wiki,
claude_dir=tmp_path / ".claude",
recommendation_top_k=5,
harness_recommendation_min_fit_score=0.20,
harness_reliability_weights={
"context": 0.34,
"constraints": 0.33,
"convergence": 0.33,
},
),
)
results = ci.recommend_harnesses(
"openai agent workflow",
model_provider="openai",
model="openai/gpt-5.5",
)
assert [row["name"] for row in results[:2]] == [
"reliable-agent",
"thin-agent",
]
assert results[0]["fit_score"] == results[1]["fit_score"]
assert results[0]["reliability_score"] > results[1]["reliability_score"]
def test_recommend_harnesses_avoids_semantic_model_load_by_default(
tmp_path: Path,
monkeypatch,
) -> None:
graph = nx.Graph()
graph.add_node("harness:langgraph", label="langgraph", type="harness")
monkeypatch.setattr(ci, "_load_harness_recommendation_graph", lambda: graph)
monkeypatch.setattr(ci, "_harness_supports_provider", lambda *args, **kwargs: True)
monkeypatch.setattr(ci, "_installed_harness_slugs", lambda _path: set())
monkeypatch.setattr(
ci,
"_annotate_harness_fit",
lambda *_args, **_kwargs: {"fit_score": 0.99, "fit_signals": ["agent"]},
)
import ctx_config
monkeypatch.setattr(
ctx_config,
"cfg",
SimpleNamespace(
claude_dir=tmp_path / ".claude",
recommendation_top_k=5,
harness_recommendation_min_fit_score=0.85,
),
)
calls: dict[str, object] = {}
def fake_recommend_by_tags(*_args, **kwargs):
calls.update(kwargs)
return [{"name": "langgraph", "type": "harness", "score": 1.0}]
monkeypatch.setitem(
sys.modules,
"ctx.core.resolve.recommendations",
type(
"FakeRecommendModule",
(),
{
"query_to_tags": staticmethod(lambda _query: ["agent"]),
"recommend_by_tags": staticmethod(fake_recommend_by_tags),
},
),
)
results = ci.recommend_harnesses(
"build an agent workflow",
model_provider="openai",
model="openai/gpt-5.5",
)
assert results[0]["name"] == "langgraph"
assert calls["query"] == "build an agent workflow"
assert calls["entity_types"] == ("harness",)
assert calls["use_semantic_query"] is False
def test_main_custom_model_requires_model(tmp_path: Path, monkeypatch) -> None:
monkeypatch.setattr(ci, "_claude_dir", lambda: tmp_path)
monkeypatch.setattr(ci, "seed_toolboxes", lambda force=False: 0)
assert ci.main(["--model-mode", "custom"]) == 1
def test_validate_model_flag_invokes_connection_check(
tmp_path: Path,
monkeypatch,
) -> None:
monkeypatch.setattr(ci, "_claude_dir", lambda: tmp_path)
monkeypatch.setattr(ci, "seed_toolboxes", lambda force=False: 0)
monkeypatch.setattr(
ci,
"recommend_harnesses",
lambda goal, top_k=5, model_provider=None, model=None: [],
)
calls: list[dict] = []
def fake_validate(**kwargs):
calls.append(kwargs)
return 0
monkeypatch.setattr(ci, "validate_model_connection", fake_validate)
rc = ci.main([
"--model-mode", "custom",
"--model", "ollama/llama3.1",
"--validate-model",
])
assert rc == 0
assert calls == [{
"model": "ollama/llama3.1",
"api_key_env": None,
"base_url": None,
}]
|