File size: 26,227 Bytes
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1bbe168
 
 
3fbbaab
 
 
 
1bbe168
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0bcc198
 
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0bcc198
 
 
 
 
 
 
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1bbe168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fbbaab
 
 
 
 
 
 
 
1bbe168
3fbbaab
 
 
 
 
1bbe168
 
 
3fbbaab
 
 
 
 
 
 
 
1bbe168
 
 
 
 
 
 
 
 
 
 
 
 
3fbbaab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1bbe168
 
 
 
 
 
3fbbaab
 
1bbe168
 
3fbbaab
 
 
 
 
 
 
 
 
1bbe168
 
 
 
 
3fbbaab
 
 
 
1bbe168
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
"""

test_alive_loop_e2e.py -- A-Z end-to-end simulation of the alive skill system.



This is the canary. If any handoff in the alive loop regresses, this

test fails. It chains every module the loop depends on in a single

pytest function, against a synthetic wiki fixture, with zero network

and zero touches to ~/.claude/.



The chain mirrors the product-intent user journey:



    1. User writes code (simulated: three tool-use invocations carrying

       unmatched signals).

    2. context_monitor accumulates those signals across invocations

       and, once the cumulative threshold fires, writes

       pending-skills.json with a ranked graph-derived bundle. This is

       the regression-pinned behaviour of code-reviewer BLOCKER fixed

       in commit 6569387 — every individual invocation below the

       threshold does NOT write, only the cumulative state does.

    3. bundle_orchestrator reads pending-skills.json and renders a

       user-facing message with the top-K bundle categorised by

       Skills / Agents / MCPs (contract pinned in test_bundle_

       orchestrator.py; here we only assert the shape is sane).

    4. User approves a skill → ctx-skill-install copies the body into

       the live skills dir, bumps entity status to `installed`, and adds

       a manifest load entry. The A-Z test also covers agent install and a

       mocked ctx-mcp-install registration through the Claude MCP wrapper.

    5. The manifest reconciles install entries across entity types

       via the install_utils tuple-based dedup (pinned in P1.3

       regression suite).

    6. User decides the skill is stale → skill_unload.unload_skill

       drops the load entry and adds an unload entry for the typed entity.



When this test passes, the install/unload leg of the alive loop

works against a representative corpus. When it fails, something

between the modules broke — CI should gate a PR landing on this

test going green.



NOT covered here (by design):

  - The lifecycle auto-demote threshold firing (tested in

    test_ctx_lifecycle.py); would require a time-mock setup that's

    cleaner in its own test.

  - Real sentence-transformer embeddings. We use a tiny synthetic

    graph and don't exercise the embedding cache.

"""

from __future__ import annotations

import json
import sys
from pathlib import Path

import networkx as nx
import pytest

sys.path.insert(0, str(Path(__file__).parents[1]))


# ────────────────────────────────────────────────────────────────────
# Synthetic wiki fixture — a minimal corpus with all three entity types
# ────────────────────────────────────────────────────────────────────


def _write_skill_entity(wiki_dir: Path, slug: str, *, tags: list[str]) -> Path:
    entity = wiki_dir / "entities" / "skills" / f"{slug}.md"
    entity.parent.mkdir(parents=True, exist_ok=True)
    tags_block = "\n".join(f"  - {t}" for t in tags)
    entity.write_text(
        f"---\n"
        f"title: {slug}\n"
        f"type: skill\n"
        f"status: cataloged\n"
        f"tags:\n{tags_block}\n"
        f"---\n"
        f"# {slug}\n\n"
        f"Synthetic skill body for E2E test.\n",
        encoding="utf-8",
    )
    return entity


def _write_skill_body(wiki_dir: Path, slug: str, body: str) -> Path:
    """Drop `converted/<slug>/SKILL.md` so ctx-skill-install finds content."""
    path = wiki_dir / "converted" / slug / "SKILL.md"
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(body, encoding="utf-8")
    return path


