File size: 31,752 Bytes
3fbbaab
 
e965652
 
 
d5d5035
e965652
21ff762
d5d5035
e965652
21ff762
3fbbaab
21ff762
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e965652
 
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e965652
d5d5035
e965652
 
21ff762
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e965652
 
 
 
 
 
 
 
 
 
 
 
 
 
d5d5035
e965652
 
 
 
 
 
 
f2361ba
 
 
 
 
 
e965652
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5d5035
f2361ba
 
 
 
 
 
 
 
d5d5035
 
e965652
 
 
 
 
 
 
 
 
 
 
 
 
 
d5d5035
e965652
 
 
 
 
 
 
 
 
 
 
d5d5035
 
 
e965652
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5d5035
 
 
e965652
 
 
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5d5035
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ae1b9f
 
 
 
 
 
d5d5035
 
 
 
 
 
 
 
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
d5d5035
3fbbaab
d5d5035
 
 
1ae1b9f
 
 
d5d5035
3fbbaab
 
 
d5d5035
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ae1b9f
 
 
 
 
d5d5035
 
 
 
 
1ae1b9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5d5035
 
 
 
 
 
 
 
 
1ae1b9f
 
 
 
 
 
 
d5d5035
1ae1b9f
 
 
 
 
 
d5d5035
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ae1b9f
 
 
 
 
d5d5035
 
 
 
 
 
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e965652
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fbbaab
 
 
 
 
 
 
 
 
 
e965652
 
 
 
 
 
3fbbaab
 
f2361ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21ff762
 
 
 
 
 
 
 
 
 
 
 
f2361ba
21ff762
f2361ba
 
 
 
 
21ff762
 
 
 
 
 
f2361ba
 
 
 
 
 
21ff762
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f09db1a
 
21ff762
 
 
1bbe168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9238de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5d09fc
21ff762
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5d09fc
21ff762
 
3fbbaab
 
 
 
 
 
 
 
 
 
 
e965652
 
 
 
 
 
 
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Tests for ctx_init — bootstrap ~/.claude/ scaffolding."""

from __future__ import annotations

import builtins
import io
import json
import sys
import tarfile
from pathlib import Path
from types import SimpleNamespace

import networkx as nx
import ctx_init as ci


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",
    }
    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) -> 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",
    )
    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")
    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 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,
        "_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_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,
    )

    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",
    )
    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_install_force_prunes_stale_generated_files(

    tmp_path: Path,

    monkeypatch,

) -> 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,
    )

    assert ci.main([
        "--graph",
        "--graph-install-mode", "full",
        "--force",
        "--model-mode", "skip",
    ]) == 0
    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,
    )

    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_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,
    }]