Buckets:

download
raw
24.3 kB
"""
/sync command implementation - Knowledge refresh workflow
"""
import asyncio
import logging
import time
from datetime import UTC, datetime, timedelta
from pathlib import Path
from typing import Any
from grid.knowledge import Entity, EntityId, PersistentJSONKnowledgeStore
from grid.knowledge.graph_schema import RelationType
from .base import CommandContext, CommandResult, KnowledgeCommand
logger = logging.getLogger(__name__)
class SyncCommand(KnowledgeCommand):
"""Execute knowledge synchronization and refresh"""
def __init__(self):
super().__init__()
self.name = "sync"
self.rag_system = None
self.knowledge_graph = None
async def execute(self, args: list[str], kwargs: dict[str, Any], context: CommandContext) -> CommandResult:
"""Execute knowledge synchronization"""
start_time = time.time()
try:
# Parse arguments
rag_only = "--rag-only" in args
graph_only = "--graph-only" in args
quick_mode = "--quick" in args
# Validate conflicting options
if rag_only and graph_only:
return CommandResult(
success=False,
message="Cannot use --rag-only and --graph-only together",
execution_time=time.time() - start_time,
)
# Initialize systems
await self._initialize_systems()
# Execute sync operations
results = {}
if not graph_only:
results["rag_update"] = await self._update_rag_index(quick_mode)
if not rag_only:
results["knowledge_graph_refresh"] = await self._refresh_knowledge_graph(quick_mode)
results["index_rebuild"] = await self._rebuild_indices(quick_mode)
if not quick_mode:
results["quality_check"] = await self._run_quality_checks()
# Generate summary
summary = self._generate_sync_summary(results)
# Create recommendations
recommendations = self._generate_recommendations(results)
execution_time = time.time() - start_time
return CommandResult(
success=summary["overall_success"],
message=summary["status"],
data={
"sync_results": results,
"summary": summary,
"rag_only": rag_only,
"graph_only": graph_only,
"quick_mode": quick_mode,
},
recommendations=recommendations,
execution_time=execution_time,
)
except Exception as e:
logger.error(f"Sync command failed: {e}")
return CommandResult(
success=False,
message=f"Knowledge sync failed: {str(e)}",
error_details=str(e),
execution_time=time.time() - start_time,
)
async def _initialize_systems(self):
"""Initialize RAG and knowledge graph systems"""
try:
# Initialize enhanced RAG
from tools.rag.enhanced.embeddings import EnhancedRAG, RetrievalConfig
config = RetrievalConfig()
self.rag_system = EnhancedRAG(config)
logger.info("Initialized enhanced RAG system")
except ImportError as e:
logger.warning(f"Could not initialize enhanced RAG: {e}")
# Fallback to basic RAG if available
try:
from tools.rag.vector_store import RAGSystem
self.rag_system = RAGSystem()
logger.info("Initialized basic RAG system")
except ImportError:
logger.error("No RAG system available")
self.rag_system = None
# Initialize knowledge graph (Persistent JSON store)
self.knowledge_graph = PersistentKnowledgeGraphWrapper()
self.knowledge_graph.connect()
logger.info("Initialized persistent knowledge graph")
async def _update_rag_index(self, quick_mode: bool = False) -> dict[str, Any]:
"""Update RAG vector database"""
try:
logger.info("Updating RAG index...")
# Find new/modified documents
docs_path = Path("docs")
new_documents = []
def _find_md_files(p: Path) -> list[Path]:
return list(p.rglob("*.md"))
if await asyncio.to_thread(docs_path.exists):
md_files = await asyncio.to_thread(_find_md_files, docs_path)
for doc_file in md_files:
if self._should_index_document(doc_file, quick_mode):
content = await asyncio.to_thread(doc_file.read_text, encoding="utf-8")
stat_result = await asyncio.to_thread(doc_file.stat)
metadata = {
"file_path": str(doc_file.relative_to(docs_path)),
"modified": stat_result.st_mtime,
"doc_id": doc_file.stem,
"file_size": len(content),
}
new_documents.append((content, metadata))
# Index documents
indexed_count = 0
chunks_created = 0
if self.rag_system and new_documents:
for content, metadata in new_documents:
try:
ids = self.rag_system.index_document(content, metadata)
indexed_count += 1
chunks_created += len(ids)
except Exception as e:
logger.error(f"Failed to index document {metadata['doc_id']}: {e}")
# Get RAG stats
rag_stats = {}
if self.rag_system:
rag_stats = self.rag_system.get_retrieval_stats()
return {
"success": True,
"documents_indexed": indexed_count,
"chunks_created": chunks_created,
"rag_stats": rag_stats,
"quick_mode": quick_mode,
"duration": time.time(),
}
except Exception as e:
return {"success": False, "error": str(e), "documents_indexed": 0, "duration": time.time()}
async def _refresh_knowledge_graph(self, quick_mode: bool = False) -> dict[str, Any]:
"""Refresh knowledge graph connections"""
try:
logger.info("Refreshing knowledge graph...")
graph_updates = {"new_nodes": [], "new_connections": [], "strengthened_edges": [], "removed_edges": []}
# Scan for new library files
src_path = Path("src/grid")
def _find_py_files(p: Path) -> list[Path]:
return list(p.rglob("*.py"))
if await asyncio.to_thread(src_path.exists):
py_files = await asyncio.to_thread(_find_py_files, src_path)
for lib_file in py_files:
if lib_file.name.endswith("_library.py"):
lib_name = lib_file.stem.replace("_library", "")
# Extract library information
capabilities = self._extract_library_capabilities(lib_file)
node_info = {
"name": lib_name,
"type": "library",
"file_path": str(lib_file.relative_to(src_path)),
"capabilities": capabilities,
"centrality_estimate": 0.5, # Initial estimate
"discovered_at": datetime.now(UTC).isoformat(),
}
graph_updates["new_nodes"].append(node_info)
# Analyze connections between nodes
if not quick_mode:
graph_updates["new_connections"] = self._analyze_connections(graph_updates["new_nodes"])
graph_updates["strengthened_edges"] = self._identify_weak_edges()
# Update knowledge graph
if self.knowledge_graph:
for node in graph_updates["new_nodes"]:
self.knowledge_graph.add_node(node)
for connection in graph_updates["new_connections"]:
self.knowledge_graph.add_connection(connection)
# Persist changes
self.knowledge_graph.disconnect()
return {
"success": True,
"nodes_discovered": len(graph_updates["new_nodes"]),
"connections_added": len(graph_updates["new_connections"]),
"edges_strengthened": len(graph_updates["strengthened_edges"]),
"graph_updates": graph_updates,
"quick_mode": quick_mode,
"duration": time.time(),
}
except Exception as e:
return {"success": False, "error": str(e), "nodes_discovered": 0, "duration": time.time()}
async def _rebuild_indices(self, quick_mode: bool = False) -> dict[str, Any]:
"""Rebuild search indices"""
try:
logger.info("Rebuilding search indices...")
# This would rebuild various search indices
# For now, we'll simulate the process
indices_rebuilt = []
if not quick_mode:
# Rebuild full-text search index
indices_rebuilt.append("full_text_search")
# Rebuild semantic search index
indices_rebuilt.append("semantic_search")
# Rebuild metadata index
indices_rebuilt.append("metadata_index")
else:
# Quick mode - only rebuild essential indices
indices_rebuilt.append("full_text_search")
return {
"success": True,
"indices_rebuilt": indices_rebuilt,
"quick_mode": quick_mode,
"duration": time.time(),
}
except Exception as e:
return {"success": False, "error": str(e), "indices_rebuilt": [], "duration": time.time()}
async def _run_quality_checks(self) -> dict[str, Any]:
"""Run quality checks on knowledge systems"""
try:
logger.info("Running quality checks...")
quality_results = {"rag_quality": {}, "graph_quality": {}, "index_quality": {}}
# RAG quality checks
if self.rag_system:
rag_stats = self.rag_system.get_retrieval_stats()
quality_results["rag_quality"] = {
"total_chunks": rag_stats.get("total_chunks", 0),
"avg_semantic_density": rag_stats.get("avg_semantic_density", 0.0),
"chunk_type_distribution": rag_stats.get("chunk_types", {}),
"quality_score": self._calculate_rag_quality_score(rag_stats),
}
# Graph quality checks
if self.knowledge_graph:
graph_stats = self.knowledge_graph.get_stats()
quality_results["graph_quality"] = {
"total_nodes": graph_stats.get("total_nodes", 0),
"total_edges": graph_stats.get("total_edges", 0),
"avg_connectivity": graph_stats.get("avg_connectivity", 0.0),
"quality_score": self._calculate_graph_quality_score(graph_stats),
}
# Overall quality score
rag_score = quality_results["rag_quality"].get("quality_score", 0.0)
graph_score = quality_results["graph_quality"].get("quality_score", 0.0)
overall_score = (rag_score + graph_score) / 2 if (rag_score > 0 or graph_score > 0) else 0.0
return {
"success": True,
"quality_results": quality_results,
"overall_quality_score": overall_score,
"quality_status": self._get_quality_status(overall_score),
"duration": time.time(),
}
except Exception as e:
return {"success": False, "error": str(e), "overall_quality_score": 0.0, "duration": time.time()}
def _should_index_document(self, file_path: Path, quick_mode: bool) -> bool:
"""Check if document should be indexed"""
if not file_path.exists() or not file_path.is_file():
return False
# In quick mode, only index recently modified files
if quick_mode:
modified_time = datetime.fromtimestamp(file_path.stat().st_mtime)
return modified_time > datetime.now(UTC) - timedelta(days=1)
# Full mode - index all markdown files
return file_path.suffix == ".md"
def _extract_library_capabilities(self, lib_file: Path) -> list[str]:
"""Extract capabilities from library file"""
try:
content = lib_file.read_text(encoding="utf-8")
# Look for capability indicators
capabilities = []
# Check for class definitions
classes = [line.strip() for line in content.split("\n") if line.strip().startswith("class ")]
for class_line in classes:
class_name = class_line.split("(")[0].replace("class ", "").strip()
capabilities.append(f"class:{class_name}")
# Check for function definitions
functions = [line.strip() for line in content.split("\n") if line.strip().startswith("def ")]
for func_line in functions:
func_name = func_line.split("(")[0].replace("def ", "").strip()
if not func_name.startswith("_"): # Skip private functions
capabilities.append(f"function:{func_name}")
# Look for common capability patterns
if "monitor" in content.lower():
capabilities.append("monitoring")
if "track" in content.lower():
capabilities.append("tracking")
if "analyze" in content.lower():
capabilities.append("analysis")
if "optimize" in content.lower():
capabilities.append("optimization")
return capabilities[:10] # Limit to 10 most relevant capabilities
except Exception as e:
logger.error(f"Failed to extract capabilities from {lib_file}: {e}")
return []
def _analyze_connections(self, nodes: list[dict]) -> list[dict]:
"""Analyze potential connections between nodes"""
connections = []
for i, node1 in enumerate(nodes):
for node2 in nodes[i + 1 :]:
# Check for capability overlap
caps1 = set(node1.get("capabilities", []))
caps2 = set(node2.get("capabilities", []))
overlap = caps1.intersection(caps2)
if overlap:
connection = {
"source": node1["name"],
"target": node2["name"],
"type": "capability_overlap",
"strength": len(overlap) / max(len(caps1), len(caps2)),
"shared_capabilities": list(overlap),
}
connections.append(connection)
return connections
def _identify_weak_edges(self) -> list[dict]:
"""Identify weak edges that need strengthening"""
# Mock implementation - would analyze actual graph
return [
{
"source": "strategic_reasoning_library",
"target": "eufle_engineering_library",
"current_strength": 0.4,
"recommended_strength": 0.7,
"reason": "Low information flow between reasoning and engineering",
}
]
def _calculate_rag_quality_score(self, rag_stats: dict) -> float:
"""Calculate RAG quality score"""
score = 0.0
# Base score for having chunks
total_chunks = rag_stats.get("total_chunks", 0)
if total_chunks > 0:
score += 0.3
# Semantic density bonus
avg_density = rag_stats.get("avg_semantic_density", 0.0)
score += avg_density * 0.3
# Chunk type diversity bonus
chunk_types = rag_stats.get("chunk_types", {})
if len(chunk_types) >= 3:
score += 0.2
elif len(chunk_types) >= 2:
score += 0.1
# Volume bonus (reasonable amount of content)
if total_chunks > 100:
score += 0.2
elif total_chunks > 50:
score += 0.1
return min(score, 1.0)
def _calculate_graph_quality_score(self, graph_stats: dict) -> float:
"""Calculate knowledge graph quality score"""
score = 0.0
# Base score for having nodes
total_nodes = graph_stats.get("total_nodes", 0)
if total_nodes > 0:
score += 0.3
# Connectivity score
total_edges = graph_stats.get("total_edges", 0)
if total_nodes > 0:
connectivity = total_edges / (total_nodes * (total_nodes - 1) / 2) # Density
score += min(connectivity * 2, 0.4) # Cap at 0.4
# Average connectivity bonus
avg_connectivity = graph_stats.get("avg_connectivity", 0.0)
score += avg_connectivity * 0.3
return min(score, 1.0)
def _get_quality_status(self, score: float) -> str:
"""Get quality status from score"""
if score >= 0.8:
return "excellent"
elif score >= 0.6:
return "good"
elif score >= 0.4:
return "fair"
else:
return "poor"
def _generate_sync_summary(self, results: dict[str, Any]) -> dict[str, Any]:
"""Generate sync operation summary"""
success_count = sum(
1 for result in results.values() if isinstance(result, dict) and result.get("success", False)
)
total_operations = len([r for r in results.values() if isinstance(r, dict)])
if success_count == total_operations:
status = "✅ All sync operations successful"
overall_success = True
elif success_count > total_operations // 2:
status = "⚠️ Partial success"
overall_success = True
else:
status = "❌ Multiple failures"
overall_success = False
# Extract key metrics
documents_indexed = results.get("rag_update", {}).get("documents_indexed", 0)
nodes_discovered = results.get("knowledge_graph_refresh", {}).get("nodes_discovered", 0)
quality_score = results.get("quality_check", {}).get("overall_quality_score", 0.0)
return {
"status": status,
"overall_success": overall_success,
"successful_operations": success_count,
"total_operations": total_operations,
"documents_indexed": documents_indexed,
"nodes_discovered": nodes_discovered,
"quality_score": quality_score,
"details": results,
}
def _generate_recommendations(self, results: dict[str, Any]) -> list[str]:
"""Generate recommendations based on sync results"""
recommendations = []
# RAG recommendations
rag_result = results.get("rag_update", {})
if rag_result.get("success", False):
docs_indexed = rag_result.get("documents_indexed", 0)
if docs_indexed == 0:
recommendations.append("No new documents found - consider running /sync without --quick")
elif docs_indexed < 5:
recommendations.append(f"Only {docs_indexed} documents indexed - check if documents need updating")
else:
recommendations.append("RAG update failed - check vector database configuration")
# Knowledge graph recommendations
graph_result = results.get("knowledge_graph_refresh", {})
if graph_result.get("success", False):
nodes_discovered = graph_result.get("nodes_discovered", 0)
if nodes_discovered > 0:
recommendations.append(f"Discovered {nodes_discovered} new library nodes - review connections")
else:
recommendations.append("Knowledge graph refresh failed - check graph configuration")
# Quality recommendations
quality_result = results.get("quality_check", {})
if quality_result.get("success", False):
quality_score = quality_result.get("overall_quality_score", 0.0)
if quality_score < 0.6:
recommendations.append("Quality score below 60% - consider content enrichment")
else:
recommendations.append("Quality checks failed - review system configuration")
if not recommendations:
recommendations.append("All systems synchronized successfully!")
return recommendations
def get_help(self) -> str:
"""Return help text for the /sync command"""
return """
/sync - Knowledge Refresh Workflow
USAGE:
/sync [OPTIONS]
OPTIONS:
--rag-only Only update RAG index
--graph-only Only refresh knowledge graph
--quick Skip quality checks and use recent documents only
DESCRIPTION:
Synchronizes and refreshes knowledge systems including RAG vector database,
knowledge graph connections, and search indices.
EXAMPLES:
/sync # Full synchronization
/sync --rag-only # RAG index only
/sync --graph-only # Knowledge graph only
/sync --quick # Quick sync with recent changes only
INTEGRATION:
- Updates rag-query skill for improved search
- Enhances cognitive-integration skill with new connections
- Provides data for ai_behavior.md rule adjustments
"""
def get_required_permissions(self) -> list[str]:
"""Return required permissions for sync command"""
return ["read_files", "write_files", "modify_knowledge"]
# Wrapper to bridge SyncCommand requirements with PersistentJSONKnowledgeStore
class PersistentKnowledgeGraphWrapper:
"""Wrapper for PersistentJSONKnowledgeStore to match SyncCommand interface"""
def __init__(self):
self.store = PersistentJSONKnowledgeStore()
def connect(self):
self.store.connect()
def disconnect(self):
self.store.disconnect()
def add_node(self, node_info: dict):
"""Add a library/capability node to the persistent graph"""
# Map node_info to Entity
entity = Entity(
id=node_info["name"],
entity_type="Skill", # Libraries are essentially Skill containers
properties={
"file_path": node_info.get("file_path"),
"capabilities": node_info.get("capabilities", []),
"centrality": node_info.get("centrality_estimate", 0.5),
"discovered_at": node_info.get("discovered_at"),
},
)
self.store.store_entity(entity)
def add_connection(self, connection: dict):
"""Add a connection to the persistent graph"""
# Map connection to Relationship
from_id = EntityId(connection["source"])
to_id = EntityId(connection["target"])
self.store.create_relationship(
from_id=from_id,
to_id=to_id,
relationship_type=RelationType.RELATED_TO,
properties={
"type": connection.get("type"),
"strength": connection.get("strength"),
"shared_capabilities": connection.get("shared_capabilities", []),
},
)
def get_stats(self) -> dict[str, Any]:
"""Convert store statistics to SyncCommand format"""
stats = self.store.get_graph_statistics()
return {
"total_nodes": stats["total_entities"],
"total_edges": stats["total_relationships"],
"avg_connectivity": stats["total_relationships"] / max(stats["total_entities"], 1),
}
# Command instance for registration
Command = SyncCommand

Xet Storage Details

Size:
24.3 kB
·
Xet hash:
24c610f81d93815e7d90ad0144ba5772025450669822212c5e9bb036b1f0f24d

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.