11use anyhow:: { Context , Result } ;
2+ use std:: collections:: HashMap ;
23use std:: path:: Path ;
34use std:: sync:: Arc ;
45use std:: time:: Duration ;
@@ -122,6 +123,7 @@ impl Executor {
122123 batch : Arc < Batch > ,
123124 archive : ExtractedArchive ,
124125 concurrent_limit : usize ,
126+ agent_env : HashMap < String , String > ,
125127 ) {
126128 let config = self . config . clone ( ) ;
127129 let sessions = self . sessions . clone ( ) ;
@@ -131,7 +133,7 @@ impl Executor {
131133 let start = std:: time:: Instant :: now ( ) ;
132134 metrics. start_batch ( ) ;
133135
134- let result = run_batch ( & config, & batch, archive, concurrent_limit) . await ;
136+ let result = run_batch ( & config, & batch, archive, concurrent_limit, agent_env ) . await ;
135137 let duration_ms = start. elapsed ( ) . as_millis ( ) as u64 ;
136138
137139 let mut res = batch. result . lock ( ) . await ;
@@ -176,10 +178,12 @@ async fn run_batch(
176178 batch : & Batch ,
177179 archive : ExtractedArchive ,
178180 concurrent_limit : usize ,
181+ agent_env : HashMap < String , String > ,
179182) -> Result < BatchResult > {
180183 let total_tasks = archive. tasks . len ( ) ;
181184 let agent_code = Arc :: new ( archive. agent_code ) ;
182185 let agent_language = Arc :: new ( archive. agent_language ) ;
186+ let agent_env = Arc :: new ( agent_env) ;
183187
184188 {
185189 let mut res = batch. result . lock ( ) . await ;
@@ -210,6 +214,7 @@ async fn run_batch(
210214 let events_tx = batch. events_tx . clone ( ) ;
211215 let agent_code = agent_code. clone ( ) ;
212216 let agent_language = agent_language. clone ( ) ;
217+ let agent_env = agent_env. clone ( ) ;
213218 let semaphore = semaphore. clone ( ) ;
214219 let task_results = task_results. clone ( ) ;
215220 let cancel_rx = batch. cancel . subscribe ( ) ;
@@ -236,7 +241,7 @@ async fn run_batch(
236241 } ) ;
237242
238243 let result =
239- run_single_task ( & config, & task, & agent_code, & agent_language, cancel_rx) . await ;
244+ run_single_task ( & config, & task, & agent_code, & agent_language, & agent_env , cancel_rx) . await ;
240245
241246 let _ = events_tx. send ( crate :: session:: WsEvent {
242247 event : "task_complete" . to_string ( ) ,
@@ -291,6 +296,7 @@ async fn run_single_task(
291296 task : & SweForgeTask ,
292297 agent_code : & str ,
293298 agent_language : & str ,
299+ agent_env : & HashMap < String , String > ,
294300 cancel_rx : tokio:: sync:: watch:: Receiver < bool > ,
295301) -> TaskResult {
296302 let start = std:: time:: Instant :: now ( ) ;
@@ -308,6 +314,7 @@ async fn run_single_task(
308314 task,
309315 agent_code,
310316 agent_language,
317+ agent_env,
311318 & work_dir,
312319 & cancel_rx,
313320 )
@@ -336,6 +343,7 @@ async fn run_task_pipeline(
336343 task : & SweForgeTask ,
337344 agent_code : & str ,
338345 agent_language : & str ,
346+ agent_env : & HashMap < String , String > ,
339347 work_dir : & Path ,
340348 cancel_rx : & tokio:: sync:: watch:: Receiver < bool > ,
341349) -> Result < TaskResult > {
@@ -399,6 +407,7 @@ async fn run_task_pipeline(
399407 & task. prompt ,
400408 & repo_dir,
401409 config. agent_timeout_secs ,
410+ agent_env,
402411 )
403412 . await ?;
404413
@@ -535,6 +544,7 @@ async fn run_agent(
535544 prompt : & str ,
536545 repo_dir : & Path ,
537546 timeout_secs : u64 ,
547+ agent_env : & HashMap < String , String > ,
538548) -> Result < String > {
539549 let ext = agent_extension ( agent_language) ;
540550 let script_name = format ! ( "_agent_code{}" , ext) ;
@@ -544,15 +554,23 @@ async fn run_agent(
544554 let prompt_path = repo_dir. join ( "_task_prompt.md" ) ;
545555 tokio:: fs:: write ( & prompt_path, prompt) . await ?;
546556
547- let argv_owned = agent_runner ( agent_language, & script_name) ;
557+ let mut argv_owned = agent_runner ( agent_language, & script_name) ;
558+ // Pass --instruction for Python agents (baseagent convention)
559+ if matches ! ( agent_language. to_lowercase( ) . as_str( ) , "python" | "py" ) {
560+ argv_owned. push ( "--instruction" . into ( ) ) ;
561+ argv_owned. push ( prompt. into ( ) ) ;
562+ }
548563 let argv: Vec < & str > = argv_owned. iter ( ) . map ( |s| s. as_str ( ) ) . collect ( ) ;
549- info ! ( "Running agent: {:?}" , argv) ;
564+ info ! ( "Running agent: {:?} with {} env vars " , argv, agent_env . len ( ) ) ;
550565
551- let env_vars = [
552- ( "TASK_PROMPT" , prompt_path. to_string_lossy ( ) . to_string ( ) ) ,
553- ( "REPO_DIR" , repo_dir. to_string_lossy ( ) . to_string ( ) ) ,
566+ let mut all_env : Vec < ( String , String ) > = vec ! [
567+ ( "TASK_PROMPT" . into ( ) , prompt_path. to_string_lossy( ) . to_string( ) ) ,
568+ ( "REPO_DIR" . into ( ) , repo_dir. to_string_lossy( ) . to_string( ) ) ,
554569 ] ;
555- let env_refs: Vec < ( & str , & str ) > = env_vars. iter ( ) . map ( |( k, v) | ( * k, v. as_str ( ) ) ) . collect ( ) ;
570+ for ( k, v) in agent_env {
571+ all_env. push ( ( k. clone ( ) , v. clone ( ) ) ) ;
572+ }
573+ let env_refs: Vec < ( & str , & str ) > = all_env. iter ( ) . map ( |( k, v) | ( k. as_str ( ) , v. as_str ( ) ) ) . collect ( ) ;
556574
557575 let ( stdout, stderr, exit) = run_cmd (
558576 & argv,
0 commit comments