feat: Implement waiting timeout handling in BaseAgent and AgentState

This commit is contained in:
Ahmed Allam
2025-10-20 21:13:27 -07:00
committed by Ahmed Allam
parent c78f7d37de
commit 49df6ef8e0
4 changed files with 43 additions and 7 deletions
+20
View File
@@ -206,6 +206,26 @@ class BaseAgent(metaclass=AgentMeta):
async def _wait_for_input(self) -> None:
import asyncio
if self.state.has_waiting_timeout():
self.state.resume_from_waiting()
self.state.add_message("assistant", "Waiting timeout reached. Resuming execution.")
from strix.cli.tracer import get_global_tracer
tracer = get_global_tracer()
if tracer:
tracer.update_agent_status(self.state.agent_id, "running")
try:
from strix.tools.agents_graph.agents_graph_actions import _agent_graph
if self.state.agent_id in _agent_graph["nodes"]:
_agent_graph["nodes"][self.state.agent_id]["status"] = "running"
except (ImportError, KeyError):
pass
return
await asyncio.sleep(0.5)
async def _enter_waiting_state(
+18 -1
View File
@@ -24,6 +24,7 @@ class AgentState(BaseModel):
stop_requested: bool = False
waiting_for_input: bool = False
llm_failed: bool = False
waiting_start_time: datetime | None = None
final_result: dict[str, Any] | None = None
messages: list[dict[str, Any]] = Field(default_factory=list)
@@ -88,12 +89,13 @@ class AgentState(BaseModel):
def enter_waiting_state(self, llm_failed: bool = False) -> None:
self.waiting_for_input = True
self.stop_requested = False
self.waiting_start_time = datetime.now(UTC)
self.llm_failed = llm_failed
self.last_updated = datetime.now(UTC).isoformat()
def resume_from_waiting(self, new_task: str | None = None) -> None:
self.waiting_for_input = False
self.waiting_start_time = None
self.stop_requested = False
self.completed = False
self.llm_failed = False
@@ -104,6 +106,21 @@ class AgentState(BaseModel):
def has_reached_max_iterations(self) -> bool:
return self.iteration >= self.max_iterations
def has_waiting_timeout(self) -> bool:
if not self.waiting_for_input or not self.waiting_start_time:
return False
if (
self.stop_requested
or self.llm_failed
or self.completed
or self.has_reached_max_iterations()
):
return False
elapsed = (datetime.now(UTC) - self.waiting_start_time).total_seconds()
return elapsed > 120
def has_empty_last_messages(self, count: int = 3) -> bool:
if len(self.messages) < count:
return False