Update app.py
Browse files
app.py
CHANGED
|
@@ -1853,7 +1853,7 @@ def launch_ui(bootstrap_instance: "Bootstrap"):
|
|
| 1853 |
if not res.get("ok"): return f"Test failed: {res.get('reason','unknown')}"
|
| 1854 |
if _last["obj"].kind=="code" and role!="owner" and not CFG["OPT_AUTO_APPLY"]: return "Awaiting Owner approval for code changes." # type: ignore
|
| 1855 |
ok,msg=hive_instance.changes.apply(res); return msg if ok else f"Apply failed: {msg}" # type: ignore
|
| 1856 |
-
demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", "7860")), share=False)
|
| 1857 |
|
| 1858 |
class Bootstrap:
|
| 1859 |
"""Handles the entire application startup sequence cleanly."""
|
|
@@ -1929,6 +1929,10 @@ class Bootstrap:
|
|
| 1929 |
"""Performs a hot-reload of the application logic without restarting the process."""
|
| 1930 |
print("[Bootstrap] Performing soft restart (hot-reload)...")
|
| 1931 |
self.hive_ready.clear()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1932 |
|
| 1933 |
# Reload the app module to get the new code
|
| 1934 |
import app
|
|
@@ -1982,12 +1986,12 @@ class Bootstrap:
|
|
| 1982 |
print("\nExiting Hive CLI.")
|
| 1983 |
pass
|
| 1984 |
|
| 1985 |
-
def graceful_shutdown(self, signum, frame):
|
| 1986 |
"""Handles SIGINT/SIGTERM for clean shutdown."""
|
| 1987 |
print("\n[Bootstrap] Graceful shutdown requested...")
|
| 1988 |
-
if self.hive_instance:
|
| 1989 |
-
|
| 1990 |
-
|
| 1991 |
print("[Bootstrap] Exiting.")
|
| 1992 |
sys.exit(0)
|
| 1993 |
|
|
@@ -1995,86 +1999,4 @@ class Bootstrap:
|
|
| 1995 |
if __name__=="__main__":
|
| 1996 |
bootstrap = Bootstrap(CFG)
|
| 1997 |
bootstrap.run()
|
| 1998 |
-
|
| 1999 |
-
class Bootstrap:
|
| 2000 |
-
"""Handles the entire application startup sequence cleanly."""
|
| 2001 |
-
def __init__(self, config: Dict):
|
| 2002 |
-
self.config = config
|
| 2003 |
-
self.caps: Optional[Dict] = None
|
| 2004 |
-
self.hive_instance: Optional[Hive] = None
|
| 2005 |
-
self.hive_lite_instance: Optional[Hive] = None
|
| 2006 |
-
self.hive_ready = threading.Event()
|
| 2007 |
-
|
| 2008 |
-
def run(self):
|
| 2009 |
-
"""Executes the full startup sequence."""
|
| 2010 |
-
print("[Bootstrap] Starting Hive System...")
|
| 2011 |
-
self.caps = probe_caps()
|
| 2012 |
-
print(f"[Bootstrap] System capabilities: {self.caps}")
|
| 2013 |
-
|
| 2014 |
-
# Create a 'lite' instance immediately for basic chat
|
| 2015 |
-
print("[Bootstrap] Initializing Lite Hive core...")
|
| 2016 |
-
self.hive_lite_instance = Hive(lite=True)
|
| 2017 |
-
print("[Bootstrap] Lite Hive core is ready.")
|
| 2018 |
-
|
| 2019 |
-
# Launch UI immediately, it will wait for the hive_ready event
|
| 2020 |
-
ui_thread = threading.Thread(target=self.launch, daemon=True)
|
| 2021 |
-
ui_thread.start()
|
| 2022 |
-
|
| 2023 |
-
print("[Bootstrap] Initializing Hive core in background...")
|
| 2024 |
-
# Now initialize the full instance. This is the slow part.
|
| 2025 |
-
self.hive_instance = Hive(lite=False)
|
| 2026 |
-
|
| 2027 |
-
self.hive_ready.set() # Signal that the Hive instance is ready
|
| 2028 |
-
print("[Bootstrap] Hive core is ready.")
|
| 2029 |
-
|
| 2030 |
-
self.setup_memory()
|
| 2031 |
-
ui_thread.join() # Keep main thread alive
|
| 2032 |
-
|
| 2033 |
-
def setup_memory(self):
|
| 2034 |
-
"""Handles memory restoration and staged ingestion."""
|
| 2035 |
-
def _memory_task():
|
| 2036 |
-
print("[Bootstrap] Starting background memory setup...")
|
| 2037 |
-
try:
|
| 2038 |
-
ok_restored, restore_msg = restore_curves_if_missing(str(self.config["CURVE_DIR"]))
|
| 2039 |
-
with open(os.path.join(self.config["STATE_DIR"], "restore_status.log"), "a", encoding="utf-8") as f:
|
| 2040 |
-
f.write(json.dumps({"ok":bool(ok_restored),"msg":restore_msg,"ts":time.time()})+"\n")
|
| 2041 |
-
if ok_restored:
|
| 2042 |
-
print(f"[Bootstrap] Memory restore status: {restore_msg}")
|
| 2043 |
-
else:
|
| 2044 |
-
print("[Bootstrap] No memory restored, proceeding to staged ingestion in background...")
|
| 2045 |
-
staged_ingest_chain_if_enabled(str(self.config["CURVE_DIR"]))
|
| 2046 |
-
except Exception as e:
|
| 2047 |
-
with open(os.path.join(self.config["STATE_DIR"], "restore_error.log"), "a", encoding="utf-8") as f:
|
| 2048 |
-
f.write(f"restore/ingest: {e}\n")
|
| 2049 |
-
# Run the memory setup in a background thread to not block the UI
|
| 2050 |
-
threading.Thread(target=_memory_task, daemon=True).start()
|
| 2051 |
-
|
| 2052 |
-
def launch(self):
|
| 2053 |
-
"""Launches the appropriate interface (UI or CLI)."""
|
| 2054 |
-
if self.config["LAUNCH_UI"]:
|
| 2055 |
-
print("[Bootstrap] Launching Web UI...")
|
| 2056 |
-
launch_ui(self)
|
| 2057 |
-
else:
|
| 2058 |
-
print("[Bootstrap] Launching CLI...")
|
| 2059 |
-
self.run_cli_loop()
|
| 2060 |
-
|
| 2061 |
-
def run_cli_loop(self):
|
| 2062 |
-
"""Runs a command-line interface loop for Hive. Waits for full init."""
|
| 2063 |
-
self.hive_ready.wait()
|
| 2064 |
-
print("Hive is ready. Type a message and press Enter (Ctrl+C to exit).")
|
| 2065 |
-
try:
|
| 2066 |
-
while True:
|
| 2067 |
-
s = input("> ").strip()
|
| 2068 |
-
if not s: continue
|
| 2069 |
-
reply = self.hive_instance.chat(s, effective_role="user", caller_id="cli") # type: ignore
|
| 2070 |
-
print(reply)
|
| 2071 |
-
except (KeyboardInterrupt, EOFError):
|
| 2072 |
-
print("\nExiting Hive CLI.")
|
| 2073 |
-
pass
|
| 2074 |
-
|
| 2075 |
-
# ----------- entry -----------
|
| 2076 |
-
if __name__=="__main__":
|
| 2077 |
-
|
| 2078 |
-
bootstrap = Bootstrap(CFG)
|
| 2079 |
-
bootstrap.run()
|
| 2080 |
|
|
|
|
| 1853 |
if not res.get("ok"): return f"Test failed: {res.get('reason','unknown')}"
|
| 1854 |
if _last["obj"].kind=="code" and role!="owner" and not CFG["OPT_AUTO_APPLY"]: return "Awaiting Owner approval for code changes." # type: ignore
|
| 1855 |
ok,msg=hive_instance.changes.apply(res); return msg if ok else f"Apply failed: {msg}" # type: ignore
|
| 1856 |
+
demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", "7860")), share=False, quiet=True)
|
| 1857 |
|
| 1858 |
class Bootstrap:
|
| 1859 |
"""Handles the entire application startup sequence cleanly."""
|
|
|
|
| 1929 |
"""Performs a hot-reload of the application logic without restarting the process."""
|
| 1930 |
print("[Bootstrap] Performing soft restart (hot-reload)...")
|
| 1931 |
self.hive_ready.clear()
|
| 1932 |
+
if self.hive_instance:
|
| 1933 |
+
self.hive_instance.module_manager.stop_all()
|
| 1934 |
+
if self.app and hasattr(self.app, 'close'):
|
| 1935 |
+
self.app.close()
|
| 1936 |
|
| 1937 |
# Reload the app module to get the new code
|
| 1938 |
import app
|
|
|
|
| 1986 |
print("\nExiting Hive CLI.")
|
| 1987 |
pass
|
| 1988 |
|
| 1989 |
+
def graceful_shutdown(self, signum=None, frame=None):
|
| 1990 |
"""Handles SIGINT/SIGTERM for clean shutdown."""
|
| 1991 |
print("\n[Bootstrap] Graceful shutdown requested...")
|
| 1992 |
+
if self.hive_instance and hasattr(self.hive_instance, 'module_manager'):
|
| 1993 |
+
self.hive_instance.module_manager.stop_all()
|
| 1994 |
+
gr.close_all()
|
| 1995 |
print("[Bootstrap] Exiting.")
|
| 1996 |
sys.exit(0)
|
| 1997 |
|
|
|
|
| 1999 |
if __name__=="__main__":
|
| 2000 |
bootstrap = Bootstrap(CFG)
|
| 2001 |
bootstrap.run()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2002 |
|