From a78899245992aebf1a358bf5c5059fc619d375b1 Mon Sep 17 00:00:00 2001 From: Nubenetes Bot Date: Wed, 13 May 2026 09:37:06 +0200 Subject: [PATCH] fix: correctly accumulate historical metrics from branch and clean Gemini failures from report --- src/gitops_manager.py | 8 ++++++++ src/main.py | 29 +++++++++++++++++++++++------ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/gitops_manager.py b/src/gitops_manager.py index 10726502..35a37b0b 100644 --- a/src/gitops_manager.py +++ b/src/gitops_manager.py @@ -1,4 +1,5 @@ import json +import base64 from github import Github from datetime import datetime @@ -12,6 +13,13 @@ class RepositoryController: base_branch = self.repository.get_branch(self.default_branch_name) self.repository.create_git_ref(ref=f"refs/heads/{branch_name}", sha=base_branch.commit.sha) + def get_file_from_branch(self, file_path: str, branch_name: str) -> str: + try: + file_meta = self.repository.get_contents(file_path, ref=branch_name) + return base64.b64decode(file_meta.content).decode("utf-8") + except: + return "" + def apply_historical_chunk(self, updates: dict, next_since: str) -> None: branch_name = "bot/historical-accumulator" diff --git a/src/main.py b/src/main.py index 4798ec6b..f74f9afa 100644 --- a/src/main.py +++ b/src/main.py @@ -130,12 +130,29 @@ async def master_orchestrator(): # 5. Gestión de Métricas Acumulativas (Para reporte final) metrics_file = "src/memory/historical_metrics.json" accumulated_report = full_extraction_report - if is_historical and os.path.exists(metrics_file): - try: - with open(metrics_file, 'r') as f: - prev_metrics = json.load(f) - accumulated_report.extend(prev_metrics.get("full_report", [])) - except: pass + + # Intentar cargar métricas previas (Local o desde la rama accumulator) + prev_metrics = None + if is_historical: + if os.path.exists(metrics_file): + try: + with open(metrics_file, 'r') as f: prev_metrics = json.load(f) + except: pass + else: + # Intentar desde la rama + acc_content = git_controller.get_file_from_branch(metrics_file, "bot/historical-accumulator") + if acc_content: + try: prev_metrics = json.loads(acc_content) + except: pass + + if prev_metrics: + # Filtrar fallos críticos de Gemini del histórico para no "ensuciar" el reporte final + # y permitir que se re-intenten si el tramo se solapa. + clean_prev_report = [ + item for item in prev_metrics.get("full_report", []) + if "Fallo crítico Gemini" not in item.get("reason", "") + ] + accumulated_report.extend(clean_prev_report) metrics = { "total_extracted": len(accumulated_report),