1010
1111from .archive import ArchiveManager
1212from .config import AgentConfig , INPUT_MARKER
13+ from .debug import DebugLogger
1314from .llm import create_llm
1415from .mcp_client import connect_mcp , find_mcp_server
1516from .prompts import (
2223from .skills import load_skills
2324from .skills .generator import GeneratorSkill
2425
26+ DEFAULT_PROMPT = """Create six_hump_camel APOSMM scripts:
27+ - Executable: six_hump_camel/six_hump_camel.x
28+ - Input: six_hump_camel/input.txt
29+ - Template vars: X0, X1
30+ - 4 workers, 100 sims.
31+ - The output file for each simulation is output.txt
32+ - The bounds should be 0,1 and -1,2 for X0 and X1 respectively"""
33+
2534
2635async def run_agent (config : AgentConfig ):
2736 """Main entry point: build skills, connect MCP, run the agent loop."""
@@ -32,6 +41,12 @@ async def run_agent(config: AgentConfig):
3241 # Set up archive manager
3342 archive = ArchiveManager (config .output_dir )
3443
44+ # Debug logger
45+ debug = None
46+ if config .debug :
47+ log_path = Path (config .output_dir ) / "debug_log.txt"
48+ debug = DebugLogger (log_path , model = config .model )
49+
3550 # Load skills — drop generator when using existing scripts
3651 skill_names = config .skills
3752 if config .scripts_dir and "generator" in skill_names :
@@ -69,16 +84,20 @@ async def run_agent(config: AgentConfig):
6984 system_prompt = build_system_prompt (skills , has_generator )
7085 messages = [("system" , system_prompt )]
7186
87+ if debug :
88+ debug .log_system_prompt (system_prompt )
89+ debug .log_tool_schemas (tools )
90+
7291 if config .show_prompts :
7392 print (f"System prompt:\n { system_prompt } \n " )
7493
7594 # Determine initial message
7695 initial_msg = _build_initial_message (config , archive )
7796
7897 if not config .interactive :
79- await _run_autonomous (agent , messages , initial_msg , config )
98+ await _run_autonomous (agent , messages , initial_msg , config , debug )
8099 else :
81- await _run_interactive (agent , messages , initial_msg , config , has_generator )
100+ await _run_interactive (agent , messages , initial_msg , config , has_generator , debug )
82101
83102
84103def _build_initial_message (config , archive ):
@@ -105,20 +124,12 @@ def _build_initial_message(config, archive):
105124 user_input = input ().strip ()
106125 if user_input :
107126 return user_input
108- print ("Using default demo prompt" )
109127
110- return (
111- "Create six_hump_camel APOSMM scripts:\n "
112- "- Executable: six_hump_camel/six_hump_camel.x\n "
113- "- Input: six_hump_camel/input.txt\n "
114- "- Template vars: X0, X1\n "
115- "- 4 workers, 100 sims.\n "
116- "- The output file for each simulation is output.txt\n "
117- "- The bounds should be 0,1 and -1,2 for X0 and X1 respectively"
118- )
128+ print ("Using demo prompt" )
129+ return DEFAULT_PROMPT
119130
120131
121- async def _run_autonomous (agent , messages , initial_msg , config ):
132+ async def _run_autonomous (agent , messages , initial_msg , config , debug ):
122133 """Single invocation — agent generates/loads, runs, fixes, reports."""
123134 goal = AUTONOMOUS_GOAL .format (initial_msg = initial_msg )
124135 messages .append (("user" , goal ))
@@ -128,6 +139,8 @@ async def _run_autonomous(agent, messages, initial_msg, config):
128139 print ("Starting agent...\n " )
129140
130141 result = await agent .ainvoke ({"messages" : messages })
142+ if debug :
143+ debug .dump_messages (result ["messages" ], "Autonomous run complete" )
131144 print (f"\n { '=' * 60 } " )
132145 print ("Agent completed" )
133146 print (f"{ '=' * 60 } " )
@@ -137,19 +150,23 @@ async def _run_autonomous(agent, messages, initial_msg, config):
137150 print (content )
138151
139152
140- async def _run_interactive (agent , messages , initial_msg , config , has_generator ):
153+ async def _run_interactive (agent , messages , initial_msg , config , has_generator , debug ):
141154 """Chat loop — agent responds, waits for user input, repeats."""
142155 if has_generator :
143156 goal = INTERACTIVE_GOAL .format (initial_msg = initial_msg )
144157 else :
145158 goal = initial_msg
146159 messages .append (("user" , goal ))
147160 print ("Starting agent...\n " )
161+ turn = 0
148162
149163 while True :
150164 try :
151165 result = await agent .ainvoke ({"messages" : messages })
152166 messages = result ["messages" ]
167+ turn += 1
168+ if debug :
169+ debug .dump_messages (messages , f"Interactive turn { turn } " )
153170 response = messages [- 1 ].content
154171 if isinstance (response , list ):
155172 response = "" .join (block .get ("text" , "" ) for block in response )
0 commit comments