Fix lint errors (B023, W293, B007, PERF102)

Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
Devin AI
2026-01-03 17:31:49 +00:00
parent 5dc87c04af
commit 563e2eccbd

View File

@@ -957,15 +957,24 @@ class Crew(FlowTrackable, BaseModel):
) )
# Wrap task execution to capture tokens immediately after completion # Wrap task execution to capture tokens immediately after completion
async def _wrapped_task_execution(): # Use default arguments to bind loop variables at definition time (fixes B023)
result = await task.aexecute_sync( agent = exec_data.agent
agent=exec_data.agent, tools = exec_data.tools
context=context,
tools=exec_data.tools, async def _wrapped_task_execution(
_task=task,
_agent=agent,
_tools=tools,
_context=context,
):
result = await _task.aexecute_sync(
agent=_agent,
context=_context,
tools=_tools,
) )
# Capture tokens immediately after task completes # Capture tokens immediately after task completes
# This reduces (but doesn't eliminate) race conditions # This reduces (but doesn't eliminate) race conditions
tokens_after = self._get_agent_token_usage(exec_data.agent) tokens_after = self._get_agent_token_usage(_agent)
return result, tokens_after return result, tokens_after
async_task = asyncio.create_task(_wrapped_task_execution()) async_task = asyncio.create_task(_wrapped_task_execution())
@@ -1730,7 +1739,7 @@ class Crew(FlowTrackable, BaseModel):
if workflow_metrics.per_task: if workflow_metrics.per_task:
# Sum up tokens for each agent from their tasks # Sum up tokens for each agent from their tasks
# We need to find which agent_id corresponds to each task's agent_name # We need to find which agent_id corresponds to each task's agent_name
for task_name, task_metrics in workflow_metrics.per_task.items(): for task_metrics in workflow_metrics.per_task.values():
agent_name = task_metrics.agent_name agent_name = task_metrics.agent_name
# Find the agent_id for this agent_name from agent_info_map # Find the agent_id for this agent_name from agent_info_map
# For now, we'll use the agent_name as a temporary key but this needs improvement # For now, we'll use the agent_name as a temporary key but this needs improvement
@@ -2126,7 +2135,7 @@ To enable tracing, do any one of these:
if isinstance(agent.llm, BaseLLM): if isinstance(agent.llm, BaseLLM):
return agent.llm.get_token_usage_summary() return agent.llm.get_token_usage_summary()
elif hasattr(agent, "_token_process"): if hasattr(agent, "_token_process"):
return agent._token_process.get_summary() return agent._token_process.get_summary()
return UsageMetrics() return UsageMetrics()