diff --git a/backend/app/services/knowledge_base_service.py b/backend/app/services/knowledge_base_service.py index a782895..b8a8770 100644 --- a/backend/app/services/knowledge_base_service.py +++ b/backend/app/services/knowledge_base_service.py @@ -259,8 +259,8 @@ class KnowledgeBaseService: ) await session.commit() - # Invalidate reference docs cache - self.reference_docs.invalidate_cache(agent_key) + # Update reference docs cache with new spec content + self.reference_docs.invalidate_cache(agent_key, new_spec_content=spec_content) logger.info(f"[KB_SERVICE] Pipeline complete for {agent_key}, spec version {spec.version_number}") except Exception as e: diff --git a/backend/app/services/reference_docs.py b/backend/app/services/reference_docs.py index b565ed8..138a1d7 100755 --- a/backend/app/services/reference_docs.py +++ b/backend/app/services/reference_docs.py @@ -47,8 +47,14 @@ class ReferenceDocsService: self._db_specs[key] = spec.content print(f" Loaded DB spec for {key}: {len(spec.content)} chars (v{spec.version_number})") - def invalidate_cache(self, agent_key: str | None = None) -> None: - """Clear cached specs. If agent_key is None, clear all.""" + def invalidate_cache(self, agent_key: str | None = None, new_spec_content: str | None = None) -> None: + """Clear cached specs and optionally replace with new content. + + Args: + agent_key: The agent key to invalidate (or None for all). + new_spec_content: If provided, immediately populate the DB cache + with this content so the next analysis uses it without a restart. + """ if agent_key is None: self._db_specs.clear() self._barclaycard_brand_spec = None @@ -57,8 +63,11 @@ class ReferenceDocsService: self._channel_tech_specs_spec = None self._legal_spec = None else: - self._db_specs.pop(agent_key, None) - # Also clear the file-based cache so next call re-checks DB + if new_spec_content is not None: + self._db_specs[agent_key] = new_spec_content + else: + self._db_specs.pop(agent_key, None) + # Also clear the file-based cache so it won't be stale cache_map = { "legal": "_legal_spec", "brand_barclays": "_barclays_brand_spec",