def _write_agent_entity(wiki_dir: Path, slug: str, *, tags: list[str]) -> Path:
    entity = wiki_dir / "entities" / "agents" / f"{slug}.md"
    entity.parent.mkdir(parents=True, exist_ok=True)
    tags_block = "\n".join(f"  - {t}" for t in tags)
    entity.write_text(
        f"---\n"
        f"title: {slug}\n"
        f"type: agent\n"
        f"status: cataloged\n"
        f"tags:\n{tags_block}\n"
        f"---\n"
        f"# {slug}\n\nSynthetic agent card for E2E test.\n",
        encoding="utf-8",
    )
    return entity


def _write_agent_body(wiki_dir: Path, slug: str, body: str) -> Path:
    """Drop `converted-agents/<slug>.md` so ctx-agent-install finds content."""
    path = wiki_dir / "converted-agents" / f"{slug}.md"
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(body, encoding="utf-8")
    return path


def _write_mcp_entity(wiki_dir: Path, slug: str, *, tags: list[str]) -> Path:
    shard = slug[0] if slug and slug[0].isalpha() else "0-9"
    entity = wiki_dir / "entities" / "mcp-servers" / shard / f"{slug}.md"
    entity.parent.mkdir(parents=True, exist_ok=True)
    tags_block = "\n".join(f"  - {t}" for t in tags)
    entity.write_text(
        f"---\n"
        f"type: mcp-server\n"
        f"slug: {slug}\n"
        f"name: {slug}\n"
        f"status: cataloged\n"
        f"tags:\n{tags_block}\n"
        f"---\n"
        f"# {slug}\n\nSynthetic MCP card for E2E test.\n",
        encoding="utf-8",
    )
    return entity


def _write_graph_json(wiki_dir: Path, nodes: list[dict], edges: list[dict]) -> Path:
    """Build a graph.json that context_monitor.graph_suggest can walk."""
    G = nx.Graph()
    G.graph["semantic_build_floor"] = 0.5
    G.graph["external_catalog_nodes"] = {"skills.sh": 1}
    G.graph["source_catalog_nodes"] = {"skills.sh": 1}
    for n in nodes:
        G.add_node(n["id"], **{k: v for k, v in n.items() if k != "id"})
    for e in edges:
        G.add_edge(e["source"], e["target"], **{k: v for k, v in e.items() if k not in {"source", "target"}})
    data = nx.node_link_data(G, edges="edges")
    path = wiki_dir / "graphify-out" / "graph.json"
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(json.dumps(data), encoding="utf-8")
    return path


