Skip to content

Commit c66c110

Browse files
authored
refactor: ModelService start logic (alibaba#182)
1 parent b3adc69 commit c66c110

4 files changed

Lines changed: 173 additions & 173 deletions

File tree

rock/sdk/sandbox/agent/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
from abc import ABC, abstractmethod
22

33
from rock.actions.sandbox.base import AbstractSandbox
4+
from rock.sdk.sandbox.model_service.base import ModelService
45

56

67
class Agent(ABC):
78
def __init__(self, sandbox: AbstractSandbox):
89
self._sandbox = sandbox
10+
self.model_service: ModelService | None = None
911

1012
@abstractmethod
1113
async def init(self):

rock/sdk/sandbox/agent/iflow_cli.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -469,11 +469,6 @@ async def run(
469469
logger.debug(f"[{sandbox_id}] Project path: {project_path}, Problem statement: {problem_statement[:100]}...")
470470

471471
try:
472-
# Start ModelService if configured
473-
if self.model_service and not self.model_service.is_started:
474-
logger.info(f"[{sandbox_id}] Starting ModelService")
475-
await self.model_service.start()
476-
477472
# Step 1: Change to project directory
478473
logger.info(f"[{sandbox_id}] Changing working directory to: {project_path}")
479474
result = await self._sandbox.arun(
@@ -558,13 +553,6 @@ async def run(
558553
exc_info=True,
559554
)
560555
raise
561-
finally:
562-
# Clean up ModelService if started
563-
if self.model_service and self.model_service.is_started:
564-
try:
565-
await self.model_service.stop()
566-
except Exception as e:
567-
logger.warning(f"[{sandbox_id}] Failed to stop ModelService: {str(e)}")
568556

569557
async def _agent_run(
570558
self,
@@ -644,3 +632,9 @@ async def _agent_run(
644632
except Exception as e:
645633
error_msg = f"Failed to execute nohup command '{cmd}': {str(e)}"
646634
return Observation(output=error_msg, exit_code=1, failure_reason=error_msg)
635+
636+
async def start_model_service(self):
637+
if not self.model_service:
638+
raise RuntimeError(f"ModelService is not initialized in {self.config.agent_type}!")
639+
640+
await self.model_service.start()

rock/sdk/sandbox/agent/swe_agent.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -464,11 +464,6 @@ async def run(
464464
logger.info(f"[{sandbox_id}] SWE-agent execution started")
465465

466466
try:
467-
# Start ModelService if configured
468-
if self.model_service:
469-
logger.info(f"[{sandbox_id}] Starting ModelService")
470-
await self.model_service.start()
471-
472467
with self._config_template_context(problem_statement, project_path, instance_id) as generated_config_path:
473468
config_filename = Path(generated_config_path).name
474469

@@ -534,14 +529,6 @@ async def run(
534529
exc_info=True,
535530
)
536531
raise
537-
finally:
538-
# Clean up ModelService if started
539-
if self.model_service and self.model_service.is_started:
540-
try:
541-
logger.info(f"[{sandbox_id}] Stopping ModelService")
542-
await self.model_service.stop()
543-
except Exception as e:
544-
logger.warning(f"[{sandbox_id}] Failed to stop ModelService: {str(e)}")
545532

546533
async def _agent_run(
547534
self,
@@ -621,3 +608,9 @@ async def _agent_run(
621608
except Exception as e:
622609
error_msg = f"Failed to execute nohup command '{cmd}': {str(e)}"
623610
return Observation(output=error_msg, exit_code=1, failure_reason=error_msg)
611+
612+
async def start_model_service(self):
613+
if not self.model_service:
614+
raise RuntimeError(f"ModelService is not initialized in {self.config.agent_type}!")
615+
616+
await self.model_service.start()

0 commit comments

Comments
 (0)