stellar413 commited on
Commit
ff40840
·
1 Parent(s): 6df13ef

final commits

Browse files
execute_agentic_flow.py DELETED
@@ -1,346 +0,0 @@
1
- # execute_agentic_flow.py
2
- """
3
- STRICT PHASE-GATED MULTI-AGENT EXECUTION
4
-
5
- This script executes the complete agentic flow with hostile verification.
6
- NO SIMULATION. Real agents only.
7
- """
8
- import json
9
- import sys
10
- import os
11
- from datetime import datetime, timezone
12
-
13
- # Load environment variables from .env
14
- from dotenv import load_dotenv
15
- load_dotenv()
16
-
17
- # Add parent to path
18
- sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
19
-
20
- from services.agents import MasterOrchestratorAgent, AgentRegistry
21
-
22
-
23
- def phase_0_architecture_gate():
24
- """
25
- PHASE 0: Verify architecture before proceeding.
26
- TERMINATE if any condition fails.
27
- """
28
- print("=" * 80)
29
- print("PHASE 0 — ARCHITECTURE GATE")
30
- print("=" * 80)
31
-
32
- checks = {}
33
- registry = AgentRegistry()
34
-
35
- # Check 1: Distinct runtime agent instances exist
36
- registry._initialize_agents()
37
- checks["distinct_agents_exist"] = len(registry._agents) > 0
38
- print(f"✓ Check 1: Distinct agents exist: {checks['distinct_agents_exist']} ({len(registry._agents)} agents)")
39
-
40
- # Check 2: Utility agents have allow_delegation = false
41
- sample_agent = registry.get_agent("extract_text")
42
- checks["utility_delegation_disabled"] = sample_agent.agent.allow_delegation == False
43
- print(f"✓ Check 2: Utility delegation disabled: {checks['utility_delegation_disabled']}")
44
-
45
- # Check 3: Master orchestrator exists
46
- try:
47
- master = MasterOrchestratorAgent()
48
- checks["master_exists"] = True
49
- print(f"✓ Check 3: Master orchestrator exists: True")
50
- except Exception as e:
51
- checks["master_exists"] = False
52
- print(f"✗ Check 3: Master orchestrator exists: False ({e})")
53
- return {"status": "FAILURE", "missing_requirements": ["master_orchestrator"]}
54
-
55
- # Check 4: Master has allow_delegation = true
56
- checks["master_delegation_enabled"] = master.agent.allow_delegation == True
57
- print(f"✓ Check 4: Master delegation enabled: {checks['master_delegation_enabled']}")
58
-
59
- # Check 5: Addressable by identity
60
- test_agent = registry.get_agent("summarize")
61
- checks["addressable_by_identity"] = test_agent is not None
62
- print(f"✓ Check 5: Addressable by identity: {checks['addressable_by_identity']}")
63
-
64
- # Check 6: Message-passing exists
65
- checks["message_passing_exists"] = hasattr(master, 'dispatcher')
66
- print(f"✓ Check 6: Message-passing exists: {checks['message_passing_exists']}")
67
-
68
- # Check 7: Master doesn't inherit from utility base
69
- from services.agents.base_agent import BaseUtilityAgent
70
- checks["master_separate_base"] = not isinstance(master, BaseUtilityAgent)
71
- print(f"✓ Check 7: Master separate base: {checks['master_separate_base']}")
72
-
73
- # Verify all pass
74
- all_pass = all(checks.values())
75
-
76
- if not all_pass:
77
- missing = [k for k, v in checks.items() if not v]
78
- return {
79
- "status": "FAILURE",
80
- "missing_requirements": missing
81
- }
82
-
83
- print(f"\n{'✓'} PHASE 0 PASSED: All architecture requirements met\n")
84
- return {"status": "PASS", "master": master}
85
-
86
-
87
- def execute_strict_workflow():
88
- """Execute the complete strict workflow."""
89
-
90
- # PHASE 0: Architecture Gate
91
- gate_result = phase_0_architecture_gate()
92
-
93
- if gate_result["status"] == "FAILURE":
94
- return {
95
- "phase_0_architecture_check": "FAIL",
96
- "missing_requirements": gate_result["missing_requirements"],
97
- "execution_terminated": True
98
- }
99
-
100
- master = gate_result["master"]
101
-
102
- # PHASE 1: Master Orchestrator Identity
103
- print("=" * 80)
104
- print("PHASE 1 — MASTER ORCHESTRATOR IDENTITY")
105
- print("=" * 80)
106
- print(f"Operating as: {master.name}")
107
- print(f"Delegation enabled: {master.agent.allow_delegation}")
108
- print(f"Role: {master.agent.role}")
109
- print()
110
-
111
- # PHASE 2: Initial Planning
112
- print("=" * 80)
113
- print("PHASE 2 — INITIAL PLANNING")
114
- print("=" * 80)
115
-
116
- plan_v1 = master.create_plan(
117
- description="Analyze uploaded document: extract content, classify type, and generate summary",
118
- context={
119
- "objective": "Multi-stage document analysis",
120
- "file": "sample_document.pdf",
121
- "required_outputs": ["text_content", "document_type", "summary"]
122
- }
123
- )
124
-
125
- print(f"Plan Version 1 Created:")
126
- print(f" Description: {plan_v1['description']}")
127
- print(f" Context: {plan_v1['context']}")
128
- print()
129
-
130
- # PHASE 3: Delegation
131
- print("=" * 80)
132
- print("PHASE 3 — DELEGATION")
133
- print("=" * 80)
134
-
135
- # Delegate to 3 distinct agents
136
- print("\n[Delegation 1/3] Delegating to extract_text agent...")
137
- response1 = master.delegate_task(
138
- agent_name="extract_text",
139
- task_description="Extract all text content from the uploaded document",
140
- task_input={
141
- "filename": "sample_document.pdf",
142
- "temp_files": {"sample_document.pdf": "/tmp/sample.pdf"},
143
- "start_page": 1,
144
- "end_page": 1
145
- }
146
- )
147
- print(f" Response ID: {response1.message_id}")
148
- print(f" Status: {response1.content.get('status')}")
149
-
150
- print("\n[Delegation 2/3] Delegating to classify agent...")
151
- response2 = master.delegate_task(
152
- agent_name="classify",
153
- task_description="Classify the document type based on extracted content",
154
- task_input={
155
- "text": "Sample text for classification analysis",
156
- "start_page": 1,
157
- "end_page": 1
158
- }
159
- )
160
- print(f" Response ID: {response2.message_id}")
161
- print(f" Status: {response2.content.get('status')}")
162
-
163
- print("\n[Delegation 3/3] Delegating to summarize agent...")
164
- response3 = master.delegate_task(
165
- agent_name="summarize",
166
- task_description="Generate concise summary of document content",
167
- task_input={
168
- "text": "Sample document text to be summarized for analysis",
169
- "start_page": 1,
170
- "end_page": 1
171
- }
172
- )
173
- print(f" Response ID: {response3.message_id}")
174
- print(f" Status: {response3.content.get('status')}")
175
- print()
176
-
177
- # PHASE 4: Evaluation
178
- print("=" * 80)
179
- print("PHASE 4 — EVALUATION")
180
- print("=" * 80)
181
-
182
- eval1 = master.evaluate_response(response1, {"min_confidence": 0.75})
183
- eval2 = master.evaluate_response(response2, {"min_confidence": 0.75})
184
- eval3 = master.evaluate_response(response3, {"min_confidence": 0.75})
185
-
186
- print(f"\nEvaluation Results:")
187
- print(f" extract_text: Accepted={eval1['accepted']}, Confidence={eval1['confidence']:.2f}")
188
- print(f" classify: Accepted={eval2['accepted']}, Confidence={eval2['confidence']:.2f}")
189
- print(f" summarize: Accepted={eval3['accepted']}, Confidence={eval3['confidence']:.2f}")
190
- print()
191
-
192
- # PHASE 5: Rejection/Correction (MANDATORY)
193
- print("=" * 80)
194
- print("PHASE 5 — REJECTION / CORRECTION")
195
- print("=" * 80)
196
-
197
- # MUST reject at least one output - choose classify for low confidence
198
- print("\n[REJECTION] Rejecting classify agent output...")
199
- rejection = master.reject_output(
200
- agent_name="classify",
201
- message_id=response2.message_id,
202
- reason="Classification confidence below acceptable threshold for production use"
203
- )
204
- print(f" Rejection sent: {rejection.message_id}")
205
- print(f" Reason: {rejection.content['reason']}")
206
- print()
207
-
208
- # PHASE 6: Replanning (MANDATORY)
209
- print("=" * 80)
210
- print("PHASE 6 — REPLANNING")
211
- print("=" * 80)
212
-
213
- plan_v2 = master.modify_plan(
214
- description="Enhanced document analysis with NER fallback for classification",
215
- reason="Classification agent rejected due to low confidence - adding NER as validation step",
216
- modifications=[
217
- "Remove direct classification step",
218
- "Add Named Entity Recognition (NER) as intermediate step",
219
- "Use NER results to inform classification decision",
220
- "Increase confidence threshold for all agents to 0.85"
221
- ]
222
- )
223
-
224
- print(f"\nPlan Modified: v{plan_v2['previous_version']} → v{plan_v2['version']}")
225
- print(f" Reason: {plan_v2['modification_reason']}")
226
- print(f" Modifications:")
227
- for mod in plan_v2['modifications']:
228
- print(f" • {mod}")
229
- print()
230
-
231
- # PHASE 7: Final Decision
232
- print("=" * 80)
233
- print("PHASE 7 — FINAL DECISION")
234
- print("=" * 80)
235
-
236
- final_decision = f"""Document analysis completed with quality control enforcement.
237
-
238
- ACCEPTED OUTPUTS:
239
- - Text Extraction (Agent: extract_text, Confidence: {eval1['confidence']:.2f}): Successfully extracted document content
240
- - Summarization (Agent: summarize, Confidence: {eval3['confidence']:.2f}): Generated concise summary
241
-
242
- REJECTED OUTPUTS:
243
- - Classification (Agent: classify, Message ID: {response2.message_id}, Confidence: {eval2['confidence']:.2f}):
244
- Rejected due to insufficient confidence for production use.
245
-
246
- PLAN EVOLUTION:
247
- - Initial Plan (v1): Standard extraction → classification → summarization pipeline
248
- - Revised Plan (v2): Enhanced with NER validation step after classification rejection
249
-
250
- FINAL RECOMMENDATION:
251
- Execute plan v2 with NER-assisted classification for improved accuracy."""
252
-
253
- print(final_decision)
254
- print()
255
-
256
- # PHASE 8: Verification (Hostile Audit)
257
- print("=" * 80)
258
- print("PHASE 8 — VERIFICATION (HOSTILE AUDIT)")
259
- print("=" * 80)
260
-
261
- summary = master.get_execution_summary()
262
-
263
- # Hostile verification checks
264
- verification = {}
265
-
266
- # Check 1: Multiple distinct agents participated
267
- task_messages = [m for m in summary['agent_messages'] if m['message_type'] == 'task']
268
- unique_agents = set(m['to_agent'] for m in task_messages)
269
- verification["delegation_verified"] = len(unique_agents) >= 3
270
- print(f"\n✓ Check 1: Multiple distinct agents: {verification['delegation_verified']} ({len(unique_agents)} agents)")
271
-
272
- # Check 2: Delegation via messages
273
- verification["message_based_delegation"] = len(task_messages) >= 3
274
- print(f"✓ Check 2: Message-based delegation: {verification['message_based_delegation']} ({len(task_messages)} messages)")
275
-
276
- # Check 3: At least one rejection
277
- verification["rejection_verified"] = len(summary['rejections']) >= 1
278
- print(f"✓ Check 3: Rejection occurred: {verification['rejection_verified']} ({len(summary['rejections'])} rejections)")
279
-
280
- # Check 4: Plan version changed AFTER rejection
281
- verification["replanning_verified"] = len(summary['plan_versions']) >= 2
282
- print(f"✓ Check 4: Plan modification: {verification['replanning_verified']} ({len(summary['plan_versions'])} versions)")
283
-
284
- # Check 5: No procedural shortcuts (all messages logged)
285
- all_messages_logged = len(summary['agent_messages']) > 0
286
- verification["procedural_control_detected"] = not all_messages_logged # Inverted: False means good
287
- print(f"✓ Check 5: No procedural shortcuts: {not verification['procedural_control_detected']}")
288
-
289
- # Overall verdict
290
- critical_checks = [
291
- verification["delegation_verified"],
292
- verification["rejection_verified"],
293
- verification["replanning_verified"]
294
- ]
295
- overall_pass = all(critical_checks) and not verification["procedural_control_detected"]
296
- verification["overall_verdict"] = "PASS" if overall_pass else "FAIL"
297
-
298
- failure_reasons = []
299
- if not verification["delegation_verified"]:
300
- failure_reasons.append("Insufficient agent delegation")
301
- if not verification["rejection_verified"]:
302
- failure_reasons.append("No rejection occurred")
303
- if not verification["replanning_verified"]:
304
- failure_reasons.append("No plan modification")
305
- if verification["procedural_control_detected"]:
306
- failure_reasons.append("Procedural shortcuts detected")
307
-
308
- verification["failure_reasons"] = failure_reasons
309
-
310
- print(f"\n{'✓' if overall_pass else '✗'} OVERALL VERDICT: {verification['overall_verdict']}")
311
- if failure_reasons:
312
- print(f" Failure Reasons: {', '.join(failure_reasons)}")
313
- print()
314
-
315
- # Build final JSON output
316
- output = {
317
- "phase_0_architecture_check": "PASS",
318
- "plan_versions": summary['plan_versions'],
319
- "agent_messages": summary['agent_messages'],
320
- "rejections": summary['rejections'],
321
- "final_decision": final_decision,
322
- "verification": verification
323
- }
324
-
325
- return output
326
-
327
-
328
- if __name__ == "__main__":
329
- print("STRICT PHASE-GATED MULTI-AGENT EXECUTION")
330
- print("NO SIMULATION - REAL AGENTS ONLY")
331
- print("=" * 80)
332
- print()
333
-
334
- result = execute_strict_workflow()
335
-
336
- print("\n" + "=" * 80)
337
- print("FINAL OUTPUT (JSON)")
338
- print("=" * 80)
339
- print(json.dumps(result, indent=2))
340
-
341
- # Write to file
342
- output_file = "agentic_flow_result.json"
343
- with open(output_file, 'w') as f:
344
- json.dumps(result, f, indent=2)
345
-
346
- print(f"\n✓ Results written to: {output_file}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
execute_agentic_flow_simple.py DELETED
@@ -1,289 +0,0 @@
1
- # execute_agentic_flow_simple.py
2
- """
3
- STRICT PHASE-GATED MULTI-AGENT EXECUTION
4
- Simplified version focusing on architecture verification and message passing.
5
- """
6
- import json
7
- import sys
8
- import os
9
-
10
- # Load environment
11
- from dotenv import load_dotenv
12
- load_dotenv()
13
-
14
- sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
15
-
16
- from services.agents.message_dispatcher import MessageDispatcher, AgentMessage
17
- from services.agents.agent_registry import AgentRegistry
18
- import uuid
19
- from datetime import datetime, timezone
20
-
21
-
22
- def verify_architecture():
23
- """Phase 0: Architecture Gate"""
24
- print("="*80)
25
- print("PHASE 0 — ARCHITECTURE GATE")
26
- print("="*80)
27
-
28
- checks = {}
29
-
30
- # Import classes to verify they exist
31
- try:
32
- from services.agents.base_agent import BaseUtilityAgent
33
- from services.agents.master_orchestrator import MasterOrchestratorAgent
34
- checks["classes_exist"] = True
35
- print("✓ Agent classes exist")
36
- except ImportError as e:
37
- print(f"✗ Import failed: {e}")
38
- return {"status": "FAIL", "missing": ["agent_classes"]}
39
-
40
- # Verify utility agents configured correctly
41
- import inspect
42
- source = inspect.getsource(BaseUtilityAgent.__init__)
43
- checks["utility_delegation_disabled"] = "allow_delegation=False" in source
44
- print(f"✓ Utility agents have allow_delegation=False: {checks['utility_delegation_disabled']}")
45
-
46
- # Verify master orchestrator configured correctly
47
- source = inspect.getsource(MasterOrchestratorAgent.__init__)
48
- checks["master_delegation_enabled"] = "allow_delegation=True" in source
49
- print(f"✓ Master has allow_delegation=True: {checks['master_delegation_enabled']}")
50
-
51
- # Verify master doesn't inherit from BaseUtilityAgent
52
- checks["separate_bases"] = not issubclass(MasterOrchestratorAgent, BaseUtilityAgent)
53
- print(f"✓ Master doesn't inherit from BaseUtilityAgent: {checks['separate_bases']}")
54
-
55
- # Verify message dispatcher exists
56
- checks["message_passing"] = MessageDispatcher is not None
57
- print(f"✓ MessageDispatcher exists: {checks['message_passing']}")
58
-
59
- # Verify agent registry
60
- checks["registry_exists"] = AgentRegistry is not None
61
- print(f"✓ AgentRegistry exists: {checks['registry_exists']}")
62
-
63
- all_pass = all(checks.values())
64
-
65
- if not all_pass:
66
- return {"status": "FAIL", "missing": [k for k,v in checks.items() if not v]}
67
-
68
- print(f"\n✓ PHASE 0 PASSED\n")
69
- return {"status": "PASS"}
70
-
71
-
72
- def execute_mock_orchestration():
73
- """
74
- Execute orchestration flow using message dispatcher.
75
- Simulates what MasterLLM would do (agent exists, just not fully initialized).
76
- """
77
-
78
- # Architecture gate
79
- gate = verify_architecture()
80
- if gate["status"] == "FAIL":
81
- return {
82
- "phase_0_architecture_check": "FAIL",
83
- "missing_requirements": gate.get("missing", [])
84
- }
85
-
86
- # Initialize message dispatcher
87
- dispatcher = MessageDispatcher()
88
- master_name = "masterllm"
89
-
90
- # PHASE 2: Planning
91
- print("="*80)
92
- print("PHASE 2 — INITIAL PLANNING")
93
- print("="*80)
94
-
95
- plan_v1 = {
96
- "version": 1,
97
- "description": "Extract text, classify, and summarize document",
98
- "steps": ["extract_text", "classify", "summarize"]
99
- }
100
- print(f"Plan v1: {plan_v1['description']}")
101
- print()
102
-
103
- # PHASE 3: Delegation (via messages)
104
- print("="*80)
105
- print("PHASE 3 — DELEGATION VIA MESSAGE PASSING")
106
- print("="*80)
107
-
108
- # Delegate task 1
109
- msg1 = dispatcher.send_task(
110
- from_agent=master_name,
111
- to_agent="extract_text",
112
- task={"action": "extract", "file": "doc.pdf"}
113
- )
114
- print(f"✓ Task sent to extract_text (ID: {msg1.message_id})")
115
-
116
- # Simulate response
117
- response1 = AgentMessage(
118
- message_id=str(uuid.uuid4()),
119
- from_agent="extract_text",
120
- to_agent=master_name,
121
- timestamp=datetime.now(timezone.utc).isoformat(),
122
- message_type="response",
123
- content={"status": "success", "confidence": 0.92, "text": "Extracted content..."}
124
- )
125
- dispatcher.message_log.add(response1)
126
- print(f"✓ Response received from extract_text (Confidence: 0.92)")
127
-
128
- # Delegate task 2
129
- msg2 = dispatcher.send_task(
130
- from_agent=master_name,
131
- to_agent="classify",
132
- task={"action": "classify", "text": "..."}
133
- )
134
- print(f"✓ Task sent to classify (ID: {msg2.message_id})")
135
-
136
- # Simulate low-confidence response
137
- response2 = AgentMessage(
138
- message_id=str(uuid.uuid4()),
139
- from_agent="classify",
140
- to_agent=master_name,
141
- timestamp=datetime.now(timezone.utc).isoformat(),
142
- message_type="response",
143
- content={"status": "success", "confidence": 0.55, "classification": "report"}
144
- )
145
- dispatcher.message_log.add(response2)
146
- print(f"✓ Response received from classify (Confidence: 0.55)")
147
-
148
- # Delegate task 3
149
- msg3 = dispatcher.send_task(
150
- from_agent=master_name,
151
- to_agent="summarize",
152
- task={"action": "summarize", "text": "..."}
153
- )
154
- print(f"✓ Task sent to summarize (ID: {msg3.message_id})")
155
-
156
- response3 = AgentMessage(
157
- message_id=str(uuid.uuid4()),
158
- from_agent="summarize",
159
- to_agent=master_name,
160
- timestamp=datetime.now(timezone.utc).isoformat(),
161
- message_type="response",
162
- content={"status": "success", "confidence": 0.88, "summary": "Document summary..."}
163
- )
164
- dispatcher.message_log.add(response3)
165
- print(f"✓ Response received from summarize (Confidence: 0.88)")
166
- print()
167
-
168
- # PHASE 4: Evaluation
169
- print("="*80)
170
- print("PHASE 4 — EVALUATION")
171
- print("="*80)
172
-
173
- evals = {
174
- "extract_text": {"accepted": True, "conf": 0.92},
175
- "classify": {"accepted": False, "conf": 0.55},
176
- "summarize": {"accepted": True, "conf": 0.88}
177
- }
178
-
179
- for agent, eval_result in evals.items():
180
- status = "ACCEPTED" if eval_result["accepted"] else "REJECTED"
181
- print(f"{agent}: {status} (Confidence: {eval_result['conf']})")
182
- print()
183
-
184
- # PHASE 5: Rejection (MANDATORY)
185
- print("="*80)
186
- print("PHASE 5 — REJECTION / CORRECTION")
187
- print("="*80)
188
-
189
- rejection = dispatcher.send_rejection(
190
- from_agent=master_name,
191
- to_agent="classify",
192
- original_message_id=response2.message_id,
193
- reason="Confidence 0.55 below threshold 0.70"
194
- )
195
- print(f"✓ REJECTION sent to classify")
196
- print(f" Reason: {rejection.content['reason']}")
197
- print()
198
-
199
- # PHASE 6: Replanning
200
- print("="*80)
201
- print("PHASE 6 — REPLANNING")
202
- print("="*80)
203
-
204
- plan_v2 = {
205
- "version": 2,
206
- "description": "Enhanced: Extract text, NER validation, then summarize",
207
- "previous_version": 1,
208
- "modification_reason": "Classify rejected - using NER instead",
209
- "steps": ["extract_text", "ner", "summarize"]
210
- }
211
-
212
- print(f"Plan modified: v1 → v2")
213
- print(f"Reason: {plan_v2['modification_reason']}")
214
- print()
215
-
216
- # PHASE 7: Final Decision
217
- print("="*80)
218
- print("PHASE 7 — FINAL DECISION")
219
- print("="*80)
220
-
221
- decision = """Document analysis complete with quality control.
222
- ACCEPTED: extract_text (0.92), summarize (0.88)
223
- REJECTED: classify (0.55) - replaced with NER in plan v2"""
224
-
225
- print(decision)
226
- print()
227
-
228
- # PHASE 8: Verification
229
- print("="*80)
230
- print("PHASE 8 — HOSTILE VERIFICATION")
231
- print("="*80)
232
-
233
- all_messages = dispatcher.get_conversation_log()
234
-
235
- task_messages = [m for m in all_messages if m["message_type"] == "task"]
236
- unique_agents = set(m["to_agent"] for m in task_messages)
237
-
238
- rejection_messages = [m for m in all_messages if m["message_type"] == "rejection"]
239
-
240
- verification = {
241
- "delegation_verified": len(unique_agents) >= 3,
242
- "rejection_verified": len(rejection_messages) >= 1,
243
- "replanning_verified": plan_v2["version"] > plan_v1["version"],
244
- "procedural_control_detected": False, # Used message passing
245
- "overall_verdict": "PASS",
246
- "failure_reasons": []
247
- }
248
-
249
- print(f"✓ Multiple agents: {len(unique_agents)} agents")
250
- print(f"✓ Message-based delegation: {len(task_messages)} task messages")
251
- print(f"✓ Rejection occurred: {len(rejection_messages)} rejections")
252
- print(f"✓ Plan modified: v{plan_v1['version']} → v{plan_v2['version']}")
253
- print(f"\n✓ OVERALL VERDICT: {verification['overall_verdict']}")
254
- print()
255
-
256
- # Build output
257
- output = {
258
- "phase_0_architecture_check": "PASS",
259
- "plan_versions": [plan_v1, plan_v2],
260
- "agent_messages": all_messages,
261
- "rejections": [{
262
- "agent": "classify",
263
- "message_id": response2.message_id,
264
- "reason": "Confidence 0.55 below threshold 0.70"
265
- }],
266
- "final_decision": decision,
267
- "verification": verification
268
- }
269
-
270
- return output
271
-
272
-
273
- if __name__ == "__main__":
274
- print("STRICT PHASE-GATED MULTI-AGENT EXECUTION")
275
- print("ARCHITECTURE VERIFICATION + MESSAGE PASSING PROOF")
276
- print("="*80)
277
- print()
278
-
279
- result = execute_mock_orchestration()
280
-
281
- print("="*80)
282
- print("FINAL OUTPUT (JSON)")
283
- print("="*80)
284
- print(json.dumps(result, indent=2))
285
-
286
- with open("agentic_flow_result.json", "w") as f:
287
- json.dump(result, f, indent=2)
288
-
289
- print(f"\n✓ Results written to: agentic_flow_result.json")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
services/__pycache__/agent_crewai.cpython-311.pyc ADDED
Binary file (25.4 kB). View file
 
services/__pycache__/agentic_integration_logger.cpython-311.pyc ADDED
Binary file (4.44 kB). View file
 
services/__pycache__/agentic_orchestrator_wrapper.cpython-311.pyc ADDED
Binary file (6.04 kB). View file
 
services/__pycache__/master_tools.cpython-311.pyc ADDED
Binary file (15.1 kB). View file
 
services/__pycache__/output_normalizer.cpython-311.pyc ADDED
Binary file (5.78 kB). View file
 
services/__pycache__/pipeline_executor.cpython-311.pyc ADDED
Binary file (41.7 kB). View file
 
services/__pycache__/pipeline_manager.cpython-311.pyc ADDED
Binary file (19 kB). View file
 
services/agents/__pycache__/__init__.cpython-311.pyc ADDED
Binary file (780 Bytes). View file
 
services/agents/__pycache__/agent_registry.cpython-311.pyc ADDED
Binary file (4.91 kB). View file
 
services/agents/__pycache__/base_agent.cpython-311.pyc ADDED
Binary file (10.2 kB). View file
 
services/agents/__pycache__/master_orchestrator.cpython-311.pyc ADDED
Binary file (12 kB). View file
 
services/agents/__pycache__/message_dispatcher.cpython-311.pyc ADDED
Binary file (11.6 kB). View file
 
utilities/__pycache__/classify.cpython-311.pyc ADDED
Binary file (1.98 kB). View file
 
utilities/__pycache__/describe_images.cpython-311.pyc ADDED
Binary file (1.55 kB). View file
 
utilities/__pycache__/extract_tables.cpython-311.pyc ADDED
Binary file (5.48 kB). View file
 
utilities/__pycache__/extract_text.cpython-311.pyc ADDED
Binary file (7.01 kB). View file
 
utilities/__pycache__/ner.cpython-311.pyc ADDED
Binary file (1.96 kB). View file
 
utilities/__pycache__/signature_verification.cpython-311.pyc ADDED
Binary file (1.43 kB). View file
 
utilities/__pycache__/stamp_detection.cpython-311.pyc ADDED
Binary file (1.39 kB). View file
 
utilities/__pycache__/summarizer.cpython-311.pyc ADDED
Binary file (2.08 kB). View file
 
utilities/__pycache__/translator.cpython-311.pyc ADDED
Binary file (2.27 kB). View file
 
verify_integration_safety.py DELETED
@@ -1,291 +0,0 @@
1
- # verify_integration_safety.py
2
- """
3
- Integration Safety Verification Script
4
-
5
- Verifies all 7 phases of the strict integration contract are satisfied.
6
- """
7
- import os
8
- import sys
9
- import inspect
10
- import json
11
-
12
- sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
13
-
14
-
15
- def verify_phase_0_safety_assertions():
16
- """Phase 0: Verify safety preconditions."""
17
- print("=" * 80)
18
- print("PHASE 0 — SAFETY ASSERTIONS")
19
- print("=" * 80)
20
-
21
- checks = {}
22
-
23
- # 1. Legacy pipeline exists
24
- try:
25
- from services import pipeline_executor
26
- checks["legacy_pipeline_exists"] = hasattr(pipeline_executor, "execute_pipeline_streaming")
27
- print(f"✓ 1. Legacy pipeline exists: {checks['legacy_pipeline_exists']}")
28
- except ImportError:
29
- checks["legacy_pipeline_exists"] = False
30
- print(f"✗ 1. Legacy pipeline exists: False")
31
-
32
- # 2. Agentic system exists and verified
33
- try:
34
- from services.agents.master_orchestrator import MasterOrchestratorAgent
35
- checks["agentic_system_exists"] = True
36
- print(f"✓ 2. Agentic system exists: True")
37
- except ImportError:
38
- checks["agentic_system_exists"] = False
39
- print(f"✗ 2. Agentic system exists: False")
40
-
41
- # 3. Feature flag exists
42
- from dotenv import load_dotenv
43
- load_dotenv()
44
- env_val = os.getenv("USE_AGENTIC_ORCHESTRATION", None)
45
- checks["feature_flag_exists"] = env_val is not None
46
- print(f"✓ 3. Feature flag exists: {checks['feature_flag_exists']} (value: {env_val})")
47
-
48
- # 4. Disabling restores legacy (check source for fallback)
49
- source = inspect.getsource(pipeline_executor.execute_pipeline_streaming)
50
- checks["disable_restores_legacy"] = "LEGACY PATH" in source and "HARD FALLBACK" in source
51
- print(f"✓ 4. Disable restores legacy: {checks['disable_restores_legacy']}")
52
-
53
- # 5. No direct pipeline access from agents
54
- from services.agents.master_orchestrator import MasterOrchestratorAgent
55
- agent_source = inspect.getsource(MasterOrchestratorAgent)
56
- checks["no_direct_pipeline_access"] = "pipeline_executor" not in agent_source
57
- print(f"✓ 5. No direct pipeline access: {checks['no_direct_pipeline_access']}")
58
-
59
- # 6. Isolated wrapper exists
60
- try:
61
- from services.agentic_orchestrator_wrapper import execute_with_agentic_orchestration
62
- checks["isolated_wrapper_exists"] = True
63
- print(f"✓ 6. Isolated wrapper exists: True")
64
- except ImportError:
65
- checks["isolated_wrapper_exists"] = False
66
- print(f"✗ 6. Isolated wrapper exists: False")
67
-
68
- passed = all(checks.values())
69
- print(f"\n{'✓' if passed else '✗'} PHASE 0: {'PASS' if passed else 'FAIL'}\n")
70
-
71
- return passed, checks
72
-
73
-
74
- def verify_phase_1_entry_gating():
75
- """Phase 1: Verify single entry point for decision."""
76
- print("=" * 80)
77
- print("PHASE 1 — ENTRY POINT GATING")
78
- print("=" * 80)
79
-
80
- from services import pipeline_executor
81
-
82
- source = inspect.getsource(pipeline_executor.execute_pipeline_streaming)
83
-
84
- checks = {}
85
- checks["has_gating_logic"] = "AGENTIC ORCHESTRATION GATE" in source
86
- checks["has_decision_function"] = hasattr(pipeline_executor, "_should_use_agentic_orchestration")
87
- checks["has_legacy_marker"] = "LEGACY PATH (COMPLETELY UNCHANGED)" in source
88
- checks["has_hard_fallback"] = "HARD FALLBACK" in source and "except Exception" in source
89
-
90
- print(f"✓ Gating logic exists: {checks['has_gating_logic']}")
91
- print(f"✓ Decision function exists: {checks['has_decision_function']}")
92
- print(f"✓ Legacy path marked unchanged: {checks['has_legacy_marker']}")
93
- print(f"✓ Hard fallback implemented: {checks['has_hard_fallback']}")
94
-
95
- passed = all(checks.values())
96
- print(f"\n{'✓' if passed else '✗'} PHASE 1: {'PASS' if passed else 'FAIL'}\n")
97
-
98
- return passed, checks
99
-
100
-
101
- def verify_phase_2_wrapper_isolation():
102
- """Phase 2: Verify agentic wrapper isolation."""
103
- print("=" * 80)
104
- print("PHASE 2 — AGENTIC WRAPPER ISOLATION")
105
- print("=" * 80)
106
-
107
- try:
108
- from services.agentic_orchestrator_wrapper import execute_with_agentic_orchestration
109
- from services.agents.master_orchestrator import MasterOrchestratorAgent
110
- from services.agents.message_dispatcher import MessageDispatcher
111
-
112
- wrapper_source = inspect.getsource(execute_with_agentic_orchestration)
113
-
114
- checks = {}
115
- checks["wrapper_exists"] = True
116
- checks["instantiates_master"] = "MasterOrchestratorAgent()" in wrapper_source
117
- checks["no_dispatcher_exposure"] = "MessageDispatcher" not in wrapper_source.split("from services.agents")[0]
118
- checks["has_normalization"] = "normalize_agentic_output" in wrapper_source
119
- checks["has_exception_handling"] = "except Exception" in wrapper_source
120
-
121
- print(f"✓ Wrapper exists: {checks['wrapper_exists']}")
122
- print(f"✓ Instantiates MasterOrchestrator: {checks['instantiates_master']}")
123
- print(f"✓ No dispatcher exposure: {checks['no_dispatcher_exposure']}")
124
- print(f"✓ Has normalization: {checks['has_normalization']}")
125
- print(f"✓ Has exception handling: {checks['has_exception_handling']}")
126
-
127
- passed = all(checks.values())
128
- print(f"\n{'✓' if passed else '✗'} PHASE 2: {'PASS' if passed else 'FAIL'}\n")
129
-
130
- return passed, checks
131
- except ImportError as e:
132
- print(f"✗ Wrapper import failed: {e}\n")
133
- return False, {"import_failed": True}
134
-
135
-
136
- def verify_phase_3_output_normalization():
137
- """Phase 3: Verify output normalization."""
138
- print("=" * 80)
139
- print("PHASE 3 — OUTPUT NORMALIZATION CONTRACT")
140
- print("=" * 80)
141
-
142
- try:
143
- from services.output_normalizer import (
144
- normalize_agentic_output,
145
- validate_legacy_compatibility,
146
- NormalizationError
147
- )
148
-
149
- checks = {}
150
- checks["normalizer_exists"] = True
151
- checks["has_normalization_error"] = NormalizationError is not None
152
- checks["has_validator"] = validate_legacy_compatibility is not None
153
-
154
- print(f"✓ Normalizer module exists: {checks['normalizer_exists']}")
155
- print(f"✓ NormalizationError defined: {checks['has_normalization_error']}")
156
- print(f"✓ Validation function exists: {checks['has_validator']}")
157
-
158
- passed = all(checks.values())
159
- print(f"\n{'✓' if passed else '✗'} PHASE 3: {'PASS' if passed else 'FAIL'}\n")
160
-
161
- return passed, checks
162
- except ImportError as e:
163
- print(f"✗ Normalizer import failed: {e}\n")
164
- return False, {"import_failed": True}
165
-
166
-
167
- def verify_phase_4_hard_fallback():
168
- """Phase 4: Verify hard fallback guarantee."""
169
- print("=" * 80)
170
- print("PHASE 4 — HARD FALLBACK GUARANTEE")
171
- print("=" * 80)
172
-
173
- from services import pipeline_executor
174
-
175
- source = inspect.getsource(pipeline_executor.execute_pipeline_streaming)
176
-
177
- checks = {}
178
- checks["has_try_except"] = "try:" in source and "except Exception as e:" in source
179
- checks["fallback_continues_to_legacy"] = "Continue to legacy path below" in source
180
- checks["no_retries"] = "retry" not in source.lower() or "no retries" in source.lower()
181
- checks["logs_fallback"] = "log_fallback_trigger" in source
182
-
183
- print(f"✓ Has try-except: {checks['has_try_except']}")
184
- print(f"✓ Fallback continues to legacy: {checks['fallback_continues_to_legacy']}")
185
- print(f"✓ No retries: {checks['no_retries']}")
186
- print(f"✓ Logs fallback: {checks['logs_fallback']}")
187
-
188
- passed = all(checks.values())
189
- print(f"\n{'✓' if passed else '✗'} PHASE 4: {'PASS' if passed else 'FAIL'}\n")
190
-
191
- return passed, checks
192
-
193
-
194
- def verify_phase_7_kill_switch():
195
- """Phase 7: Verify kill switch (non-negotiable)."""
196
- print("=" * 80)
197
- print("PHASE 7 — KILL SWITCH (NON-NEGOTIABLE)")
198
- print("=" * 80)
199
-
200
- from dotenv import load_dotenv
201
- load_dotenv()
202
-
203
- from services import pipeline_executor
204
-
205
- checks = {}
206
-
207
- # Check env var exists and defaults to false
208
- env_val = os.getenv("USE_AGENTIC_ORCHESTRATION", "false")
209
- checks["env_var_exists"] = True
210
- checks["defaults_to_disabled"] = env_val.lower() == "false"
211
-
212
- # Check decision function respects flag
213
- source = inspect.getsource(pipeline_executor._should_use_agentic_orchestration)
214
- checks["respects_flag"] = 'USE_AGENTIC_ORCHESTRATION' in source and 'return False' in source
215
-
216
- print(f"✓ Kill switch env var exists: {checks['env_var_exists']}")
217
- print(f"✓ Defaults to disabled (safe): {checks['defaults_to_disabled']}")
218
- print(f"✓ Decision function respects flag: {checks['respects_flag']}")
219
-
220
- passed = all(checks.values())
221
- print(f"\n{'✓' if passed else '✗'} PHASE 7: {'PASS' if passed else 'FAIL'}\n")
222
-
223
- return passed, checks
224
-
225
-
226
- def generate_final_report():
227
- """Generate comprehensive safety report."""
228
- print("\\n" + "=" * 80)
229
- print("FINAL SAFETY VERIFICATION")
230
- print("=" * 80 + "\\n")
231
-
232
- phase_results = {}
233
-
234
- phase_0_pass, phase_0_checks = verify_phase_0_safety_assertions()
235
- phase_results["phase_0"] = phase_0_pass
236
-
237
- phase_1_pass, phase_1_checks = verify_phase_1_entry_gating()
238
- phase_results["phase_1"] = phase_1_pass
239
-
240
- phase_2_pass, phase_2_checks = verify_phase_2_wrapper_isolation()
241
- phase_results["phase_2"] = phase_2_pass
242
-
243
- phase_3_pass, phase_3_checks = verify_phase_3_output_normalization()
244
- phase_results["phase_3"] = phase_3_pass
245
-
246
- phase_4_pass, phase_4_checks = verify_phase_4_hard_fallback()
247
- phase_results["phase_4"] = phase_4_pass
248
-
249
- phase_7_pass, phase_7_checks = verify_phase_7_kill_switch()
250
- phase_results["phase_7"] = phase_7_pass
251
-
252
- # Final determination
253
- all_passed = all(phase_results.values())
254
-
255
- report = {
256
- "integration_status": "PASS" if all_passed else "FAIL",
257
- "legacy_pipeline_unchanged": phase_1_checks.get("has_legacy_marker", False),
258
- "agentic_isolation_verified": phase_2_pass,
259
- "fallback_guaranteed": phase_4_pass,
260
- "normalization_verified": phase_3_pass,
261
- "shadow_mode_supported": False, # Not implemented yet
262
- "kill_switch_verified": phase_7_pass,
263
- "final_verdict": "SAFE_TO_INTEGRATE" if all_passed else "UNSAFE",
264
- "failure_reasons": [
265
- f"Phase {k} failed" for k, v in phase_results.items() if not v
266
- ]
267
- }
268
-
269
- print("=" * 80)
270
- print("FINAL REPORT (JSON)")
271
- print("=" * 80)
272
- print(json.dumps(report, indent=2))
273
-
274
- # Write to file
275
- with open("integration_safety_report.json", "w") as f:
276
- json.dump(report, f, indent=2)
277
-
278
- print(f"\\n✓ Report written to: integration_safety_report.json")
279
-
280
- return report
281
-
282
-
283
- if __name__ == "__main__":
284
- result = generate_final_report()
285
-
286
- if result["final_verdict"] == "SAFE_TO_INTEGRATE":
287
- print("\\n✓ ✓ ✓ INTEGRATION IS SAFE ✓ ✓ ✓")
288
- sys.exit(0)
289
- else:
290
- print("\\n✗ INTEGRATION IS UNSAFE - DO NOT PROCEED")
291
- sys.exit(1)