Wire OptimizationLoop to project metrics and add metrics optimize.
Add from_metrics_store factory, OptimizerStore persistence, metrics optimize CLI, consolidate duplicate optimization agent, and add integration tests.
This commit is contained in:
@@ -205,4 +205,43 @@ class MetricsStore:
|
||||
return removed
|
||||
|
||||
def _has_idempotency_key(self, key: str) -> bool:
|
||||
return any(r.get("idempotency_key") == key for r in self.read_executions())
|
||||
return any(r.get("idempotency_key") == key for r in self.read_executions())
|
||||
|
||||
|
||||
@dataclass
|
||||
class OptimizerStore:
|
||||
"""Persist optimizer analysis output under .kaizen/metrics/optimizer/."""
|
||||
|
||||
project_root: Path
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
self.project_root = Path(self.project_root).resolve()
|
||||
self.optimizer_dir = self.project_root / ".kaizen" / "metrics" / "optimizer"
|
||||
self.analysis_path = self.optimizer_dir / "analysis.json"
|
||||
self.recommendations_path = self.optimizer_dir / "recommendations.jsonl"
|
||||
|
||||
def write_analysis(self, report: Dict[str, Any]) -> Path:
|
||||
self.optimizer_dir.mkdir(parents=True, exist_ok=True)
|
||||
self.analysis_path.write_text(
|
||||
json.dumps(report, indent=2, sort_keys=True) + "\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
return self.analysis_path
|
||||
|
||||
def append_recommendations(
|
||||
self,
|
||||
agent_name: str,
|
||||
recommendations: List[Dict[str, Any]],
|
||||
*,
|
||||
metrics_count: int,
|
||||
) -> None:
|
||||
self.optimizer_dir.mkdir(parents=True, exist_ok=True)
|
||||
entry = {
|
||||
"timestamp": _utc_now_iso(),
|
||||
"agent": agent_name,
|
||||
"metrics_count": metrics_count,
|
||||
"recommendations": recommendations,
|
||||
}
|
||||
with self.recommendations_path.open("a", encoding="utf-8") as handle:
|
||||
handle.write(json.dumps(entry, sort_keys=True))
|
||||
handle.write("\n")
|
||||
Reference in New Issue
Block a user