@pytest.fixture()
def e2e_world(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> dict[str, Path]:
    """A hermetic synthetic world for the alive-loop E2E test.



    Returns a dict of paths the test uses to drive each module.

    Monkeypatches every module-level path constant so no real

    ~/.claude/ content is touched.

    """
    claude_dir = tmp_path / "claude"
    wiki_dir = claude_dir / "skill-wiki"
    skills_dir = claude_dir / "skills"
    agents_dir = claude_dir / "agents"
    for d in (claude_dir, wiki_dir, skills_dir, agents_dir):
        d.mkdir(parents=True)

    manifest = claude_dir / "skill-manifest.json"
    intent_log = claude_dir / "intent-log.jsonl"
    pending_skills = claude_dir / "pending-skills.json"

    # ── Synthetic corpus: 2 skills (python + security), 2 agents, 2 MCPs ──
    # The "python" signal is the trigger we'll feed into the monitor.
    _write_skill_entity(wiki_dir, "python-patterns", tags=["python", "patterns"])
    _write_skill_body(
        wiki_dir, "python-patterns",
        "# python-patterns\n\nBest practices for idiomatic Python.\n",
    )
    _write_skill_entity(wiki_dir, "security-basics", tags=["security"])
    _write_skill_body(
        wiki_dir, "security-basics",
        "# security-basics\n\nSecurity fundamentals.\n",
    )

    _write_agent_entity(wiki_dir, "code-reviewer", tags=["python", "review"])
    _write_agent_body(
        wiki_dir, "code-reviewer",
        "---\nname: code-reviewer\ndescription: review code\n---\n\n"
        "Review code for quality and security issues.\n",
    )
    _write_agent_entity(wiki_dir, "devops-engineer", tags=["devops", "docker"])
    _write_agent_body(
        wiki_dir, "devops-engineer",
        "---\nname: devops-engineer\ndescription: devops\n---\n\n"
        "DevOps tooling and CI/CD.\n",
    )

    _write_mcp_entity(wiki_dir, "anthropic-python-sdk", tags=["python", "sdk"])
    _write_mcp_entity(wiki_dir, "atlassian-cloud", tags=["saas"])

    # ── Synthetic graph: link every python-tagged entity via an explicit edge ──
    _write_graph_json(
        wiki_dir,
        nodes=[
            {"id": "skill:python-patterns", "label": "python-patterns",
             "type": "skill", "tags": ["python", "patterns"]},
            {"id": "skill:security-basics", "label": "security-basics",
             "type": "skill", "tags": ["security"]},
            {"id": "agent:code-reviewer", "label": "code-reviewer",
             "type": "agent", "tags": ["python", "review"]},
            {"id": "agent:devops-engineer", "label": "devops-engineer",
             "type": "agent", "tags": ["devops", "docker"]},
            {"id": "mcp-server:anthropic-python-sdk",
             "label": "anthropic-python-sdk",
             "type": "mcp-server", "tags": ["python", "sdk"]},
            {"id": "mcp-server:atlassian-cloud",
             "label": "atlassian-cloud",
             "type": "mcp-server", "tags": ["saas"]},
        ],
        edges=[
            {"source": "skill:python-patterns", "target": "agent:code-reviewer",
             "weight": 1, "shared_tags": ["python"]},
            {"source": "skill:python-patterns",
             "target": "mcp-server:anthropic-python-sdk",
             "weight": 1, "shared_tags": ["python"]},
            {"source": "agent:code-reviewer",
             "target": "mcp-server:anthropic-python-sdk",
             "weight": 1, "shared_tags": ["python"]},
        ],
    )

    # ── Monkeypatch every module's path constants ─────────────────────────
    from ctx.adapters.claude_code.hooks import context_monitor as _cm
    monkeypatch.setattr(_cm, "CLAUDE_DIR", claude_dir)
    monkeypatch.setattr(_cm, "INTENT_LOG", intent_log)
    monkeypatch.setattr(_cm, "PENDING_SKILLS", pending_skills)
    monkeypatch.setattr(_cm, "MANIFEST_PATH", manifest)

    from ctx.adapters.claude_code.hooks import bundle_orchestrator as _bo
    monkeypatch.setattr(_bo, "CLAUDE_DIR", claude_dir)
    monkeypatch.setattr(_bo, "PENDING_SKILLS", pending_skills)
    monkeypatch.setattr(_bo, "PENDING_UNLOAD", claude_dir / "pending-unload.json")
    monkeypatch.setattr(_bo, "SHOWN_FLAG", claude_dir / ".bundle-shown")

    from ctx.adapters.claude_code.install import install_utils as _iu
    monkeypatch.setattr(_iu, "MANIFEST_PATH", manifest)

    import ctx_config
    monkeypatch.setattr(
        ctx_config.cfg,
        "graph_semantic_cache_dir",
        tmp_path / "semantic-cache",
    )

    return {
        "claude": claude_dir,
        "wiki": wiki_dir,
        "skills": skills_dir,
        "agents": agents_dir,
        "manifest": manifest,
        "intent_log": intent_log,
        "pending_skills": pending_skills,
    }


# ────────────────────────────────────────────────────────────────────
# The A-Z test
# ────────────────────────────────────────────────────────────────────


class TestAliveLoopE2E:
    """One test per chain link + one test for the full journey."""

    def test_signals_below_threshold_do_not_fire(

        self, e2e_world, monkeypatch,

    ):
        """Single invocation with one unmatched signal stays below the

        default threshold=3 — pending-skills.json must NOT be written.



        Pins the regression from code-reviewer BLOCKER (commit 6569387):

        pre-fix, len(unmatched)>=THRESHOLD never fired; post-fix, the

        cumulative check also doesn't fire until enough signals have

        accumulated."""
        from ctx.adapters.claude_code.hooks import context_monitor as _cm

        monkeypatch.setattr(sys, "argv", [
            "context_monitor.py", "--tool", "Read",
            "--input", json.dumps({"file_path": "App.tsx"}),
        ])
        _cm.main()

        assert not e2e_world["pending_skills"].exists(), (
            "pending-skills.json fired after 1 signal (threshold=3)"
        )

    def test_cumulative_threshold_fires_pending_write(

        self, e2e_world, monkeypatch,

    ):
        """Cumulative signals across multiple invocations trigger the

        pending-skills write once the threshold is reached. The alive

        loop's suggestion arm now fires in production — code-reviewer

        BLOCKER verified in-situ."""
        from ctx.adapters.claude_code.hooks import context_monitor as _cm

        # Mix Read + Bash to exercise extension-based AND command-based
        # signal extraction. Each invocation surfaces a distinct signal
        # set; cumulative union crosses threshold=3.
        invocations = [
            ("Read", {"file_path": "App.tsx"}),           # -> react
            ("Read", {"file_path": "Dockerfile"}),        # -> docker
            ("Bash", {"command": "pip install fastapi"}), # -> fastapi, python
        ]
        for tool, tool_input in invocations:
            monkeypatch.setattr(sys, "argv", [
                "context_monitor.py", "--tool", tool,
                "--input", json.dumps(tool_input),
            ])
            _cm.main()

        assert e2e_world["pending_skills"].exists()
        payload = json.loads(e2e_world["pending_skills"].read_text(encoding="utf-8"))
        # Cumulative union includes ALL signals seen today, not just
        # the current invocation's.
        assert len(payload.get("unmatched_signals", [])) >= 3
        # Graph-derived bundle populated (may be empty for signals that
        # didn't match any graph node, but the key must exist).
        assert "graph_suggestions" in payload

    def test_graph_suggest_returns_cross_type_bundle(

        self, e2e_world, monkeypatch,

    ):
        """With a python signal and a graph containing a python skill +

        python agent + python MCP, graph_suggest returns entries

        spanning all three types. Pins the cross-type contract the

        bundle orchestrator depends on."""
        from ctx.adapters.claude_code.hooks import context_monitor as _cm

        sugs = _cm.graph_suggest(["python"])
        assert len(sugs) > 0
        types = {s["type"] for s in sugs}
        # At least two of the three types should be ranked in (the
        # python-tagged skill AND agent AND MCP all share the tag).
        assert len(types & {"skill", "agent", "mcp-server"}) >= 2, (
            f"expected multi-type bundle, got types={types}"
        )

    def test_bundle_orchestrator_renders_categorised_message(

        self, e2e_world, capsys, monkeypatch,

    ):
        """Fire the cumulative threshold, then run the bundle orchestrator

        hook. Output must contain the three-type categorised layout and

        be valid Claude-Code-hook JSON."""
        from ctx.adapters.claude_code.hooks import context_monitor as _cm
        from ctx.adapters.claude_code.hooks import bundle_orchestrator as _bo

        # Feed signals that include a python-adjacent one so
        # graph_suggest hits our python-tagged nodes.
        invocations = [
            ("Read", {"file_path": "App.tsx"}),
            ("Read", {"file_path": "Dockerfile"}),
            ("Bash", {"command": "pip install fastapi"}),
        ]
        for tool, tool_input in invocations:
            monkeypatch.setattr(sys, "argv", [
                "context_monitor.py", "--tool", tool,
                "--input", json.dumps(tool_input),
            ])
            _cm.main()

        # Sanity: pending-skills.json was written.
        assert e2e_world["pending_skills"].exists()

        _bo.main()
        out = capsys.readouterr().out.strip()
        assert out, "bundle_orchestrator emitted no output despite pending file"
        envelope = json.loads(out)
        assert envelope["hookSpecificOutput"]["hookEventName"] == "PostToolUse"
        msg = envelope["hookSpecificOutput"]["additionalContext"]
        # The exact set of headers depends on which types the graph
        # returned; we assert at least two of three are present since
        # the python signal hits our skill + agent + MCP all.
        headers = {h for h in ("Skills:", "Agents:", "MCPs:") if h in msg}
        assert len(headers) >= 2, (
            f"expected multi-type categorisation, got headers={headers}"
        )

    def test_install_skill_copies_body_and_records_manifest(

        self, e2e_world,

    ):
        """User approves a skill from the bundle → skill_install copies

        wiki body into the live skills dir, flips entity status to

        `installed`, records (slug, 'skill') in the manifest load list."""
        from ctx.adapters.claude_code.install.skill_install import install_skill

        result = install_skill(
            "python-patterns",
            wiki_dir=e2e_world["wiki"],
            skills_dir=e2e_world["skills"],
        )

        assert result.status == "installed"
        assert (e2e_world["skills"] / "python-patterns" / "SKILL.md").is_file()

        manifest = json.loads(e2e_world["manifest"].read_text(encoding="utf-8"))
        loaded = {(e.get("skill"), e.get("entity_type")) for e in manifest["load"]}
        assert ("python-patterns", "skill") in loaded

        # Entity status bumped.
        entity_text = (
            e2e_world["wiki"] / "entities" / "skills" / "python-patterns.md"
        ).read_text(encoding="utf-8")
        assert "status: installed" in entity_text

    def test_install_agent_writes_body_and_records_manifest_distinct_from_skill(

        self, e2e_world,

    ):
        """Approving an agent with the SAME slug as an installed skill

        must not collide — the manifest dedups on (slug, entity_type)

        tuple, not slug alone. Pins the P1.3 regression."""
        from ctx.adapters.claude_code.install.skill_install import install_skill
        from ctx.adapters.claude_code.install.agent_install import install_agent

        # Install a skill and an agent sharing the slug "same-slug".
        # This fixture doesn't have one; use the distinct slugs from
        # the corpus and assert the manifest has both.
        install_skill(
            "python-patterns",
            wiki_dir=e2e_world["wiki"],
            skills_dir=e2e_world["skills"],
        )
        install_agent(
            "code-reviewer",
            wiki_dir=e2e_world["wiki"],
            agents_dir=e2e_world["agents"],
        )

        manifest = json.loads(e2e_world["manifest"].read_text(encoding="utf-8"))
        pairs = {(e.get("skill"), e.get("entity_type")) for e in manifest["load"]}
        assert ("python-patterns", "skill") in pairs
        assert ("code-reviewer", "agent") in pairs

    def test_install_and_uninstall_mcp_records_manifest(

        self, e2e_world, monkeypatch,

    ):
        """MCP approval and removal reconcile typed manifest entries."""
        from ctx.adapters.claude_code.install import mcp_install

        calls: list[list[str]] = []

        def fake_run(args: list[str]) -> tuple[int, str, str]:
            calls.append(args)
            return 0, "ok", ""

        monkeypatch.setattr(mcp_install, "_run_claude_mcp", fake_run)

        install_result = mcp_install.install_mcp(
            "anthropic-python-sdk",
            wiki_dir=e2e_world["wiki"],
            command="npx -y @anthropic-ai/sdk-mcp",
            auto=True,
        )
        assert install_result.status == "installed"
        assert calls[0] == [
            "add",
            "anthropic-python-sdk",
            "--",
            "npx",
            "-y",
            "@anthropic-ai/sdk-mcp",
        ]

        manifest = json.loads(e2e_world["manifest"].read_text(encoding="utf-8"))
        loaded = {(e.get("skill"), e.get("entity_type")) for e in manifest["load"]}
        assert ("anthropic-python-sdk", "mcp-server") in loaded

        entity = (
            e2e_world["wiki"]
            / "entities"
            / "mcp-servers"
            / "a"
            / "anthropic-python-sdk.md"
        )
        assert "status: installed" in entity.read_text(encoding="utf-8")

        uninstall_result = mcp_install.uninstall_mcp(
            "anthropic-python-sdk",
            wiki_dir=e2e_world["wiki"],
        )
        assert uninstall_result.status == "uninstalled"
        assert calls[-1] == ["remove", "anthropic-python-sdk"]

        manifest = json.loads(e2e_world["manifest"].read_text(encoding="utf-8"))
        loaded = {(e.get("skill"), e.get("entity_type")) for e in manifest["load"]}
        unloaded = {
            (e.get("skill"), e.get("entity_type")) for e in manifest["unload"]
        }
        assert ("anthropic-python-sdk", "mcp-server") not in loaded
        assert ("anthropic-python-sdk", "mcp-server") in unloaded
        assert "status: cataloged" in entity.read_text(encoding="utf-8")

    def test_full_user_journey_signals_to_install(

        self, e2e_world, capsys, monkeypatch,

    ):
        """The A-Z.



        A user who:

          1. opens Python files (three signals)

          2. sees the categorised bundle surfaced via the hook

          3. approves a skill + an agent + an MCP from the bundle



        must end the journey with:

          - pending-skills.json written with a cross-type bundle

          - skill installed at ~/.claude/skills/<slug>/

          - agent installed at ~/.claude/agents/<slug>.md

          - MCP registered through the Claude MCP wrapper

          - manifest carrying all entries keyed on (slug, type)

          - entity status fields flipped to `installed` on all cards



        If any link in the chain regresses, this test fails and CI

        blocks the PR. This is the load-bearing integration contract.

        """
        from ctx.adapters.claude_code.hooks import context_monitor as _cm
        from ctx.adapters.claude_code.hooks import bundle_orchestrator as _bo
        from ctx.adapters.claude_code.install.skill_install import install_skill
        from ctx.adapters.claude_code.install.agent_install import install_agent
        from ctx.adapters.claude_code.install import mcp_install

        mcp_calls: list[list[str]] = []

        def fake_mcp_run(args: list[str]) -> tuple[int, str, str]:
            mcp_calls.append(args)
            return 0, "ok", ""

        monkeypatch.setattr(
            mcp_install,
            "_run_claude_mcp",
            fake_mcp_run,
        )

        # ── 1. Accumulate signals ────────────────────────────────────
        # Three invocations spanning extension-based AND command-based
        # signal extraction. Crosses the default threshold=3.
        invocations = [
            ("Read", {"file_path": "App.tsx"}),
            ("Read", {"file_path": "Dockerfile"}),
            ("Bash", {"command": "pip install fastapi"}),
        ]
        for tool, tool_input in invocations:
            monkeypatch.setattr(sys, "argv", [
                "context_monitor.py", "--tool", tool,
                "--input", json.dumps(tool_input),
            ])
            _cm.main()
        assert e2e_world["pending_skills"].exists(), (
            "alive loop broken: cumulative threshold didn't fire"
        )

        # ── 2. Bundle orchestrator renders recommendation ────────────
        _bo.main()
        out = capsys.readouterr().out.strip()
        assert out, "bundle_orchestrator emitted nothing"
        msg = json.loads(out)["hookSpecificOutput"]["additionalContext"]
        assert "python-patterns" in msg or "code-reviewer" in msg, (
            f"bundle missing expected python entities, got: {msg}"
        )

        # ── 3. User approves skill + agent ───────────────────────────
        skill_result = install_skill(
            "python-patterns",
            wiki_dir=e2e_world["wiki"],
            skills_dir=e2e_world["skills"],
        )
        agent_result = install_agent(
            "code-reviewer",
            wiki_dir=e2e_world["wiki"],
            agents_dir=e2e_world["agents"],
        )
        mcp_result = mcp_install.install_mcp(
            "anthropic-python-sdk",
            wiki_dir=e2e_world["wiki"],
            command="npx -y @anthropic-ai/sdk-mcp",
            auto=True,
        )
        assert skill_result.status == "installed"
        assert agent_result.status == "installed"
        assert mcp_result.status == "installed"
        assert mcp_calls and mcp_calls[0][0] == "add"

        # ── 4. End state: filesystem + manifest + entity status ──────
        assert (e2e_world["skills"] / "python-patterns" / "SKILL.md").is_file()
        assert (e2e_world["agents"] / "code-reviewer.md").is_file()

        manifest = json.loads(e2e_world["manifest"].read_text(encoding="utf-8"))
        loaded_pairs = {
            (e.get("skill"), e.get("entity_type")) for e in manifest["load"]
        }
        assert {
            ("python-patterns", "skill"),
            ("code-reviewer", "agent"),
            ("anthropic-python-sdk", "mcp-server"),
        } <= loaded_pairs

        for entity_rel in (
            "entities/skills/python-patterns.md",
            "entities/agents/code-reviewer.md",
            "entities/mcp-servers/a/anthropic-python-sdk.md",
        ):
            text = (e2e_world["wiki"] / entity_rel).read_text(encoding="utf-8")
            assert "status: installed" in text, (
                f"status not flipped on {entity_rel}"
            )