agentloop-profiler 0.2.2__py3-none-any.whl → 0.2.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
agentloop/cli.py CHANGED
@@ -2,6 +2,7 @@ from __future__ import annotations
2
2
 
3
3
  import json
4
4
  import os
5
+ import sys
5
6
  from pathlib import Path
6
7
 
7
8
  import typer
@@ -66,6 +67,32 @@ from agentloop.value import build_value_report
66
67
 
67
68
  app = typer.Typer(help="AgentLoop profiler CLI")
68
69
  console = Console()
70
+
71
+
72
+ def _utf8_output() -> None:
73
+ """Write UTF-8 when output is redirected on a legacy code page.
74
+
75
+ On Windows, piped or redirected output uses the ANSI code page (for example
76
+ cp1252). Task IDs, trace names and other user text outside that code page
77
+ then raise UnicodeEncodeError mid-command and hide the real result.
78
+ Interactive consoles already use UTF-8 and are left unchanged.
79
+ """
80
+ for stream in (sys.stdout, sys.stderr):
81
+ encoding = (getattr(stream, "encoding", None) or "").lower().replace("-", "")
82
+ if encoding in {"utf8", "utf_8"} or not hasattr(stream, "reconfigure"):
83
+ continue
84
+ try:
85
+ stream.reconfigure(encoding="utf-8", errors="replace")
86
+ except (OSError, ValueError):
87
+ continue
88
+
89
+
90
+ @app.callback()
91
+ def _main() -> None:
92
+ """AgentLoop profiler CLI"""
93
+ _utf8_output()
94
+
95
+
69
96
  study_app = typer.Typer(help="Summarize offline studies and preserved intervention evidence.")
70
97
  app.add_typer(study_app, name="study")
71
98
  app.add_typer(aggregate_app, name="aggregate")
agentloop/estimates.py CHANGED
@@ -8,6 +8,7 @@ from typing import Any
8
8
 
9
9
  from agentloop.costs import is_cost_evaluable
10
10
  from agentloop.markdown import markdown_code_span, markdown_text
11
+ from agentloop.parallelism import REUSE_FORMULA
11
12
 
12
13
 
13
14
  @dataclass(frozen=True)
@@ -92,8 +93,18 @@ def estimate_snapshot(candidate: Any, report: dict[str, Any], nodes: list[Any])
92
93
  affected = set(candidate.affected_nodes or [])
93
94
  durations = [node.duration_ms for node in nodes if node.node_id in affected]
94
95
  cost_status = report.get("cost_status", "complete")
96
+ estimator_id = candidate.rule_id
97
+ extra_inputs = {}
98
+ if candidate.rule_id == "parallelize_tools":
99
+ retained = (candidate.observations or {}).get("retained_span_id")
100
+ if retained is not None:
101
+ estimator_id = "request_local_tool_reuse"
102
+ spec = Estimator(REUSE_FORMULA, ())
103
+ extra_inputs["retained_duration_ms"] = next(
104
+ node.duration_ms for node in nodes if node.node_id == retained
105
+ )
95
106
  return {
96
- "estimator_id": candidate.rule_id,
107
+ "estimator_id": estimator_id,
97
108
  "estimator_version": spec.version,
98
109
  "method": "heuristic",
99
110
  "confidence": candidate.confidence,
@@ -110,6 +121,7 @@ def estimate_snapshot(candidate: Any, report: dict[str, Any], nodes: list[Any])
110
121
  "repeated_context_ratio": report.get("repeated_context_ratio", 0.0),
111
122
  "cost_status": cost_status,
112
123
  "token_status": report.get("token_status", "unspecified"),
124
+ **extra_inputs,
113
125
  },
114
126
  "assumptions": list(dict.fromkeys([*(candidate.assumptions or []), *spec.assumptions])),
115
127
  "unmodeled_metrics": (["latency"] if candidate.rule_id == "cache_context" else ["cost"]),
agentloop/findings.py CHANGED
@@ -256,6 +256,13 @@ def _finding_from_card(
256
256
  ]
257
257
  semantic = (card.get("estimate") or {}).get("estimator_id") == "semantic_leaf_removal"
258
258
  observations = dict(card.get("observations", {}))
259
+ identity_title = title
260
+ if (
261
+ finding_type == "parallelize_tools"
262
+ and observations.get("recommended_action") == "inspect_repetition"
263
+ ):
264
+ # The wording change preserves existing reviewed finding IDs.
265
+ identity_title = f"Parallelize repeated `{observations['tool_name']}` tool calls"
259
266
  if semantic:
260
267
  severity = "medium" if card.get("confidence") == "medium" else "low"
261
268
  evidence.extend(
@@ -265,7 +272,7 @@ def _finding_from_card(
265
272
  return OptimizationFinding(
266
273
  finding_id=_stable_finding_id(
267
274
  finding_type,
268
- title,
275
+ identity_title,
269
276
  affected_spans,
270
277
  context={
271
278
  "investigation_id": observations.get("investigation_id"),
agentloop/graph.py CHANGED
@@ -290,9 +290,10 @@ class ExecutionGraph:
290
290
  and len(identities) == len(self.nodes),
291
291
  }
292
292
 
293
- def parallelizable_groups(self) -> list[dict[str, Any]]:
293
+ def parallelizable_groups(self, *, consider_reuse: bool = False) -> list[dict[str, Any]]:
294
294
  return parallelization_candidates(
295
295
  self.nodes,
296
+ consider_reuse=consider_reuse,
296
297
  dependency_edges=(
297
298
  (edge.source, edge.target)
298
299
  for edge in self.edges