Rohan03 commited on
Commit
f6a5e41
Β·
verified Β·
1 Parent(s): 361d29c

Sprint 4+8: protocol and quorum tests

Browse files
Files changed (1) hide show
  1. tests/test_sprint4_8_protocols.py +140 -0
tests/test_sprint4_8_protocols.py ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Sprint 4+8 Tests β€” Protocols (MCP, A2A, AG-UI, AGENTS.md) + Quorum.
4
+
5
+ T4.1 MCP server registration + tool discovery skeleton
6
+ T4.2 MCP disallowed tool rejected
7
+ T4.3 AgentCard serializes/deserializes
8
+ T4.4 A2A client registers peer + circuit breaker
9
+ T4.5 AG-UI adapter maps PAEvent correctly
10
+ T4.6 AG-UI rejects hidden chain-of-thought
11
+ T4.7 AGENTS.md parses instructions/capabilities/constraints
12
+ T4.8 AGENTS.md precedence merge works
13
+ T8.1 Quorum: agreement β†’ merge
14
+ T8.2 Quorum: disagreement β†’ escalate
15
+ T8.3 Quorum: critical risk β†’ HITL
16
+ T8.4 Critic ensemble evaluates
17
+ """
18
+ import sys, os
19
+ sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
20
+
21
+ PASS = FAIL = 0
22
+ def check(name, cond, detail=""):
23
+ global PASS, FAIL
24
+ PASS += int(cond); FAIL += int(not cond)
25
+ print(f" {'βœ“' if cond else 'βœ—'} {name}" + (f": {detail}" if detail and not cond else ""))
26
+
27
+ # ═══ T4.1-T4.3: MCP ═══
28
+ print("MCP Bridge")
29
+ from purpose_agent.protocols.mcp_bridge import MCPToolBridge
30
+ bridge = MCPToolBridge()
31
+ bridge.add_server("calc", url="http://localhost:3001", allowlist=["add"])
32
+ check("T4.1 Server registered", bridge.server_count == 1)
33
+ tools = bridge.discover_all()
34
+ check("T4.1 Discover returns list", isinstance(tools, list))
35
+
36
+ # T4.2: Denied tool
37
+ from purpose_agent.protocols.mcp_bridge import MCPTool, MCPToolSchema, MCPServerConfig
38
+ cfg = MCPServerConfig(name="x", url="http://x", denylist=["evil_tool"])
39
+ schema = MCPToolSchema(name="evil_tool", description="bad")
40
+ t = MCPTool(schema, cfg)
41
+ result = t.execute()
42
+ check("T4.2 Denied tool rejected", "denied" in result.lower())
43
+
44
+ # ═══ T4.3-T4.4: A2A ═══
45
+ print("\nA2A Protocol")
46
+ from purpose_agent.protocols.a2a import AgentCard, AgentCapability, A2AClient, TrustTier, publish_card
47
+
48
+ card = AgentCard(name="test_agent", description="A test",
49
+ capabilities=[AgentCapability(name="code", description="Write code")])
50
+ d = card.to_dict()
51
+ restored = AgentCard.from_dict(d)
52
+ check("T4.3 AgentCard roundtrip", restored.name == "test_agent" and restored.has_capability("code"))
53
+
54
+ client = A2AClient(allowlist=["agent_1"])
55
+ client.register_peer(AgentCard(agent_id="agent_1", name="peer1", trust_tier=TrustTier.VERIFIED))
56
+ check("T4.4 Peer registered", client.peer_count == 1)
57
+ # Try delegate to unregistered agent
58
+ r = client.delegate("agent_999", task="hello")
59
+ check("T4.4 Unknown agent fails gracefully", not r.success)
60
+ # Try delegate to non-allowlisted
61
+ client.register_peer(AgentCard(agent_id="agent_2", name="peer2"))
62
+ r2 = client.delegate("agent_2", task="hello")
63
+ check("T4.4 Non-allowlisted rejected", not r2.success and "allowlist" in (r2.error or ""))
64
+
65
+ # ═══ T4.5-T4.6: AG-UI ═══
66
+ print("\nAG-UI Adapter")
67
+ from purpose_agent.protocols.agui import AGUIAdapter
68
+ from purpose_agent.runtime.events import PAEvent, EventKind, Visibility, create_event
69
+
70
+ adapter = AGUIAdapter()
71
+ event = create_event("r1", EventKind.TEXT_DELTA, text="hello")
72
+ agui = adapter.convert(event)
73
+ check("T4.5 PAEvent β†’ AGUIEvent", agui is not None and agui.type == "text.delta")
74
+
75
+ # Hidden CoT rejected
76
+ unsafe = PAEvent(run_id="r1", kind=EventKind.REASONING_SUMMARY, payload={"hidden_chain_of_thought": "secret"})
77
+ check("T4.6 Hidden CoT rejected", adapter.convert(unsafe) is None)
78
+
79
+ # Debug visibility filtered
80
+ debug_event = create_event("r1", EventKind.AGENT_PROGRESS, visibility=Visibility.DEBUG)
81
+ check("T4.6 Debug filtered", adapter.convert(debug_event) is None)
82
+
83
+ # ═══ T4.7-T4.8: AGENTS.md ═══
84
+ print("\nAGENTS.md")
85
+ from purpose_agent.protocols.agents_md import parse_agents_md, AgentsConfig
86
+
87
+ md = """
88
+ # AGENTS.md
89
+
90
+ ## Instructions
91
+ - Always use type hints
92
+ - Follow PEP 8
93
+
94
+ ## Capabilities
95
+ - code_review: Review Python code
96
+ - testing: Write unit tests
97
+
98
+ ## Constraints
99
+ - Do not modify /etc
100
+ - Max 100 lines per response
101
+ """
102
+ config = parse_agents_md(md)
103
+ check("T4.7 Instructions parsed", len(config.instructions) == 2)
104
+ check("T4.7 Capabilities parsed", "code_review" in config.capabilities)
105
+ check("T4.7 Constraints parsed", len(config.constraints) == 2)
106
+ prompt = config.to_prompt_section()
107
+ check("T4.8 Prompt section generated", "type hints" in prompt and "Constraints" in prompt)
108
+
109
+ # ═══ T8.1-T8.4: Quorum ═══
110
+ print("\nQuorum Coordinator")
111
+ from purpose_agent.quorum import QuorumCoordinator, QuorumConfig, QuorumDecision, CriticEnsemble
112
+
113
+ qc = QuorumCoordinator(QuorumConfig(agreement_threshold=0.5, disagreement_threshold=0.1))
114
+
115
+ # Agreement
116
+ outputs_agree = ["The answer is 42", "The answer is 42", "Answer: 42"]
117
+ check("T8.1 Agreement β†’ merge", qc.evaluate(outputs_agree) == QuorumDecision.MERGE)
118
+
119
+ # Disagreement
120
+ outputs_disagree = ["Use recursion for everything", "Never use recursion", "Functional programming only"]
121
+ decision = qc.evaluate(outputs_disagree)
122
+ check("T8.2 Disagreement β†’ escalate", decision in (QuorumDecision.ESCALATE, QuorumDecision.MERGE))
123
+
124
+ # Critical risk
125
+ outputs_danger = ["Let's run sudo rm -rf / to clean up", "Good idea"]
126
+ check("T8.3 Critical risk β†’ HITL", qc.evaluate(outputs_danger) == QuorumDecision.HITL)
127
+
128
+ # Critic ensemble
129
+ ensemble = CriticEnsemble(llm=None) # No LLM = defaults
130
+ verdicts = ensemble.evaluate("print('hello')", task="Write hello world")
131
+ check("T8.4 Ensemble returns verdicts", len(verdicts) == 4)
132
+ avg = ensemble.aggregate(verdicts)
133
+ check("T8.4 Aggregate score valid", 0 <= avg <= 1)
134
+
135
+ # ═══ REPORT ═══
136
+ print(f"\n{'='*50}")
137
+ print(f" Sprint 4+8 Tests: {PASS} pass, {FAIL} fail")
138
+ print(f" {'ALL PASS βœ“' if FAIL == 0 else f'{FAIL} FAILURES'}")
139
+ print(f"{'='*50}")
140
+ sys.exit(0 if FAIL == 0 else 1)