@@ -45,7 +45,8 @@ impl CreateAgentHandler {
4545 . collect ( )
4646 }
4747
48- /// Get the agents directory based on location.
48+ /// Get the agents directory based on location (legacy, uses process cwd).
49+ #[ allow( dead_code) ]
4950 fn get_agents_dir ( location : & str ) -> Result < PathBuf > {
5051 match location {
5152 "project" => Ok ( PathBuf :: from ( ".cortex/agents" ) ) ,
@@ -61,6 +62,23 @@ impl CreateAgentHandler {
6162 ) ) ) ,
6263 }
6364 }
65+
66+ /// Get the agents directory based on location, using the provided cwd for project location.
67+ fn get_agents_dir_with_cwd ( location : & str , cwd : & PathBuf ) -> Result < PathBuf > {
68+ match location {
69+ "project" => Ok ( cwd. join ( ".cortex/agents" ) ) ,
70+ "personal" => {
71+ let home = dirs:: home_dir ( ) . ok_or_else ( || {
72+ CortexError :: Internal ( "Could not determine home directory" . to_string ( ) )
73+ } ) ?;
74+ Ok ( home. join ( ".cortex/agents" ) )
75+ }
76+ _ => Err ( CortexError :: InvalidInput ( format ! (
77+ "Invalid location: {}. Must be 'project' or 'personal'" ,
78+ location
79+ ) ) ) ,
80+ }
81+ }
6482}
6583
6684impl Default for CreateAgentHandler {
@@ -75,7 +93,7 @@ impl ToolHandler for CreateAgentHandler {
7593 "CreateAgent"
7694 }
7795
78- async fn execute ( & self , arguments : Value , _context : & ToolContext ) -> Result < ToolResult > {
96+ async fn execute ( & self , arguments : Value , context : & ToolContext ) -> Result < ToolResult > {
7997 let description = arguments
8098 . get ( "description" )
8199 . and_then ( |v| v. as_str ( ) )
@@ -108,7 +126,8 @@ impl ToolHandler for CreateAgentHandler {
108126 ) ) ;
109127 }
110128
111- let agents_dir = Self :: get_agents_dir ( location) ?;
129+ // Use context.cwd for project location to avoid relying on process cwd
130+ let agents_dir = Self :: get_agents_dir_with_cwd ( location, & context. cwd ) ?;
112131 let agent_path = agents_dir. join ( format ! ( "{}.toml" , agent_name) ) ;
113132
114133 if agent_path. exists ( ) {
@@ -162,17 +181,13 @@ description = """
162181mod tests {
163182 use super :: * ;
164183 use serde_json:: json;
165- use std:: path:: PathBuf ;
166184 use tempfile:: TempDir ;
167185
168186 #[ tokio:: test]
169187 async fn test_create_agent_project_location ( ) {
170188 let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
171189 let context = ToolContext :: new ( temp_dir. path ( ) . to_path_buf ( ) ) ;
172190
173- let original_dir = std:: env:: current_dir ( ) . unwrap ( ) ;
174- std:: env:: set_current_dir ( temp_dir. path ( ) ) . unwrap ( ) ;
175-
176191 let handler = CreateAgentHandler :: new ( ) ;
177192 let description = "Process and transform data files" ;
178193 let args = json ! ( {
@@ -182,8 +197,6 @@ mod tests {
182197
183198 let result = handler. execute ( args, & context) . await ;
184199
185- std:: env:: set_current_dir ( & original_dir) . unwrap ( ) ;
186-
187200 assert ! ( result. is_ok( ) , "Handler execution failed: {:?}" , result) ;
188201
189202 let tool_result = result. unwrap ( ) ;
@@ -215,9 +228,6 @@ mod tests {
215228 let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
216229 let context = ToolContext :: new ( temp_dir. path ( ) . to_path_buf ( ) ) ;
217230
218- let home_dir = temp_dir. path ( ) . join ( "home" ) ;
219- std:: fs:: create_dir_all ( & home_dir) . unwrap ( ) ;
220-
221231 let handler = CreateAgentHandler :: new ( ) ;
222232 let args = json ! ( {
223233 "description" : "Custom data processor for personal projects" ,
@@ -226,7 +236,10 @@ mod tests {
226236
227237 let result = handler. execute ( args, & context) . await ;
228238
239+ // Personal location uses the actual home directory, so this test
240+ // just verifies it doesn't fail unexpectedly
229241 if result. is_err ( ) {
242+ // Expected if home dir doesn't exist or is not writable
230243 assert ! ( true ) ;
231244 } else {
232245 let tool_result = result. unwrap ( ) ;
@@ -299,9 +312,6 @@ mod tests {
299312 let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
300313 let context = ToolContext :: new ( temp_dir. path ( ) . to_path_buf ( ) ) ;
301314
302- let original_dir = std:: env:: current_dir ( ) . unwrap ( ) ;
303- std:: env:: set_current_dir ( temp_dir. path ( ) ) . unwrap ( ) ;
304-
305315 let handler = CreateAgentHandler :: new ( ) ;
306316 let description = "Agent with default location parameter" ;
307317 let args = json ! ( {
@@ -310,8 +320,6 @@ mod tests {
310320
311321 let result = handler. execute ( args, & context) . await ;
312322
313- std:: env:: set_current_dir ( & original_dir) . unwrap ( ) ;
314-
315323 if let Err ( ref e) = result {
316324 eprintln ! ( "Error: {:?}" , e) ;
317325 }
@@ -336,19 +344,15 @@ mod tests {
336344 let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
337345 let context = ToolContext :: new ( temp_dir. path ( ) . to_path_buf ( ) ) ;
338346
339- let original_dir = std:: env:: current_dir ( ) . unwrap ( ) ;
340- std:: env:: set_current_dir ( temp_dir. path ( ) ) . unwrap ( ) ;
341-
342347 let handler = CreateAgentHandler :: new ( ) ;
343348 let args = json ! ( {
344349 "description" : "Duplicate agent test case" ,
345350 "location" : "project"
346351 } ) ;
347352
353+ // Create the first agent
348354 let result1 = handler. execute ( args. clone ( ) , & context) . await ;
349355
350- std:: env:: set_current_dir ( & original_dir) . unwrap ( ) ;
351-
352356 assert ! ( result1. is_ok( ) , "First agent creation should succeed" ) ;
353357
354358 let agent_path = temp_dir
@@ -360,9 +364,8 @@ mod tests {
360364 agent_path
361365 ) ;
362366
363- std :: env :: set_current_dir ( temp_dir . path ( ) ) . unwrap ( ) ;
367+ // Try to create a duplicate
364368 let result2 = handler. execute ( args, & context) . await ;
365- std:: env:: set_current_dir ( & original_dir) . unwrap ( ) ;
366369
367370 assert ! ( result2. is_err( ) , "Second agent creation should fail" ) ;
368371
0 commit comments