wu981526092 commited on
Commit
75bf438
Β·
1 Parent(s): e20ad3b
analyze_real_agent_trace.py DELETED
@@ -1,280 +0,0 @@
1
- #!/usr/bin/env python3
2
- """
3
- Analyze Real Agent Trace for Prompt Reconstruction Enhancement
4
-
5
- This script analyzes the provided real agent trace to understand how we can
6
- enhance our prompt reconstruction to better simulate actual agent reasoning
7
- patterns and internal thought processes.
8
- """
9
-
10
- import json
11
- import sys
12
- import os
13
- sys.path.append('/Users/zekunwu/Desktop/agent_monitoring/huggingface/AgentGraph')
14
-
15
- def analyze_real_trace_structure():
16
- """Analyze the structure of the real agent trace."""
17
-
18
- print("πŸ” REAL AGENT TRACE ANALYSIS")
19
- print("=" * 70)
20
-
21
- # Simulate the provided trace structure
22
- real_trace_example = {
23
- "type": "ai",
24
- "content": "",
25
- "additional_kwargs": {
26
- "reasoning_content": """We need to act as supervisor agent. The last user message: "write me final report for q1 2024!" We must follow instructions. The last message is the task. We need to produce final report. We must do steps: 1. Summarize request, period. 2. Retrieve data using get_order_data_for_period. Then delegate to revenue analyst, etc. We need to call tools accordingly. Let's do step 1: produce plan. Then step 2: call get_order_data_for_period with start_date 2024-01-01 end_date 2024-03-31...""",
27
- "tool_calls": [
28
- {
29
- "id": "fc_2372f7b7-2858-4b94-b5b2-4441ed19b66f",
30
- "function": {
31
- "arguments": {
32
- "end_date": "2024-03-31",
33
- "save_to_filename": "2024-01-01_to_2024-03-31_order.json",
34
- "start_date": "2024-01-01"
35
- },
36
- "name": "get_order_data_for_period"
37
- },
38
- "type": "function"
39
- }
40
- ]
41
- },
42
- "response_metadata": {
43
- "token_usage": {
44
- "completion_tokens": 533,
45
- "prompt_tokens": 2105,
46
- "total_tokens": 2638
47
- },
48
- "model_name": "openai/gpt-oss-20b"
49
- }
50
- }
51
-
52
- print("πŸ“Š Key Components in Real Agent Trace:")
53
- print("─" * 50)
54
- print("βœ… reasoning_content: Agent's internal thinking process")
55
- print("βœ… tool_calls: Structured function calls with arguments")
56
- print("βœ… response_metadata: Token usage and model information")
57
- print("βœ… content: Public response (often empty when using tools)")
58
- print()
59
-
60
- reasoning_content = real_trace_example["additional_kwargs"]["reasoning_content"]
61
- print(f"🧠 Reasoning Content Analysis:")
62
- print(f" Length: {len(reasoning_content)} characters")
63
- print(f" Contains planning: {'step' in reasoning_content.lower()}")
64
- print(f" Contains decision making: {'need to' in reasoning_content.lower()}")
65
- print(f" Contains self-reflection: {'but' in reasoning_content.lower()}")
66
- print()
67
-
68
- tool_call = real_trace_example["additional_kwargs"]["tool_calls"][0]
69
- print(f"πŸ”§ Tool Call Analysis:")
70
- print(f" Function: {tool_call['function']['name']}")
71
- print(f" Arguments: {len(tool_call['function']['arguments'])} parameters")
72
- print(f" ID: {tool_call['id']}")
73
-
74
- def extract_agent_reasoning_patterns():
75
- """Extract and analyze agent reasoning patterns from the trace."""
76
-
77
- print(f"\n🎯 AGENT REASONING PATTERNS")
78
- print("=" * 70)
79
-
80
- # Extract key reasoning patterns from the provided trace
81
- reasoning_patterns = {
82
- "task_analysis": {
83
- "pattern": "The last user message: \"write me final report for q1 2024!\"",
84
- "description": "Agent identifies and quotes the user's request"
85
- },
86
- "instruction_following": {
87
- "pattern": "We must follow instructions. The last message is the task.",
88
- "description": "Agent acknowledges constraints and task definition"
89
- },
90
- "step_planning": {
91
- "pattern": "We must do steps: 1. Summarize request, period. 2. Retrieve data...",
92
- "description": "Agent breaks down complex tasks into sequential steps"
93
- },
94
- "tool_selection": {
95
- "pattern": "We need to call tools accordingly. Let's do step 1: produce plan. Then step 2: call get_order_data_for_period",
96
- "description": "Agent selects appropriate tools for each step"
97
- },
98
- "parameter_reasoning": {
99
- "pattern": "with start_date 2024-01-01 end_date 2024-03-31",
100
- "description": "Agent reasons about tool parameters based on context"
101
- },
102
- "constraint_awareness": {
103
- "pattern": "But we don't have actual agent. We can simulate?",
104
- "description": "Agent recognizes system limitations and adapts"
105
- },
106
- "self_correction": {
107
- "pattern": "Maybe we can skip and produce final report with placeholders? But instructions say must not end until all analyses done.",
108
- "description": "Agent evaluates options and corrects course"
109
- }
110
- }
111
-
112
- for pattern_name, pattern_info in reasoning_patterns.items():
113
- print(f"πŸ“‹ {pattern_name.replace('_', ' ').title()}:")
114
- print(f" Pattern: {pattern_info['pattern'][:100]}...")
115
- print(f" Purpose: {pattern_info['description']}")
116
- print()
117
-
118
- def design_enhanced_prompt_reconstruction():
119
- """Design enhanced prompt reconstruction that captures agent reasoning."""
120
-
121
- print(f"πŸš€ ENHANCED PROMPT RECONSTRUCTION DESIGN")
122
- print("=" * 70)
123
-
124
- enhanced_structure = {
125
- "system_prompt": {
126
- "role_definition": "You are a supervisor agent responsible for coordinating multi-agent workflows",
127
- "reasoning_instructions": "Think through each step carefully. Show your reasoning process explicitly.",
128
- "constraint_awareness": "Be aware of system limitations and adapt accordingly",
129
- "tool_usage": "Select appropriate tools for each task step"
130
- },
131
- "user_message": {
132
- "task_definition": "write me final report for q1 2024!",
133
- "context": "Previous conversations and system state",
134
- "expectations": "Follow the workflow: analyze β†’ delegate β†’ compile β†’ report"
135
- },
136
- "reasoning_framework": {
137
- "task_analysis": "Identify and quote the user's request",
138
- "instruction_parsing": "Acknowledge constraints and requirements",
139
- "step_planning": "Break down complex tasks into sequential steps",
140
- "tool_selection": "Choose appropriate tools for each step",
141
- "parameter_reasoning": "Reason about tool parameters based on context",
142
- "constraint_handling": "Recognize limitations and adapt strategy",
143
- "self_correction": "Evaluate options and correct course when needed"
144
- },
145
- "expected_output": {
146
- "reasoning_content": "Detailed internal thinking process",
147
- "tool_calls": "Structured function calls with reasoned arguments",
148
- "content": "Public response or empty when using tools"
149
- }
150
- }
151
-
152
- print("πŸ“ Enhanced Reconstruction Components:")
153
- print("─" * 50)
154
-
155
- for component, details in enhanced_structure.items():
156
- print(f"βœ… {component.replace('_', ' ').title()}:")
157
- if isinstance(details, dict):
158
- for key, value in details.items():
159
- print(f" β€’ {key.replace('_', ' ').title()}: {value}")
160
- else:
161
- print(f" {details}")
162
- print()
163
-
164
- def create_enhanced_agent_prompt_template():
165
- """Create an enhanced agent prompt template based on real trace analysis."""
166
-
167
- print(f"πŸ“„ ENHANCED AGENT PROMPT TEMPLATE")
168
- print("=" * 70)
169
-
170
- template = """system: You are a {agent_role} responsible for {agent_capabilities}.
171
-
172
- Your reasoning process should be explicit and structured:
173
- 1. Task Analysis: Identify and understand the user's request
174
- 2. Instruction Parsing: Acknowledge constraints and requirements
175
- 3. Step Planning: Break down complex tasks into sequential steps
176
- 4. Tool Selection: Choose appropriate tools for each step
177
- 5. Parameter Reasoning: Reason about tool parameters based on context
178
- 6. Constraint Handling: Recognize limitations and adapt strategy
179
- 7. Self Correction: Evaluate options and correct course when needed
180
-
181
- Available Tools:
182
- {tool_definitions}
183
-
184
- Response Format:
185
- - Use reasoning_content to show your internal thinking process
186
- - Make structured tool calls with reasoned arguments
187
- - Provide public content only when not using tools
188
-
189
- Instructions:
190
- {specific_instructions}
191
-
192
- user: {user_message}
193
-
194
- Expected Workflow:
195
- {workflow_steps}
196
-
197
- Remember: Show your reasoning explicitly. Think through each decision step by step."""
198
-
199
- print("πŸ“‹ Template Structure:")
200
- print(template)
201
-
202
- print(f"\n🎯 Template Features:")
203
- print("─" * 30)
204
- print("βœ… Explicit reasoning instructions")
205
- print("βœ… Structured thinking framework")
206
- print("βœ… Tool usage guidance")
207
- print("βœ… Response format specifications")
208
- print("βœ… Workflow expectations")
209
- print("βœ… Self-reflection encouragement")
210
-
211
- def demonstrate_enhanced_reconstruction():
212
- """Demonstrate how to apply enhanced reconstruction to our sample."""
213
-
214
- print(f"\n🎭 ENHANCED RECONSTRUCTION DEMONSTRATION")
215
- print("=" * 70)
216
-
217
- # Enhanced reconstruction for our algorithm sample
218
- enhanced_verification_agent = {
219
- "agent_role": "Verification Expert",
220
- "agent_capabilities": "validating information accuracy and conducting detailed analysis",
221
- "specific_instructions": """Your task is to verify the accuracy of provided costs for daily tickets and season passes for California's Great America in San Jose for summer 2024.
222
-
223
- You must:
224
- 1. Confirm the cost of a daily ticket for California's Great America in 2024
225
- 2. Confirm the cost of a season pass for California's Great America in 2024
226
- 3. Provide verified results with explanations
227
-
228
- Constraints:
229
- - Costs must be accurate and reflect 2024 summer prices
230
- - Show your verification methodology
231
- - Explain your reasoning process""",
232
- "user_message": "How much did I save by purchasing a season pass instead of daily tickets for California's Great America in San Jose, if I planned to visit once a month in June, July, August, and September during the summer of 2024?",
233
- "workflow_steps": "1. Analyze the question β†’ 2. Verify ticket prices β†’ 3. Calculate savings β†’ 4. Provide detailed explanation",
234
- "expected_reasoning": """Let me analyze this step by step. The user is asking about savings from a season pass vs daily tickets. I need to:
235
-
236
- 1. Identify the specific venue: California's Great America in San Jose
237
- 2. Confirm current pricing for both daily tickets and season passes for 2024
238
- 3. Calculate cost for 4 visits (June, July, August, September)
239
- 4. Compare total costs and determine savings
240
-
241
- First, let me verify the current pricing. Based on historical patterns and typical amusement park pricing..."""
242
- }
243
-
244
- print("πŸŽͺ Enhanced Verification Agent Reconstruction:")
245
- print("─" * 50)
246
- print(f"Role: {enhanced_verification_agent['agent_role']}")
247
- print(f"Capabilities: {enhanced_verification_agent['agent_capabilities']}")
248
- print(f"Task: {enhanced_verification_agent['user_message'][:100]}...")
249
- print(f"Expected Reasoning Length: {len(enhanced_verification_agent['expected_reasoning'])} characters")
250
- print()
251
- print("πŸ“Š This captures the detailed thinking process similar to the real trace!")
252
-
253
- def main():
254
- """Main analysis function."""
255
-
256
- analyze_real_trace_structure()
257
- extract_agent_reasoning_patterns()
258
- design_enhanced_prompt_reconstruction()
259
- create_enhanced_agent_prompt_template()
260
- demonstrate_enhanced_reconstruction()
261
-
262
- print(f"\nπŸŽ‰ CONCLUSION")
263
- print("=" * 70)
264
- print("βœ… Real agent traces contain rich reasoning_content that shows:")
265
- print(" β€’ Step-by-step thinking processes")
266
- print(" β€’ Tool selection reasoning")
267
- print(" β€’ Constraint awareness and adaptation")
268
- print(" β€’ Self-correction and replanning")
269
- print()
270
- print("🎯 Our prompt reconstruction should capture this by:")
271
- print(" β€’ Adding explicit reasoning instructions")
272
- print(" β€’ Structuring thinking frameworks")
273
- print(" β€’ Encouraging step-by-step analysis")
274
- print(" β€’ Modeling internal decision processes")
275
- print()
276
- print("πŸš€ This will enable more realistic perturbation testing and")
277
- print(" causal analysis by capturing actual agent cognition patterns!")
278
-
279
- if __name__ == "__main__":
280
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
create_realistic_prompt_reconstruction.py DELETED
@@ -1,362 +0,0 @@
1
- #!/usr/bin/env python3
2
- """
3
- Create Realistic Prompt Reconstruction Based on Real Agent Trace Patterns
4
-
5
- This script creates a more realistic prompt reconstruction by incorporating
6
- the reasoning patterns and internal thought processes observed in real agent traces.
7
- """
8
-
9
- import json
10
- import sys
11
- import os
12
- import copy
13
- sys.path.append('/Users/zekunwu/Desktop/agent_monitoring/huggingface/AgentGraph')
14
-
15
- from agentgraph.reconstruction import PromptReconstructor
16
-
17
- def create_realistic_agent_prompts():
18
- """Create realistic agent prompts with internal reasoning patterns."""
19
-
20
- print("🧠 CREATING REALISTIC AGENT PROMPTS")
21
- print("=" * 70)
22
-
23
- realistic_agents = {
24
- 'agent_002': { # Verification Expert
25
- 'name': 'Verification_Expert',
26
- 'raw_prompt': """You are a Verification_Expert responsible for validating information accuracy and conducting detailed analysis.
27
-
28
- Your reasoning process should be explicit and structured:
29
- 1. Task Analysis: Identify and understand the user's request
30
- 2. Instruction Parsing: Acknowledge constraints and requirements
31
- 3. Step Planning: Break down complex tasks into sequential steps
32
- 4. Tool Selection: Choose appropriate tools for each step
33
- 5. Parameter Reasoning: Reason about tool parameters based on context
34
- 6. Constraint Handling: Recognize limitations and adapt strategy
35
- 7. Self Correction: Evaluate options and correct course when needed
36
-
37
- Available Tools:
38
- - web_search: Search for current pricing information
39
- - calculator: Perform mathematical calculations
40
- - data_retrieval: Access historical pricing data
41
-
42
- Response Format:
43
- - Use reasoning_content to show your internal thinking process
44
- - Make structured tool calls with reasoned arguments
45
- - Provide public content with detailed explanations
46
-
47
- Your expertise includes:
48
- - Verifying costs, prices, and numerical data
49
- - Cross-checking information against historical patterns
50
- - Conducting detailed analysis and calculations
51
- - Providing verified results with explanations
52
-
53
- Remember: Show your reasoning explicitly. Think through each decision step by step."""
54
- },
55
- 'agent_001': { # Problem Solving Expert
56
- 'name': 'ProblemSolving_Expert',
57
- 'raw_prompt': """You are a ProblemSolving_Expert specialized in task coordination and management.
58
-
59
- Your reasoning process should be explicit and structured:
60
- 1. Task Analysis: Break down complex problems into manageable components
61
- 2. Workflow Planning: Design step-by-step solution approaches
62
- 3. Resource Allocation: Assign tasks to appropriate experts
63
- 4. Progress Monitoring: Track task completion and quality
64
- 5. Coordination: Ensure smooth handoffs between team members
65
- 6. Quality Assurance: Validate outputs meet requirements
66
-
67
- Available Tools:
68
- - task_planner: Create detailed task breakdown structures
69
- - team_coordinator: Assign tasks to team members
70
- - progress_tracker: Monitor task completion status
71
-
72
- Response Format:
73
- - Use reasoning_content to show your coordination thinking
74
- - Structure tasks clearly with priorities and dependencies
75
- - Provide clear instructions to team members
76
-
77
- Your role is to:
78
- - Analyze complex problems and break them down into manageable tasks
79
- - Coordinate with other experts to solve multi-step problems
80
- - Provide task descriptions and guidance to verification experts
81
- - Ensure proper workflow execution"""
82
- },
83
- 'agent_003': { # Arithmetic Progressions Expert
84
- 'name': 'ArithmeticProgressions_Expert',
85
- 'raw_prompt': """You are an ArithmeticProgressions_Expert specialized in mathematical calculations and analysis.
86
-
87
- Your reasoning process should be explicit and structured:
88
- 1. Mathematical Analysis: Identify the mathematical nature of the problem
89
- 2. Formula Selection: Choose appropriate mathematical formulas and methods
90
- 3. Calculation Planning: Structure calculations in logical sequence
91
- 4. Validation: Cross-check results using alternative methods
92
- 5. Pattern Recognition: Identify mathematical patterns and sequences
93
- 6. Result Interpretation: Explain mathematical findings in context
94
-
95
- Available Tools:
96
- - advanced_calculator: Perform complex mathematical operations
97
- - formula_library: Access mathematical formulas and theorems
98
- - pattern_analyzer: Identify mathematical patterns
99
-
100
- Response Format:
101
- - Use reasoning_content to show your mathematical thinking
102
- - Present calculations with clear step-by-step explanations
103
- - Validate results through multiple approaches
104
-
105
- Your expertise includes:
106
- - Validating arithmetic calculations and mathematical reasoning
107
- - Analyzing numerical sequences and patterns
108
- - Confirming computational results
109
- - Providing mathematical validation for problem solutions"""
110
- }
111
- }
112
-
113
- for agent_id, agent_data in realistic_agents.items():
114
- print(f"βœ… Created realistic prompt for {agent_id}: {agent_data['name']}")
115
- print(f" Length: {len(agent_data['raw_prompt'])} characters")
116
- print(f" Features: Reasoning framework, tool definitions, role clarity")
117
- print()
118
-
119
- return realistic_agents
120
-
121
- def create_realistic_interaction_prompts():
122
- """Create realistic interaction prompts with reasoning content."""
123
-
124
- print("πŸ’¬ CREATING REALISTIC INTERACTION PROMPTS")
125
- print("=" * 70)
126
-
127
- realistic_interactions = {
128
- 'relation_002': { # agent_002 β†’ task_001 (PERFORMS)
129
- 'interaction_prompt': """Task Assignment with Manager Instructions:
130
-
131
- You are given: (1) a task and advises from your manager with a specific plan and (2) a general task.
132
- Collect information from the general task, follow the suggestions from manager to solve the task.
133
-
134
- # General Task
135
- How much did I save by purchasing a season pass instead of daily tickets for California's Great America in San Jose, if I planned to visit once a month in June, July, August, and September during the summer of 2024? Please solve the task carefully.
136
-
137
- # Task and suggestions from manager
138
- ## Task description
139
- Verify the accuracy of the provided costs for a daily ticket and a season pass for California's Great America in San Jose for the summer of 2024.
140
-
141
- ## Plan for solving the task
142
- 1. Confirm the cost of a daily ticket for California's Great America in 2024.
143
- 2. Confirm the cost of a season pass for California's Great America in 2024.
144
-
145
- ## Output format
146
- - Verified cost of a daily ticket in 2024
147
- - Verified cost of a season pass in 2024
148
-
149
- ## Constraints and conditions for completion
150
- - The costs must be accurate and reflect the prices for the summer of 2024.
151
-
152
- ## Results from last response
153
- - Cost of a daily ticket in 2024: $60
154
- - Cost of a season pass in 2024: $120
155
-
156
- Expected Reasoning Process:
157
- Think through this step by step. Show your reasoning about:
158
- - How you will verify these prices
159
- - What sources you trust for accuracy
160
- - How you handle any conflicting information
161
- - Your methodology for ensuring 2024 summer pricing"""
162
- },
163
- 'rel_uses_computer': { # agent_001 β†’ agent_004 (USES)
164
- 'interaction_prompt': """Tool Usage Request with Reasoning Context:
165
-
166
- I need to use the Computer Terminal for computational tasks related to the ticket pricing analysis.
167
-
168
- My reasoning for this tool usage:
169
- 1. Task Context: We need to calculate savings from season pass vs daily tickets
170
- 2. Calculation Required: 4 visits Γ— daily ticket price vs season pass price
171
- 3. Tool Selection: Computer Terminal is appropriate for mathematical calculations
172
- 4. Expected Output: Precise calculation with clear breakdown
173
-
174
- Specific calculation request:
175
- - Calculate: 4 visits Γ— $60 per visit = total cost for daily tickets
176
- - Compare with: $120 season pass cost
177
- - Determine: Savings amount and percentage
178
-
179
- Parameters:
180
- - Number of visits: 4 (June, July, August, September)
181
- - Daily ticket cost: $60 (to be verified)
182
- - Season pass cost: $120 (to be verified)
183
- - Output format: Clear numerical breakdown with explanation"""
184
- }
185
- }
186
-
187
- for relation_id, interaction_data in realistic_interactions.items():
188
- print(f"βœ… Created realistic interaction for {relation_id}")
189
- print(f" Length: {len(interaction_data['interaction_prompt'])} characters")
190
- print(f" Features: Context, reasoning framework, explicit instructions")
191
- print()
192
-
193
- return realistic_interactions
194
-
195
- def create_enhanced_knowledge_graph():
196
- """Create an enhanced knowledge graph with realistic agent reasoning."""
197
-
198
- print("πŸ”§ CREATING ENHANCED KNOWLEDGE GRAPH")
199
- print("=" * 70)
200
-
201
- # Load the original knowledge graph
202
- kg_path = '/Users/zekunwu/Desktop/agent_monitoring/huggingface/AgentGraph/backend/database/samples/knowledge_graphs/kg_algorithm_sample_0.json'
203
-
204
- with open(kg_path, 'r') as f:
205
- original_kg = json.load(f)
206
-
207
- # Create enhanced version
208
- enhanced_kg = copy.deepcopy(original_kg)
209
-
210
- # Get realistic agent prompts and interactions
211
- realistic_agents = create_realistic_agent_prompts()
212
- realistic_interactions = create_realistic_interaction_prompts()
213
-
214
- # Update agent entities with realistic prompts
215
- for entity in enhanced_kg['graph_data']['entities']:
216
- if entity['type'] == 'Agent' and entity['id'] in realistic_agents:
217
- agent_data = realistic_agents[entity['id']]
218
- entity['raw_prompt'] = agent_data['raw_prompt']
219
- entity['name'] = agent_data['name']
220
- print(f"πŸ“ Enhanced agent {entity['id']} with realistic reasoning framework")
221
-
222
- # Update relations with realistic interaction prompts
223
- for relation in enhanced_kg['graph_data']['relations']:
224
- if relation['id'] in realistic_interactions:
225
- relation['interaction_prompt'] = realistic_interactions[relation['id']]['interaction_prompt']
226
- print(f"πŸ’¬ Enhanced relation {relation['id']} with realistic interaction content")
227
-
228
- # Add enhancement metadata
229
- enhanced_kg['realistic_enhancement_info'] = {
230
- 'enhanced_at': '2025-01-27',
231
- 'enhancement_type': 'realistic_agent_reasoning',
232
- 'features_added': [
233
- 'explicit_reasoning_frameworks',
234
- 'step_by_step_thinking_instructions',
235
- 'tool_selection_reasoning',
236
- 'constraint_awareness_prompts',
237
- 'self_correction_mechanisms',
238
- 'contextual_interaction_content'
239
- ],
240
- 'reasoning_pattern_source': 'real_agent_trace_analysis',
241
- 'total_reasoning_instructions': len(realistic_agents) + len(realistic_interactions)
242
- }
243
-
244
- return enhanced_kg
245
-
246
- def test_realistic_reconstruction(enhanced_kg):
247
- """Test the realistic prompt reconstruction."""
248
-
249
- print(f"\n🎯 TESTING REALISTIC PROMPT RECONSTRUCTION")
250
- print("=" * 70)
251
-
252
- try:
253
- reconstructor = PromptReconstructor(enhanced_kg['graph_data'])
254
-
255
- # Test the key PERFORMS relation with realistic reasoning
256
- relation_id = 'relation_002' # agent_002 β†’ task_001 (PERFORMS)
257
- result = reconstructor.reconstruct_relation_prompt(relation_id)
258
-
259
- if 'error' in result:
260
- print(f"❌ Reconstruction failed: {result['error']}")
261
- return
262
-
263
- reconstructed_prompt = result['reconstructed_prompt']
264
-
265
- print(f"πŸ“Š Realistic Reconstruction Results:")
266
- print(f" Length: {len(reconstructed_prompt)} characters")
267
- print(f" Contains reasoning framework: {'reasoning process should be explicit' in reconstructed_prompt}")
268
- print(f" Contains step-by-step instructions: {'step planning' in reconstructed_prompt.lower()}")
269
- print(f" Contains tool selection guidance: {'tool selection' in reconstructed_prompt.lower()}")
270
- print(f" Contains self-correction prompts: {'self correction' in reconstructed_prompt.lower()}")
271
-
272
- print(f"\nπŸ“ Realistic Prompt Preview:")
273
- print("─" * 50)
274
- lines = reconstructed_prompt.split('\n')
275
- for i, line in enumerate(lines[:20], 1): # First 20 lines
276
- print(f"{i:2}: {line}")
277
- if len(lines) > 20:
278
- print(f" ... and {len(lines)-20} more lines")
279
-
280
- print(f"\n🧠 Reasoning Content Analysis:")
281
- reasoning_indicators = {
282
- 'task_analysis': 'task analysis' in reconstructed_prompt.lower(),
283
- 'step_planning': 'step planning' in reconstructed_prompt.lower(),
284
- 'tool_selection': 'tool selection' in reconstructed_prompt.lower(),
285
- 'constraint_handling': 'constraint handling' in reconstructed_prompt.lower(),
286
- 'self_correction': 'self correction' in reconstructed_prompt.lower(),
287
- 'explicit_reasoning': 'show your reasoning' in reconstructed_prompt.lower()
288
- }
289
-
290
- for indicator, present in reasoning_indicators.items():
291
- status = "βœ…" if present else "❌"
292
- print(f" {status} {indicator.replace('_', ' ').title()}: {'Present' if present else 'Missing'}")
293
-
294
- return reconstructed_prompt
295
-
296
- except Exception as e:
297
- print(f"πŸ’₯ Realistic reconstruction failed: {str(e)}")
298
- return None
299
-
300
- def save_realistic_enhanced_sample(enhanced_kg):
301
- """Save the realistic enhanced sample."""
302
-
303
- print(f"\nπŸ’Ύ SAVING REALISTIC ENHANCED SAMPLE")
304
- print("=" * 70)
305
-
306
- # Save realistic enhanced knowledge graph
307
- realistic_kg_path = '/Users/zekunwu/Desktop/agent_monitoring/huggingface/AgentGraph/backend/database/samples/knowledge_graphs/kg_algorithm_sample_0_realistic.json'
308
-
309
- with open(realistic_kg_path, 'w') as f:
310
- json.dump(enhanced_kg, f, indent=2, ensure_ascii=False)
311
-
312
- print(f"βœ… Saved realistic enhanced knowledge graph to:")
313
- print(f" {realistic_kg_path}")
314
-
315
- # Test the realistic reconstruction
316
- realistic_prompt = test_realistic_reconstruction(enhanced_kg)
317
-
318
- if realistic_prompt:
319
- # Save reconstruction example
320
- reconstruction_example_path = '/Users/zekunwu/Desktop/agent_monitoring/huggingface/AgentGraph/backend/database/samples/reconstructions/realistic_prompt_example.txt'
321
-
322
- os.makedirs(os.path.dirname(reconstruction_example_path), exist_ok=True)
323
-
324
- with open(reconstruction_example_path, 'w') as f:
325
- f.write("# Realistic Agent Prompt Reconstruction Example\n")
326
- f.write("# Based on Real Agent Trace Reasoning Patterns\n\n")
327
- f.write(realistic_prompt)
328
-
329
- print(f"βœ… Saved realistic prompt example to:")
330
- print(f" {reconstruction_example_path}")
331
-
332
- def main():
333
- """Main function to create realistic prompt reconstruction."""
334
-
335
- print("πŸš€ CREATING REALISTIC PROMPT RECONSTRUCTION")
336
- print("Based on Real Agent Trace Reasoning Patterns")
337
- print("=" * 70)
338
-
339
- # Create enhanced knowledge graph with realistic reasoning
340
- enhanced_kg = create_enhanced_knowledge_graph()
341
-
342
- # Save the realistic enhanced sample
343
- save_realistic_enhanced_sample(enhanced_kg)
344
-
345
- print(f"\nπŸŽ‰ REALISTIC ENHANCEMENT COMPLETE")
346
- print("=" * 70)
347
- print("βœ… Key improvements based on real agent traces:")
348
- print(" β€’ Explicit reasoning frameworks for each agent")
349
- print(" β€’ Step-by-step thinking instructions")
350
- print(" β€’ Tool selection reasoning guidance")
351
- print(" β€’ Constraint awareness and adaptation prompts")
352
- print(" β€’ Self-correction mechanisms")
353
- print(" β€’ Contextual interaction content with reasoning")
354
- print()
355
- print("🎯 This enables:")
356
- print(" β€’ More realistic perturbation testing")
357
- print(" β€’ Better simulation of actual agent cognition")
358
- print(" β€’ Improved causal analysis of reasoning patterns")
359
- print(" β€’ Production-quality agent interaction modeling")
360
-
361
- if __name__ == "__main__":
362
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
validate_enhanced_reconstruction.py DELETED
@@ -1,94 +0,0 @@
1
- #!/usr/bin/env python3
2
- """
3
- Validate Enhanced Prompt Reconstruction
4
-
5
- Quick validation script to ensure the enhanced prompt reconstruction
6
- maintains the same input/output interface while improving internal logic.
7
- """
8
-
9
- import json
10
- import sys
11
- import os
12
- sys.path.append('/Users/zekunwu/Desktop/agent_monitoring/huggingface/AgentGraph')
13
-
14
- from agentgraph.reconstruction import PromptReconstructor
15
-
16
- def validate_reconstruction():
17
- """Validate that the enhanced reconstruction works correctly."""
18
-
19
- print("πŸ” VALIDATING ENHANCED PROMPT RECONSTRUCTION")
20
- print("=" * 70)
21
-
22
- # Load the algorithm sample 0 knowledge graph
23
- kg_path = '/Users/zekunwu/Desktop/agent_monitoring/huggingface/AgentGraph/backend/database/samples/knowledge_graphs/kg_algorithm_sample_0.json'
24
-
25
- try:
26
- with open(kg_path, 'r') as f:
27
- kg_data = json.load(f)
28
-
29
- kg = kg_data['graph_data']
30
- print(f"βœ… Loaded knowledge graph with {len(kg['entities'])} entities and {len(kg['relations'])} relations")
31
-
32
- # Initialize reconstructor
33
- reconstructor = PromptReconstructor(kg)
34
- print("βœ… PromptReconstructor initialized successfully")
35
-
36
- # Test reconstruction of all relations
37
- reconstructed_relations = reconstructor.reconstruct_relations()
38
- print(f"βœ… Successfully reconstructed {len(reconstructed_relations)} relations")
39
-
40
- # Show sample reconstruction for PERFORMS relation
41
- performs_relations = [r for r in reconstructed_relations if r.get('type') == 'PERFORMS']
42
- if performs_relations:
43
- sample_relation = performs_relations[0]
44
- print(f"\nπŸ“‹ SAMPLE ENHANCED RECONSTRUCTION:")
45
- print(f"Relation: {sample_relation['source_entity']['name']} β†’ {sample_relation['target_entity']['name']}")
46
- print(f"Type: {sample_relation['type']}")
47
- print(f"Prompt length: {len(sample_relation.get('prompt', ''))} characters")
48
-
49
- # Show first 500 characters of the enhanced prompt
50
- prompt_preview = sample_relation.get('prompt', '')[:500]
51
- print(f"\nPrompt preview (first 500 chars):")
52
- print("-" * 50)
53
- print(prompt_preview)
54
- if len(sample_relation.get('prompt', '')) > 500:
55
- print("... [truncated]")
56
- print("-" * 50)
57
-
58
- # Verify enhanced features are present
59
- full_prompt = sample_relation.get('prompt', '')
60
- enhancements_found = []
61
-
62
- if "CRITICAL INSTRUCTIONS:" in full_prompt:
63
- enhancements_found.append("βœ… Critical instructions")
64
- if "step by step" in full_prompt.lower():
65
- enhancements_found.append("βœ… Step-by-step reasoning")
66
- if "Reasoning:" in full_prompt:
67
- enhancements_found.append("βœ… Enhanced response format")
68
- if "Task Analysis:" in full_prompt:
69
- enhancements_found.append("βœ… Task analysis structure")
70
- if "systematic approach" in full_prompt.lower():
71
- enhancements_found.append("βœ… Systematic approach emphasis")
72
-
73
- print(f"\n🎯 ENHANCED FEATURES DETECTED:")
74
- for enhancement in enhancements_found:
75
- print(f" {enhancement}")
76
-
77
- if len(enhancements_found) >= 4:
78
- print(f"\nπŸŽ‰ SUCCESS: Enhanced reconstruction is working correctly!")
79
- print(f" Found {len(enhancements_found)}/5 expected enhancements")
80
- else:
81
- print(f"\n⚠️ WARNING: Only found {len(enhancements_found)}/5 expected enhancements")
82
-
83
- print(f"\nβœ… VALIDATION COMPLETE: Enhanced prompt reconstruction is functional")
84
- return True
85
-
86
- except Exception as e:
87
- print(f"❌ VALIDATION FAILED: {str(e)}")
88
- import traceback
89
- traceback.print_exc()
90
- return False
91
-
92
- if __name__ == "__main__":
93
- success = validate_reconstruction()
94
- sys.exit(0 if success else 1)