From 089656195daac68d62ad96da02ac4d45344cfc2a Mon Sep 17 00:00:00 2001 From: alex-clawd Date: Wed, 13 May 2026 00:12:18 -0700 Subject: [PATCH] =?UTF-8?q?fix:=20address=20remaining=20review=20comments?= =?UTF-8?q?=20=E2=80=94=20broken=20import,=20race=20condition,=20duplicate?= =?UTF-8?q?=20logic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 4.6 --- lib/cli/src/crewai_cli/agent_tui.py | 80 +++++++++++------------------ lib/cli/src/crewai_cli/benchmark.py | 16 ++---- lib/cli/src/crewai_cli/cli.py | 4 +- 3 files changed, 37 insertions(+), 63 deletions(-) diff --git a/lib/cli/src/crewai_cli/agent_tui.py b/lib/cli/src/crewai_cli/agent_tui.py index 905680d54..c4a4ebdbb 100644 --- a/lib/cli/src/crewai_cli/agent_tui.py +++ b/lib/cli/src/crewai_cli/agent_tui.py @@ -1035,6 +1035,34 @@ class AgentTUI(App[None]): return self._get_or_create_agent(self._agent_names[0]) return None + def _format_memory_record(self, i: int, mem: object) -> list[str]: + """Format a single memory record into Rich markup lines.""" + record = getattr(mem, "record", mem) + content = getattr(record, "content", "") or str(mem) + if len(content) > 150: + content = content[:150] + "..." + + meta = getattr(record, "metadata", {}) or {} + mem_type = meta.get("type", "raw") + importance = getattr(record, "importance", "") or meta.get("importance", "") + scope = getattr(record, "scope", "") or meta.get("scope", "") + timestamp = getattr(record, "created_at", "") + + type_tag = ( + f"[bold cyan]{mem_type}[/]" + if mem_type == "canonical" + else f"[dim]{mem_type}[/]" + ) + importance_tag = f" [yellow]★{importance}[/]" if importance else "" + scope_tag = f" [{_DIM}]scope:{scope}[/]" if scope else "" + time_tag = f" [{_DIM}]{timestamp}[/]" if timestamp else "" + + return [ + f" {i}. {type_tag}{importance_tag}{scope_tag}{time_tag}", + f" {content}", + "", + ] + def _show_memory_panel(self) -> None: """Show recent memories for the focused agent (GAP-92: rich formatting).""" agent = self._get_focused_agent() @@ -1055,31 +1083,7 @@ class AgentTUI(App[None]): lines = [f"[bold]Memory Inspector — {agent_name}[/]\n"] for i, mem in enumerate(memories, 1): - record = getattr(mem, "record", mem) - content = getattr(record, "content", "") or str(mem) - if len(content) > 150: - content = content[:150] + "..." - - meta = getattr(record, "metadata", {}) or {} - mem_type = meta.get("type", "raw") - importance = getattr(record, "importance", "") or meta.get("importance", "") - scope = getattr(record, "scope", "") or meta.get("scope", "") - timestamp = getattr(record, "created_at", "") - - type_tag = ( - f"[bold cyan]{mem_type}[/]" - if mem_type == "canonical" - else f"[dim]{mem_type}[/]" - ) - importance_tag = f" [yellow]★{importance}[/]" if importance else "" - scope_tag = f" [{_DIM}]scope:{scope}[/]" if scope else "" - time_tag = f" [{_DIM}]{timestamp}[/]" if timestamp else "" - - lines.append( - f" {i}. {type_tag}{importance_tag}{scope_tag}{time_tag}" - ) - lines.append(f" {content}") - lines.append("") + lines.extend(self._format_memory_record(i, mem)) lines.append(f"[{_DIM}]Use /memory search to filter[/]") self._mount_sys("\n".join(lines)) @@ -1106,31 +1110,7 @@ class AgentTUI(App[None]): lines = [f"[bold]Memories matching '{query}' — {agent_name}[/]\n"] for i, mem in enumerate(results, 1): - record = getattr(mem, "record", mem) - content = getattr(record, "content", "") or str(mem) - if len(content) > 150: - content = content[:150] + "..." - - meta = getattr(record, "metadata", {}) or {} - mem_type = meta.get("type", "raw") - importance = getattr(record, "importance", "") or meta.get("importance", "") - scope = getattr(record, "scope", "") or meta.get("scope", "") - timestamp = getattr(record, "created_at", "") - - type_tag = ( - f"[bold cyan]{mem_type}[/]" - if mem_type == "canonical" - else f"[dim]{mem_type}[/]" - ) - importance_tag = f" [yellow]★{importance}[/]" if importance else "" - scope_tag = f" [{_DIM}]scope:{scope}[/]" if scope else "" - time_tag = f" [{_DIM}]{timestamp}[/]" if timestamp else "" - - lines.append( - f" {i}. {type_tag}{importance_tag}{scope_tag}{time_tag}" - ) - lines.append(f" {content}") - lines.append("") + lines.extend(self._format_memory_record(i, mem)) lines.append(f"[{_DIM}]Use /memory search to refine[/]") self._mount_sys("\n".join(lines)) diff --git a/lib/cli/src/crewai_cli/benchmark.py b/lib/cli/src/crewai_cli/benchmark.py index d88b34d0f..fc3d9ebc8 100644 --- a/lib/cli/src/crewai_cli/benchmark.py +++ b/lib/cli/src/crewai_cli/benchmark.py @@ -197,10 +197,8 @@ async def _run_model_benchmark( emit({"type": "model_start", "model": model, "total_cases": total}) sem = asyncio.Semaphore(_MAX_CASES_PARALLEL) - done_count = 0 async def _run_case(i: int, case: BenchmarkCase) -> BenchmarkResult: - nonlocal done_count async with sem: emit({"type": "case_start", "model": model, "case_index": i, "total_cases": total, "input": case.input}) @@ -220,8 +218,7 @@ async def _run_model_benchmark( try: agent = _load_agent(bench_defn, agents_dir=agents_dir) except Exception as e: - done_count += 1 - emit({"type": "case_done", "model": model, "case_index": done_count, "total_cases": total, "passed": False, "score": 0.0, "time_ms": 0, "error": str(e)}) + emit({"type": "case_done", "model": model, "case_index": i, "total_cases": total, "passed": False, "score": 0.0, "time_ms": 0, "error": str(e)}) return BenchmarkResult( case_index=i, input=case.input, expected=case.expected, actual=f"[Agent creation error: {e}]", model=model, passed=False, score=0.0, @@ -240,8 +237,7 @@ async def _run_model_benchmark( cost = response.cost except asyncio.TimeoutError: elapsed_ms = _current_time_ms() - start_ms - done_count += 1 - emit({"type": "case_done", "model": model, "case_index": done_count, "total_cases": total, "passed": False, "score": 0.0, "time_ms": elapsed_ms, "error": "timeout"}) + emit({"type": "case_done", "model": model, "case_index": i, "total_cases": total, "passed": False, "score": 0.0, "time_ms": elapsed_ms, "error": "timeout"}) return BenchmarkResult( case_index=i, input=case.input, expected=case.expected, actual=f"[Timeout after {_CASE_TIMEOUT_SECONDS}s]", model=model, passed=False, score=0.0, @@ -249,8 +245,7 @@ async def _run_model_benchmark( ) except Exception as e: elapsed_ms = _current_time_ms() - start_ms - done_count += 1 - emit({"type": "case_done", "model": model, "case_index": done_count, "total_cases": total, "passed": False, "score": 0.0, "time_ms": elapsed_ms, "error": str(e)}) + emit({"type": "case_done", "model": model, "case_index": i, "total_cases": total, "passed": False, "score": 0.0, "time_ms": elapsed_ms, "error": str(e)}) return BenchmarkResult( case_index=i, input=case.input, expected=case.expected, actual=f"[Error: {e}]", model=model, passed=False, score=0.0, @@ -261,7 +256,7 @@ async def _run_model_benchmark( if case.expected is not None: passed, score = _check_expected(case.expected, actual) if case.criteria is not None: - emit({"type": "judging", "model": model, "case_index": done_count + 1, "total_cases": total}) + emit({"type": "judging", "model": model, "case_index": i, "total_cases": total}) try: criteria_passed, criteria_score = await asyncio.wait_for( _judge_with_llm(case.criteria, case.input, actual, judge_model), @@ -275,8 +270,7 @@ async def _run_model_benchmark( else: passed, score = criteria_passed, criteria_score - done_count += 1 - emit({"type": "case_done", "model": model, "case_index": done_count, "total_cases": total, "passed": passed, "score": score, "time_ms": elapsed_ms}) + emit({"type": "case_done", "model": model, "case_index": i, "total_cases": total, "passed": passed, "score": score, "time_ms": elapsed_ms}) return BenchmarkResult( case_index=i, input=case.input, expected=case.expected, actual=actual, model=model, passed=passed, score=score, diff --git a/lib/cli/src/crewai_cli/cli.py b/lib/cli/src/crewai_cli/cli.py index 1a675e66a..d317b2909 100644 --- a/lib/cli/src/crewai_cli/cli.py +++ b/lib/cli/src/crewai_cli/cli.py @@ -229,8 +229,8 @@ def _train_new_agents(agent_files: list, n_iterations: int) -> None: click.secho(f"Training {agent_name} ({len(cases)} cases, {n_iterations} iterations)", fg="cyan", bold=True) try: - from crewai.new_agent.definition_parser import load_agent_definition - agent = load_agent_definition(str(agent_path)) + from crewai.new_agent.definition_parser import load_agent_from_definition + agent = load_agent_from_definition(str(agent_path)) except Exception as e: click.secho(f" Error loading agent {agent_name}: {e}", fg="red") continue