This repository was archived by the owner on Feb 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
172 lines (134 loc) · 5.79 KB
/
main.py
File metadata and controls
172 lines (134 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""
SNRE Main Application Entry Point
"""
import os
import sys
from typing import Optional
import yaml
from agents.loop_simplifier import LoopSimplifier
from agents.pattern_optimizer import PatternOptimizer
from agents.security_enforcer import SecurityEnforcer
from core.change_tracker import ChangeTracker
from core.consensus_engine import ConsensusEngine
from core.evolution_recorder import EvolutionRecorder
from core.swarm_coordinator import SwarmCoordinator
from interface.api import APIInterface
from interface.cli import CLIInterface
from interface.integration_hook import IntegrationHook
from snre.models.config import Config
class SNREApplication:
"""Main SNRE application orchestrator"""
def __init__(self, config_path: Optional[str] = None):
self.config = Config()
if config_path:
self.load_config(config_path)
# Initialize core components
self.coordinator = SwarmCoordinator(self.config)
self.consensus_engine = ConsensusEngine(self.config)
self.change_tracker = ChangeTracker(self.config)
self.evolution_recorder = EvolutionRecorder(self.config)
# Initialize interfaces
self.cli_interface = CLIInterface(self.coordinator, self.config)
self.api_interface = APIInterface(self.coordinator, self.config)
self.integration_hook = IntegrationHook(self.coordinator, self.config)
# Initialize and register agents
self._setup_agents()
def initialize(self) -> None:
"""Initialize all system components"""
print("Initializing SNRE...")
# Create necessary directories
os.makedirs("data/refactor_logs", exist_ok=True)
os.makedirs("data/snapshots", exist_ok=True)
# Load agent configurations
self._load_agent_profiles()
print(f"SNRE initialized with {len(self.coordinator.agents)} agents")
def load_config(self, config_path: str) -> None:
"""Load configuration from file"""
try:
with open(config_path) as f:
if config_path.endswith(".yaml") or config_path.endswith(".yml"):
config_data = yaml.safe_load(f)
else:
import json
config_data = json.load(f)
# only update recognized config fields
from dataclasses import fields as dc_fields
valid_fields = {f.name for f in dc_fields(self.config)}
for key, value in config_data.items():
if key in valid_fields:
setattr(self.config, key, value)
except Exception as e:
print(f"Warning: Could not load config from {config_path}: {str(e)}")
def start_cli(self, args: list[str] = None) -> None:
"""Start CLI interface with optional args"""
self.cli_interface.run(args)
def start_api_server(self, host: str = "localhost", port: int = 8000) -> None:
"""Start REST API server"""
print(f"Starting SNRE API server on {host}:{port}")
self.api_interface.run(host, port)
def shutdown(self) -> None:
"""Clean shutdown of all components"""
print("Shutting down SNRE...")
# Clean up old snapshots
self.evolution_recorder.cleanup_old_snapshots()
print("SNRE shutdown complete")
def _setup_agents(self) -> None:
"""Initialize and register default agents"""
agents = [
PatternOptimizer("pattern_optimizer", self.config),
SecurityEnforcer("security_enforcer", self.config),
LoopSimplifier("loop_simplifier", self.config),
]
for agent in agents:
self.coordinator.register_agent(agent)
def _load_agent_profiles(self) -> None:
"""Load agent profiles from configuration"""
profiles_path = "config/agent_profiles.yaml"
if os.path.exists(profiles_path):
try:
with open(profiles_path) as f:
profiles = yaml.safe_load(f)
# Update agent configurations based on profiles
for agent_id, agent in self.coordinator.agents.items():
if agent_id in profiles.get("agents", {}):
profile = profiles["agents"][agent_id]
agent._priority = profile.get("priority", agent._priority)
agent._confidence_threshold = profile.get(
"confidence_threshold", agent._confidence_threshold
)
except Exception as e:
print(f"Warning: Could not load agent profiles: {str(e)}")
def main():
"""Main entry point for CLI commands"""
# Check if this is the old-style mode call (cli/api) or direct command
if len(sys.argv) >= 2 and sys.argv[1] in ["cli", "api"]:
# Old-style mode-based calling
mode = sys.argv[1]
# Initialize application
app = SNREApplication()
app.initialize()
try:
if mode == "cli":
# Pass remaining args to CLI
app.start_cli(sys.argv[2:]) # Skip 'main.py' and 'cli'
elif mode == "api":
host = sys.argv[2] if len(sys.argv) > 2 else "localhost"
port = int(sys.argv[3]) if len(sys.argv) > 3 else 8000
app.start_api_server(host, port)
except KeyboardInterrupt:
print("\nShutdown requested...")
finally:
app.shutdown()
else:
# Direct command calling (new style) - pass all args to CLI
app = SNREApplication()
app.initialize()
try:
# Pass all args except the script name to CLI
app.start_cli(sys.argv[1:])
except KeyboardInterrupt:
print("\nShutdown requested...")
finally:
app.shutdown()
if __name__ == "__main__":
main()